1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2017 Linaro
4 * Bryan O'Donoghue <bryan.odonoghue@linaro.org>
11 #include <linux/libfdt.h>
12 #include <tee/optee.h>
14 #define optee_hdr_err_msg \
15 "OPTEE verification error:" \
16 "\n\thdr=%p image=0x%08lx magic=0x%08x tzdram 0x%08lx-0x%08lx " \
17 "\n\theader lo=0x%08x hi=0x%08x size=0x%08lx arch=0x%08x" \
18 "\n\tuimage params 0x%08lx-0x%08lx\n"
20 int optee_verify_image(struct optee_header *hdr, unsigned long tzdram_start,
21 unsigned long tzdram_len, unsigned long image_len)
23 unsigned long tzdram_end = tzdram_start + tzdram_len;
24 uint32_t tee_file_size;
26 tee_file_size = hdr->init_size + hdr->paged_size +
27 sizeof(struct optee_header);
29 if (hdr->magic != OPTEE_MAGIC ||
30 hdr->version != OPTEE_VERSION ||
31 hdr->init_load_addr_hi > tzdram_end ||
32 hdr->init_load_addr_lo < tzdram_start ||
33 tee_file_size > tzdram_len ||
34 tee_file_size != image_len ||
35 (hdr->init_load_addr_lo + tee_file_size) > tzdram_end) {
42 int optee_verify_bootm_image(unsigned long image_addr,
43 unsigned long image_load_addr,
44 unsigned long image_len)
46 struct optee_header *hdr = (struct optee_header *)image_addr;
47 unsigned long tzdram_start = CONFIG_OPTEE_TZDRAM_BASE;
48 unsigned long tzdram_len = CONFIG_OPTEE_TZDRAM_SIZE;
52 ret = optee_verify_image(hdr, tzdram_start, tzdram_len, image_len);
56 if (image_load_addr + sizeof(*hdr) != hdr->init_load_addr_lo) {
63 printf(optee_hdr_err_msg, hdr, image_addr, hdr->magic, tzdram_start,
64 tzdram_start + tzdram_len, hdr->init_load_addr_lo,
65 hdr->init_load_addr_hi, image_len, hdr->arch, image_load_addr,
66 image_load_addr + image_len);
71 #if defined(CONFIG_OF_LIBFDT)
72 static int optee_copy_firmware_node(const void *old_blob, void *fdt_blob)
74 int old_offs, offs, ret, len;
77 old_offs = fdt_path_offset(old_blob, "/firmware/optee");
79 debug("Original OP-TEE Device Tree node not found");
83 offs = fdt_path_offset(fdt_blob, "/firmware");
85 offs = fdt_path_offset(fdt_blob, "/");
89 offs = fdt_add_subnode(fdt_blob, offs, "firmware");
94 offs = fdt_add_subnode(fdt_blob, offs, "optee");
98 /* copy the compatible property */
99 prop = fdt_getprop(old_blob, old_offs, "compatible", &len);
101 debug("missing OP-TEE compatible property");
105 ret = fdt_setprop(fdt_blob, offs, "compatible", prop, len);
109 /* copy the method property */
110 prop = fdt_getprop(old_blob, old_offs, "method", &len);
112 debug("missing OP-TEE method property");
116 ret = fdt_setprop(fdt_blob, offs, "method", prop, len);
123 int optee_copy_fdt_nodes(const void *old_blob, void *new_blob)
125 int nodeoffset, subnode, ret;
126 struct fdt_resource res;
128 if (fdt_check_header(old_blob))
131 if (fdt_check_header(new_blob))
134 /* only proceed if there is an /firmware/optee node */
135 if (fdt_path_offset(old_blob, "/firmware/optee") < 0) {
136 debug("No OP-TEE firmware node in old fdt, nothing to do");
141 * Do not proceed if the target dt already has an OP-TEE node.
142 * In this case assume that the system knows better somehow,
143 * so do not interfere.
145 if (fdt_path_offset(new_blob, "/firmware/optee") >= 0) {
146 debug("OP-TEE Device Tree node already exists in target");
150 ret = optee_copy_firmware_node(old_blob, new_blob);
152 printf("Failed to add OP-TEE firmware node\n");
156 /* optee inserts its memory regions as reserved-memory nodes */
157 nodeoffset = fdt_subnode_offset(old_blob, 0, "reserved-memory");
158 if (nodeoffset >= 0) {
159 subnode = fdt_first_subnode(old_blob, nodeoffset);
160 while (subnode >= 0) {
161 const char *name = fdt_get_name(old_blob,
166 /* only handle optee reservations */
167 if (strncmp(name, "optee", 5))
170 /* check if this subnode has a reg property */
171 ret = fdt_get_resource(old_blob, subnode, "reg", 0,
174 struct fdt_memory carveout = {
178 char *oldname, *nodename, *tmp;
180 oldname = strdup(name);
185 nodename = strsep(&tmp, "@");
191 ret = fdtdec_add_reserved_memory(new_blob,
201 subnode = fdt_next_subnode(old_blob, subnode);