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:" \ |
Alexandru Gagniuc | 26fc667 | 2021-09-07 12:07:06 -0500 | [diff] [blame] | 19 | "\n\thdr=%p image=0x%08lx magic=0x%08x" \ |
Bryan O'Donoghue | 6ffc420 | 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 | 51827f9 | 2021-09-02 11:56:16 +0200 | [diff] [blame] | 23 | #if defined(CONFIG_OPTEE_IMAGE) |
Alexandru Gagniuc | 26fc667 | 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 | 32ce617 | 2018-03-13 16:50:27 +0000 | [diff] [blame] | 25 | { |
Bryan O'Donoghue | 32ce617 | 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 | 26fc667 | 2021-09-07 12:07:06 -0500 | [diff] [blame] | 33 | tee_file_size != image_len) { |
Bryan O'Donoghue | 32ce617 | 2018-03-13 16:50:27 +0000 | [diff] [blame] | 34 | return -EINVAL; |
| 35 | } |
| 36 | |
| 37 | return 0; |
| 38 | } |
Bryan O'Donoghue | c5a6e8b | 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 | c5a6e8b | 2018-03-13 16:50:33 +0000 | [diff] [blame] | 45 | int ret; |
| 46 | |
Alexandru Gagniuc | 26fc667 | 2021-09-07 12:07:06 -0500 | [diff] [blame] | 47 | ret = optee_verify_image(hdr, image_len); |
Bryan O'Donoghue | c5a6e8b | 2018-03-13 16:50:33 +0000 | [diff] [blame] | 48 | if (ret) |
Bryan O'Donoghue | 6ffc420 | 2018-03-13 16:50:34 +0000 | [diff] [blame] | 49 | goto error; |
Bryan O'Donoghue | c5a6e8b | 2018-03-13 16:50:33 +0000 | [diff] [blame] | 50 | |
Bryan O'Donoghue | 6ffc420 | 2018-03-13 16:50:34 +0000 | [diff] [blame] | 51 | if (image_load_addr + sizeof(*hdr) != hdr->init_load_addr_lo) { |
Bryan O'Donoghue | c5a6e8b | 2018-03-13 16:50:33 +0000 | [diff] [blame] | 52 | ret = -EINVAL; |
Bryan O'Donoghue | 6ffc420 | 2018-03-13 16:50:34 +0000 | [diff] [blame] | 53 | goto error; |
| 54 | } |
| 55 | |
| 56 | return ret; |
| 57 | error: |
Alexandru Gagniuc | 26fc667 | 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 | 6ffc420 | 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 | c5a6e8b | 2018-03-13 16:50:33 +0000 | [diff] [blame] | 62 | |
| 63 | return ret; |
| 64 | } |
Patrick Delaunay | 51827f9 | 2021-09-02 11:56:16 +0200 | [diff] [blame] | 65 | #endif |
Heiko Stuebner | 6ccb05e | 2019-10-23 16:46:40 +0200 | [diff] [blame] | 66 | |
| 67 | #if defined(CONFIG_OF_LIBFDT) |
Patrick Delaunay | a253524 | 2021-02-08 13:54:31 +0100 | [diff] [blame] | 68 | static int optee_copy_firmware_node(ofnode node, void *fdt_blob) |
Heiko Stuebner | 6ccb05e | 2019-10-23 16:46:40 +0200 | [diff] [blame] | 69 | { |
Patrick Delaunay | a253524 | 2021-02-08 13:54:31 +0100 | [diff] [blame] | 70 | int offs, ret, len; |
Heiko Stuebner | 6ccb05e | 2019-10-23 16:46:40 +0200 | [diff] [blame] | 71 | const void *prop; |
| 72 | |
Heiko Stuebner | 6ccb05e | 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 | 0f97e92 | 2020-01-26 23:20:54 +0100 | [diff] [blame] | 86 | return offs; |
Heiko Stuebner | 6ccb05e | 2019-10-23 16:46:40 +0200 | [diff] [blame] | 87 | |
| 88 | /* copy the compatible property */ |
Patrick Delaunay | a253524 | 2021-02-08 13:54:31 +0100 | [diff] [blame] | 89 | prop = ofnode_get_property(node, "compatible", &len); |
Heiko Stuebner | 6ccb05e | 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 | a253524 | 2021-02-08 13:54:31 +0100 | [diff] [blame] | 100 | prop = ofnode_get_property(node, "method", &len); |
Heiko Stuebner | 6ccb05e | 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 | a253524 | 2021-02-08 13:54:31 +0100 | [diff] [blame] | 113 | int optee_copy_fdt_nodes(void *new_blob) |
Heiko Stuebner | 6ccb05e | 2019-10-23 16:46:40 +0200 | [diff] [blame] | 114 | { |
Patrick Delaunay | a253524 | 2021-02-08 13:54:31 +0100 | [diff] [blame] | 115 | ofnode node, subnode; |
| 116 | int ret; |
| 117 | struct resource res; |
Heiko Stuebner | 6ccb05e | 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 | a253524 | 2021-02-08 13:54:31 +0100 | [diff] [blame] | 123 | node = ofnode_path("/firmware/optee"); |
| 124 | if (!ofnode_valid(node)) { |
Heiko Stuebner | 6ccb05e | 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 | a253524 | 2021-02-08 13:54:31 +0100 | [diff] [blame] | 139 | ret = optee_copy_firmware_node(node, new_blob); |
Heiko Stuebner | 6ccb05e | 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 | a253524 | 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 | 6ccb05e | 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 | a253524 | 2021-02-08 13:54:31 +0100 | [diff] [blame] | 158 | ret = ofnode_read_resource(subnode, 0, &res); |
Heiko Stuebner | 6ccb05e | 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 | }; |
Thierry Reding | b9aad37 | 2021-09-03 15:16:21 +0200 | [diff] [blame] | 164 | unsigned long flags = FDTDEC_RESERVED_MEMORY_NO_MAP; |
Heiko Stuebner | 6ccb05e | 2019-10-23 16:46:40 +0200 | [diff] [blame] | 165 | char *oldname, *nodename, *tmp; |
| 166 | |
| 167 | oldname = strdup(name); |
| 168 | if (!oldname) |
| 169 | return -ENOMEM; |
| 170 | |
| 171 | tmp = oldname; |
| 172 | nodename = strsep(&tmp, "@"); |
| 173 | if (!nodename) { |
| 174 | free(oldname); |
| 175 | return -EINVAL; |
| 176 | } |
| 177 | |
| 178 | ret = fdtdec_add_reserved_memory(new_blob, |
| 179 | nodename, |
| 180 | &carveout, |
Thierry Reding | 46cb067 | 2021-09-03 15:16:19 +0200 | [diff] [blame] | 181 | NULL, 0, |
Thierry Reding | b9aad37 | 2021-09-03 15:16:21 +0200 | [diff] [blame] | 182 | NULL, flags); |
Heiko Stuebner | 6ccb05e | 2019-10-23 16:46:40 +0200 | [diff] [blame] | 183 | free(oldname); |
| 184 | |
| 185 | if (ret < 0) |
| 186 | return ret; |
| 187 | } |
Heiko Stuebner | 6ccb05e | 2019-10-23 16:46:40 +0200 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
| 191 | return 0; |
| 192 | } |
| 193 | #endif |