diff mbox series

[U-Boot,v1,2/5] bootcount: Add function wrappers to handle bootcount increment and error checking

Message ID 20180210102032.315-3-lukma@denx.de
State Superseded
Delegated to: Tom Rini
Headers show
Series Provide SPL support for bootcount (in the case of using falcon boot mode) | expand

Commit Message

Lukasz Majewski Feb. 10, 2018, 10:20 a.m. UTC
Those two functions can be used in either SPL or U-boot proper to provide
easy bootcount management.

Signed-off-by: Lukasz Majewski <lukma@denx.de>
---

 include/bootcount.h | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
diff mbox series

Patch

diff --git a/include/bootcount.h b/include/bootcount.h
index 06fb4d3578..534875a5a5 100644
--- a/include/bootcount.h
+++ b/include/bootcount.h
@@ -38,3 +38,28 @@  static inline u32 raw_bootcount_load(volatile u32 *addr)
 	return in_be32(addr);
 }
 #endif
+
+#ifdef CONFIG_SPL_BOOTCOUNT_LIMIT
+static inline bool bootcount_error(void)
+{
+	unsigned long bootcount = bootcount_load();
+	unsigned long bootlimit = env_get_ulong("bootlimit", 10, 0);
+
+	if (bootlimit && (bootcount >= bootlimit)) {
+		printf("Warning: Bootlimit (%lu) exceeded.\n", bootcount);
+		return true;
+	}
+
+	return false;
+}
+
+static inline void bootcount_inc(void)
+{
+	unsigned long bootcount = bootcount_load();
+
+	bootcount_store(++bootcount);
+}
+#else
+static inline bool bootcount_error(void) { return false; }
+static inline void bootcount_inc(void) {}
+#endif /* CONFIG_SPL_BOOTCOUNT_LIMIT */