ath79: add new OF only target for QCA MIPS silicon
[oweals/openwrt.git] / target / linux / ath79 / base-files / lib / upgrade / platform.sh
1 #
2 # Copyright (C) 2011 OpenWrt.org
3 #
4
5 . /lib/functions/system.sh
6
7 PART_NAME=firmware
8 RAMFS_COPY_BIN='nandwrite'
9
10 CI_BLKSZ=65536
11 CI_LDADR=0x80060000
12
13 PLATFORM_DO_UPGRADE_COMBINED_SEPARATE_MTD=0
14
15 platform_find_partitions() {
16         local first dev size erasesize name
17         while read dev size erasesize name; do
18                 name=${name#'"'}; name=${name%'"'}
19                 case "$name" in
20                         vmlinux.bin.l7|vmlinux|kernel|linux|linux.bin|rootfs|filesystem)
21                                 if [ -z "$first" ]; then
22                                         first="$name"
23                                 else
24                                         echo "$erasesize:$first:$name"
25                                         break
26                                 fi
27                         ;;
28                 esac
29         done < /proc/mtd
30 }
31
32 platform_find_kernelpart() {
33         local part
34         for part in "${1%:*}" "${1#*:}"; do
35                 case "$part" in
36                         vmlinux.bin.l7|vmlinux|kernel|linux|linux.bin)
37                                 echo "$part"
38                                 break
39                         ;;
40                 esac
41         done
42 }
43
44 platform_find_rootfspart() {
45         local part
46         for part in "${1%:*}" "${1#*:}"; do
47                 [ "$part" != "$2" ] && echo "$part" && break
48         done
49 }
50
51 platform_do_upgrade_combined() {
52         local partitions=$(platform_find_partitions)
53         local kernelpart=$(platform_find_kernelpart "${partitions#*:}")
54         local erase_size=$((0x${partitions%%:*})); partitions="${partitions#*:}"
55         local kern_length=0x$(dd if="$1" bs=2 skip=1 count=4 2>/dev/null)
56         local kern_blocks=$(($kern_length / $CI_BLKSZ))
57         local root_blocks=$((0x$(dd if="$1" bs=2 skip=5 count=4 2>/dev/null) / $CI_BLKSZ))
58
59         if [ -n "$partitions" ] && [ -n "$kernelpart" ] && \
60            [ ${kern_blocks:-0} -gt 0 ] && \
61            [ ${root_blocks:-0} -gt 0 ] && \
62            [ ${erase_size:-0} -gt 0 ];
63         then
64                 local rootfspart=$(platform_find_rootfspart "$partitions" "$kernelpart")
65                 local append=""
66                 [ -f "$CONF_TAR" -a "$SAVE_CONFIG" -eq 1 ] && append="-j $CONF_TAR"
67
68                 if [ "$PLATFORM_DO_UPGRADE_COMBINED_SEPARATE_MTD" -ne 1 ]; then
69                     ( dd if="$1" bs=$CI_BLKSZ skip=1 count=$kern_blocks 2>/dev/null; \
70                       dd if="$1" bs=$CI_BLKSZ skip=$((1+$kern_blocks)) count=$root_blocks 2>/dev/null ) | \
71                             mtd -r $append -F$kernelpart:$kern_length:$CI_LDADR,rootfs write - $partitions
72                 elif [ -n "$rootfspart" ]; then
73                     dd if="$1" bs=$CI_BLKSZ skip=1 count=$kern_blocks 2>/dev/null | \
74                             mtd write - $kernelpart
75                     dd if="$1" bs=$CI_BLKSZ skip=$((1+$kern_blocks)) count=$root_blocks 2>/dev/null | \
76                             mtd -r $append write - $rootfspart
77                 fi
78         fi
79         PLATFORM_DO_UPGRADE_COMBINED_SEPARATE_MTD=0
80 }
81
82 tplink_get_image_hwid() {
83         get_image "$@" | dd bs=4 count=1 skip=16 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
84 }
85
86 tplink_get_image_mid() {
87         get_image "$@" | dd bs=4 count=1 skip=17 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
88 }
89
90 tplink_get_image_boot_size() {
91         get_image "$@" | dd bs=4 count=1 skip=37 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
92 }
93
94 tplink_pharos_check_image() {
95         local magic_long="$(get_magic_long "$1")"
96         [ "$magic_long" != "7f454c46" ] && {
97                 echo "Invalid image magic '$magic_long'"
98                 return 1
99         }
100
101         local model_string="$(tplink_pharos_get_model_string)"
102         local line
103
104         # Here $1 is given to dd directly instead of get_image as otherwise the skip
105         # will take almost a second (as dd can't seek then)
106         #
107         # This will fail if the image isn't local, but that's fine: as the
108         # read loop won't be executed at all, it will return true, so the image
109         # is accepted (loading the first 1.5M of a remote image for this check seems
110         # a bit extreme)
111         dd if="$1" bs=1 skip=1511432 count=1024 2>/dev/null | while read line; do
112                 [ "$line" = "$model_string" ] && break
113         done || {
114                 echo "Unsupported image (model not in support-list)"
115                 return 1
116         }
117
118         return 0
119 }
120
121 seama_get_type_magic() {
122         get_image "$@" | dd bs=1 count=4 skip=53 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
123 }
124
125 wrgg_get_image_magic() {
126         get_image "$@" | dd bs=4 count=1 skip=8 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
127 }
128
129 cybertan_get_image_magic() {
130         get_image "$@" | dd bs=8 count=1 skip=0  2>/dev/null | hexdump -v -n 8 -e '1/1 "%02x"'
131 }
132
133 cybertan_check_image() {
134         local magic="$(cybertan_get_image_magic "$1")"
135         local fw_magic="$(cybertan_get_hw_magic)"
136
137         [ "$fw_magic" != "$magic" ] && {
138                 echo "Invalid image, ID mismatch, got:$magic, but need:$fw_magic"
139                 return 1
140         }
141
142         return 0
143 }
144
145 platform_do_upgrade_compex() {
146         local fw_file=$1
147         local fw_part=$PART_NAME
148         local fw_mtd=$(find_mtd_part $fw_part)
149         local fw_length=0x$(dd if="$fw_file" bs=2 skip=1 count=4 2>/dev/null)
150         local fw_blocks=$(($fw_length / 65536))
151
152         if [ -n "$fw_mtd" ] &&  [ ${fw_blocks:-0} -gt 0 ]; then
153                 local append=""
154                 [ -f "$CONF_TAR" -a "$SAVE_CONFIG" -eq 1 ] && append="-j $CONF_TAR"
155
156                 sync
157                 dd if="$fw_file" bs=64k skip=1 count=$fw_blocks 2>/dev/null | \
158                         mtd $append write - "$fw_part"
159         fi
160 }
161
162 alfa_check_image() {
163         local magic_long="$(get_magic_long "$1")"
164         local fw_part_size=$(mtd_get_part_size firmware)
165
166         case "$magic_long" in
167         "27051956")
168                 [ "$fw_part_size" != "16318464" ] && {
169                         echo "Invalid image magic \"$magic_long\" for $fw_part_size bytes"
170                         return 1
171                 }
172                 ;;
173         "68737173")
174                 [ "$fw_part_size" != "7929856" ] && {
175                         echo "Invalid image magic \"$magic_long\" for $fw_part_size bytes"
176                         return 1
177                 }
178                 ;;
179         esac
180
181         return 0
182 }
183
184 platform_check_image() {
185         local board=$(board_name)
186         local magic="$(get_magic_word "$1")"
187         local magic_long="$(get_magic_long "$1")"
188
189         [ "$#" -gt 1 ] && return 1
190
191         case "$board" in
192         "ubnt,unifi")
193                 [ "$magic" != "2705" ] && {
194                         echo "Invalid image type."
195                         return 1
196                 }
197
198                 return 0
199                 ;;
200         esac
201
202         echo "Sysupgrade is not yet supported on $board."
203         return 1
204 }
205
206 platform_do_upgrade() {
207         local board=$(board_name)
208
209         case "$board" in
210         *)
211                 default_do_upgrade "$ARGV"
212                 ;;
213         esac
214 }
215
216 disable_watchdog() {
217         killall watchdog
218         ( ps | grep -v 'grep' | grep '/dev/watchdog' ) && {
219                 echo 'Could not disable watchdog'
220                 return 1
221         }
222 }
223
224 append sysupgrade_pre_upgrade disable_watchdog