target/linux/*/base-files/lib/upgrade/platform.sh - wrong check for ARGC
[openwrt.git] / target / linux / atheros / base-files / lib / upgrade / platform.sh
1 CI_BLKSZ=65536
2 CI_LDADR=0x80041000
3
4 platform_find_partitions() {
5         local first dev size erasesize name
6         while read dev size erasesize name; do
7                 name=${name#'"'}; name=${name%'"'}
8                 case "$name" in
9                         vmlinux.bin.l7|kernel|linux|rootfs)
10                                 if [ -z "$first" ]; then
11                                         first="$name"
12                                 else
13                                         echo "$erasesize:$first:$name"
14                                         break
15                                 fi
16                         ;;
17                 esac
18         done < /proc/mtd
19 }
20
21 platform_find_kernelpart() {
22         local part
23         for part in "${1%:*}" "${1#*:}"; do
24                 case "$part" in
25                         vmlinux.bin.l7|kernel|linux)
26                                 echo "$part"
27                                 break
28                         ;;
29                 esac
30         done
31 }
32
33 platform_check_image() {
34         [ "$#" -gt 1 ] && return 1
35
36         case "$(get_magic_word "$1")" in
37                 # Combined Image
38                 4349)
39                         local md5_img=$(dd if="$1" bs=2 skip=9 count=16 2>/dev/null)
40                         local md5_chk=$(dd if="$1" bs=$CI_BLKSZ skip=1 2>/dev/null | md5sum -); md5_chk="${md5_chk%% *}"
41
42                         if [ -n "$md5_img" -a -n "$md5_chk" ] && [ "$md5_img" = "$md5_chk" ]; then
43                                 return 0
44                         else
45                                 echo "Invalid image. Contents do not match checksum (image:$md5_img calculated:$md5_chk)"
46                                 return 1
47                         fi
48                 ;;
49                 *)
50                         echo "Invalid image. Use combined .img files on this platform"
51                         return 1
52                 ;;
53         esac
54 }
55
56 platform_do_upgrade() {
57         local partitions=$(platform_find_partitions)
58         local kernelpart=$(platform_find_kernelpart "${partitions#*:}")
59         local erase_size=$((0x${partitions%%:*})); partitions="${partitions#*:}"
60         local kern_length=0x$(dd if="$1" bs=2 skip=1 count=4 2>/dev/null)
61         local kern_blocks=$(($kern_length / $CI_BLKSZ))
62         local root_blocks=$((0x$(dd if="$1" bs=2 skip=5 count=4 2>/dev/null) / $CI_BLKSZ))
63
64         if [ -n "$partitions" ] && [ -n "$kernelpart" ] && \
65            [ ${kern_blocks:-0} -gt 0 ] && \
66            [ ${root_blocks:-0} -gt ${kern_blocks:-0} ] && \
67            [ ${erase_size:-0} -gt 0 ];
68         then
69                 local append=""
70                 [ -f "$CONF_TAR" -a "$SAVE_CONFIG" -eq 1 ] && append="-j $CONF_TAR"
71
72                 ( dd if="$1" bs=$CI_BLKSZ skip=1 count=$kern_blocks 2>/dev/null; \
73                   dd if="$1" bs=$CI_BLKSZ skip=$((1+$kern_blocks)) count=$root_blocks 2>/dev/null ) | \
74                         mtd -r $append -F$kernelpart:$kern_length:$CI_LDADR,rootfs write - $partitions
75         fi
76 }