tcg2.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2022 Microsoft Corporation
  4. * Copyright (C) 2024 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/efi/api.h>
  20. #include <grub/efi/efi.h>
  21. #include <grub/efi/tpm.h>
  22. #include <grub/mm.h>
  23. #include <tcg2.h>
  24. static grub_err_t
  25. tcg2_get_caps (grub_efi_tpm2_protocol_t *protocol, int *tpm2, grub_size_t *max_output_size)
  26. {
  27. grub_efi_status_t status;
  28. static bool has_caps = 0;
  29. static EFI_TCG2_BOOT_SERVICE_CAPABILITY caps =
  30. {
  31. .Size = (grub_uint8_t) sizeof (caps)
  32. };
  33. if (has_caps)
  34. goto exit;
  35. status = protocol->get_capability (protocol, &caps);
  36. if (status != GRUB_EFI_SUCCESS || !caps.TPMPresentFlag)
  37. return GRUB_ERR_FILE_NOT_FOUND;
  38. has_caps = 1;
  39. exit:
  40. if (tpm2 != NULL)
  41. *tpm2 = caps.TPMPresentFlag;
  42. if (max_output_size != NULL)
  43. *max_output_size = caps.MaxResponseSize;
  44. return GRUB_ERR_NONE;
  45. }
  46. static grub_err_t
  47. tcg2_get_protocol (grub_efi_tpm2_protocol_t **protocol)
  48. {
  49. static grub_guid_t tpm2_guid = EFI_TPM2_GUID;
  50. static grub_efi_tpm2_protocol_t *tpm2_protocol = NULL;
  51. int tpm2;
  52. grub_efi_handle_t *handles;
  53. grub_efi_uintn_t num_handles;
  54. grub_efi_handle_t tpm2_handle;
  55. grub_err_t err = GRUB_ERR_FILE_NOT_FOUND;
  56. if (tpm2_protocol != NULL)
  57. {
  58. *protocol = tpm2_protocol;
  59. return GRUB_ERR_NONE;
  60. }
  61. handles = grub_efi_locate_handle (GRUB_EFI_BY_PROTOCOL, &tpm2_guid, NULL,
  62. &num_handles);
  63. if (handles == NULL || num_handles == 0)
  64. return err;
  65. tpm2_handle = handles[0];
  66. tpm2_protocol = grub_efi_open_protocol (tpm2_handle, &tpm2_guid,
  67. GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
  68. if (tpm2_protocol == NULL)
  69. goto exit;
  70. err = tcg2_get_caps (tpm2_protocol, &tpm2, NULL);
  71. if (err != GRUB_ERR_NONE || tpm2 == 0)
  72. goto exit;
  73. *protocol = tpm2_protocol;
  74. err = GRUB_ERR_NONE;
  75. exit:
  76. grub_free (handles);
  77. return err;
  78. }
  79. grub_err_t
  80. grub_tcg2_get_max_output_size (grub_size_t *size)
  81. {
  82. grub_err_t err;
  83. grub_size_t max;
  84. grub_efi_tpm2_protocol_t *protocol;
  85. if (size == NULL)
  86. return GRUB_ERR_BAD_ARGUMENT;
  87. err = tcg2_get_protocol (&protocol);
  88. if (err != GRUB_ERR_NONE)
  89. return err;
  90. err = tcg2_get_caps (protocol, NULL, &max);
  91. if (err != GRUB_ERR_NONE)
  92. return err;
  93. *size = max;
  94. return GRUB_ERR_NONE;
  95. }
  96. grub_err_t
  97. grub_tcg2_submit_command (grub_size_t input_size,
  98. grub_uint8_t *input,
  99. grub_size_t output_size,
  100. grub_uint8_t *output)
  101. {
  102. grub_err_t err;
  103. grub_efi_status_t status;
  104. grub_efi_tpm2_protocol_t *protocol;
  105. if (input_size == 0 || input == NULL ||
  106. output_size == 0 || output == NULL)
  107. return GRUB_ERR_BAD_ARGUMENT;
  108. err = tcg2_get_protocol (&protocol);
  109. if (err != GRUB_ERR_NONE)
  110. return err;
  111. status = protocol->submit_command (protocol, input_size, input,
  112. output_size, output);
  113. if (status != GRUB_EFI_SUCCESS)
  114. return GRUB_ERR_INVALID_COMMAND;
  115. return GRUB_ERR_NONE;
  116. }