bootcount.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. #include <env.h>
  12. #ifdef CONFIG_DM_BOOTCOUNT
  13. struct bootcount_ops {
  14. /**
  15. * get() - get the current bootcount value
  16. *
  17. * Returns the current counter value of the bootcount backing
  18. * store.
  19. *
  20. * @dev: Device to read from
  21. * @bootcount: Address to put the current bootcount value
  22. */
  23. int (*get)(struct udevice *dev, u32 *bootcount);
  24. /**
  25. * set() - set a bootcount value (e.g. to reset or increment)
  26. *
  27. * Sets the value in the bootcount backing store.
  28. *
  29. * @dev: Device to read from
  30. * @bootcount: New bootcount value to store
  31. */
  32. int (*set)(struct udevice *dev, const u32 bootcount);
  33. };
  34. /* Access the operations for a bootcount device */
  35. #define bootcount_get_ops(dev) ((struct bootcount_ops *)(dev)->driver->ops)
  36. /**
  37. * dm_bootcount_get() - Read the current value from a bootcount storage
  38. *
  39. * @dev: Device to read from
  40. * @bootcount: Place to put the current bootcount
  41. * @return 0 if OK, -ve on error
  42. */
  43. int dm_bootcount_get(struct udevice *dev, u32 *bootcount);
  44. /**
  45. * dm_bootcount_set() - Write a value to a bootcount storage
  46. *
  47. * @dev: Device to read from
  48. * @bootcount: Value to be written to the backing storage
  49. * @return 0 if OK, -ve on error
  50. */
  51. int dm_bootcount_set(struct udevice *dev, u32 bootcount);
  52. #endif
  53. #if defined(CONFIG_SPL_BOOTCOUNT_LIMIT) || defined(CONFIG_BOOTCOUNT_LIMIT)
  54. #if !defined(CONFIG_SYS_BOOTCOUNT_LE) && !defined(CONFIG_SYS_BOOTCOUNT_BE)
  55. # if __BYTE_ORDER == __LITTLE_ENDIAN
  56. # define CONFIG_SYS_BOOTCOUNT_LE
  57. # else
  58. # define CONFIG_SYS_BOOTCOUNT_BE
  59. # endif
  60. #endif
  61. #ifdef CONFIG_SYS_BOOTCOUNT_LE
  62. static inline void raw_bootcount_store(volatile u32 *addr, u32 data)
  63. {
  64. out_le32(addr, data);
  65. }
  66. static inline u32 raw_bootcount_load(volatile u32 *addr)
  67. {
  68. return in_le32(addr);
  69. }
  70. #else
  71. static inline void raw_bootcount_store(volatile u32 *addr, u32 data)
  72. {
  73. out_be32(addr, data);
  74. }
  75. static inline u32 raw_bootcount_load(volatile u32 *addr)
  76. {
  77. return in_be32(addr);
  78. }
  79. #endif
  80. DECLARE_GLOBAL_DATA_PTR;
  81. static inline int bootcount_error(void)
  82. {
  83. unsigned long bootcount = bootcount_load();
  84. unsigned long bootlimit = env_get_ulong("bootlimit", 10, 0);
  85. if (bootlimit && bootcount > bootlimit) {
  86. printf("Warning: Bootlimit (%lu) exceeded.", bootlimit);
  87. if (!(gd->flags & GD_FLG_SPL_INIT))
  88. printf(" Using altbootcmd.");
  89. printf("\n");
  90. return 1;
  91. }
  92. return 0;
  93. }
  94. static inline void bootcount_inc(void)
  95. {
  96. unsigned long bootcount = bootcount_load();
  97. if (gd->flags & GD_FLG_SPL_INIT) {
  98. bootcount_store(++bootcount);
  99. return;
  100. }
  101. #ifndef CONFIG_SPL_BUILD
  102. /* Only increment bootcount when no bootcount support in SPL */
  103. #ifndef CONFIG_SPL_BOOTCOUNT_LIMIT
  104. bootcount_store(++bootcount);
  105. #endif
  106. env_set_ulong("bootcount", bootcount);
  107. #endif /* !CONFIG_SPL_BUILD */
  108. }
  109. #if defined(CONFIG_SPL_BUILD) && !defined(CONFIG_SPL_BOOTCOUNT_LIMIT)
  110. void bootcount_store(ulong a) {};
  111. ulong bootcount_load(void) { return 0; }
  112. #endif /* CONFIG_SPL_BUILD && !CONFIG_SPL_BOOTCOUNT_LIMIT */
  113. #else
  114. static inline int bootcount_error(void) { return 0; }
  115. static inline void bootcount_inc(void) {}
  116. #endif /* CONFIG_SPL_BOOTCOUNT_LIMIT || CONFIG_BOOTCOUNT_LIMIT */
  117. #endif /* _BOOTCOUNT_H__ */