4
0
Fork 0

fixed partition ID in uboot dat

This commit is contained in:
Benjamin Böhmke 2019-12-15 14:56:25 +01:00
parent 1ea1259aad
commit 36db1f5096
4 changed files with 49 additions and 37 deletions

View File

@ -18,9 +18,9 @@ sha256sum -c ${image_file}.sha256
current_idx=$(rdev | sed 's#/dev/mmcblk0p\([^ ]*\).*#\1#') current_idx=$(rdev | sed 's#/dev/mmcblk0p\([^ ]*\).*#\1#')
if [ $current_idx -eq 2 ]; then if [ $current_idx -eq 2 ]; then
echo "Start update for partition B"
else
echo "Start update for partition A" echo "Start update for partition A"
else
echo "Start update for partition B"
fi fi
flash_device="/dev/mmcblk0p${flash_idx}" flash_device="/dev/mmcblk0p${flash_idx}"

View File

@ -71,7 +71,7 @@ int main(int argc, char* argv[]) {
data[1] = 0; data[1] = 0;
// boot partition // boot partition
data[2] = 2; // A=1, B=2 data[2] = 2; // A=2, B=3
} }
// handle commands // handle commands

View File

@ -17,15 +17,15 @@ var ubootFile = "/uboot/uboot.dat"
var ubootRemountRW = "mount -o remount,rw /uboot" var ubootRemountRW = "mount -o remount,rw /uboot"
var ubootRemountRO = "mount -o remount,ro /uboot" var ubootRemountRO = "mount -o remount,ro /uboot"
var rootPartitionA = "/dev/mmcblk0p1" var rootPartitionA = "/dev/mmcblk0p2"
var rootPartitionB = "/dev/mmcblk0p2" var rootPartitionB = "/dev/mmcblk0p3"
// loadUbootDat file to byte array // loadUbootDat file to byte array
func loadUbootDat() ([]byte, error) { func loadUbootDat() ([]byte, error) {
defaultData := make([]byte, 1024) defaultData := make([]byte, 1024)
defaultData[0] = 1 // file version defaultData[0] = 1 // file version
defaultData[1] = 0 // boot counter 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) data, err := ioutil.ReadFile(ubootFile)
if err != nil { if err != nil {
@ -81,19 +81,19 @@ func UBootResetCounter() error {
return saveUbootDat(data) return saveUbootDat(data)
} }
// UBootActive returns the active partition. A=1, B=2 // UBootActive returns the active partition. A=2, B=3
func UBootActive() uint8 { func UBootActive() uint8 {
data, _ := loadUbootDat() data, _ := loadUbootDat()
return data[2] 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 { func UBootSetActive(active uint8) error {
data, _ := loadUbootDat() data, _ := loadUbootDat()
if active == 1 { if active == 2 {
data[2] = 1
} else {
data[2] = 2 data[2] = 2
} else {
data[2] = 3
} }
return saveUbootDat(data) return saveUbootDat(data)
} }
@ -102,7 +102,7 @@ func UBootSetActive(active uint8) error {
func UpdateSystem(image string) error { func UpdateSystem(image string) error {
data, _ := loadUbootDat() data, _ := loadUbootDat()
rootPart := rootPartitionA rootPart := rootPartitionA
if data[2] == 1 { if data[2] == 2 {
rootPart = rootPartitionB rootPart = rootPartitionB
} }
@ -135,9 +135,9 @@ func UpdateSystem(image string) error {
} }
// switch active partition // switch active partition
if data[2] == 1 { if data[2] == 2 {
return UBootSetActive(2) return UBootSetActive(3)
} else { } else {
return UBootSetActive(1) return UBootSetActive(2)
} }
} }

View File

@ -24,17 +24,21 @@ func init() {
func TestLoadUbootDat(t *testing.T) { func TestLoadUbootDat(t *testing.T) {
ass := assert.New(t) ass := assert.New(t)
defer func() {
_ = os.Remove(ubootFile)
}()
// file missing // file missing
data, err := loadUbootDat() data, err := loadUbootDat()
ass.EqualError(err, "failed to open file: open test_uboot: no such file or directory") 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 // file with invalid data
ass.NoError(ioutil.WriteFile(ubootFile, []byte{}, os.ModePerm)) ass.NoError(ioutil.WriteFile(ubootFile, []byte{}, os.ModePerm))
data, err = loadUbootDat() data, err = loadUbootDat()
ass.EqualError(err, "invalid dat file -> fallback to defaults") 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 // file with invalid CRC
testData := make([]byte, 1024) testData := make([]byte, 1024)
@ -45,7 +49,7 @@ func TestLoadUbootDat(t *testing.T) {
ass.NoError(ioutil.WriteFile(ubootFile, testData, os.ModePerm)) ass.NoError(ioutil.WriteFile(ubootFile, testData, os.ModePerm))
data, err = loadUbootDat() data, err = loadUbootDat()
ass.EqualError(err, "invalid crc -> fallback to defaults") 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 // file with valid CRC
binary.LittleEndian.PutUint32(testData[1020:], 0x982E8B7A) binary.LittleEndian.PutUint32(testData[1020:], 0x982E8B7A)
@ -54,13 +58,15 @@ func TestLoadUbootDat(t *testing.T) {
data, err = loadUbootDat() data, err = loadUbootDat()
ass.NoError(err) ass.NoError(err)
ass.Equal(testData, data) ass.Equal(testData, data)
_ = os.Remove(ubootFile)
} }
func TestSaveUbootDat(t *testing.T) { func TestSaveUbootDat(t *testing.T) {
ass := assert.New(t) ass := assert.New(t)
defer func() {
_ = os.Remove(ubootFile)
}()
testData := make([]byte, 1024) testData := make([]byte, 1024)
testData[0] = 1 testData[0] = 1
testData[1] = 2 testData[1] = 2
@ -72,13 +78,15 @@ func TestSaveUbootDat(t *testing.T) {
data, err := ioutil.ReadFile(ubootFile) data, err := ioutil.ReadFile(ubootFile)
ass.NoError(err) ass.NoError(err)
ass.Equal(testData, data) ass.Equal(testData, data)
_ = os.Remove(ubootFile)
} }
func TestUBootResetCounter(t *testing.T) { func TestUBootResetCounter(t *testing.T) {
ass := assert.New(t) ass := assert.New(t)
defer func() {
_ = os.Remove(ubootFile)
}()
// write test file // write test file
testData := make([]byte, 1024) testData := make([]byte, 1024)
testData[0] = 1 testData[0] = 1
@ -92,13 +100,15 @@ func TestUBootResetCounter(t *testing.T) {
data, err := ioutil.ReadFile(ubootFile) data, err := ioutil.ReadFile(ubootFile)
ass.NoError(err) ass.NoError(err)
ass.Zero(data[1]) ass.Zero(data[1])
_ = os.Remove(ubootFile)
} }
func TestUBootActive(t *testing.T) { func TestUBootActive(t *testing.T) {
ass := assert.New(t) ass := assert.New(t)
defer func() {
_ = os.Remove(ubootFile)
}()
// write test file // write test file
testData := make([]byte, 1024) testData := make([]byte, 1024)
testData[0] = 1 testData[0] = 1
@ -108,13 +118,15 @@ func TestUBootActive(t *testing.T) {
ass.NoError(ioutil.WriteFile(ubootFile, testData, os.ModePerm)) ass.NoError(ioutil.WriteFile(ubootFile, testData, os.ModePerm))
ass.Equal(uint8(2), UBootActive()) ass.Equal(uint8(2), UBootActive())
_ = os.Remove(ubootFile)
} }
func TestUBootSetActive(t *testing.T) { func TestUBootSetActive(t *testing.T) {
ass := assert.New(t) ass := assert.New(t)
defer func() {
_ = os.Remove(ubootFile)
}()
// write test file // write test file
testData := make([]byte, 1024) testData := make([]byte, 1024)
testData[0] = 1 testData[0] = 1
@ -127,14 +139,19 @@ func TestUBootSetActive(t *testing.T) {
data, err := ioutil.ReadFile(ubootFile) data, err := ioutil.ReadFile(ubootFile)
ass.NoError(err) ass.NoError(err)
ass.Equal(uint8(1), data[2]) ass.Equal(uint8(3), data[2])
_ = os.Remove(ubootFile)
} }
func TestUpdateSystem(t *testing.T) { func TestUpdateSystem(t *testing.T) {
ass := assert.New(t) ass := assert.New(t)
defer func() {
_ = os.Remove("test_image.gz")
_ = os.Remove(ubootFile)
_ = os.Remove(rootPartitionA)
_ = os.Remove(rootPartitionB)
}()
// test uboot file // test uboot file
testData := make([]byte, 1024) testData := make([]byte, 1024)
testData[0] = 1 testData[0] = 1
@ -159,21 +176,16 @@ func TestUpdateSystem(t *testing.T) {
ass.NoError(gzipWriter.Close()) ass.NoError(gzipWriter.Close())
ass.NoError(file.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")) ass.NoError(UpdateSystem("test_image.gz"))
// check if image was written // check if image was written
data, err := ioutil.ReadFile(rootPartitionA) data, err := ioutil.ReadFile(rootPartitionB)
ass.NoError(err) ass.NoError(err)
ass.Equal(testImgData, data) ass.Equal(testImgData, data)
// check if uboot dat was updated // check if uboot dat was updated
data, err = ioutil.ReadFile(ubootFile) data, err = ioutil.ReadFile(ubootFile)
ass.NoError(err) ass.NoError(err)
ass.Equal(uint8(1), data[2]) ass.Equal(uint8(3), data[2])
_ = os.Remove("test_image.gz")
_ = os.Remove(ubootFile)
_ = os.Remove(rootPartitionA)
_ = os.Remove(rootPartitionB)
} }