1 /* *********************************************************************
2 * Broadcom Common Firmware Environment (CFE)
4 * Filesystem manager File: cfe_filesys.c
6 * This module maintains the list of available file systems.
8 * Author: Mitch Lichtenberg (mpl@broadcom.com)
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"
56 #include "bsp_config.h"
58 /* *********************************************************************
60 ********************************************************************* */
62 extern const fileio_dispatch_t raw_fileops
;
64 extern const fileio_dispatch_t tftp_fileops
;
65 #if CFG_TCP && CFG_HTTPFS
66 extern const fileio_dispatch_t http_fileops
;
70 extern const fileio_dispatch_t fatfs_fileops
;
71 extern const fileio_dispatch_t pfatfs_fileops
;
74 extern const fileio_dispatch_t zlibfs_fileops
;
77 extern const fileio_dispatch_t lzmafs_fileops
;
79 extern const fileio_dispatch_t memory_fileops
;
80 /* *********************************************************************
82 ********************************************************************* */
84 static const fileio_dispatch_t
* const cfe_filesystems
[] = {
88 #if (CFG_TCP) & (CFG_HTTPFS)
106 /* *********************************************************************
107 * cfe_findfilesys(name)
109 * Locate the dispatch record for a particular file system.
112 * name - name of filesys to locate
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
;
125 if (strcmp((*disp
)->method
,name
) == 0) return *disp
;
132 /* *********************************************************************
133 * fs_init(name,fsctx,device)
135 * Initialize a filesystem context
138 * name - name of file system
139 * fsctx - returns a filesystem context
140 * device - device name or other info
145 ********************************************************************* */
146 int fs_init(char *fsname
,fileio_ctx_t
**fsctx
,void *device
)
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
;
159 res
= BDINIT(ops
,&(ctx
->fsctx
),device
);
171 /* *********************************************************************
172 * fs_hook(fsctx,name)
174 * "Hook" a filesystem to attach a filter, like a decompression
175 * or decryption engine.
178 * fsctx - result from a previous fs_init
179 * name - name of fs to hook onto the beginning
184 ********************************************************************* */
186 int fs_hook(fileio_ctx_t
*fsctx
,char *fsname
)
189 const fileio_dispatch_t
*ops
;
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
216 fsctx
->fsctx
= hookfsctx
;
222 /* *********************************************************************
225 * Uninitialize a file system context.
228 * fsctx - filesystem context to remove (from fs_init)
233 ********************************************************************* */
234 int fs_uninit(fileio_ctx_t
*fsctx
)
236 BDUNINIT(fsctx
->ops
,fsctx
->fsctx
);
244 /* *********************************************************************
245 * fs_open(fsctx,ref,filename,mode)
247 * Open a file on the file system
250 * fsctx - filesystem context (from fs_init)
251 * ref - returns file handle
252 * filename - name of file to open
253 * mode - file open mode
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
270 * fsctx - filesystem context (from fs_init)
271 * ref - file handle (from fs_open)
276 ********************************************************************* */
277 int fs_close(fileio_ctx_t
*fsctx
,void *ref
)
279 BDCLOSE(fsctx
->ops
,ref
);
285 /* *********************************************************************
286 * fs_read(fsctx,ref,buffer,len)
288 * Read data from the device.
291 * fsctx - filesystem context (from fs_init)
292 * ref - file handle (from fs_open)
293 * buffer - buffer pointer
297 * number of bytes read
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.
313 * fsctx - filesystem context (from fs_init)
314 * ref - file handle (from fs_open)
315 * buffer - buffer pointer
319 * number of bytes written
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
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)
343 ********************************************************************* */
345 int fs_seek(fileio_ctx_t
*fsctx
,void *ref
,int offset
,int how
)
347 return BDSEEK(fsctx
->ops
,ref
,offset
,how
);