efi_selftest_tcg2.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. static struct efi_boot_services *boottime;
  12. static const efi_guid_t guid_tcg2 = EFI_TCG2_PROTOCOL_GUID;
  13. /**
  14. * efi_st_tcg2_setup() - setup test
  15. *
  16. * @handle: handle of the loaded image
  17. * @systable: system table
  18. * @return: status code
  19. */
  20. static int efi_st_tcg2_setup(const efi_handle_t img_handle,
  21. const struct efi_system_table *systable)
  22. {
  23. boottime = systable->boottime;
  24. return EFI_ST_SUCCESS;
  25. }
  26. /**
  27. * efi_st_tcg2_execute() - execute test
  28. *
  29. * Call the GetCapability service of the EFI_TCG2_PROTOCOL.
  30. *
  31. * Return: status code
  32. */
  33. static int efi_st_tcg2_execute(void)
  34. {
  35. struct efi_tcg2_protocol *tcg2;
  36. struct efi_tcg2_boot_service_capability capability;
  37. efi_status_t ret;
  38. ret = boottime->locate_protocol(&guid_tcg2, NULL, (void **)&tcg2);
  39. if (ret != EFI_SUCCESS) {
  40. efi_st_error("TCG2 protocol is not available.\n");
  41. return EFI_ST_FAILURE;
  42. }
  43. capability.size = sizeof(struct efi_tcg2_boot_service_capability) - 1;
  44. ret = tcg2->get_capability(tcg2, &capability);
  45. if (ret != EFI_BUFFER_TOO_SMALL) {
  46. efi_st_error("tcg2->get_capability on small buffer failed\n");
  47. return EFI_ST_FAILURE;
  48. }
  49. capability.size = sizeof(struct efi_tcg2_boot_service_capability);
  50. ret = tcg2->get_capability(tcg2, &capability);
  51. if (ret != EFI_SUCCESS) {
  52. efi_st_error("tcg2->get_capability failed\n");
  53. return EFI_ST_FAILURE;
  54. }
  55. if (!capability.tpm_present_flag) {
  56. efi_st_error("TPM not present\n");
  57. return EFI_ST_FAILURE;
  58. }
  59. efi_st_printf("TPM supports 0x%.8x event logs\n",
  60. capability.supported_event_logs);
  61. return EFI_ST_SUCCESS;
  62. }
  63. EFI_UNIT_TEST(tcg2) = {
  64. .name = "tcg2",
  65. .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
  66. .execute = efi_st_tcg2_execute,
  67. .setup = efi_st_tcg2_setup,
  68. };