• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
38 #ifndef __FS_ROMFS_FS_ROMFS_H
39 #define __FS_ROMFS_FS_ROMFS_H
40 
41 /****************************************************************************
42  * Included Files
43  ****************************************************************************/
44 
45 #include <stdint.h>
46 #include <stdbool.h>
47 
48 #include "fs/dirent_fs.h"
49 #include "fs/fs.h"
50 #include "fs/file.h"
51 #include "disk.h"
52 #include "vnode.h"
53 
54 /****************************************************************************
55  * Pre-processor Definitions
56  ****************************************************************************/
57 
58 /* Volume header (multi-byte values are big-endian) */
59 #define ROMFS_MAGIC         0x7275
60 
61 #define ROMFS_VHDR_ROM1FS   0  /*  0-7:  "-rom1fs-" */
62 #define ROMFS_VHDR_SIZE     8  /*  8-11: Number of accessible bytes in this fs. */
63 #define ROMFS_VHDR_CHKSUM  12  /* 12-15: Checksum of the first 512 bytes. */
64 #define ROMFS_VHDR_VOLNAME 16  /* 16-..: Zero terminated volume name, padded to
65                                 *        16 byte boundary. */
66 
67 #define ROMFS_VHDR_MAGIC   "-rom1fs-"
68 
69 /* File header offset (multi-byte values are big-endian) */
70 
71 #define ROMFS_FHDR_NEXT     0  /*  0-3:  Offset of the next file header
72                                 *        (zero if no more files) */
73 #define ROMFS_FHDR_INFO     4  /*  4-7:  Info for directories/hard links/
74                                 *        devices */
75 #define ROMFS_FHDR_SIZE     8  /*  8-11: Size of this file in bytes */
76 #define ROMFS_FHDR_CHKSUM  12  /* 12-15: Checksum covering the meta data,
77                                 *        including the file name, and
78                                 *        padding. */
79 #define ROMFS_FHDR_NAME    16  /* 16-..: Zero terminated volume name, padded
80                                 *        to 16 byte boundary. */
81 
82 /* Bits 0-3 of the rf_next offset provide mode information.  These are the
83  * values specified in  */
84 
85 #define RFNEXT_MODEMASK    7    /* Bits 0-2: Mode; bit 3: Executable */
86 #define RFNEXT_ALLMODEMASK 15   /* Bits 0-3: All mode bits */
87 #define RFNEXT_OFFSETMASK (~15) /* Bits n-3: Offset to next entry */
88 
89 #define RFNEXT_HARDLINK    0    /* rf_info = Link destination file header */
90 #define RFNEXT_DIRECTORY   1    /* rf_info = First file's header */
91 #define RFNEXT_FILE        2    /* rf_info = Unused, must be zero */
92 #define RFNEXT_SOFTLINK    3    /* rf_info = Unused, must be zero */
93 #define RFNEXT_BLOCKDEV    4    /* rf_info = 16/16 bits major/minor number */
94 #define RFNEXT_CHARDEV     5    /* rf_info = 16/16 bits major/minor number */
95 #define RFNEXT_SOCKET      6    /* rf_info = Unused, must be zero */
96 #define RFNEXT_FIFO        7    /* rf_info = Unused, must be zero */
97 #define RFNEXT_EXEC        8    /* Modifier of RFNEXT_DIRECTORY and RFNEXT_FILE */
98 
99 #define IS_MODE(rfn,mode)  ((((uint32_t)(rfn))&RFNEXT_MODEMASK)==(mode))
100 #define IS_HARDLINK(rfn)   IS_MODE(rfn,RFNEXT_HARDLINK)
101 #define IS_DIRECTORY(rfn)  IS_MODE(rfn,RFNEXT_DIRECTORY)
102 #define IS_FILE(rfn)       IS_MODE(rfn,RFNEXT_FILE)
103 #define IS_EXECUTABLE(rfn) (((rfn) & RFNEXT_EXEC) != 0)
104 
105 /* RFNEXT_SOFTLINK, RFNEXT_BLOCKDEV, RFNEXT_CHARDEV, RFNEXT_SOCKET, and
106  * RFNEXT_FIFO are not presently supported.
107  */
108 
109 /* Alignment macros */
110 
111 #define ROMFS_ALIGNMENT       16
112 #define ROMFS_MAXPADDING      (ROMFS_ALIGNMENT-1)
113 #define ROMFS_ALIGNMASK       (~ROMFS_MAXPADDING)
114 #define ROMFS_ALIGNUP(addr)   ((((uint32_t)(addr))+ROMFS_MAXPADDING)&ROMFS_ALIGNMASK)
115 #define ROMFS_ALIGNDOWN(addr) (((uint32_t)(addr))&ROMFS_ALIGNMASK)
116 
117 /* Offset and sector conversions */
118 
119 #define SEC_NDXMASK(r)       ((r)->rm_hwsectorsize - 1)
120 #define SEC_NSECTORS(r,o)    ((o) / (r)->rm_hwsectorsize)
121 #define SEC_ALIGN(r,o)       ((o) & ~SEC_NDXMASK(r))
122 
123 /* Maximum numbr of links that will be followed before we decide that there
124  * is a problem.
125  */
126 
127 #define ROMF_MAX_LINKS 64
128 
129 /****************************************************************************
130  * Public Types
131  ****************************************************************************/
132 
133 /* This structure represents the overall mountpoint state.  An instance of this
134  * structure is retained as inode private data on each mountpoint that is
135  * mounted with a fat32 filesystem.
136  */
137 
138 struct romfs_file_s;
139 struct romfs_mountpt_s
140 {
141   struct inode        *rm_blkdriver; /* The block driver inode that hosts the FAT32 fs */
142   struct romfs_file_s *rm_head;      /* A list to all files opened on this mountpoint */
143 
144   bool     rm_mounted;              /* true: The file system is ready */
145   uint16_t rm_hwsectorsize;         /* HW: Sector size reported by block driver*/
146   sem_t    rm_sem;                  /* Used to assume thread-safe access */
147   uint32_t rm_rootoffset;           /* Saved offset to the first root directory entry */
148   uint32_t rm_hwnsectors;           /* HW: The number of sectors reported by the hardware */
149   uint32_t rm_volsize;              /* Size of the ROMFS volume */
150   uint32_t rm_cachesector;          /* Current sector in the rm_buffer */
151   uint8_t *rm_xipbase;              /* Base address of directly accessible media */
152   uint8_t *rm_buffer;               /* Device sector buffer, allocated if rm_xipbase==0 */
153 };
154 
155 /* This structure represents on open file under the mountpoint.  An instance
156  * of this structure is retained as struct file specific information on each
157  * opened file.
158  */
159 
160 struct romfs_file_s
161 {
162   struct romfs_file_s *rf_next; /* Retained in a singly linked list */
163   uint32_t rf_startoffset;          /* Offset to the start of the file data */
164   uint32_t rf_size;                 /* Size of the file in bytes */
165   uint32_t rf_cachesector;          /* Current sector in the rf_buffer */
166   uint8_t *rf_buffer;               /* File sector buffer, allocated if rm_xipbase==0 */
167   uint8_t rf_type;                  /* File type (for fstat()) */
168 };
169 
170 /* This structure is used internally for describing the result of
171  * walking a path
172  */
173 
174 struct fs_romfsdir_s
175 {
176   off_t fr_firstoffset;
177   off_t fr_curroffset;
178 };
179 struct romfs_dirinfo_s
180 {
181   /* These values describe the directory containing the terminal
182    * path component (of the terminal component itself if it is
183    * a directory.
184    */
185 
186   struct fs_romfsdir_s rd_dir;    /* Describes directory. */
187 
188   /* Values from the ROMFS file entry */
189 
190   uint32_t rd_next;               /* Offset of the next file header+flags */
191   uint32_t rd_size;               /* Size (if file) */
192 };
193 
194 /****************************************************************************
195  * Public Data
196  ****************************************************************************/
197 
198 #undef EXTERN
199 #if defined(__cplusplus)
200 #define EXTERN extern "C"
201 extern "C"
202 {
203 #else
204 #define EXTERN extern
205 #endif
206 
207 /****************************************************************************
208  * Public Function Prototypes
209  ****************************************************************************/
210 
211 void romfs_semtake(struct romfs_mountpt_s *rm);
212 void romfs_semgive(struct romfs_mountpt_s *rm);
213 int  romfs_hwread(struct romfs_mountpt_s *rm, uint8_t *buffer,
214        uint32_t sector, unsigned int nsectors);
215 int  romfs_filecacheread(struct romfs_mountpt_s *rm,
216        struct romfs_file_s *rf, uint32_t sector);
217 int  romfs_hwconfigure(struct romfs_mountpt_s *rm);
218 int  romfs_fsconfigure(struct romfs_mountpt_s *rm);
219 int  romfs_checkmount(struct romfs_mountpt_s *rm);
220 int  romfs_finddirentry(struct romfs_mountpt_s *rm,
221        struct romfs_dirinfo_s *dirinfo,
222        const char *path);
223 int  romfs_parsedirentry(struct romfs_mountpt_s *rm,
224        uint32_t offset, uint32_t *poffset, uint32_t *pnext,
225        uint32_t *pinfo, uint32_t *psize);
226 int  romfs_parsefilename(struct romfs_mountpt_s *rm, uint32_t offset,
227        char *pname);
228 int  romfs_datastart(struct romfs_mountpt_s *rm, uint32_t offset,
229        uint32_t *start);
230 int romfs_searchdir(struct romfs_mountpt_s *rm,
231        const char *entryname, int entrylen, uint32_t firstoffset,
232        struct romfs_dirinfo_s *dirinfo);
233 
234 #undef EXTERN
235 #if defined(__cplusplus)
236 }
237 #endif
238 
239 #endif /* __FS_ROMFS_FS_ROMFS_H */
240