CeDOS - Commit 5707f819

Bootloader now reads MBR instead of relying on values given at compile time
Celina Kalus
Sat, 18 Mar 2023 19:21:02 +0100
5 files changed, 124 insertions(+), 29 deletions(-)
M makefilemakefile

@@ -65,7 +65,7 @@

$(GLOBAL_BUILD)/fat.img: $(MODULES) # > $(LD) $(OBJECTS) -r -T link.txt -Map=$(LOG_DIR)/elf_mapfile.txt --oformat elf32-i386 -o $@ > dd if=/dev/zero of=$@ count=128 -> mkfs.fat $@ +> mkfs.fat -n "cedos" -S 512 $@ > mkdir -p ./mnt > sudo mount $@ ./mnt > sudo cp $(LOCAL_BUILD)/kernel.bin ./mnt

@@ -76,6 +76,7 @@ $(GLOBAL_BUILD)/cedos.img: $(GLOBAL_BUILD)/fat.img | $(MODULES)

> dd if=/dev/zero of=$@ count=144 > parted $@ mklabel msdos > parted $@ mkpart primary FAT32 8s 128s -s +> parted $@ set 1 boot on > dd if=$< of=$@ seek=8 conv=notrunc > dd bs=1 if=$(LOCAL_BUILD)/boot.bin of=$@ count=446 conv=notrunc > dd if=$(LOCAL_BUILD)/boot.bin of=$@ skip=1 seek=1 count=7 conv=notrunc
M src/boot/drives.ssrc/boot/drives.s

@@ -130,11 +130,6 @@ load_kernel:

movw $load_msg, %si call print - movw $0x0000, %ax - movw %ax, %ds - - movw $0x0008, %ax - movw $0x0088, %cx movw $0x0000, %bx load_sectors_loop:

@@ -142,9 +137,9 @@ push %ax

push %cx push %bx - movw $load_debug_msg, %si - call print - call print_hex_int16 + # movw $load_debug_msg, %si + # call print + # call print_hex_int16 movw %bx, %di

@@ -167,22 +162,22 @@ movb (%si), %dl

movw %di, %bx - movw $load_debug_arrow, %si - call print + # movw $load_debug_arrow, %si + # call print movw $0x1000, %ax - call print_hex_int16 movw %ax, %es # buffer address (segment) + # call print_hex_int16 - movw $load_debug_colon, %si - call print - movw %bx, %ax - call print_hex_int16 + # movw $load_debug_colon, %si + # call print + # movw %bx, %ax + # call print_hex_int16 - movw $load_debug_colon, %si - call print - movw %cx, %ax - call print_hex_int16 + # movw $load_debug_colon, %si + # call print + # movw %cx, %ax + # call print_hex_int16 movb $0x02, %ah # function 0x02: read a sector movb $0x01, %al # sectors to read count

@@ -192,12 +187,14 @@ pusha

int $0x13 jc load_error - movw $load_debug_return, %si - call print - movb %ah, %al - xorb %ah, %ah - call print_hex_int16 - movw $newline, %si + # movw $load_debug_return, %si + # call print + # movb %ah, %al + # xorb %ah, %ah + # call print_hex_int16 + # movw $newline, %si + # call print + movw $load_msg_dot, %si call print popa

@@ -261,9 +258,13 @@ .ascii "Resetting bootdrive..."

.byte 0 load_msg: - .ascii "Loading kernel..." - .byte 13 - .byte 10 + .ascii "Loading kernel" + # .byte 13 + # .byte 10 + .byte 0 + +load_msg_dot: + .ascii "." .byte 0 load_debug_msg:
M src/boot/entry.ssrc/boot/entry.s

@@ -138,6 +138,8 @@ call enable_a20

call reset_drive + call find_bootable_part + call load_kernel movw $gdt_msg, %si
M src/boot/link.txtsrc/boot/link.txt

@@ -3,6 +3,8 @@ OUTPUT_FORMAT(binary)

PAGE_SIZE = 1 << 12; +PART_TABLE_START = 0x00007DBE; + MEMORY { BOOT_VMA : ORIGIN = 0x00007C00, LENGTH = 0x00001000
A src/boot/mbr.s

@@ -0,0 +1,89 @@

+.code16 +.section .text +.global find_bootable_part +find_bootable_part: + movw $0x0000, %ax + movw %ax, %ds + + movw $0x0000, %cx + +find_part_loop: + movw $mbr_partition_msg, %si + call print + movw %cx, %ax + call print_hex_int16 + movw $newline, %si + call print + + movw $PART_TABLE_START, %si + + xor %ax, %ax + movb (%si), %al + + cmp $0x80, %al + je find_part_loop_end + + # TODO: loop over partitions + jmp find_part_loop_fail + +find_part_loop_end: + movw $mbr_bootable_msg, %si + call print + movw %cx, %ax + call print_hex_int16 + movw $newline, %si + call print + + # load partition size from MBR + movw $PART_TABLE_START, %si + add $0x0C, %si + + movw (%si), %cx + + movw $mbr_size_msg, %si + call print + movw %cx, %ax + call print_hex_int16 + movw $newline, %si + call print + + # load partition start from MBR + movw $PART_TABLE_START, %si + add $0x08, %si + + movw (%si), %ax + + movw $mbr_start_msg, %si + call print + call print_hex_int16 + movw $newline, %si + call print + + add %ax, %cx + + ret + +find_part_loop_fail: + jmp error + +.section .data +mbr_partition_msg: + .ascii " Checking partition 0x" + .byte + +mbr_bootable_msg: + .ascii " bootable partition found: 0x" + .byte 0 + +mbr_start_msg: + .ascii " Start sector: 0x" + .byte 0 + +mbr_size_msg: + .ascii " Number of sectors: 0x" + .byte 0 + +newline: + .byte 13 + .byte 10 + .byte 0