• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2021-2022 iSoftStone Device Co., Ltd.
2# Licensed under the Apache License, Version 2.0 (the "License");
3# you may not use this file except in compliance with the License.
4# You may obtain a copy of the License at
5#
6# http://www.apache.org/licenses/LICENSE-2.0
7#
8# Unless required by applicable law or agreed to in writing, software
9# distributed under the License is distributed on an "AS IS" BASIS,
10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11# See the License for the specific language governing permissions and
12# limitations under the License.
13set -e
14
15usage() {
16	cat >&2 << USAGE
17usage: $0 [-h] [-z] --boot_img BOOT_IMG [--out OUT] [--kernel KERNEL] [--ramdisk RAMDISK] [--second SECOND] [--dtb DTB ] [--recovery_dtbo RECOVERY_DTBO] -o OUTPUT
18
19optional arguments:
20  -h, --help            show this help message and exit
21  -z                    pack compressed kernel image
22  --boot_img BOOT_IMG   path to the original boot image
23  --out OUT             path to out binaries (default: out)
24  --kernel KERNEL       path to the new kernel
25  --ramdisk RAMDISK     path to the new ramdisk
26  --second SECOND       path to the new 2nd bootloader (default: resource.img)
27  --dtb DTB             path to the new dtb
28  --recovery_dtbo RECOVERY_DTBO
29                        path to the new recovery DTBO
30  -o OUTPUT, --output OUTPUT
31                        output file name
32USAGE
33}
34
35# Parse command-line arguments
36while [ $# -gt 0 ]; do
37	case $1 in
38		--boot_img)
39			boot_img=$2
40			shift 2
41			;;
42		--out)
43			out=$2
44			shift 2
45			;;
46		--kernel)
47			kernel=$2
48			shift 2
49			;;
50		--ramdisk)
51			ramdisk=$2
52			shift 2
53			;;
54		--second)
55			second=$2
56			shift 2
57			;;
58		--dtb)
59			dtb=$2
60			shift 2
61			;;
62		--recovery_dtbo)
63			recovery_dtbo=$2
64			shift 2
65			;;
66		-h)
67			usage
68			exit 0
69			;;
70		--help)
71			usage
72			exit 0
73			;;
74		-z)
75			compressed_kernel=y
76			shift
77			;;
78		-o)
79			output=$2
80			shift 2
81			;;
82		--output)
83			output=$2
84			shift 2
85			;;
86		*)
87			shift
88			;;
89        esac
90done
91
92if [ "$boot_img" == "" -o ! -e "$boot_img" ]; then
93	echo "No boot img"
94	usage
95	exit 1
96fi
97
98if [ "$output" == "" ]; then
99	echo "No output file name"
100	usage
101	exit 1
102fi
103
104srctree=${srctree-"."}
105objtree=${objtree-"."}
106out=${out-"out"}
107if [ "$($srctree/scripts/config --state CONFIG_ARM64)" == "y" ]; then
108	if [ "$compressed_kernel" == "y" ]; then
109		default_kernel=arch/arm64/boot/Image.lz4
110	else
111		default_kernel=arch/arm64/boot/Image
112	fi
113else
114	if [ "$compressed_kernel" == "y" ]; then
115		default_kernel=arch/arm/boot/zImage
116	else
117		default_kernel=arch/arm/boot/Image
118	fi
119fi
120kernel=${kernel-$objtree/$default_kernel}
121second=${second-$objtree/resource.img}
122ramdisk=${ramdisk-$out/ramdisk}
123dtb=${dtb-$out/dtb}
124recovery_dtbo=${recovery_dtbo-$out/recovery_dtbo}
125log="$out/unpack.log"
126
127mkdir -p $out
128$srctree/scripts/unpack_bootimg --boot_img $boot_img --out $out > $log
129
130cmdline=$(grep -a "^command line args: " $log | tr '\0' '\n'| sed "s/^command line args: //")
131extra_cmdline=$(grep -a "^additional command line args: " $log | tr '\0' '\n'| sed "s/^additional command line args: //")
132version=$(grep -a "^boot image header version: " $log | sed "s/^boot image header version: //")
133os_version=$(grep -a "^os version: " $log | sed "s/^os version: //")
134os_patch_level=$(grep -a "^os patch level: " $log | sed "s/^os patch level: //")
135
136dtb_size=$(grep -a "^dtb size: " $log | sed "s/^dtb size: //")
137dtb_size=${dtb_size:-0}
138if [ $dtb_size -gt 0 -a -e "$dtb" ]; then
139	DTB="--dtb $dtb"
140fi
141
142recovery_dtbo_size=$(grep -a "^recovery dtbo size: " $log | sed "s/^recovery dtbo size: //")
143recovery_dtbo_size=${recovery_dtbo_size:-0}
144if [ $recovery_dtbo_size -gt 0 -a -e "$recovery_dtbo" ]; then
145	RECOVERY_DTBO="--recovery_dtbo $recovery_dtbo"
146fi
147
148if [ $version -lt 3 ]; then
149	SECOND="--second $second"
150fi
151
152$srctree/scripts/mkbootimg \
153--kernel $kernel \
154$SECOND \
155--ramdisk $ramdisk \
156$DTB \
157$RECOVERY_DTBO \
158--cmdline "${cmdline}${extra_cmdline}" \
159--header_version $version \
160--os_version $os_version \
161--os_patch_level $os_patch_level \
162--output $output
163