blob: 393f2715a9c128f715f27413168cefa7403bde56 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Bryan O'Donoghue4b0de7c2018-03-13 16:50:27 +00002/*
3 * Copyright (C) 2017 Linaro
4 * Bryan O'Donoghue <bryan.odonoghue@linaro.org>
Bryan O'Donoghue4b0de7c2018-03-13 16:50:27 +00005 */
6
Simon Glass3ba929a2020-10-30 21:38:53 -06007#include <fdtdec.h>
Simon Glass85f13782019-12-28 10:45:03 -07008#include <image.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Heiko Stuebnerb1a4fa02019-10-23 16:46:40 +020010#include <malloc.h>
Patrick Delaunay05381402021-02-08 13:54:31 +010011#include <dm/ofnode.h>
12#include <linux/ioport.h>
Heiko Stuebnerb1a4fa02019-10-23 16:46:40 +020013#include <linux/libfdt.h>
Bryan O'Donoghue4b0de7c2018-03-13 16:50:27 +000014#include <tee/optee.h>
15
Bryan O'Donoghue0eceed62018-03-13 16:50:34 +000016#define optee_hdr_err_msg \
17 "OPTEE verification error:" \
Alexandru Gagniucdccafba2021-09-07 12:07:06 -050018 "\n\thdr=%p image=0x%08lx magic=0x%08x" \
Bryan O'Donoghue0eceed62018-03-13 16:50:34 +000019 "\n\theader lo=0x%08x hi=0x%08x size=0x%08lx arch=0x%08x" \
20 "\n\tuimage params 0x%08lx-0x%08lx\n"
21
Patrick Delaunaya7ca4802021-09-02 11:56:16 +020022#if defined(CONFIG_OPTEE_IMAGE)
Alexandru Gagniucdccafba2021-09-07 12:07:06 -050023static int optee_verify_image(struct optee_header *hdr, unsigned long image_len)
Bryan O'Donoghue4b0de7c2018-03-13 16:50:27 +000024{
Bryan O'Donoghue4b0de7c2018-03-13 16:50:27 +000025 uint32_t tee_file_size;
26
27 tee_file_size = hdr->init_size + hdr->paged_size +
28 sizeof(struct optee_header);
29
30 if (hdr->magic != OPTEE_MAGIC ||
31 hdr->version != OPTEE_VERSION ||
Alexandru Gagniucdccafba2021-09-07 12:07:06 -050032 tee_file_size != image_len) {
Bryan O'Donoghue4b0de7c2018-03-13 16:50:27 +000033 return -EINVAL;
34 }
35
36 return 0;
37}
Bryan O'Donoghue4915f9b2018-03-13 16:50:33 +000038
39int optee_verify_bootm_image(unsigned long image_addr,
40 unsigned long image_load_addr,
41 unsigned long image_len)
42{
43 struct optee_header *hdr = (struct optee_header *)image_addr;
Bryan O'Donoghue4915f9b2018-03-13 16:50:33 +000044 int ret;
45
Alexandru Gagniucdccafba2021-09-07 12:07:06 -050046 ret = optee_verify_image(hdr, image_len);
Bryan O'Donoghue4915f9b2018-03-13 16:50:33 +000047 if (ret)
Bryan O'Donoghue0eceed62018-03-13 16:50:34 +000048 goto error;
Bryan O'Donoghue4915f9b2018-03-13 16:50:33 +000049
Bryan O'Donoghue0eceed62018-03-13 16:50:34 +000050 if (image_load_addr + sizeof(*hdr) != hdr->init_load_addr_lo) {
Bryan O'Donoghue4915f9b2018-03-13 16:50:33 +000051 ret = -EINVAL;
Bryan O'Donoghue0eceed62018-03-13 16:50:34 +000052 goto error;
53 }
54
55 return ret;
56error:
Alexandru Gagniucdccafba2021-09-07 12:07:06 -050057 printf(optee_hdr_err_msg, hdr, image_addr, hdr->magic,
58 hdr->init_load_addr_lo,
Bryan O'Donoghue0eceed62018-03-13 16:50:34 +000059 hdr->init_load_addr_hi, image_len, hdr->arch, image_load_addr,
60 image_load_addr + image_len);
Bryan O'Donoghue4915f9b2018-03-13 16:50:33 +000061
62 return ret;
63}
Patrick Delaunaya7ca4802021-09-02 11:56:16 +020064#endif
Heiko Stuebnerb1a4fa02019-10-23 16:46:40 +020065
66#if defined(CONFIG_OF_LIBFDT)
Patrick Delaunay05381402021-02-08 13:54:31 +010067static int optee_copy_firmware_node(ofnode node, void *fdt_blob)
Heiko Stuebnerb1a4fa02019-10-23 16:46:40 +020068{
Patrick Delaunay05381402021-02-08 13:54:31 +010069 int offs, ret, len;
Heiko Stuebnerb1a4fa02019-10-23 16:46:40 +020070 const void *prop;
71
Heiko Stuebnerb1a4fa02019-10-23 16:46:40 +020072 offs = fdt_path_offset(fdt_blob, "/firmware");
73 if (offs < 0) {
74 offs = fdt_path_offset(fdt_blob, "/");
75 if (offs < 0)
76 return offs;
77
78 offs = fdt_add_subnode(fdt_blob, offs, "firmware");
79 if (offs < 0)
80 return offs;
81 }
82
83 offs = fdt_add_subnode(fdt_blob, offs, "optee");
84 if (offs < 0)
Christoph Müllnercfb57672020-01-26 23:20:54 +010085 return offs;
Heiko Stuebnerb1a4fa02019-10-23 16:46:40 +020086
87 /* copy the compatible property */
Patrick Delaunay05381402021-02-08 13:54:31 +010088 prop = ofnode_get_property(node, "compatible", &len);
Heiko Stuebnerb1a4fa02019-10-23 16:46:40 +020089 if (!prop) {
90 debug("missing OP-TEE compatible property");
91 return -EINVAL;
92 }
93
94 ret = fdt_setprop(fdt_blob, offs, "compatible", prop, len);
95 if (ret < 0)
96 return ret;
97
98 /* copy the method property */
Patrick Delaunay05381402021-02-08 13:54:31 +010099 prop = ofnode_get_property(node, "method", &len);
Heiko Stuebnerb1a4fa02019-10-23 16:46:40 +0200100 if (!prop) {
101 debug("missing OP-TEE method property");
102 return -EINVAL;
103 }
104
105 ret = fdt_setprop(fdt_blob, offs, "method", prop, len);
106 if (ret < 0)
107 return ret;
108
109 return 0;
110}
111
Patrick Delaunay05381402021-02-08 13:54:31 +0100112int optee_copy_fdt_nodes(void *new_blob)
Heiko Stuebnerb1a4fa02019-10-23 16:46:40 +0200113{
Patrick Delaunay05381402021-02-08 13:54:31 +0100114 ofnode node, subnode;
115 int ret;
116 struct resource res;
Heiko Stuebnerb1a4fa02019-10-23 16:46:40 +0200117
118 if (fdt_check_header(new_blob))
119 return -EINVAL;
120
121 /* only proceed if there is an /firmware/optee node */
Patrick Delaunay05381402021-02-08 13:54:31 +0100122 node = ofnode_path("/firmware/optee");
123 if (!ofnode_valid(node)) {
Heiko Stuebnerb1a4fa02019-10-23 16:46:40 +0200124 debug("No OP-TEE firmware node in old fdt, nothing to do");
125 return 0;
126 }
127
128 /*
129 * Do not proceed if the target dt already has an OP-TEE node.
130 * In this case assume that the system knows better somehow,
131 * so do not interfere.
132 */
133 if (fdt_path_offset(new_blob, "/firmware/optee") >= 0) {
134 debug("OP-TEE Device Tree node already exists in target");
135 return 0;
136 }
137
Patrick Delaunay05381402021-02-08 13:54:31 +0100138 ret = optee_copy_firmware_node(node, new_blob);
Heiko Stuebnerb1a4fa02019-10-23 16:46:40 +0200139 if (ret < 0) {
140 printf("Failed to add OP-TEE firmware node\n");
141 return ret;
142 }
143
144 /* optee inserts its memory regions as reserved-memory nodes */
Patrick Delaunay05381402021-02-08 13:54:31 +0100145 node = ofnode_path("/reserved-memory");
146 if (ofnode_valid(node)) {
147 ofnode_for_each_subnode(subnode, node) {
148 const char *name = ofnode_get_name(subnode);
Heiko Stuebnerb1a4fa02019-10-23 16:46:40 +0200149 if (!name)
150 return -EINVAL;
151
152 /* only handle optee reservations */
153 if (strncmp(name, "optee", 5))
154 continue;
155
156 /* check if this subnode has a reg property */
Patrick Delaunay05381402021-02-08 13:54:31 +0100157 ret = ofnode_read_resource(subnode, 0, &res);
Heiko Stuebnerb1a4fa02019-10-23 16:46:40 +0200158 if (!ret) {
159 struct fdt_memory carveout = {
160 .start = res.start,
161 .end = res.end,
162 };
Thierry Redingbedc4392021-09-03 15:16:21 +0200163 unsigned long flags = FDTDEC_RESERVED_MEMORY_NO_MAP;
Heiko Stuebnerb1a4fa02019-10-23 16:46:40 +0200164 char *oldname, *nodename, *tmp;
165
166 oldname = strdup(name);
167 if (!oldname)
168 return -ENOMEM;
169
170 tmp = oldname;
171 nodename = strsep(&tmp, "@");
172 if (!nodename) {
173 free(oldname);
174 return -EINVAL;
175 }
176
177 ret = fdtdec_add_reserved_memory(new_blob,
178 nodename,
179 &carveout,
Thierry Reding5e336912021-09-03 15:16:19 +0200180 NULL, 0,
Thierry Redingbedc4392021-09-03 15:16:21 +0200181 NULL, flags);
Heiko Stuebnerb1a4fa02019-10-23 16:46:40 +0200182 free(oldname);
183
184 if (ret < 0)
185 return ret;
186 }
Heiko Stuebnerb1a4fa02019-10-23 16:46:40 +0200187 }
188 }
189
190 return 0;
191}
192#endif