From c63526c6ea2ccec3e2fd259ca39e807a7238d9fb Mon Sep 17 00:00:00 2001 From: batt Date: Wed, 5 Dec 2007 22:32:45 +0000 Subject: [PATCH] Start to implement battfs_init (incomplete). git-svn-id: https://src.develer.com/svnoss/bertos/trunk@1027 38d2e660-2303-0410-9eaa-f027e97ec537 --- fs/battfs.c | 156 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 fs/battfs.c diff --git a/fs/battfs.c b/fs/battfs.c new file mode 100644 index 00000000..d4ee7233 --- /dev/null +++ b/fs/battfs.c @@ -0,0 +1,156 @@ +/** + * \file + * + * + * \version $Id:$ + * + * \author Francesco Sacchi + * + * \brief BattFS: a filesystem for embedded platforms (implementation). + */ + +#include "battfs.h" + +#include /* cpu_to_xx */ + +#include /* memset */ + +/** + * Convert from cpu endianess to filesystem endianness. + * \note filesystem is in little-endian format. + */ +INLINE void cpu_to_battfs(struct BattFsPageHeader *hdr) +{ + STATIC_ASSERT(sizeof(hdr->inode) == 1); + STATIC_ASSERT(sizeof(hdr->seq) == 1); + + STATIC_ASSERT(sizeof(hdr->mark) == 2); + hdr->mark = cpu_to_le16(hdr->mark); + + STATIC_ASSERT(sizeof(hdr->fill) == 2); + hdr->fill = cpu_to_le16(hdr->fill); + + STATIC_ASSERT(sizeof(hdr->pgoff) == 2); + hdr->pgoff = cpu_to_le16(hdr->pgoff); + + STATIC_ASSERT(sizeof(hdr->fcs) == 2); + hdr->fcs = cpu_to_le16(hdr->fcs); + + STATIC_ASSERT(sizeof(hdr->rfu) == 2); + hdr->rfu = cpu_to_le16(hdr->rfu); +} + + +/** + * Convert from filesystem endianness to cpu endianess. + * \note filesystem is in little-endian format. + */ +INLINE void battfs_to_cpu(struct BattFsPageHeader *hdr) +{ + STATIC_ASSERT(sizeof(hdr->inode) == 1); + STATIC_ASSERT(sizeof(hdr->seq) == 1); + + STATIC_ASSERT(sizeof(hdr->mark) == 2); + hdr->mark = le16_to_cpu(hdr->mark); + + STATIC_ASSERT(sizeof(hdr->fill) == 2); + hdr->fill = le16_to_cpu(hdr->fill); + + STATIC_ASSERT(sizeof(hdr->pgoff) == 2); + hdr->pgoff = le16_to_cpu(hdr->pgoff); + + STATIC_ASSERT(sizeof(hdr->fcs) == 2); + hdr->fcs = le16_to_cpu(hdr->fcs); + + STATIC_ASSERT(sizeof(hdr->rfu) == 2); + hdr->rfu = le16_to_cpu(hdr->rfu); +} + + +/** + * Read header of page \a page. + * \return true on success, false otherwise. + * \note \a hdr is dirtyed even on errors. + */ +static bool battfs_readHeader(struct BattFsSuper *disk, pgcnt_t page, struct BattFsPageHeader *hdr) +{ + /* + * Read header from disk. + * header is actually a footer, and so + * resides at page end. + */ + if (disk->read(disk, page, disk->page_size - sizeof(BattFsPageHeader), hdr, sizeof(BattFsPageHeader)) + != sizeof(BattFsPageHeader)) + return false; + + /* Fix endianess */ + battfs_to_cpu(hdr); + return true; +} + +/** + * Initialize and mount disk described by + * \a d. + * \return false on errors, true otherwise. + */ +bool battfs_init(struct BattFsSuper *disk) +{ + BattFsPageHeader hdr; + rotating_t cks; + pgoff_t filelen_table[BATTFS_MAX_FILES]; + + /* Init disk device */ + if (!disk->open(disk)) + return false; + + memset(filelen_table, 0, BATTFS_MAX_FILES * sizeof(pgoff_t)); + + for (pgcnt_t page = 0; page < disk->page_count; page++) + { + if (!battfs_readHeader(disk, page, &hdr)) + return false; + + /* Check header FCS */ + rotating_init(&cks); + rotating_update(&hdr, sizeof(BattFsPageHeader) - sizeof(rotating_t), &cks); + if (cks == hdr.fcs) + filelen_table[page]++; + else + { + #warning Finish me! + } + } + + + return true; +} + + -- 2.25.1