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

Up memtest (6.20)
author Pascal Bellard <pascal.bellard@slitaz.org>
date Thu May 11 15:58:49 2023 +0000 (12 months ago)
parents f40d97a52c42
children 64471e67874e
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
85 WaitForKey equ 0 ; +5 bytes
86 TfatSupport equ 1 ; +10 bytes
87 CheckLBAsupport equ 0 ; +11/21 bytes
89 [BITS 16]
90 [CPU 386]
92 ImageLoadSeg equ 60h
93 StackSize equ 512
95 [SECTION .text]
96 [ORG 0]
98 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
99 ;; Boot sector starts here ;;
100 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
102 boot:
103 DriveNumber:
104 jmp short start ; Windows checks for this jump
105 nop
106 bsOemName times 8 db " " ; 0x03 "EXFAT "
107 times 53 db 0 ; 0x0B
109 ;;;;;;;;;;;;;;;;;;;;;
110 ;; BPB starts here ;;
111 ;;;;;;;;;;;;;;;;;;;;;
113 bpbSectorStart DQ 0 ; 0x40 partition first sector
114 bpbSectorCount DQ 0 ; 0x48 partition sectors count
115 bpbFatSectorStart DD 0 ; 0x50 FAT first sector
116 bpbFatSectorCount DD 0 ; 0x54 FAT sectors count
117 bpbClusterSectorStart DD 0 ; 0x58 first cluster sector
118 bpbClusterCount DD 0 ; 0x5C total clusters count
119 bpbRootDirCluster DD 0 ; 0x60 first cluster of the root dir
120 bpbVolumeSerial DD 0 ; 0x64 volume serial number
121 bpbFSVersionMinor DB 0 ; 0x68
122 bpbFSVersionMajor DB 0 ; 0x69
123 bpbVolumeStateFlags DW 0 ; 0x6A bit0 = fat used, bit1 = dirty, bit2 = media error
124 bpbSectorSizeBits DB 0 ; 0x6C sector size as (1 << n)
125 bpbSectorPerClusterBits DB 0 ; 0x6D sector per cluster as (1 << n)
126 bpbNumberOfFATs DB 0 ; 0x6E always 1 or 2 (Tfat case)
127 bpbDriveNumber DB 0 ; 0x6F always 0x80
128 bpbAllocatedPercent DB 0 ; 0x70 percentage of allocated space
130 ;;;;;;;;;;;;;;;;;;;
131 ;; BPB ends here ;;
132 ;;;;;;;;;;;;;;;;;;;
134 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
135 ;; Boot sector code starts here ;;
136 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
138 start:
139 cld
141 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
142 ;; How much RAM is there? ;;
143 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
145 int 12h ; get conventional memory size (in KBs)
146 mov cx, 106h
147 dec ax ; reserve 1K bytes for the code and the stack
148 shl ax, cl ; and convert it to 16-byte paragraphs
150 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
151 ;; Reserve memory for the boot sector and its stack ;;
152 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
154 mov es, ax ; cs:0 = ds:0 = ss:0 -> top - 512 - StackSize
155 mov ss, ax
156 mov sp, 512+StackSize ; bytes 0-511 are reserved for the boot code
158 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
159 ;; Copy ourselves to top of memory ;;
160 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
162 mov si, 7C00h
163 xor di, di
164 mov ds, di
165 push es
166 mov [si(DriveNumber)], dx ; store BIOS boot drive number
167 rep movsw ; move 512 bytes (+ 12)
169 ;;;;;;;;;;;;;;;;;;;;;;
170 ;; Jump to the copy ;;
171 ;;;;;;;;;;;;;;;;;;;;;;
173 push word main
174 retf
176 %if CheckLBAsupport != 0
177 %macro BootFileName 0
178 main:
179 mov ah, 41h ; clobbers AX, BX, CX, DH
180 mov bx, 55AAh
181 int 13h
182 jc ReadError
183 ; xor bx, 0AA55h
184 ; jnz ReadError
185 ; shr cx, 1 ; function 42h support ?
186 ; jnc ReadError
187 xor cx, cx
188 %else
189 main:
190 %endif
191 push cs
192 pop ds
194 xor ebx, ebx
196 mov esi, [bx(bpbRootDirCluster)] ; esi=cluster # of root dir
198 push byte ImageLoadSeg
199 pop es ; cx = 0
201 RootDirReadContinue:
202 call ReadCluster ; read one sector of root dir
204 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
205 ;; Look for the COM/EXE file to load and run ;;
206 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
208 ; es:di -> root entries array
210 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
211 ;; Looks for the file/dir ProgramName ;;
212 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
213 ;; Input: ES:DI -> root directory array ;;
214 ;; Output: ESI = cluster number ;;
215 ;; dword [bx+FileSize] file size ;;
216 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
218 CurNameSize equ 03h ; 1 byte
219 StartCluster equ 14h ; 4 bytes
220 FileSize equ 18h ; 8 bytes
222 FindNameCycle:
223 pusha
225 %if NullEntryCheck != 0
226 xor ax, ax
227 or al, [es:di]
228 je FindNameFailed
229 %define curtag al
230 %define chktag al
231 %else
232 %define curtag byte [es:di]
233 %define chktag ax
234 %endif
235 cmp curtag, 0c0h ; EXFAT_ENTRY_FILE_INFO ?
236 jne NotFileInfo
238 mov bl, 32
239 CopyInfo:
240 mov [bx], al
241 dec bx
242 mov al, [es:di+bx]
243 jnz CopyInfo ; keep BIOS boot drive number
245 NotFileInfo:
246 %if CheckAttrib != 0
247 Attributes equ 0Bh ; 1 byte
248 cmp curtag, 85h ; EXFAT_ENTRY_FILE ?
249 jne NotEntryFile
250 mov al, [es:di+Attributes]
251 mov [ProgramName+NameLength+1], al
252 NotEntryFile:
253 %endif
254 mov chktag, 0c1h ; EXFAT_ENTRY_FILE_NAME ?
255 mov cx, NameLength+1
256 mov si, ProgramName ; ds:si -> program name
257 CheckName:
258 scasw ; compare UTF-16
259 lodsb ; with ASCII
260 loope CheckName
261 VolumeLabel equ 8
262 SubDirectory equ 10h
263 %if CheckAttrib != 0
264 jnz SkipFindName
265 test byte [si], VolumeLabel+SubDirectory
266 SkipFindName:
267 %endif
268 je FindNameFound ; cx = 0
269 popa ; restore ax, cx, si, di
271 add di, byte 32
272 cmp di, bp
273 jne FindNameCycle ; next root entry
274 loop RootDirReadContinue ; continue to the next root dir sector
275 cmp esi, byte -10 ; carry=0 if last cluster, and carry=1 otherwise
276 jc RootDirReadContinue ; continue to the next root dir cluster
277 FindNameFailed: ; end of root directory (dir end reached)
278 mov dl, [bx(DriveNumber)] ; restore BIOS boot drive number
279 call Error
280 db "File not found."
281 FindNameFound:
282 mov esi, [bx+StartCluster]
284 ;;;;;;;;;;;;;;;;;;;;;;;;;;
285 ;; Load the entire file ;;
286 ;; Input: ESI = cluster ;;
287 ;; CX = 0 ;;
288 ;;;;;;;;;;;;;;;;;;;;;;;;;;
290 push es
291 %if SectorOf512Bytes == 0
292 xor bp, bp
293 FileReadContinue:
294 shr bp, 4 ; bytes to paragraphs
295 mov di, es
296 add di, bp ; adjust segment for next sector
297 mov es, di ; es:0 updated
298 %else
299 FileReadContinue:
300 %endif
301 call ReadCluster ; read one more sector of the boot file
302 dec cx
303 sub [bx+FileSize], ebp ; max FileSize is < 640KB : check low 32 bits only
304 %if SectorOf512Bytes != 0
305 mov bp, es
306 lea bp, [bp+32]
307 mov es, bp ; es:0 updated
308 %endif
309 ja FileReadContinue
310 mov dx, [bx(DriveNumber)] ; restore BIOS boot drive number
311 xchg ax, di
312 pop bp
314 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
315 ;; Type detection, .COM or .EXE? ;;
316 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
318 mov ds, bp ; bp=ds=seg the file is loaded to
320 add bp, [bx+08h] ; bp = image base
321 mov di, [bx+18h] ; di = reloc table pointer
323 cmp word [bx], 5A4Dh ; "MZ" signature?
324 je RelocateEXE ; yes, it's an EXE program
326 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
327 ;; Setup and run a .COM program ;;
328 ;; Set CS=DS=ES=SS SP=0 IP=100h ;;
329 ;; AX=0ffffh BX=0 DX=drive and ;;
330 ;; cmdline=void ;;
331 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
333 mov di, 100h ; ip
334 mov bp, ImageLoadSeg-10h ; "org 100h" stuff :)
335 mov ss, bp
336 xor sp, sp
337 push bp ; cs, ds and es
338 jmp short Run
340 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
341 ;; Relocate, setup and run a .EXE program ;;
342 ;; Set CS:IP, SS:SP, DS, ES and AX according ;;
343 ;; to wiki.osdev.org/MZ#Initial_Program_State ;;
344 ;; AX=0ffffh BX=0 DX=drive cmdline=void ;;
345 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
347 ReloCycle:
348 add [di+2], bp ; item seg (abs)
349 les si, [di] ; si = item ofs, es = item seg
350 add [es:si], bp ; fixup
351 scasw ; di += 2
352 scasw ; point to next entry
354 RelocateEXE:
355 dec word [bx+06h] ; reloc items, 32768 max (128KB table)
356 jns ReloCycle
358 les si, [bx+0Eh]
359 add si, bp
360 mov ss, si ; ss for EXE
361 mov sp, es ; sp for EXE
363 lea si, [bp-10h] ; ds and es both point to the segment
364 push si ; containing the PSP structure
366 add bp, [bx+16h] ; cs for EXE
367 mov di, [bx+14h] ; ip for EXE
368 Run:
369 pop ds
370 push bp
371 push di
372 push ds
373 pop es
374 mov [80h], ax ; clear cmdline
375 dec ax ; both FCB in the PSP don't have a valid drive identifier
377 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
378 ;; Set the magic numbers so the program knows that it ;;
379 ;; has been loaded by this bootsector and not by MS-DOS ;;
380 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
381 mov si, 16381 ; prime number 2**14-3
382 mov di, 32749 ; prime number 2**15-19
383 mov bp, 65521 ; prime number 2**16-15
385 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
386 ;; All done, transfer control to the program now ;;
387 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
388 retf
389 %if CheckLBAsupport != 0
390 %endm
391 %endif
393 ;;;;;;;;;;;;;;;;;;;;;;;;;;
394 ;; Error Messaging Code ;;
395 ;;;;;;;;;;;;;;;;;;;;;;;;;;
397 Error:
398 pop si
400 PutStr:
401 mov ah, 0Eh
402 mov bl, 7
403 lodsb
404 int 10h
405 cmp al, "."
406 jne PutStr
407 %if WaitForKey != 0
408 cbw
409 int 16h ; wait for a key...
410 int 19h ; bootstrap
411 %endif
412 Stop:
413 hlt
414 jmp short Stop
416 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
417 ;; Reads a exFAT cluster ;;
418 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
419 ;; Input: EDX:EAX = LBA ;;
420 ;; EBX = 0 ;;
421 ;; CX = sector cnt ;;
422 ;; ESI = cluster no ;;
423 ;; ES:0 -> buffer adrs ;;
424 ;; Output: EDX:EAX = next LBA ;;
425 ;; CX = sector cnt ;;
426 ;; ESI = cluster no ;;
427 ;; EBP = bytes/sector;;
428 ;; Keep: EDI = 0 ;;
429 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
431 ReadCluster:
432 add eax, byte 1
434 inc cx ; jcxnz
435 loop ReadSectorC
437 mul ebx ; edx:eax = 0
438 %if SectorOf512Bytes != 0
439 mov al, 128
440 %define SectorPerClusterBits [bx(bpbSectorPerClusterBits)]
441 %else
442 mov ah, 40h
443 mov cx, [bx(bpbSectorSizeBits)]
444 %define SectorPerClusterBits ch
445 rol ax, cl ; eax=# of exFAT entries per sector
446 %endif
447 lea edi, [esi-2] ; edi=cluster #-2
448 xchg eax, esi
449 div esi ; eax=FAT sector #, edx=entry # in sector
451 imul si, dx, byte 4 ; si=entry # offset in sector
453 cdq
454 add eax, [bx(bpbFatSectorStart)] ; sector # relative to exFAT
455 %if TfatSupport
456 test byte [bx(bpbVolumeStateFlags)], 1
457 jz UseFat0
458 add eax, [bx(bpbFatSectorCount)]
459 UseFat0:
460 %endif
461 call ReadSectorFAT ; read 1 exFAT sector, keep edx=0, set C
463 mov esi, [es:si] ; esi=next cluster #
465 mov dl, SectorPerClusterBits
466 xor ecx, ecx
467 bts ecx, edx ; 10000h max (32MB cluster)
468 xchg eax, edi ; get cluster #-2
469 mul ecx
471 add eax, [bx(bpbClusterSectorStart)]
472 ReadSectorC:
473 mov di, bx
474 ReadSectorFAT:
475 adc edx, ebx
477 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
478 ;; Reads a sector using BIOS Int 13h ;;
479 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
480 ;; Input: EDX:EAX = LBA ;;
481 ;; EBX = 0 ;;
482 ;; ES:0 -> buffer address ;;
483 ;; Output: EBP = bytes/sector ;;
484 ;; Keep: ESI = cluster ;;
485 ;; EDI = FAT sector or 0 ;;
486 ;; ECX = sector count ;;
487 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
489 %if SectorOf512Bytes != 0
490 lea ebp, [bx+512]
491 %else
492 lea ebp, [bx+1]
493 %endif
495 pushad
497 add eax, [bx(bpbSectorStart)]
498 adc edx, [bx(bpbSectorStart)+4]
500 push edx
501 push eax
502 push es
503 push bx
504 %if SectorOf512Bytes != 0
505 push byte 1 ; sector count word = 1
506 %else
507 push bp ; sector count word = 1
508 %endif
509 push byte 16 ; packet size byte = 16, reserved byte = 0
510 ReadSectorRetry:
511 mov si, sp
512 mov ah, 42h ; ah = 42h = extended read function no.
513 mov dl, [bx(DriveNumber)] ; restore BIOS boot drive number
514 int 13h ; extended read sectors (DL, DS:SI)
516 jnc ReadSuccess
518 %if ReadRetry != 0
519 xor ax, ax
520 int 13h ; reset drive (DL)
521 dec bp
522 %if SectorOf512Bytes != 0
523 jne ReadSectorRetry ; up to 511 tries
524 %else
525 jpe ReadSectorRetry ; up to 3 tries
526 %endif
527 %endif
529 ReadError:
530 call Error
531 db "Read error."
533 ReadSuccess:
534 %if SectorOf512Bytes == 0
535 mov cl, [bx(bpbSectorSizeBits)]
536 shl word [si+16+8], cl ; (e)bp si+16: EDI ESI EBP ESP EBX EDX ECX EAX
537 %endif
538 popa ; sp += 16
539 popad ; real registers
540 ret
542 %if CheckLBAsupport != 0
543 BootFileName
544 %endif
546 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
547 ;; Fill free space with zeroes ;;
548 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
550 times (512-13-($-$$)) db 0
552 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
553 ;; Name of the file to load and run ;;
554 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
556 NameLength equ 11
557 ProgramName times NameLength db 0 ; name and extension
559 ;;;;;;;;;;;;;;;;;;;;;;;;;;
560 ;; End of the sector ID ;;
561 ;;;;;;;;;;;;;;;;;;;;;;;;;;
563 dw 0AA55h ; BIOS checks for this ID