blob: 5c75848b7d92e0a05b1eaebb04a52cd88a45f46d [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassb8790452012-12-05 14:46:36 +00002/*
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 Glassb8790452012-12-05 14:46:36 +000010 */
11
Ruchika Guptac915ceb2015-01-23 16:01:58 +053012#ifndef USE_HOSTCC
Simon Glassb8790452012-12-05 14:46:36 +000013#include <common.h>
14#include <command.h>
Simon Glass5e6201b2019-08-01 09:46:51 -060015#include <env.h>
Simon Glass0f2af882020-05-10 11:40:05 -060016#include <log.h>
Hung-ying Tyanf061b1a2014-03-03 12:19:28 +010017#include <malloc.h>
Joe Hershberger65b905b2015-03-22 17:08:59 -050018#include <mapmem.h>
Akshay Saraswatcbd8f2c2013-03-20 21:00:58 +000019#include <hw_sha.h>
Simon Glass274e0b02020-05-10 11:39:56 -060020#include <asm/cache.h>
Ruchika Guptac915ceb2015-01-23 16:01:58 +053021#include <asm/io.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090022#include <linux/errno.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070023#include <u-boot/crc.h>
Ruchika Guptac915ceb2015-01-23 16:01:58 +053024#else
25#include "mkimage.h"
26#include <time.h>
Ruchika Guptac915ceb2015-01-23 16:01:58 +053027#endif /* !USE_HOSTCC*/
28
Simon Glassb8790452012-12-05 14:46:36 +000029#include <hash.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060030#include <image.h>
Ruchika Guptac915ceb2015-01-23 16:01:58 +053031#include <u-boot/crc.h>
Jeroen Hofsteebfe88fe2014-06-12 22:27:12 +020032#include <u-boot/sha1.h>
33#include <u-boot/sha256.h>
Reuben Dowle1908fd92020-04-16 17:36:52 +120034#include <u-boot/sha512.h>
Ruchika Guptac915ceb2015-01-23 16:01:58 +053035#include <u-boot/md5.h>
Simon Glassb8790452012-12-05 14:46:36 +000036
T Karthik Reddya098b432019-03-16 15:23:01 +053037#if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
38DECLARE_GLOBAL_DATA_PTR;
39#endif
40
41static void reloc_update(void);
42
Tom Rini03361c72017-08-14 16:38:07 -040043#if defined(CONFIG_SHA1) && !defined(CONFIG_SHA_PROG_HW_ACCEL)
Hung-ying Tyanf061b1a2014-03-03 12:19:28 +010044static int hash_init_sha1(struct hash_algo *algo, void **ctxp)
45{
46 sha1_context *ctx = malloc(sizeof(sha1_context));
47 sha1_starts(ctx);
48 *ctxp = ctx;
49 return 0;
50}
51
52static int hash_update_sha1(struct hash_algo *algo, void *ctx, const void *buf,
53 unsigned int size, int is_last)
54{
55 sha1_update((sha1_context *)ctx, buf, size);
56 return 0;
57}
58
59static int hash_finish_sha1(struct hash_algo *algo, void *ctx, void *dest_buf,
60 int size)
61{
62 if (size < algo->digest_size)
63 return -1;
64
65 sha1_finish((sha1_context *)ctx, dest_buf);
66 free(ctx);
67 return 0;
68}
69#endif
70
Tom Rini03361c72017-08-14 16:38:07 -040071#if defined(CONFIG_SHA256) && !defined(CONFIG_SHA_PROG_HW_ACCEL)
Hung-ying Tyanf061b1a2014-03-03 12:19:28 +010072static int hash_init_sha256(struct hash_algo *algo, void **ctxp)
73{
74 sha256_context *ctx = malloc(sizeof(sha256_context));
75 sha256_starts(ctx);
76 *ctxp = ctx;
77 return 0;
78}
79
80static int hash_update_sha256(struct hash_algo *algo, void *ctx,
81 const void *buf, unsigned int size, int is_last)
82{
83 sha256_update((sha256_context *)ctx, buf, size);
84 return 0;
85}
86
87static int hash_finish_sha256(struct hash_algo *algo, void *ctx, void
88 *dest_buf, int size)
89{
90 if (size < algo->digest_size)
91 return -1;
92
93 sha256_finish((sha256_context *)ctx, dest_buf);
94 free(ctx);
95 return 0;
96}
97#endif
98
Reuben Dowle1908fd92020-04-16 17:36:52 +120099#if defined(CONFIG_SHA384)
100static int hash_init_sha384(struct hash_algo *algo, void **ctxp)
101{
102 sha512_context *ctx = malloc(sizeof(sha512_context));
103 sha384_starts(ctx);
104 *ctxp = ctx;
105 return 0;
106}
107
108static int hash_update_sha384(struct hash_algo *algo, void *ctx,
109 const void *buf, unsigned int size, int is_last)
110{
111 sha384_update((sha512_context *)ctx, buf, size);
112 return 0;
113}
114
115static int hash_finish_sha384(struct hash_algo *algo, void *ctx, void
116 *dest_buf, int size)
117{
118 if (size < algo->digest_size)
119 return -1;
120
121 sha384_finish((sha512_context *)ctx, dest_buf);
122 free(ctx);
123 return 0;
124}
125#endif
126
127#if defined(CONFIG_SHA512)
128static int hash_init_sha512(struct hash_algo *algo, void **ctxp)
129{
130 sha512_context *ctx = malloc(sizeof(sha512_context));
131 sha512_starts(ctx);
132 *ctxp = ctx;
133 return 0;
134}
135
136static int hash_update_sha512(struct hash_algo *algo, void *ctx,
137 const void *buf, unsigned int size, int is_last)
138{
139 sha512_update((sha512_context *)ctx, buf, size);
140 return 0;
141}
142
143static int hash_finish_sha512(struct hash_algo *algo, void *ctx, void
144 *dest_buf, int size)
145{
146 if (size < algo->digest_size)
147 return -1;
148
149 printf("hello world\n");
150
151 sha512_finish((sha512_context *)ctx, dest_buf);
152 free(ctx);
153 return 0;
154}
155#endif
156
157
Philipp Tomsich6e2ac3e2018-11-25 19:22:19 +0100158static int hash_init_crc16_ccitt(struct hash_algo *algo, void **ctxp)
159{
160 uint16_t *ctx = malloc(sizeof(uint16_t));
161 *ctx = 0;
162 *ctxp = ctx;
163 return 0;
164}
165
166static int hash_update_crc16_ccitt(struct hash_algo *algo, void *ctx,
167 const void *buf, unsigned int size,
168 int is_last)
169{
170 *((uint16_t *)ctx) = crc16_ccitt(*((uint16_t *)ctx), buf, size);
171 return 0;
172}
173
174static int hash_finish_crc16_ccitt(struct hash_algo *algo, void *ctx,
175 void *dest_buf, int size)
176{
177 if (size < algo->digest_size)
178 return -1;
179
180 *((uint16_t *)dest_buf) = *((uint16_t *)ctx);
181 free(ctx);
182 return 0;
183}
184
Hung-ying Tyanf061b1a2014-03-03 12:19:28 +0100185static int hash_init_crc32(struct hash_algo *algo, void **ctxp)
186{
187 uint32_t *ctx = malloc(sizeof(uint32_t));
188 *ctx = 0;
189 *ctxp = ctx;
190 return 0;
191}
192
193static int hash_update_crc32(struct hash_algo *algo, void *ctx,
194 const void *buf, unsigned int size, int is_last)
195{
196 *((uint32_t *)ctx) = crc32(*((uint32_t *)ctx), buf, size);
197 return 0;
198}
199
200static int hash_finish_crc32(struct hash_algo *algo, void *ctx, void *dest_buf,
201 int size)
202{
203 if (size < algo->digest_size)
204 return -1;
205
206 *((uint32_t *)dest_buf) = *((uint32_t *)ctx);
207 free(ctx);
208 return 0;
209}
210
Simon Glassb8790452012-12-05 14:46:36 +0000211/*
Tom Rini03361c72017-08-14 16:38:07 -0400212 * These are the hash algorithms we support. If we have hardware acceleration
213 * is enable we will use that, otherwise a software version of the algorithm.
214 * Note that algorithm names must be in lower case.
Simon Glassb8790452012-12-05 14:46:36 +0000215 */
216static struct hash_algo hash_algo[] = {
Tom Rini03361c72017-08-14 16:38:07 -0400217#ifdef CONFIG_SHA1
Akshay Saraswatcbd8f2c2013-03-20 21:00:58 +0000218 {
Tom Rini03361c72017-08-14 16:38:07 -0400219 .name = "sha1",
220 .digest_size = SHA1_SUM_LEN,
221 .chunk_size = CHUNKSZ_SHA1,
222#ifdef CONFIG_SHA_HW_ACCEL
223 .hash_func_ws = hw_sha1,
224#else
225 .hash_func_ws = sha1_csum_wd,
gaurav ranaef201592015-02-20 12:51:46 +0530226#endif
gaurav ranaef201592015-02-20 12:51:46 +0530227#ifdef CONFIG_SHA_PROG_HW_ACCEL
Tom Rini03361c72017-08-14 16:38:07 -0400228 .hash_init = hw_sha_init,
229 .hash_update = hw_sha_update,
230 .hash_finish = hw_sha_finish,
231#else
232 .hash_init = hash_init_sha1,
233 .hash_update = hash_update_sha1,
234 .hash_finish = hash_finish_sha1,
Akshay Saraswatcbd8f2c2013-03-20 21:00:58 +0000235#endif
Simon Glassb8790452012-12-05 14:46:36 +0000236 },
237#endif
238#ifdef CONFIG_SHA256
239 {
Tom Rini03361c72017-08-14 16:38:07 -0400240 .name = "sha256",
241 .digest_size = SHA256_SUM_LEN,
242 .chunk_size = CHUNKSZ_SHA256,
243#ifdef CONFIG_SHA_HW_ACCEL
244 .hash_func_ws = hw_sha256,
245#else
246 .hash_func_ws = sha256_csum_wd,
247#endif
248#ifdef CONFIG_SHA_PROG_HW_ACCEL
249 .hash_init = hw_sha_init,
250 .hash_update = hw_sha_update,
251 .hash_finish = hw_sha_finish,
252#else
253 .hash_init = hash_init_sha256,
254 .hash_update = hash_update_sha256,
255 .hash_finish = hash_finish_sha256,
256#endif
Simon Glassb8790452012-12-05 14:46:36 +0000257 },
258#endif
Reuben Dowle1908fd92020-04-16 17:36:52 +1200259#ifdef CONFIG_SHA384
260 {
261 .name = "sha384",
262 .digest_size = SHA384_SUM_LEN,
263 .chunk_size = CHUNKSZ_SHA384,
264 .hash_func_ws = sha384_csum_wd,
265 .hash_init = hash_init_sha384,
266 .hash_update = hash_update_sha384,
267 .hash_finish = hash_finish_sha384,
268 },
269#endif
270#ifdef CONFIG_SHA512
271 {
272 .name = "sha512",
273 .digest_size = SHA512_SUM_LEN,
274 .chunk_size = CHUNKSZ_SHA512,
275 .hash_func_ws = sha512_csum_wd,
276 .hash_init = hash_init_sha512,
277 .hash_update = hash_update_sha512,
278 .hash_finish = hash_finish_sha512,
279 },
280#endif
Simon Glass0bbd76f2013-02-24 20:30:22 +0000281 {
Philipp Tomsich6e2ac3e2018-11-25 19:22:19 +0100282 .name = "crc16-ccitt",
283 .digest_size = 2,
284 .chunk_size = CHUNKSZ,
285 .hash_func_ws = crc16_ccitt_wd_buf,
286 .hash_init = hash_init_crc16_ccitt,
287 .hash_update = hash_update_crc16_ccitt,
288 .hash_finish = hash_finish_crc16_ccitt,
289 },
290 {
Tom Rini03361c72017-08-14 16:38:07 -0400291 .name = "crc32",
292 .digest_size = 4,
293 .chunk_size = CHUNKSZ_CRC32,
294 .hash_func_ws = crc32_wd_buf,
295 .hash_init = hash_init_crc32,
296 .hash_update = hash_update_crc32,
297 .hash_finish = hash_finish_crc32,
Simon Glass0bbd76f2013-02-24 20:30:22 +0000298 },
Simon Glassb8790452012-12-05 14:46:36 +0000299};
300
Simon Glass0bbd76f2013-02-24 20:30:22 +0000301/* Try to minimize code size for boards that don't want much hashing */
Daniel Thompsona9e2c672017-05-19 17:26:58 +0100302#if defined(CONFIG_SHA256) || defined(CONFIG_CMD_SHA1SUM) || \
Reuben Dowle1908fd92020-04-16 17:36:52 +1200303 defined(CONFIG_CRC32_VERIFY) || defined(CONFIG_CMD_HASH) || \
304 defined(CONFIG_SHA384) || defined(CONFIG_SHA512)
Simon Glass0bbd76f2013-02-24 20:30:22 +0000305#define multi_hash() 1
306#else
307#define multi_hash() 0
308#endif
309
T Karthik Reddya098b432019-03-16 15:23:01 +0530310static void reloc_update(void)
311{
312#if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
313 int i;
314 static bool done;
315
316 if (!done) {
317 done = true;
318 for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
319 hash_algo[i].name += gd->reloc_off;
320 hash_algo[i].hash_func_ws += gd->reloc_off;
321 hash_algo[i].hash_init += gd->reloc_off;
322 hash_algo[i].hash_update += gd->reloc_off;
323 hash_algo[i].hash_finish += gd->reloc_off;
324 }
325 }
326#endif
327}
328
Ruchika Guptac915ceb2015-01-23 16:01:58 +0530329int hash_lookup_algo(const char *algo_name, struct hash_algo **algop)
330{
331 int i;
332
T Karthik Reddya098b432019-03-16 15:23:01 +0530333 reloc_update();
334
Ruchika Guptac915ceb2015-01-23 16:01:58 +0530335 for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
336 if (!strcmp(algo_name, hash_algo[i].name)) {
337 *algop = &hash_algo[i];
338 return 0;
339 }
340 }
341
342 debug("Unknown hash algorithm '%s'\n", algo_name);
343 return -EPROTONOSUPPORT;
344}
345
346int hash_progressive_lookup_algo(const char *algo_name,
347 struct hash_algo **algop)
348{
349 int i;
350
T Karthik Reddya098b432019-03-16 15:23:01 +0530351 reloc_update();
352
Ruchika Guptac915ceb2015-01-23 16:01:58 +0530353 for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
354 if (!strcmp(algo_name, hash_algo[i].name)) {
355 if (hash_algo[i].hash_init) {
356 *algop = &hash_algo[i];
357 return 0;
358 }
359 }
360 }
361
362 debug("Unknown hash algorithm '%s'\n", algo_name);
363 return -EPROTONOSUPPORT;
364}
365
366#ifndef USE_HOSTCC
Stefan Roese0ed2e462015-05-18 14:08:24 +0200367int hash_parse_string(const char *algo_name, const char *str, uint8_t *result)
368{
369 struct hash_algo *algo;
370 int ret;
371 int i;
372
373 ret = hash_lookup_algo(algo_name, &algo);
374 if (ret)
375 return ret;
376
377 for (i = 0; i < algo->digest_size; i++) {
378 char chr[3];
379
380 strncpy(chr, &str[i * 2], 2);
381 result[i] = simple_strtoul(chr, NULL, 16);
382 }
383
384 return 0;
385}
386
Tom Rini61738382016-01-05 08:47:48 -0500387int hash_block(const char *algo_name, const void *data, unsigned int len,
388 uint8_t *output, int *output_size)
389{
390 struct hash_algo *algo;
391 int ret;
392
393 ret = hash_lookup_algo(algo_name, &algo);
394 if (ret)
395 return ret;
396
397 if (output_size && *output_size < algo->digest_size) {
398 debug("Output buffer size %d too small (need %d bytes)",
399 *output_size, algo->digest_size);
400 return -ENOSPC;
401 }
402 if (output_size)
403 *output_size = algo->digest_size;
404 algo->hash_func_ws(data, len, output, algo->chunk_size);
405
406 return 0;
407}
408
409#if defined(CONFIG_CMD_HASH) || defined(CONFIG_CMD_SHA1SUM) || defined(CONFIG_CMD_CRC32)
Simon Glassb8790452012-12-05 14:46:36 +0000410/**
411 * store_result: Store the resulting sum to an address or variable
412 *
413 * @algo: Hash algorithm being used
414 * @sum: Hash digest (algo->digest_size bytes)
415 * @dest: Destination, interpreted as a hex address if it starts
Simon Glass80e345a2013-02-24 17:33:26 +0000416 * with * (or allow_env_vars is 0) or otherwise as an
417 * environment variable.
418 * @allow_env_vars: non-zero to permit storing the result to an
419 * variable environment
Simon Glassb8790452012-12-05 14:46:36 +0000420 */
Simon Glassa3b240b2014-06-12 07:24:41 -0600421static void store_result(struct hash_algo *algo, const uint8_t *sum,
Simon Glass80e345a2013-02-24 17:33:26 +0000422 const char *dest, int allow_env_vars)
Simon Glassb8790452012-12-05 14:46:36 +0000423{
424 unsigned int i;
Simon Glass80e345a2013-02-24 17:33:26 +0000425 int env_var = 0;
Simon Glassb8790452012-12-05 14:46:36 +0000426
Simon Glass80e345a2013-02-24 17:33:26 +0000427 /*
428 * If environment variables are allowed, then we assume that 'dest'
429 * is an environment variable, unless it starts with *, in which
430 * case we assume it is an address. If not allowed, it is always an
431 * address. This is to support the crc32 command.
432 */
433 if (allow_env_vars) {
434 if (*dest == '*')
435 dest++;
436 else
437 env_var = 1;
438 }
Simon Glassb8790452012-12-05 14:46:36 +0000439
Simon Glass80e345a2013-02-24 17:33:26 +0000440 if (env_var) {
Simon Glassb8790452012-12-05 14:46:36 +0000441 char str_output[HASH_MAX_DIGEST_SIZE * 2 + 1];
442 char *str_ptr = str_output;
443
444 for (i = 0; i < algo->digest_size; i++) {
445 sprintf(str_ptr, "%02x", sum[i]);
446 str_ptr += 2;
447 }
Jeroen Hofsteeb14c0522014-06-09 11:02:02 +0200448 *str_ptr = '\0';
Simon Glass6a38e412017-08-03 12:22:09 -0600449 env_set(dest, str_output);
Simon Glass80e345a2013-02-24 17:33:26 +0000450 } else {
Simon Glass68f15562013-02-24 17:33:31 +0000451 ulong addr;
452 void *buf;
Simon Glass80e345a2013-02-24 17:33:26 +0000453
Simon Glass68f15562013-02-24 17:33:31 +0000454 addr = simple_strtoul(dest, NULL, 16);
455 buf = map_sysmem(addr, algo->digest_size);
456 memcpy(buf, sum, algo->digest_size);
457 unmap_sysmem(buf);
Simon Glassb8790452012-12-05 14:46:36 +0000458 }
459}
460
461/**
462 * parse_verify_sum: Parse a hash verification parameter
463 *
464 * @algo: Hash algorithm being used
465 * @verify_str: Argument to parse. If it starts with * then it is
466 * interpreted as a hex address containing the hash.
467 * If the length is exactly the right number of hex digits
468 * for the digest size, then we assume it is a hex digest.
469 * Otherwise we assume it is an environment variable, and
470 * look up its value (it must contain a hex digest).
471 * @vsum: Returns binary digest value (algo->digest_size bytes)
Simon Glass80e345a2013-02-24 17:33:26 +0000472 * @allow_env_vars: non-zero to permit storing the result to an environment
473 * variable. If 0 then verify_str is assumed to be an
474 * address, and the * prefix is not expected.
Simon Glassb8790452012-12-05 14:46:36 +0000475 * @return 0 if ok, non-zero on error
476 */
Simon Glassa3b240b2014-06-12 07:24:41 -0600477static int parse_verify_sum(struct hash_algo *algo, char *verify_str,
478 uint8_t *vsum, int allow_env_vars)
Simon Glassb8790452012-12-05 14:46:36 +0000479{
Simon Glass80e345a2013-02-24 17:33:26 +0000480 int env_var = 0;
481
482 /* See comment above in store_result() */
483 if (allow_env_vars) {
484 if (*verify_str == '*')
485 verify_str++;
486 else
487 env_var = 1;
488 }
489
Nikolay Dimitrov810b84d2014-12-12 20:01:23 +0200490 if (!env_var) {
Simon Glass68f15562013-02-24 17:33:31 +0000491 ulong addr;
492 void *buf;
Simon Glassb8790452012-12-05 14:46:36 +0000493
Simon Glass68f15562013-02-24 17:33:31 +0000494 addr = simple_strtoul(verify_str, NULL, 16);
495 buf = map_sysmem(addr, algo->digest_size);
496 memcpy(vsum, buf, algo->digest_size);
Simon Glassb8790452012-12-05 14:46:36 +0000497 } else {
Simon Glassb8790452012-12-05 14:46:36 +0000498 char *vsum_str;
499 int digits = algo->digest_size * 2;
500
501 /*
502 * As with the original code from sha1sum.c, we assume that a
503 * string which matches the digest size exactly is a hex
504 * string and not an environment variable.
505 */
506 if (strlen(verify_str) == digits)
507 vsum_str = verify_str;
508 else {
Simon Glass64b723f2017-08-03 12:22:12 -0600509 vsum_str = env_get(verify_str);
Simon Glassb8790452012-12-05 14:46:36 +0000510 if (vsum_str == NULL || strlen(vsum_str) != digits) {
511 printf("Expected %d hex digits in env var\n",
512 digits);
513 return 1;
514 }
515 }
516
Stefan Roese0ed2e462015-05-18 14:08:24 +0200517 hash_parse_string(algo->name, vsum_str, vsum);
Simon Glassb8790452012-12-05 14:46:36 +0000518 }
519 return 0;
520}
521
Tom Rini61738382016-01-05 08:47:48 -0500522static void hash_show(struct hash_algo *algo, ulong addr, ulong len, uint8_t *output)
Simon Glassb8790452012-12-05 14:46:36 +0000523{
524 int i;
525
526 printf("%s for %08lx ... %08lx ==> ", algo->name, addr, addr + len - 1);
527 for (i = 0; i < algo->digest_size; i++)
528 printf("%02x", output[i]);
529}
530
Simon Glassed38aef2020-05-10 11:40:03 -0600531int hash_command(const char *algo_name, int flags, struct cmd_tbl *cmdtp,
532 int flag, int argc, char *const argv[])
Simon Glassb8790452012-12-05 14:46:36 +0000533{
Simon Glassb8790452012-12-05 14:46:36 +0000534 ulong addr, len;
Simon Glassb8790452012-12-05 14:46:36 +0000535
Nikolay Dimitrov810b84d2014-12-12 20:01:23 +0200536 if ((argc < 2) || ((flags & HASH_FLAG_VERIFY) && (argc < 3)))
Simon Glassb8790452012-12-05 14:46:36 +0000537 return CMD_RET_USAGE;
538
Simon Glass80e345a2013-02-24 17:33:26 +0000539 addr = simple_strtoul(*argv++, NULL, 16);
540 len = simple_strtoul(*argv++, NULL, 16);
541
Simon Glass0bbd76f2013-02-24 20:30:22 +0000542 if (multi_hash()) {
543 struct hash_algo *algo;
Breno Lima45e65122018-01-17 10:03:45 -0200544 u8 *output;
Simon Glassa3b240b2014-06-12 07:24:41 -0600545 uint8_t vsum[HASH_MAX_DIGEST_SIZE];
Simon Glass68f15562013-02-24 17:33:31 +0000546 void *buf;
Simon Glassb8790452012-12-05 14:46:36 +0000547
Hung-ying Tyanf061b1a2014-03-03 12:19:28 +0100548 if (hash_lookup_algo(algo_name, &algo)) {
Simon Glass0bbd76f2013-02-24 20:30:22 +0000549 printf("Unknown hash algorithm '%s'\n", algo_name);
550 return CMD_RET_USAGE;
551 }
552 argc -= 2;
553
554 if (algo->digest_size > HASH_MAX_DIGEST_SIZE) {
555 puts("HASH_MAX_DIGEST_SIZE exceeded\n");
556 return 1;
557 }
Simon Glassb8790452012-12-05 14:46:36 +0000558
Breno Lima45e65122018-01-17 10:03:45 -0200559 output = memalign(ARCH_DMA_MINALIGN,
560 sizeof(uint32_t) * HASH_MAX_DIGEST_SIZE);
561
Simon Glass68f15562013-02-24 17:33:31 +0000562 buf = map_sysmem(addr, len);
563 algo->hash_func_ws(buf, len, output, algo->chunk_size);
564 unmap_sysmem(buf);
Simon Glassb8790452012-12-05 14:46:36 +0000565
Simon Glass0bbd76f2013-02-24 20:30:22 +0000566 /* Try to avoid code bloat when verify is not needed */
Daniel Thompsona9e2c672017-05-19 17:26:58 +0100567#if defined(CONFIG_CRC32_VERIFY) || defined(CONFIG_SHA1SUM_VERIFY) || \
568 defined(CONFIG_HASH_VERIFY)
Simon Glass0bbd76f2013-02-24 20:30:22 +0000569 if (flags & HASH_FLAG_VERIFY) {
Simon Glassb8790452012-12-05 14:46:36 +0000570#else
Simon Glass0bbd76f2013-02-24 20:30:22 +0000571 if (0) {
Simon Glassb8790452012-12-05 14:46:36 +0000572#endif
Simon Glass0bbd76f2013-02-24 20:30:22 +0000573 if (parse_verify_sum(algo, *argv, vsum,
Simon Glass80e345a2013-02-24 17:33:26 +0000574 flags & HASH_FLAG_ENV)) {
Simon Glass0bbd76f2013-02-24 20:30:22 +0000575 printf("ERROR: %s does not contain a valid "
576 "%s sum\n", *argv, algo->name);
577 return 1;
578 }
579 if (memcmp(output, vsum, algo->digest_size) != 0) {
580 int i;
Simon Glassb8790452012-12-05 14:46:36 +0000581
Simon Glass7f3fa542014-06-02 22:04:49 -0600582 hash_show(algo, addr, len, output);
Simon Glass0bbd76f2013-02-24 20:30:22 +0000583 printf(" != ");
584 for (i = 0; i < algo->digest_size; i++)
585 printf("%02x", vsum[i]);
586 puts(" ** ERROR **\n");
587 return 1;
588 }
589 } else {
Simon Glass7f3fa542014-06-02 22:04:49 -0600590 hash_show(algo, addr, len, output);
Simon Glass0bbd76f2013-02-24 20:30:22 +0000591 printf("\n");
592
593 if (argc) {
594 store_result(algo, output, *argv,
595 flags & HASH_FLAG_ENV);
596 }
Breno Lima45e65122018-01-17 10:03:45 -0200597 unmap_sysmem(output);
598
Simon Glassb8790452012-12-05 14:46:36 +0000599 }
Simon Glass0bbd76f2013-02-24 20:30:22 +0000600
601 /* Horrible code size hack for boards that just want crc32 */
Simon Glassb8790452012-12-05 14:46:36 +0000602 } else {
Simon Glass0bbd76f2013-02-24 20:30:22 +0000603 ulong crc;
604 ulong *ptr;
605
606 crc = crc32_wd(0, (const uchar *)addr, len, CHUNKSZ_CRC32);
607
608 printf("CRC32 for %08lx ... %08lx ==> %08lx\n",
609 addr, addr + len - 1, crc);
Simon Glassb8790452012-12-05 14:46:36 +0000610
Tom Rini62780f02013-11-07 07:39:48 -0500611 if (argc >= 3) {
612 ptr = (ulong *)simple_strtoul(argv[0], NULL, 16);
Simon Glass0bbd76f2013-02-24 20:30:22 +0000613 *ptr = crc;
Simon Glass80e345a2013-02-24 17:33:26 +0000614 }
Simon Glassb8790452012-12-05 14:46:36 +0000615 }
616
617 return 0;
618}
Simon Glassb22ec7a2017-05-17 09:05:34 -0600619#endif /* CONFIG_CMD_HASH || CONFIG_CMD_SHA1SUM || CONFIG_CMD_CRC32) */
620#endif /* !USE_HOSTCC */