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

Normazile https://sourceforge.net/projects web_sites
author Pascal Bellard <pascal.bellard@slitaz.org>
date Fri Sep 23 08:28:09 2022 +0000 (19 months ago)
parents 78727b04c002
children 9a714ac859a3
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 = 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
84 [BITS 16]
85 [CPU 386]
87 ImageLoadSeg equ 60h
88 StackSize equ 1536
90 [SECTION .text]
91 [ORG 0]
93 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
94 ;; Boot sector starts here ;;
95 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
97 boot:
98 DriveNumber:
99 jmp short start ; Windows checks for this jump
100 nop
101 bsOemName DB "EXFAT " ; 0x03
102 times 53 db 0 ; 0x0B
104 ;;;;;;;;;;;;;;;;;;;;;
105 ;; BPB starts here ;;
106 ;;;;;;;;;;;;;;;;;;;;;
108 bpbSectorStart DQ 0 ; 0x40 partition first sector
109 bpbSectorCount DQ 0 ; 0x48 partition sectors count
110 bpbFatSectorStart DD 0 ; 0x50 FAT first sector
111 bpbFatSectorCount DD 0 ; 0x54 FAT sectors count
112 bpbClusterSectorStart DD 0 ; 0x58 first cluster sector
113 bpbClusterCount DD 0 ; 0x5C total clusters count
114 bpbRootDirCluster DD 0 ; 0x60 first cluster of the root dir
115 bpbVolumeSerial DD 0 ; 0x64 volume serial number
116 bpbFSVersionMinor DB 0 ; 0x68
117 bpbFSVersionMajor DB 0 ; 0x69
118 bpbVolumeStateFlags DW 0 ; 0x6A
119 bpbSectorSizeBits DB 0 ; 0x6C sector size as (1 << n)
120 bpbSectorPerClusterBits DB 0 ; 0x6D sector per cluster as (1 << n)
121 bpbNumberOfFATs DB 0 ; 0x6E always 1
122 bpbDriveNumber DB 0 ; 0x6F alaways 0x80
123 bpbAllocatedPercent DB 0 ; 0x70 percentage of allocated space
125 ;;;;;;;;;;;;;;;;;;;
126 ;; BPB ends here ;;
127 ;;;;;;;;;;;;;;;;;;;
129 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
130 ;; Boot sector code starts here ;;
131 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
133 start:
134 cld
136 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
137 ;; How much RAM is there? ;;
138 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
140 int 12h ; get conventional memory size (in KBs)
141 mov cx, 106h
142 dec ax
143 dec ax ; reserve 2K bytes for the code and the stack
144 shl ax, cl ; and convert it to 16-byte paragraphs
146 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
147 ;; Reserve memory for the boot sector and its stack ;;
148 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
150 mov es, ax ; cs:0 = ds:0 = ss:0 -> top - 512 - StackSize
151 mov ss, ax
152 mov sp, 512+StackSize ; bytes 0-511 are reserved for the boot code
154 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
155 ;; Copy ourselves to top of memory ;;
156 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
158 mov si, 7C00h
159 xor di, di
160 mov ds, di
161 push es
162 mov [si(DriveNumber)], dx ; store BIOS boot drive number
163 rep movsw ; move 512 bytes (+ 12)
165 ;;;;;;;;;;;;;;;;;;;;;;
166 ;; Jump to the copy ;;
167 ;;;;;;;;;;;;;;;;;;;;;;
169 push word main
170 retf
172 main:
173 push cs
174 pop ds
176 xor ebx, ebx
178 mov esi, [bx(bpbRootDirCluster)] ; esi=cluster # of root dir
180 push byte ImageLoadSeg
181 pop es ; cx = 0
183 RootDirReadContinue:
184 call ReadCluster ; read one sector of root dir
185 pushf ; save carry="not last sector" flag
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 3 ; 1 byte
202 StartCluster equ 14h ; 4 bytes
203 FileSize equ 18h ; 8 bytes
205 FindNameCycle:
206 pusha
208 %if NullEntryCheck != 0
209 xor ax, ax
210 or al, [es:di]
211 je FindNameFailed
212 %else
213 movzx ax, byte [es:di]
214 %endif
216 cmp al, 0c0h ; EXFAT_ENTRY_FILE_INFO ?
217 jne NotFileInfo
219 mov bl, 31
220 CopyInfo:
221 mov al, [es:di+bx]
222 mov [bx], al
223 dec bx
224 jnz CopyInfo ; keep BIOS boot drive number
226 NotFileInfo:
227 mov al, 0c1h ; EXFAT_ENTRY_FILE_NAME ?
228 mov cx, NameLength+1
229 mov si, ProgramName ; ds:si -> program name
230 CheckName:
231 scasw ; compare UTF-16
232 lodsb ; with ASCII
233 loope CheckName
234 je FindNameFound ; cx = 0
235 popa ; restore ax, cx, si, di
237 add di, byte 32
238 cmp di, bp
239 jne FindNameCycle ; next root entry
240 popf ; restore carry="not last sector" flag
241 jc RootDirReadContinue ; continue to the next root dir cluster
242 FindNameFailed: ; end of root directory (dir end reached)
243 mov dl, [bx(DriveNumber)] ; restore BIOS boot drive number
244 call Error
245 db "File not found."
246 FindNameFound:
247 mov esi, [bx+StartCluster]
249 ;;;;;;;;;;;;;;;;;;;;;;;;;;
250 ;; Load the entire file ;;
251 ;;;;;;;;;;;;;;;;;;;;;;;;;;
253 push es
254 xor bp, bp
255 FileReadContinue:
256 shr bp, 4 ; bytes to paragraphs
257 mov di, es
258 add di, bp ; adjust segment for next sector
259 mov es, di ; es:0 updated
260 call ReadCluster ; read one more sector of the boot file
261 sub [bx+FileSize], ebp ; max FileSize is < 640KB : check low 32 bits only
262 ja FileReadContinue
263 mov dx, [bx(DriveNumber)] ; restore BIOS boot drive number
264 xchg ax, di
265 pop bp
267 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
268 ;; Type detection, .COM or .EXE? ;;
269 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
271 mov ds, bp ; bp=ds=seg the file is loaded to
273 add bp, [bx+08h] ; bp = image base
274 mov di, [bx+18h] ; di = reloc table pointer
276 cmp word [bx], 5A4Dh ; "MZ" signature?
277 je RelocateEXE ; yes, it's an EXE program
279 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
280 ;; Setup and run a .COM program ;;
281 ;; Set CS=DS=ES=SS SP=0 IP=100h ;;
282 ;; AX=0ffffh BX=0 DX=drive and ;;
283 ;; cmdline=void ;;
284 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
286 mov di, 100h ; ip
287 mov bp, ImageLoadSeg-10h ; "org 100h" stuff :)
288 mov ss, bp
289 xor sp, sp
290 push bp ; cs, ds and es
291 jmp short Run
293 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
294 ;; Relocate, setup and run a .EXE program ;;
295 ;; Set CS:IP, SS:SP, DS, ES and AX according ;;
296 ;; to wiki.osdev.org/MZ#Initial_Program_State ;;
297 ;; AX=0ffffh BX=0 DX=drive cmdline=void ;;
298 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
300 ReloCycle:
301 add [di+2], bp ; item seg (abs)
302 les si, [di] ; si = item ofs, es = item seg
303 add [es:si], bp ; fixup
304 scasw ; di += 2
305 scasw ; point to next entry
307 RelocateEXE:
308 dec word [bx+06h] ; reloc items, 32768 max (128KB table)
309 jns ReloCycle
311 les si, [bx+0Eh]
312 add si, bp
313 mov ss, si ; ss for EXE
314 mov sp, es ; sp for EXE
316 lea si, [bp-10h] ; ds and es both point to the segment
317 push si ; containing the PSP structure
319 add bp, [bx+16h] ; cs for EXE
320 mov di, [bx+14h] ; ip for EXE
321 Run:
322 pop ds
323 push bp
324 push di
325 push ds
326 pop es
327 mov [80h], ax ; clear cmdline
328 dec ax ; both FCB in the PSP don't have a valid drive identifier
330 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
331 ;; Set the magic numbers so the program knows that it ;;
332 ;; has been loaded by this bootsector and not by MS-DOS ;;
333 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
334 mov si, 16381 ; prime number 2**14-3
335 mov di, 32749 ; prime number 2**15-19
336 mov bp, 65521 ; prime number 2**16-15
338 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
339 ;; All done, transfer control to the program now ;;
340 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
341 retf
343 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
344 ;; Reads a exFAT cluster ;;
345 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
346 ;; Input: EDX:EAX = LBA ;;
347 ;; EBX = 0 ;;
348 ;; CX = sector cnt ;;
349 ;; ESI = cluster no ;;
350 ;; ES:0 -> buffer adrs ;;
351 ;; Output: EBX = 0 ;;
352 ;; CX = next cnt ;;
353 ;; EBP = bytes/sector;;
354 ;; ES:0 -> next adrs ;;
355 ;; C=0 for last sector ;;
356 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
358 ReadCluster:
359 add eax, byte 1
361 inc cx ; jcxnz
362 loop ReadSectorC
364 mul ebx ; edx:eax = 0
365 mov cl, -2
366 add cl, [bx(bpbSectorSizeBits)]
367 bts ax, cx ; eax=# of exFAT entries per sector
368 lea edi, [esi-2] ; edi=cluster #-2
369 xchg eax, esi
370 div esi ; eax=FAT sector #, edx=entry # in sector
372 imul si, dx, byte 4 ; si=entry # offset in sector
374 cdq
375 add eax, [bx(bpbFatSectorStart)] ; sector # relative to exFAT
376 call ReadSectorFAT ; read 1 exFAT sector, keep edx=0, set C
378 mov esi, [es:si] ; esi=next cluster #
380 mov dl, [bx(bpbSectorPerClusterBits)]
381 xor ecx, ecx
382 bts ecx, edx ; 10000h max (32MB cluster)
383 xchg eax, edi ; get cluster #-2
384 mul ecx
386 add eax, [bx(bpbClusterSectorStart)]
387 ReadSectorC:
388 mov di, bx
389 ReadSectorFAT:
390 adc edx, ebx
392 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
393 ;; Reads a sector using BIOS Int 13h ;;
394 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
395 ;; Input: EDX:EAX = LBA ;;
396 ;; BX = 0 ;;
397 ;; CX = sector count ;;
398 ;; ES:0 -> buffer address ;;
399 ;; Output: BX = 0 ;;
400 ;; CX = next count ;;
401 ;; EBP = bytes/sector ;;
402 ;; ES:0 -> next address ;;
403 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
405 xor ebp, ebp
406 inc bp
408 pushad
410 add eax, [bx(bpbSectorStart)]
411 adc edx, [bx(bpbSectorStart)+4]
413 push edx
414 push eax
415 push es
416 push bx
417 push bp ; sector count word = 1
418 push byte 16 ; packet size byte = 16, reserved byte = 0
419 ReadSectorRetry:
420 mov si, sp
421 mov ah, 42h ; ah = 42h = extended read function no.
422 mov dl, [bx(DriveNumber)] ; restore BIOS boot drive number
423 int 13h ; extended read sectors (DL, DS:SI)
425 jnc ReadSuccess
427 %if ReadRetry != 0
428 xor ax, ax
429 int 13h ; reset drive (DL)
430 dec bp
431 jpe ReadSectorRetry ; up to 3 tries
432 %endif
434 call Error
435 db "Read error."
437 ReadSuccess:
438 mov cl, [bx(bpbSectorSizeBits)]
439 shl word [si+16+8], cl ; (e)bp si+16: EDI ESI EBP ESP EBX EDX ECX EAX
440 popa ; sp += 16
441 popad ; real registers
443 stc
444 loop ReadSectorNext
446 cmp esi, byte -10 ; carry=0 if last cluster, and carry=1 otherwise
447 ReadSectorNext:
448 ret
450 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
451 ;; Fill free space with zeroes ;;
452 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
454 times (512-13-20-($-$$)) db 0
456 ;;;;;;;;;;;;;;;;;;;;;;;;;;
457 ;; Error Messaging Code ;;
458 ;;;;;;;;;;;;;;;;;;;;;;;;;;
460 Error:
461 pop si
463 PutStr:
464 mov ah, 0Eh
465 mov bl, 7
466 lodsb
467 int 10h
468 cmp al, "."
469 jne PutStr
471 cbw
472 int 16h ; wait for a key...
473 int 19h ; bootstrap
475 Stop:
476 hlt
477 jmp short Stop
479 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
480 ;; Name of the file to load and run ;;
481 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
483 ProgramName db "startup.bin" ; name and extension
484 NameLength equ $-ProgramName
486 ;;;;;;;;;;;;;;;;;;;;;;;;;;
487 ;; End of the sector ID ;;
488 ;;;;;;;;;;;;;;;;;;;;;;;;;;
490 dw 0AA55h ; BIOS checks for this ID