diff --git a/resources/scripts/ab_flash b/resources/scripts/ab_flash index c44e0c5..83fd67b 100755 --- a/resources/scripts/ab_flash +++ b/resources/scripts/ab_flash @@ -18,9 +18,9 @@ sha256sum -c ${image_file}.sha256 current_idx=$(rdev | sed 's#/dev/mmcblk0p\([^ ]*\).*#\1#') if [ $current_idx -eq 2 ]; then - echo "Start update for partition B" -else echo "Start update for partition A" +else + echo "Start update for partition B" fi flash_device="/dev/mmcblk0p${flash_idx}" diff --git a/resources/uboot.c b/resources/uboot.c index bc00625..d58e8d5 100644 --- a/resources/uboot.c +++ b/resources/uboot.c @@ -71,7 +71,7 @@ int main(int argc, char* argv[]) { data[1] = 0; // boot partition - data[2] = 2; // A=1, B=2 + data[2] = 2; // A=2, B=3 } // handle commands diff --git a/update.go b/update.go index fe0316a..630c98c 100644 --- a/update.go +++ b/update.go @@ -17,15 +17,15 @@ var ubootFile = "/uboot/uboot.dat" var ubootRemountRW = "mount -o remount,rw /uboot" var ubootRemountRO = "mount -o remount,ro /uboot" -var rootPartitionA = "/dev/mmcblk0p1" -var rootPartitionB = "/dev/mmcblk0p2" +var rootPartitionA = "/dev/mmcblk0p2" +var rootPartitionB = "/dev/mmcblk0p3" // loadUbootDat file to byte array func loadUbootDat() ([]byte, error) { defaultData := make([]byte, 1024) defaultData[0] = 1 // file version defaultData[1] = 0 // boot counter - defaultData[2] = 1 // boot partition A=1, B=2 + defaultData[2] = 2 // boot partition A=2, B=3 data, err := ioutil.ReadFile(ubootFile) if err != nil { @@ -81,19 +81,19 @@ func UBootResetCounter() error { return saveUbootDat(data) } -// UBootActive returns the active partition. A=1, B=2 +// UBootActive returns the active partition. A=2, B=3 func UBootActive() uint8 { data, _ := loadUbootDat() return data[2] } -// UBootSetActive sets the active partition. A=1, B=2 +// UBootSetActive sets the active partition. A=2, B=3 func UBootSetActive(active uint8) error { data, _ := loadUbootDat() - if active == 1 { - data[2] = 1 - } else { + if active == 2 { data[2] = 2 + } else { + data[2] = 3 } return saveUbootDat(data) } @@ -102,7 +102,7 @@ func UBootSetActive(active uint8) error { func UpdateSystem(image string) error { data, _ := loadUbootDat() rootPart := rootPartitionA - if data[2] == 1 { + if data[2] == 2 { rootPart = rootPartitionB } @@ -135,9 +135,9 @@ func UpdateSystem(image string) error { } // switch active partition - if data[2] == 1 { - return UBootSetActive(2) + if data[2] == 2 { + return UBootSetActive(3) } else { - return UBootSetActive(1) + return UBootSetActive(2) } } diff --git a/update_test.go b/update_test.go index 19aad20..d506b74 100644 --- a/update_test.go +++ b/update_test.go @@ -24,17 +24,21 @@ func init() { func TestLoadUbootDat(t *testing.T) { ass := assert.New(t) + defer func() { + _ = os.Remove(ubootFile) + }() + // file missing data, err := loadUbootDat() ass.EqualError(err, "failed to open file: open test_uboot: no such file or directory") - ass.Equal([]byte{1, 0, 1}, data[:3]) + ass.Equal([]byte{1, 0, 2}, data[:3]) // file with invalid data ass.NoError(ioutil.WriteFile(ubootFile, []byte{}, os.ModePerm)) data, err = loadUbootDat() ass.EqualError(err, "invalid dat file -> fallback to defaults") - ass.Equal([]byte{1, 0, 1}, data[:3]) + ass.Equal([]byte{1, 0, 2}, data[:3]) // file with invalid CRC testData := make([]byte, 1024) @@ -45,7 +49,7 @@ func TestLoadUbootDat(t *testing.T) { ass.NoError(ioutil.WriteFile(ubootFile, testData, os.ModePerm)) data, err = loadUbootDat() ass.EqualError(err, "invalid crc -> fallback to defaults") - ass.Equal([]byte{1, 0, 1}, data[:3]) + ass.Equal([]byte{1, 0, 2}, data[:3]) // file with valid CRC binary.LittleEndian.PutUint32(testData[1020:], 0x982E8B7A) @@ -54,13 +58,15 @@ func TestLoadUbootDat(t *testing.T) { data, err = loadUbootDat() ass.NoError(err) ass.Equal(testData, data) - - _ = os.Remove(ubootFile) } func TestSaveUbootDat(t *testing.T) { ass := assert.New(t) + defer func() { + _ = os.Remove(ubootFile) + }() + testData := make([]byte, 1024) testData[0] = 1 testData[1] = 2 @@ -72,13 +78,15 @@ func TestSaveUbootDat(t *testing.T) { data, err := ioutil.ReadFile(ubootFile) ass.NoError(err) ass.Equal(testData, data) - - _ = os.Remove(ubootFile) } func TestUBootResetCounter(t *testing.T) { ass := assert.New(t) + defer func() { + _ = os.Remove(ubootFile) + }() + // write test file testData := make([]byte, 1024) testData[0] = 1 @@ -92,13 +100,15 @@ func TestUBootResetCounter(t *testing.T) { data, err := ioutil.ReadFile(ubootFile) ass.NoError(err) ass.Zero(data[1]) - - _ = os.Remove(ubootFile) } func TestUBootActive(t *testing.T) { ass := assert.New(t) + defer func() { + _ = os.Remove(ubootFile) + }() + // write test file testData := make([]byte, 1024) testData[0] = 1 @@ -108,13 +118,15 @@ func TestUBootActive(t *testing.T) { ass.NoError(ioutil.WriteFile(ubootFile, testData, os.ModePerm)) ass.Equal(uint8(2), UBootActive()) - - _ = os.Remove(ubootFile) } func TestUBootSetActive(t *testing.T) { ass := assert.New(t) + defer func() { + _ = os.Remove(ubootFile) + }() + // write test file testData := make([]byte, 1024) testData[0] = 1 @@ -127,14 +139,19 @@ func TestUBootSetActive(t *testing.T) { data, err := ioutil.ReadFile(ubootFile) ass.NoError(err) - ass.Equal(uint8(1), data[2]) - - _ = os.Remove(ubootFile) + ass.Equal(uint8(3), data[2]) } func TestUpdateSystem(t *testing.T) { ass := assert.New(t) + defer func() { + _ = os.Remove("test_image.gz") + _ = os.Remove(ubootFile) + _ = os.Remove(rootPartitionA) + _ = os.Remove(rootPartitionB) + }() + // test uboot file testData := make([]byte, 1024) testData[0] = 1 @@ -159,21 +176,16 @@ func TestUpdateSystem(t *testing.T) { ass.NoError(gzipWriter.Close()) ass.NoError(file.Close()) - ass.NoError(ioutil.WriteFile(rootPartitionA, nil, os.ModePerm)) + ass.NoError(ioutil.WriteFile(rootPartitionB, nil, os.ModePerm)) ass.NoError(UpdateSystem("test_image.gz")) // check if image was written - data, err := ioutil.ReadFile(rootPartitionA) + data, err := ioutil.ReadFile(rootPartitionB) ass.NoError(err) ass.Equal(testImgData, data) // check if uboot dat was updated data, err = ioutil.ReadFile(ubootFile) ass.NoError(err) - ass.Equal(uint8(1), data[2]) - - _ = os.Remove("test_image.gz") - _ = os.Remove(ubootFile) - _ = os.Remove(rootPartitionA) - _ = os.Remove(rootPartitionB) + ass.Equal(uint8(3), data[2]) }