refactor: move multiboot struct from kernel to loader

This commit is contained in:
d0p1 🏳️‍⚧️ 2024-03-03 16:38:35 +01:00
parent e71c7ce555
commit 060c2835f6
11 changed files with 308 additions and 241 deletions

View file

@ -9,7 +9,8 @@ STAGE0_SRCS = boot0.asm \
STAGE1_SRCS = boot1.asm \
const.inc \
a20.inc
a20.inc \
multiboot.inc
.PHONY: all
all: $(TARGET)
@ -27,3 +28,5 @@ clean:
.PHONY: install
install: $(TARGET)
@ mkdir -p $(DESTDIR)
install stpdboot.sys $(DESTDIR)

View file

@ -1,2 +1,33 @@
#+TITLE: StupidOS Bootloader
#+begin_src
0x100000 +-----------------------+
| |
| Reserved |
| |
0x080000 +-----------------------+
| |
| Kernel preload |
| |
0x00F000 +-----------------------+
| |
| buffer |
| |
0x008000 +-----------------------+
| |
| Boot0 |
| |
0x007000 +-----------------------+
| |
| Real mode stack |
| |
0x006000 +-----------------------+
| |
| Boot1 |
| |
0x001000 +-----------------------+
| |
| Reserved (BIOS & IVT) |
| |
0x000000 +-----------------------+
#+end_src

View file

@ -1,9 +1,18 @@
INCLUDE 'const.inc'
INCLUDE 'const.inc'
INCLUDE 'multiboot.inc'
ORG STAGE1_BASE
USE16
stage2:
jmp _start
mb_header MultibootHeader mb_header
_start:
cmp eax, MULTIBOOT_MAGIC
je .multiboot
;; non multiboot process
push cs
pop ds
@ -13,9 +22,22 @@ stage2:
call a20_enable
jc .error_a20
; detect memory
call memory_get_map
jc .error_memory
xchg bx, bx
call video_setup
.multiboot:
jmp .hang
.error_memory:
mov si, msg_error_memory
jmp .error
.error_a20:
mov si, msg_error_a20
.error:
call bios_print
.hang:
hlt
@ -23,6 +45,21 @@ stage2:
INCLUDE 'a20.inc'
INCLUDE 'utils.inc'
INCLUDE 'memory.inc'
INCLUDE 'video.inc'
msg_stage2 db "StupidOS Bootloader (Stage 1)", 0x0D, 0x0A, 0
msg_error_a20 db "ERROR: can't enable a20 line", 0x0D, 0x0A, 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_memory db "ERROR: can't detect available memory", CR, LF, 0
;;
bi_screen_width: dw 0
bi_screen_height: dw 0
_edata:
; BSS
rb 0x4000
_end:

View file

@ -1,5 +1,6 @@
CR = 0x0D
LF = 0x0A
; -------- Address ----------
STAGE0_BASE = 0x7C00
STAGE1_BASE = 0x1000
@ -7,3 +8,7 @@ DISK_BUFFER = 0x8000
KERNEL_PRELOAD = 0xF000
STACK_TOP = 0x7000
; ---------- Video ------------
VIDEO_WIDTH = 1024
VIDEO_HEIGHT = 768
VIDEO_DEPTH = 32

28
boot/memory.inc Normal file
View file

@ -0,0 +1,28 @@
; 0xE820 Get System Memory Map
; EAX=0x0000E820
; EDX=0x534D4150
; EBX=0x0 or continuation value
; ECX=buffer size
; ES:SI = buffer
memory_do_E820:
ret
memory_get_map:
call memory_do_E820
jnc .end
; try 0x88
clc
mov ah, 0x88
int 0x15
jc .error
test ax, ax
je .error
.end:
clc
ret
.error:
stc
ret

View file

@ -1,106 +1,107 @@
MULTIBOOT_HDR_MAGIC = 0x1BADB002
MULTIBOOT_MAGIC = 0x2BADB002
MULTIBOOT_HDR_ALIGN = 0x1
MULTIBOOT_HDR_MEMINFO = 0x2
MULTIBOOT_HDR_VIDEO = 0x4
struc MultibootHeader flags,addr,width,height,depth
{
.magic dd MULTIBOOT_HDR_MAGIC
.flags dd flags
.checksum dd -(MULTIBOOT_HDR_MAGIC + flags)
; address fields (we'll just skip them)
.header_addr dd addr
.load_addr dd 0x100000
.load_end_addr dd _edata - KBASE
.bss_end_addr dd _end - KBASE
.entry_addr dd _start
; Video mode
.mode_type dd 0x0
.width dd width
.height dd height
.depth dd depth
}
struc MultibootData
{
.flags dd ?
; if flags[0] is set
.mem_lower dd ?
.mem_upper dd ?
; if flags[1] is set
.boot_device dd ?
; if flags[2] is set
.cmdline dd ?
; if flags[3] is set
.mods_count dd ?
.mods_addr dd ?
; if flags[4] is set
.syms dd 4 dup ?
; if flags[6] is set
.mmap_length dd ?
.mmap_addr dd ?
; if flags[7] is set
.drives_length dd ?
.drives_addr dd ?
; if flags[8] is set
.config_table dd ?
; if flags[9] is set
.bootloader_name dd ?
; if flags[10] is set
.apm_table dd ?
; if flags[11] is set
.vbe_control_info dd ?
.vbe_mode_info dd ?
.vbe_mode dw ?
.vbe_if_seg dw ?
.vbe_if_off dw ?
.vbe_if_length dw ?
; if flags[12] is set
.fb_addr dq ?
.fb_pitch dd ?
.fb_width dd ?
.fb_height dd ?
.fb_bpp db ?
.fb_type db ?
.fb_misc dw 3 dup ?
}
MULTIBOOT_DATA_MEM = 0x0001
MULTIBOOT_DATA_BOOTDEV = 0x0002
MULTIBOOT_DATA_CMDLINE = 0x0004
MULTIBOOT_DATA_MODULES = 0x0008
MULTIBOOT_DATA_MMAP = 0x0040
MULTIBOOT_DATA_DRIVES = 0x0080
MULTIBOOT_DATA_BOOTLOADER_NAME = 0x0200
MULTIBOOT_DATA_VBE = 0x0800
MULTIBOOT_DATA_FB = 0x1000
struc MultibootMMap
{
.size dd ?
.addr dq ?
.length dq ?
.type dd ?
}
MULTIBOOT_MEMORY_AVAILABLE = 0x1
MULTIBOOT_MEMORY_RESERVED = 0x2
MULTIBOOT_MEMORY_ACPI = 0x3
MULTIBOOT_MEMORY_NVS = 0x4
MULTIBOOT_MEMORY_BADPARAM = 0x5
MULTIBOOT_HDR_MAGIC = 0x1BADB002
MULTIBOOT_MAGIC = 0x2BADB002
MULTIBOOT_HDR_ALIGN = 0x1
MULTIBOOT_HDR_MEMINFO = 0x2
MULTIBOOT_HDR_VIDEO = 0x4
MULTIBOOT_FLAGS = MULTIBOOT_HDR_ALIGN or MULTIBOOT_HDR_MEMINFO or MULTIBOOT_HDR_VIDEO
struc MultibootHeader addr
{
.magic dd MULTIBOOT_HDR_MAGIC
.flags dd MULTIBOOT_FLAGS
.checksum dd -(MULTIBOOT_HDR_MAGIC + MULTIBOOT_FLAGS)
; address fields (we'll just skip them)
.header_addr dd addr
.load_addr dd STAGE1_BASE
.load_end_addr dd _edata - STAGE1_BASE
.bss_end_addr dd _end - STAGE1_BASE
.entry_addr dd _start
; Video mode
.mode_type dd 0x0
.width dd VIDEO_WIDTH
.height dd VIDEO_HEIGHT
.depth dd VIDEO_DEPTH
}
struc MultibootData
{
.flags dd ?
; if flags[0] is set
.mem_lower dd ?
.mem_upper dd ?
; if flags[1] is set
.boot_device dd ?
; if flags[2] is set
.cmdline dd ?
; if flags[3] is set
.mods_count dd ?
.mods_addr dd ?
; if flags[4] is set
.syms dd 4 dup ?
; if flags[6] is set
.mmap_length dd ?
.mmap_addr dd ?
; if flags[7] is set
.drives_length dd ?
.drives_addr dd ?
; if flags[8] is set
.config_table dd ?
; if flags[9] is set
.bootloader_name dd ?
; if flags[10] is set
.apm_table dd ?
; if flags[11] is set
.vbe_control_info dd ?
.vbe_mode_info dd ?
.vbe_mode dw ?
.vbe_if_seg dw ?
.vbe_if_off dw ?
.vbe_if_length dw ?
; if flags[12] is set
.fb_addr dq ?
.fb_pitch dd ?
.fb_width dd ?
.fb_height dd ?
.fb_bpp db ?
.fb_type db ?
.fb_misc dw 3 dup ?
}
MULTIBOOT_DATA_MEM = 0x0001
MULTIBOOT_DATA_BOOTDEV = 0x0002
MULTIBOOT_DATA_CMDLINE = 0x0004
MULTIBOOT_DATA_MODULES = 0x0008
MULTIBOOT_DATA_MMAP = 0x0040
MULTIBOOT_DATA_DRIVES = 0x0080
MULTIBOOT_DATA_BOOTLOADER_NAME = 0x0200
MULTIBOOT_DATA_VBE = 0x0800
MULTIBOOT_DATA_FB = 0x1000
struc MultibootMMap
{
.size dd ?
.addr dq ?
.length dq ?
.type dd ?
}
MULTIBOOT_MEMORY_AVAILABLE = 0x1
MULTIBOOT_MEMORY_RESERVED = 0x2
MULTIBOOT_MEMORY_ACPI = 0x3
MULTIBOOT_MEMORY_NVS = 0x4
MULTIBOOT_MEMORY_BADPARAM = 0x5

3
boot/video.inc Normal file
View file

@ -0,0 +1,3 @@
video_setup:
ret

View file

@ -1,27 +1,25 @@
AS = fasm
RM = rm -f
INSTALL = install
KERNEL = vmstupid
SRCS = kernel.asm \
const.inc \
boot/multiboot.inc \
boot/boot.inc \
mm/mm.inc
.PHONY: all
all: $(KERNEL)
$(KERNEL): $(SRCS)
$(AS) kernel.asm $@
.PHONY: clean
clean:
$(RM) $(KERNEL)
.PHONY: install
install: $(KERNEL)
@ mkdir -p $(DESTDIR)
install $< $(DESTDIR)
.PHONY: all clean
AS = fasm
RM = rm -f
INSTALL = install
KERNEL = vmstupid.sys
SRCS = kernel.asm \
const.inc \
mm/mm.inc
.PHONY: all
all: $(KERNEL)
$(KERNEL): $(SRCS)
$(AS) kernel.asm $@
.PHONY: clean
clean:
$(RM) $(KERNEL)
.PHONY: install
install: $(KERNEL)
@ mkdir -p $(DESTDIR)
install $< $(DESTDIR)
.PHONY: all clean

View file

@ -1,35 +0,0 @@
ORG 0x100000
ALIGN 4
USE32
INCLUDE 'boot/multiboot.inc'
INCLUDE 'const.inc'
mb_header MultibootHeader MB_FLAGS, mb_header, VIDEO_WIDTH, VIDEO_HEIGHT, VIDEO_DEPTH
_start:
cli
cmp eax, MULTIBOOT_MAGIC
jne hang
; iterate over memory
;
;
xor esi, esi
mov edi, kernel_page_table - KBASE
mov ecx, kernel_page_directory - KBASE
mov cr3, ecx
mov ecx, cr0
hang:
hlt
jmp $-1
; SEGMENT readable writable
boot_struct dd 0x0

View file

@ -1,53 +1,46 @@
KBASE = 0xC0000000
PSIZE = 0x1000
; --------- VERSION -------------
VERSION_MAJOR = 1
VERSION_MINOR = 0
; --------- BOOT PARAMS ---------
MB_FLAGS = MULTIBOOT_HDR_ALIGN or MULTIBOOT_HDR_MEMINFO or MULTIBOOT_HDR_VIDEO
VIDEO_WIDTH = 1024
VIDEO_HEIGHT = 768
VIDEO_DEPTH = 32
; --------- Registers ------------
CR0_PE = 0x00000001
CR0_MP = 0x00000002
CR0_EM = 0x00000004
CR0_TS = 0x00000008
CR0_ET = 0x00000010
CR0_NE = 0x00000020
CR0_WP = 0x00010000
CR0_AM = 0x00040000
CR0_NW = 0x20000000
CR0_CD = 0x40000000
CR0_PG = 0x80000000
CR3_PWT = 0x08
CR3_PCD = 0x10
CR4_VME = 0x0000001
CR4_PVI = 0x0000002
CR4_TSD = 0x0000004
CR4_DE = 0x0000008
CR4_PSE = 0x0000010
CR4_PAE = 0x0000020
CR4_MCE = 0x0000040
CR4_PGE = 0x0000080
CR4_PCE = 0x0000100
CR4_OSDXSR = 0x0000200
CR4_OSXMMEXCPT = 0x0000400
CR4_UMIP = 0x0000800
CR4_VMXE = 0x0002000
CR4_SMXE = 0x0004000
CR4_FSGSBASE = 0x0010000
CR4_PCIDE = 0x0020000
CR4_OSXSAVE = 0x0040000
CR4_SMEP = 0x0100000
CR4_SMAP = 0x0200000
CR4_PKE = 0x0400000
CR4_CET = 0x0800000
CR4_PKS = 0x1000000
KBASE = 0xC0000000
PSIZE = 0x1000
; --------- VERSION -------------
VERSION_MAJOR = 1
VERSION_MINOR = 0
; --------- Registers ------------
CR0_PE = 0x00000001
CR0_MP = 0x00000002
CR0_EM = 0x00000004
CR0_TS = 0x00000008
CR0_ET = 0x00000010
CR0_NE = 0x00000020
CR0_WP = 0x00010000
CR0_AM = 0x00040000
CR0_NW = 0x20000000
CR0_CD = 0x40000000
CR0_PG = 0x80000000
CR3_PWT = 0x08
CR3_PCD = 0x10
CR4_VME = 0x0000001
CR4_PVI = 0x0000002
CR4_TSD = 0x0000004
CR4_DE = 0x0000008
CR4_PSE = 0x0000010
CR4_PAE = 0x0000020
CR4_MCE = 0x0000040
CR4_PGE = 0x0000080
CR4_PCE = 0x0000100
CR4_OSDXSR = 0x0000200
CR4_OSXMMEXCPT = 0x0000400
CR4_UMIP = 0x0000800
CR4_VMXE = 0x0002000
CR4_SMXE = 0x0004000
CR4_FSGSBASE = 0x0010000
CR4_PCIDE = 0x0020000
CR4_OSXSAVE = 0x0040000
CR4_SMEP = 0x0100000
CR4_SMAP = 0x0200000
CR4_PKE = 0x0400000
CR4_CET = 0x0800000
CR4_PKS = 0x1000000

View file

@ -1,15 +1,18 @@
INCLUDE 'boot/boot.inc'
ORG $ + KBASE
INCLUDE 'mm/mm.inc'
kmain:
nop
_edata:
; BSS
rb 0x4000
_end:
INCLUDE 'const.inc'
ORG KBASE
USE32
jmp kmain
INCLUDE 'mm/mm.inc'
kmain:
nop
_edata:
; BSS
rb 0x4000
_end: