wok view BootProg/stuff/boot32.asm @ rev 25698
Up beep (1.4.12), gsoap (2.8.134), perl-test-warnings (0.033)
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Sun May 26 08:12:35 2024 +0000 (13 months ago) |
parents | 82f370bad6b5 |
children | 65c2646b484d |
line source
1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;; ;;
3 ;; "BootProg" Loader v 1.5 by Alexey Frunze (c) 2000-2015 ;;
4 ;; 2-clause BSD license. ;;
5 ;; ;;
6 ;; ;;
7 ;; How to Compile: ;;
8 ;; ~~~~~~~~~~~~~~~ ;;
9 ;; nasm boot32.asm -f bin -o boot32.bin ;;
10 ;; ;;
11 ;; ;;
12 ;; Features: ;;
13 ;; ~~~~~~~~~ ;;
14 ;; - FAT32 supported using BIOS int 13h function 42h (IOW, it will only ;;
15 ;; work with modern BIOSes supporting HDDs bigger than 8 GB) ;;
16 ;; ;;
17 ;; - Loads a 16-bit executable file in the MS-DOS .COM or .EXE format ;;
18 ;; from the root directory of a disk and transfers control to it ;;
19 ;; (the "ProgramName" variable holds the name of the file to be loaded) ;;
20 ;; Its maximum size can be up to 637KB without Extended BIOS Data area. ;;
21 ;; ;;
22 ;; - Prints an error if the file isn't found or couldn't be read ;;
23 ;; ("File not found" or "Read error") ;;
24 ;; and waits for a key to be pressed, then executes the Int 19h ;;
25 ;; instruction and lets the BIOS continue bootstrap. ;;
26 ;; ;;
27 ;; ;;
28 ;; Known Limitation: ;;
29 ;; ~~~~~~~~~~~~~~~~~ ;;
30 ;; - Need a up to date field bpbHiddenSectors to work in a partition ; ;;
31 ;; most formatters don't update it correcly in extended partitions. ;;
32 ;; ;;
33 ;; - Requires i80386 or better CPU. ;;
34 ;; ;;
35 ;; ;;
36 ;; Known Bugs: ;;
37 ;; ~~~~~~~~~~~ ;;
38 ;; - All bugs are fixed as far as I know. The boot sector has been tested ;;
39 ;; on my HDD and an 8GB USB stick. ;;
40 ;; ;;
41 ;; ;;
42 ;; Memory Layout: ;;
43 ;; ~~~~~~~~~~~~~~ ;;
44 ;; The diagram below shows the typical memory layout. The actual location ;;
45 ;; of the boot sector and its stack may be lower than A0000H if the BIOS ;;
46 ;; reserves memory for its Extended BIOS Data Area just below A0000H and ;;
47 ;; reports less than 640 KB of RAM via its Int 12H function. ;;
48 ;; ;;
49 ;; physical address ;;
50 ;; +------------------------+ 00000H ;;
51 ;; | Interrupt Vector Table | ;;
52 ;; +------------------------+ 00400H ;;
53 ;; | BIOS Data Area | ;;
54 ;; +------------------------+ 00500H ;;
55 ;; | PrtScr Status / Unused | ;;
56 ;; +------------------------+ 00600H ;;
57 ;; | Loaded Image | ;;
58 ;; +------------------------+ nnnnnH ;;
59 ;; | Available Memory | ;;
60 ;; +------------------------+ A0000H - 1KB ;;
61 ;; | Boot Sector | ;;
62 ;; +------------------------+ A0000H - 0.5KB ;;
63 ;; | 0.5KB Boot Stack | ;;
64 ;; +------------------------+ A0000H ;;
65 ;; | Video RAM | ;;
66 ;; ;;
67 ;; ;;
68 ;; Boot Image Startup (register values): ;;
69 ;; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ;;
70 ;; ax = 0ffffh (both FCB in the PSP don't have a valid drive identifier), ;;
71 ;; bx = 0, dl = BIOS boot drive number (e.g. 0, 80H) ;;
72 ;; cs:ip = program entry point ;;
73 ;; ss:sp = program stack (don't confuse with boot sector's stack) ;;
74 ;; COM program defaults: cs = ds = es = ss = 50h, sp = 0, ip = 100h ;;
75 ;; EXE program defaults: ds = es = EXE data - 10h (fake MS-DOS psp), ;;
76 ;; cs:ip and ss:sp depends on EXE header ;;
77 ;; Magic numbers: ;;
78 ;; si = 16381 (prime number 2**14-3) ;;
79 ;; di = 32749 (prime number 2**15-19) ;;
80 ;; bp = 65521 (prime number 2**16-15) ;;
81 ;; The magic numbers let the program know whether it has been loaded by ;;
82 ;; this boot sector or by MS-DOS, which may be handy for universal, bare- ;;
83 ;; metal and MS-DOS programs. ;;
84 ;; The command line contains no arguments. ;;
85 ;; ;;
86 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
88 %define bx(label) bx+label-boot
89 %define si(label) si+label-boot
90 ExtraBootSector equ 1
91 ClusterMask equ 1 ; +9 bytes
92 NullEntryCheck equ 1 ; +5 bytes
93 CheckAttrib equ 0 ; +6 bytes
94 NonMirroredFATs equ 1 ; +20 bytes
95 ReadRetry equ 1 ; +6/7 bytes
96 LBA48bits equ 1 ; +10 bytes
97 CHSsupport equ 1 ; +35 bytes max 8GB
98 CHSgeometryCheck equ 1 ; +20 bytes
99 CHSsanityCheck equ 1 ; +9 bytes
100 SectorOf512Bytes equ 0 ; -4 bytes
101 Always2FATs equ 0 ; -4 bytes
102 WaitForKey equ 0 ; +5 bytes
103 AnyWhere equ 1 ; +4 bytes
105 [BITS 16]
106 [CPU 386]
108 ImageLoadSeg equ 60h ; <=07Fh because of "push byte ImageLoadSeg" instructions
109 StackSize equ 512
111 [SECTION .text]
112 [ORG 0]
114 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
115 ;; Boot sector starts here ;;
116 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
118 boot:
119 DriveNumber equ boot+0
120 HiLBA equ boot+2
121 jmp short start ; MS-DOS/Windows checks for this jump
122 nop
123 bsOemName DB "BootProg" ; 0x03
125 ;;;;;;;;;;;;;;;;;;;;;;
126 ;; BPB1 starts here ;;
127 ;;;;;;;;;;;;;;;;;;;;;;
129 bpbBytesPerSector DW 0 ; 0x0B 512, 1024, 2048 or 4096
130 bpbSectorsPerCluster DB 0 ; 0x0D 1, 2, 4, 8, 16, 32, 64 or 128
131 bpbReservedSectors DW 0 ; 0x0E 32
132 bpbNumberOfFATs DB 0 ; 0x10 2
133 bpbRootEntries DW 0 ; 0x11 0
134 bpbTotalSectors DW 0 ; 0x13 0
135 bpbMedia DB 0 ; 0x15 F8, F0
136 bpbSectorsPerFAT DW 0 ; 0x16 0
137 bpbSectorsPerTrack DW 0 ; 0x18
138 bpbHeadsPerCylinder DW 0 ; 0x1A
139 bpbHiddenSectors DD 0 ; 0x1C
140 bpbTotalSectorsBig DD 0 ; 0x20
142 ;;;;;;;;;;;;;;;;;;;;
143 ;; BPB1 ends here ;;
144 ;;;;;;;;;;;;;;;;;;;;
146 ;;;;;;;;;;;;;;;;;;;;;;
147 ;; BPB2 starts here ;;
148 ;;;;;;;;;;;;;;;;;;;;;;
150 bsSectorsPerFAT32 DD 0 ; 0x24
151 bsExtendedFlags DW 0 ; 0x28
152 bsFSVersion DW 0 ; 0x2A
153 bsRootDirectoryClusterNo DD 0 ; 0x2C
154 bsFSInfoSectorNo DW 0 ; 0x30
155 bsBackupBootSectorNo DW 0 ; 0x32
156 bsreserved times 12 DB 0 ; 0x34
157 bsDriveNumber DB 0 ; 0x40
158 bsreserved1 DB 0 ; 0x41
159 bsExtendedBootSignature DB 0 ; 0x42 29
160 bsVolumeSerialNumber DD 0 ; 0x43
161 bsVolumeLabel times 11 DB " " ; 0x47 "NO NAME "
162 bsFileSystemName times 8 DB " " ; 0x52 "FAT32 "
164 ;;;;;;;;;;;;;;;;;;;;
165 ;; BPB2 ends here ;;
166 ;;;;;;;;;;;;;;;;;;;;
168 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
169 ;; Boot sector code starts here ;;
170 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
172 start:
174 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
175 ;; How much RAM is there? ;;
176 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
178 int 12h ; get conventional memory size (in KBs)
179 %if AnyWhere
180 call here
181 here:
182 pop si
183 sub si, here - boot
184 push cs
185 pop ds
186 %endif
187 dec ax ; reserve 1K bytes for the code and the stack
188 mov cx, 106h
189 shl ax, cl ; and convert it to 16-byte paragraphs
191 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
192 ;; Reserve memory for the boot sector and its stack ;;
193 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
195 mov es, ax ; cs:0 = ds:0 = ss:0 -> top - 512 - StackSize
196 %if ExtraBootSector == 0
197 mov ss, ax
198 mov sp, 512+StackSize ; bytes 0-511 are reserved for the boot code
199 %endif
201 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
202 ;; Copy ourselves to top of memory ;;
203 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
205 xor di, di
206 %if AnyWhere == 0
207 mov si, 7C00h
208 mov ds, di
209 %endif
210 push es
211 mov [si(DriveNumber)], dx ; store BIOS boot drive number
212 cld
213 rep movsw ; move 512 bytes (+ 12)
215 ;;;;;;;;;;;;;;;;;;;;;;
216 ;; Jump to the copy ;;
217 ;;;;;;;;;;;;;;;;;;;;;;
219 %if ExtraBootSector != 0
220 push word main
221 %else
222 push byte main
223 %endif
224 retf
226 %if ExtraBootSector != 0
227 %macro MovedCode 0
228 %endif
229 main:
230 %if ExtraBootSector != 0
231 add al, 32
232 push ax
233 %endif
234 ;;;;;;;;;;;;;;;;;;;;;;;;;;
235 ;; Get drive parameters ;;
236 ;; Update heads count ;;
237 ;; for current BIOS ;;
238 ;;;;;;;;;;;;;;;;;;;;;;;;;;
240 %if CHSgeometryCheck != 0
241 mov ah, 8 ; update AX,BL,CX,DX,DI, and ES registers
242 int 13h ; may destroy SI,BP, and DS registers
243 %endif
244 push cs
245 pop ds
246 xor ebx, ebx
248 %if CHSgeometryCheck != 0
249 and cx, byte 3Fh
250 je BadParams ; verify updated and validity
251 mov [bx(bpbSectorsPerTrack)], cx
252 mov cl, dh
253 inc cx
254 mov [bx(bpbHeadsPerCylinder)], cx
255 xor cx, cx
256 BadParams:
257 %endif
258 %if ExtraBootSector != 0
259 pop es
260 mul ebx ; edx:eax = 0
261 inc ax
262 call ReadSectorBoot
263 push ds
264 pop ss
265 mov sp, 512+StackSize ; bytes 0-511 are reserved for the boot code
266 %endif
268 %if ClusterMask != 0
269 and byte [bx(bsRootDirectoryClusterNo+3)], 0Fh ; mask cluster value
270 %endif
271 mov esi, [bx(bsRootDirectoryClusterNo)] ; esi=cluster # of root dir
273 push byte ImageLoadSeg
274 pop es
276 RootDirReadContinue:
277 call ReadClusterSector ; read one sector of the root dir
279 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
280 ;; Look for the COM/EXE file to load and run ;;
281 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
283 xor di, di ; es:di -> root entries array
285 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
286 ;; Looks for a file/dir by its name ;;
287 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
288 ;; Input: DS:SI -> file name (11 chars) ;;
289 ;; ES:DI -> root directory array ;;
290 ;; BP = paragraphs in sector ;;
291 ;; Output: ESI = cluster number ;;
292 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
294 FindNameCycle:
295 %if NullEntryCheck != 0
296 cmp byte [es:di], bh
297 je ErrFind ; end of root directory (NULL entry found)
298 %endif
299 pusha
300 mov cl, NameLength
301 mov si, ProgramName ; ds:si -> program name
302 repe cmpsb
303 VolumeLabel equ 8
304 SubDirectory equ 10h
305 %if CheckAttrib != 0
306 jnz SkipFindName
307 test byte [es:di], VolumeLabel+SubDirectory
308 SkipFindName:
309 %endif
310 je FindNameFound
311 popa
312 add di, byte 32
313 dec bp
314 dec bp
315 jnz FindNameCycle ; next root entry
316 loop RootDirReadContinue ; next sector in cluster
317 cmp esi, 0FFFFFF6h ; carry=0 if last cluster, and carry=1 otherwise
318 jnc RootDirReadContinue ; continue to the next root dir cluster
319 ErrFind:
320 call Error ; end of root directory (dir end reached)
321 db "File not found."
322 %if ExtraBootSector != 0
323 %endm
324 %macro BootFileName 0
325 %endif
326 FindNameFound:
327 push word [es:di+14h-11]
328 push word [es:di+1Ah-11]
329 pop esi ; esi = cluster no. cx = 0
331 dec dword [es:di+1Ch-11] ; load ((n - 1)/256)*16 +1 paragraphs
332 imul di, [es:di+1Ch+1-11], byte 16 ; file size in paragraphs (full pages)
334 ;;;;;;;;;;;;;;;;;;;;;;;;;;
335 ;; Load the entire file ;;
336 ;;;;;;;;;;;;;;;;;;;;;;;;;;
338 push es
339 FileReadContinue:
340 push di
341 call ReadClusterSector ; read one sector of the boot file
342 dec cx
343 mov di, es
344 add di, bp
345 mov es, di ; es:bx updated
346 pop di
348 sub di, bp
349 jae FileReadContinue
350 xor ax, ax
351 pop bp
353 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
354 ;; Type detection, .COM or .EXE? ;;
355 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
357 mov ds, bp ; bp=ds=seg the file is loaded to
359 add bp, [bx+08h] ; bp = image base
360 mov di, [bx+18h] ; di = reloc table pointer
362 cmp word [bx], 5A4Dh ; "MZ" signature?
363 je RelocateEXE ; yes, it's an EXE program
365 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
366 ;; Setup and run a .COM program ;;
367 ;; Set CS=DS=ES=SS SP=0 IP=100h ;;
368 ;; AX=0ffffh BX=0 DX=drive and ;;
369 ;; cmdline=void ;;
370 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
372 mov di, 100h ; ip
373 mov bp, ImageLoadSeg-10h ; "org 100h" stuff :)
374 mov ss, bp
375 xor sp, sp
376 push bp ; cs, ds and es
377 jmp short Run
378 %if ExtraBootSector != 0
379 %endm
380 %macro BootCode 0
381 %endif
383 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
384 ;; Relocate, setup and run a .EXE program ;;
385 ;; Set CS:IP, SS:SP, DS, ES and AX according ;;
386 ;; to wiki.osdev.org/MZ#Initial_Program_State ;;
387 ;; AX=0ffffh BX=0 DX=drive cmdline=void ;;
388 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
390 ReloCycle:
391 add [di+2], bp ; item seg (abs)
392 les si, [di] ; si = item ofs, es = item seg
393 add [es:si], bp ; fixup
394 scasw ; di += 2
395 scasw ; point to next entry
397 RelocateEXE:
398 dec word [bx+06h] ; reloc items, 32768 max (128KB table)
399 jns ReloCycle
401 les si, [bx+0Eh]
402 add si, bp
403 mov ss, si ; ss for EXE
404 mov sp, es ; sp for EXE
406 lea si, [bp-10h] ; ds and es both point to the segment
407 push si ; containing the PSP structure
409 add bp, [bx+16h] ; cs for EXE
410 mov di, [bx+14h] ; ip for EXE
411 Run:
412 pop ds
413 push bp
414 push di
415 push ds
416 pop es
417 mov [80h], ax ; clear cmdline
418 dec ax ; both FCB in the PSP don't have a valid drive identifier
420 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
421 ;; Set the magic numbers so the program knows that it ;;
422 ;; has been loaded by this bootsector and not by MS-DOS ;;
423 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
424 mov si, 16381 ; prime number 2**14-3
425 mov di, 32749 ; prime number 2**15-19
426 mov bp, 65521 ; prime number 2**16-15
428 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
429 ;; All done, transfer control to the program now ;;
430 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
431 retf
432 %if ExtraBootSector != 0
433 %endm
434 %endif
436 ;;;;;;;;;;;;;;;;;;;;;;;;;;
437 ;; Error Messaging Code ;;
438 ;;;;;;;;;;;;;;;;;;;;;;;;;;
440 Error:
441 pop si
442 puts:
443 mov ah, 0Eh
444 mov bl, 7
445 lodsb
446 int 10h
447 cmp al, '.'
448 jne puts
449 %if WaitForKey != 0
450 cbw
451 int 16h ; wait for a key...
452 int 19h ; bootstrap
453 %endif
455 Stop:
456 hlt
457 jmp short Stop
459 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
460 ;; Reads a FAT32 sector ;;
461 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
462 ;; Inout: ES:BX -> buffer ;;
463 ;; EAX = prev sector ;;
464 ;; CX = rem sectors in cluster ;;
465 ;; ESI = next cluster ;;
466 ;; Output: EAX = current sector ;;
467 ;; CX = rem sectors in cluster ;;
468 ;; ESI = next cluster ;;
469 ;; BP -> para / sector ;;
470 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
472 ReadClusterSector:
473 %if SectorOf512Bytes != 0
474 mov bp, 32 ; bp = paragraphs per sector
475 %else
476 mov bp, [bx(bpbBytesPerSector)]
477 shr bp, 4 ; bp = paragraphs per sector
478 %endif
479 inc eax ; adjust LBA for next sector
480 inc cx
481 loop ReadSectorLBA
483 mul ebx ; edx:eax = 0
484 %if SectorOf512Bytes != 0
485 mov al, 128 ; ax=# of FAT32 entries per sector
486 %else
487 imul ax, bp, byte 4 ; ax=# of FAT32 entries per sector
488 %endif
489 lea edi, [esi-2] ; esi=cluster #
490 xchg eax, esi
491 div esi ; eax=FAT sector #, edx=entry # in sector
493 imul si, dx, byte 4 ; si=entry # in sector, clear C
494 %if NonMirroredFATs != 0
495 cdq
496 or dl, byte [bx(bsExtendedFlags)]
497 jns MirroredFATs ; Non-mirrored FATs ?
498 and dl, 0Fh
499 imul edx, dword [bx(bsSectorsPerFAT32)] ; we need to read the active one
500 add eax, edx
501 MirroredFATs:
502 cdq
503 %else
504 %if LBA48bits != 0
505 cdq
506 %endif
507 %endif
508 call ReadSectorLBAfromFAT ; read 1 FAT32 sector
510 %if ClusterMask != 0
511 and byte [es:si+3], 0Fh ; mask cluster value
512 %endif
513 mov esi, [es:si] ; esi=next cluster #
515 %if Always2FATs != 0
516 imul eax, dword [bx(bsSectorsPerFAT32)], 2
517 %else
518 movzx eax, byte [bx(bpbNumberOfFATs)]
519 mul dword [bx(bsSectorsPerFAT32)]
520 %endif
522 xchg eax, edi
523 movzx ecx, byte [bx(bpbSectorsPerCluster)] ; 8..128
524 mul ecx ; edx:eax=sector number in data area
525 add eax, edi
527 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
528 ;; Reads a sector form the start of FAT ;;
529 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
531 ReadSectorLBAfromFAT:
532 %if LBA48bits != 0
533 push dx
534 %endif
535 mov dx, [bx(bpbReservedSectors)]
536 add eax, edx
537 %if LBA48bits != 0
538 pop dx ; 16TB FAT32 max
539 adc dx, bx
540 ReadSectorBoot:
541 mov [bx(HiLBA)], dx
542 %endif
544 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
545 ;; Reads a sector using BIOS Int 13h fn 42h ;;
546 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
547 ;; Input: EAX+EDX = LBA ;;
548 ;; ES:BX -> buffer address ;;
549 ;; Keep: ESI = next cluster ;;
550 ;; EDI = data cluster ;;
551 ;; BP = paragraphs / sector ;;
552 ;; CX = sector count ;;
553 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
555 ReadSectorLBA:
556 %if LBA48bits != 0
557 mov dx, [bx(HiLBA)]
558 %else
559 xor dx, dx ; 2TB FAT32 max
560 ReadSectorBoot:
561 %endif
562 pushad
564 add eax, [bx(bpbHiddenSectors)]
565 adc dx, bx ; 2TB partition offset max
567 push edx
568 push eax
569 push es
570 push bx
571 push byte 1 ; sector count word = 1
572 push byte 16 ; packet size byte = 16, reserved byte = 0
574 %if CHSsupport != 0
575 ; Busybox mkdosfs creates fat32 for floppies.
576 ; Floppies may support CHS only.
577 %if LBA48bits != 0
578 ; The following check is only useful with bpbSectorsPerTrack < 8 with disk > 4TB...
579 ; setnz dl ; force cylinder overflow without divide error
580 %endif
581 movzx ecx, word [bx(bpbSectorsPerTrack)]
582 idiv ecx
583 ; eax = LBA / SPT
584 ; edx = LBA % SPT = sector - 1
585 inc dx
586 push dx ; save sector no.
587 cdq
588 mov cx, word [bx(bpbHeadsPerCylinder)]
589 idiv ecx
590 pop cx
591 ; eax = (LBA / SPT) / HPC = cylinder
592 ; dl = (LBA / SPT) % HPC = head
594 xchg ch, al ; clear al
595 ; ch = LSB 0...7 of cylinder no.
596 %if CHSsanityCheck != 0
597 shl eax, 6
598 %else
599 shl ax, 6
600 %endif
601 or cl, ah
602 ; cl = MSB 8...9 of cylinder no. + sector no.
603 mov dh, dl
604 ; dh = head no.
605 %endif
607 ReadSectorRetry:
608 mov dl, [bx(DriveNumber)] ; restore BIOS boot drive number
609 mov si, sp
610 mov ah, 42h ; ah = 42h = extended read function no.
611 int 13h ; extended read sectors (DL, DS:SI)
612 jnc ReadSuccess ; CF = 0 if no error
614 %if CHSsupport != 0
615 mov ax, 201h ; al = sector count = 1
616 ; ah = 2 = read function no.
617 %if CHSsanityCheck != 0
618 cmp eax, 0FFFFh ; max cylinder = 1023
619 ja SkipCHS
620 %endif
621 int 13h ; read sectors (AL, CX, DX, ES:BX)
623 jnc ReadSuccess ; CF = 0 if no error
624 SkipCHS:
625 %endif
626 %if ReadRetry != 0
627 %if CHSsupport != 0
628 cbw ; ah = 0 = reset function
629 %else
630 xor ax, ax ; ah = 0 = reset function
631 %endif
632 int 13h ; reset drive (DL)
634 dec bp ; up to 32 retries
635 jnz ReadSectorRetry
636 %endif
638 call Error
639 db "Read error."
641 ReadSuccess:
643 popa ; sp += 16
644 popad
645 mov dx, [bx(DriveNumber)] ; restore BIOS boot drive number
646 ret
648 %if ExtraBootSector != 0
649 MovedCode
650 BootCode
651 %endif
653 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
654 ;; Fill free space with zeroes ;;
655 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
657 times (512-13-($-$$)) db 0
659 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
660 ;; Name of the file to load and run ;;
661 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
663 NameLength equ 11
664 ProgramName times NameLength db 0 ; name and extension
666 ;;;;;;;;;;;;;;;;;;;;;;;;;;
667 ;; End of the sector ID ;;
668 ;;;;;;;;;;;;;;;;;;;;;;;;;;
670 dw 0AA55h ; BIOS checks for this ID
672 %if ExtraBootSector != 0
673 dd 61415252h ; "RRaA"
674 BootFileName
675 times (996-($-$$)) db 0
676 ; dd 41617272h ; "rrAa"
677 %endif