bootcount.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2010-2012
  4. * Stefan Roese, DENX Software Engineering, sr@denx.de.
  5. */
  6. #include <bootcount.h>
  7. #include <linux/compiler.h>
  8. /* Now implement the generic default functions */
  9. __weak void bootcount_store(ulong a)
  10. {
  11. void *reg = (void *)CONFIG_SYS_BOOTCOUNT_ADDR;
  12. uintptr_t flush_start = rounddown(CONFIG_SYS_BOOTCOUNT_ADDR,
  13. CONFIG_SYS_CACHELINE_SIZE);
  14. uintptr_t flush_end;
  15. #if defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD)
  16. raw_bootcount_store(reg, (BOOTCOUNT_MAGIC & 0xffff0000) | a);
  17. flush_end = roundup(CONFIG_SYS_BOOTCOUNT_ADDR + 4,
  18. CONFIG_SYS_CACHELINE_SIZE);
  19. #else
  20. raw_bootcount_store(reg, a);
  21. raw_bootcount_store(reg + 4, BOOTCOUNT_MAGIC);
  22. flush_end = roundup(CONFIG_SYS_BOOTCOUNT_ADDR + 8,
  23. CONFIG_SYS_CACHELINE_SIZE);
  24. #endif /* defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD */
  25. flush_dcache_range(flush_start, flush_end);
  26. }
  27. __weak ulong bootcount_load(void)
  28. {
  29. void *reg = (void *)CONFIG_SYS_BOOTCOUNT_ADDR;
  30. #if defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD)
  31. u32 tmp = raw_bootcount_load(reg);
  32. if ((tmp & 0xffff0000) != (BOOTCOUNT_MAGIC & 0xffff0000))
  33. return 0;
  34. else
  35. return (tmp & 0x0000ffff);
  36. #else
  37. if (raw_bootcount_load(reg + 4) != BOOTCOUNT_MAGIC)
  38. return 0;
  39. else
  40. return raw_bootcount_load(reg);
  41. #endif /* defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD) */
  42. }