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

View file

@ -1,9 +1,18 @@
bios_print: ;; File: bios.inc
lodsb
or al, al
jz @f ;; Function: bios_print
mov ah, 0x0E ;;
int 0x10 ;; Parameters:
jmp bios_print ;;
@@: ;; si - null-terminated string to print
ret ;;
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_READ_ONLY = 0x01
ATTR_HIDDEN = 0x02 ATTR_HIDDEN = 0x02
ATTR_SYSTEM = 0x04 ATTR_SYSTEM = 0x04
ATTR_VOLUME_ID = 0x08 ATTR_VOLUME_ID = 0x08
ATTR_DIRECTORY = 0x10 ATTR_DIRECTORY = 0x10
ATTR_ARCHIVE = 0x20 ATTR_ARCHIVE = 0x20
struc fat_entry struc fat_entry
{ {
.name db 8 dup ? .name db 8 dup ?
.ext db 3 dup ? .ext db 3 dup ?
.attrs db ? .attrs db ?
.reserved dw ? .reserved dw ?
.creation_time dw ? .creation_time dw ?
.creation_date dw ? .creation_date dw ?
.access_date dw ? .access_date dw ?
.reserved2 dw ? .reserved2 dw ?
.mod_time dw ? .mod_time dw ?
.mod_date dw ? .mod_date dw ?
.start dw ? .start dw ?
.size dd ? .size dd ?
} }
virtual at di virtual at di
fat_entry fat_entry fat_entry fat_entry
end virtual end virtual
; CHS to LBA ; CHS to LBA
; LBA = (C * HPC + H) * SPT + (S - 1) ; LBA = (C * HPC + H) * SPT + (S - 1)
;;; Read sectors from disk to buffer ;;; Read sectors from disk to buffer
;;; ;;;
;;; @param AX LBA starting sector ;;; @param AX LBA starting sector
;;; @param CX sector count ;;; @param CX sector count
;;; @param ES:BX buffer ;;; @param ES:BX buffer
;;; ;;;
disk_read_sectors: disk_read_sectors:
; https://en.wikipedia.org/wiki/Logical_block_addressing ; https://en.wikipedia.org/wiki/Logical_block_addressing
; convert LBA to CHS ; convert LBA to CHS
; HPC = Head per Cluster ; HPC = Head per Cluster
; SPT = Sector per Track ; SPT = Sector per Track
; S = (LBA % SPT) + 1 ; S = (LBA % SPT) + 1
push ax push ax
push bx push bx
push cx push cx
xor dx, dx xor dx, dx
div word [sectors_per_track] div word [sectors_per_track]
inc dx inc dx
mov [S], dx mov [S], dx
; H = (LBA / SPT) % HPC ; H = (LBA / SPT) % HPC
; C = LBA / (HPC * SPT) ; C = LBA / (HPC * SPT)
xor dx, dx xor dx, dx
div word [heads_per_cylinder] div word [heads_per_cylinder]
mov [C], ax mov [C], ax
mov [H], dx mov [H], dx
; read sectors ; read sectors
mov ah, 0x2 mov ah, 0x2
mov al, 0x1 mov al, 0x1
mov ch, byte [C] mov ch, byte [C]
mov cl, byte [S] mov cl, byte [S]
mov dh, byte [H] mov dh, byte [H]
mov dl, [drive_number] mov dl, [drive_number]
int 0x13 int 0x13
jc @f jc @f
pop cx pop cx
pop bx pop bx
pop ax pop ax
add bx, word [bytes_per_sector] add bx, word [bytes_per_sector]
inc ax inc ax
loop disk_read_sectors loop disk_read_sectors
ret ret
@@: @@:
mov si, msg_error_sector mov si, msg_error_sector
call bios_print call bios_print
ret ret
C dw 0x00 C dw 0x00
H dw 0x00 H dw 0x00
S dw 0x00 S dw 0x00
fat_load_root: fat_load_root:
mov ax, DISK_BUFFER/0x10 mov ax, DISK_BUFFER/0x10
mov es, ax mov es, ax
; load root directory ; load root directory
mov ax, [sectors_per_FAT] mov ax, [sectors_per_FAT]
xor cx, cx xor cx, cx
mov cl, [FAT_count] mov cl, [FAT_count]
mul cx mul cx
add ax, [reserved_sectors] add ax, [reserved_sectors]
push ax push ax
mov bx, [bytes_per_sector] mov bx, [bytes_per_sector]
mov cl, 0x5 mov cl, 0x5
shr bx, cl shr bx, cl
mov ax, [root_dir_entries] mov ax, [root_dir_entries]
xor dx, dx xor dx, dx
div bx div bx
mov cx, ax mov cx, ax
pop ax pop ax
mov [data_start], ax mov [data_start], ax
add [data_start], cx add [data_start], cx
xor bx, bx xor bx, bx
call disk_read_sectors call disk_read_sectors
ret ret
;; @param SI filename to search ;; @param SI filename to search
fat_search_root: fat_search_root:
mov cx, [root_dir_entries] mov cx, [root_dir_entries]
mov di, 0x0 mov di, 0x0
@@: @@:
push si push si
push cx push cx
mov cx, 0xB ; name(8) + ext(3) mov cx, 0xB ; name(8) + ext(3)
push di push di
rep cmpsb rep cmpsb
pop di pop di
pop cx pop cx
pop si pop si
je .file_found je .file_found
add di, 0x20 add di, 0x20
loop @b loop @b
; set carry if not found ; set carry if not found
stc stc
ret ret
.file_found: .file_found:
mov ax, [es:fat_entry.start] mov ax, [es:fat_entry.start]
clc clc
ret ret
;;; @param AX cluster ;;; @param AX cluster
;;; @param ES:BX buffer ;;; @param ES:BX buffer
fat_load_binary: fat_load_binary:
push ax push ax
sub ax, 0x2 sub ax, 0x2
xor cx, cx xor cx, cx
mov cl, [sectors_per_cluster] mov cl, [sectors_per_cluster]
mul cx mul cx
add ax, [data_start] add ax, [data_start]
xor cx, cx xor cx, cx
mov cl, [sectors_per_cluster] mov cl, [sectors_per_cluster]
call disk_read_sectors call disk_read_sectors
pop ax pop ax
mov cx, ax mov cx, ax
mov dx, ax mov dx, ax
shr dx, 0x1 shr dx, 0x1
add cx, dx add cx, dx
push bx push bx
mov bx, DISK_BUFFER mov bx, DISK_BUFFER
add bx, cx add bx, cx
mov dx, [bx] mov dx, [bx]
pop bx pop bx
test ax, 0x1 test ax, 0x1
jnz .odd_cluster jnz .odd_cluster
.even_cluster: .even_cluster:
and dx, 0xFFF and dx, 0xFFF
jmp .end jmp .end
.odd_cluster: .odd_cluster:
shr dx, 0x4 shr dx, 0x4
.end: .end:
mov ax, dx mov ax, dx
cmp dx, 0xFF0 cmp dx, 0xFF0
jb fat_load_binary jb fat_load_binary
ret ret
msg_error_sector db "ERROR: reading sector", CR, LF, 0 msg_error_sector db "ERROR: reading sector", CR, LF, 0
data_start dw 0x0 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 format PE DLL EFI at 10000000h
entry efimain entry efimain
@ -8,9 +8,17 @@
include '../common/macro.inc' include '../common/macro.inc'
include 'uefi.inc' include 'uefi.inc'
; ESP => return address ;; Function: efimain
; ESP + 4 => Handle ;;
; ESP + 8 => SysTable ;; Parameters:
;;
;; [esp+4] - handle
;; [esp+8] - <EFI_SYSTEM_TABLE>
;;
;; Returns:
;;
;; eax - efi status
;;
efimain: efimain:
mov eax, [esp+4] mov eax, [esp+4]
mov [handle], eax mov [handle], eax

View file

@ -1,3 +1,5 @@
;; File: uefi.inc
struc BOOLEAN struc BOOLEAN
{ {
. db ? . 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_SYSTEM_TABLE_REVISION = EFI_2_90_SYSTEM_TABLE_REVISION
EFI_SPECIFICATION_VERSION = EFI_SYSTEM_TABLE_REVISION EFI_SPECIFICATION_VERSION = EFI_SYSTEM_TABLE_REVISION
;; Struct: EFI_SYSTEM_TABLE
struc EFI_SYSTEM_TABLE struc EFI_SYSTEM_TABLE
{ {
.Hdr EFI_TABLE_HEADER .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: _start:
cmp eax, MULTIBOOT_MAGIC cmp eax, MULTIBOOT_MAGIC
je .multiboot je multiboot
use16 use16
;; non multiboot process
; =========================================================================
; real mode code
; =========================================================================
push cs push cs
pop ds pop ds
@ -31,12 +34,13 @@ _start:
; detect memory ; detect memory
call memory_get_map call memory_get_map
jc .error_memory jc .error_memory
xchg bx, bx
call video_setup call video_setup
.multiboot: ;cli
jmp .hang ;lgdt [gdt_ptr]
;jmp 0x8:common32
;jmp $
.error_memory: .error_memory:
mov si, msg_error_memory mov si, msg_error_memory
@ -45,14 +49,15 @@ _start:
mov si, msg_error_a20 mov si, msg_error_a20
.error: .error:
call bios_print call bios_print
.hang: @@:
hlt hlt
jmp $ jmp @b
include 'a20.inc' include 'a20.inc'
include '../common/bios.inc' include '../common/bios.inc'
include 'memory.inc' include 'memory.inc'
include 'video.inc' include 'video.inc'
include 'gdt.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
@ -62,9 +67,33 @@ 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
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: _edata:
; BSS align 4096
rb 0x4000 boot_page_directory:
rb 4096
boot_0_page_table:
rb 4096
boot_768_page_table:
rb 4096
_end: _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: video_setup:
ret 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