lfs_testbd.h 3.9 KB

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