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" \
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 static int optee_verify_image(struct optee_header *hdr, unsigned long image_len)
26 uint32_t tee_file_size;
28 tee_file_size = hdr->init_size + hdr->paged_size +
29 sizeof(struct optee_header);
31 if (hdr->magic != OPTEE_MAGIC ||
32 hdr->version != OPTEE_VERSION ||
33 tee_file_size != image_len) {
40 int optee_verify_bootm_image(unsigned long image_addr,
41 unsigned long image_load_addr,
42 unsigned long image_len)
44 struct optee_header *hdr = (struct optee_header *)image_addr;
47 ret = optee_verify_image(hdr, image_len);
51 if (image_load_addr + sizeof(*hdr) != hdr->init_load_addr_lo) {
58 printf(optee_hdr_err_msg, hdr, image_addr, hdr->magic,
59 hdr->init_load_addr_lo,
60 hdr->init_load_addr_hi, image_len, hdr->arch, image_load_addr,
61 image_load_addr + image_len);
67 #if defined(CONFIG_OF_LIBFDT)
68 static int optee_copy_firmware_node(ofnode node, void *fdt_blob)
73 offs = fdt_path_offset(fdt_blob, "/firmware");
75 offs = fdt_path_offset(fdt_blob, "/");
79 offs = fdt_add_subnode(fdt_blob, offs, "firmware");
84 offs = fdt_add_subnode(fdt_blob, offs, "optee");
88 /* copy the compatible property */
89 prop = ofnode_get_property(node, "compatible", &len);
91 debug("missing OP-TEE compatible property");
95 ret = fdt_setprop(fdt_blob, offs, "compatible", prop, len);
99 /* copy the method property */
100 prop = ofnode_get_property(node, "method", &len);
102 debug("missing OP-TEE method property");
106 ret = fdt_setprop(fdt_blob, offs, "method", prop, len);
113 int optee_copy_fdt_nodes(void *new_blob)
115 ofnode node, subnode;
119 if (fdt_check_header(new_blob))
122 /* only proceed if there is an /firmware/optee node */
123 node = ofnode_path("/firmware/optee");
124 if (!ofnode_valid(node)) {
125 debug("No OP-TEE firmware node in old fdt, nothing to do");
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.
134 if (fdt_path_offset(new_blob, "/firmware/optee") >= 0) {
135 debug("OP-TEE Device Tree node already exists in target");
139 ret = optee_copy_firmware_node(node, new_blob);
141 printf("Failed to add OP-TEE firmware node\n");
145 /* optee inserts its memory regions as reserved-memory nodes */
146 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);
153 /* only handle optee reservations */
154 if (strncmp(name, "optee", 5))
157 /* check if this subnode has a reg property */
158 ret = ofnode_read_resource(subnode, 0, &res);
160 struct fdt_memory carveout = {
164 char *oldname, *nodename, *tmp;
166 oldname = strdup(name);
171 nodename = strsep(&tmp, "@");
177 ret = fdtdec_add_reserved_memory(new_blob,