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

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

BSS Corruption Problem located (auxdebug on) [2012-02-21]

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!!

Problem

o Function with Xrefs goes out-of-bounds because hideparttable is too small

Has only 30 entries and should be 45.
Lost partition checker initializes out-of-bounds.

Info

o About the hideparttable

For each partition it can be specified which other partitions need to
be hidden when that partition is booted. This is useful for legacy DOS
but also braindead Windows that presents HPFS/JFS partitions as
unformatted and tries to persuade the user to format them.
With v1.07 the numer of partitions that can be handled was expanded from
30 to 45, but the size of the hideparttable was overseen.

o The need to compress the hideparttable

The old size was 30x30=900 bytes while the required size is 45x45=2045 bytes.
This amount of space is not available in the image.
Since 6 bits are enough to identify the partition number to be hidden,
the solution is to devide the table into bitfields. This will result
in a table of (45*45*6)/8=1519 bytes, which can be fitted.

Changes

Revamped the sources quite a bit and moved the history to a separate
file. (AIR-BOOT.HIS)

New

o FIXCODE script for Linux

Just until the C version is ready...

File size: 10.4 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.COM', 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.com not found', 13, 10, '$'
54COM_FailedReadMBR db 'Read mbr-prot\mbr_prot.com failed', 13, 10, '$'
55COM_FailedInvalidMBR db 'Invalid mbr-prot\mbr_prot.com', 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 jmp EndProgram
71 ret
72ShowError EndP
73
74COM_Init:
75 ; Setup and Copyright.
76 mov ax, cs
77 mov ds, ax
78 mov es, ax ; CS==DS==ES
79 mov dx, offset COM_Copyright
80 call ShowMessage
81
82 ; Open AIR-BOOT.COM
83 mov dx, offset COM_LoadCode
84 call ShowMessage
85 mov ax, 3D00h
86 mov dx, offset COM_CodeName
87 xor cl, cl
88 int 21h ; DOS: OPEN EXISTING FILE
89 jnc DoneOpenCode
90 mov dx, offset COM_FailedOpenCode
91 call ShowError
92
93DoneOpenCode: mov bx, ax ; BX = Filehandle
94
95 ; Load AIR-BOOT.COM
96 mov ah, 3Fh
97 mov cx, image_size ; Image size
98 mov dx, offset BootCode
99 int 21h ; DOS: READ FILE
100 jnc DoneReadCode
101 mov dx, offset COM_FailedReadCode
102 call ShowError
103
104
105DoneReadCode:
106 ; See if at least 'image-size' is loaded.
107 cmp ax, image_size
108 je DoneReadCode2
109InvalidCode: mov dx, offset COM_FailedInvalidCode
110 call ShowError
111
112 ; Try to read again which is expected to fail.
113 ; Otherwise image is too large.
114DoneReadCode2: mov ah, 3Fh
115 mov cx, 1
116 mov dx, offset BootCode
117 int 21h ; DOS: READ FILE
118 jc DoneReadCode3
119 or ax, ax
120 jz DoneReadCode3 ; EOF -> is now expected
121 jmp InvalidCode
122
123 ; It's loaded now, close it.
124DoneReadCode3: mov ah, 3Eh
125 int 21h ; DOS: CLOSE FILE
126
127 mov dx, offset COM_Okay
128 call ShowMessage
129
130 ; Open MBR_PROT.COM
131 mov dx, offset COM_LoadMBR
132 call ShowMessage
133 mov ax, 3D00h
134 mov dx, offset COM_MBRName
135 xor cl, cl
136 int 21h ; DOS: OPEN EXISTING FILE
137 jnc DoneOpenMBR
138 mov dx, offset COM_FailedOpenMBR
139 call ShowError
140
141DoneOpenMBR: mov bx, ax ; BX = Filehandle
142
143 ; Load MBR_PROT.COM
144 mov ah, 3Fh
145 mov cx, 1024 ; Image size
146 mov dx, offset MBRProtection
147 int 21h ; DOS: READ FILE
148 jnc DoneReadMBR
149 mov dx, offset COM_FailedReadMBR
150 call ShowError
151
152DoneReadMBR:
153 ; See if at least 1kB is loaded.
154 cmp ax, 1024
155 je DoneReadMBR2
156InvalidMBR: mov dx, offset COM_FailedInvalidMBR
157 call ShowError
158
159 ; Try to read again which is expected to fail.
160 ; Otherwise image is too large.
161DoneReadMBR2: mov ah, 3Fh
162 mov cx, 1
163 mov dx, offset MBRProtection
164 int 21h ; DOS: READ FILE
165 jc DoneReadMBR3
166 or ax, ax
167 jz DoneReadMBR3 ; EOF -> is now expected
168 jmp InvalidMBR
169
170 ; It's loaded now, close file.
171DoneReadMBR3: mov ah, 3Eh
172 int 21h ; DOS: CLOSE FILE
173
174 mov dx, offset COM_Okay
175 call ShowMessage
176
177
178
179 ; ========================== Merge MBR-Protection into Bootcode
180 mov dx, offset COM_MergeMBR
181 call ShowMessage
182
183 ; Search for signature in Bootcode
184 ; Note the search is with sector granularity.
185 ; This means the signature must be 512 bytes aligned.
186 mov si, offset BootCode
187 mov di, offset MBRProtectionSignature
188 mov cx, MBRProtectionSignatureLen
189 ; 54 sectors where signature may be.
190 ; (all sectors preceding config sector)
191 mov dx, 54
192COM_SignatureLoop:
193 push cx
194 push si
195 push di
196 repe cmpsb
197 pop di
198 pop si
199 pop cx
200 je COM_GotSignature
201 add si, 512
202 dec dx
203 jnz COM_SignatureLoop
204 mov dx, offset COM_Failed
205 call ShowMessage
206 jmp EndProgram
207
208COM_GotSignature:
209 ; Now copy MBR-protection into bootcode
210 mov di, si
211 mov si, offset MBRProtection
212 mov cx, 512
213 rep movsw
214 mov dx, offset COM_Okay
215 call ShowMessage
216
217 ; ====================== Count code sectors and adjust bootcode
218 mov dx, offset COM_CountCode
219 call ShowMessage
220
221 mov si, offset BootCode
222 add si, 512*53
223 mov cx, 256
224 mov dx, 53
225COM_CodeEndLoop:push cx
226 push si
227 push di
228COM_CodeEndLoop2:
229 cmp wptr ds:[si], 0
230 jne COM_ExitCodeEndLoop
231 add si, 2
232 dec cx
233 jnz COM_CodeEndLoop2
234COM_ExitCodeEndLoop:
235 pop di
236 pop si
237 pop cx
238 jne COM_FoundCodeEnd
239 sub si, 512
240 dec dx
241 jnz COM_CodeEndLoop
242 mov dx, offset COM_Failed
243 call ShowError
244
245COM_FoundCodeEnd:
246 mov [BootCode+10h], dl
247 mov dx, offset COM_Okay
248 call ShowMessage
249
250 ; ================================ Save bootcode back into file
251 mov dx, offset COM_WriteCode
252 call ShowMessage
253 mov ax, 3D02h
254 mov dx, offset COM_CodeName
255 xor cl, cl
256 int 21h ; DOS: OPEN EXISTING FILE
257 jnc DoneOpenCode2
258 mov dx, offset COM_FailedOpenCode
259 call ShowError
260
261DoneOpenCode2: mov bx, ax ; BX = Filehandle
262
263 mov ah, 40h
264 mov cx, image_size ; Image size
265 mov dx, offset BootCode
266 int 21h ; DOS: WRITE FILE
267 jnc DoneWriteCode
268FailedWriteCode:mov dx, offset COM_FailedWriteCode
269 call ShowError
270
271DoneWriteCode: cmp ax, image_size
272 jne FailedWriteCode
273
274 mov ah, 3Eh
275 int 21h ; DOS: CLOSE FILE
276
277 mov dx, offset COM_Okay
278 call ShowMessage
279 jmp EndProgram
280
281
282EndProgram:
283 ; DOS: TERMINATE PROGRAM
284 mov ax, 4C00h
285 int 21h
286
287
288
289COM_EndOfSegment:
290
291code_seg ends
292
293bss_data segment use16 public 'BSS'
294; Buffers for files
295BootCode db image_size dup (?)
296MBRProtection db 1024 dup (?)
297bss_data ends
298
299 end COM_StartUp
Note: See TracBrowser for help on using the repository browser.