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 <cfg/debug.h>
43 #include <cfg/macros.h> /* MIN, MAX */
44 #include <mware/byteorder.h> /* cpu_to_xx */
47 #include <string.h> /* memset */
50 * Convert from cpu endianess to filesystem endianness.
51 * \note filesystem is in little-endian format.
53 INLINE void cpu_to_battfs(struct BattFsPageHeader *hdr)
55 STATIC_ASSERT(sizeof(hdr->inode) == 1);
56 STATIC_ASSERT(sizeof(hdr->seq) == 1);
58 STATIC_ASSERT(sizeof(hdr->mark) == 2);
59 hdr->mark = cpu_to_le16(hdr->mark);
61 STATIC_ASSERT(sizeof(hdr->fill) == 2);
62 hdr->fill = cpu_to_le16(hdr->fill);
64 STATIC_ASSERT(sizeof(hdr->pgoff) == 2);
65 hdr->pgoff = cpu_to_le16(hdr->pgoff);
67 STATIC_ASSERT(sizeof(hdr->fcs) == 2);
68 hdr->fcs = cpu_to_le16(hdr->fcs);
70 STATIC_ASSERT(sizeof(hdr->rfu) == 2);
71 hdr->rfu = cpu_to_le16(hdr->rfu);
76 * Convert from filesystem endianness to cpu endianess.
77 * \note filesystem is in little-endian format.
79 INLINE void battfs_to_cpu(struct BattFsPageHeader *hdr)
81 STATIC_ASSERT(sizeof(hdr->inode) == 1);
82 STATIC_ASSERT(sizeof(hdr->seq) == 1);
84 STATIC_ASSERT(sizeof(hdr->mark) == 2);
85 hdr->mark = le16_to_cpu(hdr->mark);
87 STATIC_ASSERT(sizeof(hdr->fill) == 2);
88 hdr->fill = le16_to_cpu(hdr->fill);
90 STATIC_ASSERT(sizeof(hdr->pgoff) == 2);
91 hdr->pgoff = le16_to_cpu(hdr->pgoff);
93 STATIC_ASSERT(sizeof(hdr->fcs) == 2);
94 hdr->fcs = le16_to_cpu(hdr->fcs);
96 STATIC_ASSERT(sizeof(hdr->rfu) == 2);
97 hdr->rfu = le16_to_cpu(hdr->rfu);
102 * Read header of page \a page.
103 * \return true on success, false otherwise.
104 * \note \a hdr is dirtyed even on errors.
106 static bool battfs_readHeader(struct BattFsSuper *disk, pgcnt_t page, struct BattFsPageHeader *hdr)
109 * Read header from disk.
110 * header is actually a footer, and so
111 * resides at page end.
113 if (disk->read(disk, page, disk->page_size - sizeof(BattFsPageHeader), hdr, sizeof(BattFsPageHeader))
114 != sizeof(BattFsPageHeader))
116 TRACEMSG("Error: page[%d]\n", page);
123 ASSERT(hdr->fill <= disk->page_size - sizeof(BattFsPageHeader));
128 * Initialize and mount disk described by
130 * \return false on errors, true otherwise.
132 bool battfs_init(struct BattFsSuper *disk)
134 BattFsPageHeader hdr;
136 pgoff_t filelen_table[BATTFS_MAX_FILES];
138 /* Init disk device */
139 if (!disk->open(disk))
141 TRACEMSG("Open error\n");
145 memset(filelen_table, 0, BATTFS_MAX_FILES * sizeof(pgoff_t));
147 /* Initialize min free sequence number to max value */
148 disk->min_free = MARK_PAGE_VALID;
149 /* Initialize max free sequence number to min value */
152 disk->free_bytes = 0;
153 disk->disk_size = (disk_size_t)(disk->page_size - sizeof(BattFsPageHeader)) * disk->page_count;
155 /* Count the number of disk page per files */
156 for (pgcnt_t page = 0; page < disk->page_count; page++)
158 if (!battfs_readHeader(disk, page, &hdr))
161 /* Check header FCS */
163 rotating_update(&hdr, sizeof(BattFsPageHeader) - sizeof(rotating_t), &cks);
167 filelen_table[hdr.inode]++;
169 /* Keep trace of free space */
170 disk->free_bytes += disk->page_size - sizeof(BattFsPageHeader) - hdr.fill;
174 /* Check if putting mark to MARK_PAGE_VALID makes fcs correct */
175 mark_t old_mark = hdr.mark;
176 hdr.mark = MARK_PAGE_VALID;
178 rotating_update(&hdr, sizeof(BattFsPageHeader) - sizeof(rotating_t), &cks);
182 * This page is a valid free page.
183 * Update min and max free page sequence numbers.
185 disk->min_free = MIN(disk->min_free, old_mark);
186 disk->max_free = MAX(disk->max_free, old_mark);
189 TRACEMSG("Page [%d] invalid, keeping as free\n", page);
191 /* Increase free space */
192 filelen_table[BATTFS_FREE_INODE]++;
193 disk->free_bytes += disk->page_size - sizeof(BattFsPageHeader);
197 /* Once here, we have filelen_table filled with file lengths */
198 #warning Complete me!