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 os |
Simon Glass | f78c9b5 | 2023-02-14 06:58:10 -0700 | [diff] [blame] | 101 | from pathlib import Path |
Mike Frysinger | 66d32cd | 2019-12-17 14:55:29 -0500 | [diff] [blame] | 102 | import subprocess |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 103 | import sys |
Simon Glass | 313cc8a | 2023-06-21 10:24:39 +0100 | [diff] [blame] | 104 | from typing import Any, Dict, List |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 105 | |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 106 | from chromite.lib import commandline |
Chris McDonald | 59650c3 | 2021-07-20 15:29:28 -0600 | [diff] [blame] | 107 | from chromite.lib import constants |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 108 | from chromite.lib import cros_build_lib |
| 109 | from chromite.lib import osutils |
| 110 | from chromite.lib import parallel |
| 111 | |
| 112 | |
| 113 | arch = None |
| 114 | board = None |
| 115 | compiler = None |
| 116 | default_board = None |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 117 | in_chroot = True |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 118 | outdir = "" |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 119 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 120 | # If you have multiple boards connected on different servo ports, put lines |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 121 | # like 'SERVO_PORT{"peach_pit"} = 7777' in your ~/.crosfwrc |
| 122 | SERVO_PORT = {} |
| 123 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 124 | src_root = os.path.join(constants.SOURCE_ROOT, "src") |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 125 | in_chroot = cros_build_lib.IsInsideChroot() |
| 126 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 127 | uboard = "" |
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 | default_board = "peach_pit" |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 130 | use_ccache = False |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 131 | |
| 132 | # Special cases for the U-Boot board config, the SOCs and default device tree |
| 133 | # since the naming is not always consistent. |
| 134 | # x86 has a lot of boards, but to U-Boot they are all the same |
| 135 | UBOARDS = { |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 136 | "daisy": "smdk5250", |
| 137 | "peach": "smdk5420", |
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 | for b in [ |
| 140 | "alex", |
| 141 | "butterfly", |
| 142 | "emeraldlake2", |
| 143 | "link", |
| 144 | "lumpy", |
| 145 | "parrot", |
| 146 | "stout", |
| 147 | "stumpy", |
| 148 | ]: |
| 149 | UBOARDS[b] = "coreboot-x86" |
| 150 | UBOARDS["chromeos_%s" % b] = "chromeos_coreboot" |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 151 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 152 | OUT_DIR = "/tmp/crosfw" |
Simon Glass | b89ae89 | 2013-07-18 15:23:35 -0600 | [diff] [blame] | 153 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 154 | rc_file = os.path.expanduser("~/.crosfwrc") |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 155 | if os.path.exists(rc_file): |
Mike Frysinger | 31fdddd | 2023-02-24 15:50:55 -0500 | [diff] [blame] | 156 | with open(rc_file, "rb") as fp: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 157 | # pylint: disable=exec-used |
| 158 | exec(compile(fp.read(), rc_file, "exec")) |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 159 | |
| 160 | |
Simon Glass | 313cc8a | 2023-06-21 10:24:39 +0100 | [diff] [blame] | 161 | def run(cmd: List[str], **kwargs: Dict[Any, Any]): |
| 162 | """Run a command with the common settings. |
| 163 | |
| 164 | Args: |
| 165 | cmd: Command + arguments to run. |
| 166 | **kwargs: keyword arguments to pass on to cros_build_lib.run(). |
| 167 | |
| 168 | Returns: |
| 169 | A CompletedProcess object. |
| 170 | """ |
| 171 | kwargs.setdefault("print_cmd", False) |
| 172 | kwargs.setdefault("check", False) |
| 173 | kwargs.setdefault("encoding", "utf-8") |
| 174 | return cros_build_lib.run(cmd, **kwargs) |
| 175 | |
| 176 | |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 177 | def Dumper(flag, infile, outfile): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 178 | """Run objdump on an input file. |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 179 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 180 | Args: |
Trent Apted | 66736d8 | 2023-05-25 10:38:28 +1000 | [diff] [blame] | 181 | flag: Flag to pass objdump (e.g. '-d'). |
| 182 | infile: Input file to process. |
| 183 | outfile: Output file to write to. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 184 | """ |
Simon Glass | 313cc8a | 2023-06-21 10:24:39 +0100 | [diff] [blame] | 185 | result = run([CompilerTool("objdump"), flag, infile], stdout=outfile) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 186 | if result.returncode: |
| 187 | sys.exit() |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 188 | |
| 189 | |
| 190 | def CompilerTool(tool): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 191 | """Returns the cross-compiler tool filename. |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 192 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 193 | Args: |
Trent Apted | 66736d8 | 2023-05-25 10:38:28 +1000 | [diff] [blame] | 194 | tool: Tool name to return, e.g. 'size'. |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 195 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 196 | Returns: |
Trent Apted | 66736d8 | 2023-05-25 10:38:28 +1000 | [diff] [blame] | 197 | Filename of requested tool. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 198 | """ |
| 199 | return "%s%s" % (compiler, tool) |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 200 | |
| 201 | |
| 202 | def ParseCmdline(argv): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 203 | """Parse all command line options. |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 204 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 205 | Args: |
Trent Apted | 66736d8 | 2023-05-25 10:38:28 +1000 | [diff] [blame] | 206 | argv: Arguments to parse. |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 207 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 208 | Returns: |
Trent Apted | 66736d8 | 2023-05-25 10:38:28 +1000 | [diff] [blame] | 209 | The parsed options object |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 210 | """ |
Simon Glass | 95a316d | 2023-02-10 11:31:47 -0700 | [diff] [blame] | 211 | parser = commandline.ArgumentParser( |
| 212 | description=__doc__, default_log_level="notice" |
| 213 | ) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 214 | parser.add_argument( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 215 | "-B", |
| 216 | "--build", |
| 217 | action="store_false", |
| 218 | default=True, |
| 219 | help="Don't build U-Boot, just configure device tree", |
| 220 | ) |
| 221 | parser.add_argument( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 222 | "--dt", |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 223 | help="Select name of device tree file to use", |
| 224 | ) |
| 225 | parser.add_argument( |
Simon Glass | e94b4a4 | 2023-02-15 06:35:55 -0700 | [diff] [blame] | 226 | "--dtb", |
| 227 | type="file_exists", |
| 228 | help="Select a binary .dtb, passed to the U-Boot build using EXT_DTB", |
| 229 | ) |
| 230 | parser.add_argument( |
Simon Glass | 77cc6ea | 2023-02-12 08:04:43 -0700 | [diff] [blame] | 231 | "-f", |
| 232 | "--force-reconfig", |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 233 | action="store_true", |
| 234 | default=False, |
Simon Glass | 77cc6ea | 2023-02-12 08:04:43 -0700 | [diff] [blame] | 235 | help="Reconfigure before building", |
| 236 | ) |
| 237 | parser.add_argument( |
| 238 | "-F", |
| 239 | "--force-distclean", |
| 240 | action="store_true", |
| 241 | default=False, |
| 242 | help="Run distclean and reconfigure before building", |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 243 | ) |
| 244 | parser.add_argument( |
Simon Glass | 73a2f48 | 2023-02-15 06:18:50 -0700 | [diff] [blame] | 245 | "-j", |
| 246 | "--jobs", |
| 247 | type=int, |
| 248 | default=os.cpu_count(), |
| 249 | help="Select the number of CPUs to use (defaults to all)", |
| 250 | ) |
| 251 | parser.add_argument( |
Simon Glass | 37da44f | 2023-02-15 06:30:58 -0700 | [diff] [blame] | 252 | "-I", |
| 253 | "--in-tree", |
| 254 | action="store_true", |
| 255 | default=False, |
| 256 | help="Build in-tree", |
| 257 | ) |
| 258 | parser.add_argument( |
Simon Glass | d080aa7 | 2023-02-14 07:04:52 -0700 | [diff] [blame] | 259 | "-L", |
| 260 | "--no-lto", |
| 261 | dest="lto", |
| 262 | action="store_false", |
| 263 | default=True, |
| 264 | help="Disable Link-time Optimisation (LTO) when building", |
| 265 | ) |
| 266 | parser.add_argument( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 267 | "-O", |
| 268 | "--objdump", |
| 269 | action="store_true", |
| 270 | default=False, |
| 271 | help="Write disassembly output", |
| 272 | ) |
| 273 | parser.add_argument( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 274 | "-t", |
| 275 | "--trace", |
| 276 | action="store_true", |
| 277 | default=False, |
| 278 | help="Enable trace support", |
| 279 | ) |
| 280 | parser.add_argument( |
Simon Glass | a68488c | 2023-02-09 16:53:47 -0700 | [diff] [blame] | 281 | "-T", |
| 282 | "--target", |
| 283 | nargs="?", |
| 284 | default="all", |
| 285 | help="Select target to build", |
| 286 | ) |
| 287 | parser.add_argument( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 288 | "-V", |
| 289 | "--verified", |
| 290 | action="store_true", |
| 291 | default=False, |
| 292 | help="Include Chrome OS verified boot components", |
| 293 | ) |
| 294 | parser.add_argument( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 295 | "-z", |
| 296 | "--size", |
| 297 | action="store_true", |
| 298 | default=False, |
| 299 | help="Display U-Boot image size", |
| 300 | ) |
| 301 | parser.add_argument( |
Simon Glass | a68488c | 2023-02-09 16:53:47 -0700 | [diff] [blame] | 302 | "board", |
| 303 | type=str, |
| 304 | default=default_board, |
| 305 | help="Select board to build (daisy/peach_pit/link)", |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 306 | ) |
| 307 | return parser.parse_args(argv) |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 308 | |
| 309 | |
| 310 | def SetupBuild(options): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 311 | """Set up parameters needed for the build. |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 312 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 313 | This checks the current environment and options and sets up various things |
| 314 | needed for the build, including 'base' which holds the base flags for |
| 315 | passing to the U-Boot Makefile. |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 316 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 317 | Args: |
Trent Apted | 66736d8 | 2023-05-25 10:38:28 +1000 | [diff] [blame] | 318 | options: Command line options |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 319 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 320 | Returns: |
Trent Apted | 66736d8 | 2023-05-25 10:38:28 +1000 | [diff] [blame] | 321 | Base flags to use for U-Boot, as a list. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 322 | """ |
| 323 | # pylint: disable=global-statement |
Simon Glass | 826b1df | 2023-03-02 05:46:45 -0700 | [diff] [blame] | 324 | global arch, board, compiler, outdir, uboard |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 325 | |
Simon Glass | 9674afc | 2023-02-10 11:21:30 -0700 | [diff] [blame] | 326 | logging.info("Building for %s", options.board) |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 327 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 328 | # Separate out board_variant string: "peach_pit" becomes "peach", "pit". |
| 329 | # But don't mess up upstream boards which use _ in their name. |
| 330 | parts = options.board.split("_") |
| 331 | if parts[0] in ["daisy", "peach"]: |
| 332 | board = parts[0] |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 333 | else: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 334 | board = options.board |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 335 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 336 | # To allow this to be run from 'cros_sdk' |
| 337 | if in_chroot: |
| 338 | os.chdir(os.path.join(src_root, "third_party", "u-boot", "files")) |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 339 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 340 | base_board = board |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 341 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 342 | if options.verified: |
| 343 | base_board = "chromeos_%s" % base_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 | uboard = UBOARDS.get(base_board, base_board) |
Simon Glass | 9674afc | 2023-02-10 11:21:30 -0700 | [diff] [blame] | 346 | logging.info("U-Boot board is %s", uboard) |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 347 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 348 | # Pull out some information from the U-Boot boards config file |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 349 | (PRE_KBUILD, PRE_KCONFIG, KCONFIG) = range(3) |
| 350 | if os.path.exists("MAINTAINERS"): |
| 351 | board_format = PRE_KBUILD |
| 352 | else: |
| 353 | board_format = PRE_KCONFIG |
Simon Glass | 0b847c4 | 2023-06-15 11:37:19 +0100 | [diff] [blame] | 354 | |
| 355 | # Create the boards.cfg file if missing. |
| 356 | if not os.path.exists("board.cfg"): |
Simon Glass | 313cc8a | 2023-06-21 10:24:39 +0100 | [diff] [blame] | 357 | run(["buildman", "-R"]) |
Simon Glass | 0b847c4 | 2023-06-15 11:37:19 +0100 | [diff] [blame] | 358 | |
| 359 | # Buildman puts it in the directory above, so move it. |
| 360 | # https://source.denx.de/u-boot/u-boot/-/issues/17 |
| 361 | os.rename("../boards.cfg", "boards.cfg") |
| 362 | |
Mike Frysinger | 31fdddd | 2023-02-24 15:50:55 -0500 | [diff] [blame] | 363 | with open("boards.cfg", encoding="utf-8") as f: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 364 | for line in f: |
| 365 | if "genboardscfg" in line: |
| 366 | board_format = KCONFIG |
| 367 | if uboard in line: |
| 368 | if line[0] == "#": |
| 369 | continue |
| 370 | fields = line.split() |
| 371 | if not fields: |
| 372 | continue |
Simon Glass | a44a1f9 | 2023-02-09 16:31:34 -0700 | [diff] [blame] | 373 | target = fields[6] |
| 374 | # Make sure this is the right target. |
| 375 | if target != uboard: |
| 376 | continue |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 377 | arch = fields[1] |
| 378 | fields += [None, None, None] |
Simon Glass | 826b1df | 2023-03-02 05:46:45 -0700 | [diff] [blame] | 379 | if board_format in (PRE_KCONFIG, KCONFIG): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 380 | target = fields[0] |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 381 | if not arch: |
| 382 | cros_build_lib.Die( |
| 383 | "Selected board '%s' not found in boards.cfg." % board |
| 384 | ) |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 385 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 386 | vboot = os.path.join("build", board, "usr") |
Simon Glass | 4158d73 | 2023-02-17 11:38:31 -0700 | [diff] [blame] | 387 | if arch == "sandbox": |
| 388 | compiler = "" |
| 389 | elif in_chroot: |
Simon Glass | a44a1f9 | 2023-02-09 16:31:34 -0700 | [diff] [blame] | 390 | if arch == "x86": |
| 391 | compiler = "i686-cros-linux-gnu-" |
| 392 | elif arch == "arm": |
Simon Glass | a345662 | 2023-03-02 05:46:45 -0700 | [diff] [blame] | 393 | compiler = "armv7a-cros-linux-gnueabihf-" |
Simon Glass | a44a1f9 | 2023-02-09 16:31:34 -0700 | [diff] [blame] | 394 | elif arch == "aarch64": |
Simon Glass | a345662 | 2023-03-02 05:46:45 -0700 | [diff] [blame] | 395 | compiler = "aarch64-cros-linux-gnu-" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 396 | else: |
Simon Glass | 313cc8a | 2023-06-21 10:24:39 +0100 | [diff] [blame] | 397 | result = run( |
Simon Glass | a44a1f9 | 2023-02-09 16:31:34 -0700 | [diff] [blame] | 398 | ["buildman", "-A", "--boards", options.board], |
| 399 | capture_output=True, |
Simon Glass | a44a1f9 | 2023-02-09 16:31:34 -0700 | [diff] [blame] | 400 | ) |
| 401 | compiler = result.stdout.strip() |
| 402 | if not compiler: |
| 403 | cros_build_lib.Die("Selected arch '%s' not supported.", arch) |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 404 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 405 | base = [ |
| 406 | "make", |
Simon Glass | 73a2f48 | 2023-02-15 06:18:50 -0700 | [diff] [blame] | 407 | "-j%d" % options.jobs, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 408 | "CROSS_COMPILE=%s" % compiler, |
| 409 | "--no-print-directory", |
| 410 | "HOSTSTRIP=true", |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 411 | "QEMU_ARCH=", |
Simon Glass | 0c0617f | 2023-02-19 18:32:16 -0700 | [diff] [blame] | 412 | "KCONFIG_NOSILENTUPDATE=1", |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 413 | ] |
Simon Glass | f665da6 | 2023-02-15 06:40:51 -0700 | [diff] [blame] | 414 | if options.dt: |
| 415 | base.append(f"DEVICE_TREE={options.dt}") |
Simon Glass | 37da44f | 2023-02-15 06:30:58 -0700 | [diff] [blame] | 416 | if not options.in_tree: |
| 417 | outdir = os.path.join(OUT_DIR, uboard) |
| 418 | base.append(f"O={outdir}") |
Simon Glass | d080aa7 | 2023-02-14 07:04:52 -0700 | [diff] [blame] | 419 | if not options.lto: |
| 420 | base.append("NO_LTO=1") |
Simon Glass | e94b4a4 | 2023-02-15 06:35:55 -0700 | [diff] [blame] | 421 | if options.dtb: |
| 422 | base.append(f"EXT_DTB={options.dtb}") |
Simon Glass | d080aa7 | 2023-02-14 07:04:52 -0700 | [diff] [blame] | 423 | |
Simon Glass | 95a316d | 2023-02-10 11:31:47 -0700 | [diff] [blame] | 424 | # Enable quiet output at INFO level, everything at DEBUG level |
| 425 | if logging.getLogger().getEffectiveLevel() <= logging.DEBUG: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 426 | base.append("V=1") |
Simon Glass | 95a316d | 2023-02-10 11:31:47 -0700 | [diff] [blame] | 427 | elif logging.getLogger().getEffectiveLevel() >= logging.NOTICE: |
| 428 | base.append("-s") |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 429 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 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, |
| 437 | ] |
| 438 | base.append("VBOOT_DEBUG=1") |
| 439 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 440 | base.append("BUILD_ROM=1") |
| 441 | if options.trace: |
| 442 | base.append("FTRACE=1") |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 443 | |
Simon Glass | 77cc6ea | 2023-02-12 08:04:43 -0700 | [diff] [blame] | 444 | if not options.force_reconfig: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 445 | config_mk = "%s/include/autoconf.mk" % outdir |
| 446 | if not os.path.exists(config_mk): |
Simon Glass | 77cc6ea | 2023-02-12 08:04:43 -0700 | [diff] [blame] | 447 | logging.warning("No build found for %s - adding -f", board) |
| 448 | options.force_reconfig = True |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 449 | |
| 450 | config_mk = "include/autoconf.mk" |
| 451 | if os.path.exists(config_mk): |
Simon Glass | 37da44f | 2023-02-15 06:30:58 -0700 | [diff] [blame] | 452 | logging.warning("Warning: '%s' exists, try 'make mrproper'", config_mk) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 453 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 454 | return base |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 455 | |
| 456 | |
Simon Glass | f78c9b5 | 2023-02-14 06:58:10 -0700 | [diff] [blame] | 457 | def CheckConfigChange() -> bool: |
| 458 | """See if we need to reconfigure due to config files changing |
| 459 | |
| 460 | Checks if any defconfig or Kconfig file has changed in the source tree |
| 461 | since the last time U-Boot was configured for this build. For simplicity, |
| 462 | any defconfig change will trigger this, not just one for the board being |
| 463 | built, since the cost of a reconfigure is fairly small. |
| 464 | |
| 465 | Returns: |
| 466 | True if any config file has changed since U-Boot was last configured |
| 467 | """ |
| 468 | fname = os.path.join(outdir, ".config") |
| 469 | ref_time = os.path.getctime(fname) |
| 470 | for p in Path.cwd().glob("configs/*"): |
| 471 | if p.stat().st_ctime > ref_time: |
| 472 | logging.warning("config/ dir has changed - adding -f") |
| 473 | return True |
| 474 | |
| 475 | for p in Path.cwd().glob("**/Kconfig*"): |
| 476 | if p.stat().st_ctime > ref_time: |
| 477 | logging.warning("Kconfig file(s) changed - adding -f") |
| 478 | return True |
| 479 | |
| 480 | return False |
| 481 | |
| 482 | |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 483 | def RunBuild(options, base, target, queue): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 484 | """Run the U-Boot build. |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 485 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 486 | Args: |
Trent Apted | 66736d8 | 2023-05-25 10:38:28 +1000 | [diff] [blame] | 487 | options: Command line options. |
| 488 | base: Base U-Boot flags. |
| 489 | target: Target to build. |
| 490 | queue: A parallel queue to add jobs to. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 491 | """ |
Simon Glass | 9674afc | 2023-02-10 11:21:30 -0700 | [diff] [blame] | 492 | logging.info("U-Boot build flags: %s", " ".join(base)) |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 493 | |
Simon Glass | 77cc6ea | 2023-02-12 08:04:43 -0700 | [diff] [blame] | 494 | if options.force_distclean: |
| 495 | options.force_reconfig = True |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 496 | # Ignore any error from this, some older U-Boots fail on this. |
Simon Glass | 313cc8a | 2023-06-21 10:24:39 +0100 | [diff] [blame] | 497 | run(base + ["distclean"], capture_output=True) |
Simon Glass | 77cc6ea | 2023-02-12 08:04:43 -0700 | [diff] [blame] | 498 | |
Simon Glass | f78c9b5 | 2023-02-14 06:58:10 -0700 | [diff] [blame] | 499 | if not options.force_reconfig: |
| 500 | options.force_reconfig = CheckConfigChange() |
| 501 | |
Simon Glass | 77cc6ea | 2023-02-12 08:04:43 -0700 | [diff] [blame] | 502 | # Reconfigure U-Boot. |
| 503 | if options.force_reconfig: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [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)] |
Simon Glass | 313cc8a | 2023-06-21 10:24:39 +0100 | [diff] [blame] | 509 | result = run(cmd, stdout=True, stderr=subprocess.STDOUT) |
Simon Glass | dee7d6b | 2023-02-10 11:35:18 -0700 | [diff] [blame] | 510 | if ( |
| 511 | result.returncode |
| 512 | or logging.getLogger().getEffectiveLevel() <= logging.DEBUG |
| 513 | ): |
| 514 | print(f"cmd: {result.cmdstr}") |
| 515 | print(result.stdout, file=sys.stderr) |
| 516 | if result.returncode: |
| 517 | sys.exit(result.returncode) |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 518 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 519 | # Do the actual build. |
| 520 | if options.build: |
Simon Glass | 313cc8a | 2023-06-21 10:24:39 +0100 | [diff] [blame] | 521 | result = run(base + [target], input="", capture_output=True) |
Simon Glass | dee7d6b | 2023-02-10 11:35:18 -0700 | [diff] [blame] | 522 | if ( |
| 523 | result.returncode |
| 524 | or logging.getLogger().getEffectiveLevel() <= logging.INFO |
Simon Glass | c12ccd7 | 2023-03-01 13:35:29 -0700 | [diff] [blame] | 525 | or result.stderr |
Simon Glass | dee7d6b | 2023-02-10 11:35:18 -0700 | [diff] [blame] | 526 | ): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 527 | # The build failed, so output the results to stderr. |
Simon Glass | c12ccd7 | 2023-03-01 13:35:29 -0700 | [diff] [blame] | 528 | print(f"cmd: {result.cmdstr}", file=sys.stderr) |
| 529 | print(result.stderr, file=sys.stderr) |
| 530 | |
| 531 | # Note that stdout and stderr are separated here, so warnings |
| 532 | # associated with a file will appear separately from any output |
| 533 | # from the build system |
| 534 | if logging.getLogger().getEffectiveLevel() <= logging.INFO: |
| 535 | print(result.stdout) |
Simon Glass | dee7d6b | 2023-02-10 11:35:18 -0700 | [diff] [blame] | 536 | if result.returncode: |
| 537 | sys.exit(result.returncode) |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 538 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 539 | files = ["%s/u-boot" % outdir] |
| 540 | spl = glob.glob("%s/spl/u-boot-spl" % outdir) |
| 541 | if spl: |
| 542 | files += spl |
| 543 | if options.size: |
Simon Glass | 313cc8a | 2023-06-21 10:24:39 +0100 | [diff] [blame] | 544 | result = run([CompilerTool("size")] + files) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 545 | if result.returncode: |
| 546 | sys.exit() |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 547 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 548 | # Create disassembly files .dis and .Dis (full dump) |
| 549 | for f in files: |
| 550 | base = os.path.splitext(f)[0] |
| 551 | if options.objdump: |
| 552 | queue.put(("-d", f, base + ".dis")) |
| 553 | queue.put(("-D", f, base + ".Dis")) |
| 554 | else: |
| 555 | # Remove old files which otherwise might be confusing |
| 556 | osutils.SafeUnlink(base + ".dis") |
| 557 | osutils.SafeUnlink(base + ".Dis") |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 558 | |
Simon Glass | 9674afc | 2023-02-10 11:21:30 -0700 | [diff] [blame] | 559 | logging.info("Output directory %s", outdir) |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 560 | |
| 561 | |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 562 | def main(argv): |
Simon Glass | e6201d7 | 2023-02-09 16:42:22 -0700 | [diff] [blame] | 563 | """Main function for script to build firmware. |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 564 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 565 | Args: |
Trent Apted | 66736d8 | 2023-05-25 10:38:28 +1000 | [diff] [blame] | 566 | argv: Program arguments. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 567 | """ |
| 568 | options = ParseCmdline(argv) |
| 569 | base = SetupBuild(options) |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 570 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 571 | with parallel.BackgroundTaskRunner(Dumper) as queue: |
| 572 | RunBuild(options, base, options.target, queue) |
Simon Glass | 0274168 | 2013-05-26 07:07:58 -0700 | [diff] [blame] | 573 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 574 | if options.objdump: |
Simon Glass | 9674afc | 2023-02-10 11:21:30 -0700 | [diff] [blame] | 575 | logging.info("Writing diasssembly files") |