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 2010 Develer S.r.l. (http://www.develer.com/)
33 * \author Francesco Sacchi <batt@develer.com>
35 * \brief KBlock interface
41 #define LOG_LEVEL LOG_LVL_ERR
42 #define LOG_FORMAT LOG_FMT_VERBOSE
47 INLINE size_t kblock_readDirect(struct KBlock *b, block_idx_t index, void *buf, size_t offset, size_t size)
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);
54 INLINE size_t kblock_writeDirect(struct KBlock *b, block_idx_t index, const void *buf, size_t offset, size_t size)
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);
61 INLINE size_t kblock_readBuf(struct KBlock *b, void *buf, size_t offset, size_t size)
63 KB_ASSERT_METHOD(b, readBuf);
64 ASSERT(offset + size <= b->blk_size);
66 return b->priv.vt->readBuf(b, buf, offset, size);
69 INLINE size_t kblock_writeBuf(struct KBlock *b, const void *buf, size_t offset, size_t size)
71 KB_ASSERT_METHOD(b, writeBuf);
72 ASSERT(offset + size <= b->blk_size);
73 return b->priv.vt->writeBuf(b, buf, offset, size);
76 INLINE int kblock_load(struct KBlock *b, block_idx_t index)
78 KB_ASSERT_METHOD(b, load);
79 ASSERT(index < b->blk_cnt);
81 LOG_INFO("index %ld\n", index);
82 return b->priv.vt->load(b, b->priv.blk_start + index);
85 INLINE int kblock_store(struct KBlock *b, block_idx_t index)
87 KB_ASSERT_METHOD(b, store);
88 ASSERT(index < b->blk_cnt);
90 LOG_INFO("index %ld\n", index);
91 return b->priv.vt->store(b, b->priv.blk_start + index);
94 INLINE void kblock_setDirty(struct KBlock *b, bool dirty)
97 b->priv.flags |= KB_CACHE_DIRTY;
99 b->priv.flags &= ~KB_CACHE_DIRTY;
104 size_t kblock_read(struct KBlock *b, block_idx_t idx, void *buf, size_t offset, size_t size)
108 ASSERT(offset + size <= b->blk_size);
109 LOG_INFO("blk_idx %ld, offset %u, size %u\n", idx, offset, size);
111 if (kblock_buffered(b) && idx == b->priv.curr_blk)
112 return kblock_readBuf(b, buf, offset, size);
114 return kblock_readDirect(b, idx, buf, offset, size);
118 int kblock_flush(struct KBlock *b)
122 if (!kblock_buffered(b))
125 if (kblock_cacheDirty(b))
127 LOG_INFO("flushing block %ld\n", b->priv.curr_blk);
128 if (kblock_store(b, b->priv.curr_blk) == 0)
129 kblock_setDirty(b, false);
137 static bool kblock_loadPage(struct KBlock *b, block_idx_t idx)
141 if (idx != b->priv.curr_blk)
143 LOG_INFO("loading block %ld\n", idx);
144 if (kblock_flush(b) != 0 || kblock_load(b, idx) != 0)
147 b->priv.curr_blk = idx;
153 size_t kblock_write(struct KBlock *b, block_idx_t idx, const void *buf, size_t offset, size_t size)
157 ASSERT(idx < b->blk_cnt);
158 ASSERT(offset + size <= b->blk_size);
160 LOG_INFO("blk_idx %ld, offset %u, size %u\n", idx, offset, size);
162 if (kblock_buffered(b))
164 if (!kblock_loadPage(b, idx))
167 kblock_setDirty(b, true);
168 return kblock_writeBuf(b, buf, offset, size);
173 if (offset != 0 || size != b->blk_size)
174 ASSERT(kblock_partialWrite(b));
176 return kblock_writeDirect(b, idx, buf, offset, size);
180 int kblock_copy(struct KBlock *b, block_idx_t src, block_idx_t dest)
183 ASSERT(src < b->blk_cnt);
184 ASSERT(dest < b->blk_cnt);
186 if (kblock_buffered(b))
188 if (!kblock_loadPage(b, src))
191 b->priv.curr_blk = dest;
192 kblock_setDirty(b, true);
195 else if (kblock_partialWrite(b))
198 size_t blk_size = b->blk_size;
203 size_t size = MIN(sizeof(buf), blk_size);
204 if (kblock_readDirect(b, src, buf, offset, size) != size)
206 if (kblock_writeDirect(b, dest, buf, offset, size) != size)
221 int kblock_swLoad(struct KBlock *b, block_idx_t index)
224 * Since this is a low level API, the index here is a fisical index.
225 * If we call another low level API, logical to fisical translation
226 * would be applied twice.
227 * In order to avoid this we subtract the start block index.
229 ASSERT(index >= b->priv.blk_start);
230 return (kblock_readDirect(b, index - b->priv.blk_start, b->priv.buf, 0, b->blk_size) == b->blk_size) ? 0 : EOF;
233 int kblock_swStore(struct KBlock *b, block_idx_t index)
236 * Since this is a low level API, the index here is a fisical index.
237 * If we call another low level API, logical to fisical translation
238 * would be applied twice.
239 * In order to avoid this we subtract the start block index.
241 ASSERT(index >= b->priv.blk_start);
242 return (kblock_writeDirect(b, index - b->priv.blk_start, b->priv.buf, 0, b->blk_size) == b->blk_size) ? 0 : EOF;
245 size_t kblock_swReadBuf(struct KBlock *b, void *buf, size_t offset, size_t size)
248 ASSERT(offset + size <= b->blk_size);
250 memcpy(buf, (uint8_t *)b->priv.buf + offset, size);
254 size_t kblock_swWriteBuf(struct KBlock *b, const void *buf, size_t offset, size_t size)
257 ASSERT(offset + size <= b->blk_size);
258 memcpy((uint8_t *)b->priv.buf + offset, buf, size);
262 int kblock_swClose(UNUSED_ARG(struct KBlock, *b))