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

fusecloop/extract_compressed_fs: can convert to v0.68 or v1.0
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sat Jun 22 12:48:49 2024 +0000 (7 days ago)
parents 0af8df0b46fc
children
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 ;; ("No bootfile" 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
105 AnyWhere equ 1 ; +4 bytes
107 [BITS 16]
108 [CPU 8086]
110 ImageLoadSeg equ 60h
111 StackSize equ 3072 ; Stack + cluster list
113 [SECTION .text]
114 [ORG 0]
116 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
117 ;; Boot sector starts here ;;
118 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
120 boot:
121 DriveNumber:
122 jmp short start ; MS-DOS/Windows checks for this jump
123 nop
124 bsOemName DB "BootProg" ; 0x03
126 ;;;;;;;;;;;;;;;;;;;;;
127 ;; BPB starts here ;;
128 ;;;;;;;;;;;;;;;;;;;;;
130 bpbBytesPerSector DW 0 ; 0x0B 512, 1024, 2048 or 4096
131 bpbSectorsPerCluster DB 0 ; 0x0D 1, 2, 4, 8, 16, 32 or 64
132 bpbReservedSectors DW 0 ; 0x0E 1
133 bpbNumberOfFATs DB 0 ; 0x10 2
134 bpbRootEntries DW 0 ; 0x11
135 bpbTotalSectors DW 0 ; 0x13
136 bpbMedia DB 0 ; 0x15
137 bpbSectorsPerFAT DW 0 ; 0x16
138 bpbSectorsPerTrack DW 0 ; 0x18
139 bpbHeadsPerCylinder DW 0 ; 0x1A
140 bpbHiddenSectors DD 0 ; 0x1C
141 bpbTotalSectorsBig DD 0 ; 0x20
143 ;;;;;;;;;;;;;;;;;;;
144 ;; BPB ends here ;;
145 ;;;;;;;;;;;;;;;;;;;
147 bsDriveNumber DB 0 ; 0x24
148 bsUnused DB 0 ; 0x25
149 bsExtBootSignature DB 0 ; 0x26 29
150 bsSerialNumber DD 0 ; 0x27
151 bsVolumeLabel times 11 DB " " ; 0x2B "NO NAME "
152 bsFileSystem times 8 DB " " ; 0x36 "FAT12 " or "FAT16 "
154 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
155 ;; Boot sector code starts here ;;
156 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
158 start:
160 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
161 ;; How much RAM is there? ;;
162 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
164 int 12h ; get conventional memory size (in KBs)
165 %if AnyWhere
166 call here
167 here:
168 pop si
169 sub si, here - boot - 2
170 push cs
171 pop ds
172 %endif
173 mov cx, 106h
174 shl ax, cl ; and convert it to 16-byte paragraphs
176 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
177 ;; Reserve memory for the boot sector and its stack ;;
178 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
180 sub ax, (512+StackSize) /16 ; reserve bytes for the code and the stack
181 mov es, ax ; cs:0 = ds:0 = ss:0 -> top - 512 - StackSize
182 mov ss, ax
183 mov sp, 512+StackSize ; bytes 0-511 are reserved for the boot code
185 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
186 ;; Copy ourselves to top of memory ;;
187 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
189 xor di, di
190 %if AnyWhere == 0
191 mov si, 7C02h ; exclude DriveNumber
192 mov ds, di
193 %endif
194 push es
195 cld
196 xchg ax, dx
197 stosw ; store BIOS boot drive number
198 rep movsw ; move 512 bytes (+ 12), no double prefix: 8088 bug
200 ;;;;;;;;;;;;;;;;;;;;;;
201 ;; Jump to the copy ;;
202 ;;;;;;;;;;;;;;;;;;;;;;
204 mov cl, byte main
205 push cx
206 retf
208 main:
209 %if ImageLoadSeg != main-boot
210 %if ImageLoadSeg >= 100h
211 mov cx, ImageLoadSeg
212 %else
213 mov cl, ImageLoadSeg
214 %endif
215 %endif
216 push cx
218 ;;;;;;;;;;;;;;;;;;;;;;;;;;
219 ;; Get drive parameters ;;
220 ;; Update heads count ;;
221 ;; for current BIOS ;;
222 ;;;;;;;;;;;;;;;;;;;;;;;;;;
224 %if GeometryCheck != 0
225 %if ((main-boot) & 3Fh) != 0
226 xor cx, cx
227 %endif
228 mov ah, 8 ; update AX,BL,CX,DX,DI, and ES registers
229 int 13h ; may destroy SI,BP, and DS registers
230 %endif
231 push cs
232 pop ds
233 xor bx, bx
235 %if GeometryCheck != 0
236 and cx, byte 3Fh
237 je BadParams ; verify updated and validity
238 mov [bx(bpbSectorsPerTrack)], cx
239 mov cl, dh
240 inc cx
241 mov [bx(bpbHeadsPerCylinder)], cx
242 BadParams:
243 %endif
245 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
246 ;; Load FAT (FAT12: 6KB max, FAT16: 128KB max) ;;
247 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
249 pop es ; ImageLoadSeg
250 push es
252 mul bx ; dx:ax = 0 = LBA (LBA are relative to FAT)
253 mov di, word [bx(bpbSectorsPerFAT)]
255 call ReadDISectors ; read fat; clear ax, cx, di; bp = SectorsPerFAT
257 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
258 ;; load the root directory in ;;
259 ;; its entirety (16KB max) ;;
260 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
262 %if SectorOf512Bytes != 0
263 mov di, word [bx(bpbRootEntries)]
264 mov cl, 4
265 shr di, cl ; di = root directory size in sectors
266 %else
267 mov al, 32
269 mul word [bx(bpbRootEntries)]
270 div word [bx(bpbBytesPerSector)]
271 xchg ax, di ; di = root directory size in sectors, clear ah
272 %endif
274 %if Always2FATs != 0
275 mov al, 2
276 %else
277 mov al, [bpbNumberOfFATs]
278 %endif
279 mul bp ; [bx(bpbSectorsPerFAT)], set by ReadDISectors
281 push es ; read root directory
282 call ReadDISectors ; clear ax, cx, di; bp = first data sector
283 pop es
285 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
286 ;; Look for the COM/EXE file to load and run ;;
287 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
289 ; es:di -> root entries array
291 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
292 ;; Looks for the file/dir ProgramName ;;
293 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
294 ;; Input: ES:DI -> root directory array ;;
295 ;; Output: SI = cluster number ;;
296 ;; AX = file sector count ;;
297 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
299 FindNameCycle:
300 push di
301 mov cl, NameLength
302 mov si, ProgramName ; ds:si -> program name
303 repe cmpsb
304 %if CheckAttrib != 0
305 VolumeLabel equ 8
306 SubDirectory equ 10h
307 jnz SkipFindName
308 test byte [es:di], VolumeLabel+SubDirectory
309 SkipFindName:
310 %endif
311 pop di
312 je FindNameFound
313 %if NullEntryCheck != 0
314 scasb ; Z == NC
315 cmc
316 lea di, [di+31]
317 dec word [bx(bpbRootEntries)]
318 ja FindNameCycle ; next root entry
319 %else
320 add di, byte 32
321 dec word [bx(bpbRootEntries)]
322 jnz FindNameCycle ; next root entry
323 %endif
325 FindNameFailed:
326 call Error
327 db "No bootfile."
329 FindNameFound:
330 push si
331 mov si, [es:di+1Ah] ; si = cluster no.
332 les ax, [es:di+1Ch] ; file size
333 mov dx, es
334 div word [bx(bpbBytesPerSector)]
335 cmp bx, dx ; sector aligned ?
336 adc ax, bx ; file last sector
337 pop di ; di = ClusterList
339 pop es ; ImageLoadSeg
340 push ax
342 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
343 ;; build cluster list ;;
344 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
345 ;; Input: ES:0 -> FAT ;;
346 ;; SI = first cluster ;;
347 ;; DI = cluster list :;
348 ;; CH = 0 ;;
349 ;; Output: SI = cluster list ;;
350 ;; CH = 0 ;;
351 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
353 FAT12 equ 1
354 FAT16 equ 1
355 TINYFAT16 equ 1 ; < 4072 clusters fat16 support
356 push di ; up to 2 * 635K / BytesPerCluster = 2540 bytes
357 %if FAT12 != 1 && FAT16 != 1
358 %error "FAT12 and/or FAT16 must be set"
359 %endif
360 %if FAT12 == 1
361 mov cl, 12
362 %else
363 nop
364 nop
365 %endif
366 ClusterLoop:
367 mov [di], si
368 add si, si ; si = cluster * 2
369 %if FAT16 == 1
370 mov ax, es ; ax = FAT segment = ImageLoadSeg
371 jnc First64k
372 mov ah, (1000h+ImageLoadSeg)>>8 ; adjust segment for 2nd part of FAT16
373 First64k:
374 %if FAT12 == 1
375 mov dx, 0FFF6h
376 %if TINYFAT16 == 1
377 test [bx(bsFileSystem+4)], cl ; FAT12 or FAT16 ? clear C (bit 2)
378 jne ReadClusterFat16
379 %else
380 cmp [bx(bpbSectorsPerFAT)], cx ; 1..12 = FAT12, 16..256 = FAT16
381 ja ReadClusterFat16
382 %endif
383 mov dh, 0Fh
384 %endif
385 %endif
386 %if FAT12 == 1
387 add si, [di]
388 shr si, 1 ; si = cluster * 3 / 2
389 %else
390 nop
391 nop
392 %endif
393 %if FAT16 == 1
394 ReadClusterFat16:
395 push ds
396 mov ds, ax
397 lodsw ; ax = next cluster
398 pop ds
399 %else
400 es lodsw
401 %endif
402 %if FAT12 == 1
403 jnc ReadClusterEven
404 rol ax, cl
405 ReadClusterEven:
406 scasw ; di += 2
407 %if FAT16 == 1
408 and ah, dh ; mask cluster value
409 cmp ax, dx
410 %else
411 and ah, 0Fh ; mask cluster value
412 cmp ax, 0FF6h
413 %endif
414 %else
415 scasw ; di += 2
416 cmp ax, 0FFF6h
417 %endif
418 xchg ax, si
419 jc ClusterLoop
420 pop si
421 pop di ; file size in sectors
423 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
424 ;; Load the entire file ;;
425 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
426 ;; Input: ES:0 -> buffer ;;
427 ;; SI = cluster list ;;
428 ;; DI = file sectors ;;
429 ;; CH = 0 ;;
430 ;; BP = 1st data sec ;;
431 ;; Output: BP:0 -> buffer ;;
432 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
434 push es
436 ReadClusters:
437 lodsw
438 dec ax
439 dec ax
441 mov cl, [bx(bpbSectorsPerCluster)] ; 1..128
442 mul cx ; cx = sector count (ch = 0)
444 add ax, bp ; LBA for cluster data
445 adc dx, bx ; dx:ax = LBA
447 call ReadSectors ; clear ax, restore dx
449 jne ReadClusters ; until end of file
451 pop bp ; ImageLoadSeg
453 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
454 ;; Type detection, .COM or .EXE? ;;
455 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
457 mov ds, bp ; bp=ds=seg the file is loaded to
459 add bp, [bx+08h] ; bp = image base
460 mov di, [bx+18h] ; di = reloc table pointer
462 cmp word [bx], 5A4Dh ; "MZ" signature?
463 je RelocateEXE ; yes, it's an EXE program
465 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
466 ;; Setup and run a .COM program ;;
467 ;; Set CS=DS=ES=SS SP=0 IP=100h ;;
468 ;; AX=0ffffh BX=0 DX=drive and ;;
469 ;; cmdline=void ;;
470 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
472 mov di, 100h ; ip
473 mov bp, ImageLoadSeg-10h ; "org 100h" stuff :)
474 mov ss, bp
475 xor sp, sp
476 push bp ; cs, ds and es
477 jmp short Run
479 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
480 ;; Relocate, setup and run a .EXE program ;;
481 ;; Set CS:IP, SS:SP, DS, ES and AX according ;;
482 ;; to wiki.osdev.org/MZ#Initial_Program_State ;;
483 ;; AX=0ffffh BX=0 DX=drive cmdline=void ;;
484 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
486 ReloCycle:
487 add [di+2], bp ; item seg (abs)
488 les si, [di] ; si = item ofs, es = item seg
489 add [es:si], bp ; fixup
490 scasw ; di += 2
491 scasw ; point to next entry
493 RelocateEXE:
494 dec word [bx+06h] ; reloc items, 32768 max (128KB table)
495 jns ReloCycle
496 ; PSP don't have a valid drive identifier
497 les si, [bx+0Eh]
498 add si, bp
499 mov ss, si ; ss for EXE
500 mov sp, es ; sp for EXE
502 lea si, [bp-10h] ; ds and es both point to the segment
503 push si ; containing the PSP structure
505 add bp, [bx+16h] ; cs for EXE
506 mov di, [bx+14h] ; ip for EXE
507 Run:
508 pop ds
509 push bp
510 push di
511 push ds
512 pop es
513 mov [80h], ax ; clear cmdline
514 dec ax ; both FCB in the PSP don't have a valid drive identifier
516 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
517 ;; Set the magic numbers so the program knows that it ;;
518 ;; has been loaded by this bootsector and not by MS-DOS ;;
519 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
520 mov si, 16381 ; prime number 2**14-3
521 mov di, 32749 ; prime number 2**15-19
522 mov bp, 65521 ; prime number 2**16-15
524 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
525 ;; All done, transfer control to the program now ;;
526 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
527 retf
529 ;;;;;;;;;;;;;;;;;;;;;;;;;;
530 ;; Error Messaging Code ;;
531 ;;;;;;;;;;;;;;;;;;;;;;;;;;
533 Error:
534 pop si
536 PutStr:
537 mov ah, 0Eh
538 mov bl, 7
539 lodsb
540 int 10h
541 cmp al, "."
542 jne PutStr
543 %if WaitForKey != 0
544 cbw
545 int 16h ; wait for a key...
546 int 19h ; bootstrap
547 %endif
548 Stop:
549 hlt
550 jmp short Stop
552 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
553 ;; Read sectors using BIOS Int 13h ;;
554 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
555 ;; Input: DX:AX = LBA relative to FAT ;;
556 ;; BX = 0 ;;
557 ;; DI = sector count ;;
558 ;; ES:BX -> buffer address ;;
559 ;; Output: ES:BX -> next address ;;
560 ;; BX = 0 ;;
561 ;; CX = 0 ;;
562 ;; DI = 0 ;;
563 ;; DL = drive number ;;
564 ;; 0:BP = next LBA from FAT ;;
565 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
567 ReadDISectors:
568 mov bp, di
569 add bp, ax ; adjust LBA for cluster data
571 mov cx, di ; no cluster size limit
573 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
574 ;; Read sectors using BIOS Int 13h ;;
575 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
576 ;; Input: DX:AX = LBA relative to FAT ;;
577 ;; BX = 0 ;;
578 ;; CX = sector count ;;
579 ;; DI = file sectors ;;
580 ;; ES:BX -> buffer address ;;
581 ;; Output: ES:BX -> next address ;;
582 ;; BX = 0 ;;
583 ;; CX or DI = 0 ;;
584 ;; DL = drive number ;;
585 ;; Keep: BP, SI ;;
586 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
588 ReadSectors:
589 add ax, [bx(bpbHiddenSectors)]
590 adc dx, [bx(bpbHiddenSectors)+2]
591 add ax, [bx(bpbReservedSectors)]
593 push si
594 ReadSectorNext:
595 adc dx, bx
596 push di
597 push cx
599 %if LBAsupport != 0
600 push bx
601 push bx
602 %endif
603 push dx ; 32-bit LBA: up to 2TB
604 push ax
605 push es
606 %if ReadRetry != 0 || LBAsupport != 0
607 mov di, 16 ; packet size byte = 16, reserved byte = 0
608 %endif
609 %if LBAsupport != 0
610 push bx
611 inc bx ; sector count word = 1
612 push bx
613 dec bx
614 push di
615 %endif
617 xchg ax, cx ; save low LBA
618 xchg ax, dx ; get high LBA
619 cwd ; clear dx (LBA offset <1TB)
620 idiv word [bx(bpbSectorsPerTrack)] ; up to 8GB disks
622 %if CHSsanityCheck != 0
623 cmp ax, [bx(bpbHeadsPerCylinder)]
624 jns CHSoverflow
625 %endif
626 xchg ax, cx ; restore low LBA, save high LBA / SPT
627 idiv word [bx(bpbSectorsPerTrack)]
628 ; ax = LBA / SPT
629 ; dx = LBA % SPT = sector - 1
630 inc dx
632 xchg cx, dx ; restore high LBA / SPT, save sector no.
633 idiv word [bx(bpbHeadsPerCylinder)] ; may overflow if SectorsPerTrack * HeadsPerCylinder < 256 or disk > 8GB
634 ; ax = (LBA / SPT) / HPC = cylinder
635 ; dx = (LBA / SPT) % HPC = head
637 xchg ch, al ; clear al, save cylinder no.
638 ; ch = LSB 0...7 of cylinder no.
639 shr ax, 1
640 shr ax, 1
641 or cl, al
642 ; cl = MSB 8...9 of cylinder no. + sector no.
643 mov dh, dl
644 ; dh = head no.
646 %if CHSsanityCheck != 0
647 dec ah ; set S if cylinder < 1024 (8GB max)
648 CHSoverflow:
649 %endif
650 mov si, sp
651 %if CHSsanityCheck != 0 && (ReadRetry != 0 || LBAsupport != 0)
652 pushf
653 %endif
655 ReadSectorRetry:
656 mov dl, [bx(DriveNumber)]
657 ; dl = drive no.
658 %if LBAsupport != 0
659 mov ah, 42h ; ah = 42h = extended read function no.
660 int 13h ; extended read sectors (DL, DS:SI)
661 jnc ReadSectorNextSegment
662 %endif
663 mov ax, 201h ; al = sector count = 1
664 ; ah = 2 = read function no.
665 %if CHSsanityCheck != 0
666 %if ReadRetry != 0 || LBAsupport != 0
667 popf
668 pushf
669 %endif
670 jns SkipCHS
671 %endif
672 int 13h ; read sectors (AL, CX, DX, ES:BX)
673 jnc ReadSectorNextSegment
674 SkipCHS:
675 %if ReadRetry != 0
676 cbw ; ah = 0 = reset function
677 int 13h ; reset drive (DL)
678 dec di
679 jnz ReadSectorRetry ; up to 15 extra attempt
680 %endif
682 ReadError:
683 call Error
684 db "Read error."
686 ReadSectorNextSegment:
687 %if CHSsanityCheck != 0 && (ReadRetry != 0 || LBAsupport != 0)
688 popf
689 %endif
691 %macro adjust_es 2
692 %if SectorOf512Bytes != 0
693 add word %1, byte 32 ; adjust segment for next sector
694 %else
695 %if %2 != 0
696 mov al, %2
697 %endif
698 mul byte [bx(bpbBytesPerSector)+1] ; = (bpbBytesPerSector/256)*16
699 add %1, ax ; adjust segment for next sector
700 %endif
701 %endmacro
703 %if LBAsupport != 0
704 pop ax ; al = 16
705 adjust_es [si+6], 0
706 pop cx ; sector count = 1
707 pop bx
708 %else
709 adjust_es [si], 16
710 %endif
711 pop es ; es:0 updated
712 pop ax
713 pop dx
714 %if LBAsupport != 0
715 pop di
716 pop di
717 add ax, cx ; adjust LBA for next sector
718 %else
719 add ax, 1 ; adjust LBA for next sector
720 %endif
722 pop cx ; cluster sectors to read
723 pop di ; file sectors to read
724 dec di ; keep C
725 loopne ReadSectorNext ; until cluster sector count or file sector count
726 pop si
727 mov ax, bx ; clear ax
728 mov dx, [bx(DriveNumber)] ; pass the BIOS boot drive to Run or Error
730 ret
732 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
733 ;; Fill free space with zeroes ;;
734 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
736 times (512-13-($-$$)) db 0
738 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
739 ;; Name of the file to load and run ;;
740 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
742 NameLength equ 11
743 ProgramName times NameLength db 0 ; name and extension
745 ;;;;;;;;;;;;;;;;;;;;;;;;;;
746 ;; End of the sector ID ;;
747 ;;;;;;;;;;;;;;;;;;;;;;;;;;
749 ClusterList dw 0AA55h ; BIOS checks for this ID