Added SMART flash filesystem to RP2040

This commit is contained in:
curuvar
2022-09-04 19:02:24 -04:00
committed by Alan Carvalho de Assis
parent fb852440af
commit 9ad75fd95d
37 changed files with 1980 additions and 34 deletions
+12 -2
View File
@@ -197,8 +197,18 @@
# define offsetof(type, member) ((size_t) & (((type *)0)->member))
#endif
#define SMARTFS_NEXTSECTOR(h) (*((uint16_t *)h->nextsector))
#define SMARTFS_USED(h) (*((uint16_t *)h->used))
#ifdef CONFIG_SMARTFS_ALIGNED_ACCESS
# define SMARTFS_NEXTSECTOR(h) (smartfs_rdle16(h->nextsector))
# define SMARTFS_SET_NEXTSECTOR(h, v) smartfs_wrle16(h->nextsector, v)
# define SMARTFS_USED(h) (smartfs_rdle16(h->used))
# define SMARTFS_SET_USED(h, v) smartfs_wrle16(h->used, v)
#else
# define SMARTFS_NEXTSECTOR(h) (*((uint16_t *)h->nextsector))
# define SMARTFS_SET_NEXTSECTOR(h, v) ((*((uint16_t *)h->nextsector)) = v)
# define SMARTFS_USED(h) (*((uint16_t *)h->used))
# define SMARTFS_SET_USED(h, v) ((*((uint16_t *)h->used)) = v)
#endif
#ifdef CONFIG_MTD_SMART_ENABLE_CRC
#define CONFIG_SMARTFS_USE_SECTOR_BUFFER
+55 -7
View File
@@ -45,6 +45,10 @@
#include "smartfs.h"
#ifdef CONFIG_DEBUG_FS_INFO
# include <stdio.h>
#endif
/****************************************************************************
* Private Type
****************************************************************************/
@@ -528,6 +532,8 @@ static ssize_t smartfs_read(FAR struct file *filep, char *buffer,
DEBUGASSERT(filep->f_priv != NULL && filep->f_inode != NULL);
finfo("Reading %u bytes\n", (unsigned) buflen);
/* Recover our private data from the struct file instance */
sf = filep->f_priv;
@@ -578,7 +584,7 @@ static ssize_t smartfs_read(FAR struct file *filep, char *buffer,
/* Get number of used bytes in this sector */
bytesinsector = *((uint16_t *) header->used);
bytesinsector = SMARTFS_USED(header);
if (bytesinsector == SMARTFS_ERASEDSTATE_16BIT)
{
/* No bytes to read from this sector */
@@ -633,6 +639,18 @@ static ssize_t smartfs_read(FAR struct file *filep, char *buffer,
/* Return the number of bytes we read */
#ifdef CONFIG_DEBUG_FS_INFO
finfo("Read %lu bytes:", bytesread);
for (uint32_t i = 0; i < bytesread; ++i)
{
if ((i & 0x0f) == 0) printf("\n ");
printf("%02x, ", buffer[i]);
}
printf("\n");
#endif
ret = bytesread;
errout_with_semaphore:
@@ -657,6 +675,18 @@ static ssize_t smartfs_write(FAR struct file *filep, const char *buffer,
DEBUGASSERT(filep->f_priv != NULL && filep->f_inode != NULL);
#ifdef CONFIG_DEBUG_FS_INFO
finfo("Writing %lu bytes:", (uint32_t)buflen);
for (uint32_t i = 0; i < buflen; ++i)
{
if ((i & 0x0f) == 0) printf("\n ");
printf("%02x, ", buffer[i]);
}
printf("\n");
#endif
/* Recover our private data from the struct file instance */
sf = filep->f_priv;
@@ -852,7 +882,7 @@ static ssize_t smartfs_write(FAR struct file *filep, const char *buffer,
/* Copy the new sector to the old one and chain it */
header = (struct smartfs_chain_header_s *) sf->buffer;
*((uint16_t *) header->nextsector) = (uint16_t) ret;
SMARTFS_SET_NEXTSECTOR(header, (uint16_t) ret);
/* Now sync the file to write this sector out */
@@ -908,7 +938,7 @@ static ssize_t smartfs_write(FAR struct file *filep, const char *buffer,
/* Copy the new sector to the old one and chain it */
header = (struct smartfs_chain_header_s *) fs->fs_rwbuffer;
*((uint16_t *) header->nextsector) = (uint16_t) ret;
SMARTFS_SET_NEXTSECTOR(header, (uint16_t) ret);
readwrite.offset = offsetof(struct smartfs_chain_header_s,
nextsector);
readwrite.buffer = (uint8_t *) header->nextsector;
@@ -941,6 +971,8 @@ static ssize_t smartfs_write(FAR struct file *filep, const char *buffer,
ret = byteswritten;
finfo("Wrote %u bytes\n", (unsigned) byteswritten);
errout_with_semaphore:
smartfs_semgive(fs);
return ret;
@@ -1325,6 +1357,7 @@ static int smartfs_readdir(FAR struct inode *mountpt,
entrysize = sizeof(struct smartfs_entry_header_s) +
fs->fs_llformat.namesize;
while (sdir->fs_currsector != SMARTFS_ERASEDSTATE_16BIT)
{
/* Read the logical sector */
@@ -1341,7 +1374,11 @@ static int smartfs_readdir(FAR struct inode *mountpt,
/* Now search for entries, starting at curroffset */
while (sdir->fs_curroffset < ret)
/* Note: directories don't use the header's used field
* so we search all possilble directory entries.
*/
while (sdir->fs_curroffset + entrysize < ret)
{
/* Point to next entry */
@@ -1350,24 +1387,35 @@ static int smartfs_readdir(FAR struct inode *mountpt,
/* Test if this entry is valid and active */
#ifdef CONFIG_SMARTFS_ALIGNED_ACCESS
if (((smartfs_rdle16(&entry->flags)
& SMARTFS_DIRENT_EMPTY) ==
(SMARTFS_ERASEDSTATE_16BIT & SMARTFS_DIRENT_EMPTY)) ||
((smartfs_rdle16(&entry->flags)
& SMARTFS_DIRENT_ACTIVE) !=
(SMARTFS_ERASEDSTATE_16BIT & SMARTFS_DIRENT_ACTIVE)))
#else
if (((entry->flags & SMARTFS_DIRENT_EMPTY) ==
(SMARTFS_ERASEDSTATE_16BIT & SMARTFS_DIRENT_EMPTY)) ||
((entry->flags & SMARTFS_DIRENT_ACTIVE) !=
(SMARTFS_ERASEDSTATE_16BIT & SMARTFS_DIRENT_ACTIVE)))
#endif
{
/* This entry isn't valid, skip it */
sdir->fs_curroffset += entrysize;
entry = (struct smartfs_entry_header_s *)
&fs->fs_rwbuffer[sdir->fs_curroffset];
continue;
}
/* Entry found! Report it */
#ifdef CONFIG_SMARTFS_ALIGNED_ACCESS
if ((smartfs_rdle16(&entry->flags) & SMARTFS_DIRENT_TYPE) ==
SMARTFS_DIRENT_TYPE_DIR)
#else
if ((entry->flags & SMARTFS_DIRENT_TYPE) ==
SMARTFS_DIRENT_TYPE_DIR)
#endif
{
dentry->d_type = DTYPE_DIRECTORY;
}
+20 -16
View File
@@ -714,11 +714,11 @@ int smartfs_finddirentry(struct smartfs_mountpt_s *fs,
* to next sector
*/
if (*((FAR uint16_t *)header->used) !=
if (SMARTFS_USED(header) !=
SMARTFS_ERASEDSTATE_16BIT)
{
direntry->datlen +=
*((uint16_t *)header->used);
SMARTFS_USED(header);
}
dirsector = SMARTFS_NEXTSECTOR(header);
@@ -1293,7 +1293,7 @@ int smartfs_deleteentry(struct smartfs_mountpt_s *fs,
{
/* We found ourselves in the chain. Update the chain. */
SMARTFS_NEXTSECTOR(header) = nextsector;
SMARTFS_SET_NEXTSECTOR(header, nextsector);
readwrite.offset = offsetof(struct smartfs_chain_header_s,
nextsector);
readwrite.count = sizeof(uint16_t);
@@ -1449,13 +1449,15 @@ int smartfs_sync_internal(FAR struct smartfs_mountpt_s *fs,
/* Update the header with the number of bytes written */
header = (struct smartfs_chain_header_s *)sf->buffer;
if (*((uint16_t *)header->used) == SMARTFS_ERASEDSTATE_16BIT)
if (SMARTFS_USED(header) == SMARTFS_ERASEDSTATE_16BIT)
{
*((uint16_t *)header->used) = sf->byteswritten;
SMARTFS_SET_USED(header, sf->byteswritten);
}
else
{
*((uint16_t *)header->used) += sf->byteswritten;
SMARTFS_SET_USED(header, SMARTFS_USED(header)
+ sf->byteswritten);
}
/* Write the entire sector to FLASH */
@@ -1504,13 +1506,15 @@ int smartfs_sync_internal(FAR struct smartfs_mountpt_s *fs,
/* Add new byteswritten to existing value */
header = (struct smartfs_chain_header_s *) fs->fs_rwbuffer;
if (*((uint16_t *) header->used) == SMARTFS_ERASEDSTATE_16BIT)
if (SMARTFS_USED(header) == SMARTFS_ERASEDSTATE_16BIT)
{
*((uint16_t *) header->used) = sf->byteswritten;
SMARTFS_SET_USED(header, sf->byteswritten);
}
else
{
*((uint16_t *) header->used) += sf->byteswritten;
SMARTFS_SET_USED(header, SMARTFS_USED(header)
+ sf->byteswritten);
}
readwrite.offset = offsetof(struct smartfs_chain_header_s, used);
@@ -1813,9 +1817,8 @@ int smartfs_shrinkfile(FAR struct smartfs_mountpt_s *fs,
dest = (FAR uint8_t *)&fs->fs_rwbuffer[offset];
destsize = fs->fs_llformat.availbytes - offset;
*((uint16_t *)header->used) = remaining;
*((uint16_t *)header->nextsector) = SMARTFS_ERASEDSTATE_16BIT;
SMARTFS_SET_USED(header, remaining);
SMARTFS_SET_NEXTSECTOR(header, SMARTFS_ERASEDSTATE_16BIT);
remaining = 0;
}
@@ -1879,8 +1882,9 @@ int smartfs_shrinkfile(FAR struct smartfs_mountpt_s *fs,
destsize = fs->fs_llformat.availbytes - offset;
header = (struct smartfs_chain_header_s *)sf->buffer;
*((uint16_t *)header->used) = length;
*((uint16_t *)header->nextsector) = SMARTFS_ERASEDSTATE_16BIT;
SMARTFS_SET_USED(header, length);
SMARTFS_SET_NEXTSECTOR(header, SMARTFS_ERASEDSTATE_16BIT);
}
memset(dest, CONFIG_SMARTFS_ERASEDSTATE, destsize);
@@ -2027,7 +2031,7 @@ int smartfs_extendfile(FAR struct smartfs_mountpt_s *fs,
/* Copy the new sector to the old one and chain it */
header = (struct smartfs_chain_header_s *) sf->buffer;
*((uint16_t *)header->nextsector) = (uint16_t)ret;
SMARTFS_SET_NEXTSECTOR(header, (uint16_t)ret);
/* Now sync the file to write this sector out */
@@ -2083,7 +2087,7 @@ int smartfs_extendfile(FAR struct smartfs_mountpt_s *fs,
/* Copy the new sector to the old one and chain it */
header = (struct smartfs_chain_header_s *)fs->fs_rwbuffer;
*((FAR uint16_t *)header->nextsector) = (uint16_t)ret;
SMARTFS_SET_NEXTSECTOR(header, (uint16_t)ret);
readwrite.offset = offsetof(struct smartfs_chain_header_s,
nextsector);