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

Typos in BootProg
author Pascal Bellard <pascal.bellard@slitaz.org>
date Fri Feb 18 10:06:45 2022 +0000 (2022-02-18)
parents 89c8d8b6cf48
children d211771a0500
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 ;; ;;
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 Limitations: ;;
28 ;; ~~~~~~~~~~~~~~~~~~ ;;
29 ;; - Works only on the 1st MBR partition which must be a DOS partition ;;
30 ;; with FAT32 (File System ID: 0Bh,0Ch) ;;
31 ;; ;;
32 ;; ;;
33 ;; Known Bugs: ;;
34 ;; ~~~~~~~~~~~ ;;
35 ;; - All bugs are fixed as far as I know. The boot sector has been tested ;;
36 ;; on my HDD and an 8GB USB stick. ;;
37 ;; ;;
38 ;; ;;
39 ;; Memory Layout: ;;
40 ;; ~~~~~~~~~~~~~~ ;;
41 ;; The diagram below shows the typical memory layout. The actual location ;;
42 ;; of the boot sector and its stack may be lower than A0000H if the BIOS ;;
43 ;; reserves memory for its Extended BIOS Data Area just below A0000H and ;;
44 ;; reports less than 640 KB of RAM via its Int 12H function. ;;
45 ;; ;;
46 ;; physical address ;;
47 ;; +------------------------+ 00000H ;;
48 ;; | Interrupt Vector Table | ;;
49 ;; +------------------------+ 00400H ;;
50 ;; | BIOS Data Area | ;;
51 ;; +------------------------+ 00500H ;;
52 ;; | PrtScr Status / Unused | ;;
53 ;; +------------------------+ 00600H ;;
54 ;; | Loaded Image | ;;
55 ;; +------------------------+ nnnnnH ;;
56 ;; | Available Memory | ;;
57 ;; +------------------------+ A0000H - 512 - 2KB ;;
58 ;; | 2KB Boot Stack | ;;
59 ;; +------------------------+ A0000H - 512 ;;
60 ;; | Boot Sector | ;;
61 ;; +------------------------+ A0000H ;;
62 ;; | Video RAM | ;;
63 ;; ;;
64 ;; ;;
65 ;; Boot Image Startup (register values): ;;
66 ;; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ;;
67 ;; dl = BIOS boot drive number (e.g. 80H) ;;
68 ;; cs:ip = program entry point ;;
69 ;; ss:sp = program stack (don't confuse with boot sector's stack) ;;
70 ;; COM program defaults: cs = ds = es = ss = 50h, sp = 0, ip = 100h ;;
71 ;; EXE program defaults: ds = es = EXE data - 10h (fake MS-DOS psp), ;;
72 ;; ax = 0ffffh (both FCB in the PSP don't have a valid drive identifier), ;;
73 ;; cs:ip and ss:sp depends on EXE header ;;
74 ;; Magic numbers: ;;
75 ;; si = 16381 (prime number 2**14-3) ;;
76 ;; di = 32749 (prime number 2**15-19) ;;
77 ;; bp = 65521 (prime number 2**16-15) ;;
78 ;; The magic numbers let the program know whether it has been loaded by ;;
79 ;; this boot sector or by MS-DOS, which may be handy for universal, bare- ;;
80 ;; metal and MS-DOS programs. ;;
81 ;; ;;
82 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
84 [BITS 16]
86 ? equ 0
87 ImageLoadSeg equ 60h ; <=07Fh because of "push byte ImageLoadSeg" instructions
89 [SECTION .text]
90 [ORG 0]
92 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
93 ;; Boot sector starts here ;;
94 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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 ? ; 0x0B
105 bpbSectorsPerCluster DB ? ; 0x0D
106 bpbReservedSectors DW ? ; 0x0E
107 bpbNumberOfFATs DB ? ; 0x10
108 bpbRootEntries DW ? ; 0x11
109 bpbTotalSectors DW ? ; 0x13
110 bpbMedia DB ? ; 0x15
111 bpbSectorsPerFAT DW ? ; 0x16
112 bpbSectorsPerTrack DW ? ; 0x18
113 bpbHeadsPerCylinder DW ? ; 0x1A
114 bpbHiddenSectors DD ? ; 0x1C
115 bpbTotalSectorsBig DD ? ; 0x20
117 ;;;;;;;;;;;;;;;;;;;;
118 ;; BPB1 ends here ;;
119 ;;;;;;;;;;;;;;;;;;;;
121 ;;;;;;;;;;;;;;;;;;;;;;
122 ;; BPB2 starts here ;;
123 ;;;;;;;;;;;;;;;;;;;;;;
125 bsSectorsPerFAT32 DD ? ; 0x24
126 bsExtendedFlags DW ? ; 0x28
127 bsFSVersion DW ? ; 0x2A
128 bsRootDirectoryClusterNo DD ? ; 0x2C
129 bsFSInfoSectorNo DW ? ; 0x30
130 bsBackupBootSectorNo DW ? ; 0x32
131 bsreserved times 12 DB ? ; 0x34
132 bsDriveNumber DB ? ; 0x40
133 bsreserved1 DB ? ; 0x41
134 bsExtendedBootSignature DB ? ; 0x42
135 bsVolumeSerialNumber DD ? ; 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 shl ax, 6 ; and convert it to 16-byte paragraphs
157 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
158 ;; Reserve memory for the boot sector and its stack ;;
159 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
161 sub ax, 512 / 16 ; reserve 512 bytes for the boot sector code
162 mov es, ax ; es:0 -> top - 512
164 sub ax, 2048 / 16 ; reserve 2048 bytes for the stack
165 mov ss, ax ; ss:0 -> top - 512 - 2048
166 mov sp, 2048 ; 2048 bytes for the stack
168 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
169 ;; Copy ourselves to top of memory ;;
170 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
172 mov cx, 256
173 mov si, 7C00h
174 xor di, di
175 mov ds, di
176 rep movsw
178 ;;;;;;;;;;;;;;;;;;;;;;
179 ;; Jump to the copy ;;
180 ;;;;;;;;;;;;;;;;;;;;;;
182 push es
183 push byte main
184 retf
186 main:
187 push cs
188 pop ds
190 mov [bsDriveNumber], dl ; store BIOS boot drive number
192 and byte [bsRootDirectoryClusterNo+3], 0Fh ; mask cluster value
193 mov esi, [bsRootDirectoryClusterNo] ; esi=cluster # of root dir
195 RootDirReadContinue:
196 push byte ImageLoadSeg
197 pop es
198 xor bx, bx
199 push es
200 call ReadCluster ; read one cluster of root dir
201 pop es
202 pushad ; save esi=next cluster # of root dir
203 ; save eax=next sector # of root dir
204 pushf ; save carry="not last cluster" flag
206 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
207 ;; Look for the COM/EXE file to load and run ;;
208 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
210 xor di, di ; es:di -> root entries array
211 mov si, ProgramName ; ds:si -> program name
213 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
214 ;; Looks for a file/dir by its name ;;
215 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
216 ;; Input: DS:SI -> file name (11 chars) ;;
217 ;; ES:DI -> root directory array ;;
218 ;; DX = number of root entries ;;
219 ;; Output: ESI = cluster number ;;
220 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
222 FindName:
223 mov cx, 11
224 FindNameCycle:
225 cmp byte [es:di], ch
226 je ErrFind ; end of root directory (NULL entry found)
227 FindNameNotEnd:
228 pusha
229 repe cmpsb
230 popa
231 je FindNameFound
232 add di, 32
233 dec bp
234 dec bp
235 jnz FindNameCycle ; next root entry
236 popf ; restore carry="not last cluster" flag
237 ; restore eax=next sector # of root dir
238 popad ; restore esi=next cluster # of root dir
239 jc RootDirReadContinue ; continue to the next root dir cluster
240 ErrFind:
241 call Error ; end of root directory (dir end reached)
242 db "File not found."
243 FindNameFound:
244 push word [es:di+14h]
245 push word [es:di+1Ah]
246 pop esi ; esi = cluster no. cx = 0
248 dec dword [es:di+1Ch] ; load ((n - 1)/256)*16 +1 paragraphs
249 imul di, [es:di+1Ch+1], 16 ; file size in paragraphs (full pages)
250 xor cx, cx
252 ;;;;;;;;;;;;;;;;;;;;;;;;;;
253 ;; Load the entire file ;;
254 ;;;;;;;;;;;;;;;;;;;;;;;;;;
256 push es
257 FileReadContinue:
258 push di
259 call ReadCluster ; read one cluster of root dir
260 mov di, es
261 add di, bp
262 mov es, di ; es:bx updated
263 pop di
265 sub di, bp
266 jae FileReadContinue
267 pop bp
269 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
270 ;; Type detection, .COM or .EXE? ;;
271 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
273 mov ds, bp ; bp=ds=seg the file is loaded to
274 add bp, [bx+08h] ; bp = image base
275 mov ax, [bx+06h] ; ax = reloc items
276 mov di, [bx+18h] ; di = reloc table pointer
278 cmp word [bx], 5A4Dh ; "MZ" signature?
280 je RelocateEXE ; yes, it's an EXE program
282 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
283 ;; Setup and run a .COM program ;;
284 ;; Set CS=DS=ES=SP SP=0 IP=100h ;;
285 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
287 mov bp, ImageLoadSeg-10h ; "org 100h" stuff :)
288 mov ss, bp
289 xor sp, sp
290 push bp ; cs, ds and es
291 mov bh, 1 ; ip
292 jmp short Run
294 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
295 ;; Relocate, setup and run a .EXE program ;;
296 ;; Set CS:IP, SS:SP, DS, ES and AX according ;;
297 ;; to wiki.osdev.org/MZ#Initial_Program_State ;;
298 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
300 ReloCycle:
301 add [di+2], bp ; item seg (abs)
302 les si, [di] ; si = item ofs, es = item seg
303 add [es:si], bp ; fixup
304 add di, 4 ; point to next entry
306 RelocateEXE:
307 dec ax ; 32768 max (128KB table)
308 jns ReloCycle ; leave with ax=0ffffh: both FCB in the
309 ; PSP don't have a valid drive identifier
310 les si, [bx+0Eh]
311 add si, bp
312 mov ss, si ; ss for EXE
313 mov sp, es ; sp for EXE
315 lea si, [bp-10h] ; ds and es both point to the segment
316 push si ; containing the PSP structure
318 add bp, [bx+16h] ; cs for EXE
319 mov bx, [bx+14h] ; ip for EXE
320 Run:
321 pop ds
322 push bp
323 push bx
324 push ds
325 pop es
327 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
328 ;; Set the magic numbers so the program knows that it ;;
329 ;; has been loaded by this bootsector and not by MS-DOS ;;
330 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
331 mov si, 16381 ; prime number 2**14-3
332 mov di, 32749 ; prime number 2**15-19
333 mov bp, 65521 ; prime number 2**16-15
335 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
336 ;; All done, transfer control to the program now ;;
337 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
338 retf
340 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
341 ;; Reads a FAT32 cluster ;;
342 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
343 ;; Inout: ES:BX -> buffer ;;
344 ;; ESI = cluster no ;;
345 ;; Output: ESI = next cluster ;;
346 ;; ES:BX -> next addr ;;
347 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
349 ReadCluster:
350 inc cx
351 loop ReadSectorLBA
353 mov ax, [bpbBytesPerSector]
354 push ax
355 shr ax, 2 ; ax=# of FAT32 entries per sector
356 cwde
357 lea ebp, [esi-2] ; esi=cluster #
358 xchg eax, esi
359 cdq
360 div esi ; eax=FAT sector #, edx=entry # in sector
362 imul si, dx, 4 ; si=entry # in sector
363 call ReadSectorLBAabsolute ; read 1 FAT32 sector
365 and byte [es:si+3], 0Fh ; mask cluster value
366 mov esi, [es:si] ; esi=next cluster #
368 xchg eax, ebp
369 movzx ecx, byte [bpbSectorsPerCluster]
370 mul ecx
371 xchg eax, ebp
373 movzx eax, byte [bpbNumberOfFATs]
374 mul dword [bsSectorsPerFAT32]
376 add eax, ebp
378 pop bp ; [bpbBytesPerSector]
379 shr bp, 4 ; bp = paragraphs per sector
381 ReadSectorLBAabsolute:
382 movzx edx, word [bpbReservedSectors]
383 add eax, edx
384 add eax, [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 mov dl, [bsDriveNumber] ; restore BIOS boot drive number
397 pusha
399 push bx
400 push bx ; 32-bit LBA only: up to 2TB disks
401 push eax
402 push es
403 push bx
404 push byte 1 ; sector count word = 1
405 mov cx, 16 ; retry count
406 push cx ; packet size byte = 16, reserved byte = 0
408 ReadSectorRetry:
409 mov ah, 42h ; ah = 42h = extended read function no.
410 mov si, sp
411 push ss
412 pop ds
413 int 13h ; extended read sectors (DL, DS:SI)
414 push cs
415 pop ds
416 jnc ReadSuccess ; CF = 0 if no error
418 xor ax, ax ; ah = 0 = reset function
419 int 13h ; reset drive (DL)
421 loop ReadSectorRetry ; extra attempt
422 call Error
423 db "Read error."
425 ReadSuccess:
427 popa ; sp += 16
429 popa
431 inc eax ; adjust LBA for next sector
433 stc
434 loop ReadSectorNext
436 cmp esi, 0FFFFFF8h ; carry=0 if last cluster, and carry=1 otherwise
438 ReadSectorNext:
439 ret
441 ;;;;;;;;;;;;;;;;;;;;;;;;;;
442 ;; Error Messaging Code ;;
443 ;;;;;;;;;;;;;;;;;;;;;;;;;;
445 Error:
446 pop si
447 puts:
448 mov ah, 0Eh
449 mov bl, 7
450 lodsb
451 int 10h
452 cmp al, '.'
453 jne puts
454 cbw
455 int 16h ; wait for a key...
456 int 19h ; bootstrap
458 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
459 ;; Fill free space with zeroes ;;
460 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
462 times (512-13-($-$$)) db 0
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