2 # Copyright (C) 2019 OpenWrt.org
7 iodata_mstc_prepare_fail
() {
8 echo "failed to check and prepare the environment, rebooting..."
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
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"
27 if [ -z "$dflag_offset" ]; then
28 echo 'no debugflag offset provided'
29 iodata_mstc_prepare_fail
32 if [ -z "$persist_mtd" ] ||
[ -z "$factory_mtd" ]; then
33 echo 'cannot find mtd partition(s), "factory" or "persist"'
34 iodata_mstc_prepare_fail
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})
40 if [ "$bootnum" != "1" ] && [ "$bootnum" != "2" ]; then
41 echo "failed to get bootnum, please check the value at 0x4 in ${persist_mtd}"
42 iodata_mstc_prepare_fail
44 if [ "$debugflag" != "0" ] && [ "$debugflag" != "1" ]; then
45 echo "failed to get debugflag, please check the value at ${dflag_offset} in ${factory_mtd}"
46 iodata_mstc_prepare_fail
48 echo "current: bootnum => ${bootnum}, debugflag => ${debugflag}"
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
55 echo "### switch to 1st os-image on next boot ###"
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
62 echo "### enable debug ###"