blob: 5aa9f4d9f2aa8765bb946e0334198fcbb59012f7 [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 os
Simon Glassf78c9b52023-02-14 06:58:10 -0700101from pathlib import Path
Mike Frysinger66d32cd2019-12-17 14:55:29 -0500102import subprocess
Simon Glass02741682013-05-26 07:07:58 -0700103import sys
104
Simon Glass02741682013-05-26 07:07:58 -0700105from chromite.lib import commandline
Chris McDonald59650c32021-07-20 15:29:28 -0600106from chromite.lib import constants
Simon Glass02741682013-05-26 07:07:58 -0700107from chromite.lib import cros_build_lib
108from chromite.lib import osutils
109from chromite.lib import parallel
110
111
112arch = None
113board = None
114compiler = None
115default_board = None
Simon Glass02741682013-05-26 07:07:58 -0700116in_chroot = True
117
Alex Klein1699fab2022-09-08 08:46:06 -0600118kwargs = {
119 "print_cmd": False,
120 "check": False,
Simon Glassdee7d6b2023-02-10 11:35:18 -0700121 "encoding": "utf-8",
Alex Klein1699fab2022-09-08 08:46:06 -0600122}
Simon Glass02741682013-05-26 07:07:58 -0700123
Alex Klein1699fab2022-09-08 08:46:06 -0600124outdir = ""
Simon Glass02741682013-05-26 07:07:58 -0700125
Alex Klein1699fab2022-09-08 08:46:06 -0600126# If you have multiple boards connected on different servo ports, put lines
Simon Glass02741682013-05-26 07:07:58 -0700127# like 'SERVO_PORT{"peach_pit"} = 7777' in your ~/.crosfwrc
128SERVO_PORT = {}
129
Alex Klein1699fab2022-09-08 08:46:06 -0600130src_root = os.path.join(constants.SOURCE_ROOT, "src")
Simon Glass02741682013-05-26 07:07:58 -0700131in_chroot = cros_build_lib.IsInsideChroot()
132
Alex Klein1699fab2022-09-08 08:46:06 -0600133uboard = ""
Simon Glass02741682013-05-26 07:07:58 -0700134
Alex Klein1699fab2022-09-08 08:46:06 -0600135default_board = "peach_pit"
Simon Glass02741682013-05-26 07:07:58 -0700136use_ccache = False
Simon Glass02741682013-05-26 07:07:58 -0700137
138# Special cases for the U-Boot board config, the SOCs and default device tree
139# since the naming is not always consistent.
140# x86 has a lot of boards, but to U-Boot they are all the same
141UBOARDS = {
Alex Klein1699fab2022-09-08 08:46:06 -0600142 "daisy": "smdk5250",
143 "peach": "smdk5420",
Simon Glass02741682013-05-26 07:07:58 -0700144}
Alex Klein1699fab2022-09-08 08:46:06 -0600145for b in [
146 "alex",
147 "butterfly",
148 "emeraldlake2",
149 "link",
150 "lumpy",
151 "parrot",
152 "stout",
153 "stumpy",
154]:
155 UBOARDS[b] = "coreboot-x86"
156 UBOARDS["chromeos_%s" % b] = "chromeos_coreboot"
Simon Glass02741682013-05-26 07:07:58 -0700157
Alex Klein1699fab2022-09-08 08:46:06 -0600158OUT_DIR = "/tmp/crosfw"
Simon Glassb89ae892013-07-18 15:23:35 -0600159
Alex Klein1699fab2022-09-08 08:46:06 -0600160rc_file = os.path.expanduser("~/.crosfwrc")
Simon Glass02741682013-05-26 07:07:58 -0700161if os.path.exists(rc_file):
Mike Frysinger31fdddd2023-02-24 15:50:55 -0500162 with open(rc_file, "rb") as fp:
Alex Klein1699fab2022-09-08 08:46:06 -0600163 # pylint: disable=exec-used
164 exec(compile(fp.read(), rc_file, "exec"))
Simon Glass02741682013-05-26 07:07:58 -0700165
166
Simon Glass02741682013-05-26 07:07:58 -0700167def Dumper(flag, infile, outfile):
Alex Klein1699fab2022-09-08 08:46:06 -0600168 """Run objdump on an input file.
Simon Glass02741682013-05-26 07:07:58 -0700169
Alex Klein1699fab2022-09-08 08:46:06 -0600170 Args:
Trent Apted66736d82023-05-25 10:38:28 +1000171 flag: Flag to pass objdump (e.g. '-d').
172 infile: Input file to process.
173 outfile: Output file to write to.
Alex Klein1699fab2022-09-08 08:46:06 -0600174 """
175 result = cros_build_lib.run(
176 [CompilerTool("objdump"), flag, infile], stdout=outfile, **kwargs
177 )
178 if result.returncode:
179 sys.exit()
Simon Glass02741682013-05-26 07:07:58 -0700180
181
182def CompilerTool(tool):
Alex Klein1699fab2022-09-08 08:46:06 -0600183 """Returns the cross-compiler tool filename.
Simon Glass02741682013-05-26 07:07:58 -0700184
Alex Klein1699fab2022-09-08 08:46:06 -0600185 Args:
Trent Apted66736d82023-05-25 10:38:28 +1000186 tool: Tool name to return, e.g. 'size'.
Simon Glass02741682013-05-26 07:07:58 -0700187
Alex Klein1699fab2022-09-08 08:46:06 -0600188 Returns:
Trent Apted66736d82023-05-25 10:38:28 +1000189 Filename of requested tool.
Alex Klein1699fab2022-09-08 08:46:06 -0600190 """
191 return "%s%s" % (compiler, tool)
Simon Glass02741682013-05-26 07:07:58 -0700192
193
194def ParseCmdline(argv):
Alex Klein1699fab2022-09-08 08:46:06 -0600195 """Parse all command line options.
Simon Glass02741682013-05-26 07:07:58 -0700196
Alex Klein1699fab2022-09-08 08:46:06 -0600197 Args:
Trent Apted66736d82023-05-25 10:38:28 +1000198 argv: Arguments to parse.
Simon Glass02741682013-05-26 07:07:58 -0700199
Alex Klein1699fab2022-09-08 08:46:06 -0600200 Returns:
Trent Apted66736d82023-05-25 10:38:28 +1000201 The parsed options object
Alex Klein1699fab2022-09-08 08:46:06 -0600202 """
Simon Glass95a316d2023-02-10 11:31:47 -0700203 parser = commandline.ArgumentParser(
204 description=__doc__, default_log_level="notice"
205 )
Alex Klein1699fab2022-09-08 08:46:06 -0600206 parser.add_argument(
Alex Klein1699fab2022-09-08 08:46:06 -0600207 "-B",
208 "--build",
209 action="store_false",
210 default=True,
211 help="Don't build U-Boot, just configure device tree",
212 )
213 parser.add_argument(
Alex Klein1699fab2022-09-08 08:46:06 -0600214 "--dt",
Alex Klein1699fab2022-09-08 08:46:06 -0600215 help="Select name of device tree file to use",
216 )
217 parser.add_argument(
Simon Glasse94b4a42023-02-15 06:35:55 -0700218 "--dtb",
219 type="file_exists",
220 help="Select a binary .dtb, passed to the U-Boot build using EXT_DTB",
221 )
222 parser.add_argument(
Simon Glass77cc6ea2023-02-12 08:04:43 -0700223 "-f",
224 "--force-reconfig",
Alex Klein1699fab2022-09-08 08:46:06 -0600225 action="store_true",
226 default=False,
Simon Glass77cc6ea2023-02-12 08:04:43 -0700227 help="Reconfigure before building",
228 )
229 parser.add_argument(
230 "-F",
231 "--force-distclean",
232 action="store_true",
233 default=False,
234 help="Run distclean and reconfigure before building",
Alex Klein1699fab2022-09-08 08:46:06 -0600235 )
236 parser.add_argument(
Simon Glass73a2f482023-02-15 06:18:50 -0700237 "-j",
238 "--jobs",
239 type=int,
240 default=os.cpu_count(),
241 help="Select the number of CPUs to use (defaults to all)",
242 )
243 parser.add_argument(
Simon Glass37da44f2023-02-15 06:30:58 -0700244 "-I",
245 "--in-tree",
246 action="store_true",
247 default=False,
248 help="Build in-tree",
249 )
250 parser.add_argument(
Simon Glassd080aa72023-02-14 07:04:52 -0700251 "-L",
252 "--no-lto",
253 dest="lto",
254 action="store_false",
255 default=True,
256 help="Disable Link-time Optimisation (LTO) when building",
257 )
258 parser.add_argument(
Alex Klein1699fab2022-09-08 08:46:06 -0600259 "-O",
260 "--objdump",
261 action="store_true",
262 default=False,
263 help="Write disassembly output",
264 )
265 parser.add_argument(
Alex Klein1699fab2022-09-08 08:46:06 -0600266 "-t",
267 "--trace",
268 action="store_true",
269 default=False,
270 help="Enable trace support",
271 )
272 parser.add_argument(
Simon Glassa68488c2023-02-09 16:53:47 -0700273 "-T",
274 "--target",
275 nargs="?",
276 default="all",
277 help="Select target to build",
278 )
279 parser.add_argument(
Alex Klein1699fab2022-09-08 08:46:06 -0600280 "-V",
281 "--verified",
282 action="store_true",
283 default=False,
284 help="Include Chrome OS verified boot components",
285 )
286 parser.add_argument(
Alex Klein1699fab2022-09-08 08:46:06 -0600287 "-z",
288 "--size",
289 action="store_true",
290 default=False,
291 help="Display U-Boot image size",
292 )
293 parser.add_argument(
Simon Glassa68488c2023-02-09 16:53:47 -0700294 "board",
295 type=str,
296 default=default_board,
297 help="Select board to build (daisy/peach_pit/link)",
Alex Klein1699fab2022-09-08 08:46:06 -0600298 )
299 return parser.parse_args(argv)
Simon Glass02741682013-05-26 07:07:58 -0700300
301
302def SetupBuild(options):
Alex Klein1699fab2022-09-08 08:46:06 -0600303 """Set up parameters needed for the build.
Simon Glass02741682013-05-26 07:07:58 -0700304
Alex Klein1699fab2022-09-08 08:46:06 -0600305 This checks the current environment and options and sets up various things
306 needed for the build, including 'base' which holds the base flags for
307 passing to the U-Boot Makefile.
Simon Glass02741682013-05-26 07:07:58 -0700308
Alex Klein1699fab2022-09-08 08:46:06 -0600309 Args:
Trent Apted66736d82023-05-25 10:38:28 +1000310 options: Command line options
Simon Glass02741682013-05-26 07:07:58 -0700311
Alex Klein1699fab2022-09-08 08:46:06 -0600312 Returns:
Trent Apted66736d82023-05-25 10:38:28 +1000313 Base flags to use for U-Boot, as a list.
Alex Klein1699fab2022-09-08 08:46:06 -0600314 """
315 # pylint: disable=global-statement
Simon Glass826b1df2023-03-02 05:46:45 -0700316 global arch, board, compiler, outdir, uboard
Simon Glass02741682013-05-26 07:07:58 -0700317
Simon Glass9674afc2023-02-10 11:21:30 -0700318 logging.info("Building for %s", options.board)
Simon Glass02741682013-05-26 07:07:58 -0700319
Alex Klein1699fab2022-09-08 08:46:06 -0600320 # Separate out board_variant string: "peach_pit" becomes "peach", "pit".
321 # But don't mess up upstream boards which use _ in their name.
322 parts = options.board.split("_")
323 if parts[0] in ["daisy", "peach"]:
324 board = parts[0]
Simon Glass02741682013-05-26 07:07:58 -0700325 else:
Alex Klein1699fab2022-09-08 08:46:06 -0600326 board = options.board
Simon Glass02741682013-05-26 07:07:58 -0700327
Alex Klein1699fab2022-09-08 08:46:06 -0600328 # To allow this to be run from 'cros_sdk'
329 if in_chroot:
330 os.chdir(os.path.join(src_root, "third_party", "u-boot", "files"))
Simon Glass02741682013-05-26 07:07:58 -0700331
Alex Klein1699fab2022-09-08 08:46:06 -0600332 base_board = board
Simon Glass02741682013-05-26 07:07:58 -0700333
Alex Klein1699fab2022-09-08 08:46:06 -0600334 if options.verified:
335 base_board = "chromeos_%s" % base_board
Simon Glass02741682013-05-26 07:07:58 -0700336
Alex Klein1699fab2022-09-08 08:46:06 -0600337 uboard = UBOARDS.get(base_board, base_board)
Simon Glass9674afc2023-02-10 11:21:30 -0700338 logging.info("U-Boot board is %s", uboard)
Simon Glass02741682013-05-26 07:07:58 -0700339
Alex Klein1699fab2022-09-08 08:46:06 -0600340 # Pull out some information from the U-Boot boards config file
Alex Klein1699fab2022-09-08 08:46:06 -0600341 (PRE_KBUILD, PRE_KCONFIG, KCONFIG) = range(3)
342 if os.path.exists("MAINTAINERS"):
343 board_format = PRE_KBUILD
344 else:
345 board_format = PRE_KCONFIG
Simon Glass0b847c42023-06-15 11:37:19 +0100346
347 # Create the boards.cfg file if missing.
348 if not os.path.exists("board.cfg"):
349 cros_build_lib.run(["buildman", "-R"], **kwargs)
350
351 # Buildman puts it in the directory above, so move it.
352 # https://source.denx.de/u-boot/u-boot/-/issues/17
353 os.rename("../boards.cfg", "boards.cfg")
354
Mike Frysinger31fdddd2023-02-24 15:50:55 -0500355 with open("boards.cfg", encoding="utf-8") as f:
Alex Klein1699fab2022-09-08 08:46:06 -0600356 for line in f:
357 if "genboardscfg" in line:
358 board_format = KCONFIG
359 if uboard in line:
360 if line[0] == "#":
361 continue
362 fields = line.split()
363 if not fields:
364 continue
Simon Glassa44a1f92023-02-09 16:31:34 -0700365 target = fields[6]
366 # Make sure this is the right target.
367 if target != uboard:
368 continue
Alex Klein1699fab2022-09-08 08:46:06 -0600369 arch = fields[1]
370 fields += [None, None, None]
Simon Glass826b1df2023-03-02 05:46:45 -0700371 if board_format in (PRE_KCONFIG, KCONFIG):
Alex Klein1699fab2022-09-08 08:46:06 -0600372 target = fields[0]
Alex Klein1699fab2022-09-08 08:46:06 -0600373 if not arch:
374 cros_build_lib.Die(
375 "Selected board '%s' not found in boards.cfg." % board
376 )
Simon Glass02741682013-05-26 07:07:58 -0700377
Alex Klein1699fab2022-09-08 08:46:06 -0600378 vboot = os.path.join("build", board, "usr")
Simon Glass4158d732023-02-17 11:38:31 -0700379 if arch == "sandbox":
380 compiler = ""
381 elif in_chroot:
Simon Glassa44a1f92023-02-09 16:31:34 -0700382 if arch == "x86":
383 compiler = "i686-cros-linux-gnu-"
384 elif arch == "arm":
Simon Glassa3456622023-03-02 05:46:45 -0700385 compiler = "armv7a-cros-linux-gnueabihf-"
Simon Glassa44a1f92023-02-09 16:31:34 -0700386 elif arch == "aarch64":
Simon Glassa3456622023-03-02 05:46:45 -0700387 compiler = "aarch64-cros-linux-gnu-"
Alex Klein1699fab2022-09-08 08:46:06 -0600388 else:
Simon Glassa44a1f92023-02-09 16:31:34 -0700389 result = cros_build_lib.run(
390 ["buildman", "-A", "--boards", options.board],
391 capture_output=True,
Simon Glassa44a1f92023-02-09 16:31:34 -0700392 **kwargs,
393 )
394 compiler = result.stdout.strip()
395 if not compiler:
396 cros_build_lib.Die("Selected arch '%s' not supported.", arch)
Simon Glass02741682013-05-26 07:07:58 -0700397
Alex Klein1699fab2022-09-08 08:46:06 -0600398 base = [
399 "make",
Simon Glass73a2f482023-02-15 06:18:50 -0700400 "-j%d" % options.jobs,
Alex Klein1699fab2022-09-08 08:46:06 -0600401 "CROSS_COMPILE=%s" % compiler,
402 "--no-print-directory",
403 "HOSTSTRIP=true",
Alex Klein1699fab2022-09-08 08:46:06 -0600404 "QEMU_ARCH=",
Simon Glass0c0617f2023-02-19 18:32:16 -0700405 "KCONFIG_NOSILENTUPDATE=1",
Alex Klein1699fab2022-09-08 08:46:06 -0600406 ]
Simon Glassf665da62023-02-15 06:40:51 -0700407 if options.dt:
408 base.append(f"DEVICE_TREE={options.dt}")
Simon Glass37da44f2023-02-15 06:30:58 -0700409 if not options.in_tree:
410 outdir = os.path.join(OUT_DIR, uboard)
411 base.append(f"O={outdir}")
Simon Glassd080aa72023-02-14 07:04:52 -0700412 if not options.lto:
413 base.append("NO_LTO=1")
Simon Glasse94b4a42023-02-15 06:35:55 -0700414 if options.dtb:
415 base.append(f"EXT_DTB={options.dtb}")
Simon Glassd080aa72023-02-14 07:04:52 -0700416
Simon Glass95a316d2023-02-10 11:31:47 -0700417 # Enable quiet output at INFO level, everything at DEBUG level
418 if logging.getLogger().getEffectiveLevel() <= logging.DEBUG:
Alex Klein1699fab2022-09-08 08:46:06 -0600419 base.append("V=1")
Simon Glass95a316d2023-02-10 11:31:47 -0700420 elif logging.getLogger().getEffectiveLevel() >= logging.NOTICE:
421 base.append("-s")
Simon Glass02741682013-05-26 07:07:58 -0700422
Alex Klein1699fab2022-09-08 08:46:06 -0600423 if options.verified:
424 base += [
425 "VBOOT=%s" % vboot,
426 "MAKEFLAGS_VBOOT=DEBUG=1",
427 "QUIET=1",
428 "CFLAGS_EXTRA_VBOOT=-DUNROLL_LOOPS",
429 "VBOOT_SOURCE=%s/platform/vboot_reference" % src_root,
430 ]
431 base.append("VBOOT_DEBUG=1")
432
Alex Klein1699fab2022-09-08 08:46:06 -0600433 base.append("BUILD_ROM=1")
434 if options.trace:
435 base.append("FTRACE=1")
Alex Klein1699fab2022-09-08 08:46:06 -0600436
Simon Glass77cc6ea2023-02-12 08:04:43 -0700437 if not options.force_reconfig:
Alex Klein1699fab2022-09-08 08:46:06 -0600438 config_mk = "%s/include/autoconf.mk" % outdir
439 if not os.path.exists(config_mk):
Simon Glass77cc6ea2023-02-12 08:04:43 -0700440 logging.warning("No build found for %s - adding -f", board)
441 options.force_reconfig = True
Alex Klein1699fab2022-09-08 08:46:06 -0600442
443 config_mk = "include/autoconf.mk"
444 if os.path.exists(config_mk):
Simon Glass37da44f2023-02-15 06:30:58 -0700445 logging.warning("Warning: '%s' exists, try 'make mrproper'", config_mk)
Alex Klein1699fab2022-09-08 08:46:06 -0600446
Alex Klein1699fab2022-09-08 08:46:06 -0600447 return base
Simon Glass02741682013-05-26 07:07:58 -0700448
449
Simon Glassf78c9b52023-02-14 06:58:10 -0700450def CheckConfigChange() -> bool:
451 """See if we need to reconfigure due to config files changing
452
453 Checks if any defconfig or Kconfig file has changed in the source tree
454 since the last time U-Boot was configured for this build. For simplicity,
455 any defconfig change will trigger this, not just one for the board being
456 built, since the cost of a reconfigure is fairly small.
457
458 Returns:
459 True if any config file has changed since U-Boot was last configured
460 """
461 fname = os.path.join(outdir, ".config")
462 ref_time = os.path.getctime(fname)
463 for p in Path.cwd().glob("configs/*"):
464 if p.stat().st_ctime > ref_time:
465 logging.warning("config/ dir has changed - adding -f")
466 return True
467
468 for p in Path.cwd().glob("**/Kconfig*"):
469 if p.stat().st_ctime > ref_time:
470 logging.warning("Kconfig file(s) changed - adding -f")
471 return True
472
473 return False
474
475
Simon Glass02741682013-05-26 07:07:58 -0700476def RunBuild(options, base, target, queue):
Alex Klein1699fab2022-09-08 08:46:06 -0600477 """Run the U-Boot build.
Simon Glass02741682013-05-26 07:07:58 -0700478
Alex Klein1699fab2022-09-08 08:46:06 -0600479 Args:
Trent Apted66736d82023-05-25 10:38:28 +1000480 options: Command line options.
481 base: Base U-Boot flags.
482 target: Target to build.
483 queue: A parallel queue to add jobs to.
Alex Klein1699fab2022-09-08 08:46:06 -0600484 """
Simon Glass9674afc2023-02-10 11:21:30 -0700485 logging.info("U-Boot build flags: %s", " ".join(base))
Simon Glass02741682013-05-26 07:07:58 -0700486
Simon Glass77cc6ea2023-02-12 08:04:43 -0700487 if options.force_distclean:
488 options.force_reconfig = True
Alex Klein1699fab2022-09-08 08:46:06 -0600489 # Ignore any error from this, some older U-Boots fail on this.
Simon Glassdee7d6b2023-02-10 11:35:18 -0700490 cros_build_lib.run(base + ["distclean"], capture_output=True, **kwargs)
Simon Glass77cc6ea2023-02-12 08:04:43 -0700491
Simon Glassf78c9b52023-02-14 06:58:10 -0700492 if not options.force_reconfig:
493 options.force_reconfig = CheckConfigChange()
494
Simon Glass77cc6ea2023-02-12 08:04:43 -0700495 # Reconfigure U-Boot.
496 if options.force_reconfig:
Alex Klein1699fab2022-09-08 08:46:06 -0600497 if os.path.exists("tools/genboardscfg.py"):
498 mtarget = "defconfig"
499 else:
500 mtarget = "config"
501 cmd = base + ["%s_%s" % (uboard, mtarget)]
502 result = cros_build_lib.run(
503 cmd, stdout=True, stderr=subprocess.STDOUT, **kwargs
504 )
Simon Glassdee7d6b2023-02-10 11:35:18 -0700505 if (
506 result.returncode
507 or logging.getLogger().getEffectiveLevel() <= logging.DEBUG
508 ):
509 print(f"cmd: {result.cmdstr}")
510 print(result.stdout, file=sys.stderr)
511 if result.returncode:
512 sys.exit(result.returncode)
Simon Glass02741682013-05-26 07:07:58 -0700513
Alex Klein1699fab2022-09-08 08:46:06 -0600514 # Do the actual build.
515 if options.build:
516 result = cros_build_lib.run(
Simon Glass0c0617f2023-02-19 18:32:16 -0700517 base + [target],
518 input="",
Simon Glassc12ccd72023-03-01 13:35:29 -0700519 capture_output=True,
Simon Glass0c0617f2023-02-19 18:32:16 -0700520 **kwargs,
Alex Klein1699fab2022-09-08 08:46:06 -0600521 )
Simon Glassdee7d6b2023-02-10 11:35:18 -0700522 if (
523 result.returncode
524 or logging.getLogger().getEffectiveLevel() <= logging.INFO
Simon Glassc12ccd72023-03-01 13:35:29 -0700525 or result.stderr
Simon Glassdee7d6b2023-02-10 11:35:18 -0700526 ):
Alex Klein1699fab2022-09-08 08:46:06 -0600527 # The build failed, so output the results to stderr.
Simon Glassc12ccd72023-03-01 13:35:29 -0700528 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 Glassdee7d6b2023-02-10 11:35:18 -0700536 if result.returncode:
537 sys.exit(result.returncode)
Simon Glass02741682013-05-26 07:07:58 -0700538
Alex Klein1699fab2022-09-08 08:46:06 -0600539 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:
544 result = cros_build_lib.run([CompilerTool("size")] + files, **kwargs)
545 if result.returncode:
546 sys.exit()
Simon Glass02741682013-05-26 07:07:58 -0700547
Alex Klein1699fab2022-09-08 08:46:06 -0600548 # 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 Glass02741682013-05-26 07:07:58 -0700558
Simon Glass9674afc2023-02-10 11:21:30 -0700559 logging.info("Output directory %s", outdir)
Simon Glass02741682013-05-26 07:07:58 -0700560
561
Simon Glass02741682013-05-26 07:07:58 -0700562def main(argv):
Simon Glasse6201d72023-02-09 16:42:22 -0700563 """Main function for script to build firmware.
Simon Glass02741682013-05-26 07:07:58 -0700564
Alex Klein1699fab2022-09-08 08:46:06 -0600565 Args:
Trent Apted66736d82023-05-25 10:38:28 +1000566 argv: Program arguments.
Alex Klein1699fab2022-09-08 08:46:06 -0600567 """
568 options = ParseCmdline(argv)
569 base = SetupBuild(options)
Simon Glass02741682013-05-26 07:07:58 -0700570
Alex Klein1699fab2022-09-08 08:46:06 -0600571 with parallel.BackgroundTaskRunner(Dumper) as queue:
572 RunBuild(options, base, options.target, queue)
Simon Glass02741682013-05-26 07:07:58 -0700573
Alex Klein1699fab2022-09-08 08:46:06 -0600574 if options.objdump:
Simon Glass9674afc2023-02-10 11:21:30 -0700575 logging.info("Writing diasssembly files")