xref: /optee_os/core/drivers/crypto/se050/session.c (revision 1c025012fb55e89f8c2afc4358e58238da0db089)
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 <kernel/panic.h>
10 #include <se050.h>
11 
12 sss_se05x_key_store_t *se050_kstore;
13 sss_se05x_session_t *se050_session;
14 struct sss_se05x_ctx se050_ctx;
15 
16 TEE_Result se050_core_early_init(struct se050_scp_key *keys)
17 {
18 	sss_status_t status = kStatus_SSS_Success;
19 
20 	status = se050_session_open(&se050_ctx, keys);
21 	if (status != kStatus_SSS_Success)
22 		return TEE_ERROR_GENERIC;
23 
24 	if (IS_ENABLED(CFG_CORE_SE05X_INIT_NVM)) {
25 		status = se050_factory_reset(&se050_ctx.session.s_ctx);
26 		if (status != kStatus_SSS_Success)
27 			return TEE_ERROR_GENERIC;
28 	}
29 
30 	if (se050_ctx.session.subsystem == kType_SSS_SubSystem_NONE)
31 		return TEE_ERROR_GENERIC;
32 
33 	status = se050_key_store_and_object_init(&se050_ctx);
34 	if (status != kStatus_SSS_Success)
35 		return TEE_ERROR_GENERIC;
36 
37 	se050_session = (sss_se05x_session_t *)((void *)&se050_ctx.session);
38 	se050_kstore = (sss_se05x_key_store_t *)((void *)&se050_ctx.ks);
39 
40 	return TEE_SUCCESS;
41 }
42 
43 static TEE_Result update_se_info(void)
44 {
45 	sss_status_t status = kStatus_SSS_Success;
46 
47 	status = se050_get_se_info(se050_session,
48 				   IS_ENABLED(CFG_CORE_SE05X_DISPLAY_INFO));
49 
50 	/* the session must be closed after accessing the board information */
51 	sss_se05x_session_close(se050_session);
52 
53 	if (status != kStatus_SSS_Success)
54 		return TEE_ERROR_GENERIC;
55 
56 	return se050_core_early_init(NULL);
57 }
58 
59 static TEE_Result enable_scp03(void)
60 {
61 	if (se050_enable_scp03(se050_session) != kStatus_SSS_Success)
62 		return TEE_ERROR_GENERIC;
63 
64 	return TEE_SUCCESS;
65 }
66 
67 static TEE_Result se050_early_init(void)
68 {
69 	TEE_Result ret = TEE_SUCCESS;
70 
71 	ret = se050_core_early_init(NULL);
72 	if (ret) {
73 		EMSG("Failed to open the default session");
74 		goto out;
75 	}
76 
77 	ret = update_se_info();
78 	if (ret) {
79 		EMSG("Failed to read the secure element configuration");
80 		goto out;
81 	}
82 
83 	if (IS_ENABLED(CFG_CORE_SE05X_SCP03_EARLY)) {
84 		ret = enable_scp03();
85 		if (ret)
86 			EMSG("Failed to open the SCP03 session");
87 	}
88 out:
89 	if (ret)
90 		panic();
91 
92 	return ret;
93 }
94 
95 driver_init(se050_early_init);
96