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, 2008 Develer S.r.l. (http://www.develer.com/)
35 * \author Francesco Sacchi <batt@develer.com>
38 #include <fs/battfs.h>
40 #include <cfg/debug.h>
47 #define FILE_SIZE 32768
49 #define PAGE_COUNT FILE_SIZE / PAGE_SIZE
54 const char test_filename[]="battfs_disk.bin";
57 static bool disk_open(struct BattFsSuper *d)
59 fp = fopen(test_filename, "r+b");
61 fseek(fp, 0, SEEK_END);
62 d->page_size = PAGE_SIZE;
63 d->page_count = ftell(fp) / d->page_size;
64 d->page_array = malloc(d->page_count * sizeof(pgcnt_t));
65 //TRACEMSG("page_size:%d, page_count:%d\n", d->page_size, d->page_count);
66 return (fp && d->page_array);
69 static size_t disk_page_read(struct BattFsSuper *d, pgcnt_t page, pgaddr_t addr, void *buf, size_t size)
71 //TRACEMSG("page:%d, addr:%d, size:%d\n", page, addr, size);
72 fseek(fp, page * d->page_size + addr, SEEK_SET);
73 return fread(buf, 1, size, fp);
76 static size_t disk_page_write(struct BattFsSuper *d, pgcnt_t page, pgaddr_t addr, const void *buf, size_t size)
78 //TRACEMSG("page:%d, addr:%d, size:%d\n", page, addr, size);
79 fseek(fp, page * d->page_size + addr, SEEK_SET);
80 return fwrite(buf, 1, size, fp);
83 static bool disk_page_erase(struct BattFsSuper *d, pgcnt_t page)
85 //TRACEMSG("page:%d\n", page);
86 fseek(fp, page * d->page_size, SEEK_SET);
88 for (int i = 0; i < d->page_size; i++)
89 if (fputc(0xff, fp) == EOF)
94 static bool disk_close(struct BattFsSuper *d)
98 return (fclose(fp) != EOF);
101 static void testCheck(BattFsSuper *disk, pgcnt_t *reference)
103 ASSERT(battfs_init(disk));
105 for (int i = 0; i < disk->page_count; i++)
107 if (disk->page_array[i] != reference[i])
109 kprintf("Error at addr %d: page_array read", i);
110 for (pgcnt_t i = 0; i < disk->page_count; i++)
114 kprintf("%04d ", disk->page_array[i]);
117 kprintf("Expected:");
118 for (pgcnt_t i = 0; i < disk->page_count; i++)
122 kprintf("%04d ", reference[i]);
132 static void test1(BattFsSuper *disk)
134 pgcnt_t ref[PAGE_COUNT];
135 kprintf("Test1: disk new\n");
137 FILE *fpt = fopen(test_filename, "w+");
139 for (int i = 0; i < FILE_SIZE; i++)
142 for (int i = 0; i < PAGE_COUNT; i++)
143 ref[i] = PAGE_COUNT - i - 1;
145 testCheck(disk, ref);
146 kprintf("Test1: passed\n");
149 static void test2(BattFsSuper *disk)
151 pgcnt_t ref[PAGE_COUNT];
152 kprintf("Test2: disk full with 1 contiguos file\n");
155 fp = fopen(test_filename, "w+");
157 for (int i = 0; i < PAGE_COUNT; i++)
159 battfs_writeTestBlock(disk, i, 0, 0, 0, i, MARK_PAGE_VALID);
164 testCheck(disk, ref);
165 kprintf("Test2: passed\n");
169 static void test3(BattFsSuper *disk)
171 pgcnt_t ref[PAGE_COUNT];
172 kprintf("Test3: disk half full with 1 contiguos file, rest unformatted\n");
175 fp = fopen(test_filename, "w+");
177 for (int i = 0; i < PAGE_COUNT / 2; i++)
179 battfs_writeTestBlock(disk, i, 0, 0, 0, i, MARK_PAGE_VALID);
182 fseek(fp, FILE_SIZE / 2, SEEK_SET);
183 for (int i = FILE_SIZE / 2; i < FILE_SIZE; i++)
187 for (int i = PAGE_COUNT / 2; i < PAGE_COUNT; i++)
189 ref[i] = PAGE_COUNT + PAGE_COUNT / 2 - i - 1;
193 testCheck(disk, ref);
194 kprintf("Test3: passed\n");
197 static void test4(BattFsSuper *disk)
199 pgcnt_t ref[PAGE_COUNT];
200 kprintf("Test4: disk half full with 1 contiguos file, rest marked free\n");
203 fp = fopen(test_filename, "w+");
205 for (int i = 0; i < PAGE_COUNT / 2; i++)
207 battfs_writeTestBlock(disk, i, 0, 0, 0, i, MARK_PAGE_VALID);
210 for (int i = PAGE_COUNT / 2; i < PAGE_COUNT; i++)
212 battfs_writeTestBlock(disk, i, 0, 0, 0, i, i);
218 testCheck(disk, ref);
219 kprintf("Test4: passed\n");
222 static void test5(BattFsSuper *disk)
224 pgcnt_t ref[PAGE_COUNT];
225 kprintf("Test5: disk 1/3 full with 1 contiguos file, 1/3 marked free, rest unformatted\n");
228 fp = fopen(test_filename, "w+");
230 for (int i = 0; i < FILE_SIZE; i++)
233 for (int i = 0; i < PAGE_COUNT / 3; i++)
235 battfs_writeTestBlock(disk, i, 0, 0, 0, i, MARK_PAGE_VALID);
238 for (int i = PAGE_COUNT / 3; i < 2 * (PAGE_COUNT / 3); i++)
240 battfs_writeTestBlock(disk, i, 0, 0, 0, i, i);
241 ref[i + PAGE_COUNT / 3 + 1] = i;
245 for (int i = PAGE_COUNT / 3; i < 2 * (PAGE_COUNT / 3) + 1; i++)
246 ref[i] = PAGE_COUNT + PAGE_COUNT / 3 - i - 1;
248 testCheck(disk, ref);
249 kprintf("Test5: passed\n");
252 static void test6(BattFsSuper *disk)
255 kprintf("Test6: 1 file with 1 old seq num, 1 free block\n");
258 fp = fopen(test_filename, "w+");
260 battfs_writeTestBlock(disk, 0, 0, 0, 0, 0, MARK_PAGE_VALID);
261 battfs_writeTestBlock(disk, 1, 0, 0, 0, 1, MARK_PAGE_VALID);
262 battfs_writeTestBlock(disk, 2, 0, 1, 0, 1, MARK_PAGE_VALID);
263 battfs_writeTestBlock(disk, 3, 0, 0, 0, 0, 123);
271 testCheck(disk, ref);
272 kprintf("Test6: passed\n");
275 static void test7(BattFsSuper *disk)
278 kprintf("Test7: 1 file with 1 old seq num, 1 free block\n");
281 fp = fopen(test_filename, "w+");
283 battfs_writeTestBlock(disk, 0, 0, 0, 0, 0, MARK_PAGE_VALID);
284 battfs_writeTestBlock(disk, 1, 0, 1, 0, 1, MARK_PAGE_VALID);
285 battfs_writeTestBlock(disk, 2, 0, 0, 0, 1, MARK_PAGE_VALID);
286 battfs_writeTestBlock(disk, 3, 0, 0, 0, 0, 123);
294 testCheck(disk, ref);
295 kprintf("Test7: passed\n");
298 static void test8(BattFsSuper *disk)
301 kprintf("Test8: 1 file with 1 old seq num, 1 free block\n");
304 fp = fopen(test_filename, "w+");
306 battfs_writeTestBlock(disk, 0, 0, 0, 0, 0, 1235);
307 battfs_writeTestBlock(disk, 1, 0, 0, 0, 0, MARK_PAGE_VALID);
308 battfs_writeTestBlock(disk, 2, 0, 1, 0, 1, MARK_PAGE_VALID);
309 battfs_writeTestBlock(disk, 3, 0, 0, 0, 1, MARK_PAGE_VALID);
318 testCheck(disk, ref);
319 kprintf("Test8: passed\n");
322 static void test9(BattFsSuper *disk)
325 kprintf("Test9: 2 file with old seq num, 2 free block\n");
328 fp = fopen(test_filename, "w+");
330 battfs_writeTestBlock(disk, 0, 0, 0, 0, 0, 1235);
331 battfs_writeTestBlock(disk, 1, 0, 0, 0, 0, MARK_PAGE_VALID);
332 battfs_writeTestBlock(disk, 2, 0, 3, 0, 1, MARK_PAGE_VALID);
333 battfs_writeTestBlock(disk, 3, 0, 0, 0, 1, MARK_PAGE_VALID);
334 battfs_writeTestBlock(disk, 4, 0, 0, 0, 0, 1236);
335 battfs_writeTestBlock(disk, 5, 4, 0, 0, 0, MARK_PAGE_VALID);
336 battfs_writeTestBlock(disk, 6, 4, 1, 0, 1, MARK_PAGE_VALID);
337 battfs_writeTestBlock(disk, 7, 4, 0, 0, 1, MARK_PAGE_VALID);
350 testCheck(disk, ref);
351 kprintf("Test9: passed\n");
354 static void test10(BattFsSuper *disk)
358 kprintf("Test10: open file test, inode 0 and inode 4\n");
360 fp = fopen(test_filename, "w+");
362 unsigned int PAGE_FILL = 116;
363 unsigned int INODE = 0;
364 unsigned int INODE2 = 4;
365 unsigned int INEXISTENT_INODE = 123;
366 unsigned int MODE = 0;
368 battfs_writeTestBlock(disk, 0, 123, 0, PAGE_FILL, 0, 1235);
369 battfs_writeTestBlock(disk, 1, INODE, 0, PAGE_FILL, 0, MARK_PAGE_VALID);
370 battfs_writeTestBlock(disk, 2, INODE, 3, PAGE_FILL, 1, MARK_PAGE_VALID);
371 battfs_writeTestBlock(disk, 3, INODE, 0, PAGE_FILL, 1, MARK_PAGE_VALID);
372 battfs_writeTestBlock(disk, 4, INODE2, 0, PAGE_FILL, 0, 1236);
373 battfs_writeTestBlock(disk, 5, INODE2, 0, PAGE_FILL, 0, MARK_PAGE_VALID);
374 battfs_writeTestBlock(disk, 6, INODE2, 1, PAGE_FILL, 1, MARK_PAGE_VALID);
375 battfs_writeTestBlock(disk, 7, INODE2, 0, PAGE_FILL, 1, MARK_PAGE_VALID);
379 ASSERT(battfs_init(disk));
380 ASSERT(!battfs_fileExists(disk, INEXISTENT_INODE));
382 ASSERT(battfs_fileExists(disk, INODE));
383 ASSERT(battfs_fileopen(disk, &fd1, INODE, MODE));
384 ASSERT(fd1.fd.size == PAGE_FILL * 2);
385 ASSERT(fd1.fd.seek_pos == 0);
386 ASSERT(fd1.mode == MODE);
387 ASSERT(fd1.inode == INODE);
388 ASSERT(fd1.start == &disk->page_array[0]);
389 ASSERT(fd1.disk == disk);
390 ASSERT(LIST_HEAD(&disk->file_opened_list) == &fd1.link);
392 ASSERT(kfile_reopen(&fd1.fd) == &fd1.fd);
393 ASSERT(fd1.fd.size == PAGE_FILL * 2);
394 ASSERT(fd1.fd.seek_pos == 0);
395 ASSERT(fd1.mode == MODE);
396 ASSERT(fd1.inode == INODE);
397 ASSERT(fd1.start == &disk->page_array[0]);
398 ASSERT(fd1.disk == disk);
399 ASSERT(LIST_HEAD(&disk->file_opened_list) == &fd1.link);
401 ASSERT(battfs_fileExists(disk, INODE2));
402 ASSERT(battfs_fileopen(disk, &fd2, INODE2, MODE));
403 ASSERT(fd2.fd.size == PAGE_FILL * 2);
404 ASSERT(fd2.fd.seek_pos == 0);
405 ASSERT(fd2.mode == MODE);
406 ASSERT(fd2.inode == INODE2);
407 ASSERT(fd2.start == &disk->page_array[2]);
408 ASSERT(fd2.disk == disk);
409 ASSERT(LIST_HEAD(&disk->file_opened_list)->succ == &fd2.link);
411 ASSERT(kfile_close(&fd1.fd) == 0);
412 ASSERT(kfile_close(&fd2.fd) == 0);
413 ASSERT(LIST_EMPTY(&disk->file_opened_list));
414 ASSERT(battfs_close(disk));
416 kprintf("Test10: passed\n");
419 static void test11(BattFsSuper *disk)
424 kprintf("Test11: read file test\n");
426 fp = fopen(test_filename, "w+");
428 unsigned int PAGE_FILL = 116;
429 unsigned int INODE = 0;
430 unsigned int INODE2 = 4;
431 unsigned int MODE = 0;
433 battfs_writeTestBlock(disk, 0, 123, 0, PAGE_FILL, 0, 1235);
434 battfs_writeTestBlock(disk, 1, INODE, 0, PAGE_FILL, 0, MARK_PAGE_VALID);
435 battfs_writeTestBlock(disk, 2, INODE, 3, PAGE_FILL, 1, MARK_PAGE_VALID);
436 battfs_writeTestBlock(disk, 3, INODE, 0, PAGE_FILL, 1, MARK_PAGE_VALID);
437 battfs_writeTestBlock(disk, 4, INODE2, 0, PAGE_FILL, 0, 1236);
438 battfs_writeTestBlock(disk, 5, INODE2, 0, PAGE_FILL, 0, MARK_PAGE_VALID);
439 battfs_writeTestBlock(disk, 6, INODE2, 1, PAGE_FILL, 1, MARK_PAGE_VALID);
440 battfs_writeTestBlock(disk, 7, INODE2, 0, PAGE_FILL, 1, MARK_PAGE_VALID);
444 ASSERT(battfs_init(disk));
445 ASSERT(battfs_fileopen(disk, &fd1, INODE, MODE));
446 ASSERT(kfile_read(&fd1.fd, buf, sizeof(buf)) == sizeof(buf));
447 ASSERT(fd1.fd.seek_pos == sizeof(buf));
448 for (size_t i = 0; i < sizeof(buf); i++)
451 ASSERT(kfile_close(&fd1.fd) == 0);
452 ASSERT(battfs_close(disk));
454 kprintf("Test11: passed\n");
457 static void test12(BattFsSuper *disk)
461 kprintf("Test12: read file test across page boundary and seek test\n");
463 fp = fopen(test_filename, "w+");
465 const unsigned int PAGE_FILL = 116;
466 unsigned int INODE = 0;
467 unsigned int MODE = 0;
468 uint8_t buf[PAGE_FILL + 10];
470 battfs_writeTestBlock(disk, 0, 123, 0, PAGE_FILL, 0, 1235);
471 battfs_writeTestBlock(disk, 1, INODE, 0, PAGE_FILL, 0, MARK_PAGE_VALID);
472 battfs_writeTestBlock(disk, 2, INODE, 3, PAGE_FILL, 1, MARK_PAGE_VALID);
473 battfs_writeTestBlock(disk, 3, INODE, 0, PAGE_FILL, 1, MARK_PAGE_VALID);
474 battfs_writeTestBlock(disk, 4, INODE, 0, PAGE_FILL, 0, 1236);
475 battfs_writeTestBlock(disk, 5, INODE, 0, PAGE_FILL, 2, MARK_PAGE_VALID);
476 battfs_writeTestBlock(disk, 6, INODE, 1, PAGE_FILL, 3, MARK_PAGE_VALID);
477 battfs_writeTestBlock(disk, 7, INODE, 0, PAGE_FILL, 3, MARK_PAGE_VALID);
481 ASSERT(battfs_init(disk));
482 ASSERT(battfs_fileopen(disk, &fd1, INODE, MODE));
484 ASSERT(kfile_read(&fd1.fd, buf, sizeof(buf)) == sizeof(buf));
485 ASSERT(fd1.fd.seek_pos == (kfile_off_t)sizeof(buf));
486 for (size_t i = 0; i < sizeof(buf); i++)
489 ASSERT(kfile_read(&fd1.fd, buf, sizeof(buf)) == sizeof(buf));
490 ASSERT(fd1.fd.seek_pos == (kfile_off_t)sizeof(buf) * 2);
491 for (size_t i = 0; i < sizeof(buf); i++)
494 ASSERT(kfile_read(&fd1.fd, buf, sizeof(buf)) == sizeof(buf));
495 ASSERT(fd1.fd.seek_pos == (kfile_off_t)sizeof(buf) * 3);
496 for (size_t i = 0; i < sizeof(buf); i++)
499 ASSERT(kfile_read(&fd1.fd, buf, sizeof(buf)) == 86);
500 ASSERT(fd1.fd.seek_pos == (kfile_off_t)fd1.fd.size);
501 for (size_t i = 0; i < 86; i++)
504 ASSERT(kfile_seek(&fd1.fd, 0, KSM_SEEK_SET) == 0);
505 ASSERT(fd1.fd.seek_pos == 0);
507 ASSERT(kfile_seek(&fd1.fd, 0, KSM_SEEK_END) == (kfile_off_t)fd1.fd.size);
508 ASSERT(fd1.fd.seek_pos = (kfile_off_t)fd1.fd.size);
510 ASSERT(kfile_close(&fd1.fd) == 0);
511 ASSERT(battfs_close(disk));
513 kprintf("Test12: passed\n");
517 int battfs_testRun(void)
521 disk.open = disk_open;
522 disk.read = disk_page_read;
523 disk.write = disk_page_write;
524 disk.erase = disk_page_erase;
525 disk.close = disk_close;
538 kprintf("All tests passed!\n");
543 int battfs_testSetup(void)
548 int battfs_testTearDown(void)
555 #include <fs/battfs.c>
556 #include <kern/kfile.c>
557 #include <drv/kdebug.c>
558 #include <mware/formatwr.c>
559 #include <mware/hex.c>