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