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

Update some web_site
author Pascal Bellard <pascal.bellard@slitaz.org>
date Tue Sep 27 16:21:52 2022 +0000 (19 months ago)
parents ad0bc3efbf37
children 7dd01dedad38
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
83 SectorOf512Bytes equ 1 ; -13 bytes
85 [BITS 16]
86 [CPU 386]
88 ImageLoadSeg equ 60h
89 StackSize equ 1536
91 [SECTION .text]
92 [ORG 0]
94 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
95 ;; Boot sector starts here ;;
96 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
98 boot:
99 DriveNumber:
100 jmp short start ; Windows checks for this jump
101 nop
102 bsOemName times 8 db " " ; 0x03 "EXFAT "
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 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 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 loop RootDirReadContinue ; continue to the next root dir sector
241 cmp esi, byte -10 ; carry=0 if last cluster, and carry=1 otherwise
242 jc RootDirReadContinue ; continue to the next root dir cluster
243 FindNameFailed: ; end of root directory (dir end reached)
244 mov dl, [bx(DriveNumber)] ; restore BIOS boot drive number
245 call Error
246 db "File not found."
247 FindNameFound:
248 mov esi, [bx+StartCluster]
250 ;;;;;;;;;;;;;;;;;;;;;;;;;;
251 ;; Load the entire file ;;
252 ;;;;;;;;;;;;;;;;;;;;;;;;;;
254 push es
255 %if SectorOf512Bytes == 0
256 xor bp, bp
257 FileReadContinue:
258 shr bp, 4 ; bytes to paragraphs
259 mov di, es
260 add di, bp ; adjust segment for next sector
261 mov es, di ; es:0 updated
262 %else
263 FileReadContinue:
264 %endif
265 call ReadCluster ; read one more sector of the boot file
266 dec cx
267 sub [bx+FileSize], ebp ; max FileSize is < 640KB : check low 32 bits only
268 %if SectorOf512Bytes != 0
269 mov bp, es
270 lea bp, [bp+32]
271 mov es, bp ; es:0 updated
272 %endif
273 ja FileReadContinue
274 mov dx, [bx(DriveNumber)] ; restore BIOS boot drive number
275 xchg ax, di
276 pop bp
278 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
279 ;; Type detection, .COM or .EXE? ;;
280 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
282 mov ds, bp ; bp=ds=seg the file is loaded to
284 add bp, [bx+08h] ; bp = image base
285 mov di, [bx+18h] ; di = reloc table pointer
287 cmp word [bx], 5A4Dh ; "MZ" signature?
288 je RelocateEXE ; yes, it's an EXE program
290 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
291 ;; Setup and run a .COM program ;;
292 ;; Set CS=DS=ES=SS SP=0 IP=100h ;;
293 ;; AX=0ffffh BX=0 DX=drive and ;;
294 ;; cmdline=void ;;
295 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
297 mov di, 100h ; ip
298 mov bp, ImageLoadSeg-10h ; "org 100h" stuff :)
299 mov ss, bp
300 xor sp, sp
301 push bp ; cs, ds and es
302 jmp short Run
304 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
305 ;; Relocate, setup and run a .EXE program ;;
306 ;; Set CS:IP, SS:SP, DS, ES and AX according ;;
307 ;; to wiki.osdev.org/MZ#Initial_Program_State ;;
308 ;; AX=0ffffh BX=0 DX=drive cmdline=void ;;
309 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
311 ReloCycle:
312 add [di+2], bp ; item seg (abs)
313 les si, [di] ; si = item ofs, es = item seg
314 add [es:si], bp ; fixup
315 scasw ; di += 2
316 scasw ; point to next entry
318 RelocateEXE:
319 dec word [bx+06h] ; reloc items, 32768 max (128KB table)
320 jns ReloCycle
322 les si, [bx+0Eh]
323 add si, bp
324 mov ss, si ; ss for EXE
325 mov sp, es ; sp for EXE
327 lea si, [bp-10h] ; ds and es both point to the segment
328 push si ; containing the PSP structure
330 add bp, [bx+16h] ; cs for EXE
331 mov di, [bx+14h] ; ip for EXE
332 Run:
333 pop ds
334 push bp
335 push di
336 push ds
337 pop es
338 mov [80h], ax ; clear cmdline
339 dec ax ; both FCB in the PSP don't have a valid drive identifier
341 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
342 ;; Set the magic numbers so the program knows that it ;;
343 ;; has been loaded by this bootsector and not by MS-DOS ;;
344 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
345 mov si, 16381 ; prime number 2**14-3
346 mov di, 32749 ; prime number 2**15-19
347 mov bp, 65521 ; prime number 2**16-15
349 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
350 ;; All done, transfer control to the program now ;;
351 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
352 retf
354 ;;;;;;;;;;;;;;;;;;;;;;;;;;
355 ;; Error Messaging Code ;;
356 ;;;;;;;;;;;;;;;;;;;;;;;;;;
358 Error:
359 pop si
361 PutStr:
362 mov ah, 0Eh
363 mov bl, 7
364 lodsb
365 int 10h
366 cmp al, "."
367 jne PutStr
369 cbw
370 int 16h ; wait for a key...
371 int 19h ; bootstrap
373 Stop:
374 hlt
375 jmp short Stop
377 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
378 ;; Reads a exFAT cluster ;;
379 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
380 ;; Input: EDX:EAX = LBA ;;
381 ;; EBX = 0 ;;
382 ;; CX = sector cnt ;;
383 ;; ESI = cluster no ;;
384 ;; ES:0 -> buffer adrs ;;
385 ;; Output: EBX = 0 ;;
386 ;; CX = next cnt ;;
387 ;; EBP = bytes/sector;;
388 ;; ES:0 -> next adrs ;;
389 ;; C=0 for last sector ;;
390 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
392 ReadCluster:
393 add eax, byte 1
395 inc cx ; jcxnz
396 loop ReadSectorC
398 mul ebx ; edx:eax = 0
399 %if SectorOf512Bytes != 0
400 mov al, 128
401 %else
402 mov cl, -2
403 add cl, [bx(bpbSectorSizeBits)]
404 bts ax, cx ; eax=# of exFAT entries per sector
405 %endif
406 lea edi, [esi-2] ; edi=cluster #-2
407 xchg eax, esi
408 div esi ; eax=FAT sector #, edx=entry # in sector
410 imul si, dx, byte 4 ; si=entry # offset in sector
412 cdq
413 add eax, [bx(bpbFatSectorStart)] ; sector # relative to exFAT
414 call ReadSectorFAT ; read 1 exFAT sector, keep edx=0, set C
416 mov esi, [es:si] ; esi=next cluster #
418 mov dl, [bx(bpbSectorPerClusterBits)]
419 xor ecx, ecx
420 bts ecx, edx ; 10000h max (32MB cluster)
421 xchg eax, edi ; get cluster #-2
422 mul ecx
424 add eax, [bx(bpbClusterSectorStart)]
425 ReadSectorC:
426 mov di, bx
427 ReadSectorFAT:
428 adc edx, ebx
430 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
431 ;; Reads a sector using BIOS Int 13h ;;
432 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
433 ;; Input: EDX:EAX = LBA ;;
434 ;; BX = 0 ;;
435 ;; CX = sector count ;;
436 ;; ES:0 -> buffer address ;;
437 ;; Output: BX = 0 ;;
438 ;; CX = next count ;;
439 ;; EBP = bytes/sector ;;
440 ;; ES:0 -> next address ;;
441 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
443 %if SectorOf512Bytes != 0
444 lea ebp, [bx+512]
445 %else
446 lea ebp, [bx+1]
447 %endif
449 pushad
451 add eax, [bx(bpbSectorStart)]
452 adc edx, [bx(bpbSectorStart)+4]
454 push edx
455 push eax
456 push es
457 push bx
458 %if SectorOf512Bytes != 0
459 push byte 1 ; sector count word = 1
460 %else
461 push bp ; sector count word = 1
462 %endif
463 push byte 16 ; packet size byte = 16, reserved byte = 0
464 ReadSectorRetry:
465 mov si, sp
466 mov ah, 42h ; ah = 42h = extended read function no.
467 mov dl, [bx(DriveNumber)] ; restore BIOS boot drive number
468 int 13h ; extended read sectors (DL, DS:SI)
470 jnc ReadSuccess
472 %if ReadRetry != 0
473 xor ax, ax
474 int 13h ; reset drive (DL)
475 dec bp
476 %if SectorOf512Bytes != 0
477 jne ReadSectorRetry ; up to 511 tries
478 %else
479 jpe ReadSectorRetry ; up to 3 tries
480 %endif
481 %endif
483 call Error
484 db "Read error."
486 ReadSuccess:
487 %if SectorOf512Bytes == 0
488 mov cl, [bx(bpbSectorSizeBits)]
489 shl word [si+16+8], cl ; (e)bp si+16: EDI ESI EBP ESP EBX EDX ECX EAX
490 %endif
491 popa ; sp += 16
492 popad ; real registers
493 ret
495 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
496 ;; Fill free space with zeroes ;;
497 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
499 times (512-13-($-$$)) db 0
501 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
502 ;; Name of the file to load and run ;;
503 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
505 ProgramName db "startup.bin" ; name and extension
506 NameLength equ $-ProgramName
508 ;;;;;;;;;;;;;;;;;;;;;;;;;;
509 ;; End of the sector ID ;;
510 ;;;;;;;;;;;;;;;;;;;;;;;;;;
512 dw 0AA55h ; BIOS checks for this ID