mirror of
https://github.com/apache/nuttx.git
synced 2026-05-21 21:34:07 +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
|
||||
|
||||
/* 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
|
||||
****************************************************************************/
|
||||
@@ -211,10 +221,9 @@ struct nxffs_data_s
|
||||
{
|
||||
uint8_t magic[4]; /* 0-3: Magic number for valid data */
|
||||
uint8_t crc[4]; /* 4-7: CRC32 */
|
||||
uint8_t datlen[4]; /* 8-11: Length of data in bytes */
|
||||
/* 12-: Variable length data follows */
|
||||
uint8_t datlen[2]; /* 8-9: Length of data in bytes */
|
||||
};
|
||||
#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
|
||||
* FLASH and with additional state information.
|
||||
@@ -251,14 +260,12 @@ struct nxffs_wrfile_s
|
||||
struct nxffs_ofile_s ofile;
|
||||
|
||||
/* The following fields are required to support the current write
|
||||
* operation. Note that the size of the current block can be determined
|
||||
* from (wroffset - dathdr - SIZEOF_NXFFS_DATA_HDR). Basic write
|
||||
* operation:
|
||||
* operation.
|
||||
*
|
||||
* 1. Inode 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.
|
||||
* 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
|
||||
* calculated and the block header is also written to flash.
|
||||
* 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 */
|
||||
uint16_t wrlen; /* Number of bytes written in data block */
|
||||
off_t dathdr; /* FLASH offset to the current data header */
|
||||
uint16_t datlen; /* Number of bytes written in data block */
|
||||
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. */
|
||||
@@ -396,6 +404,8 @@ extern uint16_t nxffs_rdle16(FAR const uint8_t *val);
|
||||
* Returned Values:
|
||||
* None
|
||||
*
|
||||
* Defined in nxffs_util.c
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
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:
|
||||
* None
|
||||
*
|
||||
* Defined in nxffs_util.c
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
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
|
||||
*
|
||||
* 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:
|
||||
* buffer - Address of the start of the memory to check.
|
||||
* buflen - The number of bytes to check.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
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
|
||||
@@ -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);
|
||||
|
||||
/****************************************************************************
|
||||
* 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
|
||||
*
|
||||
|
||||
@@ -166,6 +166,25 @@ void nxffs_ioseek(FAR struct nxffs_volume_s *volume, off_t offset)
|
||||
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
|
||||
*
|
||||
|
||||
+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 */
|
||||
|
||||
wrfile->ofile.entry.hoffset =
|
||||
volume->ioblock * volume->geo.blocksize + volume->iooffset;
|
||||
wrfile->ofile.entry.hoffset = nxffs_iotell(volume);
|
||||
}
|
||||
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 */
|
||||
|
||||
wrfile->ofile.entry.noffset =
|
||||
volume->ioblock * volume->geo.blocksize + volume->iooffset;
|
||||
wrfile->ofile.entry.noffset = nxffs_iotell(volume);
|
||||
}
|
||||
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 */
|
||||
|
||||
wrfile->ofile.entry.hoffset =
|
||||
volume->ioblock * volume->geo.blocksize + volume->iooffset;
|
||||
wrfile->ofile.entry.hoffset = nxffs_iotell(volume);
|
||||
}
|
||||
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 */
|
||||
|
||||
wrfile->ofile.entry.hoffset =
|
||||
volume->ioblock * volume->geo.blocksize + volume->iooffset;
|
||||
wrfile->ofile.entry.hoffset = nxffs_iotell(volume);
|
||||
}
|
||||
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.
|
||||
*/
|
||||
|
||||
#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));
|
||||
if (!wrfile)
|
||||
{
|
||||
ret = -ENOMEM;
|
||||
goto errout;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Initialize the open file state structure */
|
||||
|
||||
@@ -553,7 +562,9 @@ static inline int nxffs_wropen(FAR struct nxffs_volume_s *volume,
|
||||
return OK;
|
||||
|
||||
errout_with_ofile:
|
||||
#ifndef CONFIG_NXFSS_PREALLOCATED
|
||||
kfree(wrfile);
|
||||
#endif
|
||||
errout:
|
||||
return ret;
|
||||
}
|
||||
@@ -672,10 +683,20 @@ static inline void nxffs_freeofile(FAR struct nxffs_volume_s *volume,
|
||||
volume->ofiles = ofile->flink;
|
||||
}
|
||||
|
||||
/* Then free the open file */
|
||||
/* Release the open file 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
|
||||
{
|
||||
|
||||
@@ -191,8 +191,9 @@ static int nxffs_badblocks(FAR struct nxffs_volume_s *volume)
|
||||
|
||||
else
|
||||
{
|
||||
good = nxffs_erased(&blkptr[SIZEOF_NXFFS_BLOCK_HDR],
|
||||
volume->geo.blocksize - SIZEOF_NXFFS_BLOCK_HDR);
|
||||
size_t blocksize = 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
|
||||
|
||||
@@ -153,26 +153,29 @@ void nxffs_wrle32(uint8_t *dest, uint32_t val)
|
||||
* Name: nxffs_erased
|
||||
*
|
||||
* 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:
|
||||
* buffer - Address of the start of the memory to check.
|
||||
* buflen - The number of bytes to check.
|
||||
*
|
||||
* 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)
|
||||
{
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
buffer++;
|
||||
}
|
||||
return true;
|
||||
|
||||
return nerased;
|
||||
}
|
||||
|
||||
+445
-16
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user