]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - keystone-rtos/ibl.git/blob - src/util/btoccs/b2ccs.c
Version 0.4 from Mike Line
[keystone-rtos/ibl.git] / src / util / btoccs / b2ccs.c
1 /* Convert a hex b file into a ccs data file */
3 #include <stdio.h>
4 #include <malloc.h>
7 int asciiByte (unsigned char c)
8 {
9   if ((c >= '0') && (c <= '9'))
10     return (1);
12   if ((c >= 'A') && (c <= 'F'))
13     return (1);
15   return (0);
16 }
18 int toNum (unsigned char c)
19 {
20   if ((c >= '0') && (c <= '9'))
21     return (c - '0');
23   return (c - 'A' + 10);
25 }
28 void  stripLine (FILE *s)
29 {
30   char iline[132];
32   fgets (iline, 131, s);
34 }
36 /* Read a .b file. */
37 int readBFile (FILE *s, unsigned char *data, unsigned maxSize)
38 {
39   unsigned char x, y;
40   int byteCount = 0;
42   /* Strip the 1st two lines */
43   stripLine (s);
44   stripLine (s);
46   for (;;) {
48     /* read the 1st ascii char */
49     do  {
50       x = fgetc (s);
51       if (x == (unsigned char)EOF)
52         return (byteCount);
54     } while (!asciiByte(x));
56     /* Read the next ascii char */
57     y = fgetc (s);
58     if (y == (unsigned char)EOF)
59       return (byteCount);
60     if (asciiByte(y))
61       data[byteCount++] = (toNum(x) << 4) | toNum (y);
63     if (byteCount >= maxSize)  {
64       fprintf (stderr, "Max input array size exceeded\n");
65       return (-1);
66     }
68   }
71 }
74 unsigned dwordConvert (unsigned char *data, int idx, int iMax)
75 {
76   unsigned value;
77   unsigned char c[4];
78   int i;
80   c[0] = c[1] = c[2] = c[3] = 0;
81   
82   for (i = 0; i < 4; i++)  {
83     if (idx >= iMax)
84       break;
85     c[i] = data[idx++];
86   }
88   value = c[3] | (c[2] << 8) | (c[1] << 16) | (c[0] << 24);
90   return (value);
92 }
96 #define SIZE    0x100000   /* max array size */
98 int main (int argc, char *argv[])
99 {
100   FILE *strin;
101   FILE *strout;
103   unsigned char *dataSet1;
105   unsigned char block[128];
106   unsigned blockSize;
108   unsigned pIn;
109   unsigned pOut;
111   int inSize;
112   int i;
114   /* Arg check */
115   if (argc != 3)  {
116     fprintf (stderr, "usage: %s infile outfile\n", argv[0]);
117     return (-1);
118   }
120   /* Open the input file */
121   strin = fopen (argv[1], "r");
122   if (strin == NULL)  {
123     fprintf (stderr, "%s: Could not open file %s for reading\n", argv[0], argv[1]);
124     return (-1);
125   }
127   /* Allocate the two data set memories */
128   dataSet1 = malloc (SIZE * sizeof (unsigned char));
129   if (dataSet1 == NULL)  {
130     fprintf (stderr, "%s: Malloc failure\n", argv[0]);
131     return (-1);
132   }
134   /* Read the data into the byte stream */
135   if ((inSize = readBFile (strin, dataSet1, SIZE)) < 0)
136     return (inSize);
137   fclose (strin);
139   strout = fopen (argv[2], "w");
140   if (strout == NULL)  {
141     fprintf (stderr, "%s error: Could not open output file %s\n", argv[0], argv[2]);
142     free (dataSet1);
143     return (-1);
144   }
146   /* Write the CCS header */
147   fprintf (strout, "1651 1 10000 1 %x\n", (inSize + 3) / 4);
149   /* Write out each 32 bit line. */
150   for (i = 0; i < inSize; i += 4)
151     fprintf (strout, "0x%08x\n", dwordConvert (dataSet1, i, inSize));
153   free (dataSet1);
154   fclose (strout);
157   return (0);
163