From 670b635ae8d92b12ca5d20c26852be9e07460336 Mon Sep 17 00:00:00 2001 From: mintsuki Date: Thu, 15 Jun 2023 02:23:48 +0200 Subject: [PATCH] config: Add and document ARCH built-in macro --- CONFIG.md | 6 ++++++ common/lib/config.c | 24 ++++++++++++++++++++++++ test/limine.cfg | 2 +- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/CONFIG.md b/CONFIG.md index ec59d860..7f0688ab 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`, `riscv64`. 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..cdfa2e0f 100644 --- a/common/lib/config.c +++ b/common/lib/config.c @@ -9,6 +9,7 @@ #include #include #include +#include #define CONFIG_B2SUM_SIGNATURE "++CONFIG_B2SUM_SIGNATURE++" #define CONFIG_B2SUM_EMPTY "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" @@ -178,6 +179,29 @@ 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"); +#elif defined (__riscv64) + strcpy(arch_macro->value, "riscv64"); +#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}