colibri_imx6: fix video stdout in default environment
[oweals/u-boot.git] / common / hash.c
1 // SPDX-License-Identifier: GPL-2.0+
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.
10  */
11
12 #ifndef USE_HOSTCC
13 #include <common.h>
14 #include <command.h>
15 #include <env.h>
16 #include <log.h>
17 #include <malloc.h>
18 #include <mapmem.h>
19 #include <hw_sha.h>
20 #include <asm/cache.h>
21 #include <asm/io.h>
22 #include <linux/errno.h>
23 #include <u-boot/crc.h>
24 #else
25 #include "mkimage.h"
26 #include <time.h>
27 #endif /* !USE_HOSTCC*/
28
29 #include <hash.h>
30 #include <image.h>
31 #include <u-boot/crc.h>
32 #include <u-boot/sha1.h>
33 #include <u-boot/sha256.h>
34 #include <u-boot/md5.h>
35
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
42 #if defined(CONFIG_SHA1) && !defined(CONFIG_SHA_PROG_HW_ACCEL)
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
70 #if defined(CONFIG_SHA256) && !defined(CONFIG_SHA_PROG_HW_ACCEL)
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
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
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
151 /*
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.
155  */
156 static struct hash_algo hash_algo[] = {
157 #ifdef CONFIG_SHA1
158         {
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,
166 #endif
167 #ifdef CONFIG_SHA_PROG_HW_ACCEL
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,
175 #endif
176         },
177 #endif
178 #ifdef CONFIG_SHA256
179         {
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
197         },
198 #endif
199         {
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         {
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,
216         },
217 };
218
219 /* Try to minimize code size for boards that don't want much hashing */
220 #if defined(CONFIG_SHA256) || defined(CONFIG_CMD_SHA1SUM) || \
221         defined(CONFIG_CRC32_VERIFY) || defined(CONFIG_CMD_HASH)
222 #define multi_hash()    1
223 #else
224 #define multi_hash()    0
225 #endif
226
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
246 int hash_lookup_algo(const char *algo_name, struct hash_algo **algop)
247 {
248         int i;
249
250         reloc_update();
251
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
268         reloc_update();
269
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
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
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)
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
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
337  */
338 static void store_result(struct hash_algo *algo, const uint8_t *sum,
339                          const char *dest, int allow_env_vars)
340 {
341         unsigned int i;
342         int env_var = 0;
343
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         }
356
357         if (env_var) {
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                 }
365                 *str_ptr = '\0';
366                 env_set(dest, str_output);
367         } else {
368                 ulong addr;
369                 void *buf;
370
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);
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)
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.
392  * @return 0 if ok, non-zero on error
393  */
394 static int parse_verify_sum(struct hash_algo *algo, char *verify_str,
395                             uint8_t *vsum, int allow_env_vars)
396 {
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
407         if (!env_var) {
408                 ulong addr;
409                 void *buf;
410
411                 addr = simple_strtoul(verify_str, NULL, 16);
412                 buf = map_sysmem(addr, algo->digest_size);
413                 memcpy(vsum, buf, algo->digest_size);
414         } else {
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 {
426                         vsum_str = env_get(verify_str);
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
434                 hash_parse_string(algo->name, vsum_str, vsum);
435         }
436         return 0;
437 }
438
439 static void hash_show(struct hash_algo *algo, ulong addr, ulong len, uint8_t *output)
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
448 int hash_command(const char *algo_name, int flags, struct cmd_tbl *cmdtp,
449                  int flag, int argc, char *const argv[])
450 {
451         ulong addr, len;
452
453         if ((argc < 2) || ((flags & HASH_FLAG_VERIFY) && (argc < 3)))
454                 return CMD_RET_USAGE;
455
456         addr = simple_strtoul(*argv++, NULL, 16);
457         len = simple_strtoul(*argv++, NULL, 16);
458
459         if (multi_hash()) {
460                 struct hash_algo *algo;
461                 u8 *output;
462                 uint8_t vsum[HASH_MAX_DIGEST_SIZE];
463                 void *buf;
464
465                 if (hash_lookup_algo(algo_name, &algo)) {
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                 }
475
476                 output = memalign(ARCH_DMA_MINALIGN,
477                                   sizeof(uint32_t) * HASH_MAX_DIGEST_SIZE);
478
479                 buf = map_sysmem(addr, len);
480                 algo->hash_func_ws(buf, len, output, algo->chunk_size);
481                 unmap_sysmem(buf);
482
483                 /* Try to avoid code bloat when verify is not needed */
484 #if defined(CONFIG_CRC32_VERIFY) || defined(CONFIG_SHA1SUM_VERIFY) || \
485         defined(CONFIG_HASH_VERIFY)
486                 if (flags & HASH_FLAG_VERIFY) {
487 #else
488                 if (0) {
489 #endif
490                         if (parse_verify_sum(algo, *argv, vsum,
491                                         flags & HASH_FLAG_ENV)) {
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;
498
499                                 hash_show(algo, addr, len, output);
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 {
507                         hash_show(algo, addr, len, output);
508                         printf("\n");
509
510                         if (argc) {
511                                 store_result(algo, output, *argv,
512                                         flags & HASH_FLAG_ENV);
513                         }
514                 unmap_sysmem(output);
515
516                 }
517
518         /* Horrible code size hack for boards that just want crc32 */
519         } else {
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);
527
528                 if (argc >= 3) {
529                         ptr = (ulong *)simple_strtoul(argv[0], NULL, 16);
530                         *ptr = crc;
531                 }
532         }
533
534         return 0;
535 }
536 #endif /* CONFIG_CMD_HASH || CONFIG_CMD_SHA1SUM || CONFIG_CMD_CRC32) */
537 #endif /* !USE_HOSTCC */