86729f8f2a44117ac73da0c6d726b0ac75910b97
[openwrt/staging/wigyori.git] / target / linux / ath79 / nand / base-files / lib / upgrade / glinet.sh
1 # SPDX-License-Identifier: GPL-2.0
2 #
3 # Copyright (C) 2019 Jeff Kletsky
4 #
5
6 glinet_using_boot_dev_switch() {
7 if [ "$(fw_printenv -n boot_dev 2>/dev/null)" = "on" ] ; then
8 >&2 echo "NOTE: boot_dev=on; use switch to control boot partition"
9 true
10 else
11 false
12 fi
13 }
14
15 glinet_set_next_boot_nand() {
16 mkdir -p /var/lock
17 ! glinet_using_boot_dev_switch && \
18 fw_setenv bootcount 0 && \
19 >&2 echo "Next boot set for NAND"
20 }
21
22 glinet_set_next_boot_nor() {
23 mkdir -p /var/lock
24 ! glinet_using_boot_dev_switch && \
25 fw_setenv bootcount 3 && \
26 >&2 echo "Next boot set for NOR"
27 }
28
29 glinet_nand_nor_do_upgrade() {
30 set_next_boot_nand() { glinet_set_next_boot_nand; }
31 set_next_boot_nor() { glinet_set_next_boot_nor; }
32 nand_nor_do_upgrade "$1"
33 }
34
35 nand_nor_do_upgrade() {
36 local upgrade_file="$1"
37
38 local pn
39 local found=""
40 local err
41
42 case "$(get_magic_long "$upgrade_file")" in
43
44 "27051956") # U-Boot Image Magic
45
46 for pn in "nor_${PART_NAME}" "$PART_NAME" ; do # firmware
47 if [ "$(find_mtd_index "$pn")" ] ; then
48 PART_NAME="$pn"
49 found="yes"
50 break
51 fi
52 done
53 if [ "$found" = "yes" ] ; then
54 >&2 echo "Running NOR upgrade"
55 default_do_upgrade "$upgrade_file"
56 # At this time, default_do_upgrade() exits on error
57 type set_next_boot_nor >/dev/null && set_next_boot_nor
58 else
59 >&2 echo "ERROR: UPGRADE FAILED: Unable to locate '$PART_NAME' or 'nor_${PART_NAME}'"
60 exit 1
61 fi
62 ;;
63
64 *) # otherwise a file that nand_do_upgrade can process
65
66 for pn in "nand_${CI_KERNPART}" "$CI_KERNPART" ; do # kernel
67 if [ "$(find_mtd_index "$pn")" ] ; then
68 CI_KERNPART="$pn"
69 break
70 fi
71 done
72 for pn in "nand_${CI_UBIPART}" "$CI_UBIPART" ; do # ubi
73 if [ "$(find_mtd_index "$pn")" ] ; then
74 CI_UBIPART="$pn"
75 break
76 fi
77 done
78 for pn in "nand_${CI_ROOTPART}" "$CI_ROOTPART" ; do #rootfs
79 if [ "$(find_mtd_index "$pn")" ] ; then
80 CI_ROOTPART="$pn"
81 break
82 fi
83 done
84 >&2 echo "Running NAND upgrade"
85 # TODO: change order when NAND upgrade offers return
86 type set_next_boot_nand >/dev/null && set_next_boot_nand
87 nand_do_upgrade "$upgrade_file"
88 ;;
89 esac
90 }