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

Up beep (1.4.12), gsoap (2.8.134), perl-test-warnings (0.033)
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sun May 26 08:12:35 2024 +0000 (4 weeks ago)
parents 9d807f162a22
children 65c2646b484d
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
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
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, 7C00h
192 mov ds, di
193 %endif
194 push es
195 mov [si(DriveNumber)], dx ; store BIOS boot drive number
196 cld
197 rep movsw ; move 512 bytes (+ 12)
199 ;;;;;;;;;;;;;;;;;;;;;;
200 ;; Jump to the copy ;;
201 ;;;;;;;;;;;;;;;;;;;;;;
203 mov cl, byte main
204 push cx
205 retf
207 main:
208 %if ImageLoadSeg != main-boot
209 %if ImageLoadSeg >= 100h
210 mov cx, ImageLoadSeg
211 %else
212 mov cl, ImageLoadSeg
213 %endif
214 %endif
215 push cx
217 ;;;;;;;;;;;;;;;;;;;;;;;;;;
218 ;; Get drive parameters ;;
219 ;; Update heads count ;;
220 ;; for current BIOS ;;
221 ;;;;;;;;;;;;;;;;;;;;;;;;;;
223 %if GeometryCheck != 0
224 %if ((main-boot) & 3Fh) != 0
225 xor cx, cx
226 %endif
227 mov ah, 8 ; update AX,BL,CX,DX,DI, and ES registers
228 int 13h ; may destroy SI,BP, and DS registers
229 %endif
230 push cs
231 pop ds
232 xor bx, bx
234 %if GeometryCheck != 0
235 and cx, byte 3Fh
236 je BadParams ; verify updated and validity
237 mov [bx(bpbSectorsPerTrack)], cx
238 mov cl, dh
239 inc cx
240 mov [bx(bpbHeadsPerCylinder)], cx
241 BadParams:
242 %endif
244 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
245 ;; Load FAT (FAT12: 6KB max, FAT16: 128KB max) ;;
246 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
248 pop es ; ImageLoadSeg
249 push es
251 mul bx ; dx:ax = 0 = LBA (LBA are relative to FAT)
252 mov di, word [bx(bpbSectorsPerFAT)]
254 call ReadDISectors ; read fat; clear ax, cx, di; bp = SectorsPerFAT
256 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
257 ;; load the root directory in ;;
258 ;; its entirety (16KB max) ;;
259 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
261 %if SectorOf512Bytes != 0
262 mov di, word [bx(bpbRootEntries)]
263 mov cl, 4
264 shr di, cl ; di = root directory size in sectors
265 %else
266 mov al, 32
268 mul word [bx(bpbRootEntries)]
269 div word [bx(bpbBytesPerSector)]
270 xchg ax, di ; di = root directory size in sectors, clear ah
271 %endif
273 %if Always2FATs != 0
274 mov al, 2
275 %else
276 mov al, [bpbNumberOfFATs]
277 %endif
278 mul bp ; [bx(bpbSectorsPerFAT)], set by ReadDISectors
280 push es ; read root directory
281 call ReadDISectors ; clear ax, cx, di; bp = first data sector
282 pop es
284 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
285 ;; Look for the COM/EXE file to load and run ;;
286 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
288 ; es:di -> root entries array
290 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
291 ;; Looks for the file/dir ProgramName ;;
292 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
293 ;; Input: ES:DI -> root directory array ;;
294 ;; Output: SI = cluster number ;;
295 ;; AX = file sector count ;;
296 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
298 FindNameCycle:
299 push di
300 mov cl, NameLength
301 mov si, ProgramName ; ds:si -> program name
302 repe cmpsb
303 %if CheckAttrib != 0
304 VolumeLabel equ 8
305 SubDirectory equ 10h
306 jnz SkipFindName
307 test byte [es:di], VolumeLabel+SubDirectory
308 SkipFindName:
309 %endif
310 pop di
311 je FindNameFound
312 %if NullEntryCheck != 0
313 scasb ; Z == NC
314 cmc
315 lea di, [di+31]
316 dec word [bx(bpbRootEntries)]
317 ja FindNameCycle ; next root entry
318 %else
319 add di, byte 32
320 dec word [bx(bpbRootEntries)]
321 jnz FindNameCycle ; next root entry
322 %endif
324 FindNameFailed:
325 call Error
326 db "File not found."
328 FindNameFound:
329 push si
330 mov si, [es:di+1Ah] ; si = cluster no.
331 les ax, [es:di+1Ch] ; file size
332 mov dx, es
333 div word [bx(bpbBytesPerSector)]
334 cmp bx, dx ; sector aligned ?
335 adc ax, bx ; file last sector
336 pop di ; di = ClusterList
338 pop es ; ImageLoadSeg
339 push ax
341 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
342 ;; build cluster list ;;
343 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
344 ;; Input: ES:0 -> FAT ;;
345 ;; SI = first cluster ;;
346 ;; DI = cluster list :;
347 ;; CH = 0 ;;
348 ;; Output: SI = cluster list ;;
349 ;; CH = 0 ;;
350 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
352 FAT12 equ 1
353 FAT16 equ 1
354 TINYFAT16 equ 1 ; < 4072 clusters fat16 support
355 push di ; up to 2 * 635K / BytesPerCluster = 2540 bytes
356 %if FAT12 != 1 && FAT16 != 1
357 %error "FAT12 and/or FAT16 must be set"
358 %endif
359 %if FAT12 == 1
360 mov cl, 12
361 %else
362 nop
363 nop
364 %endif
365 ClusterLoop:
366 mov [di], si
367 add si, si ; si = cluster * 2
368 %if FAT16 == 1
369 mov ax, es ; ax = FAT segment = ImageLoadSeg
370 jnc First64k
371 mov ah, (1000h+ImageLoadSeg)>>8 ; adjust segment for 2nd part of FAT16
372 First64k:
373 %if FAT12 == 1
374 mov dx, 0FFF6h
375 %if TINYFAT16 == 1
376 test [bx(bsFileSystem+4)], cl ; FAT12 or FAT16 ? clear C (bit 2)
377 jne ReadClusterFat16
378 %else
379 cmp [bx(bpbSectorsPerFAT)], cx ; 1..12 = FAT12, 16..256 = FAT16
380 ja ReadClusterFat16
381 %endif
382 mov dh, 0Fh
383 %endif
384 %endif
385 %if FAT12 == 1
386 add si, [di]
387 shr si, 1 ; si = cluster * 3 / 2
388 %else
389 nop
390 nop
391 %endif
392 %if FAT16 == 1
393 ReadClusterFat16:
394 push ds
395 mov ds, ax
396 lodsw ; ax = next cluster
397 pop ds
398 %else
399 es lodsw
400 %endif
401 %if FAT12 == 1
402 jnc ReadClusterEven
403 rol ax, cl
404 ReadClusterEven:
405 scasw ; di += 2
406 %if FAT16 == 1
407 and ah, dh ; mask cluster value
408 cmp ax, dx
409 %else
410 and ah, 0Fh ; mask cluster value
411 cmp ax, 0FF6h
412 %endif
413 %else
414 scasw ; di += 2
415 cmp ax, 0FFF6h
416 %endif
417 xchg ax, si
418 jc ClusterLoop
419 pop si
420 pop di ; file size in sectors
422 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
423 ;; Load the entire file ;;
424 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
425 ;; Input: ES:0 -> buffer ;;
426 ;; SI = cluster list ;;
427 ;; DI = file sectors ;;
428 ;; CH = 0 ;;
429 ;; BP = 1st data sec ;;
430 ;; Output: BP:0 -> buffer ;;
431 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
433 push es
435 ReadClusters:
436 lodsw
437 dec ax
438 dec ax
440 mov cl, [bx(bpbSectorsPerCluster)] ; 1..128
441 mul cx ; cx = sector count (ch = 0)
443 add ax, bp ; LBA for cluster data
444 adc dx, bx ; dx:ax = LBA
446 call ReadSectors ; clear ax, restore dx
448 jne ReadClusters ; until end of file
450 pop bp ; ImageLoadSeg
452 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
453 ;; Type detection, .COM or .EXE? ;;
454 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
456 mov ds, bp ; bp=ds=seg the file is loaded to
458 add bp, [bx+08h] ; bp = image base
459 mov di, [bx+18h] ; di = reloc table pointer
461 cmp word [bx], 5A4Dh ; "MZ" signature?
462 je RelocateEXE ; yes, it's an EXE program
464 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
465 ;; Setup and run a .COM program ;;
466 ;; Set CS=DS=ES=SS SP=0 IP=100h ;;
467 ;; AX=0ffffh BX=0 DX=drive and ;;
468 ;; cmdline=void ;;
469 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
471 mov di, 100h ; ip
472 mov bp, ImageLoadSeg-10h ; "org 100h" stuff :)
473 mov ss, bp
474 xor sp, sp
475 push bp ; cs, ds and es
476 jmp short Run
478 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
479 ;; Relocate, setup and run a .EXE program ;;
480 ;; Set CS:IP, SS:SP, DS, ES and AX according ;;
481 ;; to wiki.osdev.org/MZ#Initial_Program_State ;;
482 ;; AX=0ffffh BX=0 DX=drive cmdline=void ;;
483 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
485 ReloCycle:
486 add [di+2], bp ; item seg (abs)
487 les si, [di] ; si = item ofs, es = item seg
488 add [es:si], bp ; fixup
489 scasw ; di += 2
490 scasw ; point to next entry
492 RelocateEXE:
493 dec word [bx+06h] ; reloc items, 32768 max (128KB table)
494 jns ReloCycle
495 ; PSP don't have a valid drive identifier
496 les si, [bx+0Eh]
497 add si, bp
498 mov ss, si ; ss for EXE
499 mov sp, es ; sp for EXE
501 lea si, [bp-10h] ; ds and es both point to the segment
502 push si ; containing the PSP structure
504 add bp, [bx+16h] ; cs for EXE
505 mov di, [bx+14h] ; ip for EXE
506 Run:
507 pop ds
508 push bp
509 push di
510 push ds
511 pop es
512 mov [80h], ax ; clear cmdline
513 dec ax ; both FCB in the PSP don't have a valid drive identifier
515 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
516 ;; Set the magic numbers so the program knows that it ;;
517 ;; has been loaded by this bootsector and not by MS-DOS ;;
518 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
519 mov si, 16381 ; prime number 2**14-3
520 mov di, 32749 ; prime number 2**15-19
521 mov bp, 65521 ; prime number 2**16-15
523 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
524 ;; All done, transfer control to the program now ;;
525 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
526 retf
528 ;;;;;;;;;;;;;;;;;;;;;;;;;;
529 ;; Error Messaging Code ;;
530 ;;;;;;;;;;;;;;;;;;;;;;;;;;
532 Error:
533 pop si
535 PutStr:
536 mov ah, 0Eh
537 mov bl, 7
538 lodsb
539 int 10h
540 cmp al, "."
541 jne PutStr
542 %if WaitForKey != 0
543 cbw
544 int 16h ; wait for a key...
545 int 19h ; bootstrap
546 %endif
547 Stop:
548 hlt
549 jmp short Stop
551 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
552 ;; Read sectors using BIOS Int 13h ;;
553 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
554 ;; Input: DX:AX = LBA relative to FAT ;;
555 ;; BX = 0 ;;
556 ;; DI = sector count ;;
557 ;; ES:BX -> buffer address ;;
558 ;; Output: ES:BX -> next address ;;
559 ;; BX = 0 ;;
560 ;; CX = 0 ;;
561 ;; DI = 0 ;;
562 ;; DL = drive number ;;
563 ;; 0:BP = next LBA from FAT ;;
564 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
566 ReadDISectors:
567 mov bp, di
568 add bp, ax ; adjust LBA for cluster data
570 mov cx, di ; no cluster size limit
572 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
573 ;; Read sectors using BIOS Int 13h ;;
574 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
575 ;; Input: DX:AX = LBA relative to FAT ;;
576 ;; BX = 0 ;;
577 ;; CX = sector count ;;
578 ;; DI = file sectors ;;
579 ;; ES:BX -> buffer address ;;
580 ;; Output: ES:BX -> next address ;;
581 ;; BX = 0 ;;
582 ;; CX or DI = 0 ;;
583 ;; DL = drive number ;;
584 ;; Keep: BP, SI ;;
585 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
587 ReadSectors:
588 add ax, [bx(bpbHiddenSectors)]
589 adc dx, [bx(bpbHiddenSectors)+2]
590 add ax, [bx(bpbReservedSectors)]
592 push si
593 ReadSectorNext:
594 adc dx, bx
595 push di
596 push cx
598 %if LBAsupport != 0
599 push bx
600 push bx
601 %endif
602 push dx ; 32-bit LBA: up to 2TB
603 push ax
604 push es
605 %if ReadRetry != 0 || LBAsupport != 0
606 mov di, 16 ; packet size byte = 16, reserved byte = 0
607 %endif
608 %if LBAsupport != 0
609 push bx
610 inc bx ; sector count word = 1
611 push bx
612 dec bx
613 push di
614 %endif
616 xchg ax, cx ; save low LBA
617 xchg ax, dx ; get high LBA
618 cwd ; clear dx (LBA offset <1TB)
619 idiv word [bx(bpbSectorsPerTrack)] ; up to 8GB disks
621 %if CHSsanityCheck != 0
622 cmp ax, [bx(bpbHeadsPerCylinder)]
623 jns CHSoverflow
624 %endif
625 xchg ax, cx ; restore low LBA, save high LBA / SPT
626 idiv word [bx(bpbSectorsPerTrack)]
627 ; ax = LBA / SPT
628 ; dx = LBA % SPT = sector - 1
629 inc dx
631 xchg cx, dx ; restore high LBA / SPT, save sector no.
632 idiv word [bx(bpbHeadsPerCylinder)] ; may overflow if SectorsPerTrack * HeadsPerCylinder < 256 or disk > 8GB
633 ; ax = (LBA / SPT) / HPC = cylinder
634 ; dx = (LBA / SPT) % HPC = head
636 xchg ch, al ; clear al, save cylinder no.
637 ; ch = LSB 0...7 of cylinder no.
638 shr ax, 1
639 shr ax, 1
640 or cl, al
641 ; cl = MSB 8...9 of cylinder no. + sector no.
642 mov dh, dl
643 ; dh = head no.
645 %if CHSsanityCheck != 0
646 dec ah ; set S if cylinder < 1024 (8GB max)
647 CHSoverflow:
648 %endif
649 mov si, sp
650 %if CHSsanityCheck != 0 && (ReadRetry != 0 || LBAsupport != 0)
651 pushf
652 %endif
654 ReadSectorRetry:
655 mov dl, [bx(DriveNumber)]
656 ; dl = drive no.
657 %if LBAsupport != 0
658 mov ah, 42h ; ah = 42h = extended read function no.
659 int 13h ; extended read sectors (DL, DS:SI)
660 jnc ReadSectorNextSegment
661 %endif
662 mov ax, 201h ; al = sector count = 1
663 ; ah = 2 = read function no.
664 %if CHSsanityCheck != 0
665 %if ReadRetry != 0 || LBAsupport != 0
666 popf
667 pushf
668 %endif
669 jns SkipCHS
670 %endif
671 int 13h ; read sectors (AL, CX, DX, ES:BX)
672 jnc ReadSectorNextSegment
673 SkipCHS:
674 %if ReadRetry != 0
675 cbw ; ah = 0 = reset function
676 int 13h ; reset drive (DL)
677 dec di
678 jnz ReadSectorRetry ; up to 15 extra attempt
679 %endif
681 ReadError:
682 call Error
683 db "Read error."
685 ReadSectorNextSegment:
686 %if CHSsanityCheck != 0 && (ReadRetry != 0 || LBAsupport != 0)
687 popf
688 %endif
690 %macro adjust_es 2
691 %if SectorOf512Bytes != 0
692 add word %1, byte 32 ; adjust segment for next sector
693 %else
694 %if %2 != 0
695 mov al, %2
696 %endif
697 mul byte [bx(bpbBytesPerSector)+1] ; = (bpbBytesPerSector/256)*16
698 add %1, ax ; adjust segment for next sector
699 %endif
700 %endmacro
702 %if LBAsupport != 0
703 pop ax ; al = 16
704 adjust_es [si+6], 0
705 pop cx ; sector count = 1
706 pop bx
707 %else
708 adjust_es [si], 16
709 %endif
710 pop es ; es:0 updated
711 pop ax
712 pop dx
713 %if LBAsupport != 0
714 pop di
715 pop di
716 add ax, cx ; adjust LBA for next sector
717 %else
718 add ax, 1 ; adjust LBA for next sector
719 %endif
721 pop cx ; cluster sectors to read
722 pop di ; file sectors to read
723 dec di ; keep C
724 loopne ReadSectorNext ; until cluster sector count or file sector count
725 pop si
726 mov ax, bx ; clear ax
727 mov dx, [bx(DriveNumber)] ; pass the BIOS boot drive to Run or Error
729 ret
731 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
732 ;; Fill free space with zeroes ;;
733 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
735 times (512-13-($-$$)) db 0
737 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
738 ;; Name of the file to load and run ;;
739 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
741 NameLength equ 11
742 ProgramName times NameLength db 0 ; name and extension
744 ;;;;;;;;;;;;;;;;;;;;;;;;;;
745 ;; End of the sector ID ;;
746 ;;;;;;;;;;;;;;;;;;;;;;;;;;
748 ClusterList dw 0AA55h ; BIOS checks for this ID