OLD | NEW |
1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // Utility for manipulating Google Binary Block (GBB) | 5 // Utility for manipulating Google Binary Block (GBB) |
6 // | 6 // |
7 | 7 |
8 #include "gbb_utility.h" | 8 #include "gbb_utility.h" |
9 | 9 |
10 #include <assert.h> | 10 #include <assert.h> |
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 *pname = "root_key"; | 250 *pname = "root_key"; |
251 break; | 251 break; |
252 | 252 |
253 case PROP_BMPFV: | 253 case PROP_BMPFV: |
254 *poffset = header_.bmpfv_offset; | 254 *poffset = header_.bmpfv_offset; |
255 *psize = header_.bmpfv_size; | 255 *psize = header_.bmpfv_size; |
256 if (pname) | 256 if (pname) |
257 *pname = "bmp_fv"; | 257 *pname = "bmp_fv"; |
258 break; | 258 break; |
259 | 259 |
| 260 case PROP_RCVKEY: |
| 261 *poffset = header_.recovery_key_offset;; |
| 262 *psize = header_.recovery_key_size; |
| 263 if (pname) |
| 264 *pname = "recovery_key"; |
| 265 break; |
| 266 |
260 default: | 267 default: |
| 268 if (verbose) { |
| 269 fprintf(stderr, " internal error: unknown property (%d).\n", |
| 270 static_cast<int>(i)); |
| 271 } |
261 assert(!"invalid property index."); | 272 assert(!"invalid property index."); |
262 return false; | 273 return false; |
263 } | 274 } |
264 | 275 |
265 return true; | 276 return true; |
266 } | 277 } |
267 | 278 |
268 bool GoogleBinaryBlockUtil::set_property(PROPINDEX i, const string &value) { | 279 bool GoogleBinaryBlockUtil::set_property(PROPINDEX i, const string &value) { |
269 uint32_t prop_size; | 280 uint32_t prop_size; |
270 uint32_t prop_offset; | 281 uint32_t prop_offset; |
271 const char *prop_name; | 282 const char *prop_name; |
272 | 283 |
273 assert(is_valid_gbb); | 284 assert(is_valid_gbb); |
274 | 285 |
275 if (!find_property(i, &prop_offset, &prop_size, &prop_name)) { | 286 if (!find_property(i, &prop_offset, &prop_size, &prop_name)) |
276 if (verbose) | |
277 fprintf(stderr, " internal error: unknown property (%d).\n", | |
278 static_cast<int>(i)); | |
279 return false; | 287 return false; |
280 } | |
281 | 288 |
282 if (prop_size < value.size()) { | 289 if (prop_size < value.size()) { |
283 if (verbose) | 290 if (verbose) |
284 fprintf(stderr, " error: value size (%zu) exceed property capacity " | 291 fprintf(stderr, " error: value size (%zu) exceed property capacity " |
285 "(%u): %s\n", value.size(), prop_size, prop_name); | 292 "(%u): %s\n", value.size(), prop_size, prop_name); |
286 return false; | 293 return false; |
287 } | 294 } |
288 | 295 |
289 if (i == PROP_HWID && prop_size == value.size()) { | 296 if (i == PROP_HWID && prop_size == value.size()) { |
290 // special case: this is NUL-terminated so it's better to keep one more \0 | 297 // special case: this is NUL-terminated so it's better to keep one more \0 |
(...skipping 10 matching lines...) Expand all Loading... |
301 return true; | 308 return true; |
302 } | 309 } |
303 | 310 |
304 string GoogleBinaryBlockUtil::get_property(PROPINDEX i) const { | 311 string GoogleBinaryBlockUtil::get_property(PROPINDEX i) const { |
305 uint32_t prop_size; | 312 uint32_t prop_size; |
306 uint32_t prop_offset; | 313 uint32_t prop_offset; |
307 const char *prop_name; | 314 const char *prop_name; |
308 | 315 |
309 assert(is_valid_gbb); | 316 assert(is_valid_gbb); |
310 | 317 |
311 if (!find_property(i, &prop_offset, &prop_size, &prop_name)) { | 318 if (!find_property(i, &prop_offset, &prop_size, &prop_name)) |
312 if (verbose) | |
313 fprintf(stderr, " internal error: unknown property (%d).\n", | |
314 static_cast<int>(i)); | |
315 return ""; | 319 return ""; |
316 } | |
317 | 320 |
318 // check range again to allow empty value (for compatbility) | 321 // check range again to allow empty value (for compatbility) |
319 if (prop_offset == 0 && prop_size == 0) { | 322 if (prop_offset == 0 && prop_size == 0) { |
320 if (verbose) | 323 if (verbose) |
321 fprintf(stderr, " warning: empty property (%d): %s.\n", | 324 fprintf(stderr, " warning: empty property (%d): %s.\n", |
322 static_cast<int>(i), prop_name); | 325 static_cast<int>(i), prop_name); |
323 return ""; | 326 return ""; |
324 } | 327 } |
325 | 328 |
326 string::const_iterator dest = file_content_.begin() + | 329 string::const_iterator dest = file_content_.begin() + |
327 header_offset_ + prop_offset; | 330 header_offset_ + prop_offset; |
328 return string(dest, dest + prop_size); | 331 return string(dest, dest + prop_size); |
329 } | 332 } |
330 | 333 |
331 string GoogleBinaryBlockUtil::get_property_name(PROPINDEX i) const { | 334 string GoogleBinaryBlockUtil::get_property_name(PROPINDEX i) const { |
332 uint32_t unused_off, unused_size; | 335 uint32_t unused_off, unused_size; |
333 const char *prop_name; | 336 const char *prop_name; |
334 | 337 |
335 if (!find_property(i, &unused_off, &unused_size, &prop_name)) { | 338 if (!find_property(i, &unused_off, &unused_size, &prop_name)) { |
336 if (verbose) | |
337 fprintf(stderr, " internal error: unknown property (%d).\n", | |
338 static_cast<int>(i)); | |
339 assert(!"invalid property index."); | 339 assert(!"invalid property index."); |
340 return ""; | 340 return ""; |
341 } | 341 } |
342 | 342 |
343 return prop_name; | 343 return prop_name; |
344 } | 344 } |
345 | 345 |
346 bool GoogleBinaryBlockUtil::set_hwid(const char *hwid) { | 346 bool GoogleBinaryBlockUtil::set_hwid(const char *hwid) { |
347 return set_property(PROP_HWID, hwid); | 347 return set_property(PROP_HWID, hwid); |
348 } | 348 } |
349 | 349 |
350 bool GoogleBinaryBlockUtil::set_rootkey(const std::string &value) { | 350 bool GoogleBinaryBlockUtil::set_rootkey(const std::string &value) { |
351 return set_property(PROP_ROOTKEY, value); | 351 return set_property(PROP_ROOTKEY, value); |
352 } | 352 } |
353 | 353 |
354 bool GoogleBinaryBlockUtil::set_bmpfv(const string &value) { | 354 bool GoogleBinaryBlockUtil::set_bmpfv(const string &value) { |
355 return set_property(PROP_BMPFV, value); | 355 return set_property(PROP_BMPFV, value); |
356 } | 356 } |
357 | 357 |
| 358 bool GoogleBinaryBlockUtil::set_recovery_key(const string &value) { |
| 359 return set_property(PROP_RCVKEY, value); |
| 360 } |
| 361 |
358 } // namespace vboot_reference | 362 } // namespace vboot_reference |
359 | 363 |
360 #ifdef WITH_UTIL_MAIN | 364 #ifdef WITH_UTIL_MAIN |
361 | 365 |
362 /////////////////////////////////////////////////////////////////////// | 366 /////////////////////////////////////////////////////////////////////// |
363 // command line utilities | 367 // command line utilities |
364 | 368 |
365 #include <map> | 369 #include <map> |
366 | 370 |
367 using vboot_reference::GoogleBinaryBlockUtil; | 371 using vboot_reference::GoogleBinaryBlockUtil; |
368 | 372 |
369 // utility function: provide usage of this utility and exit. | 373 // utility function: provide usage of this utility and exit. |
370 static void usagehelp_exit(const char *prog_name) { | 374 static void usagehelp_exit(const char *prog_name) { |
371 printf( | 375 printf( |
372 "Utility to manage Google Binary Block (GBB)\n" | 376 "Utility to manage Google Binary Block (GBB)\n" |
373 "Usage: %s [-g|-s] [OPTIONS] bios_file [output_file]\n" | 377 "Usage: %s [-g|-s] [OPTIONS] bios_file [output_file]\n" |
374 "\n" | 378 "\n" |
375 "GET MODE:\n" | 379 "GET MODE:\n" |
376 "-g, --get (default)\tGet (read) from bios_file, " | 380 "-g, --get (default)\tGet (read) from bios_file, " |
377 "with following options:\n" | 381 "with following options:\n" |
378 " --hwid \tReport hardware id (default).\n" | 382 " --hwid \tReport hardware id (default).\n" |
379 " -k, --rootkey=FILE \tFile name to export Root Key.\n" | 383 " -k, --rootkey=FILE \tFile name to export Root Key.\n" |
380 " -b, --bmpfv=FILE \tFile name to export Bitmap FV.\n" | 384 " -b, --bmpfv=FILE \tFile name to export Bitmap FV.\n" |
| 385 " --recoverykey=FILE\tFile name to export Recovery Key.\n" |
381 "\n" | 386 "\n" |
382 "SET MODE:\n" | 387 "SET MODE:\n" |
383 "-s, --set \tSet (write) to bios_file, " | 388 "-s, --set \tSet (write) to bios_file, " |
384 "with following options:\n" | 389 "with following options:\n" |
385 " -o, --output=FILE \tNew file name for ouptput.\n" | 390 " -o, --output=FILE \tNew file name for ouptput.\n" |
386 " -i, --hwid=HWID \tThe new hardware id to be changed.\n" | 391 " -i, --hwid=HWID \tThe new hardware id to be changed.\n" |
387 " -k, --rootkey=FILE \tFile name of new Root Key.\n" | 392 " -k, --rootkey=FILE \tFile name of new Root Key.\n" |
388 " -b, --bmpfv=FILE \tFile name of new Bitmap FV.\n" | 393 " -b, --bmpfv=FILE \tFile name of new Bitmap FV.\n" |
| 394 " --recoverykey=FILE\tFile name of new Recovery Key.\n" |
389 "\n" | 395 "\n" |
390 "SAMPLE:\n" | 396 "SAMPLE:\n" |
391 " %s -g bios.bin\n" | 397 " %s -g bios.bin\n" |
392 " %s --set --hwid='New Model' -k key.bin bios.bin newbios.bin\n" | 398 " %s --set --hwid='New Model' -k key.bin bios.bin newbios.bin\n" |
393 , prog_name, prog_name, prog_name); | 399 , prog_name, prog_name, prog_name); |
394 exit(1); | 400 exit(1); |
395 } | 401 } |
396 | 402 |
397 // utility function: export a property from GBB to given file. | 403 // utility function: export a property from GBB to given file. |
398 // if filename was empty, export to console (screen). | 404 // if filename was empty, export to console (screen). |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
483 | 489 |
484 // snippets for getopt_long | 490 // snippets for getopt_long |
485 int option_index, opt; | 491 int option_index, opt; |
486 static struct option long_options[] = { | 492 static struct option long_options[] = { |
487 {"get", 0, NULL, 'g' }, | 493 {"get", 0, NULL, 'g' }, |
488 {"set", 0, NULL, 's' }, | 494 {"set", 0, NULL, 's' }, |
489 {"output", 1, NULL, 'o' }, | 495 {"output", 1, NULL, 'o' }, |
490 {"hwid", 2, NULL, 'i' }, | 496 {"hwid", 2, NULL, 'i' }, |
491 {"rootkey", 1, NULL, 'k' }, | 497 {"rootkey", 1, NULL, 'k' }, |
492 {"bmpfv", 1, NULL, 'b' }, | 498 {"bmpfv", 1, NULL, 'b' }, |
| 499 {"recoverykey", 1, NULL, 'R' }, |
493 { NULL, 0, NULL, 0 }, | 500 { NULL, 0, NULL, 0 }, |
494 }; | 501 }; |
495 | 502 |
496 // parse command line options | 503 // parse command line options |
497 while ((opt = getopt_long(argc, argv, "gso:i:k:b:", | 504 while ((opt = getopt_long(argc, argv, "gso:i:k:b:", |
498 long_options, &option_index)) >= 0) { | 505 long_options, &option_index)) >= 0) { |
499 switch (opt) { | 506 switch (opt) { |
500 case 'g': | 507 case 'g': |
501 myopts.get_mode = true; | 508 myopts.get_mode = true; |
502 break; | 509 break; |
(...skipping 17 matching lines...) Expand all Loading... |
520 GoogleBinaryBlockUtil::PROP_ROOTKEY, optarg)) | 527 GoogleBinaryBlockUtil::PROP_ROOTKEY, optarg)) |
521 usagehelp_exit(myname); | 528 usagehelp_exit(myname); |
522 break; | 529 break; |
523 | 530 |
524 case 'b': | 531 case 'b': |
525 if (!opt_props.set_new_value( | 532 if (!opt_props.set_new_value( |
526 GoogleBinaryBlockUtil::PROP_BMPFV, optarg)) | 533 GoogleBinaryBlockUtil::PROP_BMPFV, optarg)) |
527 usagehelp_exit(myname); | 534 usagehelp_exit(myname); |
528 break; | 535 break; |
529 | 536 |
| 537 case 'R': |
| 538 if (!opt_props.set_new_value( |
| 539 GoogleBinaryBlockUtil::PROP_RCVKEY, optarg)) |
| 540 usagehelp_exit(myname); |
| 541 break; |
| 542 |
530 default: | 543 default: |
531 case '?': | 544 case '?': |
532 usagehelp_exit(myname); | 545 usagehelp_exit(myname); |
533 break; | 546 break; |
534 } | 547 } |
535 } | 548 } |
536 argc -= optind; | 549 argc -= optind; |
537 argv += optind; | 550 argv += optind; |
538 | 551 |
539 // adjust non-dashed parameters | 552 // adjust non-dashed parameters |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
626 } else { | 639 } else { |
627 printf("successfully saved new image to: %s\n", myopts.output_fn.c_str()); | 640 printf("successfully saved new image to: %s\n", myopts.output_fn.c_str()); |
628 } | 641 } |
629 } | 642 } |
630 | 643 |
631 return 0; | 644 return 0; |
632 } | 645 } |
633 | 646 |
634 #endif // WITH_UTIL_MAIN | 647 #endif // WITH_UTIL_MAIN |
635 | 648 |
OLD | NEW |