miniffs.h 4.3 KB

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