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

Remove nimrod
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sun May 14 19:10:16 2023 +0000 (12 months ago)
parents 82f370bad6b5
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 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 8088 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 of the disk 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 ; +9/+13 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 && FAT16 != 1
346 %error "FAT12 and/or FAT16 must be set"
347 %endif
348 %if FAT12 == 1
349 mov cl, 12
350 %else
351 nop
352 nop
353 %endif
354 ClusterLoop:
355 mov [di], si
356 add si, si ; si = cluster * 2
357 %if FAT16 == 1
358 mov ax, es ; ax = FAT segment = ImageLoadSeg
359 jnc First64k
360 mov ah, (1000h+ImageLoadSeg)>>8 ; adjust segment for 2nd part of FAT16
361 First64k:
362 %if FAT12 == 1
363 mov dx, 0FFF6h
364 %if TINYFAT16 == 1
365 test [bx(bsFileSystem+4)], cl ; FAT12 or FAT16 ? clear C (bit 2)
366 jne ReadClusterFat16
367 %else
368 cmp [bx(bpbSectorsPerFAT)], cx ; 1..12 = FAT12, 16..256 = FAT16
369 ja ReadClusterFat16
370 %endif
371 mov dh, 0Fh
372 %endif
373 %endif
374 %if FAT12 == 1
375 add si, [di]
376 shr si, 1 ; si = cluster * 3 / 2
377 %else
378 nop
379 nop
380 %endif
381 %if FAT16 == 1
382 ReadClusterFat16:
383 push ds
384 mov ds, ax
385 lodsw ; ax = next cluster
386 pop ds
387 %else
388 es lodsw
389 %endif
390 %if FAT12 == 1
391 jnc ReadClusterEven
392 rol ax, cl
393 ReadClusterEven:
394 scasw ; di += 2
395 %if FAT16 == 1
396 and ah, dh ; mask cluster value
397 cmp ax, dx
398 %else
399 and ah, 0Fh ; mask cluster value
400 cmp ax, 0FF6h
401 %endif
402 %else
403 scasw ; di += 2
404 cmp ax, 0FFF6h
405 %endif
406 xchg ax, si
407 jc ClusterLoop
408 pop si
409 pop di ; file size in sectors
411 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
412 ;; Load the entire file ;;
413 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
414 ;; Input: ES:0 -> buffer ;;
415 ;; SI = cluster list ;;
416 ;; DI = file sectors ;;
417 ;; CH = 0 ;;
418 ;; BP = 1st data sec ;;
419 ;; Output: BP:0 -> buffer ;;
420 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
422 push es
424 ReadClusters:
425 lodsw
426 dec ax
427 dec ax
429 mov cl, [bx(bpbSectorsPerCluster)] ; 1..128
430 mul cx ; cx = sector count (ch = 0)
432 add ax, bp ; LBA for cluster data
433 adc dx, bx ; dx:ax = LBA
435 call ReadSectors ; clear ax, restore dx
437 jne ReadClusters ; until end of file
439 pop bp ; ImageLoadSeg
441 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
442 ;; Type detection, .COM or .EXE? ;;
443 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
445 mov ds, bp ; bp=ds=seg the file is loaded to
447 add bp, [bx+08h] ; bp = image base
448 mov di, [bx+18h] ; di = reloc table pointer
450 cmp word [bx], 5A4Dh ; "MZ" signature?
451 je RelocateEXE ; yes, it's an EXE program
453 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
454 ;; Setup and run a .COM program ;;
455 ;; Set CS=DS=ES=SS SP=0 IP=100h ;;
456 ;; AX=0ffffh BX=0 DX=drive and ;;
457 ;; cmdline=void ;;
458 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
460 mov di, 100h ; ip
461 mov bp, ImageLoadSeg-10h ; "org 100h" stuff :)
462 mov ss, bp
463 xor sp, sp
464 push bp ; cs, ds and es
465 jmp short Run
467 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
468 ;; Relocate, setup and run a .EXE program ;;
469 ;; Set CS:IP, SS:SP, DS, ES and AX according ;;
470 ;; to wiki.osdev.org/MZ#Initial_Program_State ;;
471 ;; AX=0ffffh BX=0 DX=drive cmdline=void ;;
472 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
474 ReloCycle:
475 add [di+2], bp ; item seg (abs)
476 les si, [di] ; si = item ofs, es = item seg
477 add [es:si], bp ; fixup
478 scasw ; di += 2
479 scasw ; point to next entry
481 RelocateEXE:
482 dec word [bx+06h] ; reloc items, 32768 max (128KB table)
483 jns ReloCycle
484 ; PSP don't have a valid drive identifier
485 les si, [bx+0Eh]
486 add si, bp
487 mov ss, si ; ss for EXE
488 mov sp, es ; sp for EXE
490 lea si, [bp-10h] ; ds and es both point to the segment
491 push si ; containing the PSP structure
493 add bp, [bx+16h] ; cs for EXE
494 mov di, [bx+14h] ; ip for EXE
495 Run:
496 pop ds
497 push bp
498 push di
499 push ds
500 pop es
501 mov [80h], ax ; clear cmdline
502 dec ax ; both FCB in the PSP don't have a valid drive identifier
504 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
505 ;; Set the magic numbers so the program knows that it ;;
506 ;; has been loaded by this bootsector and not by MS-DOS ;;
507 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
508 mov si, 16381 ; prime number 2**14-3
509 mov di, 32749 ; prime number 2**15-19
510 mov bp, 65521 ; prime number 2**16-15
512 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
513 ;; All done, transfer control to the program now ;;
514 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
515 retf
517 ;;;;;;;;;;;;;;;;;;;;;;;;;;
518 ;; Error Messaging Code ;;
519 ;;;;;;;;;;;;;;;;;;;;;;;;;;
521 Error:
522 pop si
524 PutStr:
525 mov ah, 0Eh
526 mov bl, 7
527 lodsb
528 int 10h
529 cmp al, "."
530 jne PutStr
531 %if WaitForKey != 0
532 cbw
533 int 16h ; wait for a key...
534 int 19h ; bootstrap
535 %endif
536 Stop:
537 hlt
538 jmp short Stop
540 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
541 ;; Read sectors using BIOS Int 13h ;;
542 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
543 ;; Input: DX:AX = LBA relative to FAT ;;
544 ;; BX = 0 ;;
545 ;; DI = sector count ;;
546 ;; ES:BX -> buffer address ;;
547 ;; Output: ES:BX -> next address ;;
548 ;; BX = 0 ;;
549 ;; CX = 0 ;;
550 ;; DI = 0 ;;
551 ;; DL = drive number ;;
552 ;; 0:BP = next LBA from FAT ;;
553 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
555 ReadDISectors:
556 mov bp, di
557 add bp, ax ; adjust LBA for cluster data
559 mov cx, di ; no cluster size limit
561 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
562 ;; Read sectors using BIOS Int 13h ;;
563 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
564 ;; Input: DX:AX = LBA relative to FAT ;;
565 ;; BX = 0 ;;
566 ;; CX = sector count ;;
567 ;; DI = file sectors ;;
568 ;; ES:BX -> buffer address ;;
569 ;; Output: ES:BX -> next address ;;
570 ;; BX = 0 ;;
571 ;; CX or DI = 0 ;;
572 ;; DL = drive number ;;
573 ;; Keep: BP, SI ;;
574 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
576 ReadSectors:
577 add ax, [bx(bpbHiddenSectors)]
578 adc dx, [bx(bpbHiddenSectors)+2]
579 add ax, [bx(bpbReservedSectors)]
581 push si
582 ReadSectorNext:
583 adc dx, bx
584 push di
585 push cx
587 %if LBAsupport != 0
588 push bx
589 push bx
590 %endif
591 push dx ; 32-bit LBA: up to 2TB
592 push ax
593 push es
594 %if ReadRetry != 0 || LBAsupport != 0
595 mov di, 16 ; packet size byte = 16, reserved byte = 0
596 %endif
597 %if LBAsupport != 0
598 push bx
599 inc bx ; sector count word = 1
600 push bx
601 dec bx
602 push di
603 %endif
605 xchg ax, cx ; save low LBA
606 xchg ax, dx ; get high LBA
607 cwd ; clear dx (LBA offset <1TB)
608 idiv word [bx(bpbSectorsPerTrack)] ; up to 8GB disks
610 %if CHSsanityCheck != 0
611 cmp ax, [bx(bpbHeadsPerCylinder)]
612 jns CHSoverflow
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, save cylinder no.
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 dec ah ; set S if cylinder < 1024 (8GB max)
636 CHSoverflow:
637 %endif
638 mov si, sp
639 %if CHSsanityCheck != 0 && (ReadRetry != 0 || LBAsupport != 0)
640 pushf
641 %endif
643 ReadSectorRetry:
644 mov dl, [bx(DriveNumber)]
645 ; dl = drive no.
646 %if LBAsupport != 0
647 mov ah, 42h ; ah = 42h = extended read function no.
648 int 13h ; extended read sectors (DL, DS:SI)
649 jnc ReadSectorNextSegment
650 %endif
651 mov ax, 201h ; al = sector count = 1
652 ; ah = 2 = read function no.
653 %if CHSsanityCheck != 0
654 %if ReadRetry != 0 || LBAsupport != 0
655 popf
656 pushf
657 %endif
658 jns SkipCHS
659 %endif
660 int 13h ; read sectors (AL, CX, DX, ES:BX)
661 jnc ReadSectorNextSegment
662 SkipCHS:
663 %if ReadRetry != 0
664 cbw ; ah = 0 = reset function
665 int 13h ; reset drive (DL)
666 dec di
667 jnz ReadSectorRetry ; up to 15 extra attempt
668 %endif
670 ReadError:
671 call Error
672 db "Read error."
674 ReadSectorNextSegment:
675 %if CHSsanityCheck != 0 && (ReadRetry != 0 || LBAsupport != 0)
676 popf
677 %endif
679 %macro adjust_es 2
680 %if SectorOf512Bytes != 0
681 add word %1, byte 32 ; adjust segment for next sector
682 %else
683 %if %2 != 0
684 mov al, %2
685 %endif
686 mul byte [bx(bpbBytesPerSector)+1] ; = (bpbBytesPerSector/256)*16
687 add %1, ax ; adjust segment for next sector
688 %endif
689 %endmacro
691 %if LBAsupport != 0
692 pop ax ; al = 16
693 adjust_es [si+6], 0
694 pop cx ; sector count = 1
695 pop bx
696 %else
697 adjust_es [si], 16
698 %endif
699 pop es ; es:0 updated
700 pop ax
701 pop dx
702 %if LBAsupport != 0
703 pop di
704 pop di
705 add ax, cx ; adjust LBA for next sector
706 %else
707 add ax, 1 ; adjust LBA for next sector
708 %endif
710 pop cx ; cluster sectors to read
711 pop di ; file sectors to read
712 dec di ; keep C
713 loopne ReadSectorNext ; until cluster sector count or file sector count
714 pop si
715 mov ax, bx ; clear ax
716 mov dx, [bx(DriveNumber)] ; pass the BIOS boot drive to Run or Error
718 ret
720 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
721 ;; Fill free space with zeroes ;;
722 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
724 times (512-13-($-$$)) db 0
726 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
727 ;; Name of the file to load and run ;;
728 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
730 NameLength equ 11
731 ProgramName times NameLength db 0 ; name and extension
733 ;;;;;;;;;;;;;;;;;;;;;;;;;;
734 ;; End of the sector ID ;;
735 ;;;;;;;;;;;;;;;;;;;;;;;;;;
737 ClusterList dw 0AA55h ; BIOS checks for this ID