071a132e025b3a1ed3ae435e83205fe9523fd5ef
[openwrt/openwrt.git] / target / linux / ramips / mt7621 / base-files / lib / upgrade / iodata.sh
1 #
2 # Copyright (C) 2019 OpenWrt.org
3 #
4
5 . /lib/functions.sh
6
7 iodata_mstc_prepare_fail() {
8 echo "failed to check and prepare the environment, rebooting..."
9 umount -a
10 reboot -f
11 }
12
13 # I-O DATA devices manufactured by MSTC (MitraStar Technology Corp.)
14 # have two important flags:
15 # - bootnum: switch between two os images
16 # use 1st image in OpenWrt
17 # - debugflag: enable/disable debug
18 # users can interrupt Z-Loader for recovering the device if enabled
19 #
20 # parameters:
21 # - $1: the offset of "debugflag"
22 iodata_mstc_upgrade_prepare() {
23 local persist_mtd="$(find_mtd_part persist)"
24 local factory_mtd="$(find_mtd_part factory)"
25 local dflag_offset="$1"
26
27 if [ -z "$dflag_offset" ]; then
28 echo 'no debugflag offset provided'
29 iodata_mstc_prepare_fail
30 fi
31
32 if [ -z "$persist_mtd" -o -z "$factory_mtd" ]; then
33 echo 'cannot find mtd partition(s), "factory" or "persist"'
34 iodata_mstc_prepare_fail
35 fi
36
37 local bootnum=$(hexdump -s 4 -n 1 -e '"%x"' ${persist_mtd})
38 local debugflag=$(hexdump -s $((dflag_offset)) -n 1 -e '"%x"' ${factory_mtd})
39
40 if [ "$bootnum" != "1" -a "$bootnum" != "2" ]; then
41 echo "failed to get bootnum, please check the value at 0x4 in ${persist_mtd}"
42 iodata_mstc_prepare_fail
43 fi
44 if [ "$debugflag" != "0" -a "$debugflag" != "1" ]; then
45 echo "failed to get debugflag, please check the value at ${dflag_offset} in ${factory_mtd}"
46 iodata_mstc_prepare_fail
47 fi
48 echo "current: bootnum => ${bootnum}, debugflag => ${debugflag}"
49
50 if [ "$bootnum" = "2" ]; then
51 if ! (echo -ne "\x01" | dd bs=1 count=1 seek=4 conv=notrunc of=${persist_mtd} 2>/dev/null); then
52 echo "failed to set bootnum"
53 iodata_mstc_prepare_fail
54 fi
55 echo "### switch to 1st os-image on next boot ###"
56 fi
57 if [ "$debugflag" = "0" ]; then
58 if ! (echo -ne "\x01" | dd bs=1 count=1 seek=$((dflag_offset)) conv=notrunc of=${factory_mtd} 2>/dev/null); then
59 echo "failed to set debugflag"
60 iodata_mstc_prepare_fail
61 fi
62 echo "### enable debug ###"
63 fi
64 }