Skip to content

Commit 9decade

Browse files
authored
[FREELDR] Adapt bootsectors and multiboot code for future base address change (reactos#7785)
See also PRs reactos#8439 and reactos#7530. CORE-19882
1 parent 0362651 commit 9decade

File tree

9 files changed

+70
-41
lines changed

9 files changed

+70
-41
lines changed

boot/freeldr/bootsect/btrfs.S

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ FreeLdrFound:
386386
//mov dh, 0 // Load boot partition into DH (not needed, FreeLbr detects it itself)
387387

388388
/* Transfer execution to the bootloader */
389-
ljmp16 0, FREELDR_BASE
389+
ljmp16 FREELDR_BASE / 16, 0
390390

391391

392392
// Insert chunk into chunk map (located at DS:[CHUNK_MAP_OFFSET])

boot/freeldr/bootsect/fat.S

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ DataAreaStartLow = 4
4848
BiosCHSDriveSizeHigh = 6
4949
BiosCHSDriveSizeLow = 8
5050
BiosCHSDriveSize = 8
51-
ReadSectorsOffset = 10
52-
ReadClusterOffset = 12
53-
PutCharsOffset = 14
54-
BootSectorStackTop = HEX(7c00) - 16
51+
ReadSectorsOffset = 12
52+
ReadClusterOffset = 16
53+
PutCharsOffset = 20
54+
BootSectorStackTop = HEX(7c00) - 22
5555

5656

5757
// org 7c00h
@@ -218,17 +218,22 @@ FoundFreeLoader:
218218

219219
// Save the addresses of needed functions so
220220
// the helper code will know where to call them.
221-
mov word ptr [bp-ReadSectorsOffset], offset ReadSectors // Save the address of ReadSectors
222-
mov word ptr [bp-ReadClusterOffset], offset ReadCluster // Save the address of ReadCluster
223-
mov word ptr [bp-PutCharsOffset], offset PutChars // Save the address of PutChars
221+
xor bx, bx
222+
mov word ptr [bp-(ReadSectorsOffset-2)], bx // Save the segment of ReadSectors
223+
mov word ptr [bp-(ReadClusterOffset-2)], bx // Save the segment of ReadCluster
224+
mov word ptr [bp-(PutCharsOffset-2)], bx // Save the segment of PutChars
225+
mov word ptr [bp-ReadSectorsOffset], offset ReadSectors // Save the address of ReadSectors
226+
mov word ptr [bp-ReadClusterOffset], offset ReadCluster // Save the address of ReadCluster
227+
mov word ptr [bp-PutCharsOffset], offset PutChars // Save the address of PutChars
228+
mov byte ptr [PatchedRet], HEX(CB) // Patch 'ret' with 'retf'
224229

225230
// Now AX has start cluster of FreeLoader and we
226231
// have loaded the helper code in the first 512 bytes
227232
// of FreeLoader to 0000:F800. Now transfer control
228233
// to the helper code. Skip the first three bytes
229234
// because they contain a jump instruction to skip
230235
// over the helper code in the FreeLoader image.
231-
ljmp16 0, FREELDR_BASE + 3
236+
ljmp16 FREELDR_BASE / 16, 3
232237

233238

234239

@@ -249,13 +254,11 @@ Reboot:
249254
PutChars:
250255
lodsb
251256
or al,al
252-
jz short Done
257+
jz PatchedRet
253258
mov ah, HEX(0e)
254259
mov bx, 7
255260
int HEX(10)
256261
jmp short PutChars
257-
Done:
258-
ret
259262

260263
// Displays a bad boot message
261264
// And reboots
@@ -265,7 +268,6 @@ BadBoot:
265268

266269
jmp short Reboot
267270

268-
269271
// Reads cluster number in AX into [ES:0000]
270272
ReadCluster:
271273
// StartSector = ((Cluster - 2) * SectorsPerCluster) + ReservedSectors + HiddenSectors;
@@ -354,7 +356,7 @@ ReadSectorsLBALoop:
354356

355357
loop ReadSectorsLBALoop // Read next sector
356358

357-
ret
359+
jmp PatchedRet
358360

359361

360362
// Reads logical sectors into [ES:BX]
@@ -403,11 +405,12 @@ NoCarryCHS:
403405
// Increment read buffer for next sector
404406
loop ReadSectorsCHSLoop // Read next sector
405407

408+
PatchedRet:
406409
ret
407410

408-
409411
msgDiskError:
410-
.ascii "Disk error", CR, LF, NUL
412+
// .ascii "Disk error", CR, LF, NUL
413+
.ascii "ERR", CR, LF, NUL
411414
msgFreeLdr:
412415
.ascii "Ldr not found", CR, LF, NUL
413416
// Sorry, need the space...

boot/freeldr/bootsect/fat32.S

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ LoadFileDone:
425425
mov dh, byte ptr ds:[BootPartition] // Load boot partition into DH
426426

427427
/* Transfer execution to the bootloader */
428-
ljmp16 0, FREELDR_BASE
428+
ljmp16 FREELDR_BASE / 16, 0
429429

430430
// Returns the FAT entry for a given cluster number
431431
// On entry EAX has cluster number

boot/freeldr/bootsect/faty.S

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ main:
400400

401401
/* Now the complete freeldr imag is loaded.
402402
Jump to the realmode entry point. */
403-
ljmp16 0, FREELDR_BASE
403+
ljmp16 FREELDR_BASE / 16, 0
404404

405405

406406

boot/freeldr/bootsect/isoboot.S

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ found_drive:
303303

304304
.jump_to_freeldr:
305305
// Transfer execution to the bootloader.
306-
ljmp16 0, FREELDR_BASE
306+
ljmp16 FREELDR_BASE / 16, 0
307307

308308

309309
/* FUNCTIONS *****************************************************************/

boot/freeldr/bootsect/ntfs.S

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ StartSearch:
417417

418418
mov dl, byte ptr [BootDrive]
419419
mov dh, byte ptr [BootPartition]
420-
ljmp16 0, FREELDR_BASE
420+
ljmp16 FREELDR_BASE / 16, 0
421421

422422
// Error message if Freeldr is compressed, encrypted or sparse
423423
CompressedFreeldr:

boot/freeldr/bootsect/pc98/fat12fdd.S

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ DataAreaStartLow = 4
2525
BiosCHSDriveSizeHigh = 6
2626
BiosCHSDriveSizeLow = 8
2727
BiosCHSDriveSize = 8
28-
ReadSectorsOffset = 10
29-
ReadClusterOffset = 12
30-
PutCharsOffset = 14
31-
BootSectorStackTop = HEX(7C00) - 16
28+
ReadSectorsOffset = 12
29+
ReadClusterOffset = 16
30+
PutCharsOffset = 20
31+
BootSectorStackTop = HEX(7C00) - 22
3232

3333
if 0
3434
.macro DEBUG_STOP
@@ -242,9 +242,14 @@ FoundFreeLoader:
242242
* Save the addresses of needed functions so
243243
* the helper code will know where to call them
244244
*/
245-
mov word ptr [bp - ReadSectorsOffset], offset ReadSectors // Save the address of ReadSectors
246-
mov word ptr [bp - ReadClusterOffset], offset ReadCluster // Save the address of ReadCluster
247-
mov word ptr [bp - PutCharsOffset], offset PrintString // Save the address of PrintString
245+
xor bx, bx
246+
mov word ptr [bp-(ReadSectorsOffset-2)], bx // Save the segment of ReadSectors
247+
mov word ptr [bp-(ReadClusterOffset-2)], bx // Save the segment of ReadCluster
248+
mov word ptr [bp-(PutCharsOffset-2)], bx // Save the segment of PrintString
249+
mov word ptr [bp-ReadSectorsOffset], offset ReadSectors // Save the address of ReadSectors
250+
mov word ptr [bp-ReadClusterOffset], offset ReadCluster // Save the address of ReadCluster
251+
mov word ptr [bp-PutCharsOffset], offset PrintString // Save the address of PrintString
252+
mov byte ptr [PatchedRet], HEX(CB) // Patch 'ret' with 'retf'
248253

249254
/*
250255
* Now AX has start cluster of FreeLoader and we
@@ -254,7 +259,7 @@ FoundFreeLoader:
254259
* because they contain a jump instruction to skip
255260
* over the helper code in the FreeLoader image
256261
*/
257-
ljmp16 0, FREELDR_BASE + 3
262+
ljmp16 FREELDR_BASE / 16, 3
258263

259264
/*
260265
* Reads cluster number in AX into [ES:BX]
@@ -345,7 +350,7 @@ ReadSectors:
345350
pop bx
346351
loop .ReadSectorsLoop // Increment read buffer for next sector, read next sector
347352

348-
ret
353+
jmp PatchedRet
349354

350355
/*
351356
* Prints a character
@@ -380,7 +385,7 @@ PrintString:
380385
lodsb // Get a single char from a ptr
381386

382387
or al, al
383-
jz short .PrintEnd // Found NULL
388+
jz short PatchedRet // Found NULL
384389

385390
cmp al, HEX(0D)
386391
jz short .PrintStringHandleCR // Found CR
@@ -398,7 +403,7 @@ PrintString:
398403
inc si // Skip the next LF character
399404
jmp short PrintString
400405

401-
.PrintEnd:
406+
PatchedRet:
402407
ret
403408

404409
if 0
@@ -476,7 +481,8 @@ msgDiskError:
476481
msgNotFoundError:
477482
.ascii "NFE", CR, LF, NUL
478483
msgAnyKey:
479-
.ascii "Press any key", NUL
484+
// .ascii "Press any key", NUL
485+
.ascii "Press key", NULL
480486
filename:
481487
.ascii "FREELDR SYS"
482488

boot/freeldr/freeldr/arch/i386/multiboot.S

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
44
* PURPOSE: Support for the Multiboot v1 specification.
55
* COPYRIGHT: Copyright 1998-2002 Brian Palmer <[email protected]>
6-
* Copyright 2024 Daniel Victor <[email protected]>
6+
* Copyright 2024-2025 Daniel Victor <[email protected]>
77
*/
88

99
#include <asm.inc>
@@ -190,7 +190,7 @@ mb4:
190190
mov cr0, rax
191191

192192
/* Jump to real entry point */
193-
ljmp16 0, FREELDR_BASE
193+
ljmp16 FREELDR_BASE / 16, 0
194194

195195

196196
/* Force 8-byte alignment */

boot/freeldr/freeldr/arch/realmode/fathelp.inc

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@
1414
#define BiosCHSDriveSizeHigh 6
1515
#define BiosCHSDriveSizeLow 8
1616
#define BiosCHSDriveSize 8
17-
#define ReadSectorsOffset 10
18-
#define ReadClusterOffset 12
19-
#define PutCharsOffset 14
17+
#define ReadSectorsOffset 12
18+
#define ReadClusterOffset 16
19+
#define PutCharsOffset 20
20+
21+
#define ReadSectors dword ptr ss:[bp-ReadSectorsOffset]
22+
#define ReadCluster dword ptr ss:[bp-ReadClusterOffset]
23+
#define PutChars dword ptr ss:[bp-PutCharsOffset]
2024

2125
#define OEMName 3
2226
#define BytesPerSector 11
@@ -65,8 +69,8 @@ FatHelperEntryPoint:
6569
push ax
6670

6771
/* Display "Loading FreeLoader..." message */
68-
mov si, offset msgLoading
69-
call word ptr [bp-PutCharsOffset]
72+
mov si, offset msgLoading - FREELDR_BASE
73+
call CS_PutChars
7074

7175
call ReadFatIntoMemory
7276

@@ -92,7 +96,7 @@ LoadFile3:
9296
push ax
9397
xor bx,bx // Load ROSLDR starting at 0000:8000h
9498
push es
95-
call word ptr [bp-ReadClusterOffset]
99+
call ReadCluster
96100
pop es
97101

98102
xor bx,bx
@@ -133,7 +137,7 @@ ReadFatIntoMemory:
133137
mov bx, HEX(7000)
134138
mov es,bx
135139
xor bx,bx
136-
call word ptr [bp-ReadSectorsOffset]
140+
call ReadSectors
137141
ret
138142

139143

@@ -216,6 +220,22 @@ IsFat12_2:
216220
IsFat12_Done:
217221
ret
218222

223+
CS_PutChars:
224+
/* Save necessary registers */
225+
push ax
226+
push ds
227+
228+
/* Prepare ds before PutChars call */
229+
mov ax, cs
230+
mov ds, ax
231+
232+
/* Display the message */
233+
call PutChars
234+
235+
/* Restore necessary registers and return */
236+
pop ds
237+
pop ax
238+
ret
219239

220240
msgLoading:
221241
.ascii "Loading FreeLoader...", CR, LF, NUL

0 commit comments

Comments
 (0)