Anton Staaf | c624427 | 2011-02-24 10:17:50 -0800 | [diff] [blame] | 1 | /** |
| 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 Staaf | e517a4f | 2011-03-14 12:28:06 -0700 | [diff] [blame] | 27 | #include "parse.h" |
Anton Staaf | c624427 | 2011-02-24 10:17:50 -0800 | [diff] [blame] | 28 | |
| 29 | #include <string.h> |
| 30 | |
| 31 | int enable_debug = 0; |
| 32 | |
| 33 | typedef struct { |
| 34 | nvbct_lib_id id; |
| 35 | char const * message; |
| 36 | } value_data; |
| 37 | |
| 38 | static value_data const values[] = { |
Anton Staaf | 20379c1 | 2011-03-14 15:38:59 -0700 | [diff] [blame] | 39 | { 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 Staaf | c624427 | 2011-02-24 10:17:50 -0800 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | static value_data const bl_values[] = { |
Anton Staaf | 20379c1 | 2011-03-14 15:38:59 -0700 | [diff] [blame] | 53 | { 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 Palatin | 9947584 | 2011-03-07 16:09:25 -0500 | [diff] [blame] | 60 | }; |
| 61 | |
Anton Staaf | e517a4f | 2011-03-14 12:28:06 -0700 | [diff] [blame] | 62 | /*****************************************************************************/ |
| 63 | static void usage(void) |
Anton Staaf | c624427 | 2011-02-24 10:17:50 -0800 | [diff] [blame] | 64 | { |
| 65 | printf("Usage: bct_dump bctfile\n"); |
| 66 | printf(" bctfile BCT filename to read and display\n"); |
| 67 | } |
Anton Staaf | e517a4f | 2011-03-14 12:28:06 -0700 | [diff] [blame] | 68 | /*****************************************************************************/ |
| 69 | static int max_width(field_item const * table) |
| 70 | { |
| 71 | int width = 0; |
| 72 | int i; |
Anton Staaf | c624427 | 2011-02-24 10:17:50 -0800 | [diff] [blame] | 73 | |
Anton Staaf | e517a4f | 2011-03-14 12:28:06 -0700 | [diff] [blame] | 74 | 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 Staaf | 20379c1 | 2011-03-14 15:38:59 -0700 | [diff] [blame] | 85 | static 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 | /*****************************************************************************/ |
| 105 | static 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 | /*****************************************************************************/ |
| 117 | static int display_field_value(build_image_context *context, |
| 118 | field_item const * item, |
| 119 | u_int32_t value) |
Anton Staaf | e517a4f | 2011-03-14 12:28:06 -0700 | [diff] [blame] | 120 | { |
| 121 | switch (item->type) |
| 122 | { |
| 123 | case field_type_enum: |
Anton Staaf | 20379c1 | 2011-03-14 15:38:59 -0700 | [diff] [blame] | 124 | display_enum_value(context, item->enum_table, value); |
| 125 | break; |
Anton Staaf | e517a4f | 2011-03-14 12:28:06 -0700 | [diff] [blame] | 126 | |
| 127 | case field_type_u32: |
| 128 | printf("0x%08x", value); |
| 129 | break; |
| 130 | |
| 131 | case field_type_u8: |
Anton Staaf | 20379c1 | 2011-03-14 15:38:59 -0700 | [diff] [blame] | 132 | printf("%d", value); |
Anton Staaf | e517a4f | 2011-03-14 12:28:06 -0700 | [diff] [blame] | 133 | break; |
| 134 | |
| 135 | default: |
| 136 | printf("<UNKNOWN FIELD TYPE (%d)>", item->type); |
| 137 | return 1; |
| 138 | } |
| 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | /*****************************************************************************/ |
| 143 | int main(int argc, char *argv[]) |
Anton Staaf | c624427 | 2011-02-24 10:17:50 -0800 | [diff] [blame] | 144 | { |
| 145 | int e; |
| 146 | build_image_context context; |
| 147 | u_int32_t bootloaders_used; |
Anton Staaf | 496f965 | 2011-03-02 09:23:27 -0800 | [diff] [blame] | 148 | u_int32_t parameters_used; |
Vincent Palatin | 9947584 | 2011-03-07 16:09:25 -0500 | [diff] [blame] | 149 | u_int32_t sdram_used; |
Anton Staaf | 496f965 | 2011-03-02 09:23:27 -0800 | [diff] [blame] | 150 | nvboot_dev_type type; |
Anton Staaf | c624427 | 2011-02-24 10:17:50 -0800 | [diff] [blame] | 151 | 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 Staaf | 496f965 | 2011-03-02 09:23:27 -0800 | [diff] [blame] | 173 | /* Display root values */ |
Anton Staaf | c624427 | 2011-02-24 10:17:50 -0800 | [diff] [blame] | 174 | for (i = 0; i < sizeof(values) / sizeof(values[0]); ++i) { |
| 175 | e = context.bctlib.get_value(values[i].id, &data, context.bct); |
Anton Staaf | 20379c1 | 2011-03-14 15:38:59 -0700 | [diff] [blame] | 176 | |
| 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 Staaf | c624427 | 2011-02-24 10:17:50 -0800 | [diff] [blame] | 184 | } |
| 185 | |
Anton Staaf | 496f965 | 2011-03-02 09:23:27 -0800 | [diff] [blame] | 186 | /* Display bootloader values */ |
Anton Staaf | c624427 | 2011-02-24 10:17:50 -0800 | [diff] [blame] | 187 | e = context.bctlib.get_value(nvbct_lib_id_bootloader_used, |
Anton Staaf | 2b6c88c | 2011-03-02 09:22:25 -0800 | [diff] [blame] | 188 | &bootloaders_used, |
| 189 | context.bct); |
Anton Staaf | c624427 | 2011-02-24 10:17:50 -0800 | [diff] [blame] | 190 | |
Anton Staaf | 20379c1 | 2011-03-14 15:38:59 -0700 | [diff] [blame] | 191 | if ((e == 0) && (bootloaders_used > 0)) { |
| 192 | int bl_count = sizeof(bl_values) / sizeof(bl_values[0]); |
Anton Staaf | c624427 | 2011-02-24 10:17:50 -0800 | [diff] [blame] | 193 | |
Anton Staaf | 20379c1 | 2011-03-14 15:38:59 -0700 | [diff] [blame] | 194 | 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 Staaf | c624427 | 2011-02-24 10:17:50 -0800 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
Anton Staaf | 20379c1 | 2011-03-14 15:38:59 -0700 | [diff] [blame] | 216 | /* Display flash device parameters */ |
Anton Staaf | 496f965 | 2011-03-02 09:23:27 -0800 | [diff] [blame] | 217 | e = context.bctlib.get_value(nvbct_lib_id_num_param_sets, |
| 218 | ¶meters_used, |
| 219 | context.bct); |
| 220 | |
| 221 | for (i = 0; (e == 0) && (i < parameters_used); ++i) { |
Anton Staaf | e517a4f | 2011-03-14 12:28:06 -0700 | [diff] [blame] | 222 | field_item const * device_field_table = NULL; |
Anton Staaf | 20379c1 | 2011-03-14 15:38:59 -0700 | [diff] [blame] | 223 | char const * prefix = NULL; |
Anton Staaf | e517a4f | 2011-03-14 12:28:06 -0700 | [diff] [blame] | 224 | field_item const * item; |
| 225 | |
Anton Staaf | 496f965 | 2011-03-02 09:23:27 -0800 | [diff] [blame] | 226 | e = context.bctlib.getdev_param(i, |
| 227 | nvbct_lib_id_dev_type, |
| 228 | &type, |
| 229 | context.bct); |
| 230 | |
Anton Staaf | 20379c1 | 2011-03-14 15:38:59 -0700 | [diff] [blame] | 231 | printf("\n" |
| 232 | "DevType[%d] = ", i); |
| 233 | display_enum_value(&context, s_devtype_table, type); |
| 234 | printf(";\n"); |
| 235 | |
Anton Staaf | 496f965 | 2011-03-02 09:23:27 -0800 | [diff] [blame] | 236 | switch (type) |
| 237 | { |
| 238 | case nvboot_dev_type_spi: |
Anton Staaf | e517a4f | 2011-03-14 12:28:06 -0700 | [diff] [blame] | 239 | device_field_table = s_spiflash_table; |
Anton Staaf | 20379c1 | 2011-03-14 15:38:59 -0700 | [diff] [blame] | 240 | prefix = "SpiFlashParams"; |
Anton Staaf | 496f965 | 2011-03-02 09:23:27 -0800 | [diff] [blame] | 241 | break; |
| 242 | |
| 243 | case nvboot_dev_type_sdmmc: |
Anton Staaf | e517a4f | 2011-03-14 12:28:06 -0700 | [diff] [blame] | 244 | device_field_table = s_sdmmc_table; |
Anton Staaf | 20379c1 | 2011-03-14 15:38:59 -0700 | [diff] [blame] | 245 | prefix = "SdmmcParams"; |
Anton Staaf | e517a4f | 2011-03-14 12:28:06 -0700 | [diff] [blame] | 246 | break; |
| 247 | |
| 248 | case nvboot_dev_type_nand: |
Anton Staaf | e517a4f | 2011-03-14 12:28:06 -0700 | [diff] [blame] | 249 | device_field_table = s_nand_table; |
Anton Staaf | 20379c1 | 2011-03-14 15:38:59 -0700 | [diff] [blame] | 250 | prefix = "NandParams"; |
Anton Staaf | 496f965 | 2011-03-02 09:23:27 -0800 | [diff] [blame] | 251 | break; |
| 252 | |
| 253 | default: |
Anton Staaf | e517a4f | 2011-03-14 12:28:06 -0700 | [diff] [blame] | 254 | device_field_table = NULL; |
Anton Staaf | 20379c1 | 2011-03-14 15:38:59 -0700 | [diff] [blame] | 255 | prefix = ""; |
Anton Staaf | 496f965 | 2011-03-02 09:23:27 -0800 | [diff] [blame] | 256 | break; |
| 257 | } |
| 258 | |
Anton Staaf | e517a4f | 2011-03-14 12:28:06 -0700 | [diff] [blame] | 259 | if (!device_field_table) |
| 260 | continue; |
Anton Staaf | 496f965 | 2011-03-02 09:23:27 -0800 | [diff] [blame] | 261 | |
Anton Staaf | e517a4f | 2011-03-14 12:28:06 -0700 | [diff] [blame] | 262 | int width = max_width(device_field_table); |
| 263 | |
| 264 | for (item = device_field_table; item->name != NULL; ++item) { |
Anton Staaf | 496f965 | 2011-03-02 09:23:27 -0800 | [diff] [blame] | 265 | e = context.bctlib.getdev_param(i, |
Anton Staaf | e517a4f | 2011-03-14 12:28:06 -0700 | [diff] [blame] | 266 | item->enum_value, |
Anton Staaf | 496f965 | 2011-03-02 09:23:27 -0800 | [diff] [blame] | 267 | &data, |
| 268 | context.bct); |
Anton Staaf | 20379c1 | 2011-03-14 15:38:59 -0700 | [diff] [blame] | 269 | 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 Staaf | 496f965 | 2011-03-02 09:23:27 -0800 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | |
Anton Staaf | 20379c1 | 2011-03-14 15:38:59 -0700 | [diff] [blame] | 281 | /* Display SDRAM parameters */ |
Vincent Palatin | 9947584 | 2011-03-07 16:09:25 -0500 | [diff] [blame] | 282 | 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 Staaf | e517a4f | 2011-03-14 12:28:06 -0700 | [diff] [blame] | 287 | int width = max_width(s_sdram_field_table); |
| 288 | field_item const * item; |
| 289 | |
Anton Staaf | 20379c1 | 2011-03-14 15:38:59 -0700 | [diff] [blame] | 290 | printf("\n"); |
| 291 | |
Anton Staaf | e517a4f | 2011-03-14 12:28:06 -0700 | [diff] [blame] | 292 | for (item = s_sdram_field_table; item->name != NULL; ++item) { |
Vincent Palatin | 9947584 | 2011-03-07 16:09:25 -0500 | [diff] [blame] | 293 | e = context.bctlib.get_sdram_params(i, |
Anton Staaf | e517a4f | 2011-03-14 12:28:06 -0700 | [diff] [blame] | 294 | item->enum_value, |
| 295 | &data, |
| 296 | context.bct); |
Anton Staaf | 20379c1 | 2011-03-14 15:38:59 -0700 | [diff] [blame] | 297 | printf("SDRAM[%d].%-*s = ", i, width, item->name); |
Anton Staaf | e517a4f | 2011-03-14 12:28:06 -0700 | [diff] [blame] | 298 | |
| 299 | if (e != 0) |
| 300 | printf("<ERROR reading parameter (%d)>", e); |
| 301 | else |
Anton Staaf | 20379c1 | 2011-03-14 15:38:59 -0700 | [diff] [blame] | 302 | display_field_value(&context, item, data); |
Anton Staaf | e517a4f | 2011-03-14 12:28:06 -0700 | [diff] [blame] | 303 | |
Anton Staaf | 20379c1 | 2011-03-14 15:38:59 -0700 | [diff] [blame] | 304 | printf(";\n"); |
Vincent Palatin | 9947584 | 2011-03-07 16:09:25 -0500 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | |
Anton Staaf | c624427 | 2011-02-24 10:17:50 -0800 | [diff] [blame] | 308 | /* Clean up memory. */ |
| 309 | cleanup_context(&context); |
| 310 | |
| 311 | return e; |
| 312 | } |
Anton Staaf | 20379c1 | 2011-03-14 15:38:59 -0700 | [diff] [blame] | 313 | /*****************************************************************************/ |