ramdisk.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /****************************************************************************
  2. * drivers/ramdisk.c
  3. *
  4. * Copyright (C) 2008-2009, 2011 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <sys/types.h>
  40. #include <sys/ioctl.h>
  41. #include <stdint.h>
  42. #include <stdbool.h>
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <string.h>
  46. #include <debug.h>
  47. #include <errno.h>
  48. #include <nuttx/kmalloc.h>
  49. #include <nuttx/fs/fs.h>
  50. #include <nuttx/ramdisk.h>
  51. /****************************************************************************
  52. * Private Definitions
  53. ****************************************************************************/
  54. /****************************************************************************
  55. * Private Types
  56. ****************************************************************************/
  57. struct rd_struct_s
  58. {
  59. uint32_t rd_nsectors; /* Number of sectors on device */
  60. uint16_t rd_sectsize; /* The size of one sector */
  61. #ifdef CONFIG_FS_WRITABLE
  62. bool rd_writeenabled; /* true: can write to ram disk */
  63. uint8_t *rd_buffer; /* RAM disk backup memory */
  64. #else
  65. const uint8_t *rd_buffer; /* ROM disk backup memory */
  66. #endif
  67. };
  68. /****************************************************************************
  69. * Private Function Prototypes
  70. ****************************************************************************/
  71. static int rd_open(FAR struct inode *inode);
  72. static int rd_close(FAR struct inode *inode);
  73. static ssize_t rd_read(FAR struct inode *inode, unsigned char *buffer,
  74. size_t start_sector, unsigned int nsectors);
  75. #ifdef CONFIG_FS_WRITABLE
  76. static ssize_t rd_write(FAR struct inode *inode, const unsigned char *buffer,
  77. size_t start_sector, unsigned int nsectors);
  78. #endif
  79. static int rd_geometry(FAR struct inode *inode, struct geometry *geometry);
  80. static int rd_ioctl(FAR struct inode *inode, int cmd, unsigned long arg);
  81. /****************************************************************************
  82. * Private Data
  83. ****************************************************************************/
  84. static const struct block_operations g_bops =
  85. {
  86. rd_open, /* open */
  87. rd_close, /* close */
  88. rd_read, /* read */
  89. #ifdef CONFIG_FS_WRITABLE
  90. rd_write, /* write */
  91. #else
  92. NULL, /* write */
  93. #endif
  94. rd_geometry, /* geometry */
  95. rd_ioctl /* ioctl */
  96. };
  97. /****************************************************************************
  98. * Private Functions
  99. ****************************************************************************/
  100. /****************************************************************************
  101. * Name: rd_open
  102. *
  103. * Description: Open the block device
  104. *
  105. ****************************************************************************/
  106. static int rd_open(FAR struct inode *inode)
  107. {
  108. fvdbg("Entry\n");
  109. return OK;
  110. }
  111. /****************************************************************************
  112. * Name: rd_closel
  113. *
  114. * Description: close the block device
  115. *
  116. ****************************************************************************/
  117. static int rd_close(FAR struct inode *inode)
  118. {
  119. fvdbg("Entry\n");
  120. return OK;
  121. }
  122. /****************************************************************************
  123. * Name: rd_read
  124. *
  125. * Description: Read the specified numer of sectors
  126. *
  127. ****************************************************************************/
  128. static ssize_t rd_read(FAR struct inode *inode, unsigned char *buffer,
  129. size_t start_sector, unsigned int nsectors)
  130. {
  131. struct rd_struct_s *dev;
  132. DEBUGASSERT(inode && inode->i_private);
  133. dev = (struct rd_struct_s *)inode->i_private;
  134. fvdbg("sector: %d nsectors: %d sectorsize: %d\n",
  135. start_sector, dev->rd_sectsize, nsectors);
  136. if (start_sector < dev->rd_nsectors &&
  137. start_sector + nsectors <= dev->rd_nsectors)
  138. {
  139. fvdbg("Transfer %d bytes from %p\n",
  140. nsectors * dev->rd_sectsize,
  141. &dev->rd_buffer[start_sector * dev->rd_sectsize]);
  142. memcpy(buffer,
  143. &dev->rd_buffer[start_sector * dev->rd_sectsize],
  144. nsectors * dev->rd_sectsize);
  145. return nsectors;
  146. }
  147. return -EINVAL;
  148. }
  149. /****************************************************************************
  150. * Name: rd_write
  151. *
  152. * Description: Write the specified number of sectors
  153. *
  154. ****************************************************************************/
  155. #ifdef CONFIG_FS_WRITABLE
  156. static ssize_t rd_write(FAR struct inode *inode, const unsigned char *buffer,
  157. size_t start_sector, unsigned int nsectors)
  158. {
  159. struct rd_struct_s *dev;
  160. DEBUGASSERT(inode && inode->i_private);
  161. dev = (struct rd_struct_s *)inode->i_private;
  162. fvdbg("sector: %d nsectors: %d sectorsize: %d\n",
  163. start_sector, dev->rd_sectsize, nsectors);
  164. if (!dev->rd_writeenabled)
  165. {
  166. return -EACCES;
  167. }
  168. else if (start_sector < dev->rd_nsectors &&
  169. start_sector + nsectors <= dev->rd_nsectors)
  170. {
  171. fvdbg("Transfer %d bytes from %p\n",
  172. nsectors * dev->rd_sectsize,
  173. &dev->rd_buffer[start_sector * dev->rd_sectsize]);
  174. memcpy(&dev->rd_buffer[start_sector * dev->rd_sectsize],
  175. buffer,
  176. nsectors * dev->rd_sectsize);
  177. return nsectors;
  178. }
  179. return -EFBIG;
  180. }
  181. #endif
  182. /****************************************************************************
  183. * Name: rd_geometry
  184. *
  185. * Description: Return device geometry
  186. *
  187. ****************************************************************************/
  188. static int rd_geometry(FAR struct inode *inode, struct geometry *geometry)
  189. {
  190. struct rd_struct_s *dev;
  191. fvdbg("Entry\n");
  192. DEBUGASSERT(inode);
  193. if (geometry)
  194. {
  195. dev = (struct rd_struct_s *)inode->i_private;
  196. geometry->geo_available = true;
  197. geometry->geo_mediachanged = false;
  198. #ifdef CONFIG_FS_WRITABLE
  199. geometry->geo_writeenabled = dev->rd_writeenabled;
  200. #else
  201. geometry->geo_writeenabled = false;
  202. #endif
  203. geometry->geo_nsectors = dev->rd_nsectors;
  204. geometry->geo_sectorsize = dev->rd_sectsize;
  205. fvdbg("available: true mediachanged: false writeenabled: %s\n",
  206. geometry->geo_writeenabled ? "true" : "false");
  207. fvdbg("nsectors: %d sectorsize: %d\n",
  208. geometry->geo_nsectors, geometry->geo_sectorsize);
  209. return OK;
  210. }
  211. return -EINVAL;
  212. }
  213. /****************************************************************************
  214. * Name: rd_ioctl
  215. *
  216. * Description: Return device geometry
  217. *
  218. ****************************************************************************/
  219. static int rd_ioctl(FAR struct inode *inode, int cmd, unsigned long arg)
  220. {
  221. struct rd_struct_s *dev ;
  222. void **ppv = (void**)((uintptr_t)arg);
  223. fvdbg("Entry\n");
  224. /* Only one ioctl command is supported */
  225. DEBUGASSERT(inode && inode->i_private);
  226. if (cmd == BIOC_XIPBASE && ppv)
  227. {
  228. dev = (struct rd_struct_s *)inode->i_private;
  229. *ppv = (void*)dev->rd_buffer;
  230. fvdbg("ppv: %p\n", *ppv);
  231. return OK;
  232. }
  233. return -ENOTTY;
  234. }
  235. /****************************************************************************
  236. * Public Functions
  237. ****************************************************************************/
  238. /****************************************************************************
  239. * Name: ramdisk_register
  240. *
  241. * Description: Register the a ramdisk
  242. ****************************************************************************/
  243. #ifdef CONFIG_FS_WRITABLE
  244. int ramdisk_register(int minor, uint8_t *buffer, uint32_t nsectors,
  245. uint16_t sectsize, bool writeenabled)
  246. #else
  247. int romdisk_register(int minor, uint8_t *buffer, uint32_t nsectors,
  248. uint16_t sectsize)
  249. #endif
  250. {
  251. struct rd_struct_s *dev;
  252. char devname[16];
  253. int ret = -ENOMEM;
  254. fvdbg("buffer: %p nsectors: %d sectsize: %d\n", buffer, nsectors, sectsize);
  255. /* Sanity check */
  256. #ifdef CONFIG_DEBUG
  257. if (minor < 0 || minor > 255 || !buffer || !nsectors || !sectsize)
  258. {
  259. return -EINVAL;
  260. }
  261. #endif
  262. /* Allocate a ramdisk device structure */
  263. dev = (struct rd_struct_s *)kmalloc(sizeof(struct rd_struct_s));
  264. if (dev)
  265. {
  266. /* Initialize the ramdisk device structure */
  267. dev->rd_nsectors = nsectors; /* Number of sectors on device */
  268. dev->rd_sectsize = sectsize; /* The size of one sector */
  269. #ifdef CONFIG_FS_WRITABLE
  270. dev->rd_writeenabled = writeenabled; /* true: can write to ram disk */
  271. #endif
  272. dev->rd_buffer = buffer; /* RAM disk backup memory */
  273. /* Create a ramdisk device name */
  274. snprintf(devname, 16, "/dev/ram%d", minor);
  275. /* Inode private data is a reference to the ramdisk device stgructure */
  276. ret = register_blockdriver(devname, &g_bops, 0, dev);
  277. if (ret < 0)
  278. {
  279. fdbg("register_blockdriver failed: %d\n", -ret);
  280. kfree(dev);
  281. }
  282. }
  283. return ret;
  284. }