target/linux/*/base-files/lib/upgrade/platform.sh - wrong check for ARGC
[lede.git] / target / linux / ixp4xx / base-files / lib / upgrade / platform.sh
1 . /lib/ixp4xx.sh
2
3 RAMFS_COPY_DATA="/lib/ixp4xx.sh"
4
5 CI_BLKSZ=65536
6 CI_LDADR=0x00800000
7
8 platform_find_partitions() {
9         local first dev size erasesize name
10         while read dev size erasesize name; do
11                 name=${name#'"'}; name=${name%'"'}
12                 case "$name" in
13                         vmlinux.bin.l7|kernel|linux|rootfs)
14                                 if [ -z "$first" ]; then
15                                         first="$name"
16                                 else
17                                         echo "$erasesize:$first:$name"
18                                         break
19                                 fi
20                         ;;
21                 esac
22         done < /proc/mtd
23 }
24
25 platform_find_kernelpart() {
26         local part
27         for part in "${1%:*}" "${1#*:}"; do
28                 case "$part" in
29                         vmlinux.bin.l7|kernel|linux)
30                                 echo "$part"
31                                 break
32                         ;;
33                 esac
34         done
35 }
36
37 platform_find_part_size() {
38         local first dev size erasesize name
39         while read dev size erasesize name; do
40                 name=${name#'"'}; name=${name%'"'}
41                 [ "$name" = "$1" ] && {
42                         echo "$size"
43                         break
44                 }
45         done < /proc/mtd
46 }
47
48 platform_do_upgrade_combined() {
49         local partitions=$(platform_find_partitions)
50         local kernelpart=$(platform_find_kernelpart "${partitions#*:}")
51         local erase_size=$((0x${partitions%%:*})); partitions="${partitions#*:}"
52         local kern_part_size=0x$(platform_find_part_size "$kernelpart")
53         local kern_part_blocks=$(($kern_part_size / $CI_BLKSZ))
54         local kern_length=0x$(dd if="$1" bs=2 skip=1 count=4 2>/dev/null)
55         local kern_blocks=$(($kern_length / $CI_BLKSZ))
56         local root_blocks=$((0x$(dd if="$1" bs=2 skip=5 count=4 2>/dev/null) / $CI_BLKSZ))
57
58         v "platform_do_upgrade_combined"
59         v "partitions=$partitions"
60         v "kernelpart=$kernelpart"
61         v "kernel_part_size=$kern_part_size"
62         v "kernel_part_blocks=$kern_part_blocks"
63         v "kern_length=$kern_length"
64         v "erase_size=$erase_size"
65         v "kern_blocks=$kern_blocks"
66         v "root_blocks=$root_blocks"
67         v "kern_pad_blocks=$(($kern_part_blocks-$kern_blocks))"
68
69         if [ -n "$partitions" ] && [ -n "$kernelpart" ] && \
70            [ ${kern_blocks:-0} -gt 0 ] && \
71            [ ${root_blocks:-0} -gt 0 ] && \
72            [ ${erase_size:-0} -gt 0 ];
73         then
74                 local append=""
75                 [ -f "$CONF_TAR" -a "$SAVE_CONFIG" -eq 1 ] && append="-j $CONF_TAR"
76
77                 # write the kernel
78                 dd if="$1" bs=$CI_BLKSZ skip=1 count=$kern_blocks 2>/dev/null | \
79                         mtd -F$kernelpart:$kern_part_size:$CI_LDADR write - $kernelpart
80                 # write the rootfs
81                 dd if="$1" bs=$CI_BLKSZ skip=$((1+$kern_blocks)) count=$root_blocks 2>/dev/null | \
82                         mtd $append write - rootfs
83         else
84                 echo "invalid image"
85         fi
86 }
87
88 platform_check_image() {
89         local board=$(ixp4xx_board_name)
90         local magic="$(get_magic_word "$1")"
91         local partitions=$(platform_find_partitions)
92         local kernelpart=$(platform_find_kernelpart "${partitions#*:}")
93         local kern_part_size=0x$(platform_find_part_size "$kernelpart")
94         local kern_length=0x$(dd if="$1" bs=2 skip=1 count=4 2>/dev/null)
95
96         [ "$#" -gt 1 ] && return 1
97
98         case "$board" in
99         avila | cambria )
100                 [ "$magic" != "4349" ] && {
101                         echo "Invalid image. Use *-sysupgrade.bin files on this board"
102                         return 1
103                 }
104
105                 kern_length_b=$(printf '%d' $kern_length)
106                 kern_part_size_b=$(printf '%d' $kern_part_size)
107                 if [ $kern_length_b -gt $kern_part_size_b ]; then
108                         echo "Invalid image. Kernel size ($kern_length) exceeds kernel partition ($kern_part_size)"
109                         return 1
110                 fi
111
112                 local md5_img=$(dd if="$1" bs=2 skip=9 count=16 2>/dev/null)
113                 local md5_chk=$(dd if="$1" bs=$CI_BLKSZ skip=1 2>/dev/null | md5sum -); md5_chk="${md5_chk%% *}"
114                 if [ -n "$md5_img" -a -n "$md5_chk" ] && [ "$md5_img" = "$md5_chk" ]; then
115                         return 0
116                 else
117                         echo "Invalid image. Contents do not match checksum (image:$md5_img calculated:$md5_chk)"
118                         return 1
119                 fi
120
121                 return 0
122                 ;;
123         esac
124
125         echo "Sysupgrade is not yet supported on $board."
126         return 1
127 }
128
129 platform_do_upgrade() {
130         local board=$(ixp4xx_board_name)
131
132         v "board=$board"
133         case "$board" in
134         avila | cambria )
135                 platform_do_upgrade_combined "$ARGV"
136                 ;;
137         *)
138                 default_do_upgrade "$ARGV"
139                 ;;
140         esac
141 }
142
143 disable_watchdog() {
144         v "killing watchdog"
145         killall watchdog
146         ( ps | grep -v 'grep' | grep '/dev/watchdog' ) && {
147                 echo 'Could not disable watchdog'
148                 return 1
149         }
150 }
151
152 # CONFIG_WATCHDOG_NOWAYOUT=y - can't kill watchdog unless kernel cmdline has a mpcore_wdt.nowayout=0
153 #append sysupgrade_pre_upgrade disable_watchdog