blob: b018d87bd08b7c0d0612344093eaf3f6c0b5583e [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001#include <common.h>
2#include <cpu_func.h>
3#include <malloc.h>
4#include <tee.h>
5#include <asm/cache.h>
6#include <asm/errno.h>
7
8#define PTA_RNG_ACCESS_UUID \
9 { \
10 0x185e0a22, 0x969f, 0x43b9, \
11 { 0xbb, 0x94, 0x66, 0xe2, 0x88, 0x8e, 0x26, 0x26 } \
12 }
13
14#define CMD_RNG_READ_DATA 0x1
15
16
17static int tee_read_rng_data(u32 len)
18{
19 struct tee_device *tdev;
20 struct tee_shm *shm_output;
21 struct tee_open_session_arg arg = {0};
22 struct tee_invoke_arg arg_func = {0};
23 struct tee_param param[1] = {0};
24 char *rng_data;
25 int i, ret;
26
27 const struct tee_optee_ta_uuid uuid = PTA_RNG_ACCESS_UUID;
28
29 tdev = tee_find_device(NULL, NULL, NULL, NULL);
30 if (!tdev) {
31 printf("Cannot get OP-TEE device\n");
32 return -1;
33 }
34
35 //printf("Get TEE device %s\n", tdev->name);
36
37 /* Set TA UUID */
38 tee_optee_ta_uuid_to_octets(arg.uuid, &uuid);
39
40 /* Open TA session */
41 ret = tee_open_session(tdev, &arg, 0, NULL);
42 if (ret < 0) {
43 printf("Cannot open session with PTA Blob 0x%X\n", ret);
44 return -1;
45 }
46
47
48 rng_data = malloc(len);
49 if (!rng_data) {
50 printf("No enough memory\n");
51 return -1;
52 }
53 memset(rng_data, 0, len);
54
55 ret = tee_shm_register(tdev, (void *)(ulong)rng_data, len,
56 0x0, &shm_output);
57 if (ret < 0) {
58 printf("Cannot register output shared memory 0x%X\n", ret);
59 goto error;
60 }
61
62 param[0].u.memref.shm = shm_output;
63 param[0].u.memref.size = shm_output->size;
64 param[0].attr = TEE_PARAM_ATTR_TYPE_MEMREF_INOUT;
65
66 arg_func.func = CMD_RNG_READ_DATA;
67 arg_func.session = arg.session;
68
69 ret = tee_invoke_func(tdev, &arg_func, 1, param);
70 if (ret < 0) {
71 printf("Cannot get rng data from OP-TEE PTA_RNG\n");
72 } else {
73 for (i = 0; i < len; i++) {
74 printf("0x%02x ", rng_data[i]);
75 if (((i+1)%8) == 0)
76 printf("\n");
77 }
78 printf("\n");
79 }
80
81 tee_shm_free(shm_output);
82
83error:
84 free(rng_data);
85
86 ret = tee_close_session(tdev, arg.session);
87 if (ret < 0)
88 printf("Cannot close session with PTA_RNG 0x%X\n", ret);
89
90 return ret;
91}
92
93int do_tee_rng (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
94{
95 int ret;
96 u32 length;
97
98 if (argc < 2) {
99 printf("run 'help tee_rng' to check usage\n");
100 return 0;
101 }
102
103 length = simple_strtoul(argv[1], NULL, 10);
104
105 ret = tee_read_rng_data(length);
106
107 return ret;
108}
109
110U_BOOT_CMD(
111 tee_rng, 2, 1, do_tee_rng,
112 "Read random from OP-TEE PTA_RNG and dump command",
113 "<size> - read <size> bytes random from OP-TEE PTA\n"
114);