#include <common.h> | |
#include <cpu_func.h> | |
#include <malloc.h> | |
#include <tee.h> | |
#include <asm/cache.h> | |
#include <asm/errno.h> | |
#define PTA_RNG_ACCESS_UUID \ | |
{ \ | |
0x185e0a22, 0x969f, 0x43b9, \ | |
{ 0xbb, 0x94, 0x66, 0xe2, 0x88, 0x8e, 0x26, 0x26 } \ | |
} | |
#define CMD_RNG_READ_DATA 0x1 | |
static int tee_read_rng_data(u32 len) | |
{ | |
struct tee_device *tdev; | |
struct tee_shm *shm_output; | |
struct tee_open_session_arg arg = {0}; | |
struct tee_invoke_arg arg_func = {0}; | |
struct tee_param param[1] = {0}; | |
char *rng_data; | |
int i, ret; | |
const struct tee_optee_ta_uuid uuid = PTA_RNG_ACCESS_UUID; | |
tdev = tee_find_device(NULL, NULL, NULL, NULL); | |
if (!tdev) { | |
printf("Cannot get OP-TEE device\n"); | |
return -1; | |
} | |
//printf("Get TEE device %s\n", tdev->name); | |
/* Set TA UUID */ | |
tee_optee_ta_uuid_to_octets(arg.uuid, &uuid); | |
/* Open TA session */ | |
ret = tee_open_session(tdev, &arg, 0, NULL); | |
if (ret < 0) { | |
printf("Cannot open session with PTA Blob 0x%X\n", ret); | |
return -1; | |
} | |
rng_data = malloc(len); | |
if (!rng_data) { | |
printf("No enough memory\n"); | |
return -1; | |
} | |
memset(rng_data, 0, len); | |
ret = tee_shm_register(tdev, (void *)(ulong)rng_data, len, | |
0x0, &shm_output); | |
if (ret < 0) { | |
printf("Cannot register output shared memory 0x%X\n", ret); | |
goto error; | |
} | |
param[0].u.memref.shm = shm_output; | |
param[0].u.memref.size = shm_output->size; | |
param[0].attr = TEE_PARAM_ATTR_TYPE_MEMREF_INOUT; | |
arg_func.func = CMD_RNG_READ_DATA; | |
arg_func.session = arg.session; | |
ret = tee_invoke_func(tdev, &arg_func, 1, param); | |
if (ret < 0) { | |
printf("Cannot get rng data from OP-TEE PTA_RNG\n"); | |
} else { | |
for (i = 0; i < len; i++) { | |
printf("0x%02x ", rng_data[i]); | |
if (((i+1)%8) == 0) | |
printf("\n"); | |
} | |
printf("\n"); | |
} | |
tee_shm_free(shm_output); | |
error: | |
free(rng_data); | |
ret = tee_close_session(tdev, arg.session); | |
if (ret < 0) | |
printf("Cannot close session with PTA_RNG 0x%X\n", ret); | |
return ret; | |
} | |
int do_tee_rng (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[]) | |
{ | |
int ret; | |
u32 length; | |
if (argc < 2) { | |
printf("run 'help tee_rng' to check usage\n"); | |
return 0; | |
} | |
length = simple_strtoul(argv[1], NULL, 10); | |
ret = tee_read_rng_data(length); | |
return ret; | |
} | |
U_BOOT_CMD( | |
tee_rng, 2, 1, do_tee_rng, | |
"Read random from OP-TEE PTA_RNG and dump command", | |
"<size> - read <size> bytes random from OP-TEE PTA\n" | |
); |