wok-6.x view BootProg/stuff/bootex.asm @ rev 24479

BootProg: nasm 2.15.05 support
author Pascal Bellard <pascal.bellard@slitaz.org>
date Fri Feb 18 11:32:41 2022 +0000 (2022-02-18)
parents 04472f031354
children d8c511e24c20
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 Limitations: ;;
28 ;; ~~~~~~~~~~~~~~~~~~ ;;
29 ;; - Works only on the 1st MBR partition which must be a PRI DOS partition ;;
30 ;; with exFAT (File System ID: 07h) ;;
31 ;; ;;
32 ;; ;;
33 ;; Known Bugs: ;;
34 ;; ~~~~~~~~~~~ ;;
35 ;; - All bugs are fixed as far as I know. The boot sector has been tested ;;
36 ;; on a 128MB qemu image. ;;
37 ;; ;;
38 ;; ;;
39 ;; Memory Layout: ;;
40 ;; ~~~~~~~~~~~~~~ ;;
41 ;; The diagram below shows the typical memory layout. The actual location ;;
42 ;; of the boot sector and its stack may be lower than A0000H if the BIOS ;;
43 ;; reserves memory for its Extended BIOS Data Area just below A0000H and ;;
44 ;; reports less than 640 KB of RAM via its Int 12H function. ;;
45 ;; ;;
46 ;; physical address ;;
47 ;; +------------------------+ 00000H ;;
48 ;; | Interrupt Vector Table | ;;
49 ;; +------------------------+ 00400H ;;
50 ;; | BIOS Data Area | ;;
51 ;; +------------------------+ 00500H ;;
52 ;; | PrtScr Status / Unused | ;;
53 ;; +------------------------+ 00600H ;;
54 ;; | Loaded Image | ;;
55 ;; +------------------------+ nnnnnH ;;
56 ;; | Available Memory | ;;
57 ;; +------------------------+ A0000H - 2KB ;;
58 ;; | Boot Sector | ;;
59 ;; +------------------------+ A0000H - 1.5KB ;;
60 ;; | 1.5KB Boot Stack | ;;
61 ;; +------------------------+ A0000H ;;
62 ;; | Video RAM | ;;
63 ;; ;;
64 ;; ;;
65 ;; Boot Image Startup (register values): ;;
66 ;; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ;;
67 ;; dl = BIOS boot drive number (e.g. 80H) ;;
68 ;; cs:ip = program entry point ;;
69 ;; ss:sp = program stack (don't confuse with boot sector's stack) ;;
70 ;; COM program defaults: cs = ds = es = ss = 50h, sp = 0, ip = 100h ;;
71 ;; EXE program defaults: ds = es = EXE data - 10h (fake MS-DOS psp), ;;
72 ;; ax = 0ffffh (both FCB in the PSP don't have a valid drive identifier), ;;
73 ;; cs:ip and ss:sp depends on EXE header ;;
74 ;; Magic numbers: ;;
75 ;; si = 16381 (prime number 2**14-3) ;;
76 ;; di = 32749 (prime number 2**15-19) ;;
77 ;; bp = 65521 (prime number 2**16-15) ;;
78 ;; The magic numbers let the program know whether it has been loaded by ;;
79 ;; this boot sector or by MS-DOS, which may be handy for universal, bare- ;;
80 ;; metal and MS-DOS programs. ;;
81 ;; ;;
82 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
84 %define bx(label) bx+label-boot
86 [BITS 16]
87 [CPU 386]
89 ImageLoadSeg equ 60h
90 StackSize equ 1536
92 [SECTION .text]
93 [ORG 0]
95 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
96 ;; Boot sector starts here ;;
97 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
99 boot:
100 jmp short start ; Windows checks for this jump
101 nop
102 bsOemName DB "EXFAT " ; 0x03
103 times 53 db 0 ; 0x0B
105 ;;;;;;;;;;;;;;;;;;;;;
106 ;; BPB starts here ;;
107 ;;;;;;;;;;;;;;;;;;;;;
109 bpbSectorStart DQ 0 ; 0x40 partition first sector
110 bpbSectorCount DQ 0 ; 0x48 partition sectors count
111 bpbFatSectorStart DD 0 ; 0x50 FAT first sector
112 bpbFatSectorCount DD 0 ; 0x54 FAT sectors count
113 bpbClusterSectorStart DD 0 ; 0x58 first cluster sector
114 bpbClusterCount DD 0 ; 0x5C total clusters count
115 bpbRootDirCluster DD 0 ; 0x60 first cluster of the root dir
116 bpbVolumeSerial DD 0 ; 0x64 volume serial number
117 bpbFSVersionMinor DB 0 ; 0x68
118 bpbFSVersionMajor DB 0 ; 0x69
119 bpbVolumeStateFlags DW 0 ; 0x6A
120 bpbSectorSizeBits DB 0 ; 0x6C sector size as (1 << n)
121 bpbSectorPerClusterBits DB 0 ; 0x6D sector per cluster as (1 << n)
122 bpbNumberOfFATs DB 0 ; 0x6E always 1
123 bpbDriveNumber DB 0 ; 0x6F alaways 0x80
124 bpbAllocatedPercent DB 0 ; 0x70 percentage of allocated space
126 ;;;;;;;;;;;;;;;;;;;
127 ;; BPB ends here ;;
128 ;;;;;;;;;;;;;;;;;;;
130 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
131 ;; Boot sector code starts here ;;
132 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
134 start:
135 cld
137 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
138 ;; How much RAM is there? ;;
139 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
141 int 12h ; get conventional memory size (in KBs)
142 mov cx, 106h
143 dec ax
144 dec ax ; reserve 2K 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 rep movsw ; move 512 bytes (+ 12)
164 ;;;;;;;;;;;;;;;;;;;;;;
165 ;; Jump to the copy ;;
166 ;;;;;;;;;;;;;;;;;;;;;;
168 push es
169 push byte main
170 retf
172 main:
173 push cs
174 pop ds
176 xor ebx, ebx
177 mov [bx], dx ; store BIOS boot drive number
179 mov esi, [bx(bpbRootDirCluster)] ; esi=cluster # of root dir
181 push byte ImageLoadSeg
182 pop es
184 RootDirReadContinue:
185 call ReadCluster ; read one sector of root dir
186 pushf ; save carry="not last sector" flag
188 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
189 ;; Look for the COM/EXE file to load and run ;;
190 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
192 xor di, di ; es:di -> root entries array
194 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
195 ;; Looks for the file/dir ProgramName ;;
196 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
197 ;; Input: ES:DI -> root directory array ;;
198 ;; Output: ESI = cluster number ;;
199 ;; dword [bx+FileSize] file size ;;
200 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
202 CurNameSize equ 3
203 StartCluster equ 14h
204 FileSize equ 18h
206 FindNameCycle:
207 pusha
209 xor ax, ax
210 or al, [es:di]
211 je FindNameFailed
213 cmp al, 0c0h ; EXFAT_ENTRY_FILE_INFO ?
214 jne NotFileInfo
216 mov bl, 30
217 CopyInfo:
218 mov ax, [es:di+bx]
219 mov [bx], ax
220 dec bx
221 dec bx
222 jnz CopyInfo
224 NotFileInfo:
225 mov al, 0c1h ; EXFAT_ENTRY_FILE_NAME ?
226 mov cx, NameLength+1
227 mov si, ProgramName ; ds:si -> program name
228 CheckName:
229 scasw ; compare UTF-16
230 lodsb ; with ASCII
231 loope CheckName
232 je FindNameFound ; cx = 0
233 popa ; restore ax, cx, si, di
235 add di, byte 32
236 cmp di, bp
237 jne FindNameCycle ; next root entry
238 popf ; restore carry="not last sector" flag
239 jc RootDirReadContinue ; continue to the next root dir cluster
240 FindNameFailed: ; end of root directory (dir end reached)
241 call Error
242 db "File not found."
243 FindNameFound:
244 mov esi, [bx+StartCluster]
246 ;;;;;;;;;;;;;;;;;;;;;;;;;;
247 ;; Load the entire file ;;
248 ;;;;;;;;;;;;;;;;;;;;;;;;;;
250 push es
251 xor bp, bp
252 FileReadContinue:
253 shr bp, 4 ; bytes to paragraphs
254 mov di, es
255 add di, bp ; adjust segment for next sector
256 mov es, di ; es:0 updated
257 call ReadCluster ; read one cluster of root dir
258 sub [bx+FileSize], ebp
259 ja FileReadContinue
260 pop bp
262 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
263 ;; Type detection, .COM or .EXE? ;;
264 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
266 mov dl, [bx] ; pass the BIOS boot drive
267 mov ds, bp ; bp=ds=seg the file is loaded to
269 add bp, [bx+08h] ; bp = image base
270 mov ax, [bx+06h] ; ax = reloc items
271 mov di, [bx+18h] ; di = reloc table pointer
273 cmp word [bx], 5A4Dh ; "MZ" signature?
274 je RelocateEXE ; yes, it's an EXE program
276 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
277 ;; Setup and run a .COM program ;;
278 ;; Set CS=DS=ES=SS SP=0 IP=100h ;;
279 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
281 mov bp, ImageLoadSeg-10h ; "org 100h" stuff :)
282 mov ss, bp
283 xor sp, sp
284 push bp ; cs, ds and es
285 mov bh, 1 ; ip
286 jmp short Run
288 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
289 ;; Relocate, setup and run a .EXE program ;;
290 ;; Set CS:IP, SS:SP, DS, ES and AX according ;;
291 ;; to wiki.osdev.org/MZ#Initial_Program_State ;;
292 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
294 ReloCycle:
295 add [di+2], bp ; item seg (abs)
296 les si, [di] ; si = item ofs, es = item seg
297 add [es:si], bp ; fixup
298 scasw ; di += 2
299 scasw ; point to next entry
301 RelocateEXE:
302 dec ax ; 32768 max (128KB table)
303 jns ReloCycle ; leave with ax=0ffffh: both FCB in the
304 ; PSP don't have a valid drive identifier
305 les si, [bx+0Eh]
306 add si, bp
307 mov ss, si ; ss for EXE
308 mov sp, es ; sp for EXE
310 lea si, [bp-10h] ; ds and es both point to the segment
311 push si ; containing the PSP structure
313 add bp, [bx+16h] ; cs for EXE
314 mov bx, [bx+14h] ; ip for EXE
315 Run:
316 pop ds
317 push bp
318 push bx
319 push ds
320 pop es
322 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
323 ;; Set the magic numbers so the program knows that it ;;
324 ;; has been loaded by this bootsector and not by MS-DOS ;;
325 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
326 mov si, 16381 ; prime number 2**14-3
327 mov di, 32749 ; prime number 2**15-19
328 mov bp, 65521 ; prime number 2**16-15
330 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
331 ;; All done, transfer control to the program now ;;
332 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
333 retf
335 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
336 ;; Reads a exFAT cluster ;;
337 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
338 ;; Input: EDX:EAX = LBA ;;
339 ;; EBX = 0 ;;
340 ;; CX = sector cnt ;;
341 ;; ESI = cluster no ;;
342 ;; ES:0 -> buffer adrs ;;
343 ;; Output: EBX = 0 ;;
344 ;; CX = next cnt ;;
345 ;; EBP = bytes/sector;;
346 ;; ES:0 -> next adrs ;;
347 ;; C=0 for last sector ;;
348 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
350 ReadCluster:
351 inc cx ; jcxnz
352 add eax, byte 1
353 loop ReadSectorC
355 mov cl, [bx(bpbSectorSizeBits)]
356 dec cx
357 dec cx
358 mul ebx ; edx:eax = 0
359 inc ax
360 shl eax, cl ; eax=# of exFAT entries per sector
361 lea edi, [esi-2] ; edi=cluster #-2
362 xchg eax, esi
363 div esi ; eax=FAT sector #, edx=entry # in sector
365 imul si, dx, 4 ; si=entry # offset in sector
367 cdq
368 add eax, [bx(bpbFatSectorStart)] ; sector # relative to FAT32
369 call ReadSectorC ; read 1 FAT32 sector
371 mov esi, [es:si] ; esi=next cluster #
373 xor eax, eax
374 inc ax
375 mov cl, [bx(bpbSectorPerClusterBits)]
376 shl eax, cl ; 10000h max (32MB cluster)
377 xchg eax, ecx
378 xchg eax, edi ; get cluster #-2
379 mul ecx
381 add eax, [bx(bpbClusterSectorStart)]
382 ReadSectorC:
383 adc edx, ebx
385 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
386 ;; Reads a sector using BIOS Int 13h ;;
387 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
388 ;; Input: EDX:EAX = LBA ;;
389 ;; BX = 0 ;;
390 ;; CX = sector count ;;
391 ;; ES:0 -> buffer address ;;
392 ;; Output: BX = 0 ;;
393 ;; CX = next count ;;
394 ;; EBP = bytes/sector ;;
395 ;; ES:0 -> next address ;;
396 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
398 ReadSector:
400 xor ebp, ebp
401 inc bp
403 pushad
405 add eax, [bx(bpbSectorStart)]
406 adc edx, [bx(bpbSectorStart)+4]
408 push edx
409 push eax
410 push es
411 push bx
412 push bp ; sector count word = 1
413 mov cx, 16
414 push cx ; packet size byte = 16, reserved byte = 0
415 ReadSectorRetry:
416 mov si, sp
417 mov ah, 42h ; ah = 42h = extended read function no.
418 mov dl, [bx]
419 int 13h ; extended read sectors (DL, DS:SI)
421 jnc ReadSuccess
423 xor ax, ax
424 int 13h ; reset drive (DL)
425 loop ReadSectorRetry
427 call Error
428 db "Read error."
430 ReadSuccess:
431 mov cl, [bx(bpbSectorSizeBits)]
432 shl word [si+16+8], cl ; (e)bp si+16: EDI ESI EBP ESP EBX EDX ECX EAX
433 popa ; sp += 16
434 popad ; real registers
436 stc
437 loop ReadSectorNext
439 cmp esi, byte -10 ; carry=0 if last cluster, and carry=1 otherwise
440 ReadSectorNext:
441 ret
443 ;;;;;;;;;;;;;;;;;;;;;;;;;;
444 ;; Error Messaging Code ;;
445 ;;;;;;;;;;;;;;;;;;;;;;;;;;
447 Error:
448 pop si
449 mov dl, [bx] ; restore BIOS boot drive number
451 PutStr:
452 mov ah, 0Eh
453 mov bl, 7
454 lodsb
455 int 10h
456 cmp al, "."
457 jne PutStr
459 cbw
460 int 16h ; wait for a key...
461 int 19h ; bootstrap
463 Stop:
464 hlt
465 jmp short Stop
467 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
468 ;; Fill free space with zeroes ;;
469 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
471 times (512-13-($-$$)) db 0
473 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
474 ;; Name of the file to load and run ;;
475 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
477 ProgramName db "startup.bin" ; name and extension
478 NameLength equ $-ProgramName
480 ;;;;;;;;;;;;;;;;;;;;;;;;;;
481 ;; End of the sector ID ;;
482 ;;;;;;;;;;;;;;;;;;;;;;;;;;
484 dw 0AA55h ; BIOS checks for this ID