ar71xx: add sysupgrade support for OpenMesh MR600v2
[openwrt/openwrt.git] / target / linux / ar71xx / base-files / lib / upgrade / openmesh.sh
1 # The U-Boot loader of the OpenMesh devices requires image sizes and
2 # checksums to be provided in the U-Boot environment.
3 # The OpenMesh devices come with 2 main partitions - while one is active
4 # sysupgrade will flash the other. The boot order is changed to boot the
5 # newly flashed partition. If the new partition can't be booted due to
6 # upgrade failures the previously used partition is loaded.
7
8 trim()
9 {
10 echo $1
11 }
12
13 cfg_value_get()
14 {
15 local cfg=$1 cfg_opt
16 local section=$2 our_section=0
17 local param=$3 our_param=
18
19 for cfg_opt in $cfg
20 do
21 [ "$cfg_opt" = "[$section]" ] && our_section=1 && continue
22 [ "$our_section" = "1" ] || continue
23
24 our_param=$(echo ${cfg_opt%%=*})
25 [ "$param" = "$our_param" ] && echo ${cfg_opt##*=} && break
26 done
27 }
28
29 # make sure we got uboot-envtools and fw_env.config copied over to the ramfs
30 platform_add_ramfs_ubootenv()
31 {
32 [ -e /usr/sbin/fw_printenv ] && install_bin /usr/sbin/fw_printenv /usr/sbin/fw_setenv
33 [ -e /etc/fw_env.config ] && install_file /etc/fw_env.config
34 }
35 append sysupgrade_pre_upgrade platform_add_ramfs_ubootenv
36
37 platform_check_image_openmesh()
38 {
39 local img_magic=$1
40 local img_path=$2
41 local fw_printenv=/usr/sbin/fw_printenv
42 local img_board_target= img_num_files= i=0
43 local cfg_name= kernel_name= rootfs_name=
44
45 case "$img_magic" in
46 # Combined Extended Image v1
47 43453031)
48 img_board_target=$(trim $(dd if="$img_path" bs=4 skip=1 count=8 2>/dev/null))
49 img_num_files=$(trim $(dd if="$img_path" bs=2 skip=18 count=1 2>/dev/null))
50 ;;
51 *)
52 echo "Invalid image ($img_magic). Use combined extended images on this platform"
53 return 1
54 ;;
55 esac
56
57 case "$img_board_target" in
58 OM2P)
59 [ "$board" = "om2p" ] && break
60 [ "$board" = "om2p-lc" ] && break
61 [ "$board" = "om2p-hs" ] && break
62 echo "Invalid image board target ($img_board_target) for this platform: $board. Use the correct image for this platform"
63 return 1
64 ;;
65 MR600)
66 [ "$board" = "mr600" ] && break
67 [ "$board" = "mr600v2" ] && break
68 echo "Invalid image board target ($img_board_target) for this platform: $board. Use the correct image for this platform"
69 return 1
70 ;;
71 *)
72 echo "Invalid board target ($img_board_target). Use the correct image for this platform"
73 return 1
74 ;;
75 esac
76
77 [ $img_num_files -ne 3 ] && {
78 echo "Invalid number of embedded images ($img_num_files). Use the correct image for this platform"
79 return 1
80 }
81
82 cfg_name=$(trim $(dd if="$img_path" bs=2 skip=19 count=16 2>/dev/null))
83
84 [ "$cfg_name" != "fwupgrade.cfg" ] && {
85 echo "Invalid embedded config file ($cfg_name). Use the correct image for this platform"
86 return 1
87 }
88
89 kernel_name=$(trim $(dd if="$img_path" bs=2 skip=55 count=16 2>/dev/null))
90
91 [ "$kernel_name" != "kernel" ] && {
92 echo "Invalid embedded kernel file ($kernel_name). Use the correct image for this platform"
93 return 1
94 }
95
96 rootfs_name=$(trim $(dd if="$img_path" bs=2 skip=91 count=16 2>/dev/null))
97
98 [ "$rootfs_name" != "rootfs" ] && {
99 echo "Invalid embedded kernel file ($rootfs_name). Use the correct image for this platform"
100 return 1
101 }
102
103 [ ! -x "$fw_printenv" ] && {
104 echo "Please install uboot-envtools!"
105 return 1
106 }
107
108 [ ! -r "/etc/fw_env.config" ] && {
109 echo "/etc/fw_env.config is missing"
110 return 1
111 }
112
113 return 0
114 }
115
116 platform_do_upgrade_openmesh()
117 {
118 local img_path=$1 img_board_target=
119 local kernel_start_addr= kernel_start_addr1= kernel_start_addr2=
120 local kernel_size= kernel_md5=
121 local rootfs_size= rootfs_checksize= rootfs_md5=
122 local kernel_bsize= total_size=7340032
123 local data_offset=$((64 * 1024)) block_size= offset=
124 local uboot_env_upgrade="/tmp/fw_env_upgrade"
125 local cfg_size= kernel_size= rootfs_size=
126 local append=""
127
128 [ -f "$CONF_TAR" -a "$SAVE_CONFIG" -eq 1 ] && append="-j $CONF_TAR"
129
130 cfg_size=$(dd if="$img_path" bs=2 skip=35 count=4 2>/dev/null)
131 kernel_size=$(dd if="$img_path" bs=2 skip=71 count=4 2>/dev/null)
132 rootfs_size=$(dd if="$img_path" bs=2 skip=107 count=4 2>/dev/null)
133
134 img_board_target=$(trim $(dd if="$img_path" bs=4 skip=1 count=8 2>/dev/null))
135 cfg_content=$(dd if="$img_path" bs=1 skip=$data_offset count=$(echo $((0x$cfg_size))) 2>/dev/null)
136
137 case $img_board_target in
138 OM2P)
139 block_size=$((256 * 1024))
140 kernel_start_addr1=0x9f1c0000
141 kernel_start_addr2=0x9f8c0000
142 ;;
143 MR600)
144 block_size=$((64 * 1024))
145 kernel_start_addr1=0x9f0b0000
146 kernel_start_addr2=0x9f850000
147 ;;
148 esac
149
150 kernel_md5=$(cfg_value_get "$cfg_content" "vmlinux" "md5sum")
151 rootfs_md5=$(cfg_value_get "$cfg_content" "rootfs" "md5sum")
152 rootfs_checksize=$(cfg_value_get "$cfg_content" "rootfs" "checksize")
153
154 if [ "$((0x$kernel_size % $block_size))" = "0" ]
155 then
156 kernel_bsize=$(echo $((0x$kernel_size)))
157 else
158 kernel_bsize=$((0x$kernel_size + ($block_size - (0x$kernel_size % $block_size))))
159 fi
160
161 mtd -q erase inactive
162
163 offset=$(echo $(($data_offset + 0x$cfg_size + 0x$kernel_size)))
164 dd if="$img_path" bs=1 skip=$offset count=$(echo $((0x$rootfs_size))) 2>&- | mtd -n -p $kernel_bsize $append write - "inactive"
165
166 offset=$(echo $(($data_offset + 0x$cfg_size)))
167 dd if="$img_path" bs=1 skip=$offset count=$(echo $((0x$kernel_size))) 2>&- | mtd -n write - "inactive"
168
169 rm $uboot_env_upgrade 2>&-
170
171 if [ "$(grep 'mtd3:.*inactive' /proc/mtd)" ]
172 then
173 printf "kernel_size_1 %u\n" $(($kernel_bsize / 1024)) >> $uboot_env_upgrade
174 printf "rootfs_size_1 %u\n" $((($total_size - $kernel_bsize) / 1024)) >> $uboot_env_upgrade
175 printf "bootseq 1,2\n" >> $uboot_env_upgrade
176 kernel_start_addr=$kernel_start_addr1
177 else
178 printf "kernel_size_2 %u\n" $(($kernel_bsize / 1024)) >> $uboot_env_upgrade
179 printf "rootfs_size_2 %u\n" $((($total_size - $kernel_bsize) / 1024)) >> $uboot_env_upgrade
180 printf "bootseq 2,1\n" >> $uboot_env_upgrade
181 kernel_start_addr=$kernel_start_addr2
182 fi
183
184 printf "vmlinux_start_addr %s\n" $kernel_start_addr >> $uboot_env_upgrade
185 printf "vmlinux_size 0x%s\n" $kernel_size >> $uboot_env_upgrade
186 printf "vmlinux_checksum %s\n" $kernel_md5 >> $uboot_env_upgrade
187 printf "rootfs_start_addr 0x%x\n" $(($kernel_start_addr + $kernel_bsize)) >> $uboot_env_upgrade
188 printf "rootfs_size %s\n" $rootfs_checksize >> $uboot_env_upgrade
189 printf "rootfs_checksum %s\n" $rootfs_md5 >> $uboot_env_upgrade
190
191 fw_setenv -s $uboot_env_upgrade || {
192 echo "failed to update U-Boot environment"
193 return 1
194 }
195 }