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

Update some web_site
author Pascal Bellard <pascal.bellard@slitaz.org>
date Tue Sep 27 16:21:52 2022 +0000 (19 months ago)
parents ad0bc3efbf37
children 7dd01dedad38
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 0 ; +5 bytes
84 ReadRetry equ 1 ; +7 bytes
85 LBA48bits equ 1 ; +15 bytes
86 CHSsupport equ 1 ; +27 bytes
87 CHSupTo8GB equ 1 ; +11 bytes
88 CHSupTo32MB equ 1 ; +7 bytes
89 SectorOf512Bytes equ 1 ; -5 bytes
90 Always2FATs equ 0 ; -4 bytes
92 [BITS 16]
94 ImageLoadSeg equ 60h ; <=07Fh because of "push byte ImageLoadSeg" instructions
95 StackSize equ 1536
97 [SECTION .text]
98 [ORG 0]
100 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
101 ;; Boot sector starts here ;;
102 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
104 boot:
105 DriveNumber equ boot+0
106 HiLBA equ boot+2
107 jmp short start ; MS-DOS/Windows checks for this jump
108 nop
109 bsOemName DB "BootProg" ; 0x03
111 ;;;;;;;;;;;;;;;;;;;;;;
112 ;; BPB1 starts here ;;
113 ;;;;;;;;;;;;;;;;;;;;;;
115 bpbBytesPerSector DW 0 ; 0x0B
116 bpbSectorsPerCluster DB 0 ; 0x0D
117 bpbReservedSectors DW 0 ; 0x0E
118 bpbNumberOfFATs DB 0 ; 0x10
119 bpbRootEntries DW 0 ; 0x11
120 bpbTotalSectors DW 0 ; 0x13
121 bpbMedia DB 0 ; 0x15
122 bpbSectorsPerFAT DW 0 ; 0x16
123 bpbSectorsPerTrack DW 0 ; 0x18
124 bpbHeadsPerCylinder DW 0 ; 0x1A
125 bpbHiddenSectors DD 0 ; 0x1C
126 bpbTotalSectorsBig DD 0 ; 0x20
128 ;;;;;;;;;;;;;;;;;;;;
129 ;; BPB1 ends here ;;
130 ;;;;;;;;;;;;;;;;;;;;
132 ;;;;;;;;;;;;;;;;;;;;;;
133 ;; BPB2 starts here ;;
134 ;;;;;;;;;;;;;;;;;;;;;;
136 bsSectorsPerFAT32 DD 0 ; 0x24
137 bsExtendedFlags DW 0 ; 0x28
138 bsFSVersion DW 0 ; 0x2A
139 bsRootDirectoryClusterNo DD 0 ; 0x2C
140 bsFSInfoSectorNo DW 0 ; 0x30
141 bsBackupBootSectorNo DW 0 ; 0x32
142 bsreserved times 12 DB 0 ; 0x34
143 bsDriveNumber DB 0 ; 0x40
144 bsreserved1 DB 0 ; 0x41
145 bsExtendedBootSignature DB 0 ; 0x42
146 bsVolumeSerialNumber DD 0 ; 0x43
147 bsVolumeLabel times 11 DB " " ; 0x47 "NO NAME "
148 bsFileSystemName times 8 DB " " ; 0x52 "FAT32 "
150 ;;;;;;;;;;;;;;;;;;;;
151 ;; BPB2 ends here ;;
152 ;;;;;;;;;;;;;;;;;;;;
154 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
155 ;; Boot sector code starts here ;;
156 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
158 start:
159 cld
161 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
162 ;; How much RAM is there? ;;
163 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
165 int 12h ; get conventional memory size (in KBs)
166 dec ax
167 dec ax ; reserve 2K bytes for the code and the stack
168 mov cx, 106h
169 shl ax, cl ; and convert it to 16-byte paragraphs
171 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
172 ;; Reserve memory for the boot sector and its stack ;;
173 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
175 mov es, ax ; cs:0 = ds:0 = ss:0 -> top - 512 - StackSize
176 mov ss, ax
177 mov sp, 512+StackSize ; bytes 0-511 are reserved for the boot code
179 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
180 ;; Copy ourselves to top of memory ;;
181 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
183 mov si, 7C00h
184 xor di, di
185 mov ds, di
186 push es
187 mov [si(DriveNumber)], dx ; store BIOS boot drive number
188 rep movsw
190 ;;;;;;;;;;;;;;;;;;;;;;
191 ;; Jump to the copy ;;
192 ;;;;;;;;;;;;;;;;;;;;;;
194 push byte main
195 retf
197 main:
198 push cs
199 pop ds
201 xor ebx, ebx
203 %if ClusterMask != 0
204 and byte [bx(bsRootDirectoryClusterNo+3)], 0Fh ; mask cluster value
205 %endif
206 mov esi, [bx(bsRootDirectoryClusterNo)] ; esi=cluster # of root dir
208 push byte ImageLoadSeg
209 pop es
211 RootDirReadContinue:
212 call ReadClusterSector ; read one sector of the root dir
214 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
215 ;; Look for the COM/EXE file to load and run ;;
216 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
218 xor di, di ; es:di -> root entries array
220 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
221 ;; Looks for a file/dir by its name ;;
222 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
223 ;; Input: DS:SI -> file name (11 chars) ;;
224 ;; ES:DI -> root directory array ;;
225 ;; BP = paragraphs in sector ;;
226 ;; Output: ESI = cluster number ;;
227 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
229 FindNameCycle:
230 %if NullEntryCheck != 0
231 cmp byte [es:di], bh
232 je ErrFind ; end of root directory (NULL entry found)
233 %endif
234 pusha
235 mov cl, NameLength
236 mov si, ProgramName ; ds:si -> program name
237 repe cmpsb
238 je FindNameFound
239 popa
240 add di, byte 32
241 dec bp
242 dec bp
243 jnz FindNameCycle ; next root entry
244 loop RootDirReadContinue ; next sector in cluster
245 cmp esi, 0FFFFFF6h ; carry=0 if last cluster, and carry=1 otherwise
246 jnc RootDirReadContinue ; continue to the next root dir cluster
247 ErrFind:
248 call Error ; end of root directory (dir end reached)
249 db "File not found."
250 FindNameFound:
251 push word [es:di+14h-11]
252 push word [es:di+1Ah-11]
253 pop esi ; esi = cluster no. cx = 0
255 dec dword [es:di+1Ch-11] ; load ((n - 1)/256)*16 +1 paragraphs
256 imul di, [es:di+1Ch+1-11], byte 16 ; file size in paragraphs (full pages)
258 ;;;;;;;;;;;;;;;;;;;;;;;;;;
259 ;; Load the entire file ;;
260 ;;;;;;;;;;;;;;;;;;;;;;;;;;
262 push es
263 FileReadContinue:
264 push di
265 call ReadClusterSector ; read one sector of the boot file
266 dec cx
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;
354 ;; Error Messaging Code ;;
355 ;;;;;;;;;;;;;;;;;;;;;;;;;;
357 Error:
358 pop si
359 puts:
360 mov ah, 0Eh
361 mov bl, 7
362 lodsb
363 int 10h
364 cmp al, '.'
365 jne puts
366 cbw
367 int 16h ; wait for a key...
368 int 19h ; bootstrap
370 Stop:
371 hlt
372 jmp short Stop
374 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
375 ;; Reads a FAT32 sector ;;
376 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
377 ;; Inout: ES:BX -> buffer ;;
378 ;; EAX = prev sector ;;
379 ;; CX = rem sectors in cluster ;;
380 ;; ESI = next cluster ;;
381 ;; Output: EAX = current sector ;;
382 ;; CX = rem sectors in cluster ;;
383 ;; ESI = next cluster ;;
384 ;; BP -> para / sector ;;
385 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
387 ReadClusterSector:
388 %if SectorOf512Bytes != 0
389 mov bp, 32 ; bp = paragraphs per sector
390 %else
391 mov bp, [bx(bpbBytesPerSector)]
392 shr bp, 4 ; bp = paragraphs per sector
393 %endif
394 mov dx, 1 ; adjust LBA for next sector
395 inc cx
396 loop ReadSectorLBA
398 mul ebx ; edx:eax = 0
399 %if SectorOf512Bytes != 0
400 mov al, 128 ; ax=# of FAT32 entries per sector
401 %else
402 imul ax, bp, byte 4 ; ax=# of FAT32 entries per sector
403 %endif
404 lea edi, [esi-2] ; esi=cluster #
405 xchg eax, esi
406 div esi ; eax=FAT sector #, edx=entry # in sector
408 imul si, dx, byte 4 ; si=entry # in sector, clear C
409 %if LBA48bits != 0
410 xor dx, dx
411 %endif
412 call ReadSectorLBAfromFAT ; read 1 FAT32 sector
414 %if ClusterMask != 0
415 and byte [es:si+3], 0Fh ; mask cluster value
416 %endif
417 mov esi, [es:si] ; esi=next cluster #
419 %if Always2FATs != 0
420 imul eax, dword [bx(bsSectorsPerFAT32)], 2
421 %else
422 movzx eax, byte [bx(bpbNumberOfFATs)]
423 mul dword [bx(bsSectorsPerFAT32)]
424 %endif
426 xchg eax, edi
427 movzx ecx, byte [bx(bpbSectorsPerCluster)] ; 8..128
428 mul ecx ; edx:eax=sector number in data area
429 add eax, edi
430 %if LBA48bits != 0
431 adc dx, bx
432 %endif
434 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
435 ;; Reads a sector form the start of FAT ;;
436 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
438 ReadSectorLBAfromFAT:
439 add eax, [bx(bpbHiddenSectors)]
440 %if LBA48bits != 0
441 adc dx, bx
442 mov word [bx(HiLBA)], dx
443 %endif
444 mov dx, [bx(bpbReservedSectors)]
446 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
447 ;; Reads a sector using BIOS Int 13h fn 42h ;;
448 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
449 ;; Input: EAX = LBA ;;
450 ;; CX = sector count ;;
451 ;; ES:BX -> buffer address ;;
452 ;; Output: CF = 0 if no more sectors ;;
453 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
455 ReadSectorLBA:
456 add eax, edx
457 %if LBA48bits != 0
458 adc word [bx(HiLBA)], bx
459 %endif
460 mov dx, [bx(DriveNumber)] ; restore BIOS boot drive number
461 pusha
463 push bx
464 %if LBA48bits != 0
465 push word [bx(HiLBA)] ; 48-bit LBA
466 %else
467 push bx
468 %endif
469 push eax
470 push es
471 push bx
472 push byte 1 ; sector count word = 1
473 push byte 16 ; packet size byte = 16, reserved byte = 0
475 %if CHSsupport != 0
476 %if CHSupTo8GB != 0
477 push eax
478 pop cx ; save low LBA
479 pop ax ; get high LBA
480 cwd ; clear dx (assume LBA offset <1TB)
481 idiv word [bx(bpbSectorsPerTrack)] ; up to 8GB disks, avoid divide error
483 xchg ax, cx ; restore low LBA, save high LBA / SPT
484 %else
485 ; Busybox mkdosfs creates fat32 for floppies.
486 ; Floppies may support CHS only.
487 %if CHSupTo32MB != 0
488 xor dx, dx ; clear dx (LBA offset <32MB)
489 %else
490 cwd ; clear dx (LBA offset <16MB)
491 %endif
492 xor cx, cx ; high LBA / SPT = 0
493 %endif
494 idiv word [bx(bpbSectorsPerTrack)]
495 ; ax = LBA / SPT
496 ; dx = LBA % SPT = sector - 1
497 inc dx
499 xchg cx, dx ; restore high LBA / SPT, save sector no.
500 idiv word [bx(bpbHeadsPerCylinder)]
501 ; ax = (LBA / SPT) / HPC = cylinder
502 ; dx = (LBA / SPT) % HPC = head
504 xchg ch, al ; clear al
505 ; ch = LSB 0...7 of cylinder no.
506 %if CHSupTo8GB != 0 || CHSupTo32MB != 0
507 shr ax, 2
508 or cl, al
509 ; cl = MSB 8...9 of cylinder no. + sector no.
510 %endif
511 mov dh, dl
512 ; dh = head no.
513 mov dl, [bx(DriveNumber)] ; restore BIOS boot drive number
514 %endif
516 ReadSectorRetry:
517 mov si, sp
518 mov ah, 42h ; ah = 42h = extended read function no.
519 int 13h ; extended read sectors (DL, DS:SI)
520 jnc ReadSuccess ; CF = 0 if no error
522 %if CHSsupport != 0
523 mov ax, 201h ; al = sector count = 1
524 ; ah = 2 = read function no.
525 int 13h ; read sectors (AL, CX, DX, ES:BX)
527 jnc ReadSuccess ; CF = 0 if no error
528 %endif
529 %if ReadRetry != 0
530 %if CHSsupport != 0
531 cbw ; ah = 0 = reset function
532 %else
533 xor ax, ax ; ah = 0 = reset function
534 %endif
535 int 13h ; reset drive (DL)
537 dec bp ; up to 32 retries
538 jnz ReadSectorRetry
539 %endif
541 call Error
542 db "Read error."
544 ReadSuccess:
546 popa ; sp += 16
547 popa
548 ret
550 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
551 ;; Fill free space with zeroes ;;
552 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
554 times (512-13-($-$$)) db 0
556 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
557 ;; Name of the file to load and run ;;
558 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
560 ProgramName db "STARTUP BIN" ; name and extension each must be
561 times (510-($-$$)) db ' ' ; padded with spaces (11 bytes total)
562 NameLength equ $-ProgramName
564 ;;;;;;;;;;;;;;;;;;;;;;;;;;
565 ;; End of the sector ID ;;
566 ;;;;;;;;;;;;;;;;;;;;;;;;;;
568 dw 0AA55h ; BIOS checks for this ID