Skip to content

Commit 49ee103

Browse files
committed
zephyr: Allow enabling logical sectors
Add Kconfigs: - CONFIG_MCUBOOT_LOGICAL_SECTOR_SIZE - CONFIG_MCUBOOT_VERIFY_LOGICAL_SECTORS Signed-off-by: Dominik Ermel <[email protected]>
1 parent c5bc7b9 commit 49ee103

File tree

3 files changed

+87
-17
lines changed

3 files changed

+87
-17
lines changed

boot/zephyr/Kconfig

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1263,14 +1263,36 @@ config BOOT_FIH_PROFILE_DEFAULT_HIGH
12631263

12641264
endmenu
12651265

1266+
config MCUBOOT_LOGICAL_SECTOR_SIZE
1267+
int "Size of a logical sector"
1268+
default 0
1269+
help
1270+
Set to 0 to use hardware sectors. Any other value here should be
1271+
aligned to hardware sectors in size and alignment.
1272+
1273+
config MCUBOOT_LOGICAL_SECTOR_SIZE_SET
1274+
bool
1275+
default y
1276+
depends on MCUBOOT_LOGICAL_SECTOR_SIZE != 0
1277+
1278+
config MCUBOOT_VERIFY_LOGICAL_SECTORS
1279+
bool "Validate logical sector layout"
1280+
default y
1281+
depends on MCUBOOT_LOGICAL_SECTOR_SIZE_SET
1282+
help
1283+
Validation of logical sector size against hardware constrains.
1284+
Should be used to validate compile-time configuration against run-time
1285+
system.
1286+
12661287
config MCUBOOT_DEVICE_SETTINGS
12671288
# Hidden selector for device-specific settings
12681289
bool
12691290
default y
12701291
# CPU options
12711292
select MCUBOOT_DEVICE_CPU_CORTEX_M0 if CPU_CORTEX_M0
12721293
# Enable flash page layout if available
1273-
select FLASH_PAGE_LAYOUT if FLASH_HAS_PAGE_LAYOUT
1294+
select FLASH_PAGE_LAYOUT if FLASH_HAS_PAGE_LAYOUT && !MCUBOOT_LOGICAL_SECTOR_SIZE_SET
1295+
select FLASH_PAGE_LAYOUT if FLASH_HAS_PAGE_LAYOUT && MCUBOOT_VERIFY_LOGICAL_SECTORS
12741296
# Enable flash_map module as flash I/O back-end
12751297
select FLASH_MAP
12761298

boot/zephyr/flash_map_extended.c

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,6 @@ BOOT_LOG_MODULE_DECLARE(mcuboot);
7070
#error "FLASH_DEVICE_ID could not be determined"
7171
#endif
7272

73-
static const struct device *flash_dev = DEVICE_DT_GET(FLASH_DEVICE_NODE);
74-
7573
int flash_device_base(uint8_t fd_id, uintptr_t *ret)
7674
{
7775
if (fd_id != FLASH_DEVICE_ID) {
@@ -161,10 +159,28 @@ int flash_area_id_from_direct_image(int image_id)
161159
}
162160
#endif
163161

162+
uint8_t flash_area_get_device_id(const struct flash_area *fa)
163+
{
164+
(void)fa;
165+
166+
return FLASH_DEVICE_ID;
167+
}
168+
169+
#define ERASED_VAL 0xff
170+
__weak uint8_t flash_area_erased_val(const struct flash_area *fap)
171+
{
172+
(void)fap;
173+
174+
return ERASED_VAL;
175+
}
176+
177+
#if (defined(CONFIG_MCUBOOT_LOGICAL_SECTOR_SIZE) && CONFIG_MCUBOOT_LOGICAL_SECTOR_SIZE == 0) || \
178+
defined(CONFIG_MCUBOOT_VERIFY_LOGICAL_SECTORS)
164179
int flash_area_sector_from_off(off_t off, struct flash_sector *sector)
165180
{
166181
int rc;
167182
struct flash_pages_info page;
183+
static const struct device *flash_dev = DEVICE_DT_GET(FLASH_DEVICE_NODE);
168184

169185
rc = flash_get_page_info_by_offs(flash_dev, off, &page);
170186
if (rc) {
@@ -177,26 +193,13 @@ int flash_area_sector_from_off(off_t off, struct flash_sector *sector)
177193
return rc;
178194
}
179195

180-
uint8_t flash_area_get_device_id(const struct flash_area *fa)
181-
{
182-
(void)fa;
183-
return FLASH_DEVICE_ID;
184-
}
185-
186-
#define ERASED_VAL 0xff
187-
__weak uint8_t flash_area_erased_val(const struct flash_area *fap)
188-
{
189-
(void)fap;
190-
return ERASED_VAL;
191-
}
192-
193196
int flash_area_get_sector(const struct flash_area *fap, off_t off,
194197
struct flash_sector *fsp)
195198
{
196199
struct flash_pages_info fpi;
197200
int rc;
198201

199-
if (off < 0 || (size_t) off >= fap->fa_size) {
202+
if (off < 0 || (size_t)off >= fap->fa_size) {
200203
return -ERANGE;
201204
}
202205

@@ -210,3 +213,27 @@ int flash_area_get_sector(const struct flash_area *fap, off_t off,
210213

211214
return rc;
212215
}
216+
#else
217+
int flash_area_sector_from_off(off_t off, struct flash_sector *sector)
218+
{
219+
sector->fs_off = off & ~(CONFIG_MCUBOOT_LOGICAL_SECTOR_SIZE - 1);
220+
sector->fs_size = CONFIG_MCUBOOT_LOGICAL_SECTOR_SIZE;
221+
222+
return 0;
223+
}
224+
225+
int flash_area_get_sector(const struct flash_area *fap, off_t off,
226+
struct flash_sector *fsp)
227+
{
228+
if (off < 0 || (size_t)off >= flash_area_get_size(fap)) {
229+
BOOT_LOG_ERR("flash_area_get_sector: off %ld out of area %p",
230+
(long)off, fap);
231+
return -ERANGE;
232+
}
233+
234+
fsp->fs_off = off & ~(CONFIG_MCUBOOT_LOGICAL_SECTOR_SIZE - 1);
235+
fsp->fs_size = CONFIG_MCUBOOT_LOGICAL_SECTOR_SIZE;
236+
237+
return 0;
238+
}
239+
#endif

boot/zephyr/include/mcuboot_config/mcuboot_config.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,27 @@
399399
# endif
400400
#endif
401401

402+
/* If set to non-0 it will use logical sectors rather than querying
403+
* device for sector sizes. This slightly reduces code and RAM usage.
404+
* Note that the logical sector size has to be multiple of erase
405+
* sector size that is biggest for of all devices in the system.
406+
*/
407+
#if defined(CONFIG_MCUBOOT_LOGICAL_SECTOR_SIZE)
408+
#define MCUBOOT_LOGICAL_SECTOR_SIZE CONFIG_MCUBOOT_LOGICAL_SECTOR_SIZE
409+
#endif
410+
411+
/* Enable to validate compile time logical sector setup vs the real device layout.
412+
* This increases the size of bootloader, but is useful to check whether
413+
* selected logical sector size can be used with provided partitions
414+
* and devices they are placed on.
415+
* Once layout is tested, this option should be disabled for production
416+
* devices, as it is pointless to re-validate non-changing setup on
417+
* every MCUboot run.
418+
*/
419+
#if defined(CONFIG_MCUBOOT_VERIFY_LOGICAL_SECTORS)
420+
#define MCUBOOT_VERIFY_LOGICAL_SECTORS 1
421+
#endif
422+
402423
#if defined(CONFIG_BOOT_MAX_IMG_SECTORS_AUTO) && defined(MIN_SECTOR_COUNT)
403424

404425
#define MCUBOOT_MAX_IMG_SECTORS MIN_SECTOR_COUNT

0 commit comments

Comments
 (0)