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