| #!/usr/bin/python |
| |
| # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| |
| """This utility builds a firmware image for a tegra-based board. |
| |
| This utility uses a number of libraries for its activity. |
| |
| Hint: in order to run this outside the chroot you will need the following |
| from the chroot: |
| |
| /usr/bin: |
| bmpblk_utility |
| gbb_utility |
| cbootimage |
| vbutil_firmware |
| dtget |
| dtput |
| |
| /usr/lib: |
| liblzma.so.0* |
| libyaml-0.so.1* |
| |
| """ |
| |
| # Python imports |
| import optparse |
| import os |
| import sys |
| |
| # Add the path to our own libraries |
| base = os.path.dirname(sys.argv[0]) |
| sys.path.append(base) |
| sys.path.append(os.path.join(base, 'lib')) |
| |
| from fdt import Fdt |
| from pack_firmware import PackFirmware |
| from write_firmware import WriteFirmware |
| from bundle_firmware import Bundle |
| import cros_output |
| from tools import Tools |
| from tools import CmdError |
| |
| def _CheckTools(tools, options): |
| """Check that all required tools are present. |
| |
| This just does a simple presence test on the external tools we thing we |
| might need. |
| |
| Args: |
| tools: Tools object to use to check tools. |
| Options: Command line options provided. |
| |
| Raises: |
| CmdError if a required tool is not found. |
| """ |
| if options.write: |
| tools.CheckTool('nvflash') |
| tools.CheckTool('dtput', 'dtc') |
| tools.CheckTool('dtget', 'dtc') |
| |
| def main(): |
| """Main function for cros_bundle_firmware.""" |
| parser = optparse.OptionParser() |
| parser.add_option('-v', '--verbosity', dest='verbosity', default=1, |
| type='int', help='Control verbosity: 0=silent, 1=progress, 3=full, ' |
| '4=debug') |
| # TODO(sjg): Support multiple BCT files |
| # TODO(sjg): Support source BCT files |
| parser.add_option('-c', '--bct', dest='bct', type='string', action='store', |
| help='Path to BCT source file: only one can be given') |
| parser.add_option('-k', '--key', dest='key', type='string', action='store', |
| help='Path to signing key directory (default to dev key)', |
| default='##/usr/share/vboot/devkeys') |
| # TODO(sjg): Support source FDT files |
| parser.add_option('-d', '--dt', dest='fdt', type='string', action='store', |
| help='Path to fdt binary blob .dtb file to use') |
| parser.add_option('-I', '--hwid', dest='hardware_id', type='string', |
| action='store', help='Hardware ID string to use') |
| parser.add_option('-u', '--uboot', dest='uboot', type='string', |
| action='store', help='Executable bootloader file (U-Boot)') |
| parser.add_option('-b', '--board', dest='board', type='string', |
| action='store', help='Board name to use (e.g. tegra2_kaen)', |
| default='tegra2_seaboard') |
| parser.add_option('-O', '--outdir', dest='outdir', type='string', |
| action='store', help='Path to directory to use for intermediate and ' |
| 'output files') |
| parser.add_option('-o', '--output', dest='output', type='string', |
| action='store', help='Filename of final output image') |
| parser.add_option('-p', '--preserve', dest='preserve', action='store_true',\ |
| help='Preserve temporary output directory') |
| parser.add_option('-s', '--small', dest='small', action='store_true', |
| help='Create/write only the signed U-Boot binary (not the full image)') |
| parser.add_option('--bootcmd', dest='bootcmd', type='string', |
| help='Set U-Boot boot command') |
| parser.add_option('--add-config-str', dest='add_config_str', type='string', |
| nargs=2, action='append', help='Add a /config string to the U-Boot fdt') |
| parser.add_option('--add-config-int', dest='add_config_int', type='string', |
| nargs=2, action='append', help='Add a /config integer to the U-Boot fdt') |
| |
| # TODO(sjg): Move this into cros_write_firmware |
| parser.add_option('-w', '--write', dest='write', action='store_true', |
| default=False, help='Write firmware to SPI flash with USB A-A cable') |
| (options, args) = parser.parse_args(sys.argv) |
| |
| output = cros_output.Output(options.verbosity) |
| tools = Tools(output) |
| _CheckTools(tools, options) |
| tools.PrepareOutputDir(options.outdir, options.preserve) |
| |
| bundle = Bundle(tools, output) |
| bundle.SetDirs(keydir=options.key) |
| bundle.SetFiles(board=options.board, uboot=options.uboot, bct=options.bct) |
| bundle.SetOptions(small=options.small) |
| |
| try: |
| # Set up the fdt and options that we want. |
| bundle.SelectFdt(options.fdt) |
| bundle.SetBootcmd(options.bootcmd) |
| bundle.AddConfigList(options.add_config_str) |
| bundle.AddConfigList(options.add_config_int, use_int=True) |
| |
| out_fname = bundle.Start(options.output) |
| |
| # Write it to the board if required. |
| if options.write: |
| write = WriteFirmware(tools, bundle.fdt, output) |
| if write.FlashImage(bundle.uboot_fname, bundle.bct_fname, out_fname): |
| output.Progress('Image uploaded - please wait for flashing to ' |
| 'complete') |
| else: |
| raise CmdError('Image upload failed - please check board connection') |
| except (CmdError, ValueError) as err: |
| # For verbosity 4 we want to display all possible information |
| if options.verbosity >= 4: |
| raise |
| else: |
| output.Error(str(err)) |
| del tools |
| del output |
| sys.exit(1) |
| del tools |
| del output |
| |
| |
| def _Test(): |
| """Run any built-in tests.""" |
| import doctest |
| doctest.testmod() |
| |
| if __name__ == '__main__': |
| # If first argument is --test, run testing code. |
| if sys.argv[1:2] == ["--test"]: |
| _Test(*sys.argv[2:]) |
| else: |
| main() |