dac9ddd5686544d50bd9ae806d753728017f892d
[openwrt/staging/stintel.git] / target / linux / ipq807x / base-files / lib / upgrade / mmc.sh
1 #
2 # Copyright (C) 2016 lede-project.org
3 #
4
5 # this can be used as a generic mmc upgrade script
6 # just add a device entry in platform.sh,
7 # define "kernelname" and "rootfsname" and call mmc_do_upgrade
8 # after the kernel and rootfs flash a loopdev (as overlay) is
9 # setup on top of the rootfs partition
10 # for the proper function a padded rootfs image is needed, basically
11 # append "pad-to 64k" to the image definition
12 # this is based on the ipq806x zyxel.sh mmc upgrade
13
14 . /lib/functions.sh
15
16 mmc_do_upgrade() {
17 local tar_file="$1"
18 local rootfs=
19 local kernel=
20
21 [ -z "$kernel" ] && kernel=$(find_mmc_part ${kernelname})
22 [ -z "$rootfs" ] && rootfs=$(find_mmc_part ${rootfsname})
23
24 [ -z "$kernel" ] && echo "Upgrade failed: kernel partition not found! Rebooting..." && reboot -f
25 [ -z "$rootfs" ] && echo "Upgrade failed: rootfs partition not found! Rebooting..." && reboot -f
26
27 mmc_do_flash $tar_file $kernel $rootfs
28
29 return 0
30 }
31
32 mmc_do_flash() {
33 local tar_file=$1
34 local kernel=$2
35 local rootfs=$3
36
37 # keep sure its unbound
38 losetup --detach-all || {
39 echo Failed to detach all loop devices. Skip this try.
40 reboot -f
41 }
42
43 # use the first found directory in the tar archive
44 local board_dir=$(tar tf $tar_file | grep -m 1 '^sysupgrade-.*/$')
45 board_dir=${board_dir%/}
46
47 echo "flashing kernel to $kernel"
48 tar xf $tar_file ${board_dir}/kernel -O >$kernel
49
50 echo "flashing rootfs to ${rootfs}"
51 tar xf $tar_file ${board_dir}/root -O >"${rootfs}"
52
53 # a padded rootfs is needed for overlay fs creation
54 local offset=$(tar xf $tar_file ${board_dir}/root -O | wc -c)
55 [ $offset -lt 65536 ] && {
56 echo Wrong size for rootfs: $offset
57 sleep 10
58 reboot -f
59 }
60
61 # Mount loop for rootfs_data
62 local loopdev="$(losetup -f)"
63 losetup -o $offset $loopdev $rootfs || {
64 echo "Failed to mount looped rootfs_data."
65 sleep 10
66 reboot -f
67 }
68
69 echo "Format new rootfs_data at position ${offset}."
70 mkfs.ext4 -F -L rootfs_data $loopdev
71 mkdir /tmp/new_root
72 mount -t ext4 $loopdev /tmp/new_root && {
73 echo "Saving config to rootfs_data at position ${offset}."
74 cp -v "$UPGRADE_BACKUP" "/tmp/new_root/$BACKUP_FILE"
75 umount /tmp/new_root
76 }
77
78 # Cleanup
79 losetup -d $loopdev >/dev/null 2>&1
80 sync
81 umount -a
82 reboot -f
83 }