blob: 6511621fd09bb0bd342379617c524de7a71aefe5 [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2013 The ChromiumOS Authors
Simon Glass02741682013-05-26 07:07:58 -07002# 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
7Builds a firmware image for any board and writes it to the board. The image
8can be pure upstream or include Chrome OS components (-V). Some device
9tree parameters can be provided, including silent console (-C) and secure
10boot (-S). Use -i for a faster incremental build. The image is written to
11the board by default using USB/em100 (or sdcard with -x). Use -b to specify
12the board to build. Options can be added to ~/.crosfwrc - see the script for
13details.
14
15It can also flash SPI by writing a 'magic flasher' U-Boot with a payload
16to the board.
17
Simon Glass02741682013-05-26 07:07:58 -070018The script is normally run from within the U-Boot directory which is
19.../src/third_party/u-boot/files
20
21Example 1: Build upstream image for coreboot and write to a 'link':
22
Simon Glass77cc6ea2023-02-12 08:04:43 -070023 crosfw link
Simon Glass02741682013-05-26 07:07:58 -070024
Simon Glass77cc6ea2023-02-12 08:04:43 -070025Example 2: Build verified boot image (V) for daisy/snow.
Simon Glass02741682013-05-26 07:07:58 -070026
Simon Glass77cc6ea2023-02-12 08:04:43 -070027 crosfw daisy -V
Simon Glass02741682013-05-26 07:07:58 -070028
Simon Glass77cc6ea2023-02-12 08:04:43 -070029You can force a reconfigure with -f and a full distclean with -F.
Simon Glass02741682013-05-26 07:07:58 -070030
Simon Glass77cc6ea2023-02-12 08:04:43 -070031To increase verbosity use the -v and --debug options.
Simon Glass02741682013-05-26 07:07:58 -070032
Simon Glass77cc6ea2023-02-12 08:04:43 -070033This script does not use an ebuild. It does a similar thing to the
Simon Glass02741682013-05-26 07:07:58 -070034chromeos-u-boot ebuild, and runs cros_bundle_firmware to produce various
35types of image, a little like the chromeos-bootimage ebuild.
36
37The purpose of this script is to make it easier and faster to perform
38common firmware build tasks without changing boards, manually updating
39device tree files or lots of USE flags and complexity in the ebuilds.
40
41This script has been tested with snow, link and peach_pit. It builds for
42peach_pit by default. Note that it will also build any upstream ARM
Simon Glass77cc6ea2023-02-12 08:04:43 -070043board - e.g. "snapper9260" will build an image for that board.
Simon Glass02741682013-05-26 07:07:58 -070044
45Mostly you can use the script inside and outside the chroot. The main
46limitation is that dut-control doesn't really work outside the chroot,
47so writing the image to the board over USB is not possible, nor can the
48board be automatically reset on x86 platforms.
49
50For an incremental build (faster), run with -i
51
52To get faster clean builds, install ccache, and create ~/.crosfwrc with
53this line:
54
Simon Glass6ddc7f12013-07-18 15:22:41 -060055 USE_CCACHE = True
Simon Glass02741682013-05-26 07:07:58 -070056
57(make sure ~/.ccache is not on NFS, or set CCACHE_DIR)
58
59Other options are the default board to build, and verbosity (0-4), e.g.:
60
Simon Glass6ddc7f12013-07-18 15:22:41 -060061 DEFAULT_BOARD = 'daisy'
62 VERBOSE = 1
Simon Glass02741682013-05-26 07:07:58 -070063
64It is possible to use multiple servo boards, each on its own port. Add
65these lines to your ~/.crosfwrc to set the servo port to use for each
66board:
67
68 SERVO_PORT['link'] = 8888
69 SERVO_PORT['daisy'] = 9999
70 SERVO_PORT['peach_pit'] = 7777
71
Simon Glassb89ae892013-07-18 15:23:35 -060072All builds appear in the <outdir>/<board> subdirectory and images are written
73to <outdir>/<uboard>/out, where <uboard> is the U-Boot name for the board (in
74the U-Boot boards.cfg file)
75
76The 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 Glass02741682013-05-26 07:07:58 -070080
81For 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 Frysinger0246c1f2021-12-14 01:17:17 -050089--bmpblk ~/chromiumos/src/third_party/u-boot/bmp.bin
Simon Glass02741682013-05-26 07:07:58 -070090
91For example: -a "--gbb-flags -force-dev-switch-on"
92
93Note the standard bmpblk is at:
Mike Frysinger0246c1f2021-12-14 01:17:17 -050094 ~/chromiumos/src/third_party/chromiumos-overlay/sys-boot/
95 chromeos-bootimage/files/bmpblk.bin
Simon Glass02741682013-05-26 07:07:58 -070096"""
97
98import glob
Chris McDonald59650c32021-07-20 15:29:28 -060099import logging
Simon Glass02741682013-05-26 07:07:58 -0700100import multiprocessing
101import os
Simon Glassf78c9b52023-02-14 06:58:10 -0700102from pathlib import Path
Simon Glass02741682013-05-26 07:07:58 -0700103import re
Mike Frysinger66d32cd2019-12-17 14:55:29 -0500104import subprocess
Simon Glass02741682013-05-26 07:07:58 -0700105import sys
106
Simon Glass02741682013-05-26 07:07:58 -0700107from chromite.lib import commandline
Chris McDonald59650c32021-07-20 15:29:28 -0600108from chromite.lib import constants
Simon Glass02741682013-05-26 07:07:58 -0700109from chromite.lib import cros_build_lib
110from chromite.lib import osutils
111from chromite.lib import parallel
112
113
114arch = None
115board = None
116compiler = None
117default_board = None
118family = None
119in_chroot = True
120
Alex Klein1699fab2022-09-08 08:46:06 -0600121kwargs = {
122 "print_cmd": False,
123 "check": False,
Simon Glassdee7d6b2023-02-10 11:35:18 -0700124 "encoding": "utf-8",
Alex Klein1699fab2022-09-08 08:46:06 -0600125}
Simon Glass02741682013-05-26 07:07:58 -0700126
Alex Klein1699fab2022-09-08 08:46:06 -0600127outdir = ""
Simon Glass02741682013-05-26 07:07:58 -0700128
Alex Klein1699fab2022-09-08 08:46:06 -0600129# If you have multiple boards connected on different servo ports, put lines
Simon Glass02741682013-05-26 07:07:58 -0700130# like 'SERVO_PORT{"peach_pit"} = 7777' in your ~/.crosfwrc
131SERVO_PORT = {}
132
133smdk = None
Alex Klein1699fab2022-09-08 08:46:06 -0600134src_root = os.path.join(constants.SOURCE_ROOT, "src")
Simon Glass02741682013-05-26 07:07:58 -0700135in_chroot = cros_build_lib.IsInsideChroot()
136
Alex Klein1699fab2022-09-08 08:46:06 -0600137uboard = ""
Simon Glass02741682013-05-26 07:07:58 -0700138
Alex Klein1699fab2022-09-08 08:46:06 -0600139default_board = "peach_pit"
Simon Glass02741682013-05-26 07:07:58 -0700140use_ccache = False
141vendor = None
Simon Glass02741682013-05-26 07:07:58 -0700142
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
146UBOARDS = {
Alex Klein1699fab2022-09-08 08:46:06 -0600147 "daisy": "smdk5250",
148 "peach": "smdk5420",
Simon Glass02741682013-05-26 07:07:58 -0700149}
Alex Klein1699fab2022-09-08 08:46:06 -0600150for 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 Glass02741682013-05-26 07:07:58 -0700162
163SOCS = {
Alex Klein1699fab2022-09-08 08:46:06 -0600164 "coreboot-x86": "",
165 "chromeos_coreboot": "",
166 "daisy": "exynos5250-",
167 "peach": "exynos5420-",
Simon Glass02741682013-05-26 07:07:58 -0700168}
169
170DEFAULT_DTS = {
Alex Klein1699fab2022-09-08 08:46:06 -0600171 "daisy": "snow",
172 "daisy_spring": "spring",
173 "peach_pit": "peach-pit",
Simon Glass02741682013-05-26 07:07:58 -0700174}
175
Alex Klein1699fab2022-09-08 08:46:06 -0600176OUT_DIR = "/tmp/crosfw"
Simon Glassb89ae892013-07-18 15:23:35 -0600177
Alex Klein1699fab2022-09-08 08:46:06 -0600178rc_file = os.path.expanduser("~/.crosfwrc")
Simon Glass02741682013-05-26 07:07:58 -0700179if os.path.exists(rc_file):
Alex Klein1699fab2022-09-08 08:46:06 -0600180 with open(rc_file) as fp:
181 # pylint: disable=exec-used
182 exec(compile(fp.read(), rc_file, "exec"))
Simon Glass02741682013-05-26 07:07:58 -0700183
184
Simon Glass02741682013-05-26 07:07:58 -0700185def Dumper(flag, infile, outfile):
Alex Klein1699fab2022-09-08 08:46:06 -0600186 """Run objdump on an input file.
Simon Glass02741682013-05-26 07:07:58 -0700187
Alex Klein1699fab2022-09-08 08:46:06 -0600188 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 Glass02741682013-05-26 07:07:58 -0700198
199
200def CompilerTool(tool):
Alex Klein1699fab2022-09-08 08:46:06 -0600201 """Returns the cross-compiler tool filename.
Simon Glass02741682013-05-26 07:07:58 -0700202
Alex Klein1699fab2022-09-08 08:46:06 -0600203 Args:
204 tool: Tool name to return, e.g. 'size'.
Simon Glass02741682013-05-26 07:07:58 -0700205
Alex Klein1699fab2022-09-08 08:46:06 -0600206 Returns:
207 Filename of requested tool.
208 """
209 return "%s%s" % (compiler, tool)
Simon Glass02741682013-05-26 07:07:58 -0700210
211
212def ParseCmdline(argv):
Alex Klein1699fab2022-09-08 08:46:06 -0600213 """Parse all command line options.
Simon Glass02741682013-05-26 07:07:58 -0700214
Alex Klein1699fab2022-09-08 08:46:06 -0600215 Args:
216 argv: Arguments to parse.
Simon Glass02741682013-05-26 07:07:58 -0700217
Alex Klein1699fab2022-09-08 08:46:06 -0600218 Returns:
219 The parsed options object
220 """
Simon Glass95a316d2023-02-10 11:31:47 -0700221 parser = commandline.ArgumentParser(
222 description=__doc__, default_log_level="notice"
223 )
Alex Klein1699fab2022-09-08 08:46:06 -0600224 parser.add_argument(
Alex Klein1699fab2022-09-08 08:46:06 -0600225 "-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 Klein1699fab2022-09-08 08:46:06 -0600232 "-d",
233 "--dt",
234 default="seaboard",
235 help="Select name of device tree file to use",
236 )
237 parser.add_argument(
Simon Glass77cc6ea2023-02-12 08:04:43 -0700238 "-f",
239 "--force-reconfig",
Alex Klein1699fab2022-09-08 08:46:06 -0600240 action="store_true",
241 default=False,
Simon Glass77cc6ea2023-02-12 08:04:43 -0700242 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 Klein1699fab2022-09-08 08:46:06 -0600250 )
251 parser.add_argument(
Alex Klein1699fab2022-09-08 08:46:06 -0600252 "-O",
253 "--objdump",
254 action="store_true",
255 default=False,
256 help="Write disassembly output",
257 )
258 parser.add_argument(
Alex Klein1699fab2022-09-08 08:46:06 -0600259 "-t",
260 "--trace",
261 action="store_true",
262 default=False,
263 help="Enable trace support",
264 )
265 parser.add_argument(
Simon Glassa68488c2023-02-09 16:53:47 -0700266 "-T",
267 "--target",
268 nargs="?",
269 default="all",
270 help="Select target to build",
271 )
272 parser.add_argument(
Alex Klein1699fab2022-09-08 08:46:06 -0600273 "-V",
274 "--verified",
275 action="store_true",
276 default=False,
277 help="Include Chrome OS verified boot components",
278 )
279 parser.add_argument(
Alex Klein1699fab2022-09-08 08:46:06 -0600280 "-z",
281 "--size",
282 action="store_true",
283 default=False,
284 help="Display U-Boot image size",
285 )
286 parser.add_argument(
Simon Glassa68488c2023-02-09 16:53:47 -0700287 "board",
288 type=str,
289 default=default_board,
290 help="Select board to build (daisy/peach_pit/link)",
Alex Klein1699fab2022-09-08 08:46:06 -0600291 )
292 return parser.parse_args(argv)
Simon Glass02741682013-05-26 07:07:58 -0700293
294
Simon Glassc1a323a2016-08-04 20:29:51 -0600295def FindCompiler(gcc, cros_prefix):
Alex Klein1699fab2022-09-08 08:46:06 -0600296 """Look up the compiler for an architecture.
Simon Glassc1a323a2016-08-04 20:29:51 -0600297
Alex Klein1699fab2022-09-08 08:46:06 -0600298 Args:
299 gcc: GCC architecture, either 'arm' or 'aarch64'
300 cros_prefix: Full Chromium OS toolchain prefix
301 """
302 if in_chroot:
303 # Use the Chromium OS toolchain.
304 prefix = cros_prefix
305 else:
306 prefix = glob.glob("/opt/linaro/gcc-linaro-%s-linux-*/bin/*gcc" % gcc)
307 if not prefix:
308 cros_build_lib.Die(
309 """Please install an %s toolchain for your machine.
Simon Glassc1a323a2016-08-04 20:29:51 -0600310Install a Linaro toolchain from:
311https://launchpad.net/linaro-toolchain-binaries
Alex Klein1699fab2022-09-08 08:46:06 -0600312or see cros/commands/cros_chrome_sdk.py."""
313 % gcc
314 )
315 prefix = re.sub("gcc$", "", prefix[0])
316 return prefix
Simon Glassc1a323a2016-08-04 20:29:51 -0600317
318
Simon Glass02741682013-05-26 07:07:58 -0700319def SetupBuild(options):
Alex Klein1699fab2022-09-08 08:46:06 -0600320 """Set up parameters needed for the build.
Simon Glass02741682013-05-26 07:07:58 -0700321
Alex Klein1699fab2022-09-08 08:46:06 -0600322 This checks the current environment and options and sets up various things
323 needed for the build, including 'base' which holds the base flags for
324 passing to the U-Boot Makefile.
Simon Glass02741682013-05-26 07:07:58 -0700325
Alex Klein1699fab2022-09-08 08:46:06 -0600326 Args:
327 options: Command line options
Simon Glass02741682013-05-26 07:07:58 -0700328
Alex Klein1699fab2022-09-08 08:46:06 -0600329 Returns:
330 Base flags to use for U-Boot, as a list.
331 """
332 # pylint: disable=global-statement
Simon Glass95a316d2023-02-10 11:31:47 -0700333 global arch, board, compiler, family, outdir, smdk, uboard, vendor
Simon Glass02741682013-05-26 07:07:58 -0700334
Simon Glass9674afc2023-02-10 11:21:30 -0700335 logging.info("Building for %s", options.board)
Simon Glass02741682013-05-26 07:07:58 -0700336
Alex Klein1699fab2022-09-08 08:46:06 -0600337 # Separate out board_variant string: "peach_pit" becomes "peach", "pit".
338 # But don't mess up upstream boards which use _ in their name.
339 parts = options.board.split("_")
340 if parts[0] in ["daisy", "peach"]:
341 board = parts[0]
Simon Glass02741682013-05-26 07:07:58 -0700342 else:
Alex Klein1699fab2022-09-08 08:46:06 -0600343 board = options.board
Simon Glass02741682013-05-26 07:07:58 -0700344
Alex Klein1699fab2022-09-08 08:46:06 -0600345 # To allow this to be run from 'cros_sdk'
346 if in_chroot:
347 os.chdir(os.path.join(src_root, "third_party", "u-boot", "files"))
Simon Glass02741682013-05-26 07:07:58 -0700348
Alex Klein1699fab2022-09-08 08:46:06 -0600349 base_board = board
Simon Glass02741682013-05-26 07:07:58 -0700350
Alex Klein1699fab2022-09-08 08:46:06 -0600351 if options.verified:
352 base_board = "chromeos_%s" % base_board
Simon Glass02741682013-05-26 07:07:58 -0700353
Alex Klein1699fab2022-09-08 08:46:06 -0600354 uboard = UBOARDS.get(base_board, base_board)
Simon Glass9674afc2023-02-10 11:21:30 -0700355 logging.info("U-Boot board is %s", uboard)
Simon Glass02741682013-05-26 07:07:58 -0700356
Alex Klein1699fab2022-09-08 08:46:06 -0600357 # Pull out some information from the U-Boot boards config file
358 family = None
359 (PRE_KBUILD, PRE_KCONFIG, KCONFIG) = range(3)
360 if os.path.exists("MAINTAINERS"):
361 board_format = PRE_KBUILD
362 else:
363 board_format = PRE_KCONFIG
364 with open("boards.cfg") as f:
365 for line in f:
366 if "genboardscfg" in line:
367 board_format = KCONFIG
368 if uboard in line:
369 if line[0] == "#":
370 continue
371 fields = line.split()
372 if not fields:
373 continue
Simon Glassa44a1f92023-02-09 16:31:34 -0700374 target = fields[6]
375 # Make sure this is the right target.
376 if target != uboard:
377 continue
Alex Klein1699fab2022-09-08 08:46:06 -0600378 arch = fields[1]
379 fields += [None, None, None]
380 if board_format == PRE_KBUILD:
381 smdk = fields[3]
382 vendor = fields[4]
383 family = fields[5]
Alex Klein1699fab2022-09-08 08:46:06 -0600384 elif board_format in (PRE_KCONFIG, KCONFIG):
385 smdk = fields[5]
386 vendor = fields[4]
387 family = fields[3]
388 target = fields[0]
Alex Klein1699fab2022-09-08 08:46:06 -0600389 if not arch:
390 cros_build_lib.Die(
391 "Selected board '%s' not found in boards.cfg." % board
392 )
Simon Glass02741682013-05-26 07:07:58 -0700393
Alex Klein1699fab2022-09-08 08:46:06 -0600394 vboot = os.path.join("build", board, "usr")
Simon Glassa44a1f92023-02-09 16:31:34 -0700395 if in_chroot:
396 if arch == "x86":
397 compiler = "i686-cros-linux-gnu-"
398 elif arch == "arm":
399 compiler = FindCompiler(arch, "armv7a-cros-linux-gnueabihf-")
400 elif arch == "aarch64":
401 compiler = FindCompiler(arch, "aarch64-cros-linux-gnu-")
Alex Klein1699fab2022-09-08 08:46:06 -0600402 elif arch == "sandbox":
403 compiler = ""
404 else:
Simon Glassa44a1f92023-02-09 16:31:34 -0700405 result = cros_build_lib.run(
406 ["buildman", "-A", "--boards", options.board],
407 capture_output=True,
Simon Glassa44a1f92023-02-09 16:31:34 -0700408 **kwargs,
409 )
410 compiler = result.stdout.strip()
411 if not compiler:
412 cros_build_lib.Die("Selected arch '%s' not supported.", arch)
Simon Glass02741682013-05-26 07:07:58 -0700413
Alex Klein1699fab2022-09-08 08:46:06 -0600414 cpus = multiprocessing.cpu_count()
Simon Glass02741682013-05-26 07:07:58 -0700415
Alex Klein1699fab2022-09-08 08:46:06 -0600416 outdir = os.path.join(OUT_DIR, uboard)
417 base = [
418 "make",
419 "-j%d" % cpus,
420 "O=%s" % outdir,
Alex Klein1699fab2022-09-08 08:46:06 -0600421 "CROSS_COMPILE=%s" % compiler,
422 "--no-print-directory",
423 "HOSTSTRIP=true",
424 "DEV_TREE_SRC=%s-%s" % (family, options.dt),
425 "QEMU_ARCH=",
426 ]
Simon Glass02741682013-05-26 07:07:58 -0700427
Simon Glass95a316d2023-02-10 11:31:47 -0700428 # Enable quiet output at INFO level, everything at DEBUG level
429 if logging.getLogger().getEffectiveLevel() <= logging.DEBUG:
Alex Klein1699fab2022-09-08 08:46:06 -0600430 base.append("V=1")
Simon Glass95a316d2023-02-10 11:31:47 -0700431 elif logging.getLogger().getEffectiveLevel() >= logging.NOTICE:
432 base.append("-s")
Simon Glass02741682013-05-26 07:07:58 -0700433
Alex Klein1699fab2022-09-08 08:46:06 -0600434 if options.verified:
435 base += [
436 "VBOOT=%s" % vboot,
437 "MAKEFLAGS_VBOOT=DEBUG=1",
438 "QUIET=1",
439 "CFLAGS_EXTRA_VBOOT=-DUNROLL_LOOPS",
440 "VBOOT_SOURCE=%s/platform/vboot_reference" % src_root,
441 ]
442 base.append("VBOOT_DEBUG=1")
443
Alex Klein1699fab2022-09-08 08:46:06 -0600444 base.append("BUILD_ROM=1")
445 if options.trace:
446 base.append("FTRACE=1")
Alex Klein1699fab2022-09-08 08:46:06 -0600447
Simon Glass77cc6ea2023-02-12 08:04:43 -0700448 if not options.force_reconfig:
Alex Klein1699fab2022-09-08 08:46:06 -0600449 config_mk = "%s/include/autoconf.mk" % outdir
450 if not os.path.exists(config_mk):
Simon Glass77cc6ea2023-02-12 08:04:43 -0700451 logging.warning("No build found for %s - adding -f", board)
452 options.force_reconfig = True
Alex Klein1699fab2022-09-08 08:46:06 -0600453
454 config_mk = "include/autoconf.mk"
455 if os.path.exists(config_mk):
456 logging.warning("Warning: '%s' exists, try 'make distclean'", config_mk)
457
Alex Klein1699fab2022-09-08 08:46:06 -0600458 return base
Simon Glass02741682013-05-26 07:07:58 -0700459
460
Simon Glassf78c9b52023-02-14 06:58:10 -0700461def CheckConfigChange() -> bool:
462 """See if we need to reconfigure due to config files changing
463
464 Checks if any defconfig or Kconfig file has changed in the source tree
465 since the last time U-Boot was configured for this build. For simplicity,
466 any defconfig change will trigger this, not just one for the board being
467 built, since the cost of a reconfigure is fairly small.
468
469 Returns:
470 True if any config file has changed since U-Boot was last configured
471 """
472 fname = os.path.join(outdir, ".config")
473 ref_time = os.path.getctime(fname)
474 for p in Path.cwd().glob("configs/*"):
475 if p.stat().st_ctime > ref_time:
476 logging.warning("config/ dir has changed - adding -f")
477 return True
478
479 for p in Path.cwd().glob("**/Kconfig*"):
480 if p.stat().st_ctime > ref_time:
481 logging.warning("Kconfig file(s) changed - adding -f")
482 return True
483
484 return False
485
486
Simon Glass02741682013-05-26 07:07:58 -0700487def RunBuild(options, base, target, queue):
Alex Klein1699fab2022-09-08 08:46:06 -0600488 """Run the U-Boot build.
Simon Glass02741682013-05-26 07:07:58 -0700489
Alex Klein1699fab2022-09-08 08:46:06 -0600490 Args:
491 options: Command line options.
492 base: Base U-Boot flags.
493 target: Target to build.
494 queue: A parallel queue to add jobs to.
495 """
Simon Glass9674afc2023-02-10 11:21:30 -0700496 logging.info("U-Boot build flags: %s", " ".join(base))
Simon Glass02741682013-05-26 07:07:58 -0700497
Simon Glass77cc6ea2023-02-12 08:04:43 -0700498 if options.force_distclean:
499 options.force_reconfig = True
Alex Klein1699fab2022-09-08 08:46:06 -0600500 # Ignore any error from this, some older U-Boots fail on this.
Simon Glassdee7d6b2023-02-10 11:35:18 -0700501 cros_build_lib.run(base + ["distclean"], capture_output=True, **kwargs)
Simon Glass77cc6ea2023-02-12 08:04:43 -0700502
Simon Glassf78c9b52023-02-14 06:58:10 -0700503 if not options.force_reconfig:
504 options.force_reconfig = CheckConfigChange()
505
Simon Glass77cc6ea2023-02-12 08:04:43 -0700506 # Reconfigure U-Boot.
507 if options.force_reconfig:
Alex Klein1699fab2022-09-08 08:46:06 -0600508 if os.path.exists("tools/genboardscfg.py"):
509 mtarget = "defconfig"
510 else:
511 mtarget = "config"
512 cmd = base + ["%s_%s" % (uboard, mtarget)]
513 result = cros_build_lib.run(
514 cmd, stdout=True, stderr=subprocess.STDOUT, **kwargs
515 )
Simon Glassdee7d6b2023-02-10 11:35:18 -0700516 if (
517 result.returncode
518 or logging.getLogger().getEffectiveLevel() <= logging.DEBUG
519 ):
520 print(f"cmd: {result.cmdstr}")
521 print(result.stdout, file=sys.stderr)
522 if result.returncode:
523 sys.exit(result.returncode)
Simon Glass02741682013-05-26 07:07:58 -0700524
Alex Klein1699fab2022-09-08 08:46:06 -0600525 # Do the actual build.
526 if options.build:
527 result = cros_build_lib.run(
528 base + [target], stdout=True, stderr=subprocess.STDOUT, **kwargs
529 )
Simon Glassdee7d6b2023-02-10 11:35:18 -0700530 if (
531 result.returncode
532 or logging.getLogger().getEffectiveLevel() <= logging.INFO
533 ):
Alex Klein1699fab2022-09-08 08:46:06 -0600534 # The build failed, so output the results to stderr.
Simon Glassdee7d6b2023-02-10 11:35:18 -0700535 print(f"cmd: {result.cmdstr}")
536 print(result.stdout, file=sys.stderr)
537 if result.returncode:
538 sys.exit(result.returncode)
Simon Glass02741682013-05-26 07:07:58 -0700539
Alex Klein1699fab2022-09-08 08:46:06 -0600540 files = ["%s/u-boot" % outdir]
541 spl = glob.glob("%s/spl/u-boot-spl" % outdir)
542 if spl:
543 files += spl
544 if options.size:
545 result = cros_build_lib.run([CompilerTool("size")] + files, **kwargs)
546 if result.returncode:
547 sys.exit()
Simon Glass02741682013-05-26 07:07:58 -0700548
Alex Klein1699fab2022-09-08 08:46:06 -0600549 # Create disassembly files .dis and .Dis (full dump)
550 for f in files:
551 base = os.path.splitext(f)[0]
552 if options.objdump:
553 queue.put(("-d", f, base + ".dis"))
554 queue.put(("-D", f, base + ".Dis"))
555 else:
556 # Remove old files which otherwise might be confusing
557 osutils.SafeUnlink(base + ".dis")
558 osutils.SafeUnlink(base + ".Dis")
Simon Glass02741682013-05-26 07:07:58 -0700559
Simon Glass9674afc2023-02-10 11:21:30 -0700560 logging.info("Output directory %s", outdir)
Simon Glass02741682013-05-26 07:07:58 -0700561
562
Simon Glass02741682013-05-26 07:07:58 -0700563def main(argv):
Simon Glasse6201d72023-02-09 16:42:22 -0700564 """Main function for script to build firmware.
Simon Glass02741682013-05-26 07:07:58 -0700565
Alex Klein1699fab2022-09-08 08:46:06 -0600566 Args:
567 argv: Program arguments.
568 """
569 options = ParseCmdline(argv)
570 base = SetupBuild(options)
Simon Glass02741682013-05-26 07:07:58 -0700571
Alex Klein1699fab2022-09-08 08:46:06 -0600572 with parallel.BackgroundTaskRunner(Dumper) as queue:
573 RunBuild(options, base, options.target, queue)
Simon Glass02741682013-05-26 07:07:58 -0700574
Alex Klein1699fab2022-09-08 08:46:06 -0600575 if options.objdump:
Simon Glass9674afc2023-02-10 11:21:30 -0700576 logging.info("Writing diasssembly files")