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

Add some current_version
author Pascal Bellard <pascal.bellard@slitaz.org>
date Fri Feb 18 09:29:30 2022 +0000 (2022-02-18)
parents
children 04472f031354
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 or 02h. ;;
15 ;; ;;
16 ;; - Loads a 16-bit executable file in the MS-DOS .COM or .EXE format ;;
17 ;; from the root directory of a disk and transfers control to it ;;
18 ;; (the "ProgramName" variable holds the name of the file to be loaded) ;;
19 ;; Its maximum size can be up to 636KB without Extended BIOS Data area. ;;
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 PRI 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 - 2KB ;;
58 ;; | Boot Sector | ;;
59 ;; +------------------------+ A0000H - 1.5KB ;;
60 ;; | 1.5KB Boot Stack | ;;
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 %define bx(label) bx+label-boot
86 [BITS 16]
87 [CPU 8086]
89 ? equ 0
90 ImageLoadSeg equ 60h
91 StackSize equ 1536
93 [SECTION .text]
94 [ORG 0]
96 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
97 ;; Boot sector starts here ;;
98 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
100 boot:
101 jmp short start ; MS-DOS/Windows checks for this jump
102 nop
103 bsOemName DB "BootProg" ; 0x03
105 ;;;;;;;;;;;;;;;;;;;;;;
106 ;; BPB1 starts here ;;
107 ;;;;;;;;;;;;;;;;;;;;;;
109 bpbBytesPerSector DW ? ; 0x0B
110 bpbSectorsPerCluster DB ? ; 0x0D
111 bpbReservedSectors DW ? ; 0x0E
112 bpbNumberOfFATs DB ? ; 0x10
113 bpbRootEntries DW ? ; 0x11
114 bpbTotalSectors DW ? ; 0x13
115 bpbMedia DB ? ; 0x15
116 bpbSectorsPerFAT DW ? ; 0x16
117 bpbSectorsPerTrack DW ? ; 0x18
118 bpbHeadsPerCylinder DW ? ; 0x1A
119 bpbHiddenSectors DD ? ; 0x1C
120 bpbTotalSectorsBig DD ? ; 0x20
122 ;;;;;;;;;;;;;;;;;;;;
123 ;; BPB1 ends here ;;
124 ;;;;;;;;;;;;;;;;;;;;
126 ;;;;;;;;;;;;;;;;;;;;;;
127 ;; BPB2 starts here ;;
128 ;;;;;;;;;;;;;;;;;;;;;;
130 bsSectorsPerFAT32 DD ? ; 0x24
131 bsExtendedFlags DW ? ; 0x28
132 bsFSVersion DW ? ; 0x2A
133 bsRootDirectoryClusterNo DD ? ; 0x2C
134 bsFSInfoSectorNo DW ? ; 0x30
135 bsBackupBootSectorNo DW ? ; 0x32
136 bsreserved times 12 DB ? ; 0x34
137 bsDriveNumber DB ? ; 0x40
138 bsreserved1 DB ? ; 0x41
139 bsExtendedBootSignature DB ? ; 0x42
140 bsVolumeSerialNumber DD ? ; 0x43
141 bsVolumeLabel DB "NO NAME " ; 0x47
142 bsFileSystemName DB "FAT32 " ; 0x52
144 ;;;;;;;;;;;;;;;;;;;;
145 ;; BPB2 ends here ;;
146 ;;;;;;;;;;;;;;;;;;;;
148 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
149 ;; Boot sector code starts here ;;
150 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
152 start:
153 cld
155 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
156 ;; How much RAM is there? ;;
157 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
159 int 12h ; get conventional memory size (in KBs)
160 mov cx, 106h
161 dec ax
162 dec ax ; reserve 2K bytes for the code and the stack
163 shl ax, cl ; and convert it to 16-byte paragraphs
165 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
166 ;; Reserve memory for the boot sector and its stack ;;
167 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
169 mov es, ax ; cs:0 = ds:0 = ss:0 -> top - 512 - StackSize
170 mov ss, ax
171 mov sp, 512+StackSize ; bytes 0-511 are reserved for the boot code
173 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
174 ;; Copy ourselves to top of memory ;;
175 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
177 mov si, 7C00h
178 xor di, di
179 mov ds, di
180 rep movsw ; move 512 bytes (+ 12)
182 ;;;;;;;;;;;;;;;;;;;;;;
183 ;; Jump to the copy ;;
184 ;;;;;;;;;;;;;;;;;;;;;;
186 push es
187 mov cl, main
188 push cx
189 retf
191 main:
192 push cs
193 pop ds
195 xor bx, bx
196 mov [bx], dx ; store BIOS boot drive number
198 and byte [bx(bsRootDirectoryClusterNo)+3], 0Fh ; mask cluster value
199 les si, [bx(bsRootDirectoryClusterNo)] ; si2:si=cluster # of root dir
200 mov si2, es
202 mov cl, ImageLoadSeg
203 mov es, cx
205 RootDirReadContinue:
206 call ReadCluster ; read one sector of root dir; clear ch
207 pushf ; save carry="not last sector" flag
209 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
210 ;; Look for the COM/EXE file to load and run ;;
211 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
213 xor di, di ; es:di -> root entries array
215 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
216 ;; Looks for the file/dir ProgramName ;;
217 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
218 ;; Input: ES:DI -> root directory array ;;
219 ;; BP = paragraphs count ;;
220 ;; Output: ESI = cluster number ;;
221 ;; DI = file paragraph count ;;
222 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
224 FindNameCycle:
225 cmp byte [es:di], bh
226 je FindNameFailed ; end of root directory (NULL entry found)
227 push si
228 push di
229 mov cl, 11
230 mov si, ProgramName ; ds:si -> program name
231 repe cmpsb
232 pop di
233 pop si
234 je FindNameFound
235 add di, 32 ; max cluster = 64k
236 dec bp
237 dec bp
238 jnz FindNameCycle ; next root entry
239 popf ; restore carry="not last sector" flag
240 jc RootDirReadContinue ; continue to the next root dir cluster
241 FindNameFailed: ; end of root directory (dir end reached)
242 call Error
243 db "File not found."
244 FindNameFound:
245 mov si2, word [es:di+14h]
246 mov si, word [es:di+1Ah] ; si2:si = cluster no; cx = 0.
248 ==============
249 dec dword [es:di+1Ch] ; load ((n - 1)/256)*16 +1 paragraphs
250 imul di, [es:di+1Dh], 16 ; file size in paragraphs (full pages)
252 ;;;;;;;;;;;;;;;;;;;;;;;;;;
253 ;; Load the entire file ;;
254 ;;;;;;;;;;;;;;;;;;;;;;;;;;
256 push es
257 FileReadContinue:
258 push di
259 call ReadCluster ; read one more sector
260 mov di, es
261 add di, bp ; adjust segment for next sector
262 mov es, di ; es:0 updated
263 pop di
264 sub di, bp
265 jae FileReadContinue
266 ==============
267 pop bp
269 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
270 ;; Type detection, .COM or .EXE? ;;
271 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
273 mov dl, [bx] ; pass the BIOS boot drive
274 mov ds, bp ; bp=ds=seg the file is loaded to
276 add bp, [bx+08h] ; bp = image base
277 mov ax, [bx+06h] ; ax = reloc items
278 mov di, [bx+18h] ; di = reloc table pointer
280 cmp word [bx], 5A4Dh ; "MZ" signature?
281 je RelocateEXE ; yes, it's an EXE program
283 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
284 ;; Setup and run a .COM program ;;
285 ;; Set CS=DS=ES=SS SP=0 IP=100h ;;
286 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
288 mov bp, ImageLoadSeg-10h ; "org 100h" stuff :)
289 mov ss, bp
290 xor sp, sp
291 push bp ; cs, ds and es
292 mov bh, 1 ; ip
293 jmp short Run
295 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
296 ;; Relocate, setup and run a .EXE program ;;
297 ;; Set CS:IP, SS:SP, DS, ES and AX according ;;
298 ;; to wiki.osdev.org/MZ#Initial_Program_State ;;
299 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
301 ReloCycle:
302 add [di+2], bp ; item seg (abs)
303 les si, [di] ; si = item ofs, es = item seg
304 add [es:si], bp ; fixup
305 scasw ; di += 2
306 scasw ; point to next entry
308 RelocateEXE:
309 dec ax ; 32768 max (128KB table)
310 jns ReloCycle ; leave with ax=0ffffh: both FCB in the
311 ; PSP don't have a valid drive identifier
312 les si, [bx+0Eh]
313 add si, bp
314 mov ss, si ; ss for EXE
315 mov sp, es ; sp for EXE
317 lea si, [bp-10h] ; ds and es both point to the segment
318 push si ; containing the PSP structure
320 add bp, [bx+16h] ; cs for EXE
321 mov bx, [bx+14h] ; ip for EXE
322 Run:
323 pop ds
324 push bp
325 push bx
326 push ds
327 pop es
329 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
330 ;; Set the magic numbers so the program knows that it ;;
331 ;; has been loaded by this bootsector and not by MS-DOS ;;
332 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
333 mov si, 16381 ; prime number 2**14-3
334 mov di, 32749 ; prime number 2**15-19
335 mov bp, 65521 ; prime number 2**16-15
337 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
338 ;; All done, transfer control to the program now ;;
339 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
340 retf
342 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
343 ;; Reads a FAT32 cluster ;;
344 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
345 ;; Input: ES:0 -> buffer ;;
346 ;; EDX:EAX = sector no ;;
347 ;; CX = sectors to load ;;
348 ;; ESI = cluster no ;;
349 ;; BX = 0 ;;
350 ;; Output: ES:0 -> buffer ;;
351 ;; EDX:EAX = sector no ;;
352 ;; CX = sectors to load ;;
353 ;; ESI = next cluster ;;
354 ;; BX = 0 ;;
355 ;; CH = 0 ;;
356 ;; BP = para/sector ;;
357 ;; C=0 for last sector ;;
358 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
360 ReadCluster:
361 ==============
362 mov bp, word [bx(bpbBytesPerSector)]
363 shr bp, 4 ; paragraphs per sector
364 inc cx
365 add ax, 1 ; adjust LBA for next sector
366 ? adc dx, bx
367 loop ReadSectorLBANext
369 imul ax, bp, 2
370 cwde ; eax=# of FAT32 entries per sector
371 lea edi, [esi-2] ; edi=cluster #-2
372 xchg eax, esi
373 cdq
374 div esi ; eax=FAT sector #, edx=entry # in sector < 128
376 imul si, dx, 4 ; si=entry # offset in sector
378 call ReadSectorLBA ; read 1 FAT32 sector
380 mov si2, 0FFFh
381 and si2, [es:si+2] ; mask cluster value
382 mov si, [es:si] ; si2:si=next cluster #
384 movzx eax, byte [bx(bpbNumberOfFATs)]
385 mul dword [bx(bsSectorsPerFAT32)] ; edx < 256
387 xchg eax, edi ; get cluster #-2 ; save data offset
389 movzx ecx, byte [bx(bpbSectorsPerCluster)]
390 mul ecx
392 add eax, edi ; sector # relative to FAT32
394 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
395 ;; Reads a sector using BIOS Int 13h ;;
396 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
397 ;; Input: ES:0 -> buffer address ;;
398 ;; EAX = LBA (FAT based) ;;
399 ;; BX = 0 ;;
400 ;; CX = sector count ;;
401 ;; Output: ES:0 -> buffer address ;;
402 ;; EDX:EAX = absolute LBA ;;
403 ;; BX = 0 ;;
404 ;; CX = next sector count ;;
405 ;; BP = paragraphs/sector ;;
406 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
408 ReadSectorLBA:
410 mov dx, word [bx(bpbReservedSectors)]
411 add eax, edx
412 mov dx, bx
413 adc dx, bx
414 add eax, [bx(bpbHiddenSectors)]
416 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
417 ;; Input: ES:0 -> buffer address ;;
418 ;; EDX:EAX = absolute LBA ;;
419 ;; BX = 0 ;;
420 ;; CX = sector count ;;
421 ;; Output: ES:0 -> buffer address ;;
422 ;; EDX:EAX = absolute LBA ;;
423 ;; BX = 0 ;;
424 ;; CX = next sector count ;;
425 ;; BP = paragraphs/sector ;;
426 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
428 ReadSectorLBANext:
430 adc dx, bx ; FAT32 partition can start up to 2TB
432 pusha
434 push edx ; 33-bit LBA only: up to 4TB disks
435 push eax ; 32-bit LBA: up to 2TB FAT32 partition start
436 push es
437 push bx
438 push byte 1 ; sector count word = 1
439 push byte 16 ; packet size byte = 16, reserved byte = 0
441 push eax
442 pop cx ; low LBA
443 pop ax ; high LBA, dx=0 (<2TB)
444 div word [bx(bpbSectorsPerTrack)] ; up to 8GB disks
446 xchg ax, cx ; restore low LBA, save high LBA / SPT
447 div word [bx(bpbSectorsPerTrack)]
448 ; ax = LBA / SPT
449 ; dx = LBA % SPT = sector - 1
450 inc dx
452 xchg cx, dx ; restore high LBA / SPT, save sector no.
453 div word [bx(bpbHeadsPerCylinder)]
454 ; ax = (LBA / SPT) / HPC = cylinder
455 ; dx = (LBA / SPT) % HPC = head
456 shl ah, 6
457 mov ch, al
458 ; ch = LSB 0...7 of cylinder no.
459 or cl, ah
460 ; cl = MSB 8...9 of cylinder no. + sector no.
461 mov dh, dl
462 ; dh = head no.
464 ReadSectorLBARetry:
465 mov dl, [bx]
466 ; dl = drive no.
467 mov si, sp
468 mov ah, 42h ; ah = 42h = extended read function no.
469 int 13h ; extended read sectors (DL, DS:SI)
470 jnc ReadSectorNextSegment
472 ReadSectorCHSRetry:
473 mov ax, 201h ; al = sector count = 1
474 ; ah = 2 = read function no.
475 int 13h ; read sectors (AL, CX, DX, ES:BX)
476 jnc ReadSectorNextSegment
478 cbw ; ah = 0 = reset function
479 int 13h ; reset drive (DL)
481 dec bp
482 jnz ReadSectorLBARetry
484 call Error
485 db "Read error."
487 ReadSectorNextSegment:
489 popa ; sp += 16
490 popa ; restore real registers
491 ==============
492 stc
493 loop ReadSectorDone
495 cmp si2, 0FFFh
496 jne ReadSectorDone
497 cmp si, 0FFF8h ; carry=0 if last cluster, and carry=1 otherwise
499 ReadSectorDone:
500 ret
502 ;;;;;;;;;;;;;;;;;;;;;;;;;;
503 ;; Error Messaging Code ;;
504 ;;;;;;;;;;;;;;;;;;;;;;;;;;
506 Error:
507 pop si
508 mov dl, [bx] ; restore BIOS boot drive number
509 mov ah, 0Eh
510 mov bl, 7
512 PutStr:
513 lodsb
514 int 10h
515 cmp al, "."
516 jne PutStr
518 cbw
519 int 16h ; wait for a key...
520 int 19h ; bootstrap
522 Stop:
523 hlt
524 jmp short Stop
526 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
527 ;; Fill free space with zeroes ;;
528 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
530 times (512-13-($-$$)) db 0
532 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
533 ;; Name of the file to load and run ;;
534 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
536 ProgramName db "STARTUP BIN" ; name and extension each must be
537 ; padded with spaces (11 bytes total)
539 ;;;;;;;;;;;;;;;;;;;;;;;;;;
540 ;; End of the sector ID ;;
541 ;;;;;;;;;;;;;;;;;;;;;;;;;;
543 dw 0AA55h ; BIOS checks for this ID