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

BootProg: fix boot16.asm
author Pascal Bellard <pascal.bellard@slitaz.org>
date Fri May 12 21:35:09 2023 +0000 (2023-05-12)
parents 3c71bb4367c3
children 82f370bad6b5
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/or 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 Limitation: ;;
30 ;; ~~~~~~~~~~~~~~~~~ ;;
31 ;; - Need a up to date field bpbHiddenSectors to work in a partition ; ;;
32 ;; most formatters don't update it correcly in extended partitions. ;;
33 ;; ;;
34 ;; - Partition must fit in the first 8GB disk of the for CHS reads. ;;
35 ;; (or 2TB for LBA reads). ;;
36 ;; ;;
37 ;; ;;
38 ;; Known Bugs: ;;
39 ;; ~~~~~~~~~~~ ;;
40 ;; - All bugs are fixed as far as I know. The boot sector has been tested ;;
41 ;; on the following types of diskettes (FAT12): ;;
42 ;; - 360KB 5"25 ;;
43 ;; - 1.2MB 5"25 ;;
44 ;; - 1.44MB 3"5 ;;
45 ;; on my HDD (FAT16). ;;
46 ;; ;;
47 ;; ;;
48 ;; Memory Layout: ;;
49 ;; ~~~~~~~~~~~~~~ ;;
50 ;; The diagram below shows the typical memory layout. The actual location ;;
51 ;; of the boot sector and its stack may be lower than A0000H if the BIOS ;;
52 ;; reserves memory for its Extended BIOS Data Area just below A0000H and ;;
53 ;; reports less than 640 KB of RAM via its Int 12H function. ;;
54 ;; ;;
55 ;; physical address ;;
56 ;; +------------------------+ 00000H ;;
57 ;; | Interrupt Vector Table | ;;
58 ;; +------------------------+ 00400H ;;
59 ;; | BIOS Data Area | ;;
60 ;; +------------------------+ 00500H ;;
61 ;; | PrtScr Status / Unused | ;;
62 ;; +------------------------+ 00600H ;;
63 ;; | Loaded Image | ;;
64 ;; +------------------------+ nnnnnH ;;
65 ;; | Available Memory | ;;
66 ;; +------------------------+ A0000H - 512 - 3KB ;;
67 ;; | Boot Sector | ;;
68 ;; +------------------------+ A0000H - 3KB ;;
69 ;; | 3KB Boot Stack | ;;
70 ;; +------------------------+ A0000H ;;
71 ;; | Video RAM | ;;
72 ;; ;;
73 ;; ;;
74 ;; Boot Image Startup (register values): ;;
75 ;; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ;;
76 ;; ax = 0ffffh (both FCB in the PSP don't have a valid drive identifier), ;;
77 ;; bx = 0, dl = BIOS boot drive number (e.g. 0, 80H) ;;
78 ;; cs:ip = program entry point ;;
79 ;; ss:sp = program stack (don't confuse with boot sector's stack) ;;
80 ;; COM program defaults: cs = ds = es = ss = 50h, sp = 0, ip = 100h ;;
81 ;; EXE program defaults: ds = es = EXE data - 10h (fake MS-DOS psp), ;;
82 ;; cs:ip and ss:sp depends on EXE header ;;
83 ;; Magic numbers: ;;
84 ;; si = 16381 (prime number 2**14-3) ;;
85 ;; di = 32749 (prime number 2**15-19) ;;
86 ;; bp = 65521 (prime number 2**16-15) ;;
87 ;; The magic numbers let the program know whether it has been loaded by ;;
88 ;; this boot sector or by MS-DOS, which may be handy for universal, bare- ;;
89 ;; metal and MS-DOS programs. ;;
90 ;; The command line contains no arguments. ;;
91 ;; ;;
92 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
94 %define bx(label) bx+label-boot
95 %define si(label) si+label-boot
96 NullEntryCheck equ 1 ; +2 bytes
97 ReadRetry equ 1 ; +9/+13 bytes
98 LBAsupport equ 1 ; +18 bytes
99 CHSsanityCheck equ 1 ; +12/+16 bytes
100 GeometryCheck equ 1 ; +20 bytes
101 CheckAttrib equ 0 ; +6 bytes
102 WaitForKey equ 0 ; +5 bytes
103 SectorOf512Bytes equ 0 ; -4/-6 bytes
104 Always2FATs equ 0 ; -1 bytes
106 [BITS 16]
107 [CPU 8086]
109 ImageLoadSeg equ 60h
110 StackSize equ 3072 ; Stack + cluster list
112 [SECTION .text]
113 [ORG 0]
115 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
116 ;; Boot sector starts here ;;
117 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
119 boot:
120 DriveNumber:
121 jmp short start ; MS-DOS/Windows checks for this jump
122 nop
123 bsOemName DB "BootProg" ; 0x03
125 ;;;;;;;;;;;;;;;;;;;;;
126 ;; BPB starts here ;;
127 ;;;;;;;;;;;;;;;;;;;;;
129 bpbBytesPerSector DW 0 ; 0x0B 512, 1024, 2048 or 4096
130 bpbSectorsPerCluster DB 0 ; 0x0D 1, 2, 4, 8, 16, 32 or 64
131 bpbReservedSectors DW 0 ; 0x0E 1
132 bpbNumberOfFATs DB 0 ; 0x10 2
133 bpbRootEntries DW 0 ; 0x11
134 bpbTotalSectors DW 0 ; 0x13
135 bpbMedia DB 0 ; 0x15
136 bpbSectorsPerFAT DW 0 ; 0x16
137 bpbSectorsPerTrack DW 0 ; 0x18
138 bpbHeadsPerCylinder DW 0 ; 0x1A
139 bpbHiddenSectors DD 0 ; 0x1C
140 bpbTotalSectorsBig DD 0 ; 0x20
142 ;;;;;;;;;;;;;;;;;;;
143 ;; BPB ends here ;;
144 ;;;;;;;;;;;;;;;;;;;
146 bsDriveNumber DB 0 ; 0x24
147 bsUnused DB 0 ; 0x25
148 bsExtBootSignature DB 0 ; 0x26 29
149 bsSerialNumber DD 0 ; 0x27
150 bsVolumeLabel times 11 DB " " ; 0x2B "NO NAME "
151 bsFileSystem times 8 DB " " ; 0x36 "FAT12 " or "FAT16 "
153 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
154 ;; Boot sector code starts here ;;
155 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
157 start:
158 cld
160 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
161 ;; How much RAM is there? ;;
162 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
164 int 12h ; get conventional memory size (in KBs)
165 mov cx, 106h
166 shl ax, cl ; and convert it to 16-byte paragraphs
168 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
169 ;; Reserve memory for the boot sector and its stack ;;
170 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
172 sub ax, (512+StackSize) /16 ; reserve bytes for the code and the stack
173 mov es, ax ; cs:0 = ds:0 = ss:0 -> top - 512 - StackSize
174 mov ss, ax
175 mov sp, 512+StackSize ; bytes 0-511 are reserved for the boot code
177 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
178 ;; Copy ourselves to top of memory ;;
179 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
181 mov si, 7C00h
182 xor di, di
183 mov ds, di
184 push es
185 mov [si(DriveNumber)], dx ; store BIOS boot drive number
186 rep movsw ; move 512 bytes (+ 12)
188 ;;;;;;;;;;;;;;;;;;;;;;
189 ;; Jump to the copy ;;
190 ;;;;;;;;;;;;;;;;;;;;;;
192 mov cl, byte main
193 push cx
194 retf
196 main:
197 %if ImageLoadSeg != main-boot
198 %if ImageLoadSeg >= 100h
199 mov cx, ImageLoadSeg
200 %else
201 mov cl, ImageLoadSeg
202 %endif
203 %endif
204 push cx
206 ;;;;;;;;;;;;;;;;;;;;;;;;;;
207 ;; Get drive parameters ;;
208 ;; Update heads count ;;
209 ;; for current BIOS ;;
210 ;;;;;;;;;;;;;;;;;;;;;;;;;;
212 %if GeometryCheck != 0
213 %if ((main-boot) & 3Fh) != 0
214 xor cx, cx
215 %endif
216 mov ah, 8 ; update AX,BL,CX,DX,DI, and ES registers
217 int 13h ; may destroy SI,BP, and DS registers
218 %endif
219 push cs
220 pop ds
221 xor bx, bx
223 %if GeometryCheck != 0
224 and cx, byte 3Fh
225 je BadParams ; verify updated and validity
226 mov [bx(bpbSectorsPerTrack)], cx
227 mov cl, dh
228 inc cx
229 mov [bx(bpbHeadsPerCylinder)], cx
230 BadParams:
231 %endif
233 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
234 ;; Load FAT (FAT12: 6KB max, FAT16: 128KB max) ;;
235 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
237 pop es ; ImageLoadSeg
238 push es
240 mul bx ; dx:ax = 0 = LBA (LBA are relative to FAT)
241 mov di, word [bx(bpbSectorsPerFAT)]
243 call ReadDISectors ; read fat; clear ax, cx, di; bp = SectorsPerFAT
245 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
246 ;; load the root directory in ;;
247 ;; its entirety (16KB max) ;;
248 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
250 %if SectorOf512Bytes != 0
251 mov di, word [bx(bpbRootEntries)]
252 mov cl, 4
253 shr di, cl ; di = root directory size in sectors
254 %else
255 mov al, 32
257 mul word [bx(bpbRootEntries)]
258 div word [bx(bpbBytesPerSector)]
259 xchg ax, di ; di = root directory size in sectors, clear ah
260 %endif
262 %if Always2FATs != 0
263 mov al, 2
264 %else
265 mov al, [bpbNumberOfFATs]
266 %endif
267 mul bp ; [bx(bpbSectorsPerFAT)], set by ReadDISectors
269 push es ; read root directory
270 call ReadDISectors ; clear ax, cx, di; bp = first data sector
271 pop es
273 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
274 ;; Look for the COM/EXE file to load and run ;;
275 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
277 ; es:di -> root entries array
279 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
280 ;; Looks for the file/dir ProgramName ;;
281 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
282 ;; Input: ES:DI -> root directory array ;;
283 ;; Output: SI = cluster number ;;
284 ;; AX = file sector count ;;
285 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
287 FindNameCycle:
288 push di
289 mov cl, NameLength
290 mov si, ProgramName ; ds:si -> program name
291 repe cmpsb
292 %if CheckAttrib != 0
293 VolumeLabel equ 8
294 SubDirectory equ 10h
295 jnz SkipFindName
296 test byte [es:di], VolumeLabel+SubDirectory
297 SkipFindName:
298 %endif
299 pop di
300 je FindNameFound
301 %if NullEntryCheck != 0
302 scasb ; Z == NC
303 cmc
304 lea di, [di+31]
305 dec word [bx(bpbRootEntries)]
306 ja FindNameCycle ; next root entry
307 %else
308 add di, byte 32
309 dec word [bx(bpbRootEntries)]
310 jnz FindNameCycle ; next root entry
311 %endif
313 FindNameFailed:
314 call Error
315 db "File not found."
317 FindNameFound:
318 push si
319 mov si, [es:di+1Ah] ; si = cluster no.
320 les ax, [es:di+1Ch] ; file size
321 mov dx, es
322 div word [bx(bpbBytesPerSector)]
323 cmp bx, dx ; sector aligned ?
324 adc ax, bx ; file last sector
325 pop di ; di = ClusterList
327 pop es ; ImageLoadSeg
328 push ax
330 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
331 ;; build cluster list ;;
332 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
333 ;; Input: ES:0 -> FAT ;;
334 ;; SI = first cluster ;;
335 ;; DI = cluster list :;
336 ;; CH = 0 ;;
337 ;; Output: SI = cluster list ;;
338 ;; CH = 0 ;;
339 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
341 FAT12 equ 1
342 FAT16 equ 1
343 TINYFAT16 equ 1 ; < 4072 clusters fat16 support
344 push di ; up to 2 * 635K / BytesPerCluster = 2540 bytes
345 %if FAT12 == 1
346 mov cl, 12
347 %else
348 nop
349 nop
350 %endif
351 ClusterLoop:
352 mov [di], si
353 add si, si ; si = cluster * 2
354 %if FAT16 == 1
355 mov ax, es ; ax = FAT segment = ImageLoadSeg
356 jnc First64k
357 mov ah, (1000h+ImageLoadSeg)>>8 ; adjust segment for 2nd part of FAT16
358 First64k:
359 %if FAT12 == 1
360 mov dx, 0FFF6h
361 %if TINYFAT16 == 1
362 test [bx(bsFileSystem+4)], cl ; FAT12 or FAT16 ? clear C (bit 2)
363 jne ReadClusterFat16
364 %else
365 cmp [bx(bpbSectorsPerFAT)], cx ; 1..12 = FAT12, 16..256 = FAT16
366 ja ReadClusterFat16
367 %endif
368 mov dh, 0Fh
369 %endif
370 %endif
371 %if FAT12 == 1
372 add si, [di]
373 shr si, 1 ; si = cluster * 3 / 2
374 %else
375 nop
376 nop
377 %endif
378 %if FAT16 == 1
379 ReadClusterFat16:
380 push ds
381 mov ds, ax
382 lodsw ; ax = next cluster
383 pop ds
384 %else
385 es lodsw
386 %endif
387 %if FAT12 == 1
388 jnc ReadClusterEven
389 rol ax, cl
390 ReadClusterEven:
391 scasw ; di += 2
392 %if FAT16 == 1
393 and ah, dh ; mask cluster value
394 cmp ax, dx
395 %else
396 and ah, 0Fh ; mask cluster value
397 cmp ax, 0FF6h
398 %endif
399 %else
400 scasw ; di += 2
401 cmp ax, 0FFF6h
402 %endif
403 xchg ax, si
404 jc ClusterLoop
405 pop si
406 pop di ; file size in sectors
408 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
409 ;; Load the entire file ;;
410 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
411 ;; Input: ES:0 -> buffer ;;
412 ;; SI = cluster list ;;
413 ;; DI = file sectors ;;
414 ;; CH = 0 ;;
415 ;; BP = 1st data sec ;;
416 ;; Output: BP:0 -> buffer ;;
417 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
419 push es
421 ReadClusters:
422 lodsw
423 dec ax
424 dec ax
426 mov cl, [bx(bpbSectorsPerCluster)] ; 1..128
427 mul cx ; cx = sector count (ch = 0)
429 add ax, bp ; LBA for cluster data
430 adc dx, bx ; dx:ax = LBA
432 call ReadSectors ; clear ax, restore dx
434 jne ReadClusters ; until end of file
436 pop bp ; ImageLoadSeg
438 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
439 ;; Type detection, .COM or .EXE? ;;
440 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
442 mov ds, bp ; bp=ds=seg the file is loaded to
444 add bp, [bx+08h] ; bp = image base
445 mov di, [bx+18h] ; di = reloc table pointer
447 cmp word [bx], 5A4Dh ; "MZ" signature?
448 je RelocateEXE ; yes, it's an EXE program
450 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
451 ;; Setup and run a .COM program ;;
452 ;; Set CS=DS=ES=SS SP=0 IP=100h ;;
453 ;; AX=0ffffh BX=0 DX=drive and ;;
454 ;; cmdline=void ;;
455 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
457 mov di, 100h ; ip
458 mov bp, ImageLoadSeg-10h ; "org 100h" stuff :)
459 mov ss, bp
460 xor sp, sp
461 push bp ; cs, ds and es
462 jmp short Run
464 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
465 ;; Relocate, setup and run a .EXE program ;;
466 ;; Set CS:IP, SS:SP, DS, ES and AX according ;;
467 ;; to wiki.osdev.org/MZ#Initial_Program_State ;;
468 ;; AX=0ffffh BX=0 DX=drive cmdline=void ;;
469 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
471 ReloCycle:
472 add [di+2], bp ; item seg (abs)
473 les si, [di] ; si = item ofs, es = item seg
474 add [es:si], bp ; fixup
475 scasw ; di += 2
476 scasw ; point to next entry
478 RelocateEXE:
479 dec word [bx+06h] ; reloc items, 32768 max (128KB table)
480 jns ReloCycle
481 ; PSP don't have a valid drive identifier
482 les si, [bx+0Eh]
483 add si, bp
484 mov ss, si ; ss for EXE
485 mov sp, es ; sp for EXE
487 lea si, [bp-10h] ; ds and es both point to the segment
488 push si ; containing the PSP structure
490 add bp, [bx+16h] ; cs for EXE
491 mov di, [bx+14h] ; ip for EXE
492 Run:
493 pop ds
494 push bp
495 push di
496 push ds
497 pop es
498 mov [80h], ax ; clear cmdline
499 dec ax ; both FCB in the PSP don't have a valid drive identifier
501 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
502 ;; Set the magic numbers so the program knows that it ;;
503 ;; has been loaded by this bootsector and not by MS-DOS ;;
504 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
505 mov si, 16381 ; prime number 2**14-3
506 mov di, 32749 ; prime number 2**15-19
507 mov bp, 65521 ; prime number 2**16-15
509 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
510 ;; All done, transfer control to the program now ;;
511 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
512 retf
514 ;;;;;;;;;;;;;;;;;;;;;;;;;;
515 ;; Error Messaging Code ;;
516 ;;;;;;;;;;;;;;;;;;;;;;;;;;
518 Error:
519 pop si
521 PutStr:
522 mov ah, 0Eh
523 mov bl, 7
524 lodsb
525 int 10h
526 cmp al, "."
527 jne PutStr
528 %if WaitForKey != 0
529 cbw
530 int 16h ; wait for a key...
531 int 19h ; bootstrap
532 %endif
533 Stop:
534 hlt
535 jmp short Stop
537 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
538 ;; Read sectors using BIOS Int 13h ;;
539 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
540 ;; Input: DX:AX = LBA relative to FAT ;;
541 ;; BX = 0 ;;
542 ;; DI = sector count ;;
543 ;; ES:BX -> buffer address ;;
544 ;; Output: ES:BX -> next address ;;
545 ;; BX = 0 ;;
546 ;; CX = 0 ;;
547 ;; DI = 0 ;;
548 ;; DL = drive number ;;
549 ;; 0:BP = next LBA from FAT ;;
550 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
552 ReadDISectors:
553 mov bp, di
554 add bp, ax ; adjust LBA for cluster data
556 mov cx, di ; no cluster size limit
558 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
559 ;; Read sectors using BIOS Int 13h ;;
560 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
561 ;; Input: DX:AX = LBA relative to FAT ;;
562 ;; BX = 0 ;;
563 ;; CX = sector count ;;
564 ;; DI = file sectors ;;
565 ;; ES:BX -> buffer address ;;
566 ;; Output: ES:BX -> next address ;;
567 ;; BX = 0 ;;
568 ;; CX or DI = 0 ;;
569 ;; DL = drive number ;;
570 ;; Keep: BP, SI ;;
571 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
573 ReadSectors:
574 add ax, [bx(bpbHiddenSectors)]
575 adc dx, [bx(bpbHiddenSectors)+2]
576 add ax, [bx(bpbReservedSectors)]
578 push si
579 ReadSectorNext:
580 adc dx, bx
581 push di
582 push cx
584 %if LBAsupport != 0
585 push bx
586 push bx
587 %endif
588 push dx ; 32-bit LBA: up to 2TB
589 push ax
590 push es
591 %if ReadRetry != 0 || LBAsupport != 0
592 mov di, 16 ; packet size byte = 16, reserved byte = 0
593 %endif
594 %if LBAsupport != 0
595 push bx
596 inc bx ; sector count word = 1
597 push bx
598 dec bx
599 push di
600 %endif
601 mov si, sp
603 xchg ax, cx ; save low LBA
604 xchg ax, dx ; get high LBA
605 cwd ; clear dx (LBA offset <1TB)
606 idiv word [bx(bpbSectorsPerTrack)] ; up to 8GB disks
608 %if CHSsanityCheck != 0
609 inc ax
610 cmp ax, [bx(bpbHeadsPerCylinder)]
611 ja CHSoverflow
612 dec ax
613 %endif
614 xchg ax, cx ; restore low LBA, save high LBA / SPT
615 idiv word [bx(bpbSectorsPerTrack)]
616 ; ax = LBA / SPT
617 ; dx = LBA % SPT = sector - 1
618 inc dx
620 xchg cx, dx ; restore high LBA / SPT, save sector no.
621 idiv word [bx(bpbHeadsPerCylinder)] ; may overflow if SectorsPerTrack * HeadsPerCylinder < 256 or disk > 8GB
622 ; ax = (LBA / SPT) / HPC = cylinder
623 ; dx = (LBA / SPT) % HPC = head
625 xchg ch, al ; clear al
626 ; ch = LSB 0...7 of cylinder no.
627 shr ax, 1
628 shr ax, 1
629 or cl, al
630 ; cl = MSB 8...9 of cylinder no. + sector no.
631 mov dh, dl
632 ; dh = head no.
634 %if CHSsanityCheck != 0
635 %if LBAsupport != 0
636 or ah, [si+8+3] ; 8GB max, last cylinder 1023
637 %else
638 or ah, [si+2+3] ; 8GB max, last cylinder 1023
639 %endif
640 %endif
641 CHSoverflow:
642 %if CHSsanityCheck != 0 && (ReadRetry != 0 || LBAsupport != 0)
643 pushf
644 %endif
646 ReadSectorRetry:
647 mov dl, [bx(DriveNumber)]
648 ; dl = drive no.
649 %if LBAsupport != 0
650 mov ah, 42h ; ah = 42h = extended read function no.
651 int 13h ; extended read sectors (DL, DS:SI)
652 jnc ReadSectorNextSegment
653 %endif
654 %if CHSsanityCheck != 0
655 %if ReadRetry != 0 || LBAsupport != 0
656 popf
657 pushf
658 %endif
659 jne SkipCHS
660 %endif
662 mov ax, 201h ; al = sector count = 1
663 ; ah = 2 = read function no.
664 int 13h ; read sectors (AL, CX, DX, ES:BX)
666 jnc ReadSectorNextSegment
667 SkipCHS:
668 %if ReadRetry != 0
669 cbw ; ah = 0 = reset function
670 int 13h ; reset drive (DL)
672 dec di
673 jnz ReadSectorRetry ; up to 15 extra attempt
674 %endif
676 ReadError:
677 call Error
678 db "Read error."
680 ReadSectorNextSegment:
681 %if CHSsanityCheck != 0 && (ReadRetry != 0 || LBAsupport != 0)
682 popf
683 %endif
685 %macro adjust_es 2
686 %if SectorOf512Bytes != 0
687 add word %1, byte 32 ; adjust segment for next sector
688 %else
689 %if %2 != 0
690 mov al, %2
691 %endif
692 mul byte [bx(bpbBytesPerSector)+1] ; = (bpbBytesPerSector/256)*16
693 add %1, ax ; adjust segment for next sector
694 %endif
695 %endmacro
697 %if LBAsupport != 0
698 pop ax ; al = 16
699 adjust_es [si+6], 0
700 pop cx ; sector count = 1
701 pop bx
702 %else
703 adjust_es [si], 16
704 %endif
705 pop es ; es:0 updated
706 pop ax
707 pop dx
708 %if LBAsupport != 0
709 pop di
710 pop di
711 add ax, cx ; adjust LBA for next sector
712 %else
713 add ax, 1 ; adjust LBA for next sector
714 %endif
716 pop cx ; cluster sectors to read
717 pop di ; file sectors to read
718 dec di ; keep C
719 loopne ReadSectorNext ; until cluster sector count or file sector count
720 pop si
721 mov ax, bx ; clear ax
722 mov dx, [bx(DriveNumber)] ; pass the BIOS boot drive to Run or Error
724 ret
726 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
727 ;; Fill free space with zeroes ;;
728 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
730 times (512-13-($-$$)) db 0
732 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
733 ;; Name of the file to load and run ;;
734 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
736 NameLength equ 11
737 ProgramName times NameLength db 0 ; name and extension
739 ;;;;;;;;;;;;;;;;;;;;;;;;;;
740 ;; End of the sector ID ;;
741 ;;;;;;;;;;;;;;;;;;;;;;;;;;
743 ClusterList dw 0AA55h ; BIOS checks for this ID