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

Add some current_version
author Pascal Bellard <pascal.bellard@slitaz.org>
date Wed Feb 23 11:49:52 2022 +0000 (2022-02-23)
parents d77ab883a8b3
children d1f31f5f6401
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 ;; dl = BIOS boot drive number (e.g. 80H) ;;
63 ;; cs:ip = program entry point ;;
64 ;; ss:sp = program stack (don't confuse with boot sector's stack) ;;
65 ;; COM program defaults: cs = ds = es = ss = 50h, sp = 0, ip = 100h ;;
66 ;; EXE program defaults: ds = es = EXE data - 10h (fake MS-DOS psp), ;;
67 ;; ax = 0ffffh (both FCB in the PSP don't have a valid drive identifier), ;;
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 ;; ;;
77 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
79 %define bx(label) bx+label-boot
81 [BITS 16]
83 ImageLoadSeg equ 60h ; <=07Fh because of "push byte ImageLoadSeg" instructions
84 StackSize equ 1536
86 [SECTION .text]
87 [ORG 0]
89 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
90 ;; Boot sector starts here ;;
91 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
93 boot:
94 HiLBA equ boot+0
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 dec ax
155 dec ax ; reserve 2K bytes for the code and the stack
156 mov cx, 106h
157 shl ax, cl ; and convert it to 16-byte paragraphs
159 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
160 ;; Reserve memory for the boot sector and its stack ;;
161 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
163 mov es, ax ; cs:0 = ds:0 = ss:0 -> top - 512 - StackSize
164 mov ss, ax
165 mov sp, 512+StackSize ; bytes 0-511 are reserved for the boot code
167 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
168 ;; Copy ourselves to top of memory ;;
169 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
171 mov si, 7C00h
172 xor di, di
173 mov ds, di
174 rep movsw
176 ;;;;;;;;;;;;;;;;;;;;;;
177 ;; Jump to the copy ;;
178 ;;;;;;;;;;;;;;;;;;;;;;
180 push es
181 push byte main
182 retf
184 main:
185 push cs
186 pop ds
188 xor bx, bx
189 mov [bx(bsDriveNumber)], dx ; store BIOS boot drive number
191 and byte [bx(bsRootDirectoryClusterNo+3)], 0Fh ; mask cluster value
192 mov esi, [bx(bsRootDirectoryClusterNo)] ; esi=cluster # of root dir
194 RootDirReadContinue:
195 push byte ImageLoadSeg
196 pop es
197 call ReadCluster ; read one cluster of root dir
198 pushf ; save carry="not last cluster" flag
200 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
201 ;; Look for the COM/EXE file to load and run ;;
202 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
204 xor di, di ; es:di -> root entries array
206 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
207 ;; Looks for a file/dir by its name ;;
208 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
209 ;; Input: DS:SI -> file name (11 chars) ;;
210 ;; ES:DI -> root directory array ;;
211 ;; DX = number of root entries ;;
212 ;; BP = paragraphs in sector ;;
213 ;; Output: ESI = cluster number ;;
214 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
216 FindName:
217 FindNameCycle:
218 cmp byte [es:di], bh
219 je ErrFind ; end of root directory (NULL entry found)
220 FindNameNotEnd:
221 pusha
222 mov cl, 11
223 mov si, ProgramName ; ds:si -> program name
224 repe cmpsb
225 je FindNameFound
226 popa
227 add di, byte 32
228 dec bp
229 dec bp
230 jnz FindNameCycle ; next root entry
231 popf ; restore carry="not last cluster" flag
232 jc RootDirReadContinue ; continue to the next root dir cluster
233 ErrFind:
234 call Error ; end of root directory (dir end reached)
235 db "File not found."
236 FindNameFound:
237 push word [es:di+14h-11]
238 push word [es:di+1Ah-11]
239 pop esi ; esi = cluster no. cx = 0
241 dec dword [es:di+1Ch-11] ; load ((n - 1)/256)*16 +1 paragraphs
242 imul di, [es:di+1Ch+1-11], byte 16 ; file size in paragraphs (full pages)
244 ;;;;;;;;;;;;;;;;;;;;;;;;;;
245 ;; Load the entire file ;;
246 ;;;;;;;;;;;;;;;;;;;;;;;;;;
248 push es
249 FileReadContinue:
250 push di
251 call ReadCluster ; read one cluster of root dir
252 mov di, es
253 add di, bp
254 mov es, di ; es:bx updated
255 pop di
257 sub di, bp
258 jae FileReadContinue
259 pop bp
261 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
262 ;; Type detection, .COM or .EXE? ;;
263 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
265 mov ds, bp ; bp=ds=seg the file is loaded to
267 add bp, [bx+08h] ; bp = image base
268 mov ax, [bx+06h] ; ax = reloc items
269 mov di, [bx+18h] ; di = reloc table pointer
271 cmp word [bx], 5A4Dh ; "MZ" signature?
272 je RelocateEXE ; yes, it's an EXE program
274 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
275 ;; Setup and run a .COM program ;;
276 ;; Set CS=DS=ES=SS SP=0 IP=100h ;;
277 ;; AX=0ffffh BX=0 CX=0 DX=drive ;;
278 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
280 mov ax, 0ffffh ; both FCB in the PSP don't have a valid drive identifier
281 mov di, 100h ; ip
282 mov bp, ImageLoadSeg-10h ; "org 100h" stuff :)
283 mov ss, bp
284 xor sp, sp
285 push bp ; cs, ds and es
286 jmp short Run
288 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
289 ;; Relocate, setup and run a .EXE program ;;
290 ;; Set CS:IP, SS:SP, DS, ES and AX according ;;
291 ;; to wiki.osdev.org/MZ#Initial_Program_State ;;
292 ;; AX=0ffffh BX=0 CX=0 DX=drive ;;
293 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
295 ReloCycle:
296 add [di+2], bp ; item seg (abs)
297 les si, [di] ; si = item ofs, es = item seg
298 add [es:si], bp ; fixup
299 scasw ; di += 2
300 scasw ; point to next entry
302 RelocateEXE:
303 dec ax ; 32768 max (128KB table)
304 jns ReloCycle ; leave with ax=0ffffh: both FCB in the
305 ; PSP don't have a valid drive identifier
306 les si, [bx+0Eh]
307 add si, bp
308 mov ss, si ; ss for EXE
309 mov sp, es ; sp for EXE
311 lea si, [bp-10h] ; ds and es both point to the segment
312 push si ; containing the PSP structure
314 add bp, [bx+16h] ; cs for EXE
315 mov di, [bx+14h] ; ip for EXE
316 Run:
317 pop ds
318 push bp
319 push di
320 push ds
321 pop es
323 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
324 ;; Set the magic numbers so the program knows that it ;;
325 ;; has been loaded by this bootsector and not by MS-DOS ;;
326 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
327 mov si, 16381 ; prime number 2**14-3
328 mov di, 32749 ; prime number 2**15-19
329 mov bp, 65521 ; prime number 2**16-15
331 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
332 ;; All done, transfer control to the program now ;;
333 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
334 retf
336 ReadCluster:
337 mov bp, [bx(bpbBytesPerSector)]
338 shr bp, 4 ; bp = paragraphs per sector
339 add eax, byte 1 ; adjust LBA for next sector
340 inc cx
341 loop ReadSectorLBA
343 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
344 ;; Reads a FAT32 cluster ;;
345 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
346 ;; Inout: ES:BX -> buffer ;;
347 ;; ESI = cluster no ;;
348 ;; Output: ESI = next cluster ;;
349 ;; BP -> para / sector ;;
350 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
352 imul ax, bp, byte 2 ; ax=# of FAT32 entries per sector
353 cwde
354 lea edi, [esi-2] ; esi=cluster #
355 xchg eax, esi
356 cdq
357 div esi ; eax=FAT sector #, edx=entry # in sector
359 imul si, dx, byte 4 ; si=entry # in sector, clear C
360 mov word [bx(HiLBA)], bx
361 call ReadSectorLBAabsolute ; read 1 FAT32 sector
363 and byte [es:si+3], 0Fh ; mask cluster value
364 mov esi, [es:si] ; esi=next cluster #
366 movzx eax, byte [bx(bpbNumberOfFATs)]
367 mul dword [bx(bsSectorsPerFAT32)]
368 mov word [bx(HiLBA)], dx
370 xchg eax, edi
371 movzx ecx, byte [bx(bpbSectorsPerCluster)]
372 mul ecx ; edx:eax=sector number in data area
374 add eax, edi
375 adc word [bx(HiLBA)], dx
377 ReadSectorLBAabsolute:
378 mov dx, word [bx(bpbReservedSectors)]
379 add eax, edx
380 adc word [bx(HiLBA)], bx
381 add eax, [bx(bpbHiddenSectors)]
383 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
384 ;; Reads a sector using BIOS Int 13h fn 42h ;;
385 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
386 ;; Input: EAX = LBA ;;
387 ;; CX = sector count ;;
388 ;; ES:BX -> buffer address ;;
389 ;; Output: CF = 0 if no more sectors ;;
390 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
392 ReadSectorLBA:
393 adc word [bx(HiLBA)], bx
394 mov dx, [bx(bsDriveNumber)] ; restore BIOS boot drive number
395 pusha
397 push bx
398 push word [bx(HiLBA)] ; 48-bit LBA
399 push eax
400 push es
401 push bx
402 push byte 1 ; sector count word = 1
403 push byte 16 ; packet size byte = 16, reserved byte = 0
405 ReadSectorLBARetry:
406 mov si, sp
407 mov ah, 42h ; ah = 42h = extended read function no.
408 int 13h ; extended read sectors (DL, DS:SI)
409 jnc ReadSuccess ; CF = 0 if no error
411 cbw ; ah = 0 = reset function
412 int 13h ; reset drive (DL)
414 dec bp
415 jnz ReadSectorLBARetry
417 call Error
418 db "Read error."
420 ReadSuccess:
422 popa ; sp += 16
424 popa
426 stc
427 loop ReadSectorNext
429 cmp esi, 0FFFFFF8h ; carry=0 if last cluster, and carry=1 otherwise
431 ReadSectorNext:
432 ret
434 ;;;;;;;;;;;;;;;;;;;;;;;;;;
435 ;; Error Messaging Code ;;
436 ;;;;;;;;;;;;;;;;;;;;;;;;;;
438 Error:
439 pop si
440 puts:
441 mov ah, 0Eh
442 mov bl, 7
443 lodsb
444 int 10h
445 cmp al, '.'
446 jne puts
447 cbw
448 int 16h ; wait for a key...
449 int 19h ; bootstrap
451 Stop:
452 hlt
453 jmp short Stop
455 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
456 ;; Fill free space with zeroes ;;
457 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
459 times (512-13-($-$$)) db 0
461 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
462 ;; Name of the file to load and run ;;
463 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
465 ProgramName db "STARTUP BIN" ; name and extension each must be
466 ; padded with spaces (11 bytes total)
468 ;;;;;;;;;;;;;;;;;;;;;;;;;;
469 ;; End of the sector ID ;;
470 ;;;;;;;;;;;;;;;;;;;;;;;;;;
472 dw 0AA55h ; BIOS checks for this ID