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

Typos in BootProg
author Pascal Bellard <pascal.bellard@slitaz.org>
date Fri Feb 18 10:06:45 2022 +0000 (2022-02-18)
parents 89c8d8b6cf48
children d211771a0500
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 ? equ 0
90 ImageLoadSeg equ 60h
91 StackSize equ 1536
93 [SECTION .text]
94 [ORG 0]
96 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
97 ;; Boot sector starts here ;;
98 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
100 boot:
101 jmp short start ; Windows checks for this jump
102 nop
103 bsOemName DB "EXFAT " ; 0x03
104 times 53 db 0 ; 0x0B
106 ;;;;;;;;;;;;;;;;;;;;;
107 ;; BPB starts here ;;
108 ;;;;;;;;;;;;;;;;;;;;;
110 bpbSectorStart DQ ? ; 0x40 partition first sector
111 bpbSectorCount DQ ? ; 0x48 partition sectors count
112 bpbFatSectorStart DD ? ; 0x50 FAT first sector
113 bpbFatSectorCount DD ? ; 0x54 FAT sectors count
114 bpbClusterSectorStart DD ? ; 0x58 first cluster sector
115 bpbClusterCount DD ? ; 0x5C total clusters count
116 bpbRootDirCluster DD ? ; 0x60 first cluster of the root dir
117 bpbVolumeSerial DD ? ; 0x64 volume serial number
118 bpbFSVersionMinor DB ? ; 0x68
119 bpbFSVersionMajor DB ? ; 0x69
120 bpbVolumeStateFlags DW ? ; 0x6A
121 bpbSectorSizeBits DB ? ; 0x6C sector size as (1 << n)
122 bpbSectorPerClusterBits DB ? ; 0x6D sector per cluster as (1 << n)
123 bpbNumberOfFATs DB ? ; 0x6E always 1
124 bpbDriveNumber DB ? ; 0x6F alaways 0x80
125 bpbAllocatedPercent DB ? ; 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
145 dec ax ; reserve 2K bytes for the code and the stack
146 shl ax, cl ; and convert it to 16-byte paragraphs
148 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
149 ;; Reserve memory for the boot sector and its stack ;;
150 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
152 mov es, ax ; cs:0 = ds:0 = ss:0 -> top - 512 - StackSize
153 mov ss, ax
154 mov sp, 512+StackSize ; bytes 0-511 are reserved for the boot code
156 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
157 ;; Copy ourselves to top of memory ;;
158 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
160 mov si, 7C00h
161 xor di, di
162 mov ds, di
163 rep movsw ; move 512 bytes (+ 12)
165 ;;;;;;;;;;;;;;;;;;;;;;
166 ;; Jump to the copy ;;
167 ;;;;;;;;;;;;;;;;;;;;;;
169 push es
170 push main
171 retf
173 main:
174 push cs
175 pop ds
177 xor ebx, ebx
178 mov [bx], dx ; store BIOS boot drive number
180 mov esi, [bx(bpbRootDirCluster)] ; esi=cluster # of root dir
182 push ImageLoadSeg
183 pop es
185 RootDirReadContinue:
186 call ReadCluster ; read one sector of root dir
187 pushf ; save carry="not last sector" flag
189 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
190 ;; Look for the COM/EXE file to load and run ;;
191 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
193 xor di, di ; es:di -> root entries array
195 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
196 ;; Looks for the file/dir ProgramName ;;
197 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
198 ;; Input: ES:DI -> root directory array ;;
199 ;; Output: ESI = cluster number ;;
200 ;; dword [bx+FileSize] file size ;;
201 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
203 CurNameSize equ 3
204 StartCluster equ 14h
205 FileSize equ 18h
207 FindNameCycle:
208 pusha
210 xor ax, ax
211 or al, [es:di]
212 je FindNameFailed
214 cmp al, 0c0h ; EXFAT_ENTRY_FILE_INFO ?
215 jne NotFileInfo
217 mov bl, 30
218 CopyInfo:
219 mov ax, [es:di+bx]
220 mov [bx], ax
221 dec bx
222 dec bx
223 jnz CopyInfo
225 NotFileInfo:
226 mov al, 0c1h ; EXFAT_ENTRY_FILE_NAME ?
227 mov cx, NameLength+1
228 mov si, ProgramName ; ds:si -> program name
229 CheckName:
230 scasw ; compare UTF-16
231 lodsb ; with ASCII
232 loope CheckName
233 je FindNameFound ; cx = 0
234 popa ; restore ax, cx, si, di
236 add di, 32
237 cmp di, bp
238 jne FindNameCycle ; next root entry
239 popf ; restore carry="not last sector" flag
240 jc RootDirReadContinue ; continue to the next root dir cluster
241 FindNameFailed: ; end of root directory (dir end reached)
242 call Error
243 db "File not found."
244 FindNameFound:
245 mov esi, [bx+StartCluster]
247 ;;;;;;;;;;;;;;;;;;;;;;;;;;
248 ;; Load the entire file ;;
249 ;;;;;;;;;;;;;;;;;;;;;;;;;;
251 push es
252 xor bp, bp
253 FileReadContinue:
254 shr bp, 4 ; bytes to paragraphs
255 mov di, es
256 add di, bp ; adjust segment for next sector
257 mov es, di ; es:0 updated
258 call ReadCluster ; read one cluster of root dir
259 sub [bx+FileSize], ebp
260 ja FileReadContinue
261 pop bp
263 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
264 ;; Type detection, .COM or .EXE? ;;
265 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
267 mov dl, [bx] ; pass the BIOS boot drive
268 mov ds, bp ; bp=ds=seg the file is loaded to
270 add bp, [bx+08h] ; bp = image base
271 mov ax, [bx+06h] ; ax = reloc items
272 mov di, [bx+18h] ; di = reloc table pointer
274 cmp word [bx], 5A4Dh ; "MZ" signature?
275 je RelocateEXE ; yes, it's an EXE program
277 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
278 ;; Setup and run a .COM program ;;
279 ;; Set CS=DS=ES=SS SP=0 IP=100h ;;
280 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
282 mov bp, ImageLoadSeg-10h ; "org 100h" stuff :)
283 mov ss, bp
284 xor sp, sp
285 push bp ; cs, ds and es
286 mov bh, 1 ; ip
287 jmp short Run
289 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
290 ;; Relocate, setup and run a .EXE program ;;
291 ;; Set CS:IP, SS:SP, DS, ES and AX according ;;
292 ;; to wiki.osdev.org/MZ#Initial_Program_State ;;
293 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
295 ReloCycle:
296 add [di+2], bp ; item seg (abs)
297 les si, [di] ; si = item ofs, es = item seg
298 add [es:si], bp ; fixup
299 scasw ; di += 2
300 scasw ; point to next entry
302 RelocateEXE:
303 dec ax ; 32768 max (128KB table)
304 jns ReloCycle ; leave with ax=0ffffh: both FCB in the
305 ; PSP don't have a valid drive identifier
306 les si, [bx+0Eh]
307 add si, bp
308 mov ss, si ; ss for EXE
309 mov sp, es ; sp for EXE
311 lea si, [bp-10h] ; ds and es both point to the segment
312 push si ; containing the PSP structure
314 add bp, [bx+16h] ; cs for EXE
315 mov bx, [bx+14h] ; ip for EXE
316 Run:
317 pop ds
318 push bp
319 push bx
320 push ds
321 pop es
323 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
324 ;; Set the magic numbers so the program knows that it ;;
325 ;; has been loaded by this bootsector and not by MS-DOS ;;
326 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
327 mov si, 16381 ; prime number 2**14-3
328 mov di, 32749 ; prime number 2**15-19
329 mov bp, 65521 ; prime number 2**16-15
331 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
332 ;; All done, transfer control to the program now ;;
333 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
334 retf
336 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
337 ;; Reads a exFAT cluster ;;
338 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
339 ;; Input: EDX:EAX = LBA ;;
340 ;; EBX = 0 ;;
341 ;; CX = sector cnt ;;
342 ;; ESI = cluster no ;;
343 ;; ES:0 -> buffer adrs ;;
344 ;; Output: EBX = 0 ;;
345 ;; CX = next cnt ;;
346 ;; EBP = bytes/sector;;
347 ;; ES:0 -> next adrs ;;
348 ;; C=0 for last sector ;;
349 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
351 ReadCluster:
352 inc cx ; jcxnz
353 add eax, 1
354 loop ReadSectorC
356 mov cl, [bx(bpbSectorSizeBits)]
357 dec cx
358 dec cx
359 mul ebx ; edx:eax = 0
360 inc ax
361 shl eax, cl ; eax=# of exFAT entries per sector
362 lea edi, [esi-2] ; edi=cluster #-2
363 xchg eax, esi
364 div esi ; eax=FAT sector #, edx=entry # in sector
366 imul si, dx, 4 ; si=entry # offset in sector
368 cdq
369 add eax, [bx(bpbFatSectorStart)] ; sector # relative to FAT32
370 call ReadSectorC ; read 1 FAT32 sector
372 mov esi, [es:si] ; esi=next cluster #
374 xor eax, eax
375 inc ax
376 mov cl, [bx(bpbSectorPerClusterBits)]
377 shl eax, cl ; 10000h max (32MB cluster)
378 xchg eax, ecx
379 xchg eax, edi ; get cluster #-2
380 mul ecx
382 add eax, [bx(bpbClusterSectorStart)]
383 ReadSectorC:
384 adc edx, ebx
386 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
387 ;; Reads a sector using BIOS Int 13h ;;
388 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
389 ;; Input: EDX:EAX = LBA ;;
390 ;; BX = 0 ;;
391 ;; CX = sector count ;;
392 ;; ES:0 -> buffer address ;;
393 ;; Output: BX = 0 ;;
394 ;; CX = next count ;;
395 ;; EBP = bytes/sector ;;
396 ;; ES:0 -> next address ;;
397 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
399 ReadSector:
401 xor ebp, ebp
402 inc bp
404 pushad
406 add eax, [bx(bpbSectorStart)]
407 adc edx, [bx(bpbSectorStart)+4]
409 push edx
410 push eax
411 push es
412 push bx
413 push bp ; sector count word = 1
414 mov cx, 16
415 push cx ; packet size byte = 16, reserved byte = 0
416 ReadSectorRetry:
417 mov si, sp
418 mov ah, 42h ; ah = 42h = extended read function no.
419 mov dl, [bx]
420 int 13h ; extended read sectors (DL, DS:SI)
422 jnc ReadSuccess
424 xor ax, ax
425 int 13h ; reset drive (DL)
426 loop ReadSectorRetry
428 call Error
429 db "Read error."
431 ReadSuccess:
432 mov cl, [bx(bpbSectorSizeBits)]
433 shl word [si+16+8], cl ; (e)bp si+16: EDI ESI EBP ESP EBX EDX ECX EAX
434 popa ; sp += 16
435 popad ; real registers
437 stc
438 loop ReadSectorNext
440 cmp esi, 0FFFFFFF6h ; carry=0 if last cluster, and carry=1 otherwise
441 ReadSectorNext:
442 ret
444 ;;;;;;;;;;;;;;;;;;;;;;;;;;
445 ;; Error Messaging Code ;;
446 ;;;;;;;;;;;;;;;;;;;;;;;;;;
448 Error:
449 pop si
450 mov dl, [bx] ; restore BIOS boot drive number
452 PutStr:
453 mov ah, 0Eh
454 mov bl, 7
455 lodsb
456 int 10h
457 cmp al, "."
458 jne PutStr
460 cbw
461 int 16h ; wait for a key...
462 int 19h ; bootstrap
464 Stop:
465 hlt
466 jmp short Stop
468 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
469 ;; Fill free space with zeroes ;;
470 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
472 times (512-13-($-$$)) db 0
474 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
475 ;; Name of the file to load and run ;;
476 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
478 ProgramName db "startup.bin" ; name and extension
479 NameLength equ $-ProgramName
481 ;;;;;;;;;;;;;;;;;;;;;;;;;;
482 ;; End of the sector ID ;;
483 ;;;;;;;;;;;;;;;;;;;;;;;;;;
485 dw 0AA55h ; BIOS checks for this ID