blob: b0362240446960fe3f1b58705bba119e809a35cc [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:" \
Alexandru Gagniuc26fc6672021-09-07 12:07:06 -050019 "\n\thdr=%p image=0x%08lx magic=0x%08x" \
Bryan O'Donoghue6ffc4202018-03-13 16:50:34 +000020 "\n\theader lo=0x%08x hi=0x%08x size=0x%08lx arch=0x%08x" \
21 "\n\tuimage params 0x%08lx-0x%08lx\n"
22
Patrick Delaunay51827f92021-09-02 11:56:16 +020023#if defined(CONFIG_OPTEE_IMAGE)
Alexandru Gagniuc26fc6672021-09-07 12:07:06 -050024static int optee_verify_image(struct optee_header *hdr, unsigned long image_len)
Bryan O'Donoghue32ce6172018-03-13 16:50:27 +000025{
Bryan O'Donoghue32ce6172018-03-13 16:50:27 +000026 uint32_t tee_file_size;
27
28 tee_file_size = hdr->init_size + hdr->paged_size +
29 sizeof(struct optee_header);
30
31 if (hdr->magic != OPTEE_MAGIC ||
32 hdr->version != OPTEE_VERSION ||
Alexandru Gagniuc26fc6672021-09-07 12:07:06 -050033 tee_file_size != image_len) {
Bryan O'Donoghue32ce6172018-03-13 16:50:27 +000034 return -EINVAL;
35 }
36
37 return 0;
38}
Bryan O'Donoghuec5a6e8b2018-03-13 16:50:33 +000039
40int optee_verify_bootm_image(unsigned long image_addr,
41 unsigned long image_load_addr,
42 unsigned long image_len)
43{
44 struct optee_header *hdr = (struct optee_header *)image_addr;
Bryan O'Donoghuec5a6e8b2018-03-13 16:50:33 +000045 int ret;
46
Alexandru Gagniuc26fc6672021-09-07 12:07:06 -050047 ret = optee_verify_image(hdr, image_len);
Bryan O'Donoghuec5a6e8b2018-03-13 16:50:33 +000048 if (ret)
Bryan O'Donoghue6ffc4202018-03-13 16:50:34 +000049 goto error;
Bryan O'Donoghuec5a6e8b2018-03-13 16:50:33 +000050
Bryan O'Donoghue6ffc4202018-03-13 16:50:34 +000051 if (image_load_addr + sizeof(*hdr) != hdr->init_load_addr_lo) {
Bryan O'Donoghuec5a6e8b2018-03-13 16:50:33 +000052 ret = -EINVAL;
Bryan O'Donoghue6ffc4202018-03-13 16:50:34 +000053 goto error;
54 }
55
56 return ret;
57error:
Alexandru Gagniuc26fc6672021-09-07 12:07:06 -050058 printf(optee_hdr_err_msg, hdr, image_addr, hdr->magic,
59 hdr->init_load_addr_lo,
Bryan O'Donoghue6ffc4202018-03-13 16:50:34 +000060 hdr->init_load_addr_hi, image_len, hdr->arch, image_load_addr,
61 image_load_addr + image_len);
Bryan O'Donoghuec5a6e8b2018-03-13 16:50:33 +000062
63 return ret;
64}
Patrick Delaunay51827f92021-09-02 11:56:16 +020065#endif
Heiko Stuebner6ccb05e2019-10-23 16:46:40 +020066
67#if defined(CONFIG_OF_LIBFDT)
Patrick Delaunaya2535242021-02-08 13:54:31 +010068static int optee_copy_firmware_node(ofnode node, void *fdt_blob)
Heiko Stuebner6ccb05e2019-10-23 16:46:40 +020069{
Patrick Delaunaya2535242021-02-08 13:54:31 +010070 int offs, ret, len;
Heiko Stuebner6ccb05e2019-10-23 16:46:40 +020071 const void *prop;
72
Heiko Stuebner6ccb05e2019-10-23 16:46:40 +020073 offs = fdt_path_offset(fdt_blob, "/firmware");
74 if (offs < 0) {
75 offs = fdt_path_offset(fdt_blob, "/");
76 if (offs < 0)
77 return offs;
78
79 offs = fdt_add_subnode(fdt_blob, offs, "firmware");
80 if (offs < 0)
81 return offs;
82 }
83
84 offs = fdt_add_subnode(fdt_blob, offs, "optee");
85 if (offs < 0)
Christoph Müllner0f97e922020-01-26 23:20:54 +010086 return offs;
Heiko Stuebner6ccb05e2019-10-23 16:46:40 +020087
88 /* copy the compatible property */
Patrick Delaunaya2535242021-02-08 13:54:31 +010089 prop = ofnode_get_property(node, "compatible", &len);
Heiko Stuebner6ccb05e2019-10-23 16:46:40 +020090 if (!prop) {
91 debug("missing OP-TEE compatible property");
92 return -EINVAL;
93 }
94
95 ret = fdt_setprop(fdt_blob, offs, "compatible", prop, len);
96 if (ret < 0)
97 return ret;
98
99 /* copy the method property */
Patrick Delaunaya2535242021-02-08 13:54:31 +0100100 prop = ofnode_get_property(node, "method", &len);
Heiko Stuebner6ccb05e2019-10-23 16:46:40 +0200101 if (!prop) {
102 debug("missing OP-TEE method property");
103 return -EINVAL;
104 }
105
106 ret = fdt_setprop(fdt_blob, offs, "method", prop, len);
107 if (ret < 0)
108 return ret;
109
110 return 0;
111}
112
Patrick Delaunaya2535242021-02-08 13:54:31 +0100113int optee_copy_fdt_nodes(void *new_blob)
Heiko Stuebner6ccb05e2019-10-23 16:46:40 +0200114{
Patrick Delaunaya2535242021-02-08 13:54:31 +0100115 ofnode node, subnode;
116 int ret;
117 struct resource res;
Heiko Stuebner6ccb05e2019-10-23 16:46:40 +0200118
119 if (fdt_check_header(new_blob))
120 return -EINVAL;
121
122 /* only proceed if there is an /firmware/optee node */
Patrick Delaunaya2535242021-02-08 13:54:31 +0100123 node = ofnode_path("/firmware/optee");
124 if (!ofnode_valid(node)) {
Heiko Stuebner6ccb05e2019-10-23 16:46:40 +0200125 debug("No OP-TEE firmware node in old fdt, nothing to do");
126 return 0;
127 }
128
129 /*
130 * Do not proceed if the target dt already has an OP-TEE node.
131 * In this case assume that the system knows better somehow,
132 * so do not interfere.
133 */
134 if (fdt_path_offset(new_blob, "/firmware/optee") >= 0) {
135 debug("OP-TEE Device Tree node already exists in target");
136 return 0;
137 }
138
Patrick Delaunaya2535242021-02-08 13:54:31 +0100139 ret = optee_copy_firmware_node(node, new_blob);
Heiko Stuebner6ccb05e2019-10-23 16:46:40 +0200140 if (ret < 0) {
141 printf("Failed to add OP-TEE firmware node\n");
142 return ret;
143 }
144
145 /* optee inserts its memory regions as reserved-memory nodes */
Patrick Delaunaya2535242021-02-08 13:54:31 +0100146 node = ofnode_path("/reserved-memory");
147 if (ofnode_valid(node)) {
148 ofnode_for_each_subnode(subnode, node) {
149 const char *name = ofnode_get_name(subnode);
Heiko Stuebner6ccb05e2019-10-23 16:46:40 +0200150 if (!name)
151 return -EINVAL;
152
153 /* only handle optee reservations */
154 if (strncmp(name, "optee", 5))
155 continue;
156
157 /* check if this subnode has a reg property */
Patrick Delaunaya2535242021-02-08 13:54:31 +0100158 ret = ofnode_read_resource(subnode, 0, &res);
Heiko Stuebner6ccb05e2019-10-23 16:46:40 +0200159 if (!ret) {
160 struct fdt_memory carveout = {
161 .start = res.start,
162 .end = res.end,
163 };
Thierry Redingb9aad372021-09-03 15:16:21 +0200164 unsigned long flags = FDTDEC_RESERVED_MEMORY_NO_MAP;
Heiko Stuebner6ccb05e2019-10-23 16:46:40 +0200165 char *oldname, *nodename, *tmp;
166
167 oldname = strdup(name);
168 if (!oldname)
169 return -ENOMEM;
170
171 tmp = oldname;
172 nodename = strsep(&tmp, "@");
173 if (!nodename) {
174 free(oldname);
175 return -EINVAL;
176 }
177
178 ret = fdtdec_add_reserved_memory(new_blob,
179 nodename,
180 &carveout,
Thierry Reding46cb0672021-09-03 15:16:19 +0200181 NULL, 0,
Thierry Redingb9aad372021-09-03 15:16:21 +0200182 NULL, flags);
Heiko Stuebner6ccb05e2019-10-23 16:46:40 +0200183 free(oldname);
184
185 if (ret < 0)
186 return ret;
187 }
Heiko Stuebner6ccb05e2019-10-23 16:46:40 +0200188 }
189 }
190
191 return 0;
192}
193#endif