Change parser strings to include length.
[bertos.git] / bertos / io / kblock.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 2010 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \author Francesco Sacchi <batt@develer.com>
34  *
35  * \brief KBlock interface
36  */
37
38
39 #include "kblock.h"
40
41 #define LOG_LEVEL   LOG_LVL_ERR
42 #define LOG_FORMAT  LOG_FMT_VERBOSE
43
44 #include <cfg/log.h>
45 #include <string.h>
46
47 INLINE size_t kblock_readDirect(struct KBlock *b, block_idx_t index, void *buf, size_t offset, size_t size)
48 {
49         KB_ASSERT_METHOD(b, readDirect);
50         ASSERT(index < b->blk_cnt);
51         return b->priv.vt->readDirect(b, b->priv.blk_start + index, buf, offset, size);
52 }
53
54 INLINE size_t kblock_writeDirect(struct KBlock *b, block_idx_t index, const void *buf, size_t offset, size_t size)
55 {
56         KB_ASSERT_METHOD(b, writeDirect);
57         ASSERT(index < b->blk_cnt);
58         return b->priv.vt->writeDirect(b, b->priv.blk_start + index, buf, offset, size);
59 }
60
61 INLINE size_t kblock_readBuf(struct KBlock *b, void *buf, size_t offset, size_t size)
62 {
63         KB_ASSERT_METHOD(b, readBuf);
64         ASSERT(offset + size <= b->blk_size);
65
66         return b->priv.vt->readBuf(b, buf, offset, size);
67 }
68
69 INLINE size_t kblock_writeBuf(struct KBlock *b, const void *buf, size_t offset, size_t size)
70 {
71         KB_ASSERT_METHOD(b, writeBuf);
72         ASSERT(offset + size <= b->blk_size);
73         return b->priv.vt->writeBuf(b, buf, offset, size);
74 }
75
76 INLINE int kblock_load(struct KBlock *b, block_idx_t index)
77 {
78         KB_ASSERT_METHOD(b, load);
79         ASSERT(index < b->blk_cnt);
80
81         LOG_INFO("index %ld\n", index);
82         return b->priv.vt->load(b, b->priv.blk_start + index);
83 }
84
85 INLINE int kblock_store(struct KBlock *b, block_idx_t index)
86 {
87         KB_ASSERT_METHOD(b, store);
88         ASSERT(index < b->blk_cnt);
89
90         LOG_INFO("index %ld\n", index);
91         return b->priv.vt->store(b, b->priv.blk_start + index);
92 }
93
94 INLINE void kblock_setDirty(struct KBlock *b, bool dirty)
95 {
96         if (dirty)
97                 b->priv.flags |= KB_CACHE_DIRTY;
98         else
99                 b->priv.flags &= ~KB_CACHE_DIRTY;
100 }
101
102
103 size_t kblock_read(struct KBlock *b, block_idx_t idx, void *buf, size_t offset, size_t size)
104 {
105         ASSERT(b);
106         ASSERT(buf);
107         ASSERT(offset + size <= b->blk_size);
108         LOG_INFO("blk_idx %ld, offset %u, size %u\n", idx, offset, size);
109
110         if (kblock_buffered(b) && idx == b->priv.curr_blk)
111                 return kblock_readBuf(b, buf, offset, size);
112         else
113                 return kblock_readDirect(b, idx, buf, offset, size);
114 }
115
116
117 int kblock_flush(struct KBlock *b)
118 {
119         ASSERT(b);
120
121         if (!kblock_buffered(b))
122                 return 0;
123
124         if (kblock_cacheDirty(b))
125         {
126                 LOG_INFO("flushing block %ld\n", b->priv.curr_blk);
127                 if (kblock_store(b, b->priv.curr_blk) == 0)
128                         kblock_setDirty(b, false);
129                 else
130                         return EOF;
131         }
132         return 0;
133 }
134
135
136 static bool kblock_loadPage(struct KBlock *b, block_idx_t idx)
137 {
138         ASSERT(b);
139
140         if (idx != b->priv.curr_blk)
141         {
142                 LOG_INFO("loading block %ld\n", idx);
143                 if (kblock_flush(b) != 0 || kblock_load(b, idx) != 0)
144                                 return false;
145
146                 b->priv.curr_blk = idx;
147         }
148         return true;
149 }
150
151
152 int kblock_trim(struct KBlock *b, block_idx_t start, block_idx_t count)
153 {
154         ASSERT(start + count <= b->blk_cnt);
155
156         if (kblock_buffered(b))
157         {
158                 if (!kblock_loadPage(b, start))
159                         return EOF;
160         }
161
162         b->priv.blk_start += start;
163         b->priv.curr_blk = 0; // adjust logical address
164         b->blk_cnt = count;
165         return 0;
166 }
167
168
169 size_t kblock_write(struct KBlock *b, block_idx_t idx, const void *buf, size_t offset, size_t size)
170 {
171         ASSERT(b);
172         ASSERT(buf);
173         ASSERT(idx < b->blk_cnt);
174         ASSERT(offset + size <= b->blk_size);
175
176         LOG_INFO("blk_idx %ld, offset %u, size %u\n", idx, offset, size);
177
178         if (kblock_buffered(b))
179         {
180                 if (!kblock_loadPage(b, idx))
181                         return 0;
182
183                 kblock_setDirty(b, true);
184                 return kblock_writeBuf(b, buf, offset, size);
185         }
186         else
187         {
188                 #ifdef _DEBUG
189                 if (offset != 0 || size != b->blk_size)
190                         ASSERT(kblock_partialWrite(b));
191                 #endif
192                 return kblock_writeDirect(b, idx, buf, offset, size);
193         }
194 }
195
196 int kblock_copy(struct KBlock *b, block_idx_t src, block_idx_t dest)
197 {
198         ASSERT(b);
199         ASSERT(src < b->blk_cnt);
200         ASSERT(dest < b->blk_cnt);
201
202         if (kblock_buffered(b))
203         {
204                 if (!kblock_loadPage(b, src))
205                         return EOF;
206
207                 b->priv.curr_blk = dest;
208                 kblock_setDirty(b, true);
209                 return 0;
210         }
211         else if (kblock_partialWrite(b))
212         {
213                 uint8_t buf[16];
214                 size_t blk_size = b->blk_size;
215                 size_t offset = 0;
216
217                 while (blk_size)
218                 {
219                         size_t size = MIN(sizeof(buf), blk_size);
220                         if (kblock_readDirect(b, src, buf, offset, size) != size)
221                                 return EOF;
222                         if (kblock_writeDirect(b, dest, buf, offset, size) != size)
223                                 return EOF;
224
225                         blk_size -= size;
226                         offset += size;
227                 }
228                 return 0;
229         }
230         else
231         {
232                 ASSERT(0);
233                 return EOF;
234         }
235 }
236
237 int kblock_swLoad(struct KBlock *b, block_idx_t index)
238 {
239         /*
240          * Since this is a low level API, the index here is a fisical index.
241          * If we call another low level API, logical to fisical translation
242          * would be applied twice.
243          * In order to avoid this we subtract the start block index.
244          */
245         ASSERT(index >= b->priv.blk_start);
246         return (kblock_readDirect(b, index - b->priv.blk_start, b->priv.buf, 0, b->blk_size) == b->blk_size) ? 0 : EOF;
247 }
248
249 int kblock_swStore(struct KBlock *b, block_idx_t index)
250 {
251         /*
252          * Since this is a low level API, the index here is a fisical index.
253          * If we call another low level API, logical to fisical translation
254          * would be applied twice.
255          * In order to avoid this we subtract the start block index.
256          */
257         ASSERT(index >= b->priv.blk_start);
258         return (kblock_writeDirect(b, index - b->priv.blk_start, b->priv.buf, 0, b->blk_size) == b->blk_size) ? 0 : EOF;
259 }
260
261 size_t kblock_swReadBuf(struct KBlock *b, void *buf, size_t offset, size_t size)
262 {
263         ASSERT(buf);
264         ASSERT(offset + size <= b->blk_size);
265
266         memcpy(buf, (uint8_t *)b->priv.buf + offset, size);
267         return size;
268 }
269
270 size_t kblock_swWriteBuf(struct KBlock *b, const void *buf, size_t offset, size_t size)
271 {
272         ASSERT(buf);
273         ASSERT(offset + size <= b->blk_size);
274         memcpy((uint8_t *)b->priv.buf + offset, buf, size);
275         return size;
276 }
277
278 int kblock_swClose(UNUSED_ARG(struct KBlock, *b))
279 {
280         return 0;
281 }