diff --git a/packkernelimg/Android.mk b/packkernelimg/Android.mk new file mode 100644 index 00000000..66f340e3 --- /dev/null +++ b/packkernelimg/Android.mk @@ -0,0 +1,10 @@ +LOCAL_PATH:= $(call my-dir) +include $(CLEAR_VARS) + +LOCAL_SRC_FILES := packkernelimg +LOCAL_MODULE_CLASS := EXECUTABLES +LOCAL_IS_HOST_MODULE := true + +LOCAL_MODULE := packkernelimg + +include $(BUILD_PREBUILT) diff --git a/packkernelimg/packkernelimg b/packkernelimg/packkernelimg new file mode 100644 index 00000000..d1e4fb80 --- /dev/null +++ b/packkernelimg/packkernelimg @@ -0,0 +1,76 @@ +#!/usr/bin/env python +#Copyright (c) 2016, The Linux Foundation. All rights reserved. +#Not a contribution.Apache license notifications and license are +#retained for attribution purposes only. +# Copyright 2015, The Android Open Source Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from __future__ import print_function +from sys import argv, exit, stderr +from argparse import ArgumentParser, FileType, Action +from os import fstat +from struct import pack +from hashlib import sha1 +import sys +import re +import os + +def filesize(f): + if f is None: + return 0 + try: + return fstat(f.fileno()).st_size + except OSError: + return 0 +def parse_cmdline(): + parser = ArgumentParser() + parser.add_argument('--kernel', help='Path to UNCOMPRESSED kernel image',required=True) + parser.add_argument('--dt', help='Path to device tree files', required=True) + return parser.parse_args() + +def main(): + args = parse_cmdline() + KERNEL_TYPE = 'UNCOMPRESSED_IMG'.encode() + kernel = open(args.kernel, 'rb') + str = kernel.readline() + if str.startswith('UNCOMPRESSED_IMG'): + print("File already patched") + exit(0) + kernel.seek(0) + print("Kernel size is %d" % filesize(kernel)) + #Temp file to hold output + output = open(args.kernel + 'tmp', 'wb') + output.write(pack('16s', KERNEL_TYPE)) + output.write(pack('1I', filesize(kernel))) + output.write(kernel.read()) + #Walk through the device tree dir and append all blobs to the output + for root, directories, files in os.walk(args.dt): + for filename in files: + if filename.endswith('.dtb'): + print("Processing file %s" % filename) + filepath = os.path.join(root, filename) + with open(filepath) as dtb_file: + output.write(dtb_file.read()) + output.close() + kernel.close() + #Write output from tmp file to original + output = open(args.kernel, 'wb') + kernel = open(args.kernel + 'tmp', 'rb') + output.write(kernel.read()) + output.close() + kernel.close() + #delete tmp file + os.remove(args.kernel + 'tmp') +if __name__ == '__main__': + main()