lfs_emubd.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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_config.h"
  10. #include "lfs_util.h"
  11. #include "lfs_bd.h"
  12. // Config options
  13. #ifndef LFS_EMUBD_READ_SIZE
  14. #define LFS_EMUBD_READ_SIZE 1
  15. #endif
  16. #ifndef LFS_EMUBD_PROG_SIZE
  17. #define LFS_EMUBD_PROG_SIZE 1
  18. #endif
  19. #ifndef LFS_EMUBD_ERASE_SIZE
  20. #define LFS_EMUBD_ERASE_SIZE 512
  21. #endif
  22. #ifndef LFS_EMUBD_TOTAL_SIZE
  23. #define LFS_EMUBD_TOTAL_SIZE 524288
  24. #endif
  25. // Stats for debugging and optimization
  26. struct lfs_bd_stats {
  27. uint64_t read_count;
  28. uint64_t prog_count;
  29. uint64_t erase_count;
  30. };
  31. // The emu bd state
  32. typedef struct lfs_emubd {
  33. char *path;
  34. char *child;
  35. struct lfs_bd_info info;
  36. struct lfs_bd_stats stats;
  37. } lfs_emubd_t;
  38. // Create a block device using path for the directory to store blocks
  39. int lfs_emubd_create(lfs_emubd_t *emu, const char *path);
  40. // Clean up memory associated with emu block device
  41. void lfs_emubd_destroy(lfs_emubd_t *emu);
  42. // Read a block
  43. int lfs_emubd_read(lfs_emubd_t *bd, lfs_block_t block,
  44. lfs_off_t off, lfs_size_t size, void *buffer);
  45. // Program a block
  46. //
  47. // The block must have previously been erased.
  48. int lfs_emubd_prog(lfs_emubd_t *bd, lfs_block_t block,
  49. lfs_off_t off, lfs_size_t size, const void *buffer);
  50. // Erase a block
  51. //
  52. // A block must be erased before being programmed. The
  53. // state of an erased block is undefined.
  54. int lfs_emubd_erase(lfs_emubd_t *bd, lfs_block_t block,
  55. lfs_off_t off, lfs_size_t size);
  56. // Sync the block device
  57. int lfs_emubd_sync(lfs_emubd_t *bd);
  58. // Get a description of the block device
  59. //
  60. // Any unknown information may be left unmodified
  61. int lfs_emubd_info(lfs_emubd_t *bd, struct lfs_bd_info *info);
  62. // Get stats of operations on the block device
  63. //
  64. // Used for debugging and optimizations
  65. int lfs_emubd_stats(lfs_emubd_t *bd, struct lfs_bd_stats *stats);
  66. // Block device operations
  67. extern const struct lfs_bd_ops lfs_emubd_ops;
  68. #endif