bootcount.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. /** bootcount_store() - store the current bootcount */
  54. void bootcount_store(ulong);
  55. /**
  56. * bootcount_load() - load the current bootcount
  57. *
  58. * @return bootcount, read from the appropriate location
  59. */
  60. ulong bootcount_load(void);
  61. #if defined(CONFIG_SPL_BOOTCOUNT_LIMIT) || defined(CONFIG_BOOTCOUNT_LIMIT)
  62. #if !defined(CONFIG_SYS_BOOTCOUNT_LE) && !defined(CONFIG_SYS_BOOTCOUNT_BE)
  63. # if __BYTE_ORDER == __LITTLE_ENDIAN
  64. # define CONFIG_SYS_BOOTCOUNT_LE
  65. # else
  66. # define CONFIG_SYS_BOOTCOUNT_BE
  67. # endif
  68. #endif
  69. #ifdef CONFIG_SYS_BOOTCOUNT_LE
  70. static inline void raw_bootcount_store(volatile u32 *addr, u32 data)
  71. {
  72. out_le32(addr, data);
  73. }
  74. static inline u32 raw_bootcount_load(volatile u32 *addr)
  75. {
  76. return in_le32(addr);
  77. }
  78. #else
  79. static inline void raw_bootcount_store(volatile u32 *addr, u32 data)
  80. {
  81. out_be32(addr, data);
  82. }
  83. static inline u32 raw_bootcount_load(volatile u32 *addr)
  84. {
  85. return in_be32(addr);
  86. }
  87. #endif
  88. DECLARE_GLOBAL_DATA_PTR;
  89. static inline int bootcount_error(void)
  90. {
  91. unsigned long bootcount = bootcount_load();
  92. unsigned long bootlimit = env_get_ulong("bootlimit", 10, 0);
  93. if (bootlimit && bootcount > bootlimit) {
  94. printf("Warning: Bootlimit (%lu) exceeded.", bootlimit);
  95. if (!(gd->flags & GD_FLG_SPL_INIT))
  96. printf(" Using altbootcmd.");
  97. printf("\n");
  98. return 1;
  99. }
  100. return 0;
  101. }
  102. static inline void bootcount_inc(void)
  103. {
  104. unsigned long bootcount = bootcount_load();
  105. if (gd->flags & GD_FLG_SPL_INIT) {
  106. bootcount_store(++bootcount);
  107. return;
  108. }
  109. #ifndef CONFIG_SPL_BUILD
  110. /* Only increment bootcount when no bootcount support in SPL */
  111. #ifndef CONFIG_SPL_BOOTCOUNT_LIMIT
  112. bootcount_store(++bootcount);
  113. #endif
  114. env_set_ulong("bootcount", bootcount);
  115. #endif /* !CONFIG_SPL_BUILD */
  116. }
  117. #else
  118. static inline int bootcount_error(void) { return 0; }
  119. static inline void bootcount_inc(void) {}
  120. #endif /* CONFIG_SPL_BOOTCOUNT_LIMIT || CONFIG_BOOTCOUNT_LIMIT */
  121. #endif /* _BOOTCOUNT_H__ */