ath79: Add support for Plasma Cloud PA300E
[openwrt/openwrt.git] / target / linux / ath79 / generic / base-files / lib / upgrade / dualboot_datachk.sh
1 # The U-Boot loader with the datachk patchset for dualbooting requires image
2 # sizes and checksums to be provided in the U-Boot environment.
3 # The 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 platform_do_upgrade_dualboot_datachk() {
9 local tar_file="$1"
10 local restore_backup
11 local primary_kernel_mtd
12
13 local setenv_script="/tmp/fw_env_upgrade"
14
15 local inactive_mtd="$(find_mtd_index $PART_NAME)"
16 local inactive_offset="$(cat /sys/class/mtd/mtd${inactive_mtd}/offset)"
17 local total_size="$(cat /sys/class/mtd/mtd${inactive_mtd}/size)"
18 local flash_start_mem=0x9f000000
19
20 # detect to which flash region the new image is written to.
21 #
22 # 1. check what is the mtd index for the first flash region on this
23 # device
24 # 2. check if the target partition ("inactive") has the mtd index of
25 # the first flash region
26 #
27 # - when it is: the new bootseq will be 1,2 and the first region is
28 # modified
29 # - when it isnt: bootseq will be 2,1 and the second region is
30 # modified
31 #
32 # The detection has to be done via the hardcoded mtd partition because
33 # the current boot might be done with the fallback region. Let us
34 # assume that the current bootseq is 1,2. The bootloader detected that
35 # the image in flash region 1 is corrupt and thus switches to flash
36 # region 2. The bootseq in the u-boot-env is now still the same and
37 # the sysupgrade code can now only rely on the actual mtd indexes and
38 # not the bootseq variable to detect the currently booted flash
39 # region/image.
40 #
41 # In the above example, an implementation which uses bootseq ("1,2") to
42 # detect the currently booted image would assume that region 1 is booted
43 # and then overwrite the variables for the wrong flash region (aka the
44 # one which isn't modified). This could result in a device which doesn't
45 # boot anymore to Linux until it was reflashed with ap51-flash.
46 local next_boot_part="1"
47 case "$(board_name)" in
48 plasmacloud,pa300|\
49 plasmacloud,pa300e)
50 primary_kernel_mtd=3
51 ;;
52 *)
53 echo "failed to detect primary kernel mtd partition for board"
54 return 1
55 ;;
56 esac
57 [ "$inactive_mtd" = "$primary_kernel_mtd" ] || next_boot_part="2"
58
59 local board_dir=$(tar tf $tar_file | grep -m 1 '^sysupgrade-.*/$')
60 board_dir=${board_dir%/}
61
62 local kernel_length=$(tar xf $tar_file ${board_dir}/kernel -O | wc -c)
63 local rootfs_length=$(tar xf $tar_file ${board_dir}/root -O | wc -c)
64 # rootfs without EOF marker
65 rootfs_length=$((rootfs_length-4))
66
67 local kernel_md5=$(tar xf $tar_file ${board_dir}/kernel -O | md5sum); kernel_md5="${kernel_md5%% *}"
68 # md5 checksum of rootfs with EOF marker
69 local rootfs_md5=$(tar xf $tar_file ${board_dir}/root -O | dd bs=1 count=$rootfs_length | md5sum); rootfs_md5="${rootfs_md5%% *}"
70
71 #
72 # add tar support to get_image() to use default_do_upgrade() instead?
73 #
74
75 # take care of restoring a saved config
76 [ -n "$UPGRADE_BACKUP" ] && restore_backup="${MTD_CONFIG_ARGS} -j ${UPGRADE_BACKUP}"
77
78 mtd -q erase inactive
79 tar xf $tar_file ${board_dir}/root -O | mtd -n -p $kernel_length $restore_backup write - $PART_NAME
80 tar xf $tar_file ${board_dir}/kernel -O | mtd -n write - $PART_NAME
81
82 # prepare new u-boot env
83 if [ "$next_boot_part" = "1" ]; then
84 echo "bootseq 1,2" > $setenv_script
85 else
86 echo "bootseq 2,1" > $setenv_script
87 fi
88
89 printf "kernel_size_%i %i\n" $next_boot_part $((kernel_length / 1024)) >> $setenv_script
90 printf "vmlinux_start_addr 0x%08x\n" $((flash_start_mem + inactive_offset)) >> $setenv_script
91 printf "vmlinux_size 0x%08x\n" ${kernel_length} >> $setenv_script
92 printf "vmlinux_checksum %s\n" ${kernel_md5} >> $setenv_script
93
94 printf "rootfs_size_%i %i\n" $next_boot_part $(((total_size-kernel_length) / 1024)) >> $setenv_script
95 printf "rootfs_start_addr 0x%08x\n" $((flash_start_mem+inactive_offset+kernel_length)) >> $setenv_script
96 printf "rootfs_size 0x%08x\n" ${rootfs_length} >> $setenv_script
97 printf "rootfs_checksum %s\n" ${rootfs_md5} >> $setenv_script
98
99 # store u-boot env changes
100 mkdir -p /var/lock
101 fw_setenv -s $setenv_script || {
102 echo "failed to update U-Boot environment"
103 return 1
104 }
105 }