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

4.104.114.84.95
Last change on this file since 5184958 was 5184958, checked in by Joel Sherrill <joel.sherrill@…>, on Jun 3, 1998 at 6:47:44 PM

Update from Pedro Romano <pmcnr@…> which replaces C++
versions of these utilities with C implementations.

  • Property mode set to 100644
File size: 6.1 KB
Line 
1/*-------------------------------------------------------------------------+
2| bin2boot.c v1.0 - PC386 BSP - 1998/04/09
3+--------------------------------------------------------------------------+
4| This file contains the i386 binary to boot image filter.
5+--------------------------------------------------------------------------+
6| (C) Copyright 1997 -
7| - NavIST Group - Real-Time Distributed Systems and Industrial Automation
8|
9| http://pandora.ist.utl.pt
10|
11| Instituto Superior Tecnico * Lisboa * PORTUGAL
12+--------------------------------------------------------------------------+
13| Disclaimer:
14|
15| This file is provided "AS IS" without warranty of any kind, either
16| expressed or implied.
17+--------------------------------------------------------------------------*/
18 
19#include "Image.h"
20#include "Header.h"
21#include "bytetype.h"
22
23#include <stdio.h>
24#include <stdlib.h>
25
26/*-------------------------------------------------------------------------+
27| Constants
28+--------------------------------------------------------------------------*/
29
30#define MAX_IMAGES 15
31
32/*-------------------------------------------------------------------------+
33| Global Variables
34+--------------------------------------------------------------------------*/
35
36// Help message for users
37const char UsageMsg[] = "\
38Usage: bin2boot <outFile> <locAddr> <inFile1> <startAddr1> <memSize1> \\\n\
39               [<infile2> <startAddr2> <memSize2>][...][-v][-h][-?]\n\
40\n\
41<outFile>    : output file name (mandatory)\n\
42<locAddr>    : location address in memory of image header (mandatory)\n\
43<inFileX>    : name of Xth input file\n\
44<startAddrX> : start address of Xth image\n\
45<memSizeX>   : actual size (for compressed images), use 0 if uncompressed\n\
46-v           : verbose output\n\
47-h, -?       : this help message\n\
48\n\
49At least one set of <inFile> <startAddr> and <memSize> is mandatory!\n\
50<locAddr>, <startAddrX> and <memSizeX> can be in Decimal, Hexadecimal or Octal.\n\
51The maximum number of input files is 15.\n";
52
53/*-------------------------------------------------------------------------+
54| External Prototypes (for use with getopt)
55+--------------------------------------------------------------------------*/
56
57extern char *optarg;
58
59int getopt(int, char *const[], const char *);
60
61/*-------------------------------------------------------------------------+
62| Auxiliary Functions
63+--------------------------------------------------------------------------*/
64
65static DWord
66getNumArg(char *arg)
67{
68  char *dummy;
69
70  if (arg[0] == '0')
71    if ((arg[1] == 'x') || (arg[1] == 'X'))  /* Hexadecimal */
72      return (DWord)strtol(arg, &dummy, 16);
73    else  /* Octal */
74      return (DWord)strtol(arg, &dummy, 8);
75    else  /* Decimal */
76      return (DWord)strtol(arg, &dummy, 10);
77} /* getNumArg */
78
79/*-------------------------------------------------------------------------+
80| Main
81+--------------------------------------------------------------------------*/
82
83int main(int argc, char* argv[])
84{
85  Image    img[MAX_IMAGES]; /* array to store up to MAX_IMAGE images */
86  Header   hdr;             /* boot image file header                */
87  FILE*    outFile;         /* boot image file stream                */
88  char*    outFileName;     /* name of boot image file               */
89
90  int  argPos = 1, numImages, i;
91  int  verboseFlag = 0, helpFlag = 0; /* flags for command line options */
92
93  /*-------------------------------------------------------------------------+
94  | Parse command line arguments and options
95  +-------------------------------------------------------------------------*/
96 
97  {
98    char opt;
99
100    /* parse command line options */
101    while ((opt = getopt(argc, argv, "vh?")) >= 0)
102    {
103      argPos++;
104      switch (opt)
105      {
106        case 'v' : verboseFlag = 1; break;
107        case 'h' : case '?' : helpFlag = 1; break;
108      }
109    }
110  }
111
112  if (helpFlag)
113  {
114    fprintf(stderr, "%s\n", UsageMsg);
115    if (argc == 2)
116      exit(0);
117  }
118
119  numImages = (argc - argPos - 2) / 3;
120 
121  if (numImages < 1)
122  {
123    fprintf(stderr,
124           "ERROR!!! Not enough command line arguments.\n\n%s\n",
125           UsageMsg);
126    exit(1);
127  }
128  if (numImages > 15)
129  {
130    fprintf(stderr, "ERROR!!! Too many input files.\n\n%s\n", UsageMsg);
131    exit(1);
132  }
133
134  newHeader(&hdr); /* initialize hdr */
135
136  argPos = 1;
137
138  while (argv[argPos][0] == '-') argPos++; /* discard options */
139  if(!(outFile = fopen((outFileName = argv[argPos++]), "w")))
140  {
141    fprintf(stderr,
142            "ERROR!!! While opening file '%s' for output.\n",
143            outFileName);
144    exit(1);
145  }
146  reserveSpaceHeader(outFile);
147
148  while (argv[argPos][0] == '-') argPos++; /* discard options */
149  setLocAddrHeader(&hdr, getNumArg(argv[argPos++]));
150
151  /*-------------------------------------------------------------------------+
152  | Parse command line arguments concerning images
153  +-------------------------------------------------------------------------*/
154
155  for(i = 0; i < numImages; i++)
156  {
157    newImage(&img[i]);
158    while (argv[argPos][0] == '-') argPos++; /* discard options */
159    setImageFile(&img[i], argv[argPos++]);
160    while (argv[argPos][0] == '-') argPos++; /* discard options */
161    setLoadAddrImage(&img[i], getNumArg(argv[argPos++]));
162    while (argv[argPos][0] == '-') argPos++; /* discard options */
163    setMemSizeImage(&img[i], getNumArg(argv[argPos++]));
164  }
165
166  setLstImgFlagImage(&img[numImages - 1]);        /* set flag on last image */
167  setExecAddrHeader(&hdr, getLoadAddrImage(&img[0]));
168                /* boot file execution address is the same as first image's */
169  if (verboseFlag)
170  {
171    fprintf(stderr, "BootImage file '%s' info:\n\n", outFileName);
172    fprintHeader(stderr, &hdr);
173    fprintf(stderr, "\n");
174  }
175
176  /*-------------------------------------------------------------------------+
177  | Dump images and header to file
178  +-------------------------------------------------------------------------*/
179
180  for(i = 0; i < numImages; i++)
181  {
182    addImageToHeader(&hdr, &img[i]);
183    dumpImageToFileAndPad(outFile, &img[i]);
184
185    if (verboseFlag)
186      fprintImage(stderr, &img[i]);
187
188    /* kill image. we don't need anymore and should free its resources */
189    killImage(&img[i]);
190  }
191
192  dumpHeaderToFile(outFile, &hdr);
193
194  fclose(outFile);
195
196  exit(0);
197} /* main */
198
Note: See TracBrowser for help on using the repository browser.