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

BootProg: nasm 2.15.05 support
author Pascal Bellard <pascal.bellard@slitaz.org>
date Fri Feb 18 11:32:41 2022 +0000 (2022-02-18)
parents 04472f031354
children d8c511e24c20
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 ImageLoadSeg equ 60h ; <=07Fh because of "push byte ImageLoadSeg" instructions
88 [SECTION .text]
89 [ORG 0]
91 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
92 ;; Boot sector starts here ;;
93 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
95 jmp short start ; MS-DOS/Windows checks for this jump
96 nop
97 bsOemName DB "BootProg" ; 0x03
99 ;;;;;;;;;;;;;;;;;;;;;;
100 ;; BPB1 starts here ;;
101 ;;;;;;;;;;;;;;;;;;;;;;
103 bpbBytesPerSector DW 0 ; 0x0B
104 bpbSectorsPerCluster DB 0 ; 0x0D
105 bpbReservedSectors DW 0 ; 0x0E
106 bpbNumberOfFATs DB 0 ; 0x10
107 bpbRootEntries DW 0 ; 0x11
108 bpbTotalSectors DW 0 ; 0x13
109 bpbMedia DB 0 ; 0x15
110 bpbSectorsPerFAT DW 0 ; 0x16
111 bpbSectorsPerTrack DW 0 ; 0x18
112 bpbHeadsPerCylinder DW 0 ; 0x1A
113 bpbHiddenSectors DD 0 ; 0x1C
114 bpbTotalSectorsBig DD 0 ; 0x20
116 ;;;;;;;;;;;;;;;;;;;;
117 ;; BPB1 ends here ;;
118 ;;;;;;;;;;;;;;;;;;;;
120 ;;;;;;;;;;;;;;;;;;;;;;
121 ;; BPB2 starts here ;;
122 ;;;;;;;;;;;;;;;;;;;;;;
124 bsSectorsPerFAT32 DD 0 ; 0x24
125 bsExtendedFlags DW 0 ; 0x28
126 bsFSVersion DW 0 ; 0x2A
127 bsRootDirectoryClusterNo DD 0 ; 0x2C
128 bsFSInfoSectorNo DW 0 ; 0x30
129 bsBackupBootSectorNo DW 0 ; 0x32
130 bsreserved times 12 DB 0 ; 0x34
131 bsDriveNumber DB 0 ; 0x40
132 bsreserved1 DB 0 ; 0x41
133 bsExtendedBootSignature DB 0 ; 0x42
134 bsVolumeSerialNumber DD 0 ; 0x43
135 bsVolumeLabel DB "NO NAME " ; 0x47
136 bsFileSystemName DB "FAT32 " ; 0x52
138 ;;;;;;;;;;;;;;;;;;;;
139 ;; BPB2 ends here ;;
140 ;;;;;;;;;;;;;;;;;;;;
142 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
143 ;; Boot sector code starts here ;;
144 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
146 start:
147 cld
149 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
150 ;; How much RAM is there? ;;
151 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
153 int 12h ; get conventional memory size (in KBs)
154 shl ax, 6 ; and convert it to 16-byte paragraphs
156 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
157 ;; Reserve memory for the boot sector and its stack ;;
158 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
160 sub ax, 512 / 16 ; reserve 512 bytes for the boot sector code
161 mov es, ax ; es:0 -> top - 512
163 sub ax, 2048 / 16 ; reserve 2048 bytes for the stack
164 mov ss, ax ; ss:0 -> top - 512 - 2048
165 mov sp, 2048 ; 2048 bytes for the stack
167 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
168 ;; Copy ourselves to top of memory ;;
169 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
171 mov cx, 256
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 mov [bsDriveNumber], dl ; store BIOS boot drive number
191 and byte [bsRootDirectoryClusterNo+3], 0Fh ; mask cluster value
192 mov esi, [bsRootDirectoryClusterNo] ; esi=cluster # of root dir
194 RootDirReadContinue:
195 push byte ImageLoadSeg
196 pop es
197 xor bx, bx
198 push es
199 call ReadCluster ; read one cluster of root dir
200 pop es
201 pushad ; save esi=next cluster # of root dir
202 ; save eax=next sector # of root dir
203 pushf ; save carry="not last cluster" flag
205 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
206 ;; Look for the COM/EXE file to load and run ;;
207 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
209 xor di, di ; es:di -> root entries array
210 mov si, ProgramName ; ds:si -> program name
212 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
213 ;; Looks for a file/dir by its name ;;
214 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
215 ;; Input: DS:SI -> file name (11 chars) ;;
216 ;; ES:DI -> root directory array ;;
217 ;; DX = number of root entries ;;
218 ;; Output: ESI = cluster number ;;
219 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
221 FindName:
222 mov cx, 11
223 FindNameCycle:
224 cmp byte [es:di], ch
225 je ErrFind ; end of root directory (NULL entry found)
226 FindNameNotEnd:
227 pusha
228 repe cmpsb
229 popa
230 je FindNameFound
231 add di, 32
232 dec bp
233 dec bp
234 jnz FindNameCycle ; next root entry
235 popf ; restore carry="not last cluster" flag
236 ; restore eax=next sector # of root dir
237 popad ; restore esi=next cluster # of root dir
238 jc RootDirReadContinue ; continue to the next root dir cluster
239 ErrFind:
240 call Error ; end of root directory (dir end reached)
241 db "File not found."
242 FindNameFound:
243 push word [es:di+14h]
244 push word [es:di+1Ah]
245 pop esi ; esi = cluster no. cx = 0
247 dec dword [es:di+1Ch] ; load ((n - 1)/256)*16 +1 paragraphs
248 imul di, [es:di+1Ch+1], 16 ; file size in paragraphs (full pages)
249 xor cx, cx
251 ;;;;;;;;;;;;;;;;;;;;;;;;;;
252 ;; Load the entire file ;;
253 ;;;;;;;;;;;;;;;;;;;;;;;;;;
255 push es
256 FileReadContinue:
257 push di
258 call ReadCluster ; read one cluster of root dir
259 mov di, es
260 add di, bp
261 mov es, di ; es:bx updated
262 pop di
264 sub di, bp
265 jae FileReadContinue
266 pop bp
268 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
269 ;; Type detection, .COM or .EXE? ;;
270 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
272 mov ds, bp ; bp=ds=seg the file is loaded to
273 add bp, [bx+08h] ; bp = image base
274 mov ax, [bx+06h] ; ax = reloc items
275 mov di, [bx+18h] ; di = reloc table pointer
277 cmp word [bx], 5A4Dh ; "MZ" signature?
279 je RelocateEXE ; yes, it's an EXE program
281 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
282 ;; Setup and run a .COM program ;;
283 ;; Set CS=DS=ES=SP SP=0 IP=100h ;;
284 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
286 mov bp, ImageLoadSeg-10h ; "org 100h" stuff :)
287 mov ss, bp
288 xor sp, sp
289 push bp ; cs, ds and es
290 mov bh, 1 ; ip
291 jmp short Run
293 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
294 ;; Relocate, setup and run a .EXE program ;;
295 ;; Set CS:IP, SS:SP, DS, ES and AX according ;;
296 ;; to wiki.osdev.org/MZ#Initial_Program_State ;;
297 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
299 ReloCycle:
300 add [di+2], bp ; item seg (abs)
301 les si, [di] ; si = item ofs, es = item seg
302 add [es:si], bp ; fixup
303 add di, 4 ; point to next entry
305 RelocateEXE:
306 dec ax ; 32768 max (128KB table)
307 jns ReloCycle ; leave with ax=0ffffh: both FCB in the
308 ; PSP don't have a valid drive identifier
309 les si, [bx+0Eh]
310 add si, bp
311 mov ss, si ; ss for EXE
312 mov sp, es ; sp for EXE
314 lea si, [bp-10h] ; ds and es both point to the segment
315 push si ; containing the PSP structure
317 add bp, [bx+16h] ; cs for EXE
318 mov bx, [bx+14h] ; ip for EXE
319 Run:
320 pop ds
321 push bp
322 push bx
323 push ds
324 pop es
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
340 ;; Reads a FAT32 cluster ;;
341 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
342 ;; Inout: ES:BX -> buffer ;;
343 ;; ESI = cluster no ;;
344 ;; Output: ESI = next cluster ;;
345 ;; ES:BX -> next addr ;;
346 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
348 ReadCluster:
349 inc cx
350 loop ReadSectorLBA
352 mov ax, [bpbBytesPerSector]
353 push ax
354 shr ax, 2 ; ax=# of FAT32 entries per sector
355 cwde
356 lea ebp, [esi-2] ; esi=cluster #
357 xchg eax, esi
358 cdq
359 div esi ; eax=FAT sector #, edx=entry # in sector
361 imul si, dx, 4 ; si=entry # in sector
362 call ReadSectorLBAabsolute ; read 1 FAT32 sector
364 and byte [es:si+3], 0Fh ; mask cluster value
365 mov esi, [es:si] ; esi=next cluster #
367 xchg eax, ebp
368 movzx ecx, byte [bpbSectorsPerCluster]
369 mul ecx
370 xchg eax, ebp
372 movzx eax, byte [bpbNumberOfFATs]
373 mul dword [bsSectorsPerFAT32]
375 add eax, ebp
377 pop bp ; [bpbBytesPerSector]
378 shr bp, 4 ; bp = paragraphs per sector
380 ReadSectorLBAabsolute:
381 movzx edx, word [bpbReservedSectors]
382 add eax, edx
383 add eax, [bpbHiddenSectors]
385 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
386 ;; Reads a sector using BIOS Int 13h fn 42h ;;
387 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
388 ;; Input: EAX = LBA ;;
389 ;; CX = sector count ;;
390 ;; ES:BX -> buffer address ;;
391 ;; Output: CF = 0 if no more sectors ;;
392 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
394 ReadSectorLBA:
395 mov dl, [bsDriveNumber] ; restore BIOS boot drive number
396 pusha
398 push bx
399 push bx ; 32-bit LBA only: up to 2TB disks
400 push eax
401 push es
402 push bx
403 push byte 1 ; sector count word = 1
404 mov cx, 16 ; retry count
405 push cx ; packet size byte = 16, reserved byte = 0
407 ReadSectorRetry:
408 mov ah, 42h ; ah = 42h = extended read function no.
409 mov si, sp
410 push ss
411 pop ds
412 int 13h ; extended read sectors (DL, DS:SI)
413 push cs
414 pop ds
415 jnc ReadSuccess ; CF = 0 if no error
417 xor ax, ax ; ah = 0 = reset function
418 int 13h ; reset drive (DL)
420 loop ReadSectorRetry ; extra attempt
421 call Error
422 db "Read error."
424 ReadSuccess:
426 popa ; sp += 16
428 popa
430 inc eax ; adjust LBA for next sector
432 stc
433 loop ReadSectorNext
435 cmp esi, 0FFFFFF8h ; carry=0 if last cluster, and carry=1 otherwise
437 ReadSectorNext:
438 ret
440 ;;;;;;;;;;;;;;;;;;;;;;;;;;
441 ;; Error Messaging Code ;;
442 ;;;;;;;;;;;;;;;;;;;;;;;;;;
444 Error:
445 pop si
446 puts:
447 mov ah, 0Eh
448 mov bl, 7
449 lodsb
450 int 10h
451 cmp al, '.'
452 jne puts
453 cbw
454 int 16h ; wait for a key...
455 int 19h ; bootstrap
457 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
458 ;; Fill free space with zeroes ;;
459 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
461 times (512-13-($-$$)) db 0
463 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
464 ;; Name of the file to load and run ;;
465 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
467 ProgramName db "STARTUP BIN" ; name and extension each must be
468 ; padded with spaces (11 bytes total)
470 ;;;;;;;;;;;;;;;;;;;;;;;;;;
471 ;; End of the sector ID ;;
472 ;;;;;;;;;;;;;;;;;;;;;;;;;;
474 dw 0AA55h ; BIOS checks for this ID