| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 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 |
|
| 35 |
|
| 36 |
int enable_debug; |
| 37 |
static int help_only; |
| 38 |
cbootimage_soc_config * g_soc_config; |
| 39 |
|
| 40 |
|
| 41 |
|
| 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 |
|
| 122 |
case 't': |
| 123 |
|
| 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 |
|
| 159 |
if (!context->boot_data_version) |
| 160 |
t20_get_soc_config(context, &g_soc_config); |
| 161 |
|
| 162 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 217 |
printf("bct size: %d\n", context.bct_size); |
| 218 |
} |
| 219 |
|
| 220 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 282 |
else if (context.generate_bct == 0) |
| 283 |
process_config_file(&context, 1); |
| 284 |
|
| 285 |
else { |
| 286 |
|
| 287 |
init_bct(&context); |
| 288 |
|
| 289 |
process_config_file(&context, 0); |
| 290 |
|
| 291 |
begin_update(&context); |
| 292 |
|
| 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 |
|
| 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 |
|
| 324 |
|
| 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 |
|
| 333 |
if (context.raw_file) |
| 334 |
fclose(context.raw_file); |
| 335 |
|
| 336 |
|
| 337 |
cleanup_context(&context); |
| 338 |
return e; |
| 339 |
} |