1 // SPDX-License-Identifier: BSD-3-Clause
2 /*
3  * Copyright (c) 2016-2020, STMicroelectronics
4  */
5 
6 #include <drivers/stm32_bsec.h>
7 #include <kernel/thread.h>
8 #include <tee_api_types.h>
9 #include <trace.h>
10 
11 #include "bsec_svc.h"
12 #include "stm32mp1_smc.h"
13 
bsec_main(struct thread_smc_args * args)14 void bsec_main(struct thread_smc_args *args)
15 {
16 	TEE_Result result = TEE_ERROR_GENERIC;
17 	uint32_t cmd = args->a1;
18 	uint32_t otp_id = args->a2;
19 	uint32_t in_value = args->a3;
20 	uint32_t *out_value = &args->a1;
21 	uint32_t tmp = 0;
22 
23 	if (!stm32_bsec_nsec_can_access_otp(otp_id)) {
24 		args->a0 = STM32_SIP_SVC_INVALID_PARAMS;
25 		return;
26 	}
27 
28 	switch (cmd) {
29 	case STM32_SIP_SVC_BSEC_READ_SHADOW:
30 		FMSG("read shadow @%#"PRIx32, otp_id);
31 		result = stm32_bsec_read_otp(out_value, otp_id);
32 		break;
33 	case STM32_SIP_SVC_BSEC_PROG_OTP:
34 		FMSG("program @%#"PRIx32, otp_id);
35 		result = stm32_bsec_program_otp(in_value, otp_id);
36 		break;
37 	case STM32_SIP_SVC_BSEC_WRITE_SHADOW:
38 		FMSG("write shadow @%#"PRIx32, otp_id);
39 		result = stm32_bsec_write_otp(in_value, otp_id);
40 		break;
41 	case STM32_SIP_SVC_BSEC_READ_OTP:
42 		FMSG("read @%#"PRIx32, otp_id);
43 		result = stm32_bsec_read_otp(&tmp, otp_id);
44 		if (!result)
45 			result = stm32_bsec_shadow_register(otp_id);
46 		if (!result)
47 			result = stm32_bsec_read_otp(out_value, otp_id);
48 		if (!result)
49 			result = stm32_bsec_write_otp(tmp, otp_id);
50 		break;
51 	case STM32_SIP_SVC_BSEC_WRLOCK_OTP:
52 		FMSG("permanent write lock @%#"PRIx32, otp_id);
53 		result = stm32_bsec_permanent_lock_otp(otp_id);
54 		break;
55 	default:
56 		DMSG("Invalid command %#"PRIx32, cmd);
57 		result = TEE_ERROR_BAD_PARAMETERS;
58 		break;
59 	}
60 
61 	if (!result)
62 		args->a0 = STM32_SIP_SVC_OK;
63 	else if (result == TEE_ERROR_BAD_PARAMETERS)
64 		args->a0 = STM32_SIP_SVC_INVALID_PARAMS;
65 	else
66 		args->a0 = STM32_SIP_SVC_FAILED;
67 }
68