2017-12-13 22:53:20 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2017 Nordic Semiconductor ASA
|
|
|
|
* Copyright (c) 2015 Runtime Inc
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2022-05-06 17:12:04 +08:00
|
|
|
#include <zephyr/fs/fcb.h>
|
2017-12-13 22:53:20 +08:00
|
|
|
#include "fcb_priv.h"
|
|
|
|
|
|
|
|
int
|
|
|
|
fcb_rotate(struct fcb *fcb)
|
|
|
|
{
|
2018-01-27 06:07:10 +08:00
|
|
|
struct flash_sector *sector;
|
2017-12-13 22:53:20 +08:00
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
rc = k_mutex_lock(&fcb->f_mtx, K_FOREVER);
|
|
|
|
if (rc) {
|
2019-07-11 17:21:35 +08:00
|
|
|
return -EINVAL;
|
2017-12-13 22:53:20 +08:00
|
|
|
}
|
|
|
|
|
2018-01-27 06:07:10 +08:00
|
|
|
rc = fcb_erase_sector(fcb, fcb->f_oldest);
|
2017-12-13 22:53:20 +08:00
|
|
|
if (rc) {
|
2019-07-11 17:21:35 +08:00
|
|
|
rc = -EIO;
|
2017-12-13 22:53:20 +08:00
|
|
|
goto out;
|
|
|
|
}
|
2018-01-27 06:07:10 +08:00
|
|
|
if (fcb->f_oldest == fcb->f_active.fe_sector) {
|
2017-12-13 22:53:20 +08:00
|
|
|
/*
|
|
|
|
* Need to create a new active area, as we're wiping
|
|
|
|
* the current.
|
|
|
|
*/
|
2018-01-27 06:07:10 +08:00
|
|
|
sector = fcb_getnext_sector(fcb, fcb->f_oldest);
|
|
|
|
rc = fcb_sector_hdr_init(fcb, sector, fcb->f_active_id + 1);
|
2017-12-13 22:53:20 +08:00
|
|
|
if (rc) {
|
|
|
|
goto out;
|
|
|
|
}
|
2018-01-27 06:07:10 +08:00
|
|
|
fcb->f_active.fe_sector = sector;
|
2022-05-04 22:10:47 +08:00
|
|
|
fcb->f_active.fe_elem_off = fcb_len_in_flash(fcb, sizeof(struct fcb_disk_area));
|
2017-12-13 22:53:20 +08:00
|
|
|
fcb->f_active_id++;
|
|
|
|
}
|
2018-01-27 06:07:10 +08:00
|
|
|
fcb->f_oldest = fcb_getnext_sector(fcb, fcb->f_oldest);
|
2017-12-13 22:53:20 +08:00
|
|
|
out:
|
|
|
|
k_mutex_unlock(&fcb->f_mtx);
|
|
|
|
return rc;
|
|
|
|
}
|