Skip to content

Commit 3e834e8

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 5876342 commit 3e834e8

File tree

3 files changed

+86
-17
lines changed

3 files changed

+86
-17
lines changed

boot/zephyr/Kconfig

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1251,14 +1251,35 @@ config BOOT_FIH_PROFILE_DEFAULT_HIGH
12511251

12521252
endmenu
12531253

1254+
config MCUBOOT_LOGICAL_SECTOR_SIZE
1255+
int "Size of a logical sector"
1256+
default 0
1257+
help
1258+
Set to 0 to use hardware sectors. Any other value here should be
1259+
aligned to hardware sectors in size and alignment.
1260+
1261+
config MCUBOOT_LOGICAL_SECTOR_SIZE_SET
1262+
bool
1263+
default y if MCUBOOT_LOGICAL_SECTOR_SIZE != 0
1264+
1265+
config MCUBOOT_VERIFY_LOGICAL_SECTORS
1266+
bool "Validate logical sector layout"
1267+
default true if MCUBOOT_LOGICAL_SECTOR_SIZE != 0
1268+
depends on MCUBOOT_LOGICAL_SECTOR_SIZE_SET
1269+
help
1270+
Validation of logical sector size against hardware constrains.
1271+
Should be used to validate compile-time configuration against run-time
1272+
system.
1273+
12541274
config MCUBOOT_DEVICE_SETTINGS
12551275
# Hidden selector for device-specific settings
12561276
bool
12571277
default y
12581278
# CPU options
12591279
select MCUBOOT_DEVICE_CPU_CORTEX_M0 if CPU_CORTEX_M0
12601280
# Enable flash page layout if available
1261-
select FLASH_PAGE_LAYOUT if FLASH_HAS_PAGE_LAYOUT
1281+
select FLASH_PAGE_LAYOUT if FLASH_HAS_PAGE_LAYOUT && !MCUBOOT_LOGICAL_SECTOR_SIZE_SET
1282+
select FLASH_PAGE_LAYOUT if FLASH_HAS_PAGE_LAYOUT && MCUBOOT_VERIFY_LOGICAL_SECTORS
12621283
# Enable flash_map module as flash I/O back-end
12631284
select FLASH_MAP
12641285

boot/zephyr/flash_map_extended.c

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

68-
static const struct device *flash_dev = DEVICE_DT_GET(FLASH_DEVICE_NODE);
69-
7068
int flash_device_base(uint8_t fd_id, uintptr_t *ret)
7169
{
7270
if (fd_id != FLASH_DEVICE_ID) {
@@ -156,10 +154,28 @@ int flash_area_id_from_direct_image(int image_id)
156154
}
157155
#endif
158156

157+
uint8_t flash_area_get_device_id(const struct flash_area *fa)
158+
{
159+
(void)fa;
160+
161+
return FLASH_DEVICE_ID;
162+
}
163+
164+
#define ERASED_VAL 0xff
165+
__weak uint8_t flash_area_erased_val(const struct flash_area *fap)
166+
{
167+
(void)fap;
168+
169+
return ERASED_VAL;
170+
}
171+
172+
#if (defined(CONFIG_MCUBOOT_LOGICAL_SECTOR_SIZE) && CONFIG_MCUBOOT_LOGICAL_SECTOR_SIZE == 0) || \
173+
defined(CONFIG_MCUBOOT_VERIFY_LOGICAL_SECTORS)
159174
int flash_area_sector_from_off(off_t off, struct flash_sector *sector)
160175
{
161176
int rc;
162177
struct flash_pages_info page;
178+
static const struct device *flash_dev = DEVICE_DT_GET(FLASH_DEVICE_NODE);
163179

164180
rc = flash_get_page_info_by_offs(flash_dev, off, &page);
165181
if (rc) {
@@ -172,26 +188,13 @@ int flash_area_sector_from_off(off_t off, struct flash_sector *sector)
172188
return rc;
173189
}
174190

175-
uint8_t flash_area_get_device_id(const struct flash_area *fa)
176-
{
177-
(void)fa;
178-
return FLASH_DEVICE_ID;
179-
}
180-
181-
#define ERASED_VAL 0xff
182-
__weak uint8_t flash_area_erased_val(const struct flash_area *fap)
183-
{
184-
(void)fap;
185-
return ERASED_VAL;
186-
}
187-
188191
int flash_area_get_sector(const struct flash_area *fap, off_t off,
189192
struct flash_sector *fsp)
190193
{
191194
struct flash_pages_info fpi;
192195
int rc;
193196

194-
if (off < 0 || (size_t) off >= fap->fa_size) {
197+
if (off < 0 || (size_t)off >= fap->fa_size) {
195198
return -ERANGE;
196199
}
197200

@@ -205,3 +208,27 @@ int flash_area_get_sector(const struct flash_area *fap, off_t off,
205208

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

boot/zephyr/include/mcuboot_config/mcuboot_config.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,27 @@
394394
# endif
395395
#endif
396396

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

399420
#define MCUBOOT_MAX_IMG_SECTORS MIN_SECTOR_COUNT

0 commit comments

Comments
 (0)