Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2012 The Chromium OS Authors. |
| 4 | * |
| 5 | * (C) Copyright 2011 |
| 6 | * Joe Hershberger, National Instruments, joe.hershberger@ni.com |
| 7 | * |
| 8 | * (C) Copyright 2000 |
| 9 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
Ruchika Gupta | c915ceb | 2015-01-23 16:01:58 +0530 | [diff] [blame] | 12 | #ifndef USE_HOSTCC |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 13 | #include <common.h> |
| 14 | #include <command.h> |
Simon Glass | 5e6201b | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 15 | #include <env.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame^] | 16 | #include <log.h> |
Hung-ying Tyan | f061b1a | 2014-03-03 12:19:28 +0100 | [diff] [blame] | 17 | #include <malloc.h> |
Joe Hershberger | 65b905b | 2015-03-22 17:08:59 -0500 | [diff] [blame] | 18 | #include <mapmem.h> |
Akshay Saraswat | cbd8f2c | 2013-03-20 21:00:58 +0000 | [diff] [blame] | 19 | #include <hw_sha.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 20 | #include <asm/cache.h> |
Ruchika Gupta | c915ceb | 2015-01-23 16:01:58 +0530 | [diff] [blame] | 21 | #include <asm/io.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 22 | #include <linux/errno.h> |
Simon Glass | 48b6c6b | 2019-11-14 12:57:16 -0700 | [diff] [blame] | 23 | #include <u-boot/crc.h> |
Ruchika Gupta | c915ceb | 2015-01-23 16:01:58 +0530 | [diff] [blame] | 24 | #else |
| 25 | #include "mkimage.h" |
| 26 | #include <time.h> |
Ruchika Gupta | c915ceb | 2015-01-23 16:01:58 +0530 | [diff] [blame] | 27 | #endif /* !USE_HOSTCC*/ |
| 28 | |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 29 | #include <hash.h> |
Simon Glass | 2dc9c34 | 2020-05-10 11:40:01 -0600 | [diff] [blame] | 30 | #include <image.h> |
Ruchika Gupta | c915ceb | 2015-01-23 16:01:58 +0530 | [diff] [blame] | 31 | #include <u-boot/crc.h> |
Jeroen Hofstee | bfe88fe | 2014-06-12 22:27:12 +0200 | [diff] [blame] | 32 | #include <u-boot/sha1.h> |
| 33 | #include <u-boot/sha256.h> |
Ruchika Gupta | c915ceb | 2015-01-23 16:01:58 +0530 | [diff] [blame] | 34 | #include <u-boot/md5.h> |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 35 | |
T Karthik Reddy | a098b43 | 2019-03-16 15:23:01 +0530 | [diff] [blame] | 36 | #if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC) |
| 37 | DECLARE_GLOBAL_DATA_PTR; |
| 38 | #endif |
| 39 | |
| 40 | static void reloc_update(void); |
| 41 | |
Tom Rini | 03361c7 | 2017-08-14 16:38:07 -0400 | [diff] [blame] | 42 | #if defined(CONFIG_SHA1) && !defined(CONFIG_SHA_PROG_HW_ACCEL) |
Hung-ying Tyan | f061b1a | 2014-03-03 12:19:28 +0100 | [diff] [blame] | 43 | static int hash_init_sha1(struct hash_algo *algo, void **ctxp) |
| 44 | { |
| 45 | sha1_context *ctx = malloc(sizeof(sha1_context)); |
| 46 | sha1_starts(ctx); |
| 47 | *ctxp = ctx; |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | static int hash_update_sha1(struct hash_algo *algo, void *ctx, const void *buf, |
| 52 | unsigned int size, int is_last) |
| 53 | { |
| 54 | sha1_update((sha1_context *)ctx, buf, size); |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | static int hash_finish_sha1(struct hash_algo *algo, void *ctx, void *dest_buf, |
| 59 | int size) |
| 60 | { |
| 61 | if (size < algo->digest_size) |
| 62 | return -1; |
| 63 | |
| 64 | sha1_finish((sha1_context *)ctx, dest_buf); |
| 65 | free(ctx); |
| 66 | return 0; |
| 67 | } |
| 68 | #endif |
| 69 | |
Tom Rini | 03361c7 | 2017-08-14 16:38:07 -0400 | [diff] [blame] | 70 | #if defined(CONFIG_SHA256) && !defined(CONFIG_SHA_PROG_HW_ACCEL) |
Hung-ying Tyan | f061b1a | 2014-03-03 12:19:28 +0100 | [diff] [blame] | 71 | static int hash_init_sha256(struct hash_algo *algo, void **ctxp) |
| 72 | { |
| 73 | sha256_context *ctx = malloc(sizeof(sha256_context)); |
| 74 | sha256_starts(ctx); |
| 75 | *ctxp = ctx; |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | static int hash_update_sha256(struct hash_algo *algo, void *ctx, |
| 80 | const void *buf, unsigned int size, int is_last) |
| 81 | { |
| 82 | sha256_update((sha256_context *)ctx, buf, size); |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | static int hash_finish_sha256(struct hash_algo *algo, void *ctx, void |
| 87 | *dest_buf, int size) |
| 88 | { |
| 89 | if (size < algo->digest_size) |
| 90 | return -1; |
| 91 | |
| 92 | sha256_finish((sha256_context *)ctx, dest_buf); |
| 93 | free(ctx); |
| 94 | return 0; |
| 95 | } |
| 96 | #endif |
| 97 | |
Philipp Tomsich | 6e2ac3e | 2018-11-25 19:22:19 +0100 | [diff] [blame] | 98 | static int hash_init_crc16_ccitt(struct hash_algo *algo, void **ctxp) |
| 99 | { |
| 100 | uint16_t *ctx = malloc(sizeof(uint16_t)); |
| 101 | *ctx = 0; |
| 102 | *ctxp = ctx; |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | static int hash_update_crc16_ccitt(struct hash_algo *algo, void *ctx, |
| 107 | const void *buf, unsigned int size, |
| 108 | int is_last) |
| 109 | { |
| 110 | *((uint16_t *)ctx) = crc16_ccitt(*((uint16_t *)ctx), buf, size); |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | static int hash_finish_crc16_ccitt(struct hash_algo *algo, void *ctx, |
| 115 | void *dest_buf, int size) |
| 116 | { |
| 117 | if (size < algo->digest_size) |
| 118 | return -1; |
| 119 | |
| 120 | *((uint16_t *)dest_buf) = *((uint16_t *)ctx); |
| 121 | free(ctx); |
| 122 | return 0; |
| 123 | } |
| 124 | |
Hung-ying Tyan | f061b1a | 2014-03-03 12:19:28 +0100 | [diff] [blame] | 125 | static int hash_init_crc32(struct hash_algo *algo, void **ctxp) |
| 126 | { |
| 127 | uint32_t *ctx = malloc(sizeof(uint32_t)); |
| 128 | *ctx = 0; |
| 129 | *ctxp = ctx; |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | static int hash_update_crc32(struct hash_algo *algo, void *ctx, |
| 134 | const void *buf, unsigned int size, int is_last) |
| 135 | { |
| 136 | *((uint32_t *)ctx) = crc32(*((uint32_t *)ctx), buf, size); |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | static int hash_finish_crc32(struct hash_algo *algo, void *ctx, void *dest_buf, |
| 141 | int size) |
| 142 | { |
| 143 | if (size < algo->digest_size) |
| 144 | return -1; |
| 145 | |
| 146 | *((uint32_t *)dest_buf) = *((uint32_t *)ctx); |
| 147 | free(ctx); |
| 148 | return 0; |
| 149 | } |
| 150 | |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 151 | /* |
Tom Rini | 03361c7 | 2017-08-14 16:38:07 -0400 | [diff] [blame] | 152 | * These are the hash algorithms we support. If we have hardware acceleration |
| 153 | * is enable we will use that, otherwise a software version of the algorithm. |
| 154 | * Note that algorithm names must be in lower case. |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 155 | */ |
| 156 | static struct hash_algo hash_algo[] = { |
Tom Rini | 03361c7 | 2017-08-14 16:38:07 -0400 | [diff] [blame] | 157 | #ifdef CONFIG_SHA1 |
Akshay Saraswat | cbd8f2c | 2013-03-20 21:00:58 +0000 | [diff] [blame] | 158 | { |
Tom Rini | 03361c7 | 2017-08-14 16:38:07 -0400 | [diff] [blame] | 159 | .name = "sha1", |
| 160 | .digest_size = SHA1_SUM_LEN, |
| 161 | .chunk_size = CHUNKSZ_SHA1, |
| 162 | #ifdef CONFIG_SHA_HW_ACCEL |
| 163 | .hash_func_ws = hw_sha1, |
| 164 | #else |
| 165 | .hash_func_ws = sha1_csum_wd, |
gaurav rana | ef20159 | 2015-02-20 12:51:46 +0530 | [diff] [blame] | 166 | #endif |
gaurav rana | ef20159 | 2015-02-20 12:51:46 +0530 | [diff] [blame] | 167 | #ifdef CONFIG_SHA_PROG_HW_ACCEL |
Tom Rini | 03361c7 | 2017-08-14 16:38:07 -0400 | [diff] [blame] | 168 | .hash_init = hw_sha_init, |
| 169 | .hash_update = hw_sha_update, |
| 170 | .hash_finish = hw_sha_finish, |
| 171 | #else |
| 172 | .hash_init = hash_init_sha1, |
| 173 | .hash_update = hash_update_sha1, |
| 174 | .hash_finish = hash_finish_sha1, |
Akshay Saraswat | cbd8f2c | 2013-03-20 21:00:58 +0000 | [diff] [blame] | 175 | #endif |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 176 | }, |
| 177 | #endif |
| 178 | #ifdef CONFIG_SHA256 |
| 179 | { |
Tom Rini | 03361c7 | 2017-08-14 16:38:07 -0400 | [diff] [blame] | 180 | .name = "sha256", |
| 181 | .digest_size = SHA256_SUM_LEN, |
| 182 | .chunk_size = CHUNKSZ_SHA256, |
| 183 | #ifdef CONFIG_SHA_HW_ACCEL |
| 184 | .hash_func_ws = hw_sha256, |
| 185 | #else |
| 186 | .hash_func_ws = sha256_csum_wd, |
| 187 | #endif |
| 188 | #ifdef CONFIG_SHA_PROG_HW_ACCEL |
| 189 | .hash_init = hw_sha_init, |
| 190 | .hash_update = hw_sha_update, |
| 191 | .hash_finish = hw_sha_finish, |
| 192 | #else |
| 193 | .hash_init = hash_init_sha256, |
| 194 | .hash_update = hash_update_sha256, |
| 195 | .hash_finish = hash_finish_sha256, |
| 196 | #endif |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 197 | }, |
| 198 | #endif |
Simon Glass | 0bbd76f | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 199 | { |
Philipp Tomsich | 6e2ac3e | 2018-11-25 19:22:19 +0100 | [diff] [blame] | 200 | .name = "crc16-ccitt", |
| 201 | .digest_size = 2, |
| 202 | .chunk_size = CHUNKSZ, |
| 203 | .hash_func_ws = crc16_ccitt_wd_buf, |
| 204 | .hash_init = hash_init_crc16_ccitt, |
| 205 | .hash_update = hash_update_crc16_ccitt, |
| 206 | .hash_finish = hash_finish_crc16_ccitt, |
| 207 | }, |
| 208 | { |
Tom Rini | 03361c7 | 2017-08-14 16:38:07 -0400 | [diff] [blame] | 209 | .name = "crc32", |
| 210 | .digest_size = 4, |
| 211 | .chunk_size = CHUNKSZ_CRC32, |
| 212 | .hash_func_ws = crc32_wd_buf, |
| 213 | .hash_init = hash_init_crc32, |
| 214 | .hash_update = hash_update_crc32, |
| 215 | .hash_finish = hash_finish_crc32, |
Simon Glass | 0bbd76f | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 216 | }, |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 217 | }; |
| 218 | |
Simon Glass | 0bbd76f | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 219 | /* Try to minimize code size for boards that don't want much hashing */ |
Daniel Thompson | a9e2c67 | 2017-05-19 17:26:58 +0100 | [diff] [blame] | 220 | #if defined(CONFIG_SHA256) || defined(CONFIG_CMD_SHA1SUM) || \ |
| 221 | defined(CONFIG_CRC32_VERIFY) || defined(CONFIG_CMD_HASH) |
Simon Glass | 0bbd76f | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 222 | #define multi_hash() 1 |
| 223 | #else |
| 224 | #define multi_hash() 0 |
| 225 | #endif |
| 226 | |
T Karthik Reddy | a098b43 | 2019-03-16 15:23:01 +0530 | [diff] [blame] | 227 | static void reloc_update(void) |
| 228 | { |
| 229 | #if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC) |
| 230 | int i; |
| 231 | static bool done; |
| 232 | |
| 233 | if (!done) { |
| 234 | done = true; |
| 235 | for (i = 0; i < ARRAY_SIZE(hash_algo); i++) { |
| 236 | hash_algo[i].name += gd->reloc_off; |
| 237 | hash_algo[i].hash_func_ws += gd->reloc_off; |
| 238 | hash_algo[i].hash_init += gd->reloc_off; |
| 239 | hash_algo[i].hash_update += gd->reloc_off; |
| 240 | hash_algo[i].hash_finish += gd->reloc_off; |
| 241 | } |
| 242 | } |
| 243 | #endif |
| 244 | } |
| 245 | |
Ruchika Gupta | c915ceb | 2015-01-23 16:01:58 +0530 | [diff] [blame] | 246 | int hash_lookup_algo(const char *algo_name, struct hash_algo **algop) |
| 247 | { |
| 248 | int i; |
| 249 | |
T Karthik Reddy | a098b43 | 2019-03-16 15:23:01 +0530 | [diff] [blame] | 250 | reloc_update(); |
| 251 | |
Ruchika Gupta | c915ceb | 2015-01-23 16:01:58 +0530 | [diff] [blame] | 252 | for (i = 0; i < ARRAY_SIZE(hash_algo); i++) { |
| 253 | if (!strcmp(algo_name, hash_algo[i].name)) { |
| 254 | *algop = &hash_algo[i]; |
| 255 | return 0; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | debug("Unknown hash algorithm '%s'\n", algo_name); |
| 260 | return -EPROTONOSUPPORT; |
| 261 | } |
| 262 | |
| 263 | int hash_progressive_lookup_algo(const char *algo_name, |
| 264 | struct hash_algo **algop) |
| 265 | { |
| 266 | int i; |
| 267 | |
T Karthik Reddy | a098b43 | 2019-03-16 15:23:01 +0530 | [diff] [blame] | 268 | reloc_update(); |
| 269 | |
Ruchika Gupta | c915ceb | 2015-01-23 16:01:58 +0530 | [diff] [blame] | 270 | for (i = 0; i < ARRAY_SIZE(hash_algo); i++) { |
| 271 | if (!strcmp(algo_name, hash_algo[i].name)) { |
| 272 | if (hash_algo[i].hash_init) { |
| 273 | *algop = &hash_algo[i]; |
| 274 | return 0; |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | debug("Unknown hash algorithm '%s'\n", algo_name); |
| 280 | return -EPROTONOSUPPORT; |
| 281 | } |
| 282 | |
| 283 | #ifndef USE_HOSTCC |
Stefan Roese | 0ed2e46 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 284 | int hash_parse_string(const char *algo_name, const char *str, uint8_t *result) |
| 285 | { |
| 286 | struct hash_algo *algo; |
| 287 | int ret; |
| 288 | int i; |
| 289 | |
| 290 | ret = hash_lookup_algo(algo_name, &algo); |
| 291 | if (ret) |
| 292 | return ret; |
| 293 | |
| 294 | for (i = 0; i < algo->digest_size; i++) { |
| 295 | char chr[3]; |
| 296 | |
| 297 | strncpy(chr, &str[i * 2], 2); |
| 298 | result[i] = simple_strtoul(chr, NULL, 16); |
| 299 | } |
| 300 | |
| 301 | return 0; |
| 302 | } |
| 303 | |
Tom Rini | 6173838 | 2016-01-05 08:47:48 -0500 | [diff] [blame] | 304 | int hash_block(const char *algo_name, const void *data, unsigned int len, |
| 305 | uint8_t *output, int *output_size) |
| 306 | { |
| 307 | struct hash_algo *algo; |
| 308 | int ret; |
| 309 | |
| 310 | ret = hash_lookup_algo(algo_name, &algo); |
| 311 | if (ret) |
| 312 | return ret; |
| 313 | |
| 314 | if (output_size && *output_size < algo->digest_size) { |
| 315 | debug("Output buffer size %d too small (need %d bytes)", |
| 316 | *output_size, algo->digest_size); |
| 317 | return -ENOSPC; |
| 318 | } |
| 319 | if (output_size) |
| 320 | *output_size = algo->digest_size; |
| 321 | algo->hash_func_ws(data, len, output, algo->chunk_size); |
| 322 | |
| 323 | return 0; |
| 324 | } |
| 325 | |
| 326 | #if defined(CONFIG_CMD_HASH) || defined(CONFIG_CMD_SHA1SUM) || defined(CONFIG_CMD_CRC32) |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 327 | /** |
| 328 | * store_result: Store the resulting sum to an address or variable |
| 329 | * |
| 330 | * @algo: Hash algorithm being used |
| 331 | * @sum: Hash digest (algo->digest_size bytes) |
| 332 | * @dest: Destination, interpreted as a hex address if it starts |
Simon Glass | 80e345a | 2013-02-24 17:33:26 +0000 | [diff] [blame] | 333 | * with * (or allow_env_vars is 0) or otherwise as an |
| 334 | * environment variable. |
| 335 | * @allow_env_vars: non-zero to permit storing the result to an |
| 336 | * variable environment |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 337 | */ |
Simon Glass | a3b240b | 2014-06-12 07:24:41 -0600 | [diff] [blame] | 338 | static void store_result(struct hash_algo *algo, const uint8_t *sum, |
Simon Glass | 80e345a | 2013-02-24 17:33:26 +0000 | [diff] [blame] | 339 | const char *dest, int allow_env_vars) |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 340 | { |
| 341 | unsigned int i; |
Simon Glass | 80e345a | 2013-02-24 17:33:26 +0000 | [diff] [blame] | 342 | int env_var = 0; |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 343 | |
Simon Glass | 80e345a | 2013-02-24 17:33:26 +0000 | [diff] [blame] | 344 | /* |
| 345 | * If environment variables are allowed, then we assume that 'dest' |
| 346 | * is an environment variable, unless it starts with *, in which |
| 347 | * case we assume it is an address. If not allowed, it is always an |
| 348 | * address. This is to support the crc32 command. |
| 349 | */ |
| 350 | if (allow_env_vars) { |
| 351 | if (*dest == '*') |
| 352 | dest++; |
| 353 | else |
| 354 | env_var = 1; |
| 355 | } |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 356 | |
Simon Glass | 80e345a | 2013-02-24 17:33:26 +0000 | [diff] [blame] | 357 | if (env_var) { |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 358 | char str_output[HASH_MAX_DIGEST_SIZE * 2 + 1]; |
| 359 | char *str_ptr = str_output; |
| 360 | |
| 361 | for (i = 0; i < algo->digest_size; i++) { |
| 362 | sprintf(str_ptr, "%02x", sum[i]); |
| 363 | str_ptr += 2; |
| 364 | } |
Jeroen Hofstee | b14c052 | 2014-06-09 11:02:02 +0200 | [diff] [blame] | 365 | *str_ptr = '\0'; |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 366 | env_set(dest, str_output); |
Simon Glass | 80e345a | 2013-02-24 17:33:26 +0000 | [diff] [blame] | 367 | } else { |
Simon Glass | 68f1556 | 2013-02-24 17:33:31 +0000 | [diff] [blame] | 368 | ulong addr; |
| 369 | void *buf; |
Simon Glass | 80e345a | 2013-02-24 17:33:26 +0000 | [diff] [blame] | 370 | |
Simon Glass | 68f1556 | 2013-02-24 17:33:31 +0000 | [diff] [blame] | 371 | addr = simple_strtoul(dest, NULL, 16); |
| 372 | buf = map_sysmem(addr, algo->digest_size); |
| 373 | memcpy(buf, sum, algo->digest_size); |
| 374 | unmap_sysmem(buf); |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * parse_verify_sum: Parse a hash verification parameter |
| 380 | * |
| 381 | * @algo: Hash algorithm being used |
| 382 | * @verify_str: Argument to parse. If it starts with * then it is |
| 383 | * interpreted as a hex address containing the hash. |
| 384 | * If the length is exactly the right number of hex digits |
| 385 | * for the digest size, then we assume it is a hex digest. |
| 386 | * Otherwise we assume it is an environment variable, and |
| 387 | * look up its value (it must contain a hex digest). |
| 388 | * @vsum: Returns binary digest value (algo->digest_size bytes) |
Simon Glass | 80e345a | 2013-02-24 17:33:26 +0000 | [diff] [blame] | 389 | * @allow_env_vars: non-zero to permit storing the result to an environment |
| 390 | * variable. If 0 then verify_str is assumed to be an |
| 391 | * address, and the * prefix is not expected. |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 392 | * @return 0 if ok, non-zero on error |
| 393 | */ |
Simon Glass | a3b240b | 2014-06-12 07:24:41 -0600 | [diff] [blame] | 394 | static int parse_verify_sum(struct hash_algo *algo, char *verify_str, |
| 395 | uint8_t *vsum, int allow_env_vars) |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 396 | { |
Simon Glass | 80e345a | 2013-02-24 17:33:26 +0000 | [diff] [blame] | 397 | int env_var = 0; |
| 398 | |
| 399 | /* See comment above in store_result() */ |
| 400 | if (allow_env_vars) { |
| 401 | if (*verify_str == '*') |
| 402 | verify_str++; |
| 403 | else |
| 404 | env_var = 1; |
| 405 | } |
| 406 | |
Nikolay Dimitrov | 810b84d | 2014-12-12 20:01:23 +0200 | [diff] [blame] | 407 | if (!env_var) { |
Simon Glass | 68f1556 | 2013-02-24 17:33:31 +0000 | [diff] [blame] | 408 | ulong addr; |
| 409 | void *buf; |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 410 | |
Simon Glass | 68f1556 | 2013-02-24 17:33:31 +0000 | [diff] [blame] | 411 | addr = simple_strtoul(verify_str, NULL, 16); |
| 412 | buf = map_sysmem(addr, algo->digest_size); |
| 413 | memcpy(vsum, buf, algo->digest_size); |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 414 | } else { |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 415 | char *vsum_str; |
| 416 | int digits = algo->digest_size * 2; |
| 417 | |
| 418 | /* |
| 419 | * As with the original code from sha1sum.c, we assume that a |
| 420 | * string which matches the digest size exactly is a hex |
| 421 | * string and not an environment variable. |
| 422 | */ |
| 423 | if (strlen(verify_str) == digits) |
| 424 | vsum_str = verify_str; |
| 425 | else { |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 426 | vsum_str = env_get(verify_str); |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 427 | if (vsum_str == NULL || strlen(vsum_str) != digits) { |
| 428 | printf("Expected %d hex digits in env var\n", |
| 429 | digits); |
| 430 | return 1; |
| 431 | } |
| 432 | } |
| 433 | |
Stefan Roese | 0ed2e46 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 434 | hash_parse_string(algo->name, vsum_str, vsum); |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 435 | } |
| 436 | return 0; |
| 437 | } |
| 438 | |
Tom Rini | 6173838 | 2016-01-05 08:47:48 -0500 | [diff] [blame] | 439 | static void hash_show(struct hash_algo *algo, ulong addr, ulong len, uint8_t *output) |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 440 | { |
| 441 | int i; |
| 442 | |
| 443 | printf("%s for %08lx ... %08lx ==> ", algo->name, addr, addr + len - 1); |
| 444 | for (i = 0; i < algo->digest_size; i++) |
| 445 | printf("%02x", output[i]); |
| 446 | } |
| 447 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 448 | int hash_command(const char *algo_name, int flags, struct cmd_tbl *cmdtp, |
| 449 | int flag, int argc, char *const argv[]) |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 450 | { |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 451 | ulong addr, len; |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 452 | |
Nikolay Dimitrov | 810b84d | 2014-12-12 20:01:23 +0200 | [diff] [blame] | 453 | if ((argc < 2) || ((flags & HASH_FLAG_VERIFY) && (argc < 3))) |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 454 | return CMD_RET_USAGE; |
| 455 | |
Simon Glass | 80e345a | 2013-02-24 17:33:26 +0000 | [diff] [blame] | 456 | addr = simple_strtoul(*argv++, NULL, 16); |
| 457 | len = simple_strtoul(*argv++, NULL, 16); |
| 458 | |
Simon Glass | 0bbd76f | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 459 | if (multi_hash()) { |
| 460 | struct hash_algo *algo; |
Breno Lima | 45e6512 | 2018-01-17 10:03:45 -0200 | [diff] [blame] | 461 | u8 *output; |
Simon Glass | a3b240b | 2014-06-12 07:24:41 -0600 | [diff] [blame] | 462 | uint8_t vsum[HASH_MAX_DIGEST_SIZE]; |
Simon Glass | 68f1556 | 2013-02-24 17:33:31 +0000 | [diff] [blame] | 463 | void *buf; |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 464 | |
Hung-ying Tyan | f061b1a | 2014-03-03 12:19:28 +0100 | [diff] [blame] | 465 | if (hash_lookup_algo(algo_name, &algo)) { |
Simon Glass | 0bbd76f | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 466 | printf("Unknown hash algorithm '%s'\n", algo_name); |
| 467 | return CMD_RET_USAGE; |
| 468 | } |
| 469 | argc -= 2; |
| 470 | |
| 471 | if (algo->digest_size > HASH_MAX_DIGEST_SIZE) { |
| 472 | puts("HASH_MAX_DIGEST_SIZE exceeded\n"); |
| 473 | return 1; |
| 474 | } |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 475 | |
Breno Lima | 45e6512 | 2018-01-17 10:03:45 -0200 | [diff] [blame] | 476 | output = memalign(ARCH_DMA_MINALIGN, |
| 477 | sizeof(uint32_t) * HASH_MAX_DIGEST_SIZE); |
| 478 | |
Simon Glass | 68f1556 | 2013-02-24 17:33:31 +0000 | [diff] [blame] | 479 | buf = map_sysmem(addr, len); |
| 480 | algo->hash_func_ws(buf, len, output, algo->chunk_size); |
| 481 | unmap_sysmem(buf); |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 482 | |
Simon Glass | 0bbd76f | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 483 | /* Try to avoid code bloat when verify is not needed */ |
Daniel Thompson | a9e2c67 | 2017-05-19 17:26:58 +0100 | [diff] [blame] | 484 | #if defined(CONFIG_CRC32_VERIFY) || defined(CONFIG_SHA1SUM_VERIFY) || \ |
| 485 | defined(CONFIG_HASH_VERIFY) |
Simon Glass | 0bbd76f | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 486 | if (flags & HASH_FLAG_VERIFY) { |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 487 | #else |
Simon Glass | 0bbd76f | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 488 | if (0) { |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 489 | #endif |
Simon Glass | 0bbd76f | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 490 | if (parse_verify_sum(algo, *argv, vsum, |
Simon Glass | 80e345a | 2013-02-24 17:33:26 +0000 | [diff] [blame] | 491 | flags & HASH_FLAG_ENV)) { |
Simon Glass | 0bbd76f | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 492 | printf("ERROR: %s does not contain a valid " |
| 493 | "%s sum\n", *argv, algo->name); |
| 494 | return 1; |
| 495 | } |
| 496 | if (memcmp(output, vsum, algo->digest_size) != 0) { |
| 497 | int i; |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 498 | |
Simon Glass | 7f3fa54 | 2014-06-02 22:04:49 -0600 | [diff] [blame] | 499 | hash_show(algo, addr, len, output); |
Simon Glass | 0bbd76f | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 500 | printf(" != "); |
| 501 | for (i = 0; i < algo->digest_size; i++) |
| 502 | printf("%02x", vsum[i]); |
| 503 | puts(" ** ERROR **\n"); |
| 504 | return 1; |
| 505 | } |
| 506 | } else { |
Simon Glass | 7f3fa54 | 2014-06-02 22:04:49 -0600 | [diff] [blame] | 507 | hash_show(algo, addr, len, output); |
Simon Glass | 0bbd76f | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 508 | printf("\n"); |
| 509 | |
| 510 | if (argc) { |
| 511 | store_result(algo, output, *argv, |
| 512 | flags & HASH_FLAG_ENV); |
| 513 | } |
Breno Lima | 45e6512 | 2018-01-17 10:03:45 -0200 | [diff] [blame] | 514 | unmap_sysmem(output); |
| 515 | |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 516 | } |
Simon Glass | 0bbd76f | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 517 | |
| 518 | /* Horrible code size hack for boards that just want crc32 */ |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 519 | } else { |
Simon Glass | 0bbd76f | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 520 | ulong crc; |
| 521 | ulong *ptr; |
| 522 | |
| 523 | crc = crc32_wd(0, (const uchar *)addr, len, CHUNKSZ_CRC32); |
| 524 | |
| 525 | printf("CRC32 for %08lx ... %08lx ==> %08lx\n", |
| 526 | addr, addr + len - 1, crc); |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 527 | |
Tom Rini | 62780f0 | 2013-11-07 07:39:48 -0500 | [diff] [blame] | 528 | if (argc >= 3) { |
| 529 | ptr = (ulong *)simple_strtoul(argv[0], NULL, 16); |
Simon Glass | 0bbd76f | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 530 | *ptr = crc; |
Simon Glass | 80e345a | 2013-02-24 17:33:26 +0000 | [diff] [blame] | 531 | } |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | return 0; |
| 535 | } |
Simon Glass | b22ec7a | 2017-05-17 09:05:34 -0600 | [diff] [blame] | 536 | #endif /* CONFIG_CMD_HASH || CONFIG_CMD_SHA1SUM || CONFIG_CMD_CRC32) */ |
| 537 | #endif /* !USE_HOSTCC */ |