ar71xx: remove hard-coded folder name from Mikrotik RB upgrade
[openwrt/openwrt.git] / target / linux / ar71xx / base-files / lib / upgrade / merakinand.sh
1 #!/bin/sh
2 #
3 # Copyright (C) 2015-2016 Chris Blake <chrisrblake93@gmail.com>
4 #
5 # Custom upgrade script for Meraki NAND devices (ex. MR18)
6 # Based on dir825.sh and stock nand functions
7 #
8 . /lib/ar71xx.sh
9 . /lib/functions.sh
10
11 get_magic_at() {
12 local mtddev=$1
13 local pos=$2
14 dd bs=1 count=2 skip=$pos if=$mtddev 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
15 }
16
17 meraki_is_caldata_valid() {
18 local board=$1
19 local mtddev=$2
20 local magic
21
22 case "$board" in
23 "mr18")
24 magic=$(get_magic_at $mtddev 4096)
25 [ "$magic" != "0202" ] && return 0
26
27 magic=$(get_magic_at $mtddev 20480)
28 [ "$magic" != "0202" ] && return 0
29
30 magic=$(get_magic_at $mtddev 36864)
31 [ "$magic" != "0202" ] && return 0
32
33 return 1
34 ;;
35 "z1")
36 magic=$(get_magic_at $mtddev 4096)
37 [ "$magic" != "0202" ] && return 0
38
39 magic=$(get_magic_at $mtddev 86016)
40 [ "$magic" != "a55a" ] && return 0
41
42 return 1
43 ;;
44 *)
45 return 1
46 ;;
47 esac
48 }
49
50 merakinand_copy_caldata() {
51 local cal_src=$1
52 local cal_dst=$2
53 local ubidev="$(nand_find_ubi $CI_UBIPART)"
54 local board_name="$(board_name)"
55 local rootfs_size="$(ubinfo /dev/ubi0 -N rootfs_data | grep "Size" | awk '{ print $6 }')"
56
57 # Setup partitions using board name, in case of future platforms
58 case "$board_name" in
59 "mr18"|\
60 "z1")
61 # Src is MTD
62 mtd_src="$(find_mtd_chardev $cal_src)"
63 [ -n "$mtd_src" ] || {
64 echo "no mtd device found for partition $cal_src"
65 exit 1
66 }
67
68 # Dest is UBI
69 # TODO: possibly add create (hard to do when rootfs_data is expanded & mounted)
70 # Would need to be done from ramdisk
71 mtd_dst="$(nand_find_volume $ubidev $cal_dst)"
72 [ -n "$mtd_dst" ] || {
73 echo "no ubi device found for partition $cal_dst"
74 exit 1
75 }
76
77 meraki_is_caldata_valid "$board_name" "$mtd_src" && {
78 echo "no valid calibration data found in $cal_src"
79 exit 1
80 }
81
82 meraki_is_caldata_valid "$board_name" "/dev/$mtd_dst" && {
83 echo "Copying calibration data from $cal_src to $cal_dst..."
84 dd if="$mtd_src" of=/tmp/caldata.tmp 2>/dev/null
85 ubiupdatevol "/dev/$mtd_dst" /tmp/caldata.tmp
86 rm /tmp/caldata.tmp
87 sync
88 }
89 return 0
90 ;;
91 *)
92 echo "Unsupported device $board_name";
93 return 1
94 ;;
95 esac
96 }
97
98 merakinand_do_kernel_check() {
99 local board_name="$1"
100 local tar_file="$2"
101 local image_magic_word=`(tar xf $tar_file sysupgrade-$board_name/kernel -O 2>/dev/null | dd bs=1 count=4 skip=0 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"')`
102
103 # What is our kernel magic string?
104 case "$board_name" in
105 "mr18")
106 [ "$image_magic_word" == "8e73ed8a" ] && {
107 echo "pass" && return 0
108 }
109 ;;
110 "z1")
111 [ "$image_magic_word" == "4d495053" ] && {
112 echo "pass" && return 0
113 }
114 ;;
115 esac
116
117 exit 1
118 }
119
120 merakinand_do_platform_check() {
121 local board_name="$1"
122 local tar_file="$2"
123 local control_length=`(tar xf $tar_file sysupgrade-$board_name/CONTROL -O | wc -c) 2> /dev/null`
124 local file_type="$(identify_tar $2 sysupgrade-$board_name/root)"
125 local kernel_magic="$(merakinand_do_kernel_check $1 $2)"
126
127 case "$board_name" in
128 "mr18"|\
129 "z1")
130 [ "$control_length" = 0 -o "$file_type" != "squashfs" -o "$kernel_magic" != "pass" ] && {
131 echo "Invalid sysupgrade file for $board_name"
132 return 1
133 }
134 ;;
135 *)
136 echo "Unsupported device $board_name";
137 return 1
138 ;;
139 esac
140
141 return 0
142 }
143
144 merakinand_do_upgrade() {
145 local tar_file="$1"
146 local board_name="$(board_name)"
147
148 # Do we need to do any platform tweaks?
149 case "$board_name" in
150 "mr18")
151 # Check and create UBI caldata if it's invalid
152 merakinand_copy_caldata "odm-caldata" "caldata"
153 nand_do_upgrade $1
154 ;;
155 "z1")
156 # Check and create UBI caldata if it's invalid
157 merakinand_copy_caldata "origcaldata" "caldata"
158 nand_do_upgrade $1
159 ;;
160 *)
161 echo "Unsupported device $board_name";
162 exit 1
163 ;;
164 esac
165 }