summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRom Lemarchand2015-05-19 18:58:40 -0500
committerRom Lemarchand2015-06-02 18:10:52 -0500
commitad6ec0c7fe8b5cd80dff619f1d3b6067db85110a (patch)
tree8c4bae9549e1e6e64d4518e3fb6ed519af584de4 /mkbootimg
parent1333694c859d7e09f6a80546cb2cdc225430c897 (diff)
downloadplatform-system-core-ad6ec0c7fe8b5cd80dff619f1d3b6067db85110a.tar.gz
platform-system-core-ad6ec0c7fe8b5cd80dff619f1d3b6067db85110a.tar.xz
platform-system-core-ad6ec0c7fe8b5cd80dff619f1d3b6067db85110a.zip
mkbootimg: turn into a python script
Turn mkbootimg into a python script instead of a C utility Change-Id: I36d35dfacfbbef2cc16bd6695ab15ee716bb0cdb
Diffstat (limited to 'mkbootimg')
-rw-r--r--mkbootimg/Android.mk10
-rwxr-xr-xmkbootimg/mkbootimg136
2 files changed, 140 insertions, 6 deletions
diff --git a/mkbootimg/Android.mk b/mkbootimg/Android.mk
index 0c9b0c657..8661d7d6b 100644
--- a/mkbootimg/Android.mk
+++ b/mkbootimg/Android.mk
@@ -2,12 +2,10 @@
2LOCAL_PATH:= $(call my-dir) 2LOCAL_PATH:= $(call my-dir)
3include $(CLEAR_VARS) 3include $(CLEAR_VARS)
4 4
5LOCAL_SRC_FILES := mkbootimg.c 5LOCAL_SRC_FILES := mkbootimg
6LOCAL_STATIC_LIBRARIES := libmincrypt 6LOCAL_MODULE_CLASS := EXECUTABLES
7LOCAL_CFLAGS := -Werror 7LOCAL_IS_HOST_MODULE := true
8 8
9LOCAL_MODULE := mkbootimg 9LOCAL_MODULE := mkbootimg
10 10
11include $(BUILD_HOST_EXECUTABLE) 11include $(BUILD_PREBUILT)
12
13$(call dist-for-goals,dist_files,$(LOCAL_BUILT_MODULE))
diff --git a/mkbootimg/mkbootimg b/mkbootimg/mkbootimg
new file mode 100755
index 000000000..8c27ff795
--- /dev/null
+++ b/mkbootimg/mkbootimg
@@ -0,0 +1,136 @@
1#!/usr/bin/env python
2# Copyright 2015, 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
16from __future__ import print_function
17from sys import argv, exit, stderr
18from argparse import ArgumentParser, FileType, Action
19from os import fstat
20from struct import pack
21from hashlib import sha1
22
23def filesize(f):
24 if f is None:
25 return 0
26 try:
27 return fstat(f.fileno()).st_size
28 except OSError:
29 return 0
30
31
32def update_sha(sha, f):
33 if f:
34 sha.update(f.read())
35 f.seek(0)
36 sha.update(pack('I', filesize(f)))
37 else:
38 sha.update(pack('I', 0))
39
40
41def pad_file(f, padding):
42 pad = (padding - (f.tell() & (padding - 1))) & (padding - 1)
43 f.write(pack(str(pad) + 'x'))
44
45
46def write_header(args):
47 BOOT_MAGIC = 'ANDROID!'.encode()
48 args.output.write(pack('8s', BOOT_MAGIC))
49 args.output.write(pack('8I',
50 filesize(args.kernel), # size in bytes
51 args.base + args.kernel_offset, # physical load addr
52 filesize(args.ramdisk), # size in bytes
53 args.base + args.ramdisk_offset, # physical load addr
54 filesize(args.second), # size in bytes
55 args.base + args.second_offset, # physical load addr
56 args.base + args.tags_offset, # physical addr for kernel tags
57 args.pagesize)) # flash page size we assume
58 args.output.write(pack('8x')) # future expansion: should be 0
59 args.output.write(pack('16s', args.board.encode())) # asciiz product name
60 args.output.write(pack('512s', args.cmdline[:512].encode()))
61
62 sha = sha1()
63 update_sha(sha, args.kernel)
64 update_sha(sha, args.ramdisk)
65 update_sha(sha, args.second)
66 img_id = pack('32s', sha.digest())
67
68 args.output.write(img_id)
69 args.output.write(pack('1024s', args.cmdline[512:].encode()))
70 pad_file(args.output, args.pagesize)
71 return img_id
72
73
74class ValidateStrLenAction(Action):
75 def __init__(self, option_strings, dest, nargs=None, **kwargs):
76 if 'maxlen' not in kwargs:
77 raise ValueError('maxlen must be set')
78 self.maxlen = int(kwargs['maxlen'])
79 del kwargs['maxlen']
80 super(ValidateStrLenAction, self).__init__(option_strings, dest, **kwargs)
81
82 def __call__(self, parser, namespace, values, option_string=None):
83 if len(values) > self.maxlen:
84 raise ValueError('String argument too long: max {0:d}, got {1:d}'.
85 format(self.maxlen, len(values)))
86 setattr(namespace, self.dest, values)
87
88
89def write_padded_file(f_out, f_in, padding):
90 if f_in is None:
91 return
92 f_out.write(f_in.read())
93 pad_file(f_out, padding)
94
95
96def parse_cmdline():
97 parser = ArgumentParser()
98 parser.add_argument('--kernel', help='path to the kernel', type=FileType('rb'),
99 required=True)
100 parser.add_argument('--ramdisk', help='path to the ramdisk', type=FileType('rb'))
101 parser.add_argument('--second', help='path to the 2nd bootloader', type=FileType('rb'))
102 parser.add_argument('--cmdline', help='extra arguments to be passed on the '
103 'kernel command line', default='', action=ValidateStrLenAction, maxlen=1536)
104 parser.add_argument('--base', help='base address', type=int, default=0x10000000)
105 parser.add_argument('----kernel_offset', help='kernel offset', type=int, default=0x00008000)
106 parser.add_argument('--ramdisk_offset', help='ramdisk offset', type=int, default=0x01000000)
107 parser.add_argument('--second_offset', help='2nd bootloader offset', type=int,
108 default=0x00f00000)
109 parser.add_argument('--tags_offset', help='tags offset', type=int, default=0x00000100)
110 parser.add_argument('--board', help='board name', default='', action=ValidateStrLenAction,
111 maxlen=16)
112 parser.add_argument('--pagesize', help='page size', type=int,
113 choices=[2**i for i in range(11,15)], default=2048)
114 parser.add_argument('--id', help='print the image ID on standard output',
115 action='store_true')
116 parser.add_argument('-o', '--output', help='output file name', type=FileType('wb'),
117 required=True)
118 return parser.parse_args()
119
120
121def write_data(args):
122 write_padded_file(args.output, args.kernel, args.pagesize)
123 write_padded_file(args.output, args.ramdisk, args.pagesize)
124 write_padded_file(args.output, args.second, args.pagesize)
125
126
127def main():
128 args = parse_cmdline()
129 img_id = write_header(args)
130 write_data(args)
131 if args.id:
132 print('0x' + ''.join('{:02x}'.format(ord(c)) for c in img_id))
133
134
135if __name__ == '__main__':
136 main()