efi_selftest_tcg2.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * efi_selftest_devicepath
  4. *
  5. * Copyright (c) 2020 Heinrich Schuchardt <xypron.glpk@gmx.de>
  6. *
  7. * Test the EFI_TCG2_PROTOCOL
  8. */
  9. #include <efi_selftest.h>
  10. #include <efi_tcg2.h>
  11. /*
  12. * Include containing the miniapp.efi application.
  13. * Note that tcg2 selftest measures the PE/COFF image,
  14. * so we must have the pre-build efi application for
  15. * each architecture.
  16. */
  17. #if defined(__arm__)
  18. #include "efi_miniapp_tcg2_arm.h"
  19. #elif defined(__aarch64__)
  20. #include "efi_miniapp_tcg2_arm64.h"
  21. #elif defined(__i386__)
  22. #include "efi_miniapp_tcg2_ia32.h"
  23. #elif defined(__x86_64__)
  24. #include "efi_miniapp_tcg2_x86_64.h"
  25. #elif defined(__riscv) && (__riscv_xlen == 32)
  26. #include "efi_miniapp_tcg2_riscv32.h"
  27. #elif defined(__riscv) && (__riscv_xlen == 64)
  28. #include "efi_miniapp_tcg2_riscv64.h"
  29. #endif
  30. #include <linux/unaligned/be_byteshift.h>
  31. #include <linux/unaligned/le_byteshift.h>
  32. #include <mapmem.h>
  33. #include <smbios.h>
  34. #include <tables_csum.h>
  35. static struct efi_boot_services *boottime;
  36. static const efi_guid_t guid_tcg2 = EFI_TCG2_PROTOCOL_GUID;
  37. /* Block size of compressed disk image */
  38. #define COMPRESSED_DISK_IMAGE_BLOCK_SIZE 8
  39. static efi_handle_t image_handle;
  40. /* Decompressed file image */
  41. static u8 *image;
  42. /* One 8 byte block of the compressed disk image */
  43. struct line {
  44. size_t addr;
  45. char *line;
  46. };
  47. /* Compressed file image */
  48. struct compressed_file_image {
  49. size_t length;
  50. struct line lines[];
  51. };
  52. static struct compressed_file_image img = EFI_ST_DISK_IMG;
  53. static struct efi_tcg2_event *efi_tcg2_event;
  54. static struct efi_runtime_services *runtime;
  55. #define BOOT_NAME_1000 u"Boot1000"
  56. #define BOOT_NAME_1001 u"Boot1001"
  57. #define BOOT_NAME_1002 u"Boot1002"
  58. #define DEFAULT_ATTR (EFI_VARIABLE_NON_VOLATILE | \
  59. EFI_VARIABLE_BOOTSERVICE_ACCESS | \
  60. EFI_VARIABLE_RUNTIME_ACCESS)
  61. /* "efidebug boot add -b 1000 test1000 virtio 0:1 /EFI/debian/grubaa64.efi" */
  62. static const u8 boot_1000[] = {
  63. 0x01, 0x00, 0x00, 0x00, 0x8d, 0x00, 0x74, 0x00, 0x65, 0x00, 0x73, 0x00,
  64. 0x74, 0x00, 0x30, 0x00, 0x00, 0x00, 0x01, 0x04, 0x14, 0x00, 0xb9, 0x73,
  65. 0x1d, 0xe6, 0x84, 0xa3, 0xcc, 0x4a, 0xae, 0xab, 0x82, 0xe8, 0x28, 0xf3,
  66. 0x62, 0x8b, 0x01, 0x04, 0x15, 0x00, 0x92, 0x37, 0x29, 0x63, 0xf5, 0xad,
  67. 0x25, 0x93, 0xb9, 0x9f, 0x4e, 0x0e, 0x45, 0x5c, 0x1b, 0x1e, 0x00, 0x04,
  68. 0x01, 0x2a, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
  69. 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x57,
  70. 0x5a, 0x47, 0xc2, 0x35, 0x27, 0x44, 0x47, 0x9f, 0x01, 0x67, 0xfe, 0xfa,
  71. 0x1d, 0x06, 0xae, 0x02, 0x02, 0x04, 0x04, 0x36, 0x00, 0x5c, 0x00, 0x45,
  72. 0x00, 0x46, 0x00, 0x49, 0x00, 0x5c, 0x00, 0x64, 0x00, 0x65, 0x00, 0x62,
  73. 0x00, 0x69, 0x00, 0x61, 0x00, 0x6e, 0x00, 0x5c, 0x00, 0x67, 0x00, 0x72,
  74. 0x00, 0x75, 0x00, 0x62, 0x00, 0x61, 0x00, 0x61, 0x00, 0x36, 0x00, 0x34,
  75. 0x00, 0x2e, 0x00, 0x65, 0x00, 0x66, 0x00, 0x69, 0x00, 0x00, 0x00, 0x7f,
  76. 0xff, 0x04, 0x00 };
  77. /* "efidebug boot add -b 1001 test1001 virtio 0:1 /EFI/debian/grubaa64.efi" */
  78. static const u8 boot_1001[] = {
  79. 0x01, 0x00, 0x00, 0x00, 0x8d, 0x00, 0x74, 0x00, 0x65, 0x00, 0x73, 0x00,
  80. 0x74, 0x00, 0x31, 0x00, 0x00, 0x00, 0x01, 0x04, 0x14, 0x00, 0xb9, 0x73,
  81. 0x1d, 0xe6, 0x84, 0xa3, 0xcc, 0x4a, 0xae, 0xab, 0x82, 0xe8, 0x28, 0xf3,
  82. 0x62, 0x8b, 0x01, 0x04, 0x15, 0x00, 0x92, 0x37, 0x29, 0x63, 0xf5, 0xad,
  83. 0x25, 0x93, 0xb9, 0x9f, 0x4e, 0x0e, 0x45, 0x5c, 0x1b, 0x1e, 0x00, 0x04,
  84. 0x01, 0x2a, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
  85. 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x57,
  86. 0x5a, 0x47, 0xc2, 0x35, 0x27, 0x44, 0x47, 0x9f, 0x01, 0x67, 0xfe, 0xfa,
  87. 0x1d, 0x06, 0xae, 0x02, 0x02, 0x04, 0x04, 0x36, 0x00, 0x5c, 0x00, 0x45,
  88. 0x00, 0x46, 0x00, 0x49, 0x00, 0x5c, 0x00, 0x64, 0x00, 0x65, 0x00, 0x62,
  89. 0x00, 0x69, 0x00, 0x61, 0x00, 0x6e, 0x00, 0x5c, 0x00, 0x67, 0x00, 0x72,
  90. 0x00, 0x75, 0x00, 0x62, 0x00, 0x61, 0x00, 0x61, 0x00, 0x36, 0x00, 0x34,
  91. 0x00, 0x2e, 0x00, 0x65, 0x00, 0x66, 0x00, 0x69, 0x00, 0x00, 0x00, 0x7f,
  92. 0xff, 0x04, 0x00 };
  93. /* "efidebug boot add -b 1002 test1002 virtio 0:1 /EFI/debian/grubaa64.efi" */
  94. static const u8 boot_1002[] = {
  95. 0x01, 0x00, 0x00, 0x00, 0x8d, 0x00, 0x74, 0x00, 0x65, 0x00, 0x73, 0x00,
  96. 0x74, 0x00, 0x32, 0x00, 0x00, 0x00, 0x01, 0x04, 0x14, 0x00, 0xb9, 0x73,
  97. 0x1d, 0xe6, 0x84, 0xa3, 0xcc, 0x4a, 0xae, 0xab, 0x82, 0xe8, 0x28, 0xf3,
  98. 0x62, 0x8b, 0x01, 0x04, 0x15, 0x00, 0x92, 0x37, 0x29, 0x63, 0xf5, 0xad,
  99. 0x25, 0x93, 0xb9, 0x9f, 0x4e, 0x0e, 0x45, 0x5c, 0x1b, 0x1e, 0x00, 0x04,
  100. 0x01, 0x2a, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
  101. 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x57,
  102. 0x5a, 0x47, 0xc2, 0x35, 0x27, 0x44, 0x47, 0x9f, 0x01, 0x67, 0xfe, 0xfa,
  103. 0x1d, 0x06, 0xae, 0x02, 0x02, 0x04, 0x04, 0x36, 0x00, 0x5c, 0x00, 0x45,
  104. 0x00, 0x46, 0x00, 0x49, 0x00, 0x5c, 0x00, 0x64, 0x00, 0x65, 0x00, 0x62,
  105. 0x00, 0x69, 0x00, 0x61, 0x00, 0x6e, 0x00, 0x5c, 0x00, 0x67, 0x00, 0x72,
  106. 0x00, 0x75, 0x00, 0x62, 0x00, 0x61, 0x00, 0x61, 0x00, 0x36, 0x00, 0x34,
  107. 0x00, 0x2e, 0x00, 0x65, 0x00, 0x66, 0x00, 0x69, 0x00, 0x00, 0x00, 0x7f,
  108. 0xff, 0x04, 0x00};
  109. /* "efidebug boot order 1002 1000 1001" */
  110. static u8 boot_order[] = {0x02, 0x10, 0x00, 0x10, 0x01, 0x10};
  111. static void *orig_smbios_table;
  112. static u64 dmi_addr = U32_MAX;
  113. #define SMBIOS_ENTRY_HEADER_SIZE 0x20
  114. /* smbios table for the measurement test */
  115. static u8 smbios_table_test[] = {
  116. 0x5f, 0x53, 0x4d, 0x5f, 0x2c, 0x1f, 0x03, 0x00, 0x54, 0x00, 0x00, 0x00,
  117. 0x00, 0x00, 0x00, 0x00, 0x5f, 0x44, 0x4d, 0x49, 0x5f, 0xe4, 0x5c, 0x01,
  118. 0x20, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00,
  119. 0x01, 0x02, 0x00, 0x00, 0x03, 0x00, 0x80, 0x08, 0x01, 0x00, 0x00, 0x00,
  120. 0x00, 0x00, 0x00, 0x0c, 0x15, 0x0a, 0xff, 0xff, 0x55, 0x2d, 0x42, 0x6f,
  121. 0x6f, 0x74, 0x00, 0x32, 0x30, 0x32, 0x31, 0x2e, 0x31, 0x30, 0x2d, 0x72,
  122. 0x63, 0x34, 0x2d, 0x30, 0x30, 0x30, 0x30, 0x35, 0x2d, 0x67, 0x37, 0x32,
  123. 0x37, 0x63, 0x33, 0x66, 0x33, 0x32, 0x35, 0x39, 0x2d, 0x64, 0x69, 0x72,
  124. 0x74, 0x79, 0x00, 0x31, 0x30, 0x2f, 0x30, 0x31, 0x2f, 0x32, 0x30, 0x32,
  125. 0x31, 0x00, 0x00, 0x01, 0x1b, 0x01, 0x00, 0x01, 0x02, 0x00, 0x03, 0x31,
  126. 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00,
  127. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x55, 0x6e, 0x6b, 0x6e, 0x6f, 0x77,
  128. 0x6e, 0x00, 0x55, 0x6e, 0x6b, 0x6e, 0x6f, 0x77, 0x6e, 0x20, 0x50, 0x72,
  129. 0x6f, 0x64, 0x75, 0x63, 0x74, 0x00, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
  130. 0x37, 0x38, 0x00, 0x00, 0x02, 0x0e, 0x02, 0x00, 0x01, 0x02, 0x00, 0x04,
  131. 0x03, 0x01, 0x01, 0x01, 0x00, 0x0a, 0x55, 0x6e, 0x6b, 0x6e, 0x6f, 0x77,
  132. 0x6e, 0x00, 0x55, 0x6e, 0x6b, 0x6e, 0x6f, 0x77, 0x6e, 0x20, 0x50, 0x72,
  133. 0x6f, 0x64, 0x75, 0x63, 0x74, 0x00, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  134. 0x33, 0x33, 0x00, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x00,
  135. 0x00, 0x03, 0x15, 0x03, 0x00, 0x01, 0x03, 0x00, 0x02, 0x03, 0x03, 0x03,
  136. 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0x6e,
  137. 0x6b, 0x6e, 0x6f, 0x77, 0x6e, 0x00, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
  138. 0x37, 0x38, 0x00, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x00,
  139. 0x00, 0x04, 0x30, 0x04, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0x00, 0x00,
  140. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00,
  141. 0x00, 0x01, 0x06, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x02, 0x03, 0x04,
  142. 0x04, 0x04, 0x08, 0x00, 0x00, 0x02, 0x00, 0x08, 0x00, 0x08, 0x00, 0x01,
  143. 0x00, 0x55, 0x6e, 0x6b, 0x6e, 0x6f, 0x77, 0x6e, 0x00, 0x31, 0x32, 0x33,
  144. 0x34, 0x35, 0x36, 0x37, 0x38, 0x00, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  145. 0x33, 0x33, 0x00, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x00,
  146. 0x00, 0x20, 0x0b, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  147. 0x00, 0x00, 0x7f, 0x04, 0x06, 0x00, 0x00, 0x00
  148. };
  149. #define IDX_ARRAY_SZ 3 /* support 24 PCRs */
  150. #define TPM2_CMD_BUF_SIZE 64
  151. /* TPM command is big endian */
  152. #define __MSB(x) ((x) >> 8)
  153. #define __LSB(x) ((x) & 0xFF)
  154. #define tpm_u16(x) __MSB(x), __LSB(x)
  155. #define tpm_u32(x) tpm_u16((x) >> 16), tpm_u16((x) & 0xFFFF)
  156. #define TPM2_PCR_READ_HEADER_SIZE 30
  157. static u8 (*pcrs)[TPM2_SHA256_DIGEST_SIZE];
  158. static u8 expected_pcrs[EFI_TCG2_MAX_PCR_INDEX + 1][TPM2_SHA256_DIGEST_SIZE] = {
  159. {0x91, 0x21, 0x37, 0xc7, 0x1a, 0x49, 0x19, 0xc8,
  160. 0xf1, 0xfb, 0xa9, 0x84, 0x5c, 0x65, 0xa9, 0xdd,
  161. 0x7b, 0xb9, 0xfe, 0xa1, 0xcd, 0x64, 0x49, 0xdd,
  162. 0xed, 0xe2, 0x65, 0x82, 0xc5, 0x3e, 0xf4, 0xc4},
  163. {0xf5, 0x79, 0xf3, 0x20, 0x62, 0x6e, 0x8b, 0x58,
  164. 0x62, 0xa3, 0x4e, 0x2f, 0xb7, 0x10, 0xac, 0x34,
  165. 0x4e, 0x68, 0x94, 0x37, 0x87, 0x29, 0xc4, 0xbe,
  166. 0xa3, 0xc4, 0xd9, 0x14, 0x2b, 0x66, 0x79, 0x9b},
  167. {0x3d, 0x45, 0x8c, 0xfe, 0x55, 0xcc, 0x03, 0xea,
  168. 0x1f, 0x44, 0x3f, 0x15, 0x62, 0xbe, 0xec, 0x8d,
  169. 0xf5, 0x1c, 0x75, 0xe1, 0x4a, 0x9f, 0xcf, 0x9a,
  170. 0x72, 0x34, 0xa1, 0x3f, 0x19, 0x8e, 0x79, 0x69},
  171. {0x3d, 0x45, 0x8c, 0xfe, 0x55, 0xcc, 0x03, 0xea,
  172. 0x1f, 0x44, 0x3f, 0x15, 0x62, 0xbe, 0xec, 0x8d,
  173. 0xf5, 0x1c, 0x75, 0xe1, 0x4a, 0x9f, 0xcf, 0x9a,
  174. 0x72, 0x34, 0xa1, 0x3f, 0x19, 0x8e, 0x79, 0x69},
  175. /* PCR[4] is different per architecture */
  176. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  177. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  178. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  179. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  180. {0x3d, 0x45, 0x8c, 0xfe, 0x55, 0xcc, 0x03, 0xea,
  181. 0x1f, 0x44, 0x3f, 0x15, 0x62, 0xbe, 0xec, 0x8d,
  182. 0xf5, 0x1c, 0x75, 0xe1, 0x4a, 0x9f, 0xcf, 0x9a,
  183. 0x72, 0x34, 0xa1, 0x3f, 0x19, 0x8e, 0x79, 0x69},
  184. /* PCR[6] is different per architecture */
  185. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  186. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  187. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  188. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  189. {0x96, 0x74, 0xae, 0xcd, 0x3f, 0x40, 0xb4, 0xa9,
  190. 0x36, 0xae, 0x19, 0xc8, 0x84, 0x8a, 0xb9, 0x5a,
  191. 0x87, 0x99, 0xd8, 0x89, 0x7f, 0xfc, 0x40, 0x48,
  192. 0x05, 0x99, 0x65, 0x2e, 0x55, 0xd4, 0x93, 0x32},
  193. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  194. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  195. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  196. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  197. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  198. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  199. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  200. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  201. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  202. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  203. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  204. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  205. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  206. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  207. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  208. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  209. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  210. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  211. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  212. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  213. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  214. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  215. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  216. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  217. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  218. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  219. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  220. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  221. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  222. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  223. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  224. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  225. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  226. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  227. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  228. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  229. {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  230. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  231. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  232. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
  233. {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  234. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  235. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  236. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
  237. {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  238. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  239. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  240. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
  241. {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  242. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  243. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  244. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
  245. {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  246. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  247. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  248. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
  249. {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  250. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  251. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  252. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
  253. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  254. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  255. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  256. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
  257. };
  258. /*
  259. * PCR[4] and PCR[6] have the PE/COFF image measurement,
  260. * this PCRs have different value in each architecture.
  261. */
  262. #if defined(__arm__)
  263. static u8 expected_pcrs_per_arch[][TPM2_SHA256_DIGEST_SIZE] = {
  264. /* PCR[4] */
  265. {0xcd, 0xa2, 0x06, 0xad, 0x83, 0x9b, 0x8f, 0x92,
  266. 0x01, 0xf9, 0xc8, 0x3d, 0xc9, 0x54, 0x66, 0xb3,
  267. 0x97, 0x35, 0x88, 0xe1, 0xea, 0xd3, 0x1a, 0xd6,
  268. 0x56, 0xee, 0x43, 0x1c, 0xdb, 0x4b, 0xf9, 0x1f},
  269. /* PCR[6] */
  270. {0x9c, 0xb8, 0x9d, 0x4a, 0xf6, 0x63, 0x95, 0xb0,
  271. 0x95, 0xfe, 0x44, 0x30, 0x0f, 0x3a, 0x0b, 0x7c,
  272. 0xef, 0xc7, 0xb6, 0x6a, 0x59, 0xae, 0xcb, 0xf6,
  273. 0xbd, 0x2d, 0xb5, 0xb9, 0xb4, 0x95, 0x7d, 0xaf}
  274. };
  275. #elif defined(__aarch64__)
  276. static u8 expected_pcrs_per_arch[][TPM2_SHA256_DIGEST_SIZE] = {
  277. /* PCR[4] */
  278. {0x69, 0xdb, 0x01, 0x5e, 0x07, 0xed, 0x9c, 0xbb,
  279. 0x27, 0x65, 0xb1, 0xf0, 0x7b, 0x04, 0xbc, 0x31,
  280. 0xd1, 0xec, 0x00, 0xe4, 0xe1, 0x49, 0xdb, 0x1e,
  281. 0x8b, 0x2d, 0xa2, 0x26, 0xb5, 0x8d, 0x07, 0xe2},
  282. /* PCR[6] */
  283. {0x53, 0x1b, 0x27, 0xb2, 0x6f, 0x2d, 0xab, 0x9b,
  284. 0x6f, 0xbc, 0xd1, 0x8f, 0xc9, 0x14, 0x48, 0xe7,
  285. 0x6d, 0x1b, 0xfb, 0x1b, 0x53, 0xc5, 0x8e, 0xf4,
  286. 0x41, 0x50, 0x79, 0x24, 0x66, 0x57, 0x7b, 0xf8}
  287. };
  288. #elif defined(__i386__)
  289. static u8 expected_pcrs_per_arch[][TPM2_SHA256_DIGEST_SIZE] = {
  290. /* PCR[4] */
  291. {0xec, 0x5e, 0xdb, 0x68, 0x13, 0x48, 0x36, 0x0a,
  292. 0x3a, 0xbc, 0x7b, 0x7b, 0xbc, 0x74, 0x7a, 0xa5,
  293. 0x55, 0xea, 0xb9, 0x09, 0x6a, 0x6e, 0xc3, 0x21,
  294. 0x51, 0x46, 0x22, 0xd2, 0x9d, 0xc9, 0xd5, 0x6a},
  295. /* PCR[6] */
  296. {0x26, 0x14, 0xe7, 0xde, 0x91, 0xd1, 0xf3, 0xde,
  297. 0x7a, 0xc2, 0x78, 0xaf, 0x4b, 0x2e, 0x05, 0x9d,
  298. 0x35, 0x17, 0xee, 0xcc, 0x0e, 0x77, 0x8d, 0x3f,
  299. 0x7e, 0x20, 0x75, 0xfa, 0xbc, 0xbc, 0x24, 0x3e}
  300. };
  301. #elif defined(__x86_64__)
  302. static u8 expected_pcrs_per_arch[][TPM2_SHA256_DIGEST_SIZE] = {
  303. /* PCR[4] */
  304. {0x9a, 0x75, 0x99, 0x8b, 0x74, 0x45, 0xb6, 0x26,
  305. 0x50, 0xe0, 0xbb, 0xfa, 0x2a, 0xa6, 0x19, 0xec,
  306. 0x97, 0x12, 0x0c, 0xb5, 0xc8, 0x2a, 0xfe, 0xe5,
  307. 0x29, 0xc8, 0xd3, 0x98, 0xe9, 0xd1, 0x9d, 0xd5},
  308. /* PCR[6] */
  309. {0xa2, 0xa2, 0xd3, 0xa7, 0x84, 0xc2, 0x95, 0x2a,
  310. 0xab, 0x6f, 0xe7, 0xe8, 0x86, 0x9f, 0x99, 0xc6,
  311. 0x6a, 0x8c, 0xcc, 0x5c, 0xb8, 0x83, 0xfa, 0x86,
  312. 0x56, 0x5e, 0x91, 0x17, 0x0b, 0x5f, 0x54, 0xa8}
  313. };
  314. #elif defined(__riscv) && (__riscv_xlen == 32)
  315. static u8 expected_pcrs_per_arch[][TPM2_SHA256_DIGEST_SIZE] = {
  316. /* PCR[4] */
  317. {0x64, 0xe9, 0x25, 0xb3, 0xd8, 0x33, 0xb3, 0x1b,
  318. 0x74, 0x0c, 0x81, 0x45, 0xef, 0x61, 0xf1, 0x87,
  319. 0xef, 0x65, 0x67, 0x28, 0x1a, 0x54, 0x97, 0xb2,
  320. 0xd3, 0x62, 0x00, 0xe7, 0xb6, 0x7a, 0xd5, 0x8e},
  321. /* PCR[6] */
  322. {0x82, 0xab, 0xc5, 0x6a, 0xbf, 0x08, 0x43, 0x3f,
  323. 0x85, 0xbd, 0x8f, 0x8e, 0x23, 0x62, 0x48, 0x4a,
  324. 0x44, 0x53, 0xf0, 0xae, 0x8d, 0x4c, 0xda, 0x04,
  325. 0x89, 0x9c, 0x0b, 0x81, 0x3a, 0x53, 0xf3, 0xac}
  326. };
  327. #elif defined(__riscv) && (__riscv_xlen == 64)
  328. static u8 expected_pcrs_per_arch[][TPM2_SHA256_DIGEST_SIZE] = {
  329. /* PCR[4] */
  330. {0x9b, 0x5f, 0x10, 0x24, 0x28, 0x5d, 0x7d, 0x1f,
  331. 0x9f, 0xee, 0xe9, 0x90, 0xf1, 0x7a, 0x03, 0xb1,
  332. 0x68, 0x7b, 0x28, 0x45, 0x98, 0x5e, 0xf5, 0x5e,
  333. 0xc1, 0x22, 0x61, 0x8c, 0x2f, 0xb5, 0xbf, 0x80},
  334. /* PCR[6] */
  335. {0x6d, 0x16, 0x17, 0xf4, 0x9a, 0xa8, 0x49, 0xc2,
  336. 0xf4, 0x9c, 0x35, 0x30, 0x0c, 0xde, 0x65, 0xdb,
  337. 0xd3, 0x37, 0x9c, 0xe2, 0x9f, 0x14, 0x81, 0x74,
  338. 0xc3, 0x94, 0x8a, 0x9e, 0x26, 0xbf, 0xfb, 0xb2}
  339. };
  340. #endif
  341. struct boot_variable {
  342. u16 name[16];
  343. u8 *buf;
  344. efi_uintn_t size;
  345. u32 attr;
  346. const u8 *test_data;
  347. efi_uintn_t test_data_size;
  348. };
  349. static struct boot_variable boot_variable_test[] = {
  350. {u"BootOrder", NULL, 0, DEFAULT_ATTR, boot_order, sizeof(boot_order)},
  351. {BOOT_NAME_1000, NULL, 0, DEFAULT_ATTR, boot_1000, sizeof(boot_1000)},
  352. {BOOT_NAME_1001, NULL, 0, DEFAULT_ATTR, boot_1001, sizeof(boot_1001)},
  353. {BOOT_NAME_1002, NULL, 0, DEFAULT_ATTR, boot_1002, sizeof(boot_1002)},
  354. };
  355. /*
  356. * efi_status_t decompress() - Decompress the disk image.
  357. *
  358. * @image decompressed disk image
  359. * Return: status code
  360. */
  361. static efi_status_t decompress(u8 **image)
  362. {
  363. u8 *buf;
  364. size_t i;
  365. size_t addr;
  366. size_t len;
  367. efi_status_t ret;
  368. ret = boottime->allocate_pool(EFI_LOADER_DATA, img.length,
  369. (void **)&buf);
  370. if (ret != EFI_SUCCESS) {
  371. efi_st_error("Out of memory\n");
  372. return ret;
  373. }
  374. boottime->set_mem(buf, img.length, 0);
  375. for (i = 0; ; ++i) {
  376. if (!img.lines[i].line)
  377. break;
  378. addr = img.lines[i].addr;
  379. len = COMPRESSED_DISK_IMAGE_BLOCK_SIZE;
  380. if (addr + len > img.length)
  381. len = img.length - addr;
  382. boottime->copy_mem(buf + addr, img.lines[i].line, len);
  383. }
  384. *image = buf;
  385. return ret;
  386. }
  387. /*
  388. * efi_status_t setup_boot_variable() - configure dummy boot variables
  389. *
  390. * Preexisting variable values are saved and will be restored by
  391. * calling restore_boot_variable().
  392. *
  393. * Return: status code
  394. */
  395. static efi_status_t setup_boot_variable(void)
  396. {
  397. efi_status_t ret;
  398. u32 i;
  399. efi_uintn_t size;
  400. for (i = 0; i < ARRAY_SIZE(boot_variable_test); i++) {
  401. size = 0;
  402. ret = runtime->get_variable(boot_variable_test[i].name,
  403. &efi_global_variable_guid,
  404. &boot_variable_test[i].attr,
  405. &size,
  406. NULL);
  407. if (ret == EFI_BUFFER_TOO_SMALL) {
  408. /* Variable exists, save the current value */
  409. boot_variable_test[i].size = size;
  410. ret = boottime->allocate_pool(EFI_LOADER_DATA,
  411. boot_variable_test[i].size,
  412. (void **)&boot_variable_test[i].buf);
  413. if (ret != EFI_SUCCESS) {
  414. efi_st_error("Failed to allocate buffer for boot variable\n");
  415. return ret;
  416. }
  417. ret = runtime->get_variable(boot_variable_test[i].name,
  418. &efi_global_variable_guid,
  419. &boot_variable_test[i].attr,
  420. &boot_variable_test[i].size,
  421. boot_variable_test[i].buf);
  422. if (ret != EFI_SUCCESS) {
  423. efi_st_error("Failed to get current boot variable\n");
  424. return ret;
  425. }
  426. }
  427. /* set boot variable for the measurement test */
  428. ret = runtime->set_variable(boot_variable_test[i].name,
  429. &efi_global_variable_guid,
  430. boot_variable_test[i].attr,
  431. boot_variable_test[i].test_data_size,
  432. boot_variable_test[i].test_data);
  433. if (ret != EFI_SUCCESS) {
  434. efi_st_error("Failed to set test boot variable(%d)n", i);
  435. return ret;
  436. }
  437. }
  438. return 0;
  439. }
  440. /*
  441. * efi_status_t restore_boot_variable() - restore original values
  442. *
  443. * Restore the variable values saved in setup_boot_variable().
  444. *
  445. * Return: status code
  446. */
  447. static efi_status_t restore_boot_variable(void)
  448. {
  449. int i;
  450. efi_status_t ret;
  451. for (i = 0; i < ARRAY_SIZE(boot_variable_test); i++) {
  452. if (boot_variable_test[i].buf) {
  453. ret = runtime->set_variable(boot_variable_test[i].name,
  454. &efi_global_variable_guid,
  455. boot_variable_test[i].attr,
  456. boot_variable_test[i].size,
  457. boot_variable_test[i].buf);
  458. if (ret != EFI_SUCCESS) {
  459. efi_st_error("Failed to restore boot variable\n");
  460. return ret;
  461. }
  462. ret = boottime->free_pool(boot_variable_test[i].buf);
  463. if (ret != EFI_SUCCESS) {
  464. efi_st_error("Failed to free boot variable\n");
  465. return ret;
  466. }
  467. } else {
  468. /* delete the variable used only for testing */
  469. ret = runtime->set_variable(boot_variable_test[i].name,
  470. &efi_global_variable_guid,
  471. 0, 0, NULL);
  472. if (ret != EFI_SUCCESS) {
  473. efi_st_error("Failed to delete boot variable\n");
  474. return ret;
  475. }
  476. }
  477. }
  478. return EFI_SUCCESS;
  479. }
  480. /**
  481. * void *find_smbios_table() - Find smbios table
  482. *
  483. * @systable system table
  484. * Return: status code
  485. */
  486. static void *find_smbios_table(const struct efi_system_table *systable)
  487. {
  488. u32 i;
  489. for (i = 0; i < systable->nr_tables; i++) {
  490. if (!guidcmp(&smbios_guid, &systable->tables[i].guid))
  491. return systable->tables[i].table;
  492. }
  493. return NULL;
  494. }
  495. /**
  496. * efi_status_t setup_smbios_table() - Prepare the dummy SMBIOS table
  497. *
  498. * @systable system table
  499. * Return: status code
  500. */
  501. static efi_status_t setup_smbios_table(const struct efi_system_table *systable)
  502. {
  503. struct smbios_entry *se;
  504. efi_status_t ret;
  505. /* Map within the low 32 bits, to allow for 32bit SMBIOS tables */
  506. void *dmi;
  507. char *istart;
  508. int isize;
  509. if (sizeof(smbios_table_test) > EFI_PAGE_SIZE)
  510. return EFI_OUT_OF_RESOURCES;
  511. orig_smbios_table = find_smbios_table(systable);
  512. /* Reserve 4kiB page for SMBIOS */
  513. ret = boottime->allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
  514. EFI_RUNTIME_SERVICES_DATA, 1, &dmi_addr);
  515. if (ret != EFI_SUCCESS) {
  516. /* Could not find space in lowmem, use highmem instead */
  517. ret = boottime->allocate_pages(EFI_ALLOCATE_ANY_PAGES,
  518. EFI_RUNTIME_SERVICES_DATA, 1,
  519. &dmi_addr);
  520. if (ret != EFI_SUCCESS)
  521. return ret;
  522. }
  523. dmi = (void *)(uintptr_t)dmi_addr;
  524. se = dmi;
  525. boottime->copy_mem(se, smbios_table_test, sizeof(smbios_table_test));
  526. /* update smbios table start address */
  527. se->struct_table_address = (uintptr_t)((u8 *)dmi + SMBIOS_ENTRY_HEADER_SIZE);
  528. /* calculate checksums */
  529. istart = (char *)se + SMBIOS_INTERMEDIATE_OFFSET;
  530. isize = sizeof(struct smbios_entry) - SMBIOS_INTERMEDIATE_OFFSET;
  531. se->intermediate_checksum = table_compute_checksum(istart, isize);
  532. se->checksum = table_compute_checksum(se, sizeof(struct smbios_entry));
  533. /* Install SMBIOS information as configuration table */
  534. ret = boottime->install_configuration_table(&smbios_guid, dmi);
  535. if (ret != EFI_SUCCESS) {
  536. efi_st_error("Cannot install SMBIOS table\n");
  537. boottime->free_pages(dmi_addr, 1);
  538. }
  539. return ret;
  540. }
  541. /**
  542. * efi_st_tcg2_setup() - setup test
  543. *
  544. * @handle: handle of the loaded image
  545. * @systable: system table
  546. * Return: status code
  547. */
  548. static int efi_st_tcg2_setup(const efi_handle_t img_handle,
  549. const struct efi_system_table *systable)
  550. {
  551. efi_status_t ret;
  552. struct uefi_image_load_event image_load_event;
  553. image_handle = img_handle;
  554. boottime = systable->boottime;
  555. runtime = systable->runtime;
  556. /* Load the application image into memory */
  557. decompress(&image);
  558. ret = boottime->allocate_pool(EFI_LOADER_DATA,
  559. sizeof(struct efi_tcg2_event) +
  560. sizeof(struct uefi_image_load_event),
  561. (void **)&efi_tcg2_event);
  562. if (ret != EFI_SUCCESS) {
  563. efi_st_error("Out of memory\n");
  564. return EFI_ST_FAILURE;
  565. }
  566. efi_tcg2_event->size = sizeof(struct efi_tcg2_event) +
  567. sizeof(struct uefi_image_load_event);
  568. efi_tcg2_event->header.header_size = sizeof(struct efi_tcg2_event_header);
  569. efi_tcg2_event->header.header_version = 1;
  570. efi_tcg2_event->header.pcr_index = 6;
  571. efi_tcg2_event->header.event_type = EV_EFI_RUNTIME_SERVICES_DRIVER;
  572. image_load_event.image_location_in_memory = 0x12345678;
  573. image_load_event.image_length_in_memory = 0x300000;
  574. image_load_event.image_link_time_address = 0x87654321;
  575. image_load_event.length_of_device_path = 0;
  576. boottime->copy_mem(efi_tcg2_event->event, &image_load_event,
  577. sizeof(struct uefi_image_load_event));
  578. ret = setup_boot_variable();
  579. if (ret != EFI_SUCCESS)
  580. return EFI_ST_FAILURE;
  581. ret = setup_smbios_table(systable);
  582. if (ret != EFI_SUCCESS)
  583. return EFI_ST_FAILURE;
  584. ret = boottime->allocate_pool(EFI_LOADER_DATA,
  585. (EFI_TCG2_MAX_PCR_INDEX + 1) *
  586. TPM2_SHA256_DIGEST_SIZE,
  587. (void **)&pcrs);
  588. if (ret != EFI_SUCCESS) {
  589. efi_st_error("Out of memory\n");
  590. return EFI_ST_FAILURE;
  591. }
  592. boottime->set_mem(pcrs, (EFI_TCG2_MAX_PCR_INDEX + 1) * TPM2_SHA256_DIGEST_SIZE, 0);
  593. /* setup expected PCRs per architecture */
  594. boottime->copy_mem(&expected_pcrs[4], &expected_pcrs_per_arch[0], TPM2_SHA256_DIGEST_SIZE);
  595. boottime->copy_mem(&expected_pcrs[6], &expected_pcrs_per_arch[1], TPM2_SHA256_DIGEST_SIZE);
  596. return EFI_ST_SUCCESS;
  597. }
  598. /**
  599. * efi_status_t get_manufacturer_id() - Get manufacturer_id through submit_command API
  600. *
  601. * @tcg2 tcg2 protocol
  602. * @manufacturer_id pointer to the manufacturer_id
  603. * Return: status code
  604. */
  605. static efi_status_t get_manufacturer_id(struct efi_tcg2_protocol *tcg2, u32 *manufacturer_id)
  606. {
  607. efi_status_t ret;
  608. u8 cmd[TPM2_CMD_BUF_SIZE] = {
  609. tpm_u16(TPM2_ST_NO_SESSIONS), /* TAG */
  610. tpm_u32(22), /* Length */
  611. tpm_u32(TPM2_CC_GET_CAPABILITY), /* Command code */
  612. tpm_u32(TPM2_CAP_TPM_PROPERTIES), /* Capability */
  613. tpm_u32(TPM2_PT_MANUFACTURER), /* Property */
  614. tpm_u32(1), /* Property count */
  615. };
  616. u8 resp[TPM2_CMD_BUF_SIZE];
  617. unsigned int value_off;
  618. ret = tcg2->submit_command(tcg2, 22, cmd,
  619. TPM2_CMD_BUF_SIZE, resp);
  620. if (ret != EFI_SUCCESS)
  621. return ret;
  622. /*
  623. * In the response buffer, the properties are located after the:
  624. * tag (u16), response size (u32), response code (u32),
  625. * YES/NO flag (u8), TPM_CAP (u32).
  626. * The value is located after count (u32), property (u32).
  627. */
  628. value_off = sizeof(u16) + sizeof(u32) + sizeof(u32) +
  629. sizeof(u8) + sizeof(u32) + sizeof(u32) + sizeof(u32);
  630. *manufacturer_id = get_unaligned_be32(&resp[value_off]);
  631. return ret;
  632. }
  633. /**
  634. * efi_status_t get_manufacturer_id_buffer_small() - call submit_command with small resp buffer
  635. *
  636. * @tcg2 tcg2 protocol
  637. * @manufacturer_id pointer to the manufacturer_id
  638. * Return: status code
  639. */
  640. static efi_status_t get_manufacturer_id_buffer_small(struct efi_tcg2_protocol *tcg2)
  641. {
  642. efi_status_t ret;
  643. u8 cmd[TPM2_CMD_BUF_SIZE] = {
  644. tpm_u16(TPM2_ST_NO_SESSIONS), /* TAG */
  645. tpm_u32(22), /* Length */
  646. tpm_u32(TPM2_CC_GET_CAPABILITY), /* Command code */
  647. tpm_u32(TPM2_CAP_TPM_PROPERTIES), /* Capability */
  648. tpm_u32(TPM2_PT_MANUFACTURER), /* Property */
  649. tpm_u32(1), /* Property count */
  650. };
  651. u8 resp[1]; /* set smaller buffer than expected */
  652. ret = tcg2->submit_command(tcg2, 22, cmd, 1, resp);
  653. return ret;
  654. }
  655. /**
  656. * efi_status_t read_pcr() - Read the PCR from the TPM device
  657. *
  658. * @tcg2 tcg2 protocol
  659. * @idx pcr index to read
  660. * Return: status code
  661. */
  662. static efi_status_t read_pcr(struct efi_tcg2_protocol *tcg2, u32 idx)
  663. {
  664. efi_status_t ret;
  665. u32 cmd_len = 17 + IDX_ARRAY_SZ;
  666. u8 cmd[TPM2_CMD_BUF_SIZE] = {
  667. tpm_u16(TPM2_ST_NO_SESSIONS), /* TAG */
  668. tpm_u32(cmd_len), /* Length */
  669. tpm_u32(TPM2_CC_PCR_READ), /* Command code */
  670. /* TPML_PCR_SELECTION */
  671. tpm_u32(1), /* Number of selections */
  672. tpm_u16(TPM2_ALG_SHA256), /* Algorithm of the hash */
  673. IDX_ARRAY_SZ, /* Array size for selection */
  674. /* bitmap(idx), Selected PCR bitmap */
  675. };
  676. u8 resp[TPM2_CMD_BUF_SIZE];
  677. u32 pcr_sel_idx = idx / 8;
  678. u8 pcr_sel_bit = BIT(idx % 8);
  679. cmd[17 + pcr_sel_idx] = pcr_sel_bit;
  680. ret = tcg2->submit_command(tcg2, cmd_len, cmd,
  681. TPM2_CMD_BUF_SIZE, resp);
  682. if (ret != EFI_SUCCESS) {
  683. efi_st_error("tcg2->submit_command fail to read PCR\n");
  684. return ret;
  685. }
  686. boottime->copy_mem(pcrs[idx], &resp[TPM2_PCR_READ_HEADER_SIZE],
  687. TPM2_SHA256_DIGEST_SIZE);
  688. return ret;
  689. }
  690. /**
  691. * int validate_pcrs() - Compare the expected and actual pcrs
  692. *
  693. * Return: status code
  694. */
  695. static int validate_pcrs(void)
  696. {
  697. u32 i;
  698. /*
  699. * - Skip PCR[0] validation. PCR[0] contains U-Boot version measurement
  700. * it contains the commit hash, so the measurement varies every build
  701. * with different commit hash.
  702. * - Skip PCR[7] validation. PCR[7] contains UEFI Secure Boot variables
  703. * measurement. These variables can not be updated through efi_selftest and
  704. * vary depending on the platform.
  705. * - Skip PCR[17..22] validation, they are not used in TCG PC Client
  706. * Platform Firmware Profile Specification
  707. */
  708. for (i = 1; i < (EFI_TCG2_MAX_PCR_INDEX + 1); i++) {
  709. if (i == 7 || (i > 16 && i < 23))
  710. continue; /* skip validation */
  711. if (memcmp(pcrs[i], expected_pcrs[i], TPM2_SHA256_DIGEST_SIZE)) {
  712. efi_st_error("PCR[%d] is not the expected value\n", i);
  713. return EFI_ST_FAILURE;
  714. }
  715. }
  716. return EFI_ST_SUCCESS;
  717. }
  718. /**
  719. * efi_st_tcg2_execute() - execute test
  720. *
  721. * Call EFI_TCG2_PROTOCOL services and check the
  722. * Measured Boot behavior.
  723. *
  724. * Return: status code
  725. */
  726. static int efi_st_tcg2_execute(void)
  727. {
  728. struct efi_tcg2_protocol *tcg2;
  729. struct efi_tcg2_boot_service_capability capability;
  730. efi_status_t ret;
  731. u32 active_pcr_banks;
  732. u64 eventlog, eventlog_last_entry;
  733. bool eventlog_truncated;
  734. efi_handle_t handle;
  735. efi_uintn_t exit_data_size = 0;
  736. u16 *exit_data = NULL;
  737. u32 i;
  738. u32 manufacturer_id;
  739. ret = boottime->locate_protocol(&guid_tcg2, NULL, (void **)&tcg2);
  740. if (ret != EFI_SUCCESS) {
  741. efi_st_error("TCG2 protocol is not available.\n");
  742. return EFI_ST_FAILURE;
  743. }
  744. /* EFI_TCG2_PROTOCOL.GetCapability test */
  745. capability.size = sizeof(struct efi_tcg2_boot_service_capability) - 1;
  746. ret = tcg2->get_capability(tcg2, &capability);
  747. if (ret != EFI_BUFFER_TOO_SMALL) {
  748. efi_st_error("tcg2->get_capability on small buffer failed\n");
  749. return EFI_ST_FAILURE;
  750. }
  751. capability.size = sizeof(struct efi_tcg2_boot_service_capability);
  752. ret = tcg2->get_capability(tcg2, &capability);
  753. if (ret != EFI_SUCCESS) {
  754. efi_st_error("tcg2->get_capability failed\n");
  755. return EFI_ST_FAILURE;
  756. }
  757. if (!capability.tpm_present_flag) {
  758. efi_st_error("TPM not present\n");
  759. return EFI_ST_FAILURE;
  760. }
  761. efi_st_printf("TPM supports 0x%.8x event logs\n",
  762. capability.supported_event_logs);
  763. /* EFI_TCG2_PROTOCOL.GetActivePcrBanks test */
  764. ret = tcg2->get_active_pcr_banks(tcg2, &active_pcr_banks);
  765. if (ret != EFI_SUCCESS) {
  766. efi_st_error("tcg2->get_active_pcr_banks failed\n");
  767. return EFI_ST_FAILURE;
  768. }
  769. if (active_pcr_banks != capability.active_pcr_banks) {
  770. efi_st_error("tcg2->get_active_pcr_banks return wrong value\n");
  771. return EFI_ST_FAILURE;
  772. }
  773. /* EFI_TCG2_PROTOCOL.HashLogExtendEvent test */
  774. ret = tcg2->hash_log_extend_event(tcg2, EFI_TCG2_EXTEND_ONLY,
  775. (uintptr_t)image,
  776. img.length, efi_tcg2_event);
  777. if (ret != EFI_SUCCESS) {
  778. efi_st_error("tcg2->hash_log_extend_event(EXTEND_ONLY) failed\n");
  779. return EFI_ST_FAILURE;
  780. }
  781. ret = tcg2->hash_log_extend_event(tcg2, PE_COFF_IMAGE, (uintptr_t)image,
  782. img.length, efi_tcg2_event);
  783. if (ret != EFI_SUCCESS) {
  784. efi_st_error("tcg2->hash_log_extend_event(PE_COFF_IMAGE) failed\n");
  785. return EFI_ST_FAILURE;
  786. }
  787. /* EFI_TCG2_PROTOCOL.SubmitCommand test */
  788. ret = get_manufacturer_id_buffer_small(tcg2);
  789. if (ret != EFI_OUT_OF_RESOURCES) {
  790. efi_st_error("get_manufacturer_id buffer too small failed\n");
  791. return EFI_ST_FAILURE;
  792. }
  793. ret = get_manufacturer_id(tcg2, &manufacturer_id);
  794. if (ret != EFI_SUCCESS) {
  795. efi_st_error("get_manufacturer_id failed\n");
  796. return EFI_ST_FAILURE;
  797. }
  798. if (capability.manufacturer_id != manufacturer_id) {
  799. efi_st_error("tcg2->submit_command test failed\n");
  800. return EFI_ST_FAILURE;
  801. }
  802. /* tcg2_measure_pe_image test */
  803. ret = boottime->load_image(false, image_handle, NULL, image,
  804. img.length, &handle);
  805. if (ret != EFI_SUCCESS) {
  806. efi_st_error("Failed to load image\n");
  807. return EFI_ST_FAILURE;
  808. }
  809. /* measure ready_to_boot event(boot variables, smbios table, etc.) */
  810. /* TODO: add GPT measurement test */
  811. ret = boottime->start_image(handle, &exit_data_size, &exit_data);
  812. if (ret != EFI_UNSUPPORTED) {
  813. efi_st_error("Wrong return value from application\n");
  814. return EFI_ST_FAILURE;
  815. }
  816. ret = boottime->free_pool(exit_data);
  817. if (ret != EFI_SUCCESS) {
  818. efi_st_error("Failed to free exit data\n");
  819. return EFI_ST_FAILURE;
  820. }
  821. /* validate PCR read from the TPM device */
  822. for (i = 0; i < (EFI_TCG2_MAX_PCR_INDEX + 1); i++) {
  823. ret = read_pcr(tcg2, i);
  824. if (ret != EFI_SUCCESS) {
  825. efi_st_error("read pcr error\n");
  826. return EFI_ST_FAILURE;
  827. }
  828. }
  829. if (validate_pcrs()) {
  830. efi_st_error("PCR validation failed\n");
  831. return EFI_ST_FAILURE;
  832. }
  833. /* EFI_TCG2_PROTOCOL.GetEventLog test */
  834. ret = tcg2->get_eventlog(tcg2, TCG2_EVENT_LOG_FORMAT_TCG_2, &eventlog,
  835. &eventlog_last_entry, &eventlog_truncated);
  836. if (ret != EFI_SUCCESS) {
  837. efi_st_error("tcg2->get_eventlog failed\n");
  838. return EFI_ST_FAILURE;
  839. }
  840. /* TODO: eventlog format check */
  841. return EFI_ST_SUCCESS;
  842. }
  843. /*
  844. * efi_st_tcg2_teardown() - Tear down unit test
  845. *
  846. * Return: EFI_ST_SUCCESS for success
  847. */
  848. static int efi_st_tcg2_teardown(void)
  849. {
  850. efi_status_t r = EFI_ST_SUCCESS;
  851. if (image) {
  852. r = boottime->free_pool(image);
  853. if (r != EFI_SUCCESS) {
  854. efi_st_error("Failed to free image\n");
  855. return EFI_ST_FAILURE;
  856. }
  857. }
  858. if (efi_tcg2_event) {
  859. r = boottime->free_pool(efi_tcg2_event);
  860. if (r != EFI_SUCCESS) {
  861. efi_st_error("Failed to free efi_tcg2_event\n");
  862. return EFI_ST_FAILURE;
  863. }
  864. }
  865. if (pcrs) {
  866. r = boottime->free_pool(pcrs);
  867. if (r != EFI_SUCCESS) {
  868. efi_st_error("Failed to free pcr\n");
  869. return EFI_ST_FAILURE;
  870. }
  871. }
  872. r = restore_boot_variable();
  873. if (r != EFI_SUCCESS) {
  874. efi_st_error("Failed to restore boot variables\n");
  875. return EFI_ST_FAILURE;
  876. }
  877. /*
  878. * Restore SMBIOS table
  879. * If orig_smbios_table is NULL, calling install_configuration_table()
  880. * removes dummy SMBIOS table form systab.
  881. */
  882. r = boottime->install_configuration_table(&smbios_guid, orig_smbios_table);
  883. if (r != EFI_SUCCESS) {
  884. efi_st_error("Failed to restore SMBOIS table\n");
  885. return EFI_ST_FAILURE;
  886. }
  887. if (dmi_addr) {
  888. r = boottime->free_pages(dmi_addr, 1);
  889. if (r != EFI_SUCCESS) {
  890. efi_st_error("Failed to free dummy smbios table\n");
  891. return EFI_ST_FAILURE;
  892. }
  893. }
  894. return r;
  895. }
  896. EFI_UNIT_TEST(tcg2) = {
  897. .name = "tcg2",
  898. .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
  899. .execute = efi_st_tcg2_execute,
  900. .setup = efi_st_tcg2_setup,
  901. .teardown = efi_st_tcg2_teardown,
  902. .on_request = true,
  903. };