2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Bootsector
4 * FILE: boot/freeldr/bootsect/fatx.S
5 * PURPOSE: Combined FAT16 and FAT32 boot sector
6 * PROGRAMMERS: Brian Palmer
10 #define DISKREADBUFFER HEX(8E000)
13 * Layout of a FAT volume:
15 * |---------------------------------------------------------
17 * | * FS Information Sector (FAT32 only) | ReservedSectors
18 * | * ... more reserved sectors ... |
19 * |--------------------------------------------------------
20 * | * FAT 1 | NumberOfFats
22 * | * [more FATs] | SectorsPerFat
23 * |---------------------------------------------------------
24 * | * Root Directory (FAT12/FAT16 only) | MaxRootEntries / 16
25 * |---------------------------------------------------------
28 * |----------------------------------------
31 /* INCLUDES ******************************************************************/
34 #include <freeldr/include/arch/pc/x86common.h>
36 #define ADDRESS_FOR_DIRENTRIES HEX(10000)
40 /* Put the stack below the data area */
41 BootSectorStackTop = (HEX(7c00) - SizeOfDataArea)
43 /* Data area offsets for uninitialized data */
44 DataAreaStart = BootSectorStackTop + 0 /* dword */
46 RootDirStartSector = BootSectorStackTop + 4 /* dword */
48 BiosCHSDriveSize = BootSectorStackTop + 8 /* dword */
49 LBASectorsRead = BootSectorStackTop + 12 /* dword */
50 ReadSectorsOffset = BootSectorStackTop + 16 /* word */
51 ReadClusterOffset = BootSectorStackTop + 18 /* word */
52 PutCharsOffset = BootSectorStackTop + 20 /* word */
54 /* Macro for bp relative memory access to reduce code size */
55 #define BP_REL(x) ss:[bp + x - BootSectorStackTop]
57 /* The code starts at 0x7c00 */
62 /******************************************************************************
63 * BIOS Parameter Block (BPB) *
64 ******************************************************************************/
65 /* We have 3 bytes at the entry point to jump over the data area */
70 /* Here starts the BIOS Parameter Block (BPB) data.
71 The real data will be copied during install */
83 .word 0 // Always zero for FAT32 volumes
85 .word 0 // Always zero for FAT32 volumes
89 .word 0 // Always zero for FAT32 volumes
99 /* Extra data for FAT32 volumes */
114 .byte 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
131 /******************************************************************************
133 ******************************************************************************/
139 .ascii "Load failed!", CR, LF, NUL
142 .ascii "Press any key to reboot...", NUL
145 /******************************************************************************
147 * Input: DL = Boot drive *
148 ******************************************************************************/
150 /* First setup the segment registers */
155 /* Load the stack pointer */
156 mov sp, BootSectorStackTop
158 /* Load bp for relative memory access, which saves us some bytes of code
159 size, when used with 32 bit instructions */
162 /* Load the boot drive from the BPB into al */
163 mov al, byte ptr ds:[BootDrive]
165 /* Check if it's valid */
169 /* Copy it into dl */
173 /* Save the bootdrive in the BPB */
174 mov byte ptr ds:[BootDrive], dl
177 /******************************************************************************
178 * Get drive parameters *
179 ******************************************************************************/
181 /* Call INT 13 to get the drive parameters:
183 DL = drive (bit 7 set for hard disk)
184 ES:DI = 0000h:0000h to guard against BIOS bugs */
189 /* Return from INT 13h/08h:
190 CF set on error -> AH = status (07h)
191 CF clear if successful -> AH = 00h
192 AL = 00h on at least some BIOSes
193 BL = drive type (AT/PS2 floppies only)
194 CH = low eight bits of maximum cylinder number
195 CL = bits 0:5 maximum sector number, bits 7:8 high two bits of maximum cylinder number
196 DH = maximum head number
197 DL = number of drives
198 ES:DI -> drive parameter table (floppies only) */
200 /* Check for failure */
204 /******************************************************************************
205 * Calculate drive size *
206 ******************************************************************************/
208 movzx ebx, ch // Put the low 8-bits of the cylinder count into EBX
209 mov bh, cl // Put the high 2-bits in BH
210 shr bh, 6 // Shift them into position, now BX contains the cylinder count
212 and cl, HEX(3f) // Mask off cylinder bits from sector count
213 movzx ecx, cl // Move the sectors per track into ECX
215 movzx eax, dh // Move the heads into EAX
217 inc eax // Make it one based because the bios returns it zero based
218 inc ebx // Make the cylinder count one based also
219 mul ecx // Multiply heads with the sectors per track, result in edx:eax
220 mul ebx // Multiply the cylinders with (heads * sectors) [stored in edx:eax already]
222 // We now have the total number of sectors as reported
223 // by the bios in eax, so store it in our variable
224 mov dword ptr BP_REL(BiosCHSDriveSize), eax
227 /******************************************************************************
229 ******************************************************************************/
231 /* Load the number of first sector of the FAT into eax */
232 movzx eax, word ptr BP_REL(ReservedSectors)
233 add eax, dword ptr BP_REL(HiddenSectors)
235 /* Load sector count into ecx */
237 mov ecx, BP_REL(SectorsPerFatBig)
239 movzx ecx, word ptr BP_REL(SectorsPerFat)
242 /* Save FAT sector and size for later use */
245 /* Point ES:DI to the memory that is later the disk read buffer for freeldr.
246 This way we cannot overwrite our FAT with freeldr data */
247 mov bx, DISKREADBUFFER / 16
251 /* Read the sectors */
254 /* Restore FAT sector and size */
258 /******************************************************************************
259 * Get root directory / data area start *
260 ******************************************************************************/
262 /* Copy reserved + hidden sectors to EBX */
265 /* Calculate (NumberOfFats * SectorsPerFat) */
266 movzx eax, byte ptr BP_REL(NumberOfFats)
269 /* Add reserved sectors and hidden sectors */
273 /* Save the starting sector of the root directory */
274 mov dword ptr BP_REL(RootDirStartSector), eax
276 /* Calculate number of sectors for the root dir:
277 sectors = MaxRootEntries * 32 / 512 (rounded up!) */
278 movzx ebx, word ptr BP_REL(MaxRootEntries)
282 /* Add the root dir start sector and save it as DataAreaStart */
284 mov dword ptr BP_REL(DataAreaStart), ebx
286 mov dword ptr BP_REL(DataAreaStart), eax
288 /* On FAT32 volumes the root dir start cluster is stored in the BPB */
289 mov eax, dword ptr BP_REL(RootDirStartCluster)
293 /******************************************************************************
294 * Search the root directory for freeldr *
295 ******************************************************************************/
298 /* Load ES with the segment where we put the dir entries */
299 mov bx, ADDRESS_FOR_DIRENTRIES / 16
302 /* Set the address offset to 0 */
306 /* Read the dir cluster. This loads the next cluster into EAX */
309 /* Calculate the numer of dir entries in this cluster:
310 dx = SectorsPerCluster * 512 / 32 */
311 movzx dx, byte ptr ds:[SectorsPerCluster]
314 /* Set the number of sectors to read to 1 */
318 /* Read the sector, but preserve ES */
323 /* Set entry count to entries per sector */
327 /* Load the start offset of the dir entries into ebx */
331 /* Load the address of the name into di */
334 /* If the first byte of the entry is 0 then we have reached the end */
335 cmp byte ptr es:[di], ch
338 /* Compare with freeldr file name */
339 mov si, offset filename
343 /* Check if we found the file */
346 /* File didn't match, go to next entry */
349 /* Decrement entry count and check if we reached the end */
354 /* Check to see if this was the last cluster in the chain */
355 cmp eax, HEX(0ffffff8)
359 /* Repeat the search process with the next sector / cluster.
360 eax is already incremented in ReadSectors / ReadCluster */
361 jmp .SearchForFreeldr
364 /******************************************************************************
366 ******************************************************************************/
369 /* Load the cluster number of freeldr into eax */
373 movzx eax, word ptr es:[bx + HEX(1A)]
376 /* Load es:di with the freeldr start address */
377 mov dx, FREELDR_BASE / 16
382 /* Load the cluster to the current address. EAX is adjusted to the next
383 cluster and ES is adjusted for the next read */
386 /* Check if this is the last cluster in the chain */
388 cmp eax, HEX(0ffffff8)
396 /* Load boot drive into DL, boot partition into DH */
397 mov dl, byte ptr ds:[BootDrive]
398 mov dh, byte ptr ds:[BootPartition]
400 /* Now the complete freeldr imag is loaded.
401 Jump to the realmode entry point. */
402 ljmp16 0, FREELDR_BASE
407 mov si, offset msgBootFailure
412 /* Output "Press any key to reboot" message */
413 mov si, offset msgAnyKey
416 /* Wait for a keypress */
424 /******************************************************************************
425 * PROCEDURE ReadCluster *
426 * Input: EAX = Cluster number, ES:DI = Target *
427 * Modifies: EAX (next cluster number), BX, DX (undefined) *
428 ******************************************************************************/
433 // StartSector = ((Cluster - 2) * SectorsPerCluster) + SectorsForFat + ReservedSectors + HiddenSectors
434 // StartSector = ((Cluster - 2) * SectorsPerCluster) + DataAreaStart
440 /* Multiply with SectorsPerCluster */
441 movzx ecx, byte ptr BP_REL(SectorsPerCluster)
444 /* Add DataAreaStart */
445 add eax, dword ptr BP_REL(DataAreaStart)
447 /* Call ReadSectors. EAX = SectorNumber, ECX = SectorsPerCluster */
450 /* Restore the cluster number */
457 #error FAT23 not implemented
459 #error FAT12 not implemented
461 /* DX:AX = AX * 2 (since FAT16 entries are 2 bytes) */
465 /* Shift DX, so that it is the segment offset: DX = DX * (64K / 16) */
468 /* Put segment address of FAT into ES */
469 add dx, DISKREADBUFFER / 16
472 /* Put the FAT entry offset into EBX for indirect mov */
475 /* Put the content of the FAT entry into AX */
479 /* Restore ES and return */
484 /******************************************************************************
485 * PROCEDURE ReadSectors *
486 * Input: EAX = Sector start number, ECX = number of sectors, ES:DI = Target *
487 * Modifies: EAX (incremented by sector count), CX = 0, ES (incremented), *
489 ******************************************************************************/
491 /* We could possibly also implement CHS, but it's currently unimplemented */
495 /* Copy number of sectors to ebx */
498 /* Since the LBA calls only support 0x7F sectors at a time,
499 we will limit ourselves to 64 */
506 /* Save logical sector number & sector count */
509 /* Setup the disk address packet on the stack */
510 .byte HEX(66) // size overwrite prefix for next push
511 push 0 // Put 64-bit logical block address (high part) on stack
512 push eax // Put 64-bit logical block address (low part) on stack
513 push es // Put transfer segment on stack
514 push di // Put transfer offset on stack
515 push bx // Set transfer count (for this round)
516 push 16 // Set size of packet to 16
518 /* Point si to the disk address packet on stack */
521 /* Set the drive number */
522 mov dl, byte ptr ds:[BootDrive]
524 /* Call INT 13h, AH = 42h - Extended Read
530 /* Check for failure */
533 /* Remove disk address packet from stack */
536 /* Adjust ES to point to the next sector */
542 /* Restore sector count & logical sector number */
545 /* Adjust the sector number to the next sector we need to read
546 by adding the number of sectors that we read */
549 /* Adjust remaining sectors */
558 /******************************************************************************
559 * PROCEDURE PutChars *
560 * Input: ESI = Points to string to be printed *
561 * Modifies: AL, AH, SI *
562 ******************************************************************************/
574 /******************************************************************************
575 * Padding and boot sector signature *
576 ******************************************************************************/
577 /* Pad to 509 bytes */
584 .word HEX(0aa55) // BootSector signature