uromfs.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Copyright (C) 2001-2003 by egnite Software GmbH. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  25. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  27. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. * For additional information see http://www.ethernut.de/
  31. *
  32. */
  33. /*
  34. * $Log$
  35. * Revision 1.11 2009/02/13 14:52:05 haraldkipp
  36. * Include memdebug.h for heap management debugging support.
  37. *
  38. * Revision 1.10 2009/02/06 15:40:29 haraldkipp
  39. * Using newly available strdup() and calloc().
  40. * Replaced NutHeap routines by standard malloc/free.
  41. * Replaced pointer value 0 by NULL.
  42. *
  43. * Revision 1.9 2009/01/17 11:26:46 haraldkipp
  44. * Getting rid of two remaining BSD types in favor of stdint.
  45. * Replaced 'u_int' by 'unsinged int' and 'uptr_t' by 'uintptr_t'.
  46. *
  47. * Revision 1.8 2008/04/18 13:22:27 haraldkipp
  48. * Added type casts to fix ICCAVR V7.16 compile errors.
  49. *
  50. * Revision 1.7 2006/03/02 19:57:34 haraldkipp
  51. * ICCARM insists on a (void *) typecast for the second parameter of memcpy().
  52. *
  53. * Revision 1.6 2006/01/05 16:45:20 haraldkipp
  54. * Dynamic NUTFILE allocation for detached block device.
  55. *
  56. * Revision 1.5 2005/08/05 11:29:07 olereinhardt
  57. * Added IOCTL function with support for seek
  58. *
  59. * Revision 1.4 2004/03/18 11:37:06 haraldkipp
  60. * Deprecated functions removed
  61. *
  62. * Revision 1.3 2004/03/16 16:48:27 haraldkipp
  63. * Added Jan Dubiec's H8/300 port.
  64. *
  65. * Revision 1.2 2003/07/20 19:27:59 haraldkipp
  66. * Patch by Alessandro Zummo. Moves the urom filesystem filenames to
  67. * AVR's flash memory.
  68. *
  69. * Revision 1.1.1.1 2003/05/09 14:41:02 haraldkipp
  70. * Initial using 3.2.1
  71. *
  72. * Revision 1.12 2003/04/21 16:58:20 harald
  73. * Make use of predefined eof
  74. *
  75. * Revision 1.11 2003/02/04 17:57:14 harald
  76. * Version 3 released
  77. *
  78. * Revision 1.10 2002/11/02 15:16:09 harald
  79. * Compiler warning avoided
  80. *
  81. * Revision 1.9 2002/06/26 17:29:13 harald
  82. * First pre-release with 2.4 stack
  83. *
  84. */
  85. #include <sys/heap.h>
  86. #include <sys/file.h>
  87. #include <sys/device.h>
  88. #include <stdlib.h>
  89. #include <string.h>
  90. #include <errno.h>
  91. #include <stdio.h>
  92. #include <memdebug.h>
  93. #include <fs/fs.h>
  94. #include <dev/urom.h>
  95. #include <fs/uromfs.h>
  96. /*
  97. static int UromRead(NUTFILE * fp, void *buffer, int size);
  98. static int UromWrite(NUTFILE * fp, const void *buffer, int len);
  99. #ifdef __HARVARD_ARCH__
  100. static int UromWrite_P(NUTFILE * fp, PGM_P buffer, int len);
  101. #endif
  102. static NUTFILE *UromOpen(NUTDEVICE * dev, const char *name, int mode,
  103. int acc);
  104. static int UromClose(NUTFILE * fp);
  105. static long UromSize(NUTFILE * fp);
  106. */
  107. /*!
  108. * \addtogroup xgurom
  109. */
  110. /*@{*/
  111. static int UromSeek(NUTFILE * fp, long *pos, int whence)
  112. {
  113. ROMFILE *romf = fp->nf_fcb;
  114. ROMENTRY *rome = romf->romf_entry;
  115. int rc = 0;
  116. long npos = *pos;
  117. switch (whence) {
  118. case SEEK_CUR:
  119. npos += romf->romf_pos;
  120. break;
  121. case SEEK_END:
  122. npos += rome->rome_size;
  123. break;
  124. }
  125. if (npos < 0 || npos > rome->rome_size) {
  126. rc = EINVAL;
  127. } else {
  128. romf->romf_pos = npos;
  129. *pos = npos;
  130. }
  131. return rc;
  132. }
  133. /*!
  134. * \brief Read from device.
  135. */
  136. static int UromRead(NUTFILE * fp, void *buffer, int size)
  137. {
  138. ROMFILE *romf = fp->nf_fcb;
  139. ROMENTRY *rome = romf->romf_entry;
  140. if ((unsigned int) size > rome->rome_size - romf->romf_pos)
  141. size = rome->rome_size - romf->romf_pos;
  142. if (size) {
  143. memcpy_P(buffer, (PGM_P)(rome->rome_data + romf->romf_pos), size);
  144. romf->romf_pos += size;
  145. }
  146. return size;
  147. }
  148. /*!
  149. * \brief Write data to a file.
  150. *
  151. * \return Always returns -1.
  152. */
  153. static int UromWrite(NUTFILE * fp, const void *buffer, int len)
  154. {
  155. return -1;
  156. }
  157. /*!
  158. * \brief Write data from progam space to a file.
  159. *
  160. * \return Always returns -1.
  161. */
  162. #ifdef __HARVARD_ARCH__
  163. static int UromWrite_P(NUTFILE * fp, PGM_P buffer, int len)
  164. {
  165. return -1;
  166. }
  167. #endif
  168. /*!
  169. * \brief Open a file.
  170. */
  171. static NUTFILE *UromOpen(NUTDEVICE * dev, const char *name, int mode,
  172. int acc)
  173. {
  174. NUTFILE *fp = malloc(sizeof(NUTFILE));
  175. ROMENTRY *rome;
  176. ROMFILE *romf = 0;
  177. if (fp == 0) {
  178. errno = ENOMEM;
  179. return NUTFILE_EOF;
  180. }
  181. if (*name == '/') {
  182. name++;
  183. }
  184. for (rome = romEntryList; rome; rome = rome->rome_next) {
  185. if (strcmp_P(name, rome->rome_name) == 0)
  186. break;
  187. }
  188. if (rome) {
  189. if ((romf = calloc(1, sizeof(ROMFILE))) != 0)
  190. romf->romf_entry = rome;
  191. else
  192. errno = ENOMEM;
  193. } else
  194. errno = ENOENT;
  195. if (romf) {
  196. fp->nf_dev = dev;
  197. fp->nf_fcb = romf;
  198. } else {
  199. free(fp);
  200. fp = NUTFILE_EOF;
  201. }
  202. return fp;
  203. }
  204. /*!
  205. * \brief Close a file.
  206. */
  207. static int UromClose(NUTFILE * fp)
  208. {
  209. if (fp && fp != NUTFILE_EOF) {
  210. if (fp->nf_fcb)
  211. free(fp->nf_fcb);
  212. free(fp);
  213. }
  214. return 0;
  215. }
  216. /*!
  217. * \brief Query file size.
  218. */
  219. static long UromSize(NUTFILE * fp)
  220. {
  221. ROMFILE *romf = fp->nf_fcb;
  222. ROMENTRY *rome = romf->romf_entry;
  223. return (long) rome->rome_size;
  224. }
  225. /*!
  226. * \brief Device specific functions.
  227. */
  228. int UromIOCtl(NUTDEVICE * dev, int req, void *conf)
  229. {
  230. int rc = -1;
  231. switch (req) {
  232. case FS_FILE_SEEK:
  233. UromSeek((NUTFILE *) ((IOCTL_ARG3 *) conf)->arg1, /* */
  234. (long *) ((IOCTL_ARG3 *) conf)->arg2, /* */
  235. (int) ((IOCTL_ARG3 *) conf)->arg3);
  236. break;
  237. }
  238. return rc;
  239. }
  240. /*!
  241. * \brief UROM device information structure.
  242. *
  243. * All this UROM stuff is so simple, that it even mimics
  244. * its own device driver.
  245. */
  246. NUTDEVICE devUrom = {
  247. 0, /*!< Pointer to next device. */
  248. {'U', 'R', 'O', 'M', 0, 0, 0, 0, 0}, /*!< Unique device name. */
  249. IFTYP_ROM, /*!< Type of device. Obsolete. */
  250. 0, /*!< Base address. Unused. */
  251. 0, /*!< First interrupt number. Unused. */
  252. 0, /*!< Interface control block. Unused. */
  253. 0, /*!< Driver control block. Unused. */
  254. 0, /*!< Driver initialization routine. Not supported. */
  255. UromIOCtl, /*!< Driver specific control function. Not supported. */
  256. UromRead, /*!< Read data from a file. */
  257. UromWrite, /*!< Write data to a file. */
  258. #ifdef __HARVARD_ARCH__
  259. UromWrite_P, /*!< Write data from program space to a file. */
  260. #endif
  261. UromOpen, /*!< Open a file. */
  262. UromClose, /*!< Close a file. */
  263. UromSize, /*!< Return file size. */
  264. NULL, /*!< Select function, optional, not yet implemented */
  265. };
  266. /*@}*/