[FREELDR] Adapt bootsectors and multiboot code for future base address change (#7785)

See also PRs #8439 and #7530.
CORE-19882
This commit is contained in:
Daniel Victor
2025-11-08 16:40:01 -03:00
committed by GitHub
parent 0362651eb8
commit 9decadee83
9 changed files with 70 additions and 41 deletions

View File

@@ -386,7 +386,7 @@ FreeLdrFound:
//mov dh, 0 // Load boot partition into DH (not needed, FreeLbr detects it itself)
/* Transfer execution to the bootloader */
ljmp16 0, FREELDR_BASE
ljmp16 FREELDR_BASE / 16, 0
// Insert chunk into chunk map (located at DS:[CHUNK_MAP_OFFSET])

View File

@@ -48,10 +48,10 @@ DataAreaStartLow = 4
BiosCHSDriveSizeHigh = 6
BiosCHSDriveSizeLow = 8
BiosCHSDriveSize = 8
ReadSectorsOffset = 10
ReadClusterOffset = 12
PutCharsOffset = 14
BootSectorStackTop = HEX(7c00) - 16
ReadSectorsOffset = 12
ReadClusterOffset = 16
PutCharsOffset = 20
BootSectorStackTop = HEX(7c00) - 22
// org 7c00h
@@ -218,9 +218,14 @@ FoundFreeLoader:
// Save the addresses of needed functions so
// the helper code will know where to call them.
mov word ptr [bp-ReadSectorsOffset], offset ReadSectors // Save the address of ReadSectors
mov word ptr [bp-ReadClusterOffset], offset ReadCluster // Save the address of ReadCluster
mov word ptr [bp-PutCharsOffset], offset PutChars // Save the address of PutChars
xor bx, bx
mov word ptr [bp-(ReadSectorsOffset-2)], bx // Save the segment of ReadSectors
mov word ptr [bp-(ReadClusterOffset-2)], bx // Save the segment of ReadCluster
mov word ptr [bp-(PutCharsOffset-2)], bx // Save the segment of PutChars
mov word ptr [bp-ReadSectorsOffset], offset ReadSectors // Save the address of ReadSectors
mov word ptr [bp-ReadClusterOffset], offset ReadCluster // Save the address of ReadCluster
mov word ptr [bp-PutCharsOffset], offset PutChars // Save the address of PutChars
mov byte ptr [PatchedRet], HEX(CB) // Patch 'ret' with 'retf'
// Now AX has start cluster of FreeLoader and we
// have loaded the helper code in the first 512 bytes
@@ -228,7 +233,7 @@ FoundFreeLoader:
// to the helper code. Skip the first three bytes
// because they contain a jump instruction to skip
// over the helper code in the FreeLoader image.
ljmp16 0, FREELDR_BASE + 3
ljmp16 FREELDR_BASE / 16, 3
@@ -249,13 +254,11 @@ Reboot:
PutChars:
lodsb
or al,al
jz short Done
jz PatchedRet
mov ah, HEX(0e)
mov bx, 7
int HEX(10)
jmp short PutChars
Done:
ret
// Displays a bad boot message
// And reboots
@@ -265,7 +268,6 @@ BadBoot:
jmp short Reboot
// Reads cluster number in AX into [ES:0000]
ReadCluster:
// StartSector = ((Cluster - 2) * SectorsPerCluster) + ReservedSectors + HiddenSectors;
@@ -354,7 +356,7 @@ ReadSectorsLBALoop:
loop ReadSectorsLBALoop // Read next sector
ret
jmp PatchedRet
// Reads logical sectors into [ES:BX]
@@ -403,11 +405,12 @@ NoCarryCHS:
// Increment read buffer for next sector
loop ReadSectorsCHSLoop // Read next sector
PatchedRet:
ret
msgDiskError:
.ascii "Disk error", CR, LF, NUL
// .ascii "Disk error", CR, LF, NUL
.ascii "ERR", CR, LF, NUL
msgFreeLdr:
.ascii "Ldr not found", CR, LF, NUL
// Sorry, need the space...

View File

@@ -425,7 +425,7 @@ LoadFileDone:
mov dh, byte ptr ds:[BootPartition] // Load boot partition into DH
/* Transfer execution to the bootloader */
ljmp16 0, FREELDR_BASE
ljmp16 FREELDR_BASE / 16, 0
// Returns the FAT entry for a given cluster number
// On entry EAX has cluster number

View File

@@ -400,7 +400,7 @@ main:
/* Now the complete freeldr imag is loaded.
Jump to the realmode entry point. */
ljmp16 0, FREELDR_BASE
ljmp16 FREELDR_BASE / 16, 0

View File

@@ -303,7 +303,7 @@ found_drive:
.jump_to_freeldr:
// Transfer execution to the bootloader.
ljmp16 0, FREELDR_BASE
ljmp16 FREELDR_BASE / 16, 0
/* FUNCTIONS *****************************************************************/

View File

@@ -417,7 +417,7 @@ StartSearch:
mov dl, byte ptr [BootDrive]
mov dh, byte ptr [BootPartition]
ljmp16 0, FREELDR_BASE
ljmp16 FREELDR_BASE / 16, 0
// Error message if Freeldr is compressed, encrypted or sparse
CompressedFreeldr:

View File

@@ -25,10 +25,10 @@ DataAreaStartLow = 4
BiosCHSDriveSizeHigh = 6
BiosCHSDriveSizeLow = 8
BiosCHSDriveSize = 8
ReadSectorsOffset = 10
ReadClusterOffset = 12
PutCharsOffset = 14
BootSectorStackTop = HEX(7C00) - 16
ReadSectorsOffset = 12
ReadClusterOffset = 16
PutCharsOffset = 20
BootSectorStackTop = HEX(7C00) - 22
if 0
.macro DEBUG_STOP
@@ -242,9 +242,14 @@ FoundFreeLoader:
* Save the addresses of needed functions so
* the helper code will know where to call them
*/
mov word ptr [bp - ReadSectorsOffset], offset ReadSectors // Save the address of ReadSectors
mov word ptr [bp - ReadClusterOffset], offset ReadCluster // Save the address of ReadCluster
mov word ptr [bp - PutCharsOffset], offset PrintString // Save the address of PrintString
xor bx, bx
mov word ptr [bp-(ReadSectorsOffset-2)], bx // Save the segment of ReadSectors
mov word ptr [bp-(ReadClusterOffset-2)], bx // Save the segment of ReadCluster
mov word ptr [bp-(PutCharsOffset-2)], bx // Save the segment of PrintString
mov word ptr [bp-ReadSectorsOffset], offset ReadSectors // Save the address of ReadSectors
mov word ptr [bp-ReadClusterOffset], offset ReadCluster // Save the address of ReadCluster
mov word ptr [bp-PutCharsOffset], offset PrintString // Save the address of PrintString
mov byte ptr [PatchedRet], HEX(CB) // Patch 'ret' with 'retf'
/*
* Now AX has start cluster of FreeLoader and we
@@ -254,7 +259,7 @@ FoundFreeLoader:
* because they contain a jump instruction to skip
* over the helper code in the FreeLoader image
*/
ljmp16 0, FREELDR_BASE + 3
ljmp16 FREELDR_BASE / 16, 3
/*
* Reads cluster number in AX into [ES:BX]
@@ -345,7 +350,7 @@ ReadSectors:
pop bx
loop .ReadSectorsLoop // Increment read buffer for next sector, read next sector
ret
jmp PatchedRet
/*
* Prints a character
@@ -380,7 +385,7 @@ PrintString:
lodsb // Get a single char from a ptr
or al, al
jz short .PrintEnd // Found NULL
jz short PatchedRet // Found NULL
cmp al, HEX(0D)
jz short .PrintStringHandleCR // Found CR
@@ -398,7 +403,7 @@ PrintString:
inc si // Skip the next LF character
jmp short PrintString
.PrintEnd:
PatchedRet:
ret
if 0
@@ -476,7 +481,8 @@ msgDiskError:
msgNotFoundError:
.ascii "NFE", CR, LF, NUL
msgAnyKey:
.ascii "Press any key", NUL
// .ascii "Press any key", NUL
.ascii "Press key", NULL
filename:
.ascii "FREELDR SYS"

View File

@@ -3,7 +3,7 @@
* LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
* PURPOSE: Support for the Multiboot v1 specification.
* COPYRIGHT: Copyright 1998-2002 Brian Palmer <brianp@sginet.com>
* Copyright 2024 Daniel Victor <ilauncherdeveloper@gmail.com>
* Copyright 2024-2025 Daniel Victor <ilauncherdeveloper@gmail.com>
*/
#include <asm.inc>
@@ -190,7 +190,7 @@ mb4:
mov cr0, rax
/* Jump to real entry point */
ljmp16 0, FREELDR_BASE
ljmp16 FREELDR_BASE / 16, 0
/* Force 8-byte alignment */

View File

@@ -14,9 +14,13 @@
#define BiosCHSDriveSizeHigh 6
#define BiosCHSDriveSizeLow 8
#define BiosCHSDriveSize 8
#define ReadSectorsOffset 10
#define ReadClusterOffset 12
#define PutCharsOffset 14
#define ReadSectorsOffset 12
#define ReadClusterOffset 16
#define PutCharsOffset 20
#define ReadSectors dword ptr ss:[bp-ReadSectorsOffset]
#define ReadCluster dword ptr ss:[bp-ReadClusterOffset]
#define PutChars dword ptr ss:[bp-PutCharsOffset]
#define OEMName 3
#define BytesPerSector 11
@@ -65,8 +69,8 @@ FatHelperEntryPoint:
push ax
/* Display "Loading FreeLoader..." message */
mov si, offset msgLoading
call word ptr [bp-PutCharsOffset]
mov si, offset msgLoading - FREELDR_BASE
call CS_PutChars
call ReadFatIntoMemory
@@ -92,7 +96,7 @@ LoadFile3:
push ax
xor bx,bx // Load ROSLDR starting at 0000:8000h
push es
call word ptr [bp-ReadClusterOffset]
call ReadCluster
pop es
xor bx,bx
@@ -133,7 +137,7 @@ ReadFatIntoMemory:
mov bx, HEX(7000)
mov es,bx
xor bx,bx
call word ptr [bp-ReadSectorsOffset]
call ReadSectors
ret
@@ -216,6 +220,22 @@ IsFat12_2:
IsFat12_Done:
ret
CS_PutChars:
/* Save necessary registers */
push ax
push ds
/* Prepare ds before PutChars call */
mov ax, cs
mov ds, ax
/* Display the message */
call PutChars
/* Restore necessary registers and return */
pop ds
pop ax
ret
msgLoading:
.ascii "Loading FreeLoader...", CR, LF, NUL