zephyr/subsys/fs/fcb/fcb_rotate.c
Achatzi Julian 90236b0650 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>
2022-05-25 14:57:45 +02:00

46 lines
945 B
C

/*
* Copyright (c) 2017 Nordic Semiconductor ASA
* Copyright (c) 2015 Runtime Inc
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/fs/fcb.h>
#include "fcb_priv.h"
int
fcb_rotate(struct fcb *fcb)
{
struct flash_sector *sector;
int rc = 0;
rc = k_mutex_lock(&fcb->f_mtx, K_FOREVER);
if (rc) {
return -EINVAL;
}
rc = fcb_erase_sector(fcb, fcb->f_oldest);
if (rc) {
rc = -EIO;
goto out;
}
if (fcb->f_oldest == fcb->f_active.fe_sector) {
/*
* Need to create a new active area, as we're wiping
* the current.
*/
sector = fcb_getnext_sector(fcb, fcb->f_oldest);
rc = fcb_sector_hdr_init(fcb, sector, fcb->f_active_id + 1);
if (rc) {
goto out;
}
fcb->f_active.fe_sector = sector;
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);
out:
k_mutex_unlock(&fcb->f_mtx);
return rc;
}