Skip to content

Commit 06c777a

Browse files
committed
zephyr: Allow enabling logical sectors
Add Kconfigs: - CONFIG_MCUBOOT_LOGICAL_SECTOR_SIZE - CONFIG_MCUBOOT_LOGICAL_SECTOR_VALIDATION Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
1 parent 4772da1 commit 06c777a

File tree

3 files changed

+83
-16
lines changed

3 files changed

+83
-16
lines changed

boot/zephyr/Kconfig

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

11131113
endmenu
11141114

1115+
config MCUBOOT_LOGICAL_SECTOR_SIZE
1116+
int "Size of a logical sector"
1117+
default 0
1118+
help
1119+
Set to 0 to use hardware sectors. Any other value here should be
1120+
aligned to hardware sectors in size and alignment.
1121+
1122+
config MCUBOOT_LOGICAL_SECTOR_SIZE_SET
1123+
bool
1124+
default y if MCUBOOT_LOGICAL_SECTOR_SIZE != 0
1125+
1126+
config MCUBOOT_LOGICAL_SECTOR_VALIDATION
1127+
bool "Validate logical sector layout"
1128+
default true if MCUBOOT_LOGICAL_SECTOR_SIZE != 0
1129+
depends on MCUBOOT_LOGICAL_SECTOR_SIZE_SET
1130+
help
1131+
Validation of logical sector size against hardware constrains.
1132+
Should be used to validate compile-time configuration against run-time
1133+
system.
1134+
11151135
config MCUBOOT_DEVICE_SETTINGS
11161136
# Hidden selector for device-specific settings
11171137
bool
11181138
default y
11191139
# CPU options
11201140
select MCUBOOT_DEVICE_CPU_CORTEX_M0 if CPU_CORTEX_M0
11211141
# Enable flash page layout if available
1122-
select FLASH_PAGE_LAYOUT if FLASH_HAS_PAGE_LAYOUT
1142+
select FLASH_PAGE_LAYOUT if FLASH_HAS_PAGE_LAYOUT && !MCUBOOT_LOGICAL_SECTOR_SIZE_SET
1143+
select FLASH_PAGE_LAYOUT if FLASH_HAS_PAGE_LAYOUT && MCUBOOT_LOGICAL_SECTOR_VALIDATION
11231144
# Enable flash_map module as flash I/O back-end
11241145
select FLASH_MAP
11251146

boot/zephyr/flash_map_extended.c

Lines changed: 40 additions & 15 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,26 @@ 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+
return FLASH_DEVICE_ID;
161+
}
162+
163+
#define ERASED_VAL 0xff
164+
__weak uint8_t flash_area_erased_val(const struct flash_area *fap)
165+
{
166+
(void)fap;
167+
return ERASED_VAL;
168+
}
169+
170+
#if (defined(CONFIG_MCUBOOT_LOGICAL_SECTOR_SIZE) && CONFIG_MCUBOOT_LOGICAL_SECTOR_SIZE == 0) || \
171+
defined(CONFIG_MCUBOOT_LOGICAL_SECTOR_VALIDATION)
159172
int flash_area_sector_from_off(off_t off, struct flash_sector *sector)
160173
{
161174
int rc;
162175
struct flash_pages_info page;
176+
static const struct device *flash_dev = DEVICE_DT_GET(FLASH_DEVICE_NODE);
163177

164178
rc = flash_get_page_info_by_offs(flash_dev, off, &page);
165179
if (rc) {
@@ -172,19 +186,6 @@ int flash_area_sector_from_off(off_t off, struct flash_sector *sector)
172186
return rc;
173187
}
174188

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-
188189
int flash_area_get_sector(const struct flash_area *fap, off_t off,
189190
struct flash_sector *fsp)
190191
{
@@ -205,3 +206,27 @@ int flash_area_get_sector(const struct flash_area *fap, off_t off,
205206

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

boot/zephyr/include/mcuboot_config/mcuboot_config.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,27 @@
357357
# endif
358358
#endif
359359

360+
/* If set to non-0 it will use logical sectors rather than querying
361+
* device for sector sizes. This slightly reduces code and RAM usage.
362+
* Note that the logical sector size has to be multiple of erase
363+
* sector size that is biggest for of all devices in the system.
364+
*/
365+
#if defined(CONFIG_MCUBOOT_LOGICAL_SECTOR_SIZE)
366+
#define MCUBOOT_LOGICAL_SECTOR_SIZE CONFIG_MCUBOOT_LOGICAL_SECTOR_SIZE
367+
#endif
368+
369+
/* Enable to validate compile time logical sector setup vs the real device layout.
370+
* This increases the size of bootloader but is useful to check whether
371+
* selected logical sector size can be used with provided partitions
372+
* and devices they are placed on.
373+
* Once layout is tested, this option should be disabled, for production
374+
* devices, as it is pointless to re-validate non-changing setup on
375+
* every MCUboot run.
376+
*/
377+
#if defined(CONFIG_MCUBOOT_LOGICAL_SECTOR_VALIDATION)
378+
#define MCUBOOT_LOGICAL_SECTOR_VALIDATION 1
379+
#endif
380+
360381
#if defined(CONFIG_BOOT_MAX_IMG_SECTORS_AUTO) && defined(MIN_SECTOR_COUNT)
361382

362383
#define MCUBOOT_MAX_IMG_SECTORS MIN_SECTOR_COUNT

0 commit comments

Comments
 (0)