lfs_emubd.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Block device emulated on standard files
  3. *
  4. * Copyright (c) 2017 Christopher Haster
  5. * Distributed under the MIT license
  6. */
  7. #ifndef LFS_EMUBD_H
  8. #define LFS_EMUBD_H
  9. #include "lfs.h"
  10. #include "lfs_util.h"
  11. // Config options
  12. #ifndef LFS_EMUBD_READ_SIZE
  13. #define LFS_EMUBD_READ_SIZE 1
  14. #endif
  15. #ifndef LFS_EMUBD_PROG_SIZE
  16. #define LFS_EMUBD_PROG_SIZE 1
  17. #endif
  18. #ifndef LFS_EMUBD_ERASE_SIZE
  19. #define LFS_EMUBD_ERASE_SIZE 512
  20. #endif
  21. #ifndef LFS_EMUBD_TOTAL_SIZE
  22. #define LFS_EMUBD_TOTAL_SIZE 524288
  23. #endif
  24. // The emu bd state
  25. typedef struct lfs_emubd {
  26. char *path;
  27. char *child;
  28. struct {
  29. uint64_t read_count;
  30. uint64_t prog_count;
  31. uint64_t erase_count;
  32. } stats;
  33. struct {
  34. uint32_t read_size;
  35. uint32_t prog_size;
  36. uint32_t block_size;
  37. uint32_t block_count;
  38. } cfg;
  39. } lfs_emubd_t;
  40. // Create a block device using path for the directory to store blocks
  41. int lfs_emubd_create(const struct lfs_config *cfg, const char *path);
  42. // Clean up memory associated with emu block device
  43. void lfs_emubd_destroy(const struct lfs_config *cfg);
  44. // Read a block
  45. int lfs_emubd_read(const struct lfs_config *cfg, lfs_block_t block,
  46. lfs_off_t off, lfs_size_t size, void *buffer);
  47. // Program a block
  48. //
  49. // The block must have previously been erased.
  50. int lfs_emubd_prog(const struct lfs_config *cfg, lfs_block_t block,
  51. lfs_off_t off, lfs_size_t size, const void *buffer);
  52. // Erase a block
  53. //
  54. // A block must be erased before being programmed. The
  55. // state of an erased block is undefined.
  56. int lfs_emubd_erase(const struct lfs_config *cfg, lfs_block_t block);
  57. // Sync the block device
  58. int lfs_emubd_sync(const struct lfs_config *cfg);
  59. #endif