emmc.sh 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. [ "$CI_DTBPART" -a -z "$EMMC_DTB_DEV" ] && export EMMC_DTB_DEV="$(find_mmc_part $CI_DTBPART $CI_ROOTDEV)"
  10. local has_kernel
  11. local has_rootfs
  12. local has_dtb
  13. local gz
  14. local board_dir
  15. [ "$(identify_magic_long $(get_magic_long "$tar_file" cat))" = "gzip" ] && \
  16. gz="z"
  17. board_dir=$(tar t${gz}f "$tar_file" | grep -m 1 '^sysupgrade-.*/$')
  18. board_dir=${board_dir%/}
  19. tar t${gz}f "$tar_file" ${board_dir}/kernel 1>/dev/null 2>/dev/null && has_kernel=1
  20. tar t${gz}f "$tar_file" ${board_dir}/root 1>/dev/null 2>/dev/null && has_rootfs=1
  21. tar t${gz}f "$tar_file" ${board_dir}/dtb 1>/dev/null 2>/dev/null && has_dtb=1
  22. [ "$has_rootfs" = 1 -a "$EMMC_ROOT_DEV" ] && {
  23. # Invalidate kernel image while rootfs is being written
  24. [ "$has_kernel" = 1 -a "$EMMC_KERN_DEV" ] && {
  25. dd if=/dev/zero of="$EMMC_KERN_DEV" bs=512 count=8
  26. sync
  27. }
  28. export EMMC_ROOTFS_BLOCKS=$(($(tar x${gz}f "$tar_file" ${board_dir}/root -O | dd of="$EMMC_ROOT_DEV" bs=512 2>&1 | grep "records out" | cut -d' ' -f1)))
  29. # Account for 64KiB ROOTDEV_OVERLAY_ALIGN in libfstools
  30. EMMC_ROOTFS_BLOCKS=$(((EMMC_ROOTFS_BLOCKS + 127) & ~127))
  31. sync
  32. }
  33. [ "$has_dtb" = 1 -a "$EMMC_DTB_DEV" ] &&
  34. export EMMC_DTB_BLOCKS=$(($(tar x${gz}f "$tar_file" ${board_dir}/dtb -O | dd of="$EMMC_DTB_DEV" bs=512 2>&1 | grep "records out" | cut -d' ' -f1)))
  35. [ "$has_kernel" = 1 -a "$EMMC_KERN_DEV" ] &&
  36. export EMMC_KERNEL_BLOCKS=$(($(tar x${gz}f "$tar_file" ${board_dir}/kernel -O | dd of="$EMMC_KERN_DEV" bs=512 2>&1 | grep "records out" | cut -d' ' -f1)))
  37. if [ -z "$UPGRADE_BACKUP" ]; then
  38. if [ "$EMMC_DATA_DEV" ]; then
  39. dd if=/dev/zero of="$EMMC_DATA_DEV" bs=512 count=8
  40. elif [ "$EMMC_ROOTFS_BLOCKS" ]; then
  41. dd if=/dev/zero of="$EMMC_ROOT_DEV" bs=512 seek=$EMMC_ROOTFS_BLOCKS count=8
  42. elif [ "$EMMC_KERNEL_BLOCKS" ]; then
  43. dd if=/dev/zero of="$EMMC_KERN_DEV" bs=512 seek=$EMMC_KERNEL_BLOCKS count=8
  44. fi
  45. fi
  46. }
  47. emmc_upgrade_fit() {
  48. local fit_file="$1"
  49. [ "$CI_KERNPART" -a -z "$EMMC_KERN_DEV" ] && export EMMC_KERN_DEV="$(find_mmc_part $CI_KERNPART $CI_ROOTDEV)"
  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. [ -z "$UPGRADE_BACKUP" ] && dd if=/dev/zero of="$EMMC_KERN_DEV" bs=512 seek=$EMMC_KERNEL_BLOCKS count=8
  53. fi
  54. }
  55. emmc_copy_config() {
  56. if [ "$EMMC_DATA_DEV" ]; then
  57. dd if="$UPGRADE_BACKUP" of="$EMMC_DATA_DEV" bs=512
  58. elif [ "$EMMC_ROOTFS_BLOCKS" ]; then
  59. dd if="$UPGRADE_BACKUP" of="$EMMC_ROOT_DEV" bs=512 seek=$EMMC_ROOTFS_BLOCKS
  60. elif [ "$EMMC_KERNEL_BLOCKS" ]; then
  61. dd if="$UPGRADE_BACKUP" of="$EMMC_KERN_DEV" bs=512 seek=$EMMC_KERNEL_BLOCKS
  62. fi
  63. }
  64. emmc_do_upgrade() {
  65. local file_type=$(identify_magic_long "$(get_magic_long "$1")")
  66. case "$file_type" in
  67. "fit") emmc_upgrade_fit $1;;
  68. *) emmc_upgrade_tar $1;;
  69. esac
  70. }