bdb_helper.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* Copyright 2016 The Chromium OS Authors. All rights reserved.
  2. * Use of this source code is governed by a BSD-style license that can be
  3. * found in the LICENSE file.
  4. *
  5. * Boot descriptor block helper functions
  6. */
  7. #include <inttypes.h>
  8. #include <stdio.h>
  9. #include "2sha.h"
  10. #include "bdb.h"
  11. #include "bdb_struct.h"
  12. #include "file_type.h"
  13. enum futil_file_type ft_recognize_bdb(uint8_t *buf, uint32_t len)
  14. {
  15. const struct bdb_header *header = bdb_get_header(buf);
  16. if (bdb_check_header(header, len))
  17. return FILE_TYPE_UNKNOWN;
  18. return FILE_TYPE_BDB;
  19. }
  20. static void print_digest(const char *label, const uint8_t *digest, size_t size)
  21. {
  22. int i;
  23. if (label)
  24. printf("%s", label);
  25. for (i = 0; i < size; i++)
  26. printf("%02x", digest[i]);
  27. printf("\n");
  28. }
  29. static void print_hash_entry(const char *label, const struct bdb_hash *hash)
  30. {
  31. if (label)
  32. printf("%s", label);
  33. printf(" Offset: 0x%" PRIx64 "\n", hash->offset);
  34. printf(" Size: %d\n", hash->size);
  35. printf(" Partition: %d\n", hash->partition);
  36. printf(" Type: %d\n", hash->type);
  37. printf(" Load Address: 0x%" PRIx64 "\n", hash->load_address);
  38. print_digest(" Digest: ", hash->digest, sizeof(hash->digest));
  39. }
  40. static void print_key_info(const char *label, const struct bdb_key *key)
  41. {
  42. uint8_t digest[BDB_SHA256_DIGEST_SIZE];
  43. if (label)
  44. printf("%s", label);
  45. printf(" Struct Version: 0x%x:0x%x\n",
  46. key->struct_major_version, key->struct_minor_version);
  47. printf(" Size: %d\n", key->struct_size);
  48. printf(" Hash Algorithm: %d\n", key->hash_alg);
  49. printf(" Sign Algorithm: %d\n", key->sig_alg);
  50. printf(" Version: %d\n", key->key_version);
  51. printf(" Description: %s\n", key->description);
  52. bdb_sha256(digest, key, key->struct_size);
  53. print_digest(" Digest: ", digest, sizeof(digest));
  54. }
  55. static void print_sig_info(const char *label, const struct bdb_sig *sig)
  56. {
  57. if (label)
  58. printf("%s", label);
  59. printf(" Struct Version: 0x%x:0x%x\n",
  60. sig->struct_major_version, sig->struct_minor_version);
  61. printf(" Hash Algorithm: %d\n", sig->hash_alg);
  62. printf(" Sign Algorithm: %d\n", sig->sig_alg);
  63. printf(" Signed Size: %d\n", sig->signed_size);
  64. printf(" Description: %s\n", sig->description);
  65. }
  66. static void show_bdb_header(const uint8_t *bdb)
  67. {
  68. const struct bdb_header *header = bdb_get_header(bdb);
  69. printf("BDB Header:\n");
  70. printf(" Struct Version: 0x%x:0x%x\n",
  71. header->struct_major_version, header->struct_minor_version);
  72. printf(" Struct Size: %d\n", header->struct_size);
  73. printf(" Load Address: 0x%" PRIx64 "\n", header->bdb_load_address);
  74. printf(" Size: %d\n", header->bdb_size);
  75. printf(" Signed Size: %d\n", header->signed_size);
  76. printf(" OEM0 Size: %d\n", header->oem_area_0_size);
  77. }
  78. static void show_bdbkey_info(const uint8_t *bdb)
  79. {
  80. print_key_info("BDB key:\n", bdb_get_bdbkey(bdb));
  81. }
  82. static void show_datakey_info(const uint8_t *bdb)
  83. {
  84. print_key_info("Data key:\n", bdb_get_datakey(bdb));
  85. }
  86. static void show_header_signature(const uint8_t *bdb)
  87. {
  88. print_sig_info("Header Signature:\n" , bdb_get_header_sig(bdb));
  89. }
  90. static void show_data_header(const uint8_t *bdb)
  91. {
  92. const struct bdb_data *data = bdb_get_data(bdb);
  93. printf("Data Header:\n");
  94. printf(" Struct Version: 0x%x:0x%x\n",
  95. data->struct_major_version, data->struct_minor_version);
  96. printf(" Data Version: %d\n", data->data_version);
  97. printf(" # of Hashes: %d\n", data->num_hashes);
  98. printf(" Hash Entry Size: %d\n", data->hash_entry_size);
  99. printf(" Signed Size: %d\n", data->signed_size);
  100. printf(" Description: %s\n", data->description);
  101. }
  102. static void show_hashes(const uint8_t *bdb)
  103. {
  104. const struct bdb_data *data = bdb_get_data(bdb);
  105. int i;
  106. for (i = 0; i < data->num_hashes; i++) {
  107. const struct bdb_hash *hash = bdb_get_hash_by_index(bdb, i);
  108. printf("Hash #%d:\n", i);
  109. print_hash_entry(NULL, hash);
  110. }
  111. }
  112. static void show_data_signature(const uint8_t *bdb)
  113. {
  114. print_sig_info("Data Signature:\n" , bdb_get_data_sig(bdb));
  115. }
  116. int ft_show_bdb(const char *name, uint8_t *buf, uint32_t len, void *data)
  117. {
  118. const struct bdb_header *header = bdb_get_header(buf);
  119. int rv;
  120. /* We can get here because of '--type' option */
  121. rv = bdb_check_header(header, len);
  122. if (rv) {
  123. fprintf(stderr, "ERROR: Invalid BDB blob: %d\n", rv);
  124. return 1;
  125. }
  126. printf("Boot Descriptor Block: %s\n", name);
  127. show_bdb_header(buf);
  128. show_bdbkey_info(buf);
  129. show_datakey_info(buf);
  130. show_header_signature(buf);
  131. show_data_header(buf);
  132. show_hashes(buf);
  133. show_data_signature(buf);
  134. return 0;
  135. }