Remove unneeded debug message. Fix test function.
[bertos.git] / kern / kfile.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 2007 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief Virtual KFile I/O interface.
34  * This module implement a standard fd.seek and a kfile
35  * test function.
36  *
37  * \version $Id$
38  * \author Francesco Sacchi <batt@develer.com>
39  * \author Daniele Basile <asterix@develer.com>
40  *
41  */
42
43 #include <kern/kfile.h>
44 #include <cfg/debug.h>
45
46 #include <string.h>
47
48 #include <appconfig.h>
49
50 /**
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.
55  */
56 kfile_off_t kfile_seek(struct _KFile *fd, kfile_off_t offset, KSeekMode whence)
57 {
58         uint32_t seek_pos;
59
60         switch(whence)
61         {
62
63         case KSM_SEEK_SET:
64                 seek_pos = 0;
65                 break;
66         case KSM_SEEK_END:
67                 seek_pos = fd->size - 1;
68                 break;
69         case KSM_SEEK_CUR:
70                 seek_pos = fd->seek_pos;
71                 break;
72         default:
73                 ASSERT(0);
74                 return -1;
75                 break;
76
77         }
78
79         /* Bound check */
80         if (seek_pos + offset > fd->size)
81         {
82                 ASSERT(0);
83                 return -1;
84         }
85
86         fd->seek_pos = seek_pos + offset;
87         kprintf("Flash seek to [%lu]\n", fd->seek_pos);
88
89         return fd->seek_pos;
90 }
91
92 #if CONFIG_TEST
93
94 /**
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:
101  * <pre>
102  * buf[i] = i & 0xff
103  * </pre>
104  */
105 static bool kfile_rwTest(KFile *f, uint8_t *buf, size_t _size)
106 {
107         int32_t size = _size;
108
109         // Write test buffer
110         if (f->write(f, buf, size) != size)
111                 return false;
112         f->seek(f, -size, KSM_SEEK_CUR);
113
114         // Reset test buffer
115         memset(buf, 0, size);
116
117         // Read flash in test buffer
118         if (f->read(f, buf, size) != size)
119                 return false;
120         f->seek(f, -size, KSM_SEEK_CUR);
121
122         // Check test result
123         for (size_t i = 0; i < size; i++)
124                 if (buf[i] != (i & 0xff))
125                         return false;
126
127         return true;
128 }
129
130 /**
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.
136  */
137 bool kfile_test(KFile *fd, uint8_t *test_buf, size_t _size , uint8_t *save_buf, size_t save_buf_size)
138 {
139         int32_t size = _size;
140
141         /*
142          * Part of test buf size that you would write.
143          * This var is useded in test 3 to check fd.write
144          * when write outside size limit. Normaly we want
145          * perform a write until is space to write, otherwise
146          * we return.
147          */
148         int32_t len = size/2;
149
150
151         /* Fill test buffer */
152         for (size_t i = 0; i < size; i++)
153                 test_buf[i] = (i & 0xff);
154
155         /*
156          * Open fd handler
157          */
158         fd->open(fd, NULL, 0);
159         kprintf("Opened fd handler..\n");
160
161         /*
162          * If necessary, user could save content,
163          * for later restore.
164          */
165         if (save_buf != NULL)
166         {
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);
169         }
170
171         /* TEST 1 BEGIN. */
172         kprintf("Test 1: write from pos 0 to [%lu]\n", size);
173
174         /*
175          * Seek to addr 0
176          */
177         if (fd->seek(fd, 0, KSM_SEEK_SET) != 0)
178                 goto kfile_test_end;
179
180         /*
181          * Test flash read/write to address 0..size
182          */
183         if (!kfile_rwTest(fd, test_buf, size))
184                 goto kfile_test_end;
185
186         kprintf("Test 1: ok!\n");
187
188         /*
189          * Restore previous read content
190          */
191         if (save_buf != NULL)
192         {
193                 fd->seek(fd, 0, KSM_SEEK_SET);
194
195                 if (fd->write(fd, save_buf, save_buf_size) != size)
196                         goto kfile_test_end;
197
198                 kprintf("Restore content..form [%lu] to [%lu]\n", fd->seek_pos, fd->seek_pos + save_buf_size);
199         }
200         /* TEST 1 END. */
201
202         /* TEST 2 BEGIN. */
203         kprintf("Test 2: write from pos [%lu] to [%lu]\n", fd->size/2 , size);
204
205         /*
206          * Go to half test size.
207          */
208         fd->seek(fd, (fd->size/ 2), KSM_SEEK_SET);
209
210         /*
211          * If necessary, user could save content,
212          * for later restore.
213          */
214         if (save_buf != NULL)
215         {
216                 fd->read(fd, save_buf, save_buf_size);
217                 fd->seek(fd, -size, KSM_SEEK_CUR);
218                 kprintf("Saved content..form [%lu] to [%lu]\n", fd->seek_pos, fd->seek_pos + save_buf_size);
219         }
220
221         /*
222          * Test flash read/write to address FLASHEND/2 ... FLASHEND/2 + size
223          */
224         if (!kfile_rwTest(fd, test_buf, size))
225                 goto kfile_test_end;
226
227         kprintf("Test 2: ok!\n");
228
229         /*
230          * Restore previous read content
231          */
232         if (save_buf != NULL)
233         {
234                 fd->seek(fd, -size, KSM_SEEK_CUR);
235
236                 if (fd->write(fd, save_buf, save_buf_size) != size)
237                         goto kfile_test_end;
238
239                 kprintf("Restore content..form [%lu] to [%lu]\n", fd->seek_pos, fd->seek_pos + save_buf_size);
240         }
241
242         /* TEST 2 END. */
243
244         /* TEST 3 BEGIN. */
245         kprintf("Test 3: write outside of fd->size limit [%lu]\n", fd->size);
246
247         /*
248          * Go to the Flash end
249          */
250         fd->seek(fd, -len, KSM_SEEK_END);
251
252         /*
253          * If necessary, user could save content,
254          * for later restore.
255          */
256         if (save_buf != NULL)
257         {
258                 ASSERT(len > save_buf_size);
259
260                 fd->read(fd, save_buf, len);
261                 fd->seek(fd, -len, KSM_SEEK_CUR);
262                 kprintf("Saved content..form [%lu] to [%lu]\n", fd->seek_pos, fd->seek_pos + len);
263         }
264
265         /*
266          * Test flash read/write to address (FLASHEND - size) ... FLASHEND
267          */
268         if (kfile_rwTest(fd, test_buf, size))
269                 goto kfile_test_end;
270
271         kprintf("Test 3: ok !\n");
272
273         /*
274          * Restore previous read content
275          */
276         if (save_buf != NULL)
277         {
278                 fd->seek(fd, -len, KSM_SEEK_END);
279
280                 if (fd->write(fd, save_buf, len) != len)
281                         goto kfile_test_end;
282
283                 kprintf("Restore content..form [%lu] to [%lu]\n", fd->seek_pos, fd->seek_pos + len);
284         }
285
286         /* TEST 3 END. */
287
288         fd->close(fd);
289         return true;
290
291 kfile_test_end:
292         fd->close(fd);
293         return false;
294 }
295
296 #endif /* CONFIG_TEST */