blob: 672690dc538939288c1de84067dce6644bf9103e [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 Glass401d1c42020-10-30 21:38:53 -06008#include <fdtdec.h>
Simon Glass8e8ccfe2019-12-28 10:45:03 -07009#include <image.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060010#include <log.h>
Heiko Stuebner6ccb05e2019-10-23 16:46:40 +020011#include <malloc.h>
Patrick Delaunaya2535242021-02-08 13:54:31 +010012#include <dm/ofnode.h>
13#include <linux/ioport.h>
Heiko Stuebner6ccb05e2019-10-23 16:46:40 +020014#include <linux/libfdt.h>
Bryan O'Donoghue32ce6172018-03-13 16:50:27 +000015#include <tee/optee.h>
16
Bryan O'Donoghue6ffc4202018-03-13 16:50:34 +000017#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
Bryan O'Donoghue32ce6172018-03-13 16:50:27 +000023int optee_verify_image(struct optee_header *hdr, unsigned long tzdram_start,
24 unsigned long tzdram_len, unsigned long image_len)
25{
26 unsigned long tzdram_end = tzdram_start + tzdram_len;
27 uint32_t tee_file_size;
28
29 tee_file_size = hdr->init_size + hdr->paged_size +
30 sizeof(struct optee_header);
31
32 if (hdr->magic != OPTEE_MAGIC ||
33 hdr->version != OPTEE_VERSION ||
34 hdr->init_load_addr_hi > tzdram_end ||
35 hdr->init_load_addr_lo < tzdram_start ||
36 tee_file_size > tzdram_len ||
37 tee_file_size != image_len ||
38 (hdr->init_load_addr_lo + tee_file_size) > tzdram_end) {
39 return -EINVAL;
40 }
41
42 return 0;
43}
Bryan O'Donoghuec5a6e8b2018-03-13 16:50:33 +000044
45int optee_verify_bootm_image(unsigned long image_addr,
46 unsigned long image_load_addr,
47 unsigned long image_len)
48{
49 struct optee_header *hdr = (struct optee_header *)image_addr;
50 unsigned long tzdram_start = CONFIG_OPTEE_TZDRAM_BASE;
51 unsigned long tzdram_len = CONFIG_OPTEE_TZDRAM_SIZE;
52
53 int ret;
54
55 ret = optee_verify_image(hdr, tzdram_start, tzdram_len, image_len);
56 if (ret)
Bryan O'Donoghue6ffc4202018-03-13 16:50:34 +000057 goto error;
Bryan O'Donoghuec5a6e8b2018-03-13 16:50:33 +000058
Bryan O'Donoghue6ffc4202018-03-13 16:50:34 +000059 if (image_load_addr + sizeof(*hdr) != hdr->init_load_addr_lo) {
Bryan O'Donoghuec5a6e8b2018-03-13 16:50:33 +000060 ret = -EINVAL;
Bryan O'Donoghue6ffc4202018-03-13 16:50:34 +000061 goto error;
62 }
63
64 return ret;
65error:
66 printf(optee_hdr_err_msg, hdr, image_addr, hdr->magic, tzdram_start,
67 tzdram_start + tzdram_len, hdr->init_load_addr_lo,
68 hdr->init_load_addr_hi, image_len, hdr->arch, image_load_addr,
69 image_load_addr + image_len);
Bryan O'Donoghuec5a6e8b2018-03-13 16:50:33 +000070
71 return ret;
72}
Heiko Stuebner6ccb05e2019-10-23 16:46:40 +020073
74#if defined(CONFIG_OF_LIBFDT)
Patrick Delaunaya2535242021-02-08 13:54:31 +010075static int optee_copy_firmware_node(ofnode node, void *fdt_blob)
Heiko Stuebner6ccb05e2019-10-23 16:46:40 +020076{
Patrick Delaunaya2535242021-02-08 13:54:31 +010077 int offs, ret, len;
Heiko Stuebner6ccb05e2019-10-23 16:46:40 +020078 const void *prop;
79
Heiko Stuebner6ccb05e2019-10-23 16:46:40 +020080 offs = fdt_path_offset(fdt_blob, "/firmware");
81 if (offs < 0) {
82 offs = fdt_path_offset(fdt_blob, "/");
83 if (offs < 0)
84 return offs;
85
86 offs = fdt_add_subnode(fdt_blob, offs, "firmware");
87 if (offs < 0)
88 return offs;
89 }
90
91 offs = fdt_add_subnode(fdt_blob, offs, "optee");
92 if (offs < 0)
Christoph Müllner0f97e922020-01-26 23:20:54 +010093 return offs;
Heiko Stuebner6ccb05e2019-10-23 16:46:40 +020094
95 /* copy the compatible property */
Patrick Delaunaya2535242021-02-08 13:54:31 +010096 prop = ofnode_get_property(node, "compatible", &len);
Heiko Stuebner6ccb05e2019-10-23 16:46:40 +020097 if (!prop) {
98 debug("missing OP-TEE compatible property");
99 return -EINVAL;
100 }
101
102 ret = fdt_setprop(fdt_blob, offs, "compatible", prop, len);
103 if (ret < 0)
104 return ret;
105
106 /* copy the method property */
Patrick Delaunaya2535242021-02-08 13:54:31 +0100107 prop = ofnode_get_property(node, "method", &len);
Heiko Stuebner6ccb05e2019-10-23 16:46:40 +0200108 if (!prop) {
109 debug("missing OP-TEE method property");
110 return -EINVAL;
111 }
112
113 ret = fdt_setprop(fdt_blob, offs, "method", prop, len);
114 if (ret < 0)
115 return ret;
116
117 return 0;
118}
119
Patrick Delaunaya2535242021-02-08 13:54:31 +0100120int optee_copy_fdt_nodes(void *new_blob)
Heiko Stuebner6ccb05e2019-10-23 16:46:40 +0200121{
Patrick Delaunaya2535242021-02-08 13:54:31 +0100122 ofnode node, subnode;
123 int ret;
124 struct resource res;
Heiko Stuebner6ccb05e2019-10-23 16:46:40 +0200125
126 if (fdt_check_header(new_blob))
127 return -EINVAL;
128
129 /* only proceed if there is an /firmware/optee node */
Patrick Delaunaya2535242021-02-08 13:54:31 +0100130 node = ofnode_path("/firmware/optee");
131 if (!ofnode_valid(node)) {
Heiko Stuebner6ccb05e2019-10-23 16:46:40 +0200132 debug("No OP-TEE firmware node in old fdt, nothing to do");
133 return 0;
134 }
135
136 /*
137 * Do not proceed if the target dt already has an OP-TEE node.
138 * In this case assume that the system knows better somehow,
139 * so do not interfere.
140 */
141 if (fdt_path_offset(new_blob, "/firmware/optee") >= 0) {
142 debug("OP-TEE Device Tree node already exists in target");
143 return 0;
144 }
145
Patrick Delaunaya2535242021-02-08 13:54:31 +0100146 ret = optee_copy_firmware_node(node, new_blob);
Heiko Stuebner6ccb05e2019-10-23 16:46:40 +0200147 if (ret < 0) {
148 printf("Failed to add OP-TEE firmware node\n");
149 return ret;
150 }
151
152 /* optee inserts its memory regions as reserved-memory nodes */
Patrick Delaunaya2535242021-02-08 13:54:31 +0100153 node = ofnode_path("/reserved-memory");
154 if (ofnode_valid(node)) {
155 ofnode_for_each_subnode(subnode, node) {
156 const char *name = ofnode_get_name(subnode);
Heiko Stuebner6ccb05e2019-10-23 16:46:40 +0200157 if (!name)
158 return -EINVAL;
159
160 /* only handle optee reservations */
161 if (strncmp(name, "optee", 5))
162 continue;
163
164 /* check if this subnode has a reg property */
Patrick Delaunaya2535242021-02-08 13:54:31 +0100165 ret = ofnode_read_resource(subnode, 0, &res);
Heiko Stuebner6ccb05e2019-10-23 16:46:40 +0200166 if (!ret) {
167 struct fdt_memory carveout = {
168 .start = res.start,
169 .end = res.end,
170 };
171 char *oldname, *nodename, *tmp;
172
173 oldname = strdup(name);
174 if (!oldname)
175 return -ENOMEM;
176
177 tmp = oldname;
178 nodename = strsep(&tmp, "@");
179 if (!nodename) {
180 free(oldname);
181 return -EINVAL;
182 }
183
184 ret = fdtdec_add_reserved_memory(new_blob,
185 nodename,
186 &carveout,
Etienne Carriere3e15c312020-09-10 10:50:01 +0200187 NULL, true);
Heiko Stuebner6ccb05e2019-10-23 16:46:40 +0200188 free(oldname);
189
190 if (ret < 0)
191 return ret;
192 }
Heiko Stuebner6ccb05e2019-10-23 16:46:40 +0200193 }
194 }
195
196 return 0;
197}
198#endif