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 ccs file to a raw binary file
39 *
40 * usage: ccs2bin [-swap] ccsfile binfile
41 */
43 #include <stdio.h>
45 unsigned int swap(unsigned int v)
46 {
47 unsigned int w;
49 w = (((v >> 24) & 0xff) << 0) |
50 (((v >> 16) & 0xff) << 8) |
51 (((v >> 8) & 0xff) << 16) |
52 (((v >> 0) & 0xff) << 24);
54 return (w);
56 }
57 FILE *fin = NULL;
58 FILE *fout = NULL;
59 int doswap = 0;
61 #define USAGE "usage: %s [-swap] ccsfile binfile"
63 int parseit (int argc, char *argv[])
64 {
65 int i;
67 if ((argc != 3) && (argc != 4)) {
68 fprintf (stderr, USAGE, argv[0]);
69 return (-1);
70 }
72 for (i = 1; i < argc; i++) {
74 if (!strcmp (argv[i], "-swap"))
75 doswap = 1;
77 else if (fin == NULL) {
78 fin = fopen (argv[i], "r");
79 if (fin == NULL) {
80 fprintf (stderr, "%s: Could not open file %s\n", argv[0], argv[i]);
81 return (-1);
82 }
84 } else if (fout == NULL) {
85 fout = fopen (argv[i], "wb");
86 if (fout == NULL) {
87 fprintf (stderr, "%s: Could not open file %s\n", argv[0], argv[i]);
88 fclose (fin);
89 return (-1);
90 }
92 } else {
94 fprintf (stderr, USAGE, argv[0]);
95 fclose (fout);
96 fclose (fin);
97 return (-1);
98 }
99 }
101 return (0);
103 }
109 int main (int argc, char *argv[])
110 {
111 unsigned int n;
112 unsigned int v;
113 unsigned int i;
115 int a, b, c, d;
118 char iline[132];
120 if (parseit (argc, argv))
121 return (-1);
123 fgets (iline, 131, fin);
124 sscanf (iline, "%x %x %x %x %x", &a, &b, &c, &d, &n);
127 for (i = 0; i < n; i++) {
128 fgets (iline, 131, fin);
129 sscanf (&iline[2], "%x", &v);
130 if (doswap)
131 v = swap(v);
132 fwrite (&v, sizeof(unsigned int), 1, fout);
133 }
135 fclose (fout);
136 fclose (fin);
138 return (0);
140 }