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