fs_romfs.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /****************************************************************************
  2. * fs/romfs/fs_romfs.h
  3. *
  4. * Copyright (C) 2008-2009, 2011 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * References: Linux/Documentation/filesystems/romfs.txt
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. * 3. Neither the name NuttX nor the names of its contributors may be
  20. * used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  26. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  27. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  28. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  29. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  30. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  31. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  33. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. * POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. ****************************************************************************/
  37. #ifndef __FS_ROMFS_FS_ROMFS_H
  38. #define __FS_ROMFS_FS_ROMFS_H
  39. /****************************************************************************
  40. * Included Files
  41. ****************************************************************************/
  42. #include <nuttx/config.h>
  43. #include <stdint.h>
  44. #include <stdbool.h>
  45. #include <nuttx/fs/dirent.h>
  46. #include "inode/inode.h"
  47. /****************************************************************************
  48. * Pre-processor Definitions
  49. ****************************************************************************/
  50. /* Volume header (multi-byte values are big-endian) */
  51. #define ROMFS_VHDR_ROM1FS 0 /* 0-7: "-rom1fs-" */
  52. #define ROMFS_VHDR_SIZE 8 /* 8-11: Number of accessible bytes in this fs. */
  53. #define ROMFS_VHDR_CHKSUM 12 /* 12-15: Checksum of the first 512 bytes. */
  54. #define ROMFS_VHDR_VOLNAME 16 /* 16-..: Zero terminated volume name, padded to
  55. * 16 byte boundary. */
  56. #define ROMFS_VHDR_MAGIC "-rom1fs-"
  57. /* File header offset (multi-byte values are big-endian) */
  58. #define ROMFS_FHDR_NEXT 0 /* 0-3: Offset of the next file header
  59. * (zero if no more files) */
  60. #define ROMFS_FHDR_INFO 4 /* 4-7: Info for directories/hard links/
  61. * devices */
  62. #define ROMFS_FHDR_SIZE 8 /* 8-11: Size of this file in bytes */
  63. #define ROMFS_FHDR_CHKSUM 12 /* 12-15: Checksum covering the meta data,
  64. * including the file name, and
  65. * padding. */
  66. #define ROMFS_FHDR_NAME 16 /* 16-..: Zero terminated volume name, padded
  67. * to 16 byte boundary. */
  68. /* Bits 0-3 of the rf_next offset provide mode information. These are the
  69. * values specified in */
  70. #define RFNEXT_MODEMASK 7 /* Bits 0-2: Mode; bit 3: Executable */
  71. #define RFNEXT_ALLMODEMASK 15 /* Bits 0-3: All mode bits */
  72. #define RFNEXT_OFFSETMASK (~15) /* Bits n-3: Offset to next entry */
  73. #define RFNEXT_HARDLINK 0 /* rf_info = Link destination file header */
  74. #define RFNEXT_DIRECTORY 1 /* rf_info = First file's header */
  75. #define RFNEXT_FILE 2 /* rf_info = Unused, must be zero */
  76. #define RFNEXT_SOFTLINK 3 /* rf_info = Unused, must be zero */
  77. #define RFNEXT_BLOCKDEV 4 /* rf_info = 16/16 bits major/minor number */
  78. #define RFNEXT_CHARDEV 5 /* rf_info = 16/16 bits major/minor number */
  79. #define RFNEXT_SOCKET 6 /* rf_info = Unused, must be zero */
  80. #define RFNEXT_FIFO 7 /* rf_info = Unused, must be zero */
  81. #define RFNEXT_EXEC 8 /* Modifier of RFNEXT_DIRECTORY and RFNEXT_FILE */
  82. #define IS_MODE(rfn,mode) ((((uint32_t)(rfn))&RFNEXT_MODEMASK)==(mode))
  83. #define IS_HARDLINK(rfn) IS_MODE(rfn,RFNEXT_HARDLINK)
  84. #define IS_DIRECTORY(rfn) IS_MODE(rfn,RFNEXT_DIRECTORY)
  85. #define IS_FILE(rfn) IS_MODE(rfn,RFNEXT_FILE)
  86. #define IS_EXECUTABLE(rfn) (((rfn) & RFNEXT_EXEC) != 0)
  87. /* RFNEXT_SOFTLINK, RFNEXT_BLOCKDEV, RFNEXT_CHARDEV, RFNEXT_SOCKET, and
  88. * RFNEXT_FIFO are not presently supported in NuttX.
  89. */
  90. /* Alignment macros */
  91. #define ROMFS_ALIGNMENT 16
  92. #define ROMFS_MAXPADDING (ROMFS_ALIGNMENT-1)
  93. #define ROMFS_ALIGNMASK (~ROMFS_MAXPADDING)
  94. #define ROMFS_ALIGNUP(addr) ((((uint32_t)(addr))+ROMFS_MAXPADDING)&ROMFS_ALIGNMASK)
  95. #define ROMFS_ALIGNDOWN(addr) (((uint32_t)(addr))&ROMFS_ALIGNMASK)
  96. /* Offset and sector conversions */
  97. #define SEC_NDXMASK(r) ((r)->rm_hwsectorsize - 1)
  98. #define SEC_NSECTORS(r,o) ((o) / (r)->rm_hwsectorsize)
  99. #define SEC_ALIGN(r,o) ((o) & ~SEC_NDXMASK(r))
  100. /* Maximum numbr of links that will be followed before we decide that there
  101. * is a problem.
  102. */
  103. #define ROMF_MAX_LINKS 64
  104. /****************************************************************************
  105. * Public Types
  106. ****************************************************************************/
  107. /* This structure represents the overall mountpoint state. An instance of this
  108. * structure is retained as inode private data on each mountpoint that is
  109. * mounted with a fat32 filesystem.
  110. */
  111. struct romfs_file_s;
  112. struct romfs_mountpt_s
  113. {
  114. struct inode *rm_blkdriver; /* The block driver inode that hosts the FAT32 fs */
  115. struct romfs_file_s *rm_head; /* A list to all files opened on this mountpoint */
  116. bool rm_mounted; /* true: The file system is ready */
  117. uint16_t rm_hwsectorsize; /* HW: Sector size reported by block driver*/
  118. sem_t rm_sem; /* Used to assume thread-safe access */
  119. uint32_t rm_rootoffset; /* Saved offset to the first root directory entry */
  120. uint32_t rm_hwnsectors; /* HW: The number of sectors reported by the hardware */
  121. uint32_t rm_volsize; /* Size of the ROMFS volume */
  122. uint32_t rm_cachesector; /* Current sector in the rm_buffer */
  123. uint8_t *rm_xipbase; /* Base address of directly accessible media */
  124. uint8_t *rm_buffer; /* Device sector buffer, allocated if rm_xipbase==0 */
  125. };
  126. /* This structure represents on open file under the mountpoint. An instance
  127. * of this structure is retained as struct file specific information on each
  128. * opened file.
  129. */
  130. struct romfs_file_s
  131. {
  132. FAR struct romfs_file_s *rf_next; /* Retained in a singly linked list */
  133. uint32_t rf_startoffset; /* Offset to the start of the file data */
  134. uint32_t rf_size; /* Size of the file in bytes */
  135. uint32_t rf_cachesector; /* Current sector in the rf_buffer */
  136. uint8_t *rf_buffer; /* File sector buffer, allocated if rm_xipbase==0 */
  137. uint8_t rf_type; /* File type (for fstat()) */
  138. };
  139. /* This structure is used internally for describing the result of
  140. * walking a path
  141. */
  142. struct romfs_dirinfo_s
  143. {
  144. /* These values describe the directory containing the terminal
  145. * path component (of the terminal component itself if it is
  146. * a directory.
  147. */
  148. struct fs_romfsdir_s rd_dir; /* Describes directory. */
  149. /* Values from the ROMFS file entry */
  150. uint32_t rd_next; /* Offset of the next file header+flags */
  151. uint32_t rd_size; /* Size (if file) */
  152. };
  153. /****************************************************************************
  154. * Public Data
  155. ****************************************************************************/
  156. #undef EXTERN
  157. #if defined(__cplusplus)
  158. #define EXTERN extern "C"
  159. extern "C"
  160. {
  161. #else
  162. #define EXTERN extern
  163. #endif
  164. /****************************************************************************
  165. * Public Function Prototypes
  166. ****************************************************************************/
  167. void romfs_semtake(FAR struct romfs_mountpt_s *rm);
  168. void romfs_semgive(FAR struct romfs_mountpt_s *rm);
  169. int romfs_hwread(FAR struct romfs_mountpt_s *rm, FAR uint8_t *buffer,
  170. uint32_t sector, unsigned int nsectors);
  171. int romfs_filecacheread(FAR struct romfs_mountpt_s *rm,
  172. FAR struct romfs_file_s *rf, uint32_t sector);
  173. int romfs_hwconfigure(FAR struct romfs_mountpt_s *rm);
  174. int romfs_fsconfigure(FAR struct romfs_mountpt_s *rm);
  175. int romfs_fileconfigure(FAR struct romfs_mountpt_s *rm,
  176. FAR struct romfs_file_s *rf);
  177. int romfs_checkmount(FAR struct romfs_mountpt_s *rm);
  178. int romfs_finddirentry(FAR struct romfs_mountpt_s *rm,
  179. FAR struct romfs_dirinfo_s *dirinfo,
  180. FAR const char *path);
  181. int romfs_parsedirentry(FAR struct romfs_mountpt_s *rm,
  182. uint32_t offset, FAR uint32_t *poffset, FAR uint32_t *pnext,
  183. FAR uint32_t *pinfo, FAR uint32_t *psize);
  184. int romfs_parsefilename(FAR struct romfs_mountpt_s *rm, uint32_t offset,
  185. FAR char *pname);
  186. int romfs_datastart(FAR struct romfs_mountpt_s *rm, uint32_t offset,
  187. FAR uint32_t *start);
  188. #undef EXTERN
  189. #if defined(__cplusplus)
  190. }
  191. #endif
  192. #endif /* __FS_ROMFS_FS_ROMFS_H */