Skip to content

Commit 765155f

Browse files
eivindj-nordicrlubos
authored andcommitted
[nrf fromtree] fs: fcb: add option to disable CRC for fcb entries
Add option to disable CRC for fcb entries. This improves the write throughput significantly at the cost of not detecting corrupted data in flash. This is beneficial for aplications that needs the extra write throughput, where error detection is done elsewhere. Allow the FCB entries in flash to have a valid CRC when CRC is disabled in the FCB. This allows existing solutions to disable CRC checking, while keeping the CRC areas intact. Note that this is a one-way option. Fixes #53707 Signed-off-by: Eivind Jølsgard <[email protected]> (cherry picked from commit cfa1ba1)
1 parent 06319fe commit 765155f

File tree

16 files changed

+201
-46
lines changed

16 files changed

+201
-46
lines changed

doc/releases/release-notes-3.3.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,9 @@ Libraries / Subsystems
377377

378378
* Added new API call `fs_mkfs`.
379379
* Added new sample `samples/subsys/fs/format`.
380+
* Added the option to disable CRC checking in :ref:`fcb_api` by enabling the
381+
Kconfig option :kconfig:option:`CONFIG_FCB_ALLOW_FIXED_ENDMARKER`
382+
and setting the `FCB_FLAGS_CRC_DISABLED` flag in the :c:struct:`fcb` struct.
380383

381384
* Management
382385

include/zephyr/fs/fcb.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017-2020 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
@@ -14,6 +14,7 @@
1414
#include <limits.h>
1515

1616
#include <zephyr/storage/flash_map.h>
17+
#include <zephyr/sys/util_macro.h>
1718

1819
#include <zephyr/kernel.h>
1920

@@ -76,6 +77,11 @@ struct fcb_entry_ctx {
7677
/**< Flash area where the entry is placed */
7778
};
7879

80+
/**
81+
* @brief Flag to disable CRC for the fcb_entries in flash.
82+
*/
83+
#define FCB_FLAGS_CRC_DISABLED BIT(0)
84+
7985
/**
8086
* @brief FCB instance structure
8187
*
@@ -129,6 +135,10 @@ struct fcb {
129135
/**< The value flash takes when it is erased. This is read from
130136
* flash parameters and initialized upon call to fcb_init.
131137
*/
138+
#ifdef CONFIG_FCB_ALLOW_FIXED_ENDMARKER
139+
const uint8_t f_flags;
140+
/**< Flags for configuring the FCB. */
141+
#endif
132142
};
133143

134144
/**

subsys/fs/fcb/Kconfig

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Flash Circular Buffer module
22

3-
# Copyright (c) 2017 Nordic Semiconductor ASA
3+
# Copyright (c) 2017-2023 Nordic Semiconductor ASA
44
# SPDX-License-Identifier: Apache-2.0
55

66
#
@@ -13,3 +13,13 @@ config FCB
1313
select CRC
1414
help
1515
Enable support of Flash Circular Buffer.
16+
17+
if FCB
18+
19+
config FCB_ALLOW_FIXED_ENDMARKER
20+
bool "Allow FCB instances to have a fixed endmarker"
21+
help
22+
This allows the FCB instances to disable CRC checks in
23+
favor of increased write throughput.
24+
25+
endif

subsys/fs/fcb/fcb_append.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,18 +121,18 @@ int
121121
fcb_append_finish(struct fcb *fcb, struct fcb_entry *loc)
122122
{
123123
int rc;
124-
uint8_t crc8[fcb->f_align];
124+
uint8_t em[fcb->f_align];
125125
off_t off;
126126

127-
(void)memset(crc8, 0xFF, sizeof(crc8));
127+
(void)memset(em, 0xFF, sizeof(em));
128128

129-
rc = fcb_elem_crc8(fcb, loc, &crc8[0]);
129+
rc = fcb_elem_endmarker(fcb, loc, &em[0]);
130130
if (rc) {
131131
return rc;
132132
}
133133
off = loc->fe_data_off + fcb_len_in_flash(fcb, loc->fe_data_len);
134134

135-
rc = fcb_flash_write(fcb, loc->fe_sector, off, crc8, fcb->f_align);
135+
rc = fcb_flash_write(fcb, loc->fe_sector, off, em, fcb->f_align);
136136
if (rc) {
137137
return -EIO;
138138
}

subsys/fs/fcb/fcb_elem_info.c

Lines changed: 75 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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
@@ -10,12 +10,14 @@
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;

subsys/fs/fcb/fcb_priv.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017-2020 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
@@ -70,7 +70,7 @@ struct flash_sector *fcb_getnext_sector(struct fcb *fcb,
7070
int fcb_getnext_nolock(struct fcb *fcb, struct fcb_entry *loc);
7171

7272
int fcb_elem_info(struct fcb *fcb, struct fcb_entry *loc);
73-
int fcb_elem_crc8(struct fcb *fcb, struct fcb_entry *loc, uint8_t *crc8p);
73+
int fcb_elem_endmarker(struct fcb *fcb, struct fcb_entry *loc, uint8_t *crc8p);
7474

7575
int fcb_sector_hdr_init(struct fcb *fcb, struct flash_sector *sector, uint16_t id);
7676
int fcb_sector_hdr_read(struct fcb *fcb, struct flash_sector *sector,

tests/subsys/fs/fcb/prj.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ CONFIG_FLASH_PAGE_LAYOUT=y
55
CONFIG_FLASH_MAP=y
66
CONFIG_ARM_MPU=n
77
CONFIG_FCB=y
8+
CONFIG_FCB_ALLOW_FIXED_ENDMARKER=y
89
CONFIG_SOC_FLASH_NRF_EMULATE_ONE_BYTE_WRITE_ACCESS=y
910
CONFIG_ZTEST_NEW_API=y

tests/subsys/fs/fcb/prj_native_posix.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ CONFIG_FLASH=y
44
CONFIG_FLASH_PAGE_LAYOUT=y
55
CONFIG_FLASH_MAP=y
66
CONFIG_FCB=y
7+
CONFIG_FCB_ALLOW_FIXED_ENDMARKER=y
78
CONFIG_ZTEST_NEW_API=y

tests/subsys/fs/fcb/prj_native_posix_64.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ CONFIG_FLASH=y
44
CONFIG_FLASH_PAGE_LAYOUT=y
55
CONFIG_FLASH_MAP=y
66
CONFIG_FCB=y
7+
CONFIG_FCB_ALLOW_FIXED_ENDMARKER=y
78
CONFIG_ZTEST_NEW_API=y

tests/subsys/fs/fcb/prj_nucleo_h743zi.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ CONFIG_FLASH_PAGE_LAYOUT=y
66
CONFIG_FLASH_MAP=y
77
CONFIG_ARM_MPU=n
88
CONFIG_FCB=y
9-
9+
CONFIG_FCB_ALLOW_FIXED_ENDMARKER=y
1010
CONFIG_MAIN_STACK_SIZE=4096
1111
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=4096
1212
CONFIG_ZTEST_NEW_API=y

0 commit comments

Comments
 (0)