2
0

emmc.sh 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Copyright (C) 2021 OpenWrt.org
  2. #
  3. . /lib/functions.sh
  4. emmc_upgrade_tar() {
  5. local tar_file="$1"
  6. [ "$CI_KERNPART" -a -z "$EMMC_KERN_DEV" ] && export EMMC_KERN_DEV="$(find_mmc_part $CI_KERNPART $CI_ROOTDEV)"
  7. [ "$CI_ROOTPART" -a -z "$EMMC_ROOT_DEV" ] && export EMMC_ROOT_DEV="$(find_mmc_part $CI_ROOTPART $CI_ROOTDEV)"
  8. [ "$CI_DATAPART" -a -z "$EMMC_DATA_DEV" ] && export EMMC_DATA_DEV="$(find_mmc_part $CI_DATAPART $CI_ROOTDEV)"
  9. local has_kernel
  10. local has_rootfs
  11. local board_dir=$(tar tf "$tar_file" | grep -m 1 '^sysupgrade-.*/$')
  12. board_dir=${board_dir%/}
  13. tar tf "$tar_file" ${board_dir}/kernel 1>/dev/null 2>/dev/null && has_kernel=1
  14. tar tf "$tar_file" ${board_dir}/root 1>/dev/null 2>/dev/null && has_rootfs=1
  15. [ "$has_kernel" = 1 -a "$EMMC_KERN_DEV" ] &&
  16. 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)))
  17. [ "$has_rootfs" = 1 -a "$EMMC_ROOT_DEV" ] &&
  18. 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)))
  19. if [ -z "$UPGRADE_BACKUP" ]; then
  20. if [ "$EMMC_DATA_DEV" ]; then
  21. dd if=/dev/zero of="$EMMC_DATA_DEV" bs=512 count=8
  22. elif [ "$EMMC_ROOTFS_BLOCKS" ]; then
  23. dd if=/dev/zero of="$EMMC_ROOT_DEV" bs=512 seek=$EMMC_ROOTFS_BLOCKS count=8
  24. elif [ "$EMMC_KERNEL_BLOCKS" ]; then
  25. dd if=/dev/zero of="$EMMC_KERN_DEV" bs=512 seek=$EMMC_KERNEL_BLOCKS count=8
  26. fi
  27. fi
  28. }
  29. emmc_upgrade_fit() {
  30. local fit_file="$1"
  31. [ "$CI_KERNPART" -a -z "$EMMC_KERN_DEV" ] && export EMMC_KERN_DEV="$(find_mmc_part $CI_KERNPART $CI_ROOTDEV)"
  32. if [ "$EMMC_KERN_DEV" ]; then
  33. 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)))
  34. [ -z "$UPGRADE_BACKUP" ] && dd if=/dev/zero of="$EMMC_KERN_DEV" bs=512 seek=$EMMC_KERNEL_BLOCKS count=8
  35. fi
  36. }
  37. emmc_copy_config() {
  38. if [ "$EMMC_DATA_DEV" ]; then
  39. dd if="$UPGRADE_BACKUP" of="$EMMC_DATA_DEV" bs=512
  40. elif [ "$EMMC_ROOTFS_BLOCKS" ]; then
  41. dd if="$UPGRADE_BACKUP" of="$EMMC_ROOT_DEV" bs=512 seek=$EMMC_ROOTFS_BLOCKS
  42. elif [ "$EMMC_KERNEL_BLOCKS" ]; then
  43. dd if="$UPGRADE_BACKUP" of="$EMMC_KERN_DEV" bs=512 seek=$EMMC_KERNEL_BLOCKS
  44. fi
  45. }
  46. emmc_do_upgrade() {
  47. local file_type=$(identify $1)
  48. case "$file_type" in
  49. "fit") emmc_upgrade_fit $1;;
  50. *) emmc_upgrade_tar $1;;
  51. esac
  52. }