source: trunk/TOOLS/INTERNAL/FIXCODE.ASM@ 46

Last change on this file since 46 was 46, checked in by Ben Rietbroek, 11 years ago

Various Changes [2012-04-14]

WARNING!!

All commits upto and including the commit of [2012-05-13] contain
a severe bug!! Building from these sources and then disabling
the 'force LBA' feature while also using the drive-letter feature or
editing the label can DESTROY THE MBR on ALL ATTACHED DISKS!!
DO NOT DISABLE 'FORCE LBA USAGE' WHEN BUILT FROM THE THESE COMMITS!!

Changes

o Added BLDLEVEL support
o Enhanced Master Make
o Sanitized sources
o Support for Wasm and Masm6 (experimental)
o Renamed MBR_PROT.ASM to MBR-PROT.ASM
o Merged bitfield code Into Installer
o First steps for cross platform Installer
o More...

File size: 10.6 KB
Line 
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
25Include ../../INCLUDE/ASM.INC
26
27 .286p
28 .model small, basic
29
30fixcode group code_seg,bss_data
31
32code_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
36COM_StartUp: jmp COM_Init
37
38COM_Copyright db 'AiR-BOOT Bootcode Image Fix', 13, 10
39 db ' - (c) Copyright 2009 by M. Kiewitz', 13, 10, '$'
40COM_LoadCode db ' - Loading bootcode from file...$'
41COM_CodeName db 'AIR-BOOT.COM', 0
42COM_LoadMBR db ' - Loading MBR-protection from file...$'
43COM_MBRName db 'MBR-PROT\MBR-PROT.BIN', 0
44COM_MergeMBR db ' - Merging MBR-protection into bootcode...$'
45COM_CountCode db ' - Count code in bootcode-image...$'
46COM_WriteCode db ' - Saving bootcode to file...$'
47COM_Okay db 'ok', 13, 10, '$'
48COM_Failed db 'failed', 13, 10, '$'
49
50COM_FailedOpenCode db 'air-boot.com not found', 13, 10, '$'
51COM_FailedReadCode db 'Read air-boot.com failed', 13, 10, '$'
52COM_FailedInvalidCode db 'Invalid air-boot.com', 13, 10, '$'
53COM_FailedOpenMBR db 'mbr-prot\mbr-prot.bin not found', 13, 10, '$'
54COM_FailedReadMBR db 'Read mbr-prot\mbr-prot.bin failed', 13, 10, '$'
55COM_FailedInvalidMBR db 'Invalid mbr-prot\mbr-prot.bin', 13, 10, '$'
56COM_FailedWriteCode db 'Write air-boot.com failed', 13, 10, '$'
57
58MBRProtectionSignature db 'AiR-BOOT MBR-Protection Image'
59MBRProtectionSignatureLen equ 29
60
61ShowMessage Proc Near Uses ax
62 mov ah, 09h
63 int 21h ; DOS: WRITE STRING DS:DX TO CONSOLE
64 ret
65ShowMessage EndP
66
67ShowError 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
73ShowError EndP
74
75COM_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
94DoneOpenCode: 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
106DoneReadCode:
107 ; See if at least 'image-size' is loaded.
108 cmp ax, image_size
109 je DoneReadCode2
110InvalidCode: 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.
115DoneReadCode2: 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.
125DoneReadCode3: 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
142DoneOpenMBR: 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
153DoneReadMBR:
154 ; See if at least 1kB is loaded.
155 cmp ax, 1024
156 je DoneReadMBR2
157InvalidMBR: 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.
162DoneReadMBR2: 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.
172DoneReadMBR3: 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
193COM_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
210COM_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
227COM_CodeEndLoop:push cx
228 push si
229 push di
230COM_CodeEndLoop2:
231 cmp wptr ds:[si], 0
232 jne COM_ExitCodeEndLoop
233 add si, 2
234 dec cx
235 jnz COM_CodeEndLoop2
236COM_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
247COM_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
263DoneOpenCode2: 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
270FailedWriteCode:mov dx, offset COM_FailedWriteCode
271 call ShowError
272
273DoneWriteCode: 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
285EndProgram:
286 ; DOS: TERMINATE PROGRAM
287 mov ah, 04Ch
288 int 21h
289
290
291
292COM_EndOfSegment:
293
294code_seg ends
295
296bss_data segment use16 public 'BSS'
297; Buffers for files
298BootCode db image_size dup (?)
299MBRProtection db 1024 dup (?)
300bss_data ends
301
302 end COM_StartUp
Note: See TracBrowser for help on using the repository browser.