1 /* SPDX-License-Identifier: GPL-2.0+ */
4 * Stefan Roese, DENX Software Engineering, sr@denx.de.
11 #include <asm/byteorder.h>
13 #ifdef CONFIG_DM_BOOTCOUNT
15 struct bootcount_ops {
17 * get() - get the current bootcount value
19 * Returns the current counter value of the bootcount backing
22 * @dev: Device to read from
23 * @bootcount: Address to put the current bootcount value
25 int (*get)(struct udevice *dev, u32 *bootcount);
28 * set() - set a bootcount value (e.g. to reset or increment)
30 * Sets the value in the bootcount backing store.
32 * @dev: Device to read from
33 * @bootcount: New bootcount value to store
35 int (*set)(struct udevice *dev, const u32 bootcount);
38 /* Access the operations for a bootcount device */
39 #define bootcount_get_ops(dev) ((struct bootcount_ops *)(dev)->driver->ops)
42 * dm_bootcount_get() - Read the current value from a bootcount storage
44 * @dev: Device to read from
45 * @bootcount: Place to put the current bootcount
46 * @return 0 if OK, -ve on error
48 int dm_bootcount_get(struct udevice *dev, u32 *bootcount);
51 * dm_bootcount_set() - Write a value to a bootcount storage
53 * @dev: Device to read from
54 * @bootcount: Value to be written to the backing storage
55 * @return 0 if OK, -ve on error
57 int dm_bootcount_set(struct udevice *dev, u32 bootcount);
61 #if defined(CONFIG_SPL_BOOTCOUNT_LIMIT) || defined(CONFIG_BOOTCOUNT_LIMIT)
63 #if !defined(CONFIG_SYS_BOOTCOUNT_LE) && !defined(CONFIG_SYS_BOOTCOUNT_BE)
64 # if __BYTE_ORDER == __LITTLE_ENDIAN
65 # define CONFIG_SYS_BOOTCOUNT_LE
67 # define CONFIG_SYS_BOOTCOUNT_BE
71 #ifdef CONFIG_SYS_BOOTCOUNT_LE
72 static inline void raw_bootcount_store(volatile u32 *addr, u32 data)
77 static inline u32 raw_bootcount_load(volatile u32 *addr)
82 static inline void raw_bootcount_store(volatile u32 *addr, u32 data)
87 static inline u32 raw_bootcount_load(volatile u32 *addr)
93 DECLARE_GLOBAL_DATA_PTR;
94 static inline int bootcount_error(void)
96 unsigned long bootcount = bootcount_load();
97 unsigned long bootlimit = env_get_ulong("bootlimit", 10, 0);
99 if (bootlimit && bootcount > bootlimit) {
100 printf("Warning: Bootlimit (%lu) exceeded.", bootlimit);
101 if (!(gd->flags & GD_FLG_SPL_INIT))
102 printf(" Using altbootcmd.");
111 static inline void bootcount_inc(void)
113 unsigned long bootcount = bootcount_load();
115 if (gd->flags & GD_FLG_SPL_INIT) {
116 bootcount_store(++bootcount);
120 #ifndef CONFIG_SPL_BUILD
121 /* Only increment bootcount when no bootcount support in SPL */
122 #ifndef CONFIG_SPL_BOOTCOUNT_LIMIT
123 bootcount_store(++bootcount);
125 env_set_ulong("bootcount", bootcount);
126 #endif /* !CONFIG_SPL_BUILD */
129 #if defined(CONFIG_SPL_BUILD) && !defined(CONFIG_SPL_BOOTCOUNT_LIMIT)
130 void bootcount_store(ulong a) {};
131 ulong bootcount_load(void) { return 0; }
132 #endif /* CONFIG_SPL_BUILD && !CONFIG_SPL_BOOTCOUNT_LIMIT */
134 static inline int bootcount_error(void) { return 0; }
135 static inline void bootcount_inc(void) {}
136 #endif /* CONFIG_SPL_BOOTCOUNT_LIMIT || CONFIG_BOOTCOUNT_LIMIT */
137 #endif /* _BOOTCOUNT_H__ */