wok-6.x view BootProg/stuff/boot32.asm @ rev 24491

Add some current_version
author Pascal Bellard <pascal.bellard@slitaz.org>
date Fri Feb 18 22:59:06 2022 +0000 (2022-02-18)
parents d211771a0500
children d77ab883a8b3
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 Bugs: ;;
28 ;; ~~~~~~~~~~~ ;;
29 ;; - All bugs are fixed as far as I know. The boot sector has been tested ;;
30 ;; on my HDD and an 8GB USB stick. ;;
31 ;; ;;
32 ;; ;;
33 ;; Memory Layout: ;;
34 ;; ~~~~~~~~~~~~~~ ;;
35 ;; The diagram below shows the typical memory layout. The actual location ;;
36 ;; of the boot sector and its stack may be lower than A0000H if the BIOS ;;
37 ;; reserves memory for its Extended BIOS Data Area just below A0000H and ;;
38 ;; reports less than 640 KB of RAM via its Int 12H function. ;;
39 ;; ;;
40 ;; physical address ;;
41 ;; +------------------------+ 00000H ;;
42 ;; | Interrupt Vector Table | ;;
43 ;; +------------------------+ 00400H ;;
44 ;; | BIOS Data Area | ;;
45 ;; +------------------------+ 00500H ;;
46 ;; | PrtScr Status / Unused | ;;
47 ;; +------------------------+ 00600H ;;
48 ;; | Loaded Image | ;;
49 ;; +------------------------+ nnnnnH ;;
50 ;; | Available Memory | ;;
51 ;; +------------------------+ A0000H - 512 - 2KB ;;
52 ;; | 2KB Boot Stack | ;;
53 ;; +------------------------+ A0000H - 512 ;;
54 ;; | Boot Sector | ;;
55 ;; +------------------------+ A0000H ;;
56 ;; | Video RAM | ;;
57 ;; ;;
58 ;; ;;
59 ;; Boot Image Startup (register values): ;;
60 ;; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ;;
61 ;; dl = BIOS boot drive number (e.g. 80H) ;;
62 ;; cs:ip = program entry point ;;
63 ;; ss:sp = program stack (don't confuse with boot sector's stack) ;;
64 ;; COM program defaults: cs = ds = es = ss = 50h, sp = 0, ip = 100h ;;
65 ;; EXE program defaults: ds = es = EXE data - 10h (fake MS-DOS psp), ;;
66 ;; ax = 0ffffh (both FCB in the PSP don't have a valid drive identifier), ;;
67 ;; cs:ip and ss:sp depends on EXE header ;;
68 ;; Magic numbers: ;;
69 ;; si = 16381 (prime number 2**14-3) ;;
70 ;; di = 32749 (prime number 2**15-19) ;;
71 ;; bp = 65521 (prime number 2**16-15) ;;
72 ;; The magic numbers let the program know whether it has been loaded by ;;
73 ;; this boot sector or by MS-DOS, which may be handy for universal, bare- ;;
74 ;; metal and MS-DOS programs. ;;
75 ;; ;;
76 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
78 %define bx(label) bx+label-boot
80 [BITS 16]
82 ImageLoadSeg equ 60h ; <=07Fh because of "push byte ImageLoadSeg" instructions
84 [SECTION .text]
85 [ORG 0]
87 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
88 ;; Boot sector starts here ;;
89 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
91 boot:
92 HiLBA equ boot+0
93 jmp short start ; MS-DOS/Windows checks for this jump
94 nop
95 bsOemName DB "BootProg" ; 0x03
97 ;;;;;;;;;;;;;;;;;;;;;;
98 ;; BPB1 starts here ;;
99 ;;;;;;;;;;;;;;;;;;;;;;
101 bpbBytesPerSector DW 0 ; 0x0B
102 bpbSectorsPerCluster DB 0 ; 0x0D
103 bpbReservedSectors DW 0 ; 0x0E
104 bpbNumberOfFATs DB 0 ; 0x10
105 bpbRootEntries DW 0 ; 0x11
106 bpbTotalSectors DW 0 ; 0x13
107 bpbMedia DB 0 ; 0x15
108 bpbSectorsPerFAT DW 0 ; 0x16
109 bpbSectorsPerTrack DW 0 ; 0x18
110 bpbHeadsPerCylinder DW 0 ; 0x1A
111 bpbHiddenSectors DD 0 ; 0x1C
112 bpbTotalSectorsBig DD 0 ; 0x20
114 ;;;;;;;;;;;;;;;;;;;;
115 ;; BPB1 ends here ;;
116 ;;;;;;;;;;;;;;;;;;;;
118 ;;;;;;;;;;;;;;;;;;;;;;
119 ;; BPB2 starts here ;;
120 ;;;;;;;;;;;;;;;;;;;;;;
122 bsSectorsPerFAT32 DD 0 ; 0x24
123 bsExtendedFlags DW 0 ; 0x28
124 bsFSVersion DW 0 ; 0x2A
125 bsRootDirectoryClusterNo DD 0 ; 0x2C
126 bsFSInfoSectorNo DW 0 ; 0x30
127 bsBackupBootSectorNo DW 0 ; 0x32
128 bsreserved times 12 DB 0 ; 0x34
129 bsDriveNumber DB 0 ; 0x40
130 bsreserved1 DB 0 ; 0x41
131 bsExtendedBootSignature DB 0 ; 0x42
132 bsVolumeSerialNumber DD 0 ; 0x43
133 bsVolumeLabel DB "NO NAME " ; 0x47
134 bsFileSystemName DB "FAT32 " ; 0x52
136 ;;;;;;;;;;;;;;;;;;;;
137 ;; BPB2 ends here ;;
138 ;;;;;;;;;;;;;;;;;;;;
140 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
141 ;; Boot sector code starts here ;;
142 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
144 start:
145 cld
147 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
148 ;; How much RAM is there? ;;
149 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
151 int 12h ; get conventional memory size (in KBs)
152 shl ax, 6 ; and convert it to 16-byte paragraphs
154 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
155 ;; Reserve memory for the boot sector and its stack ;;
156 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
158 sub ax, 512 / 16 ; reserve 512 bytes for the boot sector code
159 mov es, ax ; es:0 -> top - 512
161 sub ax, 2048 / 16 ; reserve 2048 bytes for the stack
162 mov ss, ax ; ss:0 -> top - 512 - 2048
163 mov sp, 2048 ; 2048 bytes for the stack
165 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
166 ;; Copy ourselves to top of memory ;;
167 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
169 mov cx, 256
170 mov si, 7C00h
171 xor di, di
172 mov ds, di
173 rep movsw
175 ;;;;;;;;;;;;;;;;;;;;;;
176 ;; Jump to the copy ;;
177 ;;;;;;;;;;;;;;;;;;;;;;
179 push es
180 push byte main
181 retf
183 main:
184 push cs
185 pop ds
187 xor bx, bx
188 mov [bx(bsDriveNumber)], dx ; store BIOS boot drive number
190 and byte [bx(bsRootDirectoryClusterNo+3)], 0Fh ; mask cluster value
191 mov esi, [bx(bsRootDirectoryClusterNo)] ; esi=cluster # of root dir
193 RootDirReadContinue:
194 push byte ImageLoadSeg
195 pop es
196 push es
197 call ReadCluster ; read one cluster of root dir
198 pop es
199 pushad ; save esi=next cluster # of root dir
200 ; save eax=next sector # of root dir
201 pushf ; save carry="not last cluster" flag
203 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
204 ;; Look for the COM/EXE file to load and run ;;
205 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
207 xor di, di ; es:di -> root entries array
208 mov si, ProgramName ; ds:si -> program name
210 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
211 ;; Looks for a file/dir by its name ;;
212 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
213 ;; Input: DS:SI -> file name (11 chars) ;;
214 ;; ES:DI -> root directory array ;;
215 ;; DX = number of root entries ;;
216 ;; Output: ESI = cluster number ;;
217 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
219 FindName:
220 mov cx, 11
221 FindNameCycle:
222 cmp byte [es:di], ch
223 je ErrFind ; end of root directory (NULL entry found)
224 FindNameNotEnd:
225 pusha
226 repe cmpsb
227 popa
228 je FindNameFound
229 add di, 32
230 dec bp
231 dec bp
232 jnz FindNameCycle ; next root entry
233 popf ; restore carry="not last cluster" flag
234 ; restore eax=next sector # of root dir
235 popad ; restore esi=next cluster # of root dir
236 jc RootDirReadContinue ; continue to the next root dir cluster
237 ErrFind:
238 call Error ; end of root directory (dir end reached)
239 db "File not found."
240 FindNameFound:
241 push word [es:di+14h]
242 push word [es:di+1Ah]
243 pop esi ; esi = cluster no. cx = 0
245 dec dword [es:di+1Ch] ; load ((n - 1)/256)*16 +1 paragraphs
246 imul di, [es:di+1Ch+1], 16 ; file size in paragraphs (full pages)
247 xor cx, cx
249 ;;;;;;;;;;;;;;;;;;;;;;;;;;
250 ;; Load the entire file ;;
251 ;;;;;;;;;;;;;;;;;;;;;;;;;;
253 push es
254 FileReadContinue:
255 push di
256 call ReadCluster ; read one cluster of root dir
257 mov di, es
258 add di, bp
259 mov es, di ; es:bx updated
260 pop di
262 sub di, bp
263 jae FileReadContinue
264 pop bp
266 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
267 ;; Type detection, .COM or .EXE? ;;
268 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
270 mov ds, bp ; bp=ds=seg the file is loaded to
271 add bp, [bx+08h] ; bp = image base
272 mov ax, [bx+06h] ; ax = reloc items
273 mov di, [bx+18h] ; di = reloc table pointer
275 cmp word [bx], 5A4Dh ; "MZ" signature?
277 je RelocateEXE ; yes, it's an EXE program
279 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
280 ;; Setup and run a .COM program ;;
281 ;; Set CS=DS=ES=SP SP=0 IP=100h ;;
282 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
284 mov bp, ImageLoadSeg-10h ; "org 100h" stuff :)
285 mov ss, bp
286 xor sp, sp
287 push bp ; cs, ds and es
288 mov bh, 1 ; ip
289 jmp short Run
291 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
292 ;; Relocate, setup and run a .EXE program ;;
293 ;; Set CS:IP, SS:SP, DS, ES and AX according ;;
294 ;; to wiki.osdev.org/MZ#Initial_Program_State ;;
295 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
297 ReloCycle:
298 add [di+2], bp ; item seg (abs)
299 les si, [di] ; si = item ofs, es = item seg
300 add [es:si], bp ; fixup
301 add di, 4 ; point to next entry
303 RelocateEXE:
304 dec ax ; 32768 max (128KB table)
305 jns ReloCycle ; leave with ax=0ffffh: both FCB in the
306 ; PSP don't have a valid drive identifier
307 les si, [bx+0Eh]
308 add si, bp
309 mov ss, si ; ss for EXE
310 mov sp, es ; sp for EXE
312 lea si, [bp-10h] ; ds and es both point to the segment
313 push si ; containing the PSP structure
315 add bp, [bx+16h] ; cs for EXE
316 mov bx, [bx+14h] ; ip for EXE
317 Run:
318 pop ds
319 push bp
320 push bx
321 push ds
322 pop es
324 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
325 ;; Set the magic numbers so the program knows that it ;;
326 ;; has been loaded by this bootsector and not by MS-DOS ;;
327 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
328 mov si, 16381 ; prime number 2**14-3
329 mov di, 32749 ; prime number 2**15-19
330 mov bp, 65521 ; prime number 2**16-15
332 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
333 ;; All done, transfer control to the program now ;;
334 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
335 retf
337 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
338 ;; Reads a FAT32 cluster ;;
339 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
340 ;; Inout: ES:BX -> buffer ;;
341 ;; ESI = cluster no ;;
342 ;; Output: ESI = next cluster ;;
343 ;; ES:BX -> next addr ;;
344 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
346 ReadCluster:
347 inc cx
348 loop ReadSectorLBA
350 mov ax, [bpbBytesPerSector]
351 push ax
352 shr ax, 2 ; ax=# of FAT32 entries per sector
353 cwde
354 lea ebp, [esi-2] ; esi=cluster #
355 xchg eax, esi
356 cdq
357 div esi ; eax=FAT sector #, edx=entry # in sector
359 imul si, dx, 4 ; si=entry # in sector
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 xchg eax, ebp
367 movzx ecx, byte [bx(bpbSectorsPerCluster)]
368 mul ecx ; edx:eax=sector number in data area
369 xchg eax, ebp
370 mov word [bx(HiLBA)], dx
372 movzx eax, byte [bx(bpbNumberOfFATs)]
373 mul dword [bx(bsSectorsPerFAT32)]
375 add eax, ebp
376 adc word [bx(HiLBA)], dx
378 pop bp ; [bpbBytesPerSector]
379 shr bp, 4 ; bp = paragraphs per sector
381 ReadSectorLBAabsolute:
382 movzx edx, word [bx(bpbReservedSectors)]
383 add eax, edx
384 adc word [bx(HiLBA)], bx
385 add eax, [bx(bpbHiddenSectors)]
386 adc word [bx(HiLBA)], bx
388 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
389 ;; Reads a sector using BIOS Int 13h fn 42h ;;
390 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
391 ;; Input: EAX = LBA ;;
392 ;; CX = sector count ;;
393 ;; ES:BX -> buffer address ;;
394 ;; Output: CF = 0 if no more sectors ;;
395 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
397 ReadSectorLBA:
398 mov dx, [bx(bsDriveNumber)] ; restore BIOS boot drive number
399 pusha
401 push bx
402 push word [bx(HiLBA)] ; 48-bit LBA
403 push eax
404 push es
405 push bx
406 push byte 1 ; sector count word = 1
407 mov cx, 16 ; retry count
408 push cx ; packet size byte = 16, reserved byte = 0
410 ReadSectorRetry:
411 mov ah, 42h ; ah = 42h = extended read function no.
412 mov si, sp
413 push ss
414 pop ds
415 int 13h ; extended read sectors (DL, DS:SI)
416 push cs
417 pop ds
418 jnc ReadSuccess ; CF = 0 if no error
420 xor ax, ax ; ah = 0 = reset function
421 int 13h ; reset drive (DL)
423 loop ReadSectorRetry ; extra attempt
424 call Error
425 db "Read error."
427 ReadSuccess:
429 popa ; sp += 16
431 popa
433 add eax, byte 1 ; adjust LBA for next sector
434 adc word [bx(HiLBA)], bx
436 stc
437 loop ReadSectorNext
439 cmp esi, 0FFFFFF8h ; carry=0 if last cluster, and carry=1 otherwise
441 ReadSectorNext:
442 ret
444 ;;;;;;;;;;;;;;;;;;;;;;;;;;
445 ;; Error Messaging Code ;;
446 ;;;;;;;;;;;;;;;;;;;;;;;;;;
448 Error:
449 pop si
450 puts:
451 mov ah, 0Eh
452 mov bl, 7
453 lodsb
454 int 10h
455 cmp al, '.'
456 jne puts
457 cbw
458 int 16h ; wait for a key...
459 int 19h ; bootstrap
461 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
462 ;; Fill free space with zeroes ;;
463 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
465 times (512-13-($-$$)) db 0
467 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
468 ;; Name of the file to load and run ;;
469 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
471 ProgramName db "STARTUP BIN" ; name and extension each must be
472 ; padded with spaces (11 bytes total)
474 ;;;;;;;;;;;;;;;;;;;;;;;;;;
475 ;; End of the sector ID ;;
476 ;;;;;;;;;;;;;;;;;;;;;;;;;;
478 dw 0AA55h ; BIOS checks for this ID