Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013, Andreas Oetken. |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 4 | */ |
| 5 | |
Heiko Schocher | 29a23f9 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 6 | #ifndef USE_HOSTCC |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 7 | #include <common.h> |
| 8 | #include <fdtdec.h> |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 9 | #include <asm/byteorder.h> |
Masahiro Yamada | 1221ce4 | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 10 | #include <linux/errno.h> |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 11 | #include <asm/unaligned.h> |
Ruchika Gupta | b37b46f | 2015-01-23 16:01:59 +0530 | [diff] [blame] | 12 | #include <hash.h> |
Heiko Schocher | 29a23f9 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 13 | #else |
| 14 | #include "fdt_host.h" |
Ruchika Gupta | b37b46f | 2015-01-23 16:01:59 +0530 | [diff] [blame] | 15 | #endif |
| 16 | #include <u-boot/rsa.h> |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 17 | |
Ruchika Gupta | b37b46f | 2015-01-23 16:01:59 +0530 | [diff] [blame] | 18 | int hash_calculate(const char *name, |
| 19 | const struct image_region region[], |
| 20 | int region_count, uint8_t *checksum) |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 21 | { |
Ruchika Gupta | b37b46f | 2015-01-23 16:01:59 +0530 | [diff] [blame] | 22 | struct hash_algo *algo; |
| 23 | int ret = 0; |
| 24 | void *ctx; |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 25 | uint32_t i; |
| 26 | i = 0; |
| 27 | |
Ruchika Gupta | b37b46f | 2015-01-23 16:01:59 +0530 | [diff] [blame] | 28 | ret = hash_progressive_lookup_algo(name, &algo); |
| 29 | if (ret) |
| 30 | return ret; |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 31 | |
Ruchika Gupta | b37b46f | 2015-01-23 16:01:59 +0530 | [diff] [blame] | 32 | ret = algo->hash_init(algo, &ctx); |
| 33 | if (ret) |
| 34 | return ret; |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 35 | |
Ruchika Gupta | b37b46f | 2015-01-23 16:01:59 +0530 | [diff] [blame] | 36 | for (i = 0; i < region_count - 1; i++) { |
| 37 | ret = algo->hash_update(algo, ctx, region[i].data, |
| 38 | region[i].size, 0); |
| 39 | if (ret) |
| 40 | return ret; |
| 41 | } |
| 42 | |
| 43 | ret = algo->hash_update(algo, ctx, region[i].data, region[i].size, 1); |
| 44 | if (ret) |
| 45 | return ret; |
| 46 | ret = algo->hash_finish(algo, ctx, checksum, algo->digest_size); |
| 47 | if (ret) |
| 48 | return ret; |
| 49 | |
| 50 | return 0; |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 51 | } |