| 1 | ; AiR-BOOT (c) Copyright 1998-2009 M. Kiewitz
|
|---|
| 2 | ;
|
|---|
| 3 | ; This file is part of AiR-BOOT
|
|---|
| 4 | ;
|
|---|
| 5 | ; AiR-BOOT is free software: you can redistribute it and/or modify it under
|
|---|
| 6 | ; the terms of the GNU General Public License as published by the Free
|
|---|
| 7 | ; Software Foundation, either version 3 of the License, or (at your option)
|
|---|
| 8 | ; any later version.
|
|---|
| 9 | ;
|
|---|
| 10 | ; AiR-BOOT is distributed in the hope that it will be useful, but WITHOUT ANY
|
|---|
| 11 | ; WARRANTY: without even the implied warranty of MERCHANTABILITY or FITNESS
|
|---|
| 12 | ; FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|---|
| 13 | ; details.
|
|---|
| 14 | ;
|
|---|
| 15 | ; You should have received a copy of the GNU General Public License along with
|
|---|
| 16 | ; AiR-BOOT. If not, see <http://www.gnu.org/licenses/>.
|
|---|
| 17 | ;
|
|---|
| 18 |
|
|---|
| 19 | ; This program fixes air-boot.com image.
|
|---|
| 20 | ; a) it includes MBR protection image from mbr-prot\mbr_prot.com
|
|---|
| 21 | ; b) it writes totally used sectors to byte at offset 10h in the image
|
|---|
| 22 |
|
|---|
| 23 | ;JUMPS
|
|---|
| 24 |
|
|---|
| 25 | Include ../../INCLUDE/ASM.INC
|
|---|
| 26 |
|
|---|
| 27 | .286p
|
|---|
| 28 | .model small, basic
|
|---|
| 29 |
|
|---|
| 30 | fixcode group code_seg,bss_data
|
|---|
| 31 |
|
|---|
| 32 | code_seg segment use16 public 'CODE'
|
|---|
| 33 | ;assume cs:code_seg, ds:code_seg, es:nothing, ss:nothing
|
|---|
| 34 | assume cs:fixcode, ds:fixcode, es:nothing, ss:nothing
|
|---|
| 35 | org 100h
|
|---|
| 36 | COM_StartUp: jmp COM_Init
|
|---|
| 37 |
|
|---|
| 38 | COM_Copyright db 'AiR-BOOT Bootcode Image Fix', 13, 10
|
|---|
| 39 | db ' - (c) Copyright 2009 by M. Kiewitz', 13, 10, '$'
|
|---|
| 40 | COM_LoadCode db ' - Loading bootcode from file...$'
|
|---|
| 41 | COM_CodeName db 'AIR-BOOT.COM', 0
|
|---|
| 42 | COM_LoadMBR db ' - Loading MBR-protection from file...$'
|
|---|
| 43 | COM_MBRName db 'MBR-PROT\MBR-PROT.BIN', 0
|
|---|
| 44 | COM_MergeMBR db ' - Merging MBR-protection into bootcode...$'
|
|---|
| 45 | COM_CountCode db ' - Count code in bootcode-image...$'
|
|---|
| 46 | COM_WriteCode db ' - Saving bootcode to file...$'
|
|---|
| 47 | COM_Okay db 'ok', 13, 10, '$'
|
|---|
| 48 | COM_Failed db 'failed', 13, 10, '$'
|
|---|
| 49 |
|
|---|
| 50 | COM_FailedOpenCode db 'air-boot.com not found', 13, 10, '$'
|
|---|
| 51 | COM_FailedReadCode db 'Read air-boot.com failed', 13, 10, '$'
|
|---|
| 52 | COM_FailedInvalidCode db 'Invalid air-boot.com', 13, 10, '$'
|
|---|
| 53 | COM_FailedOpenMBR db 'mbr-prot\mbr-prot.bin not found', 13, 10, '$'
|
|---|
| 54 | COM_FailedReadMBR db 'Read mbr-prot\mbr-prot.bin failed', 13, 10, '$'
|
|---|
| 55 | COM_FailedInvalidMBR db 'Invalid mbr-prot\mbr-prot.bin', 13, 10, '$'
|
|---|
| 56 | COM_FailedWriteCode db 'Write air-boot.com failed', 13, 10, '$'
|
|---|
| 57 |
|
|---|
| 58 | MBRProtectionSignature db 'AiR-BOOT MBR-Protection Image'
|
|---|
| 59 | MBRProtectionSignatureLen equ 29
|
|---|
| 60 |
|
|---|
| 61 | ShowMessage Proc Near Uses ax
|
|---|
| 62 | mov ah, 09h
|
|---|
| 63 | int 21h ; DOS: WRITE STRING DS:DX TO CONSOLE
|
|---|
| 64 | ret
|
|---|
| 65 | ShowMessage EndP
|
|---|
| 66 |
|
|---|
| 67 | ShowError Proc Near
|
|---|
| 68 | mov ah, 09h
|
|---|
| 69 | int 21h ; DOS: WRITE STRING DS:DX TO CONSOLE
|
|---|
| 70 | mov al,1 ; Error code
|
|---|
| 71 | jmp EndProgram
|
|---|
| 72 | ret
|
|---|
| 73 | ShowError EndP
|
|---|
| 74 |
|
|---|
| 75 | COM_Init:
|
|---|
| 76 | ; Setup and Copyright.
|
|---|
| 77 | mov ax, cs
|
|---|
| 78 | mov ds, ax
|
|---|
| 79 | mov es, ax ; CS==DS==ES
|
|---|
| 80 | mov dx, offset COM_Copyright
|
|---|
| 81 | call ShowMessage
|
|---|
| 82 |
|
|---|
| 83 | ; Open AIR-BOOT.COM
|
|---|
| 84 | mov dx, offset COM_LoadCode
|
|---|
| 85 | call ShowMessage
|
|---|
| 86 | mov ax, 3D00h
|
|---|
| 87 | mov dx, offset COM_CodeName
|
|---|
| 88 | xor cl, cl
|
|---|
| 89 | int 21h ; DOS: OPEN EXISTING FILE
|
|---|
| 90 | jnc DoneOpenCode
|
|---|
| 91 | mov dx, offset COM_FailedOpenCode
|
|---|
| 92 | call ShowError
|
|---|
| 93 |
|
|---|
| 94 | DoneOpenCode: mov bx, ax ; BX = Filehandle
|
|---|
| 95 |
|
|---|
| 96 | ; Load AIR-BOOT.COM
|
|---|
| 97 | mov ah, 3Fh
|
|---|
| 98 | mov cx, image_size ; Image size
|
|---|
| 99 | mov dx, offset BootCode
|
|---|
| 100 | int 21h ; DOS: READ FILE
|
|---|
| 101 | jnc DoneReadCode
|
|---|
| 102 | mov dx, offset COM_FailedReadCode
|
|---|
| 103 | call ShowError
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 | DoneReadCode:
|
|---|
| 107 | ; See if at least 'image-size' is loaded.
|
|---|
| 108 | cmp ax, image_size
|
|---|
| 109 | je DoneReadCode2
|
|---|
| 110 | InvalidCode: mov dx, offset COM_FailedInvalidCode
|
|---|
| 111 | call ShowError
|
|---|
| 112 |
|
|---|
| 113 | ; Try to read again which is expected to fail.
|
|---|
| 114 | ; Otherwise image is too large.
|
|---|
| 115 | DoneReadCode2: mov ah, 3Fh
|
|---|
| 116 | mov cx, 1
|
|---|
| 117 | mov dx, offset BootCode
|
|---|
| 118 | int 21h ; DOS: READ FILE
|
|---|
| 119 | jc DoneReadCode3
|
|---|
| 120 | or ax, ax
|
|---|
| 121 | jz DoneReadCode3 ; EOF -> is now expected
|
|---|
| 122 | jmp InvalidCode
|
|---|
| 123 |
|
|---|
| 124 | ; It's loaded now, close it.
|
|---|
| 125 | DoneReadCode3: mov ah, 3Eh
|
|---|
| 126 | int 21h ; DOS: CLOSE FILE
|
|---|
| 127 |
|
|---|
| 128 | mov dx, offset COM_Okay
|
|---|
| 129 | call ShowMessage
|
|---|
| 130 |
|
|---|
| 131 | ; Open MBR_PROT.BIN
|
|---|
| 132 | mov dx, offset COM_LoadMBR
|
|---|
| 133 | call ShowMessage
|
|---|
| 134 | mov ax, 3D00h
|
|---|
| 135 | mov dx, offset COM_MBRName
|
|---|
| 136 | xor cl, cl
|
|---|
| 137 | int 21h ; DOS: OPEN EXISTING FILE
|
|---|
| 138 | jnc DoneOpenMBR
|
|---|
| 139 | mov dx, offset COM_FailedOpenMBR
|
|---|
| 140 | call ShowError
|
|---|
| 141 |
|
|---|
| 142 | DoneOpenMBR: mov bx, ax ; BX = Filehandle
|
|---|
| 143 |
|
|---|
| 144 | ; Load MBR-PROT.BIN
|
|---|
| 145 | mov ah, 3Fh
|
|---|
| 146 | mov cx, 1024 ; Image size
|
|---|
| 147 | mov dx, offset MBRProtection
|
|---|
| 148 | int 21h ; DOS: READ FILE
|
|---|
| 149 | jnc DoneReadMBR
|
|---|
| 150 | mov dx, offset COM_FailedReadMBR
|
|---|
| 151 | call ShowError
|
|---|
| 152 |
|
|---|
| 153 | DoneReadMBR:
|
|---|
| 154 | ; See if at least 1kB is loaded.
|
|---|
| 155 | cmp ax, 1024
|
|---|
| 156 | je DoneReadMBR2
|
|---|
| 157 | InvalidMBR: mov dx, offset COM_FailedInvalidMBR
|
|---|
| 158 | call ShowError
|
|---|
| 159 |
|
|---|
| 160 | ; Try to read again which is expected to fail.
|
|---|
| 161 | ; Otherwise image is too large.
|
|---|
| 162 | DoneReadMBR2: mov ah, 3Fh
|
|---|
| 163 | mov cx, 1
|
|---|
| 164 | mov dx, offset MBRProtection
|
|---|
| 165 | int 21h ; DOS: READ FILE
|
|---|
| 166 | jc DoneReadMBR3
|
|---|
| 167 | or ax, ax
|
|---|
| 168 | jz DoneReadMBR3 ; EOF -> is now expected
|
|---|
| 169 | jmp InvalidMBR
|
|---|
| 170 |
|
|---|
| 171 | ; It's loaded now, close file.
|
|---|
| 172 | DoneReadMBR3: mov ah, 3Eh
|
|---|
| 173 | int 21h ; DOS: CLOSE FILE
|
|---|
| 174 |
|
|---|
| 175 | mov dx, offset COM_Okay
|
|---|
| 176 | call ShowMessage
|
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 |
|
|---|
| 180 | ; ========================== Merge MBR-Protection into Bootcode
|
|---|
| 181 | mov dx, offset COM_MergeMBR
|
|---|
| 182 | call ShowMessage
|
|---|
| 183 |
|
|---|
| 184 | ; Search for signature in Bootcode
|
|---|
| 185 | ; Note the search is with sector granularity.
|
|---|
| 186 | ; This means the signature must be 512 bytes aligned.
|
|---|
| 187 | mov si, offset BootCode
|
|---|
| 188 | mov di, offset MBRProtectionSignature
|
|---|
| 189 | mov cx, MBRProtectionSignatureLen
|
|---|
| 190 | ; 54 sectors where signature may be.
|
|---|
| 191 | ; (all sectors preceding config sector)
|
|---|
| 192 | mov dx, 54
|
|---|
| 193 | COM_SignatureLoop:
|
|---|
| 194 | push cx
|
|---|
| 195 | push si
|
|---|
| 196 | push di
|
|---|
| 197 | repe cmpsb
|
|---|
| 198 | pop di
|
|---|
| 199 | pop si
|
|---|
| 200 | pop cx
|
|---|
| 201 | je COM_GotSignature
|
|---|
| 202 | add si, 512
|
|---|
| 203 | dec dx
|
|---|
| 204 | jnz COM_SignatureLoop
|
|---|
| 205 | mov dx, offset COM_Failed
|
|---|
| 206 | call ShowMessage
|
|---|
| 207 | mov al,2 ; Error code
|
|---|
| 208 | jmp EndProgram
|
|---|
| 209 |
|
|---|
| 210 | COM_GotSignature:
|
|---|
| 211 | ; Now copy MBR-protection into bootcode
|
|---|
| 212 | mov di, si
|
|---|
| 213 | mov si, offset MBRProtection
|
|---|
| 214 | mov cx, 512
|
|---|
| 215 | rep movsw
|
|---|
| 216 | mov dx, offset COM_Okay
|
|---|
| 217 | call ShowMessage
|
|---|
| 218 |
|
|---|
| 219 | ; ====================== Count code sectors and adjust bootcode
|
|---|
| 220 | mov dx, offset COM_CountCode
|
|---|
| 221 | call ShowMessage
|
|---|
| 222 |
|
|---|
| 223 | mov si, offset BootCode
|
|---|
| 224 | add si, 512*53 ; 6A00
|
|---|
| 225 | mov cx, 256 ; 512 bytes
|
|---|
| 226 | mov dx, 53 ; initial count
|
|---|
| 227 | COM_CodeEndLoop:push cx
|
|---|
| 228 | push si
|
|---|
| 229 | push di
|
|---|
| 230 | COM_CodeEndLoop2:
|
|---|
| 231 | cmp wptr ds:[si], 0
|
|---|
| 232 | jne COM_ExitCodeEndLoop
|
|---|
| 233 | add si, 2
|
|---|
| 234 | dec cx
|
|---|
| 235 | jnz COM_CodeEndLoop2
|
|---|
| 236 | COM_ExitCodeEndLoop:
|
|---|
| 237 | pop di
|
|---|
| 238 | pop si
|
|---|
| 239 | pop cx
|
|---|
| 240 | jne COM_FoundCodeEnd
|
|---|
| 241 | sub si, 512
|
|---|
| 242 | dec dx
|
|---|
| 243 | jnz COM_CodeEndLoop
|
|---|
| 244 | mov dx, offset COM_Failed
|
|---|
| 245 | call ShowError
|
|---|
| 246 |
|
|---|
| 247 | COM_FoundCodeEnd:
|
|---|
| 248 | mov [BootCode+10h], dl
|
|---|
| 249 | mov dx, offset COM_Okay
|
|---|
| 250 | call ShowMessage
|
|---|
| 251 |
|
|---|
| 252 | ; ================================ Save bootcode back into file
|
|---|
| 253 | mov dx, offset COM_WriteCode
|
|---|
| 254 | call ShowMessage
|
|---|
| 255 | mov ax, 3D02h
|
|---|
| 256 | mov dx, offset COM_CodeName
|
|---|
| 257 | xor cl, cl
|
|---|
| 258 | int 21h ; DOS: OPEN EXISTING FILE
|
|---|
| 259 | jnc DoneOpenCode2
|
|---|
| 260 | mov dx, offset COM_FailedOpenCode
|
|---|
| 261 | call ShowError
|
|---|
| 262 |
|
|---|
| 263 | DoneOpenCode2: mov bx, ax ; BX = Filehandle
|
|---|
| 264 |
|
|---|
| 265 | mov ah, 40h
|
|---|
| 266 | mov cx, image_size ; Image size
|
|---|
| 267 | mov dx, offset BootCode
|
|---|
| 268 | int 21h ; DOS: WRITE FILE
|
|---|
| 269 | jnc DoneWriteCode
|
|---|
| 270 | FailedWriteCode:mov dx, offset COM_FailedWriteCode
|
|---|
| 271 | call ShowError
|
|---|
| 272 |
|
|---|
| 273 | DoneWriteCode: cmp ax, image_size
|
|---|
| 274 | jne FailedWriteCode
|
|---|
| 275 |
|
|---|
| 276 | mov ah, 3Eh
|
|---|
| 277 | int 21h ; DOS: CLOSE FILE
|
|---|
| 278 |
|
|---|
| 279 | mov dx, offset COM_Okay
|
|---|
| 280 | call ShowMessage
|
|---|
| 281 | xor al,al ; No Error
|
|---|
| 282 | jmp EndProgram
|
|---|
| 283 |
|
|---|
| 284 |
|
|---|
| 285 | EndProgram:
|
|---|
| 286 | ; DOS: TERMINATE PROGRAM
|
|---|
| 287 | mov ah, 04Ch
|
|---|
| 288 | int 21h
|
|---|
| 289 |
|
|---|
| 290 |
|
|---|
| 291 |
|
|---|
| 292 | COM_EndOfSegment:
|
|---|
| 293 |
|
|---|
| 294 | code_seg ends
|
|---|
| 295 |
|
|---|
| 296 | bss_data segment use16 public 'BSS'
|
|---|
| 297 | ; Buffers for files
|
|---|
| 298 | BootCode db image_size dup (?)
|
|---|
| 299 | MBRProtection db 1024 dup (?)
|
|---|
| 300 | bss_data ends
|
|---|
| 301 |
|
|---|
| 302 | end COM_StartUp
|
|---|