4
0
raspi-alpine-builder/update.go

145 lines
3.0 KiB
Go
Raw Normal View History

2019-12-01 21:20:32 +01:00
package alpine_builder
import (
"compress/gzip"
"encoding/binary"
"errors"
"fmt"
"hash/crc32"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
)
2019-12-15 16:55:18 +01:00
var mountCommand = "mount"
2019-12-01 21:20:32 +01:00
var ubootFile = "/uboot/uboot.dat"
2019-12-15 16:55:18 +01:00
var ubootRemountRW = []string{"-o", "remount,rw", "/uboot"}
var ubootRemountRO = []string{"-o", "remount,ro", "/uboot"}
2019-12-01 21:20:32 +01:00
2019-12-15 14:56:25 +01:00
var rootPartitionA = "/dev/mmcblk0p2"
var rootPartitionB = "/dev/mmcblk0p3"
2019-12-01 21:20:32 +01:00
// loadUbootDat file to byte array
func loadUbootDat() ([]byte, error) {
defaultData := make([]byte, 1024)
defaultData[0] = 1 // file version
defaultData[1] = 0 // boot counter
2019-12-15 14:56:25 +01:00
defaultData[2] = 2 // boot partition A=2, B=3
2019-12-01 21:20:32 +01:00
data, err := ioutil.ReadFile(ubootFile)
if err != nil {
return defaultData, fmt.Errorf("failed to open file: %w", err)
}
// invalid dat file?
if len(data) < 1024 {
return defaultData, errors.New("invalid dat file -> fallback to defaults")
}
crc := binary.LittleEndian.Uint32(data[1020:])
bla := crc32.ChecksumIEEE(data[:1020])
if crc != bla {
return defaultData, errors.New("invalid crc -> fallback to defaults")
}
return data, nil
}
// saveUbootDat from byte array
func saveUbootDat(data []byte) error {
// update crc
binary.LittleEndian.PutUint32(data[1020:],
crc32.ChecksumIEEE(data[:1020]))
// remount boot partition - writable
2019-12-15 16:55:18 +01:00
cmd := exec.Command(mountCommand, ubootRemountRW...)
2019-12-01 21:20:32 +01:00
err := cmd.Run()
if err != nil {
return fmt.Errorf("failed to remount RW: %w", err)
}
// update uboot dat file
err = ioutil.WriteFile(ubootFile, data[:1024], os.ModePerm)
if err != nil {
return fmt.Errorf("failed write uboot dat: %w", err)
}
// remount boot partition - readonly
2019-12-15 16:55:18 +01:00
cmd = exec.Command(mountCommand, ubootRemountRO...)
2019-12-01 21:20:32 +01:00
err = cmd.Run()
if err != nil {
return fmt.Errorf("failed to remount RO: %w", err)
}
return nil
}
// UBootResetCounter to 0
func UBootResetCounter() error {
data, _ := loadUbootDat()
data[1] = 0
return saveUbootDat(data)
}
2019-12-15 14:56:25 +01:00
// UBootActive returns the active partition. A=2, B=3
2019-12-01 21:20:32 +01:00
func UBootActive() uint8 {
data, _ := loadUbootDat()
return data[2]
}
2019-12-15 14:56:25 +01:00
// UBootSetActive sets the active partition. A=2, B=3
2019-12-01 21:20:32 +01:00
func UBootSetActive(active uint8) error {
data, _ := loadUbootDat()
2019-12-15 14:56:25 +01:00
if active == 2 {
2019-12-01 21:20:32 +01:00
data[2] = 2
2019-12-15 14:56:25 +01:00
} else {
data[2] = 3
2019-12-01 21:20:32 +01:00
}
return saveUbootDat(data)
}
// UpdateSystem with the given image
func UpdateSystem(image string) error {
data, _ := loadUbootDat()
rootPart := rootPartitionA
2019-12-15 14:56:25 +01:00
if data[2] == 2 {
2019-12-01 21:20:32 +01:00
rootPart = rootPartitionB
}
// open image file
inFile, err := os.Open(image)
if err != nil {
log.Fatal(err)
}
defer inFile.Close()
// decompress image
inDecompress, err := gzip.NewReader(inFile)
if err != nil {
log.Fatal(err)
}
defer inDecompress.Close()
// open root partition
out, err := os.OpenFile(rootPart,
os.O_WRONLY|os.O_TRUNC|os.O_SYNC, os.ModePerm)
if err != nil {
log.Fatal(err)
}
defer out.Close()
// write update
_, err = io.Copy(out, inDecompress)
if err != nil {
log.Fatal(err)
}
// switch active partition
2019-12-15 14:56:25 +01:00
if data[2] == 2 {
return UBootSetActive(3)
2019-12-01 21:20:32 +01:00
} else {
2019-12-15 14:56:25 +01:00
return UBootSetActive(2)
2019-12-01 21:20:32 +01:00
}
}