blob: d4355c6d0337781f25f0fc157100653f26ce0831 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Bryan O'Donoghue32ce6172018-03-13 16:50:27 +00002/*
3 * Copyright (C) 2017 Linaro
4 * Bryan O'Donoghue <bryan.odonoghue@linaro.org>
Bryan O'Donoghue32ce6172018-03-13 16:50:27 +00005 */
6
7#include <common.h>
Simon Glass8e8ccfe2019-12-28 10:45:03 -07008#include <image.h>
Heiko Stuebner6ccb05e2019-10-23 16:46:40 +02009#include <malloc.h>
10#include <linux/libfdt.h>
Bryan O'Donoghue32ce6172018-03-13 16:50:27 +000011#include <tee/optee.h>
12
Bryan O'Donoghue6ffc4202018-03-13 16:50:34 +000013#define optee_hdr_err_msg \
14 "OPTEE verification error:" \
15 "\n\thdr=%p image=0x%08lx magic=0x%08x tzdram 0x%08lx-0x%08lx " \
16 "\n\theader lo=0x%08x hi=0x%08x size=0x%08lx arch=0x%08x" \
17 "\n\tuimage params 0x%08lx-0x%08lx\n"
18
Bryan O'Donoghue32ce6172018-03-13 16:50:27 +000019int optee_verify_image(struct optee_header *hdr, unsigned long tzdram_start,
20 unsigned long tzdram_len, unsigned long image_len)
21{
22 unsigned long tzdram_end = tzdram_start + tzdram_len;
23 uint32_t tee_file_size;
24
25 tee_file_size = hdr->init_size + hdr->paged_size +
26 sizeof(struct optee_header);
27
28 if (hdr->magic != OPTEE_MAGIC ||
29 hdr->version != OPTEE_VERSION ||
30 hdr->init_load_addr_hi > tzdram_end ||
31 hdr->init_load_addr_lo < tzdram_start ||
32 tee_file_size > tzdram_len ||
33 tee_file_size != image_len ||
34 (hdr->init_load_addr_lo + tee_file_size) > tzdram_end) {
35 return -EINVAL;
36 }
37
38 return 0;
39}
Bryan O'Donoghuec5a6e8b2018-03-13 16:50:33 +000040
41int optee_verify_bootm_image(unsigned long image_addr,
42 unsigned long image_load_addr,
43 unsigned long image_len)
44{
45 struct optee_header *hdr = (struct optee_header *)image_addr;
46 unsigned long tzdram_start = CONFIG_OPTEE_TZDRAM_BASE;
47 unsigned long tzdram_len = CONFIG_OPTEE_TZDRAM_SIZE;
48
49 int ret;
50
51 ret = optee_verify_image(hdr, tzdram_start, tzdram_len, image_len);
52 if (ret)
Bryan O'Donoghue6ffc4202018-03-13 16:50:34 +000053 goto error;
Bryan O'Donoghuec5a6e8b2018-03-13 16:50:33 +000054
Bryan O'Donoghue6ffc4202018-03-13 16:50:34 +000055 if (image_load_addr + sizeof(*hdr) != hdr->init_load_addr_lo) {
Bryan O'Donoghuec5a6e8b2018-03-13 16:50:33 +000056 ret = -EINVAL;
Bryan O'Donoghue6ffc4202018-03-13 16:50:34 +000057 goto error;
58 }
59
60 return ret;
61error:
62 printf(optee_hdr_err_msg, hdr, image_addr, hdr->magic, tzdram_start,
63 tzdram_start + tzdram_len, hdr->init_load_addr_lo,
64 hdr->init_load_addr_hi, image_len, hdr->arch, image_load_addr,
65 image_load_addr + image_len);
Bryan O'Donoghuec5a6e8b2018-03-13 16:50:33 +000066
67 return ret;
68}
Heiko Stuebner6ccb05e2019-10-23 16:46:40 +020069
70#if defined(CONFIG_OF_LIBFDT)
71static int optee_copy_firmware_node(const void *old_blob, void *fdt_blob)
72{
73 int old_offs, offs, ret, len;
74 const void *prop;
75
76 old_offs = fdt_path_offset(old_blob, "/firmware/optee");
77 if (old_offs < 0) {
78 debug("Original OP-TEE Device Tree node not found");
79 return old_offs;
80 }
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)
Christoph Müllner0f97e922020-01-26 23:20:54 +010095 return offs;
Heiko Stuebner6ccb05e2019-10-23 16:46:40 +020096
97 /* copy the compatible property */
98 prop = fdt_getprop(old_blob, old_offs, "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 = fdt_getprop(old_blob, old_offs, "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
122int optee_copy_fdt_nodes(const void *old_blob, void *new_blob)
123{
124 int nodeoffset, subnode, ret;
125 struct fdt_resource res;
126
127 if (fdt_check_header(old_blob))
128 return -EINVAL;
129
130 if (fdt_check_header(new_blob))
131 return -EINVAL;
132
133 /* only proceed if there is an /firmware/optee node */
134 if (fdt_path_offset(old_blob, "/firmware/optee") < 0) {
135 debug("No OP-TEE firmware node in old fdt, nothing to do");
136 return 0;
137 }
138
139 /*
140 * Do not proceed if the target dt already has an OP-TEE node.
141 * In this case assume that the system knows better somehow,
142 * so do not interfere.
143 */
144 if (fdt_path_offset(new_blob, "/firmware/optee") >= 0) {
145 debug("OP-TEE Device Tree node already exists in target");
146 return 0;
147 }
148
149 ret = optee_copy_firmware_node(old_blob, new_blob);
150 if (ret < 0) {
151 printf("Failed to add OP-TEE firmware node\n");
152 return ret;
153 }
154
155 /* optee inserts its memory regions as reserved-memory nodes */
156 nodeoffset = fdt_subnode_offset(old_blob, 0, "reserved-memory");
157 if (nodeoffset >= 0) {
158 subnode = fdt_first_subnode(old_blob, nodeoffset);
159 while (subnode >= 0) {
160 const char *name = fdt_get_name(old_blob,
161 subnode, NULL);
162 if (!name)
163 return -EINVAL;
164
165 /* only handle optee reservations */
166 if (strncmp(name, "optee", 5))
167 continue;
168
169 /* check if this subnode has a reg property */
170 ret = fdt_get_resource(old_blob, subnode, "reg", 0,
171 &res);
172 if (!ret) {
173 struct fdt_memory carveout = {
174 .start = res.start,
175 .end = res.end,
176 };
177 char *oldname, *nodename, *tmp;
178
179 oldname = strdup(name);
180 if (!oldname)
181 return -ENOMEM;
182
183 tmp = oldname;
184 nodename = strsep(&tmp, "@");
185 if (!nodename) {
186 free(oldname);
187 return -EINVAL;
188 }
189
190 ret = fdtdec_add_reserved_memory(new_blob,
191 nodename,
192 &carveout,
193 NULL);
194 free(oldname);
195
196 if (ret < 0)
197 return ret;
198 }
199
200 subnode = fdt_next_subnode(old_blob, subnode);
201 }
202 }
203
204 return 0;
205}
206#endif