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+");
261 // page, inode, seq, fill, pgoff
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+");
285 // page, inode, seq, fill, pgoff
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 disk->erase(disk, 3);
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 // page, inode, seq, fill, pgoff
310 disk->erase(disk, 0);
311 battfs_writeTestBlock(disk, 1, 0, 0, 0, 0);
312 battfs_writeTestBlock(disk, 2, 0, 1, 0, 1);
313 battfs_writeTestBlock(disk, 3, 0, 0, 0, 1);
322 testCheck(disk, ref);
323 kprintf("Test8: passed\n");
326 static void test9(BattFsSuper *disk)
329 kprintf("Test9: 2 file with old seq num, 2 free block\n");
332 fp = fopen(test_filename, "w+");
334 // page, inode, seq, fill, pgoff
335 disk->erase(disk, 0);
336 battfs_writeTestBlock(disk, 1, 0, 0, 0, 0);
337 battfs_writeTestBlock(disk, 2, 0, 3, 0, 1);
338 battfs_writeTestBlock(disk, 3, 0, 0, 0, 1);
339 disk->erase(disk, 4);
340 battfs_writeTestBlock(disk, 5, 4, 0, 0, 0);
341 battfs_writeTestBlock(disk, 6, 4, 1, 0, 1);
342 battfs_writeTestBlock(disk, 7, 4, 0, 0, 1);
355 testCheck(disk, ref);
356 kprintf("Test9: passed\n");
359 static void test10(BattFsSuper *disk)
363 kprintf("Test10: open file test, inode 0 and inode 4\n");
365 fp = fopen(test_filename, "w+");
367 int PAGE_FILL = PAGE_SIZE - BATTFS_HEADER_LEN;
368 unsigned int INODE = 0;
369 unsigned int INODE2 = 4;
370 unsigned int INEXISTENT_INODE = 123;
371 unsigned int MODE = 0;
373 // page, inode, seq, fill, pgoff
374 disk->erase(disk, 0);
375 battfs_writeTestBlock(disk, 1, INODE, 0, PAGE_FILL, 0);
376 battfs_writeTestBlock(disk, 2, INODE, 3, PAGE_FILL, 1);
377 battfs_writeTestBlock(disk, 3, INODE, 0, PAGE_FILL, 1);
378 disk->erase(disk, 4);
379 battfs_writeTestBlock(disk, 5, INODE2, 0, PAGE_FILL, 0);
380 battfs_writeTestBlock(disk, 6, INODE2, 1, PAGE_FILL, 1);
381 battfs_writeTestBlock(disk, 7, INODE2, 0, PAGE_FILL, 1);
385 ASSERT(battfs_init(disk));
386 ASSERT(!battfs_fileExists(disk, INEXISTENT_INODE));
388 ASSERT(battfs_fileExists(disk, INODE));
389 ASSERT(battfs_fileopen(disk, &fd1, INODE, MODE));
390 ASSERT(fd1.fd.size == PAGE_FILL * 2);
391 ASSERT(fd1.fd.seek_pos == 0);
392 ASSERT(fd1.mode == MODE);
393 ASSERT(fd1.inode == INODE);
394 ASSERT(fd1.start == &disk->page_array[0]);
395 ASSERT(fd1.disk == disk);
396 ASSERT(LIST_HEAD(&disk->file_opened_list) == &fd1.link);
398 ASSERT(kfile_reopen(&fd1.fd) == &fd1.fd);
399 ASSERT(fd1.fd.size == PAGE_FILL * 2);
400 ASSERT(fd1.fd.seek_pos == 0);
401 ASSERT(fd1.mode == MODE);
402 ASSERT(fd1.inode == INODE);
403 ASSERT(fd1.start == &disk->page_array[0]);
404 ASSERT(fd1.disk == disk);
405 ASSERT(LIST_HEAD(&disk->file_opened_list) == &fd1.link);
407 ASSERT(battfs_fileExists(disk, INODE2));
408 ASSERT(battfs_fileopen(disk, &fd2, INODE2, MODE));
409 ASSERT(fd2.fd.size == PAGE_FILL * 2);
410 ASSERT(fd2.fd.seek_pos == 0);
411 ASSERT(fd2.mode == MODE);
412 ASSERT(fd2.inode == INODE2);
413 ASSERT(fd2.start == &disk->page_array[2]);
414 ASSERT(fd2.disk == disk);
415 ASSERT(LIST_HEAD(&disk->file_opened_list)->succ == &fd2.link);
417 ASSERT(kfile_close(&fd1.fd) == 0);
418 ASSERT(kfile_close(&fd2.fd) == 0);
419 ASSERT(LIST_EMPTY(&disk->file_opened_list));
420 ASSERT(battfs_close(disk));
422 kprintf("Test10: passed\n");
425 static void test11(BattFsSuper *disk)
430 kprintf("Test11: read file test\n");
432 fp = fopen(test_filename, "w+");
434 unsigned int PAGE_FILL = PAGE_SIZE - BATTFS_HEADER_LEN;
435 unsigned int INODE = 0;
436 unsigned int INODE2 = 4;
437 unsigned int MODE = 0;
439 disk->erase(disk, 0);
440 battfs_writeTestBlock(disk, 1, INODE, 0, PAGE_FILL, 0);
441 battfs_writeTestBlock(disk, 2, INODE, 3, PAGE_FILL, 1);
442 battfs_writeTestBlock(disk, 3, INODE, 0, PAGE_FILL, 1);
443 disk->erase(disk, 4);
444 battfs_writeTestBlock(disk, 5, INODE2, 0, PAGE_FILL, 0);
445 battfs_writeTestBlock(disk, 6, INODE2, 1, PAGE_FILL, 1);
446 battfs_writeTestBlock(disk, 7, INODE2, 0, PAGE_FILL, 1);
450 ASSERT(battfs_init(disk));
451 ASSERT(battfs_fileopen(disk, &fd1, INODE, MODE));
452 ASSERT(kfile_read(&fd1.fd, buf, sizeof(buf)) == sizeof(buf));
453 ASSERT(fd1.fd.seek_pos == sizeof(buf));
454 for (size_t i = 0; i < sizeof(buf); i++)
457 ASSERT(kfile_close(&fd1.fd) == 0);
458 ASSERT(battfs_close(disk));
460 kprintf("Test11: passed\n");
463 static void test12(BattFsSuper *disk)
467 kprintf("Test12: read file test across page boundary and seek test\n");
469 fp = fopen(test_filename, "w+");
471 const unsigned int PAGE_FILL = PAGE_SIZE - BATTFS_HEADER_LEN;
472 unsigned int INODE = 0;
473 unsigned int MODE = 0;
474 uint8_t buf[PAGE_FILL + BATTFS_HEADER_LEN / 2];
476 disk->erase(disk, 0);
477 battfs_writeTestBlock(disk, 1, INODE, 0, PAGE_FILL, 0);
478 battfs_writeTestBlock(disk, 2, INODE, 3, PAGE_FILL, 1);
479 battfs_writeTestBlock(disk, 3, INODE, 0, PAGE_FILL, 1);
480 disk->erase(disk, 4);
481 battfs_writeTestBlock(disk, 5, INODE, 0, PAGE_FILL, 2);
482 battfs_writeTestBlock(disk, 6, INODE, 1, PAGE_FILL, 3);
483 battfs_writeTestBlock(disk, 7, INODE, 0, PAGE_FILL, 3);
487 ASSERT(battfs_init(disk));
488 ASSERT(battfs_fileopen(disk, &fd1, INODE, MODE));
490 ASSERT(kfile_read(&fd1.fd, buf, sizeof(buf)) == sizeof(buf));
491 ASSERT(fd1.fd.seek_pos == (kfile_off_t)sizeof(buf));
492 for (size_t i = 0; i < sizeof(buf); i++)
495 ASSERT(kfile_read(&fd1.fd, buf, sizeof(buf)) == sizeof(buf));
496 ASSERT(fd1.fd.seek_pos == (kfile_off_t)sizeof(buf) * 2);
497 for (size_t i = 0; i < sizeof(buf); i++)
500 ASSERT(kfile_read(&fd1.fd, buf, sizeof(buf)) == sizeof(buf));
501 ASSERT(fd1.fd.seek_pos == (kfile_off_t)sizeof(buf) * 3);
502 for (size_t i = 0; i < sizeof(buf); i++)
505 ASSERT(kfile_read(&fd1.fd, buf, sizeof(buf)) == PAGE_FILL * 4 - sizeof(buf) * 3);
506 ASSERT(fd1.fd.seek_pos == (kfile_off_t)fd1.fd.size);
507 for (size_t i = 0; i < PAGE_FILL * 4 - sizeof(buf) * 3; i++)
510 ASSERT(kfile_seek(&fd1.fd, 0, KSM_SEEK_SET) == 0);
511 ASSERT(fd1.fd.seek_pos == 0);
513 ASSERT(kfile_seek(&fd1.fd, 0, KSM_SEEK_END) == (kfile_off_t)fd1.fd.size);
514 ASSERT(fd1.fd.seek_pos = (kfile_off_t)fd1.fd.size);
516 ASSERT(kfile_close(&fd1.fd) == 0);
517 ASSERT(battfs_close(disk));
519 kprintf("Test12: passed\n");
523 int battfs_testRun(void)
527 disk.open = disk_open;
528 disk.read = disk_page_read;
529 disk.write = disk_page_write;
530 disk.erase = disk_page_erase;
531 disk.close = disk_close;
544 kprintf("All tests passed!\n");
549 int battfs_testSetup(void)
554 int battfs_testTearDown(void)
561 #include <fs/battfs.c>
562 #include <kern/kfile.c>
563 #include <drv/kdebug.c>
564 #include <mware/formatwr.c>
565 #include <mware/hex.c>