:: commit b16f63458d09073aec2ec178014160bd4a91ae04

mintsuki <mintsuki@protonmail.com> — 2020-09-06 03:15

parents: 45aaa72e85

Shuffle around memory layout of bootloader

diff --git a/src/bootsect/bootsect.asm b/src/bootsect/bootsect.asm
index b7404f15..6430c6b6 100644
--- a/src/bootsect/bootsect.asm
+++ b/src/bootsect/bootsect.asm
@@ -15,7 +15,7 @@ start:
     mov fs, ax
     mov gs, ax
     mov ss, ax
-    mov sp, 0xfff0
+    mov esp, 0x4000
     sti
 
     ; Some BIOSes don't pass the correct boot drive number,
@@ -39,12 +39,12 @@ start:
     mov si, LoadingMsg
     call simple_print
 
-    ; ****************** Load stage 2 ******************
+    ; ****************** Load stage 1.5 ******************
 
-    mov si, Stage2Msg
+    mov si, Stage15Msg
     call simple_print
 
-    mov eax, dword [stage2_sector]
+    mov eax, dword [stage15_sector]
     mov ebx, 0x7e00
     mov ecx, 1
     call read_sectors
@@ -73,7 +73,7 @@ halt:
 ; Data
 
 LoadingMsg db 0x0D, 0x0A, 'Limine', 0x0D, 0x0A, 0x0A, 0x00
-Stage2Msg db 'Loading stage2...', 0x00
+Stage15Msg db 'Loading stage 1.5...', 0x00
 ErrReadDiskMsg db 0x0D, 0x0A, 'Disk read error, system halted', 0x00
 ErrEnableA20Msg db 0x0D, 0x0A, 'A20 enable error, system halted', 0x00
 DoneMsg db '  DONE', 0x0D, 0x0A, 0x00
@@ -87,7 +87,7 @@ times 6 db 0
 %include 'disk.inc'
 
 times 0x1b0-($-$$) db 0
-stage2_sector: dd 1
+stage15_sector: dd 1
 
 times 0x1b8-($-$$) db 0
 times 510-($-$$) db 0
@@ -97,9 +97,9 @@ dw 0xaa55
 
 stage15:
     push es
-    push 0x6000
+    push 0x7000
     pop es
-    mov eax, dword [stage2_sector]
+    mov eax, dword [stage15_sector]
     inc eax
     xor ebx, ebx
     mov ecx, 62
@@ -132,9 +132,9 @@ stage15:
     push edx
 
     push stage2.size
-    push (stage2 - 0x8000) + 0x60000
+    push (stage2 - 0x8000) + 0x70000
 
-    call 0x60000
+    call 0x70000
 
 bits 16
 %include 'a20_enabler.inc'
diff --git a/src/bootsect/disk.inc b/src/bootsect/disk.inc
index c278648f..ecb2acaa 100644
--- a/src/bootsect/disk.inc
+++ b/src/bootsect/disk.inc
@@ -7,41 +7,44 @@
 ; DL = Drive number
 ; ES = Buffer segment
 ; BX = Buffer offset
+; CX = Sectors count
 
 ; OUT:
 ; Carry if error
 
-read_sector:
-    push eax
-    push ebx
-    push ecx
-    push edx
-    push esi
-    push edi
+read_sectors:
+    pusha
 
     push es
     pop word [.target_segment]
     mov word [.target_offset], bx
     mov dword [.lba_address_low], eax
 
-    xor esi, esi
-    mov si, .da_struct
+    mov word [.countdown], cx
+
+  .loop:
+    mov esi, .da_struct
     mov ah, 0x42
 
     clc
     int 0x13
+    jc .done
+
+    add word [.target_offset], 512
+    inc dword [.lba_address_low]
+
+    dec word [.countdown]
+    jnz .loop
 
   .done:
-    pop edi
-    pop esi
-    pop edx
-    pop ecx
-    pop ebx
-    pop eax
+    popa
     ret
 
+align 2
+  .countdown: dw 0
+
 align 4
-.da_struct:
+  .da_struct:
     .packet_size        db  16
     .unused             db  0
     .count              dw  1
@@ -49,71 +52,3 @@ align 4
     .target_segment     dw  0
     .lba_address_low    dd  0
     .lba_address_high   dd  0
-
-; ********************************************
-;     Reads multiple LBA addressed sectors
-; ********************************************
-
-; IN:
-; EAX = LBA starting sector
-; DL = Drive number
-; ES = Buffer segment
-; EBX = Buffer offset
-; ECX = Sectors count
-
-; OUT:
-; Carry if error
-
-%define TEMP_BUFFER_SEG 0x7000
-%define BYTES_PER_SECT  512
-
-read_sectors:
-    push eax									; Save GPRs
-    push ebx
-    push ecx
-    push edx
-    push esi
-    push edi
-
-  .loop:
-    push es
-    push ebx
-
-    mov bx, TEMP_BUFFER_SEG
-    mov es, bx
-    xor bx, bx
-
-    call read_sector
-
-    pop ebx
-    pop es
-
-    jc .done
-
-    push ds
-
-    mov si, TEMP_BUFFER_SEG
-    mov ds, si
-    mov edi, ebx
-    xor esi, esi
-
-    push ecx
-    mov ecx, BYTES_PER_SECT
-    a32 o32 rep movsb
-    pop ecx
-
-    pop ds
-
-    inc eax
-    add ebx, BYTES_PER_SECT
-
-    loop .loop
-
-  .done:
-    pop edi
-    pop esi
-    pop edx
-    pop ecx
-    pop ebx
-    pop eax
-    ret
diff --git a/src/bootsect/gdt.inc b/src/bootsect/gdt.inc
index 9df929b1..b6a63439 100644
--- a/src/bootsect/gdt.inc
+++ b/src/bootsect/gdt.inc
@@ -2,7 +2,7 @@ load_gdt:
     pusha
     push es
     push ds
-    push 0x7000
+    push 0x7ff0
     pop es
     xor di, di
     push 0
@@ -19,7 +19,7 @@ load_gdt:
 GDT:
 
 dw .GDTEnd - .GDTStart - 1	; GDT size
-dd 0x70000				; GDT start
+dd 0x7ff00				; GDT start
 
 .GDTStart:
 
diff --git a/src/decompressor/linker.ld b/src/decompressor/linker.ld
index baf1f0de..12b58ef4 100644
--- a/src/decompressor/linker.ld
+++ b/src/decompressor/linker.ld
@@ -3,7 +3,7 @@ ENTRY(main)
 
 SECTIONS
 {
-    . = 0x60000;
+    . = 0x70000;
 
     .text : {
         KEEP(*(.entry*))
diff --git a/src/decompressor/main.c b/src/decompressor/main.c
index e6bc3841..c097769a 100644
--- a/src/decompressor/main.c
+++ b/src/decompressor/main.c
@@ -22,9 +22,8 @@ ASM_BASIC(
 
 __attribute__((noreturn))
 void main(uint8_t *compressed_stage2, size_t stage2_size, uint8_t boot_drive) {
-    // The decompressor should decompress compressed_stage2 to address 0x500.
-    // For now, just copy it over as it is not compressed. TODO: implement decompressor.
-    volatile uint8_t *dest = (volatile uint8_t *)0x500;
+    // The decompressor should decompress compressed_stage2 to address 0x4000.
+    volatile uint8_t *dest = (volatile uint8_t *)0x4000;
 
     tinf_gzip_uncompress(dest, compressed_stage2, stage2_size);
 
diff --git a/src/lib/blib.c b/src/lib/blib.c
index 98ef721a..6cbd9ef9 100644
--- a/src/lib/blib.c
+++ b/src/lib/blib.c
@@ -45,8 +45,9 @@ __attribute__((noreturn)) void panic(const char *fmt, ...) {
     }
 }
 
-static size_t bump_allocator_base = 0x10000;
-#define BUMP_ALLOCATOR_LIMIT ((size_t)0x70000)
+extern symbol bss_end;
+static size_t bump_allocator_base = (size_t)bss_end;
+#define BUMP_ALLOCATOR_LIMIT ((size_t)0x7ff00)
 
 void brewind(size_t count) {
     bump_allocator_base -= count;
diff --git a/src/lib/real.asm b/src/lib/real.asm
index 5b815aa6..ee98ced2 100644
--- a/src/lib/real.asm
+++ b/src/lib/real.asm
@@ -1,4 +1,4 @@
-section .text
+section .realmode
 
 global rm_int
 rm_int:
diff --git a/src/lib/sleep.asm b/src/lib/sleep.asm
index 9f0bcef5..c0463e32 100644
--- a/src/lib/sleep.asm
+++ b/src/lib/sleep.asm
@@ -1,4 +1,4 @@
-section .text
+section .realmode
 
 int_08_ticks_counter: dd 0
 
diff --git a/src/linker.ld b/src/linker.ld
index f9f0057f..7f7ddf0d 100644
--- a/src/linker.ld
+++ b/src/linker.ld
@@ -3,10 +3,11 @@ ENTRY(main)
 
 SECTIONS
 {
-    . = 0x500;
+    . = 0x4000;
 
     .text : {
         KEEP(*(.entry*))
+        KEEP(*(.realmode*))
         *(.text*)
     }
 
tab: 248 wrap: offon