83 lines
2.9 KiB
Python
83 lines
2.9 KiB
Python
#!/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)
|
|
parser.add_argument('--dt_list', help='List of device tree files to pick up', required=False)
|
|
return parser.parse_args()
|
|
|
|
def main():
|
|
args = parse_cmdline()
|
|
KERNEL_TYPE = 'UNCOMPRESSED_IMG'.encode()
|
|
kernel = open(args.kernel, 'rb')
|
|
str = kernel.readline()
|
|
if args.dt_list is not None:
|
|
dtb_list = args.dt_list.split()
|
|
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'):
|
|
if args.dt_list is not None:
|
|
if filename not in dtb_list:
|
|
continue
|
|
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()
|