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/)
33 * \brief Virtual KFile I/O interface.
34 * This module implement a standard fd.seek and a kfile
38 * \author Francesco Sacchi <batt@develer.com>
39 * \author Daniele Basile <asterix@develer.com>
43 #include <kern/kfile.h>
44 #include <cfg/debug.h>
48 #include <appconfig.h>
51 * Move \a fd file seek position of \a offset bytes
52 * from current position.
53 * This is a generic implementation of seek function, you should redefine
54 * it in your local module.
56 int32_t kfile_seek(struct _KFile *fd, kfile_off_t offset, KSeekMode whence)
67 seek_pos = fd->size - 1;
70 seek_pos = fd->seek_pos;
80 if (seek_pos + offset > fd->size)
86 fd->seek_pos = seek_pos + offset;
87 kprintf("Flash seek to [%lu]\n", fd->seek_pos);
95 * Program memory read/write subtest.
96 * Try to write/read in the same \a f file location \a _size bytes.
97 * \return true if all is ok, false otherwise
98 * \note Restore file position at exit (if no error)
99 * \note Test buffer \a buf must be filled with
100 * the following statement:
105 static bool kfile_rwTest(KFile *f, uint8_t *buf, size_t _size)
107 int32_t size = _size;
110 if (f->write(f, buf, size) != size)
112 f->seek(f, -size, KSM_SEEK_CUR);
115 memset(buf, 0, size);
117 // Read flash in test buffer
118 if (f->read(f, buf, size) != size)
120 f->seek(f, -size, KSM_SEEK_CUR);
123 for (size_t i = 0; i < size; i++)
124 if (buf[i] != (i & 0xff))
131 * Test for program memory read/write.
132 * This function write and read \p test_buf long \p _size
133 * on \p fd handler. If you want not overwrite exist data
134 * you should pass an \p save_buf where test store exist data,
135 * otherwise su must pass NULL.
137 * \note some device (like flash memeory) not allow write on
138 * existing data, and so this test use ASSERT macro to warn you if
139 * you are writing on same fd.seek_pos.
142 bool kfile_test(uint8_t *test_buf, size_t _size , uint8_t *save_buf, size_t save_buf_size)
145 int32_t size = _size;
148 * Part of test buf size that you would write.
149 * This var is useded in test 3 to check fd.write
150 * when write outside size limit. Normaly we want
151 * perform a write until is space to write, otherwise
154 int32_t len = size/2;
159 fd.open(&fd, NULL, 0);
160 kprintf("Opened fd handler..\n");
163 * If necessary, user could save content,
166 if (save_buf != NULL)
168 fd.read(&fd, save_buf, save_buf_size);
169 kprintf("Saved content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + save_buf_size);
173 kprintf("Test 1: write from pos 0 to [%lu]\n", fd.size);
178 if (fd.seek(&fd, 0, KSM_SEEK_SET) != 0)
181 kprintf("Seek to [%lu], expected[0]\n", fd.seek_pos);
184 * Test flash read/write to address 0..size
186 if (!kfile_rwTest(&fd, test_buf, size))
189 kprintf("Test 1: ok!\n");
192 * Restore previous read content
194 if (save_buf != NULL)
196 fd.seek(&fd, 0, KSM_SEEK_SET);
198 if (fd.write(&fd, save_buf, save_buf_size) != size)
201 kprintf("Restore content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + save_buf_size);
206 kprintf("Test 2: write from pos [%lu] to [%lu]\n", fd.size/2 , size);
209 * Go to half test size.
211 fd.seek(&fd, (fd.size/ 2), KSM_SEEK_SET);
213 kprintf("Seek to [%lu], expected[%lu]\n", fd.seek_pos, fd.size/2);
216 * If necessary, user could save content,
219 if (save_buf != NULL)
221 fd.read(&fd, save_buf, save_buf_size);
222 fd.seek(&fd, -size, KSM_SEEK_CUR);
223 kprintf("Saved content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + save_buf_size);
227 * Test flash read/write to address FLASHEND/2 ... FLASHEND/2 + size
229 if (!kfile_rwTest(&fd, test_buf, size))
232 kprintf("Test 2: ok!\n");
235 * Restore previous read content
237 if (save_buf != NULL)
239 fd.seek(&fd, -size, KSM_SEEK_CUR);
241 if (fd.write(&fd, save_buf, save_buf_size) != size)
244 kprintf("Restore content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + save_buf_size);
250 kprintf("Test 3: write outside of fd.size limit [%lu]\n", fd.size);
253 * Go to the Flash end
255 fd.seek(&fd, -len, KSM_SEEK_END);
256 kprintf("Seek to [%lu], expected[%lu]\n", fd.seek_pos, fd.size - len);
259 * If necessary, user could save content,
262 if (save_buf != NULL)
264 ASSERT(len > save_buf_size);
266 fd.read(&fd, save_buf, len);
267 fd.seek(&fd, -len, KSM_SEEK_CUR);
268 kprintf("Saved content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + len);
272 * Test flash read/write to address (FLASHEND - size) ... FLASHEND
274 if (!kfile_rwTest(&fd, test_buf, size))
277 kprintf("Test 3: ok !\n");
280 * Restore previous read content
282 if (save_buf != NULL)
284 fd.seek(&fd, -len, KSM_SEEK_END);
286 if (fd.write(&fd, save_buf, len) != len)
289 kprintf("Restore content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + len);
302 #endif /* CONFIG_TEST */