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

Up glza (0.11.4)
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sat May 21 21:38:29 2022 +0000 (24 months ago)
parents 584e67527789
children 65784e875d45
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 ;; ax = 0ffffh (both FCB in the PSP don't have a valid drive identifier), ;;
62 ;; bx = cx = 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
81 [BITS 16]
82 [CPU 386]
84 ImageLoadSeg equ 60h
85 StackSize equ 1536
87 [SECTION .text]
88 [ORG 0]
90 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
91 ;; Boot sector starts here ;;
92 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
94 boot:
95 jmp short start ; Windows checks for this jump
96 nop
97 bsOemName DB "EXFAT " ; 0x03
98 times 53 db 0 ; 0x0B
100 ;;;;;;;;;;;;;;;;;;;;;
101 ;; BPB starts here ;;
102 ;;;;;;;;;;;;;;;;;;;;;
104 bpbSectorStart DQ 0 ; 0x40 partition first sector
105 bpbSectorCount DQ 0 ; 0x48 partition sectors count
106 bpbFatSectorStart DD 0 ; 0x50 FAT first sector
107 bpbFatSectorCount DD 0 ; 0x54 FAT sectors count
108 bpbClusterSectorStart DD 0 ; 0x58 first cluster sector
109 bpbClusterCount DD 0 ; 0x5C total clusters count
110 bpbRootDirCluster DD 0 ; 0x60 first cluster of the root dir
111 bpbVolumeSerial DD 0 ; 0x64 volume serial number
112 bpbFSVersionMinor DB 0 ; 0x68
113 bpbFSVersionMajor DB 0 ; 0x69
114 bpbVolumeStateFlags DW 0 ; 0x6A
115 bpbSectorSizeBits DB 0 ; 0x6C sector size as (1 << n)
116 bpbSectorPerClusterBits DB 0 ; 0x6D sector per cluster as (1 << n)
117 bpbNumberOfFATs DB 0 ; 0x6E always 1
118 bpbDriveNumber DB 0 ; 0x6F alaways 0x80
119 bpbAllocatedPercent DB 0 ; 0x70 percentage of allocated space
121 ;;;;;;;;;;;;;;;;;;;
122 ;; BPB ends here ;;
123 ;;;;;;;;;;;;;;;;;;;
125 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
126 ;; Boot sector code starts here ;;
127 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
129 start:
130 cld
132 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
133 ;; How much RAM is there? ;;
134 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
136 int 12h ; get conventional memory size (in KBs)
137 mov cx, 106h
138 dec ax
139 dec ax ; reserve 2K bytes for the code and the stack
140 shl ax, cl ; and convert it to 16-byte paragraphs
142 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
143 ;; Reserve memory for the boot sector and its stack ;;
144 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
146 mov es, ax ; cs:0 = ds:0 = ss:0 -> top - 512 - StackSize
147 mov ss, ax
148 mov sp, 512+StackSize ; bytes 0-511 are reserved for the boot code
150 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
151 ;; Copy ourselves to top of memory ;;
152 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
154 mov si, 7C00h
155 xor di, di
156 mov ds, di
157 rep movsw ; move 512 bytes (+ 12)
159 ;;;;;;;;;;;;;;;;;;;;;;
160 ;; Jump to the copy ;;
161 ;;;;;;;;;;;;;;;;;;;;;;
163 push es
164 push word main
165 retf
167 main:
168 push cs
169 pop ds
171 xor ebx, ebx
172 mov [bx], dl ; store BIOS boot drive number
174 mov esi, [bx(bpbRootDirCluster)] ; esi=cluster # of root dir
176 push byte ImageLoadSeg
177 pop es ; cx = 0
179 RootDirReadContinue:
180 call ReadCluster ; read one sector of root dir
181 pushf ; save carry="not last sector" flag
183 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
184 ;; Look for the COM/EXE file to load and run ;;
185 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
187 xor di, di ; es:di -> root entries array
189 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
190 ;; Looks for the file/dir ProgramName ;;
191 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
192 ;; Input: ES:DI -> root directory array ;;
193 ;; Output: ESI = cluster number ;;
194 ;; dword [bx+FileSize] file size ;;
195 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
197 CurNameSize equ 3
198 StartCluster equ 14h
199 FileSize equ 18h
201 FindNameCycle:
202 pusha
204 xor ax, ax
205 or al, [es:di]
206 je FindNameFailed
208 cmp al, 0c0h ; EXFAT_ENTRY_FILE_INFO ?
209 jne NotFileInfo
211 mov bl, 31
212 CopyInfo:
213 mov al, [es:di+bx]
214 mov [bx], al
215 dec bx
216 jnz CopyInfo ; keep BIOS boot drive number
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 mov dl, [bx] ; restore BIOS boot drive number
236 call Error
237 db "File not found."
238 FindNameFound:
239 mov esi, [bx+StartCluster]
241 ;;;;;;;;;;;;;;;;;;;;;;;;;;
242 ;; Load the entire file ;;
243 ;;;;;;;;;;;;;;;;;;;;;;;;;;
245 push es
246 xor bp, bp
247 FileReadContinue:
248 shr bp, 4 ; bytes to paragraphs
249 mov di, es
250 add di, bp ; adjust segment for next sector
251 mov es, di ; es:0 updated
252 call ReadCluster ; read one more sector of the boot file
253 sub [bx+FileSize], ebp
254 ja FileReadContinue
255 mov dl, [bx] ; restore BIOS boot drive number
256 xor ax, ax
257 pop bp
259 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
260 ;; Type detection, .COM or .EXE? ;;
261 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
263 mov ds, bp ; bp=ds=seg the file is loaded to
265 add bp, [bx+08h] ; bp = image base
266 mov di, [bx+18h] ; di = reloc table pointer
268 cmp word [bx], 5A4Dh ; "MZ" signature?
269 je RelocateEXE ; yes, it's an EXE program
271 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
272 ;; Setup and run a .COM program ;;
273 ;; Set CS=DS=ES=SS SP=0 IP=100h ;;
274 ;; AX=0ffffh BX=0 CX=0 DX=drive ;;
275 ;; and cmdline=void ;;
276 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
278 mov di, 100h ; ip
279 mov bp, ImageLoadSeg-10h ; "org 100h" stuff :)
280 mov ss, bp
281 xor sp, sp
282 push bp ; cs, ds and es
283 jmp short Run
285 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
286 ;; Relocate, setup and run a .EXE program ;;
287 ;; Set CS:IP, SS:SP, DS, ES and AX according ;;
288 ;; to wiki.osdev.org/MZ#Initial_Program_State ;;
289 ;; AX=0ffffh BX=0 CX=0 DX=drive cmdline=void ;;
290 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
292 ReloCycle:
293 add [di+2], bp ; item seg (abs)
294 les si, [di] ; si = item ofs, es = item seg
295 add [es:si], bp ; fixup
296 scasw ; di += 2
297 scasw ; point to next entry
299 RelocateEXE:
300 dec word [bx+06h] ; reloc items, 32768 max (128KB table)
301 jns ReloCycle
303 les si, [bx+0Eh]
304 add si, bp
305 mov ss, si ; ss for EXE
306 mov sp, es ; sp for EXE
308 lea si, [bp-10h] ; ds and es both point to the segment
309 push si ; containing the PSP structure
311 add bp, [bx+16h] ; cs for EXE
312 mov di, [bx+14h] ; ip for EXE
313 Run:
314 pop ds
315 push bp
316 push di
317 push ds
318 pop es
319 mov [80h], ax ; clear cmdline
320 dec ax ; both FCB in the PSP don't have a valid drive identifier
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 add eax, byte 1
353 inc cx ; jcxnz
354 loop ReadSectorC
356 mul ebx ; edx:eax = 0
357 mov cl, [bx(bpbSectorSizeBits)]
358 dec cx
359 stc
360 rcl 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, byte 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 inc dx
374 mov cl, [bx(bpbSectorPerClusterBits)]
375 shl edx, cl ; 10000h max (32MB cluster)
376 mov ecx, edx
377 xchg eax, edi ; get cluster #-2
378 mul ecx
380 add eax, [bx(bpbClusterSectorStart)]
381 ReadSectorC:
382 adc edx, ebx
384 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
385 ;; Reads a sector using BIOS Int 13h ;;
386 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
387 ;; Input: EDX:EAX = LBA ;;
388 ;; BX = 0 ;;
389 ;; CX = sector count ;;
390 ;; ES:0 -> buffer address ;;
391 ;; Output: BX = 0 ;;
392 ;; CX = next count ;;
393 ;; EBP = bytes/sector ;;
394 ;; ES:0 -> next address ;;
395 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
397 xor ebp, ebp
398 inc bp
400 pushad
402 add eax, [bx(bpbSectorStart)]
403 adc edx, [bx(bpbSectorStart)+4]
405 push edx
406 push eax
407 push es
408 push bx
409 push bp ; sector count word = 1
410 mov cx, 16
411 push cx ; packet size byte = 16, reserved byte = 0
412 ReadSectorRetry:
413 mov si, sp
414 mov ah, 42h ; ah = 42h = extended read function no.
415 mov dl, [bx] ; restore BIOS boot drive number
416 int 13h ; extended read sectors (DL, DS:SI)
418 jnc ReadSuccess
420 xor ax, ax
421 int 13h ; reset drive (DL)
422 loop ReadSectorRetry ; up to 16 retries
424 call Error
425 db "Read error."
427 ReadSuccess:
428 mov cl, [bx(bpbSectorSizeBits)]
429 shl word [si+16+8], cl ; (e)bp si+16: EDI ESI EBP ESP EBX EDX ECX EAX
430 popa ; sp += 16
431 popad ; real registers
433 stc
434 loop ReadSectorNext
436 cmp esi, byte -10 ; carry=0 if last cluster, and carry=1 otherwise
437 ReadSectorNext:
438 ret
440 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
441 ;; Fill free space with zeroes ;;
442 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
444 times (512-13-20-($-$$)) db 0
446 ;;;;;;;;;;;;;;;;;;;;;;;;;;
447 ;; Error Messaging Code ;;
448 ;;;;;;;;;;;;;;;;;;;;;;;;;;
450 Error:
451 pop si
453 PutStr:
454 mov ah, 0Eh
455 mov bl, 7
456 lodsb
457 int 10h
458 cmp al, "."
459 jne PutStr
461 cbw
462 int 16h ; wait for a key...
463 int 19h ; bootstrap
465 Stop:
466 hlt
467 jmp short Stop
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