Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Heiko Schocher | edaf9b1 | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013, Andreas Oetken. |
Heiko Schocher | edaf9b1 | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 4 | */ |
| 5 | |
Heiko Schocher | d7b4232 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 6 | #ifndef USE_HOSTCC |
Heiko Schocher | edaf9b1 | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 7 | #include <common.h> |
| 8 | #include <fdtdec.h> |
Heiko Schocher | edaf9b1 | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 9 | #include <asm/byteorder.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 10 | #include <linux/errno.h> |
Heiko Schocher | edaf9b1 | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 11 | #include <asm/unaligned.h> |
Ruchika Gupta | 2c8987f | 2015-01-23 16:01:59 +0530 | [diff] [blame] | 12 | #include <hash.h> |
Heiko Schocher | d7b4232 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 13 | #else |
| 14 | #include "fdt_host.h" |
Ruchika Gupta | 2c8987f | 2015-01-23 16:01:59 +0530 | [diff] [blame] | 15 | #endif |
Alexandru Gagniuc | db182c4 | 2021-02-19 12:45:10 -0600 | [diff] [blame] | 16 | #include <hash.h> |
| 17 | #include <image.h> |
Heiko Schocher | edaf9b1 | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 18 | |
Ruchika Gupta | 2c8987f | 2015-01-23 16:01:59 +0530 | [diff] [blame] | 19 | int hash_calculate(const char *name, |
Simon Glass | 2c6a1b0 | 2021-09-25 19:43:33 -0600 | [diff] [blame] | 20 | const struct image_region *region, |
Ruchika Gupta | 2c8987f | 2015-01-23 16:01:59 +0530 | [diff] [blame] | 21 | int region_count, uint8_t *checksum) |
Heiko Schocher | edaf9b1 | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 22 | { |
Ruchika Gupta | 2c8987f | 2015-01-23 16:01:59 +0530 | [diff] [blame] | 23 | struct hash_algo *algo; |
| 24 | int ret = 0; |
| 25 | void *ctx; |
Heinrich Schuchardt | f9b0875 | 2023-08-22 11:10:20 +0200 | [diff] [blame] | 26 | int i; |
| 27 | |
| 28 | if (region_count < 1) |
| 29 | return -EINVAL; |
Heiko Schocher | edaf9b1 | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 30 | |
Ruchika Gupta | 2c8987f | 2015-01-23 16:01:59 +0530 | [diff] [blame] | 31 | ret = hash_progressive_lookup_algo(name, &algo); |
| 32 | if (ret) |
| 33 | return ret; |
Heiko Schocher | edaf9b1 | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 34 | |
Ruchika Gupta | 2c8987f | 2015-01-23 16:01:59 +0530 | [diff] [blame] | 35 | ret = algo->hash_init(algo, &ctx); |
| 36 | if (ret) |
| 37 | return ret; |
| 38 | |
| 39 | for (i = 0; i < region_count - 1; i++) { |
| 40 | ret = algo->hash_update(algo, ctx, region[i].data, |
| 41 | region[i].size, 0); |
| 42 | if (ret) |
| 43 | return ret; |
| 44 | } |
| 45 | |
| 46 | ret = algo->hash_update(algo, ctx, region[i].data, region[i].size, 1); |
| 47 | if (ret) |
| 48 | return ret; |
| 49 | ret = algo->hash_finish(algo, ctx, checksum, algo->digest_size); |
| 50 | if (ret) |
| 51 | return ret; |
Heiko Schocher | edaf9b1 | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 52 | |
Ruchika Gupta | 2c8987f | 2015-01-23 16:01:59 +0530 | [diff] [blame] | 53 | return 0; |
Heiko Schocher | edaf9b1 | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 54 | } |