00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
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
00086
00087
00088
00089
00090
00091
00092
00093
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