fs: fcb: Make FCB work with sectors larger than 16K

Enhance FCB to also work with sectors larger than 16K and
to handle larger flash alignment constraints correctly.

use fcb_len_in_flash when setting the offset of the data
and use buffers sizes of at least the alignment value.

The test in fcb_test_append_to_big has been altered, as it
would otherwise not come to a data length which fits the
fcb on sectors larger than 16K.

Closes: https://github.com/zephyrproject-rtos/zephyr/issues/45345

Signed-off-by: Achatzi Julian <jachatzi@baumer.com>
This commit is contained in:
Achatzi Julian 2022-05-04 16:10:47 +02:00 committed by Carles Cufí
commit 90236b0650
9 changed files with 80 additions and 26 deletions

View file

@ -159,7 +159,7 @@ fcb_init(int f_area_id, struct fcb *fcb)
fcb->f_align = align;
fcb->f_oldest = oldest_sector;
fcb->f_active.fe_sector = newest_sector;
fcb->f_active.fe_elem_off = sizeof(struct fcb_disk_area);
fcb->f_active.fe_elem_off = fcb_len_in_flash(fcb, sizeof(struct fcb_disk_area));
fcb->f_active_id = newest;
while (1) {
@ -196,12 +196,12 @@ int
fcb_is_empty(struct fcb *fcb)
{
return (fcb->f_active.fe_sector == fcb->f_oldest &&
fcb->f_active.fe_elem_off == sizeof(struct fcb_disk_area));
fcb->f_active.fe_elem_off == fcb_len_in_flash(fcb, sizeof(struct fcb_disk_area)));
}
/**
* Length of an element is encoded in 1 or 2 bytes.
* 1 byte for lengths < 128 bytes, and 2 bytes for < 16384.
* 1 byte for lengths < 128 bytes, 2 bytes for < 16384.
*
* The storage of length has been originally designed to work with 0xff erasable
* flash devices and gives length 0xffff special meaning: that there is no value
@ -239,7 +239,6 @@ int
fcb_get_len(const struct fcb *fcb, uint8_t *buf, uint16_t *len)
{
int rc;
if ((buf[0] ^ ~fcb->f_erase_value) & 0x80) {
if ((buf[0] == fcb->f_erase_value) &&
(buf[1] == fcb->f_erase_value)) {

View file

@ -51,7 +51,7 @@ fcb_append_to_scratch(struct fcb *fcb)
return rc;
}
fcb->f_active.fe_sector = sector;
fcb->f_active.fe_elem_off = sizeof(struct fcb_disk_area);
fcb->f_active.fe_elem_off = fcb_len_in_flash(fcb, sizeof(struct fcb_disk_area));
fcb->f_active_id++;
return 0;
}
@ -63,7 +63,7 @@ fcb_append(struct fcb *fcb, uint16_t len, struct fcb_entry *append_loc)
struct fcb_entry *active;
int cnt;
int rc;
uint8_t tmp_str[8];
uint8_t tmp_str[MAX(8, fcb->f_align)];
cnt = fcb_put_len(fcb, tmp_str, len);
if (cnt < 0) {
@ -82,7 +82,7 @@ fcb_append(struct fcb *fcb, uint16_t len, struct fcb_entry *append_loc)
if (active->fe_elem_off + len + cnt > active->fe_sector->fs_size) {
sector = fcb_new_sector(fcb, fcb->f_scratch_cnt);
if (!sector || (sector->fs_size <
sizeof(struct fcb_disk_area) + len + cnt)) {
fcb_len_in_flash(fcb, sizeof(struct fcb_disk_area)) + len + cnt)) {
rc = -ENOSPC;
goto err;
}
@ -91,7 +91,7 @@ fcb_append(struct fcb *fcb, uint16_t len, struct fcb_entry *append_loc)
goto err;
}
fcb->f_active.fe_sector = sector;
fcb->f_active.fe_elem_off = sizeof(struct fcb_disk_area);
fcb->f_active.fe_elem_off = fcb_len_in_flash(fcb, sizeof(struct fcb_disk_area));
fcb->f_active_id++;
}

View file

@ -55,7 +55,7 @@ fcb_getnext_nolock(struct fcb *fcb, struct fcb_entry *loc)
/*
* If offset is zero, we serve the first entry from the sector.
*/
loc->fe_elem_off = sizeof(struct fcb_disk_area);
loc->fe_elem_off = fcb_len_in_flash(fcb, sizeof(struct fcb_disk_area));
rc = fcb_elem_info(fcb, loc);
switch (rc) {
case 0:
@ -89,7 +89,7 @@ next_sector:
return -ENOTSUP;
}
loc->fe_sector = fcb_getnext_sector(fcb, loc->fe_sector);
loc->fe_elem_off = sizeof(struct fcb_disk_area);
loc->fe_elem_off = fcb_len_in_flash(fcb, sizeof(struct fcb_disk_area));
rc = fcb_elem_info(fcb, loc);
switch (rc) {
case 0:

View file

@ -35,7 +35,7 @@ fcb_rotate(struct fcb *fcb)
goto out;
}
fcb->f_active.fe_sector = sector;
fcb->f_active.fe_elem_off = sizeof(struct fcb_disk_area);
fcb->f_active.fe_elem_off = fcb_len_in_flash(fcb, sizeof(struct fcb_disk_area));
fcb->f_active_id++;
}
fcb->f_oldest = fcb_getnext_sector(fcb, fcb->f_oldest);