Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 1 | # Copyright (c) 2013 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """crosfw - Chrome OS Firmware build/flash script. |
| 6 | |
| 7 | Builds a firmware image for any board and writes it to the board. The image |
| 8 | can be pure upstream or include Chrome OS components (-V). Some device |
| 9 | tree parameters can be provided, including silent console (-C) and secure |
| 10 | boot (-S). Use -i for a faster incremental build. The image is written to |
| 11 | the board by default using USB/em100 (or sdcard with -x). Use -b to specify |
| 12 | the board to build. Options can be added to ~/.crosfwrc - see the script for |
| 13 | details. |
| 14 | |
| 15 | It can also flash SPI by writing a 'magic flasher' U-Boot with a payload |
| 16 | to the board. |
| 17 | |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 18 | The script is normally run from within the U-Boot directory which is |
| 19 | .../src/third_party/u-boot/files |
| 20 | |
| 21 | Example 1: Build upstream image for coreboot and write to a 'link': |
| 22 | |
| 23 | crosfw -b link |
| 24 | |
| 25 | Example 2: Build verified boot image (V) for daisy/snow and boot in secure |
| 26 | mode (S) so that breaking in on boot is not possible. |
| 27 | |
| 28 | crosfw -b daisy -VS |
| 29 | crosfw -b daisy -VSC (no console output) |
| 30 | |
| 31 | Example 3: Build a magic flasher (F) with full verified boot for peach_pit, |
| 32 | but with console enabled, write to SD card (x) |
| 33 | |
| 34 | crosfw -b peach_pit -VSFx |
| 35 | |
| 36 | This sript does not use an ebuild. It does a similar thing to the |
| 37 | chromeos-u-boot ebuild, and runs cros_bundle_firmware to produce various |
| 38 | types of image, a little like the chromeos-bootimage ebuild. |
| 39 | |
| 40 | The purpose of this script is to make it easier and faster to perform |
| 41 | common firmware build tasks without changing boards, manually updating |
| 42 | device tree files or lots of USE flags and complexity in the ebuilds. |
| 43 | |
| 44 | This script has been tested with snow, link and peach_pit. It builds for |
| 45 | peach_pit by default. Note that it will also build any upstream ARM |
| 46 | board - e.g. "-b snapper9260" will build an image for that board. |
| 47 | |
| 48 | Mostly you can use the script inside and outside the chroot. The main |
| 49 | limitation is that dut-control doesn't really work outside the chroot, |
| 50 | so writing the image to the board over USB is not possible, nor can the |
| 51 | board be automatically reset on x86 platforms. |
| 52 | |
| 53 | For an incremental build (faster), run with -i |
| 54 | |
| 55 | To get faster clean builds, install ccache, and create ~/.crosfwrc with |
| 56 | this line: |
| 57 | |
Simon Glass | 6ddc7f1 | 2013-07-18 15:22:41 -0600 | [diff] [blame] | 58 | USE_CCACHE = True |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 59 | |
| 60 | (make sure ~/.ccache is not on NFS, or set CCACHE_DIR) |
| 61 | |
| 62 | Other options are the default board to build, and verbosity (0-4), e.g.: |
| 63 | |
Simon Glass | 6ddc7f1 | 2013-07-18 15:22:41 -0600 | [diff] [blame] | 64 | DEFAULT_BOARD = 'daisy' |
| 65 | VERBOSE = 1 |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 66 | |
| 67 | It is possible to use multiple servo boards, each on its own port. Add |
| 68 | these lines to your ~/.crosfwrc to set the servo port to use for each |
| 69 | board: |
| 70 | |
| 71 | SERVO_PORT['link'] = 8888 |
| 72 | SERVO_PORT['daisy'] = 9999 |
| 73 | SERVO_PORT['peach_pit'] = 7777 |
| 74 | |
Simon Glass | b89ae89 | 2013-07-18 15:23:35 -0600 | [diff] [blame] | 75 | All builds appear in the <outdir>/<board> subdirectory and images are written |
| 76 | to <outdir>/<uboard>/out, where <uboard> is the U-Boot name for the board (in |
| 77 | the U-Boot boards.cfg file) |
| 78 | |
| 79 | The value for <outdir> defaults to /tmp/crosfw but can be configured in your |
| 80 | ~/.crosfwrc file, e.g.:" |
| 81 | |
| 82 | OUT_DIR = '/tmp/u-boot' |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 83 | |
| 84 | For the -a option here are some useful options: |
| 85 | |
| 86 | --add-blob cros-splash /dev/null |
| 87 | --gbb-flags -force-dev-switch-on |
| 88 | --add-node-enable /spi@131b0000/cros-ecp@0 1 |
| 89 | --verify --full-erase |
| 90 | --bootcmd "cros_test sha" |
| 91 | --gbb-flags -force-dev-switch-on |
Mike Frysinger | 0246c1f | 2021-12-14 01:17:17 -0500 | [diff] [blame] | 92 | --bmpblk ~/chromiumos/src/third_party/u-boot/bmp.bin |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 93 | |
| 94 | For example: -a "--gbb-flags -force-dev-switch-on" |
| 95 | |
| 96 | Note the standard bmpblk is at: |
Mike Frysinger | 0246c1f | 2021-12-14 01:17:17 -0500 | [diff] [blame] | 97 | ~/chromiumos/src/third_party/chromiumos-overlay/sys-boot/ |
| 98 | chromeos-bootimage/files/bmpblk.bin |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 99 | """ |
| 100 | |
| 101 | import glob |
Chris McDonald | 59650c3 | 2021-07-20 15:29:28 -0600 | [diff] [blame] | 102 | import logging |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 103 | import multiprocessing |
| 104 | import os |
| 105 | import re |
Mike Frysinger | 66d32cd | 2019-12-17 14:55:29 -0500 | [diff] [blame] | 106 | import subprocess |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 107 | import sys |
| 108 | |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 109 | from chromite.lib import commandline |
Chris McDonald | 59650c3 | 2021-07-20 15:29:28 -0600 | [diff] [blame] | 110 | from chromite.lib import constants |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 111 | from chromite.lib import cros_build_lib |
| 112 | from chromite.lib import osutils |
| 113 | from chromite.lib import parallel |
| 114 | |
| 115 | |
| 116 | arch = None |
| 117 | board = None |
| 118 | compiler = None |
| 119 | default_board = None |
| 120 | family = None |
| 121 | in_chroot = True |
| 122 | |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 123 | logging.basicConfig(format='%(message)s') |
Mike Frysinger | f5a3b2d | 2019-12-12 14:36:17 -0500 | [diff] [blame] | 124 | kwargs = {'print_cmd': False, 'check': False, |
Ralph Nathan | 23a1221 | 2015-03-25 10:27:54 -0700 | [diff] [blame] | 125 | 'debug_level': logging.getLogger().getEffectiveLevel()} |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 126 | |
| 127 | outdir = '' |
| 128 | |
| 129 | # If you have multiple boards connected on different servo ports, put lines |
| 130 | # like 'SERVO_PORT{"peach_pit"} = 7777' in your ~/.crosfwrc |
| 131 | SERVO_PORT = {} |
| 132 | |
| 133 | smdk = None |
| 134 | src_root = os.path.join(constants.SOURCE_ROOT, 'src') |
| 135 | in_chroot = cros_build_lib.IsInsideChroot() |
| 136 | |
| 137 | uboard = '' |
| 138 | |
| 139 | default_board = 'peach_pit' |
| 140 | use_ccache = False |
| 141 | vendor = None |
| 142 | verbose = False |
| 143 | |
| 144 | # Special cases for the U-Boot board config, the SOCs and default device tree |
| 145 | # since the naming is not always consistent. |
| 146 | # x86 has a lot of boards, but to U-Boot they are all the same |
| 147 | UBOARDS = { |
| 148 | 'daisy': 'smdk5250', |
| 149 | 'peach': 'smdk5420', |
| 150 | } |
| 151 | for b in ['alex', 'butterfly', 'emeraldlake2', 'link', 'lumpy', 'parrot', |
| 152 | 'stout', 'stumpy']: |
| 153 | UBOARDS[b] = 'coreboot-x86' |
| 154 | UBOARDS['chromeos_%s' % b] = 'chromeos_coreboot' |
| 155 | |
| 156 | SOCS = { |
| 157 | 'coreboot-x86': '', |
| 158 | 'chromeos_coreboot': '', |
| 159 | 'daisy': 'exynos5250-', |
| 160 | 'peach': 'exynos5420-', |
| 161 | } |
| 162 | |
| 163 | DEFAULT_DTS = { |
| 164 | 'daisy': 'snow', |
| 165 | 'daisy_spring': 'spring', |
| 166 | 'peach_pit': 'peach-pit', |
| 167 | } |
| 168 | |
Simon Glass | b89ae89 | 2013-07-18 15:23:35 -0600 | [diff] [blame] | 169 | OUT_DIR = '/tmp/crosfw' |
| 170 | |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 171 | rc_file = os.path.expanduser('~/.crosfwrc') |
| 172 | if os.path.exists(rc_file): |
Mike Frysinger | 5d535ac | 2019-07-03 03:29:47 -0400 | [diff] [blame] | 173 | with open(rc_file) as fp: |
| 174 | # pylint: disable=exec-used |
| 175 | exec(compile(fp.read(), rc_file, 'exec')) |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 176 | |
| 177 | |
| 178 | def Log(msg): |
| 179 | """Print out a message if we are in verbose mode. |
| 180 | |
| 181 | Args: |
| 182 | msg: Message to print |
| 183 | """ |
| 184 | if verbose: |
Ralph Nathan | 0304728 | 2015-03-23 11:09:32 -0700 | [diff] [blame] | 185 | logging.info(msg) |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 186 | |
| 187 | |
| 188 | def Dumper(flag, infile, outfile): |
| 189 | """Run objdump on an input file. |
| 190 | |
| 191 | Args: |
| 192 | flag: Flag to pass objdump (e.g. '-d'). |
| 193 | infile: Input file to process. |
| 194 | outfile: Output file to write to. |
| 195 | """ |
Mike Frysinger | 45602c7 | 2019-09-22 02:15:11 -0400 | [diff] [blame] | 196 | result = cros_build_lib.run( |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 197 | [CompilerTool('objdump'), flag, infile], |
Mike Frysinger | ae3e2c7 | 2019-12-07 02:35:12 -0500 | [diff] [blame] | 198 | stdout=outfile, **kwargs) |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 199 | if result.returncode: |
| 200 | sys.exit() |
| 201 | |
| 202 | |
| 203 | def CompilerTool(tool): |
| 204 | """Returns the cross-compiler tool filename. |
| 205 | |
| 206 | Args: |
| 207 | tool: Tool name to return, e.g. 'size'. |
| 208 | |
| 209 | Returns: |
| 210 | Filename of requested tool. |
| 211 | """ |
| 212 | return '%s%s' % (compiler, tool) |
| 213 | |
| 214 | |
| 215 | def ParseCmdline(argv): |
| 216 | """Parse all command line options. |
| 217 | |
| 218 | Args: |
| 219 | argv: Arguments to parse. |
| 220 | |
| 221 | Returns: |
Mike Frysinger | 4e91d82 | 2015-06-04 02:01:40 -0400 | [diff] [blame] | 222 | The parsed options object |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 223 | """ |
Mike Frysinger | 4e91d82 | 2015-06-04 02:01:40 -0400 | [diff] [blame] | 224 | parser = commandline.ArgumentParser(description=__doc__) |
| 225 | parser.add_argument('-a', '--cbfargs', action='append', |
| 226 | help='Pass extra arguments to cros_bundle_firmware') |
| 227 | parser.add_argument('-b', '--board', type=str, default=default_board, |
| 228 | help='Select board to build (daisy/peach_pit/link)') |
| 229 | parser.add_argument('-B', '--build', action='store_false', default=True, |
| 230 | help="Don't build U-Boot, just configure device tree") |
| 231 | parser.add_argument('-C', '--console', action='store_false', default=True, |
| 232 | help='Permit console output') |
| 233 | parser.add_argument('-d', '--dt', default='seaboard', |
| 234 | help='Select name of device tree file to use') |
| 235 | parser.add_argument('-D', '--nodefaults', dest='use_defaults', |
| 236 | action='store_false', default=True, |
| 237 | help="Don't select default filenames for those not given") |
| 238 | parser.add_argument('-F', '--flash', action='store_true', default=False, |
| 239 | help='Create magic flasher for SPI flash') |
| 240 | parser.add_argument('-M', '--mmc', action='store_true', default=False, |
| 241 | help='Create magic flasher for eMMC') |
| 242 | parser.add_argument('-i', '--incremental', action='store_true', default=False, |
| 243 | help="Don't reconfigure and clean") |
| 244 | parser.add_argument('-k', '--kernel', action='store_true', default=False, |
| 245 | help='Send kernel to board also') |
| 246 | parser.add_argument('-O', '--objdump', action='store_true', default=False, |
| 247 | help='Write disassembly output') |
| 248 | parser.add_argument('-r', '--run', action='store_false', default=True, |
| 249 | help='Run the boot command') |
| 250 | parser.add_argument('--ro', action='store_true', default=False, |
| 251 | help='Create Chrome OS read-only image') |
| 252 | parser.add_argument('--rw', action='store_true', default=False, |
| 253 | help='Create Chrome OS read-write image') |
| 254 | parser.add_argument('-s', '--separate', action='store_false', default=True, |
| 255 | help='Link device tree into U-Boot, instead of separate') |
| 256 | parser.add_argument('-S', '--secure', action='store_true', default=False, |
| 257 | help='Use vboot_twostop secure boot') |
| 258 | parser.add_argument('--small', action='store_true', default=False, |
| 259 | help='Create Chrome OS small image') |
| 260 | parser.add_argument('-t', '--trace', action='store_true', default=False, |
| 261 | help='Enable trace support') |
Mike Frysinger | 4e91d82 | 2015-06-04 02:01:40 -0400 | [diff] [blame] | 262 | parser.add_argument('-V', '--verified', action='store_true', default=False, |
| 263 | help='Include Chrome OS verified boot components') |
| 264 | parser.add_argument('-w', '--write', action='store_false', default=True, |
| 265 | help="Don't write image to board using usb/em100") |
| 266 | parser.add_argument('-x', '--sdcard', action='store_true', default=False, |
| 267 | help='Write to SD card instead of USB/em100') |
| 268 | parser.add_argument('-z', '--size', action='store_true', default=False, |
| 269 | help='Display U-Boot image size') |
Simon Glass | f67f93e | 2016-08-04 20:29:51 -0600 | [diff] [blame] | 270 | parser.add_argument('target', nargs='?', default='all', |
Mike Frysinger | 4e91d82 | 2015-06-04 02:01:40 -0400 | [diff] [blame] | 271 | help='The target to work on') |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 272 | return parser.parse_args(argv) |
| 273 | |
| 274 | |
Simon Glass | c1a323a | 2016-08-04 20:29:51 -0600 | [diff] [blame] | 275 | def FindCompiler(gcc, cros_prefix): |
| 276 | """Look up the compiler for an architecture. |
| 277 | |
| 278 | Args: |
| 279 | gcc: GCC architecture, either 'arm' or 'aarch64' |
| 280 | cros_prefix: Full Chromium OS toolchain prefix |
| 281 | """ |
| 282 | if in_chroot: |
| 283 | # Use the Chromium OS toolchain. |
| 284 | prefix = cros_prefix |
| 285 | else: |
| 286 | prefix = glob.glob('/opt/linaro/gcc-linaro-%s-linux-*/bin/*gcc' % gcc) |
| 287 | if not prefix: |
Simon Glass | d0557f0 | 2016-11-15 08:51:34 -0700 | [diff] [blame] | 288 | cros_build_lib.Die("""Please install an %s toolchain for your machine. |
Simon Glass | c1a323a | 2016-08-04 20:29:51 -0600 | [diff] [blame] | 289 | Install a Linaro toolchain from: |
| 290 | https://launchpad.net/linaro-toolchain-binaries |
Simon Glass | d0557f0 | 2016-11-15 08:51:34 -0700 | [diff] [blame] | 291 | or see cros/commands/cros_chrome_sdk.py.""" % gcc) |
Simon Glass | c1a323a | 2016-08-04 20:29:51 -0600 | [diff] [blame] | 292 | prefix = re.sub('gcc$', '', prefix[0]) |
| 293 | return prefix |
| 294 | |
| 295 | |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 296 | def SetupBuild(options): |
| 297 | """Set up parameters needed for the build. |
| 298 | |
| 299 | This checks the current environment and options and sets up various things |
| 300 | needed for the build, including 'base' which holds the base flags for |
| 301 | passing to the U-Boot Makefile. |
| 302 | |
| 303 | Args: |
| 304 | options: Command line options |
| 305 | |
| 306 | Returns: |
| 307 | Base flags to use for U-Boot, as a list. |
| 308 | """ |
Mike Frysinger | 27e21b7 | 2018-07-12 14:20:21 -0400 | [diff] [blame] | 309 | # pylint: disable=global-statement |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 310 | global arch, board, compiler, family, outdir, smdk, uboard, vendor, verbose |
| 311 | |
| 312 | if not verbose: |
| 313 | verbose = options.verbose != 0 |
| 314 | |
Ralph Nathan | 23a1221 | 2015-03-25 10:27:54 -0700 | [diff] [blame] | 315 | logging.getLogger().setLevel(options.verbose) |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 316 | |
| 317 | Log('Building for %s' % options.board) |
| 318 | |
Simon Glass | 9d9bf94 | 2013-07-10 16:32:42 -0700 | [diff] [blame] | 319 | # Separate out board_variant string: "peach_pit" becomes "peach", "pit". |
| 320 | # But don't mess up upstream boards which use _ in their name. |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 321 | parts = options.board.split('_') |
Simon Glass | 9d9bf94 | 2013-07-10 16:32:42 -0700 | [diff] [blame] | 322 | if parts[0] in ['daisy', 'peach']: |
| 323 | board = parts[0] |
| 324 | else: |
| 325 | board = options.board |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 326 | |
| 327 | # To allow this to be run from 'cros_sdk' |
| 328 | if in_chroot: |
| 329 | os.chdir(os.path.join(src_root, 'third_party', 'u-boot', 'files')) |
| 330 | |
| 331 | base_board = board |
| 332 | |
| 333 | if options.verified: |
| 334 | base_board = 'chromeos_%s' % base_board |
| 335 | |
| 336 | uboard = UBOARDS.get(base_board, base_board) |
| 337 | Log('U-Boot board is %s' % uboard) |
| 338 | |
| 339 | # Pull out some information from the U-Boot boards config file |
| 340 | family = None |
Simon Glass | eb0c535 | 2016-07-20 09:29:34 -0600 | [diff] [blame] | 341 | (PRE_KBUILD, PRE_KCONFIG, KCONFIG) = range(3) |
| 342 | if os.path.exists('MAINTAINERS'): |
| 343 | board_format = PRE_KBUILD |
| 344 | else: |
| 345 | board_format = PRE_KCONFIG |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 346 | with open('boards.cfg') as f: |
| 347 | for line in f: |
Simon Glass | eb0c535 | 2016-07-20 09:29:34 -0600 | [diff] [blame] | 348 | if 'genboardscfg' in line: |
| 349 | board_format = KCONFIG |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 350 | if uboard in line: |
| 351 | if line[0] == '#': |
| 352 | continue |
| 353 | fields = line.split() |
| 354 | if not fields: |
| 355 | continue |
| 356 | arch = fields[1] |
| 357 | fields += [None, None, None] |
Simon Glass | eb0c535 | 2016-07-20 09:29:34 -0600 | [diff] [blame] | 358 | if board_format == PRE_KBUILD: |
| 359 | smdk = fields[3] |
| 360 | vendor = fields[4] |
| 361 | family = fields[5] |
Simon Glass | 5a5a36e | 2016-11-15 08:51:34 -0700 | [diff] [blame] | 362 | target = fields[6] |
Simon Glass | eb0c535 | 2016-07-20 09:29:34 -0600 | [diff] [blame] | 363 | elif board_format in (PRE_KCONFIG, KCONFIG): |
| 364 | smdk = fields[5] |
| 365 | vendor = fields[4] |
| 366 | family = fields[3] |
Simon Glass | 5a5a36e | 2016-11-15 08:51:34 -0700 | [diff] [blame] | 367 | target = fields[0] |
| 368 | |
| 369 | # Make sure this is the right target. |
| 370 | if target == uboard: |
| 371 | break |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 372 | if not arch: |
| 373 | cros_build_lib.Die("Selected board '%s' not found in boards.cfg." % board) |
| 374 | |
| 375 | vboot = os.path.join('build', board, 'usr') |
| 376 | if arch == 'x86': |
| 377 | family = 'em100' |
| 378 | if in_chroot: |
| 379 | compiler = 'i686-pc-linux-gnu-' |
| 380 | else: |
| 381 | compiler = '/opt/i686/bin/i686-unknown-elf-' |
| 382 | elif arch == 'arm': |
Simon Glass | c1a323a | 2016-08-04 20:29:51 -0600 | [diff] [blame] | 383 | compiler = FindCompiler(arch, 'armv7a-cros-linux-gnueabi-') |
| 384 | elif arch == 'aarch64': |
| 385 | compiler = FindCompiler(arch, 'aarch64-cros-linux-gnu-') |
| 386 | # U-Boot builds both arm and aarch64 with the 'arm' architecture. |
| 387 | arch = 'arm' |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 388 | elif arch == 'sandbox': |
| 389 | compiler = '' |
| 390 | else: |
| 391 | cros_build_lib.Die("Selected arch '%s' not supported." % arch) |
| 392 | |
| 393 | if not options.build: |
| 394 | options.incremental = True |
| 395 | |
| 396 | cpus = multiprocessing.cpu_count() |
| 397 | |
Simon Glass | b89ae89 | 2013-07-18 15:23:35 -0600 | [diff] [blame] | 398 | outdir = os.path.join(OUT_DIR, uboard) |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 399 | base = [ |
| 400 | 'make', |
| 401 | '-j%d' % cpus, |
| 402 | 'O=%s' % outdir, |
| 403 | 'ARCH=%s' % arch, |
| 404 | 'CROSS_COMPILE=%s' % compiler, |
| 405 | '--no-print-directory', |
| 406 | 'HOSTSTRIP=true', |
| 407 | 'DEV_TREE_SRC=%s-%s' % (family, options.dt), |
| 408 | 'QEMU_ARCH='] |
| 409 | |
| 410 | if options.verbose < 2: |
| 411 | base.append('-s') |
Simon Glass | 234e5f3 | 2016-07-20 09:11:59 -0600 | [diff] [blame] | 412 | elif options.verbose > 2: |
| 413 | base.append('V=1') |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 414 | |
| 415 | if options.ro and options.rw: |
| 416 | cros_build_lib.Die('Cannot specify both --ro and --rw options') |
| 417 | if options.ro: |
| 418 | base.append('CROS_RO=1') |
| 419 | options.small = True |
| 420 | |
| 421 | if options.rw: |
| 422 | base.append('CROS_RW=1') |
| 423 | options.small = True |
| 424 | |
| 425 | if options.small: |
| 426 | base.append('CROS_SMALL=1') |
| 427 | else: |
| 428 | base.append('CROS_FULL=1') |
| 429 | |
| 430 | if options.verified: |
| 431 | base += [ |
| 432 | 'VBOOT=%s' % vboot, |
| 433 | 'MAKEFLAGS_VBOOT=DEBUG=1', |
| 434 | 'QUIET=1', |
| 435 | 'CFLAGS_EXTRA_VBOOT=-DUNROLL_LOOPS', |
| 436 | 'VBOOT_SOURCE=%s/platform/vboot_reference' % src_root] |
Simon Glass | 9d9bf94 | 2013-07-10 16:32:42 -0700 | [diff] [blame] | 437 | base.append('VBOOT_DEBUG=1') |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 438 | |
| 439 | # Handle the Chrome OS USE_STDINT workaround. Vboot needs <stdint.h> due |
| 440 | # to a recent change, the need for which I didn't fully understand. But |
| 441 | # U-Boot doesn't normally use this. We have added an option to U-Boot to |
| 442 | # enable use of <stdint.h> and without it vboot will fail to build. So we |
| 443 | # need to enable it where ww can. We can't just enable it always since |
| 444 | # that would prevent this script from building other non-Chrome OS boards |
| 445 | # with a different (older) toolchain, or Chrome OS boards without vboot. |
| 446 | # So use USE_STDINT if the toolchain supports it, and not if not. This |
| 447 | # file was originally part of glibc but has recently migrated to the |
| 448 | # compiler so it is reasonable to use it with a stand-alone program like |
| 449 | # U-Boot. At this point the comment has got long enough that we may as |
| 450 | # well include some poetry which seems to be sorely lacking the code base, |
| 451 | # so this is from Ogden Nash: |
| 452 | # To keep your marriage brimming |
| 453 | # With love in the loving cup, |
| 454 | # Whenever you're wrong, admit it; |
| 455 | # Whenever you're right, shut up. |
| 456 | cmd = [CompilerTool('gcc'), '-ffreestanding', '-x', 'c', '-c', '-'] |
Mike Frysinger | 45602c7 | 2019-09-22 02:15:11 -0400 | [diff] [blame] | 457 | result = cros_build_lib.run(cmd, |
| 458 | input='#include <stdint.h>', |
| 459 | capture_output=True, |
| 460 | **kwargs) |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 461 | if result.returncode == 0: |
| 462 | base.append('USE_STDINT=1') |
| 463 | |
Simon Glass | c0a4d0e | 2016-08-10 08:18:27 -0600 | [diff] [blame] | 464 | base.append('BUILD_ROM=1') |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 465 | if options.trace: |
| 466 | base.append('FTRACE=1') |
| 467 | if options.separate: |
| 468 | base.append('DEV_TREE_SEPARATE=1') |
| 469 | |
| 470 | if options.incremental: |
| 471 | # Get the correct board for cros_write_firmware |
Simon Glass | ef7e437 | 2016-07-20 09:38:47 -0600 | [diff] [blame] | 472 | config_mk = '%s/include/autoconf.mk' % outdir |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 473 | if not os.path.exists(config_mk): |
Lann Martin | ffb9516 | 2018-08-28 12:02:54 -0600 | [diff] [blame] | 474 | logging.warning('No build found for %s - dropping -i', board) |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 475 | options.incremental = False |
| 476 | |
Simon Glass | ef7e437 | 2016-07-20 09:38:47 -0600 | [diff] [blame] | 477 | config_mk = 'include/autoconf.mk' |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 478 | if os.path.exists(config_mk): |
Lann Martin | ffb9516 | 2018-08-28 12:02:54 -0600 | [diff] [blame] | 479 | logging.warning("Warning: '%s' exists, try 'make distclean'", config_mk) |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 480 | |
| 481 | # For when U-Boot supports ccache |
| 482 | # See http://patchwork.ozlabs.org/patch/245079/ |
| 483 | if use_ccache: |
| 484 | os.environ['CCACHE'] = 'ccache' |
| 485 | |
| 486 | return base |
| 487 | |
| 488 | |
| 489 | def RunBuild(options, base, target, queue): |
| 490 | """Run the U-Boot build. |
| 491 | |
| 492 | Args: |
| 493 | options: Command line options. |
| 494 | base: Base U-Boot flags. |
| 495 | target: Target to build. |
| 496 | queue: A parallel queue to add jobs to. |
| 497 | """ |
| 498 | Log('U-Boot build flags: %s' % ' '.join(base)) |
| 499 | |
| 500 | # Reconfigure U-Boot. |
| 501 | if not options.incremental: |
| 502 | # Ignore any error from this, some older U-Boots fail on this. |
Mike Frysinger | 45602c7 | 2019-09-22 02:15:11 -0400 | [diff] [blame] | 503 | cros_build_lib.run(base + ['distclean'], **kwargs) |
Simon Glass | 4e28d11 | 2016-11-16 13:52:48 -0700 | [diff] [blame] | 504 | if os.path.exists('tools/genboardscfg.py'): |
| 505 | mtarget = 'defconfig' |
| 506 | else: |
| 507 | mtarget = 'config' |
| 508 | cmd = base + ['%s_%s' % (uboard, mtarget)] |
Mike Frysinger | c988593 | 2020-02-26 01:02:49 -0500 | [diff] [blame] | 509 | result = cros_build_lib.run(cmd, stdout=True, stderr=subprocess.STDOUT, |
| 510 | **kwargs) |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 511 | if result.returncode: |
Mike Frysinger | 7a580c1 | 2022-08-01 21:45:41 -0400 | [diff] [blame] | 512 | print("cmd: '%s', output: '%s'" % (result.cmdstr, result.stdout)) |
Simon Glass | c2fe94c | 2016-11-16 13:56:24 -0700 | [diff] [blame] | 513 | sys.exit(result.returncode) |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 514 | |
| 515 | # Do the actual build. |
| 516 | if options.build: |
Mike Frysinger | c988593 | 2020-02-26 01:02:49 -0500 | [diff] [blame] | 517 | result = cros_build_lib.run(base + [target], stdout=True, |
Mike Frysinger | 66d32cd | 2019-12-17 14:55:29 -0500 | [diff] [blame] | 518 | stderr=subprocess.STDOUT, **kwargs) |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 519 | if result.returncode: |
Simon Glass | 08b3308 | 2017-01-23 12:16:40 -0700 | [diff] [blame] | 520 | # The build failed, so output the results to stderr. |
Mike Frysinger | 7a580c1 | 2022-08-01 21:45:41 -0400 | [diff] [blame] | 521 | print("cmd: '%s', output: '%s'" % (result.cmdstr, result.stdout), |
Simon Glass | 08b3308 | 2017-01-23 12:16:40 -0700 | [diff] [blame] | 522 | file=sys.stderr) |
Simon Glass | c2fe94c | 2016-11-16 13:56:24 -0700 | [diff] [blame] | 523 | sys.exit(result.returncode) |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 524 | |
| 525 | files = ['%s/u-boot' % outdir] |
| 526 | spl = glob.glob('%s/spl/u-boot-spl' % outdir) |
| 527 | if spl: |
| 528 | files += spl |
| 529 | if options.size: |
Mike Frysinger | 45602c7 | 2019-09-22 02:15:11 -0400 | [diff] [blame] | 530 | result = cros_build_lib.run([CompilerTool('size')] + files, **kwargs) |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 531 | if result.returncode: |
| 532 | sys.exit() |
| 533 | |
| 534 | # Create disassembly files .dis and .Dis (full dump) |
| 535 | for f in files: |
| 536 | base = os.path.splitext(f)[0] |
| 537 | if options.objdump: |
| 538 | queue.put(('-d', f, base + '.dis')) |
| 539 | queue.put(('-D', f, base + '.Dis')) |
| 540 | else: |
| 541 | # Remove old files which otherwise might be confusing |
| 542 | osutils.SafeUnlink(base + '.dis') |
| 543 | osutils.SafeUnlink(base + '.Dis') |
| 544 | |
| 545 | Log('Output directory %s' % outdir) |
| 546 | |
| 547 | |
| 548 | def WriteFirmware(options): |
| 549 | """Write firmware to the board. |
| 550 | |
| 551 | This uses cros_bundle_firmware to create a firmware image and write it to |
| 552 | the board. |
| 553 | |
| 554 | Args: |
| 555 | options: Command line options |
| 556 | """ |
| 557 | flash = [] |
| 558 | kernel = [] |
| 559 | run = [] |
| 560 | secure = [] |
| 561 | servo = [] |
| 562 | silent = [] |
| 563 | verbose_arg = [] |
Simon Glass | 4641159 | 2013-07-22 22:35:01 -0600 | [diff] [blame] | 564 | ro_uboot = [] |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 565 | |
| 566 | bl2 = ['--bl2', '%s/spl/%s-spl.bin' % (outdir, smdk)] |
| 567 | |
| 568 | if options.use_defaults: |
| 569 | bl1 = [] |
| 570 | bmpblk = [] |
| 571 | ecro = [] |
| 572 | ecrw = [] |
| 573 | defaults = [] |
| 574 | else: |
| 575 | bl1 = ['--bl1', '##/build/%s/firmware/u-boot.bl1.bin' % options.board] |
| 576 | bmpblk = ['--bmpblk', '##/build/%s/firmware/bmpblk.bin' % options.board] |
| 577 | ecro = ['--ecro', '##/build/%s/firmware/ec.RO.bin' % options.board] |
| 578 | ecrw = ['--ec', '##/build/%s/firmware/ec.RW.bin' % options.board] |
| 579 | defaults = ['-D'] |
| 580 | |
| 581 | if arch == 'x86': |
| 582 | seabios = ['--seabios', |
| 583 | '##/build/%s/firmware/seabios.cbfs' % options.board] |
| 584 | else: |
| 585 | seabios = [] |
| 586 | |
| 587 | if options.sdcard: |
| 588 | dest = 'sd:.' |
| 589 | elif arch == 'x86': |
| 590 | dest = 'em100' |
| 591 | elif arch == 'sandbox': |
| 592 | dest = '' |
| 593 | else: |
| 594 | dest = 'usb' |
| 595 | |
| 596 | port = SERVO_PORT.get(options.board, '') |
| 597 | if port: |
| 598 | servo = ['--servo', '%d' % port] |
| 599 | |
| 600 | if options.flash: |
| 601 | flash = ['-F', 'spi'] |
| 602 | |
Simon Glass | 4641159 | 2013-07-22 22:35:01 -0600 | [diff] [blame] | 603 | # The small builds don't have the command line interpreter so cannot |
| 604 | # run the magic flasher script. So use the standard U-Boot in this |
| 605 | # case. |
| 606 | if options.small: |
Ralph Nathan | 446aee9 | 2015-03-23 14:44:56 -0700 | [diff] [blame] | 607 | logging.warning('Using standard U-Boot as flasher') |
Simon Glass | 4641159 | 2013-07-22 22:35:01 -0600 | [diff] [blame] | 608 | flash += ['-U', '##/build/%s/firmware/u-boot.bin' % options.board] |
| 609 | |
Michael Pratt | c88035d | 2013-07-31 16:27:29 -0700 | [diff] [blame] | 610 | if options.mmc: |
| 611 | flash = ['-F', 'sdmmc'] |
| 612 | |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 613 | if options.verbose: |
| 614 | verbose_arg = ['-v', '%s' % options.verbose] |
| 615 | |
| 616 | if options.secure: |
| 617 | secure += ['--bootsecure', '--bootcmd', 'vboot_twostop'] |
| 618 | |
| 619 | if not options.verified: |
| 620 | # Make a small image, without GBB, etc. |
| 621 | secure.append('-s') |
| 622 | |
| 623 | if options.kernel: |
Brian Norris | 8016b99 | 2021-09-13 15:00:15 -0700 | [diff] [blame] | 624 | kernel = ['--kernel', '##/build/%s/boot/vmlinuz' % options.board] |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 625 | |
| 626 | if not options.console: |
| 627 | silent = ['--add-config-int', 'silent-console', '1'] |
| 628 | |
| 629 | if not options.run: |
| 630 | run = ['--bootcmd', 'none'] |
| 631 | |
| 632 | if arch != 'sandbox' and not in_chroot and servo: |
| 633 | if dest == 'usb': |
Ralph Nathan | 446aee9 | 2015-03-23 14:44:56 -0700 | [diff] [blame] | 634 | logging.warning('Image cannot be written to board') |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 635 | dest = '' |
| 636 | servo = [] |
| 637 | elif dest == 'em100': |
Ralph Nathan | 446aee9 | 2015-03-23 14:44:56 -0700 | [diff] [blame] | 638 | logging.warning('Please reset the board manually to boot firmware') |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 639 | servo = [] |
| 640 | |
| 641 | if not servo: |
Ralph Nathan | 446aee9 | 2015-03-23 14:44:56 -0700 | [diff] [blame] | 642 | logging.warning('(sadly dut-control does not work outside chroot)') |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 643 | |
| 644 | if dest: |
| 645 | dest = ['-w', dest] |
| 646 | else: |
| 647 | dest = [] |
| 648 | |
| 649 | soc = SOCS.get(board) |
| 650 | if not soc: |
| 651 | soc = SOCS.get(uboard, '') |
| 652 | dt_name = DEFAULT_DTS.get(options.board, options.board) |
| 653 | dts_file = 'board/%s/dts/%s%s.dts' % (vendor, soc, dt_name) |
| 654 | Log('Device tree: %s' % dts_file) |
| 655 | |
| 656 | if arch == 'sandbox': |
| 657 | uboot_fname = '%s/u-boot' % outdir |
| 658 | else: |
| 659 | uboot_fname = '%s/u-boot.bin' % outdir |
| 660 | |
Simon Glass | 4641159 | 2013-07-22 22:35:01 -0600 | [diff] [blame] | 661 | if options.ro: |
| 662 | # RO U-Boot is passed through as blob 'ro-boot'. We use the standard |
| 663 | # ebuild one as RW. |
| 664 | # TODO(sjg@chromium.org): Option to build U-Boot a second time to get |
| 665 | # a fresh RW U-Boot. |
Ralph Nathan | 446aee9 | 2015-03-23 14:44:56 -0700 | [diff] [blame] | 666 | logging.warning('Using standard U-Boot for RW') |
Simon Glass | 4641159 | 2013-07-22 22:35:01 -0600 | [diff] [blame] | 667 | ro_uboot = ['--add-blob', 'ro-boot', uboot_fname] |
| 668 | uboot_fname = '##/build/%s/firmware/u-boot.bin' % options.board |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 669 | cbf = ['%s/platform/dev/host/cros_bundle_firmware' % src_root, |
| 670 | '-b', options.board, |
| 671 | '-d', dts_file, |
| 672 | '-I', 'arch/%s/dts' % arch, '-I', 'cros/dts', |
| 673 | '-u', uboot_fname, |
| 674 | '-O', '%s/out' % outdir, |
| 675 | '-M', family] |
| 676 | |
| 677 | for other in [bl1, bl2, bmpblk, defaults, dest, ecro, ecrw, flash, kernel, |
Simon Glass | 4641159 | 2013-07-22 22:35:01 -0600 | [diff] [blame] | 678 | run, seabios, secure, servo, silent, verbose_arg, ro_uboot]: |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 679 | if other: |
| 680 | cbf += other |
| 681 | if options.cbfargs: |
| 682 | for item in options.cbfargs: |
| 683 | cbf += item.split(' ') |
| 684 | os.environ['PYTHONPATH'] = ('%s/platform/dev/host/lib:%s/..' % |
| 685 | (src_root, src_root)) |
| 686 | Log(' '.join(cbf)) |
Mike Frysinger | 45602c7 | 2019-09-22 02:15:11 -0400 | [diff] [blame] | 687 | result = cros_build_lib.run(cbf, **kwargs) |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 688 | if result.returncode: |
| 689 | cros_build_lib.Die('cros_bundle_firmware failed') |
| 690 | |
| 691 | if not dest or not result.returncode: |
Lann Martin | ffb9516 | 2018-08-28 12:02:54 -0600 | [diff] [blame] | 692 | logging.info('Image is available at %s/out/image.bin', outdir) |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 693 | else: |
| 694 | if result.returncode: |
| 695 | cros_build_lib.Die('Failed to write image to board') |
| 696 | else: |
Lann Martin | ffb9516 | 2018-08-28 12:02:54 -0600 | [diff] [blame] | 697 | logging.info('Image written to board with %s', ' '.join(dest + servo)) |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 698 | |
| 699 | |
| 700 | def main(argv): |
| 701 | """Main function for script to build/write firmware. |
| 702 | |
| 703 | Args: |
| 704 | argv: Program arguments. |
| 705 | """ |
Mike Frysinger | 4e91d82 | 2015-06-04 02:01:40 -0400 | [diff] [blame] | 706 | options = ParseCmdline(argv) |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 707 | base = SetupBuild(options) |
| 708 | |
| 709 | with parallel.BackgroundTaskRunner(Dumper) as queue: |
Mike Frysinger | 4e91d82 | 2015-06-04 02:01:40 -0400 | [diff] [blame] | 710 | RunBuild(options, base, options.target, queue) |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 711 | |
| 712 | if options.write: |
| 713 | WriteFirmware(options) |
| 714 | |
| 715 | if options.objdump: |
| 716 | Log('Writing diasssembly files') |