ar71xx: enable sysupgrade for the OpenMesh A60
[oweals/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 # create /var/lock for the lock "fw_setenv.lock" of fw_setenv
31 platform_add_ramfs_ubootenv()
32 {
33         [ -e /usr/sbin/fw_printenv ] && install_bin /usr/sbin/fw_printenv /usr/sbin/fw_setenv
34         [ -e /etc/fw_env.config ] && install_file /etc/fw_env.config
35         mkdir -p $RAM_ROOT/var/lock
36 }
37 append sysupgrade_pre_upgrade platform_add_ramfs_ubootenv
38
39 platform_check_image_target_openmesh()
40 {
41         img_board_target="$1"
42
43         case "$img_board_target" in
44                 A60)
45                         [ "$board" = "a60" ] && return 0
46                         echo "Invalid image board target ($img_board_target) for this platform: $board. Use the correct image for this platform"
47                         return 1
48                         ;;
49                 OM2P)
50                         [ "$board" = "om2p" ] && return 0
51                         [ "$board" = "om2pv2" ] && return 0
52                         [ "$board" = "om2pv4" ] && return 0
53                         [ "$board" = "om2p-lc" ] && return 0
54                         [ "$board" = "om2p-hs" ] && return 0
55                         [ "$board" = "om2p-hsv2" ] && return 0
56                         [ "$board" = "om2p-hsv3" ] && return 0
57                         [ "$board" = "om2p-hsv4" ] && return 0
58                         echo "Invalid image board target ($img_board_target) for this platform: $board. Use the correct image for this platform"
59                         return 1
60                         ;;
61                 OM5P)
62                         [ "$board" = "om5p" ] && return 0
63                         [ "$board" = "om5p-an" ] && return 0
64                         echo "Invalid image board target ($img_board_target) for this platform: $board. Use the correct image for this platform"
65                         return 1
66                         ;;
67                 OM5PAC)
68                         [ "$board" = "om5p-ac" ] && return 0
69                         [ "$board" = "om5p-acv2" ] && return 0
70                         echo "Invalid image board target ($img_board_target) for this platform: $board. Use the correct image for this platform"
71                         return 1
72                         ;;
73                 MR1750)
74                         [ "$board" = "mr1750" ] && return 0
75                         [ "$board" = "mr1750v2" ] && return 0
76                         echo "Invalid image board target ($img_board_target) for this platform: $board. Use the correct image for this platform"
77                         return 1
78                         ;;
79                 MR600)
80                         [ "$board" = "mr600" ] && return 0
81                         [ "$board" = "mr600v2" ] && return 0
82                         echo "Invalid image board target ($img_board_target) for this platform: $board. Use the correct image for this platform"
83                         return 1
84                         ;;
85                 MR900)
86                         [ "$board" = "mr900" ] && return 0
87                         [ "$board" = "mr900v2" ] && return 0
88                         echo "Invalid image board target ($img_board_target) for this platform: $board. Use the correct image for this platform"
89                         return 1
90                         ;;
91                 *)
92                         echo "Invalid board target ($img_board_target). Use the correct image for this platform"
93                         return 1
94                         ;;
95         esac
96 }
97
98 platform_check_image_openmesh()
99 {
100         local img_magic=$1
101         local img_path=$2
102         local fw_printenv=/usr/sbin/fw_printenv
103         local img_board_target= img_num_files= i=0
104         local cfg_name= kernel_name= rootfs_name=
105
106         case "$img_magic" in
107                 # Combined Extended Image v1
108                 43453031)
109                         img_board_target=$(trim $(dd if="$img_path" bs=4 skip=1 count=8 2>/dev/null))
110                         img_num_files=$(trim $(dd if="$img_path" bs=2 skip=18 count=1 2>/dev/null))
111                         ;;
112                 *)
113                         echo "Invalid image ($img_magic). Use combined extended images on this platform"
114                         return 1
115                         ;;
116         esac
117
118         platform_check_image_target_openmesh "$img_board_target" || return 1
119
120         [ $img_num_files -lt 3 ] && {
121                 echo "Invalid number of embedded images ($img_num_files). Use the correct image for this platform"
122                 return 1
123         }
124
125         cfg_name=$(trim $(dd if="$img_path" bs=2 skip=19 count=16 2>/dev/null))
126
127         [ "$cfg_name" != "fwupgrade.cfg" ] && {
128                 echo "Invalid embedded config file ($cfg_name). Use the correct image for this platform"
129                 return 1
130         }
131
132         kernel_name=$(trim $(dd if="$img_path" bs=2 skip=55 count=16 2>/dev/null))
133
134         [ "$kernel_name" != "kernel" ] && {
135                 echo "Invalid embedded kernel file ($kernel_name). Use the correct image for this platform"
136                 return 1
137         }
138
139         rootfs_name=$(trim $(dd if="$img_path" bs=2 skip=91 count=16 2>/dev/null))
140
141         [ "$rootfs_name" != "rootfs" ] && {
142                 echo "Invalid embedded kernel file ($rootfs_name). Use the correct image for this platform"
143                 return 1
144         }
145
146         [ ! -x "$fw_printenv" ] && {
147                 echo "Please install uboot-envtools!"
148                 return 1
149         }
150
151         [ ! -r "/etc/fw_env.config" ] && {
152                 echo "/etc/fw_env.config is missing"
153                 return 1
154         }
155
156         return 0
157 }
158
159 platform_do_upgrade_openmesh()
160 {
161         local img_path=$1 img_board_target=
162         local kernel_start_addr= kernel_start_addr1= kernel_start_addr2=
163         local kernel_size= kernel_md5=
164         local rootfs_size= rootfs_checksize= rootfs_md5=
165         local kernel_bsize= total_size=
166         local data_offset=$((64 * 1024)) block_size= offset=
167         local uboot_env_upgrade="/tmp/fw_env_upgrade"
168         local cfg_size= kernel_size= rootfs_size=
169         local append=""
170
171         [ -f "$CONF_TAR" -a "$SAVE_CONFIG" -eq 1 ] && append="-j $CONF_TAR"
172
173         cfg_size=$(dd if="$img_path" bs=2 skip=35 count=4 2>/dev/null)
174         kernel_size=$(dd if="$img_path" bs=2 skip=71 count=4 2>/dev/null)
175         rootfs_size=$(dd if="$img_path" bs=2 skip=107 count=4 2>/dev/null)
176
177         img_board_target=$(trim $(dd if="$img_path" bs=4 skip=1 count=8 2>/dev/null))
178         cfg_content=$(dd if="$img_path" bs=1 skip=$data_offset count=$(echo $((0x$cfg_size))) 2>/dev/null)
179
180         case $img_board_target in
181                 OM2P)
182                         block_size=$((256 * 1024))
183                         total_size=7340032
184                         kernel_start_addr1=0x9f1c0000
185                         kernel_start_addr2=0x9f8c0000
186                         ;;
187                 OM5P|OM5PAC|MR600|MR900|MR1750|A60)
188                         block_size=$((64 * 1024))
189                         total_size=7995392
190                         kernel_start_addr1=0x9f0b0000
191                         kernel_start_addr2=0x9f850000
192                         ;;
193         esac
194
195         kernel_md5=$(cfg_value_get "$cfg_content" "vmlinux" "md5sum")
196         rootfs_md5=$(cfg_value_get "$cfg_content" "rootfs" "md5sum")
197         rootfs_checksize=$(cfg_value_get "$cfg_content" "rootfs" "checksize")
198
199         if [ "$((0x$kernel_size % $block_size))" = "0" ]
200                 then
201                         kernel_bsize=$(echo $((0x$kernel_size)))
202                 else
203                         kernel_bsize=$((0x$kernel_size + ($block_size - (0x$kernel_size % $block_size))))
204         fi
205
206         mtd -q erase inactive
207
208         offset=$(echo $(($data_offset + 0x$cfg_size + 0x$kernel_size)))
209         dd if="$img_path" bs=1 skip=$offset count=$(echo $((0x$rootfs_size))) 2>&- | mtd -n -p $kernel_bsize $append write - "inactive"
210
211         offset=$(echo $(($data_offset + 0x$cfg_size)))
212         dd if="$img_path" bs=1 skip=$offset count=$(echo $((0x$kernel_size))) 2>&- | mtd -n write - "inactive"
213
214         rm $uboot_env_upgrade 2>&-
215
216         if [ "$(grep 'mtd3:.*inactive' /proc/mtd)" ]
217                 then
218                         printf "kernel_size_1 %u\n" $(($kernel_bsize / 1024)) >> $uboot_env_upgrade
219                         printf "rootfs_size_1 %u\n" $((($total_size - $kernel_bsize) / 1024)) >> $uboot_env_upgrade
220                         printf "bootseq 1,2\n" >> $uboot_env_upgrade
221                         kernel_start_addr=$kernel_start_addr1
222                 else
223                         printf "kernel_size_2 %u\n" $(($kernel_bsize / 1024)) >> $uboot_env_upgrade
224                         printf "rootfs_size_2 %u\n" $((($total_size - $kernel_bsize) / 1024)) >> $uboot_env_upgrade
225                         printf "bootseq 2,1\n" >> $uboot_env_upgrade
226                         kernel_start_addr=$kernel_start_addr2
227         fi
228
229         printf "vmlinux_start_addr %s\n" $kernel_start_addr >> $uboot_env_upgrade
230         printf "vmlinux_size 0x%s\n" $kernel_size >> $uboot_env_upgrade
231         printf "vmlinux_checksum %s\n" $kernel_md5 >> $uboot_env_upgrade
232         printf "rootfs_start_addr 0x%x\n" $(($kernel_start_addr + $kernel_bsize)) >> $uboot_env_upgrade
233         printf "rootfs_size %s\n" $rootfs_checksize >> $uboot_env_upgrade
234         printf "rootfs_checksum %s\n" $rootfs_md5 >> $uboot_env_upgrade
235
236         fw_setenv -s $uboot_env_upgrade || {
237                 echo "failed to update U-Boot environment"
238                 return 1
239         }
240 }