OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [rtems/] [c/] [src/] [lib/] [libbsp/] [i386/] [pc386/] [tools/] [bin2boot.c] - Blame information for rev 173

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.