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