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++)
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);
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);
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++)
193 testCheck(disk, ref);
194 kprintf("Test3: passed\n");
198 static void test4(BattFsSuper *disk)
200 pgcnt_t ref[PAGE_COUNT];
201 kprintf("Test4: disk half full with 1 contiguos file, rest marked free\n");
204 fp = fopen(test_filename, "w+");
206 for (int i = 0; i < PAGE_COUNT / 2; i++)
208 battfs_writeTestBlock(disk, i, 0, 0, 0, i);
211 for (int i = PAGE_COUNT / 2; i < PAGE_COUNT; i++)
213 battfs_writeTestBlock(disk, i, 0, 0, 0, i);
219 testCheck(disk, ref);
220 kprintf("Test4: passed\n");
223 static void test5(BattFsSuper *disk)
225 pgcnt_t ref[PAGE_COUNT];
226 kprintf("Test5: disk 1/3 full with 1 contiguos file, 1/3 marked free, rest unformatted\n");
229 fp = fopen(test_filename, "w+");
231 for (int i = 0; i < FILE_SIZE; i++)
234 for (int i = 0; i < PAGE_COUNT / 3; i++)
236 battfs_writeTestBlock(disk, i, 0, 0, 0, i);
239 for (int i = PAGE_COUNT / 3; i < 2 * (PAGE_COUNT / 3); i++)
241 battfs_writeTestBlock(disk, i, 0, 0, 0, i);
242 ref[i + PAGE_COUNT / 3 + 1] = i;
246 for (int i = PAGE_COUNT / 3; i < 2 * (PAGE_COUNT / 3) + 1; i++)
247 ref[i] = PAGE_COUNT + PAGE_COUNT / 3 - i - 1;
249 testCheck(disk, ref);
250 kprintf("Test5: passed\n");
254 static void test6(BattFsSuper *disk)
257 kprintf("Test6: 1 file with 1 old seq num, 1 free block\n");
260 fp = fopen(test_filename, "w+");
262 battfs_writeTestBlock(disk, 0, 0, 0, 0, 0);
263 battfs_writeTestBlock(disk, 1, 0, 0, 0, 1);
264 battfs_writeTestBlock(disk, 2, 0, 1, 0, 1);
265 disk->erase(disk, 3);
274 testCheck(disk, ref);
275 kprintf("Test6: passed\n");
278 static void test7(BattFsSuper *disk)
281 kprintf("Test7: 1 file with 1 old seq num, 1 free block\n");
284 fp = fopen(test_filename, "w+");
286 battfs_writeTestBlock(disk, 0, 0, 0, 0, 0);
287 battfs_writeTestBlock(disk, 1, 0, 1, 0, 1);
288 battfs_writeTestBlock(disk, 2, 0, 0, 0, 1);
289 battfs_writeTestBlock(disk, 3, 0, 0, 0, 0);
297 testCheck(disk, ref);
298 kprintf("Test7: passed\n");
301 static void test8(BattFsSuper *disk)
304 kprintf("Test8: 1 file with 1 old seq num, 1 free block\n");
307 fp = fopen(test_filename, "w+");
309 battfs_writeTestBlock(disk, 0, 0, 0, 0, 0);
310 battfs_writeTestBlock(disk, 1, 0, 0, 0, 0);
311 battfs_writeTestBlock(disk, 2, 0, 1, 0, 1);
312 battfs_writeTestBlock(disk, 3, 0, 0, 0, 1);
321 testCheck(disk, ref);
322 kprintf("Test8: passed\n");
325 static void test9(BattFsSuper *disk)
328 kprintf("Test9: 2 file with old seq num, 2 free block\n");
331 fp = fopen(test_filename, "w+");
333 battfs_writeTestBlock(disk, 0, 0, 0, 0, 0);
334 battfs_writeTestBlock(disk, 1, 0, 0, 0, 0);
335 battfs_writeTestBlock(disk, 2, 0, 3, 0, 1);
336 battfs_writeTestBlock(disk, 3, 0, 0, 0, 1);
337 battfs_writeTestBlock(disk, 4, 0, 0, 0, 0);
338 battfs_writeTestBlock(disk, 5, 4, 0, 0, 0);
339 battfs_writeTestBlock(disk, 6, 4, 1, 0, 1);
340 battfs_writeTestBlock(disk, 7, 4, 0, 0, 1);
353 testCheck(disk, ref);
354 kprintf("Test9: passed\n");
357 static void test10(BattFsSuper *disk)
361 kprintf("Test10: open file test, inode 0 and inode 4\n");
363 fp = fopen(test_filename, "w+");
366 unsigned int INODE = 0;
367 unsigned int INODE2 = 4;
368 unsigned int INEXISTENT_INODE = 123;
369 unsigned int MODE = 0;
371 battfs_writeTestBlock(disk, 0, 123, 0, PAGE_FILL, 0);
372 battfs_writeTestBlock(disk, 1, INODE, 0, PAGE_FILL, 0);
373 battfs_writeTestBlock(disk, 2, INODE, 3, PAGE_FILL, 1);
374 battfs_writeTestBlock(disk, 3, INODE, 0, PAGE_FILL, 1);
375 battfs_writeTestBlock(disk, 4, INODE2, 0, PAGE_FILL, 0);
376 battfs_writeTestBlock(disk, 5, INODE2, 0, PAGE_FILL, 0);
377 battfs_writeTestBlock(disk, 6, INODE2, 1, PAGE_FILL, 1);
378 battfs_writeTestBlock(disk, 7, INODE2, 0, PAGE_FILL, 1);
382 ASSERT(battfs_init(disk));
383 ASSERT(!battfs_fileExists(disk, INEXISTENT_INODE));
385 ASSERT(battfs_fileExists(disk, INODE));
386 ASSERT(battfs_fileopen(disk, &fd1, INODE, MODE));
387 ASSERT(fd1.fd.size == PAGE_FILL * 2);
388 ASSERT(fd1.fd.seek_pos == 0);
389 ASSERT(fd1.mode == MODE);
390 ASSERT(fd1.inode == INODE);
391 ASSERT(fd1.start == &disk->page_array[0]);
392 ASSERT(fd1.disk == disk);
393 ASSERT(LIST_HEAD(&disk->file_opened_list) == &fd1.link);
395 ASSERT(kfile_reopen(&fd1.fd) == &fd1.fd);
396 ASSERT(fd1.fd.size == PAGE_FILL * 2);
397 ASSERT(fd1.fd.seek_pos == 0);
398 ASSERT(fd1.mode == MODE);
399 ASSERT(fd1.inode == INODE);
400 ASSERT(fd1.start == &disk->page_array[0]);
401 ASSERT(fd1.disk == disk);
402 ASSERT(LIST_HEAD(&disk->file_opened_list) == &fd1.link);
404 ASSERT(battfs_fileExists(disk, INODE2));
405 ASSERT(battfs_fileopen(disk, &fd2, INODE2, MODE));
406 ASSERT(fd2.fd.size == PAGE_FILL * 2);
407 ASSERT(fd2.fd.seek_pos == 0);
408 ASSERT(fd2.mode == MODE);
409 ASSERT(fd2.inode == INODE2);
410 ASSERT(fd2.start == &disk->page_array[2]);
411 ASSERT(fd2.disk == disk);
412 ASSERT(LIST_HEAD(&disk->file_opened_list)->succ == &fd2.link);
414 ASSERT(kfile_close(&fd1.fd) == 0);
415 ASSERT(kfile_close(&fd2.fd) == 0);
416 ASSERT(LIST_EMPTY(&disk->file_opened_list));
417 ASSERT(battfs_close(disk));
419 kprintf("Test10: passed\n");
422 static void test11(BattFsSuper *disk)
427 kprintf("Test11: read file test\n");
429 fp = fopen(test_filename, "w+");
431 unsigned int PAGE_FILL = 116;
432 unsigned int INODE = 0;
433 unsigned int INODE2 = 4;
434 unsigned int MODE = 0;
436 battfs_writeTestBlock(disk, 0, 123, 0, PAGE_FILL, 0);
437 battfs_writeTestBlock(disk, 1, INODE, 0, PAGE_FILL, 0);
438 battfs_writeTestBlock(disk, 2, INODE, 3, PAGE_FILL, 1);
439 battfs_writeTestBlock(disk, 3, INODE, 0, PAGE_FILL, 1);
440 battfs_writeTestBlock(disk, 4, INODE2, 0, PAGE_FILL, 0);
441 battfs_writeTestBlock(disk, 5, INODE2, 0, PAGE_FILL, 0);
442 battfs_writeTestBlock(disk, 6, INODE2, 1, PAGE_FILL, 1);
443 battfs_writeTestBlock(disk, 7, INODE2, 0, PAGE_FILL, 1);
447 ASSERT(battfs_init(disk));
448 ASSERT(battfs_fileopen(disk, &fd1, INODE, MODE));
449 ASSERT(kfile_read(&fd1.fd, buf, sizeof(buf)) == sizeof(buf));
450 ASSERT(fd1.fd.seek_pos == sizeof(buf));
451 for (size_t i = 0; i < sizeof(buf); i++)
454 ASSERT(kfile_close(&fd1.fd) == 0);
455 ASSERT(battfs_close(disk));
457 kprintf("Test11: passed\n");
460 static void test12(BattFsSuper *disk)
464 kprintf("Test12: read file test across page boundary and seek test\n");
466 fp = fopen(test_filename, "w+");
468 const unsigned int PAGE_FILL = 116;
469 unsigned int INODE = 0;
470 unsigned int MODE = 0;
471 uint8_t buf[PAGE_FILL + 10];
473 battfs_writeTestBlock(disk, 0, 123, 0, PAGE_FILL, 0);
474 battfs_writeTestBlock(disk, 1, INODE, 0, PAGE_FILL, 0);
475 battfs_writeTestBlock(disk, 2, INODE, 3, PAGE_FILL, 1);
476 battfs_writeTestBlock(disk, 3, INODE, 0, PAGE_FILL, 1);
477 battfs_writeTestBlock(disk, 4, INODE, 0, PAGE_FILL, 0);
478 battfs_writeTestBlock(disk, 5, INODE, 0, PAGE_FILL, 2);
479 battfs_writeTestBlock(disk, 6, INODE, 1, PAGE_FILL, 3);
480 battfs_writeTestBlock(disk, 7, INODE, 0, PAGE_FILL, 3);
484 ASSERT(battfs_init(disk));
485 ASSERT(battfs_fileopen(disk, &fd1, INODE, MODE));
487 ASSERT(kfile_read(&fd1.fd, buf, sizeof(buf)) == sizeof(buf));
488 ASSERT(fd1.fd.seek_pos == (kfile_off_t)sizeof(buf));
489 for (size_t i = 0; i < sizeof(buf); i++)
492 ASSERT(kfile_read(&fd1.fd, buf, sizeof(buf)) == sizeof(buf));
493 ASSERT(fd1.fd.seek_pos == (kfile_off_t)sizeof(buf) * 2);
494 for (size_t i = 0; i < sizeof(buf); i++)
497 ASSERT(kfile_read(&fd1.fd, buf, sizeof(buf)) == sizeof(buf));
498 ASSERT(fd1.fd.seek_pos == (kfile_off_t)sizeof(buf) * 3);
499 for (size_t i = 0; i < sizeof(buf); i++)
502 ASSERT(kfile_read(&fd1.fd, buf, sizeof(buf)) == 86);
503 ASSERT(fd1.fd.seek_pos == (kfile_off_t)fd1.fd.size);
504 for (size_t i = 0; i < 86; i++)
507 ASSERT(kfile_seek(&fd1.fd, 0, KSM_SEEK_SET) == 0);
508 ASSERT(fd1.fd.seek_pos == 0);
510 ASSERT(kfile_seek(&fd1.fd, 0, KSM_SEEK_END) == (kfile_off_t)fd1.fd.size);
511 ASSERT(fd1.fd.seek_pos = (kfile_off_t)fd1.fd.size);
513 ASSERT(kfile_close(&fd1.fd) == 0);
514 ASSERT(battfs_close(disk));
516 kprintf("Test12: passed\n");
520 int battfs_testRun(void)
524 disk.open = disk_open;
525 disk.read = disk_page_read;
526 disk.write = disk_page_write;
527 disk.erase = disk_page_erase;
528 disk.close = disk_close;
541 kprintf("All tests passed!\n");
546 int battfs_testSetup(void)
551 int battfs_testTearDown(void)
558 #include <fs/battfs.c>
559 #include <kern/kfile.c>
560 #include <drv/kdebug.c>
561 #include <mware/formatwr.c>
562 #include <mware/hex.c>