1 | |
---|
2 | /* |
---|
3 | * Simplyfied version of original bin2boot |
---|
4 | */ |
---|
5 | |
---|
6 | #include <stdio.h> |
---|
7 | #include <stdlib.h> |
---|
8 | #include <unistd.h> |
---|
9 | #include <string.h> |
---|
10 | |
---|
11 | static unsigned char buf[512]; |
---|
12 | |
---|
13 | static void usage(void) |
---|
14 | { |
---|
15 | printf("usage: bin2boot [-h][-v] <outFile> <headerAddr> \n"); |
---|
16 | printf("<imFile1> <imAddr1> <imSize1> [<imFile2> <imAddr2> <imSize2>]\n"); |
---|
17 | printf("this function makes image bootable by netboot\n"); |
---|
18 | printf("from one or two binary images\n"); |
---|
19 | printf("-h - prints this message\n"); |
---|
20 | printf("-v - verbose output\n"); |
---|
21 | printf("outFile - output file\n"); |
---|
22 | printf("headerAddr - address to place header in memory\n"); |
---|
23 | printf(" it should be below or equal 0x97e00\n"); |
---|
24 | printf("imFile1 - first image\n"); |
---|
25 | printf("imAddr1 - its start address, image has to be placed whole\n"); |
---|
26 | printf(" below 0x98000 and should not overlap with header\n"); |
---|
27 | printf("imSize1 - actual size of compressed image, 0 for uncompressed\n"); |
---|
28 | printf("imFile2 - second image\n"); |
---|
29 | printf("imAddr2 - its start address\n"); |
---|
30 | printf("imSize2 - actual size of compressed image, 0 for uncompressed\n"); |
---|
31 | |
---|
32 | return; |
---|
33 | } |
---|
34 | |
---|
35 | int main(int argc, char* argv[]) |
---|
36 | { |
---|
37 | int c, verbose; |
---|
38 | extern int optind; |
---|
39 | FILE *ofp, *ifp; |
---|
40 | unsigned long headerAddr, addr1, addr2; |
---|
41 | int size1, size2, len1, len2, len, imageCnt, cnt; |
---|
42 | char *ofile, *ifile, *end; |
---|
43 | |
---|
44 | len2 = 0; /* avoid warning */ |
---|
45 | size2 = 0; /* avoid warning */ |
---|
46 | addr2 = 0; /* avoid warning */ |
---|
47 | verbose = 0; |
---|
48 | |
---|
49 | /* parse command line options */ |
---|
50 | while ((c = getopt(argc, argv, "hv")) >= 0) |
---|
51 | { |
---|
52 | switch (c) |
---|
53 | { |
---|
54 | case 'v': |
---|
55 | verbose = 1; |
---|
56 | break; |
---|
57 | case 'h': |
---|
58 | usage(); |
---|
59 | return 0; |
---|
60 | default: |
---|
61 | usage(); |
---|
62 | return 1; |
---|
63 | } |
---|
64 | } |
---|
65 | |
---|
66 | if((argc - optind) != 8 && (argc - optind) != 5) |
---|
67 | { |
---|
68 | usage(); |
---|
69 | return 1; |
---|
70 | } |
---|
71 | |
---|
72 | ofile = argv[optind]; |
---|
73 | ofp = fopen(ofile, "wb"); |
---|
74 | if(ofp == NULL) |
---|
75 | { |
---|
76 | fprintf(stderr, "unable to open file %s\n", ofile); |
---|
77 | return 1; |
---|
78 | } |
---|
79 | |
---|
80 | /* |
---|
81 | * Layout is very simple first 512 is header shared by all |
---|
82 | * images, then images at 512 bytes border |
---|
83 | */ |
---|
84 | |
---|
85 | /* Fill buffer with 0's */ |
---|
86 | memset(buf, 0, sizeof(buf)); |
---|
87 | |
---|
88 | fwrite(buf, 1, sizeof(buf), ofp); |
---|
89 | |
---|
90 | optind++; |
---|
91 | headerAddr = strtoul(argv[optind], &end, 0); |
---|
92 | if(end == argv[optind]) |
---|
93 | { |
---|
94 | fprintf(stderr, "bad headerAddr %s\n", argv[optind]); |
---|
95 | fclose(ofp); |
---|
96 | return 1; |
---|
97 | } |
---|
98 | |
---|
99 | if(headerAddr > 0x97e00) |
---|
100 | { |
---|
101 | fprintf(stderr, "headerAddr is too high 0x%08lx\n", headerAddr); |
---|
102 | fclose(ofp); |
---|
103 | return 1; |
---|
104 | } |
---|
105 | |
---|
106 | /* Copy the first image */ |
---|
107 | optind++; |
---|
108 | ifile = argv[optind]; |
---|
109 | ifp = fopen(ifile,"rb"); |
---|
110 | if(ifp == NULL) |
---|
111 | { |
---|
112 | fprintf(stderr, "unable to open output file %s\n", ifile); |
---|
113 | fclose(ofp); |
---|
114 | return 1; |
---|
115 | } |
---|
116 | |
---|
117 | optind++; |
---|
118 | addr1 = strtoul(argv[optind], &end, 0); |
---|
119 | if(end == argv[optind]) |
---|
120 | { |
---|
121 | fprintf(stderr, "bad image address %s\n", argv[optind]); |
---|
122 | fclose(ofp); |
---|
123 | return 1; |
---|
124 | } |
---|
125 | |
---|
126 | optind++; |
---|
127 | size1 = strtoul(argv[optind], &end, 0); |
---|
128 | if(end == argv[optind]) |
---|
129 | { |
---|
130 | fprintf(stderr, "bad image size %s\n", argv[optind]); |
---|
131 | fclose(ofp); |
---|
132 | return 1; |
---|
133 | } |
---|
134 | |
---|
135 | /* Copy first image out and remember its length */ |
---|
136 | cnt = 0; |
---|
137 | for(;;) |
---|
138 | { |
---|
139 | len = fread(buf, 1, sizeof(buf), ifp); |
---|
140 | |
---|
141 | if(len != 0) |
---|
142 | { |
---|
143 | fwrite(buf, 1, len, ofp); |
---|
144 | cnt += sizeof(buf); |
---|
145 | |
---|
146 | if(len != sizeof(buf)) |
---|
147 | { |
---|
148 | memset(buf, 0, sizeof(buf) - len); |
---|
149 | fwrite(buf, 1, sizeof(buf) - len, ofp); |
---|
150 | break; |
---|
151 | } |
---|
152 | |
---|
153 | } |
---|
154 | else |
---|
155 | { |
---|
156 | break; |
---|
157 | } |
---|
158 | } |
---|
159 | |
---|
160 | fclose(ifp); |
---|
161 | |
---|
162 | len1 = cnt; |
---|
163 | |
---|
164 | if(size1 == 0) |
---|
165 | { |
---|
166 | size1 = cnt; |
---|
167 | } |
---|
168 | else |
---|
169 | { |
---|
170 | memset(buf, 0, sizeof(buf)); |
---|
171 | |
---|
172 | while(cnt < size1) |
---|
173 | { |
---|
174 | fwrite(buf, 1, sizeof(buf), ofp); |
---|
175 | cnt += sizeof(buf); |
---|
176 | } |
---|
177 | |
---|
178 | size1 = cnt; |
---|
179 | } |
---|
180 | |
---|
181 | /* Let us check agains overlapping */ |
---|
182 | if(!(addr1 >= (headerAddr + sizeof(buf)) || (headerAddr >= addr1+size1))) |
---|
183 | { |
---|
184 | /* Areas overlapped */ |
---|
185 | printf("area overlapping: \n"); |
---|
186 | printf("header address 0x%08lx, its memory size 0x%08x\n", |
---|
187 | headerAddr, sizeof(buf)); |
---|
188 | printf("first image address 0x%08lx, its memory size 0x%08x\n", |
---|
189 | addr1, size1); |
---|
190 | |
---|
191 | fclose(ofp); |
---|
192 | return 1; |
---|
193 | } |
---|
194 | |
---|
195 | if((addr1 + size1) > 0x98000) |
---|
196 | { |
---|
197 | fprintf(stderr, "imAddr1 is too high 0x%08lx\n", addr1); |
---|
198 | fclose(ofp); |
---|
199 | return 1; |
---|
200 | } |
---|
201 | |
---|
202 | if(optind == (argc - 1)) |
---|
203 | { |
---|
204 | imageCnt = 1; |
---|
205 | goto writeHeader; |
---|
206 | } |
---|
207 | |
---|
208 | imageCnt = 2; |
---|
209 | |
---|
210 | /* Copy Second Image */ |
---|
211 | optind++; |
---|
212 | ifile = argv[optind]; |
---|
213 | ifp = fopen(ifile,"rb"); |
---|
214 | if(ifp == NULL) |
---|
215 | { |
---|
216 | fprintf(stderr, "unable to open output file %s\n", ifile); |
---|
217 | fclose(ofp); |
---|
218 | return 1; |
---|
219 | } |
---|
220 | |
---|
221 | optind++; |
---|
222 | addr2 = strtoul(argv[optind], &end, 0); |
---|
223 | if(end == argv[optind]) |
---|
224 | { |
---|
225 | fprintf(stderr, "bad image address %s\n", argv[optind]); |
---|
226 | fclose(ofp); |
---|
227 | return 1; |
---|
228 | } |
---|
229 | |
---|
230 | optind++; |
---|
231 | size2 = strtoul(argv[optind], &end, 0); |
---|
232 | if(end == argv[optind]) |
---|
233 | { |
---|
234 | fprintf(stderr, "bad image size %s\n", argv[optind]); |
---|
235 | fclose(ofp); |
---|
236 | return 1; |
---|
237 | } |
---|
238 | |
---|
239 | /* Copy second image out and remember its length */ |
---|
240 | cnt = 0; |
---|
241 | for(;;) |
---|
242 | { |
---|
243 | len = fread(buf, 1, sizeof(buf), ifp); |
---|
244 | |
---|
245 | if(len != 0) |
---|
246 | { |
---|
247 | fwrite(buf, len, 1, ofp); |
---|
248 | cnt += sizeof(buf); |
---|
249 | |
---|
250 | if(len != sizeof(buf)) |
---|
251 | { |
---|
252 | memset(buf, 0, sizeof(buf) - len); |
---|
253 | fwrite(buf, 1, sizeof(buf) - len, ofp); |
---|
254 | break; |
---|
255 | } |
---|
256 | } |
---|
257 | else |
---|
258 | { |
---|
259 | break; |
---|
260 | } |
---|
261 | } |
---|
262 | |
---|
263 | fclose(ifp); |
---|
264 | |
---|
265 | len2 = cnt; |
---|
266 | |
---|
267 | if(size2 == 0) |
---|
268 | { |
---|
269 | size2 = cnt; |
---|
270 | } |
---|
271 | else |
---|
272 | { |
---|
273 | memset(buf, 0, sizeof(buf)); |
---|
274 | |
---|
275 | while(cnt < size2) |
---|
276 | { |
---|
277 | fwrite(buf, 1, sizeof(buf), ofp); |
---|
278 | cnt += sizeof(buf); |
---|
279 | } |
---|
280 | |
---|
281 | size2 = cnt; |
---|
282 | } |
---|
283 | |
---|
284 | /* Let us check against overlapping */ |
---|
285 | if(!((addr2 >= (addr1 + size1) && addr2 >= (headerAddr + sizeof(buf))) || |
---|
286 | (addr2 < addr1 && addr2 < headerAddr) || |
---|
287 | (addr1 > headerAddr && addr2 > (headerAddr + sizeof(buf)) && |
---|
288 | (addr2 + size2) <= addr1) || |
---|
289 | (addr1 < headerAddr && addr2 > (addr1 + size1) && |
---|
290 | (addr2 + size2) <= headerAddr))) |
---|
291 | |
---|
292 | { |
---|
293 | /* Areas overlapped */ |
---|
294 | printf("area overlapping: \n"); |
---|
295 | printf("header address 0x%08lx, its memory size 0x%08x\n", |
---|
296 | headerAddr, sizeof(buf)); |
---|
297 | printf("first image address 0x%08lx, its memory size 0x%08x\n", |
---|
298 | addr1, size1); |
---|
299 | printf("second image address 0x%08lx, its memory size 0x%08x\n", |
---|
300 | addr2, size2); |
---|
301 | |
---|
302 | fclose(ofp); |
---|
303 | return 1; |
---|
304 | } |
---|
305 | |
---|
306 | writeHeader: |
---|
307 | |
---|
308 | /* We know everything so it is time to write buffer */ |
---|
309 | memset(buf, 0, 0x30); |
---|
310 | |
---|
311 | buf[0x0] = 0x36; |
---|
312 | buf[0x1] = 0x13; |
---|
313 | buf[0x2] = 0x03; |
---|
314 | buf[0x3] = 0x1b; |
---|
315 | |
---|
316 | buf[0x4] = 4; |
---|
317 | |
---|
318 | /* Header address in ds:bx format */ |
---|
319 | buf[0x8] = headerAddr & 0xf; |
---|
320 | buf[0x9] = 0; |
---|
321 | buf[0xa] = (headerAddr >> 4) & 0xff; |
---|
322 | buf[0xb] = (headerAddr >> 12) & 0xff; |
---|
323 | |
---|
324 | /* |
---|
325 | * Execute address in cs:ip format, which addr1 |
---|
326 | */ |
---|
327 | buf[0xc] = addr1 & 0xf; |
---|
328 | buf[0xd] = 0; |
---|
329 | buf[0xe] = (addr1 >> 4) & 0xff; |
---|
330 | buf[0xf] = (addr1 >> 12) & 0xff; |
---|
331 | |
---|
332 | /* Flags, tags and lengths */ |
---|
333 | buf[0x10] = 4; |
---|
334 | |
---|
335 | if(imageCnt == 1) |
---|
336 | { |
---|
337 | buf[0x13] = 4; |
---|
338 | } |
---|
339 | |
---|
340 | /* Load address */ |
---|
341 | buf[0x14] = addr1 & 0xff; |
---|
342 | buf[0x15] = (addr1 >> 8) & 0xff; |
---|
343 | buf[0x16] = (addr1 >> 16) & 0xff; |
---|
344 | buf[0x17] = (addr1 >> 24) & 0xff; |
---|
345 | |
---|
346 | /* Image Length */ |
---|
347 | buf[0x18] = len1 & 0xff; |
---|
348 | buf[0x19] = (len1 >> 8) & 0xff; |
---|
349 | buf[0x1a] = (len1 >> 16) & 0xff; |
---|
350 | buf[0x1b] = (len1 >> 24) & 0xff; |
---|
351 | |
---|
352 | /* Memory Size */ |
---|
353 | buf[0x1c] = size1 & 0xff; |
---|
354 | buf[0x1d] = (size1 >> 8) & 0xff; |
---|
355 | buf[0x1e] = (size1 >> 16) & 0xff; |
---|
356 | buf[0x1f] = (size1 >> 24) & 0xff; |
---|
357 | |
---|
358 | if(imageCnt != 1) |
---|
359 | { |
---|
360 | |
---|
361 | /* Flags, tags and lengths */ |
---|
362 | buf[0x20] = 4; |
---|
363 | |
---|
364 | buf[0x23] = 4; |
---|
365 | |
---|
366 | /* Load address */ |
---|
367 | buf[0x24] = addr2 & 0xff; |
---|
368 | buf[0x25] = (addr2 >> 8) & 0xff; |
---|
369 | buf[0x26] = (addr2 >> 16) & 0xff; |
---|
370 | buf[0x27] = (addr2 >> 24) & 0xff; |
---|
371 | |
---|
372 | /* Image Length */ |
---|
373 | buf[0x28] = len2 & 0xff; |
---|
374 | buf[0x29] = (len2 >> 8) & 0xff; |
---|
375 | buf[0x2a] = (len2 >> 16) & 0xff; |
---|
376 | buf[0x2b] = (len2 >> 24) & 0xff; |
---|
377 | |
---|
378 | /* Memory Size */ |
---|
379 | buf[0x2c] = size2 & 0xff; |
---|
380 | buf[0x2d] = (size2 >> 8) & 0xff; |
---|
381 | buf[0x2e] = (size2 >> 16) & 0xff; |
---|
382 | buf[0x2f] = (size2 >> 24) & 0xff; |
---|
383 | } |
---|
384 | |
---|
385 | rewind(ofp); |
---|
386 | |
---|
387 | fwrite(buf, 1, 0x30, ofp); |
---|
388 | |
---|
389 | fclose(ofp); |
---|
390 | |
---|
391 | if(verbose) |
---|
392 | { |
---|
393 | printf("header address 0x%08lx, its memory size 0x%08x\n", |
---|
394 | headerAddr, sizeof(buf)); |
---|
395 | printf("first image address 0x%08lx, its memory size 0x%08x\n", |
---|
396 | addr1, size1); |
---|
397 | printf("second image address 0x%08lx, its memory size 0x%08x\n", |
---|
398 | addr2, size2); |
---|
399 | } |
---|
400 | |
---|
401 | return 0; |
---|
402 | } |
---|