Paste. Fix test prototype. Remove unneeded code. Add header.
[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
49 /**
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.
54  */
55 int32_t kfile_seek(struct _KFile *fd, kfile_off_t offset, KSeekMode whence)
56 {
57         uint32_t seek_pos;
58
59         switch(whence)
60         {
61
62         case KSM_SEEK_SET:
63                 seek_pos = 0;
64                 break;
65         case KSM_SEEK_END:
66                 seek_pos = fd->size - 1;
67                 break;
68         case KSM_SEEK_CUR:
69                 seek_pos = fd->seek_pos;
70                 break;
71         default:
72                 ASSERT(0);
73                 return -1;
74                 break;
75
76         }
77
78         /* Bound check */
79         if (seek_pos + offset > fd->size)
80         {
81                 ASSERT(0);
82                 return -1;
83         }
84
85         fd->seek_pos = seek_pos + offset;
86         kprintf("Flash seek to [%lu]\n", fd->seek_pos);
87
88         return fd->seek_pos;
89 }
90
91 #if CONFIG_TEST
92
93 /**
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:
100  * <pre>
101  * buf[i] = i & 0xff
102  * </pre>
103  */
104 static bool kfile_rwTest(KFile *f, uint8_t *buf, size_t _size)
105 {
106         int32_t size = _size;
107
108         // Write test buffer
109         if (f->write(f, buf, size) != size)
110                 return false;
111         f->seek(f, -size, KSM_SEEK_CUR);
112
113         // Reset test buffer
114         memset(buf, 0, size);
115
116         // Read flash in test buffer
117         if (f->read(f, buf, size) != size)
118                 return false;
119         f->seek(f, -size, KSM_SEEK_CUR);
120
121         // Check test result
122         for (size_t i = 0; i < size; i++)
123                 if (buf[i] != (i & 0xff))
124                         return false;
125
126         return true;
127 }
128
129 /**
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.
135  *
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.
139  *
140  */
141 bool kfile_test(uint8_t *test_buf, size_t _size , uint8_t *save_buf, size_t save_buf_size)
142 {
143         KFile fd;
144         int32_t size = _size;
145
146         /*
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
151          * we return.
152          */
153         int32_t len = size/2;
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", fd.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         kprintf("Seek to [%lu], expected[0]\n", fd.seek_pos);
181
182         /*
183          * Test flash read/write to address 0..size
184          */
185         if (!kfile_rwTest(&fd, test_buf, size))
186                 goto kfile_test_end;
187
188         kprintf("Test 1: ok!\n");
189
190         /*
191          * Restore previous read content
192          */
193         if (save_buf != NULL)
194         {
195                 fd.seek(&fd, 0, KSM_SEEK_SET);
196
197                 if (fd.write(&fd, save_buf, save_buf_size) != size)
198                         goto kfile_test_end;
199
200                 kprintf("Restore content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + save_buf_size);
201         }
202         /* TEST 1 END. */
203
204         /* TEST 2 BEGIN. */
205         kprintf("Test 2: write from pos [%lu] to [%lu]\n", fd.size/2 , size);
206
207         /*
208          * Go to half test size.
209          */
210         fd.seek(&fd, (fd.size/ 2), KSM_SEEK_SET);
211
212         kprintf("Seek to [%lu], expected[%lu]\n", fd.seek_pos, fd.size/2);
213
214         /*
215          * If necessary, user could save content,
216          * for later restore.
217          */
218         if (save_buf != NULL)
219         {
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);
223         }
224
225         /*
226          * Test flash read/write to address FLASHEND/2 ... FLASHEND/2 + size
227          */
228         if (!kfile_rwTest(&fd, test_buf, size))
229                 goto kfile_test_end;
230
231         kprintf("Test 2: ok!\n");
232
233         /*
234          * Restore previous read content
235          */
236         if (save_buf != NULL)
237         {
238                 fd.seek(&fd, -size, KSM_SEEK_CUR);
239
240                 if (fd.write(&fd, save_buf, save_buf_size) != size)
241                         goto kfile_test_end;
242
243                 kprintf("Restore content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + save_buf_size);
244         }
245
246         /* TEST 2 END. */
247
248         /* TEST 3 BEGIN. */
249         kprintf("Test 3: write outside of fd.size limit [%lu]\n", fd.size);
250
251         /*
252          * Go to the Flash end
253          */
254         fd.seek(&fd, -len, KSM_SEEK_END);
255         kprintf("Seek to [%lu], expected[%lu]\n", fd.seek_pos, fd.size - len);
256
257         /*
258          * If necessary, user could save content,
259          * for later restore.
260          */
261         if (save_buf != NULL)
262         {
263                 ASSERT(len > save_buf_size);
264
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);
268         }
269
270         /*
271          * Test flash read/write to address (FLASHEND - size) ... FLASHEND
272          */
273         if (!kfile_rwTest(&fd, test_buf, size))
274                 goto kfile_test_end;
275
276         kprintf("Test 3: ok !\n");
277
278         /*
279          * Restore previous read content
280          */
281         if (save_buf != NULL)
282         {
283                 fd.seek(&fd, -len, KSM_SEEK_END);
284
285                 if (fd.write(&fd, save_buf, len) != len)
286                         goto kfile_test_end;
287
288                 kprintf("Restore content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + len);
289         }
290
291         /* TEST 3 END. */
292
293         fd.close(&fd);
294         return true;
295
296 kfile_test_end:
297         fd.close(&fd);
298         return false;
299 }
300
301 #endif /* CONFIG_TEST */