summaryrefslogtreecommitdiff
path: root/src/bct_dump.c
blob: 5e1405be7bcde6c075c514d3e698bb4f7bd847d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
 * Copyright (c) 2012, NVIDIA CORPORATION.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 */

#include "cbootimage.h"
#include "data_layout.h"
#include "context.h"
#include "parse.h"
#include "t20/nvboot_bct_t20.h"
#include <string.h>

int enable_debug;
cbootimage_soc_config * g_soc_config;

static void format_u32_hex8(parse_token id, char const * message, void * data);
static void format_u32(parse_token id, char const * message, void * data);
static void format_chipuid(parse_token id, char const * message, void * data);
static void format_hex_16_bytes(parse_token id, char const * message, void * data);
static void format_rsa_param(parse_token id, char const * message, void * data);

typedef void (*format_function)(parse_token id, char const * message, void * data);

typedef struct {
	parse_token id;
	char const * message;
	format_function format;
} value_data;

#define PARAM_TYPE_BINARY_DATA_MAX_SIZE 256
typedef union {
	uint32_t val;
	uint8_t uid[16];
	uint8_t binary[PARAM_TYPE_BINARY_DATA_MAX_SIZE];
} param_types;

#define MAX_PARAM_SIZE sizeof(param_types)

static value_data const values[] = {
	{ token_boot_data_version,   "Version       = ", format_u32_hex8 },
	{ token_block_size,          "BlockSize     = ", format_u32_hex8 },
	{ token_page_size,           "PageSize      = ", format_u32_hex8 },
	{ token_partition_size,      "PartitionSize = ", format_u32_hex8 },
	{ token_odm_data,            "OdmData       = ", format_u32_hex8 },
	{ token_secure_jtag_control, "JtagCtrl      = ", format_u32_hex8 },
	{ token_secure_debug_control, "DebugCtrl     = ", format_u32_hex8 },
	{ token_crypto_hash, 	     "BCT AES Hash  = ", format_hex_16_bytes },
	{ token_rsa_key_modulus,     "RsaKeyModulus:\n", format_rsa_param },
	{ token_rsa_pss_sig_bct,     "RsaPssSigBct:\n", format_rsa_param },
	{ token_unique_chip_id,      "ChipUid       = ", format_chipuid },
	{ token_bootloader_used,     "# Bootloader used       = ", format_u32 },
	{ token_bootloaders_max,     "# Bootloaders max       = ", format_u32 },
	{ token_bct_size,            "# BCT size              = ", format_u32 },
	{ token_hash_size,           "# Hash size             = ", format_u32 },
	{ token_crypto_offset,       "# Crypto offset         = ", format_u32 },
	{ token_crypto_length,       "# Crypto length         = ", format_u32 },
	{ token_max_bct_search_blks, "# Max BCT search blocks = ", format_u32 },
};

static value_data const bl_values[] = {
	{ token_bl_version,     "Version      = ", format_u32_hex8 },
	{ token_bl_start_blk,   "Start block  = ", format_u32 },
	{ token_bl_start_page,  "Start page   = ", format_u32 },
	{ token_bl_length,      "Length       = ", format_u32 },
	{ token_bl_load_addr,   "Load address = ", format_u32_hex8 },
	{ token_bl_entry_point, "Entry point  = ", format_u32_hex8 },
	{ token_bl_attribute,   "Attributes   = ", format_u32_hex8 },
	{ token_bl_crypto_hash, "Bl AES Hash  = ", format_hex_16_bytes },
	{ token_rsa_pss_sig_bl,	"RsaPssSigBl:\n", format_rsa_param },
};

static value_data const mts_values[] = {
	{ token_mts_info_version,      "Version      = ", format_u32_hex8 },
	{ token_mts_info_start_blk,    "Start block  = ", format_u32 },
	{ token_mts_info_start_page,   "Start page   = ", format_u32 },
	{ token_mts_info_length,       "Length       = ", format_u32 },
	{ token_mts_info_load_addr,    "Load address = ", format_u32_hex8 },
	{ token_mts_info_entry_point,  "Entry point  = ", format_u32_hex8 },
	{ token_mts_info_attribute,    "Attributes   = ", format_u32_hex8 },
};

/*****************************************************************************/
static void format_u32_hex8(parse_token id, char const * message, void * data)
{
	printf("%s0x%08x;\n", message, *((uint32_t *) data));
}

static void format_u32(parse_token id, char const * message, void * data)
{
	printf("%s%d;\n", message, *((uint32_t *) data));
}

static void format_chipuid(parse_token id, char const * message, void * data)
{
	uint8_t *uid = (uint8_t *)data;
	int byte_index;
	char uid_str[35] = "0x";
	char *s = &uid_str[2];

	for (byte_index = 15; byte_index >= 0; byte_index--, s += 2)
		sprintf(s, "%02x", uid[byte_index]);

	printf("%s%s;\n", message, uid_str);
}

static void format_hex_16_bytes(parse_token id, char const * message, void * data)
{
	uint8_t *p_byte = (uint8_t *)data;
	int byte_index;

	printf("%s", message);
	for (byte_index = 0; byte_index < 16; ++byte_index)
		printf("%02x", *p_byte++);

	printf(";\n");
}

static void format_rsa_param(parse_token id, char const * message, void * data)
{
#define MAX_BYTE_NUMBER_PER_LINE	16
	uint8_t *rsa = (uint8_t *)data;
	int size, byte_index;

	printf("%s", message);

	if (!g_soc_config->get_value_size)
		return;

	size = g_soc_config->get_value_size(id);
	for (byte_index = 0; byte_index < size; ++byte_index) {
		printf(" %02x", *rsa++);

		if ((byte_index + 1) % MAX_BYTE_NUMBER_PER_LINE == 0)
			printf("\n");
	}

	if (byte_index % MAX_BYTE_NUMBER_PER_LINE != 0)
		printf("\n");
#undef MAX_BYTE_NUMBER_PER_LINE
}

/*****************************************************************************/
static void usage(void)
{
	printf("Usage: bct_dump bctfile\n");
	printf("  bctfile   BCT filename to read and display\n");
}
/*****************************************************************************/
static int max_width(field_item const * table)
{
	int width = 0;
	int i;

	for (i = 0; table[i].name != NULL; ++i) {
		int length = strlen(table[i].name);

		if (width < length)
			width = length;
	}

	return width;
}
/*****************************************************************************/
static enum_item const * find_enum_item(build_image_context *context,
					enum_item const * table,
					uint32_t value)
{
	int i;

	for (i = 0; table[i].name != NULL; ++i) {
		if (table[i].value == value)
			return table + i;
	}

	return NULL;
}
/*****************************************************************************/
static void display_enum_value(build_image_context *context,
			       enum_item const * table,
			       uint32_t value)
{
	enum_item const * e_item = find_enum_item(context, table, value);

	if (e_item)
		printf("%s", e_item->name);
	else
		printf("<UNKNOWN ENUM VALUE (%d)>", value);
}
/*****************************************************************************/
static int display_field_value(build_image_context *context,
			       field_item const * item,
			       uint32_t value)
{
	switch (item->type) {
		case field_type_enum:
			display_enum_value(context, item->enum_table, value);
			break;

		case field_type_u32:
			printf("0x%08x", value);
			break;

		case field_type_u8:
			printf("%d", value);
			break;

		default:
			printf("<UNKNOWN FIELD TYPE (%d)>", item->type);
			return 1;
	}

	return 0;
}
/*****************************************************************************/
int main(int argc, char *argv[])
{
	int e;
	build_image_context context;
	uint32_t bootloaders_used;
	uint32_t parameters_used;
	uint32_t sdram_used;
	uint32_t mts_used;
	nvboot_dev_type type;
	param_types data;
	int i;
	int j;

	if (argc != 2)
		usage();

	memset(&context, 0, sizeof(build_image_context));
	context.bct_filename = argv[1];

	e = read_bct_file(&context);
	if (e != 0)
		return e;

	/* Display root values */
	for (i = 0; i < sizeof(values) / sizeof(values[0]); ++i) {
		if (!g_soc_config->token_supported(values[i].id))
			continue;

		e = g_soc_config->get_value(values[i].id, &data, context.bct);
		if (e)
			memset(&data, 0, MAX_PARAM_SIZE);

		values[i].format(values[i].id, values[i].message, &data);
	}

	/* Display bootloader values */
	e = g_soc_config->get_value(token_bootloader_used,
				     &bootloaders_used,
				     context.bct);

	if ((e == 0) && (bootloaders_used > 0)) {
		int bl_count = sizeof(bl_values) / sizeof(bl_values[0]);

		printf("#\n"
		       "# These values are set by cbootimage using the\n"
		       "# bootloader provided by the Bootloader=...\n"
		       "# configuration option.\n"
		       "#\n");

		for (i = 0; i < bootloaders_used; ++i) {
			for (j = 0; j < bl_count; ++j) {
				e = g_soc_config->getbl_param(i,
							       bl_values[j].id,
							       &data.val,
							       context.bct);
				printf("# Bootloader[%d].", i);

				if (e)
					data.val = -1;

				bl_values[j].format(bl_values[j].id,
							bl_values[j].message,
							&data);
			}
		}
	}

	/* Display mts values */
	e = g_soc_config->get_value(token_mts_used,
				     &mts_used,
				     context.bct);

	if ((e == 0) && (mts_used> 0)) {
		int mts_count = sizeof(mts_values) / sizeof(mts_values[0]);

		printf("#\n"
		       "# These values are set by cbootimage using the\n"
		       "# mts provided by the Mts=... or MtsPreboot=...\n"
		       "# configuration option.\n"
		       "#\n");

		for (i = 0; i < mts_used; ++i) {
			for (j = 0; j < mts_count; ++j) {
				e = g_soc_config->get_mts_info(&context,
								i,
								mts_values[j].id,
								&(data.val));
				printf("# Mts[%d].", i);

				if (e)
					data.val = -1;

				mts_values[j].format(mts_values[j].id,
							mts_values[j].message,
							&data);
			}
		}
	}
	/* Display flash device parameters */
	e = g_soc_config->get_value(token_num_param_sets,
				     &parameters_used,
				     context.bct);

	for (i = 0; (e == 0) && (i < parameters_used); ++i) {
		field_item const * device_field_table = NULL;
		char const * prefix = NULL;
		field_item const * item;

		e = g_soc_config->get_dev_param(&context,
							i,
							token_dev_type,
							&type);
		printf("\n"
		       "DevType[%d] = ", i);
		display_enum_value(&context, g_soc_config->devtype_table, type);
		printf(";\n");

		switch (type) {
			case nvboot_dev_type_spi:
				device_field_table = g_soc_config->spiflash_table;
				prefix = "SpiFlashParams";
				break;

			case nvboot_dev_type_sdmmc:
				device_field_table = g_soc_config->sdmmc_table;
				prefix = "SdmmcParams";
				break;

			case nvboot_dev_type_nand:
				device_field_table = g_soc_config->nand_table;
				prefix = "NandParams";
				break;

			default:
				device_field_table = NULL;
				prefix = "";
				break;
		}

		if (!device_field_table)
			continue;

		int width = max_width(device_field_table);

		for (item = device_field_table; item->name != NULL; ++item) {
			g_soc_config->get_dev_param(&context,
							i,
							item->token,
							&data.val);
			printf("DeviceParam[%d].%s.%-*s = ",
			       i, prefix, width, item->name);

			if (e != 0)
				printf("<ERROR reading parameter (%d)>", e);
			else
				display_field_value(&context, item, data.val);

			printf(";\n");
		}
	}

	/* Display SDRAM parameters */
	e = g_soc_config->get_value(token_num_sdram_sets,
				     &sdram_used,
				     context.bct);

	for (i = 0; (e == 0) && (i < sdram_used); ++i) {
		field_item const *item;

		printf("\n");

		int width = max_width(g_soc_config->sdram_field_table);

		for (item = g_soc_config->sdram_field_table; item->name != NULL; ++item) {
			e = g_soc_config->get_sdram_param(&context,
								i,
								item->token,
								&data.val);
			printf("SDRAM[%d].%-*s = ", i, width, item->name);

			if (e != 0)
				printf("<ERROR reading parameter (%d)>", e);
			else
				display_field_value(&context, item, data.val);

			printf(";\n");
		}
	}

	/* Clean up memory. */
	cleanup_context(&context);

	return e;
}
/*****************************************************************************/