source: rtems/c/src/lib/libbsp/i386/pc386/tools/bin2boot.c @ 9b4422a2

4.115
Last change on this file since 9b4422a2 was 9b4422a2, checked in by Joel Sherrill <joel.sherrill@…>, on 05/03/12 at 15:09:24

Remove All CVS Id Strings Possible Using a Script

Script does what is expected and tries to do it as
smartly as possible.

+ remove occurrences of two blank comment lines

next to each other after Id string line removed.

+ remove entire comment blocks which only exited to

contain CVS Ids

+ If the processing left a blank line at the top of

a file, it was removed.

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