Jens Wiklander | 8f7de3f | 2014-12-04 08:04:01 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
Summer Qin | ac3cc6c | 2017-04-18 13:59:03 +0100 | [diff] [blame] | 3 | # Copyright (c) 2014-2017, Linaro Limited |
Jens Wiklander | 8f7de3f | 2014-12-04 08:04:01 +0100 | [diff] [blame] | 4 | # All rights reserved. |
| 5 | # |
| 6 | # Redistribution and use in source and binary forms, with or without |
| 7 | # modification, are permitted provided that the following conditions are met: |
| 8 | # |
| 9 | # 1. Redistributions of source code must retain the above copyright notice, |
| 10 | # this list of conditions and the following disclaimer. |
| 11 | # |
| 12 | # 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 | # this list of conditions and the following disclaimer in the documentation |
| 14 | # and/or other materials provided with the distribution. |
| 15 | # |
| 16 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 20 | # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 | # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 | # POSSIBILITY OF SUCH DAMAGE. |
| 27 | # |
| 28 | |
| 29 | import argparse |
| 30 | import sys |
| 31 | import shutil |
| 32 | import os |
| 33 | import struct |
| 34 | import hashlib |
| 35 | |
Jerome Forissier | 7a97665 | 2015-05-06 17:23:50 +0200 | [diff] [blame] | 36 | arch_id = {'arm32': 0, 'arm64': 1} |
Summer Qin | ac3cc6c | 2017-04-18 13:59:03 +0100 | [diff] [blame] | 37 | image_id = {'pager': 0, 'paged': 1} |
Jens Wiklander | 8f7de3f | 2014-12-04 08:04:01 +0100 | [diff] [blame] | 38 | |
Summer Qin | ac3cc6c | 2017-04-18 13:59:03 +0100 | [diff] [blame] | 39 | def write_header_v1(outf, init_size, args, paged_size): |
Jens Wiklander | 8f7de3f | 2014-12-04 08:04:01 +0100 | [diff] [blame] | 40 | magic = 0x4554504f # 'OPTE' |
| 41 | version = 1; |
| 42 | outf.write(struct.pack('<IBBHIIIII', \ |
Jerome Forissier | 7a97665 | 2015-05-06 17:23:50 +0200 | [diff] [blame] | 43 | magic, version, arch_id[args.arch], args.flags, init_size, \ |
Jens Wiklander | 8f7de3f | 2014-12-04 08:04:01 +0100 | [diff] [blame] | 44 | args.init_load_addr_hi, args.init_load_addr_lo, \ |
| 45 | args.init_mem_usage, paged_size)) |
Summer Qin | ac3cc6c | 2017-04-18 13:59:03 +0100 | [diff] [blame] | 46 | |
| 47 | def write_header_v2(outf, init_size, args, paged_size): |
| 48 | magic = 0x4554504f # 'OPTE' |
| 49 | version = 2 |
| 50 | nb_images = 1 if paged_size == 0 else 2 |
| 51 | outf.write(struct.pack('<IBBHI', \ |
| 52 | magic, version, arch_id[args.arch], args.flags, nb_images)) |
| 53 | outf.write(struct.pack('<IIII', \ |
| 54 | args.init_load_addr_hi, args.init_load_addr_lo, \ |
| 55 | image_id['pager'], init_size)) |
| 56 | if nb_images == 2: |
| 57 | outf.write(struct.pack('<IIII', \ |
| 58 | 0xffffffff, 0xffffffff, image_id['paged'], paged_size)) |
Jens Wiklander | 8f7de3f | 2014-12-04 08:04:01 +0100 | [diff] [blame] | 59 | |
| 60 | def append_to(outf, start_offs, in_fname, max_bytes=0xffffffff): |
| 61 | #print "Appending %s@0x%x 0x%x bytes at position 0x%x" % \ |
| 62 | #( in_fname, start_offs, max_bytes, int(outf.tell()) ) |
Joakim Bech | 47805ee | 2016-07-29 17:48:39 +0200 | [diff] [blame] | 63 | inf = open(in_fname, 'rb'); |
Jens Wiklander | 8f7de3f | 2014-12-04 08:04:01 +0100 | [diff] [blame] | 64 | inf.seek(start_offs) |
| 65 | while True : |
| 66 | nbytes = min(16 * 1024, max_bytes) |
| 67 | if nbytes == 0 : |
| 68 | break |
| 69 | #print "Reading %s %d bytes" % (in_fname, nbytes) |
| 70 | buf = inf.read(nbytes) |
| 71 | if not buf : |
| 72 | break |
| 73 | outf.write(buf) |
| 74 | max_bytes -= len(buf) |
| 75 | inf.close() |
| 76 | |
| 77 | def append_hashes(outf, in_fname): |
| 78 | page_size = 4 * 1024 |
| 79 | |
| 80 | inf = open(in_fname, 'r') |
| 81 | while True : |
| 82 | page = inf.read(page_size) |
| 83 | if len(page) == page_size : |
| 84 | #print "Writing hash at position 0x%x" % \ |
| 85 | #int(outf.tell()) |
| 86 | outf.write(hashlib.sha256(page).digest()) |
| 87 | elif len(page) == 0 : |
| 88 | break |
| 89 | else : |
Joakim Bech | 47805ee | 2016-07-29 17:48:39 +0200 | [diff] [blame] | 90 | print("Error: short read, got " + repr(len(page))) |
Jens Wiklander | 8f7de3f | 2014-12-04 08:04:01 +0100 | [diff] [blame] | 91 | sys.exit(1) |
| 92 | |
| 93 | inf.close() |
| 94 | |
| 95 | def int_parse(str): |
| 96 | return int(str, 0) |
| 97 | |
| 98 | def get_args(): |
| 99 | parser = argparse.ArgumentParser() |
| 100 | parser.add_argument('--arch', required=True, \ |
Jerome Forissier | 7a97665 | 2015-05-06 17:23:50 +0200 | [diff] [blame] | 101 | choices=arch_id.keys(), \ |
| 102 | help='Architecture') |
Jens Wiklander | 8f7de3f | 2014-12-04 08:04:01 +0100 | [diff] [blame] | 103 | |
| 104 | parser.add_argument('--flags', \ |
| 105 | type=int, default=0, \ |
| 106 | help='Flags, currently none defined') |
| 107 | |
| 108 | parser.add_argument('--init_size', \ |
| 109 | required=True, type=int_parse, \ |
| 110 | help='Size of initialization part of binary') |
| 111 | |
| 112 | parser.add_argument('--init_load_addr_hi', \ |
| 113 | type=int_parse, default=0, \ |
| 114 | help='Upper 32 bits of load address of binary') |
| 115 | |
| 116 | parser.add_argument('--init_load_addr_lo', \ |
| 117 | required=True, type=int_parse, \ |
| 118 | help='Lower 32 bits of load address of binary') |
| 119 | |
| 120 | parser.add_argument('--init_mem_usage', \ |
| 121 | required=True, type=int_parse, \ |
| 122 | help='Total amount of used memory when initializing'); |
| 123 | |
| 124 | parser.add_argument('--tee_pager_bin', \ |
| 125 | required=True, \ |
| 126 | help='The input tee_pager.bin') |
| 127 | |
Jerome Forissier | 30ca322 | 2015-03-18 15:01:52 +0100 | [diff] [blame] | 128 | parser.add_argument('--tee_pageable_bin', \ |
Jens Wiklander | 8f7de3f | 2014-12-04 08:04:01 +0100 | [diff] [blame] | 129 | required=True, \ |
Jerome Forissier | 30ca322 | 2015-03-18 15:01:52 +0100 | [diff] [blame] | 130 | help='The input tee_pageable.bin') |
Jens Wiklander | 8f7de3f | 2014-12-04 08:04:01 +0100 | [diff] [blame] | 131 | |
| 132 | parser.add_argument('--out', \ |
Summer Qin | ac3cc6c | 2017-04-18 13:59:03 +0100 | [diff] [blame] | 133 | required=False, type=argparse.FileType('wb'), \ |
Jens Wiklander | 8f7de3f | 2014-12-04 08:04:01 +0100 | [diff] [blame] | 134 | help='The output tee.bin') |
| 135 | |
Summer Qin | ac3cc6c | 2017-04-18 13:59:03 +0100 | [diff] [blame] | 136 | parser.add_argument('--out_header_v2', \ |
| 137 | required=False, type=argparse.FileType('wb'), \ |
| 138 | help='The output tee_header_v2.bin') |
| 139 | |
| 140 | parser.add_argument('--out_pager_v2', \ |
| 141 | required=False, type=argparse.FileType('wb'), \ |
| 142 | help='The output tee_pager_v2.bin') |
| 143 | |
| 144 | parser.add_argument('--out_pageable_v2', \ |
| 145 | required=False, type=argparse.FileType('wb'), \ |
| 146 | help='The output tee_pageable_v2.bin') |
| 147 | |
Jens Wiklander | 8f7de3f | 2014-12-04 08:04:01 +0100 | [diff] [blame] | 148 | return parser.parse_args(); |
| 149 | |
| 150 | def main(): |
| 151 | args = get_args() |
Jerome Forissier | 30ca322 | 2015-03-18 15:01:52 +0100 | [diff] [blame] | 152 | init_bin_size = args.init_size |
| 153 | tee_pager_fname = args.tee_pager_bin |
| 154 | tee_pageable_fname = args.tee_pageable_bin |
Summer Qin | ac3cc6c | 2017-04-18 13:59:03 +0100 | [diff] [blame] | 155 | pager_input_size = os.path.getsize(tee_pager_fname); |
| 156 | paged_input_size = os.path.getsize(tee_pageable_fname); |
| 157 | hash_size = paged_input_size / (4 * 1024) * \ |
| 158 | hashlib.sha256().digest_size |
Jens Wiklander | 8f7de3f | 2014-12-04 08:04:01 +0100 | [diff] [blame] | 159 | |
Summer Qin | ac3cc6c | 2017-04-18 13:59:03 +0100 | [diff] [blame] | 160 | if paged_input_size % (4 * 1024) != 0: |
| 161 | print("Error: pageable size not a multiple of 4K:" + \ |
| 162 | repr(paged_input_size)) |
| 163 | sys.exit(1) |
Jens Wiklander | 8f7de3f | 2014-12-04 08:04:01 +0100 | [diff] [blame] | 164 | |
Summer Qin | ac3cc6c | 2017-04-18 13:59:03 +0100 | [diff] [blame] | 165 | init_size = pager_input_size + \ |
| 166 | min(init_bin_size, paged_input_size) + \ |
| 167 | hash_size |
| 168 | paged_size = paged_input_size - \ |
| 169 | min(init_bin_size, paged_input_size) |
Jens Wiklander | 8f7de3f | 2014-12-04 08:04:01 +0100 | [diff] [blame] | 170 | |
Summer Qin | ac3cc6c | 2017-04-18 13:59:03 +0100 | [diff] [blame] | 171 | if args.out is not None: |
| 172 | outf = args.out |
| 173 | write_header_v1(outf, init_size, args, paged_size) |
| 174 | append_to(outf, 0, tee_pager_fname) |
| 175 | append_to(outf, 0, tee_pageable_fname, init_bin_size) |
| 176 | append_hashes(outf, tee_pageable_fname) |
| 177 | append_to(outf, init_bin_size, tee_pageable_fname) |
| 178 | outf.close() |
| 179 | |
| 180 | if args.out_header_v2 is not None: |
| 181 | outf = args.out_header_v2 |
| 182 | write_header_v2(outf, init_size, args, paged_size) |
| 183 | outf.close() |
| 184 | |
| 185 | if args.out_pager_v2 is not None: |
| 186 | outf = args.out_pager_v2 |
| 187 | append_to(outf, 0, tee_pager_fname) |
| 188 | append_to(outf, 0, tee_pageable_fname, init_bin_size) |
| 189 | append_hashes(outf, tee_pageable_fname) |
| 190 | outf.close() |
| 191 | |
| 192 | if args.out_pageable_v2 is not None: |
| 193 | outf = args.out_pageable_v2 |
| 194 | append_to(outf, init_bin_size, tee_pageable_fname) |
| 195 | outf.close() |
Jens Wiklander | 8f7de3f | 2014-12-04 08:04:01 +0100 | [diff] [blame] | 196 | |
| 197 | if __name__ == "__main__": |
| 198 | main() |