Debian Codegraph @

util/nvidia/cbootimage/src/bct_dump.c

raw symbols SCIP
1 /*
2 * Copyright (c) 2012, 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 #include "cbootimage.h"
21 #include "data_layout.h"
22 #include "context.h"
23 #include "parse.h"
24 #include "t20/nvboot_bct_t20.h"
25 #include <string.h>
26
27 int enable_debug;
28 cbootimage_soc_config * g_soc_config;
29
30 static void format_u32_hex8(parse_token id, char const * message, void * data);
31 static void format_u32(parse_token id, char const * message, void * data);
32 static void format_chipuid(parse_token id, char const * message, void * data);
33 static void format_hex_16_bytes(parse_token id, char const * message, void * data);
34 static void format_rsa_param(parse_token id, char const * message, void * data);
35
36 typedef void (*format_function)(parse_token id, char const * message, void * data);
37
38 typedef struct {
39 parse_token id;
40 char const * message;
41 format_function format;
42 } value_data;
43
44 #define PARAM_TYPE_BINARY_DATA_MAX_SIZE 256
45 typedef union {
46 uint32_t val;
47 uint8_t uid[16];
48 uint8_t binary[PARAM_TYPE_BINARY_DATA_MAX_SIZE];
49 } param_types;
50
51 #define MAX_PARAM_SIZE sizeof(param_types)
52
53 static value_data const values[] = {
54 { token_boot_data_version, "Version = ", format_u32_hex8 },
55 { token_block_size, "BlockSize = ", format_u32_hex8 },
56 { token_page_size, "PageSize = ", format_u32_hex8 },
57 { token_partition_size, "PartitionSize = ", format_u32_hex8 },
58 { token_odm_data, "OdmData = ", format_u32_hex8 },
59 { token_secure_jtag_control, "JtagCtrl = ", format_u32_hex8 },
60 { token_secure_debug_control, "DebugCtrl = ", format_u32_hex8 },
61 { token_crypto_hash, "BCT AES Hash = ", format_hex_16_bytes },
62 { token_rsa_key_modulus, "RsaKeyModulus:\n", format_rsa_param },
63 { token_rsa_pss_sig_bct, "RsaPssSigBct:\n", format_rsa_param },
64 { token_unique_chip_id, "ChipUid = ", format_chipuid },
65 { token_bootloader_used, "# Bootloader used = ", format_u32 },
66 { token_bootloaders_max, "# Bootloaders max = ", format_u32 },
67 { token_bct_size, "# BCT size = ", format_u32 },
68 { token_hash_size, "# Hash size = ", format_u32 },
69 { token_crypto_offset, "# Crypto offset = ", format_u32 },
70 { token_crypto_length, "# Crypto length = ", format_u32 },
71 { token_max_bct_search_blks, "# Max BCT search blocks = ", format_u32 },
72 };
73
74 static value_data const bl_values[] = {
75 { token_bl_version, "Version = ", format_u32_hex8 },
76 { token_bl_start_blk, "Start block = ", format_u32 },
77 { token_bl_start_page, "Start page = ", format_u32 },
78 { token_bl_length, "Length = ", format_u32 },
79 { token_bl_load_addr, "Load address = ", format_u32_hex8 },
80 { token_bl_entry_point, "Entry point = ", format_u32_hex8 },
81 { token_bl_attribute, "Attributes = ", format_u32_hex8 },
82 { token_bl_crypto_hash, "Bl AES Hash = ", format_hex_16_bytes },
83 { token_rsa_pss_sig_bl, "RsaPssSigBl:\n", format_rsa_param },
84 };
85
86 static value_data const mts_values[] = {
87 { token_mts_info_version, "Version = ", format_u32_hex8 },
88 { token_mts_info_start_blk, "Start block = ", format_u32 },
89 { token_mts_info_start_page, "Start page = ", format_u32 },
90 { token_mts_info_length, "Length = ", format_u32 },
91 { token_mts_info_load_addr, "Load address = ", format_u32_hex8 },
92 { token_mts_info_entry_point, "Entry point = ", format_u32_hex8 },
93 { token_mts_info_attribute, "Attributes = ", format_u32_hex8 },
94 };
95
96 /*****************************************************************************/
97 static void format_u32_hex8(parse_token id, char const * message, void * data)
98 {
99 printf("%s0x%08x;\n", message, *((uint32_t *) data));
100 }
101
102 static void format_u32(parse_token id, char const * message, void * data)
103 {
104 printf("%s%d;\n", message, *((uint32_t *) data));
105 }
106
107 static void format_chipuid(parse_token id, char const * message, void * data)
108 {
109 uint8_t *uid = (uint8_t *)data;
110 int byte_index;
111 char uid_str[35] = "0x";
112 char *s = &uid_str[2];
113
114 for (byte_index = 15; byte_index >= 0; byte_index--, s += 2)
115 sprintf(s, "%02x", uid[byte_index]);
116
117 printf("%s%s;\n", message, uid_str);
118 }
119
120 static void format_hex_16_bytes(parse_token id, char const * message, void * data)
121 {
122 uint8_t *p_byte = (uint8_t *)data;
123 int byte_index;
124
125 printf("%s", message);
126 for (byte_index = 0; byte_index < 16; ++byte_index)
127 printf("%02x", *p_byte++);
128
129 printf(";\n");
130 }
131
132 static void format_rsa_param(parse_token id, char const * message, void * data)
133 {
134 #define MAX_BYTE_NUMBER_PER_LINE 16
135 uint8_t *rsa = (uint8_t *)data;
136 int size, byte_index;
137
138 printf("%s", message);
139
140 if (!g_soc_config->get_value_size)
141 return;
142
143 size = g_soc_config->get_value_size(id);
144 for (byte_index = 0; byte_index < size; ++byte_index) {
145 printf(" %02x", *rsa++);
146
147 if ((byte_index + 1) % MAX_BYTE_NUMBER_PER_LINE == 0)
148 printf("\n");
149 }
150
151 if (byte_index % MAX_BYTE_NUMBER_PER_LINE != 0)
152 printf("\n");
153 #undef MAX_BYTE_NUMBER_PER_LINE
154 }
155
156 /*****************************************************************************/
157 static void usage(void)
158 {
159 printf("Usage: bct_dump bctfile\n");
160 printf(" bctfile BCT filename to read and display\n");
161 }
162 /*****************************************************************************/
163 static int max_width(field_item const * table)
164 {
165 int width = 0;
166 int i;
167
168 for (i = 0; table[i].name != NULL; ++i) {
169 int length = strlen(table[i].name);
170
171 if (width < length)
172 width = length;
173 }
174
175 return width;
176 }
177 /*****************************************************************************/
178 static enum_item const * find_enum_item(build_image_context *context,
179 enum_item const * table,
180 uint32_t value)
181 {
182 int i;
183
184 for (i = 0; table[i].name != NULL; ++i) {
185 if (table[i].value == value)
186 return table + i;
187 }
188
189 return NULL;
190 }
191 /*****************************************************************************/
192 static void display_enum_value(build_image_context *context,
193 enum_item const * table,
194 uint32_t value)
195 {
196 enum_item const * e_item = find_enum_item(context, table, value);
197
198 if (e_item)
199 printf("%s", e_item->name);
200 else
201 printf("<UNKNOWN ENUM VALUE (%d)>", value);
202 }
203 /*****************************************************************************/
204 static int display_field_value(build_image_context *context,
205 field_item const * item,
206 uint32_t value)
207 {
208 switch (item->type) {
209 case field_type_enum:
210 display_enum_value(context, item->enum_table, value);
211 break;
212
213 case field_type_u32:
214 printf("0x%08x", value);
215 break;
216
217 case field_type_u8:
218 printf("%d", value);
219 break;
220
221 default:
222 printf("<UNKNOWN FIELD TYPE (%d)>", item->type);
223 return 1;
224 }
225
226 return 0;
227 }
228 /*****************************************************************************/
229 int main(int argc, char *argv[])
230 {
231 int e;
232 build_image_context context;
233 uint32_t bootloaders_used;
234 uint32_t parameters_used;
235 uint32_t sdram_used;
236 uint32_t mts_used;
237 nvboot_dev_type type;
238 param_types data;
239 int i;
240 int j;
241
242 if (argc != 2)
243 usage();
244
245 memset(&context, 0, sizeof(build_image_context));
246 context.bct_filename = argv[1];
247
248 e = read_bct_file(&context);
249 if (e != 0)
250 return e;
251
252 /* Display root values */
253 for (i = 0; i < sizeof(values) / sizeof(values[0]); ++i) {
254 if (!g_soc_config->token_supported(values[i].id))
255 continue;
256
257 e = g_soc_config->get_value(values[i].id, &data, context.bct);
258 if (e)
259 memset(&data, 0, MAX_PARAM_SIZE);
260
261 values[i].format(values[i].id, values[i].message, &data);
262 }
263
264 /* Display bootloader values */
265 e = g_soc_config->get_value(token_bootloader_used,
266 &bootloaders_used,
267 context.bct);
268
269 if ((e == 0) && (bootloaders_used > 0)) {
270 int bl_count = sizeof(bl_values) / sizeof(bl_values[0]);
271
272 printf("#\n"
273 "# These values are set by cbootimage using the\n"
274 "# bootloader provided by the Bootloader=...\n"
275 "# configuration option.\n"
276 "#\n");
277
278 for (i = 0; i < bootloaders_used; ++i) {
279 for (j = 0; j < bl_count; ++j) {
280 e = g_soc_config->getbl_param(i,
281 bl_values[j].id,
282 &data.val,
283 context.bct);
284 printf("# Bootloader[%d].", i);
285
286 if (e)
287 data.val = -1;
288
289 bl_values[j].format(bl_values[j].id,
290 bl_values[j].message,
291 &data);
292 }
293 }
294 }
295
296 /* Display mts values */
297 e = g_soc_config->get_value(token_mts_used,
298 &mts_used,
299 context.bct);
300
301 if ((e == 0) && (mts_used> 0)) {
302 int mts_count = sizeof(mts_values) / sizeof(mts_values[0]);
303
304 printf("#\n"
305 "# These values are set by cbootimage using the\n"
306 "# mts provided by the Mts=... or MtsPreboot=...\n"
307 "# configuration option.\n"
308 "#\n");
309
310 for (i = 0; i < mts_used; ++i) {
311 for (j = 0; j < mts_count; ++j) {
312 e = g_soc_config->get_mts_info(&context,
313 i,
314 mts_values[j].id,
315 &(data.val));
316 printf("# Mts[%d].", i);
317
318 if (e)
319 data.val = -1;
320
321 mts_values[j].format(mts_values[j].id,
322 mts_values[j].message,
323 &data);
324 }
325 }
326 }
327 /* Display flash device parameters */
328 e = g_soc_config->get_value(token_num_param_sets,
329 &parameters_used,
330 context.bct);
331
332 for (i = 0; (e == 0) && (i < parameters_used); ++i) {
333 field_item const * device_field_table = NULL;
334 char const * prefix = NULL;
335 field_item const * item;
336
337 e = g_soc_config->get_dev_param(&context,
338 i,
339 token_dev_type,
340 &type);
341 printf("\n"
342 "DevType[%d] = ", i);
343 display_enum_value(&context, g_soc_config->devtype_table, type);
344 printf(";\n");
345
346 switch (type) {
347 case nvboot_dev_type_spi:
348 device_field_table = g_soc_config->spiflash_table;
349 prefix = "SpiFlashParams";
350 break;
351
352 case nvboot_dev_type_sdmmc:
353 device_field_table = g_soc_config->sdmmc_table;
354 prefix = "SdmmcParams";
355 break;
356
357 case nvboot_dev_type_nand:
358 device_field_table = g_soc_config->nand_table;
359 prefix = "NandParams";
360 break;
361
362 default:
363 device_field_table = NULL;
364 prefix = "";
365 break;
366 }
367
368 if (!device_field_table)
369 continue;
370
371 int width = max_width(device_field_table);
372
373 for (item = device_field_table; item->name != NULL; ++item) {
374 g_soc_config->get_dev_param(&context,
375 i,
376 item->token,
377 &data.val);
378 printf("DeviceParam[%d].%s.%-*s = ",
379 i, prefix, width, item->name);
380
381 if (e != 0)
382 printf("<ERROR reading parameter (%d)>", e);
383 else
384 display_field_value(&context, item, data.val);
385
386 printf(";\n");
387 }
388 }
389
390 /* Display SDRAM parameters */
391 e = g_soc_config->get_value(token_num_sdram_sets,
392 &sdram_used,
393 context.bct);
394
395 for (i = 0; (e == 0) && (i < sdram_used); ++i) {
396 field_item const *item;
397
398 printf("\n");
399
400 int width = max_width(g_soc_config->sdram_field_table);
401
402 for (item = g_soc_config->sdram_field_table; item->name != NULL; ++item) {
403 e = g_soc_config->get_sdram_param(&context,
404 i,
405 item->token,
406 &data.val);
407 printf("SDRAM[%d].%-*s = ", i, width, item->name);
408
409 if (e != 0)
410 printf("<ERROR reading parameter (%d)>", e);
411 else
412 display_field_value(&context, item, data.val);
413
414 printf(";\n");
415 }
416 }
417
418 /* Clean up memory. */
419 cleanup_context(&context);
420
421 return e;
422 }
423 /*****************************************************************************/