refactor: reorganize bootloader source code
Some checks are pending
Build / test (push) Waiting to run
Docs / test (push) Waiting to run

This commit is contained in:
d0p1 🏳️‍⚧️ 2024-03-24 09:03:03 +01:00
parent a4d5179982
commit 74e09f7b33
23 changed files with 253 additions and 203 deletions

View file

@ -4,12 +4,13 @@ TOPDIR := $(dir $(realpath $(lastword $(MAKEFILE_LIST))))
SYSROOTDIR := $(TOPDIR)/sysroot SYSROOTDIR := $(TOPDIR)/sysroot
TOOLSDIR := $(TOPDIR)/tools TOOLSDIR := $(TOPDIR)/tools
RM = echo AS = fasm
RM = rm -f
MK_BUGREPORT := \"https://git.cute.engineering/d0p1/StupidOS/issues\" MK_BUGREPORT := \"https://git.cute.engineering/d0p1/StupidOS/issues\"
MK_COMMIT := \"$(shell git rev-parse --short HEAD)\" MK_COMMIT := \"$(shell git rev-parse --short HEAD)\"
SUBDIRS := tools include boot kernel lib bin SUBDIRS := external tools include boot kernel lib bin
TARGET = stupid.tar.gz floppy_boot.img TARGET = stupid.tar.gz floppy_boot.img
ifneq ($(OS),Windows_NT) ifneq ($(OS),Windows_NT)
@ -28,7 +29,7 @@ clean: GOAL:=clean
.PHONY: $(SUBDIRS) .PHONY: $(SUBDIRS)
$(SUBDIRS): $(SUBDIRS):
@echo "📁 $@" @echo "📁 $@"
DESTDIR=$(SYSROOTDIR) $(MAKE) -C $@ $(GOAL) @DESTDIR=$(SYSROOTDIR) $(MAKE) -C $@ $(GOAL)
.PHONY: stupid.iso .PHONY: stupid.iso
stupid.iso: $(SUBDIRS) stupid.iso: $(SUBDIRS)
@ -46,8 +47,8 @@ stupid.tar.gz: $(SUBDIRS)
floppy_boot.img: $(SUBDIRS) floppy_boot.img: $(SUBDIRS)
dd if=/dev/zero of=$@ bs=512 count=1440 dd if=/dev/zero of=$@ bs=512 count=1440
mformat -C -f 1440 -i $@ mformat -C -f 1440 -i $@
dd if=boot/bootsector.bin of=$@ conv=notrunc dd if=boot/bootsect/bootsector.bin of=$@ conv=notrunc
mcopy -i $@ boot/stpdboot.sys ::/STPDBOOT.SYS mcopy -i $@ boot/loader/stpdldr.sys ::/STPDLDR.SYS
mcopy -i $@ kernel/vmstupid.sys ::/VMSTUPID.SYS mcopy -i $@ kernel/vmstupid.sys ::/VMSTUPID.SYS
.PHONY: run .PHONY: run
@ -63,4 +64,4 @@ run: all
.PHONY: clean .PHONY: clean
clean: $(SUBDIRS) clean: $(SUBDIRS)
$(RM) $(TARGET) $(SYSROOTDIR) $(RM) $(TARGET)

View file

@ -1,5 +1,3 @@
SUBDIRS = cmd
TOPGOALS = all clean install TOPGOALS = all clean install
SUBDIRS = cmd SUBDIRS = cmd
@ -7,7 +5,7 @@ SUBDIRS = cmd
.PHONY: $(SUBDIRS) .PHONY: $(SUBDIRS)
$(SUBDIRS): $(SUBDIRS):
@echo "📁 bin/$@" @echo "📁 bin/$@"
DESTDIR=$(DESTDIR)/bin $(MAKE) -C $@ $(MAKECMDGOALS) @DESTDIR=$(DESTDIR)/bin $(MAKE) -C $@ $(MAKECMDGOALS)
.PHONY: $(TOPGOALS) .PHONY: $(TOPGOALS)
$(TOPGOALS): $(SUBDIRS) $(TOPGOALS): $(SUBDIRS)

View file

@ -3,6 +3,8 @@
section '.text' code section '.text' code
public main public main
public _start
_start:
main: main:
int 0x2A int 0x2A

View file

@ -1,39 +1,11 @@
AS = fasm TOPGOALS = all clean install
RM = rm
TARGET = bootsector.bin stpdboot.sys bootia32.efi SUBDIRS = bootsect loader efi
STAGE0_SRCS = boot0.asm \ .PHONY: $(SUBDIRS)
const.inc \ $(SUBDIRS):
fat12.inc @echo "📁 boot/$@"
@DESTDIR=$(DESTDIR) $(MAKE) -C $@ $(MAKECMDGOALS)
STAGE1_SRCS = boot1.asm \ .PHONY: $(TOPGOALS)
const.inc \ $(TOPGOALS): $(SUBDIRS)
a20.inc \
multiboot.inc
BOOTIA32_EFI_SRCS = bootia32.asm \
uefi.inc
.PHONY: all
all: $(TARGET)
bootsector.bin: $(STAGE0_SRCS)
$(AS) boot0.asm $@
stpdboot.sys: $(STAGE1_SRCS)
$(AS) boot1.asm $@
bootia32.efi: $(BOOTIA32_EFI_SRCS)
$(AS) bootia32.asm $@
.PHONY: clean
clean:
$(RM) $(TARGET)
.PHONY: install
install: $(TARGET)
@ mkdir -p $(DESTDIR)
install stpdboot.sys $(DESTDIR)
@ mkdir -p $(DESTDIR)/EFI/BOOT
install bootia32.efi $(DESTDIR)/EFI/BOOT

View file

@ -1,120 +0,0 @@
INCLUDE 'const.inc'
ORG STAGE0_BASE
USE16
jmp short _start
nop
; Boot Record
OEM_identifier db 'STUPID '
bytes_per_sector dw 512
sectors_per_cluster db 1
reserved_sectors dw 1
FAT_count db 2
root_dir_entries dw 224
total_sectors dw 2880
media_desc_type db 0xF0
sectors_per_FAT dw 9
sectors_per_track dw 18
heads_per_cylinder dw 2
hidden_sectors dd 0
large_sector_count dd 0
; Extended Boot Record
drive_number db 0x0
reserved db 0x0
signature db 0x29 ; 0x28 or 0x29
volume_id dd 0xB00B135 ; hope mine will grow :'(
volume_label db 'Stupid Boot'
system_id db 'FAT12 '
_start:
cli
cld
jmp 0x0:.canonicalize_cs
.canonicalize_cs:
xor ax, ax
mov ds, ax
mov ss, ax
mov es, ax
mov sp, 0x7c00
mov [drive_number], dl
; reset floppy disk
@@:
mov dl, [drive_number]
xor ah, ah
int 0x13
jc @b
call fat_load_root
; search in root directory
mov si, kernel_file
call fat_search_root
jc .error_not_found
mov [kernel_start], ax
mov si, stage1_file
call fat_search_root
jc .error_not_found
mov [stage1_start], ax
; load fat
xor ax, ax
mov al, [FAT_count]
mul word [sectors_per_FAT]
mov cx, ax
mov ax, [reserved_sectors]
xor bx, bx
call disk_read_sectors
; preload kernel
mov ax, KERNEL_PRELOAD/0x10
mov es, ax
mov ax, [kernel_start]
xor bx, bx
call fat_load_binary
; load stage 2
mov ax, STAGE1_BASE/0x10
mov es, ax
mov ax, [stage1_start]
xor bx, bx
call fat_load_binary
jmp 0x0:STAGE1_BASE
.error_not_found:
push si
mov si, msg_error
call bios_print
pop si
call bios_print
mov si, msg_not_found
call bios_print
hlt
jmp $
INCLUDE "utils.inc"
INCLUDE "fat12.inc"
msg_error db "ERROR: ", 0
msg_not_found db " not found", CR, LF, 0
kernel_file db "VMSTUPIDSYS", 0
stage1_file db "STPDBOOTSYS", 0
kernel_start dw 0x0
stage1_start dw 0x0
rb 0x7C00+512-2-$
db 0x55, 0xAA

18
boot/bootsect/Makefile Normal file
View file

@ -0,0 +1,18 @@
TARGET = bootsector.bin
FLOPPY_SRCS = floppy.asm \
../common/const.inc \
../common/fat12.inc
.PHONY: all
all: $(TARGET)
bootsector.bin: $(FLOPPY_SRCS)
$(AS) floppy.asm $@
.PHONY: clean
clean:
$(RM) $(TARGET)
.PHONY: install
install: $(TARGET)

122
boot/bootsect/floppy.asm Normal file
View file

@ -0,0 +1,122 @@
format binary
use16
include '../common/const.inc'
org BOOTSECT_BASE
jmp short _start
nop
; Boot Record
OEM_identifier db 'STUPID '
bytes_per_sector dw 512
sectors_per_cluster db 1
reserved_sectors dw 1
FAT_count db 2
root_dir_entries dw 224
total_sectors dw 2880
media_desc_type db 0xF0
sectors_per_FAT dw 9
sectors_per_track dw 18
heads_per_cylinder dw 2
hidden_sectors dd 0
large_sector_count dd 0
; Extended Boot Record
drive_number db 0x0
reserved db 0x0
signature db 0x29 ; 0x28 or 0x29
volume_id dd 0xB00B135 ; hope mine will grow :'(
volume_label db 'Stupid Boot'
system_id db 'FAT12 '
_start:
cli
cld
jmp 0x0:.canonicalize_cs
.canonicalize_cs:
xor ax, ax
mov ds, ax
mov ss, ax
mov es, ax
mov sp, 0x7c00
mov [drive_number], dl
; reset floppy disk
@@:
mov dl, [drive_number]
xor ah, ah
int 0x13
jc @b
call fat_load_root
; search in root directory
mov si, kernel_file
call fat_search_root
jc .error_not_found
mov [kernel_start], ax
mov si, stage1_file
call fat_search_root
jc .error_not_found
mov [stage1_start], ax
; load fat
xor ax, ax
mov al, [FAT_count]
mul word [sectors_per_FAT]
mov cx, ax
mov ax, [reserved_sectors]
xor bx, bx
call disk_read_sectors
; preload kernel
mov ax, KERNEL_PRELOAD/0x10
mov es, ax
mov ax, [kernel_start]
xor bx, bx
call fat_load_binary
; load stage 2
mov ax, LOADER_BASE/0x10
mov es, ax
mov ax, [stage1_start]
xor bx, bx
call fat_load_binary
jmp 0x0:LOADER_BASE
.error_not_found:
push si
mov si, msg_error
call bios_print
pop si
call bios_print
mov si, msg_not_found
call bios_print
hlt
jmp $
include '../common/bios.inc'
include '../common/fat12.inc'
msg_error db "ERROR: ", 0
msg_not_found db " not found", CR, LF, 0
kernel_file db "VMSTUPIDSYS", 0
stage1_file db "STPDLDR SYS", 0
kernel_start dw 0x0
stage1_start dw 0x0
rb 0x7C00+512-2-$
db 0x55, 0xAA

View file

@ -2,8 +2,8 @@ CR = 0x0D
LF = 0x0A LF = 0x0A
; -------- Address ---------- ; -------- Address ----------
STAGE0_BASE = 0x7C00 BOOTSECT_BASE = 0x7C00
STAGE1_BASE = 0x1000 LOADER_BASE = 0x1000
MULTIBOOT_BASE = 0x100000 MULTIBOOT_BASE = 0x100000
DISK_BUFFER = 0x8000 DISK_BUFFER = 0x8000
KERNEL_PRELOAD = 0xF000 KERNEL_PRELOAD = 0xF000

6
boot/common/macro.inc Normal file
View file

@ -0,0 +1,6 @@
macro defn name
{
virtual at 0
name name
end virtual
}

21
boot/efi/Makefile Normal file
View file

@ -0,0 +1,21 @@
TARGET = BOOTIA32.EFI
BOOTIA32_EFI_SRCS = bootia32.asm \
uefi.inc \
../common/const.inc \
../common/macro.inc
.PHONY: all
all: $(TARGET)
BOOTIA32.EFI: $(BOOTIA32_EFI_SRCS)
$(AS) bootia32.asm $@
.PHONY: clean
clean:
$(RM) $(TARGET)
.PHONY: install
install: $(TARGET)
@ mkdir -p $(DESTDIR)/EFI/BOOT
install $(TARGET) $(DESTDIR)/EFI/BOOT

View file

@ -3,6 +3,8 @@
section '.text' code executable readable section '.text' code executable readable
include '../common/const.inc'
include '../common/macro.inc'
include 'uefi.inc' include 'uefi.inc'
; ESP => return address ; ESP => return address
@ -29,14 +31,15 @@ efimain:
jmp $ jmp $
xor eax, eax
ret ret
section '.reloc' fixups data discardable section '.reloc' fixups data discardable
section '.data' data readable writeable section '.data' data readable writeable
hello_msg du 'StupidOS EFI Bootloader', 13, 10, 0 hello_msg du 'StupidOS EFI Bootloader', 13, 10, 0
kernel_file du 'vmstupid.sys', 0
handle dd ? handle dd ?
system_table dd ? system_table dd ?

View file

@ -1,10 +1,3 @@
macro defn name
{
virtual at 0
name name
end virtual
}
struc BOOLEAN struc BOOLEAN
{ {
. db ? . db ?

22
boot/loader/Makefile Normal file
View file

@ -0,0 +1,22 @@
TARGET = stpdldr.sys
LOADER_SRCS = loader.asm \
../common/const.inc \
a20.inc \
multiboot.inc
.PHONY: all
all: $(TARGET)
stpdldr.sys: $(LOADER_SRCS)
$(AS) loader.asm $@
.PHONY: clean
clean:
$(RM) $(TARGET)
.PHONY: install
install: $(TARGET)
@ mkdir -p $(DESTDIR)
install $(TARGET) $(DESTDIR)

View file

@ -1,12 +1,14 @@
INCLUDE 'const.inc' format binary
INCLUDE 'multiboot.inc'
ORG STAGE1_BASE include '../common/const.inc'
USE32 include 'multiboot.inc'
org LOADER_BASE
use32
jmp _start jmp _start
ALIGN 4 align 4
multiboot_header: multiboot_header:
mb_header MultibootHeader multiboot_header mb_header MultibootHeader multiboot_header
@ -14,7 +16,7 @@ _start:
cmp eax, MULTIBOOT_MAGIC cmp eax, MULTIBOOT_MAGIC
je .multiboot je .multiboot
USE16 use16
;; non multiboot process ;; non multiboot process
push cs push cs
pop ds pop ds
@ -46,16 +48,15 @@ _start:
hlt hlt
jmp $ jmp $
INCLUDE 'a20.inc' include 'a20.inc'
INCLUDE 'utils.inc' include '../common/bios.inc'
INCLUDE 'memory.inc' include 'memory.inc'
INCLUDE 'video.inc' include 'video.inc'
msg_stage2 db "StupidOS Bootloader (Stage 1)", CR, LF, 0 msg_stage2 db "StupidOS Bootloader (Stage 1)", CR, LF, 0
msg_error_a20 db "ERROR: can't enable a20 line", CR, LF, 0 msg_error_a20 db "ERROR: can't enable a20 line", CR, LF, 0
msg_error_memory db "ERROR: can't detect available memory", CR, LF, 0 msg_error_memory db "ERROR: can't detect available memory", CR, LF, 0
;; ;;
bi_screen_width: dw 0 bi_screen_width: dw 0
bi_screen_height: dw 0 bi_screen_height: dw 0

View file

@ -15,7 +15,7 @@ struc MultibootHeader addr
; address fields (we'll just skip them) ; address fields (we'll just skip them)
.header_addr dd addr .header_addr dd addr
.load_addr dd STAGE1_BASE .load_addr dd LOADER_BASE
.load_end_addr dd _edata .load_end_addr dd _edata
.bss_end_addr dd _end .bss_end_addr dd _end
.entry_addr dd _start .entry_addr dd _start
@ -106,3 +106,11 @@ MULTIBOOT_MEMORY_RESERVED = 0x2
MULTIBOOT_MEMORY_ACPI = 0x3 MULTIBOOT_MEMORY_ACPI = 0x3
MULTIBOOT_MEMORY_NVS = 0x4 MULTIBOOT_MEMORY_NVS = 0x4
MULTIBOOT_MEMORY_BADPARAM = 0x5 MULTIBOOT_MEMORY_BADPARAM = 0x5
struc MultibootModule
{
.mod_start dd ?
.mod_end dd ?
.cmdline dd ?
.pad dd ?
}

View file

@ -5,7 +5,7 @@ TOPGOALS = all clean install
.PHONY: $(SUBDIRS) .PHONY: $(SUBDIRS)
$(SUBDIRS): $(SUBDIRS):
@echo "📁 lib/$@" @echo "📁 lib/$@"
DESTDIR=$(DESTDIR)/lib $(MAKE) -C $@ $(MAKECMDGOALS) @DESTDIR=$(DESTDIR)/lib $(MAKE) -C $@ $(MAKECMDGOALS)
.PHONY: $(TOPGOALS) .PHONY: $(TOPGOALS)
$(TOPGOALS): $(SUBDIRS) $(TOPGOALS): $(SUBDIRS)

View file

@ -17,3 +17,6 @@ coff-ld$(EXEXT): coff-ld.c
.PHONY: install .PHONY: install
install: $(TARGET) install: $(TARGET)
.PHONY: clean
clean:

View file

@ -8,7 +8,7 @@ set default=0
menuentry "StupidOS" { menuentry "StupidOS" {
echo "verify system integrity" echo "verify system integrity"
hashsum --hash sha256 --check /boot/hashfile --prefix / hashsum --hash sha256 --check /boot/hashfile --prefix /
multiboot /stpdboot.sys multiboot /stpdldr.sys
module /vmstupid.sys module /vmstupid.sys
boot boot
} }
@ -21,9 +21,9 @@ gen_iso_file() {
echo "$grub_config" > "$2/boot/grub/grub.cfg" echo "$grub_config" > "$2/boot/grub/grub.cfg"
(cd "$2"; (cd "$2";
sha256sum -b -t "vmstupid.sys" > "boot/hashfile"; sha256sum -b -t "vmstupid.sys" > "boot/hashfile";
sha256sum -b -t "stpdboot.sys" >> "boot/hashfile" sha256sum -b -t "stpdldr.sys" >> "boot/hashfile"
) )
grub-file --is-x86-multiboot "$2/stpdboot.sys" || exit 1 grub-file --is-x86-multiboot "$2/stpdldr.sys" || exit 1
grub-mkrescue -o $1 $2 grub-mkrescue -o $1 $2
} }