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

Up libav (0.6.6 -> 12.3)
author Pascal Bellard <pascal.bellard@slitaz.org>
date Tue Feb 22 16:15:28 2022 +0000 (2022-02-22)
parents d8c511e24c20
children d1f31f5f6401
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 636KB 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 - 2KB ;;
52 ;; | Boot Sector | ;;
53 ;; +------------------------+ A0000H - 1.5KB ;;
54 ;; | 1.5KB Boot Stack | ;;
55 ;; +------------------------+ A0000H ;;
56 ;; | Video RAM | ;;
57 ;; ;;
58 ;; ;;
59 ;; Boot Image Startup (register values): ;;
60 ;; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ;;
61 ;; dl = BIOS boot drive number (e.g. 80H) ;;
62 ;; cs:ip = program entry point ;;
63 ;; ss:sp = program stack (don't confuse with boot sector's stack) ;;
64 ;; COM program defaults: cs = ds = es = ss = 50h, sp = 0, ip = 100h ;;
65 ;; EXE program defaults: ds = es = EXE data - 10h (fake MS-DOS psp), ;;
66 ;; ax = 0ffffh (both FCB in the PSP don't have a valid drive identifier), ;;
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 ;; ;;
76 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
78 %define bx(label) bx+label-boot
80 [BITS 16]
81 [CPU 386]
83 ImageLoadSeg equ 60h
84 StackSize equ 1536
86 [SECTION .text]
87 [ORG 0]
89 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
90 ;; Boot sector starts here ;;
91 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
93 boot:
94 jmp short start ; Windows checks for this jump
95 nop
96 bsOemName DB "EXFAT " ; 0x03
97 times 53 db 0 ; 0x0B
99 ;;;;;;;;;;;;;;;;;;;;;
100 ;; BPB starts here ;;
101 ;;;;;;;;;;;;;;;;;;;;;
103 bpbSectorStart DQ 0 ; 0x40 partition first sector
104 bpbSectorCount DQ 0 ; 0x48 partition sectors count
105 bpbFatSectorStart DD 0 ; 0x50 FAT first sector
106 bpbFatSectorCount DD 0 ; 0x54 FAT sectors count
107 bpbClusterSectorStart DD 0 ; 0x58 first cluster sector
108 bpbClusterCount DD 0 ; 0x5C total clusters count
109 bpbRootDirCluster DD 0 ; 0x60 first cluster of the root dir
110 bpbVolumeSerial DD 0 ; 0x64 volume serial number
111 bpbFSVersionMinor DB 0 ; 0x68
112 bpbFSVersionMajor DB 0 ; 0x69
113 bpbVolumeStateFlags DW 0 ; 0x6A
114 bpbSectorSizeBits DB 0 ; 0x6C sector size as (1 << n)
115 bpbSectorPerClusterBits DB 0 ; 0x6D sector per cluster as (1 << n)
116 bpbNumberOfFATs DB 0 ; 0x6E always 1
117 bpbDriveNumber DB 0 ; 0x6F alaways 0x80
118 bpbAllocatedPercent DB 0 ; 0x70 percentage of allocated space
120 ;;;;;;;;;;;;;;;;;;;
121 ;; BPB ends here ;;
122 ;;;;;;;;;;;;;;;;;;;
124 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
125 ;; Boot sector code starts here ;;
126 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
128 start:
129 cld
131 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
132 ;; How much RAM is there? ;;
133 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
135 int 12h ; get conventional memory size (in KBs)
136 mov cx, 106h
137 dec ax
138 dec ax ; reserve 2K bytes for the code and the stack
139 shl ax, cl ; and convert it to 16-byte paragraphs
141 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
142 ;; Reserve memory for the boot sector and its stack ;;
143 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
145 mov es, ax ; cs:0 = ds:0 = ss:0 -> top - 512 - StackSize
146 mov ss, ax
147 mov sp, 512+StackSize ; bytes 0-511 are reserved for the boot code
149 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
150 ;; Copy ourselves to top of memory ;;
151 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
153 mov si, 7C00h
154 xor di, di
155 mov ds, di
156 rep movsw ; move 512 bytes (+ 12)
158 ;;;;;;;;;;;;;;;;;;;;;;
159 ;; Jump to the copy ;;
160 ;;;;;;;;;;;;;;;;;;;;;;
162 push es
163 push byte main
164 retf
166 main:
167 push cs
168 pop ds
170 xor ebx, ebx
171 mov [bx], dx ; store BIOS boot drive number
173 mov esi, [bx(bpbRootDirCluster)] ; esi=cluster # of root dir
175 push byte ImageLoadSeg
176 pop es
178 RootDirReadContinue:
179 call ReadCluster ; read one sector of root dir
180 pushf ; save carry="not last sector" flag
182 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
183 ;; Look for the COM/EXE file to load and run ;;
184 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
186 xor di, di ; es:di -> root entries array
188 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
189 ;; Looks for the file/dir ProgramName ;;
190 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
191 ;; Input: ES:DI -> root directory array ;;
192 ;; Output: ESI = cluster number ;;
193 ;; dword [bx+FileSize] file size ;;
194 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
196 CurNameSize equ 3
197 StartCluster equ 14h
198 FileSize equ 18h
200 FindNameCycle:
201 pusha
203 xor ax, ax
204 or al, [es:di]
205 je FindNameFailed
207 cmp al, 0c0h ; EXFAT_ENTRY_FILE_INFO ?
208 jne NotFileInfo
210 mov bl, 30
211 CopyInfo:
212 mov ax, [es:di+bx]
213 mov [bx], ax
214 dec bx
215 dec bx
216 jnz CopyInfo
218 NotFileInfo:
219 mov al, 0c1h ; EXFAT_ENTRY_FILE_NAME ?
220 mov cx, NameLength+1
221 mov si, ProgramName ; ds:si -> program name
222 CheckName:
223 scasw ; compare UTF-16
224 lodsb ; with ASCII
225 loope CheckName
226 je FindNameFound ; cx = 0
227 popa ; restore ax, cx, si, di
229 add di, byte 32
230 cmp di, bp
231 jne FindNameCycle ; next root entry
232 popf ; restore carry="not last sector" flag
233 jc RootDirReadContinue ; continue to the next root dir cluster
234 FindNameFailed: ; end of root directory (dir end reached)
235 call Error
236 db "File not found."
237 FindNameFound:
238 mov esi, [bx+StartCluster]
240 ;;;;;;;;;;;;;;;;;;;;;;;;;;
241 ;; Load the entire file ;;
242 ;;;;;;;;;;;;;;;;;;;;;;;;;;
244 push es
245 xor bp, bp
246 FileReadContinue:
247 shr bp, 4 ; bytes to paragraphs
248 mov di, es
249 add di, bp ; adjust segment for next sector
250 mov es, di ; es:0 updated
251 call ReadCluster ; read one cluster of root dir
252 sub [bx+FileSize], ebp
253 ja FileReadContinue
254 pop bp
256 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
257 ;; Type detection, .COM or .EXE? ;;
258 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
260 mov ds, bp ; bp=ds=seg the file is loaded to
262 add bp, [bx+08h] ; bp = image base
263 mov ax, [bx+06h] ; ax = reloc items
264 mov di, [bx+18h] ; di = reloc table pointer
266 cmp word [bx], 5A4Dh ; "MZ" signature?
267 je RelocateEXE ; yes, it's an EXE program
269 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
270 ;; Setup and run a .COM program ;;
271 ;; Set CS=DS=ES=SS SP=0 IP=100h ;;
272 ;; AX=0ffffh BX=0 CX=0 DX=drive ;;
273 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
275 mov ax, 0ffffh ; both FCB in the PSP don't have a valid drive identifier
276 mov di, 100h ; ip
277 mov bp, ImageLoadSeg-10h ; "org 100h" stuff :)
278 mov ss, bp
279 xor sp, sp
280 push bp ; cs, ds and es
281 jmp short Run
283 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
284 ;; Relocate, setup and run a .EXE program ;;
285 ;; Set CS:IP, SS:SP, DS, ES and AX according ;;
286 ;; to wiki.osdev.org/MZ#Initial_Program_State ;;
287 ;; AX=0ffffh BX=0 CX=0 DX=drive ;;
288 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
290 ReloCycle:
291 add [di+2], bp ; item seg (abs)
292 les si, [di] ; si = item ofs, es = item seg
293 add [es:si], bp ; fixup
294 scasw ; di += 2
295 scasw ; point to next entry
297 RelocateEXE:
298 dec ax ; 32768 max (128KB table)
299 jns ReloCycle ; leave with ax=0ffffh: both FCB in the
300 ; PSP don't have a valid drive identifier
301 les si, [bx+0Eh]
302 add si, bp
303 mov ss, si ; ss for EXE
304 mov sp, es ; sp for EXE
306 lea si, [bp-10h] ; ds and es both point to the segment
307 push si ; containing the PSP structure
309 add bp, [bx+16h] ; cs for EXE
310 mov di, [bx+14h] ; ip for EXE
311 Run:
312 pop ds
313 push bp
314 push di
315 push ds
316 pop es
318 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
319 ;; Set the magic numbers so the program knows that it ;;
320 ;; has been loaded by this bootsector and not by MS-DOS ;;
321 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
322 mov si, 16381 ; prime number 2**14-3
323 mov di, 32749 ; prime number 2**15-19
324 mov bp, 65521 ; prime number 2**16-15
326 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
327 ;; All done, transfer control to the program now ;;
328 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
329 retf
331 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
332 ;; Reads a exFAT cluster ;;
333 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
334 ;; Input: EDX:EAX = LBA ;;
335 ;; EBX = 0 ;;
336 ;; CX = sector cnt ;;
337 ;; ESI = cluster no ;;
338 ;; ES:0 -> buffer adrs ;;
339 ;; Output: EBX = 0 ;;
340 ;; CX = next cnt ;;
341 ;; EBP = bytes/sector;;
342 ;; ES:0 -> next adrs ;;
343 ;; C=0 for last sector ;;
344 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
346 ReadCluster:
347 inc cx ; jcxnz
348 add eax, byte 1
349 loop ReadSectorC
351 mov cl, [bx(bpbSectorSizeBits)]
352 dec cx
353 dec cx
354 mul ebx ; edx:eax = 0
355 inc ax
356 shl eax, cl ; eax=# of exFAT entries per sector
357 lea edi, [esi-2] ; edi=cluster #-2
358 xchg eax, esi
359 div esi ; eax=FAT sector #, edx=entry # in sector
361 imul si, dx, byte 4 ; si=entry # offset in sector
363 cdq
364 add eax, [bx(bpbFatSectorStart)] ; sector # relative to FAT32
365 call ReadSectorC ; read 1 FAT32 sector
367 mov esi, [es:si] ; esi=next cluster #
369 xor eax, eax
370 inc ax
371 mov cl, [bx(bpbSectorPerClusterBits)]
372 shl eax, cl ; 10000h max (32MB cluster)
373 xchg eax, ecx
374 xchg eax, edi ; get cluster #-2
375 mul ecx
377 add eax, [bx(bpbClusterSectorStart)]
378 ReadSectorC:
379 adc edx, ebx
381 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
382 ;; Reads a sector using BIOS Int 13h ;;
383 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
384 ;; Input: EDX:EAX = LBA ;;
385 ;; BX = 0 ;;
386 ;; CX = sector count ;;
387 ;; ES:0 -> buffer address ;;
388 ;; Output: BX = 0 ;;
389 ;; CX = next count ;;
390 ;; EBP = bytes/sector ;;
391 ;; ES:0 -> next address ;;
392 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
394 ReadSector:
396 xor ebp, ebp
397 inc bp
399 pushad
401 add eax, [bx(bpbSectorStart)]
402 adc edx, [bx(bpbSectorStart)+4]
404 push edx
405 push eax
406 push es
407 push bx
408 push bp ; sector count word = 1
409 mov cx, 16
410 push cx ; packet size byte = 16, reserved byte = 0
411 ReadSectorRetry:
412 mov si, sp
413 mov ah, 42h ; ah = 42h = extended read function no.
414 mov dl, [bx]
415 int 13h ; extended read sectors (DL, DS:SI)
417 jnc ReadSuccess
419 xor ax, ax
420 int 13h ; reset drive (DL)
421 loop ReadSectorRetry
423 call Error
424 db "Read error."
426 ReadSuccess:
427 mov cl, [bx(bpbSectorSizeBits)]
428 shl word [si+16+8], cl ; (e)bp si+16: EDI ESI EBP ESP EBX EDX ECX EAX
429 popa ; sp += 16
430 popad ; real registers
432 stc
433 loop ReadSectorNext
435 cmp esi, byte -10 ; carry=0 if last cluster, and carry=1 otherwise
436 ReadSectorNext:
437 mov dl, [bx] ; restore BIOS boot drive number
438 ret
440 ;;;;;;;;;;;;;;;;;;;;;;;;;;
441 ;; Error Messaging Code ;;
442 ;;;;;;;;;;;;;;;;;;;;;;;;;;
444 Error:
445 pop si
447 PutStr:
448 mov ah, 0Eh
449 mov bl, 7
450 lodsb
451 int 10h
452 cmp al, "."
453 jne PutStr
455 cbw
456 int 16h ; wait for a key...
457 int 19h ; bootstrap
459 Stop:
460 hlt
461 jmp short Stop
463 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
464 ;; Fill free space with zeroes ;;
465 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
467 times (512-13-($-$$)) db 0
469 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
470 ;; Name of the file to load and run ;;
471 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
473 ProgramName db "startup.bin" ; name and extension
474 NameLength equ $-ProgramName
476 ;;;;;;;;;;;;;;;;;;;;;;;;;;
477 ;; End of the sector ID ;;
478 ;;;;;;;;;;;;;;;;;;;;;;;;;;
480 dw 0AA55h ; BIOS checks for this ID