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

Normazile https://sourceforge.net/projects web_sites
author Pascal Bellard <pascal.bellard@slitaz.org>
date Fri Sep 23 08:28:09 2022 +0000 (19 months ago)
parents 78727b04c002
children 9a714ac859a3
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 = 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
81 %define si(label) si+label-boot
82 ClusterMask equ 1 ; +9 bytes
83 NullEntryCheck equ 1 ; +5 bytes
84 ReadRetry equ 1 ; +7 bytes
85 LBA48bits equ 1 ; +13 bytes
86 CHSsupport equ 1 ; +27 bytes
87 CHShardDisk equ 0 ; +11 bytes
89 [BITS 16]
91 ImageLoadSeg equ 60h ; <=07Fh because of "push byte ImageLoadSeg" instructions
92 StackSize equ 1536
94 [SECTION .text]
95 [ORG 0]
97 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
98 ;; Boot sector starts here ;;
99 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
101 boot:
102 jmp short start ; MS-DOS/Windows checks for this jump
103 nop
104 bsOemName DB "BootProg" ; 0x03
106 ;;;;;;;;;;;;;;;;;;;;;;
107 ;; BPB1 starts here ;;
108 ;;;;;;;;;;;;;;;;;;;;;;
110 bpbBytesPerSector DW 0 ; 0x0B
111 bpbSectorsPerCluster DB 0 ; 0x0D
112 bpbReservedSectors DW 0 ; 0x0E
113 bpbNumberOfFATs DB 0 ; 0x10
114 bpbRootEntries DW 0 ; 0x11
115 bpbTotalSectors DW 0 ; 0x13
116 bpbMedia DB 0 ; 0x15
117 bpbSectorsPerFAT DW 0 ; 0x16
118 bpbSectorsPerTrack DW 0 ; 0x18
119 bpbHeadsPerCylinder DW 0 ; 0x1A
120 bpbHiddenSectors DD 0 ; 0x1C
121 bpbTotalSectorsBig DD 0 ; 0x20
123 ;;;;;;;;;;;;;;;;;;;;
124 ;; BPB1 ends here ;;
125 ;;;;;;;;;;;;;;;;;;;;
127 ;;;;;;;;;;;;;;;;;;;;;;
128 ;; BPB2 starts here ;;
129 ;;;;;;;;;;;;;;;;;;;;;;
131 bsSectorsPerFAT32 DD 0 ; 0x24
132 bsExtendedFlags DW 0 ; 0x28
133 bsFSVersion DW 0 ; 0x2A
134 bsRootDirectoryClusterNo DD 0 ; 0x2C
135 bsFSInfoSectorNo DW 0 ; 0x30
136 bsBackupBootSectorNo DW 0 ; 0x32
137 bsreserved times 12 DB 0 ; 0x34
138 bsDriveNumber DB 0 ; 0x40
139 %if LBA48bits != 0
140 HiLBA equ boot+0
141 DriveNumber equ bsDriveNumber+0
142 %else
143 DriveNumber equ boot+0
144 %endif
145 bsreserved1 DB 0 ; 0x41
146 bsExtendedBootSignature DB 0 ; 0x42
147 bsVolumeSerialNumber DD 0 ; 0x43
148 bsVolumeLabel DB "NO NAME " ; 0x47
149 bsFileSystemName DB "FAT32 " ; 0x52
151 ;;;;;;;;;;;;;;;;;;;;
152 ;; BPB2 ends here ;;
153 ;;;;;;;;;;;;;;;;;;;;
155 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
156 ;; Boot sector code starts here ;;
157 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
159 start:
160 cld
162 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
163 ;; How much RAM is there? ;;
164 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
166 int 12h ; get conventional memory size (in KBs)
167 dec ax
168 dec ax ; reserve 2K bytes for the code and the stack
169 mov cx, 106h
170 shl ax, cl ; and convert it to 16-byte paragraphs
172 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
173 ;; Reserve memory for the boot sector and its stack ;;
174 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
176 mov es, ax ; cs:0 = ds:0 = ss:0 -> top - 512 - StackSize
177 mov ss, ax
178 mov sp, 512+StackSize ; bytes 0-511 are reserved for the boot code
180 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
181 ;; Copy ourselves to top of memory ;;
182 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
184 mov si, 7C00h
185 xor di, di
186 mov ds, di
187 push es
188 mov [si(DriveNumber)], dx ; store BIOS boot drive number
189 rep movsw
191 ;;;;;;;;;;;;;;;;;;;;;;
192 ;; Jump to the copy ;;
193 ;;;;;;;;;;;;;;;;;;;;;;
195 push byte main
196 retf
198 main:
199 push cs
200 pop ds
202 xor ebx, ebx
204 %if ClusterMask != 0
205 and byte [bx(bsRootDirectoryClusterNo+3)], 0Fh ; mask cluster value
206 %endif
207 mov esi, [bx(bsRootDirectoryClusterNo)] ; esi=cluster # of root dir
209 push byte ImageLoadSeg
210 pop es
212 RootDirReadContinue:
213 call ReadCluster ; read one cluster of root dir
214 pushf ; save carry="not last cluster" flag
216 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
217 ;; Look for the COM/EXE file to load and run ;;
218 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
220 xor di, di ; es:di -> root entries array
222 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
223 ;; Looks for a file/dir by its name ;;
224 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
225 ;; Input: DS:SI -> file name (11 chars) ;;
226 ;; ES:DI -> root directory array ;;
227 ;; BP = paragraphs in sector ;;
228 ;; Output: ESI = cluster number ;;
229 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
231 FindNameCycle:
232 %if NullEntryCheck != 0
233 cmp byte [es:di], bh
234 je ErrFind ; end of root directory (NULL entry found)
235 %endif
236 pusha
237 mov cl, NameLength
238 mov si, ProgramName ; ds:si -> program name
239 repe cmpsb
240 je FindNameFound
241 popa
242 add di, byte 32
243 dec bp
244 dec bp
245 jnz FindNameCycle ; next root entry
246 popf ; restore carry="not last cluster" flag
247 jc RootDirReadContinue ; continue to the next root dir cluster
248 ErrFind:
249 call Error ; end of root directory (dir end reached)
250 db "File not found."
251 FindNameFound:
252 push word [es:di+14h-11]
253 push word [es:di+1Ah-11]
254 pop esi ; esi = cluster no. cx = 0
256 dec dword [es:di+1Ch-11] ; load ((n - 1)/256)*16 +1 paragraphs
257 imul di, [es:di+1Ch+1-11], byte 16 ; file size in paragraphs (full pages)
259 ;;;;;;;;;;;;;;;;;;;;;;;;;;
260 ;; Load the entire file ;;
261 ;;;;;;;;;;;;;;;;;;;;;;;;;;
263 push es
264 FileReadContinue:
265 push di
266 call ReadCluster ; read one cluster of root dir
267 mov di, es
268 add di, bp
269 mov es, di ; es:bx updated
270 pop di
272 sub di, bp
273 jae FileReadContinue
274 xor ax, ax
275 pop bp
277 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
278 ;; Type detection, .COM or .EXE? ;;
279 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
281 mov ds, bp ; bp=ds=seg the file is loaded to
283 add bp, [bx+08h] ; bp = image base
284 mov di, [bx+18h] ; di = reloc table pointer
286 cmp word [bx], 5A4Dh ; "MZ" signature?
287 je RelocateEXE ; yes, it's an EXE program
289 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
290 ;; Setup and run a .COM program ;;
291 ;; Set CS=DS=ES=SS SP=0 IP=100h ;;
292 ;; AX=0ffffh BX=0 DX=drive and ;;
293 ;; cmdline=void ;;
294 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
296 mov di, 100h ; ip
297 mov bp, ImageLoadSeg-10h ; "org 100h" stuff :)
298 mov ss, bp
299 xor sp, sp
300 push bp ; cs, ds and es
301 jmp short Run
303 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
304 ;; Relocate, setup and run a .EXE program ;;
305 ;; Set CS:IP, SS:SP, DS, ES and AX according ;;
306 ;; to wiki.osdev.org/MZ#Initial_Program_State ;;
307 ;; AX=0ffffh BX=0 DX=drive cmdline=void ;;
308 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
310 ReloCycle:
311 add [di+2], bp ; item seg (abs)
312 les si, [di] ; si = item ofs, es = item seg
313 add [es:si], bp ; fixup
314 scasw ; di += 2
315 scasw ; point to next entry
317 RelocateEXE:
318 dec word [bx+06h] ; reloc items, 32768 max (128KB table)
319 jns ReloCycle
321 les si, [bx+0Eh]
322 add si, bp
323 mov ss, si ; ss for EXE
324 mov sp, es ; sp for EXE
326 lea si, [bp-10h] ; ds and es both point to the segment
327 push si ; containing the PSP structure
329 add bp, [bx+16h] ; cs for EXE
330 mov di, [bx+14h] ; ip for EXE
331 Run:
332 pop ds
333 push bp
334 push di
335 push ds
336 pop es
337 mov [80h], ax ; clear cmdline
338 dec ax ; both FCB in the PSP don't have a valid drive identifier
340 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
341 ;; Set the magic numbers so the program knows that it ;;
342 ;; has been loaded by this bootsector and not by MS-DOS ;;
343 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
344 mov si, 16381 ; prime number 2**14-3
345 mov di, 32749 ; prime number 2**15-19
346 mov bp, 65521 ; prime number 2**16-15
348 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
349 ;; All done, transfer control to the program now ;;
350 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
351 retf
353 ReadCluster:
354 mov bp, [bx(bpbBytesPerSector)]
355 shr bp, 4 ; bp = paragraphs per sector
356 mov dx, 1 ; adjust LBA for next sector
357 inc cx
358 loop ReadSectorLBA
360 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
361 ;; Reads a FAT32 cluster ;;
362 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
363 ;; Inout: ES:BX -> buffer ;;
364 ;; ESI = cluster no ;;
365 ;; Output: ESI = next cluster ;;
366 ;; BP -> para / sector ;;
367 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
369 mul ebx ; edx:eax = 0
370 imul ax, bp, byte 4 ; ax=# of FAT32 entries per sector
371 lea edi, [esi-2] ; esi=cluster #
372 xchg eax, esi
373 div esi ; eax=FAT sector #, edx=entry # in sector
375 imul si, dx, byte 4 ; si=entry # in sector, clear C
376 %if LBA48bits != 0
377 xor dx, dx ; clear C
378 %endif
379 call ReadSectorLBAabsolute ; read 1 FAT32 sector
381 %if ClusterMask != 0
382 and byte [es:si+3], 0Fh ; mask cluster value
383 %endif
384 mov esi, [es:si] ; esi=next cluster #
386 movzx eax, byte [bx(bpbNumberOfFATs)]
387 mul dword [bx(bsSectorsPerFAT32)]
389 xchg eax, edi
390 movzx ecx, byte [bx(bpbSectorsPerCluster)] ; 8..128
391 mul ecx ; edx:eax=sector number in data area
392 add eax, edi
394 ReadSectorLBAabsolute:
395 %if LBA48bits != 0
396 adc dx, bx
397 mov word [bx(HiLBA)], dx
398 %endif
399 add eax, [bx(bpbHiddenSectors)]
400 %if LBA48bits != 0
401 adc word [bx(HiLBA)], bx
402 %endif
403 mov dx, [bx(bpbReservedSectors)]
405 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
406 ;; Reads a sector using BIOS Int 13h fn 42h ;;
407 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
408 ;; Input: EAX = LBA ;;
409 ;; CX = sector count ;;
410 ;; ES:BX -> buffer address ;;
411 ;; Output: CF = 0 if no more sectors ;;
412 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
414 ReadSectorLBA:
415 add eax, edx
416 %if LBA48bits != 0
417 adc word [bx(HiLBA)], bx
418 %endif
419 mov dx, [bx(DriveNumber)] ; restore BIOS boot drive number
420 pusha
422 push bx
423 %if LBA48bits != 0
424 push word [bx(HiLBA)] ; 48-bit LBA
425 %else
426 push bx
427 %endif
428 push eax
429 push es
430 push bx
431 push byte 1 ; sector count word = 1
432 push byte 16 ; packet size byte = 16, reserved byte = 0
434 %if CHSsupport != 0
435 %if CHShardDisk != 0
436 push eax
437 pop cx ; save low LBA
438 pop ax ; get high LBA
439 cwd ; clear dx (assume LBA offset <1TB)
440 idiv word [bx(bpbSectorsPerTrack)] ; up to 8GB disks, avoid divide error
442 xchg ax, cx ; restore low LBA, save high LBA / SPT
443 %else
444 ; Busybox mkdosfs creates fat32 for floppies.
445 ; Floppies may support CHS only.
446 cwd ; clear dx (LBA offset <16MB)
447 xor cx, cx ; high LBA / SPT = 0
448 %endif
449 idiv word [bx(bpbSectorsPerTrack)]
450 ; ax = LBA / SPT
451 ; dx = LBA % SPT = sector - 1
452 inc dx
454 xchg cx, dx ; restore high LBA / SPT, save sector no.
455 idiv word [bx(bpbHeadsPerCylinder)]
456 ; ax = (LBA / SPT) / HPC = cylinder
457 ; dx = (LBA / SPT) % HPC = head
459 mov ch, al
460 ; ch = LSB 0...7 of cylinder no.
461 %if CHShardDisk != 0
462 shl ah, 6
463 or cl, ah
464 ; cl = MSB 8...9 of cylinder no. + sector no.
465 %endif
466 mov dh, dl
467 ; dh = head no.
468 mov dl, [bx(DriveNumber)] ; restore BIOS boot drive number
469 %endif
471 ReadSectorRetry:
472 mov si, sp
473 mov ah, 42h ; ah = 42h = extended read function no.
474 int 13h ; extended read sectors (DL, DS:SI)
475 jnc ReadSuccess ; CF = 0 if no error
477 %if CHSsupport != 0
478 mov ax, 201h ; al = sector count = 1
479 ; ah = 2 = read function no.
480 int 13h ; read sectors (AL, CX, DX, ES:BX)
482 jnc ReadSuccess ; CF = 0 if no error
483 %endif
484 %if ReadRetry != 0
485 %if CHSsupport != 0
486 cbw ; ah = 0 = reset function
487 %else
488 xor ax, ax ; ah = 0 = reset function
489 %endif
490 int 13h ; reset drive (DL)
492 dec bp ; up to 32 retries
493 jnz ReadSectorRetry
494 %endif
496 call Error
497 db "Read error."
499 ReadSuccess:
501 popa ; sp += 16
503 popa
505 stc
506 loop ReadSectorNext
508 cmp esi, 0FFFFFF6h ; carry=0 if last cluster, and carry=1 otherwise
510 ReadSectorNext:
511 ret
513 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
514 ;; Fill free space with zeroes ;;
515 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
517 times (512-13-20-($-$$)) db 0
519 ;;;;;;;;;;;;;;;;;;;;;;;;;;
520 ;; Error Messaging Code ;;
521 ;;;;;;;;;;;;;;;;;;;;;;;;;;
523 Error:
524 pop si
525 puts:
526 mov ah, 0Eh
527 mov bl, 7
528 lodsb
529 int 10h
530 cmp al, '.'
531 jne puts
532 cbw
533 int 16h ; wait for a key...
534 int 19h ; bootstrap
536 Stop:
537 hlt
538 jmp short Stop
540 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
541 ;; Name of the file to load and run ;;
542 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
544 ProgramName db "STARTUP BIN" ; name and extension each must be
545 times (510-($-$$)) db ' ' ; padded with spaces (11 bytes total)
546 NameLength equ $-ProgramName
548 ;;;;;;;;;;;;;;;;;;;;;;;;;;
549 ;; End of the sector ID ;;
550 ;;;;;;;;;;;;;;;;;;;;;;;;;;
552 dw 0AA55h ; BIOS checks for this ID