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

Up gnumeric (1.12.53)
author Pascal Bellard <pascal.bellard@slitaz.org>
date Mon Oct 03 08:28:51 2022 +0000 (19 months ago)
parents 81fc994927a6
children f40d97a52c42
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
93 CheckAttrib equ 0 ; +6 bytes
95 [BITS 16]
96 [CPU 8086]
98 ImageLoadSeg equ 60h
99 StackSize equ 3072 ; Stack + cluster list
101 [SECTION .text]
102 [ORG 0]
104 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
105 ;; Boot sector starts here ;;
106 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
108 boot:
109 DriveNumber:
110 jmp short start ; MS-DOS/Windows checks for this jump
111 nop
112 bsOemName DB "BootProg" ; 0x03
114 ;;;;;;;;;;;;;;;;;;;;;
115 ;; BPB starts here ;;
116 ;;;;;;;;;;;;;;;;;;;;;
118 bpbBytesPerSector DW 0 ; 0x0B
119 bpbSectorsPerCluster DB 0 ; 0x0D
120 bpbReservedSectors DW 0 ; 0x0E
121 bpbNumberOfFATs DB 0 ; 0x10
122 bpbRootEntries DW 0 ; 0x11
123 bpbTotalSectors DW 0 ; 0x13
124 bpbMedia DB 0 ; 0x15
125 bpbSectorsPerFAT DW 0 ; 0x16
126 bpbSectorsPerTrack DW 0 ; 0x18
127 bpbHeadsPerCylinder DW 0 ; 0x1A
128 bpbHiddenSectors DD 0 ; 0x1C
129 bpbTotalSectorsBig DD 0 ; 0x20
131 ;;;;;;;;;;;;;;;;;;;
132 ;; BPB ends here ;;
133 ;;;;;;;;;;;;;;;;;;;
135 bsDriveNumber DB 0 ; 0x24
136 bsUnused DB 0 ; 0x25
137 bsExtBootSignature DB 0 ; 0x26
138 bsSerialNumber DD 0 ; 0x27
139 bsVolumeLabel times 11 DB " " ; 0x2B "NO NAME "
140 bsFileSystem times 8 DB " " ; 0x36 "FAT12 " or "FAT16 "
142 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
143 ;; Boot sector code starts here ;;
144 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
146 start:
147 cld
149 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
150 ;; How much RAM is there? ;;
151 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
153 int 12h ; get conventional memory size (in KBs)
154 mov cx, 106h
155 shl ax, cl ; and convert it to 16-byte paragraphs
157 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
158 ;; Reserve memory for the boot sector and its stack ;;
159 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
161 sub ax, (512+StackSize) /16 ; reserve bytes for the code and the stack
162 mov es, ax ; cs:0 = ds:0 = ss:0 -> top - 512 - StackSize
163 mov ss, ax
164 mov sp, 512+StackSize ; bytes 0-511 are reserved for the boot code
166 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
167 ;; Copy ourselves to top of memory ;;
168 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
170 mov si, 7C00h
171 xor di, di
172 mov ds, di
173 push es
174 mov [si(DriveNumber)], dx ; store BIOS boot drive number
175 rep movsw ; move 512 bytes (+ 12)
177 ;;;;;;;;;;;;;;;;;;;;;;
178 ;; Jump to the copy ;;
179 ;;;;;;;;;;;;;;;;;;;;;;
181 mov cl, byte main
182 push cx
183 retf
185 main:
186 %if ImageLoadSeg != main-boot
187 %if ImageLoadSeg >= 100h
188 mov cx, ImageLoadSeg
189 %else
190 mov cl, ImageLoadSeg
191 %endif
192 %endif
193 push cx
195 ;;;;;;;;;;;;;;;;;;;;;;;;;;
196 ;; Get drive parameters ;;
197 ;; Update heads count ;;
198 ;; for current BIOS ;;
199 ;;;;;;;;;;;;;;;;;;;;;;;;;;
201 %if GeometryCheck != 0
202 mov ah, 8
203 int 13h ; may destroy SI,BP, and DS registers
204 %endif
205 ; update AX,BL,CX,DX,DI, and ES registers
206 push cs
207 pop ds
208 xor bx, bx
210 %if GeometryCheck != 0
211 and cx, byte 3Fh
212 cmp [bx(bpbSectorsPerTrack)], cx
213 jne BadParams ; verify updated and validity
214 mov al, dh
215 inc ax
216 mov [bpbHeadsPerCylinder], ax
217 BadParams:
218 %endif
220 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
221 ;; Load FAT (FAT12: 6KB max, FAT16: 128KB max) ;;
222 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
224 pop es ; ImageLoadSeg
225 push es
227 mul bx ; dx:ax = 0 = LBA (LBA are relative to FAT)
228 mov di, word [bx(bpbSectorsPerFAT)]
230 call ReadDISectors ; read fat; clear ax, cx, di; bp = SectorsPerFAT
232 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
233 ;; load the root directory in ;;
234 ;; its entirety (16KB max) ;;
235 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
237 %if SectorOf512Bytes != 0
238 mov di, word [bx(bpbRootEntries)]
239 mov cl, 4
240 shr di, cl ; di = root directory size in sectors
241 %else
242 mov al, 32
244 mul word [bx(bpbRootEntries)]
245 div word [bx(bpbBytesPerSector)]
246 xchg ax, di ; di = root directory size in sectors, clear ah
247 %endif
249 mov al, [bpbNumberOfFATs]
250 mul bp ; [bx(bpbSectorsPerFAT)], set by ReadDISectors
252 push es ; read root directory
253 call ReadDISectors ; clear ax, cx, di; bp = first data sector
254 pop es
256 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
257 ;; Look for the COM/EXE file to load and run ;;
258 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
260 ; es:di -> root entries array
262 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
263 ;; Looks for the file/dir ProgramName ;;
264 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
265 ;; Input: ES:DI -> root directory array ;;
266 ;; Output: SI = cluster number ;;
267 ;; AX = file sector count ;;
268 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
270 FindNameCycle:
271 push di
272 mov cl, NameLength
273 mov si, ProgramName ; ds:si -> program name
274 repe cmpsb
275 %if CheckAttrib != 0
276 VolumeLabel equ 8
277 SubDirectory equ 10h
278 jnz SkipFindName
279 test byte [es:di], VolumeLabel+SubDirectory
280 SkipFindName:
281 %endif
282 pop di
283 je FindNameFound
284 %if NullEntryCheck != 0
285 scasb ; Z == NC
286 cmc
287 lea di, [di+31]
288 dec word [bx(bpbRootEntries)]
289 ja FindNameCycle ; next root entry
290 %else
291 add di, byte 32
292 dec word [bx(bpbRootEntries)]
293 jnz FindNameCycle ; next root entry
294 %endif
296 FindNameFailed:
297 call Error
298 db "File not found."
300 FindNameFound:
301 push si
302 mov si, [es:di+1Ah] ; si = cluster no.
303 les ax, [es:di+1Ch] ; file size
304 mov dx, es
305 div word [bx(bpbBytesPerSector)]
306 cmp bx, dx ; sector aligned ?
307 adc ax, bx ; file last sector
308 pop di ; di = ClusterList
310 pop es ; ImageLoadSeg
311 push ax
313 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
314 ;; build cluster list ;;
315 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
316 ;; Input: ES:0 -> FAT ;;
317 ;; SI = first cluster ;;
318 ;; DI = cluster list :;
319 ;; CH = 0 ;;
320 ;; Output: SI = cluster list ;;
321 ;; CH = 0 ;;
322 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
324 FAT12 equ 1
325 FAT16 equ 1
326 TINYFAT16 equ 1
327 push di ; up to 2 * 635K / BytesPerCluster = 2540 bytes
328 %if FAT12 == 1
329 mov cl, 12
330 %endif
331 ClusterLoop:
332 mov [di], si
333 add si, si ; si = cluster * 2
334 %if FAT16 == 1
335 mov ax, es ; ax = FAT segment = ImageLoadSeg
336 jnc First64k
337 mov ah, (1000h+ImageLoadSeg)>>8 ; adjust segment for 2nd part of FAT16
338 First64k:
339 %if FAT12 == 1
340 mov dx, 0FFF6h
341 %if TINYFAT16 == 1
342 test [bx(bsFileSystem+4)], cl ; FAT12 or FAT16 ? clear C
343 jne ReadClusterFat16
344 %else
345 cmp [bx(bpbSectorsPerFAT)], cx ; 1..12 = FAT12, 16..256 = FAT16
346 ja ReadClusterFat16
347 %endif
348 mov dh, 0Fh
349 %endif
350 %endif
351 %if FAT12 == 1
352 add si, [di]
353 shr si, 1 ; si = cluster * 3 / 2
354 %endif
355 %if FAT16 == 1
356 ReadClusterFat16:
357 push ds
358 mov ds, ax
359 lodsw ; ax = next cluster
360 pop ds
361 %else
362 es lodsw
363 %endif
364 %if FAT12 == 1
365 jnc ReadClusterEven
366 rol ax, cl
367 ReadClusterEven:
368 scasw ; di += 2
369 %if FAT16 == 1
370 and ah, dh ; mask cluster value
371 cmp ax, dx
372 %else
373 and ah, 0Fh ; mask cluster value
374 cmp ax, 0FF6h
375 %endif
376 %else
377 scasw ; di += 2
378 cmp ax, 0FFF6h
379 %endif
380 xchg ax, si
381 jc ClusterLoop
382 pop si
383 pop di ; file size in sectors
385 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
386 ;; Load the entire file ;;
387 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
388 ;; Input: ES:0 -> buffer ;;
389 ;; SI = cluster list ;;
390 ;; DI = file sectors ;;
391 ;; CH = 0 ;;
392 ;; BP = 1st data sec ;;
393 ;; Output: BP:0 -> buffer ;;
394 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
396 push es
398 ReadClusters:
399 lodsw
400 dec ax
401 dec ax
403 mov cl, [bx(bpbSectorsPerCluster)] ; 1..128
404 mul cx ; cx = sector count (ch = 0)
406 add ax, bp ; LBA for cluster data
407 adc dx, bx ; dx:ax = LBA
409 call ReadSectors ; clear ax, restore dx
411 jne ReadClusters ; until end of file
413 pop bp ; ImageLoadSeg
415 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
416 ;; Type detection, .COM or .EXE? ;;
417 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
419 mov ds, bp ; bp=ds=seg the file is loaded to
421 add bp, [bx+08h] ; bp = image base
422 mov di, [bx+18h] ; di = reloc table pointer
424 cmp word [bx], 5A4Dh ; "MZ" signature?
425 je RelocateEXE ; yes, it's an EXE program
427 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
428 ;; Setup and run a .COM program ;;
429 ;; Set CS=DS=ES=SS SP=0 IP=100h ;;
430 ;; AX=0ffffh BX=0 DX=drive and ;;
431 ;; cmdline=void ;;
432 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
434 mov di, 100h ; ip
435 mov bp, ImageLoadSeg-10h ; "org 100h" stuff :)
436 mov ss, bp
437 xor sp, sp
438 push bp ; cs, ds and es
439 jmp short Run
441 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
442 ;; Relocate, setup and run a .EXE program ;;
443 ;; Set CS:IP, SS:SP, DS, ES and AX according ;;
444 ;; to wiki.osdev.org/MZ#Initial_Program_State ;;
445 ;; AX=0ffffh BX=0 DX=drive cmdline=void ;;
446 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
448 ReloCycle:
449 add [di+2], bp ; item seg (abs)
450 les si, [di] ; si = item ofs, es = item seg
451 add [es:si], bp ; fixup
452 scasw ; di += 2
453 scasw ; point to next entry
455 RelocateEXE:
456 dec word [bx+06h] ; reloc items, 32768 max (128KB table)
457 jns ReloCycle
458 ; PSP don't have a valid drive identifier
459 les si, [bx+0Eh]
460 add si, bp
461 mov ss, si ; ss for EXE
462 mov sp, es ; sp for EXE
464 lea si, [bp-10h] ; ds and es both point to the segment
465 push si ; containing the PSP structure
467 add bp, [bx+16h] ; cs for EXE
468 mov di, [bx+14h] ; ip for EXE
469 Run:
470 pop ds
471 push bp
472 push di
473 push ds
474 pop es
475 mov [80h], ax ; clear cmdline
476 dec ax ; both FCB in the PSP don't have a valid drive identifier
478 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
479 ;; Set the magic numbers so the program knows that it ;;
480 ;; has been loaded by this bootsector and not by MS-DOS ;;
481 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
482 mov si, 16381 ; prime number 2**14-3
483 mov di, 32749 ; prime number 2**15-19
484 mov bp, 65521 ; prime number 2**16-15
486 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
487 ;; All done, transfer control to the program now ;;
488 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
489 retf
491 ;;;;;;;;;;;;;;;;;;;;;;;;;;
492 ;; Error Messaging Code ;;
493 ;;;;;;;;;;;;;;;;;;;;;;;;;;
495 Error:
496 pop si
498 PutStr:
499 mov ah, 0Eh
500 mov bl, 7
501 lodsb
502 int 10h
503 cmp al, "."
504 jne PutStr
506 cbw
507 int 16h ; wait for a key...
508 int 19h ; bootstrap
510 Stop:
511 hlt
512 jmp short Stop
514 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
515 ;; Read sectors using BIOS Int 13h ;;
516 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
517 ;; Input: DX:AX = LBA relative to FAT ;;
518 ;; BX = 0 ;;
519 ;; DI = sector count ;;
520 ;; ES:BX -> buffer address ;;
521 ;; Output: ES:BX -> next address ;;
522 ;; BX = 0 ;;
523 ;; CX = 0 ;;
524 ;; DI = 0 ;;
525 ;; DL = drive number ;;
526 ;; DX:BP = next LBA from FAT ;;
527 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
529 ReadDISectors:
530 mov bp, di
531 add bp, ax ; adjust LBA for cluster data
533 mov cx, di ; no cluster size limit
535 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
536 ;; Read sectors using BIOS Int 13h ;;
537 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
538 ;; Input: DX:AX = LBA relative to FAT ;;
539 ;; BX = 0 ;;
540 ;; CX = sector count ;;
541 ;; DI = file sectors ;;
542 ;; ES:BX -> buffer address ;;
543 ;; Output: ES:BX -> next address ;;
544 ;; BX = 0 ;;
545 ;; CX or DI = 0 ;;
546 ;; DL = drive number ;;
547 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
549 ReadSectors:
550 add ax, [bx(bpbHiddenSectors)]
551 adc dx, [bx(bpbHiddenSectors)+2]
552 add ax, [bx(bpbReservedSectors)]
554 push si
555 ReadSectorNext:
556 adc dx, bx
557 push di
558 push cx
560 %if LBAsupport != 0
561 push bx
562 push bx
563 %endif
564 push dx ; 32-bit LBA: up to 2TB
565 push ax
566 push es
567 %if ReadRetry != 0 || LBAsupport != 0
568 mov di, 16 ; packet size byte = 16, reserved byte = 0
569 %endif
570 %if LBAsupport != 0
571 push bx
572 inc bx ; sector count word = 1
573 push bx
574 dec bx
575 push di
576 %endif
578 %if Over2GB != 0
579 xchg ax, cx ; save low LBA
580 xchg ax, dx ; get high LBA
581 cwd ; clear dx (LBA offset <1TB)
582 idiv word [bx(bpbSectorsPerTrack)] ; up to 8GB disks
584 xchg ax, cx ; restore low LBA, save high LBA / SPT
585 %else
586 xor cx, cx ; up to 2GB disks otherwise divide error interrupt !
587 %endif
588 idiv word [bx(bpbSectorsPerTrack)]
589 ; ax = LBA / SPT
590 ; dx = LBA % SPT = sector - 1
591 inc dx
593 xchg cx, dx ; restore high LBA / SPT, save sector no.
594 idiv word [bx(bpbHeadsPerCylinder)]
595 ; ax = (LBA / SPT) / HPC = cylinder
596 ; dx = (LBA / SPT) % HPC = head
598 xchg ch, al ; clear al
599 ; ch = LSB 0...7 of cylinder no.
600 shr ax, 1
601 shr ax, 1
602 or cl, al
603 ; cl = MSB 8...9 of cylinder no. + sector no.
604 mov dh, dl
605 ; dh = head no.
607 ReadSectorRetry:
608 mov dl, [bx(DriveNumber)]
609 ; dl = drive no.
610 mov si, sp
611 %if LBAsupport != 0
612 mov ah, 42h ; ah = 42h = extended read function no.
613 int 13h ; extended read sectors (DL, DS:SI)
614 jnc ReadSectorNextSegment
615 %endif
617 mov ax, 201h ; al = sector count = 1
618 ; ah = 2 = read function no.
619 int 13h ; read sectors (AL, CX, DX, ES:BX)
621 jnc ReadSectorNextSegment
622 %if ReadRetry != 0
623 cbw ; ah = 0 = reset function
624 int 13h ; reset drive (DL)
626 dec di
627 jnz ReadSectorRetry ; up to 15 extra attempt
628 %endif
630 call Error
631 db "Read error."
633 ReadSectorNextSegment:
635 %if LBAsupport != 0
636 pop ax ; al = 16
637 %if SectorOf512Bytes != 0
638 add word [si+6], byte 32 ; adjust segment for next sector
639 %else
640 mul byte [bx(bpbBytesPerSector)+1] ; = (bpbBytesPerSector/256)*16
641 add [si+6], ax ; adjust segment for next sector
642 %endif
643 pop cx ; sector count = 1
644 pop bx
645 %else
646 %if SectorOf512Bytes != 0
647 add word [si], byte 32 ; adjust segment for next sector
648 %else
649 mov al, 16
650 mul byte [bx(bpbBytesPerSector)+1] ; = (bpbBytesPerSector/256)*16
651 add [si], ax ; adjust segment for next sector
652 %endif
653 %endif
654 pop es ; es:0 updated
655 pop ax
656 pop dx
657 %if LBAsupport != 0
658 pop di
659 pop di
660 add ax, cx ; adjust LBA for next sector
661 %else
662 add ax, 1 ; adjust LBA for next sector
663 %endif
665 pop cx ; cluster sectors to read
666 pop di ; file sectors to read
667 dec di ; keep C
668 loopne ReadSectorNext ; until cluster sector count or file sector count
669 pop si
670 mov ax, bx ; clear ax
671 mov dx, [bx(DriveNumber)] ; pass the BIOS boot drive to Run or Error
673 ret
675 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
676 ;; Fill free space with zeroes ;;
677 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
679 times (512-13-($-$$)) db 0
681 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
682 ;; Name of the file to load and run ;;
683 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
685 NameLength equ 11
686 ProgramName times NameLength db 0 ; name and extension
688 ;;;;;;;;;;;;;;;;;;;;;;;;;;
689 ;; End of the sector ID ;;
690 ;;;;;;;;;;;;;;;;;;;;;;;;;;
692 ClusterList dw 0AA55h ; BIOS checks for this ID