[processor-sdk/performance-audio-sr.git] / psdk_cust / pdk_k2g_1_0_1_1_eng / packages / ti / boot / sbl / tools / tiImageGen / src / tiimage.c
1 /*
2 * \file tiimage.c
3 *
4 * \brief Generates an image with header, load address and size
5 *
6 */
8 /*
9 * Copyright (C) 2010-2016 Texas Instruments Incorporated - http://www.ti.com/
10 */
11 /*
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 *
19 * Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the
22 * distribution.
23 *
24 * Neither the name of Texas Instruments Incorporated nor the names of
25 * its contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 */
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
48 typedef struct _ti_header_
49 {
50 unsigned int image_size;
51 unsigned int load_addr;
52 }ti_header;
55 static unsigned int tiimage_swap32(unsigned int data)
56 {
57 unsigned int result = 0;
60 result = (data & 0xFF000000) >> 24;
61 result |= (data & 0x00FF0000) >> 8;
62 result |= (data & 0x0000FF00) << 8;
63 result |= (data & 0x000000FF) << 24;
65 return result;
66 }
69 int main (int argc, char *argv[])
70 {
71 FILE *in_fp, *out_fp;
72 long image_size = 0;
73 ti_header hdr;
74 int i = 0, len;
75 unsigned int extra;
76 char dummy[4] = {0,0,0,0};
77 char *boot;
80 if (argc < 5)
81 {
82 /* expect : tiimage </path/to/boot.bin> <path/to/place/modified/boot.bin> */
83 printf("Usage : \n");
84 printf("tiimage takes the input image and adds the TI Image Header \n");
85 printf("The resulting output is placed in the output image path\n");
86 printf("Syntax: ./<executable file name> <load address> <boot mode> <input image path/name> <output image path/name>\n");
87 return -1;
88 }
91 in_fp = fopen(argv[3], "rb+");
92 if(!in_fp) {
93 printf("Error opening input image file!\n");
94 return -1;
95 }
98 out_fp = fopen(argv[4], "wb+");
99 if(!out_fp) {
100 printf("Error opening/creating out image file!\n");
101 return -1;
102 }
105 /* Calcualte the size of the input image and rewind to the begin of file */
106 fseek(in_fp, 0, SEEK_END);
107 image_size = ftell(in_fp);
109 if(0 == strcmp(argv[2], "SPI")) {
110 /* Get number of zeros to be inserted */
111 extra = (image_size & 0x03);
113 if(extra)
114 fwrite(&dummy, sizeof(char), 4 - extra, in_fp);
116 /* adjust the image size for SPI to be a multiple of 4 */
117 image_size = (image_size + 3) & ~(0x03);
118 }
120 rewind(in_fp);
121 /* Size of new image is actual bin image size + header */
122 hdr.image_size = image_size + sizeof(hdr);
123 hdr.load_addr = strtoul(argv[1], NULL, 0);
125 if(0 == strcmp(argv[2], "SPI"))
126 {
127 hdr.image_size = tiimage_swap32(hdr.image_size);
128 hdr.load_addr = tiimage_swap32(hdr.load_addr);
129 }
132 /* Insert the header first */
133 fwrite(&hdr, sizeof(hdr), 1, out_fp);
136 if(0 == strcmp(argv[2], "SPI"))
137 {
138 /* Insert the swapped image */
139 for(i = 0; i < (image_size / 4) ; i++) {
140 unsigned int temp;
141 fread(&temp, sizeof(temp), 1, in_fp);
142 temp = tiimage_swap32(temp);
143 fwrite(&temp, sizeof(temp), 1, out_fp);
144 }
145 }
146 else
147 {
148 /* Insert the actual image */
149 for(i = 0; i < image_size ; i++) {
150 unsigned char temp;
151 fread(&temp, sizeof(temp), 1, in_fp);
152 fwrite(&temp, sizeof(temp), 1, out_fp);
153 }
154 }
155 printf("\n");
157 fclose(out_fp);
158 fclose(in_fp);
160 return 0;
161 }