ramdisk.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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/drivers/ramdisk.h>
  51. /****************************************************************************
  52. * Pre-processor Definitions
  53. ****************************************************************************/
  54. /* Helpers for rdflags */
  55. /* User input flags */
  56. #define RDFLAG_USER (RDFLAG_WRENABLED | RDFLAG_FUNLINK)
  57. #define RDFLAG_IS_WRENABLED(f) (((f) & RDFLAG_WRENABLED) != 0)
  58. #define RDFLAG_IS_FUNLINK(f) (((f) & RDFLAG_WRENABLED) != 0)
  59. /* Flag set when the RAM disk block driver is unlink */
  60. #define RDFLAG_UNLINK(f) do { (f) |= RDFLAG_UNLINKED; } while (0)
  61. #define RDFLAG_IS_UNLINKED(f) (((f) & RDFLAG_UNLINKED) != 0)
  62. /****************************************************************************
  63. * Private Types
  64. ****************************************************************************/
  65. struct rd_struct_s
  66. {
  67. uint32_t rd_nsectors; /* Number of sectors on device */
  68. uint16_t rd_sectsize; /* The size of one sector */
  69. #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
  70. uint8_t rd_crefs; /* Open reference count */
  71. #endif
  72. uint8_t rd_flags; /* See RDFLAG_* definitions */
  73. #ifdef CONFIG_FS_WRITABLE
  74. FAR uint8_t *rd_buffer; /* RAM disk backup memory */
  75. #else
  76. FAR const uint8_t *rd_buffer; /* ROM disk backup memory */
  77. #endif
  78. };
  79. /****************************************************************************
  80. * Private Function Prototypes
  81. ****************************************************************************/
  82. #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
  83. static void rd_destroy(FAR struct rd_struct_s *dev);
  84. static int rd_open(FAR struct inode *inode);
  85. static int rd_close(FAR struct inode *inode);
  86. #endif
  87. static ssize_t rd_read(FAR struct inode *inode, FAR unsigned char *buffer,
  88. size_t start_sector, unsigned int nsectors);
  89. #ifdef CONFIG_FS_WRITABLE
  90. static ssize_t rd_write(FAR struct inode *inode,
  91. FAR const unsigned char *buffer, size_t start_sector,
  92. unsigned int nsectors);
  93. #endif
  94. static int rd_geometry(FAR struct inode *inode,
  95. FAR struct geometry *geometry);
  96. static int rd_ioctl(FAR struct inode *inode, int cmd,
  97. unsigned long arg);
  98. #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
  99. static int rd_unlink(FAR struct inode *inode);
  100. #endif
  101. /****************************************************************************
  102. * Private Data
  103. ****************************************************************************/
  104. static const struct block_operations g_bops =
  105. {
  106. #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
  107. rd_open, /* open */
  108. rd_close, /* close */
  109. #else
  110. 0, /* open */
  111. 0, /* close */
  112. #endif
  113. rd_read, /* read */
  114. #ifdef CONFIG_FS_WRITABLE
  115. rd_write, /* write */
  116. #else
  117. NULL, /* write */
  118. #endif
  119. rd_geometry, /* geometry */
  120. rd_ioctl, /* ioctl */
  121. #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
  122. rd_unlink /* unlink */
  123. #endif
  124. };
  125. /****************************************************************************
  126. * Private Functions
  127. ****************************************************************************/
  128. /****************************************************************************
  129. * Name: rd_destroy
  130. *
  131. * Description:
  132. * Free all resources used by the RAM disk
  133. *
  134. ****************************************************************************/
  135. #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
  136. static void rd_destroy(FAR struct rd_struct_s *dev)
  137. {
  138. finfo("Destroying RAM disk\n");
  139. /* We we configured to free the RAM disk memory when unlinked? */
  140. #ifdef CONFIG_FS_WRITABLE
  141. if (RDFLAG_IS_UNLINKED(dev->rd_flags))
  142. {
  143. /* Yes.. do it */
  144. kmm_free(dev->rd_buffer);
  145. }
  146. #endif
  147. /* And free the block driver itself */
  148. kmm_free(dev);
  149. }
  150. #endif
  151. /****************************************************************************
  152. * Name: rd_open
  153. *
  154. * Description: Open the block device
  155. *
  156. ****************************************************************************/
  157. #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
  158. static int rd_open(FAR struct inode *inode)
  159. {
  160. FAR struct rd_struct_s *dev;
  161. DEBUGASSERT(inode && inode->i_private);
  162. dev = (FAR struct rd_struct_s *)inode->i_private;
  163. /* Increment the open reference count */
  164. dev->rd_crefs++;
  165. DEBUGASSERT(dev->rd_crefs > 0);
  166. finfo("rd_crefs: %d\n", dev->rd_crefs);
  167. return OK;
  168. }
  169. #endif
  170. /****************************************************************************
  171. * Name: rd_close
  172. *
  173. * Description: close the block device
  174. *
  175. ****************************************************************************/
  176. #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
  177. static int rd_close(FAR struct inode *inode)
  178. {
  179. FAR struct rd_struct_s *dev;
  180. DEBUGASSERT(inode && inode->i_private);
  181. dev = (FAR struct rd_struct_s *)inode->i_private;
  182. /* Increment the open reference count */
  183. DEBUGASSERT(dev->rd_crefs > 0);
  184. dev->rd_crefs--;
  185. finfo("rd_crefs: %d\n", dev->rd_crefs);
  186. #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
  187. /* Was that the last open reference to the RAM disk? */
  188. if (dev->rd_crefs == 0)
  189. {
  190. /* Yes.. Have we been unlinked? */
  191. if (RDFLAG_IS_UNLINKED(dev->rd_flags))
  192. {
  193. /* Yes.. Release all of the RAM disk resources */
  194. rd_destroy(dev);
  195. }
  196. }
  197. #endif
  198. return OK;
  199. }
  200. #endif
  201. /****************************************************************************
  202. * Name: rd_read
  203. *
  204. * Description: Read the specified number of sectors
  205. *
  206. ****************************************************************************/
  207. static ssize_t rd_read(FAR struct inode *inode, unsigned char *buffer,
  208. size_t start_sector, unsigned int nsectors)
  209. {
  210. FAR struct rd_struct_s *dev;
  211. DEBUGASSERT(inode && inode->i_private);
  212. dev = (FAR struct rd_struct_s *)inode->i_private;
  213. finfo("sector: %d nsectors: %d sectorsize: %d\n",
  214. start_sector, dev->rd_sectsize, nsectors);
  215. if (start_sector < dev->rd_nsectors &&
  216. start_sector + nsectors <= dev->rd_nsectors)
  217. {
  218. finfo("Transfer %d bytes from %p\n",
  219. nsectors * dev->rd_sectsize,
  220. &dev->rd_buffer[start_sector * dev->rd_sectsize]);
  221. memcpy(buffer,
  222. &dev->rd_buffer[start_sector * dev->rd_sectsize],
  223. nsectors * dev->rd_sectsize);
  224. return nsectors;
  225. }
  226. return -EINVAL;
  227. }
  228. /****************************************************************************
  229. * Name: rd_write
  230. *
  231. * Description: Write the specified number of sectors
  232. *
  233. ****************************************************************************/
  234. #ifdef CONFIG_FS_WRITABLE
  235. static ssize_t rd_write(FAR struct inode *inode, const unsigned char *buffer,
  236. size_t start_sector, unsigned int nsectors)
  237. {
  238. struct rd_struct_s *dev;
  239. DEBUGASSERT(inode && inode->i_private);
  240. dev = (struct rd_struct_s *)inode->i_private;
  241. finfo("sector: %d nsectors: %d sectorsize: %d\n",
  242. start_sector, dev->rd_sectsize, nsectors);
  243. if (!RDFLAG_IS_WRENABLED(dev->rd_flags))
  244. {
  245. return -EACCES;
  246. }
  247. else if (start_sector < dev->rd_nsectors &&
  248. start_sector + nsectors <= dev->rd_nsectors)
  249. {
  250. finfo("Transfer %d bytes to %p\n",
  251. nsectors * dev->rd_sectsize,
  252. &dev->rd_buffer[start_sector * dev->rd_sectsize]);
  253. memcpy(&dev->rd_buffer[start_sector * dev->rd_sectsize],
  254. buffer,
  255. nsectors * dev->rd_sectsize);
  256. return nsectors;
  257. }
  258. return -EFBIG;
  259. }
  260. #endif
  261. /****************************************************************************
  262. * Name: rd_geometry
  263. *
  264. * Description: Return device geometry
  265. *
  266. ****************************************************************************/
  267. static int rd_geometry(FAR struct inode *inode, struct geometry *geometry)
  268. {
  269. struct rd_struct_s *dev;
  270. finfo("Entry\n");
  271. DEBUGASSERT(inode);
  272. if (geometry)
  273. {
  274. dev = (struct rd_struct_s *)inode->i_private;
  275. geometry->geo_available = true;
  276. geometry->geo_mediachanged = false;
  277. #ifdef CONFIG_FS_WRITABLE
  278. geometry->geo_writeenabled = RDFLAG_IS_WRENABLED(dev->rd_flags);
  279. #else
  280. geometry->geo_writeenabled = false;
  281. #endif
  282. geometry->geo_nsectors = dev->rd_nsectors;
  283. geometry->geo_sectorsize = dev->rd_sectsize;
  284. finfo("available: true mediachanged: false writeenabled: %s\n",
  285. geometry->geo_writeenabled ? "true" : "false");
  286. finfo("nsectors: %d sectorsize: %d\n",
  287. geometry->geo_nsectors, geometry->geo_sectorsize);
  288. return OK;
  289. }
  290. return -EINVAL;
  291. }
  292. /****************************************************************************
  293. * Name: rd_ioctl
  294. *
  295. * Description:
  296. * Return device geometry
  297. *
  298. ****************************************************************************/
  299. static int rd_ioctl(FAR struct inode *inode, int cmd, unsigned long arg)
  300. {
  301. FAR struct rd_struct_s *dev;
  302. FAR void **ppv = (void**)((uintptr_t)arg);
  303. finfo("Entry\n");
  304. /* Only one ioctl command is supported */
  305. DEBUGASSERT(inode && inode->i_private);
  306. if (cmd == BIOC_XIPBASE && ppv)
  307. {
  308. dev = (FAR struct rd_struct_s *)inode->i_private;
  309. *ppv = (FAR void *)dev->rd_buffer;
  310. finfo("ppv: %p\n", *ppv);
  311. return OK;
  312. }
  313. return -ENOTTY;
  314. }
  315. /****************************************************************************
  316. * Name: rd_unlink
  317. *
  318. * Description:
  319. * The block driver has been unlinked.
  320. *
  321. ****************************************************************************/
  322. #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
  323. static int rd_unlink(FAR struct inode *inode)
  324. {
  325. FAR struct rd_struct_s *dev;
  326. DEBUGASSERT(inode && inode->i_private);
  327. dev = (FAR struct rd_struct_s *)inode->i_private;
  328. /* Mark the pipe unlinked */
  329. RDFLAG_UNLINK(dev->rd_flags);
  330. /* Are the any open references to the driver? */
  331. if (dev->rd_crefs == 0)
  332. {
  333. /* No... release all resources held by the block driver */
  334. rd_destroy(dev);
  335. }
  336. return OK;
  337. }
  338. #endif
  339. /****************************************************************************
  340. * Public Functions
  341. ****************************************************************************/
  342. /****************************************************************************
  343. * Name: ramdisk_register or romdisk_register
  344. *
  345. * Description:
  346. * Non-standard function to register a ramdisk or a romdisk
  347. *
  348. * Input Parameters:
  349. * minor: Selects suffix of device named /dev/ramN, N={1,2,3...}
  350. * nsectors: Number of sectors on device
  351. * sectize: The size of one sector
  352. * rdflags: See RDFLAG_* definitions
  353. * buffer: RAM disk backup memory
  354. *
  355. * Returned Value:
  356. * Zero on success; a negated errno value on failure.
  357. *
  358. ****************************************************************************/
  359. #ifdef CONFIG_FS_WRITABLE
  360. int ramdisk_register(int minor, FAR uint8_t *buffer, uint32_t nsectors,
  361. uint16_t sectsize, uint8_t rdflags)
  362. #else
  363. int romdisk_register(int minor, FAR const uint8_t *buffer, uint32_t nsectors,
  364. uint16_t sectsize)
  365. #endif
  366. {
  367. struct rd_struct_s *dev;
  368. char devname[16];
  369. int ret = -ENOMEM;
  370. finfo("buffer: %p nsectors: %d sectsize: %d\n", buffer, nsectors, sectsize);
  371. /* Sanity check */
  372. #ifdef CONFIG_DEBUG_FEATURES
  373. if (minor < 0 || minor > 255 || !buffer || !nsectors || !sectsize)
  374. {
  375. return -EINVAL;
  376. }
  377. #endif
  378. /* Allocate a ramdisk device structure */
  379. dev = (struct rd_struct_s *)kmm_zalloc(sizeof(struct rd_struct_s));
  380. if (dev)
  381. {
  382. /* Initialize the ramdisk device structure */
  383. dev->rd_nsectors = nsectors; /* Number of sectors on device */
  384. dev->rd_sectsize = sectsize; /* The size of one sector */
  385. dev->rd_buffer = buffer; /* RAM disk backup memory */
  386. #ifdef CONFIG_FS_WRITABLE
  387. dev->rd_flags = rdflags & RDFLAG_USER;
  388. #endif
  389. /* Create a ramdisk device name */
  390. snprintf(devname, 16, "/dev/ram%d", minor);
  391. /* Inode private data is a reference to the ramdisk device structure */
  392. ret = register_blockdriver(devname, &g_bops, 0, dev);
  393. if (ret < 0)
  394. {
  395. ferr("register_blockdriver failed: %d\n", -ret);
  396. kmm_free(dev);
  397. }
  398. }
  399. return ret;
  400. }