@@ -48,10 +48,10 @@ DataAreaStartLow = 4
4848BiosCHSDriveSizeHigh = 6
4949BiosCHSDriveSizeLow = 8
5050BiosCHSDriveSize = 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:
249254PutChars:
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 ]
270272ReadCluster:
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-
409411msgDiskError:
410- .ascii "Disk error" , CR , LF , NUL
412+ // .ascii "Disk error" , CR , LF , NUL
413+ .ascii "ERR" , CR , LF , NUL
411414msgFreeLdr:
412415 .ascii "Ldr not found" , CR , LF , NUL
413416// Sorry , need the space...
0 commit comments