Skip to content

Commit 4024707

Browse files
ludvigsjrlubos
authored andcommitted
[nrf fromtree] Bluetooth: Mesh: Decouple blob_io_flash erase from flash config
This adds a Kconfig dependency on FLASH_PAGE_LAYOUT when using flash with explicit erase, removing all code dependencies on flash configuration from the erasure logic. This module already requires FLASH_PAGE_LAYOUT to be set when using it with flash with explicit erase, as a silent requirement. The current code does not work without this option enabled except for the case where a BLOB has a size which is a multiple of the page size (since, without it, trying to erase flash space for the final block, which is of variable size, will fail). Also cleans up the code a bit. Signed-off-by: Ludvig Jordet <[email protected]> (cherry picked from commit 294a2cd)
1 parent bbfecc3 commit 4024707

File tree

2 files changed

+34
-32
lines changed

2 files changed

+34
-32
lines changed

subsys/bluetooth/mesh/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,6 +1073,7 @@ config BT_MESH_BLOB_IO_FLASH_WITH_ERASE
10731073
bool "BLOB flash support for devices with erase"
10741074
default y if FLASH_HAS_EXPLICIT_ERASE
10751075
depends on FLASH_HAS_EXPLICIT_ERASE
1076+
depends on FLASH_PAGE_LAYOUT
10761077
help
10771078
Enable path supporting devices with erase. This option appears only
10781079
if there are devices requiring erase, before write, in the system

subsys/bluetooth/mesh/blob_io_flash.c

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -56,42 +56,38 @@ static void io_close(const struct bt_mesh_blob_io *io,
5656
flash_area_close(flash->area);
5757
}
5858

59+
/* Erasure code not needed if no flash in the system requires explicit erase */
60+
#ifdef CONFIG_BT_MESH_BLOB_IO_FLASH_WITH_ERASE
5961
static inline int erase_device_block(const struct flash_area *fa, off_t start, size_t size)
6062
{
61-
/* If there are no devices requiring erase, then there is nothing to do */
62-
if (IS_ENABLED(CONFIG_BT_MESH_BLOB_IO_FLASH_WITH_ERASE)) {
63-
const struct device *fdev = flash_area_get_device(fa);
64-
65-
if (!fdev) {
66-
return -ENODEV;
67-
}
68-
69-
/* We have a mix of devices in system */
70-
if (IS_ENABLED(CONFIG_BT_MESH_BLOB_IO_FLASH_WITHOUT_ERASE)) {
71-
const struct flash_parameters *fparam = flash_get_parameters(fdev);
72-
73-
/* If device has no erase requirement then do nothing */
74-
if (!(flash_params_get_erase_cap(fparam) & FLASH_ERASE_C_EXPLICIT)) {
75-
return 0;
76-
}
77-
}
78-
79-
if (IS_ENABLED(CONFIG_FLASH_PAGE_LAYOUT)) {
80-
struct flash_pages_info page;
81-
int err;
82-
83-
err = flash_get_page_info_by_offs(fdev, start, &page);
84-
if (err) {
85-
return err;
86-
}
87-
88-
size = page.size * DIV_ROUND_UP(size, page.size);
89-
start = page.start_offset;
90-
}
91-
return flash_area_erase(fa, start, size);
63+
const struct device *fdev = flash_area_get_device(fa);
64+
struct flash_pages_info page;
65+
int err;
66+
67+
if (!fdev) {
68+
return -ENODEV;
9269
}
9370

94-
return 0;
71+
#ifdef CONFIG_BT_MESH_BLOB_IO_FLASH_WITHOUT_ERASE
72+
/* We have a mix of devices in system */
73+
const struct flash_parameters *fparam = flash_get_parameters(fdev);
74+
75+
/* If device has no erase requirement then do nothing */
76+
if (!(flash_params_get_erase_cap(fparam) & FLASH_ERASE_C_EXPLICIT)) {
77+
return 0;
78+
}
79+
#endif /* CONFIG_BT_MESH_BLOB_IO_FLASH_WITHOUT_ERASE */
80+
81+
err = flash_get_page_info_by_offs(fdev, start, &page);
82+
if (err) {
83+
return err;
84+
}
85+
86+
/* Align to page boundary. */
87+
size = page.size * DIV_ROUND_UP(size, page.size);
88+
start = page.start_offset;
89+
90+
return flash_area_erase(fa, start, size);
9591
}
9692

9793
static int block_start(const struct bt_mesh_blob_io *io,
@@ -106,6 +102,7 @@ static int block_start(const struct bt_mesh_blob_io *io,
106102

107103
return erase_device_block(flash->area, flash->offset + block->offset, block->size);
108104
}
105+
#endif /* CONFIG_BT_MESH_BLOB_IO_FLASH_WITH_ERASE */
109106

110107
static int rd_chunk(const struct bt_mesh_blob_io *io,
111108
const struct bt_mesh_blob_xfer *xfer,
@@ -165,7 +162,11 @@ int bt_mesh_blob_io_flash_init(struct bt_mesh_blob_io_flash *flash,
165162
flash->offset = offset;
166163
flash->io.open = io_open;
167164
flash->io.close = io_close;
165+
#ifdef CONFIG_BT_MESH_BLOB_IO_FLASH_WITH_ERASE
168166
flash->io.block_start = block_start;
167+
#else
168+
flash->io.block_start = NULL;
169+
#endif
169170
flash->io.block_end = NULL;
170171
flash->io.rd = rd_chunk;
171172
flash->io.wr = wr_chunk;

0 commit comments

Comments
 (0)