Skip to contentSkip to conversation

riscv/purgatory: return bool from verify_sha256_digest

PATCH In review Thorsten Blum 2 participants 4 messages May 9 – May 13 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 riscv/purgatory: return bool from verify_sha256_digest

Change the function's return type from int to bool and return the result of memcmp() directly to simplify the code. While at it, cast ->start to 'const u8 *' to better match the expected type.

---
Changes in v2:
- Return true on success and false on failure (Eric)
- v1: https://lore.kernel.org/lkml/[email protected]/

---
 arch/riscv/purgatory/purgatory.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
arch/riscv/purgatory/purgatory.c +5 -6
index bbd5cfa4d741..745deeb6e3a5 100644
@@ -17,7 +17,7 @@ u8 purgatory_sha256_digest[SHA256_DIGEST_SIZE] __section(".kexec-purgatory");
 
 struct kexec_sha_region purgatory_sha_regions[KEXEC_SEGMENT_MAX] __section(".kexec-purgatory");
 
-static int verify_sha256_digest(void)
+static bool verify_sha256_digest(void)
 {
 	struct kexec_sha_region *ptr, *end;
 	struct sha256_ctx sctx;
@@ -26,11 +26,10 @@ static int verify_sha256_digest(void)
 	sha256_init(&sctx);
 	end = purgatory_sha_regions + ARRAY_SIZE(purgatory_sha_regions);
 	for (ptr = purgatory_sha_regions; ptr < end; ptr++)
-		sha256_update(&sctx, (uint8_t *)(ptr->start), ptr->len);
+		sha256_update(&sctx, (const u8 *)(ptr->start), ptr->len);
 	sha256_final(&sctx, digest);
-	if (memcmp(digest, purgatory_sha256_digest, sizeof(digest)) != 0)
-		return 1;
-	return 0;
+
+	return memcmp(digest, purgatory_sha256_digest, sizeof(digest)) == 0;
 }
 
 /* workaround for a warning with -Wmissing-prototypes */
@@ -38,7 +37,7 @@ void purgatory(void);
 
 void purgatory(void)
 {
-	if (verify_sha256_digest())
+	if (!verify_sha256_digest())
 		for (;;)
 			/* loop forever */
 			;
#1 lore raw

PATCH v2 2/2 riscv/purgatory: add asm/purgatory.h

Add arch/riscv/include/asm/purgatory.h and provide the purgatory() prototype via the architecture header, mirroring the x86 layout.

Remove the workaround from arch/riscv/purgatory/purgatory.c.

---
No changes in patch 2/2.

---
 arch/riscv/include/asm/purgatory.h | 11 +++++++++++
 arch/riscv/purgatory/purgatory.c   |  5 +----
 2 files changed, 12 insertions(+), 4 deletions(-)
 create mode 100644 arch/riscv/include/asm/purgatory.h
arch/riscv/include/asm/purgatory.h new file +11 -0
new file mode 100644
index 000000000000..a63687be2517
@@ -0,0 +1,11 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _ASM_RISCV_PURGATORY_H
+#define _ASM_RISCV_PURGATORY_H
+
+#ifndef __ASSEMBLER__
+#include <linux/purgatory.h>
+
+extern void purgatory(void);
+#endif	/* __ASSEMBLER__ */
+
+#endif /* _ASM_RISCV_PURGATORY_H */
arch/riscv/purgatory/purgatory.c +1 -4
index 745deeb6e3a5..3474e3abe5d7 100644
@@ -8,9 +8,9 @@
  *
  */
 
-#include <linux/purgatory.h>
 #include <linux/kernel.h>
 #include <linux/string.h>
+#include <asm/purgatory.h>
 #include <asm/string.h>
 
 u8 purgatory_sha256_digest[SHA256_DIGEST_SIZE] __section(".kexec-purgatory");
@@ -32,9 +32,6 @@ static bool verify_sha256_digest(void)
 	return memcmp(digest, purgatory_sha256_digest, sizeof(digest)) == 0;
 }
 
-/* workaround for a warning with -Wmissing-prototypes */
-void purgatory(void);
-
 void purgatory(void)
 {
 	if (!verify_sha256_digest())
maintainer
#2 lore raw

Re: [PATCH v2 2/2] riscv/purgatory: add asm/purgatory.h

Quoted 6 lines · Thorsten Blum

Add arch/riscv/include/asm/purgatory.h and provide the purgatory() prototype via the architecture header, mirroring the x86 layout.

Remove the workaround from arch/riscv/purgatory/purgatory.c.

Thanks, queued for v7.2.

- Paul