2011-06-07 23:22:38 +00:00
|
|
|
/*
|
|
|
|
* COPYRIGHT: See COPYING in the top level directory
|
|
|
|
* PROJECT: ReactOS Bootsector
|
2015-09-26 16:31:49 +00:00
|
|
|
* FILE: boot/freeldr/bootsect/fat32.S
|
2011-06-07 23:22:38 +00:00
|
|
|
* PURPOSE:
|
2015-12-26 20:13:27 +00:00
|
|
|
* PROGRAMMERS: Brian Palmer
|
2011-06-07 23:22:38 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* INCLUDES ******************************************************************/
|
|
|
|
|
|
|
|
#include <asm.inc>
|
2014-11-10 19:21:06 +00:00
|
|
|
#include <freeldr/include/arch/pc/x86common.h>
|
2011-06-07 23:22:38 +00:00
|
|
|
|
2011-06-08 12:45:56 +00:00
|
|
|
#define BP_REL(x) [bp+x-offset start]
|
|
|
|
|
2011-06-07 23:22:38 +00:00
|
|
|
.code16
|
|
|
|
|
|
|
|
//ORG HEX(7c00)
|
|
|
|
|
|
|
|
start:
|
|
|
|
jmp short main
|
|
|
|
nop
|
|
|
|
|
|
|
|
OEMName:
|
|
|
|
.ASCII "FrLdr1.0"
|
|
|
|
BytesPerSector:
|
2011-06-08 12:45:56 +00:00
|
|
|
.word 512
|
2011-06-07 23:22:38 +00:00
|
|
|
SectsPerCluster:
|
2011-06-08 12:45:56 +00:00
|
|
|
.byte 0
|
2011-06-07 23:22:38 +00:00
|
|
|
ReservedSectors:
|
2011-06-08 12:45:56 +00:00
|
|
|
.word 32
|
2011-06-07 23:22:38 +00:00
|
|
|
NumberOfFats:
|
2011-06-08 12:45:56 +00:00
|
|
|
.byte 2
|
2011-06-07 23:22:38 +00:00
|
|
|
MaxRootEntries:
|
2015-12-26 20:13:27 +00:00
|
|
|
.word 0 // Always zero for FAT32 volumes
|
2011-06-07 23:22:38 +00:00
|
|
|
TotalSectors:
|
2015-12-26 20:13:27 +00:00
|
|
|
.word 0 // Always zero for FAT32 volumes
|
2011-06-07 23:22:38 +00:00
|
|
|
MediaDescriptor:
|
2011-06-08 12:45:56 +00:00
|
|
|
.byte HEX(0f8)
|
2011-06-07 23:22:38 +00:00
|
|
|
SectorsPerFat:
|
2015-12-26 20:13:27 +00:00
|
|
|
.word 0 // Always zero for FAT32 volumes
|
2011-06-07 23:22:38 +00:00
|
|
|
SectorsPerTrack:
|
2011-06-08 12:45:56 +00:00
|
|
|
.word 0
|
2011-06-07 23:22:38 +00:00
|
|
|
NumberOfHeads:
|
2011-06-08 12:45:56 +00:00
|
|
|
.word 0
|
2011-06-07 23:22:38 +00:00
|
|
|
HiddenSectors:
|
2011-06-08 12:45:56 +00:00
|
|
|
.long 0
|
2011-06-07 23:22:38 +00:00
|
|
|
TotalSectorsBig:
|
2011-06-08 12:45:56 +00:00
|
|
|
.long 0
|
2011-06-07 23:22:38 +00:00
|
|
|
|
|
|
|
// FAT32 Inserted Info
|
|
|
|
SectorsPerFatBig:
|
2011-06-08 12:45:56 +00:00
|
|
|
.long 0
|
2011-06-07 23:22:38 +00:00
|
|
|
ExtendedFlags:
|
2011-06-08 12:45:56 +00:00
|
|
|
.word 0
|
2011-06-07 23:22:38 +00:00
|
|
|
FSVersion:
|
2011-06-08 12:45:56 +00:00
|
|
|
.word 0
|
2011-06-07 23:22:38 +00:00
|
|
|
RootDirStartCluster:
|
2011-06-08 12:45:56 +00:00
|
|
|
.long 0
|
2011-06-07 23:22:38 +00:00
|
|
|
FSInfoSector:
|
2011-06-08 12:45:56 +00:00
|
|
|
.word 0
|
2011-06-07 23:22:38 +00:00
|
|
|
BackupBootSector:
|
2011-06-08 12:45:56 +00:00
|
|
|
.word 6
|
2011-06-07 23:22:38 +00:00
|
|
|
Reserved1:
|
2015-12-26 21:28:01 +00:00
|
|
|
.space 12, 0
|
2011-06-07 23:22:38 +00:00
|
|
|
// End FAT32 Inserted Info
|
2015-12-26 21:28:01 +00:00
|
|
|
|
2011-06-07 23:22:38 +00:00
|
|
|
BootDrive:
|
2011-06-08 12:45:56 +00:00
|
|
|
.byte 0
|
2011-06-07 23:22:38 +00:00
|
|
|
Reserved:
|
2011-06-08 12:45:56 +00:00
|
|
|
.byte 0
|
2011-06-07 23:22:38 +00:00
|
|
|
ExtendSig:
|
2011-06-08 12:45:56 +00:00
|
|
|
.byte HEX(29)
|
2011-06-07 23:22:38 +00:00
|
|
|
SerialNumber:
|
2011-06-08 12:45:56 +00:00
|
|
|
.long 0
|
2011-06-07 23:22:38 +00:00
|
|
|
VolumeLabel:
|
2011-06-08 12:45:56 +00:00
|
|
|
.ascii "NO NAME "
|
2011-06-07 23:22:38 +00:00
|
|
|
FileSystem:
|
2011-06-08 12:45:56 +00:00
|
|
|
.ascii "FAT32 "
|
2011-06-07 23:22:38 +00:00
|
|
|
|
|
|
|
main:
|
|
|
|
xor ax,ax // Setup segment registers
|
|
|
|
mov ds,ax // Make DS correct
|
|
|
|
mov es,ax // Make ES correct
|
2015-12-26 20:13:27 +00:00
|
|
|
mov ss,ax // Make SS correct
|
2011-06-07 23:22:38 +00:00
|
|
|
mov bp, HEX(7c00)
|
|
|
|
mov sp, HEX(7c00) // Setup a stack
|
|
|
|
|
2015-12-26 20:13:27 +00:00
|
|
|
cmp byte ptr BP_REL(BootDrive), HEX(0ff) // If they have specified a boot drive then use it
|
2011-06-07 23:22:38 +00:00
|
|
|
jne CheckSectorsPerFat
|
|
|
|
|
2015-12-26 20:13:27 +00:00
|
|
|
mov byte ptr BP_REL(BootDrive), dl // Save the boot drive
|
2011-06-07 23:22:38 +00:00
|
|
|
|
|
|
|
CheckSectorsPerFat:
|
|
|
|
|
2015-12-26 20:13:27 +00:00
|
|
|
cmp word ptr BP_REL(SectorsPerFat), 0 // Check the old 16-bit value of SectorsPerFat
|
|
|
|
jnz CheckFailed // If it is non-zero then exit with an error
|
|
|
|
CheckTotalSectors: // Check the old 16-bit value of TotalSectors & MaxRootEntries
|
|
|
|
cmp dword ptr BP_REL(MaxRootEntries), 0 // by comparing the DWORD at offset MaxRootEntries to zero
|
|
|
|
jnz CheckFailed // If it is non-zero then exit with an error
|
2011-06-07 23:22:38 +00:00
|
|
|
CheckFileSystemVersion:
|
2015-12-26 20:13:27 +00:00
|
|
|
cmp word ptr BP_REL(FSVersion), 0 // Check the file system version word
|
|
|
|
jna GetDriveParameters // It is zero, so continue
|
2011-06-07 23:22:38 +00:00
|
|
|
CheckFailed:
|
2015-12-26 20:13:27 +00:00
|
|
|
jmp PrintFileSystemError // If it is not zero then exit with an error
|
2011-06-07 23:22:38 +00:00
|
|
|
|
|
|
|
GetDriveParameters:
|
|
|
|
mov ax, HEX(0800)
|
2015-12-26 20:13:27 +00:00
|
|
|
mov dl, byte ptr BP_REL(BootDrive) // Get boot drive in dl
|
|
|
|
int HEX(13) // Request drive parameters from the bios
|
|
|
|
jnc CalcDriveSize // If the call succeeded then calculate the drive size
|
2011-06-07 23:22:38 +00:00
|
|
|
|
|
|
|
// If we get here then the call to the BIOS failed
|
|
|
|
// so just set CHS equal to the maximum addressable
|
|
|
|
// size
|
|
|
|
mov cx, HEX(0ffff)
|
|
|
|
mov dh, cl
|
|
|
|
|
|
|
|
CalcDriveSize:
|
|
|
|
// Now that we have the drive geometry
|
|
|
|
// lets calculate the drive size
|
2015-12-26 20:13:27 +00:00
|
|
|
mov bl, ch // Put the low 8-bits of the cylinder count into BL
|
|
|
|
mov bh, cl // Put the high 2-bits in BH
|
|
|
|
shr bh, 6 // Shift them into position, now BX contains the cylinder count
|
|
|
|
and cl, HEX(3f) // Mask off cylinder bits from sector count
|
2011-06-07 23:22:38 +00:00
|
|
|
// CL now contains sectors per track and DH contains head count
|
2015-12-26 20:13:27 +00:00
|
|
|
movzx eax, dh // Move the heads into EAX
|
|
|
|
movzx ebx, bx // Move the cylinders into EBX
|
|
|
|
movzx ecx, cl // Move the sectors per track into ECX
|
|
|
|
inc eax // Make it one based because the bios returns it zero based
|
|
|
|
inc ebx // Make the cylinder count one based also
|
|
|
|
mul ecx // Multiply heads with the sectors per track, result in edx:eax
|
|
|
|
mul ebx // Multiply the cylinders with (heads * sectors) [stored in edx:eax already]
|
2011-06-07 23:22:38 +00:00
|
|
|
|
|
|
|
// We now have the total number of sectors as reported
|
|
|
|
// by the bios in eax, so store it in our variable
|
|
|
|
mov dword ptr ds:[BiosCHSDriveSize], eax
|
|
|
|
|
|
|
|
LoadExtraBootCode:
|
|
|
|
// First we have to load our extra boot code at
|
|
|
|
// sector 14 into memory at [0000:7e00h]
|
|
|
|
mov eax, HEX(0e)
|
2015-12-26 20:13:27 +00:00
|
|
|
add eax, dword ptr BP_REL(HiddenSectors) // Add the number of hidden sectors
|
2011-06-07 23:22:38 +00:00
|
|
|
mov cx, 1
|
|
|
|
xor bx, bx
|
2015-12-26 20:13:27 +00:00
|
|
|
mov es, bx // Read sector to [0000:7e00h]
|
2011-06-07 23:22:38 +00:00
|
|
|
mov bx, HEX(7e00)
|
|
|
|
call ReadSectors
|
|
|
|
jmp StartSearch
|
|
|
|
|
|
|
|
|
|
|
|
// Reads logical sectors into [ES:BX]
|
|
|
|
// EAX has logical sector number to read
|
|
|
|
// CX has number of sectors to read
|
|
|
|
ReadSectors:
|
2011-07-02 15:08:54 +00:00
|
|
|
push es
|
2015-12-26 20:13:27 +00:00
|
|
|
cmp eax, dword ptr ds:[BiosCHSDriveSize] // Check if they are reading a sector outside CHS range
|
|
|
|
jae ReadSectorsLBA // Yes - go to the LBA routine
|
2011-06-07 23:22:38 +00:00
|
|
|
// If at all possible we want to use LBA routines because
|
|
|
|
// They are optimized to read more than 1 sector per read
|
|
|
|
|
2015-12-26 20:13:27 +00:00
|
|
|
pushad // Save logical sector number & sector count
|
2011-06-07 23:22:38 +00:00
|
|
|
|
2015-12-26 20:13:27 +00:00
|
|
|
CheckInt13hExtensions: // Now check if this computer supports extended reads
|
2011-06-07 23:22:38 +00:00
|
|
|
mov ah, HEX(41) // AH = 41h
|
2015-12-26 20:13:27 +00:00
|
|
|
mov bx, HEX(55aa) // BX = 55AAh
|
|
|
|
mov dl, byte ptr BP_REL(BootDrive) // DL = drive (80h-FFh)
|
2011-06-07 23:22:38 +00:00
|
|
|
int HEX(13) // IBM/MS INT 13 Extensions - INSTALLATION CHECK
|
2015-12-26 20:13:27 +00:00
|
|
|
jc ReadSectorsCHS // CF set on error (extensions not supported)
|
|
|
|
cmp bx, HEX(0aa55) // BX = AA55h if installed
|
2011-06-07 23:22:38 +00:00
|
|
|
jne ReadSectorsCHS
|
2015-12-26 20:13:27 +00:00
|
|
|
test cl,1 // CX = API subset support bitmap
|
|
|
|
jz ReadSectorsCHS // Bit 0, extended disk access functions (AH=42h-44h,47h,48h) supported
|
2011-06-07 23:22:38 +00:00
|
|
|
|
2015-12-26 20:13:27 +00:00
|
|
|
popad // Restore sector count & logical sector number
|
2011-06-07 23:22:38 +00:00
|
|
|
|
|
|
|
ReadSectorsLBA:
|
2015-12-26 20:13:27 +00:00
|
|
|
pushad // Save logical sector number & sector count
|
2011-06-07 23:22:38 +00:00
|
|
|
|
2015-12-26 20:13:27 +00:00
|
|
|
cmp cx, 64 // Since the LBA calls only support 0x7F sectors at a time we will limit ourselves to 64
|
|
|
|
jbe ReadSectorsSetupDiskAddressPacket // If we are reading less than 65 sectors then just do the read
|
|
|
|
mov cx, 64 // Otherwise read only 64 sectors on this loop iteration
|
2011-06-07 23:22:38 +00:00
|
|
|
|
|
|
|
ReadSectorsSetupDiskAddressPacket:
|
|
|
|
mov word ptr ds:[LBASectorsRead],cx
|
2011-06-09 22:04:45 +00:00
|
|
|
push 0
|
|
|
|
push 0
|
2011-06-07 23:22:38 +00:00
|
|
|
push eax // Put 64-bit logical block address on stack
|
2015-12-26 20:13:27 +00:00
|
|
|
push es // Put transfer segment on stack
|
|
|
|
push bx // Put transfer offset on stack
|
|
|
|
push cx // Set transfer count
|
|
|
|
push 16 // Set size of packet to 10h
|
|
|
|
mov si, sp // Setup disk address packet on stack
|
2011-06-07 23:22:38 +00:00
|
|
|
|
2015-12-26 20:13:27 +00:00
|
|
|
mov dl, byte ptr BP_REL(BootDrive) // Drive number
|
|
|
|
mov ah, HEX(42) // Int 13h, AH = 42h - Extended Read
|
|
|
|
int HEX(13) // Call BIOS
|
|
|
|
jc PrintDiskError // If the read failed then abort
|
2011-06-07 23:22:38 +00:00
|
|
|
|
2015-12-26 20:13:27 +00:00
|
|
|
add sp, 16 // Remove disk address packet from stack
|
2011-06-07 23:22:38 +00:00
|
|
|
|
2015-12-26 20:13:27 +00:00
|
|
|
popad // Restore sector count & logical sector number
|
2011-06-07 23:22:38 +00:00
|
|
|
|
|
|
|
push bx
|
|
|
|
mov ebx, dword ptr ds:[LBASectorsRead]
|
2015-12-26 20:13:27 +00:00
|
|
|
add eax, ebx // Increment sector to read
|
2011-06-07 23:22:38 +00:00
|
|
|
shl ebx, 5
|
|
|
|
mov dx, es
|
2015-12-26 20:13:27 +00:00
|
|
|
add dx, bx // Setup read buffer for next sector
|
2011-06-07 23:22:38 +00:00
|
|
|
mov es, dx
|
|
|
|
pop bx
|
|
|
|
|
|
|
|
sub cx, word ptr ds:[LBASectorsRead]
|
2015-12-26 20:13:27 +00:00
|
|
|
jnz ReadSectorsLBA // Read next sector
|
2011-06-07 23:22:38 +00:00
|
|
|
|
2011-07-02 15:08:54 +00:00
|
|
|
pop es
|
2011-06-07 23:22:38 +00:00
|
|
|
ret
|
|
|
|
|
|
|
|
LBASectorsRead:
|
2011-06-08 12:45:56 +00:00
|
|
|
.long 0
|
2011-06-07 23:22:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Reads logical sectors into [ES:BX]
|
|
|
|
// EAX has logical sector number to read
|
|
|
|
// CX has number of sectors to read
|
|
|
|
ReadSectorsCHS:
|
|
|
|
popad // Get logical sector number & sector count off stack
|
|
|
|
|
|
|
|
ReadSectorsCHSLoop:
|
|
|
|
pushad
|
|
|
|
xor edx, edx
|
2011-06-08 12:45:56 +00:00
|
|
|
movzx ecx, word ptr BP_REL(SectorsPerTrack)
|
2011-06-07 23:22:38 +00:00
|
|
|
div ecx // Divide logical by SectorsPerTrack
|
2015-12-26 20:13:27 +00:00
|
|
|
inc dl // Sectors numbering starts at 1 not 0
|
|
|
|
mov cl, dl // Sector in CL
|
2011-06-07 23:22:38 +00:00
|
|
|
mov edx, eax
|
|
|
|
shr edx, 16
|
2015-12-26 20:13:27 +00:00
|
|
|
div word ptr BP_REL(NumberOfHeads) // Divide logical by number of heads
|
|
|
|
mov dh, dl // Head in DH
|
|
|
|
mov dl, byte ptr BP_REL(BootDrive) // Drive number in DL
|
|
|
|
mov ch, al // Cylinder in CX
|
|
|
|
ror ah, 1 // Low 8 bits of cylinder in CH, high 2 bits
|
|
|
|
ror ah, 1 // in CL shifted to bits 6 & 7
|
|
|
|
or cl, ah // Or with sector number
|
2011-06-07 23:22:38 +00:00
|
|
|
mov ax, HEX(0201)
|
|
|
|
int HEX(13) // DISK - READ SECTORS INTO MEMORY
|
|
|
|
// AL = number of sectors to read, CH = track, CL = sector
|
2015-12-26 20:13:27 +00:00
|
|
|
// DH = head, DL = drive, ES:BX -> buffer to fill
|
|
|
|
// Return: CF set on error, AH = status (see AH=01h), AL = number of sectors read
|
2011-06-07 23:22:38 +00:00
|
|
|
|
2015-12-26 20:13:27 +00:00
|
|
|
jc PrintDiskError // If the read failed then abort
|
2011-06-07 23:22:38 +00:00
|
|
|
|
|
|
|
popad
|
|
|
|
|
|
|
|
inc eax // Increment Sector to Read
|
|
|
|
|
|
|
|
mov dx, es
|
2015-12-26 20:13:27 +00:00
|
|
|
add dx, 32 // Increment read buffer for next sector
|
2011-06-07 23:22:38 +00:00
|
|
|
mov es, dx
|
|
|
|
|
2015-12-26 20:13:27 +00:00
|
|
|
loop ReadSectorsCHSLoop // Read next sector
|
2011-06-07 23:22:38 +00:00
|
|
|
|
2020-08-19 15:10:00 +00:00
|
|
|
pop es
|
2011-06-07 23:22:38 +00:00
|
|
|
ret
|
|
|
|
|
|
|
|
// Displays a disk error message
|
|
|
|
// And reboots
|
|
|
|
PrintDiskError:
|
2015-12-26 20:13:27 +00:00
|
|
|
mov si, offset msgDiskError // Bad boot disk message
|
|
|
|
call PutChars // Display it
|
2011-06-07 23:22:38 +00:00
|
|
|
|
|
|
|
jmp Reboot
|
|
|
|
|
|
|
|
// Displays a file system error message
|
|
|
|
// And reboots
|
|
|
|
PrintFileSystemError:
|
2015-12-26 20:13:27 +00:00
|
|
|
mov si, offset msgFileSystemError // FreeLdr not found message
|
|
|
|
call PutChars // Display it
|
2011-06-07 23:22:38 +00:00
|
|
|
|
|
|
|
Reboot:
|
2015-12-26 20:13:27 +00:00
|
|
|
mov si, offset msgAnyKey // Press any key message
|
|
|
|
call PutChars // Display it
|
2011-06-07 23:22:38 +00:00
|
|
|
xor ax, ax
|
2015-12-26 20:13:27 +00:00
|
|
|
int HEX(16) // Wait for a keypress
|
|
|
|
int HEX(19) // Reboot
|
2011-06-07 23:22:38 +00:00
|
|
|
|
|
|
|
PutChars:
|
|
|
|
lodsb
|
|
|
|
or al, al
|
|
|
|
jz short Done
|
|
|
|
mov ah, HEX(0e)
|
|
|
|
mov bx, 7
|
|
|
|
int HEX(10)
|
|
|
|
jmp short PutChars
|
|
|
|
Done:
|
2011-06-08 12:45:56 +00:00
|
|
|
ret
|
2011-06-07 23:22:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
BiosCHSDriveSize:
|
2011-06-08 12:45:56 +00:00
|
|
|
.long 0
|
2011-06-07 23:22:38 +00:00
|
|
|
|
|
|
|
msgDiskError:
|
2011-06-29 16:40:14 +00:00
|
|
|
.ascii "Disk error", CR, LF, NUL
|
2011-06-07 23:22:38 +00:00
|
|
|
msgFileSystemError:
|
2011-06-29 16:40:14 +00:00
|
|
|
.ascii "File system error", CR, LF, NUL
|
2011-06-07 23:22:38 +00:00
|
|
|
msgAnyKey:
|
2011-06-29 16:40:14 +00:00
|
|
|
.ascii "Press any key to restart", CR, LF, NUL
|
2011-06-07 23:22:38 +00:00
|
|
|
|
|
|
|
.org 509 // Pad to 509 bytes
|
|
|
|
|
|
|
|
BootPartition:
|
2011-06-08 12:45:56 +00:00
|
|
|
.byte 0
|
2011-06-07 23:22:38 +00:00
|
|
|
|
|
|
|
BootSignature:
|
2015-12-26 20:13:27 +00:00
|
|
|
.word HEX(0aa55) // BootSector signature
|
2011-06-07 23:22:38 +00:00
|
|
|
|
|
|
|
// End of bootsector
|
|
|
|
//
|
|
|
|
// Now starts the extra boot code that we will store
|
|
|
|
// at sector 14 on a FAT32 volume
|
|
|
|
//
|
|
|
|
// To remain multi-boot compatible with other operating
|
|
|
|
// systems we must not overwrite anything other than
|
|
|
|
// the bootsector which means we will have to use
|
|
|
|
// a different sector like 14 to store our extra boot code
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
StartSearch:
|
|
|
|
|
|
|
|
// Now we must get the first cluster of the root directory
|
2011-06-08 12:45:56 +00:00
|
|
|
mov eax, dword ptr BP_REL(RootDirStartCluster)
|
2011-06-07 23:22:38 +00:00
|
|
|
cmp eax, HEX(0ffffff8) // Check to see if this is the last cluster in the chain
|
2015-12-26 20:13:27 +00:00
|
|
|
jb ContinueSearch // If not continue, if so then we didn't find freeldr.sys
|
2011-06-07 23:22:38 +00:00
|
|
|
jmp PrintFileNotFound
|
|
|
|
|
|
|
|
ContinueSearch:
|
|
|
|
mov bx, HEX(2000)
|
|
|
|
mov es, bx // Read cluster to [2000:0000h]
|
|
|
|
call ReadCluster // Read the cluster
|
|
|
|
|
|
|
|
// Now we have to find our way through the root directory to
|
2015-12-26 20:13:27 +00:00
|
|
|
// The FREELDR.SYS file
|
2011-06-07 23:22:38 +00:00
|
|
|
xor bx,bx
|
2011-06-08 12:45:56 +00:00
|
|
|
mov bl, byte ptr BP_REL(SectsPerCluster)
|
2015-12-26 20:13:27 +00:00
|
|
|
shl bx, 4 // BX = BX * 512 / 32
|
2011-06-07 23:22:38 +00:00
|
|
|
mov ax, HEX(2000) // We loaded at 2000:0000
|
|
|
|
mov es, ax
|
|
|
|
xor di, di
|
|
|
|
mov si, offset filename
|
|
|
|
mov cx, 11
|
2015-12-26 20:13:27 +00:00
|
|
|
repe cmpsb // Compare filenames
|
2011-06-07 23:22:38 +00:00
|
|
|
jz FoundFile // If same we found it
|
|
|
|
dec bx
|
|
|
|
jnz FindFile
|
|
|
|
jmp PrintFileNotFound
|
|
|
|
|
|
|
|
FindFile:
|
2015-12-26 21:28:01 +00:00
|
|
|
mov ax, es // We didn't find it in the previous dir entry
|
|
|
|
add ax, 2 // So lets move to the next one
|
2015-12-26 20:13:27 +00:00
|
|
|
mov es, ax // And search again
|
2011-06-07 23:22:38 +00:00
|
|
|
xor di, di
|
|
|
|
mov si, offset filename
|
|
|
|
mov cx, 11
|
2015-12-26 20:13:27 +00:00
|
|
|
repe cmpsb // Compare filenames
|
2011-06-07 23:22:38 +00:00
|
|
|
jz FoundFile // If same we found it
|
|
|
|
dec bx // Keep searching till we run out of dir entries
|
|
|
|
jnz FindFile // Last entry?
|
|
|
|
|
|
|
|
// Get the next root dir cluster and try again until we run out of clusters
|
2011-06-08 12:45:56 +00:00
|
|
|
mov eax, dword ptr BP_REL(RootDirStartCluster)
|
2011-06-07 23:22:38 +00:00
|
|
|
call GetFatEntry
|
2011-06-08 12:45:56 +00:00
|
|
|
mov dword ptr BP_REL(RootDirStartCluster), eax
|
2011-06-07 23:22:38 +00:00
|
|
|
jmp StartSearch
|
|
|
|
|
|
|
|
FoundFile:
|
|
|
|
// Display "Loading FreeLoader..." message
|
|
|
|
mov si, offset msgLoading // Loading message
|
|
|
|
call PutChars // Display it
|
|
|
|
|
2015-12-26 20:13:27 +00:00
|
|
|
xor di, di // ES:DI has dir entry
|
2011-06-07 23:22:38 +00:00
|
|
|
xor dx, dx
|
2015-12-26 20:13:27 +00:00
|
|
|
mov ax, word ptr es:[di+20] // Get start cluster high word
|
2011-06-07 23:22:38 +00:00
|
|
|
shl eax, 16
|
2015-12-26 20:13:27 +00:00
|
|
|
mov ax, word ptr es:[di+26] // Get start cluster low word
|
2011-06-07 23:22:38 +00:00
|
|
|
|
|
|
|
CheckStartCluster:
|
2015-12-26 20:13:27 +00:00
|
|
|
cmp eax, 2 // Check and see if the start cluster starts at cluster 2 or above
|
|
|
|
jnb CheckEndCluster // If so then continue
|
|
|
|
jmp PrintFileSystemError // If not exit with error
|
2011-06-07 23:22:38 +00:00
|
|
|
CheckEndCluster:
|
2015-12-26 20:13:27 +00:00
|
|
|
cmp eax, HEX(0ffffff8) // Check and see if the start cluster is and end of cluster chain indicator
|
|
|
|
jb InitializeLoadSegment // If not then continue
|
|
|
|
jmp PrintFileSystemError // If so exit with error
|
2011-06-07 23:22:38 +00:00
|
|
|
|
|
|
|
InitializeLoadSegment:
|
2011-06-18 17:19:28 +00:00
|
|
|
mov bx, FREELDR_BASE / 16
|
2011-06-07 23:22:38 +00:00
|
|
|
mov es, bx
|
|
|
|
|
|
|
|
LoadFile:
|
2015-12-26 20:13:27 +00:00
|
|
|
cmp eax, HEX(0ffffff8) // Check to see if this is the last cluster in the chain
|
|
|
|
jae LoadFileDone // If so continue, if not then read the next one
|
2011-06-07 23:22:38 +00:00
|
|
|
push eax
|
2015-12-26 20:43:50 +00:00
|
|
|
xor bx, bx // Load ROSLDR starting at 0000:F800h
|
2011-06-07 23:22:38 +00:00
|
|
|
push es
|
|
|
|
call ReadCluster
|
|
|
|
pop es
|
|
|
|
|
|
|
|
xor bx, bx
|
2011-06-08 12:45:56 +00:00
|
|
|
mov bl, byte ptr BP_REL(SectsPerCluster)
|
2015-12-26 20:13:27 +00:00
|
|
|
shl bx, 5 // BX = BX * 512 / 16
|
|
|
|
mov ax, es // Increment the load address by
|
|
|
|
add ax, bx // The size of a cluster
|
2011-06-07 23:22:38 +00:00
|
|
|
mov es, ax
|
|
|
|
|
|
|
|
pop eax
|
|
|
|
push es
|
2015-12-26 20:13:27 +00:00
|
|
|
call GetFatEntry // Get the next entry
|
2011-06-07 23:22:38 +00:00
|
|
|
pop es
|
|
|
|
|
2015-12-26 20:13:27 +00:00
|
|
|
jmp LoadFile // Load the next cluster (if any)
|
2011-06-07 23:22:38 +00:00
|
|
|
|
|
|
|
LoadFileDone:
|
2015-12-26 20:13:27 +00:00
|
|
|
mov dl, byte ptr BP_REL(BootDrive) // Load boot drive into DL
|
|
|
|
mov dh, byte ptr ds:[BootPartition] // Load boot partition into DH
|
2011-06-07 23:22:38 +00:00
|
|
|
|
2011-06-12 21:21:47 +00:00
|
|
|
/* Transfer execution to the bootloader */
|
2011-06-18 17:19:28 +00:00
|
|
|
ljmp16 0, FREELDR_BASE
|
2011-06-07 23:22:38 +00:00
|
|
|
|
|
|
|
// Returns the FAT entry for a given cluster number
|
|
|
|
// On entry EAX has cluster number
|
|
|
|
// On return EAX has FAT entry for that cluster
|
|
|
|
GetFatEntry:
|
|
|
|
|
2015-12-26 20:13:27 +00:00
|
|
|
shl eax, 2 // EAX = EAX * 4 (since FAT32 entries are 4 bytes)
|
|
|
|
mov ecx, eax // Save this for later in ECX
|
2011-06-07 23:22:38 +00:00
|
|
|
xor edx, edx
|
2011-06-08 12:45:56 +00:00
|
|
|
movzx ebx, word ptr BP_REL(BytesPerSector)
|
2011-06-07 23:22:38 +00:00
|
|
|
push ebx
|
2015-12-26 20:13:27 +00:00
|
|
|
div ebx // FAT Sector Number = EAX / BytesPerSector
|
2011-06-08 12:45:56 +00:00
|
|
|
movzx ebx, word ptr BP_REL(ReservedSectors)
|
2015-12-26 20:13:27 +00:00
|
|
|
add eax, ebx // FAT Sector Number += ReservedSectors
|
2011-06-08 12:45:56 +00:00
|
|
|
mov ebx, dword ptr BP_REL(HiddenSectors)
|
2015-12-26 20:13:27 +00:00
|
|
|
add eax, ebx // FAT Sector Number += HiddenSectors
|
2011-06-07 23:22:38 +00:00
|
|
|
pop ebx
|
|
|
|
dec ebx
|
2015-12-26 20:13:27 +00:00
|
|
|
and ecx,ebx // FAT Offset Within Sector = ECX % BytesPerSector
|
|
|
|
// EAX holds logical FAT sector number
|
|
|
|
// ECX holds FAT entry offset
|
|
|
|
|
|
|
|
// Now we have to check the extended flags
|
|
|
|
// to see which FAT is the active one
|
|
|
|
// and use it, or if they are mirrored then
|
|
|
|
// no worries
|
|
|
|
movzx ebx, word ptr BP_REL(ExtendedFlags) // Get extended flags and put into ebx
|
|
|
|
and bx, HEX(0f) // Mask off upper 8 bits, now we have active fat in bl
|
|
|
|
jz LoadFatSector // If fat is mirrored then skip fat calcs
|
|
|
|
cmp bl, byte ptr BP_REL(NumberOfFats) // Compare bl to number of fats
|
2011-06-07 23:22:38 +00:00
|
|
|
jb GetActiveFatOffset
|
2015-12-26 20:13:27 +00:00
|
|
|
jmp PrintFileSystemError // If bl is bigger than numfats exit with error
|
2011-06-07 23:22:38 +00:00
|
|
|
GetActiveFatOffset:
|
2015-12-26 20:13:27 +00:00
|
|
|
push eax // Save logical FAT sector number
|
|
|
|
mov eax, dword ptr BP_REL(SectorsPerFatBig) // Get the number of sectors occupied by one fat in eax
|
|
|
|
mul ebx // Multiplied by the active FAT index we have in ebx
|
|
|
|
pop edx // Get logical FAT sector number
|
|
|
|
add eax, edx // Add the current FAT sector offset
|
2011-06-07 23:22:38 +00:00
|
|
|
|
|
|
|
LoadFatSector:
|
|
|
|
push ecx
|
2011-07-02 15:08:54 +00:00
|
|
|
|
2015-12-26 20:13:27 +00:00
|
|
|
mov bx, HEX(9000) // We will load it to [9000:0000h]
|
2011-07-02 15:08:54 +00:00
|
|
|
mov es, bx
|
|
|
|
|
2011-06-07 23:22:38 +00:00
|
|
|
// EAX holds logical FAT sector number
|
|
|
|
// Check if we have already loaded it
|
|
|
|
cmp eax, dword ptr ds:[FatSectorInCache]
|
|
|
|
je LoadFatSectorAlreadyLoaded
|
|
|
|
|
|
|
|
mov dword ptr ds:[FatSectorInCache], eax
|
2011-07-02 15:08:54 +00:00
|
|
|
xor bx, bx
|
2011-06-07 23:22:38 +00:00
|
|
|
mov cx, 1
|
|
|
|
call ReadSectors
|
|
|
|
|
|
|
|
LoadFatSectorAlreadyLoaded:
|
|
|
|
pop ecx
|
2015-12-26 20:13:27 +00:00
|
|
|
mov eax, dword ptr es:[ecx] // Get FAT entry
|
|
|
|
and eax, HEX(0fffffff) // Mask off reserved bits
|
2011-06-07 23:22:38 +00:00
|
|
|
|
|
|
|
ret
|
|
|
|
|
2015-12-26 20:13:27 +00:00
|
|
|
FatSectorInCache: // This variable tells us which sector we currently have in memory
|
|
|
|
.long HEX(0ffffffff) // There is no need to re-read the same sector if we don't have to
|
2011-06-07 23:22:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Reads cluster number in EAX into [ES:0000]
|
|
|
|
ReadCluster:
|
2015-12-26 20:13:27 +00:00
|
|
|
// StartSector = ((Cluster - 2) * SectorsPerCluster) + ReservedSectors + HiddenSectors;
|
2011-06-07 23:22:38 +00:00
|
|
|
|
|
|
|
dec eax
|
|
|
|
dec eax
|
|
|
|
xor edx, edx
|
2011-06-08 12:45:56 +00:00
|
|
|
movzx ebx, byte ptr BP_REL(SectsPerCluster)
|
2011-06-07 23:22:38 +00:00
|
|
|
mul ebx
|
|
|
|
push eax
|
|
|
|
xor edx, edx
|
2011-06-08 12:45:56 +00:00
|
|
|
movzx eax, byte ptr BP_REL(NumberOfFats)
|
|
|
|
mul dword ptr BP_REL(SectorsPerFatBig)
|
|
|
|
movzx ebx, word ptr BP_REL(ReservedSectors)
|
2011-06-07 23:22:38 +00:00
|
|
|
add eax, ebx
|
2011-06-08 12:45:56 +00:00
|
|
|
add eax, dword ptr BP_REL(HiddenSectors)
|
2011-06-07 23:22:38 +00:00
|
|
|
pop ebx
|
2015-12-26 20:13:27 +00:00
|
|
|
add eax, ebx // EAX now contains the logical sector number of the cluster
|
2011-06-07 23:22:38 +00:00
|
|
|
xor bx, bx // We will load it to [ES:0000], ES loaded before function call
|
2011-06-08 12:45:56 +00:00
|
|
|
movzx cx, byte ptr BP_REL(SectsPerCluster)
|
2011-06-07 23:22:38 +00:00
|
|
|
call ReadSectors
|
|
|
|
ret
|
|
|
|
|
|
|
|
// Displays a file not found error message
|
|
|
|
// And reboots
|
|
|
|
PrintFileNotFound:
|
2015-12-26 20:13:27 +00:00
|
|
|
mov si, offset msgFreeLdr // FreeLdr not found message
|
|
|
|
call PutChars // Display it
|
2011-06-07 23:22:38 +00:00
|
|
|
|
|
|
|
jmp Reboot
|
|
|
|
|
|
|
|
msgFreeLdr:
|
2011-06-18 14:50:24 +00:00
|
|
|
.ascii "freeldr.sys not found", CR, LF, NUL
|
2011-06-07 23:22:38 +00:00
|
|
|
filename:
|
2011-06-08 12:45:56 +00:00
|
|
|
.ascii "FREELDR SYS"
|
2011-06-07 23:22:38 +00:00
|
|
|
msgLoading:
|
2011-07-01 01:06:19 +00:00
|
|
|
.ascii "Loading FreeLoader...", CR, LF, NUL
|
2011-06-07 23:22:38 +00:00
|
|
|
|
2011-06-08 12:45:56 +00:00
|
|
|
.org 1022 // Pad to 1022 bytes
|
2011-06-07 23:22:38 +00:00
|
|
|
|
2011-06-08 12:45:56 +00:00
|
|
|
.word HEX(0aa55) // BootSector signature
|
2011-06-07 23:22:38 +00:00
|
|
|
|
|
|
|
.endcode16
|
|
|
|
|
|
|
|
END
|