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