ipq40xx: add support for Sony NCP-HG100/Cellular
[openwrt/staging/hauke.git] / target / linux / ipq40xx / base-files / lib / upgrade / sony.sh
1 . /lib/functions.sh
2
3 update_bootconfig() {
4 local offset=$1
5 local index="$2"
6 local cfgpart=$(find_mmc_part "0:BOOTCONFIG")
7 local cur_index
8
9 if [ -z "$cfgpart" ]; then
10 echo "failed to get the partition: \"0:BOOTCONFIG\""
11 return 1
12 fi
13
14 cur_index=$(dd if=${cfgpart} bs=1 count=1 skip=$offset 2> /dev/null | hexdump -e '"%d"')
15 if [ ${index} != ${cur_index} ]; then
16 echo "updating \"0:BOOTCONFIG\""
17 echo -en "\x0${index}" | \
18 dd of=${cfgpart} bs=1 count=1 seek=$offset conv=notrunc 2>/dev/null
19 fi
20
21 # also update 0:BOOTCONFIG1 if exists
22 cfgpart=$(find_mmc_part "0:BOOTCONFIG1")
23 [ -z "$cfgpart" ] && return
24
25 cur_index=$(dd if=${cfgpart} bs=1 count=1 skip=$offset 2> /dev/null | hexdump -e '"%d"')
26 if [ ${index} != ${cur_index} ]; then
27 echo "updating \"0:BOOTCONFIG1\""
28 echo -en "\x0${index}" | \
29 dd of=${cfgpart} bs=1 count=1 seek=$offset conv=notrunc 2>/dev/null
30 fi
31 }
32
33 ### Note ###
34 # After the commit bad1835f27ec31dbc30060b03cc714212275168a in fstools,
35 # p17 (label: "rootfs_data") is mounted as a rootfs_data on boot instead
36 # of the loop device labeled as "rootfs_data" in p15 (label: "rootfs").
37 #
38 # cmdline flag is added to avoid mount "rootfs_data" partition by the
39 # commit 964d1e3af0e111bad6d393f8a3be702e334c2398 in fstools, but
40 # NCP-HG100 doesn't use it because it has a large (abount 1.6GB)
41 # "rootfs_data" partition and the advantage is larger than the
42 # disadvantages, such as overwriting the stock data in "rootfs_data"
43 # partition.
44 sony_emmc_do_upgrade() {
45 local tar_file=$1
46 local kernel_dev
47 local rootfs_dev
48 local board_dir
49
50 kernel_dev=$(find_mmc_part "0:HLOS")
51 rootfs_dev=$(find_mmc_part "rootfs")
52 rootfs_data_dev=$(find_mmc_part "rootfs_data")
53
54 if [ -z "$kernel_dev" ] || [ -z "$rootfs_dev" ] || [ -z "$rootfs_data_dev" ]; then
55 echo "The partition name for kernel or rootfs or rootfs_data is not specified or failed to get the mmc device."
56 exit 1
57 fi
58
59 # use first partitions of kernel/rootfs for NCP-HG100
60 # - offset 88 (0x58): 0:HLOS (kernel)
61 # - offset 108 (0x6c): rootfs
62 update_bootconfig 88 0 || exit 1
63 update_bootconfig 108 0 || exit 1
64
65 board_dir=$(tar tf $tar_file | grep -m 1 '^sysupgrade-.*/$')
66 board_dir=${board_dir%/}
67
68 echo "Flashing kernel to ${kernel_dev}"
69 tar xf $tar_file ${board_dir}/kernel -O > $kernel_dev
70
71 echo "Flashing rootfs to ${rootfs_dev}"
72 tar xf $tar_file ${board_dir}/root -O > $rootfs_dev
73
74 echo "Format new rootfs_data"
75 mkfs.ext4 -F -L rootfs_data $rootfs_data_dev
76
77 if [ -e "$UPGRADE_BACKUP" ]; then
78 mkdir /tmp/new_root
79 mount -t ext4 $rootfs_data_dev /tmp/new_root && {
80 echo "Saving configurations to rootfs_data"
81 cp "$UPGRADE_BACKUP" "/tmp/new_root/$BACKUP_FILE"
82 umount /tmp/new_root
83 }
84 fi
85
86 echo "sysupgrade successful"
87
88 sync
89 umount -a
90 reboot -f
91 }