misc: Add a build ID to make sure that stage2 and stage3 always come from the same build
diff --git a/stage23/Makefile b/stage23/Makefile
index 3a541137..f57e7021 100644
--- a/stage23/Makefile
+++ b/stage23/Makefile
@@ -4,6 +4,7 @@ OBJCOPY = i386-elf-objcopy
OBJDUMP = i386-elf-objdump
READELF = i386-elf-readelf
+BUILD_ID := $(shell dd if=/dev/urandom count=8 bs=1 | od -An -t x8 | sed 's/^ /0x/')
LIMINE_VERSION := $(shell git describe --exact-match --tags `git log -n1 --pretty='%h'` || git log -n1 --pretty='%h')
WERROR = -Werror
CFLAGS = -Os -pipe -Wall -Wextra $(WERROR)
@@ -23,6 +24,7 @@ INTERNAL_CFLAGS = \
-mno-sse \
-mno-sse2 \
-MMD \
+ -DBUILD_ID=$(BUILD_ID) \
-DLIMINE_VERSION='"$(LIMINE_VERSION)"' \
-I. \
-I..
diff --git a/stage23/lib/blib.c b/stage23/lib/blib.c
index c66d21b9..9505f629 100644
--- a/stage23/lib/blib.c
+++ b/stage23/lib/blib.c
@@ -7,6 +7,9 @@
#include <lib/trace.h>
#include <lib/real.h>
+uint64_t build_id = BUILD_ID;
+stage3_data uint64_t stage3_build_id = BUILD_ID;
+
uint8_t boot_drive;
int boot_partition = -1;
diff --git a/stage23/lib/blib.h b/stage23/lib/blib.h
index 794353c8..880c57af 100644
--- a/stage23/lib/blib.h
+++ b/stage23/lib/blib.h
@@ -48,4 +48,7 @@ typedef char symbol[];
#define stage3_text __attribute__((section(".stage3_text")))
#define stage3_data __attribute__((section(".stage3_data")))
+extern uint64_t build_id;
+extern uint64_t stage3_build_id;
+
#endif
diff --git a/stage23/lib/config.c b/stage23/lib/config.c
index ebc92c7c..58b755df 100644
--- a/stage23/lib/config.c
+++ b/stage23/lib/config.c
@@ -31,6 +31,10 @@ int init_config_disk(struct volume *part) {
(uintptr_t)stage3_addr - 0x8000,
stage3.size - ((uintptr_t)stage3_addr - 0x8000));
+ if (build_id != stage3_build_id) {
+ panic("Limine build ID mismatch. Use the correct limine.sys.");
+ }
+
stage3_loaded = true;
}
