lfs_filebd.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Block device emulated in a file
  3. *
  4. * Copyright (c) 2022, The littlefs authors.
  5. * Copyright (c) 2017, Arm Limited. All rights reserved.
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #ifndef LFS_FILEBD_H
  9. #define LFS_FILEBD_H
  10. #include "lfs.h"
  11. #include "lfs_util.h"
  12. #ifdef __cplusplus
  13. extern "C"
  14. {
  15. #endif
  16. // Block device specific tracing
  17. #ifdef LFS_FILEBD_YES_TRACE
  18. #define LFS_FILEBD_TRACE(...) LFS_TRACE(__VA_ARGS__)
  19. #else
  20. #define LFS_FILEBD_TRACE(...)
  21. #endif
  22. // filebd config (optional)
  23. struct lfs_filebd_config {
  24. // 8-bit erase value to use for simulating erases. -1 does not simulate
  25. // erases, which can speed up testing by avoiding all the extra block-device
  26. // operations to store the erase value.
  27. int32_t erase_value;
  28. };
  29. // filebd state
  30. typedef struct lfs_filebd {
  31. int fd;
  32. const struct lfs_filebd_config *cfg;
  33. } lfs_filebd_t;
  34. // Create a file block device using the geometry in lfs_config
  35. int lfs_filebd_create(const struct lfs_config *cfg, const char *path);
  36. int lfs_filebd_createcfg(const struct lfs_config *cfg, const char *path,
  37. const struct lfs_filebd_config *bdcfg);
  38. // Clean up memory associated with block device
  39. int lfs_filebd_destroy(const struct lfs_config *cfg);
  40. // Read a block
  41. int lfs_filebd_read(const struct lfs_config *cfg, lfs_block_t block,
  42. lfs_off_t off, void *buffer, lfs_size_t size);
  43. // Program a block
  44. //
  45. // The block must have previously been erased.
  46. int lfs_filebd_prog(const struct lfs_config *cfg, lfs_block_t block,
  47. lfs_off_t off, const void *buffer, lfs_size_t size);
  48. // Erase a block
  49. //
  50. // A block must be erased before being programmed. The
  51. // state of an erased block is undefined.
  52. int lfs_filebd_erase(const struct lfs_config *cfg, lfs_block_t block);
  53. // Sync the block device
  54. int lfs_filebd_sync(const struct lfs_config *cfg);
  55. #ifdef __cplusplus
  56. } /* extern "C" */
  57. #endif
  58. #endif