fs_romfsutil.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  1. /****************************************************************************
  2. * rm/romfs/fs_romfsutil.c
  3. *
  4. * Copyright (C) 2008-2009, 2013, 2017 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * References: Linux/Documentation/filesystems/romfs.txt
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. * 3. Neither the name NuttX nor the names of its contributors may be
  20. * used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  26. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  27. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  28. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  29. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  30. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  31. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  33. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. * POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. ****************************************************************************/
  37. /****************************************************************************
  38. * Included Files
  39. ****************************************************************************/
  40. #include <nuttx/config.h>
  41. #include <sys/types.h>
  42. #include <stdint.h>
  43. #include <stdbool.h>
  44. #include <stdlib.h>
  45. #include <string.h>
  46. #include <assert.h>
  47. #include <errno.h>
  48. #include <assert.h>
  49. #include <debug.h>
  50. #include <nuttx/kmalloc.h>
  51. #include <nuttx/fs/ioctl.h>
  52. #include <nuttx/fs/dirent.h>
  53. #include <nuttx/mtd/mtd.h>
  54. #include "fs_romfs.h"
  55. /****************************************************************************
  56. * Private Functions
  57. ****************************************************************************/
  58. /****************************************************************************
  59. * Name: romfs_devread32
  60. *
  61. * Description:
  62. * Read the big-endian 32-bit value from the mount device buffer
  63. *
  64. * Assumption:
  65. * All values are aligned to 32-bit boundaries
  66. *
  67. ****************************************************************************/
  68. static uint32_t romfs_devread32(struct romfs_mountpt_s *rm, int ndx)
  69. {
  70. return ((((uint32_t)rm->rm_buffer[ndx] & 0xff) << 24) |
  71. (((uint32_t)rm->rm_buffer[ndx + 1] & 0xff) << 16) |
  72. (((uint32_t)rm->rm_buffer[ndx + 2] & 0xff) << 8) |
  73. ((uint32_t)rm->rm_buffer[ndx + 3] & 0xff));
  74. }
  75. /****************************************************************************
  76. * Name: romfs_checkentry
  77. *
  78. * Description:
  79. * Check if the entry at offset is a directory or file path segment
  80. *
  81. ****************************************************************************/
  82. static inline int romfs_checkentry(struct romfs_mountpt_s *rm,
  83. uint32_t offset, const char *entryname,
  84. int entrylen,
  85. struct romfs_dirinfo_s *dirinfo)
  86. {
  87. char name[NAME_MAX+1];
  88. uint32_t linkoffset;
  89. uint32_t next;
  90. uint32_t info;
  91. uint32_t size;
  92. int ret;
  93. /* Parse the directory entry at this offset (which may be re-directed
  94. * to some other entry if HARLINKED).
  95. */
  96. ret = romfs_parsedirentry(rm, offset, &linkoffset, &next, &info, &size);
  97. if (ret < 0)
  98. {
  99. return ret;
  100. }
  101. /* Now we are pointing to the real entry of interest. Is it a
  102. * directory? Or a file?
  103. */
  104. if (IS_DIRECTORY(next) || IS_FILE(next))
  105. {
  106. /* Get the name of the directory entry. */
  107. ret = romfs_parsefilename(rm, offset, name);
  108. if (ret < 0)
  109. {
  110. return ret;
  111. }
  112. /* Then check if this the name segment we are looking for. The
  113. * string comparison is awkward because there is no terminator
  114. * on entryname (there is a terminator on name, however)
  115. */
  116. if (memcmp(entryname, name, entrylen) == 0 &&
  117. strlen(name) == entrylen)
  118. {
  119. /* Found it -- save the component info and return success */
  120. if (IS_DIRECTORY(next))
  121. {
  122. dirinfo->rd_dir.fr_firstoffset = info;
  123. dirinfo->rd_dir.fr_curroffset = info;
  124. dirinfo->rd_size = 0;
  125. }
  126. else
  127. {
  128. dirinfo->rd_dir.fr_curroffset = offset;
  129. dirinfo->rd_size = size;
  130. }
  131. dirinfo->rd_next = next;
  132. return OK;
  133. }
  134. }
  135. /* The entry is not a directory or it does not have the matching name */
  136. return -ENOENT;
  137. }
  138. /****************************************************************************
  139. * Name: romfs_devcacheread
  140. *
  141. * Description:
  142. * Read the specified sector for specified offset into the sector cache.
  143. * Return the index into the sector corresponding to the offset
  144. *
  145. ****************************************************************************/
  146. int16_t romfs_devcacheread(struct romfs_mountpt_s *rm, uint32_t offset)
  147. {
  148. uint32_t sector;
  149. int ret;
  150. /* rm->rm_cachesector holds the current sector that is buffer in or referenced
  151. * by rm->tm_buffer. If the requested sector is the same as this sector,
  152. * then we do nothing.
  153. */
  154. sector = SEC_NSECTORS(rm, offset);
  155. if (rm->rm_cachesector != sector)
  156. {
  157. /* Check the access mode */
  158. if (rm->rm_xipbase)
  159. {
  160. /* In XIP mode, rf_buffer is just an offset pointer into the device
  161. * address space.
  162. */
  163. rm->rm_buffer = rm->rm_xipbase + SEC_ALIGN(rm, offset);
  164. }
  165. else
  166. {
  167. /* In non-XIP mode, we will have to read the new sector. */
  168. ret = romfs_hwread(rm, rm->rm_buffer, sector, 1);
  169. if (ret < 0)
  170. {
  171. return (int16_t)ret;
  172. }
  173. }
  174. /* Update the cached sector number */
  175. rm->rm_cachesector = sector;
  176. }
  177. /* Return the offset */
  178. return offset & SEC_NDXMASK(rm);
  179. }
  180. /****************************************************************************
  181. * Name: romfs_followhardlinks
  182. *
  183. * Description:
  184. * Given the offset to a file header, check if the file is a hardlink.
  185. * If so, traverse the hard links until the terminal, non-linked header
  186. * so found and return that offset.
  187. *
  188. ****************************************************************************/
  189. static int romfs_followhardlinks(struct romfs_mountpt_s *rm, uint32_t offset,
  190. uint32_t *poffset)
  191. {
  192. uint32_t next;
  193. int16_t ndx;
  194. int i;
  195. /* Loop while we are redirected by hardlinks */
  196. for (i = 0; i < ROMF_MAX_LINKS; i++)
  197. {
  198. /* Read the sector containing the offset into memory */
  199. ndx = romfs_devcacheread(rm, offset);
  200. if (ndx < 0)
  201. {
  202. return ndx;
  203. }
  204. /* Check if this is a hard link */
  205. next = romfs_devread32(rm, ndx + ROMFS_FHDR_NEXT);
  206. if (!IS_HARDLINK(next))
  207. {
  208. *poffset = offset;
  209. return OK;
  210. }
  211. /* Follow the hard-link */
  212. offset = romfs_devread32(rm, ndx + ROMFS_FHDR_INFO);
  213. }
  214. return -ELOOP;
  215. }
  216. /****************************************************************************
  217. * Name: romfs_searchdir
  218. *
  219. * Description:
  220. * This is part of the romfs_finddirentry log. Search the directory
  221. * beginning at dirinfo->fr_firstoffset for entryname.
  222. *
  223. ****************************************************************************/
  224. static inline int romfs_searchdir(struct romfs_mountpt_s *rm,
  225. const char *entryname, int entrylen,
  226. struct romfs_dirinfo_s *dirinfo)
  227. {
  228. uint32_t offset;
  229. uint32_t next;
  230. int16_t ndx;
  231. int ret;
  232. /* Then loop through the current directory until the directory
  233. * with the matching name is found. Or until all of the entries
  234. * the directory have been examined.
  235. */
  236. offset = dirinfo->rd_dir.fr_firstoffset;
  237. do
  238. {
  239. /* Read the sector into memory (do this before calling
  240. * romfs_checkentry() so we won't have to read the sector
  241. * twice in the event that the offset refers to a hardlink).
  242. */
  243. ndx = romfs_devcacheread(rm, offset);
  244. if (ndx < 0)
  245. {
  246. return ndx;
  247. }
  248. /* Because everything is chunked and aligned to 16-bit boundaries,
  249. * we know that most the basic node info fits into the sector.
  250. */
  251. next = romfs_devread32(rm, ndx + ROMFS_FHDR_NEXT) & RFNEXT_OFFSETMASK;
  252. /* Check if the name this entry is a directory with the matching
  253. * name
  254. */
  255. ret = romfs_checkentry(rm, offset, entryname, entrylen, dirinfo);
  256. if (ret == OK)
  257. {
  258. /* Its a match! Return success */
  259. return OK;
  260. }
  261. /* No match... select the offset to the next entry */
  262. offset = next;
  263. }
  264. while (next != 0);
  265. /* There is nothing in this directory with that name */
  266. return -ENOENT;
  267. }
  268. /****************************************************************************
  269. * Public Functions
  270. ****************************************************************************/
  271. /****************************************************************************
  272. * Name: romfs_semtake
  273. ****************************************************************************/
  274. void romfs_semtake(struct romfs_mountpt_s *rm)
  275. {
  276. int ret;
  277. do
  278. {
  279. /* Take the semaphore (perhaps waiting) */
  280. ret = nxsem_wait(&rm->rm_sem);
  281. /* The only case that an error should occur here is if the wait was
  282. * awakened by a signal.
  283. */
  284. DEBUGASSERT(ret == OK || ret == -EINTR);
  285. }
  286. while (ret == -EINTR);
  287. }
  288. /****************************************************************************
  289. * Name: romfs_semgive
  290. ****************************************************************************/
  291. void romfs_semgive(struct romfs_mountpt_s *rm)
  292. {
  293. nxsem_post(&rm->rm_sem);
  294. }
  295. /****************************************************************************
  296. * Name: romfs_hwread
  297. *
  298. * Description: Read the specified sector into the sector buffer
  299. *
  300. ****************************************************************************/
  301. int romfs_hwread(struct romfs_mountpt_s *rm, uint8_t *buffer, uint32_t sector,
  302. unsigned int nsectors)
  303. {
  304. int ret = OK;
  305. /* Check the access mode */
  306. if (rm->rm_xipbase)
  307. {
  308. /* In XIP mode, we just copy the requested data */
  309. memcpy(buffer,
  310. rm->rm_xipbase + sector*rm->rm_hwsectorsize,
  311. nsectors*rm->rm_hwsectorsize);
  312. }
  313. else
  314. {
  315. /* In non-XIP mode, we have to read the data from the device */
  316. struct inode *inode = rm->rm_blkdriver;
  317. ssize_t nsectorsread = -ENODEV;
  318. DEBUGASSERT(inode);
  319. if (INODE_IS_MTD(inode))
  320. {
  321. nsectorsread =
  322. MTD_BREAD(inode->u.i_mtd, sector, nsectors, buffer);
  323. }
  324. else if (inode->u.i_bops->read)
  325. {
  326. nsectorsread =
  327. inode->u.i_bops->read(inode, buffer, sector, nsectors);
  328. }
  329. if (nsectorsread == (ssize_t)nsectors)
  330. {
  331. ret = OK;
  332. }
  333. else if (nsectorsread < 0)
  334. {
  335. ret = nsectorsread;
  336. }
  337. }
  338. return ret;
  339. }
  340. /****************************************************************************
  341. * Name: romfs_filecacheread
  342. *
  343. * Description:
  344. * Read the specified sector into the sector cache
  345. *
  346. ****************************************************************************/
  347. int romfs_filecacheread(struct romfs_mountpt_s *rm, struct romfs_file_s *rf,
  348. uint32_t sector)
  349. {
  350. int ret;
  351. finfo("sector: %d cached: %d sectorsize: %d XIP base: %p buffer: %p\n",
  352. sector, rf->rf_cachesector, rm->rm_hwsectorsize,
  353. rm->rm_xipbase, rf->rf_buffer);
  354. /* rf->rf_cachesector holds the current sector that is buffer in or referenced
  355. * by rf->rf_buffer. If the requested sector is the same as this sector,
  356. * then we do nothing.
  357. */
  358. if (rf->rf_cachesector != sector)
  359. {
  360. /* Check the access mode */
  361. if (rm->rm_xipbase)
  362. {
  363. /* In XIP mode, rf_buffer is just an offset pointer into the device
  364. * address space.
  365. */
  366. rf->rf_buffer = rm->rm_xipbase + sector * rm->rm_hwsectorsize;
  367. finfo("XIP buffer: %p\n", rf->rf_buffer);
  368. }
  369. else
  370. {
  371. /* In non-XIP mode, we will have to read the new sector. */
  372. finfo("Calling romfs_hwread\n");
  373. ret = romfs_hwread(rm, rf->rf_buffer, sector, 1);
  374. if (ret < 0)
  375. {
  376. ferr("ERROR: romfs_hwread failed: %d\n", ret);
  377. return ret;
  378. }
  379. }
  380. /* Update the cached sector number */
  381. rf->rf_cachesector = sector;
  382. }
  383. return OK;
  384. }
  385. /****************************************************************************
  386. * Name: romfs_hwconfigure
  387. *
  388. * Description:
  389. * This function is called as part of the ROMFS mount operation It
  390. * configures the ROMFS filestem for use on this block driver. This includes
  391. * the accounting for the geometry of the device, setting up any XIP modes
  392. * of operation, and/or allocating any cache buffers.
  393. *
  394. ****************************************************************************/
  395. int romfs_hwconfigure(struct romfs_mountpt_s *rm)
  396. {
  397. struct inode *inode = rm->rm_blkdriver;
  398. int ret;
  399. /* Get the underlying device geometry */
  400. #ifdef CONFIG_DEBUG_FEATURES
  401. if (inode == NULL)
  402. {
  403. return -ENODEV;
  404. }
  405. #endif
  406. if (INODE_IS_MTD(inode))
  407. {
  408. struct mtd_geometry_s mgeo;
  409. ret = MTD_IOCTL(inode->u.i_mtd, MTDIOC_GEOMETRY,
  410. (unsigned long)&mgeo);
  411. if (ret != OK)
  412. {
  413. return ret;
  414. }
  415. /* Save that information in the mount structure */
  416. rm->rm_hwsectorsize = mgeo.blocksize;
  417. rm->rm_hwnsectors = mgeo.neraseblocks *
  418. (mgeo.erasesize / mgeo.blocksize);
  419. }
  420. else
  421. {
  422. struct geometry geo;
  423. ret = inode->u.i_bops->geometry(inode, &geo);
  424. if (ret != OK)
  425. {
  426. return ret;
  427. }
  428. if (!geo.geo_available)
  429. {
  430. return -EBUSY;
  431. }
  432. /* Save that information in the mount structure */
  433. rm->rm_hwsectorsize = geo.geo_sectorsize;
  434. rm->rm_hwnsectors = geo.geo_nsectors;
  435. }
  436. /* Determine if block driver supports the XIP mode of operation */
  437. rm->rm_cachesector = (uint32_t)-1;
  438. if (INODE_IS_MTD(inode))
  439. {
  440. ret = MTD_IOCTL(inode->u.i_mtd, MTDIOC_XIPBASE,
  441. (unsigned long)&rm->rm_xipbase);
  442. }
  443. else if (inode->u.i_bops->ioctl != NULL)
  444. {
  445. ret = inode->u.i_bops->ioctl(inode, BIOC_XIPBASE,
  446. (unsigned long)&rm->rm_xipbase);
  447. }
  448. else
  449. {
  450. ret = -ENOTSUP;
  451. }
  452. if (ret == OK && rm->rm_xipbase)
  453. {
  454. /* Yes.. Then we will directly access the media (vs.
  455. * copying into an allocated sector buffer.
  456. */
  457. rm->rm_buffer = rm->rm_xipbase;
  458. rm->rm_cachesector = 0;
  459. return OK;
  460. }
  461. /* Allocate the device cache buffer for normal sector accesses */
  462. rm->rm_buffer = (FAR uint8_t *)kmm_malloc(rm->rm_hwsectorsize);
  463. if (!rm->rm_buffer)
  464. {
  465. return -ENOMEM;
  466. }
  467. return OK;
  468. }
  469. /****************************************************************************
  470. * Name: romfs_fsconfigure
  471. *
  472. * Description:
  473. * This function is called as part of the ROMFS mount operation It
  474. * sets up the mount structure to include configuration information contained
  475. * in the ROMFS header. This is the place where we actually determine if
  476. * the media contains a ROMFS filesystem.
  477. *
  478. ****************************************************************************/
  479. int romfs_fsconfigure(struct romfs_mountpt_s *rm)
  480. {
  481. const char *name;
  482. int16_t ndx;
  483. /* Then get information about the ROMFS filesystem on the devices managed
  484. * by this block driver. Read sector zero which contains the volume header.
  485. */
  486. ndx = romfs_devcacheread(rm, 0);
  487. if (ndx < 0)
  488. {
  489. return ndx;
  490. }
  491. /* Verify the magic number at that identifies this as a ROMFS filesystem */
  492. if (memcmp(rm->rm_buffer, ROMFS_VHDR_MAGIC, 8) != 0)
  493. {
  494. return -EINVAL;
  495. }
  496. /* Then extract the values we need from the header and return success */
  497. rm->rm_volsize = romfs_devread32(rm, ROMFS_VHDR_SIZE);
  498. /* The root directory entry begins right after the header */
  499. name = (FAR const char *)&rm->rm_buffer[ROMFS_VHDR_VOLNAME];
  500. rm->rm_rootoffset = ROMFS_ALIGNUP(ROMFS_VHDR_VOLNAME + strlen(name) + 1);
  501. /* and return success */
  502. rm->rm_mounted = true;
  503. return OK;
  504. }
  505. /****************************************************************************
  506. * Name: romfs_fileconfigure
  507. *
  508. * Description:
  509. * This function is called as part of the ROMFS file open operation It
  510. * sets up the file structure to handle buffer appropriately, depending
  511. * upon XIP mode or not.
  512. *
  513. ****************************************************************************/
  514. int romfs_fileconfigure(struct romfs_mountpt_s *rm, struct romfs_file_s *rf)
  515. {
  516. /* Check if XIP access mode is supported. If so, then we do not need
  517. * to allocate anything.
  518. */
  519. if (rm->rm_xipbase)
  520. {
  521. /* We'll put a valid address in rf_buffer just in case. */
  522. rf->rf_cachesector = 0;
  523. rf->rf_buffer = rm->rm_xipbase;
  524. }
  525. else
  526. {
  527. /* Nothing in the cache buffer */
  528. rf->rf_cachesector = (uint32_t)-1;
  529. /* Create a file buffer to support partial sector accesses */
  530. rf->rf_buffer = (FAR uint8_t *)kmm_malloc(rm->rm_hwsectorsize);
  531. if (!rf->rf_buffer)
  532. {
  533. return -ENOMEM;
  534. }
  535. }
  536. return OK;
  537. }
  538. /****************************************************************************
  539. * Name: romfs_checkmount
  540. *
  541. * Description: Check if the mountpoint is still valid.
  542. *
  543. * The caller should hold the mountpoint semaphore
  544. *
  545. ****************************************************************************/
  546. int romfs_checkmount(struct romfs_mountpt_s *rm)
  547. {
  548. struct inode *inode;
  549. struct geometry geo;
  550. int ret;
  551. /* If the fs_mounted flag is false, then we have already handled the loss
  552. * of the mount.
  553. */
  554. DEBUGASSERT(rm && rm->rm_blkdriver);
  555. if (rm->rm_mounted)
  556. {
  557. /* We still think the mount is healthy. Check an see if this is
  558. * still the case
  559. */
  560. inode = rm->rm_blkdriver;
  561. if (INODE_IS_MTD(inode))
  562. {
  563. /* It is impossible to remove MTD device */
  564. return OK;
  565. }
  566. else if (inode->u.i_bops->geometry)
  567. {
  568. ret = inode->u.i_bops->geometry(inode, &geo);
  569. if (ret == OK && geo.geo_available && !geo.geo_mediachanged)
  570. {
  571. return OK;
  572. }
  573. }
  574. /* If we get here, the mount is NOT healthy */
  575. rm->rm_mounted = false;
  576. }
  577. return -ENODEV;
  578. }
  579. /****************************************************************************
  580. * Name: romfs_finddirentry
  581. *
  582. * Description:
  583. * Given a path to something that may or may not be in the file system,
  584. * return the directory entry of the item.
  585. *
  586. ****************************************************************************/
  587. int romfs_finddirentry(struct romfs_mountpt_s *rm,
  588. struct romfs_dirinfo_s *dirinfo, const char *path)
  589. {
  590. const char *entryname;
  591. const char *terminator;
  592. int entrylen;
  593. int ret;
  594. /* Start with the first element after the root directory */
  595. dirinfo->rd_dir.fr_firstoffset = rm->rm_rootoffset;
  596. dirinfo->rd_dir.fr_curroffset = rm->rm_rootoffset;
  597. dirinfo->rd_next = RFNEXT_DIRECTORY;
  598. dirinfo->rd_size = 0;
  599. /* The root directory is a special case */
  600. if (!path || path[0] == '\0')
  601. {
  602. return OK;
  603. }
  604. /* Then loop for each directory/file component in the full path */
  605. entryname = path;
  606. terminator = NULL;
  607. for (; ; )
  608. {
  609. /* Find the start of the next path component */
  610. while (*entryname == '/') entryname++;
  611. /* Find the end of the next path component */
  612. terminator = strchr(entryname, '/');
  613. if (!terminator)
  614. {
  615. entrylen = strlen(entryname);
  616. }
  617. else
  618. {
  619. entrylen = terminator - entryname;
  620. }
  621. /* Long path segment names will be truncated to NAME_MAX */
  622. if (entrylen > NAME_MAX)
  623. {
  624. entrylen = NAME_MAX;
  625. }
  626. /* Then find the entry in the current directory with the
  627. * matching name.
  628. */
  629. ret = romfs_searchdir(rm, entryname, entrylen, dirinfo);
  630. if (ret < 0)
  631. {
  632. return ret;
  633. }
  634. /* Was that the last path component? */
  635. if (!terminator)
  636. {
  637. /* Yes.. return success */
  638. return OK;
  639. }
  640. /* No... If that was not the last path component, then it had
  641. * better have been a directory
  642. */
  643. if (!IS_DIRECTORY(dirinfo->rd_next))
  644. {
  645. return -ENOTDIR;
  646. }
  647. /* Setup to search the next directory for the next component
  648. * of the path
  649. */
  650. entryname = terminator;
  651. }
  652. return ERROR; /* Won't get here */
  653. }
  654. /****************************************************************************
  655. * Name: romfs_parsedirentry
  656. *
  657. * Description:
  658. * Return the directory entry at this offset. If rf is NULL, then the
  659. * mount device resources are used. Otherwise, file resources are used.
  660. *
  661. ****************************************************************************/
  662. int romfs_parsedirentry(struct romfs_mountpt_s *rm, uint32_t offset,
  663. uint32_t *poffset, uint32_t *pnext, uint32_t *pinfo,
  664. uint32_t *psize)
  665. {
  666. uint32_t save;
  667. uint32_t next;
  668. int16_t ndx;
  669. int ret;
  670. /* Read the sector into memory */
  671. ndx = romfs_devcacheread(rm, offset);
  672. if (ndx < 0)
  673. {
  674. return ndx;
  675. }
  676. /* Yes.. Save the first 'next' value. That has the offset needed to
  677. * traverse the parent directory. But we may need to change the type
  678. * after we follow the hard links.
  679. */
  680. save = romfs_devread32(rm, ndx + ROMFS_FHDR_NEXT);
  681. /* Traverse hardlinks as necesssary to get to the real file header */
  682. ret = romfs_followhardlinks(rm, offset, poffset);
  683. if (ret < 0)
  684. {
  685. return ret;
  686. }
  687. /* Because everything is chunked and aligned to 16-bit boundaries,
  688. * we know that most the basic node info fits into the sector. The
  689. * associated name may not, however.
  690. */
  691. next = romfs_devread32(rm, ndx + ROMFS_FHDR_NEXT);
  692. *pnext = (save & RFNEXT_OFFSETMASK) | (next & RFNEXT_ALLMODEMASK);
  693. *pinfo = romfs_devread32(rm, ndx + ROMFS_FHDR_INFO);
  694. *psize = romfs_devread32(rm, ndx + ROMFS_FHDR_SIZE);
  695. return OK;
  696. }
  697. /****************************************************************************
  698. * Name: romfs_parsefilename
  699. *
  700. * Description:
  701. * Return the filename from directory entry at this offset
  702. *
  703. ****************************************************************************/
  704. int romfs_parsefilename(struct romfs_mountpt_s *rm, uint32_t offset,
  705. char *pname)
  706. {
  707. int16_t ndx;
  708. uint16_t namelen;
  709. uint16_t chunklen;
  710. bool done;
  711. /* Loop until the whole name is obtained or until NAME_MAX characters
  712. * of the name have been parsed.
  713. */
  714. offset += ROMFS_FHDR_NAME;
  715. for (namelen = 0, done = false; namelen < NAME_MAX && !done; )
  716. {
  717. /* Read the sector into memory */
  718. ndx = romfs_devcacheread(rm, offset + namelen);
  719. if (ndx < 0)
  720. {
  721. return ndx;
  722. }
  723. /* Is the name terminated in this 16-byte block */
  724. if (rm->rm_buffer[ndx + 15] == '\0')
  725. {
  726. /* Yes.. then this chunk is less than 16 */
  727. chunklen = strlen((FAR char *)&rm->rm_buffer[ndx]);
  728. done = true;
  729. }
  730. else
  731. {
  732. /* No.. then this chunk is 16 bytes in length */
  733. chunklen = 16;
  734. }
  735. /* Check if we would exceed the NAME_MAX */
  736. if (namelen + chunklen > NAME_MAX)
  737. {
  738. chunklen = NAME_MAX - namelen;
  739. done = true;
  740. }
  741. /* Copy the chunk */
  742. memcpy(&pname[namelen], &rm->rm_buffer[ndx], chunklen);
  743. namelen += chunklen;
  744. }
  745. /* Terminate the name (NAME_MAX+1 chars total) and return success */
  746. pname[namelen] = '\0';
  747. return OK;
  748. }
  749. /****************************************************************************
  750. * Name: romfs_datastart
  751. *
  752. * Description:
  753. * Given the offset to a file header, return the offset to the start of
  754. * the file data
  755. *
  756. ****************************************************************************/
  757. int romfs_datastart(struct romfs_mountpt_s *rm, uint32_t offset,
  758. uint32_t *start)
  759. {
  760. int16_t ndx;
  761. int ret;
  762. /* Traverse hardlinks as necesssary to get to the real file header */
  763. ret = romfs_followhardlinks(rm, offset, &offset);
  764. if (ret < 0)
  765. {
  766. return ret;
  767. }
  768. /* Loop until the header size is obtained. */
  769. offset += ROMFS_FHDR_NAME;
  770. for (; ; )
  771. {
  772. /* Read the sector into memory */
  773. ndx = romfs_devcacheread(rm, offset);
  774. if (ndx < 0)
  775. {
  776. return ndx;
  777. }
  778. /* Get the offset to the next chunk */
  779. offset += 16;
  780. if (offset >= rm->rm_volsize)
  781. {
  782. return -EIO;
  783. }
  784. /* Is the name terminated in this 16-byte block */
  785. if (rm->rm_buffer[ndx + 15] == '\0')
  786. {
  787. /* Yes.. then the data starts at the next chunk */
  788. *start = offset;
  789. return OK;
  790. }
  791. }
  792. return -EINVAL; /* Won't get here */
  793. }