lfs_testbd.h 3.9 KB

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