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 
se050_core_early_init(struct se050_scp_key * keys)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 
update_se_info(void)42 static TEE_Result update_se_info(void)
43 {
44 	sss_status_t status = kStatus_SSS_Success;
45 
46 	status = se050_get_se_info(se050_session,
47 				   IS_ENABLED(CFG_CORE_SE05X_DISPLAY_INFO));
48 
49 	/* the session must be closed after accessing the board information */
50 	sss_se05x_session_close(se050_session);
51 
52 	if (status != kStatus_SSS_Success)
53 		return TEE_ERROR_GENERIC;
54 
55 	return se050_core_early_init(NULL);
56 }
57 
enable_scp03(void)58 static TEE_Result enable_scp03(void)
59 {
60 	if (se050_enable_scp03(se050_session) != kStatus_SSS_Success)
61 		return TEE_ERROR_GENERIC;
62 
63 	return TEE_SUCCESS;
64 }
65 
se050_early_init(void)66 static TEE_Result se050_early_init(void)
67 {
68 	TEE_Result ret = TEE_SUCCESS;
69 
70 	ret = se050_core_early_init(NULL);
71 	if (ret)
72 		return ret;
73 
74 	ret = update_se_info();
75 	if (ret)
76 		return ret;
77 
78 	if (IS_ENABLED(CFG_CORE_SE05X_SCP03_EARLY))
79 		return enable_scp03();
80 
81 	return TEE_SUCCESS;
82 }
83 
84 driver_init(se050_early_init);
85