* Header is actually a footer, and so
* resides at page end.
*/
- if (disk->write(disk, page, disk->page_size - BATTFS_HEADER_LEN, buf, BATTFS_HEADER_LEN)
- != BATTFS_HEADER_LEN)
+ if (!(disk->bufferWrite(disk, disk->page_size - BATTFS_HEADER_LEN, buf, BATTFS_HEADER_LEN)
+ == BATTFS_HEADER_LEN && disk->save(disk, page)))
{
LOG_ERR("Error: page[%d]\n", page);
return false;
/* Disk open must set all of these */
ASSERT(disk->read);
- ASSERT(disk->write);
+ ASSERT(disk->load);
+ ASSERT(disk->bufferWrite);
+ ASSERT(disk->save);
ASSERT(disk->erase);
ASSERT(disk->close);
ASSERT(disk->page_size);
*/
typedef size_t (*disk_page_read_t) (struct BattFsSuper *d, pgcnt_t page, pgaddr_t addr, void *buf, size_t);
+
/**
- * Type interface for disk page write function.
- * \a page is the page address, \a addr the address inside the page,
+ * Type interface for disk page load function.
+ * The disk should supply a buffer used for loading/saving pages.
+ * This has to be done by the disk driver because it knows memory details
+ * (e.g. some memories can have the buffer inside the memory itself).
+ * \a page is the page to be loaded from the disk in the buffer.
+ * \return true if ok, false on errors.
+ */
+typedef bool (*disk_page_load_t) (struct BattFsSuper *d, pgcnt_t page);
+
+/**
+ * Type interface for disk pagebuffer write function.
+ * \a addr is the address inside the current loaded page,
* \a size the lenght to be written.
* \return the number of bytes written.
*/
-typedef size_t (*disk_page_write_t) (struct BattFsSuper *d, pgcnt_t page, pgaddr_t addr, const void *buf, size_t);
+typedef size_t (*disk_buffer_write_t) (struct BattFsSuper *d, pgaddr_t addr, const void *buf, size_t);
+
+/**
+ * Type interface for disk page save function.
+ * The disk should supply a buffer used for loading/saving pages.
+ * For details \see disk_page_load_t.
+ * \a page is the page where the buffer will be written.
+ * \return true if ok, false on errors.
+ */
+typedef bool (*disk_page_save_t) (struct BattFsSuper *d, pgcnt_t page);
/**
* Type interface for disk page erase function.
{
disk_open_t open; ///< Disk init.
disk_page_read_t read; ///< Page read.
- disk_page_write_t write; ///< Page write.
+ disk_page_load_t load; ///< Page load.
+ disk_buffer_write_t bufferWrite; ///< Buffer write.
+ disk_page_save_t save; ///< Page save.
disk_page_erase_t erase; ///< Page erase.
disk_close_t close; ///< Disk deinit.
FILE *fp;
const char test_filename[]="battfs_disk.bin";
+static uint8_t page_buffer[PAGE_SIZE];
static bool disk_open(struct BattFsSuper *d)
{
return fread(buf, 1, size, fp);
}
-static size_t disk_page_write(struct BattFsSuper *d, pgcnt_t page, pgaddr_t addr, const void *buf, size_t size)
+static size_t disk_buffer_write(struct BattFsSuper *d, pgaddr_t addr, const void *buf, size_t size)
{
//TRACEMSG("page:%d, addr:%d, size:%d\n", page, addr, size);
- fseek(fp, page * d->page_size + addr, SEEK_SET);
- return fwrite(buf, 1, size, fp);
+ ASSERT(addr + size <= d->page_size);
+ memcpy(&page_buffer[addr], buf, size);
+
+ return size;
+}
+
+static bool disk_page_load(struct BattFsSuper *d, pgcnt_t page)
+{
+ fseek(fp, page * d->page_size, SEEK_SET);
+ return fread(page_buffer, 1, d->page_size, fp) == d->page_size;
+}
+
+static bool disk_page_save(struct BattFsSuper *d, pgcnt_t page)
+{
+ fseek(fp, page * d->page_size, SEEK_SET);
+ return fwrite(page_buffer, 1, d->page_size, fp) == d->page_size;
}
static bool disk_page_erase(struct BattFsSuper *d, pgcnt_t page)
disk.open = disk_open;
disk.read = disk_page_read;
- disk.write = disk_page_write;
+ disk.load = disk_page_load;
+ disk.bufferWrite = disk_buffer_write;
+ disk.save = disk_page_save;
disk.erase = disk_page_erase;
disk.close = disk_close;
test1(&disk);