4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2007 Develer S.r.l. (http://www.develer.com/)
35 * \author Francesco Sacchi <batt@develer.com>
37 * \brief BattFS: a filesystem for embedded platforms (implementation).
42 #include <mware/byteorder.h> /* cpu_to_xx */
44 #include <string.h> /* memset */
47 * Convert from cpu endianess to filesystem endianness.
48 * \note filesystem is in little-endian format.
50 INLINE void cpu_to_battfs(struct BattFsPageHeader *hdr)
52 STATIC_ASSERT(sizeof(hdr->inode) == 1);
53 STATIC_ASSERT(sizeof(hdr->seq) == 1);
55 STATIC_ASSERT(sizeof(hdr->mark) == 2);
56 hdr->mark = cpu_to_le16(hdr->mark);
58 STATIC_ASSERT(sizeof(hdr->fill) == 2);
59 hdr->fill = cpu_to_le16(hdr->fill);
61 STATIC_ASSERT(sizeof(hdr->pgoff) == 2);
62 hdr->pgoff = cpu_to_le16(hdr->pgoff);
64 STATIC_ASSERT(sizeof(hdr->fcs) == 2);
65 hdr->fcs = cpu_to_le16(hdr->fcs);
67 STATIC_ASSERT(sizeof(hdr->rfu) == 2);
68 hdr->rfu = cpu_to_le16(hdr->rfu);
73 * Convert from filesystem endianness to cpu endianess.
74 * \note filesystem is in little-endian format.
76 INLINE void battfs_to_cpu(struct BattFsPageHeader *hdr)
78 STATIC_ASSERT(sizeof(hdr->inode) == 1);
79 STATIC_ASSERT(sizeof(hdr->seq) == 1);
81 STATIC_ASSERT(sizeof(hdr->mark) == 2);
82 hdr->mark = le16_to_cpu(hdr->mark);
84 STATIC_ASSERT(sizeof(hdr->fill) == 2);
85 hdr->fill = le16_to_cpu(hdr->fill);
87 STATIC_ASSERT(sizeof(hdr->pgoff) == 2);
88 hdr->pgoff = le16_to_cpu(hdr->pgoff);
90 STATIC_ASSERT(sizeof(hdr->fcs) == 2);
91 hdr->fcs = le16_to_cpu(hdr->fcs);
93 STATIC_ASSERT(sizeof(hdr->rfu) == 2);
94 hdr->rfu = le16_to_cpu(hdr->rfu);
99 * Read header of page \a page.
100 * \return true on success, false otherwise.
101 * \note \a hdr is dirtyed even on errors.
103 static bool battfs_readHeader(struct BattFsSuper *disk, pgcnt_t page, struct BattFsPageHeader *hdr)
106 * Read header from disk.
107 * header is actually a footer, and so
108 * resides at page end.
110 if (disk->read(disk, page, disk->page_size - sizeof(BattFsPageHeader), hdr, sizeof(BattFsPageHeader))
111 != sizeof(BattFsPageHeader))
120 * Initialize and mount disk described by
122 * \return false on errors, true otherwise.
124 bool battfs_init(struct BattFsSuper *disk)
126 BattFsPageHeader hdr;
128 pgoff_t filelen_table[BATTFS_MAX_FILES];
130 /* Init disk device */
131 if (!disk->open(disk))
134 memset(filelen_table, 0, BATTFS_MAX_FILES * sizeof(pgoff_t));
136 for (pgcnt_t page = 0; page < disk->page_count; page++)
138 if (!battfs_readHeader(disk, page, &hdr))
141 /* Check header FCS */
143 rotating_update(&hdr, sizeof(BattFsPageHeader) - sizeof(rotating_t), &cks);
145 filelen_table[hdr.inode]++;