blob: 78e398d6d55a458de3de5044b6dcd415362273aa [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001# Copyright (C) 2021 OpenWrt.org
2#
3
4. /lib/functions.sh
5
6emmc_upgrade_tar() {
7 local tar_file="$1"
8 [ "$CI_KERNPART" -a -z "$EMMC_KERN_DEV" ] && export EMMC_KERN_DEV="$(find_mmc_part $CI_KERNPART $CI_ROOTDEV)"
9 [ "$CI_ROOTPART" -a -z "$EMMC_ROOT_DEV" ] && export EMMC_ROOT_DEV="$(find_mmc_part $CI_ROOTPART $CI_ROOTDEV)"
10 [ "$CI_DATAPART" -a -z "$EMMC_DATA_DEV" ] && export EMMC_DATA_DEV="$(find_mmc_part $CI_DATAPART $CI_ROOTDEV)"
11 local has_kernel
12 local has_rootfs
13 local board_dir=$(tar tf "$tar_file" | grep -m 1 '^sysupgrade-.*/$')
14 board_dir=${board_dir%/}
15
16 tar tf "$tar_file" ${board_dir}/kernel 1>/dev/null 2>/dev/null && has_kernel=1
17 tar tf "$tar_file" ${board_dir}/root 1>/dev/null 2>/dev/null && has_rootfs=1
18
19 [ "$has_rootfs" = 1 -a "$EMMC_ROOT_DEV" ] && {
20 # Invalidate kernel image while rootfs is being written
21 [ "$has_kernel" = 1 -a "$EMMC_KERN_DEV" ] && {
22 dd if=/dev/zero of="$EMMC_KERN_DEV" bs=512 count=8
23 sync
24 }
25
26 export EMMC_ROOTFS_BLOCKS=$(($(tar xf "$tar_file" ${board_dir}/root -O | dd of="$EMMC_ROOT_DEV" bs=512 2>&1 | grep "records out" | cut -d' ' -f1)))
27 # Account for 64KiB ROOTDEV_OVERLAY_ALIGN in libfstools
28 EMMC_ROOTFS_BLOCKS=$(((EMMC_ROOTFS_BLOCKS + 127) & ~127))
29 sync
30 }
31
32 [ "$has_kernel" = 1 -a "$EMMC_KERN_DEV" ] &&
33 export EMMC_KERNEL_BLOCKS=$(($(tar xf "$tar_file" ${board_dir}/kernel -O | dd of="$EMMC_KERN_DEV" bs=512 2>&1 | grep "records out" | cut -d' ' -f1)))
34
35 if [ -z "$UPGRADE_BACKUP" ]; then
36 if [ "$EMMC_DATA_DEV" ]; then
37 dd if=/dev/zero of="$EMMC_DATA_DEV" bs=512 count=8
38 elif [ "$EMMC_ROOTFS_BLOCKS" ]; then
39 dd if=/dev/zero of="$EMMC_ROOT_DEV" bs=512 seek=$EMMC_ROOTFS_BLOCKS count=8
40 elif [ "$EMMC_KERNEL_BLOCKS" ]; then
41 dd if=/dev/zero of="$EMMC_KERN_DEV" bs=512 seek=$EMMC_KERNEL_BLOCKS count=8
42 fi
43 fi
44}
45
46emmc_upgrade_fit() {
47 local fit_file="$1"
48 [ "$CI_KERNPART" -a -z "$EMMC_KERN_DEV" ] && export EMMC_KERN_DEV="$(find_mmc_part $CI_KERNPART $CI_ROOTDEV)"
49
50 if [ "$EMMC_KERN_DEV" ]; then
51 export EMMC_KERNEL_BLOCKS=$(($(get_image "$fit_file" | fwtool -i /dev/null -T - | dd of="$EMMC_KERN_DEV" bs=512 2>&1 | grep "records out" | cut -d' ' -f1)))
52
53 [ -z "$UPGRADE_BACKUP" ] && dd if=/dev/zero of="$EMMC_KERN_DEV" bs=512 seek=$EMMC_KERNEL_BLOCKS count=8
54 fi
55}
56
57emmc_copy_config() {
58 if [ "$EMMC_DATA_DEV" ]; then
59 dd if="$UPGRADE_BACKUP" of="$EMMC_DATA_DEV" bs=512
60 elif [ "$EMMC_ROOTFS_BLOCKS" ]; then
61 dd if="$UPGRADE_BACKUP" of="$EMMC_ROOT_DEV" bs=512 seek=$EMMC_ROOTFS_BLOCKS
62 elif [ "$EMMC_KERNEL_BLOCKS" ]; then
63 dd if="$UPGRADE_BACKUP" of="$EMMC_KERN_DEV" bs=512 seek=$EMMC_KERNEL_BLOCKS
64 fi
65}
66
67emmc_do_upgrade() {
68 local file_type=$(identify_magic_long "$(get_magic_long "$1")")
69
70 case "$file_type" in
71 "fit") emmc_upgrade_fit $1;;
72 *) emmc_upgrade_tar $1;;
73 esac
74}