bootcount.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * (C) Copyright 2012
  4. * Stefan Roese, DENX Software Engineering, sr@denx.de.
  5. */
  6. #ifndef _BOOTCOUNT_H__
  7. #define _BOOTCOUNT_H__
  8. #include <common.h>
  9. #include <asm/io.h>
  10. #include <asm/byteorder.h>
  11. #if defined(CONFIG_SPL_BOOTCOUNT_LIMIT) || defined(CONFIG_BOOTCOUNT_LIMIT)
  12. #if !defined(CONFIG_SYS_BOOTCOUNT_LE) && !defined(CONFIG_SYS_BOOTCOUNT_BE)
  13. # if __BYTE_ORDER == __LITTLE_ENDIAN
  14. # define CONFIG_SYS_BOOTCOUNT_LE
  15. # else
  16. # define CONFIG_SYS_BOOTCOUNT_BE
  17. # endif
  18. #endif
  19. #ifdef CONFIG_SYS_BOOTCOUNT_LE
  20. static inline void raw_bootcount_store(volatile u32 *addr, u32 data)
  21. {
  22. out_le32(addr, data);
  23. }
  24. static inline u32 raw_bootcount_load(volatile u32 *addr)
  25. {
  26. return in_le32(addr);
  27. }
  28. #else
  29. static inline void raw_bootcount_store(volatile u32 *addr, u32 data)
  30. {
  31. out_be32(addr, data);
  32. }
  33. static inline u32 raw_bootcount_load(volatile u32 *addr)
  34. {
  35. return in_be32(addr);
  36. }
  37. #endif
  38. DECLARE_GLOBAL_DATA_PTR;
  39. static inline int bootcount_error(void)
  40. {
  41. unsigned long bootcount = bootcount_load();
  42. unsigned long bootlimit = env_get_ulong("bootlimit", 10, 0);
  43. if (bootlimit && bootcount > bootlimit) {
  44. printf("Warning: Bootlimit (%lu) exceeded.", bootlimit);
  45. if (!(gd->flags & GD_FLG_SPL_INIT))
  46. printf(" Using altbootcmd.");
  47. printf("\n");
  48. return 1;
  49. }
  50. return 0;
  51. }
  52. static inline void bootcount_inc(void)
  53. {
  54. unsigned long bootcount = bootcount_load();
  55. if (gd->flags & GD_FLG_SPL_INIT) {
  56. bootcount_store(++bootcount);
  57. return;
  58. }
  59. #ifndef CONFIG_SPL_BUILD
  60. /* Only increment bootcount when no bootcount support in SPL */
  61. #ifndef CONFIG_SPL_BOOTCOUNT_LIMIT
  62. bootcount_store(++bootcount);
  63. #endif
  64. env_set_ulong("bootcount", bootcount);
  65. #endif /* !CONFIG_SPL_BUILD */
  66. }
  67. #if defined(CONFIG_SPL_BUILD) && !defined(CONFIG_SPL_BOOTCOUNT_LIMIT)
  68. void bootcount_store(ulong a) {};
  69. ulong bootcount_load(void) { return 0; }
  70. #endif /* CONFIG_SPL_BUILD && !CONFIG_SPL_BOOTCOUNT_LIMIT */
  71. #else
  72. static inline int bootcount_error(void) { return 0; }
  73. static inline void bootcount_inc(void) {}
  74. #endif /* CONFIG_SPL_BOOTCOUNT_LIMIT || CONFIG_BOOTCOUNT_LIMIT */
  75. #endif /* _BOOTCOUNT_H__ */