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