rpcd: iwinfo plugin fixes
[openwrt.git] / target / linux / ramips / base-files / lib / upgrade / ubnt.sh
1 #
2 # Copyright (C) 2015 OpenWrt.org
3 #
4
5 . /lib/functions.sh
6 #Note: this code also uses some functions from nand.sh, but it is expected to be run by nand.sh, so we are not
7 #sourcing it explicitly here
8
9 UBNT_ERX_KERNEL_INDEX_OFFSET=160
10
11 ubnt_get_target_kernel() {
12         local factory_mtd=$1
13         local current_kernel_index=$(hexdump -s $UBNT_ERX_KERNEL_INDEX_OFFSET -n 1 -e '/1 "%X "' ${factory_mtd})
14
15         if [ $current_kernel_index == "0" ]; then
16                 echo 'kernel2'
17         elif [ $current_kernel_index == "1" ]; then
18                 echo 'kernel1'
19         fi
20 }
21
22 ubnt_update_target_kernel() {
23         local factory_mtd=$1
24         local kernel_part=$2
25
26         local new_kernel_index
27         if [ $kernel_part == "kernel1" ]; then
28                 new_kernel_index="\x00"
29         elif [ $kernel_part == "kernel2" ]; then
30                 new_kernel_index="\x01"
31         else
32                 echo 'Unknown kernel image index' >&2
33                 return 1
34         fi
35
36         if ! (echo -e $new_kernel_index | dd of=${factory_mtd} bs=1 count=1 seek=$UBNT_ERX_KERNEL_INDEX_OFFSET); then
37                 echo 'Failed to update kernel bootup index' >&2
38                 return 1
39         fi
40 }
41
42 platform_upgrade_ubnt_erx() {
43         local factory_mtd=$(find_mtd_part factory)
44         if [ -z "$factory_mtd" ]; then
45                 echo "cannot find factory partition" >&2
46                 exit 1
47         fi
48
49         local kernel_part="$(ubnt_get_target_kernel ${factory_mtd})"
50         if [ -z "$kernel_part" ]; then
51                 echo "cannot find factory partition" >&2
52                 exit 1
53         fi
54
55         # This is a global defined in nand.sh, sets partition kernel will be flashed into
56         CI_KERNPART=${kernel_part}
57
58         #Remove volume possibly left over from stock firmware
59         local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
60         if [ -z "$ubidev" ]; then
61                 local mtdnum="$( find_mtd_index "$CI_UBIPART" )"
62                 if [ -z "$mtdnum" ]; then
63                         echo "cannot find ubi mtd partition $CI_UBIPART" >&2
64                         exit 1
65                 fi
66                 ubiattach -m "$mtdnum"
67                 sync
68                 ubidev="$( nand_find_ubi "$CI_UBIPART" )"
69         fi
70         if [ -n "$ubidev" ]; then
71                 local troot_ubivol="$( nand_find_volume $ubidev troot )"
72                 [ -n "$troot_ubivol" ] && ubirmvol /dev/$ubidev -N troot || true
73         fi
74
75         ubnt_update_target_kernel ${factory_mtd} ${kernel_part} || exit 1
76 }