Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013, Google Inc. |
| 4 | * |
| 5 | * (C) Copyright 2008 Semihalf |
| 6 | * |
| 7 | * (C) Copyright 2000-2006 |
| 8 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include "mkimage.h" |
Simon Glass | a51991d | 2014-06-12 07:24:53 -0600 | [diff] [blame] | 12 | #include <bootm.h> |
Masahiro Yamada | 6dd1052 | 2020-04-16 18:30:18 +0900 | [diff] [blame] | 13 | #include <fdt_region.h> |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 14 | #include <image.h> |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 15 | #include <version.h> |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 16 | |
Philippe Reynes | 3e3899c | 2022-03-28 22:57:02 +0200 | [diff] [blame] | 17 | #include <openssl/pem.h> |
| 18 | #include <openssl/evp.h> |
| 19 | |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 20 | /** |
Simon Glass | 0ce6e3d | 2013-05-07 06:11:56 +0000 | [diff] [blame] | 21 | * fit_set_hash_value - set hash value in requested has node |
| 22 | * @fit: pointer to the FIT format image header |
| 23 | * @noffset: hash node offset |
| 24 | * @value: hash value to be set |
| 25 | * @value_len: hash value length |
| 26 | * |
| 27 | * fit_set_hash_value() attempts to set hash value in a node at offset |
| 28 | * given and returns operation status to the caller. |
| 29 | * |
| 30 | * returns |
| 31 | * 0, on success |
| 32 | * -1, on failure |
| 33 | */ |
| 34 | static int fit_set_hash_value(void *fit, int noffset, uint8_t *value, |
| 35 | int value_len) |
| 36 | { |
| 37 | int ret; |
| 38 | |
| 39 | ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len); |
| 40 | if (ret) { |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 41 | fprintf(stderr, "Can't set hash '%s' property for '%s' node(%s)\n", |
| 42 | FIT_VALUE_PROP, fit_get_name(fit, noffset, NULL), |
| 43 | fdt_strerror(ret)); |
Simon Glass | ec539f7 | 2016-07-03 09:40:44 -0600 | [diff] [blame] | 44 | return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -EIO; |
Simon Glass | 0ce6e3d | 2013-05-07 06:11:56 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | /** |
Simon Glass | 25d6e6b | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 51 | * fit_image_process_hash - Process a single subnode of the images/ node |
| 52 | * |
| 53 | * Check each subnode and process accordingly. For hash nodes we generate |
Simon Glass | 6a0efc8 | 2021-11-12 12:28:06 -0700 | [diff] [blame] | 54 | * a hash of the supplied data and store it in the node. |
Simon Glass | 25d6e6b | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 55 | * |
| 56 | * @fit: pointer to the FIT format image header |
Simon Glass | 6a0efc8 | 2021-11-12 12:28:06 -0700 | [diff] [blame] | 57 | * @image_name: name of image being processed (used to display errors) |
Simon Glass | 25d6e6b | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 58 | * @noffset: subnode offset |
| 59 | * @data: data to process |
| 60 | * @size: size of data in bytes |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 61 | * Return: 0 if ok, -1 on error |
Simon Glass | 25d6e6b | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 62 | */ |
| 63 | static int fit_image_process_hash(void *fit, const char *image_name, |
| 64 | int noffset, const void *data, size_t size) |
| 65 | { |
| 66 | uint8_t value[FIT_MAX_HASH_LEN]; |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 67 | const char *node_name; |
Simon Glass | 25d6e6b | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 68 | int value_len; |
Jan Kiszka | 27beec2 | 2022-01-14 10:21:17 +0100 | [diff] [blame] | 69 | const char *algo; |
Simon Glass | ec539f7 | 2016-07-03 09:40:44 -0600 | [diff] [blame] | 70 | int ret; |
Simon Glass | 25d6e6b | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 71 | |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 72 | node_name = fit_get_name(fit, noffset, NULL); |
Simon Glass | 25d6e6b | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 73 | |
| 74 | if (fit_image_hash_get_algo(fit, noffset, &algo)) { |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 75 | fprintf(stderr, |
| 76 | "Can't get hash algo property for '%s' hash node in '%s' image node\n", |
| 77 | node_name, image_name); |
Simon Glass | ec539f7 | 2016-07-03 09:40:44 -0600 | [diff] [blame] | 78 | return -ENOENT; |
Simon Glass | 25d6e6b | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | if (calculate_hash(data, size, algo, value, &value_len)) { |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 82 | fprintf(stderr, |
| 83 | "Unsupported hash algorithm (%s) for '%s' hash node in '%s' image node\n", |
| 84 | algo, node_name, image_name); |
Simon Glass | ec539f7 | 2016-07-03 09:40:44 -0600 | [diff] [blame] | 85 | return -EPROTONOSUPPORT; |
Simon Glass | 25d6e6b | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Simon Glass | ec539f7 | 2016-07-03 09:40:44 -0600 | [diff] [blame] | 88 | ret = fit_set_hash_value(fit, noffset, value, value_len); |
| 89 | if (ret) { |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 90 | fprintf(stderr, "Can't set hash value for '%s' hash node in '%s' image node\n", |
| 91 | node_name, image_name); |
Simon Glass | ec539f7 | 2016-07-03 09:40:44 -0600 | [diff] [blame] | 92 | return ret; |
Simon Glass | 25d6e6b | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | /** |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 99 | * fit_image_write_sig() - write the signature to a FIT |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 100 | * |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 101 | * This writes the signature and signer data to the FIT. |
| 102 | * |
| 103 | * @fit: pointer to the FIT format image header |
| 104 | * @noffset: hash node offset |
| 105 | * @value: signature value to be set |
| 106 | * @value_len: signature value length |
| 107 | * @comment: Text comment to write (NULL for none) |
| 108 | * |
| 109 | * returns |
| 110 | * 0, on success |
| 111 | * -FDT_ERR_..., on failure |
| 112 | */ |
| 113 | static int fit_image_write_sig(void *fit, int noffset, uint8_t *value, |
| 114 | int value_len, const char *comment, const char *region_prop, |
Jan Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 115 | int region_proplen, const char *cmdname, const char *algo_name) |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 116 | { |
| 117 | int string_size; |
| 118 | int ret; |
| 119 | |
| 120 | /* |
| 121 | * Get the current string size, before we update the FIT and add |
| 122 | * more |
| 123 | */ |
| 124 | string_size = fdt_size_dt_strings(fit); |
| 125 | |
| 126 | ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len); |
| 127 | if (!ret) { |
| 128 | ret = fdt_setprop_string(fit, noffset, "signer-name", |
| 129 | "mkimage"); |
| 130 | } |
| 131 | if (!ret) { |
| 132 | ret = fdt_setprop_string(fit, noffset, "signer-version", |
| 133 | PLAIN_VERSION); |
| 134 | } |
| 135 | if (comment && !ret) |
| 136 | ret = fdt_setprop_string(fit, noffset, "comment", comment); |
Alex Kiernan | 697fcdc | 2018-06-20 20:10:52 +0000 | [diff] [blame] | 137 | if (!ret) { |
| 138 | time_t timestamp = imagetool_get_source_date(cmdname, |
| 139 | time(NULL)); |
Ming Liu | f75f1d7 | 2021-05-31 09:04:51 +0200 | [diff] [blame] | 140 | uint32_t t = cpu_to_uimage(timestamp); |
Alex Kiernan | 697fcdc | 2018-06-20 20:10:52 +0000 | [diff] [blame] | 141 | |
Ming Liu | f75f1d7 | 2021-05-31 09:04:51 +0200 | [diff] [blame] | 142 | ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t, |
| 143 | sizeof(uint32_t)); |
Alex Kiernan | 697fcdc | 2018-06-20 20:10:52 +0000 | [diff] [blame] | 144 | } |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 145 | if (region_prop && !ret) { |
| 146 | uint32_t strdata[2]; |
| 147 | |
| 148 | ret = fdt_setprop(fit, noffset, "hashed-nodes", |
| 149 | region_prop, region_proplen); |
Teddy Reed | 9767bf4 | 2018-06-09 11:45:20 -0400 | [diff] [blame] | 150 | /* This is a legacy offset, it is unused, and must remain 0. */ |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 151 | strdata[0] = 0; |
| 152 | strdata[1] = cpu_to_fdt32(string_size); |
| 153 | if (!ret) { |
| 154 | ret = fdt_setprop(fit, noffset, "hashed-strings", |
| 155 | strdata, sizeof(strdata)); |
| 156 | } |
| 157 | } |
Jan Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 158 | if (algo_name && !ret) |
| 159 | ret = fdt_setprop_string(fit, noffset, "algo", algo_name); |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 160 | |
| 161 | return ret; |
| 162 | } |
| 163 | |
| 164 | static int fit_image_setup_sig(struct image_sign_info *info, |
Alexandru Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 165 | const char *keydir, const char *keyfile, void *fit, |
| 166 | const char *image_name, int noffset, const char *require_keys, |
Jan Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 167 | const char *engine_id, const char *algo_name) |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 168 | { |
| 169 | const char *node_name; |
Philippe Reynes | 1246835 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 170 | const char *padding_name; |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 171 | |
| 172 | node_name = fit_get_name(fit, noffset, NULL); |
Jan Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 173 | if (!algo_name) { |
| 174 | if (fit_image_hash_get_algo(fit, noffset, &algo_name)) { |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 175 | fprintf(stderr, |
| 176 | "Can't get algo property for '%s' signature node in '%s' image node\n", |
| 177 | node_name, image_name); |
Jan Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 178 | return -1; |
| 179 | } |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Philippe Reynes | 1246835 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 182 | padding_name = fdt_getprop(fit, noffset, "padding", NULL); |
| 183 | |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 184 | memset(info, '\0', sizeof(*info)); |
| 185 | info->keydir = keydir; |
Alexandru Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 186 | info->keyfile = keyfile; |
Simon Glass | d7aabcc | 2020-03-18 11:44:06 -0600 | [diff] [blame] | 187 | info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL); |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 188 | info->fit = fit; |
| 189 | info->node_offset = noffset; |
Masahiro Yamada | c1f137b | 2017-10-27 13:25:21 +0900 | [diff] [blame] | 190 | info->name = strdup(algo_name); |
Andrew Duda | 6616c82 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 191 | info->checksum = image_get_checksum_algo(algo_name); |
| 192 | info->crypto = image_get_crypto_algo(algo_name); |
Philippe Reynes | 1246835 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 193 | info->padding = image_get_padding_algo(padding_name); |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 194 | info->require_keys = require_keys; |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 195 | info->engine_id = engine_id; |
Andrew Duda | 6616c82 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 196 | if (!info->checksum || !info->crypto) { |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 197 | fprintf(stderr, |
| 198 | "Unsupported signature algorithm (%s) for '%s' signature node in '%s' image node\n", |
| 199 | algo_name, node_name, image_name); |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 200 | return -1; |
| 201 | } |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * fit_image_process_sig- Process a single subnode of the images/ node |
| 208 | * |
| 209 | * Check each subnode and process accordingly. For signature nodes we |
Simon Glass | 6a0efc8 | 2021-11-12 12:28:06 -0700 | [diff] [blame] | 210 | * generate a signed hash of the supplied data and store it in the node. |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 211 | * |
| 212 | * @keydir: Directory containing keys to use for signing |
Simon Glass | 6a0efc8 | 2021-11-12 12:28:06 -0700 | [diff] [blame] | 213 | * @keydest: Destination FDT blob to write public keys into (NULL if none) |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 214 | * @fit: pointer to the FIT format image header |
Simon Glass | 6a0efc8 | 2021-11-12 12:28:06 -0700 | [diff] [blame] | 215 | * @image_name: name of image being processed (used to display errors) |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 216 | * @noffset: subnode offset |
| 217 | * @data: data to process |
| 218 | * @size: size of data in bytes |
| 219 | * @comment: Comment to add to signature nodes |
| 220 | * @require_keys: Mark all keys as 'required' |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 221 | * @engine_id: Engine to use for signing |
Simon Glass | 89c3fb6 | 2021-11-12 12:28:12 -0700 | [diff] [blame] | 222 | * Return: keydest node if @keydest is non-NULL, else 0 if none; -ve error code |
| 223 | * on failure |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 224 | */ |
Alexandru Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 225 | static int fit_image_process_sig(const char *keydir, const char *keyfile, |
| 226 | void *keydest, void *fit, const char *image_name, |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 227 | int noffset, const void *data, size_t size, |
Alex Kiernan | 697fcdc | 2018-06-20 20:10:52 +0000 | [diff] [blame] | 228 | const char *comment, int require_keys, const char *engine_id, |
Jan Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 229 | const char *cmdname, const char *algo_name) |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 230 | { |
| 231 | struct image_sign_info info; |
| 232 | struct image_region region; |
| 233 | const char *node_name; |
| 234 | uint8_t *value; |
| 235 | uint value_len; |
| 236 | int ret; |
| 237 | |
Alexandru Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 238 | if (fit_image_setup_sig(&info, keydir, keyfile, fit, image_name, |
| 239 | noffset, require_keys ? "image" : NULL, |
Jan Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 240 | engine_id, algo_name)) |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 241 | return -1; |
| 242 | |
| 243 | node_name = fit_get_name(fit, noffset, NULL); |
| 244 | region.data = data; |
| 245 | region.size = size; |
Andrew Duda | 6616c82 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 246 | ret = info.crypto->sign(&info, ®ion, 1, &value, &value_len); |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 247 | if (ret) { |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 248 | fprintf(stderr, "Failed to sign '%s' signature node in '%s' image node: %d\n", |
| 249 | node_name, image_name, ret); |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 250 | |
| 251 | /* We allow keys to be missing */ |
| 252 | if (ret == -ENOENT) |
| 253 | return 0; |
| 254 | return -1; |
| 255 | } |
| 256 | |
| 257 | ret = fit_image_write_sig(fit, noffset, value, value_len, comment, |
Jan Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 258 | NULL, 0, cmdname, algo_name); |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 259 | if (ret) { |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 260 | if (ret == -FDT_ERR_NOSPACE) |
| 261 | return -ENOSPC; |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 262 | fprintf(stderr, |
| 263 | "Can't write signature for '%s' signature node in '%s' conf node: %s\n", |
| 264 | node_name, image_name, fdt_strerror(ret)); |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 265 | return -1; |
| 266 | } |
| 267 | free(value); |
| 268 | |
| 269 | /* Get keyname again, as FDT has changed and invalidated our pointer */ |
Simon Glass | d7aabcc | 2020-03-18 11:44:06 -0600 | [diff] [blame] | 270 | info.keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL); |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 271 | |
mario.six@gdsys.cc | dc4053a | 2016-07-22 08:58:40 +0200 | [diff] [blame] | 272 | /* |
| 273 | * Write the public key into the supplied FDT file; this might fail |
mario.six@gdsys.cc | d019e15 | 2016-07-19 11:07:06 +0200 | [diff] [blame] | 274 | * several times, since we try signing with successively increasing |
mario.six@gdsys.cc | dc4053a | 2016-07-22 08:58:40 +0200 | [diff] [blame] | 275 | * size values |
| 276 | */ |
Masahiro Yamada | 0148676 | 2017-10-27 15:04:20 +0900 | [diff] [blame] | 277 | if (keydest) { |
| 278 | ret = info.crypto->add_verify_data(&info, keydest); |
Simon Glass | 94336dc | 2021-11-12 12:28:11 -0700 | [diff] [blame] | 279 | if (ret < 0) { |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 280 | fprintf(stderr, |
| 281 | "Failed to add verification data for '%s' signature node in '%s' image node\n", |
| 282 | node_name, image_name); |
Masahiro Yamada | 0148676 | 2017-10-27 15:04:20 +0900 | [diff] [blame] | 283 | return ret; |
| 284 | } |
Simon Glass | 89c3fb6 | 2021-11-12 12:28:12 -0700 | [diff] [blame] | 285 | /* Return the node that was written to */ |
| 286 | return ret; |
Masahiro Yamada | 0148676 | 2017-10-27 15:04:20 +0900 | [diff] [blame] | 287 | } |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 288 | |
| 289 | return 0; |
| 290 | } |
| 291 | |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 292 | static int fit_image_read_data(char *filename, unsigned char *data, |
| 293 | int expected_size) |
| 294 | { |
| 295 | struct stat sbuf; |
| 296 | int fd, ret = -1; |
| 297 | ssize_t n; |
| 298 | |
| 299 | /* Open file */ |
| 300 | fd = open(filename, O_RDONLY | O_BINARY); |
| 301 | if (fd < 0) { |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 302 | fprintf(stderr, "Can't open file %s (err=%d => %s)\n", |
| 303 | filename, errno, strerror(errno)); |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 304 | return -1; |
| 305 | } |
| 306 | |
| 307 | /* Compute file size */ |
| 308 | if (fstat(fd, &sbuf) < 0) { |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 309 | fprintf(stderr, "Can't fstat file %s (err=%d => %s)\n", |
| 310 | filename, errno, strerror(errno)); |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 311 | goto err; |
| 312 | } |
| 313 | |
| 314 | /* Check file size */ |
| 315 | if (sbuf.st_size != expected_size) { |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 316 | fprintf(stderr, "File %s don't have the expected size (size=%lld, expected=%d)\n", |
| 317 | filename, (long long)sbuf.st_size, expected_size); |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 318 | goto err; |
| 319 | } |
| 320 | |
| 321 | /* Read data */ |
| 322 | n = read(fd, data, sbuf.st_size); |
| 323 | if (n < 0) { |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 324 | fprintf(stderr, "Can't read file %s (err=%d => %s)\n", |
| 325 | filename, errno, strerror(errno)); |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 326 | goto err; |
| 327 | } |
| 328 | |
| 329 | /* Check that we have read all the file */ |
| 330 | if (n != sbuf.st_size) { |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 331 | fprintf(stderr, "Can't read all file %s (read %zd bytes, expected %lld)\n", |
| 332 | filename, n, (long long)sbuf.st_size); |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 333 | goto err; |
| 334 | } |
| 335 | |
| 336 | ret = 0; |
| 337 | |
| 338 | err: |
| 339 | close(fd); |
| 340 | return ret; |
| 341 | } |
| 342 | |
Philippe Reynes | 64d67c4 | 2020-09-17 15:01:46 +0200 | [diff] [blame] | 343 | static int get_random_data(void *data, int size) |
| 344 | { |
| 345 | unsigned char *tmp = data; |
| 346 | struct timespec date; |
Simon Glass | 7a1745c | 2021-05-13 19:39:20 -0600 | [diff] [blame] | 347 | int i, ret; |
Philippe Reynes | 64d67c4 | 2020-09-17 15:01:46 +0200 | [diff] [blame] | 348 | |
| 349 | if (!tmp) { |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 350 | fprintf(stderr, "%s: pointer data is NULL\n", __func__); |
Philippe Reynes | 64d67c4 | 2020-09-17 15:01:46 +0200 | [diff] [blame] | 351 | ret = -1; |
| 352 | goto out; |
| 353 | } |
| 354 | |
| 355 | ret = clock_gettime(CLOCK_MONOTONIC, &date); |
Simon Glass | 7a1745c | 2021-05-13 19:39:20 -0600 | [diff] [blame] | 356 | if (ret) { |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 357 | fprintf(stderr, "%s: clock_gettime has failed (%s)\n", __func__, |
| 358 | strerror(errno)); |
Philippe Reynes | 64d67c4 | 2020-09-17 15:01:46 +0200 | [diff] [blame] | 359 | goto out; |
| 360 | } |
| 361 | |
Philippe Reynes | 95f2ab2 | 2020-11-13 16:37:46 +0100 | [diff] [blame] | 362 | srandom(date.tv_nsec); |
Philippe Reynes | 64d67c4 | 2020-09-17 15:01:46 +0200 | [diff] [blame] | 363 | |
| 364 | for (i = 0; i < size; i++) { |
Philippe Reynes | 95f2ab2 | 2020-11-13 16:37:46 +0100 | [diff] [blame] | 365 | *tmp = random() & 0xff; |
Philippe Reynes | 64d67c4 | 2020-09-17 15:01:46 +0200 | [diff] [blame] | 366 | tmp++; |
| 367 | } |
| 368 | |
| 369 | out: |
| 370 | return ret; |
| 371 | } |
| 372 | |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 373 | static int fit_image_setup_cipher(struct image_cipher_info *info, |
| 374 | const char *keydir, void *fit, |
| 375 | const char *image_name, int image_noffset, |
Patrick Oppenlander | d586fc3 | 2020-07-30 14:22:13 +1000 | [diff] [blame] | 376 | int noffset) |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 377 | { |
| 378 | char *algo_name; |
| 379 | char filename[128]; |
| 380 | int ret = -1; |
| 381 | |
| 382 | if (fit_image_cipher_get_algo(fit, noffset, &algo_name)) { |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 383 | fprintf(stderr, "Can't get algo name for cipher in image '%s'\n", |
| 384 | image_name); |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 385 | goto out; |
| 386 | } |
| 387 | |
| 388 | info->keydir = keydir; |
| 389 | |
| 390 | /* Read the key name */ |
Simon Glass | d7aabcc | 2020-03-18 11:44:06 -0600 | [diff] [blame] | 391 | info->keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL); |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 392 | if (!info->keyname) { |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 393 | fprintf(stderr, "Can't get key name for cipher in image '%s'\n", |
| 394 | image_name); |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 395 | goto out; |
| 396 | } |
| 397 | |
Philippe Reynes | 64d67c4 | 2020-09-17 15:01:46 +0200 | [diff] [blame] | 398 | /* |
| 399 | * Read the IV name |
| 400 | * |
| 401 | * If this property is not provided then mkimage will generate |
| 402 | * a random IV and store it in the FIT image |
| 403 | */ |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 404 | info->ivname = fdt_getprop(fit, noffset, "iv-name-hint", NULL); |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 405 | |
| 406 | info->fit = fit; |
| 407 | info->node_noffset = noffset; |
| 408 | info->name = algo_name; |
| 409 | |
| 410 | info->cipher = image_get_cipher_algo(algo_name); |
| 411 | if (!info->cipher) { |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 412 | fprintf(stderr, "Can't get algo for cipher '%s'\n", image_name); |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 413 | goto out; |
| 414 | } |
| 415 | |
| 416 | /* Read the key in the file */ |
| 417 | snprintf(filename, sizeof(filename), "%s/%s%s", |
| 418 | info->keydir, info->keyname, ".bin"); |
| 419 | info->key = malloc(info->cipher->key_len); |
| 420 | if (!info->key) { |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 421 | fprintf(stderr, "Can't allocate memory for key\n"); |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 422 | ret = -1; |
| 423 | goto out; |
| 424 | } |
| 425 | ret = fit_image_read_data(filename, (unsigned char *)info->key, |
| 426 | info->cipher->key_len); |
| 427 | if (ret < 0) |
| 428 | goto out; |
| 429 | |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 430 | info->iv = malloc(info->cipher->iv_len); |
| 431 | if (!info->iv) { |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 432 | fprintf(stderr, "Can't allocate memory for iv\n"); |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 433 | ret = -1; |
| 434 | goto out; |
| 435 | } |
Philippe Reynes | 64d67c4 | 2020-09-17 15:01:46 +0200 | [diff] [blame] | 436 | |
| 437 | if (info->ivname) { |
| 438 | /* Read the IV in the file */ |
| 439 | snprintf(filename, sizeof(filename), "%s/%s%s", |
| 440 | info->keydir, info->ivname, ".bin"); |
| 441 | ret = fit_image_read_data(filename, (unsigned char *)info->iv, |
| 442 | info->cipher->iv_len); |
| 443 | } else { |
| 444 | /* Generate an ramdom IV */ |
| 445 | ret = get_random_data((void *)info->iv, info->cipher->iv_len); |
| 446 | } |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 447 | |
| 448 | out: |
| 449 | return ret; |
| 450 | } |
| 451 | |
| 452 | int fit_image_write_cipher(void *fit, int image_noffset, int noffset, |
| 453 | const void *data, size_t size, |
| 454 | unsigned char *data_ciphered, int data_ciphered_len) |
| 455 | { |
| 456 | int ret = -1; |
| 457 | |
Patrick Oppenlander | be47211 | 2020-07-30 14:22:14 +1000 | [diff] [blame] | 458 | /* Replace data with ciphered data */ |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 459 | ret = fdt_setprop(fit, image_noffset, FIT_DATA_PROP, |
| 460 | data_ciphered, data_ciphered_len); |
Patrick Oppenlander | be47211 | 2020-07-30 14:22:14 +1000 | [diff] [blame] | 461 | if (ret == -FDT_ERR_NOSPACE) { |
| 462 | ret = -ENOSPC; |
| 463 | goto out; |
| 464 | } |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 465 | if (ret) { |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 466 | fprintf(stderr, "Can't replace data with ciphered data (err = %d)\n", ret); |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 467 | goto out; |
| 468 | } |
| 469 | |
| 470 | /* add non ciphered data size */ |
| 471 | ret = fdt_setprop_u32(fit, image_noffset, "data-size-unciphered", size); |
Patrick Oppenlander | be47211 | 2020-07-30 14:22:14 +1000 | [diff] [blame] | 472 | if (ret == -FDT_ERR_NOSPACE) { |
| 473 | ret = -ENOSPC; |
| 474 | goto out; |
| 475 | } |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 476 | if (ret) { |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 477 | fprintf(stderr, "Can't add unciphered data size (err = %d)\n", ret); |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 478 | goto out; |
| 479 | } |
| 480 | |
| 481 | out: |
| 482 | return ret; |
| 483 | } |
| 484 | |
| 485 | static int |
| 486 | fit_image_process_cipher(const char *keydir, void *keydest, void *fit, |
| 487 | const char *image_name, int image_noffset, |
Patrick Oppenlander | d586fc3 | 2020-07-30 14:22:13 +1000 | [diff] [blame] | 488 | int node_noffset, const void *data, size_t size, |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 489 | const char *cmdname) |
| 490 | { |
| 491 | struct image_cipher_info info; |
| 492 | unsigned char *data_ciphered = NULL; |
| 493 | int data_ciphered_len; |
| 494 | int ret; |
| 495 | |
| 496 | memset(&info, 0, sizeof(info)); |
| 497 | |
| 498 | ret = fit_image_setup_cipher(&info, keydir, fit, image_name, |
Patrick Oppenlander | d586fc3 | 2020-07-30 14:22:13 +1000 | [diff] [blame] | 499 | image_noffset, node_noffset); |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 500 | if (ret) |
| 501 | goto out; |
| 502 | |
| 503 | ret = info.cipher->encrypt(&info, data, size, |
| 504 | &data_ciphered, &data_ciphered_len); |
| 505 | if (ret) |
| 506 | goto out; |
| 507 | |
| 508 | /* |
| 509 | * Write the public key into the supplied FDT file; this might fail |
| 510 | * several times, since we try signing with successively increasing |
| 511 | * size values |
Philippe Reynes | 64d67c4 | 2020-09-17 15:01:46 +0200 | [diff] [blame] | 512 | * And, if needed, write the iv in the FIT file |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 513 | */ |
| 514 | if (keydest) { |
Philippe Reynes | 64d67c4 | 2020-09-17 15:01:46 +0200 | [diff] [blame] | 515 | ret = info.cipher->add_cipher_data(&info, keydest, fit, node_noffset); |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 516 | if (ret) { |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 517 | fprintf(stderr, |
| 518 | "Failed to add verification data for cipher '%s' in image '%s'\n", |
| 519 | info.keyname, image_name); |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 520 | goto out; |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | ret = fit_image_write_cipher(fit, image_noffset, node_noffset, |
| 525 | data, size, |
| 526 | data_ciphered, data_ciphered_len); |
| 527 | |
| 528 | out: |
| 529 | free(data_ciphered); |
| 530 | free((void *)info.key); |
| 531 | free((void *)info.iv); |
| 532 | return ret; |
| 533 | } |
| 534 | |
| 535 | int fit_image_cipher_data(const char *keydir, void *keydest, |
| 536 | void *fit, int image_noffset, const char *comment, |
| 537 | int require_keys, const char *engine_id, |
| 538 | const char *cmdname) |
| 539 | { |
| 540 | const char *image_name; |
| 541 | const void *data; |
| 542 | size_t size; |
Patrick Oppenlander | a4fafb3 | 2020-07-30 14:22:15 +1000 | [diff] [blame] | 543 | int cipher_node_offset, len; |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 544 | |
| 545 | /* Get image name */ |
| 546 | image_name = fit_get_name(fit, image_noffset, NULL); |
| 547 | if (!image_name) { |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 548 | fprintf(stderr, "Can't get image name\n"); |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 549 | return -1; |
| 550 | } |
| 551 | |
| 552 | /* Get image data and data length */ |
| 553 | if (fit_image_get_data(fit, image_noffset, &data, &size)) { |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 554 | fprintf(stderr, "Can't get image data/size\n"); |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 555 | return -1; |
| 556 | } |
| 557 | |
Patrick Oppenlander | a4fafb3 | 2020-07-30 14:22:15 +1000 | [diff] [blame] | 558 | /* |
| 559 | * Don't cipher ciphered data. |
| 560 | * |
| 561 | * If the data-size-unciphered property is present the data for this |
| 562 | * image is already encrypted. This is important as 'mkimage -F' can be |
| 563 | * run multiple times on a FIT image. |
| 564 | */ |
| 565 | if (fdt_getprop(fit, image_noffset, "data-size-unciphered", &len)) |
| 566 | return 0; |
| 567 | if (len != -FDT_ERR_NOTFOUND) { |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 568 | fprintf(stderr, "Failure testing for data-size-unciphered\n"); |
Patrick Oppenlander | a4fafb3 | 2020-07-30 14:22:15 +1000 | [diff] [blame] | 569 | return -1; |
| 570 | } |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 571 | |
Patrick Oppenlander | d586fc3 | 2020-07-30 14:22:13 +1000 | [diff] [blame] | 572 | /* Process cipher node if present */ |
| 573 | cipher_node_offset = fdt_subnode_offset(fit, image_noffset, |
| 574 | FIT_CIPHER_NODENAME); |
| 575 | if (cipher_node_offset == -FDT_ERR_NOTFOUND) |
| 576 | return 0; |
| 577 | if (cipher_node_offset < 0) { |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 578 | fprintf(stderr, "Failure getting cipher node\n"); |
Patrick Oppenlander | d586fc3 | 2020-07-30 14:22:13 +1000 | [diff] [blame] | 579 | return -1; |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 580 | } |
Patrick Oppenlander | d586fc3 | 2020-07-30 14:22:13 +1000 | [diff] [blame] | 581 | if (!IMAGE_ENABLE_ENCRYPT || !keydir) |
| 582 | return 0; |
| 583 | return fit_image_process_cipher(keydir, keydest, fit, image_name, |
| 584 | image_noffset, cipher_node_offset, data, size, cmdname); |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 585 | } |
| 586 | |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 587 | /** |
| 588 | * fit_image_add_verification_data() - calculate/set verig. data for image node |
| 589 | * |
| 590 | * This adds hash and signature values for an component image node. |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 591 | * |
| 592 | * All existing hash subnodes are checked, if algorithm property is set to |
| 593 | * one of the supported hash algorithms, hash value is computed and |
| 594 | * corresponding hash node property is set, for example: |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 595 | * |
| 596 | * Input component image node structure: |
| 597 | * |
Andre Przywara | 3234ecd | 2017-12-04 02:05:10 +0000 | [diff] [blame] | 598 | * o image-1 (at image_noffset) |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 599 | * | - data = [binary data] |
Andre Przywara | 3234ecd | 2017-12-04 02:05:10 +0000 | [diff] [blame] | 600 | * o hash-1 |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 601 | * |- algo = "sha1" |
| 602 | * |
| 603 | * Output component image node structure: |
| 604 | * |
Andre Przywara | 3234ecd | 2017-12-04 02:05:10 +0000 | [diff] [blame] | 605 | * o image-1 (at image_noffset) |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 606 | * | - data = [binary data] |
Andre Przywara | 3234ecd | 2017-12-04 02:05:10 +0000 | [diff] [blame] | 607 | * o hash-1 |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 608 | * |- algo = "sha1" |
| 609 | * |- value = sha1(data) |
| 610 | * |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 611 | * For signature details, please see doc/uImage.FIT/signature.txt |
| 612 | * |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 613 | * @keydir Directory containing *.key and *.crt files (or NULL) |
| 614 | * @keydest FDT Blob to write public keys into (NULL if none) |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 615 | * @fit: Pointer to the FIT format image header |
| 616 | * @image_noffset: Requested component image node |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 617 | * @comment: Comment to add to signature nodes |
| 618 | * @require_keys: Mark all keys as 'required' |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 619 | * @engine_id: Engine to use for signing |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 620 | * @return: 0 on success, <0 on failure |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 621 | */ |
Alexandru Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 622 | int fit_image_add_verification_data(const char *keydir, const char *keyfile, |
| 623 | void *keydest, void *fit, int image_noffset, |
| 624 | const char *comment, int require_keys, const char *engine_id, |
Jan Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 625 | const char *cmdname, const char* algo_name) |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 626 | { |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 627 | const char *image_name; |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 628 | const void *data; |
| 629 | size_t size; |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 630 | int noffset; |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 631 | |
| 632 | /* Get image data and data length */ |
| 633 | if (fit_image_get_data(fit, image_noffset, &data, &size)) { |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 634 | fprintf(stderr, "Can't get image data/size\n"); |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 635 | return -1; |
| 636 | } |
| 637 | |
Simon Glass | 25d6e6b | 2013-05-07 06:11:55 +0000 | [diff] [blame] | 638 | image_name = fit_get_name(fit, image_noffset, NULL); |
| 639 | |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 640 | /* Process all hash subnodes of the component image node */ |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 641 | for (noffset = fdt_first_subnode(fit, image_noffset); |
| 642 | noffset >= 0; |
| 643 | noffset = fdt_next_subnode(fit, noffset)) { |
| 644 | const char *node_name; |
| 645 | int ret = 0; |
| 646 | |
| 647 | /* |
| 648 | * Check subnode name, must be equal to "hash" or "signature". |
| 649 | * Multiple hash nodes require unique unit node |
Andre Przywara | 3234ecd | 2017-12-04 02:05:10 +0000 | [diff] [blame] | 650 | * names, e.g. hash-1, hash-2, signature-1, etc. |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 651 | */ |
| 652 | node_name = fit_get_name(fit, noffset, NULL); |
| 653 | if (!strncmp(node_name, FIT_HASH_NODENAME, |
| 654 | strlen(FIT_HASH_NODENAME))) { |
| 655 | ret = fit_image_process_hash(fit, image_name, noffset, |
| 656 | data, size); |
Alexandru Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 657 | } else if (IMAGE_ENABLE_SIGN && (keydir || keyfile) && |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 658 | !strncmp(node_name, FIT_SIG_NODENAME, |
| 659 | strlen(FIT_SIG_NODENAME))) { |
Alexandru Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 660 | ret = fit_image_process_sig(keydir, keyfile, keydest, |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 661 | fit, image_name, noffset, data, size, |
Jan Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 662 | comment, require_keys, engine_id, cmdname, |
| 663 | algo_name); |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 664 | } |
Simon Glass | 89c3fb6 | 2021-11-12 12:28:12 -0700 | [diff] [blame] | 665 | if (ret < 0) |
Simon Glass | ec539f7 | 2016-07-03 09:40:44 -0600 | [diff] [blame] | 666 | return ret; |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | return 0; |
| 670 | } |
| 671 | |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 672 | struct strlist { |
| 673 | int count; |
| 674 | char **strings; |
| 675 | }; |
| 676 | |
| 677 | static void strlist_init(struct strlist *list) |
| 678 | { |
| 679 | memset(list, '\0', sizeof(*list)); |
| 680 | } |
| 681 | |
| 682 | static void strlist_free(struct strlist *list) |
| 683 | { |
| 684 | int i; |
| 685 | |
| 686 | for (i = 0; i < list->count; i++) |
| 687 | free(list->strings[i]); |
| 688 | free(list->strings); |
| 689 | } |
| 690 | |
| 691 | static int strlist_add(struct strlist *list, const char *str) |
| 692 | { |
| 693 | char *dup; |
| 694 | |
| 695 | dup = strdup(str); |
| 696 | list->strings = realloc(list->strings, |
| 697 | (list->count + 1) * sizeof(char *)); |
| 698 | if (!list || !str) |
| 699 | return -1; |
| 700 | list->strings[list->count++] = dup; |
| 701 | |
| 702 | return 0; |
| 703 | } |
| 704 | |
Simon Glass | 6a0efc8 | 2021-11-12 12:28:06 -0700 | [diff] [blame] | 705 | static const char *fit_config_get_image_list(const void *fit, int noffset, |
| 706 | int *lenp, int *allow_missingp) |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 707 | { |
| 708 | static const char default_list[] = FIT_KERNEL_PROP "\0" |
| 709 | FIT_FDT_PROP; |
| 710 | const char *prop; |
| 711 | |
Simon Glass | 6a0efc8 | 2021-11-12 12:28:06 -0700 | [diff] [blame] | 712 | /* If there is an "sign-image" property, use that */ |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 713 | prop = fdt_getprop(fit, noffset, "sign-images", lenp); |
| 714 | if (prop) { |
| 715 | *allow_missingp = 0; |
| 716 | return *lenp ? prop : NULL; |
| 717 | } |
| 718 | |
| 719 | /* Default image list */ |
| 720 | *allow_missingp = 1; |
| 721 | *lenp = sizeof(default_list); |
| 722 | |
| 723 | return default_list; |
| 724 | } |
| 725 | |
Simon Glass | 6a0efc8 | 2021-11-12 12:28:06 -0700 | [diff] [blame] | 726 | /** |
| 727 | * fit_config_add_hash() - Add a list of nodes to hash for an image |
| 728 | * |
| 729 | * This adds a list of paths to image nodes (as referred to by a particular |
| 730 | * offset) that need to be hashed, to protect a configuration |
| 731 | * |
| 732 | * @fit: Pointer to the FIT format image header |
| 733 | * @image_noffset: Offset of image to process (e.g. /images/kernel-1) |
| 734 | * @node_inc: List of nodes to add to |
| 735 | * @conf_name Configuration-node name, child of /configurations node (only |
| 736 | * used for error messages) |
| 737 | * @sig_name Signature-node name (only used for error messages) |
| 738 | * @iname: Name of image being processed (e.g. "kernel-1" (only used |
| 739 | * for error messages) |
| 740 | */ |
| 741 | static int fit_config_add_hash(const void *fit, int image_noffset, |
| 742 | struct strlist *node_inc, const char *conf_name, |
| 743 | const char *sig_name, const char *iname) |
Philippe Reynes | 856bcc8 | 2020-11-24 14:39:47 +0100 | [diff] [blame] | 744 | { |
Simon Glass | 888c0c5 | 2021-11-12 12:28:07 -0700 | [diff] [blame] | 745 | char path[200]; |
Philippe Reynes | 856bcc8 | 2020-11-24 14:39:47 +0100 | [diff] [blame] | 746 | int noffset; |
| 747 | int hash_count; |
| 748 | int ret; |
| 749 | |
| 750 | ret = fdt_get_path(fit, image_noffset, path, sizeof(path)); |
| 751 | if (ret < 0) |
| 752 | goto err_path; |
| 753 | if (strlist_add(node_inc, path)) |
| 754 | goto err_mem; |
| 755 | |
Philippe Reynes | 856bcc8 | 2020-11-24 14:39:47 +0100 | [diff] [blame] | 756 | /* Add all this image's hashes */ |
| 757 | hash_count = 0; |
| 758 | for (noffset = fdt_first_subnode(fit, image_noffset); |
| 759 | noffset >= 0; |
| 760 | noffset = fdt_next_subnode(fit, noffset)) { |
| 761 | const char *name = fit_get_name(fit, noffset, NULL); |
| 762 | |
| 763 | if (strncmp(name, FIT_HASH_NODENAME, |
| 764 | strlen(FIT_HASH_NODENAME))) |
| 765 | continue; |
| 766 | ret = fdt_get_path(fit, noffset, path, sizeof(path)); |
| 767 | if (ret < 0) |
| 768 | goto err_path; |
| 769 | if (strlist_add(node_inc, path)) |
| 770 | goto err_mem; |
| 771 | hash_count++; |
| 772 | } |
| 773 | |
| 774 | if (!hash_count) { |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 775 | fprintf(stderr, |
| 776 | "Failed to find any hash nodes in configuration '%s/%s' image '%s' - without these it is not possible to verify this image\n", |
| 777 | conf_name, sig_name, iname); |
Philippe Reynes | 856bcc8 | 2020-11-24 14:39:47 +0100 | [diff] [blame] | 778 | return -ENOMSG; |
| 779 | } |
| 780 | |
| 781 | /* Add this image's cipher node if present */ |
| 782 | noffset = fdt_subnode_offset(fit, image_noffset, |
| 783 | FIT_CIPHER_NODENAME); |
| 784 | if (noffset != -FDT_ERR_NOTFOUND) { |
| 785 | if (noffset < 0) { |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 786 | fprintf(stderr, |
| 787 | "Failed to get cipher node in configuration '%s/%s' image '%s': %s\n", |
| 788 | conf_name, sig_name, iname, |
| 789 | fdt_strerror(noffset)); |
Philippe Reynes | 856bcc8 | 2020-11-24 14:39:47 +0100 | [diff] [blame] | 790 | return -EIO; |
| 791 | } |
| 792 | ret = fdt_get_path(fit, noffset, path, sizeof(path)); |
| 793 | if (ret < 0) |
| 794 | goto err_path; |
| 795 | if (strlist_add(node_inc, path)) |
| 796 | goto err_mem; |
| 797 | } |
| 798 | |
| 799 | return 0; |
| 800 | |
| 801 | err_mem: |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 802 | fprintf(stderr, "Out of memory processing configuration '%s/%s'\n", conf_name, |
| 803 | sig_name); |
Philippe Reynes | 856bcc8 | 2020-11-24 14:39:47 +0100 | [diff] [blame] | 804 | return -ENOMEM; |
| 805 | |
| 806 | err_path: |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 807 | fprintf(stderr, "Failed to get path for image '%s' in configuration '%s/%s': %s\n", |
| 808 | iname, conf_name, sig_name, fdt_strerror(ret)); |
Philippe Reynes | 856bcc8 | 2020-11-24 14:39:47 +0100 | [diff] [blame] | 809 | return -ENOENT; |
| 810 | } |
| 811 | |
Simon Glass | 6a0efc8 | 2021-11-12 12:28:06 -0700 | [diff] [blame] | 812 | /** |
| 813 | * fit_config_get_hash_list() - Get the regions to sign |
| 814 | * |
| 815 | * This calculates a list of nodes to hash for this particular configuration, |
| 816 | * returning it as a string list (struct strlist, not a devicetree string list) |
| 817 | * |
| 818 | * @fit: Pointer to the FIT format image header |
| 819 | * @conf_noffset: Offset of configuration node to sign (child of |
| 820 | * /configurations node) |
| 821 | * @sig_offset: Offset of signature node containing info about how to sign it |
| 822 | * (child of 'signatures' node) |
| 823 | * @return 0 if OK, -ENOENT if an image referred to by the configuration cannot |
| 824 | * be found, -ENOMSG if ther were no images in the configuration |
| 825 | */ |
| 826 | static int fit_config_get_hash_list(const void *fit, int conf_noffset, |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 827 | int sig_offset, struct strlist *node_inc) |
| 828 | { |
| 829 | int allow_missing; |
| 830 | const char *prop, *iname, *end; |
| 831 | const char *conf_name, *sig_name; |
Philippe Reynes | 856bcc8 | 2020-11-24 14:39:47 +0100 | [diff] [blame] | 832 | char name[200]; |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 833 | int image_count; |
| 834 | int ret, len; |
| 835 | |
| 836 | conf_name = fit_get_name(fit, conf_noffset, NULL); |
| 837 | sig_name = fit_get_name(fit, sig_offset, NULL); |
| 838 | |
| 839 | /* |
| 840 | * Build a list of nodes we need to hash. We always need the root |
| 841 | * node and the configuration. |
| 842 | */ |
| 843 | strlist_init(node_inc); |
| 844 | snprintf(name, sizeof(name), "%s/%s", FIT_CONFS_PATH, conf_name); |
| 845 | if (strlist_add(node_inc, "/") || |
| 846 | strlist_add(node_inc, name)) |
| 847 | goto err_mem; |
| 848 | |
| 849 | /* Get a list of images that we intend to sign */ |
Heiko Schocher | fdfd520 | 2014-03-03 12:19:23 +0100 | [diff] [blame] | 850 | prop = fit_config_get_image_list(fit, sig_offset, &len, |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 851 | &allow_missing); |
| 852 | if (!prop) |
| 853 | return 0; |
| 854 | |
| 855 | /* Locate the images */ |
| 856 | end = prop + len; |
| 857 | image_count = 0; |
| 858 | for (iname = prop; iname < end; iname += strlen(iname) + 1) { |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 859 | int image_noffset; |
Philippe Reynes | 1f7d9a2 | 2020-11-24 14:39:48 +0100 | [diff] [blame] | 860 | int index, max_index; |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 861 | |
Philippe Reynes | 1f7d9a2 | 2020-11-24 14:39:48 +0100 | [diff] [blame] | 862 | max_index = fdt_stringlist_count(fit, conf_noffset, iname); |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 863 | |
Philippe Reynes | 1f7d9a2 | 2020-11-24 14:39:48 +0100 | [diff] [blame] | 864 | for (index = 0; index < max_index; index++) { |
| 865 | image_noffset = fit_conf_get_prop_node_index(fit, conf_noffset, |
| 866 | iname, index); |
| 867 | |
| 868 | if (image_noffset < 0) { |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 869 | fprintf(stderr, |
| 870 | "Failed to find image '%s' in configuration '%s/%s'\n", |
| 871 | iname, conf_name, sig_name); |
Philippe Reynes | 1f7d9a2 | 2020-11-24 14:39:48 +0100 | [diff] [blame] | 872 | if (allow_missing) |
| 873 | continue; |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 874 | |
Philippe Reynes | 1f7d9a2 | 2020-11-24 14:39:48 +0100 | [diff] [blame] | 875 | return -ENOENT; |
| 876 | } |
Patrick Oppenlander | 5964c3f | 2020-07-30 14:30:47 +1000 | [diff] [blame] | 877 | |
Simon Glass | 6a0efc8 | 2021-11-12 12:28:06 -0700 | [diff] [blame] | 878 | ret = fit_config_add_hash(fit, image_noffset, node_inc, |
| 879 | conf_name, sig_name, iname); |
Philippe Reynes | 1f7d9a2 | 2020-11-24 14:39:48 +0100 | [diff] [blame] | 880 | if (ret < 0) |
| 881 | return ret; |
| 882 | |
| 883 | image_count++; |
| 884 | } |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | if (!image_count) { |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 888 | fprintf(stderr, "Failed to find any images for configuration '%s/%s'\n", |
| 889 | conf_name, sig_name); |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 890 | return -ENOMSG; |
| 891 | } |
| 892 | |
| 893 | return 0; |
| 894 | |
| 895 | err_mem: |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 896 | fprintf(stderr, "Out of memory processing configuration '%s/%s'\n", conf_name, |
| 897 | sig_name); |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 898 | return -ENOMEM; |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 899 | } |
| 900 | |
Simon Glass | 6a0efc8 | 2021-11-12 12:28:06 -0700 | [diff] [blame] | 901 | /** |
| 902 | * fit_config_get_regions() - Get the regions to sign |
| 903 | * |
| 904 | * This calculates a list of node to hash for this particular configuration, |
| 905 | * then finds which regions of the devicetree they correspond to. |
| 906 | * |
| 907 | * @fit: Pointer to the FIT format image header |
| 908 | * @conf_noffset: Offset of configuration node to sign (child of |
| 909 | * /configurations node) |
| 910 | * @sig_offset: Offset of signature node containing info about how to sign it |
| 911 | * (child of 'signatures' node) |
| 912 | * @regionp: Returns list of regions that need to be hashed (allocated; must be |
| 913 | * freed by the caller) |
| 914 | * @region_count: Returns number of regions |
| 915 | * @region_propp: Returns string-list property containing the list of nodes |
| 916 | * that correspond to the regions. Each entry is a full path to the node. |
| 917 | * This is in devicetree format, i.e. a \0 between each string. This is |
| 918 | * allocated and must be freed by the caller. |
| 919 | * @region_proplen: Returns length of *@@region_propp in bytes |
| 920 | * @return 0 if OK, -ENOMEM if out of memory, -EIO if the regions to hash could |
| 921 | * not be found, -EINVAL if no registers were found to hash |
| 922 | */ |
| 923 | static int fit_config_get_regions(const void *fit, int conf_noffset, |
| 924 | int sig_offset, struct image_region **regionp, |
| 925 | int *region_countp, char **region_propp, |
| 926 | int *region_proplen) |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 927 | { |
Sean Anderson | 016f4d5 | 2022-10-20 15:41:10 -0400 | [diff] [blame] | 928 | char * const exc_prop[] = { |
| 929 | FIT_DATA_PROP, |
| 930 | FIT_DATA_SIZE_PROP, |
| 931 | FIT_DATA_POSITION_PROP, |
| 932 | FIT_DATA_OFFSET_PROP, |
| 933 | }; |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 934 | struct strlist node_inc; |
| 935 | struct image_region *region; |
| 936 | struct fdt_region fdt_regions[100]; |
| 937 | const char *conf_name, *sig_name; |
| 938 | char path[200]; |
| 939 | int count, i; |
| 940 | char *region_prop; |
| 941 | int ret, len; |
| 942 | |
| 943 | conf_name = fit_get_name(fit, conf_noffset, NULL); |
Simon Glass | 6a0efc8 | 2021-11-12 12:28:06 -0700 | [diff] [blame] | 944 | sig_name = fit_get_name(fit, sig_offset, NULL); |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 945 | debug("%s: conf='%s', sig='%s'\n", __func__, conf_name, sig_name); |
| 946 | |
| 947 | /* Get a list of nodes we want to hash */ |
Simon Glass | 6a0efc8 | 2021-11-12 12:28:06 -0700 | [diff] [blame] | 948 | ret = fit_config_get_hash_list(fit, conf_noffset, sig_offset, |
| 949 | &node_inc); |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 950 | if (ret) |
| 951 | return ret; |
| 952 | |
| 953 | /* Get a list of regions to hash */ |
| 954 | count = fdt_find_regions(fit, node_inc.strings, node_inc.count, |
| 955 | exc_prop, ARRAY_SIZE(exc_prop), |
| 956 | fdt_regions, ARRAY_SIZE(fdt_regions), |
| 957 | path, sizeof(path), 1); |
| 958 | if (count < 0) { |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 959 | fprintf(stderr, "Failed to hash configuration '%s/%s': %s\n", conf_name, |
| 960 | sig_name, fdt_strerror(ret)); |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 961 | return -EIO; |
| 962 | } |
| 963 | if (count == 0) { |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 964 | fprintf(stderr, "No data to hash for configuration '%s/%s': %s\n", |
| 965 | conf_name, sig_name, fdt_strerror(ret)); |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 966 | return -EINVAL; |
| 967 | } |
| 968 | |
| 969 | /* Build our list of data blocks */ |
| 970 | region = fit_region_make_list(fit, fdt_regions, count, NULL); |
| 971 | if (!region) { |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 972 | fprintf(stderr, "Out of memory hashing configuration '%s/%s'\n", |
| 973 | conf_name, sig_name); |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 974 | return -ENOMEM; |
| 975 | } |
| 976 | |
| 977 | /* Create a list of all hashed properties */ |
| 978 | debug("Hash nodes:\n"); |
| 979 | for (i = len = 0; i < node_inc.count; i++) { |
| 980 | debug(" %s\n", node_inc.strings[i]); |
| 981 | len += strlen(node_inc.strings[i]) + 1; |
| 982 | } |
| 983 | region_prop = malloc(len); |
| 984 | if (!region_prop) { |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 985 | fprintf(stderr, "Out of memory setting up regions for configuration '%s/%s'\n", |
| 986 | conf_name, sig_name); |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 987 | return -ENOMEM; |
| 988 | } |
| 989 | for (i = len = 0; i < node_inc.count; |
| 990 | len += strlen(node_inc.strings[i]) + 1, i++) |
| 991 | strcpy(region_prop + len, node_inc.strings[i]); |
| 992 | strlist_free(&node_inc); |
| 993 | |
| 994 | *region_countp = count; |
| 995 | *regionp = region; |
| 996 | *region_propp = region_prop; |
| 997 | *region_proplen = len; |
| 998 | |
| 999 | return 0; |
| 1000 | } |
| 1001 | |
Simon Glass | 89c3fb6 | 2021-11-12 12:28:12 -0700 | [diff] [blame] | 1002 | /** |
| 1003 | * fit_config_process_sig - Process a single subnode of the configurations/ node |
| 1004 | * |
| 1005 | * Generate a signed hash of the supplied data and store it in the node. |
| 1006 | * |
| 1007 | * @keydir: Directory containing keys to use for signing |
| 1008 | * @keydest: Destination FDT blob to write public keys into (NULL if none) |
| 1009 | * @fit: pointer to the FIT format image header |
| 1010 | * @conf_name name of config being processed (used to display errors) |
| 1011 | * @conf_noffset: Offset of configuration node, e.g. '/configurations/conf-1' |
| 1012 | * @noffset: subnode offset, e.g. '/configurations/conf-1/sig-1' |
| 1013 | * @comment: Comment to add to signature nodes |
| 1014 | * @require_keys: Mark all keys as 'required' |
| 1015 | * @engine_id: Engine to use for signing |
| 1016 | * @cmdname: Command name used when reporting errors |
| 1017 | * @return keydest node if @keydest is non-NULL, else 0 if none; -ve error code |
| 1018 | * on failure |
| 1019 | */ |
Alexandru Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 1020 | static int fit_config_process_sig(const char *keydir, const char *keyfile, |
Simon Glass | 6a0efc8 | 2021-11-12 12:28:06 -0700 | [diff] [blame] | 1021 | void *keydest, void *fit, const char *conf_name, |
Alexandru Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 1022 | int conf_noffset, int noffset, const char *comment, |
Jan Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 1023 | int require_keys, const char *engine_id, const char *cmdname, |
| 1024 | const char *algo_name) |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1025 | { |
| 1026 | struct image_sign_info info; |
| 1027 | const char *node_name; |
| 1028 | struct image_region *region; |
| 1029 | char *region_prop; |
| 1030 | int region_proplen; |
| 1031 | int region_count; |
| 1032 | uint8_t *value; |
| 1033 | uint value_len; |
| 1034 | int ret; |
| 1035 | |
| 1036 | node_name = fit_get_name(fit, noffset, NULL); |
Simon Glass | 6a0efc8 | 2021-11-12 12:28:06 -0700 | [diff] [blame] | 1037 | if (fit_config_get_regions(fit, conf_noffset, noffset, ®ion, |
| 1038 | ®ion_count, ®ion_prop, |
| 1039 | ®ion_proplen)) |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1040 | return -1; |
| 1041 | |
Alexandru Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 1042 | if (fit_image_setup_sig(&info, keydir, keyfile, fit, conf_name, noffset, |
Jan Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 1043 | require_keys ? "conf" : NULL, engine_id, |
| 1044 | algo_name)) |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1045 | return -1; |
| 1046 | |
Andrew Duda | 6616c82 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 1047 | ret = info.crypto->sign(&info, region, region_count, &value, |
| 1048 | &value_len); |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1049 | free(region); |
| 1050 | if (ret) { |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 1051 | fprintf(stderr, "Failed to sign '%s' signature node in '%s' conf node\n", |
| 1052 | node_name, conf_name); |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1053 | |
| 1054 | /* We allow keys to be missing */ |
| 1055 | if (ret == -ENOENT) |
| 1056 | return 0; |
| 1057 | return -1; |
| 1058 | } |
| 1059 | |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 1060 | ret = fit_image_write_sig(fit, noffset, value, value_len, comment, |
Jan Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 1061 | region_prop, region_proplen, cmdname, |
| 1062 | algo_name); |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 1063 | if (ret) { |
| 1064 | if (ret == -FDT_ERR_NOSPACE) |
| 1065 | return -ENOSPC; |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 1066 | fprintf(stderr, |
| 1067 | "Can't write signature for '%s' signature node in '%s' conf node: %s\n", |
| 1068 | node_name, conf_name, fdt_strerror(ret)); |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1069 | return -1; |
| 1070 | } |
| 1071 | free(value); |
| 1072 | free(region_prop); |
| 1073 | |
| 1074 | /* Get keyname again, as FDT has changed and invalidated our pointer */ |
Simon Glass | d7aabcc | 2020-03-18 11:44:06 -0600 | [diff] [blame] | 1075 | info.keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL); |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1076 | |
| 1077 | /* Write the public key into the supplied FDT file */ |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 1078 | if (keydest) { |
Andrew Duda | 6616c82 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 1079 | ret = info.crypto->add_verify_data(&info, keydest); |
Simon Glass | 94336dc | 2021-11-12 12:28:11 -0700 | [diff] [blame] | 1080 | if (ret < 0) { |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 1081 | fprintf(stderr, |
| 1082 | "Failed to add verification data for '%s' signature node in '%s' configuration node\n", |
| 1083 | node_name, conf_name); |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 1084 | } |
Simon Glass | 89c3fb6 | 2021-11-12 12:28:12 -0700 | [diff] [blame] | 1085 | return ret; |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1086 | } |
| 1087 | |
| 1088 | return 0; |
| 1089 | } |
| 1090 | |
Alexandru Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 1091 | static int fit_config_add_verification_data(const char *keydir, |
| 1092 | const char *keyfile, void *keydest, void *fit, int conf_noffset, |
| 1093 | const char *comment, int require_keys, const char *engine_id, |
Simon Glass | e460726 | 2021-11-12 12:28:13 -0700 | [diff] [blame] | 1094 | const char *cmdname, const char *algo_name, |
| 1095 | struct image_summary *summary) |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1096 | { |
| 1097 | const char *conf_name; |
| 1098 | int noffset; |
| 1099 | |
| 1100 | conf_name = fit_get_name(fit, conf_noffset, NULL); |
| 1101 | |
| 1102 | /* Process all hash subnodes of the configuration node */ |
| 1103 | for (noffset = fdt_first_subnode(fit, conf_noffset); |
| 1104 | noffset >= 0; |
| 1105 | noffset = fdt_next_subnode(fit, noffset)) { |
| 1106 | const char *node_name; |
| 1107 | int ret = 0; |
| 1108 | |
| 1109 | node_name = fit_get_name(fit, noffset, NULL); |
| 1110 | if (!strncmp(node_name, FIT_SIG_NODENAME, |
| 1111 | strlen(FIT_SIG_NODENAME))) { |
Alexandru Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 1112 | ret = fit_config_process_sig(keydir, keyfile, keydest, |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1113 | fit, conf_name, conf_noffset, noffset, comment, |
Jan Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 1114 | require_keys, engine_id, cmdname, algo_name); |
Simon Glass | e460726 | 2021-11-12 12:28:13 -0700 | [diff] [blame] | 1115 | if (ret < 0) |
| 1116 | return ret; |
| 1117 | |
| 1118 | summary->sig_offset = noffset; |
| 1119 | fdt_get_path(fit, noffset, summary->sig_path, |
| 1120 | sizeof(summary->sig_path)); |
| 1121 | |
| 1122 | if (keydest) { |
| 1123 | summary->keydest_offset = ret; |
| 1124 | fdt_get_path(keydest, ret, |
| 1125 | summary->keydest_path, |
| 1126 | sizeof(summary->keydest_path)); |
| 1127 | } |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1128 | } |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | return 0; |
| 1132 | } |
| 1133 | |
Philippe Reynes | 3e3899c | 2022-03-28 22:57:02 +0200 | [diff] [blame] | 1134 | /* |
| 1135 | * 0) open file (open) |
| 1136 | * 1) read certificate (PEM_read_X509) |
| 1137 | * 2) get public key (X509_get_pubkey) |
| 1138 | * 3) provide der format (d2i_RSAPublicKey) |
| 1139 | */ |
| 1140 | static int read_pub_key(const char *keydir, const void *name, |
| 1141 | unsigned char **pubkey, int *pubkey_len) |
| 1142 | { |
| 1143 | char path[1024]; |
| 1144 | EVP_PKEY *key = NULL; |
| 1145 | X509 *cert; |
| 1146 | FILE *f; |
| 1147 | int ret; |
| 1148 | |
| 1149 | memset(path, 0, 1024); |
| 1150 | snprintf(path, sizeof(path), "%s/%s.crt", keydir, (char *)name); |
| 1151 | |
| 1152 | /* Open certificate file */ |
| 1153 | f = fopen(path, "r"); |
| 1154 | if (!f) { |
| 1155 | fprintf(stderr, "Couldn't open RSA certificate: '%s': %s\n", |
| 1156 | path, strerror(errno)); |
| 1157 | return -EACCES; |
| 1158 | } |
| 1159 | |
| 1160 | /* Read the certificate */ |
| 1161 | cert = NULL; |
| 1162 | if (!PEM_read_X509(f, &cert, NULL, NULL)) { |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 1163 | fprintf(stderr, "Couldn't read certificate"); |
Philippe Reynes | 3e3899c | 2022-03-28 22:57:02 +0200 | [diff] [blame] | 1164 | ret = -EINVAL; |
| 1165 | goto err_cert; |
| 1166 | } |
| 1167 | |
| 1168 | /* Get the public key from the certificate. */ |
| 1169 | key = X509_get_pubkey(cert); |
| 1170 | if (!key) { |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 1171 | fprintf(stderr, "Couldn't read public key\n"); |
Philippe Reynes | 3e3899c | 2022-03-28 22:57:02 +0200 | [diff] [blame] | 1172 | ret = -EINVAL; |
| 1173 | goto err_pubkey; |
| 1174 | } |
| 1175 | |
| 1176 | /* Get DER form */ |
| 1177 | ret = i2d_PublicKey(key, pubkey); |
| 1178 | if (ret < 0) { |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 1179 | fprintf(stderr, "Couldn't get DER form\n"); |
Philippe Reynes | 3e3899c | 2022-03-28 22:57:02 +0200 | [diff] [blame] | 1180 | ret = -EINVAL; |
| 1181 | goto err_pubkey; |
| 1182 | } |
| 1183 | |
| 1184 | *pubkey_len = ret; |
| 1185 | ret = 0; |
| 1186 | |
| 1187 | err_pubkey: |
| 1188 | X509_free(cert); |
| 1189 | err_cert: |
| 1190 | fclose(f); |
| 1191 | return ret; |
| 1192 | } |
| 1193 | |
| 1194 | int fit_pre_load_data(const char *keydir, void *keydest, void *fit) |
| 1195 | { |
| 1196 | int pre_load_noffset; |
| 1197 | const void *algo_name; |
| 1198 | const void *key_name; |
| 1199 | unsigned char *pubkey = NULL; |
| 1200 | int ret, pubkey_len; |
| 1201 | |
| 1202 | if (!keydir || !keydest || !fit) |
| 1203 | return 0; |
| 1204 | |
| 1205 | /* Search node pre-load sig */ |
| 1206 | pre_load_noffset = fdt_path_offset(keydest, IMAGE_PRE_LOAD_PATH); |
| 1207 | if (pre_load_noffset < 0) { |
| 1208 | ret = 0; |
| 1209 | goto out; |
| 1210 | } |
| 1211 | |
| 1212 | algo_name = fdt_getprop(keydest, pre_load_noffset, "algo-name", NULL); |
| 1213 | key_name = fdt_getprop(keydest, pre_load_noffset, "key-name", NULL); |
| 1214 | |
| 1215 | /* Check that all mandatory properties are present */ |
| 1216 | if (!algo_name || !key_name) { |
| 1217 | if (!algo_name) |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 1218 | fprintf(stderr, "The property algo-name is missing in the node %s\n", |
| 1219 | IMAGE_PRE_LOAD_PATH); |
Philippe Reynes | 3e3899c | 2022-03-28 22:57:02 +0200 | [diff] [blame] | 1220 | if (!key_name) |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 1221 | fprintf(stderr, "The property key-name is missing in the node %s\n", |
| 1222 | IMAGE_PRE_LOAD_PATH); |
Mark Kettenis | d4ba0b4 | 2022-04-26 19:24:38 +0200 | [diff] [blame] | 1223 | ret = -EINVAL; |
Philippe Reynes | 3e3899c | 2022-03-28 22:57:02 +0200 | [diff] [blame] | 1224 | goto out; |
| 1225 | } |
| 1226 | |
| 1227 | /* Read public key */ |
| 1228 | ret = read_pub_key(keydir, key_name, &pubkey, &pubkey_len); |
| 1229 | if (ret < 0) |
| 1230 | goto out; |
| 1231 | |
| 1232 | /* Add the public key to the device tree */ |
| 1233 | ret = fdt_setprop(keydest, pre_load_noffset, "public-key", |
| 1234 | pubkey, pubkey_len); |
| 1235 | if (ret) |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 1236 | fprintf(stderr, "Can't set public-key in node %s (ret = %d)\n", |
| 1237 | IMAGE_PRE_LOAD_PATH, ret); |
Philippe Reynes | 3e3899c | 2022-03-28 22:57:02 +0200 | [diff] [blame] | 1238 | |
| 1239 | out: |
| 1240 | return ret; |
| 1241 | } |
| 1242 | |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 1243 | int fit_cipher_data(const char *keydir, void *keydest, void *fit, |
| 1244 | const char *comment, int require_keys, |
| 1245 | const char *engine_id, const char *cmdname) |
| 1246 | { |
| 1247 | int images_noffset; |
| 1248 | int noffset; |
| 1249 | int ret; |
| 1250 | |
| 1251 | /* Find images parent node offset */ |
| 1252 | images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); |
| 1253 | if (images_noffset < 0) { |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 1254 | fprintf(stderr, "Can't find images parent node '%s' (%s)\n", |
| 1255 | FIT_IMAGES_PATH, fdt_strerror(images_noffset)); |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 1256 | return images_noffset; |
| 1257 | } |
| 1258 | |
| 1259 | /* Process its subnodes, print out component images details */ |
| 1260 | for (noffset = fdt_first_subnode(fit, images_noffset); |
| 1261 | noffset >= 0; |
| 1262 | noffset = fdt_next_subnode(fit, noffset)) { |
| 1263 | /* |
| 1264 | * Direct child node of the images parent node, |
| 1265 | * i.e. component image node. |
| 1266 | */ |
| 1267 | ret = fit_image_cipher_data(keydir, keydest, |
| 1268 | fit, noffset, comment, |
| 1269 | require_keys, engine_id, |
| 1270 | cmdname); |
| 1271 | if (ret) |
| 1272 | return ret; |
| 1273 | } |
| 1274 | |
| 1275 | return 0; |
| 1276 | } |
| 1277 | |
Alexandru Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 1278 | int fit_add_verification_data(const char *keydir, const char *keyfile, |
| 1279 | void *keydest, void *fit, const char *comment, |
| 1280 | int require_keys, const char *engine_id, |
Simon Glass | e460726 | 2021-11-12 12:28:13 -0700 | [diff] [blame] | 1281 | const char *cmdname, const char *algo_name, |
| 1282 | struct image_summary *summary) |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 1283 | { |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1284 | int images_noffset, confs_noffset; |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 1285 | int noffset; |
| 1286 | int ret; |
| 1287 | |
| 1288 | /* Find images parent node offset */ |
| 1289 | images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); |
| 1290 | if (images_noffset < 0) { |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 1291 | fprintf(stderr, "Can't find images parent node '%s' (%s)\n", |
| 1292 | FIT_IMAGES_PATH, fdt_strerror(images_noffset)); |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 1293 | return images_noffset; |
| 1294 | } |
| 1295 | |
| 1296 | /* Process its subnodes, print out component images details */ |
| 1297 | for (noffset = fdt_first_subnode(fit, images_noffset); |
| 1298 | noffset >= 0; |
| 1299 | noffset = fdt_next_subnode(fit, noffset)) { |
| 1300 | /* |
| 1301 | * Direct child node of the images parent node, |
| 1302 | * i.e. component image node. |
| 1303 | */ |
Alexandru Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 1304 | ret = fit_image_add_verification_data(keydir, keyfile, keydest, |
Alex Kiernan | 697fcdc | 2018-06-20 20:10:52 +0000 | [diff] [blame] | 1305 | fit, noffset, comment, require_keys, engine_id, |
Jan Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 1306 | cmdname, algo_name); |
Simon Glass | 3541407 | 2022-12-21 16:08:23 -0700 | [diff] [blame] | 1307 | if (ret) { |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 1308 | fprintf(stderr, "Can't add verification data for node '%s' (%s)\n", |
| 1309 | fdt_get_name(fit, noffset, NULL), |
| 1310 | fdt_strerror(ret)); |
Simon Glass | ee38265 | 2013-05-07 06:12:01 +0000 | [diff] [blame] | 1311 | return ret; |
Simon Glass | 3541407 | 2022-12-21 16:08:23 -0700 | [diff] [blame] | 1312 | } |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 1313 | } |
| 1314 | |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1315 | /* If there are no keys, we can't sign configurations */ |
Alexandru Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 1316 | if (!IMAGE_ENABLE_SIGN || !(keydir || keyfile)) |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1317 | return 0; |
| 1318 | |
| 1319 | /* Find configurations parent node offset */ |
| 1320 | confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH); |
| 1321 | if (confs_noffset < 0) { |
Oleksandr Suvorov | 9ed732d | 2023-08-17 18:36:10 +0300 | [diff] [blame] | 1322 | fprintf(stderr, "Can't find images parent node '%s' (%s)\n", |
| 1323 | FIT_CONFS_PATH, fdt_strerror(confs_noffset)); |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1324 | return -ENOENT; |
| 1325 | } |
| 1326 | |
| 1327 | /* Process its subnodes, print out component images details */ |
| 1328 | for (noffset = fdt_first_subnode(fit, confs_noffset); |
| 1329 | noffset >= 0; |
| 1330 | noffset = fdt_next_subnode(fit, noffset)) { |
Alexandru Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 1331 | ret = fit_config_add_verification_data(keydir, keyfile, keydest, |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1332 | fit, noffset, comment, |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 1333 | require_keys, |
Jan Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 1334 | engine_id, cmdname, |
Simon Glass | e460726 | 2021-11-12 12:28:13 -0700 | [diff] [blame] | 1335 | algo_name, summary); |
Simon Glass | 56ab8d6 | 2013-06-13 15:10:09 -0700 | [diff] [blame] | 1336 | if (ret) |
| 1337 | return ret; |
| 1338 | } |
| 1339 | |
Simon Glass | 10a1eca | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 1340 | return 0; |
| 1341 | } |
Heiko Schocher | d7b4232 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 1342 | |
| 1343 | #ifdef CONFIG_FIT_SIGNATURE |
Simon Glass | 0571232 | 2020-03-18 11:44:03 -0600 | [diff] [blame] | 1344 | int fit_check_sign(const void *fit, const void *key, |
| 1345 | const char *fit_uname_config) |
Heiko Schocher | d7b4232 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 1346 | { |
| 1347 | int cfg_noffset; |
| 1348 | int ret; |
| 1349 | |
Simon Glass | 0571232 | 2020-03-18 11:44:03 -0600 | [diff] [blame] | 1350 | cfg_noffset = fit_conf_get_node(fit, fit_uname_config); |
Heiko Schocher | d7b4232 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 1351 | if (!cfg_noffset) |
| 1352 | return -1; |
| 1353 | |
Simon Glass | a559bb2 | 2020-03-18 11:43:56 -0600 | [diff] [blame] | 1354 | printf("Verifying Hash Integrity for node '%s'... ", |
| 1355 | fdt_get_name(fit, cfg_noffset, NULL)); |
Simon Glass | a51991d | 2014-06-12 07:24:53 -0600 | [diff] [blame] | 1356 | ret = fit_config_verify(fit, cfg_noffset); |
| 1357 | if (ret) |
| 1358 | return ret; |
Simon Glass | 0571232 | 2020-03-18 11:44:03 -0600 | [diff] [blame] | 1359 | printf("Verified OK, loading images\n"); |
Simon Glass | a51991d | 2014-06-12 07:24:53 -0600 | [diff] [blame] | 1360 | ret = bootm_host_load_images(fit, cfg_noffset); |
| 1361 | |
Heiko Schocher | d7b4232 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 1362 | return ret; |
| 1363 | } |
| 1364 | #endif |