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

Up gnumeric (1.12.53)
author Pascal Bellard <pascal.bellard@slitaz.org>
date Mon Oct 03 08:28:51 2022 +0000 (19 months ago)
parents 81fc994927a6
children b0069c845544
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 637KB 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 - 1KB ;;
52 ;; | Boot Sector | ;;
53 ;; +------------------------+ A0000H - 0.5KB ;;
54 ;; | 0.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
83 SectorOf512Bytes equ 1 ; -13 bytes
84 CheckAttrib equ 0 ; +6 bytes
86 [BITS 16]
87 [CPU 386]
89 ImageLoadSeg equ 60h
90 StackSize equ 512
92 [SECTION .text]
93 [ORG 0]
95 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
96 ;; Boot sector starts here ;;
97 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
99 boot:
100 DriveNumber:
101 jmp short start ; Windows checks for this jump
102 nop
103 bsOemName times 8 db " " ; 0x03 "EXFAT "
104 times 53 db 0 ; 0x0B
106 ;;;;;;;;;;;;;;;;;;;;;
107 ;; BPB starts here ;;
108 ;;;;;;;;;;;;;;;;;;;;;
110 bpbSectorStart DQ 0 ; 0x40 partition first sector
111 bpbSectorCount DQ 0 ; 0x48 partition sectors count
112 bpbFatSectorStart DD 0 ; 0x50 FAT first sector
113 bpbFatSectorCount DD 0 ; 0x54 FAT sectors count
114 bpbClusterSectorStart DD 0 ; 0x58 first cluster sector
115 bpbClusterCount DD 0 ; 0x5C total clusters count
116 bpbRootDirCluster DD 0 ; 0x60 first cluster of the root dir
117 bpbVolumeSerial DD 0 ; 0x64 volume serial number
118 bpbFSVersionMinor DB 0 ; 0x68
119 bpbFSVersionMajor DB 0 ; 0x69
120 bpbVolumeStateFlags DW 0 ; 0x6A
121 bpbSectorSizeBits DB 0 ; 0x6C sector size as (1 << n)
122 bpbSectorPerClusterBits DB 0 ; 0x6D sector per cluster as (1 << n)
123 bpbNumberOfFATs DB 0 ; 0x6E always 1
124 bpbDriveNumber DB 0 ; 0x6F alaways 0x80
125 bpbAllocatedPercent DB 0 ; 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 ; reserve 1K 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 push es
163 mov [si(DriveNumber)], dx ; store BIOS boot drive number
164 rep movsw ; move 512 bytes (+ 12)
166 ;;;;;;;;;;;;;;;;;;;;;;
167 ;; Jump to the copy ;;
168 ;;;;;;;;;;;;;;;;;;;;;;
170 push word main
171 retf
173 main:
174 push cs
175 pop ds
177 xor ebx, ebx
179 mov esi, [bx(bpbRootDirCluster)] ; esi=cluster # of root dir
181 push byte ImageLoadSeg
182 pop es ; cx = 0
184 RootDirReadContinue:
185 call ReadCluster ; read one sector of root dir
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 03h ; 1 byte
202 Attributes equ 0Bh ; 1 byte
203 StartCluster equ 14h ; 4 bytes
204 FileSize equ 18h ; 8 bytes
206 FindNameCycle:
207 pusha
209 %if NullEntryCheck != 0
210 xor ax, ax
211 or al, [es:di]
212 je FindNameFailed
213 cmp al, 0c0h ; EXFAT_ENTRY_FILE_INFO ?
214 %else
215 cmp byte [es:di], 0c0h ; EXFAT_ENTRY_FILE_INFO ?
216 %endif
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 %if NullEntryCheck != 0
228 mov al, 0c1h ; EXFAT_ENTRY_FILE_NAME ?
229 %else
230 mov ax, 0c1h ; EXFAT_ENTRY_FILE_NAME ?
231 %endif
232 mov cx, NameLength+1
233 mov si, ProgramName ; ds:si -> program name
234 CheckName:
235 scasw ; compare UTF-16
236 lodsb ; with ASCII
237 loope CheckName
238 %if CheckAttrib != 0
239 VolumeLabel equ 8
240 SubDirectory equ 10h
241 jnz SkipFindName
242 test byte [bx+Attributes], VolumeLabel+SubDirectory
243 SkipFindName:
244 %endif
245 je FindNameFound ; cx = 0
246 popa ; restore ax, cx, si, di
248 add di, byte 32
249 cmp di, bp
250 jne FindNameCycle ; next root entry
251 loop RootDirReadContinue ; continue to the next root dir sector
252 cmp esi, byte -10 ; carry=0 if last cluster, and carry=1 otherwise
253 jc RootDirReadContinue ; continue to the next root dir cluster
254 FindNameFailed: ; end of root directory (dir end reached)
255 mov dl, [bx(DriveNumber)] ; restore BIOS boot drive number
256 call Error
257 db "File not found."
258 FindNameFound:
259 mov esi, [bx+StartCluster]
261 ;;;;;;;;;;;;;;;;;;;;;;;;;;
262 ;; Load the entire file ;;
263 ;;;;;;;;;;;;;;;;;;;;;;;;;;
265 push es
266 %if SectorOf512Bytes == 0
267 xor bp, bp
268 FileReadContinue:
269 shr bp, 4 ; bytes to paragraphs
270 mov di, es
271 add di, bp ; adjust segment for next sector
272 mov es, di ; es:0 updated
273 %else
274 FileReadContinue:
275 %endif
276 call ReadCluster ; read one more sector of the boot file
277 dec cx
278 sub [bx+FileSize], ebp ; max FileSize is < 640KB : check low 32 bits only
279 %if SectorOf512Bytes != 0
280 mov bp, es
281 lea bp, [bp+32]
282 mov es, bp ; es:0 updated
283 %endif
284 ja FileReadContinue
285 mov dx, [bx(DriveNumber)] ; restore BIOS boot drive number
286 xchg ax, di
287 pop bp
289 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
290 ;; Type detection, .COM or .EXE? ;;
291 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
293 mov ds, bp ; bp=ds=seg the file is loaded to
295 add bp, [bx+08h] ; bp = image base
296 mov di, [bx+18h] ; di = reloc table pointer
298 cmp word [bx], 5A4Dh ; "MZ" signature?
299 je RelocateEXE ; yes, it's an EXE program
301 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
302 ;; Setup and run a .COM program ;;
303 ;; Set CS=DS=ES=SS SP=0 IP=100h ;;
304 ;; AX=0ffffh BX=0 DX=drive and ;;
305 ;; cmdline=void ;;
306 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
308 mov di, 100h ; ip
309 mov bp, ImageLoadSeg-10h ; "org 100h" stuff :)
310 mov ss, bp
311 xor sp, sp
312 push bp ; cs, ds and es
313 jmp short Run
315 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
316 ;; Relocate, setup and run a .EXE program ;;
317 ;; Set CS:IP, SS:SP, DS, ES and AX according ;;
318 ;; to wiki.osdev.org/MZ#Initial_Program_State ;;
319 ;; AX=0ffffh BX=0 DX=drive cmdline=void ;;
320 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
322 ReloCycle:
323 add [di+2], bp ; item seg (abs)
324 les si, [di] ; si = item ofs, es = item seg
325 add [es:si], bp ; fixup
326 scasw ; di += 2
327 scasw ; point to next entry
329 RelocateEXE:
330 dec word [bx+06h] ; reloc items, 32768 max (128KB table)
331 jns ReloCycle
333 les si, [bx+0Eh]
334 add si, bp
335 mov ss, si ; ss for EXE
336 mov sp, es ; sp for EXE
338 lea si, [bp-10h] ; ds and es both point to the segment
339 push si ; containing the PSP structure
341 add bp, [bx+16h] ; cs for EXE
342 mov di, [bx+14h] ; ip for EXE
343 Run:
344 pop ds
345 push bp
346 push di
347 push ds
348 pop es
349 mov [80h], ax ; clear cmdline
350 dec ax ; both FCB in the PSP don't have a valid drive identifier
352 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
353 ;; Set the magic numbers so the program knows that it ;;
354 ;; has been loaded by this bootsector and not by MS-DOS ;;
355 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
356 mov si, 16381 ; prime number 2**14-3
357 mov di, 32749 ; prime number 2**15-19
358 mov bp, 65521 ; prime number 2**16-15
360 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
361 ;; All done, transfer control to the program now ;;
362 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
363 retf
365 ;;;;;;;;;;;;;;;;;;;;;;;;;;
366 ;; Error Messaging Code ;;
367 ;;;;;;;;;;;;;;;;;;;;;;;;;;
369 Error:
370 pop si
372 PutStr:
373 mov ah, 0Eh
374 mov bl, 7
375 lodsb
376 int 10h
377 cmp al, "."
378 jne PutStr
380 cbw
381 int 16h ; wait for a key...
382 int 19h ; bootstrap
384 Stop:
385 hlt
386 jmp short Stop
388 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
389 ;; Reads a exFAT cluster ;;
390 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
391 ;; Input: EDX:EAX = LBA ;;
392 ;; EBX = 0 ;;
393 ;; CX = sector cnt ;;
394 ;; ESI = cluster no ;;
395 ;; ES:0 -> buffer adrs ;;
396 ;; Output: EBX = 0 ;;
397 ;; CX = next cnt ;;
398 ;; EBP = bytes/sector;;
399 ;; ES:0 -> next adrs ;;
400 ;; C=0 for last sector ;;
401 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
403 ReadCluster:
404 add eax, byte 1
406 inc cx ; jcxnz
407 loop ReadSectorC
409 mul ebx ; edx:eax = 0
410 %if SectorOf512Bytes != 0
411 mov al, 128
412 %else
413 mov cl, -2
414 add cl, [bx(bpbSectorSizeBits)]
415 bts ax, cx ; eax=# of exFAT entries per sector
416 %endif
417 lea edi, [esi-2] ; edi=cluster #-2
418 xchg eax, esi
419 div esi ; eax=FAT sector #, edx=entry # in sector
421 imul si, dx, byte 4 ; si=entry # offset in sector
423 cdq
424 add eax, [bx(bpbFatSectorStart)] ; sector # relative to exFAT
425 call ReadSectorFAT ; read 1 exFAT sector, keep edx=0, set C
427 mov esi, [es:si] ; esi=next cluster #
429 mov dl, [bx(bpbSectorPerClusterBits)]
430 xor ecx, ecx
431 bts ecx, edx ; 10000h max (32MB cluster)
432 xchg eax, edi ; get cluster #-2
433 mul ecx
435 add eax, [bx(bpbClusterSectorStart)]
436 ReadSectorC:
437 mov di, bx
438 ReadSectorFAT:
439 adc edx, ebx
441 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
442 ;; Reads a sector using BIOS Int 13h ;;
443 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
444 ;; Input: EDX:EAX = LBA ;;
445 ;; BX = 0 ;;
446 ;; CX = sector count ;;
447 ;; ES:0 -> buffer address ;;
448 ;; Output: BX = 0 ;;
449 ;; CX = next count ;;
450 ;; EBP = bytes/sector ;;
451 ;; ES:0 -> next address ;;
452 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
454 %if SectorOf512Bytes != 0
455 lea ebp, [bx+512]
456 %else
457 lea ebp, [bx+1]
458 %endif
460 pushad
462 add eax, [bx(bpbSectorStart)]
463 adc edx, [bx(bpbSectorStart)+4]
465 push edx
466 push eax
467 push es
468 push bx
469 %if SectorOf512Bytes != 0
470 push byte 1 ; sector count word = 1
471 %else
472 push bp ; sector count word = 1
473 %endif
474 push byte 16 ; packet size byte = 16, reserved byte = 0
475 ReadSectorRetry:
476 mov si, sp
477 mov ah, 42h ; ah = 42h = extended read function no.
478 mov dl, [bx(DriveNumber)] ; restore BIOS boot drive number
479 int 13h ; extended read sectors (DL, DS:SI)
481 jnc ReadSuccess
483 %if ReadRetry != 0
484 xor ax, ax
485 int 13h ; reset drive (DL)
486 dec bp
487 %if SectorOf512Bytes != 0
488 jne ReadSectorRetry ; up to 511 tries
489 %else
490 jpe ReadSectorRetry ; up to 3 tries
491 %endif
492 %endif
494 call Error
495 db "Read error."
497 ReadSuccess:
498 %if SectorOf512Bytes == 0
499 mov cl, [bx(bpbSectorSizeBits)]
500 shl word [si+16+8], cl ; (e)bp si+16: EDI ESI EBP ESP EBX EDX ECX EAX
501 %endif
502 popa ; sp += 16
503 popad ; real registers
504 ret
506 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
507 ;; Fill free space with zeroes ;;
508 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
510 times (512-13-($-$$)) db 0
512 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
513 ;; Name of the file to load and run ;;
514 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
516 NameLength equ 11
517 ProgramName times NameLength db 0 ; name and extension
519 ;;;;;;;;;;;;;;;;;;;;;;;;;;
520 ;; End of the sector ID ;;
521 ;;;;;;;;;;;;;;;;;;;;;;;;;;
523 dw 0AA55h ; BIOS checks for this ID