:: commit fdc1332c34b667e7183d68d6670ca62bce257b26

mintsuki <mintsuki@protonmail.com> — 2023-06-15 00:34

parents: 9a80df3dd0

config: Add and document ARCH built-in macro

diff --git a/CONFIG.md b/CONFIG.md
index ec59d860..695825a7 100644
--- a/CONFIG.md
+++ b/CONFIG.md
@@ -193,3 +193,9 @@ CMDLINE=something before ${MY_MACRO} something after
 ```
 
 Macros must always be placed inside `${...}` where `...` is the arbitrary macro name.
+
+### Built-in macros
+
+Limine automatically defines these macros:
+
+* `ARCH` - This built-in macro expands to the architecture of the machine. Possible values are: `x86-64`, `ia-32`, `aarch64`. In the case of IA-32, BIOS or UEFI, the macro will always expand to `x86-64` if the 64-bit extensions are available, else `ia-32`.
diff --git a/common/lib/config.c b/common/lib/config.c
index 0523040e..e361f6b1 100644
--- a/common/lib/config.c
+++ b/common/lib/config.c
@@ -9,6 +9,7 @@
 #include <lib/print.h>
 #include <pxe/tftp.h>
 #include <crypt/blake2b.h>
+#include <sys/cpu.h>
 
 #define CONFIG_B2SUM_SIGNATURE "++CONFIG_B2SUM_SIGNATURE++"
 #define CONFIG_B2SUM_EMPTY "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
@@ -178,6 +179,27 @@ int init_config(size_t config_size) {
     }
 
     // Load macros
+    struct macro *arch_macro = ext_mem_alloc(sizeof(struct macro));
+    strcpy(arch_macro->name, "ARCH");
+#if defined (__x86_64__)
+    strcpy(arch_macro->value, "x86-64");
+#elif defined (__i386__)
+    {
+    uint32_t eax, ebx, ecx, edx;
+    if (!cpuid(0x80000001, 0, &eax, &ebx, &ecx, &edx) || !(edx & (1 << 29))) {
+        strcpy(arch_macro->value, "ia-32");
+    } else {
+        strcpy(arch_macro->value, "x86-64");
+    }
+    }
+#elif defined (__aarch64__)
+    strcpy(arch_macro->value, "aarch64");
+#else
+#error "Unspecified architecture"
+#endif
+    arch_macro->next = macros;
+    macros = arch_macro;
+
     for (size_t i = 0; i < config_size;) {
         if ((config_size - i >= 3 && memcmp(config_addr + i, "\n${", 3) == 0)
          || (config_size - i >= 2 && i == 0 && memcmp(config_addr, "${", 2) == 0)) {
diff --git a/test/limine.cfg b/test/limine.cfg
index a6c2313a..2ecad77f 100644
--- a/test/limine.cfg
+++ b/test/limine.cfg
@@ -11,7 +11,7 @@ TERM_WALLPAPER=${WALLPAPER_PATH}
 TERM_BACKDROP=008080
 
 :Limine Test
-    COMMENT=Test of the Limine boot protocol.
+    COMMENT=Test of the Limine boot protocol. ${ARCH}
 
     PROTOCOL=limine
     KERNEL_PATH=${TEST_KERNEL}
tab: 248 wrap: offon