wok view BootProg/stuff/boot32.asm @ rev 24945

fix BootProg
author Pascal Bellard <pascal.bellard@slitaz.org>
date Fri Apr 22 16:00:56 2022 +0000 (2022-04-22)
parents 810e3d0a38f7
children 6023b31d568c
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 (IOW, it will only ;;
15 ;; work with modern BIOSes supporting HDDs bigger than 8 GB) ;;
16 ;; ;;
17 ;; - Loads a 16-bit executable file in the MS-DOS .COM or .EXE format ;;
18 ;; from the root directory of a disk and transfers control to it ;;
19 ;; (the "ProgramName" variable holds the name of the file to be loaded) ;;
20 ;; Its maximum size can be up to 636KB without Extended BIOS Data area. ;;
21 ;; ;;
22 ;; - Prints an error if the file isn't found or couldn't be read ;;
23 ;; ("File not found" or "Read error") ;;
24 ;; and waits for a key to be pressed, then executes the Int 19h ;;
25 ;; instruction and lets the BIOS continue bootstrap. ;;
26 ;; ;;
27 ;; ;;
28 ;; Known Bugs: ;;
29 ;; ~~~~~~~~~~~ ;;
30 ;; - All bugs are fixed as far as I know. The boot sector has been tested ;;
31 ;; on my HDD and an 8GB USB stick. ;;
32 ;; ;;
33 ;; ;;
34 ;; Memory Layout: ;;
35 ;; ~~~~~~~~~~~~~~ ;;
36 ;; The diagram below shows the typical memory layout. The actual location ;;
37 ;; of the boot sector and its stack may be lower than A0000H if the BIOS ;;
38 ;; reserves memory for its Extended BIOS Data Area just below A0000H and ;;
39 ;; reports less than 640 KB of RAM via its Int 12H function. ;;
40 ;; ;;
41 ;; physical address ;;
42 ;; +------------------------+ 00000H ;;
43 ;; | Interrupt Vector Table | ;;
44 ;; +------------------------+ 00400H ;;
45 ;; | BIOS Data Area | ;;
46 ;; +------------------------+ 00500H ;;
47 ;; | PrtScr Status / Unused | ;;
48 ;; +------------------------+ 00600H ;;
49 ;; | Loaded Image | ;;
50 ;; +------------------------+ nnnnnH ;;
51 ;; | Available Memory | ;;
52 ;; +------------------------+ A0000H - 2KB ;;
53 ;; | Boot Sector | ;;
54 ;; +------------------------+ A0000H - 1.5KB ;;
55 ;; | 1.5KB Boot Stack | ;;
56 ;; +------------------------+ A0000H ;;
57 ;; | Video RAM | ;;
58 ;; ;;
59 ;; ;;
60 ;; Boot Image Startup (register values): ;;
61 ;; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ;;
62 ;; ax = 0ffffh (both FCB in the PSP don't have a valid drive identifier), ;;
63 ;; bx = cx = 0, dl = BIOS boot drive number (e.g. 0, 80H) ;;
64 ;; cs:ip = program entry point ;;
65 ;; ss:sp = program stack (don't confuse with boot sector's stack) ;;
66 ;; COM program defaults: cs = ds = es = ss = 50h, sp = 0, ip = 100h ;;
67 ;; EXE program defaults: ds = es = EXE data - 10h (fake MS-DOS psp), ;;
68 ;; cs:ip and ss:sp depends on EXE header ;;
69 ;; Magic numbers: ;;
70 ;; si = 16381 (prime number 2**14-3) ;;
71 ;; di = 32749 (prime number 2**15-19) ;;
72 ;; bp = 65521 (prime number 2**16-15) ;;
73 ;; The magic numbers let the program know whether it has been loaded by ;;
74 ;; this boot sector or by MS-DOS, which may be handy for universal, bare- ;;
75 ;; metal and MS-DOS programs. ;;
76 ;; The command line contains no arguments. ;;
77 ;; ;;
78 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
80 %define bx(label) bx+label-boot
82 [BITS 16]
84 ImageLoadSeg equ 60h ; <=07Fh because of "push byte ImageLoadSeg" instructions
85 StackSize equ 1536
87 [SECTION .text]
88 [ORG 0]
90 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
91 ;; Boot sector starts here ;;
92 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
94 boot:
95 HiLBA equ boot+0
96 jmp short start ; MS-DOS/Windows checks for this jump
97 nop
98 bsOemName DB "BootProg" ; 0x03
100 ;;;;;;;;;;;;;;;;;;;;;;
101 ;; BPB1 starts here ;;
102 ;;;;;;;;;;;;;;;;;;;;;;
104 bpbBytesPerSector DW 0 ; 0x0B
105 bpbSectorsPerCluster DB 0 ; 0x0D
106 bpbReservedSectors DW 0 ; 0x0E
107 bpbNumberOfFATs DB 0 ; 0x10
108 bpbRootEntries DW 0 ; 0x11
109 bpbTotalSectors DW 0 ; 0x13
110 bpbMedia DB 0 ; 0x15
111 bpbSectorsPerFAT DW 0 ; 0x16
112 bpbSectorsPerTrack DW 0 ; 0x18
113 bpbHeadsPerCylinder DW 0 ; 0x1A
114 bpbHiddenSectors DD 0 ; 0x1C
115 bpbTotalSectorsBig DD 0 ; 0x20
117 ;;;;;;;;;;;;;;;;;;;;
118 ;; BPB1 ends here ;;
119 ;;;;;;;;;;;;;;;;;;;;
121 ;;;;;;;;;;;;;;;;;;;;;;
122 ;; BPB2 starts here ;;
123 ;;;;;;;;;;;;;;;;;;;;;;
125 bsSectorsPerFAT32 DD 0 ; 0x24
126 bsExtendedFlags DW 0 ; 0x28
127 bsFSVersion DW 0 ; 0x2A
128 bsRootDirectoryClusterNo DD 0 ; 0x2C
129 bsFSInfoSectorNo DW 0 ; 0x30
130 bsBackupBootSectorNo DW 0 ; 0x32
131 bsreserved times 12 DB 0 ; 0x34
132 bsDriveNumber DB 0 ; 0x40
133 bsreserved1 DB 0 ; 0x41
134 bsExtendedBootSignature DB 0 ; 0x42
135 bsVolumeSerialNumber DD 0 ; 0x43
136 bsVolumeLabel DB "NO NAME " ; 0x47
137 bsFileSystemName DB "FAT32 " ; 0x52
139 ;;;;;;;;;;;;;;;;;;;;
140 ;; BPB2 ends here ;;
141 ;;;;;;;;;;;;;;;;;;;;
143 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
144 ;; Boot sector code starts here ;;
145 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
147 start:
148 cld
150 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
151 ;; How much RAM is there? ;;
152 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
154 int 12h ; get conventional memory size (in KBs)
155 dec ax
156 dec ax ; reserve 2K bytes for the code and the stack
157 mov cx, 106h
158 shl ax, cl ; and convert it to 16-byte paragraphs
160 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
161 ;; Reserve memory for the boot sector and its stack ;;
162 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
164 mov es, ax ; cs:0 = ds:0 = ss:0 -> top - 512 - StackSize
165 mov ss, ax
166 mov sp, 512+StackSize ; bytes 0-511 are reserved for the boot code
168 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
169 ;; Copy ourselves to top of memory ;;
170 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
172 mov si, 7C00h
173 xor di, di
174 mov ds, di
175 rep movsw
177 ;;;;;;;;;;;;;;;;;;;;;;
178 ;; Jump to the copy ;;
179 ;;;;;;;;;;;;;;;;;;;;;;
181 push es
182 push byte main
183 retf
185 main:
186 push cs
187 pop ds
189 xor bx, bx
190 mov [bx(bsDriveNumber)], dx ; store BIOS boot drive number
192 and byte [bx(bsRootDirectoryClusterNo+3)], 0Fh ; mask cluster value
193 mov esi, [bx(bsRootDirectoryClusterNo)] ; esi=cluster # of root dir
195 RootDirReadContinue:
196 push byte ImageLoadSeg
197 pop es
198 call ReadCluster ; read one cluster of root dir
199 pushf ; save carry="not last cluster" flag
201 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
202 ;; Look for the COM/EXE file to load and run ;;
203 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
205 xor di, di ; es:di -> root entries array
207 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
208 ;; Looks for a file/dir by its name ;;
209 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
210 ;; Input: DS:SI -> file name (11 chars) ;;
211 ;; ES:DI -> root directory array ;;
212 ;; DX = number of root entries ;;
213 ;; BP = paragraphs in sector ;;
214 ;; Output: ESI = cluster number ;;
215 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
217 FindName:
218 FindNameCycle:
219 cmp byte [es:di], bh
220 je ErrFind ; end of root directory (NULL entry found)
221 FindNameNotEnd:
222 pusha
223 mov cl, 11
224 mov si, ProgramName ; ds:si -> program name
225 repe cmpsb
226 je FindNameFound
227 popa
228 add di, byte 32
229 dec bp
230 dec bp
231 jnz FindNameCycle ; next root entry
232 popf ; restore carry="not last cluster" flag
233 jc RootDirReadContinue ; continue to the next root dir cluster
234 ErrFind:
235 call Error ; end of root directory (dir end reached)
236 db "File not found."
237 FindNameFound:
238 push word [es:di+14h-11]
239 push word [es:di+1Ah-11]
240 pop esi ; esi = cluster no. cx = 0
242 dec dword [es:di+1Ch-11] ; load ((n - 1)/256)*16 +1 paragraphs
243 imul di, [es:di+1Ch+1-11], byte 16 ; file size in paragraphs (full pages)
245 ;;;;;;;;;;;;;;;;;;;;;;;;;;
246 ;; Load the entire file ;;
247 ;;;;;;;;;;;;;;;;;;;;;;;;;;
249 push es
250 FileReadContinue:
251 push di
252 call ReadCluster ; read one cluster of root dir
253 mov di, es
254 add di, bp
255 mov es, di ; es:bx updated
256 pop di
258 sub di, bp
259 jae FileReadContinue
260 xor ax, ax
261 pop bp
263 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
264 ;; Type detection, .COM or .EXE? ;;
265 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
267 mov ds, bp ; bp=ds=seg the file is loaded to
269 add bp, [bx+08h] ; bp = image base
270 mov di, [bx+18h] ; di = reloc table pointer
272 cmp word [bx], 5A4Dh ; "MZ" signature?
273 je RelocateEXE ; yes, it's an EXE program
275 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
276 ;; Setup and run a .COM program ;;
277 ;; Set CS=DS=ES=SS SP=0 IP=100h ;;
278 ;; AX=0ffffh BX=0 CX=0 DX=drive ;;
279 ;; and cmdline=void ;;
280 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
282 mov di, 100h ; ip
283 mov bp, ImageLoadSeg-10h ; "org 100h" stuff :)
284 mov ss, bp
285 xor sp, sp
286 push bp ; cs, ds and es
287 jmp short Run
289 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
290 ;; Relocate, setup and run a .EXE program ;;
291 ;; Set CS:IP, SS:SP, DS, ES and AX according ;;
292 ;; to wiki.osdev.org/MZ#Initial_Program_State ;;
293 ;; AX=0ffffh BX=0 CX=0 DX=drive cmdline=void ;;
294 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
296 ReloCycle:
297 add [di+2], bp ; item seg (abs)
298 les si, [di] ; si = item ofs, es = item seg
299 add [es:si], bp ; fixup
300 scasw ; di += 2
301 scasw ; point to next entry
303 RelocateEXE:
304 dec word [bx+06h] ; reloc items, 32768 max (128KB table)
305 jns ReloCycle
307 les si, [bx+0Eh]
308 add si, bp
309 mov ss, si ; ss for EXE
310 mov sp, es ; sp for EXE
312 lea si, [bp-10h] ; ds and es both point to the segment
313 push si ; containing the PSP structure
315 add bp, [bx+16h] ; cs for EXE
316 mov di, [bx+14h] ; ip for EXE
317 Run:
318 pop ds
319 push bp
320 push di
321 push ds
322 pop es
323 mov [80h], ax ; clear cmdline
324 dec ax ; both FCB in the PSP don't have a valid drive identifier
326 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
327 ;; Set the magic numbers so the program knows that it ;;
328 ;; has been loaded by this bootsector and not by MS-DOS ;;
329 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
330 mov si, 16381 ; prime number 2**14-3
331 mov di, 32749 ; prime number 2**15-19
332 mov bp, 65521 ; prime number 2**16-15
334 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
335 ;; All done, transfer control to the program now ;;
336 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
337 retf
339 ReadCluster:
340 mov bp, [bx(bpbBytesPerSector)]
341 shr bp, 4 ; bp = paragraphs per sector
342 add eax, byte 1 ; adjust LBA for next sector
343 inc cx
344 loop ReadSectorLBA
346 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
347 ;; Reads a FAT32 cluster ;;
348 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
349 ;; Inout: ES:BX -> buffer ;;
350 ;; ESI = cluster no ;;
351 ;; Output: ESI = next cluster ;;
352 ;; BP -> para / sector ;;
353 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
355 imul ax, bp, byte 4 ; ax=# of FAT32 entries per sector
356 cwde
357 lea edi, [esi-2] ; esi=cluster #
358 xchg eax, esi
359 cdq
360 div esi ; eax=FAT sector #, edx=entry # in sector
362 imul si, dx, byte 4 ; si=entry # in sector, clear C
363 mov word [bx(HiLBA)], bx
364 call ReadSectorLBAabsolute ; read 1 FAT32 sector
366 and byte [es:si+3], 0Fh ; mask cluster value
367 mov esi, [es:si] ; esi=next cluster #
369 movzx eax, byte [bx(bpbNumberOfFATs)]
370 mul dword [bx(bsSectorsPerFAT32)]
371 mov word [bx(HiLBA)], dx
373 xchg eax, edi
374 movzx ecx, byte [bx(bpbSectorsPerCluster)]
375 mul ecx ; edx:eax=sector number in data area
377 add eax, edi
378 adc word [bx(HiLBA)], dx
380 ReadSectorLBAabsolute:
381 mov dx, word [bx(bpbReservedSectors)]
382 add eax, edx
383 adc word [bx(HiLBA)], bx
384 add eax, [bx(bpbHiddenSectors)]
386 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
387 ;; Reads a sector using BIOS Int 13h fn 42h ;;
388 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
389 ;; Input: EAX = LBA ;;
390 ;; CX = sector count ;;
391 ;; ES:BX -> buffer address ;;
392 ;; Output: CF = 0 if no more sectors ;;
393 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
395 ReadSectorLBA:
396 adc word [bx(HiLBA)], bx
397 mov dx, [bx(bsDriveNumber)] ; restore BIOS boot drive number
398 pusha
400 push bx
401 push word [bx(HiLBA)] ; 48-bit LBA
402 push eax
403 push es
404 push bx
405 push byte 1 ; sector count word = 1
406 push byte 16 ; packet size byte = 16, reserved byte = 0
408 ReadSectorLBARetry:
409 mov si, sp
410 mov ah, 42h ; ah = 42h = extended read function no.
411 int 13h ; extended read sectors (DL, DS:SI)
412 jnc ReadSuccess ; CF = 0 if no error
414 xor ax, ax ; ah = 0 = reset function
415 int 13h ; reset drive (DL)
417 dec bp
418 jnz ReadSectorLBARetry
420 call Error
421 db "Read error."
423 ReadSuccess:
425 popa ; sp += 16
427 popa
429 stc
430 loop ReadSectorNext
432 cmp esi, 0FFFFFF8h ; carry=0 if last cluster, and carry=1 otherwise
434 ReadSectorNext:
435 ret
437 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
438 ;; Fill free space with zeroes ;;
439 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
441 times (512-13-20-($-$$)) db 0
443 ;;;;;;;;;;;;;;;;;;;;;;;;;;
444 ;; Error Messaging Code ;;
445 ;;;;;;;;;;;;;;;;;;;;;;;;;;
447 Error:
448 pop si
449 puts:
450 mov ah, 0Eh
451 mov bl, 7
452 lodsb
453 int 10h
454 cmp al, '.'
455 jne puts
456 cbw
457 int 16h ; wait for a key...
458 int 19h ; bootstrap
460 Stop:
461 hlt
462 jmp short Stop
464 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
465 ;; Name of the file to load and run ;;
466 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
468 ProgramName db "STARTUP BIN" ; name and extension each must be
469 ; padded with spaces (11 bytes total)
471 ;;;;;;;;;;;;;;;;;;;;;;;;;;
472 ;; End of the sector ID ;;
473 ;;;;;;;;;;;;;;;;;;;;;;;;;;
475 dw 0AA55h ; BIOS checks for this ID