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";
56 static uint8_t page_buffer[PAGE_SIZE];
58 static bool disk_open(struct BattFsSuper *d)
60 fp = fopen(test_filename, "r+b");
62 fseek(fp, 0, SEEK_END);
63 d->page_size = PAGE_SIZE;
64 d->page_count = ftell(fp) / d->page_size;
65 d->page_array = malloc(d->page_count * sizeof(pgcnt_t));
66 //TRACEMSG("page_size:%d, page_count:%d\n", d->page_size, d->page_count);
67 return (fp && d->page_array);
70 static size_t disk_page_read(struct BattFsSuper *d, pgcnt_t page, pgaddr_t addr, void *buf, size_t size)
72 TRACEMSG("page:%d, addr:%d, size:%d", page, addr, size);
73 fseek(fp, page * d->page_size + addr, SEEK_SET);
74 return fread(buf, 1, size, fp);
77 static size_t disk_buffer_write(struct BattFsSuper *d, pgaddr_t addr, const void *buf, size_t size)
79 TRACEMSG("addr:%d, size:%d", addr, size);
80 ASSERT(addr + size <= d->page_size);
81 memcpy(&page_buffer[addr], buf, size);
86 static bool disk_page_load(struct BattFsSuper *d, pgcnt_t page)
88 TRACEMSG("page:%d", page);
89 fseek(fp, page * d->page_size, SEEK_SET);
90 return fread(page_buffer, 1, d->page_size, fp) == d->page_size;
93 static bool disk_page_save(struct BattFsSuper *d, pgcnt_t page)
95 TRACEMSG("page:%d", page);
96 fseek(fp, page * d->page_size, SEEK_SET);
97 return fwrite(page_buffer, 1, d->page_size, fp) == d->page_size;
100 static bool disk_page_erase(struct BattFsSuper *d, pgcnt_t page)
102 //TRACEMSG("page:%d\n", page);
103 fseek(fp, page * d->page_size, SEEK_SET);
105 for (int i = 0; i < d->page_size; i++)
106 if (fputc(0xff, fp) == EOF)
111 static bool disk_close(struct BattFsSuper *d)
115 return (fclose(fp) != EOF);
118 static void testCheck(BattFsSuper *disk, pgcnt_t *reference)
120 ASSERT(battfs_init(disk));
122 for (int i = 0; i < disk->page_count; i++)
124 if (disk->page_array[i] != reference[i])
126 kprintf("Error at addr %d: page_array read", i);
127 for (pgcnt_t i = 0; i < disk->page_count; i++)
131 kprintf("%04d ", disk->page_array[i]);
134 kprintf("Expected:");
135 for (pgcnt_t i = 0; i < disk->page_count; i++)
139 kprintf("%04d ", reference[i]);
149 static void test1(BattFsSuper *disk)
151 pgcnt_t ref[PAGE_COUNT];
152 kprintf("Test1: disk new\n");
154 FILE *fpt = fopen(test_filename, "w+");
156 for (int i = 0; i < FILE_SIZE; i++)
159 for (int i = 0; i < PAGE_COUNT; i++)
162 testCheck(disk, ref);
163 kprintf("Test1: passed\n");
166 static void test2(BattFsSuper *disk)
168 pgcnt_t ref[PAGE_COUNT];
169 kprintf("Test2: disk full with 1 contiguos file\n");
172 fp = fopen(test_filename, "w+");
174 for (int i = 0; i < PAGE_COUNT; i++)
176 battfs_writeTestBlock(disk, i, 0, 0, 0, i);
181 testCheck(disk, ref);
182 kprintf("Test2: passed\n");
186 static void test3(BattFsSuper *disk)
188 pgcnt_t ref[PAGE_COUNT];
189 kprintf("Test3: disk half full with 1 contiguos file, rest unformatted\n");
192 fp = fopen(test_filename, "w+");
194 for (int i = 0; i < PAGE_COUNT / 2; i++)
196 battfs_writeTestBlock(disk, i, 0, 0, 0, i);
199 fseek(fp, FILE_SIZE / 2, SEEK_SET);
200 for (int i = FILE_SIZE / 2; i < FILE_SIZE; i++)
204 for (int i = PAGE_COUNT / 2; i < PAGE_COUNT; i++)
210 testCheck(disk, ref);
211 kprintf("Test3: passed\n");
215 static void test4(BattFsSuper *disk)
217 pgcnt_t ref[PAGE_COUNT];
218 kprintf("Test4: disk half full with 1 contiguos file, rest marked free\n");
221 fp = fopen(test_filename, "w+");
223 for (int i = 0; i < PAGE_COUNT / 2; i++)
225 battfs_writeTestBlock(disk, i, 0, 0, 0, i);
228 for (int i = PAGE_COUNT / 2; i < PAGE_COUNT; i++)
230 battfs_writeTestBlock(disk, i, 0, 0, 0, i);
236 testCheck(disk, ref);
237 kprintf("Test4: passed\n");
240 static void test5(BattFsSuper *disk)
242 pgcnt_t ref[PAGE_COUNT];
243 kprintf("Test5: disk 1/3 full with 1 contiguos file, 1/3 marked free, rest unformatted\n");
246 fp = fopen(test_filename, "w+");
248 for (int i = 0; i < FILE_SIZE; i++)
251 for (int i = 0; i < PAGE_COUNT / 3; i++)
253 battfs_writeTestBlock(disk, i, 0, 0, 0, i);
256 for (int i = PAGE_COUNT / 3; i < 2 * (PAGE_COUNT / 3); i++)
258 battfs_writeTestBlock(disk, i, 0, 0, 0, i);
259 ref[i + PAGE_COUNT / 3 + 1] = i;
263 for (int i = PAGE_COUNT / 3; i < 2 * (PAGE_COUNT / 3) + 1; i++)
264 ref[i] = PAGE_COUNT + PAGE_COUNT / 3 - i - 1;
266 testCheck(disk, ref);
267 kprintf("Test5: passed\n");
271 static void test6(BattFsSuper *disk)
274 kprintf("Test6: 1 file with 1 old seq num, 1 free block\n");
277 fp = fopen(test_filename, "w+");
278 // page, inode, seq, fill, pgoff
279 battfs_writeTestBlock(disk, 0, 0, 0, 0, 0);
280 battfs_writeTestBlock(disk, 1, 0, 0, 0, 1);
281 battfs_writeTestBlock(disk, 2, 0, 1, 0, 1);
282 disk->erase(disk, 3);
291 testCheck(disk, ref);
292 kprintf("Test6: passed\n");
295 static void test7(BattFsSuper *disk)
298 kprintf("Test7: 1 file with 1 old seq num, 1 free block\n");
301 fp = fopen(test_filename, "w+");
302 // page, inode, seq, fill, pgoff
303 battfs_writeTestBlock(disk, 0, 0, 0, 0, 0);
304 battfs_writeTestBlock(disk, 1, 0, 1, 0, 1);
305 battfs_writeTestBlock(disk, 2, 0, 0, 0, 1);
306 disk->erase(disk, 3);
314 testCheck(disk, ref);
315 kprintf("Test7: passed\n");
318 static void test8(BattFsSuper *disk)
321 kprintf("Test8: 1 file with 1 old seq num, 1 free block\n");
324 fp = fopen(test_filename, "w+");
326 // page, inode, seq, fill, pgoff
327 disk->erase(disk, 0);
328 battfs_writeTestBlock(disk, 1, 0, 0, 0, 0);
329 battfs_writeTestBlock(disk, 2, 0, 1, 0, 1);
330 battfs_writeTestBlock(disk, 3, 0, 0, 0, 1);
339 testCheck(disk, ref);
340 kprintf("Test8: passed\n");
343 static void test9(BattFsSuper *disk)
346 kprintf("Test9: 2 file with old seq num, 2 free block\n");
349 fp = fopen(test_filename, "w+");
351 // page, inode, seq, fill, pgoff
352 disk->erase(disk, 0);
353 battfs_writeTestBlock(disk, 1, 0, 0, 0, 0);
354 battfs_writeTestBlock(disk, 2, 0, 3, 0, 1);
355 battfs_writeTestBlock(disk, 3, 0, 0, 0, 1);
356 disk->erase(disk, 4);
357 battfs_writeTestBlock(disk, 5, 4, 0, 0, 0);
358 battfs_writeTestBlock(disk, 6, 4, 1, 0, 1);
359 battfs_writeTestBlock(disk, 7, 4, 0, 0, 1);
372 testCheck(disk, ref);
373 kprintf("Test9: passed\n");
376 static void test10(BattFsSuper *disk)
380 kprintf("Test10: open file test, inode 0 and inode 4\n");
382 fp = fopen(test_filename, "w+");
384 int PAGE_FILL = PAGE_SIZE - BATTFS_HEADER_LEN;
385 unsigned int INODE = 0;
386 unsigned int INODE2 = 4;
387 unsigned int INEXISTENT_INODE = 123;
388 unsigned int MODE = 0;
390 // page, inode, seq, fill, pgoff
391 disk->erase(disk, 0);
392 battfs_writeTestBlock(disk, 1, INODE, 0, PAGE_FILL, 0);
393 battfs_writeTestBlock(disk, 2, INODE, 3, PAGE_FILL, 1);
394 battfs_writeTestBlock(disk, 3, INODE, 0, PAGE_FILL, 1);
395 disk->erase(disk, 4);
396 battfs_writeTestBlock(disk, 5, INODE2, 0, PAGE_FILL, 0);
397 battfs_writeTestBlock(disk, 6, INODE2, 1, PAGE_FILL, 1);
398 battfs_writeTestBlock(disk, 7, INODE2, 0, PAGE_FILL, 1);
402 ASSERT(battfs_init(disk));
403 ASSERT(!battfs_fileExists(disk, INEXISTENT_INODE));
405 ASSERT(battfs_fileExists(disk, INODE));
406 ASSERT(battfs_fileopen(disk, &fd1, INODE, MODE));
407 ASSERT(fd1.fd.size == PAGE_FILL * 2);
408 ASSERT(fd1.fd.seek_pos == 0);
409 ASSERT(fd1.mode == MODE);
410 ASSERT(fd1.inode == INODE);
411 ASSERT(fd1.start == &disk->page_array[0]);
412 ASSERT(fd1.disk == disk);
413 ASSERT(LIST_HEAD(&disk->file_opened_list) == &fd1.link);
415 ASSERT(kfile_reopen(&fd1.fd) == &fd1.fd);
416 ASSERT(fd1.fd.size == PAGE_FILL * 2);
417 ASSERT(fd1.fd.seek_pos == 0);
418 ASSERT(fd1.mode == MODE);
419 ASSERT(fd1.inode == INODE);
420 ASSERT(fd1.start == &disk->page_array[0]);
421 ASSERT(fd1.disk == disk);
422 ASSERT(LIST_HEAD(&disk->file_opened_list) == &fd1.link);
424 ASSERT(battfs_fileExists(disk, INODE2));
425 ASSERT(battfs_fileopen(disk, &fd2, INODE2, MODE));
426 ASSERT(fd2.fd.size == PAGE_FILL * 2);
427 ASSERT(fd2.fd.seek_pos == 0);
428 ASSERT(fd2.mode == MODE);
429 ASSERT(fd2.inode == INODE2);
430 ASSERT(fd2.start == &disk->page_array[2]);
431 ASSERT(fd2.disk == disk);
432 ASSERT(LIST_HEAD(&disk->file_opened_list)->succ == &fd2.link);
434 ASSERT(kfile_close(&fd1.fd) == 0);
435 ASSERT(kfile_close(&fd2.fd) == 0);
436 ASSERT(LIST_EMPTY(&disk->file_opened_list));
437 ASSERT(battfs_close(disk));
439 kprintf("Test10: passed\n");
442 static void test11(BattFsSuper *disk)
447 kprintf("Test11: read file test\n");
449 fp = fopen(test_filename, "w+");
451 unsigned int PAGE_FILL = PAGE_SIZE - BATTFS_HEADER_LEN;
452 unsigned int INODE = 0;
453 unsigned int INODE2 = 4;
454 unsigned int MODE = 0;
456 disk->erase(disk, 0);
457 battfs_writeTestBlock(disk, 1, INODE, 0, PAGE_FILL, 0);
458 battfs_writeTestBlock(disk, 2, INODE, 3, PAGE_FILL, 1);
459 battfs_writeTestBlock(disk, 3, INODE, 0, PAGE_FILL, 1);
460 disk->erase(disk, 4);
461 battfs_writeTestBlock(disk, 5, INODE2, 0, PAGE_FILL, 0);
462 battfs_writeTestBlock(disk, 6, INODE2, 1, PAGE_FILL, 1);
463 battfs_writeTestBlock(disk, 7, INODE2, 0, PAGE_FILL, 1);
467 ASSERT(battfs_init(disk));
468 ASSERT(battfs_fileopen(disk, &fd1, INODE, MODE));
469 ASSERT(kfile_read(&fd1.fd, buf, sizeof(buf)) == sizeof(buf));
470 ASSERT(fd1.fd.seek_pos == sizeof(buf));
471 for (size_t i = 0; i < sizeof(buf); i++)
472 ASSERT(buf[i] == 0xff);
474 ASSERT(kfile_close(&fd1.fd) == 0);
475 ASSERT(battfs_close(disk));
477 kprintf("Test11: passed\n");
480 static void test12(BattFsSuper *disk)
484 kprintf("Test12: read file test across page boundary and seek test\n");
486 fp = fopen(test_filename, "w+");
488 const unsigned int PAGE_FILL = PAGE_SIZE - BATTFS_HEADER_LEN;
489 unsigned int INODE = 0;
490 unsigned int MODE = 0;
491 uint8_t buf[PAGE_FILL + BATTFS_HEADER_LEN / 2];
493 disk->erase(disk, 0);
494 battfs_writeTestBlock(disk, 1, INODE, 0, PAGE_FILL, 0);
495 battfs_writeTestBlock(disk, 2, INODE, 3, PAGE_FILL, 1);
496 battfs_writeTestBlock(disk, 3, INODE, 0, PAGE_FILL, 1);
497 disk->erase(disk, 4);
498 battfs_writeTestBlock(disk, 5, INODE, 0, PAGE_FILL, 2);
499 battfs_writeTestBlock(disk, 6, INODE, 1, PAGE_FILL, 3);
500 battfs_writeTestBlock(disk, 7, INODE, 0, PAGE_FILL, 3);
504 ASSERT(battfs_init(disk));
505 ASSERT(battfs_fileopen(disk, &fd1, INODE, MODE));
507 ASSERT(kfile_read(&fd1.fd, buf, sizeof(buf)) == sizeof(buf));
508 ASSERT(fd1.fd.seek_pos == (kfile_off_t)sizeof(buf));
509 for (size_t i = 0; i < sizeof(buf); i++)
510 ASSERT(buf[i] == 0xff);
512 ASSERT(kfile_read(&fd1.fd, buf, sizeof(buf)) == sizeof(buf));
513 ASSERT(fd1.fd.seek_pos == (kfile_off_t)sizeof(buf) * 2);
514 for (size_t i = 0; i < sizeof(buf); i++)
515 ASSERT(buf[i] == 0xff);
517 ASSERT(kfile_read(&fd1.fd, buf, sizeof(buf)) == sizeof(buf));
518 ASSERT(fd1.fd.seek_pos == (kfile_off_t)sizeof(buf) * 3);
519 for (size_t i = 0; i < sizeof(buf); i++)
520 ASSERT(buf[i] == 0xff);
522 ASSERT(kfile_read(&fd1.fd, buf, sizeof(buf)) == PAGE_FILL * 4 - sizeof(buf) * 3);
523 ASSERT(fd1.fd.seek_pos == (kfile_off_t)fd1.fd.size);
524 for (size_t i = 0; i < PAGE_FILL * 4 - sizeof(buf) * 3; i++)
525 ASSERT(buf[i] == 0xff);
527 ASSERT(kfile_seek(&fd1.fd, 0, KSM_SEEK_SET) == 0);
528 ASSERT(fd1.fd.seek_pos == 0);
530 ASSERT(kfile_seek(&fd1.fd, 0, KSM_SEEK_END) == (kfile_off_t)fd1.fd.size);
531 ASSERT(fd1.fd.seek_pos = (kfile_off_t)fd1.fd.size);
533 ASSERT(kfile_close(&fd1.fd) == 0);
534 ASSERT(battfs_close(disk));
536 kprintf("Test12: passed\n");
540 int battfs_testRun(void)
544 disk.open = disk_open;
545 disk.read = disk_page_read;
546 disk.load = disk_page_load;
547 disk.bufferWrite = disk_buffer_write;
548 disk.save = disk_page_save;
549 disk.erase = disk_page_erase;
550 disk.close = disk_close;
563 kprintf("All tests passed!\n");
568 int battfs_testSetup(void)
573 int battfs_testTearDown(void)
580 #include <fs/battfs.c>
581 #include <kern/kfile.c>
582 #include <drv/kdebug.c>
583 #include <mware/formatwr.c>
584 #include <mware/hex.c>