miniffs.h 4.3 KB

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