wok view BootProg/stuff/boot16.asm @ rev 25463

Up expat (2.4.9) fixes CVE-2022-40674
author Pascal Bellard <pascal.bellard@slitaz.org>
date Thu Sep 29 20:05:23 2022 +0000 (19 months ago)
parents 9a714ac859a3
children 4c101652eb90
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 boot16.asm -f bin -o boot16.bin ;;
10 ;; ;;
11 ;; ;;
12 ;; Features: ;;
13 ;; ~~~~~~~~~ ;;
14 ;; - FAT12 and FAT16 supported using BIOS int 13h function 42h or 02h. ;;
15 ;; ;;
16 ;; - Loads a 16-bit executable file in the MS-DOS .COM or .EXE format ;;
17 ;; from the root directory of a disk and transfers control to it ;;
18 ;; (the "ProgramName" variable holds the name of the file to be loaded) ;;
19 ;; Its maximum size can be up to 635KB without Extended BIOS Data area. ;;
20 ;; ;;
21 ;; - Prints an error if the file isn't found or couldn't be read ;;
22 ;; ("File not found" or "Read error") ;;
23 ;; and waits for a key to be pressed, then executes the Int 19h ;;
24 ;; instruction and lets the BIOS continue bootstrap. ;;
25 ;; ;;
26 ;; - cpu 8086 is supported ;;
27 ;; ;;
28 ;; ;;
29 ;; Known Bugs: ;;
30 ;; ~~~~~~~~~~~ ;;
31 ;; - All bugs are fixed as far as I know. The boot sector has been tested ;;
32 ;; on the following types of diskettes (FAT12): ;;
33 ;; - 360KB 5"25 ;;
34 ;; - 1.2MB 5"25 ;;
35 ;; - 1.44MB 3"5 ;;
36 ;; on my HDD (FAT16). ;;
37 ;; ;;
38 ;; ;;
39 ;; Memory Layout: ;;
40 ;; ~~~~~~~~~~~~~~ ;;
41 ;; The diagram below shows the typical memory layout. The actual location ;;
42 ;; of the boot sector and its stack may be lower than A0000H if the BIOS ;;
43 ;; reserves memory for its Extended BIOS Data Area just below A0000H and ;;
44 ;; reports less than 640 KB of RAM via its Int 12H function. ;;
45 ;; ;;
46 ;; physical address ;;
47 ;; +------------------------+ 00000H ;;
48 ;; | Interrupt Vector Table | ;;
49 ;; +------------------------+ 00400H ;;
50 ;; | BIOS Data Area | ;;
51 ;; +------------------------+ 00500H ;;
52 ;; | PrtScr Status / Unused | ;;
53 ;; +------------------------+ 00600H ;;
54 ;; | Loaded Image | ;;
55 ;; +------------------------+ nnnnnH ;;
56 ;; | Available Memory | ;;
57 ;; +------------------------+ A0000H - 512 - 3KB ;;
58 ;; | Boot Sector | ;;
59 ;; +------------------------+ A0000H - 3KB ;;
60 ;; | 3KB Boot Stack | ;;
61 ;; +------------------------+ A0000H ;;
62 ;; | Video RAM | ;;
63 ;; ;;
64 ;; ;;
65 ;; Boot Image Startup (register values): ;;
66 ;; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ;;
67 ;; ax = 0ffffh (both FCB in the PSP don't have a valid drive identifier), ;;
68 ;; bx = 0, dl = BIOS boot drive number (e.g. 0, 80H) ;;
69 ;; cs:ip = program entry point ;;
70 ;; ss:sp = program stack (don't confuse with boot sector's stack) ;;
71 ;; COM program defaults: cs = ds = es = ss = 50h, sp = 0, ip = 100h ;;
72 ;; EXE program defaults: ds = es = EXE data - 10h (fake MS-DOS psp), ;;
73 ;; cs:ip and ss:sp depends on EXE header ;;
74 ;; Magic numbers: ;;
75 ;; si = 16381 (prime number 2**14-3) ;;
76 ;; di = 32749 (prime number 2**15-19) ;;
77 ;; bp = 65521 (prime number 2**16-15) ;;
78 ;; The magic numbers let the program know whether it has been loaded by ;;
79 ;; this boot sector or by MS-DOS, which may be handy for universal, bare- ;;
80 ;; metal and MS-DOS programs. ;;
81 ;; The command line contains no arguments. ;;
82 ;; ;;
83 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
85 %define bx(label) bx+label-boot
86 %define si(label) si+label-boot
87 NullEntryCheck equ 1 ; +2 bytes
88 ReadRetry equ 1 ; +9 bytes
89 LBAsupport equ 1 ; +16 bytes
90 Over2GB equ 1 ; +5 bytes
91 GeometryCheck equ 1 ; +18 bytes
92 SectorOf512Bytes equ 1 ; -4/-6 bytes
94 [BITS 16]
95 [CPU 8086]
97 ImageLoadSeg equ 60h
98 StackSize equ 3072 ; Stack + cluster list
100 [SECTION .text]
101 [ORG 0]
103 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
104 ;; Boot sector starts here ;;
105 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
107 boot:
108 DriveNumber:
109 jmp short start ; MS-DOS/Windows checks for this jump
110 nop
111 bsOemName DB "BootProg" ; 0x03
113 ;;;;;;;;;;;;;;;;;;;;;
114 ;; BPB starts here ;;
115 ;;;;;;;;;;;;;;;;;;;;;
117 bpbBytesPerSector DW 0 ; 0x0B
118 bpbSectorsPerCluster DB 0 ; 0x0D
119 bpbReservedSectors DW 0 ; 0x0E
120 bpbNumberOfFATs DB 0 ; 0x10
121 bpbRootEntries DW 0 ; 0x11
122 bpbTotalSectors DW 0 ; 0x13
123 bpbMedia DB 0 ; 0x15
124 bpbSectorsPerFAT DW 0 ; 0x16
125 bpbSectorsPerTrack DW 0 ; 0x18
126 bpbHeadsPerCylinder DW 0 ; 0x1A
127 bpbHiddenSectors DD 0 ; 0x1C
128 bpbTotalSectorsBig DD 0 ; 0x20
130 ;;;;;;;;;;;;;;;;;;;
131 ;; BPB ends here ;;
132 ;;;;;;;;;;;;;;;;;;;
134 bsDriveNumber DB 0 ; 0x24
135 bsUnused DB 0 ; 0x25
136 bsExtBootSignature DB 0 ; 0x26
137 bsSerialNumber DD 0 ; 0x27
138 bsVolumeLabel times 11 DB " " ; 0x2B "NO NAME "
139 bsFileSystem times 8 DB " " ; 0x36 "FAT12 " or "FAT16 "
141 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
142 ;; Boot sector code starts here ;;
143 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
145 start:
146 cld
148 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
149 ;; How much RAM is there? ;;
150 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
152 int 12h ; get conventional memory size (in KBs)
153 mov cx, 106h
154 shl ax, cl ; and convert it to 16-byte paragraphs
156 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
157 ;; Reserve memory for the boot sector and its stack ;;
158 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
160 sub ax, (512+StackSize) /16 ; reserve bytes for the code and the stack
161 mov es, ax ; cs:0 = ds:0 = ss:0 -> top - 512 - StackSize
162 mov ss, ax
163 mov sp, 512+StackSize ; bytes 0-511 are reserved for the boot code
165 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
166 ;; Copy ourselves to top of memory ;;
167 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
169 mov si, 7C00h
170 xor di, di
171 mov ds, di
172 push es
173 mov [si(DriveNumber)], dx ; store BIOS boot drive number
174 rep movsw ; move 512 bytes (+ 12)
176 ;;;;;;;;;;;;;;;;;;;;;;
177 ;; Jump to the copy ;;
178 ;;;;;;;;;;;;;;;;;;;;;;
180 mov cl, byte main
181 push cx
182 retf
184 main:
185 %if ImageLoadSeg != main-boot
186 %if ImageLoadSeg >= 100h
187 mov cx, ImageLoadSeg
188 %else
189 mov cl, ImageLoadSeg
190 %endif
191 %endif
192 push cx
194 ;;;;;;;;;;;;;;;;;;;;;;;;;;
195 ;; Get drive parameters ;;
196 ;; Update heads count ;;
197 ;; for current BIOS ;;
198 ;;;;;;;;;;;;;;;;;;;;;;;;;;
200 %if GeometryCheck != 0
201 mov ah, 8
202 int 13h ; may destroy SI,BP, and DS registers
203 %endif
204 ; update AX,BL,CX,DX,DI, and ES registers
205 push cs
206 pop ds
207 xor bx, bx
209 %if GeometryCheck != 0
210 and cx, byte 3Fh
211 cmp [bx(bpbSectorsPerTrack)], cx
212 jne BadParams ; verify updated and validity
213 mov al, dh
214 inc ax
215 mov [bpbHeadsPerCylinder], ax
216 BadParams:
217 %endif
219 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
220 ;; Load FAT (FAT12: 6KB max, FAT16: 128KB max) ;;
221 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
223 pop es ; ImageLoadSeg
224 push es
226 mul bx ; dx:ax = 0 = LBA (LBA are relative to FAT)
227 mov di, word [bx(bpbSectorsPerFAT)]
229 call ReadDISectors ; read fat; clear ax, cx, di; bp = SectorsPerFAT
231 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
232 ;; load the root directory in ;;
233 ;; its entirety (16KB max) ;;
234 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
236 %if SectorOf512Bytes != 0
237 mov di, word [bx(bpbRootEntries)]
238 mov cl, 4
239 shr di, cl ; di = root directory size in sectors
240 %else
241 mov al, 32
243 mul word [bx(bpbRootEntries)]
244 div word [bx(bpbBytesPerSector)]
245 xchg ax, di ; di = root directory size in sectors, clear ah
246 %endif
248 mov al, [bpbNumberOfFATs]
249 mul bp ; [bx(bpbSectorsPerFAT)], set by ReadDISectors
251 push es ; read root directory
252 call ReadDISectors ; clear ax, cx, di; bp = first data sector
253 pop es
255 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
256 ;; Look for the COM/EXE file to load and run ;;
257 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
259 ; es:di -> root entries array
261 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
262 ;; Looks for the file/dir ProgramName ;;
263 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
264 ;; Input: ES:DI -> root directory array ;;
265 ;; Output: SI = cluster number ;;
266 ;; AX = file sector count ;;
267 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
269 FindNameCycle:
270 push di
271 mov cl, NameLength
272 mov si, ProgramName ; ds:si -> program name
273 repe cmpsb
274 pop di
275 je FindNameFound
276 %if NullEntryCheck != 0
277 scasb ; Z == NC
278 cmc
279 lea di, [di+31]
280 dec word [bx(bpbRootEntries)]
281 ja FindNameCycle ; next root entry
282 %else
283 add di, byte 32
284 dec word [bx(bpbRootEntries)]
285 jnz FindNameCycle ; next root entry
286 %endif
288 FindNameFailed:
289 call Error
290 db "File not found."
292 FindNameFound:
293 push si
294 mov si, [es:di+1Ah] ; si = cluster no.
295 les ax, [es:di+1Ch] ; file size
296 mov dx, es
297 div word [bx(bpbBytesPerSector)]
298 cmp bx, dx ; sector aligned ?
299 adc ax, bx ; file last sector
300 pop di ; di = ClusterList
302 pop es ; ImageLoadSeg
303 push ax
305 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
306 ;; build cluster list ;;
307 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
308 ;; Input: ES:0 -> FAT ;;
309 ;; SI = first cluster ;;
310 ;; DI = cluster list :;
311 ;; CH = 0 ;;
312 ;; Output: SI = cluster list ;;
313 ;; CH = 0 ;;
314 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
316 FAT12 equ 1
317 FAT16 equ 1
318 TINYFAT16 equ 1
319 push di ; up to 2 * 635K / BytesPerCluster = 2540 bytes
320 %if FAT12 == 1
321 mov cl, 12
322 %endif
323 ClusterLoop:
324 mov [di], si
325 add si, si ; si = cluster * 2
326 %if FAT16 == 1
327 mov ax, es ; ax = FAT segment = ImageLoadSeg
328 jnc First64k
329 mov ah, (1000h+ImageLoadSeg)>>8 ; adjust segment for 2nd part of FAT16
330 First64k:
331 %if FAT12 == 1
332 mov dx, 0FFF6h
333 %if TINYFAT16 == 1
334 test [bx(bsFileSystem+4)], cl ; FAT12 or FAT16 ? clear C
335 jne ReadClusterFat16
336 %else
337 cmp [bx(bpbSectorsPerFAT)], cx ; 1..12 = FAT12, 16..256 = FAT16
338 ja ReadClusterFat16
339 %endif
340 mov dh, 0Fh
341 %endif
342 %endif
343 %if FAT12 == 1
344 add si, [di]
345 shr si, 1 ; si = cluster * 3 / 2
346 %endif
347 %if FAT16 == 1
348 ReadClusterFat16:
349 push ds
350 mov ds, ax
351 lodsw ; ax = next cluster
352 pop ds
353 %else
354 es lodsw
355 %endif
356 %if FAT12 == 1
357 jnc ReadClusterEven
358 rol ax, cl
359 ReadClusterEven:
360 scasw ; di += 2
361 %if FAT16 == 1
362 and ah, dh ; mask cluster value
363 cmp ax, dx
364 %else
365 and ah, 0Fh ; mask cluster value
366 cmp ax, 0FF6h
367 %endif
368 %else
369 scasw ; di += 2
370 cmp ax, 0FFF6h
371 %endif
372 xchg ax, si
373 jc ClusterLoop
374 pop si
375 pop di ; file size in sectors
377 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
378 ;; Load the entire file ;;
379 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
380 ;; Input: ES:0 -> buffer ;;
381 ;; SI = cluster list ;;
382 ;; DI = file sectors ;;
383 ;; CH = 0 ;;
384 ;; BP = 1st data sec ;;
385 ;; Output: BP:0 -> buffer ;;
386 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
388 push es
390 ReadClusters:
391 lodsw
392 dec ax
393 dec ax
395 mov cl, [bx(bpbSectorsPerCluster)] ; 1..128
396 mul cx ; cx = sector count (ch = 0)
398 add ax, bp ; LBA for cluster data
399 adc dx, bx ; dx:ax = LBA
401 call ReadSectors ; clear ax, restore dx
403 jne ReadClusters ; until end of file
405 pop bp ; ImageLoadSeg
407 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
408 ;; Type detection, .COM or .EXE? ;;
409 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
411 mov ds, bp ; bp=ds=seg the file is loaded to
413 add bp, [bx+08h] ; bp = image base
414 mov di, [bx+18h] ; di = reloc table pointer
416 cmp word [bx], 5A4Dh ; "MZ" signature?
417 je RelocateEXE ; yes, it's an EXE program
419 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
420 ;; Setup and run a .COM program ;;
421 ;; Set CS=DS=ES=SS SP=0 IP=100h ;;
422 ;; AX=0ffffh BX=0 DX=drive and ;;
423 ;; cmdline=void ;;
424 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
426 mov di, 100h ; ip
427 mov bp, ImageLoadSeg-10h ; "org 100h" stuff :)
428 mov ss, bp
429 xor sp, sp
430 push bp ; cs, ds and es
431 jmp short Run
433 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
434 ;; Relocate, setup and run a .EXE program ;;
435 ;; Set CS:IP, SS:SP, DS, ES and AX according ;;
436 ;; to wiki.osdev.org/MZ#Initial_Program_State ;;
437 ;; AX=0ffffh BX=0 DX=drive cmdline=void ;;
438 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
440 ReloCycle:
441 add [di+2], bp ; item seg (abs)
442 les si, [di] ; si = item ofs, es = item seg
443 add [es:si], bp ; fixup
444 scasw ; di += 2
445 scasw ; point to next entry
447 RelocateEXE:
448 dec word [bx+06h] ; reloc items, 32768 max (128KB table)
449 jns ReloCycle
450 ; PSP don't have a valid drive identifier
451 les si, [bx+0Eh]
452 add si, bp
453 mov ss, si ; ss for EXE
454 mov sp, es ; sp for EXE
456 lea si, [bp-10h] ; ds and es both point to the segment
457 push si ; containing the PSP structure
459 add bp, [bx+16h] ; cs for EXE
460 mov di, [bx+14h] ; ip for EXE
461 Run:
462 pop ds
463 push bp
464 push di
465 push ds
466 pop es
467 mov [80h], ax ; clear cmdline
468 dec ax ; both FCB in the PSP don't have a valid drive identifier
470 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
471 ;; Set the magic numbers so the program knows that it ;;
472 ;; has been loaded by this bootsector and not by MS-DOS ;;
473 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
474 mov si, 16381 ; prime number 2**14-3
475 mov di, 32749 ; prime number 2**15-19
476 mov bp, 65521 ; prime number 2**16-15
478 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
479 ;; All done, transfer control to the program now ;;
480 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
481 retf
483 ;;;;;;;;;;;;;;;;;;;;;;;;;;
484 ;; Error Messaging Code ;;
485 ;;;;;;;;;;;;;;;;;;;;;;;;;;
487 Error:
488 pop si
490 PutStr:
491 mov ah, 0Eh
492 mov bl, 7
493 lodsb
494 int 10h
495 cmp al, "."
496 jne PutStr
498 cbw
499 int 16h ; wait for a key...
500 int 19h ; bootstrap
502 Stop:
503 hlt
504 jmp short Stop
506 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
507 ;; Read sectors using BIOS Int 13h ;;
508 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
509 ;; Input: DX:AX = LBA relative to FAT ;;
510 ;; BX = 0 ;;
511 ;; DI = sector count ;;
512 ;; ES:BX -> buffer address ;;
513 ;; Output: ES:BX -> next address ;;
514 ;; BX = 0 ;;
515 ;; CX = 0 ;;
516 ;; DI = 0 ;;
517 ;; DL = drive number ;;
518 ;; DX:BP = next LBA from FAT ;;
519 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
521 ReadDISectors:
522 mov bp, di
523 add bp, ax ; adjust LBA for cluster data
525 mov cx, di ; no cluster size limit
527 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
528 ;; Read sectors using BIOS Int 13h ;;
529 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
530 ;; Input: DX:AX = LBA relative to FAT ;;
531 ;; BX = 0 ;;
532 ;; CX = sector count ;;
533 ;; DI = file sectors ;;
534 ;; ES:BX -> buffer address ;;
535 ;; Output: ES:BX -> next address ;;
536 ;; BX = 0 ;;
537 ;; CX or DI = 0 ;;
538 ;; DL = drive number ;;
539 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
541 ReadSectors:
542 add ax, [bx(bpbHiddenSectors)]
543 adc dx, [bx(bpbHiddenSectors)+2]
544 add ax, [bx(bpbReservedSectors)]
546 push si
547 ReadSectorNext:
548 adc dx, bx
549 push di
550 push cx
552 %if LBAsupport != 0
553 push bx
554 push bx
555 %endif
556 push dx ; 32-bit LBA: up to 2TB
557 push ax
558 push es
559 %if ReadRetry != 0 || LBAsupport != 0
560 mov di, 16 ; packet size byte = 16, reserved byte = 0
561 %endif
562 %if LBAsupport != 0
563 push bx
564 inc bx ; sector count word = 1
565 push bx
566 dec bx
567 push di
568 %endif
570 %if Over2GB != 0
571 xchg ax, cx ; save low LBA
572 xchg ax, dx ; get high LBA
573 cwd ; clear dx (LBA offset <1TB)
574 idiv word [bx(bpbSectorsPerTrack)] ; up to 8GB disks
576 xchg ax, cx ; restore low LBA, save high LBA / SPT
577 %else
578 xor cx, cx ; up to 2GB disks otherwise divide error interrupt !
579 %endif
580 idiv word [bx(bpbSectorsPerTrack)]
581 ; ax = LBA / SPT
582 ; dx = LBA % SPT = sector - 1
583 inc dx
585 xchg cx, dx ; restore high LBA / SPT, save sector no.
586 idiv word [bx(bpbHeadsPerCylinder)]
587 ; ax = (LBA / SPT) / HPC = cylinder
588 ; dx = (LBA / SPT) % HPC = head
590 xchg ch, al ; clear al
591 ; ch = LSB 0...7 of cylinder no.
592 shr ax, 1
593 shr ax, 1
594 or cl, al
595 ; cl = MSB 8...9 of cylinder no. + sector no.
596 mov dh, dl
597 ; dh = head no.
599 ReadSectorRetry:
600 mov dl, [bx(DriveNumber)]
601 ; dl = drive no.
602 mov si, sp
603 %if LBAsupport != 0
604 mov ah, 42h ; ah = 42h = extended read function no.
605 int 13h ; extended read sectors (DL, DS:SI)
606 jnc ReadSectorNextSegment
607 %endif
609 mov ax, 201h ; al = sector count = 1
610 ; ah = 2 = read function no.
611 int 13h ; read sectors (AL, CX, DX, ES:BX)
613 jnc ReadSectorNextSegment
614 %if ReadRetry != 0
615 cbw ; ah = 0 = reset function
616 int 13h ; reset drive (DL)
618 dec di
619 jnz ReadSectorRetry ; up to 15 extra attempt
620 %endif
622 call Error
623 db "Read error."
625 ReadSectorNextSegment:
627 %if LBAsupport != 0
628 pop ax ; al = 16
629 %if SectorOf512Bytes != 0
630 add word [si+6], byte 32 ; adjust segment for next sector
631 %else
632 mul byte [bx(bpbBytesPerSector)+1] ; = (bpbBytesPerSector/256)*16
633 add [si+6], ax ; adjust segment for next sector
634 %endif
635 pop cx ; sector count = 1
636 pop bx
637 %else
638 %if SectorOf512Bytes != 0
639 add word [si], byte 32 ; adjust segment for next sector
640 %else
641 mov al, 16
642 mul byte [bx(bpbBytesPerSector)+1] ; = (bpbBytesPerSector/256)*16
643 add [si], ax ; adjust segment for next sector
644 %endif
645 %endif
646 pop es ; es:0 updated
647 pop ax
648 pop dx
649 %if LBAsupport != 0
650 pop di
651 pop di
652 add ax, cx ; adjust LBA for next sector
653 %else
654 add ax, 1 ; adjust LBA for next sector
655 %endif
657 pop cx ; cluster sectors to read
658 pop di ; file sectors to read
659 dec di ; keep C
660 loopne ReadSectorNext ; until cluster sector count or file sector count
661 pop si
662 mov ax, bx ; clear ax
663 mov dx, [bx(DriveNumber)] ; pass the BIOS boot drive to Run or Error
665 ret
667 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
668 ;; Fill free space with zeroes ;;
669 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
671 times (512-13-($-$$)) db 0
673 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
674 ;; Name of the file to load and run ;;
675 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
677 NameLength equ 11
678 ProgramName times NameLength db 0 ; name and extension
680 ;;;;;;;;;;;;;;;;;;;;;;;;;;
681 ;; End of the sector ID ;;
682 ;;;;;;;;;;;;;;;;;;;;;;;;;;
684 ClusterList dw 0AA55h ; BIOS checks for this ID