| 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 | ; include ..\..\include\dos\airboot.inc
|
|---|
| 27 |
|
|---|
| 28 | .386p
|
|---|
| 29 | model large, basic
|
|---|
| 30 |
|
|---|
| 31 | code_seg segment public use16
|
|---|
| 32 | assume cs:code_seg, ds:code_seg, es:nothing, ss:nothing
|
|---|
| 33 | org 100h
|
|---|
| 34 | COM_StartUp: jmp COM_Init
|
|---|
| 35 |
|
|---|
| 36 | COM_Copyright db 'AiR-BOOT Bootcode Image Fix', 13, 10
|
|---|
| 37 | db ' - (c) Copyright 2009 by M. Kiewitz', 13, 10, '$'
|
|---|
| 38 | COM_LoadCode db ' - Loading bootcode from file...$'
|
|---|
| 39 | COM_CodeName db 'air-boot.com', 0
|
|---|
| 40 | COM_LoadMBR db ' - Loading MBR-protection from file...$'
|
|---|
| 41 | COM_MBRName db 'mbr-prot\mbr_prot.com', 0
|
|---|
| 42 | COM_MergeMBR db ' - Merging MBR-protection into bootcode...$'
|
|---|
| 43 | COM_CountCode db ' - Count code in bootcode-image...$'
|
|---|
| 44 | COM_WriteCode db ' - Saving bootcode to file...$'
|
|---|
| 45 | COM_Okay db 'ok', 13, 10, '$'
|
|---|
| 46 | COM_Failed db 'failed', 13, 10, '$'
|
|---|
| 47 |
|
|---|
| 48 | COM_FailedOpenCode db 'air-boot.com not found', 13, 10, '$'
|
|---|
| 49 | COM_FailedReadCode db 'Read air-boot.com failed', 13, 10, '$'
|
|---|
| 50 | COM_FailedInvalidCode db 'Invalid air-boot.com', 13, 10, '$'
|
|---|
| 51 | COM_FailedOpenMBR db 'mbr-prot\mbr_prot.com not found', 13, 10, '$'
|
|---|
| 52 | COM_FailedReadMBR db 'Read mbr-prot\mbr_prot.com failed', 13, 10, '$'
|
|---|
| 53 | COM_FailedInvalidMBR db 'Invalid mbr-prot\mbr_prot.com', 13, 10, '$'
|
|---|
| 54 | COM_FailedWriteCode db 'Write air-boot.com failed', 13, 10, '$'
|
|---|
| 55 |
|
|---|
| 56 | MBRProtectionSignature db 'AiR-BOOT MBR-Protection Image'
|
|---|
| 57 | MBRProtectionSignatureLen equ 29
|
|---|
| 58 |
|
|---|
| 59 | ShowMessage Proc Near Uses ax
|
|---|
| 60 | mov ah, 09h
|
|---|
| 61 | int 21h ; DOS: WRITE STRING DS:DX TO CONSOLE
|
|---|
| 62 | ret
|
|---|
| 63 | ShowMessage EndP
|
|---|
| 64 |
|
|---|
| 65 | ShowError Proc Near Uses
|
|---|
| 66 | mov ah, 09h
|
|---|
| 67 | int 21h ; DOS: WRITE STRING DS:DX TO CONSOLE
|
|---|
| 68 | EndProgram:
|
|---|
| 69 | mov ax, 4C00h
|
|---|
| 70 | int 21h ; DOS: TERMINATE PROGRAM
|
|---|
| 71 | ShowError EndP
|
|---|
| 72 |
|
|---|
| 73 | COM_Init: mov ax, cs
|
|---|
| 74 | mov ds, ax
|
|---|
| 75 | mov es, ax ; CS==DS==ES
|
|---|
| 76 | mov dx, offset COM_Copyright
|
|---|
| 77 | call ShowMessage
|
|---|
| 78 |
|
|---|
| 79 | ; =========================================== Load air-boot.bin
|
|---|
| 80 | mov dx, offset COM_LoadCode
|
|---|
| 81 | call ShowMessage
|
|---|
| 82 | mov ax, 3D00h
|
|---|
| 83 | mov dx, offset COM_CodeName
|
|---|
| 84 | xor cl, cl
|
|---|
| 85 | int 21h ; DOS: OPEN EXISTING FILE
|
|---|
| 86 | jnc DoneOpenCode
|
|---|
| 87 | mov dx, offset COM_FailedOpenCode
|
|---|
| 88 | call ShowError
|
|---|
| 89 |
|
|---|
| 90 | DoneOpenCode: mov bx, ax ; BX = Filehandle
|
|---|
| 91 |
|
|---|
| 92 | mov ah, 3Fh
|
|---|
| 93 | mov cx, image_size ; Image size
|
|---|
| 94 | mov dx, offset BootCode
|
|---|
| 95 | int 21h ; DOS: READ FILE
|
|---|
| 96 | jnc DoneReadCode
|
|---|
| 97 | mov dx, offset COM_FailedReadCode
|
|---|
| 98 | call ShowError
|
|---|
| 99 |
|
|---|
| 100 | DoneReadCode: cmp ax, image_size
|
|---|
| 101 | je DoneReadCode2
|
|---|
| 102 | InvalidCode: mov dx, offset COM_FailedInvalidCode
|
|---|
| 103 | call ShowError
|
|---|
| 104 |
|
|---|
| 105 | DoneReadCode2: mov ah, 3Fh
|
|---|
| 106 | mov cx, 1
|
|---|
| 107 | mov dx, offset BootCode
|
|---|
| 108 | int 21h ; DOS: READ FILE
|
|---|
| 109 | jc DoneReadCode3
|
|---|
| 110 | or ax, ax
|
|---|
| 111 | jz DoneReadCode3 ; EOF -> is now expected
|
|---|
| 112 | jmp InvalidCode
|
|---|
| 113 |
|
|---|
| 114 | DoneReadCode3: mov ah, 3Eh
|
|---|
| 115 | int 21h ; DOS: CLOSE FILE
|
|---|
| 116 |
|
|---|
| 117 | mov dx, offset COM_Okay
|
|---|
| 118 | call ShowMessage
|
|---|
| 119 |
|
|---|
| 120 | ; =================================== Load MBR-protection image
|
|---|
| 121 | mov dx, offset COM_LoadMBR
|
|---|
| 122 | call ShowMessage
|
|---|
| 123 | mov ax, 3D00h
|
|---|
| 124 | mov dx, offset COM_MBRName
|
|---|
| 125 | xor cl, cl
|
|---|
| 126 | int 21h ; DOS: OPEN EXISTING FILE
|
|---|
| 127 | jnc DoneOpenMBR
|
|---|
| 128 | mov dx, offset COM_FailedOpenMBR
|
|---|
| 129 | call ShowError
|
|---|
| 130 |
|
|---|
| 131 | DoneOpenMBR: mov bx, ax ; BX = Filehandle
|
|---|
| 132 |
|
|---|
| 133 | mov ah, 3Fh
|
|---|
| 134 | mov cx, 1024 ; Image size
|
|---|
| 135 | mov dx, offset MBRProtection
|
|---|
| 136 | int 21h ; DOS: READ FILE
|
|---|
| 137 | jnc DoneReadMBR
|
|---|
| 138 | mov dx, offset COM_FailedReadMBR
|
|---|
| 139 | call ShowError
|
|---|
| 140 |
|
|---|
| 141 | DoneReadMBR: cmp ax, 1024
|
|---|
| 142 | je DoneReadMBR2
|
|---|
| 143 | InvalidMBR: mov dx, offset COM_FailedInvalidMBR
|
|---|
| 144 | call ShowError
|
|---|
| 145 |
|
|---|
| 146 | DoneReadMBR2: mov ah, 3Fh
|
|---|
| 147 | mov cx, 1
|
|---|
| 148 | mov dx, offset MBRProtection
|
|---|
| 149 | int 21h ; DOS: READ FILE
|
|---|
| 150 | jc DoneReadMBR3
|
|---|
| 151 | or ax, ax
|
|---|
| 152 | jz DoneReadMBR3 ; EOF -> is now expected
|
|---|
| 153 | jmp InvalidMBR
|
|---|
| 154 |
|
|---|
| 155 | DoneReadMBR3: mov ah, 3Eh
|
|---|
| 156 | int 21h ; DOS: CLOSE FILE
|
|---|
| 157 |
|
|---|
| 158 | mov dx, offset COM_Okay
|
|---|
| 159 | call ShowMessage
|
|---|
| 160 |
|
|---|
| 161 | ; ========================== Merge MBR-Protection into Bootcode
|
|---|
| 162 | mov dx, offset COM_MergeMBR
|
|---|
| 163 | call ShowMessage
|
|---|
| 164 |
|
|---|
| 165 | ; Search for signature in Bootcode
|
|---|
| 166 | mov si, offset BootCode
|
|---|
| 167 | mov di, offset MBRProtectionSignature
|
|---|
| 168 | mov cx, MBRProtectionSignatureLen
|
|---|
| 169 | mov dx, 54 ; 54 sectors where signature may be (all sectors preceding config sector)
|
|---|
| 170 | COM_SignatureLoop:
|
|---|
| 171 | push cx si di
|
|---|
| 172 | repe cmpsb
|
|---|
| 173 | pop di si cx
|
|---|
| 174 | je COM_GotSignature
|
|---|
| 175 | add si, 512
|
|---|
| 176 | dec dx
|
|---|
| 177 | jnz COM_SignatureLoop
|
|---|
| 178 | mov dx, offset COM_Failed
|
|---|
| 179 | call ShowMessage
|
|---|
| 180 | jmp EndProgram
|
|---|
| 181 |
|
|---|
| 182 | COM_GotSignature:
|
|---|
| 183 | ; Now copy MBR-protection into bootcode
|
|---|
| 184 | mov di, si
|
|---|
| 185 | mov si, offset MBRProtection
|
|---|
| 186 | mov cx, 512
|
|---|
| 187 | rep movsw
|
|---|
| 188 | mov dx, offset COM_Okay
|
|---|
| 189 | call ShowMessage
|
|---|
| 190 |
|
|---|
| 191 | ; ====================== Count code sectors and adjust bootcode
|
|---|
| 192 | mov dx, offset COM_CountCode
|
|---|
| 193 | call ShowMessage
|
|---|
| 194 |
|
|---|
| 195 | mov si, offset BootCode
|
|---|
| 196 | add si, 512*53
|
|---|
| 197 | mov cx, 256
|
|---|
| 198 | mov dx, 53
|
|---|
| 199 | COM_CodeEndLoop:push cx si di
|
|---|
| 200 | COM_CodeEndLoop2:
|
|---|
| 201 | cmp wptr ds:[si], 0
|
|---|
| 202 | jne COM_ExitCodeEndLoop
|
|---|
| 203 | add si, 2
|
|---|
| 204 | dec cx
|
|---|
| 205 | jnz COM_CodeEndLoop2
|
|---|
| 206 | COM_ExitCodeEndLoop:
|
|---|
| 207 | pop di si cx
|
|---|
| 208 | jne COM_FoundCodeEnd
|
|---|
| 209 | sub si, 512
|
|---|
| 210 | dec dx
|
|---|
| 211 | jnz COM_CodeEndLoop
|
|---|
| 212 | mov dx, offset COM_Failed
|
|---|
| 213 | call ShowError
|
|---|
| 214 |
|
|---|
| 215 | COM_FoundCodeEnd:
|
|---|
| 216 | mov [BootCode+10h], dl
|
|---|
| 217 | mov dx, offset COM_Okay
|
|---|
| 218 | call ShowMessage
|
|---|
| 219 |
|
|---|
| 220 | ; ================================ Save bootcode back into file
|
|---|
| 221 | mov dx, offset COM_WriteCode
|
|---|
| 222 | call ShowMessage
|
|---|
| 223 | mov ax, 3D02h
|
|---|
| 224 | mov dx, offset COM_CodeName
|
|---|
| 225 | xor cl, cl
|
|---|
| 226 | int 21h ; DOS: OPEN EXISTING FILE
|
|---|
| 227 | jnc DoneOpenCode2
|
|---|
| 228 | mov dx, offset COM_FailedOpenCode
|
|---|
| 229 | call ShowError
|
|---|
| 230 |
|
|---|
| 231 | DoneOpenCode2: mov bx, ax ; BX = Filehandle
|
|---|
| 232 |
|
|---|
| 233 | mov ah, 40h
|
|---|
| 234 | mov cx, image_size ; Image size
|
|---|
| 235 | mov dx, offset BootCode
|
|---|
| 236 | int 21h ; DOS: WRITE FILE
|
|---|
| 237 | jnc DoneWriteCode
|
|---|
| 238 | FailedWriteCode:mov dx, offset COM_FailedWriteCode
|
|---|
| 239 | call ShowError
|
|---|
| 240 |
|
|---|
| 241 | DoneWriteCode: cmp ax, image_size
|
|---|
| 242 | jne FailedWriteCode
|
|---|
| 243 |
|
|---|
| 244 | mov ah, 3Eh
|
|---|
| 245 | int 21h ; DOS: CLOSE FILE
|
|---|
| 246 |
|
|---|
| 247 | mov dx, offset COM_Okay
|
|---|
| 248 | call ShowMessage
|
|---|
| 249 | jmp EndProgram
|
|---|
| 250 |
|
|---|
| 251 | ; Buffers for files
|
|---|
| 252 | BootCode db image_size dup (?)
|
|---|
| 253 | MBRProtection db 1024 dup (?)
|
|---|
| 254 |
|
|---|
| 255 | COM_EndOfSegment:
|
|---|
| 256 |
|
|---|
| 257 | code_seg ends
|
|---|
| 258 | end COM_StartUp
|
|---|