wok-6.x diff BootProg/stuff/boot32.asm @ rev 24525
Up libav (0.6.6 -> 12.3)
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Tue Feb 22 16:15:28 2022 +0000 (2022-02-22) |
parents | d8c511e24c20 |
children | 7c0170dd3ecc |
line diff
1.1 --- a/BootProg/stuff/boot32.asm Fri Feb 18 22:59:06 2022 +0000 1.2 +++ b/BootProg/stuff/boot32.asm Tue Feb 22 16:15:28 2022 +0000 1.3 @@ -11,12 +11,12 @@ 1.4 ;; ;; 1.5 ;; Features: ;; 1.6 ;; ~~~~~~~~~ ;; 1.7 -;; - FAT32 supported using BIOS int 13h function 42h (IOW, it will only ;; 1.8 -;; work with modern BIOSes supporting HDDs bigger than 8 GB) ;; 1.9 +;; - FAT32 supported using BIOS int 13h function 42h or 02h. ;; 1.10 ;; ;; 1.11 ;; - Loads a 16-bit executable file in the MS-DOS .COM or .EXE format ;; 1.12 ;; from the root directory of a disk and transfers control to it ;; 1.13 ;; (the "ProgramName" variable holds the name of the file to be loaded) ;; 1.14 +;; Its maximum size can be up to 636KB without Extended BIOS Data area. ;; 1.15 ;; ;; 1.16 ;; - Prints an error if the file isn't found or couldn't be read ;; 1.17 ;; ("File not found" or "Read error") ;; 1.18 @@ -48,10 +48,10 @@ 1.19 ;; | Loaded Image | ;; 1.20 ;; +------------------------+ nnnnnH ;; 1.21 ;; | Available Memory | ;; 1.22 -;; +------------------------+ A0000H - 512 - 2KB ;; 1.23 -;; | 2KB Boot Stack | ;; 1.24 -;; +------------------------+ A0000H - 512 ;; 1.25 +;; +------------------------+ A0000H - 2KB ;; 1.26 ;; | Boot Sector | ;; 1.27 +;; +------------------------+ A0000H - 1.5KB ;; 1.28 +;; | 1.5KB Boot Stack | ;; 1.29 ;; +------------------------+ A0000H ;; 1.30 ;; | Video RAM | ;; 1.31 ;; ;; 1.32 @@ -80,6 +80,7 @@ 1.33 [BITS 16] 1.34 1.35 ImageLoadSeg equ 60h ; <=07Fh because of "push byte ImageLoadSeg" instructions 1.36 +StackSize equ 1536 1.37 1.38 [SECTION .text] 1.39 [ORG 0] 1.40 @@ -149,24 +150,23 @@ 1.41 ;;;;;;;;;;;;;;;;;;;;;;;;;;;; 1.42 1.43 int 12h ; get conventional memory size (in KBs) 1.44 - shl ax, 6 ; and convert it to 16-byte paragraphs 1.45 + dec ax 1.46 + dec ax ; reserve 2K bytes for the code and the stack 1.47 + mov cx, 106h 1.48 + shl ax, cl ; and convert it to 16-byte paragraphs 1.49 1.50 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 1.51 ;; Reserve memory for the boot sector and its stack ;; 1.52 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 1.53 1.54 - sub ax, 512 / 16 ; reserve 512 bytes for the boot sector code 1.55 - mov es, ax ; es:0 -> top - 512 1.56 - 1.57 - sub ax, 2048 / 16 ; reserve 2048 bytes for the stack 1.58 - mov ss, ax ; ss:0 -> top - 512 - 2048 1.59 - mov sp, 2048 ; 2048 bytes for the stack 1.60 + mov es, ax ; cs:0 = ds:0 = ss:0 -> top - 512 - StackSize 1.61 + mov ss, ax 1.62 + mov sp, 512+StackSize ; bytes 0-511 are reserved for the boot code 1.63 1.64 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 1.65 ;; Copy ourselves to top of memory ;; 1.66 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 1.67 1.68 - mov cx, 256 1.69 mov si, 7C00h 1.70 xor di, di 1.71 mov ds, di 1.72 @@ -193,11 +193,7 @@ 1.73 RootDirReadContinue: 1.74 push byte ImageLoadSeg 1.75 pop es 1.76 - push es 1.77 call ReadCluster ; read one cluster of root dir 1.78 - pop es 1.79 - pushad ; save esi=next cluster # of root dir 1.80 - ; save eax=next sector # of root dir 1.81 pushf ; save carry="not last cluster" flag 1.82 1.83 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 1.84 @@ -205,7 +201,6 @@ 1.85 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 1.86 1.87 xor di, di ; es:di -> root entries array 1.88 - mov si, ProgramName ; ds:si -> program name 1.89 1.90 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 1.91 ;; Looks for a file/dir by its name ;; 1.92 @@ -213,38 +208,37 @@ 1.93 ;; Input: DS:SI -> file name (11 chars) ;; 1.94 ;; ES:DI -> root directory array ;; 1.95 ;; DX = number of root entries ;; 1.96 +;; BP = paragraphs in sector ;; 1.97 ;; Output: ESI = cluster number ;; 1.98 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 1.99 1.100 FindName: 1.101 - mov cx, 11 1.102 FindNameCycle: 1.103 - cmp byte [es:di], ch 1.104 + cmp byte [es:di], bh 1.105 je ErrFind ; end of root directory (NULL entry found) 1.106 FindNameNotEnd: 1.107 pusha 1.108 + mov cl, 11 1.109 + mov si, ProgramName ; ds:si -> program name 1.110 repe cmpsb 1.111 + je FindNameFound 1.112 popa 1.113 - je FindNameFound 1.114 - add di, 32 1.115 + add di, byte 32 1.116 dec bp 1.117 dec bp 1.118 jnz FindNameCycle ; next root entry 1.119 popf ; restore carry="not last cluster" flag 1.120 - ; restore eax=next sector # of root dir 1.121 - popad ; restore esi=next cluster # of root dir 1.122 jc RootDirReadContinue ; continue to the next root dir cluster 1.123 ErrFind: 1.124 call Error ; end of root directory (dir end reached) 1.125 db "File not found." 1.126 FindNameFound: 1.127 - push word [es:di+14h] 1.128 - push word [es:di+1Ah] 1.129 + push word [es:di+14h-11] 1.130 + push word [es:di+1Ah-11] 1.131 pop esi ; esi = cluster no. cx = 0 1.132 1.133 - dec dword [es:di+1Ch] ; load ((n - 1)/256)*16 +1 paragraphs 1.134 - imul di, [es:di+1Ch+1], 16 ; file size in paragraphs (full pages) 1.135 - xor cx, cx 1.136 + dec dword [es:di+1Ch-11] ; load ((n - 1)/256)*16 +1 paragraphs 1.137 + imul di, [es:di+1Ch+1-11], byte 16 ; file size in paragraphs (full pages) 1.138 1.139 ;;;;;;;;;;;;;;;;;;;;;;;;;; 1.140 ;; Load the entire file ;; 1.141 @@ -298,7 +292,8 @@ 1.142 add [di+2], bp ; item seg (abs) 1.143 les si, [di] ; si = item ofs, es = item seg 1.144 add [es:si], bp ; fixup 1.145 - add di, 4 ; point to next entry 1.146 + scasw ; di += 2 1.147 + scasw ; point to next entry 1.148 1.149 RelocateEXE: 1.150 dec ax ; 32768 max (128KB table) 1.151 @@ -334,52 +329,48 @@ 1.152 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 1.153 retf 1.154 1.155 +ReadCluster: 1.156 + mov bp, [bx(bpbBytesPerSector)] 1.157 + shr bp, 4 ; bp = paragraphs per sector 1.158 + inc cx 1.159 + loop ReadSectorLBA 1.160 + 1.161 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 1.162 ;; Reads a FAT32 cluster ;; 1.163 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 1.164 ;; Inout: ES:BX -> buffer ;; 1.165 ;; ESI = cluster no ;; 1.166 ;; Output: ESI = next cluster ;; 1.167 -;; ES:BX -> next addr ;; 1.168 +;; BP -> para / sector ;; 1.169 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 1.170 1.171 -ReadCluster: 1.172 - inc cx 1.173 - loop ReadSectorLBA 1.174 - 1.175 - mov ax, [bpbBytesPerSector] 1.176 - push ax 1.177 - shr ax, 2 ; ax=# of FAT32 entries per sector 1.178 + imul ax, bp, byte 2 ; ax=# of FAT32 entries per sector 1.179 cwde 1.180 - lea ebp, [esi-2] ; esi=cluster # 1.181 + lea edi, [esi-2] ; esi=cluster # 1.182 xchg eax, esi 1.183 cdq 1.184 div esi ; eax=FAT sector #, edx=entry # in sector 1.185 1.186 - imul si, dx, 4 ; si=entry # in sector 1.187 + imul si, dx, byte 4 ; si=entry # in sector, clear C 1.188 mov word [bx(HiLBA)], bx 1.189 call ReadSectorLBAabsolute ; read 1 FAT32 sector 1.190 1.191 and byte [es:si+3], 0Fh ; mask cluster value 1.192 mov esi, [es:si] ; esi=next cluster # 1.193 1.194 - xchg eax, ebp 1.195 + movzx eax, byte [bx(bpbNumberOfFATs)] 1.196 + mul dword [bx(bsSectorsPerFAT32)] 1.197 + mov word [bx(HiLBA)], dx 1.198 + 1.199 + xchg eax, edi 1.200 movzx ecx, byte [bx(bpbSectorsPerCluster)] 1.201 mul ecx ; edx:eax=sector number in data area 1.202 - xchg eax, ebp 1.203 - mov word [bx(HiLBA)], dx 1.204 1.205 - movzx eax, byte [bx(bpbNumberOfFATs)] 1.206 - mul dword [bx(bsSectorsPerFAT32)] 1.207 - 1.208 - add eax, ebp 1.209 + add eax, edi 1.210 adc word [bx(HiLBA)], dx 1.211 1.212 - pop bp ; [bpbBytesPerSector] 1.213 - shr bp, 4 ; bp = paragraphs per sector 1.214 - 1.215 ReadSectorLBAabsolute: 1.216 - movzx edx, word [bx(bpbReservedSectors)] 1.217 + mov dx, word [bx(bpbReservedSectors)] 1.218 add eax, edx 1.219 adc word [bx(HiLBA)], bx 1.220 add eax, [bx(bpbHiddenSectors)] 1.221 @@ -395,7 +386,6 @@ 1.222 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 1.223 1.224 ReadSectorLBA: 1.225 - mov dx, [bx(bsDriveNumber)] ; restore BIOS boot drive number 1.226 pusha 1.227 1.228 push bx 1.229 @@ -404,23 +394,50 @@ 1.230 push es 1.231 push bx 1.232 push byte 1 ; sector count word = 1 1.233 - mov cx, 16 ; retry count 1.234 - push cx ; packet size byte = 16, reserved byte = 0 1.235 + push byte 16 ; packet size byte = 16, reserved byte = 0 1.236 + push eax 1.237 + pop cx ; low LBA 1.238 + pop ax ; high LBA 1.239 + cwd ; clear dx (CHS disk <2TB) 1.240 + div word [bx(bpbSectorsPerTrack)] ; up to 8GB CHS disks 1.241 1.242 -ReadSectorRetry: 1.243 + xchg ax, cx ; restore low LBA, save high LBA / SPT 1.244 + div word [bx(bpbSectorsPerTrack)] 1.245 + ; ax = LBA / SPT 1.246 + ; dx = LBA % SPT = sector - 1 1.247 + inc dx 1.248 + 1.249 + xchg cx, dx ; restore high LBA / SPT, save sector no. 1.250 + div word [bx(bpbHeadsPerCylinder)] 1.251 + ; ax = (LBA / SPT) / HPC = cylinder 1.252 + ; dx = (LBA / SPT) % HPC = head 1.253 + shl ah, 6 1.254 + mov ch, al 1.255 + ; ch = LSB 0...7 of cylinder no. 1.256 + or cl, ah 1.257 + ; cl = MSB 8...9 of cylinder no. + sector no. 1.258 + mov dh, dl 1.259 + ; dh = head no. 1.260 + 1.261 +ReadSectorLBARetry: 1.262 + mov dl, [bx(bsDriveNumber)] ; restore BIOS boot drive number 1.263 + mov si, sp 1.264 mov ah, 42h ; ah = 42h = extended read function no. 1.265 - mov si, sp 1.266 - push ss 1.267 - pop ds 1.268 int 13h ; extended read sectors (DL, DS:SI) 1.269 - push cs 1.270 - pop ds 1.271 jnc ReadSuccess ; CF = 0 if no error 1.272 1.273 - xor ax, ax ; ah = 0 = reset function 1.274 +ReadSectorCHSRetry: 1.275 + mov ax, 201h ; al = sector count = 1 1.276 + ; ah = 2 = read function no. 1.277 + int 13h ; read sectors (AL, CX, DX, ES:BX) 1.278 + jnc ReadSuccess ; CF = 0 if no error 1.279 + 1.280 + cbw ; ah = 0 = reset function 1.281 int 13h ; reset drive (DL) 1.282 1.283 - loop ReadSectorRetry ; extra attempt 1.284 + dec bp 1.285 + jnz ReadSectorLBARetry 1.286 + 1.287 call Error 1.288 db "Read error." 1.289 1.290 @@ -439,6 +456,7 @@ 1.291 cmp esi, 0FFFFFF8h ; carry=0 if last cluster, and carry=1 otherwise 1.292 1.293 ReadSectorNext: 1.294 + mov dx, [bx(bsDriveNumber)] ; restore BIOS boot drive number 1.295 ret 1.296 1.297 ;;;;;;;;;;;;;;;;;;;;;;;;;; 1.298 @@ -455,8 +473,12 @@ 1.299 cmp al, '.' 1.300 jne puts 1.301 cbw 1.302 - int 16h ; wait for a key... 1.303 - int 19h ; bootstrap 1.304 +; int 16h ; wait for a key... 1.305 +; int 19h ; bootstrap 1.306 + 1.307 +Stop: 1.308 + hlt 1.309 + jmp short Stop 1.310 1.311 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 1.312 ;; Fill free space with zeroes ;;