sandbox_common.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Common features for sandbox TPM1 and TPM2 implementations
  4. *
  5. * Copyright 2021 Google LLC
  6. */
  7. #define LOG_CATEGORY UCLASS_TPM
  8. #include <common.h>
  9. #include <tpm-v1.h>
  10. #include <tpm-v2.h>
  11. #include <asm/unaligned.h>
  12. #include "sandbox_common.h"
  13. #define TPM_ERR_CODE_OFS (2 + 4) /* after tag and size */
  14. int sb_tpm_index_to_seq(u32 index)
  15. {
  16. index &= ~HR_NV_INDEX;
  17. switch (index) {
  18. case FIRMWARE_NV_INDEX:
  19. return NV_SEQ_FIRMWARE;
  20. case KERNEL_NV_INDEX:
  21. return NV_SEQ_KERNEL;
  22. case BACKUP_NV_INDEX:
  23. return NV_SEQ_BACKUP;
  24. case FWMP_NV_INDEX:
  25. return NV_SEQ_FWMP;
  26. case MRC_REC_HASH_NV_INDEX:
  27. return NV_SEQ_REC_HASH;
  28. case 0:
  29. return NV_SEQ_GLOBAL_LOCK;
  30. case TPM_NV_INDEX_LOCK:
  31. return NV_SEQ_ENABLE_LOCKING;
  32. }
  33. printf("Invalid nv index %#x\n", index);
  34. return -1;
  35. }
  36. void sb_tpm_read_data(const struct nvdata_state nvdata[NV_SEQ_COUNT],
  37. enum sandbox_nv_space seq, u8 *buf, int data_ofs,
  38. int length)
  39. {
  40. const struct nvdata_state *nvd = &nvdata[seq];
  41. if (!nvd->present)
  42. put_unaligned_be32(TPM_BADINDEX, buf + TPM_ERR_CODE_OFS);
  43. else if (length > nvd->length)
  44. put_unaligned_be32(TPM_BAD_DATASIZE, buf + TPM_ERR_CODE_OFS);
  45. else
  46. memcpy(buf + data_ofs, &nvd->data, length);
  47. }
  48. void sb_tpm_write_data(struct nvdata_state nvdata[NV_SEQ_COUNT],
  49. enum sandbox_nv_space seq, const u8 *buf, int data_ofs,
  50. int length)
  51. {
  52. struct nvdata_state *nvd = &nvdata[seq];
  53. if (length > nvd->length)
  54. log_err("Invalid length %x (max %x)\n", length, nvd->length);
  55. else
  56. memcpy(&nvdata[seq].data, buf + data_ofs, length);
  57. }
  58. void sb_tpm_define_data(struct nvdata_state nvdata[NV_SEQ_COUNT],
  59. enum sandbox_nv_space seq, int length)
  60. {
  61. struct nvdata_state *nvd = &nvdata[seq];
  62. if (length > NV_DATA_SIZE)
  63. log_err("Invalid length %x (max %x)\n", length, NV_DATA_SIZE);
  64. nvd->length = length;
  65. nvd->present = true;
  66. }