wok view BootProg/stuff/bootex.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 months ago)
parents 64471e67874e
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 bootex.asm -f bin -o bootex.bin ;;
10 ;; ;;
11 ;; ;;
12 ;; Features: ;;
13 ;; ~~~~~~~~~ ;;
14 ;; - exFAT supported using BIOS int 13h function 42h. ;;
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 637KB 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 ;; ;;
27 ;; Known Bugs: ;;
28 ;; ~~~~~~~~~~~ ;;
29 ;; - All bugs are fixed as far as I know. The boot sector has been tested ;;
30 ;; on a 128MB qemu image. ;;
31 ;; ;;
32 ;; ;;
33 ;; Memory Layout: ;;
34 ;; ~~~~~~~~~~~~~~ ;;
35 ;; The diagram below shows the typical memory layout. The actual location ;;
36 ;; of the boot sector and its stack may be lower than A0000H if the BIOS ;;
37 ;; reserves memory for its Extended BIOS Data Area just below A0000H and ;;
38 ;; reports less than 640 KB of RAM via its Int 12H function. ;;
39 ;; ;;
40 ;; physical address ;;
41 ;; +------------------------+ 00000H ;;
42 ;; | Interrupt Vector Table | ;;
43 ;; +------------------------+ 00400H ;;
44 ;; | BIOS Data Area | ;;
45 ;; +------------------------+ 00500H ;;
46 ;; | PrtScr Status / Unused | ;;
47 ;; +------------------------+ 00600H ;;
48 ;; | Loaded Image | ;;
49 ;; +------------------------+ nnnnnH ;;
50 ;; | Available Memory | ;;
51 ;; +------------------------+ A0000H - 1KB ;;
52 ;; | Boot Sector | ;;
53 ;; +------------------------+ A0000H - 0.5KB ;;
54 ;; | 0.5KB Boot Stack | ;;
55 ;; +------------------------+ A0000H ;;
56 ;; | Video RAM | ;;
57 ;; ;;
58 ;; ;;
59 ;; Boot Image Startup (register values): ;;
60 ;; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ;;
61 ;; ax = 0ffffh (both FCB in the PSP don't have a valid drive identifier), ;;
62 ;; bx = 0, dl = BIOS boot drive number (e.g. 0, 80H) ;;
63 ;; cs:ip = program entry point ;;
64 ;; ss:sp = program stack (don't confuse with boot sector's stack) ;;
65 ;; COM program defaults: cs = ds = es = ss = 50h, sp = 0, ip = 100h ;;
66 ;; EXE program defaults: ds = es = EXE data - 10h (fake MS-DOS psp), ;;
67 ;; cs:ip and ss:sp depends on EXE header ;;
68 ;; Magic numbers: ;;
69 ;; si = 16381 (prime number 2**14-3) ;;
70 ;; di = 32749 (prime number 2**15-19) ;;
71 ;; bp = 65521 (prime number 2**16-15) ;;
72 ;; The magic numbers let the program know whether it has been loaded by ;;
73 ;; this boot sector or by MS-DOS, which may be handy for universal, bare- ;;
74 ;; metal and MS-DOS programs. ;;
75 ;; The command line contains no arguments. ;;
76 ;; ;;
77 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
79 %define bx(label) bx+label-boot
80 %define si(label) si+label-boot
81 NullEntryCheck equ 1 ; +3/5 bytes
82 ReadRetry equ 1 ; +7 bytes
83 SectorOf512Bytes equ 0 ; -12 bytes
84 CheckAttrib equ 0 ; +16/18 bytes
85 WaitForKey equ 0 ; +5 bytes
86 TfatSupport equ 1 ; +10 bytes
87 CheckLBAsupport equ 0 ; +11/21 bytes
88 AnyWhere equ 1 ; +2 bytes
90 [BITS 16]
91 [CPU 386]
93 ImageLoadSeg equ 60h
94 StackSize equ 512
96 [SECTION .text]
97 [ORG 0]
99 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
100 ;; Boot sector starts here ;;
101 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
103 boot:
104 DriveNumber:
105 jmp short start ; Windows checks for this jump
106 nop
107 bsOemName times 8 db " " ; 0x03 "EXFAT "
108 times 53 db 0 ; 0x0B
110 ;;;;;;;;;;;;;;;;;;;;;
111 ;; BPB starts here ;;
112 ;;;;;;;;;;;;;;;;;;;;;
114 bpbSectorStart DQ 0 ; 0x40 partition first sector
115 bpbSectorCount DQ 0 ; 0x48 partition sectors count
116 bpbFatSectorStart DD 0 ; 0x50 FAT first sector
117 bpbFatSectorCount DD 0 ; 0x54 FAT sectors count
118 bpbClusterSectorStart DD 0 ; 0x58 first cluster sector
119 bpbClusterCount DD 0 ; 0x5C total clusters count
120 bpbRootDirCluster DD 0 ; 0x60 first cluster of the root dir
121 bpbVolumeSerial DD 0 ; 0x64 volume serial number
122 bpbFSVersionMinor DB 0 ; 0x68
123 bpbFSVersionMajor DB 0 ; 0x69
124 bpbVolumeStateFlags DW 0 ; 0x6A bit0 = fat used, bit1 = dirty, bit2 = media error
125 bpbSectorSizeBits DB 0 ; 0x6C sector size as (1 << n)
126 bpbSectorPerClusterBits DB 0 ; 0x6D sector per cluster as (1 << n)
127 bpbNumberOfFATs DB 0 ; 0x6E always 1 or 2 (Tfat case)
128 bpbDriveNumber DB 0 ; 0x6F always 0x80
129 bpbAllocatedPercent DB 0 ; 0x70 percentage of allocated space
131 ;;;;;;;;;;;;;;;;;;;
132 ;; BPB ends here ;;
133 ;;;;;;;;;;;;;;;;;;;
135 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
136 ;; Boot sector code starts here ;;
137 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
139 start:
141 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
142 ;; How much RAM is there? ;;
143 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
145 int 12h ; get conventional memory size (in KBs)
146 %if AnyWhere
147 call start2
148 here:
149 %macro BootAnyWhere 0
150 start2:
151 pop si
152 sub si, here - boot
153 %endif
154 mov cx, 106h
155 dec ax ; reserve 1K bytes for the code and the stack
156 shl ax, cl ; and convert it to 16-byte paragraphs
158 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
159 ;; Reserve memory for the boot sector and its stack ;;
160 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
162 mov es, ax ; cs:0 = ds:0 = ss:0 -> top - 512 - StackSize
163 mov ss, ax
164 mov sp, 512+StackSize ; bytes 0-511 are reserved for the boot code
166 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
167 ;; Copy ourselves to top of memory ;;
168 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
170 xor di, di
171 %if AnyWhere == 0
172 mov si, 7C00h
173 mov ds, di
174 %endif
175 push es
176 cld
178 ;;;;;;;;;;;;;;;;;;;;;;
179 ;; Jump to the copy ;;
180 ;;;;;;;;;;;;;;;;;;;;;;
182 %if AnyWhere == 0
183 rep movsw ; move 512 bytes (+ 12)
184 push word main
185 retf
186 %else
187 rep cs movsw ; move 512 bytes (+ 12)
188 push byte main
189 retf
190 %endm
191 %endif
193 %if CheckLBAsupport != 0
194 %macro BootFileName 0
195 main:
196 mov ah, 41h ; clobbers AX, BX, CX, DH
197 mov bx, 55AAh
198 int 13h
199 jc ReadError
200 ; xor bx, 0AA55h
201 ; jnz ReadError
202 ; shr cx, 1 ; function 42h support ?
203 ; jnc ReadError
204 xor cx, cx
205 %else
206 main:
207 %endif
208 push cs
209 pop ds
211 xor ebx, ebx
213 mov [bx(DriveNumber)], dx ; store BIOS boot drive number
214 mov esi, [bx(bpbRootDirCluster)] ; esi=cluster # of root dir
216 push byte ImageLoadSeg
217 pop es ; cx = 0
219 RootDirReadContinue:
220 call ReadCluster ; read one sector of root dir
222 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
223 ;; Look for the COM/EXE file to load and run ;;
224 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
226 ; es:di -> root entries array
228 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
229 ;; Looks for the file/dir ProgramName ;;
230 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
231 ;; Input: ES:DI -> root directory array ;;
232 ;; Output: ESI = cluster number ;;
233 ;; dword [bx+FileSize] file size ;;
234 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
236 CurNameSize equ 03h ; 1 byte
237 StartCluster equ 14h ; 4 bytes
238 FileSize equ 18h ; 8 bytes
240 FindNameCycle:
241 pusha
243 %if NullEntryCheck != 0
244 xor ax, ax
245 or al, [es:di]
246 je FindNameFailed
247 %define curtag al
248 %define chktag al
249 %else
250 %define curtag byte [es:di]
251 %define chktag ax
252 %endif
253 cmp curtag, 0c0h ; EXFAT_ENTRY_FILE_INFO ?
254 jne NotFileInfo
256 mov bl, 32
257 CopyInfo:
258 mov [bx], al
259 dec bx
260 mov al, [es:di+bx]
261 jnz CopyInfo ; keep BIOS boot drive number
263 NotFileInfo:
264 %if CheckAttrib != 0
265 Attributes equ 0Bh ; 1 byte
266 cmp curtag, 85h ; EXFAT_ENTRY_FILE ?
267 jne NotEntryFile
268 mov al, [es:di+Attributes]
269 mov [ProgramName+NameLength+1], al
270 NotEntryFile:
271 %endif
272 mov chktag, 0c1h ; EXFAT_ENTRY_FILE_NAME ?
273 mov cx, NameLength+1
274 mov si, ProgramName ; ds:si -> program name
275 CheckName:
276 scasw ; compare UTF-16
277 lodsb ; with ASCII
278 loope CheckName
279 VolumeLabel equ 8
280 SubDirectory equ 10h
281 %if CheckAttrib != 0
282 jnz SkipFindName
283 test byte [si], VolumeLabel+SubDirectory
284 SkipFindName:
285 %endif
286 je FindNameFound ; cx = 0
287 popa ; restore ax, cx, si, di
289 add di, byte 32
290 cmp di, bp
291 jne FindNameCycle ; next root entry
292 loop RootDirReadContinue ; continue to the next root dir sector
293 cmp esi, byte -10 ; carry=0 if last cluster, and carry=1 otherwise
294 jc RootDirReadContinue ; continue to the next root dir cluster
295 FindNameFailed: ; end of root directory (dir end reached)
296 mov dl, [bx(DriveNumber)] ; restore BIOS boot drive number
297 call Error
298 db "File not found."
299 FindNameFound:
300 mov esi, [bx+StartCluster]
302 ;;;;;;;;;;;;;;;;;;;;;;;;;;
303 ;; Load the entire file ;;
304 ;; Input: ESI = cluster ;;
305 ;; CX = 0 ;;
306 ;;;;;;;;;;;;;;;;;;;;;;;;;;
308 push es
309 %if SectorOf512Bytes == 0
310 xor bp, bp
311 FileReadContinue:
312 shr bp, 4 ; bytes to paragraphs
313 mov di, es
314 add di, bp ; adjust segment for next sector
315 mov es, di ; es:0 updated
316 %else
317 FileReadContinue:
318 %endif
319 call ReadCluster ; read one more sector of the boot file
320 dec cx
321 sub [bx+FileSize], ebp ; max FileSize is < 640KB : check low 32 bits only
322 %if SectorOf512Bytes != 0
323 mov bp, es
324 lea bp, [bp+32]
325 mov es, bp ; es:0 updated
326 %endif
327 ja FileReadContinue
328 mov dx, [bx(DriveNumber)] ; restore BIOS boot drive number
329 xchg ax, di
330 pop bp
332 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
333 ;; Type detection, .COM or .EXE? ;;
334 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
336 mov ds, bp ; bp=ds=seg the file is loaded to
338 add bp, [bx+08h] ; bp = image base
339 mov di, [bx+18h] ; di = reloc table pointer
341 cmp word [bx], 5A4Dh ; "MZ" signature?
342 je RelocateEXE ; yes, it's an EXE program
344 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
345 ;; Setup and run a .COM program ;;
346 ;; Set CS=DS=ES=SS SP=0 IP=100h ;;
347 ;; AX=0ffffh BX=0 DX=drive and ;;
348 ;; cmdline=void ;;
349 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
351 mov di, 100h ; ip
352 mov bp, ImageLoadSeg-10h ; "org 100h" stuff :)
353 mov ss, bp
354 xor sp, sp
355 push bp ; cs, ds and es
356 jmp short Run
358 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
359 ;; Relocate, setup and run a .EXE program ;;
360 ;; Set CS:IP, SS:SP, DS, ES and AX according ;;
361 ;; to wiki.osdev.org/MZ#Initial_Program_State ;;
362 ;; AX=0ffffh BX=0 DX=drive cmdline=void ;;
363 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
365 ReloCycle:
366 add [di+2], bp ; item seg (abs)
367 les si, [di] ; si = item ofs, es = item seg
368 add [es:si], bp ; fixup
369 scasw ; di += 2
370 scasw ; point to next entry
372 RelocateEXE:
373 dec word [bx+06h] ; reloc items, 32768 max (128KB table)
374 jns ReloCycle
376 les si, [bx+0Eh]
377 add si, bp
378 mov ss, si ; ss for EXE
379 mov sp, es ; sp for EXE
381 lea si, [bp-10h] ; ds and es both point to the segment
382 push si ; containing the PSP structure
384 add bp, [bx+16h] ; cs for EXE
385 mov di, [bx+14h] ; ip for EXE
386 Run:
387 pop ds
388 push bp
389 push di
390 push ds
391 pop es
392 mov [80h], ax ; clear cmdline
393 dec ax ; both FCB in the PSP don't have a valid drive identifier
395 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
396 ;; Set the magic numbers so the program knows that it ;;
397 ;; has been loaded by this bootsector and not by MS-DOS ;;
398 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
399 mov si, 16381 ; prime number 2**14-3
400 mov di, 32749 ; prime number 2**15-19
401 mov bp, 65521 ; prime number 2**16-15
403 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
404 ;; All done, transfer control to the program now ;;
405 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
406 retf
407 %if CheckLBAsupport != 0
408 %endm
409 %endif
411 %if AnyWhere
412 BootAnyWhere
413 %endif
415 ;;;;;;;;;;;;;;;;;;;;;;;;;;
416 ;; Error Messaging Code ;;
417 ;;;;;;;;;;;;;;;;;;;;;;;;;;
419 Error:
420 pop si
422 PutStr:
423 mov ah, 0Eh
424 mov bl, 7
425 lodsb
426 int 10h
427 cmp al, "."
428 jne PutStr
429 %if WaitForKey != 0
430 cbw
431 int 16h ; wait for a key...
432 int 19h ; bootstrap
433 %endif
434 Stop:
435 hlt
436 jmp short Stop
438 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
439 ;; Reads a exFAT cluster ;;
440 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
441 ;; Input: EDX:EAX = LBA ;;
442 ;; EBX = 0 ;;
443 ;; CX = sector cnt ;;
444 ;; ESI = cluster no ;;
445 ;; ES:0 -> buffer adrs ;;
446 ;; Output: EDX:EAX = next LBA ;;
447 ;; CX = sector cnt ;;
448 ;; ESI = cluster no ;;
449 ;; EBP = bytes/sector;;
450 ;; Keep: EDI = 0 ;;
451 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
453 ReadCluster:
454 add eax, byte 1
456 inc cx ; jcxnz
457 loop ReadSectorC
459 mul ebx ; edx:eax = 0
460 %if SectorOf512Bytes != 0
461 mov al, 128
462 %define SectorPerClusterBits [bx(bpbSectorPerClusterBits)]
463 %else
464 mov ah, 40h
465 mov cx, [bx(bpbSectorSizeBits)]
466 %define SectorPerClusterBits ch
467 rol ax, cl ; eax=# of exFAT entries per sector
468 %endif
469 lea edi, [esi-2] ; edi=cluster #-2
470 xchg eax, esi
471 div esi ; eax=FAT sector #, edx=entry # in sector
473 imul si, dx, byte 4 ; si=entry # offset in sector
475 cdq
476 add eax, [bx(bpbFatSectorStart)] ; sector # relative to exFAT
477 %if TfatSupport
478 test byte [bx(bpbVolumeStateFlags)], 1
479 jz UseFat0
480 add eax, [bx(bpbFatSectorCount)]
481 UseFat0:
482 %endif
483 call ReadSectorFAT ; read 1 exFAT sector, keep edx=0, set C
485 mov esi, [es:si] ; esi=next cluster #
487 mov dl, SectorPerClusterBits
488 xor ecx, ecx
489 bts ecx, edx ; 10000h max (32MB cluster)
490 xchg eax, edi ; get cluster #-2
491 mul ecx
493 add eax, [bx(bpbClusterSectorStart)]
494 ReadSectorC:
495 mov di, bx
496 ReadSectorFAT:
497 adc edx, ebx
499 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
500 ;; Reads a sector using BIOS Int 13h ;;
501 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
502 ;; Input: EDX:EAX = LBA ;;
503 ;; EBX = 0 ;;
504 ;; ES:0 -> buffer address ;;
505 ;; Output: EBP = bytes/sector ;;
506 ;; Keep: ESI = cluster ;;
507 ;; EDI = FAT sector or 0 ;;
508 ;; ECX = sector count ;;
509 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
511 %if SectorOf512Bytes != 0
512 lea ebp, [bx+512]
513 %else
514 lea ebp, [bx+1]
515 %endif
517 pushad
519 add eax, [bx(bpbSectorStart)]
520 adc edx, [bx(bpbSectorStart)+4]
522 push edx
523 push eax
524 push es
525 push bx
526 %if SectorOf512Bytes != 0
527 push byte 1 ; sector count word = 1
528 %else
529 push bp ; sector count word = 1
530 %endif
531 push byte 16 ; packet size byte = 16, reserved byte = 0
532 ReadSectorRetry:
533 mov si, sp
534 mov ah, 42h ; ah = 42h = extended read function no.
535 mov dl, [bx(DriveNumber)] ; restore BIOS boot drive number
536 int 13h ; extended read sectors (DL, DS:SI)
538 jnc ReadSuccess
540 %if ReadRetry != 0
541 xor ax, ax
542 int 13h ; reset drive (DL)
543 dec bp
544 %if SectorOf512Bytes != 0
545 jne ReadSectorRetry ; up to 511 tries
546 %else
547 jpe ReadSectorRetry ; up to 3 tries
548 %endif
549 %endif
551 ReadError:
552 call Error
553 db "Read error."
555 ReadSuccess:
556 %if SectorOf512Bytes == 0
557 mov cl, [bx(bpbSectorSizeBits)]
558 shl word [si+16+8], cl ; (e)bp si+16: EDI ESI EBP ESP EBX EDX ECX EAX
559 %endif
560 popa ; sp += 16
561 popad ; real registers
562 ret
564 %if CheckLBAsupport != 0
565 BootFileName
566 %endif
568 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
569 ;; Fill free space with zeroes ;;
570 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
572 times (512-13-($-$$)) db 0
574 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
575 ;; Name of the file to load and run ;;
576 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
578 NameLength equ 11
579 ProgramName times NameLength db 0 ; name and extension
581 ;;;;;;;;;;;;;;;;;;;;;;;;;;
582 ;; End of the sector ID ;;
583 ;;;;;;;;;;;;;;;;;;;;;;;;;;
585 dw 0AA55h ; BIOS checks for this ID