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