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