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

Add some current_version
author Pascal Bellard <pascal.bellard@slitaz.org>
date Fri Feb 18 22:59:06 2022 +0000 (2022-02-18)
parents d211771a0500
children d77ab883a8b3
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 ;; dl = BIOS boot drive number (e.g. 80H) ;;
62 ;; cs:ip = program entry point ;;
63 ;; ss:sp = program stack (don't confuse with boot sector's stack) ;;
64 ;; COM program defaults: cs = ds = es = ss = 50h, sp = 0, ip = 100h ;;
65 ;; EXE program defaults: ds = es = EXE data - 10h (fake MS-DOS psp), ;;
66 ;; ax = 0ffffh (both FCB in the PSP don't have a valid drive identifier), ;;
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 ;; ;;
76 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
78 %define bx(label) bx+label-boot
80 [BITS 16]
81 [CPU 386]
83 ImageLoadSeg equ 60h
84 StackSize equ 1536
86 [SECTION .text]
87 [ORG 0]
89 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
90 ;; Boot sector starts here ;;
91 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
93 boot:
94 jmp short start ; Windows checks for this jump
95 nop
96 bsOemName DB "EXFAT " ; 0x03
97 times 53 db 0 ; 0x0B
99 ;;;;;;;;;;;;;;;;;;;;;
100 ;; BPB starts here ;;
101 ;;;;;;;;;;;;;;;;;;;;;
103 bpbSectorStart DQ 0 ; 0x40 partition first sector
104 bpbSectorCount DQ 0 ; 0x48 partition sectors count
105 bpbFatSectorStart DD 0 ; 0x50 FAT first sector
106 bpbFatSectorCount DD 0 ; 0x54 FAT sectors count
107 bpbClusterSectorStart DD 0 ; 0x58 first cluster sector
108 bpbClusterCount DD 0 ; 0x5C total clusters count
109 bpbRootDirCluster DD 0 ; 0x60 first cluster of the root dir
110 bpbVolumeSerial DD 0 ; 0x64 volume serial number
111 bpbFSVersionMinor DB 0 ; 0x68
112 bpbFSVersionMajor DB 0 ; 0x69
113 bpbVolumeStateFlags DW 0 ; 0x6A
114 bpbSectorSizeBits DB 0 ; 0x6C sector size as (1 << n)
115 bpbSectorPerClusterBits DB 0 ; 0x6D sector per cluster as (1 << n)
116 bpbNumberOfFATs DB 0 ; 0x6E always 1
117 bpbDriveNumber DB 0 ; 0x6F alaways 0x80
118 bpbAllocatedPercent DB 0 ; 0x70 percentage of allocated space
120 ;;;;;;;;;;;;;;;;;;;
121 ;; BPB ends here ;;
122 ;;;;;;;;;;;;;;;;;;;
124 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
125 ;; Boot sector code starts here ;;
126 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
128 start:
129 cld
131 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
132 ;; How much RAM is there? ;;
133 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
135 int 12h ; get conventional memory size (in KBs)
136 mov cx, 106h
137 dec ax
138 dec ax ; reserve 2K bytes for the code and the stack
139 shl ax, cl ; and convert it to 16-byte paragraphs
141 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
142 ;; Reserve memory for the boot sector and its stack ;;
143 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
145 mov es, ax ; cs:0 = ds:0 = ss:0 -> top - 512 - StackSize
146 mov ss, ax
147 mov sp, 512+StackSize ; bytes 0-511 are reserved for the boot code
149 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
150 ;; Copy ourselves to top of memory ;;
151 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
153 mov si, 7C00h
154 xor di, di
155 mov ds, di
156 rep movsw ; move 512 bytes (+ 12)
158 ;;;;;;;;;;;;;;;;;;;;;;
159 ;; Jump to the copy ;;
160 ;;;;;;;;;;;;;;;;;;;;;;
162 push es
163 push byte main
164 retf
166 main:
167 push cs
168 pop ds
170 xor ebx, ebx
171 mov [bx], dx ; store BIOS boot drive number
173 mov esi, [bx(bpbRootDirCluster)] ; esi=cluster # of root dir
175 push byte ImageLoadSeg
176 pop es
178 RootDirReadContinue:
179 call ReadCluster ; read one sector of root dir
180 pushf ; save carry="not last sector" flag
182 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
183 ;; Look for the COM/EXE file to load and run ;;
184 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
186 xor di, di ; es:di -> root entries array
188 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
189 ;; Looks for the file/dir ProgramName ;;
190 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
191 ;; Input: ES:DI -> root directory array ;;
192 ;; Output: ESI = cluster number ;;
193 ;; dword [bx+FileSize] file size ;;
194 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
196 CurNameSize equ 3
197 StartCluster equ 14h
198 FileSize equ 18h
200 FindNameCycle:
201 pusha
203 xor ax, ax
204 or al, [es:di]
205 je FindNameFailed
207 cmp al, 0c0h ; EXFAT_ENTRY_FILE_INFO ?
208 jne NotFileInfo
210 mov bl, 30
211 CopyInfo:
212 mov ax, [es:di+bx]
213 mov [bx], ax
214 dec bx
215 dec bx
216 jnz CopyInfo
218 NotFileInfo:
219 mov al, 0c1h ; EXFAT_ENTRY_FILE_NAME ?
220 mov cx, NameLength+1
221 mov si, ProgramName ; ds:si -> program name
222 CheckName:
223 scasw ; compare UTF-16
224 lodsb ; with ASCII
225 loope CheckName
226 je FindNameFound ; cx = 0
227 popa ; restore ax, cx, si, di
229 add di, byte 32
230 cmp di, bp
231 jne FindNameCycle ; next root entry
232 popf ; restore carry="not last sector" flag
233 jc RootDirReadContinue ; continue to the next root dir cluster
234 FindNameFailed: ; end of root directory (dir end reached)
235 call Error
236 db "File not found."
237 FindNameFound:
238 mov esi, [bx+StartCluster]
240 ;;;;;;;;;;;;;;;;;;;;;;;;;;
241 ;; Load the entire file ;;
242 ;;;;;;;;;;;;;;;;;;;;;;;;;;
244 push es
245 xor bp, bp
246 FileReadContinue:
247 shr bp, 4 ; bytes to paragraphs
248 mov di, es
249 add di, bp ; adjust segment for next sector
250 mov es, di ; es:0 updated
251 call ReadCluster ; read one cluster of root dir
252 sub [bx+FileSize], ebp
253 ja FileReadContinue
254 pop bp
256 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
257 ;; Type detection, .COM or .EXE? ;;
258 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
260 mov ds, bp ; bp=ds=seg the file is loaded to
262 add bp, [bx+08h] ; bp = image base
263 mov ax, [bx+06h] ; ax = reloc items
264 mov di, [bx+18h] ; di = reloc table pointer
266 cmp word [bx], 5A4Dh ; "MZ" signature?
267 je RelocateEXE ; yes, it's an EXE program
269 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
270 ;; Setup and run a .COM program ;;
271 ;; Set CS=DS=ES=SS SP=0 IP=100h ;;
272 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
274 mov bp, ImageLoadSeg-10h ; "org 100h" stuff :)
275 mov ss, bp
276 xor sp, sp
277 push bp ; cs, ds and es
278 mov bh, 1 ; ip
279 jmp short Run
281 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
282 ;; Relocate, setup and run a .EXE program ;;
283 ;; Set CS:IP, SS:SP, DS, ES and AX according ;;
284 ;; to wiki.osdev.org/MZ#Initial_Program_State ;;
285 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
287 ReloCycle:
288 add [di+2], bp ; item seg (abs)
289 les si, [di] ; si = item ofs, es = item seg
290 add [es:si], bp ; fixup
291 scasw ; di += 2
292 scasw ; point to next entry
294 RelocateEXE:
295 dec ax ; 32768 max (128KB table)
296 jns ReloCycle ; leave with ax=0ffffh: both FCB in the
297 ; PSP don't have a valid drive identifier
298 les si, [bx+0Eh]
299 add si, bp
300 mov ss, si ; ss for EXE
301 mov sp, es ; sp for EXE
303 lea si, [bp-10h] ; ds and es both point to the segment
304 push si ; containing the PSP structure
306 add bp, [bx+16h] ; cs for EXE
307 mov bx, [bx+14h] ; ip for EXE
308 Run:
309 pop ds
310 push bp
311 push bx
312 push ds
313 pop es
315 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
316 ;; Set the magic numbers so the program knows that it ;;
317 ;; has been loaded by this bootsector and not by MS-DOS ;;
318 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
319 mov si, 16381 ; prime number 2**14-3
320 mov di, 32749 ; prime number 2**15-19
321 mov bp, 65521 ; prime number 2**16-15
323 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
324 ;; All done, transfer control to the program now ;;
325 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
326 retf
328 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
329 ;; Reads a exFAT cluster ;;
330 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
331 ;; Input: EDX:EAX = LBA ;;
332 ;; EBX = 0 ;;
333 ;; CX = sector cnt ;;
334 ;; ESI = cluster no ;;
335 ;; ES:0 -> buffer adrs ;;
336 ;; Output: EBX = 0 ;;
337 ;; CX = next cnt ;;
338 ;; EBP = bytes/sector;;
339 ;; ES:0 -> next adrs ;;
340 ;; C=0 for last sector ;;
341 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
343 ReadCluster:
344 inc cx ; jcxnz
345 add eax, byte 1
346 loop ReadSectorC
348 mov cl, [bx(bpbSectorSizeBits)]
349 dec cx
350 dec cx
351 mul ebx ; edx:eax = 0
352 inc ax
353 shl eax, cl ; eax=# of exFAT entries per sector
354 lea edi, [esi-2] ; edi=cluster #-2
355 xchg eax, esi
356 div esi ; eax=FAT sector #, edx=entry # in sector
358 imul si, dx, 4 ; si=entry # offset in sector
360 cdq
361 add eax, [bx(bpbFatSectorStart)] ; sector # relative to FAT32
362 call ReadSectorC ; read 1 FAT32 sector
364 mov esi, [es:si] ; esi=next cluster #
366 xor eax, eax
367 inc ax
368 mov cl, [bx(bpbSectorPerClusterBits)]
369 shl eax, cl ; 10000h max (32MB cluster)
370 xchg eax, ecx
371 xchg eax, edi ; get cluster #-2
372 mul ecx
374 add eax, [bx(bpbClusterSectorStart)]
375 ReadSectorC:
376 adc edx, ebx
378 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
379 ;; Reads a sector using BIOS Int 13h ;;
380 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
381 ;; Input: EDX:EAX = LBA ;;
382 ;; BX = 0 ;;
383 ;; CX = sector count ;;
384 ;; ES:0 -> buffer address ;;
385 ;; Output: BX = 0 ;;
386 ;; CX = next count ;;
387 ;; EBP = bytes/sector ;;
388 ;; ES:0 -> next address ;;
389 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
391 ReadSector:
393 xor ebp, ebp
394 inc bp
396 pushad
398 add eax, [bx(bpbSectorStart)]
399 adc edx, [bx(bpbSectorStart)+4]
401 push edx
402 push eax
403 push es
404 push bx
405 push bp ; sector count word = 1
406 mov cx, 16
407 push cx ; packet size byte = 16, reserved byte = 0
408 ReadSectorRetry:
409 mov si, sp
410 mov ah, 42h ; ah = 42h = extended read function no.
411 mov dl, [bx]
412 int 13h ; extended read sectors (DL, DS:SI)
414 jnc ReadSuccess
416 xor ax, ax
417 int 13h ; reset drive (DL)
418 loop ReadSectorRetry
420 call Error
421 db "Read error."
423 ReadSuccess:
424 mov cl, [bx(bpbSectorSizeBits)]
425 shl word [si+16+8], cl ; (e)bp si+16: EDI ESI EBP ESP EBX EDX ECX EAX
426 popa ; sp += 16
427 popad ; real registers
429 stc
430 loop ReadSectorNext
432 cmp esi, byte -10 ; carry=0 if last cluster, and carry=1 otherwise
433 ReadSectorNext:
434 mov dl, [bx] ; restore BIOS boot drive number
435 ret
437 ;;;;;;;;;;;;;;;;;;;;;;;;;;
438 ;; Error Messaging Code ;;
439 ;;;;;;;;;;;;;;;;;;;;;;;;;;
441 Error:
442 pop si
444 PutStr:
445 mov ah, 0Eh
446 mov bl, 7
447 lodsb
448 int 10h
449 cmp al, "."
450 jne PutStr
452 cbw
453 int 16h ; wait for a key...
454 int 19h ; bootstrap
456 Stop:
457 hlt
458 jmp short Stop
460 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
461 ;; Fill free space with zeroes ;;
462 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
464 times (512-13-($-$$)) db 0
466 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
467 ;; Name of the file to load and run ;;
468 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
470 ProgramName db "startup.bin" ; name and extension
471 NameLength equ $-ProgramName
473 ;;;;;;;;;;;;;;;;;;;;;;;;;;
474 ;; End of the sector ID ;;
475 ;;;;;;;;;;;;;;;;;;;;;;;;;;
477 dw 0AA55h ; BIOS checks for this ID