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>
50 * Move \a fd file seek position of \a offset bytes
51 * from current position.
52 * This is a generic implementation of seek function, you should redefine
53 * it in your local module.
55 int32_t kfile_seek(struct _KFile *fd, kfile_off_t offset, KSeekMode whence)
66 seek_pos = fd->size - 1;
69 seek_pos = fd->seek_pos;
79 if (seek_pos + offset > fd->size)
85 fd->seek_pos = seek_pos + offset;
86 kprintf("Flash seek to [%lu]\n", fd->seek_pos);
94 * Program memory read/write subtest.
95 * Try to write/read in the same \a f file location \a _size bytes.
96 * \return true if all is ok, false otherwise
97 * \note Restore file position at exit (if no error)
98 * \note Test buffer \a buf must be filled with
99 * the following statement:
104 static bool kfile_rwTest(KFile *f, uint8_t *buf, size_t _size)
106 int32_t size = _size;
109 if (f->write(f, buf, size) != size)
111 f->seek(f, -size, KSM_SEEK_CUR);
114 memset(buf, 0, size);
116 // Read flash in test buffer
117 if (f->read(f, buf, size) != size)
119 f->seek(f, -size, KSM_SEEK_CUR);
122 for (size_t i = 0; i < size; i++)
123 if (buf[i] != (i & 0xff))
130 * Test for program memory read/write.
131 * This function write and read \p test_buf long \p _size
132 * on \p fd handler. If you want not overwrite exist data
133 * you should pass an \p save_buf where test store exist data,
134 * otherwise su must pass NULL.
136 * \note some device (like flash memeory) not allow write on
137 * existing data, and so this test use ASSERT macro to warn you if
138 * you are writing on same fd.seek_pos.
141 bool kfile_test(uint8_t *test_buf, size_t _size , uint8_t *save_buf, size_t save_buf_size)
144 int32_t size = _size;
147 * Part of test buf size that you would write.
148 * This var is useded in test 3 to check fd.write
149 * when write outside size limit. Normaly we want
150 * perform a write until is space to write, otherwise
153 int32_t len = size/2;
158 fd.open(&fd, NULL, 0);
159 kprintf("Opened fd handler..\n");
162 * If necessary, user could save content,
165 if (save_buf != NULL)
167 fd.read(&fd, save_buf, save_buf_size);
168 kprintf("Saved content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + save_buf_size);
172 kprintf("Test 1: write from pos 0 to [%lu]\n", fd.size);
177 if (fd.seek(&fd, 0, KSM_SEEK_SET) != 0)
180 kprintf("Seek to [%lu], expected[0]\n", fd.seek_pos);
183 * Test flash read/write to address 0..size
185 if (!kfile_rwTest(&fd, test_buf, size))
188 kprintf("Test 1: ok!\n");
191 * Restore previous read content
193 if (save_buf != NULL)
195 fd.seek(&fd, 0, KSM_SEEK_SET);
197 if (fd.write(&fd, save_buf, save_buf_size) != size)
200 kprintf("Restore content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + save_buf_size);
205 kprintf("Test 2: write from pos [%lu] to [%lu]\n", fd.size/2 , size);
208 * Go to half test size.
210 fd.seek(&fd, (fd.size/ 2), KSM_SEEK_SET);
212 kprintf("Seek to [%lu], expected[%lu]\n", fd.seek_pos, fd.size/2);
215 * If necessary, user could save content,
218 if (save_buf != NULL)
220 fd.read(&fd, save_buf, save_buf_size);
221 fd.seek(&fd, -size, KSM_SEEK_CUR);
222 kprintf("Saved content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + save_buf_size);
226 * Test flash read/write to address FLASHEND/2 ... FLASHEND/2 + size
228 if (!kfile_rwTest(&fd, test_buf, size))
231 kprintf("Test 2: ok!\n");
234 * Restore previous read content
236 if (save_buf != NULL)
238 fd.seek(&fd, -size, KSM_SEEK_CUR);
240 if (fd.write(&fd, save_buf, save_buf_size) != size)
243 kprintf("Restore content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + save_buf_size);
249 kprintf("Test 3: write outside of fd.size limit [%lu]\n", fd.size);
252 * Go to the Flash end
254 fd.seek(&fd, -len, KSM_SEEK_END);
255 kprintf("Seek to [%lu], expected[%lu]\n", fd.seek_pos, fd.size - len);
258 * If necessary, user could save content,
261 if (save_buf != NULL)
263 ASSERT(len > save_buf_size);
265 fd.read(&fd, save_buf, len);
266 fd.seek(&fd, -len, KSM_SEEK_CUR);
267 kprintf("Saved content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + len);
271 * Test flash read/write to address (FLASHEND - size) ... FLASHEND
273 if (!kfile_rwTest(&fd, test_buf, size))
276 kprintf("Test 3: ok !\n");
279 * Restore previous read content
281 if (save_buf != NULL)
283 fd.seek(&fd, -len, KSM_SEEK_END);
285 if (fd.write(&fd, save_buf, len) != len)
288 kprintf("Restore content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + len);
301 #endif /* CONFIG_TEST */