1 /* SPDX-License-Identifier: GPL-2.0+ */
4 * Stefan Roese, DENX Software Engineering, sr@denx.de.
10 #include <asm/global_data.h>
12 #include <asm/byteorder.h>
15 #ifdef CONFIG_DM_BOOTCOUNT
17 struct bootcount_ops {
19 * get() - get the current bootcount value
21 * Returns the current counter value of the bootcount backing
24 * @dev: Device to read from
25 * @bootcount: Address to put the current bootcount value
27 int (*get)(struct udevice *dev, u32 *bootcount);
30 * set() - set a bootcount value (e.g. to reset or increment)
32 * Sets the value in the bootcount backing store.
34 * @dev: Device to read from
35 * @bootcount: New bootcount value to store
37 int (*set)(struct udevice *dev, const u32 bootcount);
40 /* Access the operations for a bootcount device */
41 #define bootcount_get_ops(dev) ((struct bootcount_ops *)(dev)->driver->ops)
44 * dm_bootcount_get() - Read the current value from a bootcount storage
46 * @dev: Device to read from
47 * @bootcount: Place to put the current bootcount
48 * Return: 0 if OK, -ve on error
50 int dm_bootcount_get(struct udevice *dev, u32 *bootcount);
53 * dm_bootcount_set() - Write a value to a bootcount storage
55 * @dev: Device to read from
56 * @bootcount: Value to be written to the backing storage
57 * Return: 0 if OK, -ve on error
59 int dm_bootcount_set(struct udevice *dev, u32 bootcount);
63 /** bootcount_store() - store the current bootcount */
64 void bootcount_store(ulong);
67 * bootcount_load() - load the current bootcount
69 * Return: bootcount, read from the appropriate location
71 ulong bootcount_load(void);
73 #if defined(CONFIG_SPL_BOOTCOUNT_LIMIT) || defined(CONFIG_TPL_BOOTCOUNT_LIMIT) || defined(CONFIG_BOOTCOUNT_LIMIT)
75 #ifdef CONFIG_SYS_BOOTCOUNT_LE
76 static inline void raw_bootcount_store(volatile u32 *addr, u32 data)
81 static inline u32 raw_bootcount_load(volatile u32 *addr)
86 static inline void raw_bootcount_store(volatile u32 *addr, u32 data)
91 static inline u32 raw_bootcount_load(volatile u32 *addr)
97 DECLARE_GLOBAL_DATA_PTR;
98 static inline int bootcount_error(void)
100 unsigned long bootcount = bootcount_load();
101 unsigned long bootlimit = env_get_ulong("bootlimit", 10, 0);
103 if (bootlimit && bootcount > bootlimit) {
104 printf("Warning: Bootlimit (%lu) exceeded.", bootlimit);
105 if (!(gd->flags & GD_FLG_SPL_INIT))
106 printf(" Using altbootcmd.");
115 static inline void bootcount_inc(void)
117 unsigned long bootcount = bootcount_load();
119 if (gd->flags & GD_FLG_SPL_INIT) {
120 bootcount_store(++bootcount);
124 #ifndef CONFIG_SPL_BUILD
125 /* Only increment bootcount when no bootcount support in SPL */
126 #if !defined(CONFIG_SPL_BOOTCOUNT_LIMIT) && !defined(CONFIG_TPL_BOOTCOUNT_LIMIT)
127 bootcount_store(++bootcount);
129 env_set_ulong("bootcount", bootcount);
130 #endif /* !CONFIG_SPL_BUILD */
134 static inline int bootcount_error(void) { return 0; }
135 static inline void bootcount_inc(void) {}
136 #endif /* CONFIG_SPL_BOOTCOUNT_LIMIT || CONFIG_TPL_BOOTCOUNT_LIMIT || CONFIG_BOOTCOUNT_LIMIT */
137 #endif /* _BOOTCOUNT_H__ */