blob: fb0df5701ac201d595ecc2504edf2ca1e243dd52 [file] [log] [blame]
Anton Staafc6244272011-02-24 10:17:50 -08001/**
2 * Copyright (c) 2011 NVIDIA Corporation. All rights reserved.
3 *
4 * See file CREDITS for list of people who contributed to this
5 * project.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20 * MA 02111-1307 USA
21 */
22
23#include "cbootimage.h"
24#include "nvbctlib.h"
25#include "data_layout.h"
26#include "context.h"
Anton Staafe517a4f2011-03-14 12:28:06 -070027#include "parse.h"
Anton Staafc6244272011-02-24 10:17:50 -080028
29#include <string.h>
30
31int enable_debug = 0;
32
33typedef struct {
34 nvbct_lib_id id;
35 char const * message;
36} value_data;
37
38static value_data const values[] = {
Anton Staaf20379c12011-03-14 15:38:59 -070039 { nvbct_lib_id_boot_data_version, "Version = 0x%08x;\n" },
40 { nvbct_lib_id_block_size_log2, "BlockSize = 0x%08x;\n" },
41 { nvbct_lib_id_page_size_log2, "PageSize = 0x%08x;\n" },
42 { nvbct_lib_id_partition_size, "PartitionSize = 0x%08x;\n" },
43 { nvbct_lib_id_bootloader_used, "# Bootloader used = %d;\n" },
44 { nvbct_lib_id_bootloaders_max, "# Bootloaders max = %d;\n" },
45 { nvbct_lib_id_bct_size, "# BCT size = %d;\n" },
46 { nvbct_lib_id_hash_size, "# Hash size = %d;\n" },
47 { nvbct_lib_id_crypto_offset, "# Crypto offset = %d;\n" },
48 { nvbct_lib_id_crypto_length, "# Crypto length = %d;\n" },
49 { nvbct_lib_id_max_bct_search_blks, "# Max BCT search blocks = %d;\n" },
Anton Staafc6244272011-02-24 10:17:50 -080050};
51
52static value_data const bl_values[] = {
Anton Staaf20379c12011-03-14 15:38:59 -070053 { nvbct_lib_id_bl_version, "Version = 0x%08x;\n" },
54 { nvbct_lib_id_bl_start_blk, "Start block = %d;\n" },
55 { nvbct_lib_id_bl_start_page, "Start page = %d;\n" },
56 { nvbct_lib_id_bl_length, "Length = %d;\n" },
57 { nvbct_lib_id_bl_load_addr, "Load address = 0x%08x;\n" },
58 { nvbct_lib_id_bl_entry_point, "Entry point = 0x%08x;\n" },
59 { nvbct_lib_id_bl_attribute, "Attributes = 0x%08x;\n" },
Vincent Palatin99475842011-03-07 16:09:25 -050060};
61
Anton Staafe517a4f2011-03-14 12:28:06 -070062/*****************************************************************************/
63static void usage(void)
Anton Staafc6244272011-02-24 10:17:50 -080064{
65 printf("Usage: bct_dump bctfile\n");
66 printf(" bctfile BCT filename to read and display\n");
67}
Anton Staafe517a4f2011-03-14 12:28:06 -070068/*****************************************************************************/
69static int max_width(field_item const * table)
70{
71 int width = 0;
72 int i;
Anton Staafc6244272011-02-24 10:17:50 -080073
Anton Staafe517a4f2011-03-14 12:28:06 -070074 for (i = 0; table[i].name != NULL; ++i)
75 {
76 int length = strlen(table[i].name);
77
78 if (width < length)
79 width = length;
80 }
81
82 return width;
83}
84/*****************************************************************************/
Anton Staaf20379c12011-03-14 15:38:59 -070085static enum_item const * find_enum_item(build_image_context *context,
86 enum_item const * table,
87 u_int32_t value)
88{
89 int i;
90
91 for (i = 0; table[i].name != NULL; ++i)
92 {
93 u_int32_t table_value;
94
95 if (!context->bctlib.get_value(table[i].value,
96 &table_value,
97 context->bct) &&
98 table_value == value)
99 return table + i;
100 }
101
102 return NULL;
103}
104/*****************************************************************************/
105static void display_enum_value(build_image_context *context,
106 enum_item const * table,
107 u_int32_t value)
108{
109 enum_item const * e_item = find_enum_item(context, table, value);
110
111 if (e_item)
112 printf("%s", e_item->name);
113 else
114 printf("<UNKNOWN ENUM VALUE (%d)>", value);
115}
116/*****************************************************************************/
117static int display_field_value(build_image_context *context,
118 field_item const * item,
119 u_int32_t value)
Anton Staafe517a4f2011-03-14 12:28:06 -0700120{
121 switch (item->type)
122 {
123 case field_type_enum:
Anton Staaf20379c12011-03-14 15:38:59 -0700124 display_enum_value(context, item->enum_table, value);
125 break;
Anton Staafe517a4f2011-03-14 12:28:06 -0700126
127 case field_type_u32:
128 printf("0x%08x", value);
129 break;
130
131 case field_type_u8:
Anton Staaf20379c12011-03-14 15:38:59 -0700132 printf("%d", value);
Anton Staafe517a4f2011-03-14 12:28:06 -0700133 break;
134
135 default:
136 printf("<UNKNOWN FIELD TYPE (%d)>", item->type);
137 return 1;
138 }
139
140 return 0;
141}
142/*****************************************************************************/
143int main(int argc, char *argv[])
Anton Staafc6244272011-02-24 10:17:50 -0800144{
145 int e;
146 build_image_context context;
147 u_int32_t bootloaders_used;
Anton Staaf496f9652011-03-02 09:23:27 -0800148 u_int32_t parameters_used;
Vincent Palatin99475842011-03-07 16:09:25 -0500149 u_int32_t sdram_used;
Anton Staaf496f9652011-03-02 09:23:27 -0800150 nvboot_dev_type type;
Anton Staafc6244272011-02-24 10:17:50 -0800151 u_int32_t data;
152 int i;
153 int j;
154
155 if (argc != 2)
156 usage();
157
158 memset(&context, 0, sizeof(build_image_context));
159
160 context.bct_filename = argv[1];
161
162 /* Set up the Nvbctlib function pointers. */
163 nvbct_lib_get_fns(&(context.bctlib));
164
165 e = init_context(&context);
166 if (e != 0) {
167 printf("context initialization failed. Aborting.\n");
168 return e;
169 }
170
171 read_bct_file(&context);
172
Anton Staaf496f9652011-03-02 09:23:27 -0800173 /* Display root values */
Anton Staafc6244272011-02-24 10:17:50 -0800174 for (i = 0; i < sizeof(values) / sizeof(values[0]); ++i) {
175 e = context.bctlib.get_value(values[i].id, &data, context.bct);
Anton Staaf20379c12011-03-14 15:38:59 -0700176
177 if (e != 0)
178 data = -1;
179 else if (values[i].id == nvbct_lib_id_block_size_log2 ||
180 values[i].id == nvbct_lib_id_page_size_log2)
181 data = 1 << data;
182
183 printf(values[i].message, data);
Anton Staafc6244272011-02-24 10:17:50 -0800184 }
185
Anton Staaf496f9652011-03-02 09:23:27 -0800186 /* Display bootloader values */
Anton Staafc6244272011-02-24 10:17:50 -0800187 e = context.bctlib.get_value(nvbct_lib_id_bootloader_used,
Anton Staaf2b6c88c2011-03-02 09:22:25 -0800188 &bootloaders_used,
189 context.bct);
Anton Staafc6244272011-02-24 10:17:50 -0800190
Anton Staaf20379c12011-03-14 15:38:59 -0700191 if ((e == 0) && (bootloaders_used > 0)) {
192 int bl_count = sizeof(bl_values) / sizeof(bl_values[0]);
Anton Staafc6244272011-02-24 10:17:50 -0800193
Anton Staaf20379c12011-03-14 15:38:59 -0700194 printf("#\n"
195 "# These values are set by cbootimage using the\n"
196 "# bootloader provided by the Bootloader=...\n"
197 "# configuration option.\n"
198 "#\n");
199
200 for (i = 0; i < bootloaders_used; ++i) {
201 for (j = 0; j < bl_count; ++j) {
202 e = context.bctlib.getbl_param(i,
203 bl_values[j].id,
204 &data,
205 context.bct);
206 printf("# Bootloader[%d].", i);
207
208 if (e != 0)
209 data = -1;
210
211 printf(bl_values[j].message, data);
212 }
Anton Staafc6244272011-02-24 10:17:50 -0800213 }
214 }
215
Anton Staaf20379c12011-03-14 15:38:59 -0700216 /* Display flash device parameters */
Anton Staaf496f9652011-03-02 09:23:27 -0800217 e = context.bctlib.get_value(nvbct_lib_id_num_param_sets,
218 &parameters_used,
219 context.bct);
220
221 for (i = 0; (e == 0) && (i < parameters_used); ++i) {
Anton Staafe517a4f2011-03-14 12:28:06 -0700222 field_item const * device_field_table = NULL;
Anton Staaf20379c12011-03-14 15:38:59 -0700223 char const * prefix = NULL;
Anton Staafe517a4f2011-03-14 12:28:06 -0700224 field_item const * item;
225
Anton Staaf496f9652011-03-02 09:23:27 -0800226 e = context.bctlib.getdev_param(i,
227 nvbct_lib_id_dev_type,
228 &type,
229 context.bct);
230
Anton Staaf20379c12011-03-14 15:38:59 -0700231 printf("\n"
232 "DevType[%d] = ", i);
233 display_enum_value(&context, s_devtype_table, type);
234 printf(";\n");
235
Anton Staaf496f9652011-03-02 09:23:27 -0800236 switch (type)
237 {
238 case nvboot_dev_type_spi:
Anton Staafe517a4f2011-03-14 12:28:06 -0700239 device_field_table = s_spiflash_table;
Anton Staaf20379c12011-03-14 15:38:59 -0700240 prefix = "SpiFlashParams";
Anton Staaf496f9652011-03-02 09:23:27 -0800241 break;
242
243 case nvboot_dev_type_sdmmc:
Anton Staafe517a4f2011-03-14 12:28:06 -0700244 device_field_table = s_sdmmc_table;
Anton Staaf20379c12011-03-14 15:38:59 -0700245 prefix = "SdmmcParams";
Anton Staafe517a4f2011-03-14 12:28:06 -0700246 break;
247
248 case nvboot_dev_type_nand:
Anton Staafe517a4f2011-03-14 12:28:06 -0700249 device_field_table = s_nand_table;
Anton Staaf20379c12011-03-14 15:38:59 -0700250 prefix = "NandParams";
Anton Staaf496f9652011-03-02 09:23:27 -0800251 break;
252
253 default:
Anton Staafe517a4f2011-03-14 12:28:06 -0700254 device_field_table = NULL;
Anton Staaf20379c12011-03-14 15:38:59 -0700255 prefix = "";
Anton Staaf496f9652011-03-02 09:23:27 -0800256 break;
257 }
258
Anton Staafe517a4f2011-03-14 12:28:06 -0700259 if (!device_field_table)
260 continue;
Anton Staaf496f9652011-03-02 09:23:27 -0800261
Anton Staafe517a4f2011-03-14 12:28:06 -0700262 int width = max_width(device_field_table);
263
264 for (item = device_field_table; item->name != NULL; ++item) {
Anton Staaf496f9652011-03-02 09:23:27 -0800265 e = context.bctlib.getdev_param(i,
Anton Staafe517a4f2011-03-14 12:28:06 -0700266 item->enum_value,
Anton Staaf496f9652011-03-02 09:23:27 -0800267 &data,
268 context.bct);
Anton Staaf20379c12011-03-14 15:38:59 -0700269 printf("DeviceParam[%d].%s.%-*s = ",
270 i, prefix, width, item->name);
271
272 if (e != 0)
273 printf("<ERROR reading parameter (%d)>", e);
274 else
275 display_field_value(&context, item, data);
276
277 printf(";\n");
Anton Staaf496f9652011-03-02 09:23:27 -0800278 }
279 }
280
Anton Staaf20379c12011-03-14 15:38:59 -0700281 /* Display SDRAM parameters */
Vincent Palatin99475842011-03-07 16:09:25 -0500282 e = context.bctlib.get_value(nvbct_lib_id_num_sdram_sets,
283 &sdram_used,
284 context.bct);
285
286 for (i = 0; (e == 0) && (i < sdram_used); ++i) {
Anton Staafe517a4f2011-03-14 12:28:06 -0700287 int width = max_width(s_sdram_field_table);
288 field_item const * item;
289
Anton Staaf20379c12011-03-14 15:38:59 -0700290 printf("\n");
291
Anton Staafe517a4f2011-03-14 12:28:06 -0700292 for (item = s_sdram_field_table; item->name != NULL; ++item) {
Vincent Palatin99475842011-03-07 16:09:25 -0500293 e = context.bctlib.get_sdram_params(i,
Anton Staafe517a4f2011-03-14 12:28:06 -0700294 item->enum_value,
295 &data,
296 context.bct);
Anton Staaf20379c12011-03-14 15:38:59 -0700297 printf("SDRAM[%d].%-*s = ", i, width, item->name);
Anton Staafe517a4f2011-03-14 12:28:06 -0700298
299 if (e != 0)
300 printf("<ERROR reading parameter (%d)>", e);
301 else
Anton Staaf20379c12011-03-14 15:38:59 -0700302 display_field_value(&context, item, data);
Anton Staafe517a4f2011-03-14 12:28:06 -0700303
Anton Staaf20379c12011-03-14 15:38:59 -0700304 printf(";\n");
Vincent Palatin99475842011-03-07 16:09:25 -0500305 }
306 }
307
Anton Staafc6244272011-02-24 10:17:50 -0800308 /* Clean up memory. */
309 cleanup_context(&context);
310
311 return e;
312}
Anton Staaf20379c12011-03-14 15:38:59 -0700313/*****************************************************************************/