BCM WL 6.30.102.9 (r366174)
[tomato.git] / release / src-rt / cfe / cfe / main / cfe_filesys.c
blob7e9afdbfb2c5b2df292014fbf29762ed8c825068
1 /* *********************************************************************
2 * Broadcom Common Firmware Environment (CFE)
3 *
4 * Filesystem manager File: cfe_filesys.c
5 *
6 * This module maintains the list of available file systems.
7 *
8 * Author: Mitch Lichtenberg (mpl@broadcom.com)
9 *
10 *********************************************************************
12 * Copyright 2000,2001,2002,2003
13 * Broadcom Corporation. All rights reserved.
15 * This software is furnished under license and may be used and
16 * copied only in accordance with the following terms and
17 * conditions. Subject to these conditions, you may download,
18 * copy, install, use, modify and distribute modified or unmodified
19 * copies of this software in source and/or binary form. No title
20 * or ownership is transferred hereby.
22 * 1) Any source code used, modified or distributed must reproduce
23 * and retain this copyright notice and list of conditions
24 * as they appear in the source file.
26 * 2) No right is granted to use any trade name, trademark, or
27 * logo of Broadcom Corporation. The "Broadcom Corporation"
28 * name may not be used to endorse or promote products derived
29 * from this software without the prior written permission of
30 * Broadcom Corporation.
32 * 3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR
33 * IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED
34 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
35 * PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT
36 * SHALL BROADCOM BE LIABLE FOR ANY DAMAGES WHATSOEVER, AND IN
37 * PARTICULAR, BROADCOM SHALL NOT BE LIABLE FOR DIRECT, INDIRECT,
38 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
39 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
40 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
41 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
42 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
43 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE), EVEN IF ADVISED OF
44 * THE POSSIBILITY OF SUCH DAMAGE.
45 ********************************************************************* */
47 #include "lib_types.h"
48 #include "lib_string.h"
49 #include "lib_malloc.h"
51 #include "cfe_error.h"
52 #include "cfe_fileops.h"
54 #include "cfe.h"
56 #include "bsp_config.h"
58 /* *********************************************************************
59 * Externs
60 ********************************************************************* */
62 extern const fileio_dispatch_t raw_fileops;
63 #if CFG_NETWORK
64 extern const fileio_dispatch_t tftp_fileops;
65 #if CFG_TCP && CFG_HTTPFS
66 extern const fileio_dispatch_t http_fileops;
67 #endif
68 #endif
69 #if CFG_FATFS
70 extern const fileio_dispatch_t fatfs_fileops;
71 extern const fileio_dispatch_t pfatfs_fileops;
72 #endif
73 #if CFG_ZLIB
74 extern const fileio_dispatch_t zlibfs_fileops;
75 #endif
76 #if CFG_LZMA
77 extern const fileio_dispatch_t lzmafs_fileops;
78 #endif
79 extern const fileio_dispatch_t memory_fileops;
80 /* *********************************************************************
81 * File system list
82 ********************************************************************* */
84 static const fileio_dispatch_t * const cfe_filesystems[] = {
85 &raw_fileops,
86 #if CFG_NETWORK
87 &tftp_fileops,
88 #if (CFG_TCP) & (CFG_HTTPFS)
89 &http_fileops,
90 #endif
91 #endif
92 #if CFG_FATFS
93 &fatfs_fileops,
94 &pfatfs_fileops,
95 #endif
96 #if CFG_ZLIB
97 &zlibfs_fileops,
98 #endif
99 #if CFG_LZMA
100 &lzmafs_fileops,
101 #endif
102 &memory_fileops,
103 NULL
106 /* *********************************************************************
107 * cfe_findfilesys(name)
109 * Locate the dispatch record for a particular file system.
111 * Input parameters:
112 * name - name of filesys to locate
114 * Return value:
115 * pointer to dispatch table or NULL if not found
116 ********************************************************************* */
118 const fileio_dispatch_t *cfe_findfilesys(const char *name)
120 const fileio_dispatch_t * const *disp;
122 disp = cfe_filesystems;
124 while (*disp) {
125 if (strcmp((*disp)->method,name) == 0) return *disp;
126 disp++;
129 return NULL;
132 /* *********************************************************************
133 * fs_init(name,fsctx,device)
135 * Initialize a filesystem context
137 * Input parameters:
138 * name - name of file system
139 * fsctx - returns a filesystem context
140 * device - device name or other info
142 * Return value:
143 * 0 if ok
144 * else error code
145 ********************************************************************* */
146 int fs_init(char *fsname,fileio_ctx_t **fsctx,void *device)
148 fileio_ctx_t *ctx;
149 int res;
150 const fileio_dispatch_t *ops;
152 ops = cfe_findfilesys((const char *)fsname);
153 if (!ops) return CFE_ERR_FSNOTAVAIL;
155 ctx = (fileio_ctx_t *) KMALLOC(sizeof(fileio_ctx_t),0);
156 if (!ctx) return CFE_ERR_NOMEM;
158 ctx->ops = ops;
159 res = BDINIT(ops,&(ctx->fsctx),device);
161 if (res != 0) {
162 KFREE(ctx);
165 *fsctx = ctx;
167 return res;
171 /* *********************************************************************
172 * fs_hook(fsctx,name)
174 * "Hook" a filesystem to attach a filter, like a decompression
175 * or decryption engine.
177 * Input parameters:
178 * fsctx - result from a previous fs_init
179 * name - name of fs to hook onto the beginning
181 * Return value:
182 * 0 if ok
183 * else error
184 ********************************************************************* */
186 int fs_hook(fileio_ctx_t *fsctx,char *fsname)
188 void *hookfsctx;
189 const fileio_dispatch_t *ops;
190 int res;
193 * Find the hook filesystem (well, actually a filter)
196 ops = cfe_findfilesys((const char *)fsname);
197 if (!ops) return CFE_ERR_FSNOTAVAIL;
200 * initialize our hook file filter.
203 res = BDINIT(ops,&hookfsctx,fsctx);
204 if (res != 0) return res;
207 * Now replace dispatch table for current filesystem
208 * with the hook's dispatch table. When fs_read is called,
209 * we'll go to the hook, and the hook will call the original.
211 * When fs_uninit is called, the hook will call the original's
212 * uninit routine.
215 fsctx->ops = ops;
216 fsctx->fsctx = hookfsctx;
218 return 0;
222 /* *********************************************************************
223 * fs_uninit(fsctx)
225 * Uninitialize a file system context.
227 * Input parameters:
228 * fsctx - filesystem context to remove (from fs_init)
230 * Return value:
231 * 0 if ok
232 * else error
233 ********************************************************************* */
234 int fs_uninit(fileio_ctx_t *fsctx)
236 BDUNINIT(fsctx->ops,fsctx->fsctx);
238 KFREE(fsctx);
240 return 0;
244 /* *********************************************************************
245 * fs_open(fsctx,ref,filename,mode)
247 * Open a file on the file system
249 * Input parameters:
250 * fsctx - filesystem context (from fs_init)
251 * ref - returns file handle
252 * filename - name of file to open
253 * mode - file open mode
255 * Return value:
256 * 0 if ok
257 * else error code
258 ********************************************************************* */
259 int fs_open(fileio_ctx_t *fsctx,void **ref,char *filename,int mode)
261 return BDOPEN2(fsctx->ops,ref,fsctx->fsctx,filename,mode);
264 /* *********************************************************************
265 * fs_close(fsctx,ref)
267 * Close a file on the file system
269 * Input parameters:
270 * fsctx - filesystem context (from fs_init)
271 * ref - file handle (from fs_open)
273 * Return value:
274 * 0 if ok
275 * else error code
276 ********************************************************************* */
277 int fs_close(fileio_ctx_t *fsctx,void *ref)
279 BDCLOSE(fsctx->ops,ref);
281 return 0;
285 /* *********************************************************************
286 * fs_read(fsctx,ref,buffer,len)
288 * Read data from the device.
290 * Input parameters:
291 * fsctx - filesystem context (from fs_init)
292 * ref - file handle (from fs_open)
293 * buffer - buffer pointer
294 * len - length
296 * Return value:
297 * number of bytes read
298 * 0=eof
299 * <0 = error
300 ********************************************************************* */
302 int fs_read(fileio_ctx_t *fsctx,void *ref,uint8_t *buffer,int len)
304 return BDREAD(fsctx->ops,ref,buffer,len);
307 /* *********************************************************************
308 * fs_write(fsctx,ref,buffer,len)
310 * write data from the device.
312 * Input parameters:
313 * fsctx - filesystem context (from fs_init)
314 * ref - file handle (from fs_open)
315 * buffer - buffer pointer
316 * len - length
318 * Return value:
319 * number of bytes written
320 * 0=eof
321 * <0 = error
322 ********************************************************************* */
324 int fs_write(fileio_ctx_t *fsctx,void *ref,uint8_t *buffer,int len)
326 return BDWRITE(fsctx->ops,ref,buffer,len);
329 /* *********************************************************************
330 * fs_seek(fsctx,ref,offset,how)
332 * move file pointer on the device
334 * Input parameters:
335 * fsctx - filesystem context (from fs_init)
336 * ref - file handle (from fs_open)
337 * offset - distance to move
338 * how - origin (FILE_SEEK_xxx)
340 * Return value:
341 * new offset
342 * <0 = error
343 ********************************************************************* */
345 int fs_seek(fileio_ctx_t *fsctx,void *ref,int offset,int how)
347 return BDSEEK(fsctx->ops,ref,offset,how);