wok-current view BootProg/stuff/bootex.asm @ rev 25550

Add apachetop & mysqlsniffer
author Pascal Bellard <pascal.bellard@slitaz.org>
date Mon Apr 03 16:35:42 2023 +0000 (15 months ago)
parents b0069c845544
children 3c71bb4367c3
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 0 ; -11 bytes
84 CheckAttrib equ 0 ; +18 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 StartCluster equ 14h ; 4 bytes
203 FileSize equ 18h ; 8 bytes
205 FindNameCycle:
206 pusha
208 %if CheckAttrib != 0
209 Attributes equ 0Bh ; 1 byte
210 cmp byte [es:di], 85h ; EXFAT_ENTRY_FILE ?
211 jne NotEntryFile
212 mov al, [es:di+Attributes]
213 mov [ProgramName+NameLength+1], al
214 NotEntryFile:
215 %endif
216 %if NullEntryCheck != 0
217 xor ax, ax
218 or al, [es:di]
219 je FindNameFailed
220 cmp al, 0c0h ; EXFAT_ENTRY_FILE_INFO ?
221 %else
222 cmp byte [es:di], 0c0h ; EXFAT_ENTRY_FILE_INFO ?
223 %endif
224 jne NotFileInfo
226 mov bl, 31
227 CopyInfo:
228 mov al, [es:di+bx]
229 mov [bx], al
230 dec bx
231 jnz CopyInfo ; keep BIOS boot drive number
233 NotFileInfo:
234 %if NullEntryCheck != 0
235 mov al, 0c1h ; EXFAT_ENTRY_FILE_NAME ?
236 %else
237 mov ax, 0c1h ; EXFAT_ENTRY_FILE_NAME ?
238 %endif
239 mov cx, NameLength+1
240 mov si, ProgramName ; ds:si -> program name
241 CheckName:
242 scasw ; compare UTF-16
243 lodsb ; with ASCII
244 loope CheckName
245 %if CheckAttrib != 0
246 VolumeLabel equ 8
247 SubDirectory equ 10h
248 jnz SkipFindName
249 test byte [si], VolumeLabel+SubDirectory
250 SkipFindName:
251 %endif
252 je FindNameFound ; cx = 0
253 popa ; restore ax, cx, si, di
255 add di, byte 32
256 cmp di, bp
257 jne FindNameCycle ; next root entry
258 loop RootDirReadContinue ; continue to the next root dir sector
259 cmp esi, byte -10 ; carry=0 if last cluster, and carry=1 otherwise
260 jc RootDirReadContinue ; continue to the next root dir cluster
261 FindNameFailed: ; end of root directory (dir end reached)
262 mov dl, [bx(DriveNumber)] ; restore BIOS boot drive number
263 call Error
264 db "File not found."
265 FindNameFound:
266 mov esi, [bx+StartCluster]
268 ;;;;;;;;;;;;;;;;;;;;;;;;;;
269 ;; Load the entire file ;;
270 ;; Input: ESI = cluster ;;
271 ;; CX = 0 ;;
272 ;;;;;;;;;;;;;;;;;;;;;;;;;;
274 push es
275 %if SectorOf512Bytes == 0
276 xor bp, bp
277 FileReadContinue:
278 shr bp, 4 ; bytes to paragraphs
279 mov di, es
280 add di, bp ; adjust segment for next sector
281 mov es, di ; es:0 updated
282 %else
283 FileReadContinue:
284 %endif
285 call ReadCluster ; read one more sector of the boot file
286 dec cx
287 sub [bx+FileSize], ebp ; max FileSize is < 640KB : check low 32 bits only
288 %if SectorOf512Bytes != 0
289 mov bp, es
290 lea bp, [bp+32]
291 mov es, bp ; es:0 updated
292 %endif
293 ja FileReadContinue
294 mov dx, [bx(DriveNumber)] ; restore BIOS boot drive number
295 xchg ax, di
296 pop bp
298 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
299 ;; Type detection, .COM or .EXE? ;;
300 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
302 mov ds, bp ; bp=ds=seg the file is loaded to
304 add bp, [bx+08h] ; bp = image base
305 mov di, [bx+18h] ; di = reloc table pointer
307 cmp word [bx], 5A4Dh ; "MZ" signature?
308 je RelocateEXE ; yes, it's an EXE program
310 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
311 ;; Setup and run a .COM program ;;
312 ;; Set CS=DS=ES=SS SP=0 IP=100h ;;
313 ;; AX=0ffffh BX=0 DX=drive and ;;
314 ;; cmdline=void ;;
315 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
317 mov di, 100h ; ip
318 mov bp, ImageLoadSeg-10h ; "org 100h" stuff :)
319 mov ss, bp
320 xor sp, sp
321 push bp ; cs, ds and es
322 jmp short Run
324 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
325 ;; Relocate, setup and run a .EXE program ;;
326 ;; Set CS:IP, SS:SP, DS, ES and AX according ;;
327 ;; to wiki.osdev.org/MZ#Initial_Program_State ;;
328 ;; AX=0ffffh BX=0 DX=drive cmdline=void ;;
329 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
331 ReloCycle:
332 add [di+2], bp ; item seg (abs)
333 les si, [di] ; si = item ofs, es = item seg
334 add [es:si], bp ; fixup
335 scasw ; di += 2
336 scasw ; point to next entry
338 RelocateEXE:
339 dec word [bx+06h] ; reloc items, 32768 max (128KB table)
340 jns ReloCycle
342 les si, [bx+0Eh]
343 add si, bp
344 mov ss, si ; ss for EXE
345 mov sp, es ; sp for EXE
347 lea si, [bp-10h] ; ds and es both point to the segment
348 push si ; containing the PSP structure
350 add bp, [bx+16h] ; cs for EXE
351 mov di, [bx+14h] ; ip for EXE
352 Run:
353 pop ds
354 push bp
355 push di
356 push ds
357 pop es
358 mov [80h], ax ; clear cmdline
359 dec ax ; both FCB in the PSP don't have a valid drive identifier
361 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
362 ;; Set the magic numbers so the program knows that it ;;
363 ;; has been loaded by this bootsector and not by MS-DOS ;;
364 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
365 mov si, 16381 ; prime number 2**14-3
366 mov di, 32749 ; prime number 2**15-19
367 mov bp, 65521 ; prime number 2**16-15
369 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
370 ;; All done, transfer control to the program now ;;
371 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
372 retf
374 ;;;;;;;;;;;;;;;;;;;;;;;;;;
375 ;; Error Messaging Code ;;
376 ;;;;;;;;;;;;;;;;;;;;;;;;;;
378 Error:
379 pop si
381 PutStr:
382 mov ah, 0Eh
383 mov bl, 7
384 lodsb
385 int 10h
386 cmp al, "."
387 jne PutStr
389 cbw
390 int 16h ; wait for a key...
391 int 19h ; bootstrap
393 Stop:
394 hlt
395 jmp short Stop
397 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
398 ;; Reads a exFAT cluster ;;
399 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
400 ;; Input: EDX:EAX = LBA ;;
401 ;; CX = sector cnt ;;
402 ;; ESI = cluster no ;;
403 ;; ES:0 -> buffer adrs ;;
404 ;; Output: EDX:EAX = next LBA ;;
405 ;; CX = sector cnt ;;
406 ;; ESI = cluster no ;;
407 ;; EBP = bytes/sector;;
408 ;; Keep: EDI = 0 ;;
409 ;; EBX = 0 ;;
410 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
412 ReadCluster:
413 add eax, byte 1
415 inc cx ; jcxnz
416 loop ReadSectorC
418 mul ebx ; edx:eax = 0
419 %if SectorOf512Bytes != 0
420 mov al, 128
421 %else
422 mov ah, 40h
423 mov cx, [bx(bpbSectorSizeBits)]
424 rol ax, cl ; eax=# of exFAT entries per sector
425 %endif
426 lea edi, [esi-2] ; edi=cluster #-2
427 xchg eax, esi
428 div esi ; eax=FAT sector #, edx=entry # in sector
430 imul si, dx, byte 4 ; si=entry # offset in sector
432 cdq
433 add eax, [bx(bpbFatSectorStart)] ; sector # relative to exFAT
434 call ReadSectorFAT ; read 1 exFAT sector, keep edx=0, set C
436 mov esi, [es:si] ; esi=next cluster #
438 %if SectorOf512Bytes != 0
439 mov dl, [bx(bpbSectorPerClusterBits)]
440 %else
441 mov dl, ch
442 %endif
443 xor ecx, ecx
444 bts ecx, edx ; 10000h max (32MB cluster)
445 xchg eax, edi ; get cluster #-2
446 mul ecx
448 add eax, [bx(bpbClusterSectorStart)]
449 ReadSectorC:
450 mov di, bx
451 ReadSectorFAT:
452 adc edx, ebx
454 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
455 ;; Reads a sector using BIOS Int 13h ;;
456 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
457 ;; Input: EDX:EAX = LBA ;;
458 ;; ES:0 -> buffer address ;;
459 ;; Output: EBP = bytes/sector ;;
460 ;; Keep: ESI = cluster ;;
461 ;; EDI = FAT sector or 0 ;;
462 ;; ECX = sector count ;;
463 ;; EBX = 0 ;;
464 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
466 %if SectorOf512Bytes != 0
467 lea ebp, [bx+512]
468 %else
469 lea ebp, [bx+1]
470 %endif
472 pushad
474 add eax, [bx(bpbSectorStart)]
475 adc edx, [bx(bpbSectorStart)+4]
477 push edx
478 push eax
479 push es
480 push bx
481 %if SectorOf512Bytes != 0
482 push byte 1 ; sector count word = 1
483 %else
484 push bp ; sector count word = 1
485 %endif
486 push byte 16 ; packet size byte = 16, reserved byte = 0
487 ReadSectorRetry:
488 mov si, sp
489 mov ah, 42h ; ah = 42h = extended read function no.
490 mov dl, [bx(DriveNumber)] ; restore BIOS boot drive number
491 int 13h ; extended read sectors (DL, DS:SI)
493 jnc ReadSuccess
495 %if ReadRetry != 0
496 xor ax, ax
497 int 13h ; reset drive (DL)
498 dec bp
499 %if SectorOf512Bytes != 0
500 jne ReadSectorRetry ; up to 511 tries
501 %else
502 jpe ReadSectorRetry ; up to 3 tries
503 %endif
504 %endif
506 call Error
507 db "Read error."
509 ReadSuccess:
510 %if SectorOf512Bytes == 0
511 mov cl, [bx(bpbSectorSizeBits)]
512 shl word [si+16+8], cl ; (e)bp si+16: EDI ESI EBP ESP EBX EDX ECX EAX
513 %endif
514 popa ; sp += 16
515 popad ; real registers
516 ret
518 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
519 ;; Fill free space with zeroes ;;
520 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
522 times (512-13-($-$$)) db 0
524 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
525 ;; Name of the file to load and run ;;
526 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
528 NameLength equ 11
529 ProgramName times NameLength db 0 ; name and extension
531 ;;;;;;;;;;;;;;;;;;;;;;;;;;
532 ;; End of the sector ID ;;
533 ;;;;;;;;;;;;;;;;;;;;;;;;;;
535 dw 0AA55h ; BIOS checks for this ID