wok view BootProg/stuff/boot32.asm @ rev 25572

BootProg: fix boot16.asm (again)
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sat May 13 13:34:41 2023 +0000 (12 months ago)
parents 64471e67874e
children 82a123e54615
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
104 [BITS 16]
105 [CPU 386]
107 ImageLoadSeg equ 60h ; <=07Fh because of "push byte ImageLoadSeg" instructions
108 StackSize equ 512
110 [SECTION .text]
111 [ORG 0]
113 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
114 ;; Boot sector starts here ;;
115 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
117 boot:
118 DriveNumber equ boot+0
119 HiLBA equ boot+2
120 jmp short start ; MS-DOS/Windows checks for this jump
121 nop
122 bsOemName DB "BootProg" ; 0x03
124 ;;;;;;;;;;;;;;;;;;;;;;
125 ;; BPB1 starts here ;;
126 ;;;;;;;;;;;;;;;;;;;;;;
128 bpbBytesPerSector DW 0 ; 0x0B 512, 1024, 2048 or 4096
129 bpbSectorsPerCluster DB 0 ; 0x0D 1, 2, 4, 8, 16, 32, 64 or 128
130 bpbReservedSectors DW 0 ; 0x0E 32
131 bpbNumberOfFATs DB 0 ; 0x10 2
132 bpbRootEntries DW 0 ; 0x11 0
133 bpbTotalSectors DW 0 ; 0x13 0
134 bpbMedia DB 0 ; 0x15 F8, F0
135 bpbSectorsPerFAT DW 0 ; 0x16 0
136 bpbSectorsPerTrack DW 0 ; 0x18
137 bpbHeadsPerCylinder DW 0 ; 0x1A
138 bpbHiddenSectors DD 0 ; 0x1C
139 bpbTotalSectorsBig DD 0 ; 0x20
141 ;;;;;;;;;;;;;;;;;;;;
142 ;; BPB1 ends here ;;
143 ;;;;;;;;;;;;;;;;;;;;
145 ;;;;;;;;;;;;;;;;;;;;;;
146 ;; BPB2 starts here ;;
147 ;;;;;;;;;;;;;;;;;;;;;;
149 bsSectorsPerFAT32 DD 0 ; 0x24
150 bsExtendedFlags DW 0 ; 0x28
151 bsFSVersion DW 0 ; 0x2A
152 bsRootDirectoryClusterNo DD 0 ; 0x2C
153 bsFSInfoSectorNo DW 0 ; 0x30
154 bsBackupBootSectorNo DW 0 ; 0x32
155 bsreserved times 12 DB 0 ; 0x34
156 bsDriveNumber DB 0 ; 0x40
157 bsreserved1 DB 0 ; 0x41
158 bsExtendedBootSignature DB 0 ; 0x42 29
159 bsVolumeSerialNumber DD 0 ; 0x43
160 bsVolumeLabel times 11 DB " " ; 0x47 "NO NAME "
161 bsFileSystemName times 8 DB " " ; 0x52 "FAT32 "
163 ;;;;;;;;;;;;;;;;;;;;
164 ;; BPB2 ends here ;;
165 ;;;;;;;;;;;;;;;;;;;;
167 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
168 ;; Boot sector code starts here ;;
169 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
171 start:
172 cld
174 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
175 ;; How much RAM is there? ;;
176 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
178 int 12h ; get conventional memory size (in KBs)
179 dec ax ; reserve 1K bytes for the code and the stack
180 mov cx, 106h
181 shl ax, cl ; and convert it to 16-byte paragraphs
183 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
184 ;; Reserve memory for the boot sector and its stack ;;
185 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
187 mov es, ax ; cs:0 = ds:0 = ss:0 -> top - 512 - StackSize
188 %if ExtraBootSector == 0
189 mov ss, ax
190 mov sp, 512+StackSize ; bytes 0-511 are reserved for the boot code
191 %endif
193 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
194 ;; Copy ourselves to top of memory ;;
195 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
197 mov si, 7C00h
198 xor di, di
199 mov ds, di
200 push es
201 mov [si(DriveNumber)], dx ; store BIOS boot drive number
202 rep movsw
204 ;;;;;;;;;;;;;;;;;;;;;;
205 ;; Jump to the copy ;;
206 ;;;;;;;;;;;;;;;;;;;;;;
208 %if ExtraBootSector != 0
209 push word main
210 %else
211 push byte main
212 %endif
213 retf
215 %if ExtraBootSector != 0
216 %macro MovedCode 0
217 %endif
218 main:
219 %if ExtraBootSector != 0
220 add al, 32
221 push ax
222 %endif
223 ;;;;;;;;;;;;;;;;;;;;;;;;;;
224 ;; Get drive parameters ;;
225 ;; Update heads count ;;
226 ;; for current BIOS ;;
227 ;;;;;;;;;;;;;;;;;;;;;;;;;;
229 %if CHSgeometryCheck != 0
230 mov ah, 8 ; update AX,BL,CX,DX,DI, and ES registers
231 int 13h ; may destroy SI,BP, and DS registers
232 %endif
233 push cs
234 pop ds
235 xor ebx, ebx
237 %if CHSgeometryCheck != 0
238 and cx, byte 3Fh
239 je BadParams ; verify updated and validity
240 mov [bx(bpbSectorsPerTrack)], cx
241 mov cl, dh
242 inc cx
243 mov [bx(bpbHeadsPerCylinder)], cx
244 xor cx, cx
245 BadParams:
246 %endif
247 %if ExtraBootSector != 0
248 pop es
249 mul ebx ; edx:eax = 0
250 inc ax
251 call ReadSectorBoot
252 push ds
253 pop ss
254 mov sp, 512+StackSize ; bytes 0-511 are reserved for the boot code
255 %endif
257 %if ClusterMask != 0
258 and byte [bx(bsRootDirectoryClusterNo+3)], 0Fh ; mask cluster value
259 %endif
260 mov esi, [bx(bsRootDirectoryClusterNo)] ; esi=cluster # of root dir
262 push byte ImageLoadSeg
263 pop es
265 RootDirReadContinue:
266 call ReadClusterSector ; read one sector of the root dir
268 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
269 ;; Look for the COM/EXE file to load and run ;;
270 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
272 xor di, di ; es:di -> root entries array
274 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
275 ;; Looks for a file/dir by its name ;;
276 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
277 ;; Input: DS:SI -> file name (11 chars) ;;
278 ;; ES:DI -> root directory array ;;
279 ;; BP = paragraphs in sector ;;
280 ;; Output: ESI = cluster number ;;
281 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
283 FindNameCycle:
284 %if NullEntryCheck != 0
285 cmp byte [es:di], bh
286 je ErrFind ; end of root directory (NULL entry found)
287 %endif
288 pusha
289 mov cl, NameLength
290 mov si, ProgramName ; ds:si -> program name
291 repe cmpsb
292 VolumeLabel equ 8
293 SubDirectory equ 10h
294 %if CheckAttrib != 0
295 jnz SkipFindName
296 test byte [es:di], VolumeLabel+SubDirectory
297 SkipFindName:
298 %endif
299 je FindNameFound
300 popa
301 add di, byte 32
302 dec bp
303 dec bp
304 jnz FindNameCycle ; next root entry
305 loop RootDirReadContinue ; next sector in cluster
306 cmp esi, 0FFFFFF6h ; carry=0 if last cluster, and carry=1 otherwise
307 jnc RootDirReadContinue ; continue to the next root dir cluster
308 ErrFind:
309 call Error ; end of root directory (dir end reached)
310 db "File not found."
311 %if ExtraBootSector != 0
312 %endm
313 %macro BootFileName 0
314 %endif
315 FindNameFound:
316 push word [es:di+14h-11]
317 push word [es:di+1Ah-11]
318 pop esi ; esi = cluster no. cx = 0
320 dec dword [es:di+1Ch-11] ; load ((n - 1)/256)*16 +1 paragraphs
321 imul di, [es:di+1Ch+1-11], byte 16 ; file size in paragraphs (full pages)
323 ;;;;;;;;;;;;;;;;;;;;;;;;;;
324 ;; Load the entire file ;;
325 ;;;;;;;;;;;;;;;;;;;;;;;;;;
327 push es
328 FileReadContinue:
329 push di
330 call ReadClusterSector ; read one sector of the boot file
331 dec cx
332 mov di, es
333 add di, bp
334 mov es, di ; es:bx updated
335 pop di
337 sub di, bp
338 jae FileReadContinue
339 xor ax, ax
340 pop bp
342 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
343 ;; Type detection, .COM or .EXE? ;;
344 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
346 mov ds, bp ; bp=ds=seg the file is loaded to
348 add bp, [bx+08h] ; bp = image base
349 mov di, [bx+18h] ; di = reloc table pointer
351 cmp word [bx], 5A4Dh ; "MZ" signature?
352 je RelocateEXE ; yes, it's an EXE program
354 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
355 ;; Setup and run a .COM program ;;
356 ;; Set CS=DS=ES=SS SP=0 IP=100h ;;
357 ;; AX=0ffffh BX=0 DX=drive and ;;
358 ;; cmdline=void ;;
359 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
361 mov di, 100h ; ip
362 mov bp, ImageLoadSeg-10h ; "org 100h" stuff :)
363 mov ss, bp
364 xor sp, sp
365 push bp ; cs, ds and es
366 jmp short Run
367 %if ExtraBootSector != 0
368 %endm
369 %macro BootCode 0
370 %endif
372 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
373 ;; Relocate, setup and run a .EXE program ;;
374 ;; Set CS:IP, SS:SP, DS, ES and AX according ;;
375 ;; to wiki.osdev.org/MZ#Initial_Program_State ;;
376 ;; AX=0ffffh BX=0 DX=drive cmdline=void ;;
377 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
379 ReloCycle:
380 add [di+2], bp ; item seg (abs)
381 les si, [di] ; si = item ofs, es = item seg
382 add [es:si], bp ; fixup
383 scasw ; di += 2
384 scasw ; point to next entry
386 RelocateEXE:
387 dec word [bx+06h] ; reloc items, 32768 max (128KB table)
388 jns ReloCycle
390 les si, [bx+0Eh]
391 add si, bp
392 mov ss, si ; ss for EXE
393 mov sp, es ; sp for EXE
395 lea si, [bp-10h] ; ds and es both point to the segment
396 push si ; containing the PSP structure
398 add bp, [bx+16h] ; cs for EXE
399 mov di, [bx+14h] ; ip for EXE
400 Run:
401 pop ds
402 push bp
403 push di
404 push ds
405 pop es
406 mov [80h], ax ; clear cmdline
407 dec ax ; both FCB in the PSP don't have a valid drive identifier
409 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
410 ;; Set the magic numbers so the program knows that it ;;
411 ;; has been loaded by this bootsector and not by MS-DOS ;;
412 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
413 mov si, 16381 ; prime number 2**14-3
414 mov di, 32749 ; prime number 2**15-19
415 mov bp, 65521 ; prime number 2**16-15
417 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
418 ;; All done, transfer control to the program now ;;
419 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
420 retf
421 %if ExtraBootSector != 0
422 %endm
423 %endif
425 ;;;;;;;;;;;;;;;;;;;;;;;;;;
426 ;; Error Messaging Code ;;
427 ;;;;;;;;;;;;;;;;;;;;;;;;;;
429 Error:
430 pop si
431 puts:
432 mov ah, 0Eh
433 mov bl, 7
434 lodsb
435 int 10h
436 cmp al, '.'
437 jne puts
438 %if WaitForKey != 0
439 cbw
440 int 16h ; wait for a key...
441 int 19h ; bootstrap
442 %endif
444 Stop:
445 hlt
446 jmp short Stop
448 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
449 ;; Reads a FAT32 sector ;;
450 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
451 ;; Inout: ES:BX -> buffer ;;
452 ;; EAX = prev sector ;;
453 ;; CX = rem sectors in cluster ;;
454 ;; ESI = next cluster ;;
455 ;; Output: EAX = current sector ;;
456 ;; CX = rem sectors in cluster ;;
457 ;; ESI = next cluster ;;
458 ;; BP -> para / sector ;;
459 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
461 ReadClusterSector:
462 %if SectorOf512Bytes != 0
463 mov bp, 32 ; bp = paragraphs per sector
464 %else
465 mov bp, [bx(bpbBytesPerSector)]
466 shr bp, 4 ; bp = paragraphs per sector
467 %endif
468 inc eax ; adjust LBA for next sector
469 inc cx
470 loop ReadSectorLBA
472 mul ebx ; edx:eax = 0
473 %if SectorOf512Bytes != 0
474 mov al, 128 ; ax=# of FAT32 entries per sector
475 %else
476 imul ax, bp, byte 4 ; ax=# of FAT32 entries per sector
477 %endif
478 lea edi, [esi-2] ; esi=cluster #
479 xchg eax, esi
480 div esi ; eax=FAT sector #, edx=entry # in sector
482 imul si, dx, byte 4 ; si=entry # in sector, clear C
483 %if NonMirroredFATs != 0
484 cdq
485 or dl, byte [bx(bsExtendedFlags)]
486 jns MirroredFATs ; Non-mirrored FATs ?
487 and dl, 0Fh
488 imul edx, dword [bx(bsSectorsPerFAT32)] ; we need to read the active one
489 add eax, edx
490 MirroredFATs:
491 cdq
492 %else
493 %if LBA48bits != 0
494 cdq
495 %endif
496 %endif
497 call ReadSectorLBAfromFAT ; read 1 FAT32 sector
499 %if ClusterMask != 0
500 and byte [es:si+3], 0Fh ; mask cluster value
501 %endif
502 mov esi, [es:si] ; esi=next cluster #
504 %if Always2FATs != 0
505 imul eax, dword [bx(bsSectorsPerFAT32)], 2
506 %else
507 movzx eax, byte [bx(bpbNumberOfFATs)]
508 mul dword [bx(bsSectorsPerFAT32)]
509 %endif
511 xchg eax, edi
512 movzx ecx, byte [bx(bpbSectorsPerCluster)] ; 8..128
513 mul ecx ; edx:eax=sector number in data area
514 add eax, edi
516 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
517 ;; Reads a sector form the start of FAT ;;
518 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
520 ReadSectorLBAfromFAT:
521 %if LBA48bits != 0
522 push dx
523 %endif
524 mov dx, [bx(bpbReservedSectors)]
525 add eax, edx
526 %if LBA48bits != 0
527 pop dx ; 16TB FAT32 max
528 adc dx, bx
529 ReadSectorBoot:
530 mov [bx(HiLBA)], dx
531 %endif
533 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
534 ;; Reads a sector using BIOS Int 13h fn 42h ;;
535 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
536 ;; Input: EAX+EDX = LBA ;;
537 ;; ES:BX -> buffer address ;;
538 ;; Keep: ESI = next cluster ;;
539 ;; EDI = data cluster ;;
540 ;; BP = paragraphs / sector ;;
541 ;; CX = sector count ;;
542 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
544 ReadSectorLBA:
545 %if LBA48bits != 0
546 mov dx, [bx(HiLBA)]
547 %else
548 xor dx, dx ; 2TB FAT32 max
549 ReadSectorBoot:
550 %endif
551 pushad
553 add eax, [bx(bpbHiddenSectors)]
554 adc dx, bx ; 2TB partition offset max
556 push edx
557 push eax
558 push es
559 push bx
560 push byte 1 ; sector count word = 1
561 push byte 16 ; packet size byte = 16, reserved byte = 0
563 %if CHSsupport != 0
564 ; Busybox mkdosfs creates fat32 for floppies.
565 ; Floppies may support CHS only.
566 %if LBA48bits != 0
567 ; The following check is only useful with bpbSectorsPerTrack < 8 with disk > 4TB...
568 ; setnz dl ; force cylinder overflow without divide error
569 %endif
570 movzx ecx, word [bx(bpbSectorsPerTrack)]
571 idiv ecx
572 ; eax = LBA / SPT
573 ; edx = LBA % SPT = sector - 1
574 inc dx
575 push dx ; save sector no.
576 cdq
577 mov cx, word [bx(bpbHeadsPerCylinder)]
578 idiv ecx
579 pop cx
580 ; eax = (LBA / SPT) / HPC = cylinder
581 ; dl = (LBA / SPT) % HPC = head
583 xchg ch, al ; clear al
584 ; ch = LSB 0...7 of cylinder no.
585 %if CHSsanityCheck != 0
586 shl eax, 6
587 %else
588 shl ax, 6
589 %endif
590 or cl, ah
591 ; cl = MSB 8...9 of cylinder no. + sector no.
592 mov dh, dl
593 ; dh = head no.
594 %endif
596 ReadSectorRetry:
597 mov dl, [bx(DriveNumber)] ; restore BIOS boot drive number
598 mov si, sp
599 mov ah, 42h ; ah = 42h = extended read function no.
600 int 13h ; extended read sectors (DL, DS:SI)
601 jnc ReadSuccess ; CF = 0 if no error
603 %if CHSsupport != 0
604 mov ax, 201h ; al = sector count = 1
605 ; ah = 2 = read function no.
606 %if CHSsanityCheck != 0
607 cmp eax, 0FFFFh ; max cylinder = 1023
608 ja SkipCHS
609 %endif
610 int 13h ; read sectors (AL, CX, DX, ES:BX)
612 jnc ReadSuccess ; CF = 0 if no error
613 SkipCHS:
614 %endif
615 %if ReadRetry != 0
616 %if CHSsupport != 0
617 cbw ; ah = 0 = reset function
618 %else
619 xor ax, ax ; ah = 0 = reset function
620 %endif
621 int 13h ; reset drive (DL)
623 dec bp ; up to 32 retries
624 jnz ReadSectorRetry
625 %endif
627 call Error
628 db "Read error."
630 ReadSuccess:
632 popa ; sp += 16
633 popad
634 mov dx, [bx(DriveNumber)] ; restore BIOS boot drive number
635 ret
637 %if ExtraBootSector != 0
638 MovedCode
639 BootCode
640 %endif
642 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
643 ;; Fill free space with zeroes ;;
644 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
646 times (512-13-($-$$)) db 0
648 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
649 ;; Name of the file to load and run ;;
650 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
652 NameLength equ 11
653 ProgramName times NameLength db 0 ; name and extension
655 ;;;;;;;;;;;;;;;;;;;;;;;;;;
656 ;; End of the sector ID ;;
657 ;;;;;;;;;;;;;;;;;;;;;;;;;;
659 dw 0AA55h ; BIOS checks for this ID
661 %if ExtraBootSector != 0
662 dd 61415252h ; "RRaA"
663 BootFileName
664 times (996-($-$$)) db 0
665 ; dd 41617272h ; "rrAa"
666 %endif