1 /* SPDX-License-Identifier: GPL-2.0+ */
4 * Stefan Roese, DENX Software Engineering, sr@denx.de.
11 #include <asm/byteorder.h>
14 #ifdef CONFIG_DM_BOOTCOUNT
16 struct bootcount_ops {
18 * get() - get the current bootcount value
20 * Returns the current counter value of the bootcount backing
23 * @dev: Device to read from
24 * @bootcount: Address to put the current bootcount value
26 int (*get)(struct udevice *dev, u32 *bootcount);
29 * set() - set a bootcount value (e.g. to reset or increment)
31 * Sets the value in the bootcount backing store.
33 * @dev: Device to read from
34 * @bootcount: New bootcount value to store
36 int (*set)(struct udevice *dev, const u32 bootcount);
39 /* Access the operations for a bootcount device */
40 #define bootcount_get_ops(dev) ((struct bootcount_ops *)(dev)->driver->ops)
43 * dm_bootcount_get() - Read the current value from a bootcount storage
45 * @dev: Device to read from
46 * @bootcount: Place to put the current bootcount
47 * @return 0 if OK, -ve on error
49 int dm_bootcount_get(struct udevice *dev, u32 *bootcount);
52 * dm_bootcount_set() - Write a value to a bootcount storage
54 * @dev: Device to read from
55 * @bootcount: Value to be written to the backing storage
56 * @return 0 if OK, -ve on error
58 int dm_bootcount_set(struct udevice *dev, u32 bootcount);
62 /** bootcount_store() - store the current bootcount */
63 void bootcount_store(ulong);
66 * bootcount_load() - load the current bootcount
68 * @return bootcount, read from the appropriate location
70 ulong bootcount_load(void);
72 #if defined(CONFIG_SPL_BOOTCOUNT_LIMIT) || defined(CONFIG_BOOTCOUNT_LIMIT)
74 #if !defined(CONFIG_SYS_BOOTCOUNT_LE) && !defined(CONFIG_SYS_BOOTCOUNT_BE)
75 # if __BYTE_ORDER == __LITTLE_ENDIAN
76 # define CONFIG_SYS_BOOTCOUNT_LE
78 # define CONFIG_SYS_BOOTCOUNT_BE
82 #ifdef CONFIG_SYS_BOOTCOUNT_LE
83 static inline void raw_bootcount_store(volatile u32 *addr, u32 data)
88 static inline u32 raw_bootcount_load(volatile u32 *addr)
93 static inline void raw_bootcount_store(volatile u32 *addr, u32 data)
98 static inline u32 raw_bootcount_load(volatile u32 *addr)
100 return in_be32(addr);
104 DECLARE_GLOBAL_DATA_PTR;
105 static inline int bootcount_error(void)
107 unsigned long bootcount = bootcount_load();
108 unsigned long bootlimit = env_get_ulong("bootlimit", 10, 0);
110 if (bootlimit && bootcount > bootlimit) {
111 printf("Warning: Bootlimit (%lu) exceeded.", bootlimit);
112 if (!(gd->flags & GD_FLG_SPL_INIT))
113 printf(" Using altbootcmd.");
122 static inline void bootcount_inc(void)
124 unsigned long bootcount = bootcount_load();
126 if (gd->flags & GD_FLG_SPL_INIT) {
127 bootcount_store(++bootcount);
131 #ifndef CONFIG_SPL_BUILD
132 /* Only increment bootcount when no bootcount support in SPL */
133 #ifndef CONFIG_SPL_BOOTCOUNT_LIMIT
134 bootcount_store(++bootcount);
136 env_set_ulong("bootcount", bootcount);
137 #endif /* !CONFIG_SPL_BUILD */
141 static inline int bootcount_error(void) { return 0; }
142 static inline void bootcount_inc(void) {}
143 #endif /* CONFIG_SPL_BOOTCOUNT_LIMIT || CONFIG_BOOTCOUNT_LIMIT */
144 #endif /* _BOOTCOUNT_H__ */