[PATCH 07/13] fip: add function to calculate a sha256 over FIP image
Sascha Hauer
s.hauer at pengutronix.de
Thu Feb 27 23:16:55 PST 2025
This adds fip_sha256() to calculate a sha256 over a FIP image.
Signed-off-by: Sascha Hauer <s.hauer at pengutronix.de>
---
include/fiptool.h | 3 +++
lib/fip.c | 23 ++++++++++++++++++++++-
2 files changed, 25 insertions(+), 1 deletion(-)
diff --git a/include/fiptool.h b/include/fiptool.h
index bb63a79c16..73b0fbe398 100644
--- a/include/fiptool.h
+++ b/include/fiptool.h
@@ -38,6 +38,7 @@ struct fip_state {
size_t nr_image_descs;
int verbose;
void *buffer;
+ size_t bufsize;
bool buf_no_free;
};
@@ -100,4 +101,6 @@ extern toc_entry_t plat_def_toc_entries[];
struct fip_state *fip_image_open(const char *filename, size_t offset);
+int fip_sha256(struct fip_state *fip, char *hash);
+
#endif /* FIPTOOL_H */
diff --git a/lib/fip.c b/lib/fip.c
index 8086b43412..7a5e3dc844 100644
--- a/lib/fip.c
+++ b/lib/fip.c
@@ -24,6 +24,7 @@
#include <libfile.h>
#include <fs.h>
#include <linux/kernel.h>
+#include <digest.h>
#include <fip.h>
#include <fiptool.h>
@@ -168,6 +169,7 @@ static int fip_do_parse_buf(struct fip_state *fip, void *buf, size_t size,
int terminated = 0;
fip->buffer = buf;
+ fip->bufsize = size;
bufend = fip->buffer + size;
@@ -570,10 +572,29 @@ struct fip_state *fip_image_open(const char *filename, size_t offset)
close(fd);
return fip_state;
-
err:
close(fd);
fip_free(fip_state);
return ERR_PTR(ret);
}
+
+int fip_sha256(struct fip_state *fip, char *hash)
+{
+ struct digest *d;
+ int ret;
+
+ d = digest_alloc_by_algo(HASH_ALGO_SHA256);
+ if (!d)
+ return -ENOSYS;
+
+ digest_init(d);
+
+ digest_update(d, fip->buffer, fip->bufsize);
+
+ ret = digest_final(d, hash);
+
+ digest_free(d);
+
+ return ret;
+}
--
2.39.5
More information about the barebox
mailing list