Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Bryan O'Donoghue | 32ce617 | 2018-03-13 16:50:27 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2017 Linaro |
| 4 | * Bryan O'Donoghue <bryan.odonoghue@linaro.org> |
Bryan O'Donoghue | 32ce617 | 2018-03-13 16:50:27 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 8 | #include <fdtdec.h> |
Simon Glass | 8e8ccfe | 2019-12-28 10:45:03 -0700 | [diff] [blame] | 9 | #include <image.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Heiko Stuebner | 6ccb05e | 2019-10-23 16:46:40 +0200 | [diff] [blame] | 11 | #include <malloc.h> |
Patrick Delaunay | a253524 | 2021-02-08 13:54:31 +0100 | [diff] [blame] | 12 | #include <dm/ofnode.h> |
| 13 | #include <linux/ioport.h> |
Heiko Stuebner | 6ccb05e | 2019-10-23 16:46:40 +0200 | [diff] [blame] | 14 | #include <linux/libfdt.h> |
Bryan O'Donoghue | 32ce617 | 2018-03-13 16:50:27 +0000 | [diff] [blame] | 15 | #include <tee/optee.h> |
| 16 | |
Bryan O'Donoghue | 6ffc420 | 2018-03-13 16:50:34 +0000 | [diff] [blame] | 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" |
| 22 | |
Bryan O'Donoghue | 32ce617 | 2018-03-13 16:50:27 +0000 | [diff] [blame] | 23 | int optee_verify_image(struct optee_header *hdr, unsigned long tzdram_start, |
| 24 | unsigned long tzdram_len, unsigned long image_len) |
| 25 | { |
| 26 | unsigned long tzdram_end = tzdram_start + tzdram_len; |
| 27 | uint32_t tee_file_size; |
| 28 | |
| 29 | tee_file_size = hdr->init_size + hdr->paged_size + |
| 30 | sizeof(struct optee_header); |
| 31 | |
| 32 | if (hdr->magic != OPTEE_MAGIC || |
| 33 | hdr->version != OPTEE_VERSION || |
| 34 | hdr->init_load_addr_hi > tzdram_end || |
| 35 | hdr->init_load_addr_lo < tzdram_start || |
| 36 | tee_file_size > tzdram_len || |
| 37 | tee_file_size != image_len || |
| 38 | (hdr->init_load_addr_lo + tee_file_size) > tzdram_end) { |
| 39 | return -EINVAL; |
| 40 | } |
| 41 | |
| 42 | return 0; |
| 43 | } |
Bryan O'Donoghue | c5a6e8b | 2018-03-13 16:50:33 +0000 | [diff] [blame] | 44 | |
| 45 | int optee_verify_bootm_image(unsigned long image_addr, |
| 46 | unsigned long image_load_addr, |
| 47 | unsigned long image_len) |
| 48 | { |
| 49 | struct optee_header *hdr = (struct optee_header *)image_addr; |
| 50 | unsigned long tzdram_start = CONFIG_OPTEE_TZDRAM_BASE; |
| 51 | unsigned long tzdram_len = CONFIG_OPTEE_TZDRAM_SIZE; |
| 52 | |
| 53 | int ret; |
| 54 | |
| 55 | ret = optee_verify_image(hdr, tzdram_start, tzdram_len, image_len); |
| 56 | if (ret) |
Bryan O'Donoghue | 6ffc420 | 2018-03-13 16:50:34 +0000 | [diff] [blame] | 57 | goto error; |
Bryan O'Donoghue | c5a6e8b | 2018-03-13 16:50:33 +0000 | [diff] [blame] | 58 | |
Bryan O'Donoghue | 6ffc420 | 2018-03-13 16:50:34 +0000 | [diff] [blame] | 59 | if (image_load_addr + sizeof(*hdr) != hdr->init_load_addr_lo) { |
Bryan O'Donoghue | c5a6e8b | 2018-03-13 16:50:33 +0000 | [diff] [blame] | 60 | ret = -EINVAL; |
Bryan O'Donoghue | 6ffc420 | 2018-03-13 16:50:34 +0000 | [diff] [blame] | 61 | goto error; |
| 62 | } |
| 63 | |
| 64 | return ret; |
| 65 | error: |
| 66 | printf(optee_hdr_err_msg, hdr, image_addr, hdr->magic, tzdram_start, |
| 67 | tzdram_start + tzdram_len, hdr->init_load_addr_lo, |
| 68 | hdr->init_load_addr_hi, image_len, hdr->arch, image_load_addr, |
| 69 | image_load_addr + image_len); |
Bryan O'Donoghue | c5a6e8b | 2018-03-13 16:50:33 +0000 | [diff] [blame] | 70 | |
| 71 | return ret; |
| 72 | } |
Heiko Stuebner | 6ccb05e | 2019-10-23 16:46:40 +0200 | [diff] [blame] | 73 | |
| 74 | #if defined(CONFIG_OF_LIBFDT) |
Patrick Delaunay | a253524 | 2021-02-08 13:54:31 +0100 | [diff] [blame] | 75 | static int optee_copy_firmware_node(ofnode node, void *fdt_blob) |
Heiko Stuebner | 6ccb05e | 2019-10-23 16:46:40 +0200 | [diff] [blame] | 76 | { |
Patrick Delaunay | a253524 | 2021-02-08 13:54:31 +0100 | [diff] [blame] | 77 | int offs, ret, len; |
Heiko Stuebner | 6ccb05e | 2019-10-23 16:46:40 +0200 | [diff] [blame] | 78 | const void *prop; |
| 79 | |
Heiko Stuebner | 6ccb05e | 2019-10-23 16:46:40 +0200 | [diff] [blame] | 80 | offs = fdt_path_offset(fdt_blob, "/firmware"); |
| 81 | if (offs < 0) { |
| 82 | offs = fdt_path_offset(fdt_blob, "/"); |
| 83 | if (offs < 0) |
| 84 | return offs; |
| 85 | |
| 86 | offs = fdt_add_subnode(fdt_blob, offs, "firmware"); |
| 87 | if (offs < 0) |
| 88 | return offs; |
| 89 | } |
| 90 | |
| 91 | offs = fdt_add_subnode(fdt_blob, offs, "optee"); |
| 92 | if (offs < 0) |
Christoph Müllner | 0f97e92 | 2020-01-26 23:20:54 +0100 | [diff] [blame] | 93 | return offs; |
Heiko Stuebner | 6ccb05e | 2019-10-23 16:46:40 +0200 | [diff] [blame] | 94 | |
| 95 | /* copy the compatible property */ |
Patrick Delaunay | a253524 | 2021-02-08 13:54:31 +0100 | [diff] [blame] | 96 | prop = ofnode_get_property(node, "compatible", &len); |
Heiko Stuebner | 6ccb05e | 2019-10-23 16:46:40 +0200 | [diff] [blame] | 97 | if (!prop) { |
| 98 | debug("missing OP-TEE compatible property"); |
| 99 | return -EINVAL; |
| 100 | } |
| 101 | |
| 102 | ret = fdt_setprop(fdt_blob, offs, "compatible", prop, len); |
| 103 | if (ret < 0) |
| 104 | return ret; |
| 105 | |
| 106 | /* copy the method property */ |
Patrick Delaunay | a253524 | 2021-02-08 13:54:31 +0100 | [diff] [blame] | 107 | prop = ofnode_get_property(node, "method", &len); |
Heiko Stuebner | 6ccb05e | 2019-10-23 16:46:40 +0200 | [diff] [blame] | 108 | if (!prop) { |
| 109 | debug("missing OP-TEE method property"); |
| 110 | return -EINVAL; |
| 111 | } |
| 112 | |
| 113 | ret = fdt_setprop(fdt_blob, offs, "method", prop, len); |
| 114 | if (ret < 0) |
| 115 | return ret; |
| 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |
Patrick Delaunay | a253524 | 2021-02-08 13:54:31 +0100 | [diff] [blame] | 120 | int optee_copy_fdt_nodes(void *new_blob) |
Heiko Stuebner | 6ccb05e | 2019-10-23 16:46:40 +0200 | [diff] [blame] | 121 | { |
Patrick Delaunay | a253524 | 2021-02-08 13:54:31 +0100 | [diff] [blame] | 122 | ofnode node, subnode; |
| 123 | int ret; |
| 124 | struct resource res; |
Heiko Stuebner | 6ccb05e | 2019-10-23 16:46:40 +0200 | [diff] [blame] | 125 | |
| 126 | if (fdt_check_header(new_blob)) |
| 127 | return -EINVAL; |
| 128 | |
| 129 | /* only proceed if there is an /firmware/optee node */ |
Patrick Delaunay | a253524 | 2021-02-08 13:54:31 +0100 | [diff] [blame] | 130 | node = ofnode_path("/firmware/optee"); |
| 131 | if (!ofnode_valid(node)) { |
Heiko Stuebner | 6ccb05e | 2019-10-23 16:46:40 +0200 | [diff] [blame] | 132 | debug("No OP-TEE firmware node in old fdt, nothing to do"); |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | /* |
| 137 | * Do not proceed if the target dt already has an OP-TEE node. |
| 138 | * In this case assume that the system knows better somehow, |
| 139 | * so do not interfere. |
| 140 | */ |
| 141 | if (fdt_path_offset(new_blob, "/firmware/optee") >= 0) { |
| 142 | debug("OP-TEE Device Tree node already exists in target"); |
| 143 | return 0; |
| 144 | } |
| 145 | |
Patrick Delaunay | a253524 | 2021-02-08 13:54:31 +0100 | [diff] [blame] | 146 | ret = optee_copy_firmware_node(node, new_blob); |
Heiko Stuebner | 6ccb05e | 2019-10-23 16:46:40 +0200 | [diff] [blame] | 147 | if (ret < 0) { |
| 148 | printf("Failed to add OP-TEE firmware node\n"); |
| 149 | return ret; |
| 150 | } |
| 151 | |
| 152 | /* optee inserts its memory regions as reserved-memory nodes */ |
Patrick Delaunay | a253524 | 2021-02-08 13:54:31 +0100 | [diff] [blame] | 153 | node = ofnode_path("/reserved-memory"); |
| 154 | if (ofnode_valid(node)) { |
| 155 | ofnode_for_each_subnode(subnode, node) { |
| 156 | const char *name = ofnode_get_name(subnode); |
Heiko Stuebner | 6ccb05e | 2019-10-23 16:46:40 +0200 | [diff] [blame] | 157 | if (!name) |
| 158 | return -EINVAL; |
| 159 | |
| 160 | /* only handle optee reservations */ |
| 161 | if (strncmp(name, "optee", 5)) |
| 162 | continue; |
| 163 | |
| 164 | /* check if this subnode has a reg property */ |
Patrick Delaunay | a253524 | 2021-02-08 13:54:31 +0100 | [diff] [blame] | 165 | ret = ofnode_read_resource(subnode, 0, &res); |
Heiko Stuebner | 6ccb05e | 2019-10-23 16:46:40 +0200 | [diff] [blame] | 166 | if (!ret) { |
| 167 | struct fdt_memory carveout = { |
| 168 | .start = res.start, |
| 169 | .end = res.end, |
| 170 | }; |
| 171 | char *oldname, *nodename, *tmp; |
| 172 | |
| 173 | oldname = strdup(name); |
| 174 | if (!oldname) |
| 175 | return -ENOMEM; |
| 176 | |
| 177 | tmp = oldname; |
| 178 | nodename = strsep(&tmp, "@"); |
| 179 | if (!nodename) { |
| 180 | free(oldname); |
| 181 | return -EINVAL; |
| 182 | } |
| 183 | |
| 184 | ret = fdtdec_add_reserved_memory(new_blob, |
| 185 | nodename, |
| 186 | &carveout, |
Etienne Carriere | 3e15c31 | 2020-09-10 10:50:01 +0200 | [diff] [blame] | 187 | NULL, true); |
Heiko Stuebner | 6ccb05e | 2019-10-23 16:46:40 +0200 | [diff] [blame] | 188 | free(oldname); |
| 189 | |
| 190 | if (ret < 0) |
| 191 | return ret; |
| 192 | } |
Heiko Stuebner | 6ccb05e | 2019-10-23 16:46:40 +0200 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | #endif |