Add block start address even in direct Reads, add some comments.
[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 2009 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
46 INLINE size_t kblock_readDirect(struct KBlock *b, block_idx_t index, void *buf, size_t offset, size_t size)
47 {
48         KB_ASSERT_METHOD(b, readDirect);
49         return b->priv.vt->readDirect(b, b->priv.blk_start + index, buf, offset, size);
50 }
51
52 INLINE size_t kblock_readBuf(struct KBlock *b, void *buf, size_t offset, size_t size)
53 {
54         KB_ASSERT_METHOD(b, readBuf);
55         ASSERT(offset + size <= b->blk_size);
56
57         return b->priv.vt->readBuf(b, buf, offset, size);
58 }
59
60 INLINE size_t kblock_writeBuf(struct KBlock *b, const void *buf, size_t offset, size_t size)
61 {
62         KB_ASSERT_METHOD(b, writeBuf);
63         ASSERT(offset + size <= b->blk_size);
64         return b->priv.vt->writeBuf(b, buf, offset, size);
65 }
66
67 INLINE int kblock_load(struct KBlock *b, block_idx_t index)
68 {
69         KB_ASSERT_METHOD(b, load);
70         ASSERT(index < b->blk_cnt);
71
72         LOG_INFO("index %d\n", index);
73         return b->priv.vt->load(b, b->priv.blk_start + index);
74 }
75
76 INLINE int kblock_store(struct KBlock *b, block_idx_t index)
77 {
78         KB_ASSERT_METHOD(b, store);
79         ASSERT(index < b->blk_cnt);
80
81         LOG_INFO("index %d\n", index);
82         return b->priv.vt->store(b, b->priv.blk_start + index);
83 }
84
85
86
87 size_t kblock_read(struct KBlock *b, block_idx_t idx, void *_buf, size_t offset, size_t size)
88 {
89         size_t tot_rd = 0;
90         uint8_t *buf = (uint8_t *)_buf;
91
92         ASSERT(b);
93         ASSERT(buf);
94         LOG_INFO("blk_idx %d, offset %d, size %d\n", idx, offset, size);
95
96         while (size)
97         {
98                 size_t len = MIN(size, b->blk_size - offset);
99                 size_t rlen;
100
101                 if (idx == b->priv.curr_blk)
102                         rlen = kblock_readBuf(b, buf, offset, len);
103                 else
104                         rlen = kblock_readDirect(b, idx, buf, offset, len);
105
106                 tot_rd += rlen;
107                 if (rlen != len)
108                         break;
109
110                 idx++;
111                 size -= rlen;
112                 offset = 0;
113                 buf += rlen;
114         }
115
116         return tot_rd;
117 }
118
119
120 int kblock_flush(struct KBlock *b)
121 {
122         ASSERT(b);
123
124         if (b->priv.cache_dirty)
125         {
126                 LOG_INFO("flushing block %d\n", b->priv.curr_blk);
127                 if (kblock_store(b, b->priv.curr_blk) == 0)
128                         b->priv.cache_dirty = 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 %d\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 size_t kblock_write(struct KBlock *b, block_idx_t idx, const void *_buf, size_t offset, size_t size)
153 {
154         size_t tot_wr = 0;
155         const uint8_t *buf = (const uint8_t *)_buf;
156
157         ASSERT(b);
158         ASSERT(buf);
159
160         LOG_INFO("blk_idx %d, offset %d, size %d\n", idx, offset, size);
161         while (size)
162         {
163                 size_t len = MIN(size, b->blk_size - offset);
164                 size_t wlen;
165
166                 if (!kblock_loadPage(b, idx))
167                         break;
168
169                 wlen = kblock_writeBuf(b, buf, offset, len);
170                 b->priv.cache_dirty = true;
171
172                 tot_wr += wlen;
173                 if (wlen != len)
174                         break;
175
176                 idx++;
177                 size -= wlen;
178                 offset = 0;
179                 buf += wlen;
180         }
181
182         return tot_wr;
183 }
184
185 int kblock_copy(struct KBlock *b, block_idx_t idx1, block_idx_t idx2)
186 {
187         ASSERT(b);
188
189         if (!kblock_loadPage(b, idx1))
190                 return EOF;
191
192         b->priv.curr_blk = idx2;
193         b->priv.cache_dirty = true;
194         return 0;
195 }
196