]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
460408ef SG |
2 | /* |
3 | * Copyright (c) 2012 The Chromium OS Authors. | |
4 | * | |
5 | * (C) Copyright 2011 | |
6 | * Joe Hershberger, National Instruments, [email protected] | |
7 | * | |
8 | * (C) Copyright 2000 | |
9 | * Wolfgang Denk, DENX Software Engineering, [email protected]. | |
460408ef SG |
10 | */ |
11 | ||
2dd90027 | 12 | #ifndef USE_HOSTCC |
460408ef SG |
13 | #include <common.h> |
14 | #include <command.h> | |
9fb625ce | 15 | #include <env.h> |
f7ae49fc | 16 | #include <log.h> |
bf007ebb | 17 | #include <malloc.h> |
0eb25b61 | 18 | #include <mapmem.h> |
1f9c9280 | 19 | #include <hw_sha.h> |
90526e9f | 20 | #include <asm/cache.h> |
401d1c4f | 21 | #include <asm/global_data.h> |
2dd90027 | 22 | #include <asm/io.h> |
1221ce45 | 23 | #include <linux/errno.h> |
3db71108 | 24 | #include <u-boot/crc.h> |
2dd90027 RG |
25 | #else |
26 | #include "mkimage.h" | |
27 | #include <time.h> | |
2dd90027 RG |
28 | #endif /* !USE_HOSTCC*/ |
29 | ||
460408ef | 30 | #include <hash.h> |
4d72caa5 | 31 | #include <image.h> |
2dd90027 | 32 | #include <u-boot/crc.h> |
2b9912e6 JH |
33 | #include <u-boot/sha1.h> |
34 | #include <u-boot/sha256.h> | |
d16b38f4 | 35 | #include <u-boot/sha512.h> |
2dd90027 | 36 | #include <u-boot/md5.h> |
460408ef | 37 | |
10088fb0 KR |
38 | #if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC) |
39 | DECLARE_GLOBAL_DATA_PTR; | |
40 | #endif | |
41 | ||
42 | static void reloc_update(void); | |
43 | ||
78eda89e | 44 | #if defined(CONFIG_SHA1) && !defined(CONFIG_SHA_PROG_HW_ACCEL) |
bf007ebb HT |
45 | static int hash_init_sha1(struct hash_algo *algo, void **ctxp) |
46 | { | |
47 | sha1_context *ctx = malloc(sizeof(sha1_context)); | |
48 | sha1_starts(ctx); | |
49 | *ctxp = ctx; | |
50 | return 0; | |
51 | } | |
52 | ||
53 | static int hash_update_sha1(struct hash_algo *algo, void *ctx, const void *buf, | |
54 | unsigned int size, int is_last) | |
55 | { | |
56 | sha1_update((sha1_context *)ctx, buf, size); | |
57 | return 0; | |
58 | } | |
59 | ||
60 | static int hash_finish_sha1(struct hash_algo *algo, void *ctx, void *dest_buf, | |
61 | int size) | |
62 | { | |
63 | if (size < algo->digest_size) | |
64 | return -1; | |
65 | ||
66 | sha1_finish((sha1_context *)ctx, dest_buf); | |
67 | free(ctx); | |
68 | return 0; | |
69 | } | |
70 | #endif | |
71 | ||
78eda89e | 72 | #if defined(CONFIG_SHA256) && !defined(CONFIG_SHA_PROG_HW_ACCEL) |
bf007ebb HT |
73 | static int hash_init_sha256(struct hash_algo *algo, void **ctxp) |
74 | { | |
75 | sha256_context *ctx = malloc(sizeof(sha256_context)); | |
76 | sha256_starts(ctx); | |
77 | *ctxp = ctx; | |
78 | return 0; | |
79 | } | |
80 | ||
81 | static int hash_update_sha256(struct hash_algo *algo, void *ctx, | |
82 | const void *buf, unsigned int size, int is_last) | |
83 | { | |
84 | sha256_update((sha256_context *)ctx, buf, size); | |
85 | return 0; | |
86 | } | |
87 | ||
88 | static int hash_finish_sha256(struct hash_algo *algo, void *ctx, void | |
89 | *dest_buf, int size) | |
90 | { | |
91 | if (size < algo->digest_size) | |
92 | return -1; | |
93 | ||
94 | sha256_finish((sha256_context *)ctx, dest_buf); | |
95 | free(ctx); | |
96 | return 0; | |
97 | } | |
98 | #endif | |
99 | ||
a479f103 | 100 | #if defined(CONFIG_SHA384) && !defined(CONFIG_SHA_PROG_HW_ACCEL) |
d16b38f4 RD |
101 | static int hash_init_sha384(struct hash_algo *algo, void **ctxp) |
102 | { | |
103 | sha512_context *ctx = malloc(sizeof(sha512_context)); | |
104 | sha384_starts(ctx); | |
105 | *ctxp = ctx; | |
106 | return 0; | |
107 | } | |
108 | ||
109 | static int hash_update_sha384(struct hash_algo *algo, void *ctx, | |
110 | const void *buf, unsigned int size, int is_last) | |
111 | { | |
112 | sha384_update((sha512_context *)ctx, buf, size); | |
113 | return 0; | |
114 | } | |
115 | ||
116 | static int hash_finish_sha384(struct hash_algo *algo, void *ctx, void | |
117 | *dest_buf, int size) | |
118 | { | |
119 | if (size < algo->digest_size) | |
120 | return -1; | |
121 | ||
122 | sha384_finish((sha512_context *)ctx, dest_buf); | |
123 | free(ctx); | |
124 | return 0; | |
125 | } | |
126 | #endif | |
127 | ||
a479f103 | 128 | #if defined(CONFIG_SHA512) && !defined(CONFIG_SHA_PROG_HW_ACCEL) |
d16b38f4 RD |
129 | static int hash_init_sha512(struct hash_algo *algo, void **ctxp) |
130 | { | |
131 | sha512_context *ctx = malloc(sizeof(sha512_context)); | |
132 | sha512_starts(ctx); | |
133 | *ctxp = ctx; | |
134 | return 0; | |
135 | } | |
136 | ||
137 | static int hash_update_sha512(struct hash_algo *algo, void *ctx, | |
138 | const void *buf, unsigned int size, int is_last) | |
139 | { | |
140 | sha512_update((sha512_context *)ctx, buf, size); | |
141 | return 0; | |
142 | } | |
143 | ||
144 | static int hash_finish_sha512(struct hash_algo *algo, void *ctx, void | |
145 | *dest_buf, int size) | |
146 | { | |
147 | if (size < algo->digest_size) | |
148 | return -1; | |
149 | ||
d16b38f4 RD |
150 | sha512_finish((sha512_context *)ctx, dest_buf); |
151 | free(ctx); | |
152 | return 0; | |
153 | } | |
154 | #endif | |
155 | ||
156 | ||
51c2345b PT |
157 | static int hash_init_crc16_ccitt(struct hash_algo *algo, void **ctxp) |
158 | { | |
159 | uint16_t *ctx = malloc(sizeof(uint16_t)); | |
160 | *ctx = 0; | |
161 | *ctxp = ctx; | |
162 | return 0; | |
163 | } | |
164 | ||
165 | static int hash_update_crc16_ccitt(struct hash_algo *algo, void *ctx, | |
166 | const void *buf, unsigned int size, | |
167 | int is_last) | |
168 | { | |
169 | *((uint16_t *)ctx) = crc16_ccitt(*((uint16_t *)ctx), buf, size); | |
170 | return 0; | |
171 | } | |
172 | ||
173 | static int hash_finish_crc16_ccitt(struct hash_algo *algo, void *ctx, | |
174 | void *dest_buf, int size) | |
175 | { | |
176 | if (size < algo->digest_size) | |
177 | return -1; | |
178 | ||
179 | *((uint16_t *)dest_buf) = *((uint16_t *)ctx); | |
180 | free(ctx); | |
181 | return 0; | |
182 | } | |
183 | ||
bf007ebb HT |
184 | static int hash_init_crc32(struct hash_algo *algo, void **ctxp) |
185 | { | |
186 | uint32_t *ctx = malloc(sizeof(uint32_t)); | |
187 | *ctx = 0; | |
188 | *ctxp = ctx; | |
189 | return 0; | |
190 | } | |
191 | ||
192 | static int hash_update_crc32(struct hash_algo *algo, void *ctx, | |
193 | const void *buf, unsigned int size, int is_last) | |
194 | { | |
195 | *((uint32_t *)ctx) = crc32(*((uint32_t *)ctx), buf, size); | |
196 | return 0; | |
197 | } | |
198 | ||
199 | static int hash_finish_crc32(struct hash_algo *algo, void *ctx, void *dest_buf, | |
200 | int size) | |
201 | { | |
202 | if (size < algo->digest_size) | |
203 | return -1; | |
204 | ||
205 | *((uint32_t *)dest_buf) = *((uint32_t *)ctx); | |
206 | free(ctx); | |
207 | return 0; | |
208 | } | |
209 | ||
460408ef | 210 | /* |
78eda89e TR |
211 | * These are the hash algorithms we support. If we have hardware acceleration |
212 | * is enable we will use that, otherwise a software version of the algorithm. | |
213 | * Note that algorithm names must be in lower case. | |
460408ef SG |
214 | */ |
215 | static struct hash_algo hash_algo[] = { | |
78eda89e | 216 | #ifdef CONFIG_SHA1 |
1f9c9280 | 217 | { |
78eda89e TR |
218 | .name = "sha1", |
219 | .digest_size = SHA1_SUM_LEN, | |
220 | .chunk_size = CHUNKSZ_SHA1, | |
221 | #ifdef CONFIG_SHA_HW_ACCEL | |
222 | .hash_func_ws = hw_sha1, | |
223 | #else | |
224 | .hash_func_ws = sha1_csum_wd, | |
94e3c8c4 | 225 | #endif |
94e3c8c4 | 226 | #ifdef CONFIG_SHA_PROG_HW_ACCEL |
78eda89e TR |
227 | .hash_init = hw_sha_init, |
228 | .hash_update = hw_sha_update, | |
229 | .hash_finish = hw_sha_finish, | |
230 | #else | |
231 | .hash_init = hash_init_sha1, | |
232 | .hash_update = hash_update_sha1, | |
233 | .hash_finish = hash_finish_sha1, | |
1f9c9280 | 234 | #endif |
460408ef SG |
235 | }, |
236 | #endif | |
237 | #ifdef CONFIG_SHA256 | |
238 | { | |
78eda89e TR |
239 | .name = "sha256", |
240 | .digest_size = SHA256_SUM_LEN, | |
241 | .chunk_size = CHUNKSZ_SHA256, | |
242 | #ifdef CONFIG_SHA_HW_ACCEL | |
243 | .hash_func_ws = hw_sha256, | |
244 | #else | |
245 | .hash_func_ws = sha256_csum_wd, | |
246 | #endif | |
247 | #ifdef CONFIG_SHA_PROG_HW_ACCEL | |
248 | .hash_init = hw_sha_init, | |
249 | .hash_update = hw_sha_update, | |
250 | .hash_finish = hw_sha_finish, | |
251 | #else | |
252 | .hash_init = hash_init_sha256, | |
253 | .hash_update = hash_update_sha256, | |
254 | .hash_finish = hash_finish_sha256, | |
255 | #endif | |
460408ef | 256 | }, |
d16b38f4 RD |
257 | #endif |
258 | #ifdef CONFIG_SHA384 | |
259 | { | |
260 | .name = "sha384", | |
261 | .digest_size = SHA384_SUM_LEN, | |
262 | .chunk_size = CHUNKSZ_SHA384, | |
e2ae483c | 263 | #ifdef CONFIG_SHA512_HW_ACCEL |
a479f103 JS |
264 | .hash_func_ws = hw_sha384, |
265 | #else | |
d16b38f4 | 266 | .hash_func_ws = sha384_csum_wd, |
a479f103 | 267 | #endif |
e2ae483c | 268 | #if defined(CONFIG_SHA512_HW_ACCEL) && defined(CONFIG_SHA_PROG_HW_ACCEL) |
a479f103 JS |
269 | .hash_init = hw_sha_init, |
270 | .hash_update = hw_sha_update, | |
271 | .hash_finish = hw_sha_finish, | |
272 | #else | |
d16b38f4 RD |
273 | .hash_init = hash_init_sha384, |
274 | .hash_update = hash_update_sha384, | |
275 | .hash_finish = hash_finish_sha384, | |
a479f103 | 276 | #endif |
d16b38f4 RD |
277 | }, |
278 | #endif | |
279 | #ifdef CONFIG_SHA512 | |
280 | { | |
281 | .name = "sha512", | |
282 | .digest_size = SHA512_SUM_LEN, | |
283 | .chunk_size = CHUNKSZ_SHA512, | |
e2ae483c | 284 | #ifdef CONFIG_SHA512_HW_ACCEL |
a479f103 JS |
285 | .hash_func_ws = hw_sha512, |
286 | #else | |
d16b38f4 | 287 | .hash_func_ws = sha512_csum_wd, |
a479f103 | 288 | #endif |
e2ae483c | 289 | #if defined(CONFIG_SHA512_HW_ACCEL) && defined(CONFIG_SHA_PROG_HW_ACCEL) |
a479f103 JS |
290 | .hash_init = hw_sha_init, |
291 | .hash_update = hw_sha_update, | |
292 | .hash_finish = hw_sha_finish, | |
293 | #else | |
d16b38f4 RD |
294 | .hash_init = hash_init_sha512, |
295 | .hash_update = hash_update_sha512, | |
296 | .hash_finish = hash_finish_sha512, | |
a479f103 | 297 | #endif |
d16b38f4 | 298 | }, |
460408ef | 299 | #endif |
51c2345b PT |
300 | { |
301 | .name = "crc16-ccitt", | |
302 | .digest_size = 2, | |
303 | .chunk_size = CHUNKSZ, | |
304 | .hash_func_ws = crc16_ccitt_wd_buf, | |
305 | .hash_init = hash_init_crc16_ccitt, | |
306 | .hash_update = hash_update_crc16_ccitt, | |
307 | .hash_finish = hash_finish_crc16_ccitt, | |
308 | }, | |
d20a40de | 309 | { |
78eda89e TR |
310 | .name = "crc32", |
311 | .digest_size = 4, | |
312 | .chunk_size = CHUNKSZ_CRC32, | |
313 | .hash_func_ws = crc32_wd_buf, | |
314 | .hash_init = hash_init_crc32, | |
315 | .hash_update = hash_update_crc32, | |
316 | .hash_finish = hash_finish_crc32, | |
d20a40de | 317 | }, |
460408ef SG |
318 | }; |
319 | ||
d20a40de | 320 | /* Try to minimize code size for boards that don't want much hashing */ |
221a949e | 321 | #if defined(CONFIG_SHA256) || defined(CONFIG_CMD_SHA1SUM) || \ |
d16b38f4 RD |
322 | defined(CONFIG_CRC32_VERIFY) || defined(CONFIG_CMD_HASH) || \ |
323 | defined(CONFIG_SHA384) || defined(CONFIG_SHA512) | |
d20a40de SG |
324 | #define multi_hash() 1 |
325 | #else | |
326 | #define multi_hash() 0 | |
327 | #endif | |
328 | ||
10088fb0 KR |
329 | static void reloc_update(void) |
330 | { | |
331 | #if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC) | |
332 | int i; | |
333 | static bool done; | |
334 | ||
335 | if (!done) { | |
336 | done = true; | |
337 | for (i = 0; i < ARRAY_SIZE(hash_algo); i++) { | |
338 | hash_algo[i].name += gd->reloc_off; | |
339 | hash_algo[i].hash_func_ws += gd->reloc_off; | |
340 | hash_algo[i].hash_init += gd->reloc_off; | |
341 | hash_algo[i].hash_update += gd->reloc_off; | |
342 | hash_algo[i].hash_finish += gd->reloc_off; | |
343 | } | |
344 | } | |
345 | #endif | |
346 | } | |
347 | ||
2dd90027 RG |
348 | int hash_lookup_algo(const char *algo_name, struct hash_algo **algop) |
349 | { | |
350 | int i; | |
351 | ||
10088fb0 KR |
352 | reloc_update(); |
353 | ||
2dd90027 RG |
354 | for (i = 0; i < ARRAY_SIZE(hash_algo); i++) { |
355 | if (!strcmp(algo_name, hash_algo[i].name)) { | |
356 | *algop = &hash_algo[i]; | |
357 | return 0; | |
358 | } | |
359 | } | |
360 | ||
361 | debug("Unknown hash algorithm '%s'\n", algo_name); | |
362 | return -EPROTONOSUPPORT; | |
363 | } | |
364 | ||
365 | int hash_progressive_lookup_algo(const char *algo_name, | |
366 | struct hash_algo **algop) | |
367 | { | |
368 | int i; | |
369 | ||
10088fb0 KR |
370 | reloc_update(); |
371 | ||
2dd90027 RG |
372 | for (i = 0; i < ARRAY_SIZE(hash_algo); i++) { |
373 | if (!strcmp(algo_name, hash_algo[i].name)) { | |
374 | if (hash_algo[i].hash_init) { | |
375 | *algop = &hash_algo[i]; | |
376 | return 0; | |
377 | } | |
378 | } | |
379 | } | |
380 | ||
381 | debug("Unknown hash algorithm '%s'\n", algo_name); | |
382 | return -EPROTONOSUPPORT; | |
383 | } | |
384 | ||
385 | #ifndef USE_HOSTCC | |
8f0b1e24 SR |
386 | int hash_parse_string(const char *algo_name, const char *str, uint8_t *result) |
387 | { | |
388 | struct hash_algo *algo; | |
389 | int ret; | |
390 | int i; | |
391 | ||
392 | ret = hash_lookup_algo(algo_name, &algo); | |
393 | if (ret) | |
394 | return ret; | |
395 | ||
396 | for (i = 0; i < algo->digest_size; i++) { | |
397 | char chr[3]; | |
398 | ||
031725f8 | 399 | strlcpy(chr, &str[i * 2], 3); |
7e5f460e | 400 | result[i] = hextoul(chr, NULL); |
8f0b1e24 SR |
401 | } |
402 | ||
403 | return 0; | |
404 | } | |
405 | ||
48ad68de TR |
406 | int hash_block(const char *algo_name, const void *data, unsigned int len, |
407 | uint8_t *output, int *output_size) | |
408 | { | |
409 | struct hash_algo *algo; | |
410 | int ret; | |
411 | ||
412 | ret = hash_lookup_algo(algo_name, &algo); | |
413 | if (ret) | |
414 | return ret; | |
415 | ||
416 | if (output_size && *output_size < algo->digest_size) { | |
417 | debug("Output buffer size %d too small (need %d bytes)", | |
418 | *output_size, algo->digest_size); | |
419 | return -ENOSPC; | |
420 | } | |
421 | if (output_size) | |
422 | *output_size = algo->digest_size; | |
423 | algo->hash_func_ws(data, len, output, algo->chunk_size); | |
424 | ||
425 | return 0; | |
426 | } | |
427 | ||
428 | #if defined(CONFIG_CMD_HASH) || defined(CONFIG_CMD_SHA1SUM) || defined(CONFIG_CMD_CRC32) | |
460408ef SG |
429 | /** |
430 | * store_result: Store the resulting sum to an address or variable | |
431 | * | |
432 | * @algo: Hash algorithm being used | |
433 | * @sum: Hash digest (algo->digest_size bytes) | |
434 | * @dest: Destination, interpreted as a hex address if it starts | |
d5b76673 SG |
435 | * with * (or allow_env_vars is 0) or otherwise as an |
436 | * environment variable. | |
437 | * @allow_env_vars: non-zero to permit storing the result to an | |
438 | * variable environment | |
460408ef | 439 | */ |
04819a4f | 440 | static void store_result(struct hash_algo *algo, const uint8_t *sum, |
d5b76673 | 441 | const char *dest, int allow_env_vars) |
460408ef SG |
442 | { |
443 | unsigned int i; | |
d5b76673 | 444 | int env_var = 0; |
460408ef | 445 | |
d5b76673 SG |
446 | /* |
447 | * If environment variables are allowed, then we assume that 'dest' | |
448 | * is an environment variable, unless it starts with *, in which | |
449 | * case we assume it is an address. If not allowed, it is always an | |
450 | * address. This is to support the crc32 command. | |
451 | */ | |
452 | if (allow_env_vars) { | |
453 | if (*dest == '*') | |
454 | dest++; | |
455 | else | |
456 | env_var = 1; | |
457 | } | |
460408ef | 458 | |
d5b76673 | 459 | if (env_var) { |
460408ef SG |
460 | char str_output[HASH_MAX_DIGEST_SIZE * 2 + 1]; |
461 | char *str_ptr = str_output; | |
462 | ||
463 | for (i = 0; i < algo->digest_size; i++) { | |
464 | sprintf(str_ptr, "%02x", sum[i]); | |
465 | str_ptr += 2; | |
466 | } | |
8b9cc866 | 467 | *str_ptr = '\0'; |
382bee57 | 468 | env_set(dest, str_output); |
d5b76673 | 469 | } else { |
bd091b67 SG |
470 | ulong addr; |
471 | void *buf; | |
d5b76673 | 472 | |
7e5f460e | 473 | addr = hextoul(dest, NULL); |
bd091b67 SG |
474 | buf = map_sysmem(addr, algo->digest_size); |
475 | memcpy(buf, sum, algo->digest_size); | |
476 | unmap_sysmem(buf); | |
460408ef SG |
477 | } |
478 | } | |
479 | ||
480 | /** | |
481 | * parse_verify_sum: Parse a hash verification parameter | |
482 | * | |
483 | * @algo: Hash algorithm being used | |
484 | * @verify_str: Argument to parse. If it starts with * then it is | |
485 | * interpreted as a hex address containing the hash. | |
486 | * If the length is exactly the right number of hex digits | |
487 | * for the digest size, then we assume it is a hex digest. | |
488 | * Otherwise we assume it is an environment variable, and | |
489 | * look up its value (it must contain a hex digest). | |
490 | * @vsum: Returns binary digest value (algo->digest_size bytes) | |
d5b76673 SG |
491 | * @allow_env_vars: non-zero to permit storing the result to an environment |
492 | * variable. If 0 then verify_str is assumed to be an | |
493 | * address, and the * prefix is not expected. | |
460408ef SG |
494 | * @return 0 if ok, non-zero on error |
495 | */ | |
04819a4f SG |
496 | static int parse_verify_sum(struct hash_algo *algo, char *verify_str, |
497 | uint8_t *vsum, int allow_env_vars) | |
460408ef | 498 | { |
d5b76673 SG |
499 | int env_var = 0; |
500 | ||
501 | /* See comment above in store_result() */ | |
502 | if (allow_env_vars) { | |
503 | if (*verify_str == '*') | |
504 | verify_str++; | |
505 | else | |
506 | env_var = 1; | |
507 | } | |
508 | ||
3ef46a99 | 509 | if (!env_var) { |
bd091b67 SG |
510 | ulong addr; |
511 | void *buf; | |
460408ef | 512 | |
7e5f460e | 513 | addr = hextoul(verify_str, NULL); |
bd091b67 SG |
514 | buf = map_sysmem(addr, algo->digest_size); |
515 | memcpy(vsum, buf, algo->digest_size); | |
460408ef | 516 | } else { |
460408ef SG |
517 | char *vsum_str; |
518 | int digits = algo->digest_size * 2; | |
519 | ||
520 | /* | |
521 | * As with the original code from sha1sum.c, we assume that a | |
522 | * string which matches the digest size exactly is a hex | |
523 | * string and not an environment variable. | |
524 | */ | |
525 | if (strlen(verify_str) == digits) | |
526 | vsum_str = verify_str; | |
527 | else { | |
00caae6d | 528 | vsum_str = env_get(verify_str); |
460408ef SG |
529 | if (vsum_str == NULL || strlen(vsum_str) != digits) { |
530 | printf("Expected %d hex digits in env var\n", | |
531 | digits); | |
532 | return 1; | |
533 | } | |
534 | } | |
535 | ||
8f0b1e24 | 536 | hash_parse_string(algo->name, vsum_str, vsum); |
460408ef SG |
537 | } |
538 | return 0; | |
539 | } | |
540 | ||
48ad68de | 541 | static void hash_show(struct hash_algo *algo, ulong addr, ulong len, uint8_t *output) |
460408ef SG |
542 | { |
543 | int i; | |
544 | ||
545 | printf("%s for %08lx ... %08lx ==> ", algo->name, addr, addr + len - 1); | |
546 | for (i = 0; i < algo->digest_size; i++) | |
547 | printf("%02x", output[i]); | |
548 | } | |
549 | ||
09140113 SG |
550 | int hash_command(const char *algo_name, int flags, struct cmd_tbl *cmdtp, |
551 | int flag, int argc, char *const argv[]) | |
460408ef | 552 | { |
460408ef | 553 | ulong addr, len; |
460408ef | 554 | |
3ef46a99 | 555 | if ((argc < 2) || ((flags & HASH_FLAG_VERIFY) && (argc < 3))) |
460408ef SG |
556 | return CMD_RET_USAGE; |
557 | ||
7e5f460e SG |
558 | addr = hextoul(*argv++, NULL); |
559 | len = hextoul(*argv++, NULL); | |
d5b76673 | 560 | |
d20a40de SG |
561 | if (multi_hash()) { |
562 | struct hash_algo *algo; | |
d7af2baa | 563 | u8 *output; |
04819a4f | 564 | uint8_t vsum[HASH_MAX_DIGEST_SIZE]; |
bd091b67 | 565 | void *buf; |
460408ef | 566 | |
bf007ebb | 567 | if (hash_lookup_algo(algo_name, &algo)) { |
d20a40de SG |
568 | printf("Unknown hash algorithm '%s'\n", algo_name); |
569 | return CMD_RET_USAGE; | |
570 | } | |
571 | argc -= 2; | |
572 | ||
573 | if (algo->digest_size > HASH_MAX_DIGEST_SIZE) { | |
574 | puts("HASH_MAX_DIGEST_SIZE exceeded\n"); | |
575 | return 1; | |
576 | } | |
460408ef | 577 | |
d7af2baa BL |
578 | output = memalign(ARCH_DMA_MINALIGN, |
579 | sizeof(uint32_t) * HASH_MAX_DIGEST_SIZE); | |
580 | ||
bd091b67 SG |
581 | buf = map_sysmem(addr, len); |
582 | algo->hash_func_ws(buf, len, output, algo->chunk_size); | |
583 | unmap_sysmem(buf); | |
460408ef | 584 | |
d20a40de | 585 | /* Try to avoid code bloat when verify is not needed */ |
221a949e DT |
586 | #if defined(CONFIG_CRC32_VERIFY) || defined(CONFIG_SHA1SUM_VERIFY) || \ |
587 | defined(CONFIG_HASH_VERIFY) | |
d20a40de | 588 | if (flags & HASH_FLAG_VERIFY) { |
460408ef | 589 | #else |
d20a40de | 590 | if (0) { |
460408ef | 591 | #endif |
d20a40de | 592 | if (parse_verify_sum(algo, *argv, vsum, |
d5b76673 | 593 | flags & HASH_FLAG_ENV)) { |
d20a40de SG |
594 | printf("ERROR: %s does not contain a valid " |
595 | "%s sum\n", *argv, algo->name); | |
596 | return 1; | |
597 | } | |
598 | if (memcmp(output, vsum, algo->digest_size) != 0) { | |
599 | int i; | |
460408ef | 600 | |
31890ae2 | 601 | hash_show(algo, addr, len, output); |
d20a40de SG |
602 | printf(" != "); |
603 | for (i = 0; i < algo->digest_size; i++) | |
604 | printf("%02x", vsum[i]); | |
605 | puts(" ** ERROR **\n"); | |
606 | return 1; | |
607 | } | |
608 | } else { | |
31890ae2 | 609 | hash_show(algo, addr, len, output); |
d20a40de SG |
610 | printf("\n"); |
611 | ||
612 | if (argc) { | |
613 | store_result(algo, output, *argv, | |
614 | flags & HASH_FLAG_ENV); | |
615 | } | |
d7af2baa BL |
616 | unmap_sysmem(output); |
617 | ||
460408ef | 618 | } |
d20a40de SG |
619 | |
620 | /* Horrible code size hack for boards that just want crc32 */ | |
460408ef | 621 | } else { |
d20a40de SG |
622 | ulong crc; |
623 | ulong *ptr; | |
624 | ||
625 | crc = crc32_wd(0, (const uchar *)addr, len, CHUNKSZ_CRC32); | |
626 | ||
627 | printf("CRC32 for %08lx ... %08lx ==> %08lx\n", | |
628 | addr, addr + len - 1, crc); | |
460408ef | 629 | |
4b756b01 | 630 | if (argc >= 3) { |
7e5f460e | 631 | ptr = (ulong *)hextoul(argv[0], NULL); |
d20a40de | 632 | *ptr = crc; |
d5b76673 | 633 | } |
460408ef SG |
634 | } |
635 | ||
636 | return 0; | |
637 | } | |
d70f919e SG |
638 | #endif /* CONFIG_CMD_HASH || CONFIG_CMD_SHA1SUM || CONFIG_CMD_CRC32) */ |
639 | #endif /* !USE_HOSTCC */ |