bootcount.h 3.3 KB

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