1#!/usr/bin/env python3 2# -*- coding: utf-8 -*-" 3 4# Copyright 2019 The Chromium OS Authors. All rights reserved. 5# SPDX-License-Identifier: Apache-2.0 6 7"""Script to pack EC binary with manifest header. 8 9Package ecos main FW binary (kernel) and AON task binary into final EC binary 10image with a manifest header, ISH shim loader will parse this header and load 11each binaries into right memory location. 12""" 13 14import argparse 15import struct 16import os 17 18MANIFEST_ENTRY_SIZE = 0x80 19HEADER_SIZE = 0x1000 20PAGE_SIZE = 0x1000 21 22def parseargs(): 23 parser = argparse.ArgumentParser(allow_abbrev=False) 24 parser.add_argument("-k", "--kernel", 25 help="EC kernel binary to pack, \ 26 usually ec.RW.bin or ec.RW.flat.", 27 required=True) 28 parser.add_argument("-a", "--aon", 29 help="EC aontask binary to pack, \ 30 usually ish_aontask.bin.", 31 required=False) 32 parser.add_argument("-o", "--output", 33 help="Output flash binary file") 34 35 return parser.parse_args() 36 37def gen_manifest(ext_id, comp_app_name, code_offset, module_size): 38 """Returns a binary blob that represents a manifest entry""" 39 m = bytearray(MANIFEST_ENTRY_SIZE) 40 41 # 4 bytes of ASCII encode ID (little endian) 42 struct.pack_into('<4s', m, 0, ext_id) 43 # 8 bytes of ASCII encode ID (little endian) 44 struct.pack_into('<8s', m, 32, comp_app_name) 45 # 4 bytes of code offset (little endian) 46 struct.pack_into('<I', m, 96, code_offset) 47 # 2 bytes of module in page size increments (little endian) 48 struct.pack_into('<H', m, 100, module_size) 49 50 return m 51 52def roundup_page(size): 53 """Returns roundup-ed page size from size of bytes""" 54 return int(size / PAGE_SIZE) + (size % PAGE_SIZE > 0) 55 56def main(): 57 args = parseargs() 58 print(" Packing EC image file for ISH") 59 60 with open(args.output, 'wb') as f: 61 kernel_size = os.path.getsize(args.kernel) 62 63 if args.aon is not None: 64 aon_size = os.path.getsize(args.aon) 65 66 print(" kernel binary size:", kernel_size) 67 kern_rdup_pg_size = roundup_page(kernel_size) 68 # Add manifest for main ISH binary 69 f.write(gen_manifest(b'ISHM', b'ISH_KERN', HEADER_SIZE, kern_rdup_pg_size)) 70 71 if args.aon is not None: 72 print(" AON binary size: ", aon_size) 73 aon_rdup_pg_size = roundup_page(aon_size) 74 # Add manifest for aontask binary 75 f.write(gen_manifest(b'ISHM', b'AON_TASK', 76 (HEADER_SIZE + kern_rdup_pg_size * PAGE_SIZE - 77 MANIFEST_ENTRY_SIZE), aon_rdup_pg_size)) 78 79 # Add manifest that signals end of manifests 80 f.write(gen_manifest(b'ISHE', b'', 0, 0)) 81 82 # Pad the remaining HEADER with 0s 83 if args.aon is not None: 84 f.write(b'\x00' * (HEADER_SIZE - (MANIFEST_ENTRY_SIZE * 3))) 85 else: 86 f.write(b'\x00' * (HEADER_SIZE - (MANIFEST_ENTRY_SIZE * 2))) 87 88 # Append original kernel image 89 with open(args.kernel, 'rb') as in_file: 90 f.write(in_file.read()) 91 # Filling padings due to size round up as pages 92 f.write(b'\x00' * (kern_rdup_pg_size * PAGE_SIZE - kernel_size)) 93 94 if args.aon is not None: 95 # Append original aon image 96 with open(args.aon, 'rb') as in_file: 97 f.write(in_file.read()) 98 # Filling padings due to size round up as pages 99 f.write(b'\x00' * (aon_rdup_pg_size * PAGE_SIZE - aon_size)) 100 101if __name__ == '__main__': 102 main() 103