miniffs.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. #if BUILD_TARGET == MEMORY
  12. #include <miniffs/platform/memory.h>
  13. #elif BUILD_TARGET == FILE
  14. #include <miniffs/platform/file.h>
  15. #else
  16. #error Unknown build target
  17. #endif
  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[8];
  26. char ext[3];
  27. uint32_t size;
  28. uint32_t offset;
  29. } fileentry_t;
  30. typedef struct miniffs_header_t
  31. {
  32. uint32_t magic;
  33. uint32_t entry_count;
  34. fileentry_t fent[];
  35. } miniffs_header_t;
  36. #pragma pack()
  37. typedef struct miniffs_t miniffs_t;
  38. /* Somewhat similar structure to the plain C FILE structure */
  39. typedef struct file_t
  40. {
  41. void *private;
  42. fileentry_t fent; /***< file linked to this structure */
  43. uint32_t offset; /***< current position in the file */
  44. } file_t;
  45. #define MAKE4(_a, _b, _c, _d) ((_a & 0xFF) << 21) | ((_b & 0xFF) << 16) | ((_c & 0xFF) << 8) | (_d & 0xFF)
  46. #define MINIFFS_MAGIC MAKE4('M', 'F', 'F', 'S')
  47. enum {
  48. MFFS_SEEK_SET, /***< Seek from beginning of file */
  49. MFFS_SEEK_CUR, /***< Seek from current position */
  50. MFFS_SEEK_END /***< Seek from end of file */
  51. };
  52. /*
  53. * Public functions used reading the filesystem
  54. * This implementation is system dependant as it relly on how the memory is architectured and where the MiniFFS is stored.
  55. */
  56. miniffs_t *miniffs_openfs(void *address); /***< Open a MiniFFS filesystem */
  57. file_t *miniffs_open(miniffs_t *fs, char *filename); /***< Open a file */
  58. int miniffs_close(file_t *file); /***< Close a file */
  59. void *miniffs_map(file_t *file); /***< Map a file to memory */
  60. int miniffs_read(void *ptr, size_t size, size_t nmemb, file_t *file); /***< Read bytes from a file */
  61. int miniffs_seek(file_t *file, size_t offset, int whence); /***< Set position in a file */
  62. size_t miniffs_tell(file_t *file); /***< Get current position in a file*/
  63. typedef enum miniffs_error_t
  64. {
  65. MINIFFS_NOERROR = 0,
  66. MINIFFS_INVALID_FS,
  67. MINIFFS_FILE_NOT_FOUND,
  68. //MINIFFS_,
  69. } miniffs_error_t;
  70. miniffs_error_t miniffs_geterror(); /***< Return last error */
  71. #ifdef BUILDING_HOST_TOOLS
  72. /*
  73. * Functions used for offline creation of the filesystem
  74. */
  75. miniffs_t *miniffs_createfs();
  76. int miniffs_addfile(miniffs_t *fs, char *name, char *ext, char *host_path);
  77. int miniffs_delfile(miniffs_t *fs, char *name, char *ext, char *host_path);
  78. int miniffs_writeimage(miniffs_t *fs, char *host_path);
  79. int miniffs_closefs(miniffs_t *fs);
  80. #endif /* BUILDING_HOST_TOOLS */
  81. #endif /* MINIFFS_H */