xref: /OK3568_Linux_fs/u-boot/lib/optee_clientApi/OpteeClientRkFs_common.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright 2020, Rockchip Electronics Co., Ltd
3  * hisping lin, <hisping.lin@rock-chips.com>
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 
8 #include <common.h>
9 #include <stdlib.h>
10 #include <command.h>
11 #include <boot_rkimg.h>
12 #include <part.h>
13 #ifdef CONFIG_OPTEE_V1
14 #include <optee_include/OpteeClientRkFs.h>
15 #endif
16 #ifdef CONFIG_OPTEE_V2
17 #include <optee_include/OpteeClientRkNewFs.h>
18 #endif
19 
20 static int rkss_version;
get_rkss_version(void)21 static int get_rkss_version(void)
22 {
23 	struct blk_desc *dev_desc = NULL;
24 	disk_partition_t part_info;
25 	uint8_t *read_buff;
26 	unsigned long ret = 0;
27 	uint32_t *version;
28 	uint32_t *checkstr;
29 
30 	if (rkss_version != 0)
31 		return rkss_version;
32 
33 	dev_desc = rockchip_get_bootdev();
34 	if (!dev_desc) {
35 		printf("TEEC: %s: Could not find device\n", __func__);
36 		return -1;
37 	}
38 
39 	if (part_get_info_by_name(dev_desc,
40 				  "security", &part_info) < 0) {
41 		printf("TEEC: Waring: Could not find security partition\n");
42 		rkss_version = RKSS_VERSION_ERR;
43 		return rkss_version;
44 	}
45 
46 	read_buff = (uint8_t *)memalign(CONFIG_SYS_CACHELINE_SIZE, 512);
47 	if (!read_buff) {
48 		printf("TEEC: Malloc failed!\n");
49 		return -1;
50 	}
51 
52 	ret = blk_dread(dev_desc, part_info.start, 1, read_buff);
53 	if (ret != 1) {
54 		printf("TEEC: blk_dread fail\n");
55 		free(read_buff);
56 		return -1;
57 	}
58 
59 	version = (uint32_t *)(read_buff + 512 - 8);
60 	checkstr = (uint32_t *)(read_buff + 512 - 4);
61 
62 	if (*version == 1 && *checkstr == 0x12345678)
63 		rkss_version = RKSS_VERSION_V1;
64 	else
65 		rkss_version = RKSS_VERSION_V2;
66 
67 	free(read_buff);
68 	return rkss_version;
69 }
70 
OpteeClientRkFsInit(void)71 int OpteeClientRkFsInit(void)
72 {
73 	int version;
74 
75 	version = get_rkss_version();
76 	debug("TEEC: OpteeClientRkFsInit version=%d\n", version);
77 	if (version == RKSS_VERSION_V1)
78 		return tee_supp_rk_fs_init_v1();
79 	else if (version == RKSS_VERSION_V2)
80 		return tee_supp_rk_fs_init_v2();
81 	else if (version == RKSS_VERSION_ERR)
82 		return 0;
83 	else
84 		return -1;
85 }
86 
87 #ifdef CONFIG_OPTEE_V1
OpteeClientRkFsProcess(void * cmd,size_t cmd_size)88 int OpteeClientRkFsProcess(void *cmd, size_t cmd_size)
89 {
90 	int version;
91 
92 	version = get_rkss_version();
93 	debug("TEEC: OpteeClientRkFsProcess version=%d\n", version);
94 	if (version == RKSS_VERSION_V1)
95 		return tee_supp_rk_fs_process_v1(cmd, cmd_size);
96 	else if (version == RKSS_VERSION_V2)
97 		return tee_supp_rk_fs_process_v2(cmd, cmd_size);
98 	else
99 		return -1;
100 }
101 #endif
102 
103 #ifdef CONFIG_OPTEE_V2
OpteeClientRkFsProcess(size_t num_params,struct tee_ioctl_param * params)104 int OpteeClientRkFsProcess(size_t num_params,
105 			struct tee_ioctl_param *params)
106 {
107 	int version;
108 
109 	version = get_rkss_version();
110 	debug("TEEC: OpteeClientRkFsProcess version=%d\n", version);
111 	if (version == RKSS_VERSION_V1)
112 		return tee_supp_rk_fs_process_v1(num_params, params);
113 	else if (version == RKSS_VERSION_V2)
114 		return tee_supp_rk_fs_process_v2(num_params, params);
115 	else
116 		return -1;
117 }
118 #endif
119