1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2017 Linaro
4 * Bryan O'Donoghue <bryan.odonoghue@linaro.org>
10 #include <linux/libfdt.h>
11 #include <tee/optee.h>
13 #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"
19 int optee_verify_image(struct optee_header *hdr, unsigned long tzdram_start,
20 unsigned long tzdram_len, unsigned long image_len)
22 unsigned long tzdram_end = tzdram_start + tzdram_len;
23 uint32_t tee_file_size;
25 tee_file_size = hdr->init_size + hdr->paged_size +
26 sizeof(struct optee_header);
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) {
41 int optee_verify_bootm_image(unsigned long image_addr,
42 unsigned long image_load_addr,
43 unsigned long image_len)
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;
51 ret = optee_verify_image(hdr, tzdram_start, tzdram_len, image_len);
55 if (image_load_addr + sizeof(*hdr) != hdr->init_load_addr_lo) {
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);
70 #if defined(CONFIG_OF_LIBFDT)
71 static int optee_copy_firmware_node(const void *old_blob, void *fdt_blob)
73 int old_offs, offs, ret, len;
76 old_offs = fdt_path_offset(old_blob, "/firmware/optee");
78 debug("Original OP-TEE Device Tree node not found");
82 offs = fdt_path_offset(fdt_blob, "/firmware");
84 offs = fdt_path_offset(fdt_blob, "/");
88 offs = fdt_add_subnode(fdt_blob, offs, "firmware");
93 offs = fdt_add_subnode(fdt_blob, offs, "optee");
97 /* copy the compatible property */
98 prop = fdt_getprop(old_blob, old_offs, "compatible", &len);
100 debug("missing OP-TEE compatible property");
104 ret = fdt_setprop(fdt_blob, offs, "compatible", prop, len);
108 /* copy the method property */
109 prop = fdt_getprop(old_blob, old_offs, "method", &len);
111 debug("missing OP-TEE method property");
115 ret = fdt_setprop(fdt_blob, offs, "method", prop, len);
122 int optee_copy_fdt_nodes(const void *old_blob, void *new_blob)
124 int nodeoffset, subnode, ret;
125 struct fdt_resource res;
127 if (fdt_check_header(old_blob))
130 if (fdt_check_header(new_blob))
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");
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.
144 if (fdt_path_offset(new_blob, "/firmware/optee") >= 0) {
145 debug("OP-TEE Device Tree node already exists in target");
149 ret = optee_copy_firmware_node(old_blob, new_blob);
151 printf("Failed to add OP-TEE firmware node\n");
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,
165 /* only handle optee reservations */
166 if (strncmp(name, "optee", 5))
169 /* check if this subnode has a reg property */
170 ret = fdt_get_resource(old_blob, subnode, "reg", 0,
173 struct fdt_memory carveout = {
177 char *oldname, *nodename, *tmp;
179 oldname = strdup(name);
184 nodename = strsep(&tmp, "@");
190 ret = fdtdec_add_reserved_memory(new_blob,
200 subnode = fdt_next_subnode(old_blob, subnode);