[processor-sdk/performance-audio-sr.git] / psdk_cust / pdk_k2g_1_0_1_1_eng / packages / ti / boot / sbl / tools / ccsutil / ccsAddGptlr.c
1 /*
2 *
3 * Copyright (C) 2016 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 */
35 /******************************************************************************
36 * FILE PURPOSE: Add a GP Trailer to a CCS file
37 ******************************************************************************
38 * FILE NAME: ccsAddGptlr.c
39 *
40 * DESCRIPTION: Adds the 8 byte General Purpose Trailer
41 *
42 * Usage: ccsAddGptlr [-h] [-infile infile] [-outfile outfile]
43 *
44 ******************************************************************************/
45 #include <stdio.h>
46 #include <malloc.h>
47 #include <string.h>
48 #include "ccsutil.h"
50 char *infile = NULL;
51 char *outfile = NULL;
53 int parseit (int ac, char *av[])
54 {
56 int i;
58 for (i = 1; i < ac; ) {
60 if (!strcmp (av[i], "-infile")) {
61 infile = av[i+1];
62 i = i + 2;
63 continue;
64 }
66 if (!strcmp (av[i], "-outfile")) {
67 outfile = av[i+1];
68 i = i + 2;
69 continue;
70 }
72 if (!strcmp (av[i], "-h")) {
73 fprintf (stderr, "usage: %s [-h] [-infile infile] [-outfile outfile]\n", av[0]);
74 return (-1);
75 }
77 fprintf (stderr, "%s: Unknown option %s\n", av[0], av[i]);
78 return (-1);
80 }
82 return (0);
84 }
87 int main (int argc, char *argv[])
88 {
89 FILE *str;
90 unsigned int *dat, addr;
91 int n;
93 if (parseit (argc, argv))
94 return (-1);
97 if (infile != NULL) {
99 str = fopen (infile, "r");
100 if (str == NULL) {
101 fprintf (stderr, "%s: Failed to open input file %s\n", argv[0], infile);
102 return (-1);
103 }
105 } else {
107 str = stdin;
109 }
111 dat = readCcsFile (str, &n, &addr);
113 if (dat == NULL) {
115 fprintf (stderr, "%s: Error reading CCS data file\n", argv[0]);
116 return (-1);
118 }
120 if (infile != NULL)
121 fclose (str);
124 dat = realloc (dat, (n+2) * sizeof(unsigned int));
125 if (dat == NULL) {
126 fprintf (stderr, "%s: Realloc failed\n", argv[0]);
127 return (-1);
128 }
130 dat[n+0] = 0;
131 dat[n+1] = 0;
132 n = n + 2;
134 if (outfile != NULL) {
136 str = fopen (outfile, "w");
137 if (str == NULL) {
138 fprintf (stderr, "%s: Failed to open output file %s\n", argv[0], outfile);
139 free (dat);
140 return (-1);
141 }
143 } else {
145 str = stdout;
147 }
149 writeCcsFile (str, dat, n, addr);
151 if (outfile != NULL)
152 fclose (str);
154 free (dat);
156 return (0);
158 }