summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHridya Valsaraju2018-03-22 00:18:19 -0500
committerHridya Valsaraju2018-03-28 16:25:41 -0500
commit02c08cc3471f11db10853c4f668d3348ce6f403f (patch)
tree28e1dcf2b6b9c2a7b47ccd6462e6060472d34155 /mkbootimg
parent147b355bb34cbd457a260c585be93193cef43b8e (diff)
downloadplatform-system-core-02c08cc3471f11db10853c4f668d3348ce6f403f.tar.gz
platform-system-core-02c08cc3471f11db10853c4f668d3348ce6f403f.tar.xz
platform-system-core-02c08cc3471f11db10853c4f668d3348ce6f403f.zip
Add a script to unpack boot image
The script prints out the boot image header contents and extracts the kernel, ramdisk, second bootloader and recovery DTBO images. Bug: 74763691 Test: unpack_bootimg --boot_img $OUT/recovery.img Change-Id: Iadbca81c157d9e4607f808a14468ab5542347507
Diffstat (limited to 'mkbootimg')
-rw-r--r--mkbootimg/Android.mk9
-rwxr-xr-xmkbootimg/unpack_bootimg137
2 files changed, 146 insertions, 0 deletions
diff --git a/mkbootimg/Android.mk b/mkbootimg/Android.mk
index 8661d7d6b..92e1e27d2 100644
--- a/mkbootimg/Android.mk
+++ b/mkbootimg/Android.mk
@@ -9,3 +9,12 @@ LOCAL_IS_HOST_MODULE := true
9LOCAL_MODULE := mkbootimg 9LOCAL_MODULE := mkbootimg
10 10
11include $(BUILD_PREBUILT) 11include $(BUILD_PREBUILT)
12
13include $(CLEAR_VARS)
14LOCAL_SRC_FILES := unpack_bootimg
15LOCAL_MODULE_CLASS := EXECUTABLES
16LOCAL_IS_HOST_MODULE := true
17
18LOCAL_MODULE := unpack_bootimg
19
20include $(BUILD_PREBUILT)
diff --git a/mkbootimg/unpack_bootimg b/mkbootimg/unpack_bootimg
new file mode 100755
index 000000000..8e42ec029
--- /dev/null
+++ b/mkbootimg/unpack_bootimg
@@ -0,0 +1,137 @@
1#!/usr/bin/env python
2# Copyright 2018, The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16"""unpacks the bootimage.
17
18Extracts the kernel, ramdisk, second bootloader and recovery dtbo images.
19"""
20
21from __future__ import print_function
22from argparse import ArgumentParser, FileType
23from struct import unpack
24import os
25
26
27def create_out_dir(dir_path):
28 """creates a directory 'dir_path' if it does not exist"""
29 if not os.path.exists(dir_path):
30 os.makedirs(dir_path)
31
32
33def extract_image(offset, size, bootimage, extracted_image_name):
34 """extracts an image from the bootimage"""
35 bootimage.seek(offset)
36 with open(extracted_image_name, 'wb') as file_out:
37 file_out.write(bootimage.read(size))
38
39
40def get_number_of_pages(image_size, page_size):
41 """calculates the number of pages required for the image"""
42 return (image_size + page_size - 1) / page_size
43
44
45def unpack_bootimage(args):
46 """extracts kernel, ramdisk, second bootloader and recovery dtbo"""
47 boot_magic = unpack('8s', args.boot_img.read(8))
48 print('boot_magic: %s' % boot_magic)
49 kernel_ramdisk_second_info = unpack('10I', args.boot_img.read(10 * 4))
50 print('kernel_size: %s' % kernel_ramdisk_second_info[0])
51 print('kernel load address: %s' % kernel_ramdisk_second_info[1])
52 print('ramdisk size: %s' % kernel_ramdisk_second_info[2])
53 print('ramdisk load address: %s' % kernel_ramdisk_second_info[3])
54 print('second bootloader size: %s' % kernel_ramdisk_second_info[4])
55 print('second bootloader load address: %s' % kernel_ramdisk_second_info[5])
56 print('kernel tags load address: %s' % kernel_ramdisk_second_info[6])
57 print('page size: %s' % kernel_ramdisk_second_info[7])
58 print('boot image header version: %s' % kernel_ramdisk_second_info[8])
59 print('os version and patch level: %s' % kernel_ramdisk_second_info[9])
60
61 product_name = unpack('16s', args.boot_img.read(16))
62 print('product name: %s' % product_name)
63 cmdline = unpack('512s', args.boot_img.read(512))
64 print('command line args: %s' % cmdline)
65
66 args.boot_img.read(32) # ignore SHA
67
68 extra_cmdline = unpack('1024s', args.boot_img.read(1024))
69 print('additional command line args: %s' % extra_cmdline)
70
71 kernel_size = kernel_ramdisk_second_info[0]
72 ramdisk_size = kernel_ramdisk_second_info[2]
73 second_size = kernel_ramdisk_second_info[4]
74 page_size = kernel_ramdisk_second_info[7]
75 version = kernel_ramdisk_second_info[8]
76 if version > 0:
77 recovery_dtbo_size = unpack('I', args.boot_img.read(1 * 4))[0]
78 print('recovery dtbo size: %s' % recovery_dtbo_size)
79 recovery_dtbo_address = unpack('Q', args.boot_img.read(8))[0]
80 print('recovery dtbo load address: %s' % recovery_dtbo_address)
81 boot_header_size = unpack('I', args.boot_img.read(4))[0]
82 print('boot header size: %s' % boot_header_size)
83 else:
84 recovery_dtbo_size = 0
85
86 # The first page contains the boot header
87 num_header_pages = 1
88
89 num_kernel_pages = get_number_of_pages(kernel_size, page_size)
90 kernel_offset = page_size * num_header_pages # header occupies a page
91 image_info_list = [(kernel_offset, kernel_size, 'kernel')]
92
93 num_ramdisk_pages = get_number_of_pages(ramdisk_size, page_size)
94 ramdisk_offset = page_size * (num_header_pages + num_kernel_pages
95 ) # header + kernel
96 image_info_list.append((ramdisk_offset, ramdisk_size, 'ramdisk'))
97
98 num_second_pages = get_number_of_pages(second_size, page_size)
99 second_offset = page_size * (
100 num_header_pages + num_kernel_pages + num_ramdisk_pages
101 ) # header + kernel + ramdisk
102 image_info_list.append((second_offset, second_size, 'second'))
103
104 if recovery_dtbo_size > 0:
105 dtbo_offset = page_size * (num_header_pages + num_kernel_pages +
106 num_ramdisk_pages + num_second_pages)
107 image_info_list.append((dtbo_offset, recovery_dtbo_size,
108 'recovery_dtbo'))
109
110 for image_info in image_info_list:
111 extract_image(image_info[0], image_info[1], args.boot_img,
112 os.path.join(args.out, image_info[2]))
113
114
115def parse_cmdline():
116 """parse command line arguments"""
117 parser = ArgumentParser(
118 description='Unpacks boot.img/recovery.img, extracts the kernel,'
119 'ramdisk, second bootloader and recovery dtbo')
120 parser.add_argument(
121 '--boot_img',
122 help='path to boot image',
123 type=FileType('rb'),
124 required=True)
125 parser.add_argument('--out', help='path to out binaries', default='out')
126 return parser.parse_args()
127
128
129def main():
130 """parse arguments and unpack boot image"""
131 args = parse_cmdline()
132 create_out_dir(args.out)
133 unpack_bootimage(args)
134
135
136if __name__ == '__main__':
137 main()