11/*
2- * Copyright (c) 2017 Nordic Semiconductor ASA
2+ * Copyright (c) 2017-2023 Nordic Semiconductor ASA
33 * Copyright (c) 2015 Runtime Inc
44 *
55 * SPDX-License-Identifier: Apache-2.0
1010#include <zephyr/fs/fcb.h>
1111#include "fcb_priv.h"
1212
13+ #define FCB_FIXED_ENDMARKER 0xab
14+
1315/*
1416 * Given offset in flash sector, fill in rest of the fcb_entry, and crc8 over
1517 * the data.
1618 */
17- int
18- fcb_elem_crc8 (struct fcb * fcb , struct fcb_entry * loc , uint8_t * c8p )
19+ static int
20+ fcb_elem_crc8 (struct fcb * _fcb , struct fcb_entry * loc , uint8_t * c8p )
1921{
2022 uint8_t tmp_str [FCB_TMP_BUF_SZ ];
2123 int cnt ;
@@ -29,16 +31,17 @@ fcb_elem_crc8(struct fcb *fcb, struct fcb_entry *loc, uint8_t *c8p)
2931 if (loc -> fe_elem_off + 2 > loc -> fe_sector -> fs_size ) {
3032 return - ENOTSUP ;
3133 }
32- rc = fcb_flash_read (fcb , loc -> fe_sector , loc -> fe_elem_off , tmp_str , 2 );
34+
35+ rc = fcb_flash_read (_fcb , loc -> fe_sector , loc -> fe_elem_off , tmp_str , 2 );
3336 if (rc ) {
3437 return - EIO ;
3538 }
3639
37- cnt = fcb_get_len (fcb , tmp_str , & len );
40+ cnt = fcb_get_len (_fcb , tmp_str , & len );
3841 if (cnt < 0 ) {
3942 return cnt ;
4043 }
41- loc -> fe_data_off = loc -> fe_elem_off + fcb_len_in_flash (fcb , cnt );
44+ loc -> fe_data_off = loc -> fe_elem_off + fcb_len_in_flash (_fcb , cnt );
4245 loc -> fe_data_len = len ;
4346
4447 crc8 = CRC8_CCITT_INITIAL_VALUE ;
@@ -52,7 +55,7 @@ fcb_elem_crc8(struct fcb *fcb, struct fcb_entry *loc, uint8_t *c8p)
5255 blk_sz = sizeof (tmp_str );
5356 }
5457
55- rc = fcb_flash_read (fcb , loc -> fe_sector , off , tmp_str , blk_sz );
58+ rc = fcb_flash_read (_fcb , loc -> fe_sector , off , tmp_str , blk_sz );
5659 if (rc ) {
5760 return - EIO ;
5861 }
@@ -63,25 +66,83 @@ fcb_elem_crc8(struct fcb *fcb, struct fcb_entry *loc, uint8_t *c8p)
6366 return 0 ;
6467}
6568
66- int fcb_elem_info (struct fcb * fcb , struct fcb_entry * loc )
69+ #if IS_ENABLED (CONFIG_FCB_ALLOW_FIXED_ENDMARKER )
70+ /* Given the offset in flash sector, calculate the FCB entry data offset and size, and set
71+ * the fixed endmarker.
72+ */
73+ static int
74+ fcb_elem_endmarker_fixed (struct fcb * _fcb , struct fcb_entry * loc , uint8_t * em )
6775{
76+ uint8_t tmp_str [2 ];
77+ int cnt ;
78+ uint16_t len ;
6879 int rc ;
69- uint8_t crc8 ;
70- uint8_t fl_crc8 ;
80+
81+ if (loc -> fe_elem_off + 2 > loc -> fe_sector -> fs_size ) {
82+ return - ENOTSUP ;
83+ }
84+
85+ rc = fcb_flash_read (_fcb , loc -> fe_sector , loc -> fe_elem_off , tmp_str , 2 );
86+ if (rc ) {
87+ return - EIO ;
88+ }
89+
90+ cnt = fcb_get_len (_fcb , tmp_str , & len );
91+ if (cnt < 0 ) {
92+ return cnt ;
93+ }
94+ loc -> fe_data_off = loc -> fe_elem_off + fcb_len_in_flash (_fcb , cnt );
95+ loc -> fe_data_len = len ;
96+
97+ * em = FCB_FIXED_ENDMARKER ;
98+ return 0 ;
99+ }
100+ #endif /* IS_ENABLED(CONFIG_FCB_ALLOW_FIXED_ENDMARKER) */
101+
102+ /* Given the offset in flash sector, calculate the FCB entry data offset and size, and calculate
103+ * the expected endmarker.
104+ */
105+ int
106+ fcb_elem_endmarker (struct fcb * _fcb , struct fcb_entry * loc , uint8_t * em )
107+ {
108+ #if IS_ENABLED (CONFIG_FCB_ALLOW_FIXED_ENDMARKER )
109+ if (_fcb -> f_flags & FCB_FLAGS_CRC_DISABLED ) {
110+ return fcb_elem_endmarker_fixed (_fcb , loc , em );
111+ }
112+ #endif /* IS_ENABLED(CONFIG_FCB_ALLOW_FIXED_ENDMARKER) */
113+
114+ return fcb_elem_crc8 (_fcb , loc , em );
115+ }
116+
117+ /* Given the offset in flash sector, calculate the FCB entry data offset and size, and verify that
118+ * the FCB entry endmarker is correct.
119+ */
120+ int fcb_elem_info (struct fcb * _fcb , struct fcb_entry * loc )
121+ {
122+ int rc ;
123+ uint8_t em ;
124+ uint8_t fl_em ;
71125 off_t off ;
72126
73- rc = fcb_elem_crc8 ( fcb , loc , & crc8 );
127+ rc = fcb_elem_endmarker ( _fcb , loc , & em );
74128 if (rc ) {
75129 return rc ;
76130 }
77- off = loc -> fe_data_off + fcb_len_in_flash (fcb , loc -> fe_data_len );
131+ off = loc -> fe_data_off + fcb_len_in_flash (_fcb , loc -> fe_data_len );
78132
79- rc = fcb_flash_read (fcb , loc -> fe_sector , off , & fl_crc8 , sizeof (fl_crc8 ));
133+ rc = fcb_flash_read (_fcb , loc -> fe_sector , off , & fl_em , sizeof (fl_em ));
80134 if (rc ) {
81135 return - EIO ;
82136 }
83137
84- if (fl_crc8 != crc8 ) {
138+ if (IS_ENABLED (CONFIG_FCB_ALLOW_FIXED_ENDMARKER ) && (fl_em != em )) {
139+ rc = fcb_elem_crc8 (_fcb , loc , & em );
140+ if (rc ) {
141+ return rc ;
142+ }
143+ }
144+
145+ if (fl_em != em ) {
85146 return - EBADMSG ;
86147 }
87148 return 0 ;
0 commit comments