| #!/usr/bin/python |
| # |
| # Copyright 2016 Google Inc. |
| # |
| # 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. |
| |
| import argparse |
| import importlib |
| import sys |
| |
| import imagelib.layout |
| |
| parser = argparse.ArgumentParser( |
| description='Build a firmware image from its components') |
| |
| parser.add_argument('-o', dest='output_file', default='image.bin', |
| help='Output image file name') |
| |
| parser.add_argument('--verbose', dest='verbose', default=False, |
| action='store_true', |
| help='Enable verbose logging output') |
| parser.add_argument('--log-file', dest='log_file', default='', |
| help='File to write logging output to') |
| |
| subparsers = parser.add_subparsers(title='Layouts', dest='layout', |
| help='layout help') |
| |
| def main(): |
| layouts = {} |
| for layout in imagelib.layout.layouts: |
| layouts[layout] = importlib.import_module('.' + layout, |
| 'imagelib.layout') |
| |
| for name, layout in layouts.iteritems(): |
| subparser = subparsers.add_parser(name, help='%s help' % name) |
| layout.add_arguments(subparser) |
| |
| options = parser.parse_args() |
| |
| image = layouts[options.layout].prepare(options) |
| |
| if options.verbose and options.log_file: |
| with open(options.log_file, mode="wb", buffering=0) as log_file: |
| image_data = image.build(log_file) |
| else: |
| log_file = sys.stdout if options.verbose else None |
| image_data = image.build(log_file) |
| |
| with open(options.output_file, mode="wb", buffering=0) as output: |
| output.write(image_data) |
| |
| if __name__ == '__main__': |
| main() |