^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) #!/bin/bash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) # SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) # Copyright (c) 2019 Fuzhou Rockchip Electronics Co., Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) set -e
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) usage() {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) cat >&2 << USAGE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) usage: $0 [-h] [-z] --boot_img BOOT_IMG [--out OUT] [--kernel KERNEL] [--ramdisk RAMDISK] [--second SECOND] [--dtb DTB ] [--recovery_dtbo RECOVERY_DTBO] -o OUTPUT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) optional arguments:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) -h, --help show this help message and exit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) -z pack compressed kernel image
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) --boot_img BOOT_IMG path to the original boot image
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) --out OUT path to out binaries (default: out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) --kernel KERNEL path to the new kernel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) --ramdisk RAMDISK path to the new ramdisk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) --second SECOND path to the new 2nd bootloader (default: resource.img)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) --dtb DTB path to the new dtb
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) --recovery_dtbo RECOVERY_DTBO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) path to the new recovery DTBO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) -o OUTPUT, --output OUTPUT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) output file name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) USAGE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) # Parse command-line arguments
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) while [ $# -gt 0 ]; do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) case $1 in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) --boot_img)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) boot_img=$2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) shift 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) ;;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) --out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) out=$2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) shift 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) ;;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) --kernel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) kernel=$2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) shift 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) ;;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) --ramdisk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) ramdisk=$2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) shift 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) ;;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) --second)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) second=$2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) shift 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) ;;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) --dtb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) dtb=$2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) shift 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) ;;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) --recovery_dtbo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) recovery_dtbo=$2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) shift 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) ;;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) -h)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) usage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) exit 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) ;;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) --help)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) usage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) exit 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) ;;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) -z)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) compressed_kernel=y
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) shift
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) ;;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) -o)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) output=$2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) shift 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) ;;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) --output)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) output=$2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) shift 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) ;;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) shift
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) ;;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) esac
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) done
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if [ "$boot_img" == "" -o ! -e "$boot_img" ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) echo "No boot img"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) usage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) exit 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if [ "$output" == "" ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) echo "No output file name"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) usage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) exit 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) srctree=${srctree-"."}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) objtree=${objtree-"."}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) out=${out-"out"}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if [ "$($srctree/scripts/config --state CONFIG_ARM64)" == "y" ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) if [ "$compressed_kernel" == "y" ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) default_kernel=arch/arm64/boot/Image.lz4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) default_kernel=arch/arm64/boot/Image
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) if [ "$compressed_kernel" == "y" ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) default_kernel=arch/arm/boot/zImage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) default_kernel=arch/arm/boot/Image
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) kernel=${kernel-$objtree/$default_kernel}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) second=${second-$objtree/resource.img}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) ramdisk=${ramdisk-$out/ramdisk}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) dtb=${dtb-$out/dtb}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) recovery_dtbo=${recovery_dtbo-$out/recovery_dtbo}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) log="$out/unpack.log"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) mkdir -p $out
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) $srctree/scripts/unpack_bootimg --boot_img $boot_img --out $out > $log
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) cmdline=$(grep -a "^command line args: " $log | tr '\0' '\n'| sed "s/^command line args: //")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) extra_cmdline=$(grep -a "^additional command line args: " $log | tr '\0' '\n'| sed "s/^additional command line args: //")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) version=$(grep -a "^boot image header version: " $log | sed "s/^boot image header version: //")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) os_version=$(grep -a "^os version: " $log | sed "s/^os version: //")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) os_patch_level=$(grep -a "^os patch level: " $log | sed "s/^os patch level: //")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) dtb_size=$(grep -a "^dtb size: " $log | sed "s/^dtb size: //")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) dtb_size=${dtb_size:-0}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if [ $dtb_size -gt 0 -a -e "$dtb" ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) DTB="--dtb $dtb"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) recovery_dtbo_size=$(grep -a "^recovery dtbo size: " $log | sed "s/^recovery dtbo size: //")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) recovery_dtbo_size=${recovery_dtbo_size:-0}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) if [ $recovery_dtbo_size -gt 0 -a -e "$recovery_dtbo" ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) RECOVERY_DTBO="--recovery_dtbo $recovery_dtbo"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) if [ $version -lt 3 ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) SECOND="--second $second"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) $srctree/scripts/mkbootimg \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) --kernel $kernel \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) $SECOND \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) --ramdisk $ramdisk \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) $DTB \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) $RECOVERY_DTBO \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) --cmdline "${cmdline}${extra_cmdline}" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) --header_version $version \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) --os_version $os_version \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) --os_patch_level $os_patch_level \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) --output $output