Skip to content

Commit 31268f1

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 <[email protected]>
1 parent 72bf727 commit 31268f1

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
@@ -1094,14 +1094,35 @@ config BOOT_FIH_PROFILE_DEFAULT_HIGH
10941094

10951095
endmenu
10961096

1097+
config MCUBOOT_LOGICAL_SECTOR_SIZE
1098+
int "Size of a logical sector"
1099+
default 0
1100+
help
1101+
Set to 0 to use hardware sectors. Any other value here should be
1102+
aligned to hardware sectors in size and alignment.
1103+
1104+
config MCUBOOT_LOGICAL_SECTOR_SIZE_SET
1105+
bool
1106+
default y if MCUBOOT_LOGICAL_SECTOR_SIZE != 0
1107+
1108+
config MCUBOOT_LOGICAL_SECTOR_VALIDATION
1109+
bool "Validate logical sector layout"
1110+
default true if MCUBOOT_LOGICAL_SECTOR_SIZE != 0
1111+
depends on MCUBOOT_LOGICAL_SECTOR_SIZE_SET
1112+
help
1113+
Validation of logical sector size against hardware constrains.
1114+
Should be used to validate compile-time configuration against run-time
1115+
system.
1116+
10971117
config MCUBOOT_DEVICE_SETTINGS
10981118
# Hidden selector for device-specific settings
10991119
bool
11001120
default y
11011121
# CPU options
11021122
select MCUBOOT_DEVICE_CPU_CORTEX_M0 if CPU_CORTEX_M0
11031123
# Enable flash page layout if available
1104-
select FLASH_PAGE_LAYOUT if FLASH_HAS_PAGE_LAYOUT
1124+
select FLASH_PAGE_LAYOUT if FLASH_HAS_PAGE_LAYOUT && !MCUBOOT_LOGICAL_SECTOR_SIZE_SET
1125+
select FLASH_PAGE_LAYOUT if FLASH_HAS_PAGE_LAYOUT && MCUBOOT_LOGICAL_SECTOR_VALIDATION
11051126
# Enable flash_map module as flash I/O back-end
11061127
select FLASH_MAP
11071128

boot/zephyr/flash_map_extended.c

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ BOOT_LOG_MODULE_DECLARE(mcuboot);
6363
#error "FLASH_DEVICE_ID could not be determined"
6464
#endif
6565

66-
static const struct device *flash_dev = DEVICE_DT_GET(FLASH_DEVICE_NODE);
67-
6866
int flash_device_base(uint8_t fd_id, uintptr_t *ret)
6967
{
7068
if (fd_id != FLASH_DEVICE_ID) {
@@ -154,10 +152,26 @@ int flash_area_id_from_direct_image(int image_id)
154152
}
155153
#endif
156154

155+
uint8_t flash_area_get_device_id(const struct flash_area *fa)
156+
{
157+
(void)fa;
158+
return FLASH_DEVICE_ID;
159+
}
160+
161+
#define ERASED_VAL 0xff
162+
__weak uint8_t flash_area_erased_val(const struct flash_area *fap)
163+
{
164+
(void)fap;
165+
return ERASED_VAL;
166+
}
167+
168+
#if (defined(CONFIG_MCUBOOT_LOGICAL_SECTOR_SIZE) && CONFIG_MCUBOOT_LOGICAL_SECTOR_SIZE == 0) || \
169+
defined(CONFIG_MCUBOOT_LOGICAL_SECTOR_VALIDATION)
157170
int flash_area_sector_from_off(off_t off, struct flash_sector *sector)
158171
{
159172
int rc;
160173
struct flash_pages_info page;
174+
static const struct device *flash_dev = DEVICE_DT_GET(FLASH_DEVICE_NODE);
161175

162176
rc = flash_get_page_info_by_offs(flash_dev, off, &page);
163177
if (rc) {
@@ -170,19 +184,6 @@ int flash_area_sector_from_off(off_t off, struct flash_sector *sector)
170184
return rc;
171185
}
172186

173-
uint8_t flash_area_get_device_id(const struct flash_area *fa)
174-
{
175-
(void)fa;
176-
return FLASH_DEVICE_ID;
177-
}
178-
179-
#define ERASED_VAL 0xff
180-
__weak uint8_t flash_area_erased_val(const struct flash_area *fap)
181-
{
182-
(void)fap;
183-
return ERASED_VAL;
184-
}
185-
186187
int flash_area_get_sector(const struct flash_area *fap, off_t off,
187188
struct flash_sector *fsp)
188189
{
@@ -203,3 +204,27 @@ int flash_area_get_sector(const struct flash_area *fap, off_t off,
203204

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

boot/zephyr/include/mcuboot_config/mcuboot_config.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,27 @@
353353
# endif
354354
#endif
355355

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

358379
#define MCUBOOT_MAX_IMG_SECTORS MIN_SECTOR_COUNT

0 commit comments

Comments
 (0)