wok view BootProg/stuff/boot32.asm @ rev 25699
Up redis (7.2.5)
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Sun May 26 16:05:16 2024 +0000 (13 months ago) |
parents | 82a123e54615 |
children | 0af8df0b46fc |
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 ; +3 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 - 2 ; exclude DriveNumber
184 %endif
185 dec ax ; reserve 1K bytes for the code and the stack
186 mov cx, 106h
187 shl ax, cl ; and convert it to 16-byte paragraphs
189 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
190 ;; Reserve memory for the boot sector and its stack ;;
191 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
193 mov es, ax ; cs:0 = ds:0 = ss:0 -> top - 512 - StackSize
194 %if ExtraBootSector == 0
195 mov ss, ax
196 mov sp, 512+StackSize ; bytes 0-511 are reserved for the boot code
197 %endif
199 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
200 ;; Copy ourselves to top of memory ;;
201 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
203 xor di, di
204 %if AnyWhere == 0
205 mov si, 7C02h ; exclude DriveNumber
206 mov ds, di
207 %endif
208 push es
209 cld
210 xchg ax, dx
211 stosw ; store BIOS boot drive number
212 %if AnyWhere
213 rep cs movsw ; move 512 bytes (+ 12)
214 %else
215 rep movsw ; move 512 bytes (+ 12)
216 %endif
218 ;;;;;;;;;;;;;;;;;;;;;;
219 ;; Jump to the copy ;;
220 ;;;;;;;;;;;;;;;;;;;;;;
222 %if ExtraBootSector != 0
223 push word main
224 %else
225 push byte main
226 %endif
227 retf
229 %if ExtraBootSector != 0
230 %macro MovedCode 0
231 main:
232 add al, 32
233 push ax
234 %else
235 main:
236 %endif
237 ;;;;;;;;;;;;;;;;;;;;;;;;;;
238 ;; Get drive parameters ;;
239 ;; Update heads count ;;
240 ;; for current BIOS ;;
241 ;;;;;;;;;;;;;;;;;;;;;;;;;;
243 %if CHSgeometryCheck != 0
244 mov ah, 8 ; update AX,BL,CX,DX,DI, and ES registers
245 int 13h ; may destroy SI,BP, and DS registers
246 %endif
247 push cs
248 pop ds
249 xor ebx, ebx
251 %if CHSgeometryCheck != 0
252 and cx, byte 3Fh
253 je BadParams ; verify updated and validity
254 mov [bx(bpbSectorsPerTrack)], cx
255 mov cl, dh
256 inc cx
257 mov [bx(bpbHeadsPerCylinder)], cx
258 xor cx, cx
259 BadParams:
260 %endif
261 %if ExtraBootSector != 0
262 pop es
263 mul ebx ; edx:eax = 0
264 inc ax
265 call ReadSectorBoot
266 push ds
267 pop ss
268 mov sp, 512+StackSize ; bytes 0-511 are reserved for the boot code
269 %endif
271 %if ClusterMask != 0
272 and byte [bx(bsRootDirectoryClusterNo+3)], 0Fh ; mask cluster value
273 %endif
274 mov esi, [bx(bsRootDirectoryClusterNo)] ; esi=cluster # of root dir
276 push byte ImageLoadSeg
277 pop es
279 RootDirReadContinue:
280 call ReadClusterSector ; read one sector of the root dir
282 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
283 ;; Look for the COM/EXE file to load and run ;;
284 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
286 xor di, di ; es:di -> root entries array
288 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
289 ;; Looks for a file/dir by its name ;;
290 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
291 ;; Input: DS:SI -> file name (11 chars) ;;
292 ;; ES:DI -> root directory array ;;
293 ;; BP = paragraphs in sector ;;
294 ;; Output: ESI = cluster number ;;
295 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
297 FindNameCycle:
298 %if NullEntryCheck != 0
299 cmp byte [es:di], bh
300 je ErrFind ; end of root directory (NULL entry found)
301 %endif
302 pusha
303 mov cl, NameLength
304 mov si, ProgramName ; ds:si -> program name
305 repe cmpsb
306 VolumeLabel equ 8
307 SubDirectory equ 10h
308 %if CheckAttrib != 0
309 jnz SkipFindName
310 test byte [es:di], VolumeLabel+SubDirectory
311 SkipFindName:
312 %endif
313 je FindNameFound
314 popa
315 add di, byte 32
316 dec bp
317 dec bp
318 jnz FindNameCycle ; next root entry
319 loop RootDirReadContinue ; next sector in cluster
320 cmp esi, 0FFFFFF6h ; carry=0 if last cluster, and carry=1 otherwise
321 jnc RootDirReadContinue ; continue to the next root dir cluster
322 ErrFind:
323 call Error ; end of root directory (dir end reached)
324 db "File not found."
325 %if ExtraBootSector != 0
326 %endm
327 %macro BootFileName 0
328 %endif
329 FindNameFound:
330 push word [es:di+14h-11]
331 push word [es:di+1Ah-11]
332 pop esi ; esi = cluster no. cx = 0
334 dec dword [es:di+1Ch-11] ; load ((n - 1)/256)*16 +1 paragraphs
335 imul di, [es:di+1Ch+1-11], byte 16 ; file size in paragraphs (full pages)
337 ;;;;;;;;;;;;;;;;;;;;;;;;;;
338 ;; Load the entire file ;;
339 ;;;;;;;;;;;;;;;;;;;;;;;;;;
341 push es
342 FileReadContinue:
343 push di
344 call ReadClusterSector ; read one sector of the boot file
345 dec cx
346 mov di, es
347 add di, bp
348 mov es, di ; es:bx updated
349 pop di
351 sub di, bp
352 jae FileReadContinue
353 xor ax, ax
354 pop bp
356 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
357 ;; Type detection, .COM or .EXE? ;;
358 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
360 mov ds, bp ; bp=ds=seg the file is loaded to
362 add bp, [bx+08h] ; bp = image base
363 mov di, [bx+18h] ; di = reloc table pointer
365 cmp word [bx], 5A4Dh ; "MZ" signature?
366 je RelocateEXE ; yes, it's an EXE program
368 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
369 ;; Setup and run a .COM program ;;
370 ;; Set CS=DS=ES=SS SP=0 IP=100h ;;
371 ;; AX=0ffffh BX=0 DX=drive and ;;
372 ;; cmdline=void ;;
373 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
375 mov di, 100h ; ip
376 mov bp, ImageLoadSeg-10h ; "org 100h" stuff :)
377 mov ss, bp
378 xor sp, sp
379 push bp ; cs, ds and es
380 jmp short Run
381 %if ExtraBootSector != 0
382 %endm
383 %macro BootCode 0
384 %endif
386 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
387 ;; Relocate, setup and run a .EXE program ;;
388 ;; Set CS:IP, SS:SP, DS, ES and AX according ;;
389 ;; to wiki.osdev.org/MZ#Initial_Program_State ;;
390 ;; AX=0ffffh BX=0 DX=drive cmdline=void ;;
391 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
393 ReloCycle:
394 add [di+2], bp ; item seg (abs)
395 les si, [di] ; si = item ofs, es = item seg
396 add [es:si], bp ; fixup
397 scasw ; di += 2
398 scasw ; point to next entry
400 RelocateEXE:
401 dec word [bx+06h] ; reloc items, 32768 max (128KB table)
402 jns ReloCycle
404 les si, [bx+0Eh]
405 add si, bp
406 mov ss, si ; ss for EXE
407 mov sp, es ; sp for EXE
409 lea si, [bp-10h] ; ds and es both point to the segment
410 push si ; containing the PSP structure
412 add bp, [bx+16h] ; cs for EXE
413 mov di, [bx+14h] ; ip for EXE
414 Run:
415 pop ds
416 push bp
417 push di
418 push ds
419 pop es
420 mov [80h], ax ; clear cmdline
421 dec ax ; both FCB in the PSP don't have a valid drive identifier
423 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
424 ;; Set the magic numbers so the program knows that it ;;
425 ;; has been loaded by this bootsector and not by MS-DOS ;;
426 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
427 mov si, 16381 ; prime number 2**14-3
428 mov di, 32749 ; prime number 2**15-19
429 mov bp, 65521 ; prime number 2**16-15
431 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
432 ;; All done, transfer control to the program now ;;
433 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
434 retf
435 %if ExtraBootSector != 0
436 %endm
437 %endif
439 ;;;;;;;;;;;;;;;;;;;;;;;;;;
440 ;; Error Messaging Code ;;
441 ;;;;;;;;;;;;;;;;;;;;;;;;;;
443 Error:
444 pop si
445 puts:
446 mov ah, 0Eh
447 mov bl, 7
448 lodsb
449 int 10h
450 cmp al, '.'
451 jne puts
452 %if WaitForKey != 0
453 cbw
454 int 16h ; wait for a key...
455 int 19h ; bootstrap
456 %endif
458 Stop:
459 hlt
460 jmp short Stop
462 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
463 ;; Reads a FAT32 sector ;;
464 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
465 ;; Inout: ES:BX -> buffer ;;
466 ;; EAX = prev sector ;;
467 ;; CX = rem sectors in cluster ;;
468 ;; ESI = next cluster ;;
469 ;; Output: EAX = current sector ;;
470 ;; CX = rem sectors in cluster ;;
471 ;; ESI = next cluster ;;
472 ;; BP -> para / sector ;;
473 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
475 ReadClusterSector:
476 %if SectorOf512Bytes != 0
477 mov bp, 32 ; bp = paragraphs per sector
478 %else
479 mov bp, [bx(bpbBytesPerSector)]
480 shr bp, 4 ; bp = paragraphs per sector
481 %endif
482 inc eax ; adjust LBA for next sector
483 inc cx
484 loop ReadSectorLBA
486 mul ebx ; edx:eax = 0
487 %if SectorOf512Bytes != 0
488 mov al, 128 ; ax=# of FAT32 entries per sector
489 %else
490 imul ax, bp, byte 4 ; ax=# of FAT32 entries per sector
491 %endif
492 lea edi, [esi-2] ; esi=cluster #
493 xchg eax, esi
494 div esi ; eax=FAT sector #, edx=entry # in sector
496 imul si, dx, byte 4 ; si=entry # in sector, clear C
497 %if NonMirroredFATs != 0
498 cdq
499 or dl, byte [bx(bsExtendedFlags)]
500 jns MirroredFATs ; Non-mirrored FATs ?
501 and dl, 0Fh
502 imul edx, dword [bx(bsSectorsPerFAT32)] ; we need to read the active one
503 add eax, edx
504 MirroredFATs:
505 cdq
506 %else
507 %if LBA48bits != 0
508 cdq
509 %endif
510 %endif
511 call ReadSectorLBAfromFAT ; read 1 FAT32 sector
513 %if ClusterMask != 0
514 and byte [es:si+3], 0Fh ; mask cluster value
515 %endif
516 mov esi, [es:si] ; esi=next cluster #
518 %if Always2FATs != 0
519 imul eax, dword [bx(bsSectorsPerFAT32)], 2
520 %else
521 movzx eax, byte [bx(bpbNumberOfFATs)]
522 mul dword [bx(bsSectorsPerFAT32)]
523 %endif
525 xchg eax, edi
526 movzx ecx, byte [bx(bpbSectorsPerCluster)] ; 8..128
527 mul ecx ; edx:eax=sector number in data area
528 add eax, edi
530 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
531 ;; Reads a sector form the start of FAT ;;
532 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
534 ReadSectorLBAfromFAT:
535 %if LBA48bits != 0
536 push dx
537 %endif
538 mov dx, [bx(bpbReservedSectors)]
539 add eax, edx
540 %if LBA48bits != 0
541 pop dx ; 16TB FAT32 max
542 adc dx, bx
543 ReadSectorBoot:
544 mov [bx(HiLBA)], dx
545 %endif
547 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
548 ;; Reads a sector using BIOS Int 13h fn 42h ;;
549 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
550 ;; Input: EAX+EDX = LBA ;;
551 ;; ES:BX -> buffer address ;;
552 ;; Keep: ESI = next cluster ;;
553 ;; EDI = data cluster ;;
554 ;; BP = paragraphs / sector ;;
555 ;; CX = sector count ;;
556 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
558 ReadSectorLBA:
559 %if LBA48bits != 0
560 mov dx, [bx(HiLBA)]
561 %else
562 xor dx, dx ; 2TB FAT32 max
563 ReadSectorBoot:
564 %endif
565 pushad
567 add eax, [bx(bpbHiddenSectors)]
568 adc dx, bx ; 2TB partition offset max
570 push edx
571 push eax
572 push es
573 push bx
574 push byte 1 ; sector count word = 1
575 push byte 16 ; packet size byte = 16, reserved byte = 0
577 %if CHSsupport != 0
578 ; Busybox mkdosfs creates fat32 for floppies.
579 ; Floppies may support CHS only.
580 %if LBA48bits != 0
581 ; The following check is only useful with bpbSectorsPerTrack < 8 with disk > 4TB...
582 ; setnz dl ; force cylinder overflow without divide error
583 %endif
584 movzx ecx, word [bx(bpbSectorsPerTrack)]
585 idiv ecx
586 ; eax = LBA / SPT
587 ; edx = LBA % SPT = sector - 1
588 inc dx
589 push dx ; save sector no.
590 cdq
591 mov cx, word [bx(bpbHeadsPerCylinder)]
592 idiv ecx
593 pop cx
594 ; eax = (LBA / SPT) / HPC = cylinder
595 ; dl = (LBA / SPT) % HPC = head
597 xchg ch, al ; clear al
598 ; ch = LSB 0...7 of cylinder no.
599 %if CHSsanityCheck != 0
600 shl eax, 6
601 %else
602 shl ax, 6
603 %endif
604 or cl, ah
605 ; cl = MSB 8...9 of cylinder no. + sector no.
606 mov dh, dl
607 ; dh = head no.
608 %endif
610 ReadSectorRetry:
611 mov dl, [bx(DriveNumber)] ; restore BIOS boot drive number
612 mov si, sp
613 mov ah, 42h ; ah = 42h = extended read function no.
614 int 13h ; extended read sectors (DL, DS:SI)
615 jnc ReadSuccess ; CF = 0 if no error
617 %if CHSsupport != 0
618 mov ax, 201h ; al = sector count = 1
619 ; ah = 2 = read function no.
620 %if CHSsanityCheck != 0
621 cmp eax, 0FFFFh ; max cylinder = 1023
622 ja SkipCHS
623 %endif
624 int 13h ; read sectors (AL, CX, DX, ES:BX)
626 jnc ReadSuccess ; CF = 0 if no error
627 SkipCHS:
628 %endif
629 %if ReadRetry != 0
630 %if CHSsupport != 0
631 cbw ; ah = 0 = reset function
632 %else
633 xor ax, ax ; ah = 0 = reset function
634 %endif
635 int 13h ; reset drive (DL)
637 dec bp ; up to 32 retries
638 jnz ReadSectorRetry
639 %endif
641 call Error
642 db "Read error."
644 ReadSuccess:
646 popa ; sp += 16
647 popad
648 mov dx, [bx(DriveNumber)] ; restore BIOS boot drive number
649 ret
651 %if ExtraBootSector != 0
652 MovedCode
653 BootCode
654 %endif
656 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
657 ;; Fill free space with zeroes ;;
658 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
660 times (512-13-($-$$)) db 0
662 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
663 ;; Name of the file to load and run ;;
664 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
666 NameLength equ 11
667 ProgramName times NameLength db 0 ; name and extension
669 ;;;;;;;;;;;;;;;;;;;;;;;;;;
670 ;; End of the sector ID ;;
671 ;;;;;;;;;;;;;;;;;;;;;;;;;;
673 dw 0AA55h ; BIOS checks for this ID
675 %if ExtraBootSector != 0
676 dd 61415252h ; "RRaA"
677 BootFileName
678 times (996-($-$$)) db 0
679 ; dd 41617272h ; "rrAa"
680 %endif