xref: /optee_os/core/drivers/crypto/se050/session.c (revision 9fc2442cc66c279cb962c90c4375746fc9b28bb9)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (C) Foundries Ltd. 2020 - All Rights Reserved
4  * Author: Jorge Ramirez <jorge@foundries.io>
5  */
6 
7 #include <config.h>
8 #include <initcall.h>
9 #include <se050.h>
10 
11 sss_se05x_key_store_t *se050_kstore;
12 sss_se05x_session_t *se050_session;
13 struct sss_se05x_ctx se050_ctx;
14 
15 TEE_Result se050_core_early_init(struct se050_scp_key *keys)
16 {
17 	sss_status_t status = kStatus_SSS_Success;
18 
19 	status = se050_session_open(&se050_ctx, keys);
20 	if (status != kStatus_SSS_Success)
21 		return TEE_ERROR_GENERIC;
22 
23 	if (IS_ENABLED(CFG_CORE_SE05X_INIT_NVM)) {
24 		status = se050_factory_reset(&se050_ctx.session.s_ctx);
25 		if (status != kStatus_SSS_Success)
26 			return TEE_ERROR_GENERIC;
27 	}
28 
29 	if (se050_ctx.session.subsystem == kType_SSS_SubSystem_NONE)
30 		return TEE_ERROR_GENERIC;
31 
32 	status = se050_key_store_and_object_init(&se050_ctx);
33 	if (status != kStatus_SSS_Success)
34 		return TEE_ERROR_GENERIC;
35 
36 	se050_session = (sss_se05x_session_t *)((void *)&se050_ctx.session);
37 	se050_kstore = (sss_se05x_key_store_t *)((void *)&se050_ctx.ks);
38 
39 	return TEE_SUCCESS;
40 }
41 
42 static TEE_Result display_info(void)
43 {
44 	se050_display_board_info(se050_session);
45 	/* the session must be closed after accessing board information */
46 	sss_se05x_session_close(se050_session);
47 	return se050_core_early_init(NULL);
48 }
49 
50 static TEE_Result enable_scp03(void)
51 {
52 	if (se050_enable_scp03(se050_session) != kStatus_SSS_Success)
53 		return TEE_ERROR_GENERIC;
54 
55 	/*
56 	 * Do not provision the keys at this point unless there is guaranteed
57 	 * access to trusted storage so the new keys can be written.
58 	 *
59 	 * This can be done once RPMB is accessible and we can test it
60 	 *
61 	 * #if defined(CFG_CORE_SE05X_SCP03_PROVISION)
62 	 *	if (se050_rotate_scp03_keys(&se050_ctx) != kStatus_SSS_Success)
63 	 *		return TEE_ERROR_GENERIC;
64 	 * #endif
65 	 */
66 
67 	return TEE_SUCCESS;
68 }
69 
70 static TEE_Result se050_early_init(void)
71 {
72 	TEE_Result ret = TEE_SUCCESS;
73 
74 	ret = se050_core_early_init(NULL);
75 
76 	if (!ret && IS_ENABLED(CFG_CORE_SE05X_DISPLAY_INFO))
77 		ret = display_info();
78 
79 	if (!ret && IS_ENABLED(CFG_CORE_SE05X_SCP03_EARLY))
80 		return enable_scp03();
81 
82 	return ret;
83 }
84 
85 driver_init(se050_early_init);
86