imx_hab.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright 2017-2020 NXP
  3. * Copyright 2022 Leica Geosystems AG
  4. *
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. */
  7. #include <common/runtime_svc.h>
  8. #include <imx_sip_svc.h>
  9. #define HAB_CID_ATF U(2) /* TF-A Caller ID */
  10. /* HAB Status definitions */
  11. enum hab_status {
  12. HAB_STS_ANY = 0x00, /* Match any status in report_event() */
  13. HAB_FAILURE = 0x33, /* Operation failed */
  14. HAB_WARNING = 0x69, /* Operation completed with warning */
  15. HAB_SUCCESS = 0xf0 /* Operation completed successfully */
  16. };
  17. /* HAB Configuration definitions */
  18. enum hab_config {
  19. HAB_CFG_RETURN = 0x33, /* Field Return IC */
  20. HAB_CFG_OPEN = 0xf0, /* Non-secure IC */
  21. HAB_CFG_CLOSED = 0xcc /* Secure IC */
  22. };
  23. /* HAB State definitions */
  24. enum hab_state {
  25. HAB_STATE_INITIAL = 0x33, /* Initializing state (transitory) */
  26. HAB_STATE_CHECK = 0x55, /* Check state (non-secure) */
  27. HAB_STATE_NONSECURE = 0x66, /* Non-secure state */
  28. HAB_STATE_TRUSTED = 0x99, /* Trusted state */
  29. HAB_STATE_SECURE = 0xaa, /* Secure state */
  30. HAB_STATE_FAIL_SOFT = 0xcc, /* Soft fail state */
  31. HAB_STATE_FAIL_HARD = 0xff, /* Hard fail state (terminal) */
  32. HAB_STATE_NONE = 0xf0 /* No security state machine */
  33. };
  34. /* HAB Verification Target definitions */
  35. enum hab_target {
  36. HAB_TGT_MEMORY = 0x0f, /* Check memory allowed list */
  37. HAB_TGT_PERIPHERAL = 0xf0, /* Check peripheral allowed list */
  38. HAB_TGT_ANY = 0x55 /* Check memory & peripheral allowed list */
  39. };
  40. /* Authenticate Image Loader Callback prototype */
  41. typedef enum hab_status hab_loader_callback_f_t(void **, size_t *, const void *);
  42. /*
  43. * HAB Rom VectorTable (RVT) structure.
  44. * This table provides function pointers into the HAB library in ROM for
  45. * use by post-ROM boot sequence components.
  46. * Functions are ordered in the structure below based on the offsets in ROM
  47. * image, and shall not be changed!
  48. * Details on API allocation offsets and function description could be
  49. * found in following documents from NXP:
  50. * - High Assurance Boot Version 4 Application Programming Interface
  51. * Reference Manual (available in CST package)
  52. * - HABv4 RVT Guidelines and Recommendations (AN12263)
  53. */
  54. struct hab_rvt_api {
  55. uint64_t hdr;
  56. enum hab_status (*entry)(void);
  57. enum hab_status (*exit)(void);
  58. enum hab_status (*check_target)(enum hab_target type, const void *start, size_t bytes);
  59. void* (*authenticate_image)(uint8_t cid, long ivt_offset, void **start,
  60. size_t *bytes, hab_loader_callback_f_t loader);
  61. enum hab_status (*run_dcd)(const uint8_t *dcd);
  62. enum hab_status (*run_csf)(const uint8_t *csf, uint8_t cid, uint32_t srkmask);
  63. enum hab_status (*assert)(long type, const void *data, uint32_t count);
  64. enum hab_status (*report_event)(enum hab_status status, uint32_t index,
  65. uint8_t *event, size_t *bytes);
  66. enum hab_status (*report_status)(enum hab_config *config, enum hab_state *state);
  67. void (*failsafe)(void);
  68. void* (*authenticate_image_no_dcd)(uint8_t cid, long ivt_offset, void **start,
  69. size_t *bytes, hab_loader_callback_f_t loader);
  70. uint32_t (*get_version)(void);
  71. enum hab_status (*authenticate_container)(uint8_t cid, long ivt_offset, void **start,
  72. size_t *bytes, hab_loader_callback_f_t loader, uint32_t srkmask, int skip_dcd);
  73. };
  74. struct hab_rvt_api *g_hab_rvt_api = (struct hab_rvt_api *)HAB_RVT_BASE;
  75. /*******************************************************************************
  76. * Handler for servicing HAB SMC calls
  77. ******************************************************************************/
  78. int imx_hab_handler(uint32_t smc_fid,
  79. u_register_t x1,
  80. u_register_t x2,
  81. u_register_t x3,
  82. u_register_t x4)
  83. {
  84. switch (x1) {
  85. case IMX_SIP_HAB_ENTRY:
  86. return g_hab_rvt_api->entry();
  87. case IMX_SIP_HAB_EXIT:
  88. return g_hab_rvt_api->exit();
  89. case IMX_SIP_HAB_CHECK_TARGET:
  90. return g_hab_rvt_api->check_target((enum hab_target)x2,
  91. (const void *)x3, (size_t)x4);
  92. case IMX_SIP_HAB_AUTH_IMG:
  93. return (unsigned long)g_hab_rvt_api->authenticate_image(HAB_CID_ATF,
  94. x2, (void **)x3, (size_t *)x4, NULL);
  95. case IMX_SIP_HAB_REPORT_EVENT:
  96. return g_hab_rvt_api->report_event(HAB_FAILURE,
  97. (uint32_t)x2, (uint8_t *)x3, (size_t *)x4);
  98. case IMX_SIP_HAB_REPORT_STATUS:
  99. return g_hab_rvt_api->report_status((enum hab_config *)x2,
  100. (enum hab_state *)x3);
  101. case IMX_SIP_HAB_FAILSAFE:
  102. g_hab_rvt_api->failsafe();
  103. break;
  104. case IMX_SIP_HAB_AUTH_IMG_NO_DCD:
  105. return (unsigned long)g_hab_rvt_api->authenticate_image_no_dcd(
  106. HAB_CID_ATF, x2, (void **)x3, (size_t *)x4, NULL);
  107. case IMX_SIP_HAB_GET_VERSION:
  108. return g_hab_rvt_api->get_version();
  109. default:
  110. return SMC_UNK;
  111. };
  112. return SMC_OK;
  113. }