Debian Codegraph @

util/nvidia/cbootimage/src/cbootimage.c

raw symbols SCIP
1 /*
2 * Copyright (c) 2012-2015, NVIDIA CORPORATION. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * See file CREDITS for list of people who contributed to this
17 * project.
18 */
19
20 /*
21 * cbootimage.c - Implementation of the cbootimage tool.
22 */
23
24 #include <strings.h>
25 #include "cbootimage.h"
26 #include "crypto.h"
27 #include "data_layout.h"
28 #include "parse.h"
29 #include "set.h"
30 #include "context.h"
31 #include <getopt.h>
32
33 /*
34 * Global data
35 */
36 int enable_debug;
37 static int help_only; // Only print help & exit
38 cbootimage_soc_config * g_soc_config;
39
40 /*
41 * Function prototypes
42 */
43 int main(int argc, char *argv[]);
44
45 struct option cbootcmd[] = {
46 {"help", 0, NULL, 'h'},
47 {"debug", 0, NULL, 'd'},
48 {"generate", 1, NULL, 'g'},
49 {"tegra", 1, NULL, 't'},
50 {"odmdata", 1, NULL, 'o'},
51 {"soc", 1, NULL, 's'},
52 {"update", 0, NULL, 'u'},
53 {0, 0, 0, 0},
54 };
55
56 int
57 write_image_file(build_image_context *context)
58 {
59 assert(context != NULL);
60
61 return write_block_raw(context);
62 }
63
64 static void
65 usage(void)
66 {
67 printf("Usage: cbootimage [options] configfile [inputimage] outputimage\n");
68 printf(" options:\n");
69 printf(" -h, --help, -? Display this message.\n");
70 printf(" -d, --debug Output debugging information.\n");
71 printf(" -gbct Generate the new bct file.\n");
72 printf(" -o<ODM_DATA> Specify the odm_data(in hex).\n");
73 printf(" -t|--tegra NN Select target device. Must be one of:\n");
74 printf(" 20, 30, 114, 124, 132, 210.\n");
75 printf(" Default: 20.\n");
76 printf(" -s|--soc tegraNN Select target device. Must be one of:\n");
77 printf(" tegra20, tegra30, tegra114, tegra124,\n");
78 printf(" tegra132, tegra210.\n");
79 printf(" Default: tegra20.\n");
80 printf(" -u|--update Copy input image data and update bct\n");
81 printf(" configs into new image file.\n");
82 printf(" This feature is currently not supported on Tegra20/30.\n");
83 printf(" configfile File with configuration information\n");
84 printf(" inputimage Input image name. This is required\n");
85 printf(" if -u|--update option is used.\n");
86 printf(" outputimage Output image name\n");
87 }
88
89 static int
90 process_command_line(int argc, char *argv[], build_image_context *context)
91 {
92 int c, num_filenames = 2;
93
94 context->generate_bct = 0;
95
96 while ((c = getopt_long(argc, argv, "hdg:t:o:s:u", cbootcmd, NULL)) != -1) {
97 switch (c) {
98 case 'h':
99 help_only = 1;
100 usage();
101 return 0;
102 case 'd':
103 enable_debug = 1;
104 break;
105 case 'g':
106 if (!strcasecmp("bct", optarg)) {
107 context->generate_bct = 1;
108 } else {
109 printf("Invalid argument!\n");
110 usage();
111 return -EINVAL;
112 }
113 break;
114 case 's':
115 if (strncmp("tegra", optarg, 5)) {
116 printf("Unsupported chipname!\n");
117 usage();
118 return -EINVAL;
119 }
120 optarg += 5;
121 /* Deliberate fall-through */
122 case 't':
123 /* Assign the soc_config based on the chip. */
124 if (!strcasecmp("20", optarg)) {
125 t20_get_soc_config(context, &g_soc_config);
126 } else if (!strcasecmp("30", optarg)) {
127 t30_get_soc_config(context, &g_soc_config);
128 } else if (!strcasecmp("114", optarg)) {
129 t114_get_soc_config(context, &g_soc_config);
130 } else if (!strcasecmp("124", optarg)) {
131 t124_get_soc_config(context, &g_soc_config);
132 } else if (!strcasecmp("132", optarg)) {
133 t132_get_soc_config(context, &g_soc_config);
134 } else if (!strcasecmp("210", optarg)) {
135 t210_get_soc_config(context, &g_soc_config);
136 } else {
137 printf("Unsupported chipname!\n");
138 usage();
139 return -EINVAL;
140 }
141 break;
142 case 'o':
143 context->odm_data = strtoul(optarg, NULL, 16);
144 break;
145 case 'u':
146 context->update_image = 1;
147 num_filenames = 3;
148 break;
149 }
150 }
151
152 if (argc - optind != num_filenames) {
153 printf("Missing configuration and/or image file name.\n");
154 usage();
155 return -EINVAL;
156 }
157
158 /* If SoC is not specified, make the default soc_config to t20. */
159 if (!context->boot_data_version)
160 t20_get_soc_config(context, &g_soc_config);
161
162 /* Open the configuration file. */
163 context->config_file = fopen(argv[optind++], "r");
164 if (context->config_file == NULL) {
165 printf("Error opening config file.\n");
166 return -ENODATA;
167 }
168
169 /* Record the input image filename if update_image is necessary */
170 if (context->update_image)
171 {
172 if (context->boot_data_version == BOOTDATA_VERSION_T20 ||
173 context->boot_data_version == BOOTDATA_VERSION_T30) {
174 printf("Update image feature is not supported on"
175 " Tegra20/30.\n");
176 return -EINVAL;
177 }
178
179 context->input_image_filename = argv[optind++];
180 }
181
182 /* Record the output filename */
183 context->output_image_filename = argv[optind++];
184
185 return 0;
186 }
187
188 int
189 main(int argc, char *argv[])
190 {
191 int e = 0;
192 build_image_context context;
193
194 memset(&context, 0, sizeof(build_image_context));
195
196 /* Process command line arguments. */
197 if (process_command_line(argc, argv, &context) != 0)
198 return -EINVAL;
199
200 if (help_only)
201 return 1;
202
203 assert(g_soc_config != NULL);
204
205 g_soc_config->get_value(token_bct_size,
206 &context.bct_size,
207 context.bct);
208
209 e = init_context(&context);
210 if (e != 0) {
211 printf("context initialization failed. Aborting.\n");
212 return e;
213 }
214
215 if (enable_debug) {
216 /* Debugging information... */
217 printf("bct size: %d\n", context.bct_size);
218 }
219
220 /* Open the raw output file. */
221 context.raw_file = fopen(context.output_image_filename, "w+");
222 if (context.raw_file == NULL) {
223 printf("Error opening raw file %s.\n",
224 context.output_image_filename);
225 goto fail;
226 }
227
228 /* Read the bct data from image if bct configs needs to be updated */
229 if (context.update_image) {
230 uint32_t offset = 0, bct_size, actual_size;
231 uint8_t *data_block;
232 struct stat stats;
233
234 if (stat(context.input_image_filename, &stats) != 0) {
235 printf("Error: Unable to query info on input file path %s\n",
236 context.input_image_filename);
237 goto fail;
238 }
239
240 /* Get BCT_SIZE from input image file */
241 bct_size = get_bct_size_from_image(&context);
242 if (bct_size <= 0) {
243 printf("Error: Invalid input image file %s\n",
244 context.input_image_filename);
245 goto fail;
246 }
247
248 while (stats.st_size > offset) {
249 /* Read a block of data into memory */
250 if (read_from_image(context.input_image_filename, offset, bct_size,
251 &data_block, &actual_size, file_type_bct)) {
252 printf("Error reading image file %s.\n", context.input_image_filename);
253 goto fail;
254 }
255
256 /* Check if memory data is valid BCT */
257 context.bct = data_block;
258 if (data_is_valid_bct(&context)) {
259 fseek(context.config_file, 0, SEEK_SET);
260 process_config_file(&context, 0);
261 e = sign_bct(&context, context.bct);
262 if (e != 0) {
263 printf("Signing BCT failed, error: %d.\n", e);
264 goto fail;
265 }
266 }
267
268 /* Write the block of data to file */
269 if (actual_size != write_data_block(context.raw_file, offset, actual_size, data_block)) {
270 printf("Error writing image file %s.\n", context.output_image_filename);
271 goto fail;
272 }
273
274 offset += bct_size;
275 }
276
277 printf("Image file %s has been successfully generated!\n",
278 context.output_image_filename);
279 goto fail;
280 }
281 /* If we aren't generating the bct, read in config file */
282 else if (context.generate_bct == 0)
283 process_config_file(&context, 1);
284 /* Generate the new bct file */
285 else {
286 /* Initialize the bct memory */
287 init_bct(&context);
288 /* Parse & process the contents of the config file. */
289 process_config_file(&context, 0);
290 /* Update the BCT */
291 begin_update(&context);
292 /* Signing the bct. */
293 e = sign_bct(&context, context.bct);
294 if (e != 0)
295 printf("Signing BCT failed, error: %d.\n", e);
296
297 fwrite(context.bct, 1, context.bct_size,
298 context.raw_file);
299 printf("New BCT file %s has been successfully generated!\n",
300 context.output_image_filename);
301 goto fail;
302 }
303
304 if (!context.bct_init) {
305 e = 1;
306 printf("No BCT file has been read or generated.\n");
307 printf("This is likely due to an incomplete config file.\n");
308 goto fail;
309 }
310 if (!context.memory) {
311 e = 1;
312 printf("No output data generated.\n");
313 printf("This is likely due to an incomplete config file.\n");
314 goto fail;
315 }
316
317 /* Perform final signing & encryption of bct. */
318 e = sign_bct(&context, context.bct);
319 if (e != 0) {
320 printf("Signing BCT failed, error: %d.\n", e);
321 goto fail;
322 }
323 /* Write the image file. */
324 /* The image hasn't been written yet. */
325 if (write_image_file(&context) != 0)
326 printf("Error writing image file.\n");
327 else
328 printf("Image file %s has been successfully generated!\n",
329 context.output_image_filename);
330
331 fail:
332 /* Close the file(s). */
333 if (context.raw_file)
334 fclose(context.raw_file);
335
336 /* Clean up memory. */
337 cleanup_context(&context);
338 return e;
339 }