Skip to content

Commit bc055e7

Browse files
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.
1 parent 1a2a075 commit bc055e7

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

newt/builder/targetbuild.go

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -773,29 +773,38 @@ func (t *TargetBuilder) InjectSetting(key string, value string) {
773773
t.injectedSettings.Set(key, value)
774774
}
775775

776-
// Calculates the size of a single boot trailer. This is the amount of flash
777-
// that must be reserved at the end of each image slot.
778-
func (t *TargetBuilder) bootTrailerSize() int {
779-
var minWriteSz int
776+
// Retrieves an integer syscfg flag or returns the default value.
777+
func getSettingValue(settings map[string]syscfg.CfgEntry, settingName string, defaultValue int) int {
780778

781-
entry, ok := t.res.Cfg.Settings["MCU_FLASH_MIN_WRITE_SIZE"]
779+
var intValue int
780+
entry, ok := settings[settingName]
782781
if !ok {
783782
util.StatusMessage(util.VERBOSITY_DEFAULT,
784-
"* Warning: target does not define MCU_FLASH_MIN_WRITE_SIZE "+
785-
"setting; assuming a value of 1.\n")
786-
minWriteSz = 1
783+
"* Warning: target does not define %s "+
784+
"setting; assuming a value of 1.\n", settingName)
785+
intValue = defaultValue
787786
} else {
788787
val, err := util.AtoiNoOct(entry.Value)
789788
if err != nil {
790789
util.StatusMessage(util.VERBOSITY_DEFAULT,
791790
"* Warning: target specifies invalid non-integer "+
792-
"MCU_FLASH_MIN_WRITE_SIZE setting; assuming a "+
793-
"value of 1.\n")
794-
minWriteSz = 1
791+
"%s setting; assuming a "+
792+
"value of 1.\n", settingName)
793+
intValue = defaultValue
795794
} else {
796-
minWriteSz = val
795+
intValue = val
797796
}
798797
}
798+
return intValue
799+
}
800+
801+
// Calculates the size of a single boot trailer. This is the amount of flash
802+
// that must be reserved at the end of each image slot.
803+
func (t *TargetBuilder) bootTrailerSize() int {
804+
805+
minWriteSz := getSettingValue(t.res.Cfg.Settings, "MCU_FLASH_MIN_WRITE_SIZE", 1)
806+
maxImgSectors := getSettingValue(t.res.Cfg.Settings, "BOOTUTIL_MAX_IMG_SECTORS", 128)
807+
799808

800809
/* Mynewt boot trailer format:
801810
*
@@ -814,12 +823,13 @@ func (t *TargetBuilder) bootTrailerSize() int {
814823
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
815824
*/
816825

826+
817827
tsize := 16 + // Magic.
818-
128*minWriteSz*3 + // Swap status.
828+
maxImgSectors*minWriteSz*3 + // Swap status.
819829
minWriteSz + // Copy done.
820830
minWriteSz // Image Ok.
821831

822-
log.Debugf("Min-write-size=%d; boot-trailer-size=%d", minWriteSz, tsize)
832+
log.Debugf("Min-write-size=%d; max-img-sectors=%d; boot-trailer-size=%d", minWriteSz, maxImgSectors, tsize)
823833

824834
return tsize
825835
}

0 commit comments

Comments
 (0)