1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2019, Broadcom
4  */
5 
6 #include <drivers/bcm_hwrng.h>
7 #include <io.h>
8 #include <kernel/pseudo_ta.h>
9 #include <trace.h>
10 
11 #define HWRNG_SERVICE_UUID \
12 		{ 0x6272636D, 0x2019, 0x0201,  \
13 		{ 0x42, 0x43, 0x4D, 0x5F, 0x52, 0x4E, 0x47, 0x30 } }
14 
15 /*
16  * Get a HW generated random number
17  *
18  * [out]     value[0].a: Generated 32-bit random number
19  */
20 #define PTA_BCM_HWRNG_CMD_GET	0
21 
22 #define HWRNG_TA_NAME		"pta_hwrng.ta"
23 
pta_hwrng_get(uint32_t param_types,TEE_Param params[TEE_NUM_PARAMS])24 static TEE_Result pta_hwrng_get(uint32_t param_types,
25 				TEE_Param params[TEE_NUM_PARAMS])
26 {
27 	uint32_t num_words = 0;
28 	uint32_t rnd_num = 0;
29 	uint32_t res = 0;
30 	uint32_t exp_param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_OUTPUT,
31 						   TEE_PARAM_TYPE_NONE,
32 						   TEE_PARAM_TYPE_NONE,
33 						   TEE_PARAM_TYPE_NONE);
34 
35 	if (exp_param_types != param_types) {
36 		EMSG("Invalid Param types");
37 		return TEE_ERROR_BAD_PARAMETERS;
38 	}
39 
40 	num_words = bcm_hwrng_read_rng(&rnd_num, 1);
41 
42 	if (num_words < 1) {
43 		res = TEE_ERROR_NO_DATA;
44 	} else {
45 		DMSG("Random Value is: 0x%08x", rnd_num);
46 		params[0].value.a = rnd_num;
47 		res = TEE_SUCCESS;
48 	}
49 
50 	return res;
51 }
52 
invoke_command(void * session_context __unused,uint32_t cmd_id,uint32_t param_types,TEE_Param params[TEE_NUM_PARAMS])53 static TEE_Result invoke_command(void *session_context __unused,
54 				 uint32_t cmd_id,
55 				 uint32_t param_types,
56 				 TEE_Param params[TEE_NUM_PARAMS])
57 {
58 	TEE_Result res = TEE_SUCCESS;
59 
60 	DMSG("command entry point[%d] for \"%s\"", cmd_id, HWRNG_TA_NAME);
61 
62 	switch (cmd_id) {
63 	case PTA_BCM_HWRNG_CMD_GET:
64 		res = pta_hwrng_get(param_types, params);
65 		break;
66 	default:
67 		EMSG("cmd: %d Not supported %s", cmd_id, HWRNG_TA_NAME);
68 		res = TEE_ERROR_NOT_SUPPORTED;
69 		break;
70 	}
71 
72 	return res;
73 }
74 
75 pseudo_ta_register(.uuid = HWRNG_SERVICE_UUID,
76 		   .name = HWRNG_TA_NAME,
77 		   .flags = PTA_DEFAULT_FLAGS,
78 		   .invoke_command_entry_point = invoke_command);
79