xref: /rk3399_rockchip-uboot/lib/optee_clientApi/OpteeClientRkFs_common.c (revision d079c1a5ed50ce9f1e3b37be5ce4e13edec90bd0)
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 #include <optee_include/OpteeClientRkFs.h>
14 
15 static int rkss_version;
16 static int get_rkss_version(void)
17 {
18 	struct blk_desc *dev_desc = NULL;
19 	disk_partition_t part_info;
20 	uint8_t *read_buff;
21 	unsigned long ret = 0;
22 	uint32_t *version;
23 	uint32_t *checkstr;
24 
25 	if (rkss_version != 0)
26 		return rkss_version;
27 
28 	dev_desc = rockchip_get_bootdev();
29 	if (!dev_desc) {
30 		printf("TEEC: %s: Could not find device\n", __func__);
31 		return -1;
32 	}
33 
34 	if (part_get_info_by_name(dev_desc,
35 				  "security", &part_info) < 0) {
36 		printf("TEEC: Waring: Could not find security partition\n");
37 		rkss_version = RKSS_VERSION_ERR;
38 		return rkss_version;
39 	}
40 
41 	read_buff = (uint8_t *)malloc(512);
42 	if (!read_buff) {
43 		printf("TEEC: Malloc failed!\n");
44 		return -1;
45 	}
46 
47 	ret = blk_dread(dev_desc, part_info.start, 1, read_buff);
48 	if (ret != 1) {
49 		printf("TEEC: blk_dread fail\n");
50 		free(read_buff);
51 		return -1;
52 	}
53 
54 	version = (uint32_t *)(read_buff + 512 - 8);
55 	checkstr = (uint32_t *)(read_buff + 512 - 4);
56 
57 	if (*version == 1 && *checkstr == 0x12345678)
58 		rkss_version = RKSS_VERSION_V1;
59 	else
60 		rkss_version = RKSS_VERSION_V2;
61 
62 	free(read_buff);
63 	return rkss_version;
64 }
65 
66 int OpteeClientRkFsInit(void)
67 {
68 	int version;
69 
70 	version = get_rkss_version();
71 	debug("TEEC: OpteeClientRkFsInit version=%d\n", version);
72 	if (version == RKSS_VERSION_V1)
73 		return tee_supp_rk_fs_init_v1();
74 	else if (version == RKSS_VERSION_V2)
75 		return tee_supp_rk_fs_init_v2();
76 	else if (version == RKSS_VERSION_ERR)
77 		return 0;
78 	else
79 		return -1;
80 }
81 
82 int OpteeClientRkFsProcess(void *cmd, size_t cmd_size)
83 {
84 	int version;
85 
86 	version = get_rkss_version();
87 	debug("TEEC: OpteeClientRkFsProcess version=%d\n", version);
88 	if (version == RKSS_VERSION_V1)
89 		return tee_supp_rk_fs_process_v1(cmd, cmd_size);
90 	else if (version == RKSS_VERSION_V2)
91 		return tee_supp_rk_fs_process_v2(cmd, cmd_size);
92 	else
93 		return -1;
94 }
95