wok-current view BootProg/stuff/boot16.asm @ rev 24478

Typos in BootProg
author Pascal Bellard <pascal.bellard@slitaz.org>
date Fri Feb 18 10:06:45 2022 +0000 (2022-02-18)
parents 89c8d8b6cf48
children d211771a0500
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 Limitations: ;;
30 ;; ~~~~~~~~~~~~~~~~~~ ;;
31 ;; - Works only on the 1st MBR partition which must be a DOS partition ;;
32 ;; with FAT12 (File System ID: 1) or FAT16 (File System ID: 4, 6) ;;
33 ;; ;;
34 ;; ;;
35 ;; Known Bugs: ;;
36 ;; ~~~~~~~~~~~ ;;
37 ;; - All bugs are fixed as far as I know. The boot sector has been tested ;;
38 ;; on the following types of diskettes (FAT12): ;;
39 ;; - 360KB 5"25 ;;
40 ;; - 1.2MB 5"25 ;;
41 ;; - 1.44MB 3"5 ;;
42 ;; on my HDD (FAT16). ;;
43 ;; ;;
44 ;; ;;
45 ;; Memory Layout: ;;
46 ;; ~~~~~~~~~~~~~~ ;;
47 ;; The diagram below shows the typical memory layout. The actual location ;;
48 ;; of the boot sector and its stack may be lower than A0000H if the BIOS ;;
49 ;; reserves memory for its Extended BIOS Data Area just below A0000H and ;;
50 ;; reports less than 640 KB of RAM via its Int 12H function. ;;
51 ;; ;;
52 ;; physical address ;;
53 ;; +------------------------+ 00000H ;;
54 ;; | Interrupt Vector Table | ;;
55 ;; +------------------------+ 00400H ;;
56 ;; | BIOS Data Area | ;;
57 ;; +------------------------+ 00500H ;;
58 ;; | PrtScr Status / Unused | ;;
59 ;; +------------------------+ 00600H ;;
60 ;; | Loaded Image | ;;
61 ;; +------------------------+ nnnnnH ;;
62 ;; | Available Memory | ;;
63 ;; +------------------------+ A0000H - 512 - 3KB ;;
64 ;; | Boot Sector | ;;
65 ;; +------------------------+ A0000H - 3KB ;;
66 ;; | 3KB Boot Stack | ;;
67 ;; +------------------------+ A0000H ;;
68 ;; | Video RAM | ;;
69 ;; ;;
70 ;; ;;
71 ;; Boot Image Startup (register values): ;;
72 ;; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ;;
73 ;; dl = BIOS boot drive number (e.g. 0, 80H) ;;
74 ;; cs:ip = program entry point ;;
75 ;; ss:sp = program stack (don't confuse with boot sector's stack) ;;
76 ;; COM program defaults: cs = ds = es = ss = 50h, sp = 0, ip = 100h ;;
77 ;; EXE program defaults: ds = es = EXE data - 10h (fake MS-DOS psp), ;;
78 ;; ax = 0ffffh (both FCB in the PSP don't have a valid drive identifier), ;;
79 ;; cs:ip and ss:sp depends on EXE header ;;
80 ;; Magic numbers: ;;
81 ;; si = 16381 (prime number 2**14-3) ;;
82 ;; di = 32749 (prime number 2**15-19) ;;
83 ;; bp = 65521 (prime number 2**16-15) ;;
84 ;; The magic numbers let the program know whether it has been loaded by ;;
85 ;; this boot sector or by MS-DOS, which may be handy for universal, bare- ;;
86 ;; metal and MS-DOS programs. ;;
87 ;; ;;
88 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
90 %define bx(label) bx+label-boot
92 [BITS 16]
93 [CPU 8086]
95 ? equ 0
96 ImageLoadSeg equ 60h
97 StackSize equ 3072 ; Stack + cluster list
99 [SECTION .text]
100 [ORG 0]
102 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
103 ;; Boot sector starts here ;;
104 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
106 boot:
107 jmp short start ; MS-DOS/Windows checks for this jump
108 nop
109 bsOemName DB "BootProg" ; 0x03
111 ;;;;;;;;;;;;;;;;;;;;;
112 ;; BPB starts here ;;
113 ;;;;;;;;;;;;;;;;;;;;;
115 bpbBytesPerSector DW ? ; 0x0B
116 bpbSectorsPerCluster DB ? ; 0x0D
117 bpbReservedSectors DW ? ; 0x0E
118 bpbNumberOfFATs DB ? ; 0x10
119 bpbRootEntries DW ? ; 0x11
120 bpbTotalSectors DW ? ; 0x13
121 bpbMedia DB ? ; 0x15
122 bpbSectorsPerFAT DW ? ; 0x16
123 bpbSectorsPerTrack DW ? ; 0x18
124 bpbHeadsPerCylinder DW ? ; 0x1A
125 bpbHiddenSectors DD ? ; 0x1C
126 bpbTotalSectorsBig DD ? ; 0x20
128 ;;;;;;;;;;;;;;;;;;;
129 ;; BPB ends here ;;
130 ;;;;;;;;;;;;;;;;;;;
132 bsDriveNumber DB ? ; 0x24
133 bsUnused DB ? ; 0x25
134 bsExtBootSignature DB ? ; 0x26
135 bsSerialNumber DD ? ; 0x27
136 bsVolumeLabel DB "NO NAME " ; 0x2B
137 bsFileSystem DB "FAT12 " ; 0x36
139 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
140 ;; Boot sector code starts here ;;
141 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
143 start:
144 cld
146 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
147 ;; How much RAM is there? ;;
148 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
150 int 12h ; get conventional memory size (in KBs)
151 mov cx, 106h
152 shl ax, cl ; and convert it to 16-byte paragraphs
154 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
155 ;; Reserve memory for the boot sector and its stack ;;
156 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
158 sub ax, (512+StackSize) /16 ; reserve bytes for the code and the stack
159 mov es, ax ; cs:0 = ds:0 = ss:0 -> top - 512 - StackSize
160 mov ss, ax
161 mov sp, 512+StackSize ; bytes 0-511 are reserved for the boot code
163 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
164 ;; Copy ourselves to top of memory ;;
165 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
167 mov si, 7C00h
168 xor di, di
169 mov ds, di
170 mov [si], dx ; store BIOS boot drive number
171 rep movsw ; move 512 bytes (+ 12)
173 ;;;;;;;;;;;;;;;;;;;;;;
174 ;; Jump to the copy ;;
175 ;;;;;;;;;;;;;;;;;;;;;;
177 push es
178 mov cl, byte main
179 push cx
180 retf
182 main:
183 %if ImageLoadSeg != main-boot
184 %if ImageLoadSeg >= 100h
185 mov cx, ImageLoadSeg
186 %else
187 mov cl, ImageLoadSeg
188 %endif
189 %endif
190 push cx
192 ;;;;;;;;;;;;;;;;;;;;;;;;;;
193 ;; Get drive parameters ;;
194 ;; Update heads count ;;
195 ;; for current BIOS ;;
196 ;;;;;;;;;;;;;;;;;;;;;;;;;;
198 mov ah, 8
199 int 13h ; may destroy SI,BP, and DS registers
200 ; update AX,BL,CX,DX,DI, and ES registers
201 push cs
202 pop ds
203 xor bx, bx
205 and cx, 3Fh
206 cmp [bx(bpbSectorsPerTrack)], cx
207 jne BadParams ; verify updated and validity
208 mov al, dh
209 inc ax
210 mov [bpbHeadsPerCylinder], ax
211 BadParams:
213 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
214 ;; Load FAT (FAT12: 6KB max, FAT16: 128KB max) ;;
215 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
217 pop es ; ImageLoadSeg
218 push es
220 mul bx ; dx:ax = 0 = LBA (LBA are relative to FAT)
221 mov cx, word [bx(bpbSectorsPerFAT)]
223 call ReadCXSectors ; read fat and clear cx
225 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
226 ;; load the root directory in ;;
227 ;; its entirety (16KB max) ;;
228 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
230 mov ax, 32
232 mul word [bx(bpbRootEntries)]
233 div word [bx(bpbBytesPerSector)]
234 xchg ax, cx ; cx = root directory size in sectors, clear ax
236 mov al, [bpbNumberOfFATs]
237 mul bp ; [bx(bpbSectorsPerFAT)], set by ReadCXSectors
239 push es
240 call ReadCXSectors ; read root directory, clear cx and di
241 pop es
243 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
244 ;; Look for the COM/EXE file to load and run ;;
245 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
247 ; es:di -> root entries array
249 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
250 ;; Looks for the file/dir ProgramName ;;
251 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
252 ;; Input: ES:DI -> root directory array ;;
253 ;; Output: SI = cluster number ;;
254 ;; AX = file sector count ;;
255 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
257 FindNameCycle:
258 cmp byte [es:di], ch
259 je FindNameFailed ; end of root directory (NULL entry found)
260 push di
261 mov cl, 11
262 mov si, ProgramName ; ds:si -> program name
263 repe cmpsb
264 pop di
265 je FindNameFound
266 add di, 32
267 dec word [bx(bpbRootEntries)]
268 jnz FindNameCycle ; next root entry
270 FindNameFailed:
271 call Error
272 db "File not found."
274 FindNameFound:
275 push si
276 mov si, [es:di+1Ah] ; si = cluster no.
277 les ax, [es:di+1Ch] ; file size
278 mov dx, es
279 div word [bx(bpbBytesPerSector)]
280 cmp bx, dx ; sector aligned ?
281 adc ax, bx ; file last sector
282 pop di ; di = ClusterList
284 pop es ; ImageLoadSeg
285 push ax
287 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
288 ;; build cluster list ;;
289 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
290 ;; Input: ES:0 -> FAT ;;
291 ;; SI = first cluster ;;
292 ;; DI = cluster list :;
293 ;; CH = 0 ;;
294 ;; Output: SI = cluster list ;;
295 ;; CH = 0 ;;
296 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
298 push di ; up to 2 * 635K / BytesPerCluster bytes
299 mov cl, 12
300 ClusterLoop:
301 mov [di], si
303 mov ax, es ; ax = FAT segment = ImageLoadSeg
304 add si, si ; si = cluster * 2
305 jnc First64
306 mov ah, (1000h+ImageLoadSeg)>>8 ; adjust segment for 2nd part of FAT16
307 First64:
308 cmp [bx(bpbSectorsPerFAT)], cx ; 1..12 = FAT12, 16..256 = FAT16
309 mov ds, ax
310 jbe ReadClusterFat12
312 lodsw ; ax = next cluster
313 cmp ax, 0FFF8h
314 jmp ReadClusterDone
316 ReadClusterFat12:
317 add si, [cs:di]
318 shr si, 1 ; si = cluster * 3 / 2
320 lodsw ; ax = next cluster
321 jnc ReadClusterEven
323 rol ax, cl
325 ReadClusterEven:
326 and ax, 0FFFh ; mask cluster value
327 cmp ax, 0FF8h
329 ReadClusterDone:
330 push cs
331 pop ds
332 inc di
333 inc di
334 xchg ax, si
335 jc ClusterLoop
336 pop si
337 pop di ; file size in sectors
339 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
340 ;; Load the entire file ;;
341 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
342 ;; Input: ES:0 -> buffer ;;
343 ;; SI = cluster list ;;
344 ;; DI = file sectors ;;
345 ;; CH = 0 ;;
346 ;; Output: BP:0 -> buffer ;;
347 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
349 push es
351 ReadClusters:
352 lodsw
353 dec ax
354 dec ax
356 mov cl, [bx(bpbSectorsPerCluster)]
357 mul cx ; cx = sector count (ch = 0)
359 add ax, bp ; LBA for cluster data
360 adc dx, bx ; dx:ax = LBA
362 call ReadSector ; clear cx
364 jne ReadClusters
366 pop bp ; ImageLoadSeg
368 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
369 ;; Type detection, .COM or .EXE? ;;
370 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
372 mov ds, bp ; bp=ds=seg the file is loaded to
374 add bp, [bx+08h] ; bp = image base
375 mov ax, [bx+06h] ; ax = reloc items
376 mov di, [bx+18h] ; di = reloc table pointer
378 cmp word [bx], 5A4Dh ; "MZ" signature?
379 je RelocateEXE ; yes, it's an EXE program
381 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
382 ;; Setup and run a .COM program ;;
383 ;; Set CS=DS=ES=SS SP=0 IP=100h ;;
384 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
386 mov bp, ImageLoadSeg-10h ; "org 100h" stuff :)
387 mov ss, bp
388 xor sp, sp
389 push bp ; cs, ds and es
390 mov bh, 1 ; ip
391 jmp short Run
393 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
394 ;; Relocate, setup and run a .EXE program ;;
395 ;; Set CS:IP, SS:SP, DS, ES and AX according ;;
396 ;; to wiki.osdev.org/MZ#Initial_Program_State ;;
397 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
399 ReloCycle:
400 add [di+2], bp ; item seg (abs)
401 les si, [di] ; si = item ofs, es = item seg
402 add [es:si], bp ; fixup
403 scasw ; di += 2
404 scasw ; point to next entry
406 RelocateEXE:
407 dec ax ; 32768 max (128KB table)
408 jns ReloCycle ; leave with ax=0ffffh: both FCB in the
409 ; PSP don't have a valid drive identifier
410 les si, [bx+0Eh]
411 add si, bp
412 mov ss, si ; ss for EXE
413 mov sp, es ; sp for EXE
415 lea si, [bp-10h] ; ds and es both point to the segment
416 push si ; containing the PSP structure
418 add bp, [bx+16h] ; cs for EXE
419 mov bx, [bx+14h] ; ip for EXE
420 Run:
421 pop ds
422 push bp
423 push bx
424 push ds
425 pop es
427 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
428 ;; Set the magic numbers so the program knows that it ;;
429 ;; has been loaded by this bootsector and not by MS-DOS ;;
430 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
431 mov si, 16381 ; prime number 2**14-3
432 mov di, 32749 ; prime number 2**15-19
433 mov bp, 65521 ; prime number 2**16-15
435 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
436 ;; All done, transfer control to the program now ;;
437 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
438 retf
440 ReadCXSectors:
441 mov bp, cx
442 add bp, ax ; adjust LBA for cluster data
444 mov di, cx ; no file size limit
446 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
447 ;; Reads sectors using BIOS Int 13h ;;
448 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
449 ;; Input: DX:AX = LBA relative to FAT ;;
450 ;; BX = 0 ;;
451 ;; CX = sector count ;;
452 ;; DI = file sectors ;;
453 ;; ES:BX -> buffer address ;;
454 ;; Output: ES:BX -> next address ;;
455 ;; BX = 0 ;;
456 ;; CX or DI = 0 ;;
457 ;; DL = drive number ;;
458 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
460 ReadSector:
461 add ax, [bx(bpbHiddenSectors)]
462 adc dx, [bx(bpbHiddenSectors)+2]
463 add ax, [bx(bpbReservedSectors)]
465 push si
466 ReadSectorNext:
467 adc dx, bx
468 push di
469 push cx
471 push bx
472 push bx
473 push dx ; 32-bit LBA: up to 2TB
474 push ax
475 push es
476 push bx
477 inc bx ; sector count word = 1
478 push bx
479 dec bx
480 mov di, 16 ; packet size byte = 16, reserved byte = 0
481 push di
483 xchg ax, cx ; save low LBA
484 xchg ax, dx ; get high LBA
485 cwd ; clear dx (LBA offset <2TB)
486 div word [bx(bpbSectorsPerTrack)] ; up to 8GB disks
488 xchg ax, cx ; restore low LBA, save high LBA / SPT
489 div word [bx(bpbSectorsPerTrack)]
490 ; ax = LBA / SPT
491 ; dx = LBA % SPT = sector - 1
492 inc dx
494 xchg cx, dx ; restore high LBA / SPT, save sector no.
495 div word [bx(bpbHeadsPerCylinder)]
496 ; ax = (LBA / SPT) / HPC = cylinder
497 ; dx = (LBA / SPT) % HPC = head
498 mov ch, al
499 ; ch = LSB 0...7 of cylinder no.
500 mov al, 64
501 mul ah
502 or cl, al
503 ; cl = MSB 8...9 of cylinder no. + sector no.
504 mov dh, dl
505 ; dh = head no.
507 ReadSectorRetry:
508 mov dl, [bx]
509 ; dl = drive no.
510 mov ah, 42h ; ah = 42h = extended read function no.
511 mov si, sp
512 int 13h ; extended read sectors (DL, DS:SI)
513 jnc ReadSectorNextSegment
515 mov ax, 201h ; al = sector count = 1
516 ; ah = 2 = read function no.
517 int 13h ; read sectors (AL, CX, DX, ES:BX)
519 jnc ReadSectorNextSegment
520 cbw ; ah = 0 = reset function
521 int 13h ; reset drive (DL)
523 dec di
524 jnz ReadSectorRetry ; extra attempt
526 call Error
527 db "Read error."
529 ReadSectorNextSegment:
531 pop ax ; al = 16
532 mul byte [bx(bpbBytesPerSector)+1] ; = (bpbBytesPerSector/256)*16
533 pop cx ; sector count = 1
534 pop bx
535 add [si+6], ax ; adjust segment for next sector
536 pop es ; es:0 updated
537 pop ax
538 pop dx
539 pop di
540 pop di
542 add ax, cx ; adjust LBA for next sector
544 pop cx ; cluster sectors to read
545 pop di ; file sectors to read
546 dec di ; keep C
547 loopne ReadSectorNext ; until cluster sector count or file sector count is reached
548 pop si
549 mov dx, [bx] ; pass the BIOS boot drive to Run or Error
551 ret
553 ;;;;;;;;;;;;;;;;;;;;;;;;;;
554 ;; Error Messaging Code ;;
555 ;;;;;;;;;;;;;;;;;;;;;;;;;;
557 Error:
558 pop si
560 PutStr:
561 mov ah, 0Eh
562 mov bl, 7
563 lodsb
564 int 10h
565 cmp al, "."
566 jne PutStr
568 cbw
569 int 16h ; wait for a key...
570 int 19h ; bootstrap
572 Stop:
573 hlt
574 jmp short Stop
576 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
577 ;; Fill free space with zeroes ;;
578 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
580 times (512-13-($-$$)) db 0
582 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
583 ;; Name of the file to load and run ;;
584 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
586 ProgramName db "STARTUP BIN" ; name and extension each must be
587 ; padded with spaces (11 bytes total)
589 ;;;;;;;;;;;;;;;;;;;;;;;;;;
590 ;; End of the sector ID ;;
591 ;;;;;;;;;;;;;;;;;;;;;;;;;;
593 ClusterList dw 0AA55h ; BIOS checks for this ID