[processor-sdk/performance-audio-sr.git] / pdk_k2g_1_0_1_0_eng / packages / ti / boot / sbl / tools / multicoreImageGen / src / MulticoreImageGen.c
1 /**
2 * @file MulticoreImageGen.c
3 *
4 * @brief
5 * Generate multicore app image from the input ELF or COFF files.
6 * Internally convert ELF or COFF executables into RPRC files using
7 * Out2rprc tool. Merge all the RPRC files with metaheader & generate
8 * multicore app image.
9 *
10 * \par
11 * ============================================================================
12 * @n (C) Copyright 2009-2016, Texas Instruments, Inc.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 *
18 * Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 *
21 * Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the
24 * distribution.
25 *
26 * Neither the name of Texas Instruments Incorporated nor the names of
27 * its contributors may be used to endorse or promote products derived
28 * from this software without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
36 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
40 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 *
42 * \par
43 */
45 /****************************************************************
46 * INCLUDE FILES
47 ****************************************************************/
48 #include <stdio.h>
49 #include <stdint.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <unistd.h>
54 /* ============================================================================
55 * GLOBAL VARIABLES DECLARATIONS
56 * =============================================================================
57 */
59 /* ============================================================================
60 * LOCAL VARIABLES DECLARATIONS
61 * =============================================================================
62 */
63 #define MAX_INPUT_FILES 10
64 #define debug_print(x) printf(x)
66 typedef struct _meta_header_start_
67 {
68 char magic_string_str[4];
69 uint32_t num_files;
70 uint32_t dev_id;
71 uint32_t rsvd;
72 }meta_header_start;
74 typedef struct _meta_header_core_
75 {
76 uint32_t core_id;
77 uint32_t image_offset;
78 }meta_header_core;
80 typedef struct _meta_header_end_
81 {
82 uint32_t rsvd;
83 char magic_string_end[4];
84 }meta_header_end;
86 typedef struct _input_file_core_
87 {
88 uint32_t CoreId;
89 FILE *in_fp;
90 }input_file_core;
92 /* ============================================================================
93 * LOCAL FUNCTIONS PROTOTYPES
94 * =============================================================================
95 */
97 static uint32_t tiimage_swap32(uint32_t data);
99 /* ============================================================================
100 * FUNCTIONS
101 * =============================================================================
102 */
103 static uint32_t tiimage_swap32(uint32_t data)
104 {
105 uint32_t result = 0;
107 result = (data & 0xFF000000) >> 24;
108 result |= (data & 0x00FF0000) >> 8;
109 result |= (data & 0x0000FF00) << 8;
110 result |= (data & 0x000000FF) << 24;
112 return result;
113 }
115 int32_t main (int32_t argc, char *argv[])
116 {
117 FILE *out_fp;
118 input_file_core in_fp_cr[MAX_INPUT_FILES];
119 int32_t image_size[MAX_INPUT_FILES];
120 meta_header_start hdr_str;
121 meta_header_core hdr_core[MAX_INPUT_FILES];
122 meta_header_end hdr_end;
123 int32_t i = 0, j = 0, len;
124 char *boot;
125 int32_t num_input_files;
126 int32_t *swap_ptr;
128 if (argc < 6)
129 {
130 /* expect : tiimage </path/to/boot.bin> <path/to/place/modified/boot.bin> */
131 printf("Usage : \n");
132 printf("Single image create takes the rprc images and adds the Meta Header and creates single output image\n");
133 printf("The resulting output is placed in the output image path\n");
134 printf("Syntax: ./<executable file name> <ENDIANESS> <Dev_ID> <output image path/name> <Core_ID> <input image1 path/name> [<Core_ID> <input image2 path/name>, ...]\n");
135 printf("ENDIAN: BE/LE --> specifies whether TI header is in Big or Little Endian format\n");
136 printf("Dev_ID is the Device ID \n");
137 printf("For Input rprc images provide the Core_Id followed by input image name\n");
138 return -1;
139 }
141 num_input_files = (argc - 3)/2;
142 printf("Number of Input Files %d\n",num_input_files);
144 out_fp = fopen(argv[3], "wb+");
145 if(!out_fp) {
146 printf("Error opening/creating out image file!\n");
147 return -1;
148 }
149 for (i =0; i< num_input_files; i++)
150 {
151 in_fp_cr[i].CoreId = atoi(argv[(2*i)+4]);
152 in_fp_cr[i].in_fp = fopen(argv[(2*i)+5], "rb");
153 if(!(in_fp_cr[i].in_fp)) {
154 printf("Error opening input image file! %s\n",argv[(2*i)+5]);
155 //return -1;
156 --num_input_files;
157 }
158 }
159 if (num_input_files == 0)
160 {
161 return -1;
162 }
164 /* Calcualte the size of the input image and rewind to the begin of file */
165 for (i = 0; i< num_input_files; i++)
166 {
167 fseek(in_fp_cr[i].in_fp, 0, SEEK_END);
168 image_size[i] = ftell(in_fp_cr[i].in_fp);
169 rewind(in_fp_cr[i].in_fp);
170 }
172 /* Populate Meta Header start structure */
173 hdr_str.magic_string_str[0] = 'M';
174 hdr_str.magic_string_str[1] = 'S';
175 hdr_str.magic_string_str[2] = 'T';
176 hdr_str.magic_string_str[3] = 'R';
178 hdr_str.num_files = num_input_files;
179 hdr_str.dev_id = atoi(argv[2]);
181 hdr_str.rsvd = 0;
183 /* Populate Meta Header Core structure */
184 for (i=0; i< num_input_files; i++)
185 {
186 hdr_core[i].core_id = in_fp_cr[i].CoreId;
187 if (i ==0)
188 {
189 /* This is first input file offset is equal to length of Meta Header */
190 hdr_core[i].image_offset = sizeof(meta_header_start) + sizeof(meta_header_end) + (num_input_files * sizeof(meta_header_core));
191 }
192 else
193 {
194 /* This is second or subsequent file, Offset is equal to offset of previous file + size of previous file */
195 hdr_core[i].image_offset = hdr_core[i-1].image_offset + image_size[i-1];
196 }
197 }
199 /* Populate Meta Header End structure */
200 hdr_end.rsvd = 0;
201 hdr_end.magic_string_end[0] = 'M';
202 hdr_end.magic_string_end[1] = 'E';
203 hdr_end.magic_string_end[2] = 'N';
204 hdr_end.magic_string_end[3] = 'D';
206 if(0 == strcmp(argv[1], "BE"))
207 {
208 swap_ptr = (int32_t *) &hdr_str;
209 for(i = 0; i < sizeof(hdr_str)/4; i++)
210 {
211 *swap_ptr = tiimage_swap32(*swap_ptr);
212 swap_ptr++;
213 }
214 swap_ptr = (int32_t *) &hdr_end;
215 for(i = 0; i < sizeof(hdr_end)/4; i++)
216 {
217 *swap_ptr = tiimage_swap32(*swap_ptr);
218 swap_ptr++;
219 }
220 for (j = 0; j< num_input_files; j++)
221 {
222 swap_ptr = (int32_t *) &(hdr_core[j]);
223 for(i = 0; i < sizeof(meta_header_core)/4; i++)
224 {
225 *swap_ptr = tiimage_swap32(*swap_ptr);
226 swap_ptr++;
227 }
228 }
229 }
230 /* Insert Meta Header Start */
231 fwrite(&hdr_str, sizeof(hdr_str), 1, out_fp);
233 /* Insert all core info in Meta Header */
234 for (i =0; i< num_input_files; i++)
235 {
236 fwrite(&hdr_core[i], sizeof(meta_header_core), 1, out_fp);
237 }
239 /* Insert Meta Header End */
240 fwrite(&hdr_end, sizeof(hdr_end), 1, out_fp);
242 /* Insert All the the actual image */
243 for (i = 0; i< num_input_files; i++)
244 {
245 for (j=0; j< (image_size[i] /4); j++)
246 {
247 uint32_t temp;
248 fread(&temp, sizeof(temp), 1, in_fp_cr[i].in_fp);
250 if(0 == strcmp(argv[1], "BE"))
251 {
252 temp = tiimage_swap32(temp);
253 }
255 fwrite(&temp, sizeof(temp), 1, out_fp);
256 }
257 }
259 printf("\n");
260 return 0;
261 }