2011-05-01 16:48:27 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* fs/nxffs/nxffs_dump.c
|
|
|
|
*
|
2021-02-03 14:47:23 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2011-05-01 16:48:27 +02:00
|
|
|
*
|
2021-02-03 14:47:23 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-05-01 16:48:27 +02:00
|
|
|
*
|
2021-02-03 14:47:23 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2011-05-01 16:48:27 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2022-08-12 08:13:41 +02:00
|
|
|
#include <nuttx/crc32.h>
|
2011-05-01 16:48:27 +02:00
|
|
|
#include <nuttx/kmalloc.h>
|
2012-03-21 19:01:07 +01:00
|
|
|
#include <nuttx/fs/ioctl.h>
|
2013-11-15 18:22:23 +01:00
|
|
|
#include <nuttx/mtd/mtd.h>
|
2011-05-01 16:48:27 +02:00
|
|
|
|
|
|
|
#include "nxffs.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-05-01 20:16:03 +02:00
|
|
|
struct nxffs_blkinfo_s
|
|
|
|
{
|
|
|
|
struct mtd_geometry_s geo;
|
|
|
|
FAR uint8_t *buffer;
|
|
|
|
off_t nblocks;
|
|
|
|
off_t block;
|
|
|
|
off_t offset;
|
2016-06-11 22:14:08 +02:00
|
|
|
#if defined(CONFIG_DEBUG_FEATURES) && defined(CONFIG_DEBUG_FS)
|
2011-05-04 23:59:23 +02:00
|
|
|
bool verbose;
|
2014-11-25 20:46:14 +01:00
|
|
|
#endif
|
2011-05-01 20:16:03 +02:00
|
|
|
};
|
|
|
|
|
2011-05-01 16:48:27 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-05-25 20:13:27 +02:00
|
|
|
#if defined(CONFIG_DEBUG_FEATURES) && defined(CONFIG_DEBUG_FS)
|
2011-05-02 20:38:11 +02:00
|
|
|
static const char g_hdrformat[] = " BLOCK:OFFS TYPE STATE LENGTH\n";
|
2024-03-07 00:36:58 +01:00
|
|
|
static const char g_format[] = " %5"PRIi32":%-5d %s %s %5"PRIu32"\n";
|
2019-05-25 20:13:27 +02:00
|
|
|
#endif
|
2011-05-02 20:38:11 +02:00
|
|
|
|
2011-05-01 16:48:27 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-05-01 20:16:03 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxffs_analyzeinode
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Analyze one candidate inode found in the block.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#if defined(CONFIG_DEBUG_FEATURES) && defined(CONFIG_DEBUG_FS)
|
2011-05-01 20:16:03 +02:00
|
|
|
static inline ssize_t nxffs_analyzeinode(FAR struct nxffs_blkinfo_s *blkinfo,
|
|
|
|
int offset)
|
|
|
|
{
|
|
|
|
FAR struct nxffs_inode_s inode;
|
|
|
|
off_t nextblock;
|
|
|
|
uint8_t state;
|
|
|
|
uint32_t noffs;
|
|
|
|
uint32_t doffs;
|
2019-03-01 22:00:00 +01:00
|
|
|
#if 0
|
|
|
|
uint32_t utc;
|
|
|
|
#endif
|
2011-05-01 20:16:03 +02:00
|
|
|
uint32_t ecrc;
|
|
|
|
uint32_t datlen;
|
|
|
|
uint32_t crc;
|
|
|
|
size_t spaceleft;
|
|
|
|
|
|
|
|
/* Verify that there is space for an inode header remaining in the block */
|
|
|
|
|
|
|
|
if (offset + SIZEOF_NXFFS_INODE_HDR > blkinfo->geo.blocksize)
|
|
|
|
{
|
|
|
|
/* No.. then this can't be an inode header */
|
|
|
|
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Unpack the header */
|
|
|
|
|
|
|
|
memcpy(&inode, &blkinfo->buffer[offset], SIZEOF_NXFFS_INODE_HDR);
|
|
|
|
noffs = nxffs_rdle32(inode.noffs);
|
|
|
|
doffs = nxffs_rdle32(inode.doffs);
|
2019-03-01 22:00:00 +01:00
|
|
|
#if 0
|
|
|
|
utc = nxffs_rdle32(inode.utc);
|
|
|
|
#endif
|
2011-05-01 20:16:03 +02:00
|
|
|
ecrc = nxffs_rdle32(inode.crc);
|
|
|
|
datlen = nxffs_rdle32(inode.datlen);
|
|
|
|
|
|
|
|
/* Misc. sanity checks */
|
|
|
|
|
|
|
|
if (noffs < blkinfo->offset + offset + SIZEOF_NXFFS_BLOCK_HDR)
|
|
|
|
{
|
|
|
|
/* The name begins before the inode header. This can't can't be
|
|
|
|
* a real inode header (or it is a corrupted one).
|
|
|
|
*/
|
|
|
|
|
|
|
|
return ERROR;
|
|
|
|
}
|
2013-12-02 03:17:09 +01:00
|
|
|
|
2011-05-01 20:16:03 +02:00
|
|
|
/* Can we verify the inode? We need to have the inode name in the same
|
|
|
|
* block to do that (or get access to the next block)
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (doffs < blkinfo->offset + offset + SIZEOF_NXFFS_BLOCK_HDR)
|
|
|
|
{
|
2021-02-04 13:56:38 +01:00
|
|
|
/* The first data block begins before the inode header. This can't
|
|
|
|
* can't be a real inode header (or it is a corrupted one).
|
2011-05-01 20:16:03 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
spaceleft = (blkinfo->nblocks - blkinfo->block) * blkinfo->geo.blocksize;
|
|
|
|
spaceleft -= (offset + SIZEOF_NXFFS_BLOCK_HDR);
|
|
|
|
if (datlen > spaceleft)
|
|
|
|
{
|
|
|
|
/* The data length is greater than what would fit in the rest of FLASH
|
|
|
|
* (even ignoring block and data header sizes.
|
|
|
|
*/
|
2011-05-01 20:16:03 +02:00
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
2011-05-01 20:16:03 +02:00
|
|
|
|
|
|
|
/* The name begins after the inode header. Does it begin in this block? */
|
|
|
|
|
|
|
|
nextblock = blkinfo->offset + blkinfo->geo.blocksize;
|
|
|
|
if (noffs > nextblock)
|
|
|
|
{
|
2013-12-02 03:17:09 +01:00
|
|
|
/* Note than we cannot verify the inode header */
|
2011-05-01 20:16:03 +02:00
|
|
|
|
2011-05-04 23:59:23 +02:00
|
|
|
if (blkinfo->verbose)
|
|
|
|
{
|
2014-11-12 18:38:48 +01:00
|
|
|
syslog(LOG_NOTICE, g_format,
|
2014-11-12 14:15:13 +01:00
|
|
|
blkinfo->block, offset, "INODE", "UNVERFD", datlen);
|
2011-05-04 23:59:23 +02:00
|
|
|
}
|
2013-09-12 20:25:03 +02:00
|
|
|
|
2011-05-01 20:16:03 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The name begins in this block. Does it also end in this block? */
|
|
|
|
|
|
|
|
if (noffs + inode.namlen > nextblock)
|
|
|
|
{
|
|
|
|
/* No.. Assume that this is not an inode. */
|
|
|
|
|
2019-10-27 18:48:14 +01:00
|
|
|
return ERROR;
|
2011-05-01 20:16:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate the CRC */
|
|
|
|
|
|
|
|
state = inode.state;
|
|
|
|
inode.state = CONFIG_NXFFS_ERASEDSTATE;
|
|
|
|
nxffs_wrle32(inode.crc, 0);
|
|
|
|
|
|
|
|
crc = crc32((FAR const uint8_t *)&inode, SIZEOF_NXFFS_INODE_HDR);
|
2021-02-04 13:56:38 +01:00
|
|
|
crc = crc32part(&blkinfo->buffer[noffs - blkinfo->offset],
|
|
|
|
inode.namlen, crc);
|
2011-05-01 20:16:03 +02:00
|
|
|
|
|
|
|
if (crc != ecrc)
|
2019-10-27 18:48:14 +01:00
|
|
|
{
|
2014-11-12 18:38:48 +01:00
|
|
|
syslog(LOG_NOTICE, g_format,
|
2014-11-12 14:15:13 +01:00
|
|
|
blkinfo->block, offset, "INODE", "CRC BAD", datlen);
|
2011-05-01 20:16:03 +02:00
|
|
|
return ERROR;
|
2019-10-27 18:48:14 +01:00
|
|
|
}
|
2011-05-01 20:16:03 +02:00
|
|
|
|
|
|
|
/* If must be a good header */
|
|
|
|
|
2011-05-02 03:30:30 +02:00
|
|
|
if (state == INODE_STATE_FILE)
|
2011-05-01 20:16:03 +02:00
|
|
|
{
|
2011-05-04 23:59:23 +02:00
|
|
|
if (blkinfo->verbose)
|
|
|
|
{
|
2014-11-12 18:38:48 +01:00
|
|
|
syslog(LOG_NOTICE, g_format,
|
2014-11-12 14:15:13 +01:00
|
|
|
blkinfo->block, offset, "INODE", "OK ", datlen);
|
2011-05-04 23:59:23 +02:00
|
|
|
}
|
2011-05-01 20:16:03 +02:00
|
|
|
}
|
2011-05-02 03:30:30 +02:00
|
|
|
else if (state == INODE_STATE_DELETED)
|
2011-05-01 20:16:03 +02:00
|
|
|
{
|
2011-05-04 23:59:23 +02:00
|
|
|
if (blkinfo->verbose)
|
|
|
|
{
|
2014-11-12 18:38:48 +01:00
|
|
|
syslog(LOG_NOTICE, g_format,
|
2014-11-12 14:15:13 +01:00
|
|
|
blkinfo->block, offset, "INODE", "DELETED", datlen);
|
2011-05-04 23:59:23 +02:00
|
|
|
}
|
2011-05-01 20:16:03 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-11-12 18:38:48 +01:00
|
|
|
syslog(LOG_NOTICE, g_format,
|
2014-11-12 14:15:13 +01:00
|
|
|
blkinfo->block, offset, "INODE", "CORRUPT", datlen);
|
2011-05-01 20:16:03 +02:00
|
|
|
}
|
2011-05-04 20:49:53 +02:00
|
|
|
|
|
|
|
/* Return the block-relative offset to the next byte after the inode name */
|
|
|
|
|
|
|
|
return noffs + inode.namlen - offset - blkinfo->offset;
|
2011-05-01 20:16:03 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxffs_analyzedata
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Analyze one candidate inode found in the block.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#if defined(CONFIG_DEBUG_FEATURES) && defined(CONFIG_DEBUG_FS)
|
2011-05-01 20:16:03 +02:00
|
|
|
static inline ssize_t nxffs_analyzedata(FAR struct nxffs_blkinfo_s *blkinfo,
|
|
|
|
int offset)
|
|
|
|
{
|
|
|
|
struct nxffs_data_s dathdr;
|
|
|
|
uint32_t ecrc;
|
|
|
|
uint16_t datlen;
|
|
|
|
uint32_t crc;
|
|
|
|
|
|
|
|
/* Copy and unpack the data block header */
|
|
|
|
|
|
|
|
memcpy(&dathdr, &blkinfo->buffer[offset], SIZEOF_NXFFS_DATA_HDR);
|
|
|
|
ecrc = nxffs_rdle32(dathdr.crc);
|
|
|
|
datlen = nxffs_rdle16(dathdr.datlen);
|
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
|
|
|
|
if (offset + SIZEOF_NXFFS_DATA_HDR + datlen > blkinfo->geo.blocksize)
|
|
|
|
{
|
|
|
|
/* Data does not fit in within the block, this can't be a data block */
|
|
|
|
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate the CRC */
|
|
|
|
|
|
|
|
nxffs_wrle32(dathdr.crc, 0);
|
|
|
|
|
|
|
|
crc = crc32((FAR const uint8_t *)&dathdr, SIZEOF_NXFFS_DATA_HDR);
|
2021-02-04 13:56:38 +01:00
|
|
|
crc = crc32part(&blkinfo->buffer[offset + SIZEOF_NXFFS_DATA_HDR],
|
|
|
|
datlen, crc);
|
2011-05-01 20:16:03 +02:00
|
|
|
|
|
|
|
if (crc != ecrc)
|
2019-10-27 18:48:14 +01:00
|
|
|
{
|
2014-11-12 18:38:48 +01:00
|
|
|
syslog(LOG_NOTICE, g_format,
|
2024-03-07 00:36:58 +01:00
|
|
|
blkinfo->block, offset, "DATA ", "CRC BAD",
|
|
|
|
(long unsigned int)datlen);
|
2011-05-01 20:16:03 +02:00
|
|
|
return ERROR;
|
2019-10-27 18:48:14 +01:00
|
|
|
}
|
2011-05-01 20:16:03 +02:00
|
|
|
|
|
|
|
/* If must be a good header */
|
|
|
|
|
2011-05-04 23:59:23 +02:00
|
|
|
if (blkinfo->verbose)
|
|
|
|
{
|
2014-11-12 18:38:48 +01:00
|
|
|
syslog(LOG_NOTICE, g_format,
|
2024-03-07 00:36:58 +01:00
|
|
|
blkinfo->block, offset, "DATA ", "OK ",
|
|
|
|
(long unsigned int)datlen);
|
2011-05-04 23:59:23 +02:00
|
|
|
}
|
2013-12-02 03:17:09 +01:00
|
|
|
|
2011-05-01 20:16:03 +02:00
|
|
|
return SIZEOF_NXFFS_DATA_HDR + datlen;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-05-01 16:48:27 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxffs_analyze
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Analyze the content of one block.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#if defined(CONFIG_DEBUG_FEATURES) && defined(CONFIG_DEBUG_FS)
|
2011-05-01 20:16:03 +02:00
|
|
|
static inline void nxffs_analyze(FAR struct nxffs_blkinfo_s *blkinfo)
|
2011-05-01 16:48:27 +02:00
|
|
|
{
|
|
|
|
FAR struct nxffs_block_s *blkhdr;
|
2011-05-01 20:16:03 +02:00
|
|
|
ssize_t nbytes;
|
|
|
|
int hdrndx;
|
|
|
|
int datndx;
|
|
|
|
int inndx;
|
|
|
|
int i;
|
2011-05-01 16:48:27 +02:00
|
|
|
|
|
|
|
/* Verify that there is a header on the block */
|
|
|
|
|
2011-05-01 20:16:03 +02:00
|
|
|
blkhdr = (FAR struct nxffs_block_s *)blkinfo->buffer;
|
2011-05-01 16:48:27 +02:00
|
|
|
if (memcmp(blkhdr->magic, g_blockmagic, NXFFS_MAGICSIZE) != 0)
|
|
|
|
{
|
2014-11-12 18:38:48 +01:00
|
|
|
syslog(LOG_NOTICE, g_format, blkinfo->block, 0, "BLOCK", "NO FRMT",
|
2014-11-12 14:15:13 +01:00
|
|
|
blkinfo->geo.blocksize);
|
2011-05-02 20:38:11 +02:00
|
|
|
}
|
|
|
|
else if (blkhdr->state == BLOCK_STATE_GOOD)
|
|
|
|
{
|
|
|
|
size_t datsize = blkinfo->geo.blocksize - SIZEOF_NXFFS_BLOCK_HDR;
|
2019-10-27 18:48:14 +01:00
|
|
|
size_t nerased = nxffs_erased(blkinfo->buffer + SIZEOF_NXFFS_BLOCK_HDR,
|
|
|
|
datsize);
|
2011-05-02 20:38:11 +02:00
|
|
|
if (nerased == datsize)
|
|
|
|
{
|
2011-05-04 23:59:23 +02:00
|
|
|
if (blkinfo->verbose)
|
|
|
|
{
|
2021-02-04 13:56:38 +01:00
|
|
|
syslog(LOG_NOTICE,
|
|
|
|
g_format,
|
|
|
|
blkinfo->block,
|
|
|
|
0,
|
|
|
|
"BLOCK",
|
|
|
|
"ERASED ",
|
2014-11-12 14:15:13 +01:00
|
|
|
blkinfo->geo.blocksize);
|
2011-05-04 23:59:23 +02:00
|
|
|
}
|
2013-12-02 03:17:09 +01:00
|
|
|
|
2011-05-02 20:38:11 +02:00
|
|
|
return;
|
|
|
|
}
|
2011-05-04 16:20:52 +02:00
|
|
|
#if 0 /* Too much output, to little information */
|
2011-05-02 20:38:11 +02:00
|
|
|
else
|
|
|
|
{
|
2014-11-12 18:38:48 +01:00
|
|
|
syslog(LOG_NOTICE, g_format, blkinfo->block, 0, "BLOCK", "IN USE ",
|
2014-11-12 14:15:13 +01:00
|
|
|
blkinfo->geo.blocksize);
|
2011-05-02 20:38:11 +02:00
|
|
|
}
|
2011-05-04 16:20:52 +02:00
|
|
|
#endif
|
2011-05-01 16:48:27 +02:00
|
|
|
}
|
2011-05-01 20:16:03 +02:00
|
|
|
else if (blkhdr->state == BLOCK_STATE_BAD)
|
2011-05-01 16:48:27 +02:00
|
|
|
{
|
2014-11-12 18:38:48 +01:00
|
|
|
syslog(LOG_NOTICE, g_format, blkinfo->block, 0, "BLOCK", "BAD ",
|
2014-11-12 14:15:13 +01:00
|
|
|
blkinfo->geo.blocksize);
|
2011-05-01 16:48:27 +02:00
|
|
|
}
|
2011-05-02 20:38:11 +02:00
|
|
|
else
|
2011-05-01 16:48:27 +02:00
|
|
|
{
|
2014-11-12 18:38:48 +01:00
|
|
|
syslog(LOG_NOTICE, g_format, blkinfo->block, 0, "BLOCK", "CORRUPT",
|
2014-11-12 14:15:13 +01:00
|
|
|
blkinfo->geo.blocksize);
|
2011-05-01 16:48:27 +02:00
|
|
|
}
|
2011-05-01 20:16:03 +02:00
|
|
|
|
2019-05-25 20:13:27 +02:00
|
|
|
/* Search for Inode and data block headers. */
|
2011-05-01 20:16:03 +02:00
|
|
|
|
|
|
|
inndx = 0;
|
|
|
|
datndx = 0;
|
|
|
|
|
|
|
|
for (i = SIZEOF_NXFFS_BLOCK_HDR; i < blkinfo->geo.blocksize; i++)
|
2011-05-01 16:48:27 +02:00
|
|
|
{
|
2011-05-01 20:16:03 +02:00
|
|
|
uint8_t ch = blkinfo->buffer[i];
|
|
|
|
|
2011-05-02 20:38:11 +02:00
|
|
|
if (ch == g_inodemagic[inndx])
|
2011-05-01 20:16:03 +02:00
|
|
|
{
|
|
|
|
inndx++;
|
|
|
|
datndx = 0;
|
|
|
|
|
|
|
|
if (inndx == NXFFS_MAGICSIZE)
|
|
|
|
{
|
|
|
|
hdrndx = i - NXFFS_MAGICSIZE + 1;
|
|
|
|
nbytes = nxffs_analyzeinode(blkinfo, hdrndx);
|
|
|
|
if (nbytes > 0)
|
|
|
|
{
|
2011-05-04 16:20:52 +02:00
|
|
|
i = hdrndx + nbytes - 1;
|
2011-05-01 20:16:03 +02:00
|
|
|
}
|
2013-12-02 03:17:09 +01:00
|
|
|
|
2011-05-04 16:20:52 +02:00
|
|
|
inndx = 0;
|
2011-05-01 20:16:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (ch == g_datamagic[datndx])
|
|
|
|
{
|
|
|
|
datndx++;
|
|
|
|
inndx = 0;
|
|
|
|
|
|
|
|
if (datndx == NXFFS_MAGICSIZE)
|
|
|
|
{
|
|
|
|
hdrndx = i - NXFFS_MAGICSIZE + 1;
|
|
|
|
nbytes = nxffs_analyzedata(blkinfo, hdrndx);
|
|
|
|
if (nbytes > 0)
|
|
|
|
{
|
2011-05-04 16:20:52 +02:00
|
|
|
i = hdrndx + nbytes - 1;
|
2011-05-01 20:16:03 +02:00
|
|
|
}
|
2013-12-02 03:17:09 +01:00
|
|
|
|
2011-05-04 16:20:52 +02:00
|
|
|
datndx = 0;
|
2011-05-01 20:16:03 +02:00
|
|
|
}
|
|
|
|
}
|
2011-05-01 16:48:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxffs_dump
|
|
|
|
*
|
|
|
|
* Description:
|
2021-02-04 13:56:38 +01:00
|
|
|
* Dump a summary of the contents of an NXFFS file system.
|
|
|
|
* CONFIG_DEBUG_FEATURES and CONFIG_DEBUG_FS must be enabled
|
|
|
|
* for this function to do anything.
|
2011-05-01 16:48:27 +02:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
2011-05-04 23:59:23 +02:00
|
|
|
* mtd - The MTD device that provides the interface to NXFFS-formatted
|
|
|
|
* media.
|
|
|
|
* verbose - FALSE: only show errors
|
2011-05-01 16:48:27 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero is returned on success. Otherwise, a negated errno value is
|
|
|
|
* returned to indicate the nature of the failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-05-04 23:59:23 +02:00
|
|
|
int nxffs_dump(FAR struct mtd_dev_s *mtd, bool verbose)
|
2011-05-01 16:48:27 +02:00
|
|
|
{
|
2016-06-11 22:14:08 +02:00
|
|
|
#if defined(CONFIG_DEBUG_FEATURES) && defined(CONFIG_DEBUG_FS)
|
2011-05-01 20:16:03 +02:00
|
|
|
struct nxffs_blkinfo_s blkinfo;
|
2011-05-01 16:48:27 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Get the volume geometry. (casting to uintptr_t first eliminates
|
|
|
|
* complaints on some architectures where the sizeof long is different
|
|
|
|
* from the size of a pointer).
|
|
|
|
*/
|
|
|
|
|
2011-05-01 20:16:03 +02:00
|
|
|
memset(&blkinfo, 0, sizeof(struct nxffs_blkinfo_s));
|
2021-02-04 13:56:38 +01:00
|
|
|
ret = MTD_IOCTL(mtd,
|
|
|
|
MTDIOC_GEOMETRY,
|
|
|
|
(unsigned long)((uintptr_t)&blkinfo.geo));
|
2011-05-01 16:48:27 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: MTD ioctl(MTDIOC_GEOMETRY) failed: %d\n", -ret);
|
2013-12-02 03:17:09 +01:00
|
|
|
return ret;
|
2011-05-01 16:48:27 +02:00
|
|
|
}
|
|
|
|
|
2011-05-04 23:59:23 +02:00
|
|
|
/* Save the verbose output indication */
|
|
|
|
|
|
|
|
blkinfo.verbose = verbose;
|
|
|
|
|
2011-05-01 16:48:27 +02:00
|
|
|
/* Allocate a buffer to hold one block */
|
|
|
|
|
2023-08-28 09:39:47 +02:00
|
|
|
blkinfo.buffer = kmm_malloc(blkinfo.geo.blocksize);
|
2011-05-01 20:16:03 +02:00
|
|
|
if (!blkinfo.buffer)
|
2011-05-01 16:48:27 +02:00
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Failed to allocate block cache\n");
|
2013-12-02 03:17:09 +01:00
|
|
|
return -ENOMEM;
|
2011-05-01 16:48:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Now read every block on the device */
|
|
|
|
|
2014-11-12 18:38:48 +01:00
|
|
|
syslog(LOG_NOTICE, "NXFFS Dump:\n");
|
|
|
|
syslog(LOG_NOTICE, g_hdrformat);
|
2011-05-01 20:16:03 +02:00
|
|
|
|
2019-10-27 18:48:14 +01:00
|
|
|
blkinfo.nblocks = blkinfo.geo.erasesize * blkinfo.geo.neraseblocks /
|
|
|
|
blkinfo.geo.blocksize;
|
2011-05-01 20:16:03 +02:00
|
|
|
for (blkinfo.block = 0, blkinfo.offset = 0;
|
|
|
|
blkinfo.block < blkinfo.nblocks;
|
|
|
|
blkinfo.block++, blkinfo.offset += blkinfo.geo.blocksize)
|
2011-05-01 16:48:27 +02:00
|
|
|
{
|
|
|
|
/* Read the next block */
|
|
|
|
|
2011-05-01 20:16:03 +02:00
|
|
|
ret = MTD_BREAD(mtd, blkinfo.block, 1, blkinfo.buffer);
|
2011-05-01 16:48:27 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2013-12-03 20:11:11 +01:00
|
|
|
#ifndef CONFIG_NXFFS_NAND
|
|
|
|
/* Read errors are fatal */
|
|
|
|
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Failed to read block %d\n", blkinfo.block);
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(blkinfo.buffer);
|
2013-12-03 20:11:11 +01:00
|
|
|
return ret;
|
|
|
|
#else
|
2013-12-02 03:17:09 +01:00
|
|
|
/* A read error is probably fatal on all media but NAND.
|
|
|
|
* On NAND, the read error probably just signifies a block
|
|
|
|
* with an uncorrectable ECC failure. So, to handle NAND,
|
|
|
|
* just report the read error and continue.
|
|
|
|
*/
|
|
|
|
|
2014-11-12 18:38:48 +01:00
|
|
|
syslog(LOG_NOTICE, g_format, blkinfo.block, 0, "BLOCK", "RD FAIL",
|
2013-12-02 03:17:09 +01:00
|
|
|
blkinfo.geo.blocksize);
|
2013-12-03 20:11:11 +01:00
|
|
|
#endif
|
2011-05-01 16:48:27 +02:00
|
|
|
}
|
2013-12-02 03:17:09 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Analyze the block that we just read */
|
2011-05-01 16:48:27 +02:00
|
|
|
|
2013-12-02 03:17:09 +01:00
|
|
|
nxffs_analyze(&blkinfo);
|
|
|
|
}
|
2011-05-01 16:48:27 +02:00
|
|
|
}
|
2013-12-02 03:17:09 +01:00
|
|
|
|
2024-03-07 00:36:58 +01:00
|
|
|
syslog(LOG_NOTICE, "%" PRIi32 " blocks analyzed\n", blkinfo.nblocks);
|
2011-05-01 16:48:27 +02:00
|
|
|
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(blkinfo.buffer);
|
2013-12-02 03:17:09 +01:00
|
|
|
return OK;
|
2011-05-01 16:48:27 +02:00
|
|
|
|
|
|
|
#else
|
|
|
|
return -ENOSYS;
|
|
|
|
#endif
|
|
|
|
}
|