uromfs.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2001-2003 by egnite Software GmbH. All rights reserved.
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions
00006  * are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the copyright holders nor the names of
00014  *    contributors may be used to endorse or promote products derived
00015  *    from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY EGNITE SOFTWARE GMBH AND CONTRIBUTORS
00018  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00019  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00020  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EGNITE
00021  * SOFTWARE GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00022  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00023  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00024  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00025  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00026  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00027  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00028  * SUCH DAMAGE.
00029  *
00030  * For additional information see http://www.ethernut.de/
00031  *
00032  */
00033 
00034 /*
00035  * $Log: uromfs.c,v $
00036  * Revision 1.7  2006/03/02 19:57:34  haraldkipp
00037  * ICCARM insists on a (void *) typecast for the second parameter of memcpy().
00038  *
00039  * Revision 1.6  2006/01/05 16:45:20  haraldkipp
00040  * Dynamic NUTFILE allocation for detached block device.
00041  *
00042  * Revision 1.5  2005/08/05 11:29:07  olereinhardt
00043  * Added IOCTL function with support for seek
00044  *
00045  * Revision 1.4  2004/03/18 11:37:06  haraldkipp
00046  * Deprecated functions removed
00047  *
00048  * Revision 1.3  2004/03/16 16:48:27  haraldkipp
00049  * Added Jan Dubiec's H8/300 port.
00050  *
00051  * Revision 1.2  2003/07/20 19:27:59  haraldkipp
00052  * Patch by Alessandro Zummo. Moves the urom filesystem filenames to
00053  * AVR's flash memory.
00054  *
00055  * Revision 1.1.1.1  2003/05/09 14:41:02  haraldkipp
00056  * Initial using 3.2.1
00057  *
00058  * Revision 1.12  2003/04/21 16:58:20  harald
00059  * Make use of predefined eof
00060  *
00061  * Revision 1.11  2003/02/04 17:57:14  harald
00062  * Version 3 released
00063  *
00064  * Revision 1.10  2002/11/02 15:16:09  harald
00065  * Compiler warning avoided
00066  *
00067  * Revision 1.9  2002/06/26 17:29:13  harald
00068  * First pre-release with 2.4 stack
00069  *
00070  */
00071 
00072 #include <string.h>
00073 #include <errno.h>
00074 #include <stdio.h>
00075 
00076 #include <sys/heap.h>
00077 #include <sys/file.h>
00078 #include <sys/device.h>
00079 
00080 #include <fs/fs.h>
00081 #include <dev/urom.h>
00082 #include <fs/uromfs.h>
00083 
00084 /*
00085 static int UromRead(NUTFILE * fp, void *buffer, int size);
00086 static int UromWrite(NUTFILE * fp, CONST void *buffer, int len);
00087 #ifdef __HARVARD_ARCH__
00088 static int UromWrite_P(NUTFILE * fp, PGM_P buffer, int len);
00089 #endif
00090 static NUTFILE *UromOpen(NUTDEVICE * dev, CONST char *name, int mode,
00091                          int acc);
00092 static int UromClose(NUTFILE * fp);
00093 static long UromSize(NUTFILE * fp);
00094 */
00099 
00100 static int UromSeek(NUTFILE * fp, long *pos, int whence)
00101 {
00102     ROMFILE *romf = fp->nf_fcb;
00103     ROMENTRY *rome = romf->romf_entry;
00104 
00105     int rc = 0;
00106     long npos = *pos;
00107 
00108     switch (whence) {
00109     case SEEK_CUR:
00110         npos += romf->romf_pos;
00111         break;
00112     case SEEK_END:
00113         npos += rome->rome_size;
00114         break;
00115     }
00116 
00117     if (npos < 0 || npos > rome->rome_size) {
00118         rc = EINVAL;
00119     } else {
00120         romf->romf_pos = npos;
00121         *pos = npos;
00122     }
00123     return rc;
00124 }
00125 
00129 static int UromRead(NUTFILE * fp, void *buffer, int size)
00130 {
00131     ROMFILE *romf = fp->nf_fcb;
00132     ROMENTRY *rome = romf->romf_entry;
00133 
00134     if ((u_int) size > rome->rome_size - romf->romf_pos)
00135         size = rome->rome_size - romf->romf_pos;
00136     if (size) {
00137         memcpy_P(buffer, (void *)(rome->rome_data + romf->romf_pos), size);
00138         romf->romf_pos += size;
00139     }
00140     return size;
00141 }
00142 
00148 static int UromWrite(NUTFILE * fp, CONST void *buffer, int len)
00149 {
00150     return -1;
00151 }
00152 
00158 #ifdef __HARVARD_ARCH__
00159 static int UromWrite_P(NUTFILE * fp, PGM_P buffer, int len)
00160 {
00161     return -1;
00162 }
00163 #endif
00164 
00165 
00169 static NUTFILE *UromOpen(NUTDEVICE * dev, CONST char *name, int mode,
00170                          int acc)
00171 {
00172     NUTFILE *fp = NutHeapAlloc(sizeof(NUTFILE));
00173     ROMENTRY *rome;
00174     ROMFILE *romf = 0;
00175 
00176     if (fp == 0) {
00177         errno = ENOMEM;
00178         return NUTFILE_EOF;
00179     }
00180 
00181     for (rome = romEntryList; rome; rome = rome->rome_next) {
00182         if (strcmp_P(name, rome->rome_name) == 0)
00183             break;
00184     }
00185     if (rome) {
00186         if ((romf = NutHeapAllocClear(sizeof(ROMFILE))) != 0)
00187             romf->romf_entry = rome;
00188         else
00189             errno = ENOMEM;
00190     } else
00191         errno = ENOENT;
00192 
00193     if (romf) {
00194         fp->nf_next = 0;
00195         fp->nf_dev = dev;
00196         fp->nf_fcb = romf;
00197     } else {
00198         NutHeapFree(fp);
00199         fp = NUTFILE_EOF;
00200     }
00201 
00202     return fp;
00203 }
00204 
00208 static int UromClose(NUTFILE * fp)
00209 {
00210     if (fp && fp != NUTFILE_EOF) {
00211         if (fp->nf_fcb)
00212             NutHeapFree(fp->nf_fcb);
00213         NutHeapFree(fp);
00214     }
00215     return 0;
00216 }
00217 
00221 static long UromSize(NUTFILE * fp)
00222 {
00223     ROMFILE *romf = fp->nf_fcb;
00224     ROMENTRY *rome = romf->romf_entry;
00225 
00226     return (long) rome->rome_size;
00227 }
00228 
00232 int UromIOCtl(NUTDEVICE * dev, int req, void *conf)
00233 {
00234     int rc = -1;
00235 
00236     switch (req) {
00237     case FS_FILE_SEEK:
00238         UromSeek((NUTFILE *) ((IOCTL_ARG3 *) conf)->arg1,      /* */
00239                      (long *) ((IOCTL_ARG3 *) conf)->arg2,      /* */
00240                      (int) ((IOCTL_ARG3 *) conf)->arg3);
00241         break;
00242     }
00243     return rc;
00244 }
00245 
00246 
00253 NUTDEVICE devUrom = {
00254     0,                          
00255     {'U', 'R', 'O', 'M', 0, 0, 0, 0, 0},        
00256     IFTYP_ROM,                  
00257     0,                          
00258     0,                          
00259     0,                          
00260     0,                          
00261     0,                          
00262     UromIOCtl,                  
00263     UromRead,                   
00264     UromWrite,                  
00265 #ifdef __HARVARD_ARCH__
00266     UromWrite_P,                
00267 #endif
00268     UromOpen,                   
00269     UromClose,                  
00270     UromSize                    
00271 };
00272 

© 2000-2007 by egnite Software GmbH - visit http://www.ethernut.de/