wok view BootProg/stuff/bootex.asm @ rev 25699
Up redis (7.2.5)
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Sun May 26 16:05:16 2024 +0000 (13 months ago) |
parents | 82a123e54615 |
children | d2f587b805fb |
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 - 2 ; exclude DriveNumber
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, 7C02h ; exclude DriveNumber
173 mov ds, di
174 %endif
175 push es
176 cld
177 xchg ax, dx
178 stosw ; store BIOS boot drive number
179 %if AnyWhere
180 rep cs movsw ; move 512 bytes (+ 12)
181 %else
182 rep movsw ; move 512 bytes (+ 12)
183 %endif
185 ;;;;;;;;;;;;;;;;;;;;;;
186 ;; Jump to the copy ;;
187 ;;;;;;;;;;;;;;;;;;;;;;
189 %if AnyWhere
190 push byte main
191 retf
192 %endm
193 %else
194 push word main
195 retf
196 %endif
198 %if CheckLBAsupport != 0
199 %macro BootFileName 0
200 main:
201 push cs
202 pop ds
203 mov ah, 41h ; clobbers AX, BX, CX, DH
204 mov bx, 55AAh
205 int 13h
206 jc ReadError
207 ; xor bx, 0AA55h
208 ; jnz ReadError
209 ; shr cx, 1 ; function 42h support ?
210 ; jnc ReadError
211 xor cx, cx
212 %else
213 main:
214 push cs
215 pop ds
216 %endif
217 xor ebx, ebx
219 mov esi, [bx(bpbRootDirCluster)] ; esi=cluster # of root dir
221 push byte ImageLoadSeg
222 pop es ; cx = 0
224 RootDirReadContinue:
225 call ReadCluster ; read one sector of root dir
227 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
228 ;; Look for the COM/EXE file to load and run ;;
229 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
231 ; es:di -> root entries array
233 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
234 ;; Looks for the file/dir ProgramName ;;
235 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
236 ;; Input: ES:DI -> root directory array ;;
237 ;; Output: ESI = cluster number ;;
238 ;; dword [bx+FileSize] file size ;;
239 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
241 CurNameSize equ 03h ; 1 byte
242 StartCluster equ 14h ; 4 bytes
243 FileSize equ 18h ; 8 bytes
245 FindNameCycle:
246 pusha
248 %if NullEntryCheck != 0
249 xor ax, ax
250 or al, [es:di]
251 je FindNameFailed
252 %define curtag al
253 %define chktag al
254 %else
255 %define curtag byte [es:di]
256 %define chktag ax
257 %endif
258 cmp curtag, 0c0h ; EXFAT_ENTRY_FILE_INFO ?
259 jne NotFileInfo
261 mov bl, 32
262 CopyInfo:
263 mov [bx], al
264 dec bx
265 mov al, [es:di+bx]
266 jnz CopyInfo ; keep BIOS boot drive number
268 NotFileInfo:
269 %if CheckAttrib != 0
270 Attributes equ 0Bh ; 1 byte
271 cmp curtag, 85h ; EXFAT_ENTRY_FILE ?
272 jne NotEntryFile
273 mov al, [es:di+Attributes]
274 mov [ProgramName+NameLength+1], al
275 NotEntryFile:
276 %endif
277 mov chktag, 0c1h ; EXFAT_ENTRY_FILE_NAME ?
278 mov cx, NameLength+1
279 mov si, ProgramName ; ds:si -> program name
280 CheckName:
281 scasw ; compare UTF-16
282 lodsb ; with ASCII
283 loope CheckName
284 VolumeLabel equ 8
285 SubDirectory equ 10h
286 %if CheckAttrib != 0
287 jnz SkipFindName
288 test byte [si], VolumeLabel+SubDirectory
289 SkipFindName:
290 %endif
291 je FindNameFound ; cx = 0
292 popa ; restore ax, cx, si, di
294 add di, byte 32
295 cmp di, bp
296 jne FindNameCycle ; next root entry
297 loop RootDirReadContinue ; continue to the next root dir sector
298 cmp esi, byte -10 ; carry=0 if last cluster, and carry=1 otherwise
299 jc RootDirReadContinue ; continue to the next root dir cluster
300 FindNameFailed: ; end of root directory (dir end reached)
301 mov dl, [bx(DriveNumber)] ; restore BIOS boot drive number
302 call Error
303 db "File not found."
304 FindNameFound:
305 mov esi, [bx+StartCluster]
307 ;;;;;;;;;;;;;;;;;;;;;;;;;;
308 ;; Load the entire file ;;
309 ;; Input: ESI = cluster ;;
310 ;; CX = 0 ;;
311 ;;;;;;;;;;;;;;;;;;;;;;;;;;
313 push es
314 %if SectorOf512Bytes == 0
315 xor bp, bp
316 FileReadContinue:
317 shr bp, 4 ; bytes to paragraphs
318 mov di, es
319 add di, bp ; adjust segment for next sector
320 mov es, di ; es:0 updated
321 %else
322 FileReadContinue:
323 %endif
324 call ReadCluster ; read one more sector of the boot file
325 dec cx
326 sub [bx+FileSize], ebp ; max FileSize is < 640KB : check low 32 bits only
327 %if SectorOf512Bytes != 0
328 mov bp, es
329 lea bp, [bp+32]
330 mov es, bp ; es:0 updated
331 %endif
332 ja FileReadContinue
333 mov dx, [bx(DriveNumber)] ; restore BIOS boot drive number
334 xchg ax, di
335 pop bp
337 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
338 ;; Type detection, .COM or .EXE? ;;
339 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
341 mov ds, bp ; bp=ds=seg the file is loaded to
343 add bp, [bx+08h] ; bp = image base
344 mov di, [bx+18h] ; di = reloc table pointer
346 cmp word [bx], 5A4Dh ; "MZ" signature?
347 je RelocateEXE ; yes, it's an EXE program
349 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
350 ;; Setup and run a .COM program ;;
351 ;; Set CS=DS=ES=SS SP=0 IP=100h ;;
352 ;; AX=0ffffh BX=0 DX=drive and ;;
353 ;; cmdline=void ;;
354 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
356 mov di, 100h ; ip
357 mov bp, ImageLoadSeg-10h ; "org 100h" stuff :)
358 mov ss, bp
359 xor sp, sp
360 push bp ; cs, ds and es
361 jmp short Run
363 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
364 ;; Relocate, setup and run a .EXE program ;;
365 ;; Set CS:IP, SS:SP, DS, ES and AX according ;;
366 ;; to wiki.osdev.org/MZ#Initial_Program_State ;;
367 ;; AX=0ffffh BX=0 DX=drive cmdline=void ;;
368 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
370 ReloCycle:
371 add [di+2], bp ; item seg (abs)
372 les si, [di] ; si = item ofs, es = item seg
373 add [es:si], bp ; fixup
374 scasw ; di += 2
375 scasw ; point to next entry
377 RelocateEXE:
378 dec word [bx+06h] ; reloc items, 32768 max (128KB table)
379 jns ReloCycle
381 les si, [bx+0Eh]
382 add si, bp
383 mov ss, si ; ss for EXE
384 mov sp, es ; sp for EXE
386 lea si, [bp-10h] ; ds and es both point to the segment
387 push si ; containing the PSP structure
389 add bp, [bx+16h] ; cs for EXE
390 mov di, [bx+14h] ; ip for EXE
391 Run:
392 pop ds
393 push bp
394 push di
395 push ds
396 pop es
397 mov [80h], ax ; clear cmdline
398 dec ax ; both FCB in the PSP don't have a valid drive identifier
400 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
401 ;; Set the magic numbers so the program knows that it ;;
402 ;; has been loaded by this bootsector and not by MS-DOS ;;
403 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
404 mov si, 16381 ; prime number 2**14-3
405 mov di, 32749 ; prime number 2**15-19
406 mov bp, 65521 ; prime number 2**16-15
408 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
409 ;; All done, transfer control to the program now ;;
410 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
411 retf
412 %if CheckLBAsupport != 0
413 %endm
414 %endif
416 %if AnyWhere
417 BootAnyWhere
418 %endif
420 ;;;;;;;;;;;;;;;;;;;;;;;;;;
421 ;; Error Messaging Code ;;
422 ;;;;;;;;;;;;;;;;;;;;;;;;;;
424 Error:
425 pop si
427 PutStr:
428 mov ah, 0Eh
429 mov bl, 7
430 lodsb
431 int 10h
432 cmp al, "."
433 jne PutStr
434 %if WaitForKey != 0
435 cbw
436 int 16h ; wait for a key...
437 int 19h ; bootstrap
438 %endif
439 Stop:
440 hlt
441 jmp short Stop
443 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
444 ;; Reads a exFAT cluster ;;
445 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
446 ;; Input: EDX:EAX = LBA ;;
447 ;; EBX = 0 ;;
448 ;; CX = sector cnt ;;
449 ;; ESI = cluster no ;;
450 ;; ES:0 -> buffer adrs ;;
451 ;; Output: EDX:EAX = next LBA ;;
452 ;; CX = sector cnt ;;
453 ;; ESI = cluster no ;;
454 ;; EBP = bytes/sector;;
455 ;; Keep: EDI = 0 ;;
456 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
458 ReadCluster:
459 add eax, byte 1
461 inc cx ; jcxnz
462 loop ReadSectorC
464 mul ebx ; edx:eax = 0
465 %if SectorOf512Bytes != 0
466 mov al, 128
467 %define SectorPerClusterBits [bx(bpbSectorPerClusterBits)]
468 %else
469 mov ah, 40h
470 mov cx, [bx(bpbSectorSizeBits)]
471 %define SectorPerClusterBits ch
472 rol ax, cl ; eax=# of exFAT entries per sector
473 %endif
474 lea edi, [esi-2] ; edi=cluster #-2
475 xchg eax, esi
476 div esi ; eax=FAT sector #, edx=entry # in sector
478 imul si, dx, byte 4 ; si=entry # offset in sector
480 cdq
481 add eax, [bx(bpbFatSectorStart)] ; sector # relative to exFAT
482 %if TfatSupport
483 test byte [bx(bpbVolumeStateFlags)], 1
484 jz UseFat0
485 add eax, [bx(bpbFatSectorCount)]
486 UseFat0:
487 %endif
488 call ReadSectorFAT ; read 1 exFAT sector, keep edx=0, set C
490 mov esi, [es:si] ; esi=next cluster #
492 mov dl, SectorPerClusterBits
493 xor ecx, ecx
494 bts ecx, edx ; 10000h max (32MB cluster)
495 xchg eax, edi ; get cluster #-2
496 mul ecx
498 add eax, [bx(bpbClusterSectorStart)]
499 ReadSectorC:
500 mov di, bx
501 ReadSectorFAT:
502 adc edx, ebx
504 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
505 ;; Reads a sector using BIOS Int 13h ;;
506 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
507 ;; Input: EDX:EAX = LBA ;;
508 ;; EBX = 0 ;;
509 ;; ES:0 -> buffer address ;;
510 ;; Output: EBP = bytes/sector ;;
511 ;; Keep: ESI = cluster ;;
512 ;; EDI = FAT sector or 0 ;;
513 ;; ECX = sector count ;;
514 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
516 %if SectorOf512Bytes != 0
517 lea ebp, [bx+512]
518 %else
519 lea ebp, [bx+1]
520 %endif
522 pushad
524 add eax, [bx(bpbSectorStart)]
525 adc edx, [bx(bpbSectorStart)+4]
527 push edx
528 push eax
529 push es
530 push bx
531 %if SectorOf512Bytes != 0
532 push byte 1 ; sector count word = 1
533 %else
534 push bp ; sector count word = 1
535 %endif
536 push byte 16 ; packet size byte = 16, reserved byte = 0
537 ReadSectorRetry:
538 mov si, sp
539 mov ah, 42h ; ah = 42h = extended read function no.
540 mov dl, [bx(DriveNumber)] ; restore BIOS boot drive number
541 int 13h ; extended read sectors (DL, DS:SI)
543 jnc ReadSuccess
545 %if ReadRetry != 0
546 xor ax, ax
547 int 13h ; reset drive (DL)
548 dec bp
549 %if SectorOf512Bytes != 0
550 jne ReadSectorRetry ; up to 511 tries
551 %else
552 jpe ReadSectorRetry ; up to 3 tries
553 %endif
554 %endif
556 ReadError:
557 call Error
558 db "Read error."
560 ReadSuccess:
561 %if SectorOf512Bytes == 0
562 mov cl, [bx(bpbSectorSizeBits)]
563 shl word [si+16+8], cl ; (e)bp si+16: EDI ESI EBP ESP EBX EDX ECX EAX
564 %endif
565 popa ; sp += 16
566 popad ; real registers
567 ret
569 %if CheckLBAsupport != 0
570 BootFileName
571 %endif
573 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
574 ;; Fill free space with zeroes ;;
575 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
577 times (512-13-($-$$)) db 0
579 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
580 ;; Name of the file to load and run ;;
581 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
583 NameLength equ 11
584 ProgramName times NameLength db 0 ; name and extension
586 ;;;;;;;;;;;;;;;;;;;;;;;;;;
587 ;; End of the sector ID ;;
588 ;;;;;;;;;;;;;;;;;;;;;;;;;;
590 dw 0AA55h ; BIOS checks for this ID