From c27c0d04ef49beda54edc471016b4a2d4ca231d5 Mon Sep 17 00:00:00 2001 From: Wiktor Kwiatkowski Date: Tue, 7 Oct 2025 11:30:17 +0200 Subject: [PATCH] newt/builder: read BOOTUTIL_MAX_IMG_SECTORS from syscfg for boot trailer Boot trailer size was hardcoded to 128 sectors, ignoring the value set in syscfg. This caused mismatches between trailer size and the actual image size when BOOTUTIL_MAX_IMG_SECTORS was set to value other than 128, potentially making images not fit in the slot. Now the trailer size is calculated based on the syscfg value. --- newt/builder/targetbuild.go | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/newt/builder/targetbuild.go b/newt/builder/targetbuild.go index d12cf1043d..3792c4600f 100644 --- a/newt/builder/targetbuild.go +++ b/newt/builder/targetbuild.go @@ -773,29 +773,37 @@ func (t *TargetBuilder) InjectSetting(key string, value string) { t.injectedSettings.Set(key, value) } -// Calculates the size of a single boot trailer. This is the amount of flash -// that must be reserved at the end of each image slot. -func (t *TargetBuilder) bootTrailerSize() int { - var minWriteSz int +// Retrieves an integer syscfg flag or returns the default value. +func getSettingIntValue(settings map[string]syscfg.CfgEntry, settingName string, defaultValue int) int { - entry, ok := t.res.Cfg.Settings["MCU_FLASH_MIN_WRITE_SIZE"] + var intValue int + entry, ok := settings[settingName] if !ok { util.StatusMessage(util.VERBOSITY_DEFAULT, - "* Warning: target does not define MCU_FLASH_MIN_WRITE_SIZE "+ - "setting; assuming a value of 1.\n") - minWriteSz = 1 + "* Warning: target does not define %s "+ + "setting; assuming a value of %d.\n", settingName, defaultValue) + intValue = defaultValue } else { val, err := util.AtoiNoOct(entry.Value) if err != nil { util.StatusMessage(util.VERBOSITY_DEFAULT, "* Warning: target specifies invalid non-integer "+ - "MCU_FLASH_MIN_WRITE_SIZE setting; assuming a "+ - "value of 1.\n") - minWriteSz = 1 + "%s setting; assuming a "+ + "value of %d.\n", settingName, defaultValue) + intValue = defaultValue } else { - minWriteSz = val + intValue = val } } + return intValue +} + +// Calculates the size of a single boot trailer. This is the amount of flash +// that must be reserved at the end of each image slot. +func (t *TargetBuilder) bootTrailerSize() int { + + minWriteSz := getSettingIntValue(t.res.Cfg.Settings, "MCU_FLASH_MIN_WRITE_SIZE", 1) + maxImgSectors := getSettingIntValue(t.res.Cfg.Settings, "BOOTUTIL_MAX_IMG_SECTORS", 128) /* Mynewt boot trailer format: * @@ -815,11 +823,11 @@ func (t *TargetBuilder) bootTrailerSize() int { */ tsize := 16 + // Magic. - 128*minWriteSz*3 + // Swap status. + maxImgSectors*minWriteSz*3 + // Swap status. minWriteSz + // Copy done. minWriteSz // Image Ok. - log.Debugf("Min-write-size=%d; boot-trailer-size=%d", minWriteSz, tsize) + log.Debugf("Min-write-size=%d; max-img-sectors=%d; boot-trailer-size=%d", minWriteSz, maxImgSectors, tsize) return tsize }