wok view BootProg/stuff/bootex.asm @ rev 25469

Update some web_site
author Pascal Bellard <pascal.bellard@slitaz.org>
date Fri Oct 07 09:15:33 2022 +0000 (19 months ago)
parents 4c101652eb90
children f40d97a52c42
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 bytes
82 ReadRetry equ 1 ; +8 bytes
83 SectorOf512Bytes equ 1 ; -13 bytes
84 CheckAttrib equ 0 ; +19 bytes
86 [BITS 16]
87 [CPU 386]
89 ImageLoadSeg equ 60h
90 StackSize equ 512
92 [SECTION .text]
93 [ORG 0]
95 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
96 ;; Boot sector starts here ;;
97 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
99 boot:
100 DriveNumber:
101 jmp short start ; Windows checks for this jump
102 nop
103 bsOemName times 8 db " " ; 0x03 "EXFAT "
104 times 53 db 0 ; 0x0B
106 ;;;;;;;;;;;;;;;;;;;;;
107 ;; BPB starts here ;;
108 ;;;;;;;;;;;;;;;;;;;;;
110 bpbSectorStart DQ 0 ; 0x40 partition first sector
111 bpbSectorCount DQ 0 ; 0x48 partition sectors count
112 bpbFatSectorStart DD 0 ; 0x50 FAT first sector
113 bpbFatSectorCount DD 0 ; 0x54 FAT sectors count
114 bpbClusterSectorStart DD 0 ; 0x58 first cluster sector
115 bpbClusterCount DD 0 ; 0x5C total clusters count
116 bpbRootDirCluster DD 0 ; 0x60 first cluster of the root dir
117 bpbVolumeSerial DD 0 ; 0x64 volume serial number
118 bpbFSVersionMinor DB 0 ; 0x68
119 bpbFSVersionMajor DB 0 ; 0x69
120 bpbVolumeStateFlags DW 0 ; 0x6A
121 bpbSectorSizeBits DB 0 ; 0x6C sector size as (1 << n)
122 bpbSectorPerClusterBits DB 0 ; 0x6D sector per cluster as (1 << n)
123 bpbNumberOfFATs DB 0 ; 0x6E always 1
124 bpbDriveNumber DB 0 ; 0x6F alaways 0x80
125 bpbAllocatedPercent DB 0 ; 0x70 percentage of allocated space
127 ;;;;;;;;;;;;;;;;;;;
128 ;; BPB ends here ;;
129 ;;;;;;;;;;;;;;;;;;;
131 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
132 ;; Boot sector code starts here ;;
133 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
135 start:
136 cld
138 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
139 ;; How much RAM is there? ;;
140 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
142 int 12h ; get conventional memory size (in KBs)
143 mov cx, 106h
144 dec ax ; reserve 1K bytes for the code and the stack
145 shl ax, cl ; and convert it to 16-byte paragraphs
147 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
148 ;; Reserve memory for the boot sector and its stack ;;
149 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
151 mov es, ax ; cs:0 = ds:0 = ss:0 -> top - 512 - StackSize
152 mov ss, ax
153 mov sp, 512+StackSize ; bytes 0-511 are reserved for the boot code
155 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
156 ;; Copy ourselves to top of memory ;;
157 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
159 mov si, 7C00h
160 xor di, di
161 mov ds, di
162 push es
163 mov [si(DriveNumber)], dx ; store BIOS boot drive number
164 rep movsw ; move 512 bytes (+ 12)
166 ;;;;;;;;;;;;;;;;;;;;;;
167 ;; Jump to the copy ;;
168 ;;;;;;;;;;;;;;;;;;;;;;
170 push word main
171 retf
173 main:
174 push cs
175 pop ds
177 xor ebx, ebx
179 mov esi, [bx(bpbRootDirCluster)] ; esi=cluster # of root dir
181 push byte ImageLoadSeg
182 pop es ; cx = 0
184 RootDirReadContinue:
185 call ReadCluster ; read one sector of root dir
187 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
188 ;; Look for the COM/EXE file to load and run ;;
189 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
191 ; es:di -> root entries array
193 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
194 ;; Looks for the file/dir ProgramName ;;
195 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
196 ;; Input: ES:DI -> root directory array ;;
197 ;; Output: ESI = cluster number ;;
198 ;; dword [bx+FileSize] file size ;;
199 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
201 CurNameSize equ 03h ; 1 byte
202 StartCluster equ 14h ; 4 bytes
203 FileSize equ 18h ; 8 bytes
205 FindNameCycle:
206 pusha
208 %if CheckAttrib != 0
209 Attributes equ 0Bh ; 1 byte
210 cmp byte [es:di], 85h ; EXFAT_ENTRY_FILE ?
211 jne NotEntryFile
212 mov al, [es:di+Attributes]
213 mov [bx+32], al
214 NotEntryFile:
215 %endif
216 %if NullEntryCheck != 0
217 xor ax, ax
218 or al, [es:di]
219 je FindNameFailed
220 cmp al, 0c0h ; EXFAT_ENTRY_FILE_INFO ?
221 %else
222 cmp byte [es:di], 0c0h ; EXFAT_ENTRY_FILE_INFO ?
223 %endif
224 jne NotFileInfo
226 mov bl, 31
227 CopyInfo:
228 mov al, [es:di+bx]
229 mov [bx], al
230 dec bx
231 jnz CopyInfo ; keep BIOS boot drive number
233 NotFileInfo:
234 %if NullEntryCheck != 0
235 mov al, 0c1h ; EXFAT_ENTRY_FILE_NAME ?
236 %else
237 mov ax, 0c1h ; EXFAT_ENTRY_FILE_NAME ?
238 %endif
239 mov cx, NameLength+1
240 mov si, ProgramName ; ds:si -> program name
241 CheckName:
242 scasw ; compare UTF-16
243 lodsb ; with ASCII
244 loope CheckName
245 %if CheckAttrib != 0
246 VolumeLabel equ 8
247 SubDirectory equ 10h
248 jnz SkipFindName
249 test byte [bx+32], VolumeLabel+SubDirectory
250 SkipFindName:
251 %endif
252 je FindNameFound ; cx = 0
253 popa ; restore ax, cx, si, di
255 add di, byte 32
256 cmp di, bp
257 jne FindNameCycle ; next root entry
258 loop RootDirReadContinue ; continue to the next root dir sector
259 cmp esi, byte -10 ; carry=0 if last cluster, and carry=1 otherwise
260 jc RootDirReadContinue ; continue to the next root dir cluster
261 FindNameFailed: ; end of root directory (dir end reached)
262 mov dl, [bx(DriveNumber)] ; restore BIOS boot drive number
263 call Error
264 db "File not found."
265 FindNameFound:
266 mov esi, [bx+StartCluster]
268 ;;;;;;;;;;;;;;;;;;;;;;;;;;
269 ;; Load the entire file ;;
270 ;;;;;;;;;;;;;;;;;;;;;;;;;;
272 push es
273 %if SectorOf512Bytes == 0
274 xor bp, bp
275 FileReadContinue:
276 shr bp, 4 ; bytes to paragraphs
277 mov di, es
278 add di, bp ; adjust segment for next sector
279 mov es, di ; es:0 updated
280 %else
281 FileReadContinue:
282 %endif
283 call ReadCluster ; read one more sector of the boot file
284 dec cx
285 sub [bx+FileSize], ebp ; max FileSize is < 640KB : check low 32 bits only
286 %if SectorOf512Bytes != 0
287 mov bp, es
288 lea bp, [bp+32]
289 mov es, bp ; es:0 updated
290 %endif
291 ja FileReadContinue
292 mov dx, [bx(DriveNumber)] ; restore BIOS boot drive number
293 xchg ax, di
294 pop bp
296 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
297 ;; Type detection, .COM or .EXE? ;;
298 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
300 mov ds, bp ; bp=ds=seg the file is loaded to
302 add bp, [bx+08h] ; bp = image base
303 mov di, [bx+18h] ; di = reloc table pointer
305 cmp word [bx], 5A4Dh ; "MZ" signature?
306 je RelocateEXE ; yes, it's an EXE program
308 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
309 ;; Setup and run a .COM program ;;
310 ;; Set CS=DS=ES=SS SP=0 IP=100h ;;
311 ;; AX=0ffffh BX=0 DX=drive and ;;
312 ;; cmdline=void ;;
313 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
315 mov di, 100h ; ip
316 mov bp, ImageLoadSeg-10h ; "org 100h" stuff :)
317 mov ss, bp
318 xor sp, sp
319 push bp ; cs, ds and es
320 jmp short Run
322 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
323 ;; Relocate, setup and run a .EXE program ;;
324 ;; Set CS:IP, SS:SP, DS, ES and AX according ;;
325 ;; to wiki.osdev.org/MZ#Initial_Program_State ;;
326 ;; AX=0ffffh BX=0 DX=drive cmdline=void ;;
327 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
329 ReloCycle:
330 add [di+2], bp ; item seg (abs)
331 les si, [di] ; si = item ofs, es = item seg
332 add [es:si], bp ; fixup
333 scasw ; di += 2
334 scasw ; point to next entry
336 RelocateEXE:
337 dec word [bx+06h] ; reloc items, 32768 max (128KB table)
338 jns ReloCycle
340 les si, [bx+0Eh]
341 add si, bp
342 mov ss, si ; ss for EXE
343 mov sp, es ; sp for EXE
345 lea si, [bp-10h] ; ds and es both point to the segment
346 push si ; containing the PSP structure
348 add bp, [bx+16h] ; cs for EXE
349 mov di, [bx+14h] ; ip for EXE
350 Run:
351 pop ds
352 push bp
353 push di
354 push ds
355 pop es
356 mov [80h], ax ; clear cmdline
357 dec ax ; both FCB in the PSP don't have a valid drive identifier
359 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
360 ;; Set the magic numbers so the program knows that it ;;
361 ;; has been loaded by this bootsector and not by MS-DOS ;;
362 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
363 mov si, 16381 ; prime number 2**14-3
364 mov di, 32749 ; prime number 2**15-19
365 mov bp, 65521 ; prime number 2**16-15
367 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
368 ;; All done, transfer control to the program now ;;
369 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
370 retf
372 ;;;;;;;;;;;;;;;;;;;;;;;;;;
373 ;; Error Messaging Code ;;
374 ;;;;;;;;;;;;;;;;;;;;;;;;;;
376 Error:
377 pop si
379 PutStr:
380 mov ah, 0Eh
381 mov bl, 7
382 lodsb
383 int 10h
384 cmp al, "."
385 jne PutStr
387 cbw
388 int 16h ; wait for a key...
389 int 19h ; bootstrap
391 Stop:
392 hlt
393 jmp short Stop
395 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
396 ;; Reads a exFAT cluster ;;
397 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
398 ;; Input: EDX:EAX = LBA ;;
399 ;; EBX = 0 ;;
400 ;; CX = sector cnt ;;
401 ;; ESI = cluster no ;;
402 ;; ES:0 -> buffer adrs ;;
403 ;; Output: EBX = 0 ;;
404 ;; CX = next cnt ;;
405 ;; EBP = bytes/sector;;
406 ;; ES:0 -> next adrs ;;
407 ;; C=0 for last sector ;;
408 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
410 ReadCluster:
411 add eax, byte 1
413 inc cx ; jcxnz
414 loop ReadSectorC
416 mul ebx ; edx:eax = 0
417 %if SectorOf512Bytes != 0
418 mov al, 128
419 %else
420 mov cl, -2
421 add cl, [bx(bpbSectorSizeBits)]
422 bts ax, cx ; eax=# of exFAT entries per sector
423 %endif
424 lea edi, [esi-2] ; edi=cluster #-2
425 xchg eax, esi
426 div esi ; eax=FAT sector #, edx=entry # in sector
428 imul si, dx, byte 4 ; si=entry # offset in sector
430 cdq
431 add eax, [bx(bpbFatSectorStart)] ; sector # relative to exFAT
432 call ReadSectorFAT ; read 1 exFAT sector, keep edx=0, set C
434 mov esi, [es:si] ; esi=next cluster #
436 mov dl, [bx(bpbSectorPerClusterBits)]
437 xor ecx, ecx
438 bts ecx, edx ; 10000h max (32MB cluster)
439 xchg eax, edi ; get cluster #-2
440 mul ecx
442 add eax, [bx(bpbClusterSectorStart)]
443 ReadSectorC:
444 mov di, bx
445 ReadSectorFAT:
446 adc edx, ebx
448 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
449 ;; Reads a sector using BIOS Int 13h ;;
450 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
451 ;; Input: EDX:EAX = LBA ;;
452 ;; BX = 0 ;;
453 ;; CX = sector count ;;
454 ;; ES:0 -> buffer address ;;
455 ;; Output: BX = 0 ;;
456 ;; CX = next count ;;
457 ;; EBP = bytes/sector ;;
458 ;; ES:0 -> next address ;;
459 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
461 %if SectorOf512Bytes != 0
462 lea ebp, [bx+512]
463 %else
464 lea ebp, [bx+1]
465 %endif
467 pushad
469 add eax, [bx(bpbSectorStart)]
470 adc edx, [bx(bpbSectorStart)+4]
472 push edx
473 push eax
474 push es
475 push bx
476 %if SectorOf512Bytes != 0
477 push byte 1 ; sector count word = 1
478 %else
479 push bp ; sector count word = 1
480 %endif
481 push byte 16 ; packet size byte = 16, reserved byte = 0
482 ReadSectorRetry:
483 mov si, sp
484 mov ah, 42h ; ah = 42h = extended read function no.
485 mov dl, [bx(DriveNumber)] ; restore BIOS boot drive number
486 int 13h ; extended read sectors (DL, DS:SI)
488 jnc ReadSuccess
490 %if ReadRetry != 0
491 xor ax, ax
492 int 13h ; reset drive (DL)
493 dec bp
494 %if SectorOf512Bytes != 0
495 jne ReadSectorRetry ; up to 511 tries
496 %else
497 jpe ReadSectorRetry ; up to 3 tries
498 %endif
499 %endif
501 call Error
502 db "Read error."
504 ReadSuccess:
505 %if SectorOf512Bytes == 0
506 mov cl, [bx(bpbSectorSizeBits)]
507 shl word [si+16+8], cl ; (e)bp si+16: EDI ESI EBP ESP EBX EDX ECX EAX
508 %endif
509 popa ; sp += 16
510 popad ; real registers
511 ret
513 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
514 ;; Fill free space with zeroes ;;
515 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
517 times (512-13-($-$$)) db 0
519 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
520 ;; Name of the file to load and run ;;
521 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
523 NameLength equ 11
524 ProgramName times NameLength db 0 ; name and extension
526 ;;;;;;;;;;;;;;;;;;;;;;;;;;
527 ;; End of the sector ID ;;
528 ;;;;;;;;;;;;;;;;;;;;;;;;;;
530 dw 0AA55h ; BIOS checks for this ID