lfs_testbd.h 3.7 KB

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