Use correct inode address.
[bertos.git] / fs / battfs.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
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.
10  *
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.
15  *
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
19  *
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.
28  *
29  * Copyright 2007 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \version $Id:$
34  *
35  * \author Francesco Sacchi <batt@develer.com>
36  *
37  * \brief BattFS: a filesystem for embedded platforms (implementation).
38  */
39
40 #include "battfs.h"
41
42 #include <mware/byteorder.h> /* cpu_to_xx */
43
44 #include <string.h> /* memset */
45
46 /**
47  * Convert from cpu endianess to filesystem endianness.
48  * \note filesystem is in little-endian format.
49  */
50 INLINE void cpu_to_battfs(struct BattFsPageHeader *hdr)
51 {
52         STATIC_ASSERT(sizeof(hdr->inode) == 1);
53         STATIC_ASSERT(sizeof(hdr->seq) == 1);
54
55         STATIC_ASSERT(sizeof(hdr->mark) == 2);
56         hdr->mark = cpu_to_le16(hdr->mark);
57
58         STATIC_ASSERT(sizeof(hdr->fill) == 2);
59         hdr->fill = cpu_to_le16(hdr->fill);
60
61         STATIC_ASSERT(sizeof(hdr->pgoff) == 2);
62         hdr->pgoff = cpu_to_le16(hdr->pgoff);
63
64         STATIC_ASSERT(sizeof(hdr->fcs) == 2);
65         hdr->fcs = cpu_to_le16(hdr->fcs);
66
67         STATIC_ASSERT(sizeof(hdr->rfu) == 2);
68         hdr->rfu = cpu_to_le16(hdr->rfu);
69 }
70
71
72 /**
73  * Convert from filesystem endianness to cpu endianess.
74  * \note filesystem is in little-endian format.
75  */
76 INLINE void battfs_to_cpu(struct BattFsPageHeader *hdr)
77 {
78         STATIC_ASSERT(sizeof(hdr->inode) == 1);
79         STATIC_ASSERT(sizeof(hdr->seq) == 1);
80
81         STATIC_ASSERT(sizeof(hdr->mark) == 2);
82         hdr->mark = le16_to_cpu(hdr->mark);
83
84         STATIC_ASSERT(sizeof(hdr->fill) == 2);
85         hdr->fill = le16_to_cpu(hdr->fill);
86
87         STATIC_ASSERT(sizeof(hdr->pgoff) == 2);
88         hdr->pgoff = le16_to_cpu(hdr->pgoff);
89
90         STATIC_ASSERT(sizeof(hdr->fcs) == 2);
91         hdr->fcs = le16_to_cpu(hdr->fcs);
92
93         STATIC_ASSERT(sizeof(hdr->rfu) == 2);
94         hdr->rfu = le16_to_cpu(hdr->rfu);
95 }
96
97
98 /**
99  * Read header of page \a page.
100  * \return true on success, false otherwise.
101  * \note \a hdr is dirtyed even on errors.
102  */
103 static bool battfs_readHeader(struct BattFsSuper *disk, pgcnt_t page, struct BattFsPageHeader *hdr)
104 {
105         /*
106          * Read header from disk.
107          * header is actually a footer, and so
108          * resides at page end.
109          */
110         if (disk->read(disk, page, disk->page_size - sizeof(BattFsPageHeader), hdr, sizeof(BattFsPageHeader))
111             != sizeof(BattFsPageHeader))
112                 return false;
113
114         /* Fix endianess */
115         battfs_to_cpu(hdr);
116         return true;
117 }
118
119 /**
120  * Initialize and mount disk described by
121  * \a d.
122  * \return false on errors, true otherwise.
123  */
124 bool battfs_init(struct BattFsSuper *disk)
125 {
126         BattFsPageHeader hdr;
127         rotating_t cks;
128         pgoff_t filelen_table[BATTFS_MAX_FILES];
129
130         /* Init disk device */
131         if (!disk->open(disk))
132                 return false;
133
134         memset(filelen_table, 0, BATTFS_MAX_FILES * sizeof(pgoff_t));
135
136         for (pgcnt_t page = 0; page < disk->page_count; page++)
137         {
138                 if (!battfs_readHeader(disk, page, &hdr))
139                         return false;
140
141                 /* Check header FCS */
142                 rotating_init(&cks);
143                 rotating_update(&hdr, sizeof(BattFsPageHeader) - sizeof(rotating_t), &cks);
144                 if (cks == hdr.fcs)
145                         filelen_table[hdr.inode]++;
146                 else
147                 {
148                         #warning Finish me!
149                 }
150         }
151
152         
153         return true;    
154 }
155
156