chore: remove useless file and trying refactor bootloader

This commit is contained in:
d0p1 🏳️‍⚧️ 2024-03-31 12:27:15 +02:00
parent fdd16ec651
commit 6d5cfb1669
13 changed files with 320 additions and 328 deletions

View file

@ -1,33 +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
#+TITLE: StupidOS Bootloader
#+begin_src
0x100000 +-----------------------+
| |
| Reserved |
| |
0x080000 +-----------------------+
| |
| Kernel preload |
| |
0x00F000 +-----------------------+
| |
| buffer |
| |
0x008000 +-----------------------+
| |
| bootsector |
| |
0x007000 +-----------------------+
| |
| Real mode stack |
| |
0x006000 +-----------------------+
| |
| stpdldr.sys |
| |
0x001000 +-----------------------+
| |
| Reserved (BIOS & IVT) |
| |
0x000000 +-----------------------+
#+end_src

View file

@ -1,9 +1,18 @@
bios_print:
lodsb
or al, al
jz @f
mov ah, 0x0E
int 0x10
jmp bios_print
@@:
ret
;; File: bios.inc
;; Function: bios_print
;;
;; Parameters:
;;
;; si - null-terminated string to print
;;
bios_print:
lodsb
or al, al
jz @f
mov ah, 0x0E
int 0x10
jmp bios_print
@@:
ret

View file

@ -1,181 +1,181 @@
ATTR_READ_ONLY = 0x01
ATTR_HIDDEN = 0x02
ATTR_SYSTEM = 0x04
ATTR_VOLUME_ID = 0x08
ATTR_DIRECTORY = 0x10
ATTR_ARCHIVE = 0x20
struc fat_entry
{
.name db 8 dup ?
.ext db 3 dup ?
.attrs db ?
.reserved dw ?
.creation_time dw ?
.creation_date dw ?
.access_date dw ?
.reserved2 dw ?
.mod_time dw ?
.mod_date dw ?
.start dw ?
.size dd ?
}
virtual at di
fat_entry fat_entry
end virtual
; CHS to LBA
; LBA = (C * HPC + H) * SPT + (S - 1)
;;; Read sectors from disk to buffer
;;;
;;; @param AX LBA starting sector
;;; @param CX sector count
;;; @param ES:BX buffer
;;;
disk_read_sectors:
; https://en.wikipedia.org/wiki/Logical_block_addressing
; convert LBA to CHS
; HPC = Head per Cluster
; SPT = Sector per Track
; S = (LBA % SPT) + 1
push ax
push bx
push cx
xor dx, dx
div word [sectors_per_track]
inc dx
mov [S], dx
; H = (LBA / SPT) % HPC
; C = LBA / (HPC * SPT)
xor dx, dx
div word [heads_per_cylinder]
mov [C], ax
mov [H], dx
; read sectors
mov ah, 0x2
mov al, 0x1
mov ch, byte [C]
mov cl, byte [S]
mov dh, byte [H]
mov dl, [drive_number]
int 0x13
jc @f
pop cx
pop bx
pop ax
add bx, word [bytes_per_sector]
inc ax
loop disk_read_sectors
ret
@@:
mov si, msg_error_sector
call bios_print
ret
C dw 0x00
H dw 0x00
S dw 0x00
fat_load_root:
mov ax, DISK_BUFFER/0x10
mov es, ax
; load root directory
mov ax, [sectors_per_FAT]
xor cx, cx
mov cl, [FAT_count]
mul cx
add ax, [reserved_sectors]
push ax
mov bx, [bytes_per_sector]
mov cl, 0x5
shr bx, cl
mov ax, [root_dir_entries]
xor dx, dx
div bx
mov cx, ax
pop ax
mov [data_start], ax
add [data_start], cx
xor bx, bx
call disk_read_sectors
ret
;; @param SI filename to search
fat_search_root:
mov cx, [root_dir_entries]
mov di, 0x0
@@:
push si
push cx
mov cx, 0xB ; name(8) + ext(3)
push di
rep cmpsb
pop di
pop cx
pop si
je .file_found
add di, 0x20
loop @b
; set carry if not found
stc
ret
.file_found:
mov ax, [es:fat_entry.start]
clc
ret
;;; @param AX cluster
;;; @param ES:BX buffer
fat_load_binary:
push ax
sub ax, 0x2
xor cx, cx
mov cl, [sectors_per_cluster]
mul cx
add ax, [data_start]
xor cx, cx
mov cl, [sectors_per_cluster]
call disk_read_sectors
pop ax
mov cx, ax
mov dx, ax
shr dx, 0x1
add cx, dx
push bx
mov bx, DISK_BUFFER
add bx, cx
mov dx, [bx]
pop bx
test ax, 0x1
jnz .odd_cluster
.even_cluster:
and dx, 0xFFF
jmp .end
.odd_cluster:
shr dx, 0x4
.end:
mov ax, dx
cmp dx, 0xFF0
jb fat_load_binary
ret
msg_error_sector db "ERROR: reading sector", CR, LF, 0
data_start dw 0x0
ATTR_READ_ONLY = 0x01
ATTR_HIDDEN = 0x02
ATTR_SYSTEM = 0x04
ATTR_VOLUME_ID = 0x08
ATTR_DIRECTORY = 0x10
ATTR_ARCHIVE = 0x20
struc fat_entry
{
.name db 8 dup ?
.ext db 3 dup ?
.attrs db ?
.reserved dw ?
.creation_time dw ?
.creation_date dw ?
.access_date dw ?
.reserved2 dw ?
.mod_time dw ?
.mod_date dw ?
.start dw ?
.size dd ?
}
virtual at di
fat_entry fat_entry
end virtual
; CHS to LBA
; LBA = (C * HPC + H) * SPT + (S - 1)
;;; Read sectors from disk to buffer
;;;
;;; @param AX LBA starting sector
;;; @param CX sector count
;;; @param ES:BX buffer
;;;
disk_read_sectors:
; https://en.wikipedia.org/wiki/Logical_block_addressing
; convert LBA to CHS
; HPC = Head per Cluster
; SPT = Sector per Track
; S = (LBA % SPT) + 1
push ax
push bx
push cx
xor dx, dx
div word [sectors_per_track]
inc dx
mov [S], dx
; H = (LBA / SPT) % HPC
; C = LBA / (HPC * SPT)
xor dx, dx
div word [heads_per_cylinder]
mov [C], ax
mov [H], dx
; read sectors
mov ah, 0x2
mov al, 0x1
mov ch, byte [C]
mov cl, byte [S]
mov dh, byte [H]
mov dl, [drive_number]
int 0x13
jc @f
pop cx
pop bx
pop ax
add bx, word [bytes_per_sector]
inc ax
loop disk_read_sectors
ret
@@:
mov si, msg_error_sector
call bios_print
ret
C dw 0x00
H dw 0x00
S dw 0x00
fat_load_root:
mov ax, DISK_BUFFER/0x10
mov es, ax
; load root directory
mov ax, [sectors_per_FAT]
xor cx, cx
mov cl, [FAT_count]
mul cx
add ax, [reserved_sectors]
push ax
mov bx, [bytes_per_sector]
mov cl, 0x5
shr bx, cl
mov ax, [root_dir_entries]
xor dx, dx
div bx
mov cx, ax
pop ax
mov [data_start], ax
add [data_start], cx
xor bx, bx
call disk_read_sectors
ret
;; @param SI filename to search
fat_search_root:
mov cx, [root_dir_entries]
mov di, 0x0
@@:
push si
push cx
mov cx, 0xB ; name(8) + ext(3)
push di
rep cmpsb
pop di
pop cx
pop si
je .file_found
add di, 0x20
loop @b
; set carry if not found
stc
ret
.file_found:
mov ax, [es:fat_entry.start]
clc
ret
;;; @param AX cluster
;;; @param ES:BX buffer
fat_load_binary:
push ax
sub ax, 0x2
xor cx, cx
mov cl, [sectors_per_cluster]
mul cx
add ax, [data_start]
xor cx, cx
mov cl, [sectors_per_cluster]
call disk_read_sectors
pop ax
mov cx, ax
mov dx, ax
shr dx, 0x1
add cx, dx
push bx
mov bx, DISK_BUFFER
add bx, cx
mov dx, [bx]
pop bx
test ax, 0x1
jnz .odd_cluster
.even_cluster:
and dx, 0xFFF
jmp .end
.odd_cluster:
shr dx, 0x4
.end:
mov ax, dx
cmp dx, 0xFF0
jb fat_load_binary
ret
msg_error_sector db "ERROR: reading sector", CR, LF, 0
data_start dw 0x0

4
boot/common/protocol.inc Normal file
View file

@ -0,0 +1,4 @@
struct BootProtocol
{
}

View file

@ -1,4 +1,4 @@
; File: bootia32.asm
;; File: bootia32.asm
format PE DLL EFI at 10000000h
entry efimain
@ -8,9 +8,17 @@
include '../common/macro.inc'
include 'uefi.inc'
; ESP => return address
; ESP + 4 => Handle
; ESP + 8 => SysTable
;; Function: efimain
;;
;; Parameters:
;;
;; [esp+4] - handle
;; [esp+8] - <EFI_SYSTEM_TABLE>
;;
;; Returns:
;;
;; eax - efi status
;;
efimain:
mov eax, [esp+4]
mov [handle], eax

View file

@ -1,3 +1,5 @@
;; File: uefi.inc
struc BOOLEAN
{
. db ?
@ -112,6 +114,7 @@ EFI_1_02_SYSTEM_TABLE_REVISION = ((1 shl 16) or (02))
EFI_SYSTEM_TABLE_REVISION = EFI_2_90_SYSTEM_TABLE_REVISION
EFI_SPECIFICATION_VERSION = EFI_SYSTEM_TABLE_REVISION
;; Struct: EFI_SYSTEM_TABLE
struc EFI_SYSTEM_TABLE
{
.Hdr EFI_TABLE_HEADER

4
boot/intro.txt Normal file
View file

@ -0,0 +1,4 @@
File: Introduction
About: Legacy

18
boot/loader/gdt.inc Normal file
View file

@ -0,0 +1,18 @@
gdt:
; null descriptor
dd 0
dd 0
; code descriptor
dw 0xFFFF, 0x0000
db 0x00, 0x9a, 0xcf, 0x00
; data descriptor
dw 0xFFFF, 0x0000
db 0x00, 0x92, 0xcf, 0x00
.end:
gdt_ptr:
dw gdt.end - gdt - 1
dd gdt

View file

@ -15,10 +15,13 @@ multiboot_header:
_start:
cmp eax, MULTIBOOT_MAGIC
je .multiboot
je multiboot
use16
;; non multiboot process
; =========================================================================
; real mode code
; =========================================================================
push cs
pop ds
@ -31,12 +34,13 @@ _start:
; detect memory
call memory_get_map
jc .error_memory
xchg bx, bx
call video_setup
.multiboot:
jmp .hang
;cli
;lgdt [gdt_ptr]
;jmp 0x8:common32
;jmp $
.error_memory:
mov si, msg_error_memory
@ -45,14 +49,15 @@ _start:
mov si, msg_error_a20
.error:
call bios_print
.hang:
@@:
hlt
jmp $
jmp @b
include 'a20.inc'
include '../common/bios.inc'
include 'memory.inc'
include 'video.inc'
include 'gdt.inc'
msg_stage2 db "StupidOS Bootloader (Stage 1)", CR, LF, 0
msg_error_a20 db "ERROR: can't enable a20 line", CR, LF, 0
@ -62,9 +67,33 @@ msg_error_memory db "ERROR: can't detect available memory", CR, LF, 0
bi_screen_width: dw 0
bi_screen_height: dw 0
use32
; =========================================================================
; protected mode code
; =========================================================================
multiboot:
common32:
;mov bx, 0x0f01
;mov word [eax], bx
; paging
; identity map first 1MB
; map kernel to 0xC0000000
hang:
hlt
jmp $
_edata:
; BSS
rb 0x4000
align 4096
boot_page_directory:
rb 4096
boot_0_page_table:
rb 4096
boot_768_page_table:
rb 4096
_end:

7
boot/loader/mmu.inc Normal file
View file

@ -0,0 +1,7 @@
PE_PRESENT = (1 shl 0)
PE_WRITABLE = (1 shl 1)
PE_USERMODE = (1 shl 2)
PE_ACCESSED = (1 shl 5)
PE_DIRTY = (1 shl 6)

View file

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

View file

@ -1,89 +0,0 @@
---
title: pmap_bootstrap
---
flowchart TD
R_BEGIN("Begin")
R_GETMMAP["Get Memory Map entry"]
R_MMAP_LAST{"Last Item ?"}
R_END("End")
subgraph "entry to pmap_block"
C_BEGIN("Begin Proc")
C_IS_FREE{"Is
entry free?"}
C_END("End Proc")
C_OVERLAP_KERNEL{"entry
overlap
kernel?"}
C_REMOVE_KRESERVED["Remove Kernel
Memory from entry"]
C_MEMORY_SIZE_EXCEED{"Is entry size
larger than
0x0x7f80000 ?"}
C_MEMORY_SPLIT["Split entry"]
C_MEMORY_LAST["Is last ?"]
C_BEGIN-->C_IS_FREE
C_IS_FREE--No-->C_END
C_IS_FREE--Yes-->C_OVERLAP_KERNEL
C_OVERLAP_KERNEL--Yes-->C_REMOVE_KRESERVED
C_REMOVE_KRESERVED-->C_MEMORY_SIZE_EXCEED
C_OVERLAP_KERNEL--No-->C_MEMORY_SIZE_EXCEED
C_MEMORY_SIZE_EXCEED--Yes-->C_MEMORY_SPLIT
C_MEMORY_SPLIT-->C_MEMORY_LAST
C_MEMORY_LAST--Yes-->C_END
end
subgraph "create & store pmap_block"
D_BEGIN("Begin Proc")
D_BOOSTRAP_EXIST{"Bootstrap
pmap_block
exist?"}
D_SETUP_BOOTSTRAP["Setup bootstrap pmap"]
D_END("End Proc")
D_SET_HEAD_BOOTSTRAP["Set bootstrap as Head"]
D_SEARCH_FREE_PAGE["Search free page in Head->bitmap"]
D_FREE_PAGE_FOUND{"Free Page found ?"}
D_HEAD_NEXT_NULL{"Head->next == NULL?"}
D_HEAD_NEXT["Set Head to Head->next"]
D_CREATE_PMAP["Create new pmap_block
in free page"]
D_APPEND_PMAP["Append pmap_block
at end of
linked list"]
D_BEGIN-->D_BOOSTRAP_EXIST
D_BOOSTRAP_EXIST--No-->D_SETUP_BOOTSTRAP
D_SETUP_BOOTSTRAP-->D_END
D_BOOSTRAP_EXIST--Yes-->D_SET_HEAD_BOOTSTRAP
D_SET_HEAD_BOOTSTRAP-->D_SEARCH_FREE_PAGE
D_SEARCH_FREE_PAGE-->D_FREE_PAGE_FOUND
D_FREE_PAGE_FOUND--No-->D_HEAD_NEXT_NULL
D_HEAD_NEXT_NULL--No-->D_HEAD_NEXT
D_HEAD_NEXT-->D_SEARCH_FREE_PAGE
D_FREE_PAGE_FOUND--Yes-->D_CREATE_PMAP
D_CREATE_PMAP-->D_APPEND_PMAP
D_APPEND_PMAP-->D_END
end
R_BEGIN-->R_GETMMAP
R_GETMMAP-->R_MMAP_LAST
R_MMAP_LAST--Yes-->R_END
R_MMAP_LAST--No-->C_BEGIN
C_MEMORY_SIZE_EXCEED--No-->D_BEGIN
D_END--Try get next-->C_MEMORY_LAST
C_MEMORY_LAST--No-->D_BEGIN
C_END--Try get next entry-->R_MMAP_LAST
style R_BEGIN fill:#6CA300
style R_END fill:#A30000
style C_BEGIN fill:#6CA300
style C_END fill:#A30000
style D_BEGIN fill:#6CA300
style D_END fill:#A30000

Binary file not shown.

Before

Width:  |  Height:  |  Size: 93 KiB