1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2017 Linaro
4 * Bryan O'Donoghue <bryan.odonoghue@linaro.org>
12 #include <dm/ofnode.h>
13 #include <linux/ioport.h>
14 #include <linux/libfdt.h>
15 #include <tee/optee.h>
17 #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"
23 #if defined(CONFIG_OPTEE_IMAGE)
24 int optee_verify_image(struct optee_header *hdr, unsigned long tzdram_start,
25 unsigned long tzdram_len, unsigned long image_len)
27 unsigned long tzdram_end = tzdram_start + tzdram_len;
28 uint32_t tee_file_size;
30 tee_file_size = hdr->init_size + hdr->paged_size +
31 sizeof(struct optee_header);
33 if (hdr->magic != OPTEE_MAGIC ||
34 hdr->version != OPTEE_VERSION ||
35 hdr->init_load_addr_hi > tzdram_end ||
36 hdr->init_load_addr_lo < tzdram_start ||
37 tee_file_size > tzdram_len ||
38 tee_file_size != image_len ||
39 (hdr->init_load_addr_lo + tee_file_size) > tzdram_end) {
46 int optee_verify_bootm_image(unsigned long image_addr,
47 unsigned long image_load_addr,
48 unsigned long image_len)
50 struct optee_header *hdr = (struct optee_header *)image_addr;
51 unsigned long tzdram_start = CONFIG_OPTEE_TZDRAM_BASE;
52 unsigned long tzdram_len = CONFIG_OPTEE_TZDRAM_SIZE;
56 ret = optee_verify_image(hdr, tzdram_start, tzdram_len, image_len);
60 if (image_load_addr + sizeof(*hdr) != hdr->init_load_addr_lo) {
67 printf(optee_hdr_err_msg, hdr, image_addr, hdr->magic, tzdram_start,
68 tzdram_start + tzdram_len, hdr->init_load_addr_lo,
69 hdr->init_load_addr_hi, image_len, hdr->arch, image_load_addr,
70 image_load_addr + image_len);
76 #if defined(CONFIG_OF_LIBFDT)
77 static int optee_copy_firmware_node(ofnode node, void *fdt_blob)
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 = ofnode_get_property(node, "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 = ofnode_get_property(node, "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(void *new_blob)
124 ofnode node, subnode;
128 if (fdt_check_header(new_blob))
131 /* only proceed if there is an /firmware/optee node */
132 node = ofnode_path("/firmware/optee");
133 if (!ofnode_valid(node)) {
134 debug("No OP-TEE firmware node in old fdt, nothing to do");
139 * Do not proceed if the target dt already has an OP-TEE node.
140 * In this case assume that the system knows better somehow,
141 * so do not interfere.
143 if (fdt_path_offset(new_blob, "/firmware/optee") >= 0) {
144 debug("OP-TEE Device Tree node already exists in target");
148 ret = optee_copy_firmware_node(node, new_blob);
150 printf("Failed to add OP-TEE firmware node\n");
154 /* optee inserts its memory regions as reserved-memory nodes */
155 node = ofnode_path("/reserved-memory");
156 if (ofnode_valid(node)) {
157 ofnode_for_each_subnode(subnode, node) {
158 const char *name = ofnode_get_name(subnode);
162 /* only handle optee reservations */
163 if (strncmp(name, "optee", 5))
166 /* check if this subnode has a reg property */
167 ret = ofnode_read_resource(subnode, 0, &res);
169 struct fdt_memory carveout = {
173 char *oldname, *nodename, *tmp;
175 oldname = strdup(name);
180 nodename = strsep(&tmp, "@");
186 ret = fdtdec_add_reserved_memory(new_blob,