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 Develer S.r.l. (http://www.develer.com/)
33 * \version $Id: demo.c 18242 2007-10-08 17:35:23Z marco $
35 * \author Francesco Sacchi <batt@develer.com>
40 #include <cfg/debug.h>
41 #include <fs/battfs.h>
48 const char test_filename[]="battfs_disk.bin";
52 static bool disk_open(struct BattFsSuper *d)
54 fp = fopen(filename, "r+b");
56 fseek(fp, 0, SEEK_END);
57 d->page_size = PAGE_SIZE;
58 d->page_count = ftell(fp) / d->page_size;
59 d->page_array = malloc(d->page_count * sizeof(pgcnt_t));
60 TRACEMSG("page_size:%d, page_count:%d\n", d->page_size, d->page_count);
61 return (fp && d->page_array);
64 static size_t disk_page_read(struct BattFsSuper *d, pgcnt_t page, pgaddr_t addr, void *buf, size_t size)
66 TRACEMSG("page:%d, addr:%d, size:%d\n", page, addr, size);
67 fseek(fp, page * d->page_size + addr, SEEK_SET);
68 return fread(buf, 1, size, fp);
71 static size_t disk_page_write(struct BattFsSuper *d, pgcnt_t page, pgaddr_t addr, const void *buf, size_t size)
73 TRACEMSG("page:%d, addr:%d, size:%d\n", page, addr, size);
74 fseek(fp, page * d->page_size + addr, SEEK_SET);
75 return fwrite(buf, 1, size, fp);
78 static bool disk_page_erase(struct BattFsSuper *d, pgcnt_t page)
80 TRACEMSG("page:%d\n", page);
81 fseek(fp, page * d->page_size, SEEK_SET);
83 for (int i = 0; i < d->page_size; i++)
84 if (fputc(0xff, fp) == EOF)
89 static bool disk_close(struct BattFsSuper *d)
93 return (fclose(fp) != EOF);
96 int main(int argc, char *argv[])
100 /* FILE *fpt = fopen(test_filename, "w+");
101 for (int i = 0; i < 32768; i++)
104 filename = test_filename;
110 disk.open = disk_open;
111 disk.read = disk_page_read;
112 disk.write = disk_page_write;
113 disk.erase = disk_page_erase;
114 disk.close = disk_close;
116 if (battfs_init(&disk))
118 kprintf("page_array:\n");
119 for (pgcnt_t i = 0; i < disk.page_count; i++)
123 kprintf("%04d ", disk.page_array[i]);
127 for (pgcnt_t i = 0; i < disk.page_count; i++)
129 if (i < disk.page_count / 2)
131 if (!battfs_writeTestBlock(&disk, i, i, i, i/3, 0, MARK_PAGE_VALID))
133 TRACEMSG("error writing:%d\n", i);
139 if (!battfs_writeTestBlock(&disk, i, i, i, i/3, 0, i))
141 TRACEMSG("error writing:%d\n", i);