blob: 51abd5be394b3d861ed759dd94861f0c8212c380 [file] [log] [blame]
Lawrence Mokb36afa72024-08-06 15:45:52 -07001// SPDX-License-Identifier: (GPL-2.0+ OR MIT)
2/*
3 * Copyright (c) 2023 Amlogic, Inc. All rights reserved.
4 */
5
6#include <common.h>
7#include <command.h>
8#include <tee.h>
9
10#define PROVISION_PTA_UUID \
11 { 0x24b17b16, 0x89d1, 0x43a3, \
12 { 0x95, 0xed, 0x19, 0x3c, 0xe1, 0xed, 0xa7, 0x15 } }
13
14#define PROVISION_PTA_CMD_AMPK_GET_DEVICE_PUBLIC_KEY (1)
15#define TEE_PARAM_NUM (4)
16
17static int get_device_pub_key(u8 *public_key, u32 public_key_size)
18{
19 int ret = CMD_RET_FAILURE;
20 u8 input[4] = {0};
21 u32 input_size = sizeof(input);
22 struct udevice *dev = NULL;
23 struct tee_open_session_arg open_arg = { 0 };
24 struct tee_invoke_arg invoke_arg = { 0 };
25 struct tee_param param[TEE_PARAM_NUM] = { 0 };
26 const struct tee_optee_ta_uuid uuid = PROVISION_PTA_UUID;
27
28 dev = tee_find_device(NULL, NULL, NULL, NULL);
29 if (!dev) {
30 printf("tee find device failed\n");
31 return CMD_RET_FAILURE;
32 }
33
34 memset(&open_arg, 0, sizeof(open_arg));
35 tee_optee_ta_uuid_to_octets(open_arg.uuid, &uuid);
36 ret = tee_open_session(dev, &open_arg, 0, NULL);
37 if (ret) {
38 printf("open session failed, ret = 0x%x\n", ret);
39 return CMD_RET_FAILURE;
40 }
41
42 if (open_arg.ret) {
43 printf("open session failed, ret = 0x%x, ret_origin = 0x%x\n",
44 open_arg.ret, open_arg.ret_origin);
45 return CMD_RET_FAILURE;
46 }
47
48 param[0].attr = TEE_PARAM_ATTR_TYPE_MEMREF_INPUT;
49 param[0].u.memref.size = input_size;
50 ret = tee_shm_alloc(dev, input_size, 0, &param[0].u.memref.shm);
51 if (ret) {
52 printf("tee shm alloc input failed, ret = 0x%x\n", ret);
53 ret = CMD_RET_FAILURE;
54 goto exit;
55 }
56 memcpy(param[0].u.memref.shm->addr, input, input_size);
57
58 param[1].attr = TEE_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
59 param[1].u.memref.size = public_key_size;
60 ret = tee_shm_alloc(dev, public_key_size, 0, &param[1].u.memref.shm);
61 if (ret) {
62 printf("tee shm alloc for key failed, ret = 0x%x\n", ret);
63 ret = CMD_RET_FAILURE;
64 goto exit;
65 }
66
67 memset(&invoke_arg, 0, sizeof(invoke_arg));
68 invoke_arg.session = open_arg.session;
69 invoke_arg.func = PROVISION_PTA_CMD_AMPK_GET_DEVICE_PUBLIC_KEY;
70
71 ret = tee_invoke_func(dev, &invoke_arg, TEE_PARAM_NUM, param);
72 if (ret) {
73 printf("invoke failed, cmd = 0x%x, ret = 0x%x\n", invoke_arg.func, ret);
74 ret = CMD_RET_FAILURE;
75 goto exit;
76 }
77
78 if (invoke_arg.ret) {
79 printf("invoke failed, cmd = 0x%x, ret = 0x%x, origin = 0x%x\n",
80 invoke_arg.func, invoke_arg.ret, invoke_arg.ret_origin);
81 ret = CMD_RET_FAILURE;
82 goto exit;
83 }
84
85 if (param[1].u.memref.size <= public_key_size) {
86 memcpy(public_key, param[1].u.memref.shm->addr, param[1].u.memref.size);
87 ret = CMD_RET_SUCCESS;
88 } else {
89 ret = CMD_RET_FAILURE;
90 }
91
92exit:
93 if (param[0].u.memref.shm)
94 tee_shm_free(param[0].u.memref.shm);
95
96 if (param[1].u.memref.shm)
97 tee_shm_free(param[1].u.memref.shm);
98
99 tee_close_session(dev, open_arg.session);
100
101 return ret;
102}
103
104static int do_apmk(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
105{
106 u8 public_key[64];
107 u32 public_key_size = sizeof(public_key);
108
109 if (argc < 2)
110 return CMD_RET_USAGE;
111 if (strcmp(argv[1], "get_device_pub_key") == 0) {
112 if (get_device_pub_key(public_key, public_key_size) ==
113 CMD_RET_SUCCESS) {
114 u32 i = 0;
115
116 printf("ampk device public key ");
117 for (i = 0; i < public_key_size; i++)
118 printf("%02x", public_key[i]);
119 printf("\n");
120 return CMD_RET_SUCCESS;
121 }
122 }
123 return CMD_RET_USAGE;
124}
125
126U_BOOT_CMD(ampk, 2, 0, do_apmk,
127 "AMPK cmds",
128 "get_device_pub_key\n"
129);