lib: optee: remove the duplicate CONFIG_OPTEE
[platform/kernel/u-boot.git] / lib / optee / optee.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2017 Linaro
4  * Bryan O'Donoghue <bryan.odonoghue@linaro.org>
5  */
6
7 #include <common.h>
8 #include <fdtdec.h>
9 #include <image.h>
10 #include <log.h>
11 #include <malloc.h>
12 #include <dm/ofnode.h>
13 #include <linux/ioport.h>
14 #include <linux/libfdt.h>
15 #include <tee/optee.h>
16
17 #define optee_hdr_err_msg \
18         "OPTEE verification error:" \
19         "\n\thdr=%p image=0x%08lx magic=0x%08x tzdram 0x%08lx-0x%08lx " \
20         "\n\theader lo=0x%08x hi=0x%08x size=0x%08lx arch=0x%08x" \
21         "\n\tuimage params 0x%08lx-0x%08lx\n"
22
23 #if defined(CONFIG_OPTEE_IMAGE)
24 int optee_verify_image(struct optee_header *hdr, unsigned long tzdram_start,
25                        unsigned long tzdram_len, unsigned long image_len)
26 {
27         unsigned long tzdram_end = tzdram_start + tzdram_len;
28         uint32_t tee_file_size;
29
30         tee_file_size = hdr->init_size + hdr->paged_size +
31                         sizeof(struct optee_header);
32
33         if (hdr->magic != OPTEE_MAGIC ||
34             hdr->version != OPTEE_VERSION ||
35             hdr->init_load_addr_hi > tzdram_end ||
36             hdr->init_load_addr_lo < tzdram_start ||
37             tee_file_size > tzdram_len ||
38             tee_file_size != image_len ||
39             (hdr->init_load_addr_lo + tee_file_size) > tzdram_end) {
40                 return -EINVAL;
41         }
42
43         return 0;
44 }
45
46 int optee_verify_bootm_image(unsigned long image_addr,
47                              unsigned long image_load_addr,
48                              unsigned long image_len)
49 {
50         struct optee_header *hdr = (struct optee_header *)image_addr;
51         unsigned long tzdram_start = CONFIG_OPTEE_TZDRAM_BASE;
52         unsigned long tzdram_len = CONFIG_OPTEE_TZDRAM_SIZE;
53
54         int ret;
55
56         ret = optee_verify_image(hdr, tzdram_start, tzdram_len, image_len);
57         if (ret)
58                 goto error;
59
60         if (image_load_addr + sizeof(*hdr) != hdr->init_load_addr_lo) {
61                 ret = -EINVAL;
62                 goto error;
63         }
64
65         return ret;
66 error:
67         printf(optee_hdr_err_msg, hdr, image_addr, hdr->magic, tzdram_start,
68                tzdram_start + tzdram_len, hdr->init_load_addr_lo,
69                hdr->init_load_addr_hi, image_len, hdr->arch, image_load_addr,
70                image_load_addr + image_len);
71
72         return ret;
73 }
74 #endif
75
76 #if defined(CONFIG_OF_LIBFDT)
77 static int optee_copy_firmware_node(ofnode node, void *fdt_blob)
78 {
79         int offs, ret, len;
80         const void *prop;
81
82         offs = fdt_path_offset(fdt_blob, "/firmware");
83         if (offs < 0) {
84                 offs = fdt_path_offset(fdt_blob, "/");
85                 if (offs < 0)
86                         return offs;
87
88                 offs = fdt_add_subnode(fdt_blob, offs, "firmware");
89                 if (offs < 0)
90                         return offs;
91         }
92
93         offs = fdt_add_subnode(fdt_blob, offs, "optee");
94         if (offs < 0)
95                 return offs;
96
97         /* copy the compatible property */
98         prop = ofnode_get_property(node, "compatible", &len);
99         if (!prop) {
100                 debug("missing OP-TEE compatible property");
101                 return -EINVAL;
102         }
103
104         ret = fdt_setprop(fdt_blob, offs, "compatible", prop, len);
105         if (ret < 0)
106                 return ret;
107
108         /* copy the method property */
109         prop = ofnode_get_property(node, "method", &len);
110         if (!prop) {
111                 debug("missing OP-TEE method property");
112                 return -EINVAL;
113         }
114
115         ret = fdt_setprop(fdt_blob, offs, "method", prop, len);
116         if (ret < 0)
117                 return ret;
118
119         return 0;
120 }
121
122 int optee_copy_fdt_nodes(void *new_blob)
123 {
124         ofnode node, subnode;
125         int ret;
126         struct resource res;
127
128         if (fdt_check_header(new_blob))
129                 return -EINVAL;
130
131         /* only proceed if there is an /firmware/optee node */
132         node = ofnode_path("/firmware/optee");
133         if (!ofnode_valid(node)) {
134                 debug("No OP-TEE firmware node in old fdt, nothing to do");
135                 return 0;
136         }
137
138         /*
139          * Do not proceed if the target dt already has an OP-TEE node.
140          * In this case assume that the system knows better somehow,
141          * so do not interfere.
142          */
143         if (fdt_path_offset(new_blob, "/firmware/optee") >= 0) {
144                 debug("OP-TEE Device Tree node already exists in target");
145                 return 0;
146         }
147
148         ret = optee_copy_firmware_node(node, new_blob);
149         if (ret < 0) {
150                 printf("Failed to add OP-TEE firmware node\n");
151                 return ret;
152         }
153
154         /* optee inserts its memory regions as reserved-memory nodes */
155         node = ofnode_path("/reserved-memory");
156         if (ofnode_valid(node)) {
157                 ofnode_for_each_subnode(subnode, node) {
158                         const char *name = ofnode_get_name(subnode);
159                         if (!name)
160                                 return -EINVAL;
161
162                         /* only handle optee reservations */
163                         if (strncmp(name, "optee", 5))
164                                 continue;
165
166                         /* check if this subnode has a reg property */
167                         ret = ofnode_read_resource(subnode, 0, &res);
168                         if (!ret) {
169                                 struct fdt_memory carveout = {
170                                         .start = res.start,
171                                         .end = res.end,
172                                 };
173                                 char *oldname, *nodename, *tmp;
174
175                                 oldname = strdup(name);
176                                 if (!oldname)
177                                         return -ENOMEM;
178
179                                 tmp = oldname;
180                                 nodename = strsep(&tmp, "@");
181                                 if (!nodename) {
182                                         free(oldname);
183                                         return -EINVAL;
184                                 }
185
186                                 ret = fdtdec_add_reserved_memory(new_blob,
187                                                                  nodename,
188                                                                  &carveout,
189                                                                  NULL, true);
190                                 free(oldname);
191
192                                 if (ret < 0)
193                                         return ret;
194                         }
195                 }
196         }
197
198         return 0;
199 }
200 #endif