Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2013 The ChromiumOS Authors |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 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 | |
Simon Glass | 77cc6ea | 2023-02-12 08:04:43 -0700 | [diff] [blame] | 23 | crosfw link |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 24 | |
Simon Glass | 77cc6ea | 2023-02-12 08:04:43 -0700 | [diff] [blame] | 25 | Example 2: Build verified boot image (V) for daisy/snow. |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 26 | |
Simon Glass | 77cc6ea | 2023-02-12 08:04:43 -0700 | [diff] [blame] | 27 | crosfw daisy -V |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 28 | |
Simon Glass | 77cc6ea | 2023-02-12 08:04:43 -0700 | [diff] [blame] | 29 | You can force a reconfigure with -f and a full distclean with -F. |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 30 | |
Simon Glass | 77cc6ea | 2023-02-12 08:04:43 -0700 | [diff] [blame] | 31 | To increase verbosity use the -v and --debug options. |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 32 | |
Simon Glass | 77cc6ea | 2023-02-12 08:04:43 -0700 | [diff] [blame] | 33 | This script does not use an ebuild. It does a similar thing to the |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 34 | chromeos-u-boot ebuild, and runs cros_bundle_firmware to produce various |
| 35 | types of image, a little like the chromeos-bootimage ebuild. |
| 36 | |
| 37 | The purpose of this script is to make it easier and faster to perform |
| 38 | common firmware build tasks without changing boards, manually updating |
| 39 | device tree files or lots of USE flags and complexity in the ebuilds. |
| 40 | |
| 41 | This script has been tested with snow, link and peach_pit. It builds for |
| 42 | peach_pit by default. Note that it will also build any upstream ARM |
Simon Glass | 77cc6ea | 2023-02-12 08:04:43 -0700 | [diff] [blame] | 43 | board - e.g. "snapper9260" will build an image for that board. |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 44 | |
| 45 | Mostly you can use the script inside and outside the chroot. The main |
| 46 | limitation is that dut-control doesn't really work outside the chroot, |
| 47 | so writing the image to the board over USB is not possible, nor can the |
| 48 | board be automatically reset on x86 platforms. |
| 49 | |
| 50 | For an incremental build (faster), run with -i |
| 51 | |
| 52 | To get faster clean builds, install ccache, and create ~/.crosfwrc with |
| 53 | this line: |
| 54 | |
Simon Glass | 6ddc7f1 | 2013-07-18 15:22:41 -0600 | [diff] [blame] | 55 | USE_CCACHE = True |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 56 | |
| 57 | (make sure ~/.ccache is not on NFS, or set CCACHE_DIR) |
| 58 | |
| 59 | Other options are the default board to build, and verbosity (0-4), e.g.: |
| 60 | |
Simon Glass | 6ddc7f1 | 2013-07-18 15:22:41 -0600 | [diff] [blame] | 61 | DEFAULT_BOARD = 'daisy' |
| 62 | VERBOSE = 1 |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 63 | |
| 64 | It is possible to use multiple servo boards, each on its own port. Add |
| 65 | these lines to your ~/.crosfwrc to set the servo port to use for each |
| 66 | board: |
| 67 | |
| 68 | SERVO_PORT['link'] = 8888 |
| 69 | SERVO_PORT['daisy'] = 9999 |
| 70 | SERVO_PORT['peach_pit'] = 7777 |
| 71 | |
Simon Glass | b89ae89 | 2013-07-18 15:23:35 -0600 | [diff] [blame] | 72 | All builds appear in the <outdir>/<board> subdirectory and images are written |
| 73 | to <outdir>/<uboard>/out, where <uboard> is the U-Boot name for the board (in |
| 74 | the U-Boot boards.cfg file) |
| 75 | |
| 76 | The value for <outdir> defaults to /tmp/crosfw but can be configured in your |
| 77 | ~/.crosfwrc file, e.g.:" |
| 78 | |
| 79 | OUT_DIR = '/tmp/u-boot' |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 80 | |
| 81 | For the -a option here are some useful options: |
| 82 | |
| 83 | --add-blob cros-splash /dev/null |
| 84 | --gbb-flags -force-dev-switch-on |
| 85 | --add-node-enable /spi@131b0000/cros-ecp@0 1 |
| 86 | --verify --full-erase |
| 87 | --bootcmd "cros_test sha" |
| 88 | --gbb-flags -force-dev-switch-on |
Mike Frysinger | 0246c1f | 2021-12-14 01:17:17 -0500 | [diff] [blame] | 89 | --bmpblk ~/chromiumos/src/third_party/u-boot/bmp.bin |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 90 | |
| 91 | For example: -a "--gbb-flags -force-dev-switch-on" |
| 92 | |
| 93 | Note the standard bmpblk is at: |
Mike Frysinger | 0246c1f | 2021-12-14 01:17:17 -0500 | [diff] [blame] | 94 | ~/chromiumos/src/third_party/chromiumos-overlay/sys-boot/ |
| 95 | chromeos-bootimage/files/bmpblk.bin |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 96 | """ |
| 97 | |
| 98 | import glob |
Chris McDonald | 59650c3 | 2021-07-20 15:29:28 -0600 | [diff] [blame] | 99 | import logging |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 100 | import multiprocessing |
| 101 | import os |
Simon Glass | f78c9b5 | 2023-02-14 06:58:10 -0700 | [diff] [blame] | 102 | from pathlib import Path |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 103 | import re |
Mike Frysinger | 66d32cd | 2019-12-17 14:55:29 -0500 | [diff] [blame] | 104 | import subprocess |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 105 | import sys |
| 106 | |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 107 | from chromite.lib import commandline |
Chris McDonald | 59650c3 | 2021-07-20 15:29:28 -0600 | [diff] [blame] | 108 | from chromite.lib import constants |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 109 | from chromite.lib import cros_build_lib |
| 110 | from chromite.lib import osutils |
| 111 | from chromite.lib import parallel |
| 112 | |
| 113 | |
| 114 | arch = None |
| 115 | board = None |
| 116 | compiler = None |
| 117 | default_board = None |
| 118 | family = None |
| 119 | in_chroot = True |
| 120 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 121 | kwargs = { |
| 122 | "print_cmd": False, |
| 123 | "check": False, |
Simon Glass | dee7d6b | 2023-02-10 11:35:18 -0700 | [diff] [blame] | 124 | "encoding": "utf-8", |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 125 | } |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 126 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 127 | outdir = "" |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 128 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 129 | # If you have multiple boards connected on different servo ports, put lines |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 130 | # like 'SERVO_PORT{"peach_pit"} = 7777' in your ~/.crosfwrc |
| 131 | SERVO_PORT = {} |
| 132 | |
| 133 | smdk = None |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 134 | src_root = os.path.join(constants.SOURCE_ROOT, "src") |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 135 | in_chroot = cros_build_lib.IsInsideChroot() |
| 136 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 137 | uboard = "" |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 138 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 139 | default_board = "peach_pit" |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 140 | use_ccache = False |
| 141 | vendor = None |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 142 | |
| 143 | # Special cases for the U-Boot board config, the SOCs and default device tree |
| 144 | # since the naming is not always consistent. |
| 145 | # x86 has a lot of boards, but to U-Boot they are all the same |
| 146 | UBOARDS = { |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 147 | "daisy": "smdk5250", |
| 148 | "peach": "smdk5420", |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 149 | } |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 150 | for b in [ |
| 151 | "alex", |
| 152 | "butterfly", |
| 153 | "emeraldlake2", |
| 154 | "link", |
| 155 | "lumpy", |
| 156 | "parrot", |
| 157 | "stout", |
| 158 | "stumpy", |
| 159 | ]: |
| 160 | UBOARDS[b] = "coreboot-x86" |
| 161 | UBOARDS["chromeos_%s" % b] = "chromeos_coreboot" |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 162 | |
| 163 | SOCS = { |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 164 | "coreboot-x86": "", |
| 165 | "chromeos_coreboot": "", |
| 166 | "daisy": "exynos5250-", |
| 167 | "peach": "exynos5420-", |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | DEFAULT_DTS = { |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 171 | "daisy": "snow", |
| 172 | "daisy_spring": "spring", |
| 173 | "peach_pit": "peach-pit", |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 176 | OUT_DIR = "/tmp/crosfw" |
Simon Glass | b89ae89 | 2013-07-18 15:23:35 -0600 | [diff] [blame] | 177 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 178 | rc_file = os.path.expanduser("~/.crosfwrc") |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 179 | if os.path.exists(rc_file): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 180 | with open(rc_file) as fp: |
| 181 | # pylint: disable=exec-used |
| 182 | exec(compile(fp.read(), rc_file, "exec")) |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 183 | |
| 184 | |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 185 | def Dumper(flag, infile, outfile): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 186 | """Run objdump on an input file. |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 187 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 188 | Args: |
| 189 | flag: Flag to pass objdump (e.g. '-d'). |
| 190 | infile: Input file to process. |
| 191 | outfile: Output file to write to. |
| 192 | """ |
| 193 | result = cros_build_lib.run( |
| 194 | [CompilerTool("objdump"), flag, infile], stdout=outfile, **kwargs |
| 195 | ) |
| 196 | if result.returncode: |
| 197 | sys.exit() |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 198 | |
| 199 | |
| 200 | def CompilerTool(tool): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 201 | """Returns the cross-compiler tool filename. |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 202 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 203 | Args: |
| 204 | tool: Tool name to return, e.g. 'size'. |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 205 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 206 | Returns: |
| 207 | Filename of requested tool. |
| 208 | """ |
| 209 | return "%s%s" % (compiler, tool) |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 210 | |
| 211 | |
| 212 | def ParseCmdline(argv): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 213 | """Parse all command line options. |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 214 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 215 | Args: |
| 216 | argv: Arguments to parse. |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 217 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 218 | Returns: |
| 219 | The parsed options object |
| 220 | """ |
Simon Glass | 95a316d | 2023-02-10 11:31:47 -0700 | [diff] [blame] | 221 | parser = commandline.ArgumentParser( |
| 222 | description=__doc__, default_log_level="notice" |
| 223 | ) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 224 | parser.add_argument( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 225 | "-B", |
| 226 | "--build", |
| 227 | action="store_false", |
| 228 | default=True, |
| 229 | help="Don't build U-Boot, just configure device tree", |
| 230 | ) |
| 231 | parser.add_argument( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 232 | "-d", |
| 233 | "--dt", |
| 234 | default="seaboard", |
| 235 | help="Select name of device tree file to use", |
| 236 | ) |
| 237 | parser.add_argument( |
Simon Glass | 77cc6ea | 2023-02-12 08:04:43 -0700 | [diff] [blame] | 238 | "-f", |
| 239 | "--force-reconfig", |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 240 | action="store_true", |
| 241 | default=False, |
Simon Glass | 77cc6ea | 2023-02-12 08:04:43 -0700 | [diff] [blame] | 242 | help="Reconfigure before building", |
| 243 | ) |
| 244 | parser.add_argument( |
| 245 | "-F", |
| 246 | "--force-distclean", |
| 247 | action="store_true", |
| 248 | default=False, |
| 249 | help="Run distclean and reconfigure before building", |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 250 | ) |
| 251 | parser.add_argument( |
Simon Glass | d080aa7 | 2023-02-14 07:04:52 -0700 | [diff] [blame^] | 252 | "-L", |
| 253 | "--no-lto", |
| 254 | dest="lto", |
| 255 | action="store_false", |
| 256 | default=True, |
| 257 | help="Disable Link-time Optimisation (LTO) when building", |
| 258 | ) |
| 259 | parser.add_argument( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 260 | "-O", |
| 261 | "--objdump", |
| 262 | action="store_true", |
| 263 | default=False, |
| 264 | help="Write disassembly output", |
| 265 | ) |
| 266 | parser.add_argument( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 267 | "-t", |
| 268 | "--trace", |
| 269 | action="store_true", |
| 270 | default=False, |
| 271 | help="Enable trace support", |
| 272 | ) |
| 273 | parser.add_argument( |
Simon Glass | a68488c | 2023-02-09 16:53:47 -0700 | [diff] [blame] | 274 | "-T", |
| 275 | "--target", |
| 276 | nargs="?", |
| 277 | default="all", |
| 278 | help="Select target to build", |
| 279 | ) |
| 280 | parser.add_argument( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 281 | "-V", |
| 282 | "--verified", |
| 283 | action="store_true", |
| 284 | default=False, |
| 285 | help="Include Chrome OS verified boot components", |
| 286 | ) |
| 287 | parser.add_argument( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 288 | "-z", |
| 289 | "--size", |
| 290 | action="store_true", |
| 291 | default=False, |
| 292 | help="Display U-Boot image size", |
| 293 | ) |
| 294 | parser.add_argument( |
Simon Glass | a68488c | 2023-02-09 16:53:47 -0700 | [diff] [blame] | 295 | "board", |
| 296 | type=str, |
| 297 | default=default_board, |
| 298 | help="Select board to build (daisy/peach_pit/link)", |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 299 | ) |
| 300 | return parser.parse_args(argv) |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 301 | |
| 302 | |
Simon Glass | c1a323a | 2016-08-04 20:29:51 -0600 | [diff] [blame] | 303 | def FindCompiler(gcc, cros_prefix): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 304 | """Look up the compiler for an architecture. |
Simon Glass | c1a323a | 2016-08-04 20:29:51 -0600 | [diff] [blame] | 305 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 306 | Args: |
| 307 | gcc: GCC architecture, either 'arm' or 'aarch64' |
| 308 | cros_prefix: Full Chromium OS toolchain prefix |
| 309 | """ |
| 310 | if in_chroot: |
| 311 | # Use the Chromium OS toolchain. |
| 312 | prefix = cros_prefix |
| 313 | else: |
| 314 | prefix = glob.glob("/opt/linaro/gcc-linaro-%s-linux-*/bin/*gcc" % gcc) |
| 315 | if not prefix: |
| 316 | cros_build_lib.Die( |
| 317 | """Please install an %s toolchain for your machine. |
Simon Glass | c1a323a | 2016-08-04 20:29:51 -0600 | [diff] [blame] | 318 | Install a Linaro toolchain from: |
| 319 | https://launchpad.net/linaro-toolchain-binaries |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 320 | or see cros/commands/cros_chrome_sdk.py.""" |
| 321 | % gcc |
| 322 | ) |
| 323 | prefix = re.sub("gcc$", "", prefix[0]) |
| 324 | return prefix |
Simon Glass | c1a323a | 2016-08-04 20:29:51 -0600 | [diff] [blame] | 325 | |
| 326 | |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 327 | def SetupBuild(options): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 328 | """Set up parameters needed for the build. |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 329 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 330 | This checks the current environment and options and sets up various things |
| 331 | needed for the build, including 'base' which holds the base flags for |
| 332 | passing to the U-Boot Makefile. |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 333 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 334 | Args: |
| 335 | options: Command line options |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 336 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 337 | Returns: |
| 338 | Base flags to use for U-Boot, as a list. |
| 339 | """ |
| 340 | # pylint: disable=global-statement |
Simon Glass | 95a316d | 2023-02-10 11:31:47 -0700 | [diff] [blame] | 341 | global arch, board, compiler, family, outdir, smdk, uboard, vendor |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 342 | |
Simon Glass | 9674afc | 2023-02-10 11:21:30 -0700 | [diff] [blame] | 343 | logging.info("Building for %s", options.board) |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 344 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 345 | # Separate out board_variant string: "peach_pit" becomes "peach", "pit". |
| 346 | # But don't mess up upstream boards which use _ in their name. |
| 347 | parts = options.board.split("_") |
| 348 | if parts[0] in ["daisy", "peach"]: |
| 349 | board = parts[0] |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 350 | else: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 351 | board = options.board |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 352 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 353 | # To allow this to be run from 'cros_sdk' |
| 354 | if in_chroot: |
| 355 | os.chdir(os.path.join(src_root, "third_party", "u-boot", "files")) |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 356 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 357 | base_board = board |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 358 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 359 | if options.verified: |
| 360 | base_board = "chromeos_%s" % base_board |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 361 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 362 | uboard = UBOARDS.get(base_board, base_board) |
Simon Glass | 9674afc | 2023-02-10 11:21:30 -0700 | [diff] [blame] | 363 | logging.info("U-Boot board is %s", uboard) |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 364 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 365 | # Pull out some information from the U-Boot boards config file |
| 366 | family = None |
| 367 | (PRE_KBUILD, PRE_KCONFIG, KCONFIG) = range(3) |
| 368 | if os.path.exists("MAINTAINERS"): |
| 369 | board_format = PRE_KBUILD |
| 370 | else: |
| 371 | board_format = PRE_KCONFIG |
| 372 | with open("boards.cfg") as f: |
| 373 | for line in f: |
| 374 | if "genboardscfg" in line: |
| 375 | board_format = KCONFIG |
| 376 | if uboard in line: |
| 377 | if line[0] == "#": |
| 378 | continue |
| 379 | fields = line.split() |
| 380 | if not fields: |
| 381 | continue |
Simon Glass | a44a1f9 | 2023-02-09 16:31:34 -0700 | [diff] [blame] | 382 | target = fields[6] |
| 383 | # Make sure this is the right target. |
| 384 | if target != uboard: |
| 385 | continue |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 386 | arch = fields[1] |
| 387 | fields += [None, None, None] |
| 388 | if board_format == PRE_KBUILD: |
| 389 | smdk = fields[3] |
| 390 | vendor = fields[4] |
| 391 | family = fields[5] |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 392 | elif board_format in (PRE_KCONFIG, KCONFIG): |
| 393 | smdk = fields[5] |
| 394 | vendor = fields[4] |
| 395 | family = fields[3] |
| 396 | target = fields[0] |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 397 | if not arch: |
| 398 | cros_build_lib.Die( |
| 399 | "Selected board '%s' not found in boards.cfg." % board |
| 400 | ) |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 401 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 402 | vboot = os.path.join("build", board, "usr") |
Simon Glass | a44a1f9 | 2023-02-09 16:31:34 -0700 | [diff] [blame] | 403 | if in_chroot: |
| 404 | if arch == "x86": |
| 405 | compiler = "i686-cros-linux-gnu-" |
| 406 | elif arch == "arm": |
| 407 | compiler = FindCompiler(arch, "armv7a-cros-linux-gnueabihf-") |
| 408 | elif arch == "aarch64": |
| 409 | compiler = FindCompiler(arch, "aarch64-cros-linux-gnu-") |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 410 | elif arch == "sandbox": |
| 411 | compiler = "" |
| 412 | else: |
Simon Glass | a44a1f9 | 2023-02-09 16:31:34 -0700 | [diff] [blame] | 413 | result = cros_build_lib.run( |
| 414 | ["buildman", "-A", "--boards", options.board], |
| 415 | capture_output=True, |
Simon Glass | a44a1f9 | 2023-02-09 16:31:34 -0700 | [diff] [blame] | 416 | **kwargs, |
| 417 | ) |
| 418 | compiler = result.stdout.strip() |
| 419 | if not compiler: |
| 420 | cros_build_lib.Die("Selected arch '%s' not supported.", arch) |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 421 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 422 | cpus = multiprocessing.cpu_count() |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 423 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 424 | outdir = os.path.join(OUT_DIR, uboard) |
| 425 | base = [ |
| 426 | "make", |
| 427 | "-j%d" % cpus, |
| 428 | "O=%s" % outdir, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 429 | "CROSS_COMPILE=%s" % compiler, |
| 430 | "--no-print-directory", |
| 431 | "HOSTSTRIP=true", |
| 432 | "DEV_TREE_SRC=%s-%s" % (family, options.dt), |
| 433 | "QEMU_ARCH=", |
| 434 | ] |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 435 | |
Simon Glass | d080aa7 | 2023-02-14 07:04:52 -0700 | [diff] [blame^] | 436 | if not options.lto: |
| 437 | base.append("NO_LTO=1") |
| 438 | |
Simon Glass | 95a316d | 2023-02-10 11:31:47 -0700 | [diff] [blame] | 439 | # Enable quiet output at INFO level, everything at DEBUG level |
| 440 | if logging.getLogger().getEffectiveLevel() <= logging.DEBUG: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 441 | base.append("V=1") |
Simon Glass | 95a316d | 2023-02-10 11:31:47 -0700 | [diff] [blame] | 442 | elif logging.getLogger().getEffectiveLevel() >= logging.NOTICE: |
| 443 | base.append("-s") |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 444 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 445 | if options.verified: |
| 446 | base += [ |
| 447 | "VBOOT=%s" % vboot, |
| 448 | "MAKEFLAGS_VBOOT=DEBUG=1", |
| 449 | "QUIET=1", |
| 450 | "CFLAGS_EXTRA_VBOOT=-DUNROLL_LOOPS", |
| 451 | "VBOOT_SOURCE=%s/platform/vboot_reference" % src_root, |
| 452 | ] |
| 453 | base.append("VBOOT_DEBUG=1") |
| 454 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 455 | base.append("BUILD_ROM=1") |
| 456 | if options.trace: |
| 457 | base.append("FTRACE=1") |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 458 | |
Simon Glass | 77cc6ea | 2023-02-12 08:04:43 -0700 | [diff] [blame] | 459 | if not options.force_reconfig: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 460 | config_mk = "%s/include/autoconf.mk" % outdir |
| 461 | if not os.path.exists(config_mk): |
Simon Glass | 77cc6ea | 2023-02-12 08:04:43 -0700 | [diff] [blame] | 462 | logging.warning("No build found for %s - adding -f", board) |
| 463 | options.force_reconfig = True |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 464 | |
| 465 | config_mk = "include/autoconf.mk" |
| 466 | if os.path.exists(config_mk): |
| 467 | logging.warning("Warning: '%s' exists, try 'make distclean'", config_mk) |
| 468 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 469 | return base |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 470 | |
| 471 | |
Simon Glass | f78c9b5 | 2023-02-14 06:58:10 -0700 | [diff] [blame] | 472 | def CheckConfigChange() -> bool: |
| 473 | """See if we need to reconfigure due to config files changing |
| 474 | |
| 475 | Checks if any defconfig or Kconfig file has changed in the source tree |
| 476 | since the last time U-Boot was configured for this build. For simplicity, |
| 477 | any defconfig change will trigger this, not just one for the board being |
| 478 | built, since the cost of a reconfigure is fairly small. |
| 479 | |
| 480 | Returns: |
| 481 | True if any config file has changed since U-Boot was last configured |
| 482 | """ |
| 483 | fname = os.path.join(outdir, ".config") |
| 484 | ref_time = os.path.getctime(fname) |
| 485 | for p in Path.cwd().glob("configs/*"): |
| 486 | if p.stat().st_ctime > ref_time: |
| 487 | logging.warning("config/ dir has changed - adding -f") |
| 488 | return True |
| 489 | |
| 490 | for p in Path.cwd().glob("**/Kconfig*"): |
| 491 | if p.stat().st_ctime > ref_time: |
| 492 | logging.warning("Kconfig file(s) changed - adding -f") |
| 493 | return True |
| 494 | |
| 495 | return False |
| 496 | |
| 497 | |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 498 | def RunBuild(options, base, target, queue): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 499 | """Run the U-Boot build. |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 500 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 501 | Args: |
| 502 | options: Command line options. |
| 503 | base: Base U-Boot flags. |
| 504 | target: Target to build. |
| 505 | queue: A parallel queue to add jobs to. |
| 506 | """ |
Simon Glass | 9674afc | 2023-02-10 11:21:30 -0700 | [diff] [blame] | 507 | logging.info("U-Boot build flags: %s", " ".join(base)) |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 508 | |
Simon Glass | 77cc6ea | 2023-02-12 08:04:43 -0700 | [diff] [blame] | 509 | if options.force_distclean: |
| 510 | options.force_reconfig = True |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 511 | # Ignore any error from this, some older U-Boots fail on this. |
Simon Glass | dee7d6b | 2023-02-10 11:35:18 -0700 | [diff] [blame] | 512 | cros_build_lib.run(base + ["distclean"], capture_output=True, **kwargs) |
Simon Glass | 77cc6ea | 2023-02-12 08:04:43 -0700 | [diff] [blame] | 513 | |
Simon Glass | f78c9b5 | 2023-02-14 06:58:10 -0700 | [diff] [blame] | 514 | if not options.force_reconfig: |
| 515 | options.force_reconfig = CheckConfigChange() |
| 516 | |
Simon Glass | 77cc6ea | 2023-02-12 08:04:43 -0700 | [diff] [blame] | 517 | # Reconfigure U-Boot. |
| 518 | if options.force_reconfig: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 519 | if os.path.exists("tools/genboardscfg.py"): |
| 520 | mtarget = "defconfig" |
| 521 | else: |
| 522 | mtarget = "config" |
| 523 | cmd = base + ["%s_%s" % (uboard, mtarget)] |
| 524 | result = cros_build_lib.run( |
| 525 | cmd, stdout=True, stderr=subprocess.STDOUT, **kwargs |
| 526 | ) |
Simon Glass | dee7d6b | 2023-02-10 11:35:18 -0700 | [diff] [blame] | 527 | if ( |
| 528 | result.returncode |
| 529 | or logging.getLogger().getEffectiveLevel() <= logging.DEBUG |
| 530 | ): |
| 531 | print(f"cmd: {result.cmdstr}") |
| 532 | print(result.stdout, file=sys.stderr) |
| 533 | if result.returncode: |
| 534 | sys.exit(result.returncode) |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 535 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 536 | # Do the actual build. |
| 537 | if options.build: |
| 538 | result = cros_build_lib.run( |
| 539 | base + [target], stdout=True, stderr=subprocess.STDOUT, **kwargs |
| 540 | ) |
Simon Glass | dee7d6b | 2023-02-10 11:35:18 -0700 | [diff] [blame] | 541 | if ( |
| 542 | result.returncode |
| 543 | or logging.getLogger().getEffectiveLevel() <= logging.INFO |
| 544 | ): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 545 | # The build failed, so output the results to stderr. |
Simon Glass | dee7d6b | 2023-02-10 11:35:18 -0700 | [diff] [blame] | 546 | print(f"cmd: {result.cmdstr}") |
| 547 | print(result.stdout, file=sys.stderr) |
| 548 | if result.returncode: |
| 549 | sys.exit(result.returncode) |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 550 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 551 | files = ["%s/u-boot" % outdir] |
| 552 | spl = glob.glob("%s/spl/u-boot-spl" % outdir) |
| 553 | if spl: |
| 554 | files += spl |
| 555 | if options.size: |
| 556 | result = cros_build_lib.run([CompilerTool("size")] + files, **kwargs) |
| 557 | if result.returncode: |
| 558 | sys.exit() |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 559 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 560 | # Create disassembly files .dis and .Dis (full dump) |
| 561 | for f in files: |
| 562 | base = os.path.splitext(f)[0] |
| 563 | if options.objdump: |
| 564 | queue.put(("-d", f, base + ".dis")) |
| 565 | queue.put(("-D", f, base + ".Dis")) |
| 566 | else: |
| 567 | # Remove old files which otherwise might be confusing |
| 568 | osutils.SafeUnlink(base + ".dis") |
| 569 | osutils.SafeUnlink(base + ".Dis") |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 570 | |
Simon Glass | 9674afc | 2023-02-10 11:21:30 -0700 | [diff] [blame] | 571 | logging.info("Output directory %s", outdir) |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 572 | |
| 573 | |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 574 | def main(argv): |
Simon Glass | e6201d7 | 2023-02-09 16:42:22 -0700 | [diff] [blame] | 575 | """Main function for script to build firmware. |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 576 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 577 | Args: |
| 578 | argv: Program arguments. |
| 579 | """ |
| 580 | options = ParseCmdline(argv) |
| 581 | base = SetupBuild(options) |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 582 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 583 | with parallel.BackgroundTaskRunner(Dumper) as queue: |
| 584 | RunBuild(options, base, options.target, queue) |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 585 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 586 | if options.objdump: |
Simon Glass | 9674afc | 2023-02-10 11:21:30 -0700 | [diff] [blame] | 587 | logging.info("Writing diasssembly files") |