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