widevine_smc_handlers.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright (c) 2024, The ChromiumOS Authors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <stdint.h>
  8. #include <string.h>
  9. #include <common/debug.h>
  10. #include <common/runtime_svc.h>
  11. #include <lib/psci/psci.h>
  12. #include <lib/xlat_tables/xlat_tables_v2.h>
  13. #include <services/oem/chromeos/widevine_smc_handlers.h>
  14. #include <tools_share/uuid.h>
  15. #define CROS_OEM_TPM_AUTH_PK_MAX_LEN 128
  16. #define CROS_OEM_HUK_LEN 32
  17. #define CROS_OEM_ROT_LEN 32
  18. static uint8_t cros_oem_tpm_auth_pk_buffer[CROS_OEM_TPM_AUTH_PK_MAX_LEN];
  19. static uint8_t cros_oem_huk_buffer[CROS_OEM_HUK_LEN];
  20. static uint8_t cros_oem_rot_len_buffer[CROS_OEM_ROT_LEN];
  21. struct cros_oem_data cros_oem_tpm_auth_pk = {
  22. .buffer = cros_oem_tpm_auth_pk_buffer,
  23. .max_length = sizeof(cros_oem_tpm_auth_pk_buffer),
  24. };
  25. struct cros_oem_data cros_oem_huk = {
  26. .buffer = cros_oem_huk_buffer,
  27. .max_length = sizeof(cros_oem_huk_buffer),
  28. };
  29. struct cros_oem_data cros_oem_rot = {
  30. .buffer = cros_oem_rot_len_buffer,
  31. .max_length = sizeof(cros_oem_rot_len_buffer),
  32. };
  33. static uintptr_t cros_write_data(struct cros_oem_data *data,
  34. u_register_t length, u_register_t address,
  35. void *handle)
  36. {
  37. uintptr_t aligned_address;
  38. uintptr_t aligned_size;
  39. int32_t rc;
  40. if (data->length) {
  41. SMC_RET1(handle, PSCI_E_ALREADY_ON);
  42. }
  43. if (length > data->max_length) {
  44. SMC_RET1(handle, PSCI_E_INVALID_PARAMS);
  45. }
  46. aligned_address = page_align(address, DOWN);
  47. aligned_size = page_align(length + (address - aligned_address), UP);
  48. /*
  49. * We do not validate the passed in address because we are trusting the
  50. * non-secure world at this point still.
  51. */
  52. rc = mmap_add_dynamic_region(aligned_address, aligned_address,
  53. aligned_size, MT_MEMORY | MT_RO | MT_NS);
  54. if (rc != 0) {
  55. SMC_RET1(handle, PSCI_E_INVALID_ADDRESS);
  56. }
  57. memcpy(data->buffer, (void *)address, length);
  58. data->length = length;
  59. mmap_remove_dynamic_region(aligned_address, aligned_size);
  60. SMC_RET1(handle, SMC_OK);
  61. }
  62. /* Handler for servicing specific SMC calls. */
  63. static uintptr_t cros_oem_svc_smc_handler(uint32_t smc_fid, u_register_t x1,
  64. u_register_t x2, u_register_t x3,
  65. u_register_t x4, void *cookie,
  66. void *handle, u_register_t flags)
  67. {
  68. switch (smc_fid) {
  69. case CROS_OEM_SMC_DRM_SET_TPM_AUTH_PUB_FUNC_ID:
  70. return cros_write_data(&cros_oem_tpm_auth_pk, x1, x2, handle);
  71. case CROS_OEM_SMC_DRM_SET_HARDWARE_UNIQUE_KEY_FUNC_ID:
  72. return cros_write_data(&cros_oem_huk, x1, x2, handle);
  73. case CROS_OEM_SMC_DRM_SET_ROOT_OF_TRUST_FUNC_ID:
  74. return cros_write_data(&cros_oem_rot, x1, x2, handle);
  75. default:
  76. WARN("Unimplemented OEM Call: 0x%x\n", smc_fid);
  77. SMC_RET1(handle, SMC_UNK);
  78. }
  79. }
  80. /* Register OEM Service Calls as runtime service */
  81. DECLARE_RT_SVC(cros_oem_svc_smc_handler, OEN_OEM_START, OEN_OEM_END,
  82. SMC_TYPE_FAST, NULL, cros_oem_svc_smc_handler);