1 /* SPDX-License-Identifier: GPL-2.0+ */
4 * Stefan Roese, DENX Software Engineering, sr@denx.de.
11 #include <asm/byteorder.h>
13 #if defined(CONFIG_SPL_BOOTCOUNT_LIMIT) || defined(CONFIG_BOOTCOUNT_LIMIT)
15 #if !defined(CONFIG_SYS_BOOTCOUNT_LE) && !defined(CONFIG_SYS_BOOTCOUNT_BE)
16 # if __BYTE_ORDER == __LITTLE_ENDIAN
17 # define CONFIG_SYS_BOOTCOUNT_LE
19 # define CONFIG_SYS_BOOTCOUNT_BE
23 #ifdef CONFIG_SYS_BOOTCOUNT_LE
24 static inline void raw_bootcount_store(volatile u32 *addr, u32 data)
29 static inline u32 raw_bootcount_load(volatile u32 *addr)
34 static inline void raw_bootcount_store(volatile u32 *addr, u32 data)
39 static inline u32 raw_bootcount_load(volatile u32 *addr)
45 DECLARE_GLOBAL_DATA_PTR;
46 static inline int bootcount_error(void)
48 unsigned long bootcount = bootcount_load();
49 unsigned long bootlimit = env_get_ulong("bootlimit", 10, 0);
51 if (bootlimit && bootcount > bootlimit) {
52 printf("Warning: Bootlimit (%lu) exceeded.", bootlimit);
53 if (!(gd->flags & GD_FLG_SPL_INIT))
54 printf(" Using altbootcmd.");
63 static inline void bootcount_inc(void)
65 unsigned long bootcount = bootcount_load();
67 if (gd->flags & GD_FLG_SPL_INIT) {
68 bootcount_store(++bootcount);
72 #ifndef CONFIG_SPL_BUILD
73 /* Only increment bootcount when no bootcount support in SPL */
74 #ifndef CONFIG_SPL_BOOTCOUNT_LIMIT
75 bootcount_store(++bootcount);
77 env_set_ulong("bootcount", bootcount);
78 #endif /* !CONFIG_SPL_BUILD */
81 #if defined(CONFIG_SPL_BUILD) && !defined(CONFIG_SPL_BOOTCOUNT_LIMIT)
82 void bootcount_store(ulong a) {};
83 ulong bootcount_load(void) { return 0; }
84 #endif /* CONFIG_SPL_BUILD && !CONFIG_SPL_BOOTCOUNT_LIMIT */
86 static inline int bootcount_error(void) { return 0; }
87 static inline void bootcount_inc(void) {}
88 #endif /* CONFIG_SPL_BOOTCOUNT_LIMIT || CONFIG_BOOTCOUNT_LIMIT */
89 #endif /* _BOOTCOUNT_H__ */