:: commit 5e023bce3d827dfeacab2af7c12cc75eb232f0c5

mintsuki <mintsuki@protonmail.com> — 2021-09-21 12:39

parents: b29a513e6c

stage1: Misc improvements

diff --git a/stage1/cd/bootsect.asm b/stage1/cd/bootsect.asm
index 083d2e35..47f4bfc6 100644
--- a/stage1/cd/bootsect.asm
+++ b/stage1/cd/bootsect.asm
@@ -1,9 +1,5 @@
-BITS 16
-ORG 0x7C00
-
-%define STAGE2_LOCATION       0x60000
-%define DECOMPRESSOR_LOCATION 0x70000
-%define BOOT_FROM_CD 2
+org 0x7c00
+bits 16
 
 jmp skip_bpb
 nop
@@ -25,42 +21,31 @@ skip_bpb:
     cld
     jmp 0x0000:.initialise_cs
   .initialise_cs:
-    xor ax, ax
-    mov ds, ax
-    mov es, ax
-    mov ss, ax
-    mov sp, 0x7C00
+    xor si, si
+    mov ds, si
+    mov es, si
+    mov ss, si
+    mov sp, 0x7c00
     sti
 
     ; int 13h?
     mov ah, 0x41
-    mov bx, 0x55AA
+    mov bx, 0x55aa
     int 0x13
-    jc err
-    cmp bx, 0xAA55
-    jne err
-
-    mov esp, 0x7C00
+    jc err.0
+    cmp bx, 0xaa55
+    jne err.1
 
     ; --- Load the decompressor ---
     mov eax, dword [bi_boot_LBA]
-    add eax, DEC_LBA_OFFSET
-    mov ecx, DEC_LBA_COUNT
+    add eax, 1
+    mov ecx, stage2.fullsize / 2048
     ; DECOMPRESSOR_LOCATION = 0x70000 = 0x7000:0x0000
-    mov si, 0x7000
-    xor di, di
+    push 0x7000
+    pop es
+    xor bx, bx
     call read_2k_sectors
-    jc err
-
-    ; --- Load the stage2.bin.gz ---
-    mov eax, dword [bi_boot_LBA]
-    add eax, STAGE2_LBA_OFFSET
-    mov ecx, STAGE2_LBA_COUNT
-    ; STAGE2_LOCATION = 0x60000 = 0x6000:0x0000
-    mov si, 0x6000
-    xor di, di
-    call read_2k_sectors
-    jc err
+    jc err.2
 
     ; Enable GDT
     lgdt [gdt]
@@ -72,13 +57,25 @@ skip_bpb:
     jmp 0x08:pmode
 
 err:
-    hlt
-    jmp err
+  .2:
+    inc si
+  .1:
+    inc si
+  .0:
+    add si, '0' | (0x4f << 8)
+
+    push 0xb800
+    pop es
+    mov word [es:0], si
+
+    sti
+    .h: hlt
+    jmp .h
 
 %include 'read_2k_sectors.asm'
 %include '../gdt.asm'
 
-BITS 32
+bits 32
 pmode:
     mov eax, 0x10
     mov ds, ax
@@ -88,27 +85,22 @@ pmode:
     mov ss, ax
 
     ; Time to handle control over to the decompressor
-    push BOOT_FROM_CD
-    and edx, 0xFF
+    push 2
+    and edx, 0xff
     push edx  ; Boot drive
-    push STAGE2_SIZE
-    push STAGE2_LOCATION
-    call DECOMPRESSOR_LOCATION
-    hlt
-
-%define FILEPOS ($-$$)
-%define UPPER2K ((FILEPOS+2047) & ~2047)
-%define ALIGN2K times UPPER2K - FILEPOS db 0
+    push stage2.size
+    push (stage2 - decompressor) + 0x70000
+    call 0x70000
 
 ; Align stage2 to 2K ON DISK
-ALIGN2K
-DEC_LBA_OFFSET equ ($-$$)/2048
+times 2048-($-$$) db 0
+decompressor:
 incbin '../../build/decompressor/decompressor.bin'
 
-ALIGN2K
-STAGE2_START equ $-$$
-STAGE2_LBA_OFFSET equ STAGE2_START/2048
-DEC_LBA_COUNT equ STAGE2_LBA_OFFSET - DEC_LBA_OFFSET
+align 16
+stage2:
 incbin '../../build/stage23-bios/stage2.bin.gz'
-STAGE2_SIZE equ ($-$$) - STAGE2_START
-STAGE2_LBA_COUNT equ (2047 + $-$$)/2048
+.size: equ $ - stage2
+
+times ((($-$$)+2047) & ~2047)-($-$$) db 0
+.fullsize: equ $ - decompressor
diff --git a/stage1/cd/read_2k_sectors.asm b/stage1/cd/read_2k_sectors.asm
index 7ebd0ba5..14090fe7 100644
--- a/stage1/cd/read_2k_sectors.asm
+++ b/stage1/cd/read_2k_sectors.asm
@@ -1,4 +1,4 @@
-BITS 16
+bits 16
 
 ; --- Read sectors from disk ---
 ; IN:
@@ -6,8 +6,8 @@ BITS 16
 ; cx <- number of 2k sectors
 ; dl <- drive number
 ; ds <- ZERO
-; di <- buffer offset
-; si <- buffer segment
+; bx <- buffer offset
+; es <- buffer segment
 
 ; OUT:
 ; Carry if error
@@ -25,8 +25,8 @@ read_2k_sectors:
     pusha
     mov dword [dapack_LBA], eax
     mov word  [dapack_nblocks], cx
-    mov word  [dapack_offset], di
-    mov word  [dapack_segment], si
+    mov word  [dapack_offset], bx
+    mov word  [dapack_segment], es
 
     mov ah, 0x42
     mov si, dapack
diff --git a/stage1/hdd/bootsect.asm b/stage1/hdd/bootsect.asm
index 23e55436..5a341aef 100644
--- a/stage1/hdd/bootsect.asm
+++ b/stage1/hdd/bootsect.asm
@@ -16,10 +16,10 @@ start:
     cld
     jmp 0x0000:.initialise_cs
   .initialise_cs:
-    xor bx, bx
-    mov ds, bx
-    mov es, bx
-    mov ss, bx
+    xor si, si
+    mov ds, si
+    mov es, si
+    mov ss, si
     mov sp, 0x7c00
     sti
 
@@ -27,25 +27,20 @@ start:
     ; So if the value the BIOS passed is <0x80, just assume it has passed
     ; an incorrect value.
     cmp dl, 0x80
-    jb floppy_err
+    jb err.0
     ; Values above 0x8f are dubious so we assume we weren't booted properly
     ; for those either
     cmp dl, 0x8f
-    ja hdd_err
+    ja err.1
 
   .continue:
     ; Make sure int 13h extensions are supported
     mov ah, 0x41
     mov bx, 0x55aa
     int 0x13
-    jc err
+    jc err.2
     cmp bx, 0xaa55
-    jne err
-
-    ; If int 13h extensions are supported, then we are definitely running on
-    ; a 386+. We have no idea whether the upper 16 bits of esp are cleared, so
-    ; make sure that is the case now.
-    mov esp, 0x7c00
+    jne err.3
 
     push 0x7000
     pop es
@@ -56,13 +51,13 @@ start:
     xor ecx, ecx
     mov cx, word [di-4]
     call read_sectors
-    jc err
+    jc err.4
     mov eax, dword [di+8]
     mov ebp, dword [di+12]
     add bx, cx
     mov cx, word [di-2]
     call read_sectors
-    jc err
+    jc err.5
 
     lgdt [gdt]
 
@@ -82,20 +77,27 @@ times 6 db 0
 %include '../gdt.asm'
 
 err:
+  .5:
+    inc si
+  .4:
+    inc si
+  .3:
+    inc si
+  .2:
+    inc si
+  .1:
+    inc si
+  .0:
+    add si, '0' | (0x4f << 8)
+
     push 0xb800
     pop es
-    mov dword [es:0], eax
+    mov word [es:0], si
+
+    sti
     .h: hlt
     jmp .h
 
-floppy_err:
-    mov eax, 'F ! '
-    jmp err
-
-hdd_err:
-    mov eax, 'H ! '
-    jmp err
-
 bits 32
 vector:
     mov eax, 0x10
diff --git a/stage1/pxe/bootsect.asm b/stage1/pxe/bootsect.asm
index a2f8e638..f0d94be4 100644
--- a/stage1/pxe/bootsect.asm
+++ b/stage1/pxe/bootsect.asm
@@ -2,6 +2,8 @@ org 0x7c00
 bits 16
 
 start:
+    cli
+    cld
     jmp 0x0000:.initialise_cs
   .initialise_cs:
     xor ax, ax
@@ -9,10 +11,8 @@ start:
     mov es, ax
     mov ss, ax
     mov sp, 0x7c00
-    sti
-    lgdt [gdt]
 
-    cli
+    lgdt [gdt]
 
     mov eax, cr0
     bts ax, 0
@@ -21,7 +21,7 @@ start:
     jmp 0x08:.mode32
     bits 32
   .mode32:
-    mov ax, 0x10
+    mov eax, 0x10
     mov ds, ax
     mov es, ax
     mov fs, ax
@@ -42,12 +42,6 @@ start:
 
     call 0x70000
 
-bits 16
-
-err:
-    hlt
-    jmp err
-
 ; Includes
 
 %include '../gdt.asm'
tab: 248 wrap: offon