tons more work on modularizing the code

This commit is contained in:
2024-10-05 18:55:00 -04:00
parent 5440a1ae61
commit 8f95c8f267
19 changed files with 360 additions and 296 deletions

View File

@@ -23,17 +23,37 @@
[CPU KATMAI]
jmp short init
nop
%include "cdecl16.inc" ; include calling convention macros for cdecl16
%include "entry.inc" ; defines with entry points for various stages & stack
init:
cli ; We do not want to be interrupted
xor ax, ax ; 0 AX
mov ds, ax ; Set segment registers to 0
mov es, ax ; *
mov ss, ax ; Set Stack Segment to 0
mov sp, EARLY_STACK_START
; ###############
;
; Headers/Includes/Definitions
;
; ###############
%define __STEVIA_MBR
%include "cdecl16.inc"
%include "entry.inc"
%include "config.inc"
%include "mem.inc"
%include "error_codes.inc"
%include "partition_table.inc"
; ###############
; End Section
; ###############
ALIGN 4, db 0x90
init:
cli ; We do not want to be interrupted
xor ax, ax ; 0 AX
mov ds, ax ; Set segment registers to 0
mov ss, ax ; Set Stack Segment to 0
mov sp, EARLY_STACK_START ; Setup stack
mov bp, sp ; base ptr = stack ptr
sub sp, 0x20 ; local varible space
mov ch, 0x01 ; 256 WORDs in MBR (512 bytes), 0x0100 in cx
mov si, 0x7C00 ; Current MBR Address (loaded here by BIOS)
@@ -43,16 +63,28 @@ init:
sti
jmp 0:main
nop
%include "config.inc"
%include "memory.inc"
; ###############
;
; Extra/Shared Functions
;
; ###############
%include "kmem_func.inc"
%include "partition_table.inc"
%include "errors.inc"
%include "util/error_func.inc"
; ###############
; End Section
; ###############
;
; bp - 2 : uint8_t boot_drive
; bp - 4 : uint16_t part_offset
;
ALIGN 4, db 0x90
main:
mov [boot_drive], dl ; BIOS passes drive number in DL
mov byte [bp - 2], dl ; BIOS passes drive number in DL
.check_disk:
cmp dl, 0x80
@@ -62,7 +94,7 @@ main:
.check_extentions:
xor ax, ax
mov bx, 0x55AA
mov dl, byte [boot_drive]
mov dl, byte [bp - 2]
int 0x13
jnc main.find_active
ERROR MBR_ERROR_NO_INT32E ; no extended function support
@@ -83,25 +115,30 @@ main:
mov ax, bx
sub ax, DiskSig ; leaves us with the offset relative from start of table
; this gives us an offset from the begining of the partition table
mov word [part_offset], ax ; update part_offset
mov word [bp - 4], ax ; update part_offset
.read_data:
;uint8_t read_disk_raw(size_t count, uint16_t buf_segment, uint16_t buf_offset,
; uint16_t lower_lower_lba, uint16_t upper_lower_lba)
mov eax, dword [bx + PartEntry_t.lba_start]
ror eax, 16
push ax
movzx ax, byte [bp - 2]
push ax ; drive_num
ror eax, 16
push ax
mov ax, 0x1
push ax ; count
mov ax, VBR_ENTRY
push ax
mov dword eax, dword [bx + PartEntry_t.lba_start]
push dword eax ; lba
xor ax, ax
push ax ; segment = 0
push ax ; offset = 0
mov ax, VBR_ENTRY
shr ax, 4
push ax ; segment = 7C0
; uint8_t read_stage2_raw(uint16_t buf_segment, uint16_t buf_offset,
; uint32_t lba,
; uint16_t count, uint16_t drive_num)
call read_disk_raw
add sp, 0xC
call read_vbr_raw
add sp, 0x8
jnc main.goto_vbr
ERROR MBR_ERROR_DISK_READ_ERR ; error in LBA read
@@ -120,86 +157,16 @@ main:
call kmemcpy ; copy partition table to memory
add sp, 0x6
xor ah, ah ; Set Video mode BIOS function
mov al, 0x02 ; 16 color 80x25 Text mode
int 0x10 ; Call video interrupt
mov ah, 0x05 ; Select active display page BIOS function
xor al, al ; page 0
int 0x10 ; call video interrupt
mov si, word [part_offset]
mov dl, byte [boot_drive]
mov si, word [bp - 4]
mov dl, byte [bp - 2]
jmp 0:0x7C00
; Wrapper for AH=0x42 INT13h (Extended Read)
; ###############
;
; BIOS call details
; AH = 42h
; DL = drive number
; DS:SI -> disk address packet
; BIOS Functions
;
; Return:
; CF clear if successful
; AH = 00h
; CF set on error
; AH = error code
; disk address packet's block count field set to number of blocks
; successfully transferred
;
;
; uint8_t read_disk_raw(uint16_t buf_segment, uint16_t buf_offset, uint16_t lower_lower_lba, uint16_t upper_lower_lba)
read_vbr_raw:
__CDECL16_ENTRY
.func:
mov ax, 0x10
push ax ; len = 16 bytes
xor ax, ax
push ax ; val = 0
mov ax, lba_packet
push ax ; dest = lba_packet address
call kmemset
add sp, 0x06
mov byte [lba_packet + LBAPkt_t.size], 0x10
mov word [lba_packet + LBAPkt_t.xfer_size], 0x0001
mov ax, [bp + 10]
shl eax, 16
mov ax, [bp + 8]
mov dword [lba_packet + LBAPkt_t.lower_lba], eax
mov ax, [bp + 6]
mov word [lba_packet + LBAPkt_t.offset], ax
mov ax, [bp + 4]
mov word [lba_packet + LBAPkt_t.segment], ax
mov si, lba_packet
mov ah, 0x42
mov dl, byte [boot_drive]
int 0x13
jnc .endf
ERROR MBR_ERROR_INT13h_EREAD_ERR
.endf:
__CDECL16_EXIT
ret
; #############
;
; Locals
;
; #############
boot_drive:
db 0x00
mbr_reserved1:
db 0x00
; OFFSET from BEGINING of partition table, ie. DiskSig (-6 from PartEntry1)
part_offset:
dw 0x0000
; ###############
%include 'BIOS/func/ext_read.inc'
%assign bytes_remaining (440 - ($ - $$))
%warning MBR has bytes_remaining bytes remaining for code (MAX: 440 bytes)

View File

@@ -23,11 +23,27 @@
[CPU KATMAI]
jmp short init
nop
; boot drive in dl
; active partition offset in si
; ###############
;
; Headers/Includes/Definitions
;
; ###############
%define __STEVIA_STAGE2
%include "cdecl16.inc"
%include "entry.inc"
%include "config.inc"
%include "mem.inc"
%include "error_codes.inc"
; ###############
; End Section
; ###############
ALIGN 4, db 0x90
init:
cli ; We do not want to be interrupted
@@ -37,32 +53,36 @@ init:
mov fs, ax ; *
mov gs, ax ; *
mov ss, ax ; Set Stack Segment to 0
mov ss, ax ; Set Stack Segment to 0
mov sp, EARLY_STACK_START ; Set Stack Pointer
add sp, 0x4
mov ax, 0xDEAD
push word ax
mov ax, 0xBEEF
push word ax ; mark top of stack for debuging
mov bp, sp
sub sp, 0x20 ; 32 bytes for local varibles
sti
jmp 0:main
%include "config.inc"
%include "error_codes.inc"
%include "memory.inc"
%include "kmem_func.inc"
%include "partition_table.inc"
%include "fat32/bpb.inc"
%include "fat32/fat32_structures.inc"
; ###############
;
; Extra/Shared Functions
;
; ###############
%include "kmem_func.inc"
%include "util/error_func.inc"
; ###############
; End Section
; ###############
main:
mov byte [fat32_ebpb + FAT32_ebpb_t.drive_number_8], dl
mov word [partition_offset], si
lea ax, [bp - 2]
mov [boot_drive_ptr], ax
lea ax, [bp - 4]
mov [partition_offset_ptr], ax
mov byte [bp - 2], dl ; boot_drive (probably 0x80)
mov word [bp - 4], si ; partition_offset
mov eax, dword [STAGE2_SIG]
cmp eax, 0xDEADBEEF
@@ -138,7 +158,7 @@ hcf:
;
; ###############
%include 'fat32/fat32_func_old.inc'
%include 'fat32/fat32_sys.inc'
; ###############
;
@@ -306,18 +326,13 @@ NewLine_cstr:
db StrCRLF_NUL
BootTarget_str:
db "BOOTI686BIN"
; #############
;
; Locals
;
; #############
partition_offset:
boot_drive_ptr:
db 0x00
stage2_resb_1:
db 0x00
partition_offset_ptr:
dw 0x0000
; GDT documentation below:
;
; Pr: Present bit. This must be 1 for all valid selectors.

View File

@@ -30,36 +30,59 @@ phy_ebpb_start:
; fill eBPB area with 0x00 since we skip writing this part to disk
times 54 db 0x00
; ###############
;
; Headers/Includes/Definitions
;
; ###############
%define __STEVIA_VBR
%include "cdecl16.inc"
%include "entry.inc"
%include "config.inc"
%include "mem.inc"
%include "error_codes.inc"
%include "fat32/bpb_offset_bx.inc"
; ###############
; End Section
; ###############
ALIGN 4, db 0x90
init:
cli ; We do not want to be interrupted
xor ax, ax ; 0 AX
mov ds, ax ; Set segment registers to 0
mov es, ax ; *
mov ss, ax ; Set Stack Segment to 0
mov sp, EARLY_STACK_START ; Setup stack
mov sp, EARLY_STACK_START ; Setup stack
mov bp, sp ; base ptr = stack ptr
sub sp, 0x20 ; local varible space
mov bx, VBR_ENTRY ; move Bx to the new start of the initial boot sector
sti ; all done with inital setup and relocation, reenable interupts
jmp 0:main ; fix up cs:ip just in case and jump to relocated code
nop
%include "config.inc"
%include "memory.inc"
; ###############
;
; Extra/Shared Functions
;
; ###############
%include "kmem_func.inc"
%include "partition_table.inc"
%include "errors.inc"
%include "util/error_func.inc"
%include "fat32/bpb.inc"
; ###############
; End Section
; ###############
ALIGN 4, db 0x90
main:
mov [bsDriveNumber], dl ; BIOS passes drive number in DL
mov [partition_offset], si ; save passed partition entry offset
mov byte [bp - 2], dl ; boot_drive
mov [bp - 4], si ; part_offset
.check_FAT_size: ; we only support a very specific setup of FAT32
cmp dword [bsSectorHuge], 0 ; SectorsHuge will not be set if FAT12/16
@@ -69,24 +92,39 @@ main:
; read sectors 1-63 to stage2 entry point
.load_stage2:
xor ax, ax
push ax ; upper_lower_lba = 0
mov ax, (fat32_bpb_SIZE + fat32_ebpb_SIZE) ; size in byte
push ax
mov ax, (phy_bpb_start - 0x3) ; start of bpb - 0x3 for the jump short main at the start
push ax
mov ax, fat32_bpb ; defined in memory.inc, destination
push ax
call kmemcpy ; copy bpb to memory
add sp, 0x6
mov ax , 1
push ax ; lower_lower_lba = 1
mov bx, fat32_bpb ; bx now points to aligned memory structure
movzx ax, byte [bp - 2]
push ax ; drive_num
mov ax, STAGE2_SECTOR_COUNT
push ax ; count
mov dword eax, 0x1
push dword eax ; lba
xor ax, ax
push ax ; offset = 0
push ax ; offset = 0
; 07E0:0
; 07E0:0 = 0x00007e00
mov ax, STAGE2_ENTRY
shr ax, 4
push ax ; segment = 7E0
push ax ; segment = 7E0
;uint8_t read_stage2_raw(uint16_t buf_segment, uint16_t buf_offset,
; uint16_t lower_lower_lba, uint16_t upper_lower_lba)
call read_stage2_raw
add sp, 0x8
; uint8_t read_stage2_raw(uint16_t buf_segment, uint16_t buf_offset,
; uint32_t lba,
; uint16_t count, uint16_t drive_num)
call read_disk_raw
add sp, 0xC
.check_sig:
mov ax, 0x7E0
@@ -97,97 +135,16 @@ main:
ERROR VBR_ERROR_NO_SIGNATURE ; no signature present in stage2
.sig_ok:
mov ax, fat32_bpb_SIZE ; size in byte
push ax
mov ax, phy_bpb_start ; start of bpb
push ax
mov ax, fat32_bpb ; defined in memory.inc, destination
push ax
call kmemcpy ; copy bpb to memory
add sp, 0x6
mov ax, fat32_ebpb_SIZE ; 72 bytes of data
push ax
mov ax, phy_ebpb_start ; start of ebpb
push ax
mov ax, fat32_ebpb ; defined in memory.inc, destination
push ax
call kmemcpy ; copy ebpb to memory
add sp, 0x6
mov si, [partition_offset]
mov dl, [bsDriveNumber]
mov si, [bp - 4]
mov dx, [bp - 2]
jmp 0:0x7E00
stop:
hlt
jmp short stop
; Wrapper for AH=0x42 INT13h (Extended Read)
; ###############
;
; BIOS call details
; AH = 42h
; DL = drive number
; DS:SI -> disk address packet
; Required BIOS functions
;
; Return:
; CF clear if successful
; AH = 00h
; CF set on error
; AH = error code
; disk address packet's block count field set to number of blocks
; successfully transferred
;
;
; uint8_t read_disk_raw(uint16_t buf_segment, uint16_t buf_offset,
; uint16_t lower_lower_lba, uint16_t upper_lower_lba)
read_stage2_raw:
__CDECL16_ENTRY
.func:
mov ax, 0x10
push ax ; len = 16 bytes
xor ax, ax
push ax ; val = 0
mov ax, lba_packet
push ax ; dest = lba_packet address
call kmemset
add sp, 0x06
mov byte [lba_packet + LBAPkt_t.size], 0x10
mov word [lba_packet + LBAPkt_t.xfer_size], STAGE2_SECTOR_COUNT
mov ax, [bp + 10]
shl eax, 16
mov ax, [bp + 8]
mov dword [lba_packet + LBAPkt_t.lower_lba], eax
mov ax, [bp + 6]
mov word [lba_packet + LBAPkt_t.offset], ax
mov ax, [bp + 4]
mov word [lba_packet + LBAPkt_t.segment], ax
mov si, lba_packet
mov ah, 0x42
mov dl, byte [bsDriveNumber]
int 0x13
jnc .endf
ERROR VBR_ERROR_DISK_READ_ERR
.endf:
__CDECL16_EXIT
ret
; Data
; #############
;
; Locals
;
; #############
; offset from the begining of sector 0 to the active partition.
partition_offset:
dw 0x0000
; ###############
%include 'BIOS/func/ext_read.inc'
%assign bytes_remaining (420 - ($ - $$))
%warning VBR has bytes_remaining bytes remaining for code (MAX: 420 bytes)