wok-6.x view BootProg/stuff/boot32.asm @ rev 24525

Up libav (0.6.6 -> 12.3)
author Pascal Bellard <pascal.bellard@slitaz.org>
date Tue Feb 22 16:15:28 2022 +0000 (2022-02-22)
parents d8c511e24c20
children 7c0170dd3ecc
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 boot32.asm -f bin -o boot32.bin ;;
10 ;; ;;
11 ;; ;;
12 ;; Features: ;;
13 ;; ~~~~~~~~~ ;;
14 ;; - FAT32 supported using BIOS int 13h function 42h or 02h. ;;
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 my HDD and an 8GB USB stick. ;;
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]
82 ImageLoadSeg equ 60h ; <=07Fh because of "push byte ImageLoadSeg" instructions
83 StackSize equ 1536
85 [SECTION .text]
86 [ORG 0]
88 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
89 ;; Boot sector starts here ;;
90 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
92 boot:
93 HiLBA equ boot+0
94 jmp short start ; MS-DOS/Windows checks for this jump
95 nop
96 bsOemName DB "BootProg" ; 0x03
98 ;;;;;;;;;;;;;;;;;;;;;;
99 ;; BPB1 starts here ;;
100 ;;;;;;;;;;;;;;;;;;;;;;
102 bpbBytesPerSector DW 0 ; 0x0B
103 bpbSectorsPerCluster DB 0 ; 0x0D
104 bpbReservedSectors DW 0 ; 0x0E
105 bpbNumberOfFATs DB 0 ; 0x10
106 bpbRootEntries DW 0 ; 0x11
107 bpbTotalSectors DW 0 ; 0x13
108 bpbMedia DB 0 ; 0x15
109 bpbSectorsPerFAT DW 0 ; 0x16
110 bpbSectorsPerTrack DW 0 ; 0x18
111 bpbHeadsPerCylinder DW 0 ; 0x1A
112 bpbHiddenSectors DD 0 ; 0x1C
113 bpbTotalSectorsBig DD 0 ; 0x20
115 ;;;;;;;;;;;;;;;;;;;;
116 ;; BPB1 ends here ;;
117 ;;;;;;;;;;;;;;;;;;;;
119 ;;;;;;;;;;;;;;;;;;;;;;
120 ;; BPB2 starts here ;;
121 ;;;;;;;;;;;;;;;;;;;;;;
123 bsSectorsPerFAT32 DD 0 ; 0x24
124 bsExtendedFlags DW 0 ; 0x28
125 bsFSVersion DW 0 ; 0x2A
126 bsRootDirectoryClusterNo DD 0 ; 0x2C
127 bsFSInfoSectorNo DW 0 ; 0x30
128 bsBackupBootSectorNo DW 0 ; 0x32
129 bsreserved times 12 DB 0 ; 0x34
130 bsDriveNumber DB 0 ; 0x40
131 bsreserved1 DB 0 ; 0x41
132 bsExtendedBootSignature DB 0 ; 0x42
133 bsVolumeSerialNumber DD 0 ; 0x43
134 bsVolumeLabel DB "NO NAME " ; 0x47
135 bsFileSystemName DB "FAT32 " ; 0x52
137 ;;;;;;;;;;;;;;;;;;;;
138 ;; BPB2 ends here ;;
139 ;;;;;;;;;;;;;;;;;;;;
141 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
142 ;; Boot sector code starts here ;;
143 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
145 start:
146 cld
148 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
149 ;; How much RAM is there? ;;
150 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
152 int 12h ; get conventional memory size (in KBs)
153 dec ax
154 dec ax ; reserve 2K bytes for the code and the stack
155 mov cx, 106h
156 shl ax, cl ; and convert it to 16-byte paragraphs
158 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
159 ;; Reserve memory for the boot sector and its stack ;;
160 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
162 mov es, ax ; cs:0 = ds:0 = ss:0 -> top - 512 - StackSize
163 mov ss, ax
164 mov sp, 512+StackSize ; bytes 0-511 are reserved for the boot code
166 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
167 ;; Copy ourselves to top of memory ;;
168 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
170 mov si, 7C00h
171 xor di, di
172 mov ds, di
173 rep movsw
175 ;;;;;;;;;;;;;;;;;;;;;;
176 ;; Jump to the copy ;;
177 ;;;;;;;;;;;;;;;;;;;;;;
179 push es
180 push byte main
181 retf
183 main:
184 push cs
185 pop ds
187 xor bx, bx
188 mov [bx(bsDriveNumber)], dx ; store BIOS boot drive number
190 and byte [bx(bsRootDirectoryClusterNo+3)], 0Fh ; mask cluster value
191 mov esi, [bx(bsRootDirectoryClusterNo)] ; esi=cluster # of root dir
193 RootDirReadContinue:
194 push byte ImageLoadSeg
195 pop es
196 call ReadCluster ; read one cluster of root dir
197 pushf ; save carry="not last cluster" flag
199 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
200 ;; Look for the COM/EXE file to load and run ;;
201 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
203 xor di, di ; es:di -> root entries array
205 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
206 ;; Looks for a file/dir by its name ;;
207 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
208 ;; Input: DS:SI -> file name (11 chars) ;;
209 ;; ES:DI -> root directory array ;;
210 ;; DX = number of root entries ;;
211 ;; BP = paragraphs in sector ;;
212 ;; Output: ESI = cluster number ;;
213 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
215 FindName:
216 FindNameCycle:
217 cmp byte [es:di], bh
218 je ErrFind ; end of root directory (NULL entry found)
219 FindNameNotEnd:
220 pusha
221 mov cl, 11
222 mov si, ProgramName ; ds:si -> program name
223 repe cmpsb
224 je FindNameFound
225 popa
226 add di, byte 32
227 dec bp
228 dec bp
229 jnz FindNameCycle ; next root entry
230 popf ; restore carry="not last cluster" flag
231 jc RootDirReadContinue ; continue to the next root dir cluster
232 ErrFind:
233 call Error ; end of root directory (dir end reached)
234 db "File not found."
235 FindNameFound:
236 push word [es:di+14h-11]
237 push word [es:di+1Ah-11]
238 pop esi ; esi = cluster no. cx = 0
240 dec dword [es:di+1Ch-11] ; load ((n - 1)/256)*16 +1 paragraphs
241 imul di, [es:di+1Ch+1-11], byte 16 ; file size in paragraphs (full pages)
243 ;;;;;;;;;;;;;;;;;;;;;;;;;;
244 ;; Load the entire file ;;
245 ;;;;;;;;;;;;;;;;;;;;;;;;;;
247 push es
248 FileReadContinue:
249 push di
250 call ReadCluster ; read one cluster of root dir
251 mov di, es
252 add di, bp
253 mov es, di ; es:bx updated
254 pop di
256 sub di, bp
257 jae FileReadContinue
258 pop bp
260 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
261 ;; Type detection, .COM or .EXE? ;;
262 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
264 mov ds, bp ; bp=ds=seg the file is loaded to
265 add bp, [bx+08h] ; bp = image base
266 mov ax, [bx+06h] ; ax = reloc items
267 mov di, [bx+18h] ; di = reloc table pointer
269 cmp word [bx], 5A4Dh ; "MZ" signature?
271 je RelocateEXE ; yes, it's an EXE program
273 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
274 ;; Setup and run a .COM program ;;
275 ;; Set CS=DS=ES=SP SP=0 IP=100h ;;
276 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
278 mov bp, ImageLoadSeg-10h ; "org 100h" stuff :)
279 mov ss, bp
280 xor sp, sp
281 push bp ; cs, ds and es
282 mov bh, 1 ; ip
283 jmp short Run
285 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
286 ;; Relocate, setup and run a .EXE program ;;
287 ;; Set CS:IP, SS:SP, DS, ES and AX according ;;
288 ;; to wiki.osdev.org/MZ#Initial_Program_State ;;
289 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
291 ReloCycle:
292 add [di+2], bp ; item seg (abs)
293 les si, [di] ; si = item ofs, es = item seg
294 add [es:si], bp ; fixup
295 scasw ; di += 2
296 scasw ; point to next entry
298 RelocateEXE:
299 dec ax ; 32768 max (128KB table)
300 jns ReloCycle ; leave with ax=0ffffh: both FCB in the
301 ; PSP don't have a valid drive identifier
302 les si, [bx+0Eh]
303 add si, bp
304 mov ss, si ; ss for EXE
305 mov sp, es ; sp for EXE
307 lea si, [bp-10h] ; ds and es both point to the segment
308 push si ; containing the PSP structure
310 add bp, [bx+16h] ; cs for EXE
311 mov bx, [bx+14h] ; ip for EXE
312 Run:
313 pop ds
314 push bp
315 push bx
316 push ds
317 pop es
319 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
320 ;; Set the magic numbers so the program knows that it ;;
321 ;; has been loaded by this bootsector and not by MS-DOS ;;
322 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
323 mov si, 16381 ; prime number 2**14-3
324 mov di, 32749 ; prime number 2**15-19
325 mov bp, 65521 ; prime number 2**16-15
327 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
328 ;; All done, transfer control to the program now ;;
329 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
330 retf
332 ReadCluster:
333 mov bp, [bx(bpbBytesPerSector)]
334 shr bp, 4 ; bp = paragraphs per sector
335 inc cx
336 loop ReadSectorLBA
338 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
339 ;; Reads a FAT32 cluster ;;
340 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
341 ;; Inout: ES:BX -> buffer ;;
342 ;; ESI = cluster no ;;
343 ;; Output: ESI = next cluster ;;
344 ;; BP -> para / sector ;;
345 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
347 imul ax, bp, byte 2 ; ax=# of FAT32 entries per sector
348 cwde
349 lea edi, [esi-2] ; esi=cluster #
350 xchg eax, esi
351 cdq
352 div esi ; eax=FAT sector #, edx=entry # in sector
354 imul si, dx, byte 4 ; si=entry # in sector, clear C
355 mov word [bx(HiLBA)], bx
356 call ReadSectorLBAabsolute ; read 1 FAT32 sector
358 and byte [es:si+3], 0Fh ; mask cluster value
359 mov esi, [es:si] ; esi=next cluster #
361 movzx eax, byte [bx(bpbNumberOfFATs)]
362 mul dword [bx(bsSectorsPerFAT32)]
363 mov word [bx(HiLBA)], dx
365 xchg eax, edi
366 movzx ecx, byte [bx(bpbSectorsPerCluster)]
367 mul ecx ; edx:eax=sector number in data area
369 add eax, edi
370 adc word [bx(HiLBA)], dx
372 ReadSectorLBAabsolute:
373 mov dx, word [bx(bpbReservedSectors)]
374 add eax, edx
375 adc word [bx(HiLBA)], bx
376 add eax, [bx(bpbHiddenSectors)]
377 adc word [bx(HiLBA)], bx
379 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
380 ;; Reads a sector using BIOS Int 13h fn 42h ;;
381 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
382 ;; Input: EAX = LBA ;;
383 ;; CX = sector count ;;
384 ;; ES:BX -> buffer address ;;
385 ;; Output: CF = 0 if no more sectors ;;
386 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
388 ReadSectorLBA:
389 pusha
391 push bx
392 push word [bx(HiLBA)] ; 48-bit LBA
393 push eax
394 push es
395 push bx
396 push byte 1 ; sector count word = 1
397 push byte 16 ; packet size byte = 16, reserved byte = 0
398 push eax
399 pop cx ; low LBA
400 pop ax ; high LBA
401 cwd ; clear dx (CHS disk <2TB)
402 div word [bx(bpbSectorsPerTrack)] ; up to 8GB CHS disks
404 xchg ax, cx ; restore low LBA, save high LBA / SPT
405 div word [bx(bpbSectorsPerTrack)]
406 ; ax = LBA / SPT
407 ; dx = LBA % SPT = sector - 1
408 inc dx
410 xchg cx, dx ; restore high LBA / SPT, save sector no.
411 div word [bx(bpbHeadsPerCylinder)]
412 ; ax = (LBA / SPT) / HPC = cylinder
413 ; dx = (LBA / SPT) % HPC = head
414 shl ah, 6
415 mov ch, al
416 ; ch = LSB 0...7 of cylinder no.
417 or cl, ah
418 ; cl = MSB 8...9 of cylinder no. + sector no.
419 mov dh, dl
420 ; dh = head no.
422 ReadSectorLBARetry:
423 mov dl, [bx(bsDriveNumber)] ; restore BIOS boot drive number
424 mov si, sp
425 mov ah, 42h ; ah = 42h = extended read function no.
426 int 13h ; extended read sectors (DL, DS:SI)
427 jnc ReadSuccess ; CF = 0 if no error
429 ReadSectorCHSRetry:
430 mov ax, 201h ; al = sector count = 1
431 ; ah = 2 = read function no.
432 int 13h ; read sectors (AL, CX, DX, ES:BX)
433 jnc ReadSuccess ; CF = 0 if no error
435 cbw ; ah = 0 = reset function
436 int 13h ; reset drive (DL)
438 dec bp
439 jnz ReadSectorLBARetry
441 call Error
442 db "Read error."
444 ReadSuccess:
446 popa ; sp += 16
448 popa
450 add eax, byte 1 ; adjust LBA for next sector
451 adc word [bx(HiLBA)], bx
453 stc
454 loop ReadSectorNext
456 cmp esi, 0FFFFFF8h ; carry=0 if last cluster, and carry=1 otherwise
458 ReadSectorNext:
459 mov dx, [bx(bsDriveNumber)] ; restore BIOS boot drive number
460 ret
462 ;;;;;;;;;;;;;;;;;;;;;;;;;;
463 ;; Error Messaging Code ;;
464 ;;;;;;;;;;;;;;;;;;;;;;;;;;
466 Error:
467 pop si
468 puts:
469 mov ah, 0Eh
470 mov bl, 7
471 lodsb
472 int 10h
473 cmp al, '.'
474 jne puts
475 cbw
476 ; int 16h ; wait for a key...
477 ; int 19h ; bootstrap
479 Stop:
480 hlt
481 jmp short Stop
483 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
484 ;; Fill free space with zeroes ;;
485 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
487 times (512-13-($-$$)) db 0
489 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
490 ;; Name of the file to load and run ;;
491 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
493 ProgramName db "STARTUP BIN" ; name and extension each must be
494 ; padded with spaces (11 bytes total)
496 ;;;;;;;;;;;;;;;;;;;;;;;;;;
497 ;; End of the sector ID ;;
498 ;;;;;;;;;;;;;;;;;;;;;;;;;;
500 dw 0AA55h ; BIOS checks for this ID