load and jump to stage 3 separately from stage 2

This commit is contained in:
pantonshire committed 2025-04-21 10:53:34 +01:00
1 parent da529e8426
commit bb60e1c8ed
11 files changed
+117 -238

No files matched your search

+69
View File
@@ -0,0 +1,69 @@
[bits 16]
%include "fn.s"
%include "layout.s"
%include "globals.s"
%include "s2_fns.s"
extern s2_data.s3_bin_offset_sectors
extern s2_data.s3_bin_len_sectors
load_s3:
fnstart
push ebx
; Calculate the stage 3 start LBA.
mov ax, [REAL_GLOBALS + STAGE_2_START_LBA]
add ax, [s2_data.s3_bin_offset_sectors]
jc .fail
; Stage 3 should not overlap with stage 2.
cmp word [REAL_GLOBALS + STAGE_2_END_LBA], ax
jae .fail
xor ebx, ebx
; There must be at least one sector to load.
mov bx, [s2_data.s3_bin_len_sectors]
or bx, bx
jz .fail
; Calculate the end LBA (inclusive).
dec bx
add bx, ax
jc .fail
; Check stage 3 is entirely inside the partition.
cmp dword [REAL_GLOBALS + LOADER_PART_END_LBA], ebx
jb .fail
push ax ; Current LBA
push bx ; s3 end LBA
mov ebx, S3_LOAD_ADDR ; Current load address
.load_loop:
mov ax, [bp - 0x06] ; Load current LBA
cmp word [bp - 0x08], ax ; Compare to s3 end LBA
jb .load_done
mov ecx, ebx
call read_sector
jc .fail
add ebx, 512
inc word [bp - 0x06]
jmp .load_loop
.load_done:
add sp, 4
pop ebx
clc
fnret
.fail:
pop ebx
stc
fnret
global load_s3
+5 -8
View File
@@ -6,6 +6,7 @@
extern test_a20
extern enable_a20_intel_8042
extern load_s3
s2_main:
call test_a20
@@ -24,6 +25,9 @@ s2_main:
jmp panic_simple
.a20_enabled:
call load_s3
jc panic_simple
mov ax, 0x0003
int 0x10
@@ -63,19 +67,12 @@ s2_main:
mov ebp, REAL_STACK_BASE
mov esp, ebp
;jmp _start
mov eax, 0xcafebabe
jmp S3_LOAD_ADDR
.halt:
hlt
jmp .halt
; panic_simple_32:
; mov word [0xb8000], 0x4f21
; .halt:
; hlt
; jmp .halt
global s2_main
+10 -1
View File
@@ -2,7 +2,7 @@
%include "fn.s"
%include "layout.s"
%include "s1_vars.s"
%include "globals.s"
extern s2_bin_len
extern s2_bin_sectors
@@ -25,6 +25,10 @@ s2_data:
.padding:
times (S2_DATA_LEN - 4) db 0xf4
global s2_data
global s2_data.s3_bin_offset_sectors
global s2_data.s3_bin_len_sectors
%if ($ - $$) != S2_DATA_LEN
%error "incorrect prelude data size"
%endif
@@ -84,6 +88,11 @@ prelude:
cmp ebx, ecx
ja panic_simple
; Save partition / s2 extents for later.
mov [REAL_GLOBALS + LOADER_PART_END_LBA], ecx
mov [REAL_GLOBALS + STAGE_2_START_LBA], ax
mov [REAL_GLOBALS + STAGE_2_END_LBA], bx
.end_lba_ok:
; The first sector has already been loaded (we're running it right now!) so increment the