From 0f96d10c0e05b0e38de03a84a1c67cb74fc4599b Mon Sep 17 00:00:00 2001 From: Drew Moseley Date: Mon, 22 Jan 2018 15:51:40 -0500 Subject: [PATCH] Allow multiple digit partition numbers. Changelog: Title Signed-off-by: Drew Moseley (cherry picked from commit 6dc53c0ea436a5bd82dc7b2d713597875b9b4e23) --- device.go | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/device.go b/device.go index 9b6ccca99..6859ee680 100644 --- a/device.go +++ b/device.go @@ -14,9 +14,11 @@ package main import ( + "fmt" "io" "path/filepath" "strconv" + "strings" "syscall" "github.com/mendersoftware/log" @@ -57,13 +59,13 @@ func (d *device) Reboot() error { func (d *device) SwapPartitions() error { // first get inactive partition - inactivePartition, err := d.getInactivePartition() + inactivePartition, inactivePartitionHex, err := d.getInactivePartition() if err != nil { return err } log.Infof("setting partition for rollback: %s", inactivePartition) - err = d.WriteEnv(BootVars{"mender_boot_part": inactivePartition, "upgrade_available": "0"}) + err = d.WriteEnv(BootVars{"mender_boot_part": inactivePartition, "mender_boot_part_hex": inactivePartitionHex, "upgrade_available": "0"}) if err != nil { return err } @@ -137,32 +139,35 @@ func (d *device) InstallUpdate(image io.ReadCloser, size int64) error { return err } -func (d *device) getInactivePartition() (string, error) { +func (d *device) getInactivePartition() (string, string, error) { inactivePartition, err := d.GetInactive() if err != nil { - return "", errors.New("Error obtaining inactive partition: " + err.Error()) + return "", "", errors.New("Error obtaining inactive partition: " + err.Error()) } log.Debugf("Marking inactive partition (%s) as the new boot candidate.", inactivePartition) - partitionNumber := inactivePartition[len(inactivePartition)-1:] - if _, err := strconv.Atoi(partitionNumber); err != nil { - return "", errors.New("Invalid inactive partition: " + inactivePartition) + partitionNumberDecStr := inactivePartition[len(strings.TrimRight(inactivePartition, "0123456789")):] + partitionNumberDec, err := strconv.Atoi(partitionNumberDecStr) + if err != nil { + return "", "", errors.New("Invalid inactive partition: " + inactivePartition) } - return partitionNumber, nil + partitionNumberHexStr := fmt.Sprintf("%X", partitionNumberDec) + + return partitionNumberDecStr, partitionNumberHexStr, nil } func (d *device) EnableUpdatedPartition() error { - inactivePartition, err := d.getInactivePartition() + inactivePartition, inactivePartitionHex, err := d.getInactivePartition() if err != nil { return err } log.Info("Enabling partition with new image installed to be a boot candidate: ", string(inactivePartition)) // For now we are only setting boot variables - err = d.WriteEnv(BootVars{"upgrade_available": "1", "mender_boot_part": inactivePartition, "bootcount": "0"}) + err = d.WriteEnv(BootVars{"upgrade_available": "1", "mender_boot_part": inactivePartition, "mender_boot_part_hex": inactivePartitionHex, "bootcount": "0"}) if err != nil { return err }