imx: make sdcard/eMMC sysupgrade more resilient
[openwrt/openwrt.git] / target / linux / imx / base-files / lib / imx.sh
1 #
2 # Copyright (C) 2010-2013 OpenWrt.org
3 #
4
5 rootpartuuid() {
6 local cmdline=$(cat /proc/cmdline)
7 local bootpart=${cmdline##*root=}
8 bootpart=${bootpart%% *}
9 local uuid=${bootpart#PARTUUID=}
10 echo ${uuid%-02}
11 }
12
13 bootdev_from_uuid() {
14 blkid | grep "PTUUID=\"$(rootpartuuid)\"" | cut -d : -f1
15 }
16
17 bootpart_from_uuid() {
18 blkid | grep $(rootpartuuid)-01 | cut -d : -f1
19 }
20
21 rootpart_from_uuid() {
22 blkid | grep $(rootpartuuid)-02 | cut -d : -f1
23 }
24
25 imx_sdcard_mount_boot() {
26 mkdir -p /boot
27 [ -f /boot/uImage ] || {
28 mount -o rw,noatime $(bootpart_from_uuid) /boot > /dev/null
29 }
30 }
31
32 imx_sdcard_copy_config() {
33 imx_sdcard_mount_boot
34 cp -af "$UPGRADE_BACKUP" "/boot/$BACKUP_FILE"
35 sync
36 umount /boot
37 }
38
39 imx_sdcard_do_upgrade() {
40 local board_dir="$(tar tf "${1}" | grep -m 1 '^sysupgrade-.*/$')"
41 board_dir="${board_dir%/}"
42
43 imx_sdcard_mount_boot
44 get_image "$1" | tar Oxf - ${board_dir}/kernel > /boot/uImage-new && \
45 mv /boot/uImage-new /boot/uImage && \
46 sync && \
47 get_image "$1" | tar Oxf - ${board_dir}/root > $(rootpart_from_uuid) && \
48 sync
49
50 umount /boot
51 }
52
53 imx_sdcard_pre_upgrade() {
54 [ -z "$UPGRADE_BACKUP" ] && {
55 jffs2reset -y
56 umount /overlay
57 }
58 }
59
60 imx_sdcard_move_config() {
61 if [ -b $(bootpart_from_uuid) ]; then
62 imx_sdcard_mount_boot
63 [ -f "/boot/$BACKUP_FILE" ] && mv -f "/boot/$BACKUP_FILE" /
64 umount /boot
65 fi
66 }