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
00073
00074
00075 #include <string.h>
00076 #include <errno.h>
00077 #include <stdio.h>
00078
00079 #include <sys/heap.h>
00080 #include <sys/file.h>
00081 #include <sys/device.h>
00082
00083 #include <fs/fs.h>
00084 #include <dev/urom.h>
00085 #include <fs/uromfs.h>
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00102
00103 static int UromSeek(NUTFILE * fp, long *pos, int whence)
00104 {
00105 ROMFILE *romf = fp->nf_fcb;
00106 ROMENTRY *rome = romf->romf_entry;
00107
00108 int rc = 0;
00109 long npos = *pos;
00110
00111 switch (whence) {
00112 case SEEK_CUR:
00113 npos += romf->romf_pos;
00114 break;
00115 case SEEK_END:
00116 npos += rome->rome_size;
00117 break;
00118 }
00119
00120 if (npos < 0 || npos > rome->rome_size) {
00121 rc = EINVAL;
00122 } else {
00123 romf->romf_pos = npos;
00124 *pos = npos;
00125 }
00126 return rc;
00127 }
00128
00132 static int UromRead(NUTFILE * fp, void *buffer, int size)
00133 {
00134 ROMFILE *romf = fp->nf_fcb;
00135 ROMENTRY *rome = romf->romf_entry;
00136
00137 if ((u_int) size > rome->rome_size - romf->romf_pos)
00138 size = rome->rome_size - romf->romf_pos;
00139 if (size) {
00140 memcpy_P(buffer, (PGM_P)(rome->rome_data + romf->romf_pos), size);
00141 romf->romf_pos += size;
00142 }
00143 return size;
00144 }
00145
00151 static int UromWrite(NUTFILE * fp, CONST void *buffer, int len)
00152 {
00153 return -1;
00154 }
00155
00161 #ifdef __HARVARD_ARCH__
00162 static int UromWrite_P(NUTFILE * fp, PGM_P buffer, int len)
00163 {
00164 return -1;
00165 }
00166 #endif
00167
00168
00172 static NUTFILE *UromOpen(NUTDEVICE * dev, CONST char *name, int mode,
00173 int acc)
00174 {
00175 NUTFILE *fp = NutHeapAlloc(sizeof(NUTFILE));
00176 ROMENTRY *rome;
00177 ROMFILE *romf = 0;
00178
00179 if (fp == 0) {
00180 errno = ENOMEM;
00181 return NUTFILE_EOF;
00182 }
00183
00184 for (rome = romEntryList; rome; rome = rome->rome_next) {
00185 if (strcmp_P(name, rome->rome_name) == 0)
00186 break;
00187 }
00188 if (rome) {
00189 if ((romf = NutHeapAllocClear(sizeof(ROMFILE))) != 0)
00190 romf->romf_entry = rome;
00191 else
00192 errno = ENOMEM;
00193 } else
00194 errno = ENOENT;
00195
00196 if (romf) {
00197 fp->nf_next = 0;
00198 fp->nf_dev = dev;
00199 fp->nf_fcb = romf;
00200 } else {
00201 NutHeapFree(fp);
00202 fp = NUTFILE_EOF;
00203 }
00204
00205 return fp;
00206 }
00207
00211 static int UromClose(NUTFILE * fp)
00212 {
00213 if (fp && fp != NUTFILE_EOF) {
00214 if (fp->nf_fcb)
00215 NutHeapFree(fp->nf_fcb);
00216 NutHeapFree(fp);
00217 }
00218 return 0;
00219 }
00220
00224 static long UromSize(NUTFILE * fp)
00225 {
00226 ROMFILE *romf = fp->nf_fcb;
00227 ROMENTRY *rome = romf->romf_entry;
00228
00229 return (long) rome->rome_size;
00230 }
00231
00235 int UromIOCtl(NUTDEVICE * dev, int req, void *conf)
00236 {
00237 int rc = -1;
00238
00239 switch (req) {
00240 case FS_FILE_SEEK:
00241 UromSeek((NUTFILE *) ((IOCTL_ARG3 *) conf)->arg1,
00242 (long *) ((IOCTL_ARG3 *) conf)->arg2,
00243 (int) ((IOCTL_ARG3 *) conf)->arg3);
00244 break;
00245 }
00246 return rc;
00247 }
00248
00249
00256 NUTDEVICE devUrom = {
00257 0,
00258 {'U', 'R', 'O', 'M', 0, 0, 0, 0, 0},
00259 IFTYP_ROM,
00260 0,
00261 0,
00262 0,
00263 0,
00264 0,
00265 UromIOCtl,
00266 UromRead,
00267 UromWrite,
00268 #ifdef __HARVARD_ARCH__
00269 UromWrite_P,
00270 #endif
00271 UromOpen,
00272 UromClose,
00273 UromSize
00274 };
00275