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 /* Second attempt to create the ccs version file for testing the rom boot loader */
39 /* No i2c blocking is performed */
41 #include <stdio.h>
42 #include <malloc.h>
45 int asciiByte (unsigned char c)
46 {
47 if ((c >= '0') && (c <= '9'))
48 return (1);
50 if ((c >= 'A') && (c <= 'F'))
51 return (1);
53 return (0);
54 }
56 int toNum (unsigned char c)
57 {
58 if ((c >= '0') && (c <= '9'))
59 return (c - '0');
61 return (c - 'A' + 10);
63 }
66 void stripLine (FILE *s)
67 {
68 char iline[132];
70 fgets (iline, 131, s);
72 }
74 /* Read a .b file. */
75 int readBFile (FILE *s, unsigned char *data, unsigned maxSize)
76 {
77 unsigned char x, y;
78 int byteCount = 0;
80 /* Strip the 1st two lines */
81 stripLine (s);
82 stripLine (s);
84 for (;;) {
86 /* read the 1st ascii char */
87 do {
88 x = fgetc (s);
89 if (x == (unsigned char)EOF)
90 return (byteCount);
92 } while (!asciiByte(x));
94 /* Read the next ascii char */
95 y = fgetc (s);
96 if (y == (unsigned char)EOF)
97 return (byteCount);
98 if (asciiByte(y))
99 data[byteCount++] = (toNum(x) << 4) | toNum (y);
101 if (byteCount >= maxSize) {
102 fprintf (stderr, "Max input array size exceeded\n");
103 return (-1);
104 }
106 }
109 }
112 int copyBlock (unsigned char *source, int idx, int maxSize, unsigned char *dest, int count)
113 {
114 int i;
116 for (i = 0; i < count; i++) {
117 if (idx >= maxSize)
118 break;
119 dest[i] = source[idx++];
120 }
122 return (i);
124 }
126 unsigned dwordConvert (unsigned char *data, int idx, int iMax)
127 {
128 unsigned value;
129 unsigned char c[4];
130 int i;
132 c[0] = c[1] = c[2] = c[3] = 0;
134 for (i = 0; i < 4; i++) {
135 if (idx >= iMax)
136 break;
137 c[i] = data[idx++];
138 }
140 value = c[0] | (c[1] << 8) | (c[2] << 16) | (c[3] << 24);
142 return (value);
144 }
148 #define SIZE 0x10000 /* max array size */
150 int main (int argc, char *argv[])
151 {
152 FILE *strin;
153 FILE *strout;
155 unsigned char *dataSet1;
157 unsigned pIn;
158 unsigned pOut;
160 int inSize;
161 int i;
163 /* Arg check */
164 if (argc != 3) {
165 fprintf (stderr, "usage: %s infile outfile\n", argv[0]);
166 return (-1);
167 }
169 /* Open the input file */
170 strin = fopen (argv[1], "r");
171 if (strin == NULL) {
172 fprintf (stderr, "%s: Could not open file %s for reading\n", argv[0], argv[1]);
173 return (-1);
174 }
176 /* Allocate the two data set memories */
177 dataSet1 = malloc (SIZE * sizeof (unsigned char));
178 if (dataSet1 == NULL) {
179 fprintf (stderr, "%s: Malloc failure\n", argv[0]);
180 return (-1);
181 }
183 /* Read the data into the byte stream */
184 if ((inSize = readBFile (strin, dataSet1, SIZE)) < 0)
185 return (inSize);
186 fclose (strin);
188 /* Copy the resulting data set into the output file in ccs format */
189 strout = fopen (argv[2], "w");
190 if (strout == NULL) {
191 fprintf (stderr, "%s: Could not open %s for writing\n", argv[0], argv[2]);
192 return (-1);
193 }
195 /* Write the ccs header. The number of lines must be computed from the
196 * array size */
197 fprintf (strout, "1651 1 b000 1 %x\n", (inSize + 3) / 4);
199 for (i = 0; i < inSize; i += 4)
200 fprintf (strout, "0x%08x\n", dwordConvert (dataSet1, i, inSize));
202 fclose (strout);
204 return (0);
206 }