miniffs.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /******************************************************************************
  2. * MiniFFS : Mini Flat File System
  3. * miniffs.h: MiniFFS main header
  4. *
  5. * Copyright (c) 2008-2022 986-Studio. All rights reserved.
  6. *
  7. ******************************************************************************/
  8. #ifndef MINIFFS_H
  9. #define MINIFFS_H
  10. #include <stdint.h>
  11. /*
  12. * The pack(1) may not be needed, but better be safe than sorry to
  13. * have a consistent binary representation across architectures
  14. */
  15. #pragma pack(1)
  16. typedef struct fileentry_t
  17. {
  18. char name[8];
  19. char ext[3];
  20. uint32_t size;
  21. uint32_t offset;
  22. } fileentry_t;
  23. typedef struct miniffs_header_t
  24. {
  25. uint32_t magic;
  26. uint32_t entry_count;
  27. fileentry_t fent[];
  28. } miniffs_header_t;
  29. #pragma pack()
  30. #if BUILD_PLATFORM_MEMORY
  31. #include <platform/memory.h>
  32. #elif BUILD_PLATFORM_FILE
  33. #include <platform/file.h>
  34. #else
  35. #error Unknown build target.
  36. #endif
  37. /* Somewhat similar structure to the plain C FILE structure */
  38. typedef struct file_t
  39. {
  40. void *private;
  41. fileentry_t *fent; /***< file linked to this structure */
  42. uint32_t offset; /***< current position in the file */
  43. } file_t;
  44. #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  45. #define MAKE4(_a, _b, _c, _d) (((_a) & 0xFF) << 24) | (((_b) & 0xFF) << 16) | (((_c) & 0xFF) << 8) | ((_d) & 0xFF)
  46. #else
  47. #define MAKE4(_d, _c, _b, _a) (((_a) & 0xFF) << 24) | (((_b) & 0xFF) << 16) | (((_c) & 0xFF) << 8) | ((_d) & 0xFF)
  48. #endif
  49. #define MINIFFS_MAGIC MAKE4('M', 'F', 'F', 'S')
  50. enum {
  51. MFFS_SEEK_SET, /***< Seek from beginning of file */
  52. MFFS_SEEK_CUR, /***< Seek from current position */
  53. MFFS_SEEK_END /***< Seek from end of file */
  54. };
  55. /*
  56. * Public functions used reading the filesystem
  57. * This implementation is system dependant as it relly on how the memory is architectured and where the MiniFFS is stored.
  58. */
  59. /* miniffs_openfs is backend specific and will be found in the backend header file */
  60. file_t *miniffs_open(miniffs_t *fs, char *filename); /***< Open a file */
  61. int miniffs_close(file_t *file); /***< Close a file */
  62. void *miniffs_map(file_t *file); /***< Map a file to memory */
  63. int miniffs_read(void *ptr, size_t size, size_t nmemb, file_t *file); /***< Read bytes from a file */
  64. int miniffs_seek(file_t *file, size_t offset, int whence); /***< Set position in a file */
  65. size_t miniffs_tell(file_t *file); /***< Get current position in a file*/
  66. typedef enum miniffs_error_t
  67. {
  68. MINIFFS_NOERROR = 0,
  69. MINIFFS_INVALID_FS,
  70. MINIFFS_FILE_NOT_FOUND,
  71. //MINIFFS_,
  72. } miniffs_error_t;
  73. miniffs_error_t miniffs_geterror(); /***< Return last error */
  74. #ifdef BUILD_HOST_TOOLS
  75. /*
  76. * Functions used for offline creation of the filesystem
  77. */
  78. miniffs_t *miniffs_createfs();
  79. int miniffs_addfile(miniffs_t *fs, char *name, char *ext, char *host_path);
  80. int miniffs_delfile(miniffs_t *fs, char *name, char *ext, char *host_path);
  81. int miniffs_writeimage(miniffs_t *fs, char *host_path);
  82. int miniffs_closefs(miniffs_t *fs);
  83. #endif /* BUILD_HOST_TOOLS */
  84. #ifdef __miniffs_internal
  85. /*
  86. * Function that are private to the library
  87. */
  88. int miniffs_checkfs(miniffs_t *fs);
  89. fileentry_t *miniffs_findfile(miniffs_t *fs, char *filename);
  90. #endif /* __miniffs_internal */
  91. #endif /* MINIFFS_H */