lfs_testbd.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Testing block device, wraps filebd and rambd while providing a bunch
  3. * of hooks for testing littlefs in various conditions.
  4. *
  5. * Copyright (c) 2022, The littlefs authors.
  6. * Copyright (c) 2017, Arm Limited. All rights reserved.
  7. * SPDX-License-Identifier: BSD-3-Clause
  8. */
  9. #ifndef LFS_TESTBD_H
  10. #define LFS_TESTBD_H
  11. #include "lfs.h"
  12. #include "lfs_util.h"
  13. #include "bd/lfs_rambd.h"
  14. #include "bd/lfs_filebd.h"
  15. #ifdef __cplusplus
  16. extern "C"
  17. {
  18. #endif
  19. // Block device specific tracing
  20. #ifndef LFS_TESTBD_TRACE
  21. #ifdef LFS_TESTBD_YES_TRACE
  22. #define LFS_TESTBD_TRACE(...) LFS_TRACE(__VA_ARGS__)
  23. #else
  24. #define LFS_TESTBD_TRACE(...)
  25. #endif
  26. #endif
  27. // Mode determining how "bad blocks" behave during testing. This simulates
  28. // some real-world circumstances such as progs not sticking (prog-noop),
  29. // a readonly disk (erase-noop), and ECC failures (read-error).
  30. //
  31. // Not that read-noop is not allowed. Read _must_ return a consistent (but
  32. // may be arbitrary) value on every read.
  33. enum lfs_testbd_badblock_behavior {
  34. LFS_TESTBD_BADBLOCK_PROGERROR,
  35. LFS_TESTBD_BADBLOCK_ERASEERROR,
  36. LFS_TESTBD_BADBLOCK_READERROR,
  37. LFS_TESTBD_BADBLOCK_PROGNOOP,
  38. LFS_TESTBD_BADBLOCK_ERASENOOP,
  39. };
  40. // Type for measuring wear
  41. typedef uint32_t lfs_testbd_wear_t;
  42. typedef int32_t lfs_testbd_swear_t;
  43. // testbd config, this is required for testing
  44. struct lfs_testbd_config {
  45. // 8-bit erase value to use for simulating erases. -1 does not simulate
  46. // erases, which can speed up testing by avoiding all the extra block-device
  47. // operations to store the erase value.
  48. int32_t erase_value;
  49. // Number of erase cycles before a block becomes "bad". The exact behavior
  50. // of bad blocks is controlled by the badblock_mode.
  51. uint32_t erase_cycles;
  52. // The mode determining how bad blocks fail
  53. uint8_t badblock_behavior;
  54. // Number of write operations (erase/prog) before forcefully killing
  55. // the program with exit. Simulates power-loss. 0 disables.
  56. uint32_t power_cycles;
  57. // Optional buffer for RAM block device.
  58. void *buffer;
  59. // Optional buffer for wear
  60. void *wear_buffer;
  61. };
  62. // testbd state
  63. typedef struct lfs_testbd {
  64. union {
  65. struct {
  66. lfs_filebd_t bd;
  67. struct lfs_filebd_config cfg;
  68. } file;
  69. struct {
  70. lfs_rambd_t bd;
  71. struct lfs_rambd_config cfg;
  72. } ram;
  73. } u;
  74. bool persist;
  75. uint32_t power_cycles;
  76. lfs_testbd_wear_t *wear;
  77. const struct lfs_testbd_config *cfg;
  78. } lfs_testbd_t;
  79. /// Block device API ///
  80. // Create a test block device using the geometry in lfs_config
  81. //
  82. // Note that filebd is used if a path is provided, if path is NULL
  83. // testbd will use rambd which can be much faster.
  84. int lfs_testbd_create(const struct lfs_config *cfg, const char *path);
  85. int lfs_testbd_createcfg(const struct lfs_config *cfg, const char *path,
  86. const struct lfs_testbd_config *bdcfg);
  87. // Clean up memory associated with block device
  88. int lfs_testbd_destroy(const struct lfs_config *cfg);
  89. // Read a block
  90. int lfs_testbd_read(const struct lfs_config *cfg, lfs_block_t block,
  91. lfs_off_t off, void *buffer, lfs_size_t size);
  92. // Program a block
  93. //
  94. // The block must have previously been erased.
  95. int lfs_testbd_prog(const struct lfs_config *cfg, lfs_block_t block,
  96. lfs_off_t off, const void *buffer, lfs_size_t size);
  97. // Erase a block
  98. //
  99. // A block must be erased before being programmed. The
  100. // state of an erased block is undefined.
  101. int lfs_testbd_erase(const struct lfs_config *cfg, lfs_block_t block);
  102. // Sync the block device
  103. int lfs_testbd_sync(const struct lfs_config *cfg);
  104. /// Additional extended API for driving test features ///
  105. // Get simulated wear on a given block
  106. lfs_testbd_swear_t lfs_testbd_getwear(const struct lfs_config *cfg,
  107. lfs_block_t block);
  108. // Manually set simulated wear on a given block
  109. int lfs_testbd_setwear(const struct lfs_config *cfg,
  110. lfs_block_t block, lfs_testbd_wear_t wear);
  111. #ifdef __cplusplus
  112. } /* extern "C" */
  113. #endif
  114. #endif