From 5138270e466cf2dd21bf0aa6356672fdcc85fee6 Mon Sep 17 00:00:00 2001
From: =?utf8?q?Duje=20Mihanovi=C4=87?= <duje.mihanovic@skole.hr>
Date: Fri, 6 May 2022 19:33:45 +0200
Subject: [PATCH] Rehaul the memory map

This was done when I realized that not enough space is allocated to load
clusters. The current map is as follows (all numbers in hex):

* 600-800: MBR
* ee00-fe00: stack
* fe00-10000: VBR
* 800-1800: temporary FAT sector
* 10000-18000: temporary cluster
* 1800-fe00: LOADER.BIN
---
 boot/x86/fat32/fat32.s   | 4 ++--
 boot/x86/mbr.s           | 8 ++++----
 boot/x86/stage3/loader.s | 8 ++++----
 boot/x86/vbr-fat32.s     | 6 +++---
 4 files changed, 13 insertions(+), 13 deletions(-)

diff --git a/boot/x86/fat32/fat32.s b/boot/x86/fat32/fat32.s
index df5dd89..41d5688 100644
--- a/boot/x86/fat32/fat32.s
+++ b/boot/x86/fat32/fat32.s
@@ -79,12 +79,12 @@ read_cluster:
 	; load the FAT sector we're looking for
 	push di
 	push ebx ; offset
-	push es ; we want to read at 0:1000, not STAGE3_SEGMENT:1000
+	push es ; we want to read at 0:800, not STAGE3_SEGMENT:800
 	push eax ; desired LBA
 	xor ax, ax
 	mov es, ax
 	pop ebx ; pop LBA into EBX
-	mov di, 0x6000
+	mov di, 0x800
 	mov cx, 1
 	call read_sectors
 	cmp dl, 0
diff --git a/boot/x86/mbr.s b/boot/x86/mbr.s
index 613b248..b5d01c9 100644
--- a/boot/x86/mbr.s
+++ b/boot/x86/mbr.s
@@ -11,7 +11,7 @@ _start:
 	mov ds, ax
 	mov es, ax
 	mov ss, ax
-	mov sp, 0x7c00 ; just under the soon-to-be-loaded VBR, should be more than sufficient space
+	mov sp, 0xfe00 ; just under the soon-to-be-loaded VBR, should be more than sufficient space
 
 	; perform self-relocation
 	cld
@@ -54,16 +54,16 @@ real_start:
 	.load_vbr:
 		; load active partition's VBR
 		pop si
-		mov ax, 0x7c0
+		mov ax, 0xfe0
 		add si, 8
 		mov edx, [si]
 		xor bx, bx
 		call read_sector
 		; check is the VBR bootable (ends with 0x55 0xaa), if not halt
-		cmp word [0x7dfe], 0xaa55
+		cmp word [0xfffe], 0xaa55
 		jne .not_bootable
 		mov dl, [BOOT_DRIVE]
-		call 0x7c00
+		call 0xfe00
 		.not_bootable:
 			mov si, no_os
 			call print
diff --git a/boot/x86/stage3/loader.s b/boot/x86/stage3/loader.s
index bd703ba..b89d924 100644
--- a/boot/x86/stage3/loader.s
+++ b/boot/x86/stage3/loader.s
@@ -1,6 +1,6 @@
 bits 16
 cpu 686
-org 0x8000
+org 0x1800
 
 %include "../fat32/fat32-structs.s"
 
@@ -22,7 +22,7 @@ _start:
 .a20_enabled:
 	print a20_enabled
 	call get_1st_data_sec
-	mov ax, 0x2000
+	mov ax, 0x1000
 	mov es, ax
 	mov eax, BPB_RootClus
 	xor di, di
@@ -88,7 +88,7 @@ read_clus_chain_unreal:
 	push es
 	push ax
 	xor di, di
-	mov ax, 0x100
+	mov ax, 0x1000
 	mov es, ax
 	pop ax
 	call read_cluster
@@ -99,7 +99,7 @@ read_clus_chain_unreal:
 	push eax
 	push ebx
 	push ecx
-	mov esi, 0x1000
+	mov esi, 0x10000
 	xor ebx, ebx
 	movzx eax, word BPB_BytsPerSec
 	movzx bx, byte BPB_SecPerClus
diff --git a/boot/x86/vbr-fat32.s b/boot/x86/vbr-fat32.s
index 7ec3739..439157f 100644
--- a/boot/x86/vbr-fat32.s
+++ b/boot/x86/vbr-fat32.s
@@ -2,10 +2,10 @@
 ; This is what's going to be on most USB sticks and HDDs, for now
 
 bits 16
-org 0x7c00
+org 0xfe00
 cpu 686
 
-STAGE3_ADDRESS equ 0x8000
+STAGE3_ADDRESS equ 0x1800
 STAGE3_SEGMENT equ STAGE3_ADDRESS >> 4
 STAGE3_OFFSET equ STAGE3_ADDRESS & 0xf
 
@@ -25,7 +25,7 @@ times 0x57 db 0 ; skip past BPB
 real_start:
 	sti
 	; no need to set up segments and stack again, because MBR did it for us
-	mov bp, 0x7c00
+	mov bp, 0xfe00
 
 	; we expect the boot drive to be in DL and our partition table entry in DS:SI
 	mov [BOOT_DRIVE], dl
-- 
2.39.5