contrib/package: extend sysupgrade support to ar71xx target (untested)
[project/luci.git] / contrib / package / sysupgrade-atheros / files / lib / upgrade / platform.sh
1 platform_check_image() {
2         [ "$ARGC" -gt 1 ] && return 1
3
4         case "$(get_magic_word "$1")" in
5                 # Freifunk .img files
6                 4646)
7                         local kern_name=$(dd if="$1" bs=2 skip=5 count=8 2>/dev/null); kern_name="${kern_name%% *}"
8                         local root_name=$(dd if="$1" bs=2 skip=17 count=8 2>/dev/null); root_name="${root_name%% *}"
9
10                         if grep -q '"'$kern_name'"' /proc/mtd && grep -q '"'$root_name'"' /proc/mtd; then
11                                 return 0
12                         else
13                                 echo "Invalid image. Missing the '$kern_name' or '$root_name' partition"
14                                 return 1
15                         fi
16                 ;;
17                 *)
18                         echo "Invalid image. Use combined .img files on this platform"
19                         return 1
20                 ;;
21         esac
22 }
23
24 platform_do_upgrade() {
25         local kern_length=$((0x$(dd if="$1" bs=2 skip=1 count=4 2>/dev/null)/65536))
26         local kern_name=$(dd if="$1" bs=2 skip=5 count=8 2>/dev/null); kern_name="${kern_name%% *}"
27         local root_length=$((0x$(dd if="$1" bs=2 skip=13 count=4 2>/dev/null)/65536))
28         local root_name=$(dd if="$1" bs=2 skip=17 count=8 2>/dev/null); root_name="${root_name%% *}"
29
30         if grep -q '"'$kern_name'"' /proc/mtd && grep -q '"'$root_name'"' /proc/mtd; then
31                 local append=""
32                 [ -f "$CONF_TAR" -a "$SAVE_CONFIG" -eq 1 ] && append="-j $CONF_TAR"
33
34                 if [ -n "$kern_name" -a -n "$root_name" ] && \
35                    [ ${kern_length:-0} -gt 0 -a ${root_length:-0} -gt ${kern_length:-0} ];
36                 then
37                         dd if="$1" bs=65536 skip=1 count=$kern_length 2>/dev/null | \
38                                 mtd -e $kern_name write - $kern_name
39
40                         dd if="$1" bs=65536 skip=$((1+$kern_length)) count=$root_length 2>/dev/null | \
41                                 mtd -e $root_name $append write - $root_name
42                 fi
43         fi
44 }