summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/i386/pc386/tools/bin2boot.c
blob: 4449d81fb908a1166243bb8ef017893d01a782dd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*-------------------------------------------------------------------------+
| bin2boot.c v1.1 - PC386 BSP - 1997/08/18
+--------------------------------------------------------------------------+
| This file contains the i386 binary to boot image filter.
+--------------------------------------------------------------------------+
| (C) Copyright 1997 -
| - NavIST Group - Real-Time Distributed Systems and Industrial Automation
|
| http://pandora.ist.utl.pt
|
| Instituto Superior Tecnico * Lisboa * PORTUGAL
+--------------------------------------------------------------------------+
| Disclaimer:
|
| This file is provided "AS IS" without warranty of any kind, either
| expressed or implied.
|
|  $Id$
+--------------------------------------------------------------------------*/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "bytetype.h"
#include "bootimg.h"

/*-------------------------------------------------------------------------+
| Constants
+--------------------------------------------------------------------------*/
#define SEG_MASK            0xffff0000
                                /* Mask for segment part of seg:off address. */
#define OFF_MASK            0x0000ffff
                                 /* Mask for offset part of seg:off address. */
#define DEFAULT_START_ADDR  0x10200     /* Default start address for binary. */
#define BOOTIMG_HDR_SIZE    0x0200      /* Size of output file header.       */
#define BUFFER_BLOCK_SIZE   0x1000      /* Size of transfer buffer size.     */
#define LAST_BLOCK_SIZE     0x0200
                 /* The output file must have a size multiple of this value. */


/*-------------------------------------------------------------------------+
| Macros
+--------------------------------------------------------------------------*/
#define getSeg(x) (Word)((x) >> 16)
                    /* Return seg part (Word) of a seg:off address (DWord). */ 

#define getOff(x) (Word)((x) & OFF_MASK)
                    /* Return off part (Word) of a seg:off address (DWord). */ 

#define getSegOff(x) ((((x) & SEG_MASK) << 12) + ((x) & OFF_MASK))
                           /* Converts a flat address to a seg:off address. */ 

/*-------------------------------------------------------------------------+
| Global Variables
+--------------------------------------------------------------------------*/
const char UsageMsg[] = "\
Usage: bin2boot [<infile> [<outfile>]] [-s <start_address>] [-v]\n\
\n\
    <infile>     : input file name\n\
    <outfile>    : output file name\n\
    -s <outfile> : start address of binary image\n\
    -m <size>    : actual size (for compressed images)\n\
    -v           : verbose output\n";                     /* Usage message. */


/*-------------------------------------------------------------------------+
| External Prototypes (for use with getopt)
+--------------------------------------------------------------------------*/
extern char *optarg;

int getopt(int, char *const[], const char *);


/*-------------------------------------------------------------------------+
| Auxiliary Functions
+--------------------------------------------------------------------------*/
static DWord
getNumArg(char *arg)
{
  char *dummy;

  if (arg[0] == '0')
    if ((arg[1] == 'x') || (arg[1] == 'X'))  /* Hexadecimal */
      return (DWord)strtol(arg, &dummy, 16); 
    else  /* Octal */
      return (DWord)strtol(arg, &dummy, 8); 
    else  /* Decimal */
      return (DWord)strtol(arg, &dummy, 10); 
} /* getNumArg */


/*-------------------------------------------------------------------------+
| Main
+--------------------------------------------------------------------------*/
void main(int argc, char *argv[])
{
  FileHeader  bootimgFileHdr;             /* Output file header.             */
  LoadingInfo imageInfo;                  /* Section header.                 */
  Byte        auxBuf[BUFFER_BLOCK_SIZE];  /* */
  DWord       nRead;                      /* Number of bytes read.           */
  Word        padSize;                    /* Size of padding at end of file. */

  char *progName = argv[0];               /* Program name for errors.        */  
  FILE *fpIn     = stdin;
  FILE *fpOut    = stdout;
  DWord binStart = DEFAULT_START_ADDR;    /* Start address of image.         */
  DWord memLen   = 0;                  /* Real length for compressed images. */

  char currArg;
  int  argCount;
  int  flag;          /* general purpose flag    */
  int  verbose = 0;   /* flag for verbose output */

  while ((currArg = getopt(argc, argv, "hm:s:v")) >= 0)  /* parse command line */
    switch (currArg)
    {
      case 'h' :
        fprintf(stderr, UsageMsg);
        exit(0);
        break;
      case 'm' :
        memLen = getNumArg(optarg); 
        break;
      case 's' :
        binStart = getNumArg(optarg); 
        break;
      case 'v' :
        verbose = 1;
        break;
      default :
        fprintf(stderr, UsageMsg);
        exit(1);
        break;
    } /* switch */

  flag = 0;

  for (argCount = 1; argCount < argc; argCount++)
    if (argv[argCount][0] == '-')
    {
      if (argv[argCount][1] == 's')
        argCount++;
    }
    else if (flag)  /* we already have the input file => output file */
    {
      if ((fpOut = fopen(argv[argCount], "w")) == NULL)
      {
        fprintf(stderr, "%s: can't open %s\n", progName, argv[argCount]);
        exit(1);
      }
    }
    else /* input file */
    {
      if ((fpIn = fopen(argv[argCount], "r")) == NULL)
      {
        fprintf(stderr, "%s: can't open %s\n", progName, argv[argCount]);
        exit(1);
      }
      flag = 1;
    }

  /*** begin: Conversion to Bootimg */

  /*** File Header */

  if (verbose)
    fprintf(stderr, "\nBoot Image File Header:\n\n");

  bootimgFileHdr.magicNum = BOOT_IMAGE_MAGIC;  /* 4 bytes - magic number     */
  bootimgFileHdr.flagsLen = NON_VENDOR_LEN;    /* 4 bytes - flags and length */
  bootimgFileHdr.locAddr = getSegOff(binStart - BOOTIMG_HDR_SIZE);
                               /* 4 bytes - location address in ds:bx format */
  bootimgFileHdr.execAddr = getSegOff(binStart);
                                /* 4 bytes - execute address in cs:ip format */

  if (verbose)
  {
    fprintf(stderr, ">> location address in ds:bx format: %04x:%04x\n",
              getSeg(bootimgFileHdr.locAddr), getOff(bootimgFileHdr.locAddr));
    fprintf(stderr, ">>  execute address in cs:ip format: %04x:%04x\n",
              getSeg(bootimgFileHdr.execAddr), getOff(bootimgFileHdr.execAddr));
  }

  /*** Write File Header to output file */

  fwrite((void *)(& bootimgFileHdr), sizeof(FileHeader), 1, fpOut);

  /*** Sections */

  if (verbose)
    fprintf(stderr, "\nCode Section:\n\n");

  imageInfo.flagsTagsLens = 0x04000004;
                       /* flags, vendor's tags, vendor and non-vendor lengths */
  imageInfo.loadAddr      = binStart;                        /* load address  */

  rewind(fpIn);
  fseek(fpIn, 0, SEEK_END);
  nRead = ftell(fpIn);
  rewind(fpIn);
  padSize = LAST_BLOCK_SIZE - (nRead % LAST_BLOCK_SIZE);
  imageInfo.imageLength  = nRead + padSize;                  /* image length  */
  imageInfo.memoryLength = (memLen != 0) ? memLen : imageInfo.imageLength;
                                                             /* memory length */

  fwrite((void *)(&imageInfo), sizeof(LoadingInfo), 1, fpOut); 

  if (verbose)
  {
    fprintf(stderr, ">>  load address: 0x%08lx\n", imageInfo.loadAddr);
    fprintf(stderr, ">>  image length: 0x%08lx\n", imageInfo.imageLength);
    fprintf(stderr, ">> memory length: 0x%08lx\n\n", imageInfo.memoryLength);
  }

  nRead = BOOTIMG_HDR_SIZE - sizeof(FileHeader) - sizeof(LoadingInfo);
  memset((void *)auxBuf, 0x00, nRead);
  fwrite((void *)auxBuf, 1, nRead, fpOut);

  nRead = fread((void *)auxBuf, 1, BUFFER_BLOCK_SIZE, fpIn);

  while (!feof(fpIn))
  {
    fwrite((void *)auxBuf, BUFFER_BLOCK_SIZE, 1, fpOut); 
    nRead = fread((void *)auxBuf, 1, BUFFER_BLOCK_SIZE, fpIn);
  }

  fwrite((void *)auxBuf, 1, nRead, fpOut);

  memset((void *)auxBuf, 0x00, padSize);
  fwrite((void *)auxBuf, 1, padSize, fpOut);

  fclose(fpOut);
  fclose(fpIn); 

  /*** end: Conversion to Bootimg */
  
  exit(0);

} /* main */