event_print.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * Copyright (c) 2020, Arm Limited. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <string.h>
  8. #include <common/debug.h>
  9. #include <drivers/measured_boot/event_log/event_log.h>
  10. #if LOG_LEVEL >= EVENT_LOG_LEVEL
  11. /*
  12. * Print TCG_EfiSpecIDEventStruct
  13. *
  14. * @param[in/out] log_addr Pointer to Event Log
  15. * @param[in/out] log_size Pointer to Event Log size
  16. */
  17. static void id_event_print(uint8_t **log_addr, size_t *log_size)
  18. {
  19. unsigned int i;
  20. uint8_t info_size, *info_size_ptr;
  21. void *ptr = *log_addr;
  22. id_event_headers_t *event = (id_event_headers_t *)ptr;
  23. id_event_algorithm_size_t *alg_ptr;
  24. uint32_t event_size, number_of_algorithms;
  25. size_t digest_len;
  26. #if ENABLE_ASSERTIONS
  27. const uint8_t *end_ptr = (uint8_t *)((uintptr_t)*log_addr + *log_size);
  28. bool valid = true;
  29. #endif
  30. assert(*log_size >= sizeof(id_event_headers_t));
  31. /* The fields of the event log header are defined to be PCRIndex of 0,
  32. * EventType of EV_NO_ACTION, Digest of 20 bytes of 0, and
  33. * Event content defined as TCG_EfiSpecIDEventStruct.
  34. */
  35. LOG_EVENT("TCG_EfiSpecIDEvent:\n");
  36. LOG_EVENT(" PCRIndex : %u\n", event->header.pcr_index);
  37. assert(event->header.pcr_index == (uint32_t)PCR_0);
  38. LOG_EVENT(" EventType : %u\n", event->header.event_type);
  39. assert(event->header.event_type == EV_NO_ACTION);
  40. LOG_EVENT(" Digest :");
  41. for (i = 0U; i < sizeof(event->header.digest); ++i) {
  42. uint8_t val = event->header.digest[i];
  43. (void)printf(" %02x", val);
  44. if ((i & U(0xF)) == 0U) {
  45. (void)printf("\n");
  46. LOG_EVENT("\t\t :");
  47. }
  48. #if ENABLE_ASSERTIONS
  49. if (val != 0U) {
  50. valid = false;
  51. }
  52. #endif
  53. }
  54. if ((i & U(0xF)) != 0U) {
  55. (void)printf("\n");
  56. }
  57. assert(valid);
  58. /* EventSize */
  59. event_size = event->header.event_size;
  60. LOG_EVENT(" EventSize : %u\n", event_size);
  61. LOG_EVENT(" Signature : %s\n",
  62. event->struct_header.signature);
  63. LOG_EVENT(" PlatformClass : %u\n",
  64. event->struct_header.platform_class);
  65. LOG_EVENT(" SpecVersion : %u.%u.%u\n",
  66. event->struct_header.spec_version_major,
  67. event->struct_header.spec_version_minor,
  68. event->struct_header.spec_errata);
  69. LOG_EVENT(" UintnSize : %u\n",
  70. event->struct_header.uintn_size);
  71. /* NumberOfAlgorithms */
  72. number_of_algorithms = event->struct_header.number_of_algorithms;
  73. LOG_EVENT(" NumberOfAlgorithms : %u\n", number_of_algorithms);
  74. /* Address of DigestSizes[] */
  75. alg_ptr = event->struct_header.digest_size;
  76. /* Size of DigestSizes[] */
  77. digest_len = number_of_algorithms * sizeof(id_event_algorithm_size_t);
  78. assert(((uintptr_t)alg_ptr + digest_len) <= (uintptr_t)end_ptr);
  79. LOG_EVENT(" DigestSizes :\n");
  80. for (i = 0U; i < number_of_algorithms; ++i) {
  81. LOG_EVENT(" #%u AlgorithmId : SHA", i);
  82. uint16_t algorithm_id = alg_ptr[i].algorithm_id;
  83. switch (algorithm_id) {
  84. case TPM_ALG_SHA256:
  85. (void)printf("256\n");
  86. break;
  87. case TPM_ALG_SHA384:
  88. (void)printf("384\n");
  89. break;
  90. case TPM_ALG_SHA512:
  91. (void)printf("512\n");
  92. break;
  93. default:
  94. (void)printf("?\n");
  95. ERROR("Algorithm 0x%x not found\n", algorithm_id);
  96. assert(false);
  97. }
  98. LOG_EVENT(" DigestSize : %u\n",
  99. alg_ptr[i].digest_size);
  100. }
  101. /* Address of VendorInfoSize */
  102. info_size_ptr = (uint8_t *)((uintptr_t)alg_ptr + digest_len);
  103. assert((uintptr_t)info_size_ptr <= (uintptr_t)end_ptr);
  104. info_size = *info_size_ptr++;
  105. LOG_EVENT(" VendorInfoSize : %u\n", info_size);
  106. /* Check VendorInfo end address */
  107. assert(((uintptr_t)info_size_ptr + info_size) <= (uintptr_t)end_ptr);
  108. /* Check EventSize */
  109. assert(event_size == (sizeof(id_event_struct_t) +
  110. digest_len + info_size));
  111. if (info_size != 0U) {
  112. LOG_EVENT(" VendorInfo :");
  113. for (i = 0U; i < info_size; ++i) {
  114. (void)printf(" %02x", *info_size_ptr++);
  115. }
  116. (void)printf("\n");
  117. }
  118. *log_size -= (uintptr_t)info_size_ptr - (uintptr_t)*log_addr;
  119. *log_addr = info_size_ptr;
  120. }
  121. /*
  122. * Print TCG_PCR_EVENT2
  123. *
  124. * @param[in/out] log_addr Pointer to Event Log
  125. * @param[in/out] log_size Pointer to Event Log size
  126. */
  127. static void event2_print(uint8_t **log_addr, size_t *log_size)
  128. {
  129. uint32_t event_size, count;
  130. size_t sha_size, digests_size = 0U;
  131. void *ptr = *log_addr;
  132. #if ENABLE_ASSERTIONS
  133. const uint8_t *end_ptr = (uint8_t *)((uintptr_t)*log_addr + *log_size);
  134. #endif
  135. assert(*log_size >= sizeof(event2_header_t));
  136. LOG_EVENT("PCR_Event2:\n");
  137. LOG_EVENT(" PCRIndex : %u\n",
  138. ((event2_header_t *)ptr)->pcr_index);
  139. LOG_EVENT(" EventType : %u\n",
  140. ((event2_header_t *)ptr)->event_type);
  141. count = ((event2_header_t *)ptr)->digests.count;
  142. LOG_EVENT(" Digests Count : %u\n", count);
  143. /* Address of TCG_PCR_EVENT2.Digests[] */
  144. ptr = (uint8_t *)ptr + sizeof(event2_header_t);
  145. assert(((uintptr_t)ptr <= (uintptr_t)end_ptr) && (count != 0U));
  146. for (unsigned int i = 0U; i < count; ++i) {
  147. /* Check AlgorithmId address */
  148. assert(((uintptr_t)ptr +
  149. offsetof(tpmt_ha, digest)) <= (uintptr_t)end_ptr);
  150. LOG_EVENT(" #%u AlgorithmId : SHA", i);
  151. switch (((tpmt_ha *)ptr)->algorithm_id) {
  152. case TPM_ALG_SHA256:
  153. sha_size = SHA256_DIGEST_SIZE;
  154. (void)printf("256\n");
  155. break;
  156. case TPM_ALG_SHA384:
  157. sha_size = SHA384_DIGEST_SIZE;
  158. (void)printf("384\n");
  159. break;
  160. case TPM_ALG_SHA512:
  161. sha_size = SHA512_DIGEST_SIZE;
  162. (void)printf("512\n");
  163. break;
  164. default:
  165. (void)printf("?\n");
  166. ERROR("Algorithm 0x%x not found\n",
  167. ((tpmt_ha *)ptr)->algorithm_id);
  168. panic();
  169. }
  170. /* End of Digest[] */
  171. ptr = (uint8_t *)((uintptr_t)ptr + offsetof(tpmt_ha, digest));
  172. assert(((uintptr_t)ptr + sha_size) <= (uintptr_t)end_ptr);
  173. /* Total size of all digests */
  174. digests_size += sha_size;
  175. LOG_EVENT(" Digest :");
  176. for (unsigned int j = 0U; j < sha_size; ++j) {
  177. (void)printf(" %02x", *(uint8_t *)ptr++);
  178. if ((j & U(0xF)) == U(0xF)) {
  179. (void)printf("\n");
  180. if (j < (sha_size - 1U)) {
  181. LOG_EVENT("\t\t :");
  182. }
  183. }
  184. }
  185. }
  186. /* TCG_PCR_EVENT2.EventSize */
  187. assert(((uintptr_t)ptr + offsetof(event2_data_t, event)) <= (uintptr_t)end_ptr);
  188. event_size = ((event2_data_t *)ptr)->event_size;
  189. LOG_EVENT(" EventSize : %u\n", event_size);
  190. /* Address of TCG_PCR_EVENT2.Event[EventSize] */
  191. ptr = (uint8_t *)((uintptr_t)ptr + offsetof(event2_data_t, event));
  192. /* End of TCG_PCR_EVENT2.Event[EventSize] */
  193. assert(((uintptr_t)ptr + event_size) <= (uintptr_t)end_ptr);
  194. if ((event_size == sizeof(startup_locality_event_t)) &&
  195. (strcmp((const char *)ptr, TCG_STARTUP_LOCALITY_SIGNATURE) == 0)) {
  196. LOG_EVENT(" Signature : %s\n",
  197. ((startup_locality_event_t *)ptr)->signature);
  198. LOG_EVENT(" StartupLocality : %u\n",
  199. ((startup_locality_event_t *)ptr)->startup_locality);
  200. } else {
  201. LOG_EVENT(" Event : %s\n", (uint8_t *)ptr);
  202. }
  203. *log_size -= (uintptr_t)ptr + event_size - (uintptr_t)*log_addr;
  204. *log_addr = (uint8_t *)ptr + event_size;
  205. }
  206. #endif /* LOG_LEVEL >= EVENT_LOG_LEVEL */
  207. /*
  208. * Print Event Log
  209. *
  210. * @param[in] log_addr Pointer to Event Log
  211. * @param[in] log_size Event Log size
  212. */
  213. void dump_event_log(uint8_t *log_addr, size_t log_size)
  214. {
  215. #if LOG_LEVEL >= EVENT_LOG_LEVEL
  216. assert(log_addr != NULL);
  217. /* Print TCG_EfiSpecIDEvent */
  218. id_event_print(&log_addr, &log_size);
  219. while (log_size != 0U) {
  220. event2_print(&log_addr, &log_size);
  221. }
  222. #endif
  223. }