Nut/OS  4.10.3
API Reference
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$
00036  * Revision 1.11  2009/02/13 14:52:05  haraldkipp
00037  * Include memdebug.h for heap management debugging support.
00038  *
00039  * Revision 1.10  2009/02/06 15:40:29  haraldkipp
00040  * Using newly available strdup() and calloc().
00041  * Replaced NutHeap routines by standard malloc/free.
00042  * Replaced pointer value 0 by NULL.
00043  *
00044  * Revision 1.9  2009/01/17 11:26:46  haraldkipp
00045  * Getting rid of two remaining BSD types in favor of stdint.
00046  * Replaced 'u_int' by 'unsinged int' and 'uptr_t' by 'uintptr_t'.
00047  *
00048  * Revision 1.8  2008/04/18 13:22:27  haraldkipp
00049  * Added type casts to fix ICCAVR V7.16 compile errors.
00050  *
00051  * Revision 1.7  2006/03/02 19:57:34  haraldkipp
00052  * ICCARM insists on a (void *) typecast for the second parameter of memcpy().
00053  *
00054  * Revision 1.6  2006/01/05 16:45:20  haraldkipp
00055  * Dynamic NUTFILE allocation for detached block device.
00056  *
00057  * Revision 1.5  2005/08/05 11:29:07  olereinhardt
00058  * Added IOCTL function with support for seek
00059  *
00060  * Revision 1.4  2004/03/18 11:37:06  haraldkipp
00061  * Deprecated functions removed
00062  *
00063  * Revision 1.3  2004/03/16 16:48:27  haraldkipp
00064  * Added Jan Dubiec's H8/300 port.
00065  *
00066  * Revision 1.2  2003/07/20 19:27:59  haraldkipp
00067  * Patch by Alessandro Zummo. Moves the urom filesystem filenames to
00068  * AVR's flash memory.
00069  *
00070  * Revision 1.1.1.1  2003/05/09 14:41:02  haraldkipp
00071  * Initial using 3.2.1
00072  *
00073  * Revision 1.12  2003/04/21 16:58:20  harald
00074  * Make use of predefined eof
00075  *
00076  * Revision 1.11  2003/02/04 17:57:14  harald
00077  * Version 3 released
00078  *
00079  * Revision 1.10  2002/11/02 15:16:09  harald
00080  * Compiler warning avoided
00081  *
00082  * Revision 1.9  2002/06/26 17:29:13  harald
00083  * First pre-release with 2.4 stack
00084  *
00085  */
00086 
00087 #include <sys/heap.h>
00088 #include <sys/file.h>
00089 #include <sys/device.h>
00090 
00091 #include <stdlib.h>
00092 #include <string.h>
00093 #include <errno.h>
00094 #include <stdio.h>
00095 #include <memdebug.h>
00096 
00097 #include <fs/fs.h>
00098 #include <dev/urom.h>
00099 #include <fs/uromfs.h>
00100 
00101 /*
00102 static int UromRead(NUTFILE * fp, void *buffer, int size);
00103 static int UromWrite(NUTFILE * fp, CONST void *buffer, int len);
00104 #ifdef __HARVARD_ARCH__
00105 static int UromWrite_P(NUTFILE * fp, PGM_P buffer, int len);
00106 #endif
00107 static NUTFILE *UromOpen(NUTDEVICE * dev, CONST char *name, int mode,
00108                          int acc);
00109 static int UromClose(NUTFILE * fp);
00110 static long UromSize(NUTFILE * fp);
00111 */
00116 
00117 static int UromSeek(NUTFILE * fp, long *pos, int whence)
00118 {
00119     ROMFILE *romf = fp->nf_fcb;
00120     ROMENTRY *rome = romf->romf_entry;
00121 
00122     int rc = 0;
00123     long npos = *pos;
00124 
00125     switch (whence) {
00126     case SEEK_CUR:
00127         npos += romf->romf_pos;
00128         break;
00129     case SEEK_END:
00130         npos += rome->rome_size;
00131         break;
00132     }
00133 
00134     if (npos < 0 || npos > rome->rome_size) {
00135         rc = EINVAL;
00136     } else {
00137         romf->romf_pos = npos;
00138         *pos = npos;
00139     }
00140     return rc;
00141 }
00142 
00146 static int UromRead(NUTFILE * fp, void *buffer, int size)
00147 {
00148     ROMFILE *romf = fp->nf_fcb;
00149     ROMENTRY *rome = romf->romf_entry;
00150 
00151     if ((unsigned int) size > rome->rome_size - romf->romf_pos)
00152         size = rome->rome_size - romf->romf_pos;
00153     if (size) {
00154         memcpy_P(buffer, (PGM_P)(rome->rome_data + romf->romf_pos), size);
00155         romf->romf_pos += size;
00156     }
00157     return size;
00158 }
00159 
00165 static int UromWrite(NUTFILE * fp, CONST void *buffer, int len)
00166 {
00167     return -1;
00168 }
00169 
00175 #ifdef __HARVARD_ARCH__
00176 static int UromWrite_P(NUTFILE * fp, PGM_P buffer, int len)
00177 {
00178     return -1;
00179 }
00180 #endif
00181 
00182 
00186 static NUTFILE *UromOpen(NUTDEVICE * dev, CONST char *name, int mode,
00187                          int acc)
00188 {
00189     NUTFILE *fp = malloc(sizeof(NUTFILE));
00190     ROMENTRY *rome;
00191     ROMFILE *romf = 0;
00192 
00193     if (fp == 0) {
00194         errno = ENOMEM;
00195         return NUTFILE_EOF;
00196     }
00197 
00198     for (rome = romEntryList; rome; rome = rome->rome_next) {
00199         if (strcmp_P(name, rome->rome_name) == 0)
00200             break;
00201     }
00202     if (rome) {
00203         if ((romf = calloc(1, sizeof(ROMFILE))) != 0)
00204             romf->romf_entry = rome;
00205         else
00206             errno = ENOMEM;
00207     } else
00208         errno = ENOENT;
00209 
00210     if (romf) {
00211         fp->nf_next = 0;
00212         fp->nf_dev = dev;
00213         fp->nf_fcb = romf;
00214     } else {
00215         free(fp);
00216         fp = NUTFILE_EOF;
00217     }
00218 
00219     return fp;
00220 }
00221 
00225 static int UromClose(NUTFILE * fp)
00226 {
00227     if (fp && fp != NUTFILE_EOF) {
00228         if (fp->nf_fcb)
00229             free(fp->nf_fcb);
00230         free(fp);
00231     }
00232     return 0;
00233 }
00234 
00238 static long UromSize(NUTFILE * fp)
00239 {
00240     ROMFILE *romf = fp->nf_fcb;
00241     ROMENTRY *rome = romf->romf_entry;
00242 
00243     return (long) rome->rome_size;
00244 }
00245 
00249 int UromIOCtl(NUTDEVICE * dev, int req, void *conf)
00250 {
00251     int rc = -1;
00252 
00253     switch (req) {
00254     case FS_FILE_SEEK:
00255         UromSeek((NUTFILE *) ((IOCTL_ARG3 *) conf)->arg1,      /* */
00256                      (long *) ((IOCTL_ARG3 *) conf)->arg2,      /* */
00257                      (int) ((IOCTL_ARG3 *) conf)->arg3);
00258         break;
00259     }
00260     return rc;
00261 }
00262 
00263 
00270 NUTDEVICE devUrom = {
00271     0,                          
00272     {'U', 'R', 'O', 'M', 0, 0, 0, 0, 0},        
00273     IFTYP_ROM,                  
00274     0,                          
00275     0,                          
00276     0,                          
00277     0,                          
00278     0,                          
00279     UromIOCtl,                  
00280     UromRead,                   
00281     UromWrite,                  
00282 #ifdef __HARVARD_ARCH__
00283     UromWrite_P,                
00284 #endif
00285     UromOpen,                   
00286     UromClose,                  
00287     UromSize                    
00288 };
00289