nxffs_dump.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /****************************************************************************
  2. * fs/nxffs/nxffs_dump.c
  3. *
  4. * Copyright (C) 2011, 2013 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 <string.h>
  42. #include <debug.h>
  43. #include <errno.h>
  44. #include <crc32.h>
  45. #include <nuttx/kmalloc.h>
  46. #include <nuttx/fs/ioctl.h>
  47. #include <nuttx/mtd/mtd.h>
  48. #include "nxffs.h"
  49. /****************************************************************************
  50. * Pre-processor Definitions
  51. ****************************************************************************/
  52. /****************************************************************************
  53. * Private Types
  54. ****************************************************************************/
  55. struct nxffs_blkinfo_s
  56. {
  57. struct mtd_geometry_s geo;
  58. FAR uint8_t *buffer;
  59. off_t nblocks;
  60. off_t block;
  61. off_t offset;
  62. #if defined(CONFIG_DEBUG_FEATURES) && defined(CONFIG_DEBUG_FS)
  63. bool verbose;
  64. #endif
  65. };
  66. /****************************************************************************
  67. * Private Data
  68. ****************************************************************************/
  69. #if defined(CONFIG_DEBUG_FEATURES) && defined(CONFIG_DEBUG_FS)
  70. static const char g_hdrformat[] = " BLOCK:OFFS TYPE STATE LENGTH\n";
  71. static const char g_format[] = " %5d:%-5d %s %s %5d\n";
  72. #endif
  73. /****************************************************************************
  74. * Private Functions
  75. ****************************************************************************/
  76. /****************************************************************************
  77. * Name: nxffs_analyzeinode
  78. *
  79. * Description:
  80. * Analyze one candidate inode found in the block.
  81. *
  82. ****************************************************************************/
  83. #if defined(CONFIG_DEBUG_FEATURES) && defined(CONFIG_DEBUG_FS)
  84. static inline ssize_t nxffs_analyzeinode(FAR struct nxffs_blkinfo_s *blkinfo,
  85. int offset)
  86. {
  87. FAR struct nxffs_inode_s inode;
  88. off_t nextblock;
  89. uint8_t state;
  90. uint32_t noffs;
  91. uint32_t doffs;
  92. #if 0
  93. uint32_t utc;
  94. #endif
  95. uint32_t ecrc;
  96. uint32_t datlen;
  97. uint32_t crc;
  98. size_t spaceleft;
  99. /* Verify that there is space for an inode header remaining in the block */
  100. if (offset + SIZEOF_NXFFS_INODE_HDR > blkinfo->geo.blocksize)
  101. {
  102. /* No.. then this can't be an inode header */
  103. return ERROR;
  104. }
  105. /* Unpack the header */
  106. memcpy(&inode, &blkinfo->buffer[offset], SIZEOF_NXFFS_INODE_HDR);
  107. noffs = nxffs_rdle32(inode.noffs);
  108. doffs = nxffs_rdle32(inode.doffs);
  109. #if 0
  110. utc = nxffs_rdle32(inode.utc);
  111. #endif
  112. ecrc = nxffs_rdle32(inode.crc);
  113. datlen = nxffs_rdle32(inode.datlen);
  114. /* Misc. sanity checks */
  115. if (noffs < blkinfo->offset + offset + SIZEOF_NXFFS_BLOCK_HDR)
  116. {
  117. /* The name begins before the inode header. This can't can't be
  118. * a real inode header (or it is a corrupted one).
  119. */
  120. return ERROR;
  121. }
  122. /* Can we verify the inode? We need to have the inode name in the same
  123. * block to do that (or get access to the next block)
  124. */
  125. if (doffs < blkinfo->offset + offset + SIZEOF_NXFFS_BLOCK_HDR)
  126. {
  127. /* The first data block begins before the inode header. This can't can't
  128. * be a real inode header (or it is a corrupted one).
  129. */
  130. return ERROR;
  131. }
  132. spaceleft = (blkinfo->nblocks - blkinfo->block) * blkinfo->geo.blocksize;
  133. spaceleft -= (offset + SIZEOF_NXFFS_BLOCK_HDR);
  134. if (datlen > spaceleft)
  135. {
  136. /* The data length is greater than what would fit in the rest of FLASH
  137. * (even ignoring block and data header sizes.
  138. */
  139. return ERROR;
  140. }
  141. /* The name begins after the inode header. Does it begin in this block? */
  142. nextblock = blkinfo->offset + blkinfo->geo.blocksize;
  143. if (noffs > nextblock)
  144. {
  145. /* Note than we cannot verify the inode header */
  146. if (blkinfo->verbose)
  147. {
  148. syslog(LOG_NOTICE, g_format,
  149. blkinfo->block, offset, "INODE", "UNVERFD", datlen);
  150. }
  151. return ERROR;
  152. }
  153. /* The name begins in this block. Does it also end in this block? */
  154. if (noffs + inode.namlen > nextblock)
  155. {
  156. /* No.. Assume that this is not an inode. */
  157. return ERROR;
  158. }
  159. /* Calculate the CRC */
  160. state = inode.state;
  161. inode.state = CONFIG_NXFFS_ERASEDSTATE;
  162. nxffs_wrle32(inode.crc, 0);
  163. crc = crc32((FAR const uint8_t *)&inode, SIZEOF_NXFFS_INODE_HDR);
  164. crc = crc32part(&blkinfo->buffer[noffs - blkinfo->offset], inode.namlen, crc);
  165. if (crc != ecrc)
  166. {
  167. syslog(LOG_NOTICE, g_format,
  168. blkinfo->block, offset, "INODE", "CRC BAD", datlen);
  169. return ERROR;
  170. }
  171. /* If must be a good header */
  172. if (state == INODE_STATE_FILE)
  173. {
  174. if (blkinfo->verbose)
  175. {
  176. syslog(LOG_NOTICE, g_format,
  177. blkinfo->block, offset, "INODE", "OK ", datlen);
  178. }
  179. }
  180. else if (state == INODE_STATE_DELETED)
  181. {
  182. if (blkinfo->verbose)
  183. {
  184. syslog(LOG_NOTICE, g_format,
  185. blkinfo->block, offset, "INODE", "DELETED", datlen);
  186. }
  187. }
  188. else
  189. {
  190. syslog(LOG_NOTICE, g_format,
  191. blkinfo->block, offset, "INODE", "CORRUPT", datlen);
  192. }
  193. /* Return the block-relative offset to the next byte after the inode name */
  194. return noffs + inode.namlen - offset - blkinfo->offset;
  195. }
  196. #endif
  197. /****************************************************************************
  198. * Name: nxffs_analyzedata
  199. *
  200. * Description:
  201. * Analyze one candidate inode found in the block.
  202. *
  203. ****************************************************************************/
  204. #if defined(CONFIG_DEBUG_FEATURES) && defined(CONFIG_DEBUG_FS)
  205. static inline ssize_t nxffs_analyzedata(FAR struct nxffs_blkinfo_s *blkinfo,
  206. int offset)
  207. {
  208. struct nxffs_data_s dathdr;
  209. uint32_t ecrc;
  210. uint16_t datlen;
  211. uint32_t crc;
  212. /* Copy and unpack the data block header */
  213. memcpy(&dathdr, &blkinfo->buffer[offset], SIZEOF_NXFFS_DATA_HDR);
  214. ecrc = nxffs_rdle32(dathdr.crc);
  215. datlen = nxffs_rdle16(dathdr.datlen);
  216. /* Sanity checks */
  217. if (offset + SIZEOF_NXFFS_DATA_HDR + datlen > blkinfo->geo.blocksize)
  218. {
  219. /* Data does not fit in within the block, this can't be a data block */
  220. return ERROR;
  221. }
  222. /* Calculate the CRC */
  223. nxffs_wrle32(dathdr.crc, 0);
  224. crc = crc32((FAR const uint8_t *)&dathdr, SIZEOF_NXFFS_DATA_HDR);
  225. crc = crc32part(&blkinfo->buffer[offset + SIZEOF_NXFFS_DATA_HDR], datlen, crc);
  226. if (crc != ecrc)
  227. {
  228. syslog(LOG_NOTICE, g_format,
  229. blkinfo->block, offset, "DATA ", "CRC BAD", datlen);
  230. return ERROR;
  231. }
  232. /* If must be a good header */
  233. if (blkinfo->verbose)
  234. {
  235. syslog(LOG_NOTICE, g_format,
  236. blkinfo->block, offset, "DATA ", "OK ", datlen);
  237. }
  238. return SIZEOF_NXFFS_DATA_HDR + datlen;
  239. }
  240. #endif
  241. /****************************************************************************
  242. * Name: nxffs_analyze
  243. *
  244. * Description:
  245. * Analyze the content of one block.
  246. *
  247. ****************************************************************************/
  248. #if defined(CONFIG_DEBUG_FEATURES) && defined(CONFIG_DEBUG_FS)
  249. static inline void nxffs_analyze(FAR struct nxffs_blkinfo_s *blkinfo)
  250. {
  251. FAR struct nxffs_block_s *blkhdr;
  252. ssize_t nbytes;
  253. int hdrndx;
  254. int datndx;
  255. int inndx;
  256. int i;
  257. /* Verify that there is a header on the block */
  258. blkhdr = (FAR struct nxffs_block_s *)blkinfo->buffer;
  259. if (memcmp(blkhdr->magic, g_blockmagic, NXFFS_MAGICSIZE) != 0)
  260. {
  261. syslog(LOG_NOTICE, g_format, blkinfo->block, 0, "BLOCK", "NO FRMT",
  262. blkinfo->geo.blocksize);
  263. }
  264. else if (blkhdr->state == BLOCK_STATE_GOOD)
  265. {
  266. size_t datsize = blkinfo->geo.blocksize - SIZEOF_NXFFS_BLOCK_HDR;
  267. size_t nerased = nxffs_erased(blkinfo->buffer + SIZEOF_NXFFS_BLOCK_HDR,
  268. datsize);
  269. if (nerased == datsize)
  270. {
  271. if (blkinfo->verbose)
  272. {
  273. syslog(LOG_NOTICE, g_format, blkinfo->block, 0, "BLOCK", "ERASED ",
  274. blkinfo->geo.blocksize);
  275. }
  276. return;
  277. }
  278. #if 0 /* Too much output, to little information */
  279. else
  280. {
  281. syslog(LOG_NOTICE, g_format, blkinfo->block, 0, "BLOCK", "IN USE ",
  282. blkinfo->geo.blocksize);
  283. }
  284. #endif
  285. }
  286. else if (blkhdr->state == BLOCK_STATE_BAD)
  287. {
  288. syslog(LOG_NOTICE, g_format, blkinfo->block, 0, "BLOCK", "BAD ",
  289. blkinfo->geo.blocksize);
  290. }
  291. else
  292. {
  293. syslog(LOG_NOTICE, g_format, blkinfo->block, 0, "BLOCK", "CORRUPT",
  294. blkinfo->geo.blocksize);
  295. }
  296. /* Search for Inode and data block headers. */
  297. inndx = 0;
  298. datndx = 0;
  299. for (i = SIZEOF_NXFFS_BLOCK_HDR; i < blkinfo->geo.blocksize; i++)
  300. {
  301. uint8_t ch = blkinfo->buffer[i];
  302. if (ch == g_inodemagic[inndx])
  303. {
  304. inndx++;
  305. datndx = 0;
  306. if (inndx == NXFFS_MAGICSIZE)
  307. {
  308. hdrndx = i - NXFFS_MAGICSIZE + 1;
  309. nbytes = nxffs_analyzeinode(blkinfo, hdrndx);
  310. if (nbytes > 0)
  311. {
  312. i = hdrndx + nbytes - 1;
  313. }
  314. inndx = 0;
  315. }
  316. }
  317. else if (ch == g_datamagic[datndx])
  318. {
  319. datndx++;
  320. inndx = 0;
  321. if (datndx == NXFFS_MAGICSIZE)
  322. {
  323. hdrndx = i - NXFFS_MAGICSIZE + 1;
  324. nbytes = nxffs_analyzedata(blkinfo, hdrndx);
  325. if (nbytes > 0)
  326. {
  327. i = hdrndx + nbytes - 1;
  328. }
  329. datndx = 0;
  330. }
  331. }
  332. }
  333. }
  334. #endif
  335. /****************************************************************************
  336. * Public Functions
  337. ****************************************************************************/
  338. /****************************************************************************
  339. * Name: nxffs_dump
  340. *
  341. * Description:
  342. * Dump a summary of the contents of an NXFFS file system. CONFIG_DEBUG_FEATURES
  343. * and CONFIG_DEBUG_FS must be enabled for this function to do anything.
  344. *
  345. * Input Parameters:
  346. * mtd - The MTD device that provides the interface to NXFFS-formatted
  347. * media.
  348. * verbose - FALSE: only show errors
  349. *
  350. * Returned Value:
  351. * Zero is returned on success. Otherwise, a negated errno value is
  352. * returned to indicate the nature of the failure.
  353. *
  354. ****************************************************************************/
  355. int nxffs_dump(FAR struct mtd_dev_s *mtd, bool verbose)
  356. {
  357. #if defined(CONFIG_DEBUG_FEATURES) && defined(CONFIG_DEBUG_FS)
  358. struct nxffs_blkinfo_s blkinfo;
  359. int ret;
  360. /* Get the volume geometry. (casting to uintptr_t first eliminates
  361. * complaints on some architectures where the sizeof long is different
  362. * from the size of a pointer).
  363. */
  364. memset(&blkinfo, 0, sizeof(struct nxffs_blkinfo_s));
  365. ret = MTD_IOCTL(mtd, MTDIOC_GEOMETRY, (unsigned long)((uintptr_t)&blkinfo.geo));
  366. if (ret < 0)
  367. {
  368. ferr("ERROR: MTD ioctl(MTDIOC_GEOMETRY) failed: %d\n", -ret);
  369. return ret;
  370. }
  371. /* Save the verbose output indication */
  372. blkinfo.verbose = verbose;
  373. /* Allocate a buffer to hold one block */
  374. blkinfo.buffer = (FAR uint8_t *)kmm_malloc(blkinfo.geo.blocksize);
  375. if (!blkinfo.buffer)
  376. {
  377. ferr("ERROR: Failed to allocate block cache\n");
  378. return -ENOMEM;
  379. }
  380. /* Now read every block on the device */
  381. syslog(LOG_NOTICE, "NXFFS Dump:\n");
  382. syslog(LOG_NOTICE, g_hdrformat);
  383. blkinfo.nblocks = blkinfo.geo.erasesize * blkinfo.geo.neraseblocks /
  384. blkinfo.geo.blocksize;
  385. for (blkinfo.block = 0, blkinfo.offset = 0;
  386. blkinfo.block < blkinfo.nblocks;
  387. blkinfo.block++, blkinfo.offset += blkinfo.geo.blocksize)
  388. {
  389. /* Read the next block */
  390. ret = MTD_BREAD(mtd, blkinfo.block, 1, blkinfo.buffer);
  391. if (ret < 0)
  392. {
  393. #ifndef CONFIG_NXFFS_NAND
  394. /* Read errors are fatal */
  395. ferr("ERROR: Failed to read block %d\n", blkinfo.block);
  396. kmm_free(blkinfo.buffer);
  397. return ret;
  398. #else
  399. /* A read error is probably fatal on all media but NAND.
  400. * On NAND, the read error probably just signifies a block
  401. * with an uncorrectable ECC failure. So, to handle NAND,
  402. * just report the read error and continue.
  403. */
  404. syslog(LOG_NOTICE, g_format, blkinfo.block, 0, "BLOCK", "RD FAIL",
  405. blkinfo.geo.blocksize);
  406. #endif
  407. }
  408. else
  409. {
  410. /* Analyze the block that we just read */
  411. nxffs_analyze(&blkinfo);
  412. }
  413. }
  414. syslog(LOG_NOTICE, "%d blocks analyzed\n", blkinfo.nblocks);
  415. kmm_free(blkinfo.buffer);
  416. return OK;
  417. #else
  418. return -ENOSYS;
  419. #endif
  420. }