1 /* 2 * Copyright (c) 2017 Nordic Semiconductor ASA 3 * Copyright (c) 2015 Runtime Inc 4 * 5 * SPDX-License-Identifier: Apache-2.0 6 */ 7 8 #include <fs/fcb.h> 9 #include "fcb_priv.h" 10 11 int fcb_rotate(struct fcb * fcb)12fcb_rotate(struct fcb *fcb) 13 { 14 struct flash_sector *sector; 15 int rc = 0; 16 17 rc = k_mutex_lock(&fcb->f_mtx, K_FOREVER); 18 if (rc) { 19 return -EINVAL; 20 } 21 22 rc = fcb_erase_sector(fcb, fcb->f_oldest); 23 if (rc) { 24 rc = -EIO; 25 goto out; 26 } 27 if (fcb->f_oldest == fcb->f_active.fe_sector) { 28 /* 29 * Need to create a new active area, as we're wiping 30 * the current. 31 */ 32 sector = fcb_getnext_sector(fcb, fcb->f_oldest); 33 rc = fcb_sector_hdr_init(fcb, sector, fcb->f_active_id + 1); 34 if (rc) { 35 goto out; 36 } 37 fcb->f_active.fe_sector = sector; 38 fcb->f_active.fe_elem_off = sizeof(struct fcb_disk_area); 39 fcb->f_active_id++; 40 } 41 fcb->f_oldest = fcb_getnext_sector(fcb, fcb->f_oldest); 42 out: 43 k_mutex_unlock(&fcb->f_mtx); 44 return rc; 45 } 46