mirror of
https://github.com/apache/nuttx.git
synced 2026-05-27 19:36:35 +08:00
Add basic write logic
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@3544 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
+42
-12
@@ -177,6 +177,16 @@
|
|||||||
|
|
||||||
#define NXFFS_NERASED 128
|
#define NXFFS_NERASED 128
|
||||||
|
|
||||||
|
/* Quasi-standard definitions */
|
||||||
|
|
||||||
|
#ifndef MIN
|
||||||
|
# define MIN(a,b) (a < b ? a : b)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef MAX
|
||||||
|
# define MAX(a,b) (a > b ? a : b)
|
||||||
|
#endif
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Public Types
|
* Public Types
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
@@ -211,10 +221,9 @@ struct nxffs_data_s
|
|||||||
{
|
{
|
||||||
uint8_t magic[4]; /* 0-3: Magic number for valid data */
|
uint8_t magic[4]; /* 0-3: Magic number for valid data */
|
||||||
uint8_t crc[4]; /* 4-7: CRC32 */
|
uint8_t crc[4]; /* 4-7: CRC32 */
|
||||||
uint8_t datlen[4]; /* 8-11: Length of data in bytes */
|
uint8_t datlen[2]; /* 8-9: Length of data in bytes */
|
||||||
/* 12-: Variable length data follows */
|
|
||||||
};
|
};
|
||||||
#define SIZEOF_NXFFS_DATA_HDR 12
|
#define SIZEOF_NXFFS_DATA_HDR 10
|
||||||
|
|
||||||
/* This is an in-memory representation of the NXFFS inode as extracted from
|
/* This is an in-memory representation of the NXFFS inode as extracted from
|
||||||
* FLASH and with additional state information.
|
* FLASH and with additional state information.
|
||||||
@@ -251,14 +260,12 @@ struct nxffs_wrfile_s
|
|||||||
struct nxffs_ofile_s ofile;
|
struct nxffs_ofile_s ofile;
|
||||||
|
|
||||||
/* The following fields are required to support the current write
|
/* The following fields are required to support the current write
|
||||||
* operation. Note that the size of the current block can be determined
|
* operation.
|
||||||
* from (wroffset - dathdr - SIZEOF_NXFFS_DATA_HDR). Basic write
|
|
||||||
* operation:
|
|
||||||
*
|
*
|
||||||
* 1. Inode header location determined (but not yet written).
|
* 1. Inode header location determined (but not yet written).
|
||||||
* 2. Block header location determined (but not yet written).
|
* 2. Block header location determined (but not yet written).
|
||||||
* 3. Check FLASH memory to make sure that it is erased.
|
* 3. Check FLASH memory to make sure that it is erased.
|
||||||
* 4. As data is written, wrlen is updated and the data is written to FLASH.
|
* 4. As data is written, datlen is updated and the data is written to FLASH.
|
||||||
* 5. If the end of the FLASH block is encountered, the data block CRC is
|
* 5. If the end of the FLASH block is encountered, the data block CRC is
|
||||||
* calculated and the block header is also written to flash.
|
* calculated and the block header is also written to flash.
|
||||||
* 6. When the file is closed, the final, partial data block is written to
|
* 6. When the file is closed, the final, partial data block is written to
|
||||||
@@ -269,8 +276,9 @@ struct nxffs_wrfile_s
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
bool truncate; /* Delete a file of the same name */
|
bool truncate; /* Delete a file of the same name */
|
||||||
uint16_t wrlen; /* Number of bytes written in data block */
|
uint16_t datlen; /* Number of bytes written in data block */
|
||||||
off_t dathdr; /* FLASH offset to the current data header */
|
off_t doffset; /* FLASH offset to the current data header */
|
||||||
|
uint32_t crc; /* Accumulated data block CRC */
|
||||||
};
|
};
|
||||||
|
|
||||||
/* This structure represents the overall state of on NXFFS instance. */
|
/* This structure represents the overall state of on NXFFS instance. */
|
||||||
@@ -396,6 +404,8 @@ extern uint16_t nxffs_rdle16(FAR const uint8_t *val);
|
|||||||
* Returned Values:
|
* Returned Values:
|
||||||
* None
|
* None
|
||||||
*
|
*
|
||||||
|
* Defined in nxffs_util.c
|
||||||
|
*
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
extern void nxffs_wrle16(uint8_t *dest, uint16_t val);
|
extern void nxffs_wrle16(uint8_t *dest, uint16_t val);
|
||||||
@@ -431,6 +441,8 @@ extern uint32_t nxffs_rdle32(FAR const uint8_t *val);
|
|||||||
* Returned Value:
|
* Returned Value:
|
||||||
* None
|
* None
|
||||||
*
|
*
|
||||||
|
* Defined in nxffs_util.c
|
||||||
|
*
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
extern void nxffs_wrle32(uint8_t *dest, uint32_t val);
|
extern void nxffs_wrle32(uint8_t *dest, uint32_t val);
|
||||||
@@ -439,20 +451,20 @@ extern void nxffs_wrle32(uint8_t *dest, uint32_t val);
|
|||||||
* Name: nxffs_erased
|
* Name: nxffs_erased
|
||||||
*
|
*
|
||||||
* Description:
|
* Description:
|
||||||
* Check if the block of memory if in the erased state.
|
* Check if a block of memory is in the erased state.
|
||||||
*
|
*
|
||||||
* Input Parameters:
|
* Input Parameters:
|
||||||
* buffer - Address of the start of the memory to check.
|
* buffer - Address of the start of the memory to check.
|
||||||
* buflen - The number of bytes to check.
|
* buflen - The number of bytes to check.
|
||||||
*
|
*
|
||||||
* Returned Values:
|
* Returned Values:
|
||||||
* true: memory is erased; false: memory is not erased
|
* The number of erased bytes found at the beginning of the memory region.
|
||||||
*
|
*
|
||||||
* Defined in nxffs_util.c
|
* Defined in nxffs_util.c
|
||||||
*
|
*
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
extern bool nxffs_erased(FAR const uint8_t *buffer, size_t buflen);
|
extern size_t nxffs_erased(FAR const uint8_t *buffer, size_t buflen);
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Name: nxffs_rdcache
|
* Name: nxffs_rdcache
|
||||||
@@ -517,6 +529,24 @@ extern int nxffs_wrcache(FAR struct nxffs_volume_s *volume, off_t block,
|
|||||||
|
|
||||||
extern void nxffs_ioseek(FAR struct nxffs_volume_s *volume, off_t offset);
|
extern void nxffs_ioseek(FAR struct nxffs_volume_s *volume, off_t offset);
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: nxffs_iotell
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* Report the current position.
|
||||||
|
*
|
||||||
|
* Input Parameters:
|
||||||
|
* volume - Describes the NXFFS volume
|
||||||
|
*
|
||||||
|
* Returned Value:
|
||||||
|
* The offset from the beginning of FLASH to the current seek position.
|
||||||
|
*
|
||||||
|
* Defined in nxffs_cache.c
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
extern off_t nxffs_iotell(FAR struct nxffs_volume_s *volume);
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Name: nxffs_getc
|
* Name: nxffs_getc
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -166,6 +166,25 @@ void nxffs_ioseek(FAR struct nxffs_volume_s *volume, off_t offset)
|
|||||||
volume->iooffset = offset - volume->geo.blocksize * volume->ioblock;
|
volume->iooffset = offset - volume->geo.blocksize * volume->ioblock;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: nxffs_iotell
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* Report the current position.
|
||||||
|
*
|
||||||
|
* Input Parameters:
|
||||||
|
* volume - Describes the NXFFS volume
|
||||||
|
*
|
||||||
|
* Returned Value:
|
||||||
|
* The offset from the beginning of FLASH to the current seek position.
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
off_t nxffs_iotell(FAR struct nxffs_volume_s *volume)
|
||||||
|
{
|
||||||
|
return volume->ioblock * volume->geo.blocksize + volume->iooffset;
|
||||||
|
}
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Name: nxffs_getc
|
* Name: nxffs_getc
|
||||||
*
|
*
|
||||||
|
|||||||
+33
-12
@@ -64,11 +64,19 @@
|
|||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Public Variables
|
* Private Data
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
|
/* Since we are limited to a single file opened for writing, it makes sense
|
||||||
|
* to pre-allocate the write state structure.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifdef CONFIG_NXFSS_PREALLOCATED
|
||||||
|
static struct nxffs_wrfile_s g_wrfile;
|
||||||
|
#endif
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Private Variables
|
* Public Data
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
@@ -124,8 +132,7 @@ static inline int nxffs_hdrpos(FAR struct nxffs_volume_s *volume,
|
|||||||
{
|
{
|
||||||
/* Save the offset to the FLASH region reserved for the inode header */
|
/* Save the offset to the FLASH region reserved for the inode header */
|
||||||
|
|
||||||
wrfile->ofile.entry.hoffset =
|
wrfile->ofile.entry.hoffset = nxffs_iotell(volume);
|
||||||
volume->ioblock * volume->geo.blocksize + volume->iooffset;
|
|
||||||
}
|
}
|
||||||
return OK;
|
return OK;
|
||||||
}
|
}
|
||||||
@@ -180,8 +187,7 @@ static inline int nxffs_nampos(FAR struct nxffs_volume_s *volume,
|
|||||||
{
|
{
|
||||||
/* Save the offset to the FLASH region reserved for the inode name */
|
/* Save the offset to the FLASH region reserved for the inode name */
|
||||||
|
|
||||||
wrfile->ofile.entry.noffset =
|
wrfile->ofile.entry.noffset = nxffs_iotell(volume);
|
||||||
volume->ioblock * volume->geo.blocksize + volume->iooffset;
|
|
||||||
}
|
}
|
||||||
return OK;
|
return OK;
|
||||||
}
|
}
|
||||||
@@ -242,8 +248,7 @@ static inline int nxffs_hdrerased(FAR struct nxffs_volume_s *volume,
|
|||||||
{
|
{
|
||||||
/* This is where we will put the header */
|
/* This is where we will put the header */
|
||||||
|
|
||||||
wrfile->ofile.entry.hoffset =
|
wrfile->ofile.entry.hoffset = nxffs_iotell(volume);
|
||||||
volume->ioblock * volume->geo.blocksize + volume->iooffset;
|
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@@ -305,8 +310,7 @@ static inline int nxffs_namerased(FAR struct nxffs_volume_s *volume,
|
|||||||
{
|
{
|
||||||
/* This is where we will put the name */
|
/* This is where we will put the name */
|
||||||
|
|
||||||
wrfile->ofile.entry.hoffset =
|
wrfile->ofile.entry.hoffset = nxffs_iotell(volume);
|
||||||
volume->ioblock * volume->geo.blocksize + volume->iooffset;
|
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@@ -412,12 +416,17 @@ static inline int nxffs_wropen(FAR struct nxffs_volume_s *volume,
|
|||||||
* that includes additional information to support the write operation.
|
* that includes additional information to support the write operation.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#ifdef CONFIG_NXFSS_PREALLOCATED
|
||||||
|
wrfile = &g_wrfile;
|
||||||
|
memset(wrfile, 0, sizeof(struct nxffs_wrfile_s));
|
||||||
|
#else
|
||||||
wrfile = (FAR struct nxffs_wrfile_s *)kzalloc(sizeof(struct nxffs_wrfile_s));
|
wrfile = (FAR struct nxffs_wrfile_s *)kzalloc(sizeof(struct nxffs_wrfile_s));
|
||||||
if (!wrfile)
|
if (!wrfile)
|
||||||
{
|
{
|
||||||
ret = -ENOMEM;
|
ret = -ENOMEM;
|
||||||
goto errout;
|
goto errout;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
/* Initialize the open file state structure */
|
/* Initialize the open file state structure */
|
||||||
|
|
||||||
@@ -553,7 +562,9 @@ static inline int nxffs_wropen(FAR struct nxffs_volume_s *volume,
|
|||||||
return OK;
|
return OK;
|
||||||
|
|
||||||
errout_with_ofile:
|
errout_with_ofile:
|
||||||
|
#ifndef CONFIG_NXFSS_PREALLOCATED
|
||||||
kfree(wrfile);
|
kfree(wrfile);
|
||||||
|
#endif
|
||||||
errout:
|
errout:
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@@ -672,10 +683,20 @@ static inline void nxffs_freeofile(FAR struct nxffs_volume_s *volume,
|
|||||||
volume->ofiles = ofile->flink;
|
volume->ofiles = ofile->flink;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Then free the open file */
|
/* Release the open file entry */
|
||||||
|
|
||||||
nxffs_freeentry(&ofile->entry);
|
nxffs_freeentry(&ofile->entry);
|
||||||
kfree(ofile);
|
|
||||||
|
/* Then free the open file container (unless this the pre-alloated
|
||||||
|
* write-only open file container)
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifdef CONFIG_NXFSS_PREALLOCATED
|
||||||
|
if ((FAR struct nxffs_wrfile_s*)ofile != &g_wrfile)
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
kfree(ofile);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -191,8 +191,9 @@ static int nxffs_badblocks(FAR struct nxffs_volume_s *volume)
|
|||||||
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
good = nxffs_erased(&blkptr[SIZEOF_NXFFS_BLOCK_HDR],
|
size_t blocksize = volume->geo.blocksize - SIZEOF_NXFFS_BLOCK_HDR;
|
||||||
volume->geo.blocksize - SIZEOF_NXFFS_BLOCK_HDR);
|
size_t erasesize = nxffs_erased(&blkptr[SIZEOF_NXFFS_BLOCK_HDR], blocksize);
|
||||||
|
good = (blocksize == erasesize);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* If the block is bad, attempt to re-write the block header indicating
|
/* If the block is bad, attempt to re-write the block header indicating
|
||||||
|
|||||||
@@ -153,26 +153,29 @@ void nxffs_wrle32(uint8_t *dest, uint32_t val)
|
|||||||
* Name: nxffs_erased
|
* Name: nxffs_erased
|
||||||
*
|
*
|
||||||
* Description:
|
* Description:
|
||||||
* Check if the block of memory if in the erased state.
|
* Check if a block of memory is in the erased state.
|
||||||
*
|
*
|
||||||
* Input Parameters:
|
* Input Parameters:
|
||||||
* buffer - Address of the start of the memory to check.
|
* buffer - Address of the start of the memory to check.
|
||||||
* buflen - The number of bytes to check.
|
* buflen - The number of bytes to check.
|
||||||
*
|
*
|
||||||
* Returned Values:
|
* Returned Values:
|
||||||
* true: memory is erased; false: memory is not erased
|
* The number of erased bytes found at the beginning of the memory region.
|
||||||
*
|
*
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
bool nxffs_erased(FAR const uint8_t *buffer, size_t buflen)
|
size_t nxffs_erased(FAR const uint8_t *buffer, size_t buflen)
|
||||||
{
|
{
|
||||||
for (; buflen; buflen--)
|
size_t nerased = 0;
|
||||||
|
|
||||||
|
for (; nerased < buflen; nerased++)
|
||||||
{
|
{
|
||||||
if (*buffer != CONFIG_NXFFS_ERASEDSTATE)
|
if (*buffer != CONFIG_NXFFS_ERASEDSTATE)
|
||||||
{
|
{
|
||||||
return false;
|
break;
|
||||||
}
|
}
|
||||||
buffer++;
|
buffer++;
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
|
return nerased;
|
||||||
}
|
}
|
||||||
|
|||||||
+445
-16
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user