Skip to content

Commit

Permalink
refactor: reorganize bootloader source code
Browse files Browse the repository at this point in the history
  • Loading branch information
d0p1s4m4 committed Mar 24, 2024
1 parent a4d5179 commit 74e09f7
Show file tree
Hide file tree
Showing 23 changed files with 253 additions and 203 deletions.
13 changes: 7 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ TOPDIR := $(dir $(realpath $(lastword $(MAKEFILE_LIST))))
SYSROOTDIR := $(TOPDIR)/sysroot
TOOLSDIR := $(TOPDIR)/tools

RM = echo
AS = fasm
RM = rm -f

MK_BUGREPORT := \"https://git.cute.engineering/d0p1/StupidOS/issues\"
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
ifneq ($(OS),Windows_NT)
Expand All @@ -28,7 +29,7 @@ clean: GOAL:=clean
.PHONY: $(SUBDIRS)
$(SUBDIRS):
@echo "📁 $@"
DESTDIR=$(SYSROOTDIR) $(MAKE) -C $@ $(GOAL)
@DESTDIR=$(SYSROOTDIR) $(MAKE) -C $@ $(GOAL)

.PHONY: stupid.iso
stupid.iso: $(SUBDIRS)
Expand All @@ -46,8 +47,8 @@ stupid.tar.gz: $(SUBDIRS)
floppy_boot.img: $(SUBDIRS)
dd if=/dev/zero of=$@ bs=512 count=1440
mformat -C -f 1440 -i $@
dd if=boot/bootsector.bin of=$@ conv=notrunc
mcopy -i $@ boot/stpdboot.sys ::/STPDBOOT.SYS
dd if=boot/bootsect/bootsector.bin of=$@ conv=notrunc
mcopy -i $@ boot/loader/stpdldr.sys ::/STPDLDR.SYS
mcopy -i $@ kernel/vmstupid.sys ::/VMSTUPID.SYS

.PHONY: run
Expand All @@ -63,4 +64,4 @@ run: all

.PHONY: clean
clean: $(SUBDIRS)
$(RM) $(TARGET) $(SYSROOTDIR)
$(RM) $(TARGET)
4 changes: 1 addition & 3 deletions bin/Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
SUBDIRS = cmd

TOPGOALS = all clean install

SUBDIRS = cmd

.PHONY: $(SUBDIRS)
$(SUBDIRS):
@echo "📁 bin/$@"
DESTDIR=$(DESTDIR)/bin $(MAKE) -C $@ $(MAKECMDGOALS)
@DESTDIR=$(DESTDIR)/bin $(MAKE) -C $@ $(MAKECMDGOALS)

.PHONY: $(TOPGOALS)
$(TOPGOALS): $(SUBDIRS)
24 changes: 13 additions & 11 deletions bin/cmd/cmd.asm
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
format COFF

section '.text' code

public main
main:
int 0x2A

section '.data' data

INCLUDE 'builtins.inc'
format COFF

section '.text' code

public main
public _start
_start:
main:
int 0x2A

section '.data' data

INCLUDE 'builtins.inc'
44 changes: 8 additions & 36 deletions boot/Makefile
Original file line number Diff line number Diff line change
@@ -1,39 +1,11 @@
AS = fasm
RM = rm
TOPGOALS = all clean install

TARGET = bootsector.bin stpdboot.sys bootia32.efi
SUBDIRS = bootsect loader efi

STAGE0_SRCS = boot0.asm \
const.inc \
fat12.inc
.PHONY: $(SUBDIRS)
$(SUBDIRS):
@echo "📁 boot/$@"
@DESTDIR=$(DESTDIR) $(MAKE) -C $@ $(MAKECMDGOALS)

STAGE1_SRCS = boot1.asm \
const.inc \
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
.PHONY: $(TOPGOALS)
$(TOPGOALS): $(SUBDIRS)
120 changes: 0 additions & 120 deletions boot/boot0.asm

This file was deleted.

18 changes: 18 additions & 0 deletions boot/bootsect/Makefile
Original file line number Diff line number Diff line change
@@ -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 changes: 122 additions & 0 deletions boot/bootsect/floppy.asm
Original file line number Diff line number Diff line change
@@ -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
File renamed without changes.
Loading

0 comments on commit 74e09f7

Please sign in to comment.