Go to the documentation of this file.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
00076
00077
00078
00079
00080
00081
00082
00083
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
00103
00104
00105
00106
00107
00108
00109
00110
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