Skip to content

Commit 1127fdc

Browse files
wiktorkwiatkowskim-gorecki
authored andcommitted
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 1127fdc

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

newt/builder/targetbuild.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -773,29 +773,37 @@ 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 getSettingIntValue(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 %d.\n", settingName, defaultValue)
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 %d.\n", settingName, defaultValue)
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 := getSettingIntValue(t.res.Cfg.Settings, "MCU_FLASH_MIN_WRITE_SIZE", 1)
806+
maxImgSectors := getSettingIntValue(t.res.Cfg.Settings, "BOOTUTIL_MAX_IMG_SECTORS", 128)
799807

800808
/* Mynewt boot trailer format:
801809
*
@@ -815,11 +823,11 @@ func (t *TargetBuilder) bootTrailerSize() int {
815823
*/
816824

817825
tsize := 16 + // Magic.
818-
128*minWriteSz*3 + // Swap status.
826+
maxImgSectors*minWriteSz*3 + // Swap status.
819827
minWriteSz + // Copy done.
820828
minWriteSz // Image Ok.
821829

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

824832
return tsize
825833
}

0 commit comments

Comments
 (0)