blob: 1970a741294d6fd4a97e3d7e3e7464d96966eb94 [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 <fdtdec.h>
Heiko Schocher646257d2014-03-03 12:19:26 +01008#include <asm/byteorder.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +09009#include <linux/errno.h>
Heiko Schocher646257d2014-03-03 12:19:26 +010010#include <asm/unaligned.h>
Ruchika Guptab37b46f2015-01-23 16:01:59 +053011#include <hash.h>
Heiko Schocher29a23f92014-03-03 12:19:30 +010012#else
13#include "fdt_host.h"
Ruchika Guptab37b46f2015-01-23 16:01:59 +053014#endif
Alexandru Gagniuc0bcb28d2021-02-19 12:45:10 -060015#include <hash.h>
16#include <image.h>
Heiko Schocher646257d2014-03-03 12:19:26 +010017
Ruchika Guptab37b46f2015-01-23 16:01:59 +053018int hash_calculate(const char *name,
Simon Glass13c133b2021-09-25 19:43:33 -060019 const struct image_region *region,
Ruchika Guptab37b46f2015-01-23 16:01:59 +053020 int region_count, uint8_t *checksum)
Heiko Schocher646257d2014-03-03 12:19:26 +010021{
Ruchika Guptab37b46f2015-01-23 16:01:59 +053022 struct hash_algo *algo;
23 int ret = 0;
24 void *ctx;
Heinrich Schuchardteb48efc2023-08-22 11:10:20 +020025 int i;
26
27 if (region_count < 1)
28 return -EINVAL;
Heiko Schocher646257d2014-03-03 12:19:26 +010029
Ruchika Guptab37b46f2015-01-23 16:01:59 +053030 ret = hash_progressive_lookup_algo(name, &algo);
31 if (ret)
32 return ret;
Heiko Schocher646257d2014-03-03 12:19:26 +010033
Ruchika Guptab37b46f2015-01-23 16:01:59 +053034 ret = algo->hash_init(algo, &ctx);
35 if (ret)
36 return ret;
Heiko Schocher646257d2014-03-03 12:19:26 +010037
Ruchika Guptab37b46f2015-01-23 16:01:59 +053038 for (i = 0; i < region_count - 1; i++) {
39 ret = algo->hash_update(algo, ctx, region[i].data,
40 region[i].size, 0);
41 if (ret)
42 return ret;
43 }
44
45 ret = algo->hash_update(algo, ctx, region[i].data, region[i].size, 1);
46 if (ret)
47 return ret;
48 ret = algo->hash_finish(algo, ctx, checksum, algo->digest_size);
49 if (ret)
50 return ret;
51
52 return 0;
Heiko Schocher646257d2014-03-03 12:19:26 +010053}