X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;ds=sidebyside;f=include%2Fbootcount.h;h=8fa8cf82181a6b1a20b7a377f839e93689273fdd;hb=c37f594280aa3fc9d534420581f7db47ac843297;hp=c0a6b6e0e26c2e934cf317549b1bb84f71db4c5b;hpb=c1cd21db621cb9b626f41d42bd4b8ac2d86709ce;p=oweals%2Fu-boot.git diff --git a/include/bootcount.h b/include/bootcount.h index c0a6b6e0e2..8fa8cf8218 100644 --- a/include/bootcount.h +++ b/include/bootcount.h @@ -9,6 +9,57 @@ #include #include #include +#include + +#ifdef CONFIG_DM_BOOTCOUNT + +struct bootcount_ops { + /** + * get() - get the current bootcount value + * + * Returns the current counter value of the bootcount backing + * store. + * + * @dev: Device to read from + * @bootcount: Address to put the current bootcount value + */ + int (*get)(struct udevice *dev, u32 *bootcount); + + /** + * set() - set a bootcount value (e.g. to reset or increment) + * + * Sets the value in the bootcount backing store. + * + * @dev: Device to read from + * @bootcount: New bootcount value to store + */ + int (*set)(struct udevice *dev, const u32 bootcount); +}; + +/* Access the operations for a bootcount device */ +#define bootcount_get_ops(dev) ((struct bootcount_ops *)(dev)->driver->ops) + +/** + * dm_bootcount_get() - Read the current value from a bootcount storage + * + * @dev: Device to read from + * @bootcount: Place to put the current bootcount + * @return 0 if OK, -ve on error + */ +int dm_bootcount_get(struct udevice *dev, u32 *bootcount); + +/** + * dm_bootcount_set() - Write a value to a bootcount storage + * + * @dev: Device to read from + * @bootcount: Value to be written to the backing storage + * @return 0 if OK, -ve on error + */ +int dm_bootcount_set(struct udevice *dev, u32 bootcount); + +#endif + +#if defined(CONFIG_SPL_BOOTCOUNT_LIMIT) || defined(CONFIG_BOOTCOUNT_LIMIT) #if !defined(CONFIG_SYS_BOOTCOUNT_LE) && !defined(CONFIG_SYS_BOOTCOUNT_BE) # if __BYTE_ORDER == __LITTLE_ENDIAN @@ -39,4 +90,49 @@ static inline u32 raw_bootcount_load(volatile u32 *addr) return in_be32(addr); } #endif + +DECLARE_GLOBAL_DATA_PTR; +static inline int 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.", bootlimit); + if (!(gd->flags & GD_FLG_SPL_INIT)) + printf(" Using altbootcmd."); + printf("\n"); + + return 1; + } + + return 0; +} + +static inline void bootcount_inc(void) +{ + unsigned long bootcount = bootcount_load(); + + if (gd->flags & GD_FLG_SPL_INIT) { + bootcount_store(++bootcount); + return; + } + +#ifndef CONFIG_SPL_BUILD + /* Only increment bootcount when no bootcount support in SPL */ +#ifndef CONFIG_SPL_BOOTCOUNT_LIMIT + bootcount_store(++bootcount); +#endif + env_set_ulong("bootcount", bootcount); +#endif /* !CONFIG_SPL_BUILD */ +} + +#if defined(CONFIG_SPL_BUILD) && !defined(CONFIG_SPL_BOOTCOUNT_LIMIT) +void bootcount_store(ulong a) {}; +ulong bootcount_load(void) { return 0; } +#endif /* CONFIG_SPL_BUILD && !CONFIG_SPL_BOOTCOUNT_LIMIT */ +#else +static inline int bootcount_error(void) { return 0; } +static inline void bootcount_inc(void) {} +#endif /* CONFIG_SPL_BOOTCOUNT_LIMIT || CONFIG_BOOTCOUNT_LIMIT */ #endif /* _BOOTCOUNT_H__ */