Fri, 17 Mar 2023 14:06:27 +0100
15 files changed,
70 insertions(+),
63 deletions(-)
M
link.txt
→
link.txt
@@ -5,28 +5,19 @@ PAGE_SIZE = 1 << 12;
MEMORY { - BOOT_STAGE1_VMA : ORIGIN = 0x00007C00, LENGTH = 0x00000200 - BOOT_STAGE2_VMA : ORIGIN = 0x00010000, LENGTH = 0x00090000 + BOOT_VMA : ORIGIN = 0x00007C00, LENGTH = 0x00001000 KERNEL_VMA : ORIGIN = 0xC0000000, LENGTH = 0x30000000 APPLICATION_VMA : ORIGIN = 0x10000000, LENGTH = 0xB0000000 } SECTIONS { - BOOT_STAGE1 : AT(0x0000) + BOOT : AT(0x0000) { - */stage1.o(.*) - . = 510; - BYTE(0x55) - BYTE(0xAA) - } >BOOT_STAGE1_VMA + */boot.o(.*) + } >BOOT_VMA - BOOT_STAGE2 : AT(LOADADDR(BOOT_STAGE1) + SIZEOF(BOOT_STAGE1)) - { - */stage2.o(.*) - } >BOOT_STAGE2_VMA - - KERNEL : AT(LOADADDR(BOOT_STAGE2) + SIZEOF(BOOT_STAGE2)) + KERNEL : AT(LOADADDR(BOOT) + SIZEOF(BOOT)) { */kernel.o(.*) */kernel.o(.bss)@@ -48,9 +39,9 @@
} >KERNEL_VMA } -__SS_LMA = LOADADDR(BOOT_STAGE2); -__SS_VMA = ADDR(BOOT_STAGE2); -__SS_SIZE = SIZEOF(BOOT_STAGE2); +__SS_LMA = LOADADDR(BOOT); +__SS_VMA = ADDR(BOOT); +__SS_SIZE = SIZEOF(BOOT); __KERNEL_LMA = LOADADDR(KERNEL); __KERNEL_VMA = ADDR(KERNEL);
M
makefile
→
makefile
@@ -51,7 +51,7 @@
export CCFLAGS export GLOBAL_BUILD -MODULES := stage1 stage2 kernel apps +MODULES := boot kernel apps OBJECTS := $(patsubst %,$(LOCAL_BUILD)/%.o,$(MODULES)) $(LOCAL_BUILD)/apps_raw.o DIRS := $(LOCAL_BUILD)@@ -77,13 +77,9 @@
$(LOG_DIR)/objdump.txt: $(GLOBAL_BUILD)/base.o > $(OBJDUMP) -D $< > $@ -.PHONY: stage1 -stage1: -> $(MAKE) GLOBAL_BUILD=$(LOCAL_BUILD) -C src/stage1 $(LOCAL_BUILD)/stage1.o - -.PHONY: stage2 -stage2: -> $(MAKE) GLOBAL_BUILD=$(LOCAL_BUILD) -C src/stage2 $(LOCAL_BUILD)/stage2.o +.PHONY: boot +boot: +> $(MAKE) GLOBAL_BUILD=$(LOCAL_BUILD) -C src/boot $(LOCAL_BUILD)/boot.o .PHONY: kernel kernel:
A
src/boot/link.txt
@@ -0,0 +1,24 @@
+OUTPUT_ARCH(i386) +OUTPUT_FORMAT(elf32-i386) + +PAGE_SIZE = 1 << 12; + +MEMORY +{ + BOOT_VMA : ORIGIN = 0x00007C00, LENGTH = 0x00001000 + KERNEL_VMA : ORIGIN = 0xC0000000, LENGTH = 0x30000000 +} + +SECTIONS +{ + BOOT : AT(0x0000) + { + */*.o(.mbr) + . = 510; + BYTE(0x55) + BYTE(0xAA) + */*.o(.text) + */*.o(.data) + */*.o(.*) + } >BOOT_VMA +}
D
src/stage1/makefile
@@ -1,15 +0,0 @@
-.RECIPEPREFIX = > - -LOCAL_BUILD = $(GLOBAL_BUILD)/stage1 - -OBJECTS := $(GLOBAL_BUILD)/stage1.o -DIRS := $(sort $(dir $(OBJECTS))) - -$(OBJECTS): | $(DIRS) -$(DIRS): -> $(MKDIR) $(DIRS) - -.PHONY: build -build: $(OBJECTS) -$(GLOBAL_BUILD)/stage1.o: stage1.s -> $(AS) -o $@ $<
M
src/stage1/stage1.s
→
src/boot/stage1.s
@@ -1,4 +1,4 @@
-.section .text +.section .mbr .code16 # disable interrupts@@ -6,8 +6,8 @@ cli
# canonicalize %CS:%EIP to a known value ljmp $0, $start -start: +start: # setup segments movw $0x0000, %ax movw %ax, %ss@@ -24,6 +24,30 @@ movb $0, %al
movb $0x05, %ah int $0x10 + # load rest of bootloader + movw $0x0000, %bx # buffer address + movw $0x07e0, %ax + movw %ax, %es # buffer address (segment) + movb $0x02, %ah # function 0x02: read a sector + movb $0x07, %al # sectors to read count + movb $0x00, %ch # cylinder + movb $0x02, %cl # sector + movb $0x00, %dh # head + + # dl (drive) keep as is + int $0x13 + pop %cx + jnc bl_loaded + loop load + jc error + +bl_loaded: + mov $done_msg, %si + jmp print + +debug: + jmp debug + # check if A20 gate is enabled mov $a20_msg, %si call print@@ -31,6 +55,7 @@ call checkA20
je disabled enabled: call print_done + jmp error_loop # debug jmp resume disabled: call print_fail@@ -184,8 +209,9 @@ pop %es
pop %ds ret +.section .data a20_msg: - .ascii "Enabling A20..." + .ascii "Checking A20..." .byte 0 reset_msg:
D
src/stage2/link.txt
@@ -1,16 +0,0 @@
-OUTPUT_FORMAT(elf32-i386) -OUTPUT_ARCH(i386) -ENTRY(_ss_start) - -SECTIONS -{ - .text : - { - *.*(.text) - } - - .data : - { - *.*(.data) - } -}
M
src/stage2/makefile
→
src/boot/makefile
@@ -1,6 +1,6 @@
.RECIPEPREFIX = > -LOCAL_BUILD := $(GLOBAL_BUILD)/stage2 +LOCAL_BUILD := $(GLOBAL_BUILD)/boot SRC_DIR := $(shell pwd)@@ -18,9 +18,9 @@ $(DIRS):
> $(MKDIR) $(DIRS) .PHONY: build -build: $(GLOBAL_BUILD)/stage2.o -$(GLOBAL_BUILD)/stage2.o: $(OBJECTS) -> $(LD) $^ -r -T link.txt -o $@ --oformat elf32-i386 +build: $(GLOBAL_BUILD)/boot.o +$(GLOBAL_BUILD)/boot.o: $(OBJECTS) +> $(LD) $^ -r -T link.txt -Map=$(LOG_DIR)/boot_mapfile.txt -o $@ --oformat elf32-i386 $(LOCAL_BUILD)/%.s.o: %.s > $(AS) -o $@ $<