system.go 2.05 KB
package main

import (
	"io/ioutil"
	"os/exec"
)

func getVersion() string {
	env, err := getEnv(1)
	if err != nil {
		return ""
	}

	version, exist := env["falinux_version"]
	if !exist {
		return ""
	}

	return version
}

func updateSystem() error {
	env, err := getEnv(1)
	if err != nil {
		return err
	}

	bootpart := env["bootpart"]

	var dir string
	var partname string
	switch bootpart {
	case "1":
		dir = "/boot1"
		partname = "/dev/mmcblk3p2"
		env["bootpart"] = "2"
	case "2":
		dir = "/boot0"
		partname = "/dev/mmcblk3p1"
		env["bootpart"] = "1"
	default:
		dir = "/boot0"
		partname = "/dev/mmcblk3p1"
		env["bootpart"] = "1"
	}

	if !partitionIsExists() {
		makePartition()
	} else {
		formatPartition(partname)
	}

	if err := exec.Command("mount", "-o", "rw", dir).Run(); err != nil {
		return err
	}
	defer exec.Command("umount", dir).Run()

	if err := exec.Command("unzip", "-o", "/tmp/system.zip", "-d", dir).Run(); err != nil {
		return err
	}

	exec.Command("sync").Run()

	if ver, err := ioutil.ReadFile(dir + "/version"); err != nil {
		env["falinux_version"] = ""
	} else {
		env["falinux_version"] = string(ver)
	}

	return setEnv(1, env)
}

func updateApp() error {
	const cmdline = `\
	rm -rf /app/bin.old /app/bin.new; \
	mkdir /app/bin.new && \
	unzip -o /tmp/application.zip -d /app/bin.new && \
	sync && \
	mv /app/bin /app/bin.old; \
	mv /app/bin.new /app/bin; \
	sync`

	return exec.Command("/bin/sh", "-c", cmdline).Run()
}

func partitionIsExists() bool {
	err := exec.Command("test", "-e", "/dev/mmcblk3p1", "-a", "-e", "/dev/mmcblk3p2").Run()
	return err == nil
}

func makePartition() {
	exec.Command("umount", "/app").Run()
	exec.Command("/bin/sh", "-c", "/root/.falinux/mk-mmc-part.sh").Run()
}

func formatPartition(partname string) {
	exec.Command("mkfs.fat", partname).Run()
}

func reboot() {
	exec.Command("killall", "-19", "watchdog").Run()
	exec.Command("reboot").Run()
}

func restore() {
	m := make(map[string]string)

	setEnv(0, m)
	setEnv(1, m)
	setEnv(2, m)
	setEnv(3, m)

	exec.Command("/bin/sh", "-c", "/root/.falinux/mk-mmc-part.sh").Run()
}