miniffs.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. #include <stdbool.h>
  12. #define MINIFFS_VERSION_MAJOR (1)
  13. #define MINIFFS_VERSION_MINOR (0)
  14. #define MINIFFS_FILENAME_LENGTH (8)
  15. #define MINIFFS_EXTENSION_LENGTH (3)
  16. /* the full name is FILENAME + '.' + EXTENSION */
  17. #define MINIFFS_FULLNAME_LENGTH (MINIFFS_FILENAME_LENGTH + MINIFFS_EXTENSION_LENGTH + 1)
  18. /*
  19. * The pack(1) may not be needed, but better be safe than sorry to
  20. * have a consistent binary representation across architectures
  21. */
  22. #pragma pack(1)
  23. typedef struct fileentry_t
  24. {
  25. char name[MINIFFS_FILENAME_LENGTH];
  26. char ext[MINIFFS_EXTENSION_LENGTH];
  27. uint32_t size;
  28. uint32_t offset;
  29. } fileentry_t;
  30. typedef struct miniffs_header_t
  31. {
  32. uint32_t magic;
  33. uint8_t fs_version_major;
  34. uint8_t fs_version_minor;
  35. uint8_t fs_filename_len;
  36. uint8_t fs_extention_len;
  37. uint32_t entry_count;
  38. fileentry_t fent[];
  39. } miniffs_header_t;
  40. #pragma pack()
  41. #if BUILD_PLATFORM_MEMORY
  42. #include <platform/memory.h>
  43. #elif BUILD_PLATFORM_FILE
  44. #include <platform/file.h>
  45. #else
  46. #error Unknown build target.
  47. #endif
  48. /* Somewhat similar structure to the plain C FILE structure */
  49. typedef struct file_t
  50. {
  51. void *private;
  52. fileentry_t *fent; /***< file linked to this structure */
  53. uint32_t offset; /***< current position in the file */
  54. } file_t;
  55. #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  56. #define MAKE4(_a, _b, _c, _d) ((((_a) & 0xFF) << 24) | (((_b) & 0xFF) << 16) | (((_c) & 0xFF) << 8) | ((_d) & 0xFF))
  57. #else
  58. #define MAKE4(_d, _c, _b, _a) ((((_a) & 0xFF) << 24) | (((_b) & 0xFF) << 16) | (((_c) & 0xFF) << 8) | ((_d) & 0xFF))
  59. #endif
  60. #define MINIFFS_MAGIC MAKE4('M', 'F', 'F', 'S')
  61. enum {
  62. MFFS_SEEK_SET, /***< Seek from beginning of file */
  63. MFFS_SEEK_CUR, /***< Seek from current position */
  64. MFFS_SEEK_END /***< Seek from end of file */
  65. };
  66. /*
  67. * Public functions used reading the filesystem
  68. * This implementation is system dependant as it relly on how the memory is architectured and where the MiniFFS is stored.
  69. */
  70. /* miniffs_openfs is backend specific and will be found in the backend header file */
  71. file_t *miniffs_open(miniffs_t *fs, char *filename); /***< Open a file */
  72. int miniffs_close(file_t *file); /***< Close a file */
  73. void *miniffs_map(file_t *file); /***< Map a file to memory */
  74. int miniffs_read_blocks(void *ptr, size_t size, size_t nmemb, file_t *file); /***< Read blocks of bytes from a file */
  75. uint8_t miniffs_read(file_t *file); /***< Read a single byte from a file */
  76. int miniffs_seek(file_t *file, size_t offset, int whence); /***< Set position in a file */
  77. size_t miniffs_tell(file_t *file); /***< Get current position in a file*/
  78. typedef enum miniffs_error_t
  79. {
  80. MINIFFS_NOERROR = 0,
  81. MINIFFS_INVALID_FS,
  82. MINIFFS_INVALID_NAME,
  83. MINIFFS_INVALID_PARAMS,
  84. MINIFFS_FILE_NOT_FOUND,
  85. MINIFFS_ALLOCATION_ERROR,
  86. MINIFFS_END_OF_FILE,
  87. //MINIFFS_,
  88. } miniffs_error_t;
  89. miniffs_error_t miniffs_geterror(); /***< Return last error */
  90. #ifdef BUILD_HOST_TOOLS
  91. /*
  92. * Functions used for offline creation of the filesystem
  93. */
  94. miniffs_t *miniffs_createfs();
  95. int miniffs_addfile(miniffs_t *fs, char *name, char *ext, char *host_path);
  96. int miniffs_delfile(miniffs_t *fs, char *name, char *ext, char *host_path);
  97. int miniffs_writeimage(miniffs_t *fs, char *host_path);
  98. int miniffs_closefs(miniffs_t *fs);
  99. #endif /* BUILD_HOST_TOOLS */
  100. #ifdef __miniffs_internal
  101. /*
  102. * Function that are private to the library
  103. */
  104. bool miniffs_isvalidfs(miniffs_t *fs);
  105. fileentry_t *miniffs_findfile(miniffs_t *fs, char *filename);
  106. void miniffs_seterror(miniffs_error_t err);
  107. void *miniffs_getfileaddr(miniffs_t *fs, fileentry_t *fent);
  108. #endif /* __miniffs_internal */
  109. #endif /* MINIFFS_H */