GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / cfe / cfe / main / cfe_memfs.c
blob80cde95690b8bcfca59a268807a4529f5a8c4772
1 /* *********************************************************************
2 * Broadcom Common Firmware Environment (CFE)
4 * "Memory" file system File: cfe_memfs.c
6 * This filesystem driver lets you data from memory by treating
7 * it as a filesystem. Note that, there is no concept of filename..
8 * directly memory location to read from. Also, no writing is permitted for now
10 * Author: Abhay Bhorkar (abhorkar@broadcom.com)
12 * $Id: cfe_memfs.c 241205 2011-02-17 21:57:41Z $
14 * ********************************************************************
16 * Copyright 2005
17 * Broadcom Corporation. All rights reserved.
19 * This software is furnished under license and may be used and
20 * copied only in accordance with the following terms and
21 * conditions. Subject to these conditions, you may download,
22 * copy, install, use, modify and distribute modified or unmodified
23 * copies of this software in source and/or binary form. No title
24 * or ownership is transferred hereby.
26 * 1) Any source code used, modified or distributed must reproduce
27 * and retain this copyright notice and list of conditions
28 * as they appear in the source file.
30 * 2) No right is granted to use any trade name, trademark, or
31 * logo of Broadcom Corporation. The "Broadcom Corporation"
32 * name may not be used to endorse or promote products derived
33 * from this software without the prior written permission of
34 * Broadcom Corporation.
36 * 3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR
37 * IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED
38 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
39 * PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT
40 * SHALL BROADCOM BE LIABLE FOR ANY DAMAGES WHATSOEVER, AND IN
41 * PARTICULAR, BROADCOM SHALL NOT BE LIABLE FOR DIRECT, INDIRECT,
42 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
43 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
44 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
45 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
46 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
47 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE), EVEN IF ADVISED OF
48 * THE POSSIBILITY OF SUCH DAMAGE.
49 * ********************************************************************
52 #include "lib_types.h"
53 #include "lib_string.h"
54 #include "lib_queue.h"
55 #include "lib_malloc.h"
57 #include "cfe_error.h"
58 #include "cfe_fileops.h"
59 #include "cfe_iocb.h"
60 #include "cfe_devfuncs.h"
61 #include "cfe_loader.h"
63 /* *********************************************************************
64 * Memory context
65 * ********************************************************************
69 * File system context - describes overall file system info,
70 * such as the handle to the underlying device.
73 typedef struct memory_fsctx_s {
74 int memory_tmp; /* context not really needed */
75 } memory_fsctx_t;
78 * File context - describes an open file on the file system.
80 typedef struct memory_file_s {
81 memory_fsctx_t *memory_fsctx;
82 uint8_t *memory_bptr;
83 int memory_blen;
84 int memory_offset;
85 long memory_start;
86 } memory_file_t;
88 /* *********************************************************************
89 * Prototypes
90 * ********************************************************************
93 static int memory_fileop_init(void **fsctx, void *devicename);
94 static int memory_fileop_open(void **ref, void *fsctx, char *filename, int mode);
95 static int memory_fileop_read(void *ref, uint8_t *buf, int len);
96 static int memory_fileop_write(void *ref, uint8_t *buf, int len);
97 static int memory_fileop_seek(void *ref, int offset, int how);
98 static void memory_fileop_close(void *ref);
99 static void memory_fileop_uninit(void *fsctx);
101 /* *********************************************************************
102 * RAW fileio dispatch table
103 * ********************************************************************
106 const fileio_dispatch_t memory_fileops = {
107 "memory",
108 LOADFLG_SPECADDR,
109 memory_fileop_init,
110 memory_fileop_open,
111 memory_fileop_read,
112 memory_fileop_write,
113 memory_fileop_seek,
114 memory_fileop_close,
115 memory_fileop_uninit
118 static int memory_fileop_init(void **newfsctx, void *dev)
120 memory_fsctx_t *fsctx;
122 *newfsctx = NULL;
124 fsctx = KMALLOC(sizeof(memory_fsctx_t), 0);
125 if (!fsctx) {
126 return CFE_ERR_NOMEM;
129 fsctx->memory_tmp = 0;
130 *newfsctx = fsctx;
132 return 0;
135 /* filename describes the location in memory in string */
136 static int memory_fileop_open(void **ref, void *fsctx_arg, char *filename, int mode)
138 memory_fsctx_t *fsctx;
139 memory_file_t *file;
141 if (mode != FILE_MODE_READ)
142 return CFE_ERR_UNSUPPORTED;
144 fsctx = (memory_fsctx_t *) fsctx_arg;
146 file = KMALLOC(sizeof(memory_file_t), 0);
147 if (!file) {
148 return CFE_ERR_NOMEM;
151 if (filename[0] == ':')
152 filename++; /* Skip over colon */
154 file->memory_start = xtoi(filename);
156 file->memory_offset = 0;
158 *ref = file;
159 return 0;
162 static int memory_fileop_read(void *ref, uint8_t *buf, int len)
164 memory_file_t *file = (memory_file_t *) ref;
166 memcpy(buf, (void *)(file->memory_start + file->memory_offset), len);
168 file->memory_offset += len;
170 return len; /* Any error becomes "EOF" */
173 static int memory_fileop_write(void *ref, uint8_t *buf, int len)
175 return CFE_ERR_UNSUPPORTED;
178 static int memory_fileop_seek(void *ref, int offset, int how)
180 memory_file_t *file = (memory_file_t *) ref;
181 int newoffset;
183 switch (how) {
184 case FILE_SEEK_BEGINNING:
185 newoffset = offset;
186 break;
187 case FILE_SEEK_CURRENT:
188 newoffset = file->memory_offset + offset;
189 break;
190 default:
191 newoffset = offset;
192 break;
195 file->memory_offset = newoffset;
197 return file->memory_offset;
200 static void memory_fileop_close(void *ref)
202 memory_file_t *file = (memory_file_t *) ref;
204 KFREE(file);
207 static void memory_fileop_uninit(void *fsctx_arg)
209 memory_fsctx_t *fsctx = (memory_fsctx_t *) fsctx_arg;
211 KFREE(fsctx);