Rom Lemarchand | ad6ec0c | 2015-05-19 16:58:40 -0700 | [diff] [blame] | 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 | |
| 16 | from __future__ import print_function |
| 17 | from sys import argv, exit, stderr |
| 18 | from argparse import ArgumentParser, FileType, Action |
| 19 | from os import fstat |
| 20 | from struct import pack |
| 21 | from hashlib import sha1 |
| 22 | |
| 23 | def 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 | |
| 32 | def 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 | |
| 41 | def pad_file(f, padding): |
| 42 | pad = (padding - (f.tell() & (padding - 1))) & (padding - 1) |
| 43 | f.write(pack(str(pad) + 'x')) |
| 44 | |
| 45 | |
| 46 | def 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 | |
| 74 | class 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 | |
| 89 | def 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 | |
Rom Lemarchand | 45f2ce1 | 2015-06-02 19:01:25 -0700 | [diff] [blame] | 96 | def parse_int(x): |
Rom Lemarchand | a8221d3 | 2015-06-04 09:59:01 -0700 | [diff] [blame^] | 97 | return int(x, 0) |
Rom Lemarchand | 45f2ce1 | 2015-06-02 19:01:25 -0700 | [diff] [blame] | 98 | |
| 99 | |
Rom Lemarchand | ad6ec0c | 2015-05-19 16:58:40 -0700 | [diff] [blame] | 100 | def parse_cmdline(): |
| 101 | parser = ArgumentParser() |
| 102 | parser.add_argument('--kernel', help='path to the kernel', type=FileType('rb'), |
| 103 | required=True) |
| 104 | parser.add_argument('--ramdisk', help='path to the ramdisk', type=FileType('rb')) |
| 105 | parser.add_argument('--second', help='path to the 2nd bootloader', type=FileType('rb')) |
| 106 | parser.add_argument('--cmdline', help='extra arguments to be passed on the ' |
| 107 | 'kernel command line', default='', action=ValidateStrLenAction, maxlen=1536) |
Rom Lemarchand | 45f2ce1 | 2015-06-02 19:01:25 -0700 | [diff] [blame] | 108 | parser.add_argument('--base', help='base address', type=parse_int, default=0x10000000) |
Rom Lemarchand | a8221d3 | 2015-06-04 09:59:01 -0700 | [diff] [blame^] | 109 | parser.add_argument('--kernel_offset', help='kernel offset', type=parse_int, default=0x00008000) |
Rom Lemarchand | 45f2ce1 | 2015-06-02 19:01:25 -0700 | [diff] [blame] | 110 | parser.add_argument('--ramdisk_offset', help='ramdisk offset', type=parse_int, default=0x01000000) |
| 111 | parser.add_argument('--second_offset', help='2nd bootloader offset', type=parse_int, |
Rom Lemarchand | ad6ec0c | 2015-05-19 16:58:40 -0700 | [diff] [blame] | 112 | default=0x00f00000) |
Rom Lemarchand | 45f2ce1 | 2015-06-02 19:01:25 -0700 | [diff] [blame] | 113 | parser.add_argument('--tags_offset', help='tags offset', type=parse_int, default=0x00000100) |
Rom Lemarchand | ad6ec0c | 2015-05-19 16:58:40 -0700 | [diff] [blame] | 114 | parser.add_argument('--board', help='board name', default='', action=ValidateStrLenAction, |
| 115 | maxlen=16) |
Rom Lemarchand | 45f2ce1 | 2015-06-02 19:01:25 -0700 | [diff] [blame] | 116 | parser.add_argument('--pagesize', help='page size', type=parse_int, |
Rom Lemarchand | ad6ec0c | 2015-05-19 16:58:40 -0700 | [diff] [blame] | 117 | choices=[2**i for i in range(11,15)], default=2048) |
| 118 | parser.add_argument('--id', help='print the image ID on standard output', |
| 119 | action='store_true') |
| 120 | parser.add_argument('-o', '--output', help='output file name', type=FileType('wb'), |
| 121 | required=True) |
| 122 | return parser.parse_args() |
| 123 | |
| 124 | |
| 125 | def write_data(args): |
| 126 | write_padded_file(args.output, args.kernel, args.pagesize) |
| 127 | write_padded_file(args.output, args.ramdisk, args.pagesize) |
| 128 | write_padded_file(args.output, args.second, args.pagesize) |
| 129 | |
| 130 | |
| 131 | def main(): |
| 132 | args = parse_cmdline() |
| 133 | img_id = write_header(args) |
| 134 | write_data(args) |
| 135 | if args.id: |
| 136 | print('0x' + ''.join('{:02x}'.format(ord(c)) for c in img_id)) |
| 137 | |
| 138 | |
| 139 | if __name__ == '__main__': |
| 140 | main() |