+ findFreeStartNext(disk, minl, maxl, minh, maxh);
+ return true;
+}
+
+/**
+ * Fill page allocation array of \a disk
+ * using file lenghts in \a filelen_table.
+ *
+ * The page allocation array is an array containings all file infos.
+ * Is ordered by file, and within each file is ordered by page offset
+ * inside file.
+ * e.g. : at page array[0] you will find page address of the first page
+ * of the first file (if present).
+ * Free blocks are allocated after the last file, starting from invalid ones
+ * and continuing with the marked free ones.
+ *
+ * \return true if ok, false on disk read errors.
+ * \note The whole disk is scanned once.
+ */
+static bool fillPageArray(struct BattFsSuper *disk, pgoff_t *filelen_table)
+{
+ BattFsPageHeader hdr;
+ /* Fill page allocation array */
+ for (pgcnt_t page = 0; page < disk->page_count; page++)
+ {
+ if (!battfs_readHeader(disk, page, &hdr))
+ return false;
+
+ /* Check header FCS */
+ if (hdr.fcs == computeFcs(&hdr))
+ {
+ /* Page is valid and is owned by a file */
+ ASSERT(hdr.mark == MARK_PAGE_VALID);
+ ASSERT(hdr.fcs_free == FCS_FREE_VALID);
+
+ /* Compute array position */
+ pgcnt_t array_pos = countPages(filelen_table, hdr.inode);
+ array_pos += hdr.pgoff;
+
+ /* Check if position is already used by another page of the same file */
+ if (LIKELY(disk->page_array[array_pos] == PAGE_UNSET_SENTINEL))
+ disk->page_array[array_pos] = page;
+ else
+ {
+ BattFsPageHeader hdr_old;
+
+ if (!battfs_readHeader(disk, disk->page_array[array_pos], &hdr_old))
+ return false;
+
+ /* Check header FCS */
+ ASSERT(hdr_old.fcs == computeFcs(&hdr_old));
+
+ /* Only the very same page with a different seq number can be here */
+ ASSERT(hdr.inode == hdr_old.inode);
+ ASSERT(hdr.pgoff == hdr_old.pgoff);
+ ASSERT(hdr.mark == hdr_old.mark);
+ ASSERT(hdr.fcs_free == hdr_old.fcs_free);
+ ASSERT(hdr.seq != hdr_old.seq);
+
+ pgcnt_t new_page, old_page;
+ fill_t old_fill;
+
+ /* Fancy check to handle seq wraparound (2 bits only) */
+ if (((hdr.seq - hdr_old.seq) & 0x03) < 2)
+ {
+ /* Current header is newer than the previuos one */
+ old_page = disk->page_array[array_pos];
+ new_page = page;
+ old_fill = hdr_old.fill;
+ }
+ else
+ {
+ /* Previous header is newer than the current one */
+ old_page = page;
+ new_page = disk->page_array[array_pos];
+ old_fill = hdr.fill;
+ }
+
+ /* Set new page */
+ disk->page_array[array_pos] = new_page;
+
+ /* Add free space */
+ disk->free_bytes += old_fill;
+
+ /* Shift all array one position to the left, overwriting duplicate page */
+ array_pos -= hdr.pgoff;
+ array_pos += filelen_table[hdr.inode];
+ movePages(disk, array_pos, -1);
+
+ /* Decrease file page count */
+ filelen_table[hdr.inode]--;
+
+ /* Add old page to free pages pool */
+ if (!battfs_markFree(disk, &hdr, old_page))
+ return false;
+
+ insertFreePage(disk, hdr.mark, old_page);
+ }
+ }
+ else
+ {
+ /* Check if page is free */
+ if (hdr.fcs_free != computeFcsFree(&hdr))
+ /* Page is not a valid marked page, insert at list beginning */
+ hdr.mark = --disk->free_start;
+
+ insertFreePage(disk, hdr.mark, page);
+ }
+ }
+ return true;
+}
+
+/**
+ * Initialize and mount disk described by
+ * \a disk.
+ * \return false on errors, true otherwise.
+ */
+bool battfs_init(struct BattFsSuper *disk)
+{
+ pgoff_t filelen_table[BATTFS_MAX_FILES];
+
+ /* Sanity check */
+ ASSERT(disk->open);
+
+ /* Init disk device */
+ if (!disk->open(disk))
+ {
+ TRACEMSG("open error\n");
+ return false;
+ }
+
+ /* Disk open must set all of these */
+ ASSERT(disk->read);
+ ASSERT(disk->write);
+ ASSERT(disk->erase);
+ ASSERT(disk->close);
+ ASSERT(disk->page_size);
+ ASSERT(disk->page_count);
+ ASSERT(disk->page_count < PAGE_UNSET_SENTINEL - 1);
+ ASSERT(disk->page_array);
+
+ memset(filelen_table, 0, BATTFS_MAX_FILES * sizeof(pgoff_t));
+
+ disk->free_bytes = 0;
+ disk->disk_size = (disk_size_t)(disk->page_size - BATTFS_HEADER_LEN) * disk->page_count;
+
+ /* Count pages per file */
+ if (!countDiskFilePages(disk, filelen_table))
+ {
+ TRACEMSG("error counting file pages\n");
+ return false;
+ }