blob: 68c290d64d86760f6ef124447a05f3749534ce5e [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Heiko Schocher646257d2014-03-03 12:19:26 +01002/*
3 * Copyright (c) 2013, Andreas Oetken.
Heiko Schocher646257d2014-03-03 12:19:26 +01004 */
5
Heiko Schocher29a23f92014-03-03 12:19:30 +01006#ifndef USE_HOSTCC
Heiko Schocher646257d2014-03-03 12:19:26 +01007#include <common.h>
8#include <fdtdec.h>
Heiko Schocher646257d2014-03-03 12:19:26 +01009#include <asm/byteorder.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090010#include <linux/errno.h>
Heiko Schocher646257d2014-03-03 12:19:26 +010011#include <asm/unaligned.h>
Ruchika Guptab37b46f2015-01-23 16:01:59 +053012#include <hash.h>
Heiko Schocher29a23f92014-03-03 12:19:30 +010013#else
14#include "fdt_host.h"
Ruchika Guptab37b46f2015-01-23 16:01:59 +053015#endif
Alexandru Gagniuc0bcb28d2021-02-19 12:45:10 -060016#include <hash.h>
17#include <image.h>
Heiko Schocher646257d2014-03-03 12:19:26 +010018
Ruchika Guptab37b46f2015-01-23 16:01:59 +053019int hash_calculate(const char *name,
Simon Glass13c133b2021-09-25 19:43:33 -060020 const struct image_region *region,
Ruchika Guptab37b46f2015-01-23 16:01:59 +053021 int region_count, uint8_t *checksum)
Heiko Schocher646257d2014-03-03 12:19:26 +010022{
Ruchika Guptab37b46f2015-01-23 16:01:59 +053023 struct hash_algo *algo;
24 int ret = 0;
25 void *ctx;
Heinrich Schuchardteb48efc2023-08-22 11:10:20 +020026 int i;
27
28 if (region_count < 1)
29 return -EINVAL;
Heiko Schocher646257d2014-03-03 12:19:26 +010030
Ruchika Guptab37b46f2015-01-23 16:01:59 +053031 ret = hash_progressive_lookup_algo(name, &algo);
32 if (ret)
33 return ret;
Heiko Schocher646257d2014-03-03 12:19:26 +010034
Ruchika Guptab37b46f2015-01-23 16:01:59 +053035 ret = algo->hash_init(algo, &ctx);
36 if (ret)
37 return ret;
Heiko Schocher646257d2014-03-03 12:19:26 +010038
Ruchika Guptab37b46f2015-01-23 16:01:59 +053039 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;
52
53 return 0;
Heiko Schocher646257d2014-03-03 12:19:26 +010054}