2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Bootsector
5 * PURPOSE: Combined FAT16 and FAT32 boot sector
6 * PROGRAMMERS: Brian Palmer
12 * Layout of a FAT volume:
14 * |---------------------------------------------------------
16 * | * FS Information Sector (FAT32 only) | ReservedSectors
17 * | * ... more reserved sectors ... |
18 * |--------------------------------------------------------
19 * | * FAT 1 | NumberOfFats
21 * | * [more FATs] | SectorsPerFat
22 * |---------------------------------------------------------
23 * | * Root Directory (FAT12/FAT16 only) | MaxRootEntries / 16
24 * |---------------------------------------------------------
27 * |----------------------------------------
30 /* INCLUDES ******************************************************************/
33 #include "../freeldr/include/arch/pc/x86common.h"
35 #define ADDRESS_FOR_DIRENTRIES HEX(10000)
39 /* Put the stack below the data area */
40 BootSectorStackTop = (HEX(7c00) - SizeOfDataArea)
42 /* Data area offsets for uninitialized data */
43 DataAreaStart = BootSectorStackTop + 0 /* dword */
45 RootDirStartSector = BootSectorStackTop + 4 /* dword */
47 BiosCHSDriveSize = BootSectorStackTop + 8 /* dword */
48 LBASectorsRead = BootSectorStackTop + 12 /* dword */
49 ReadSectorsOffset = BootSectorStackTop + 16 /* word */
50 ReadClusterOffset = BootSectorStackTop + 18 /* word */
51 PutCharsOffset = BootSectorStackTop + 20 /* word */
53 /* Macro for bp relative memory access to reduce code size */
54 #define BP_REL(x) ss:[bp + x - BootSectorStackTop]
56 /* The code starts at 0x7c00 */
61 /******************************************************************************
62 * BIOS Parameter Block (BPB) *
63 ******************************************************************************/
64 /* We have 3 bytes at the entry point to jump over the data area */
69 /* Here starts the BIOS Parameter Block (BPB) data.
70 The real data will be copied during install */
82 .word 0 // Always zero for FAT32 volumes
84 .word 0 // Always zero for FAT32 volumes
88 .word 0 // Always zero for FAT32 volumes
98 /* Extra data for FAT32 volumes */
113 .byte 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
130 /******************************************************************************
132 ******************************************************************************/
138 .ascii "Load failed!", CR, LF, NUL
141 .ascii "Press any key to reboot...", NUL
144 /******************************************************************************
146 * Input: DL = Boot drive *
147 ******************************************************************************/
149 /* First setup the segment registers */
154 /* Load the stack pointer */
155 mov sp, BootSectorStackTop
157 /* Load bp for relative memory access, which saves us some bytes of code
158 size, when used with 32 bit instructions */
161 /* Load the boot drive from the BPB into al */
162 mov al, byte ptr ds:[BootDrive]
164 /* Check if it's valid */
168 /* Copy it into dl */
172 /* Save the bootdrive in the BPB */
173 mov byte ptr ds:[BootDrive], dl
176 /******************************************************************************
177 * Get drive parameters *
178 ******************************************************************************/
180 /* Call INT 13 to get the drive parameters:
182 DL = drive (bit 7 set for hard disk)
183 ES:DI = 0000h:0000h to guard against BIOS bugs */
188 /* Return from INT 13h/08h:
189 CF set on error -> AH = status (07h)
190 CF clear if successful -> AH = 00h
191 AL = 00h on at least some BIOSes
192 BL = drive type (AT/PS2 floppies only)
193 CH = low eight bits of maximum cylinder number
194 CL = bits 0:5 maximum sector number, bits 7:8 high two bits of maximum cylinder number
195 DH = maximum head number
196 DL = number of drives
197 ES:DI -> drive parameter table (floppies only) */
199 /* Check for failure */
203 /******************************************************************************
204 * Calculate drive size *
205 ******************************************************************************/
207 movzx ebx, ch // Put the low 8-bits of the cylinder count into EBX
208 mov bh, cl // Put the high 2-bits in BH
209 shr bh, 6 // Shift them into position, now BX contains the cylinder count
211 and cl, HEX(3f) // Mask off cylinder bits from sector count
212 movzx ecx, cl // Move the sectors per track into ECX
214 movzx eax, dh // Move the heads into EAX
216 inc eax // Make it one based because the bios returns it zero based
217 inc ebx // Make the cylinder count one based also
218 mul ecx // Multiply heads with the sectors per track, result in edx:eax
219 mul ebx // Multiply the cylinders with (heads * sectors) [stored in edx:eax already]
221 // We now have the total number of sectors as reported
222 // by the bios in eax, so store it in our variable
223 mov dword ptr BP_REL(BiosCHSDriveSize), eax
226 /******************************************************************************
228 ******************************************************************************/
230 /* Load the number of first sector of the FAT into eax */
231 movzx eax, word ptr BP_REL(ReservedSectors)
232 add eax, dword ptr BP_REL(HiddenSectors)
234 /* Load sector count into ecx */
236 mov ecx, BP_REL(SectorsPerFatBig)
238 movzx ecx, word ptr BP_REL(SectorsPerFat)
241 /* Save FAT sector and size for later use */
244 /* Point ES:DI to the memory that is later the disk read buffer for freeldr.
245 This way we cannot overwrite our FAT with freeldr data */
246 mov bx, DISKREADBUFFER / 16
250 /* Read the sectors */
253 /* Restore FAT sector and size */
257 /******************************************************************************
258 * Get root directory / data area start *
259 ******************************************************************************/
261 /* Copy reserved + hidden sectors to EBX */
264 /* Calculate (NumberOfFats * SectorsPerFat) */
265 movzx eax, byte ptr BP_REL(NumberOfFats)
268 /* Add reserved sectors and hidden sectors */
272 /* Save the starting sector of the root directory */
273 mov dword ptr BP_REL(RootDirStartSector), eax
275 /* Calculate number of sectors for the root dir:
276 sectors = MaxRootEntries * 32 / 512 (rounded up!) */
277 movzx ebx, word ptr BP_REL(MaxRootEntries)
281 /* Add the root dir start sector and save it as DataAreaStart */
283 mov dword ptr BP_REL(DataAreaStart), ebx
285 mov dword ptr BP_REL(DataAreaStart), eax
287 /* On FAT32 volumes the root dir start cluster is stored in the BPB */
288 mov eax, dword ptr BP_REL(RootDirStartCluster)
292 /******************************************************************************
293 * Search the root directory for freeldr *
294 ******************************************************************************/
297 /* Load ES with the segment where we put the dir entries */
298 mov bx, ADDRESS_FOR_DIRENTRIES / 16
301 /* Set the address offset to 0 */
305 /* Read the dir cluster. This loads the next cluster into EAX */
308 /* Calculate the numer of dir entries in this cluster:
309 dx = SectorsPerCluster * 512 / 32 */
310 movzx dx, byte ptr ds:[SectorsPerCluster]
313 /* Set the number of sectors to read to 1 */
317 /* Read the sector, but preserve ES */
322 /* Set entry count to entries per sector */
326 /* Load the start offset of the dir entries into ebx */
330 /* Load the address of the name into di */
333 /* If the first byte of the entry is 0 then we have reached the end */
334 cmp byte ptr es:[di], ch
337 /* Compare with freeldr file name */
338 mov si, offset filename
342 /* Check if we found the file */
345 /* File didn't match, go to next entry */
348 /* Decrement entry count and check if we reached the end */
353 /* Check to see if this was the last cluster in the chain */
354 cmp eax, HEX(0ffffff8)
358 /* Repeat the search process with the next sector / cluster.
359 eax is already incremented in ReadSectors / ReadCluster */
360 jmp .SearchForFreeldr
363 /******************************************************************************
365 ******************************************************************************/
368 /* Load the cluster number of freeldr into eax */
372 movzx eax, word ptr es:[bx + HEX(1A)]
375 /* Load es:di with the freeldr start address */
376 mov dx, FREELDR_BASE / 16
381 /* Load the cluster to the current address. EAX is adjusted to the next
382 cluster and ES is adjusted for the next read */
385 /* Check if this is the last cluster in the chain */
387 cmp eax, HEX(0ffffff8)
395 /* Load boot drive into DL, boot partition into DH */
396 mov dl, byte ptr ds:[BootDrive]
397 mov dh, byte ptr ds:[BootPartition]
399 /* Now the complete freeldr imag is loaded.
400 Jump to the realmode entry point. */
401 ljmp16 0, FREELDR_BASE
406 mov si, offset msgBootFailure
411 /* Output "Press any key to reboot" message */
412 mov si, offset msgAnyKey
415 /* Wait for a keypress */
423 /******************************************************************************
424 * PROCEDURE ReadCluster *
425 * Input: EAX = Cluster number, ES:DI = Target *
426 * Modifies: EAX (next cluster number), BX, DX (undefined) *
427 ******************************************************************************/
432 // StartSector = ((Cluster - 2) * SectorsPerCluster) + SectorsForFat + ReservedSectors + HiddenSectors
433 // StartSector = ((Cluster - 2) * SectorsPerCluster) + DataAreaStart
439 /* Multiply with SectorsPerCluster */
440 movzx ecx, byte ptr BP_REL(SectorsPerCluster)
443 /* Add DataAreaStart */
444 add eax, dword ptr BP_REL(DataAreaStart)
446 /* Call ReadSectors. EAX = SectorNumber, ECX = SectorsPerCluster */
449 /* Restore the cluster number */
456 #error FAT23 not implemented
458 #error FAT12 not implemented
460 /* DX:AX = AX * 2 (since FAT16 entries are 2 bytes) */
464 /* Shift DX, so that it is the segment offset: DX = DX * (64K / 16) */
467 /* Put segment address of FAT into ES */
468 add dx, DISKREADBUFFER / 16
471 /* Put the FAT entry offset into EBX for indirect mov */
474 /* Put the content of the FAT entry into AX */
478 /* Restore ES and return */
483 /******************************************************************************
484 * PROCEDURE ReadSectors *
485 * Input: EAX = Sector start number, ECX = number of sectors, ES:DI = Target *
486 * Modifies: EAX (incremented by sector count), CX = 0, ES (incremented), *
488 ******************************************************************************/
490 /* We could possibly also implement CHS, but it's currently unimplemented */
494 /* Copy number of sectors to ebx */
497 /* Since the LBA calls only support 0x7F sectors at a time,
498 we will limit ourselves to 64 */
505 /* Save logical sector number & sector count */
508 /* Setup the disk address packet on the stack */
509 .byte HEX(66) // size overwrite prefix for next push
510 push 0 // Put 64-bit logical block address (high part) on stack
511 push eax // Put 64-bit logical block address (low part) on stack
512 push es // Put transfer segment on stack
513 push di // Put transfer offset on stack
514 push bx // Set transfer count (for this round)
515 push 16 // Set size of packet to 16
517 /* Point si to the disk address packet on stack */
520 /* Set the drive number */
521 mov dl, byte ptr ds:[BootDrive]
523 /* Call INT 13h, AH = 42h - Extended Read
529 /* Check for failure */
532 /* Remove disk address packet from stack */
535 /* Adjust ES to point to the next sector */
541 /* Restore sector count & logical sector number */
544 /* Adjust the sector number to the next sector we need to read
545 by adding the number of sectors that we read */
548 /* Adjust remaining sectors */
557 /******************************************************************************
558 * PROCEDURE PutChars *
559 * Input: ESI = Points to string to be printed *
560 * Modifies: AL, AH, SI *
561 ******************************************************************************/
573 /******************************************************************************
574 * Padding and boot sector signature *
575 ******************************************************************************/
576 /* Pad to 509 bytes */
583 .word HEX(0aa55) // BootSector signature