1 /*
2 *
3 * Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com/
4 *
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the
16 * distribution.
17 *
18 * Neither the name of Texas Instruments Incorporated nor the names of
19 * its contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 */
38 /* Convert a hex b file into a ccs data file */
40 #include <stdio.h>
41 #include <malloc.h>
44 int asciiByte (unsigned char c)
45 {
46 if ((c >= '0') && (c <= '9'))
47 return (1);
49 if ((c >= 'A') && (c <= 'F'))
50 return (1);
52 return (0);
53 }
55 int toNum (unsigned char c)
56 {
57 if ((c >= '0') && (c <= '9'))
58 return (c - '0');
60 return (c - 'A' + 10);
62 }
65 void stripLine (FILE *s)
66 {
67 char iline[132];
69 fgets (iline, 131, s);
71 }
73 /* Read a .b file. */
74 int readBFile (FILE *s, unsigned char *data, unsigned maxSize)
75 {
76 unsigned char x, y;
77 int byteCount = 0;
79 /* Strip the 1st two lines */
80 stripLine (s);
81 stripLine (s);
83 for (;;) {
85 /* read the 1st ascii char */
86 do {
87 x = fgetc (s);
88 if (x == (unsigned char)EOF)
89 return (byteCount);
91 } while (!asciiByte(x));
93 /* Read the next ascii char */
94 y = fgetc (s);
95 if (y == (unsigned char)EOF)
96 return (byteCount);
97 if (asciiByte(y))
98 data[byteCount++] = (toNum(x) << 4) | toNum (y);
100 if (byteCount >= maxSize) {
101 fprintf (stderr, "Max input array size exceeded\n");
102 return (-1);
103 }
105 }
108 }
111 unsigned dwordConvert (unsigned char *data, int idx, int iMax)
112 {
113 unsigned value;
114 unsigned char c[4];
115 int i;
117 c[0] = c[1] = c[2] = c[3] = 0;
119 for (i = 0; i < 4; i++) {
120 if (idx >= iMax)
121 break;
122 c[i] = data[idx++];
123 }
125 value = c[3] | (c[2] << 8) | (c[1] << 16) | (c[0] << 24);
127 return (value);
129 }
133 #define SIZE 0x200000 /* max array size */
135 int main (int argc, char *argv[])
136 {
137 FILE *strin;
138 FILE *strout;
140 unsigned char *dataSet1;
142 unsigned char block[128];
143 unsigned blockSize;
145 unsigned pIn;
146 unsigned pOut;
148 int inSize;
149 int i;
151 /* Arg check */
152 if (argc != 3) {
153 fprintf (stderr, "usage: %s infile outfile\n", argv[0]);
154 return (-1);
155 }
157 /* Open the input file */
158 strin = fopen (argv[1], "r");
159 if (strin == NULL) {
160 fprintf (stderr, "%s: Could not open file %s for reading\n", argv[0], argv[1]);
161 return (-1);
162 }
164 /* Allocate the two data set memories */
165 dataSet1 = malloc (SIZE * sizeof (unsigned char));
166 if (dataSet1 == NULL) {
167 fprintf (stderr, "%s: Malloc failure\n", argv[0]);
168 return (-1);
169 }
171 /* Read the data into the byte stream */
172 if ((inSize = readBFile (strin, dataSet1, SIZE)) < 0)
173 return (inSize);
174 fclose (strin);
176 strout = fopen (argv[2], "w");
177 if (strout == NULL) {
178 fprintf (stderr, "%s error: Could not open output file %s\n", argv[0], argv[2]);
179 free (dataSet1);
180 return (-1);
181 }
183 /* Write the CCS header */
184 fprintf (strout, "1651 1 10000 1 %x\n", (inSize + 3) / 4);
186 /* Write out each 32 bit line. */
187 for (i = 0; i < inSize; i += 4)
188 fprintf (strout, "0x%08x\n", dwordConvert (dataSet1, i, inSize));
190 free (dataSet1);
191 fclose (strout);
194 return (0);
196 }