From a85b624c0698873155668ff91fc8edc39fb1c33e Mon Sep 17 00:00:00 2001 From: "Chloe M." Date: Thu, 20 Aug 2026 08:23:06 +0000 Subject: boot/amd64: mbr: Verify RDAR image Signed-off-by: Chloe M. --- boot/arch/amd64/mbr/mbr.S | 49 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'boot/arch') diff --git a/boot/arch/amd64/mbr/mbr.S b/boot/arch/amd64/mbr/mbr.S index ee58525..e6c84fa 100644 --- a/boot/arch/amd64/mbr/mbr.S +++ b/boot/arch/amd64/mbr/mbr.S @@ -2,6 +2,10 @@ #define BOOT_SEG 0x7C0 /* Segment we reside on startup */ #define BOOT_RELOC_SEG 0x7A0 /* Relocation segment */ +/* Constants */ +#define RDAR_MAGIC ">RDAR!" +#define RDAR_MAGIC_LEN 6 + .code16 _start: /* Normalize CS + other selectors */ @@ -29,6 +33,10 @@ _start: /* Jump to our relocated self */ ljmp $BOOT_RELOC_SEG,$reloc reloc: + /* Load %DS with our new segment */ + mov $BOOT_RELOC_SEG, %ax + mov %ax, %ds + /* Save the drive number */ mov %dl, drive_num @@ -44,10 +52,49 @@ reloc: movl $1, dap_lba_low call disk_read_lba + /* Get stage2 */ + call find_stage2 + 1: cli hlt jmp 1b +/* + * Locate stage2 on the disk + * + * TODO: This only verifies the magic number is correct, this should + * be completed. + */ +find_stage2: + push %ds + push %es + + /* Verify that magic is correct */ + mov $BOOT_SEG, %ax + mov %ax, %ds + xor %si, %si + mov $BOOT_RELOC_SEG, %ax + mov %ax, %es + mov $rdar_magic, %di + mov $RDAR_MAGIC_LEN, %cx + rep cmpsb + jne .bad_magic + + pop %es + pop %ds + ret +.bad_magic: + /* Restore %DS */ + mov $BOOT_RELOC_SEG, %ax + mov %ax, %ds + + mov $bad_magic_msg, %si + call putstr +1: cli + hlt + jmp 1b + + /* * Read sectors from the disk */ @@ -109,7 +156,9 @@ dap_lba_high: drive_num: .byte 0x00 boot_msg: .ascii "booting\r\n\0" +bad_magic_msg: .ascii "bad RDAR magic\r\n\0" disk_err_msg: .ascii "\r\n!!!disk error!!!\r\n\0" +rdar_magic: .ascii RDAR_MAGIC .org 510 .byte 0x55 -- cgit v1.2.3