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

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