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