Received: by 2002:a89:413:0:b0:1fd:dba5:e537 with SMTP id m19csp22993lqs; Thu, 13 Jun 2024 02:35:54 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXUQInCHcQS9IwZny+kAVt/h0OxzjNVpGZOVPOcQQP8x8vlywhOntNTD4ewt8QzvQBIBNyNY5wQXnJoZIKrI+1F1AjP2PeU2SPyNYQlpQ== X-Google-Smtp-Source: AGHT+IH0ZkyiLd9IA2wFP5oyYJPZWsprNVWVsFi1S96PKLggaDSt0kdTEkl/wvqmj73ZmYFRrdz9 X-Received: by 2002:a05:620a:471f:b0:795:5bdb:44ca with SMTP id af79cd13be357-797f60a417cmr446172185a.18.1718271354089; Thu, 13 Jun 2024 02:35:54 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718271354; cv=pass; d=google.com; s=arc-20160816; b=U4GE2rQZyuCz9ag6CaG225oOzJb9015YmZzOb+8KAsUyCtOWwptZbJfl9OBiUdxrA7 aIrQSfo3YSVINqdTzNC3XVjsz+FsN/TiSxG4gXjj1tA1fBnhsIE0prqyelXzAlRvwvG2 Wi2rsDOebqfs8zO0qv+GRNPWa8jXM61BKmPCr/EZyu8DeUmKsahCTwh8B8vuWDLNt28z Ztb+OK8gltnkRYu9h9XqiNhuPMxYYw/sDFha9w3N6+7SJDO0kBohSrl5QB12VOQinGNP ORMh3HAWXK/iyHyJ3KffZhnaoJbctzDWhm4kUoBuMdUmytIhvJTI9BIRuI96XMZYmaa8 teiQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=VwsSHgS8s2wQj0WzRoqwu97IEcpIHsOcGhEZ3yrM0mk=; fh=MdshvkKYZKkKTrK/qce+62k8pGDbNgXQHqXu2wDoP04=; b=abqHKfGIfA5g8ryyDV65IXpuVfNE/lmfHRHSXzyngJtEqhkBMr/YtNWnbHK/xdyzhv g2eZu3gFOcPGzinonwSKusNVk6RVc8MK9pN3Duvw5BAtje96rrsgZnyMbQcwZY3MfIer 9bU5ZFAoZsJzq4sIzIVRVi+Z7woIvOHjhKrddI1Wvs9TV7CXpLeJ2tcfc8x59MvNO3CM 5Gp0A1bddtCy9rOXWwdhfaphKJHFOzeHPmKI2h/3mUMQYFSJlDjUKYNkpkM6kgFHJGMR YHSu0d6Ij1Gq19DIF9e23x3crFRhtLHFPrXCU/rnN9HJQ57NleuKKWdQmifIZ2eCWnw2 WzFw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=R82YjmPv; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-212962-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-212962-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id d75a77b69052e-441ef3bbcaasi10315311cf.22.2024.06.13.02.35.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Jun 2024 02:35:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-212962-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=R82YjmPv; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-212962-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-212962-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id BB6341C234C8 for ; Thu, 13 Jun 2024 09:35:53 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 74FC4140E2B; Thu, 13 Jun 2024 09:34:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="R82YjmPv" Received: from mail-pg1-f196.google.com (mail-pg1-f196.google.com [209.85.215.196]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 842F014039D for ; Thu, 13 Jun 2024 09:34:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.196 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718271290; cv=none; b=kUJVB+Y8xraB7QHRKQjFr0QvRX99NdYkEYcDuTfsMpY0WZ3bBHGk5vLGrcWkGEoX/OPWvQXfm3oCZjyqBbDo23KJpzOw3J17yvBhcIz6PTaM7TQzpINaLZw7guv4JLu3zyAA2Nx5nu6rxOnaFkiRZjGQsh843O9vtqCtLBVHslI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718271290; c=relaxed/simple; bh=Wk19CPlfuV7oR9ir/74te7+uM1iysqvQXTOBDktBM1Q=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=S6sIHugRBYexYJZafpTDC9nzS1XgDHW6oP8ftPeEqP6vtrKulNfss7tmlAy9gjXYJS8pUawQ3ztdoBe75mKBLss1i/0oS//LbxPlmV+AJA29/K30LjOGCLPqUN3+9U3VAgKgFquLdl5C2zVAo+ksIbmNklnwFxC9lYwdYDM6om8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=R82YjmPv; arc=none smtp.client-ip=209.85.215.196 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Received: by mail-pg1-f196.google.com with SMTP id 41be03b00d2f7-6ce533b6409so590982a12.1 for ; Thu, 13 Jun 2024 02:34:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1718271288; x=1718876088; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=VwsSHgS8s2wQj0WzRoqwu97IEcpIHsOcGhEZ3yrM0mk=; b=R82YjmPvw8NojL4wTS9+d6oT9nlbTKTLuepWU7/bpi++o7sB7JkuCP5sKdFJM/a/88 xcyf6OijmM8A3NJy43DWHnWGcZXlerwpR/Ao/OmtIIe1/2e3CKd3lGclzOcXthHu4QKU xHq9rBGCEvw5hxxwUs+mhzuaNXKxl9WO4KIuc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718271288; x=1718876088; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=VwsSHgS8s2wQj0WzRoqwu97IEcpIHsOcGhEZ3yrM0mk=; b=aQBs4mCp+53v6RK9N25gjkaBok5KED/1qgrCS7yqmw4ZDKMaGBmrOVlIDRFLBHrED+ uwWY4n7CgEg5gf11DS0fCGA6HARRt75GbNj3+gh91Uw1FW5Gpy2MrSTSjb5huwIxRUK+ V8WcehCcBfvWgkT9sDmoVIf5K7/sPHWmBhygHoLLhe22x2hVom3kDOgMbagZ9/Mj/CkB vtyYxyzOLNkTGspJkVgjFTluNbwvoH4dzL3K5gChtFPt62SQutEFOgcBM5H7tfwCyR5C XtHQQTtEksq6ubZSptzIu7nsbo4i0y2Uz6vy7ag2A1Y6O+NVc23KzqY7kQKiaMvItwW8 T2ig== X-Forwarded-Encrypted: i=1; AJvYcCVf21mgYVxTGkWGBuuUuzFBeRXi4ZgVmVEO5QdcRBmrYy9wyj9/AoHmSkDsc1hExaLbRtlRiP8xW8xVbc4RTzSqvJBPPZD8kMEmZ9IW X-Gm-Message-State: AOJu0Yyqsm2FGHsdAwI881dDbwNY3I1YzgoX/nLrL2bZh+UAdHz0MAnf vvoEv8VIKYZehUtTJb+KYzUZC5FxFLNp7poqjC8pm9Qx/fIEehCI31EUSibX4w== X-Received: by 2002:a05:6a20:6a1d:b0:1b5:a58b:1acd with SMTP id adf61e73a8af0-1b8a9b76456mr5940574637.1.1718271287662; Thu, 13 Jun 2024 02:34:47 -0700 (PDT) Received: from wenstp920.tpe.corp.google.com ([2401:fa00:1:10:324e:368c:b7ef:8bd2]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f855e6e42csm9287625ad.73.2024.06.13.02.34.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Jun 2024 02:34:47 -0700 (PDT) From: Chen-Yu Tsai To: Simon Glass , Masahiro Yamada , Nathan Chancellor , Nicolas Schier Cc: Chen-Yu Tsai , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-kbuild@vger.kernel.org Subject: [PATCH v2] scripts/make_fit: Support decomposing DTBs Date: Thu, 13 Jun 2024 17:34:32 +0800 Message-ID: <20240613093433.131699-1-wenst@chromium.org> X-Mailer: git-send-email 2.45.2.627.g7a2c4fd464-goog Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The kernel tree builds some "composite" DTBs, where the final DTB is the result of applying one or more DTB overlays on top of a base DTB with fdtoverlay. The FIT image specification already supports configurations having one base DTB and overlays applied on top. It is then up to the bootloader to apply said overlays and either use or pass on the final result. This allows the FIT image builder to reuse the same FDT images for multiple configurations, if such cases exist. The decomposition function depends on the kernel build system, reading back the .cmd files for the to-be-packaged DTB files to check for the fdtoverlay command being called. This will not work outside the kernel tree. The function is off by default to keep compatibility with possible existing users. To facilitate the decomposition and keep the code clean, the model and compatitble string extraction have been moved out of the output_dtb function. The FDT image description is replaced with the base file name of the included image. Signed-off-by: Chen-Yu Tsai --- Changes since v1: - Replace OrderedDict with standard {} dict - Change short form command line argument to -D - Drop [] around "'fdt-{x}\x00' for x in files" - Add spaces around '+' in slice argument - Split out DTB parsing into separate function Simon's reviewed-by was dropped. This is a feature I alluded to in my replies to Simon's original submission of the make_fit.py script [1]. This is again made a runtime argument as not all firmware out there that boot FIT images support applying overlays. Like my previous submission for disabling compression for included FDT images, the bootloader found in RK3399 and MT8173 Chromebooks do not support applying overlays. Another case of this is U-boot shipped by development board vendors in binary form (without upstream) in an image or in SPI flash on the board that were built with OF_LIBFDT_OVERLAY=n. These would fail to boot FIT images with DT overlays. One such example is my Hummingboard Pulse. In these cases the firmware is either not upgradable or very hard to upgrade. I believe there is value in supporting these cases. A common script shipped with the kernel source that can be shared by distros means the distro people don't have to reimplement this in their downstream repos or meta-packages. For ChromeOS this means reducing the amount of package code we have in shell script. [1] https://lore.kernel.org/linux-kbuild/20231207142723.GA3187877@google.com/ scripts/Makefile.lib | 1 + scripts/make_fit.py | 86 ++++++++++++++++++++++++++++++++------------ 2 files changed, 65 insertions(+), 22 deletions(-) diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib index 9f06f6aaf7fc..d78b5d38beaa 100644 --- a/scripts/Makefile.lib +++ b/scripts/Makefile.lib @@ -522,6 +522,7 @@ quiet_cmd_fit = FIT $@ cmd_fit = $(MAKE_FIT) -o $@ --arch $(UIMAGE_ARCH) --os linux \ --name '$(UIMAGE_NAME)' \ $(if $(findstring 1,$(KBUILD_VERBOSE)),-v) \ + $(if $(FIT_DECOMPOSE_DTBS),--decompose-dtbs) \ --compress $(FIT_COMPRESSION) -k $< @$(word 2,$^) # XZ diff --git a/scripts/make_fit.py b/scripts/make_fit.py index 263147df80a4..4a1bb2f55861 100755 --- a/scripts/make_fit.py +++ b/scripts/make_fit.py @@ -22,6 +22,11 @@ the entire FIT. Use -c to compress the data, using bzip2, gzip, lz4, lzma, lzo and zstd algorithms. +Use -D to decompose "composite" DTBs into their base components and +deduplicate the resulting base DTBs and DTB overlays. This requires the +DTBs to be sourced from the kernel build directory, as the implementation +looks at the .cmd files produced by the kernel build. + The resulting FIT can be booted by bootloaders which support FIT, such as U-Boot, Linuxboot, Tianocore, etc. @@ -64,6 +69,8 @@ def parse_args(): help='Specifies the architecture') parser.add_argument('-c', '--compress', type=str, default='none', help='Specifies the compression') + parser.add_argument('-D', '--decompose-dtbs', action='store_true', + help='Decompose composite DTBs into base DTB and overlays') parser.add_argument('-E', '--external', action='store_true', help='Convert the FIT to use external data') parser.add_argument('-n', '--name', type=str, required=True, @@ -140,12 +147,12 @@ def finish_fit(fsw, entries): fsw.end_node() seq = 0 with fsw.add_node('configurations'): - for model, compat in entries: + for model, compat, files in entries: seq += 1 with fsw.add_node(f'conf-{seq}'): fsw.property('compatible', bytes(compat)) fsw.property_string('description', model) - fsw.property_string('fdt', f'fdt-{seq}') + fsw.property('fdt', bytes(''.join(f'fdt-{x}\x00' for x in files), "ascii")) fsw.property_string('kernel', 'kernel') fsw.end_node() @@ -193,21 +200,9 @@ def output_dtb(fsw, seq, fname, arch, compress): fname (str): Filename containing the DTB arch: FIT architecture, e.g. 'arm64' compress (str): Compressed algorithm, e.g. 'gzip' - - Returns: - tuple: - str: Model name - bytes: Compatible stringlist """ with fsw.add_node(f'fdt-{seq}'): - # Get the compatible / model information - with open(fname, 'rb') as inf: - data = inf.read() - fdt = libfdt.FdtRo(data) - model = fdt.getprop(0, 'model').as_str() - compat = fdt.getprop(0, 'compatible') - - fsw.property_string('description', model) + fsw.property_string('description', os.path.basename(fname)) fsw.property_string('type', 'flat_dt') fsw.property_string('arch', arch) fsw.property_string('compression', compress) @@ -215,9 +210,45 @@ def output_dtb(fsw, seq, fname, arch, compress): with open(fname, 'rb') as inf: compressed = compress_data(inf, compress) fsw.property('data', compressed) - return model, compat +def process_dtb(fname, args): + """Process an input DTB, decomposing it if requested and is possible + + Args: + fname (str): Filename containing the DTB + args (Namespace): Program arguments + Returns: + tuple: + str: Model name string + str: Root compatible string + files: list of filenames corresponding to the DTB + """ + # Get the compatible / model information + with open(fname, 'rb') as inf: + data = inf.read() + fdt = libfdt.FdtRo(data) + model = fdt.getprop(0, 'model').as_str() + compat = fdt.getprop(0, 'compatible') + + if args.decompose_dtbs: + # Check if the DTB needs to be decomposed + path, basename = os.path.split(fname) + cmd_fname = os.path.join(path, f'.{basename}.cmd') + with open(cmd_fname, 'r', encoding='ascii') as inf: + cmd = inf.read() + + if 'scripts/dtc/fdtoverlay' in cmd: + # This depends on the structure of the composite DTB command + files = cmd.split() + files = files[files.index('-i') + 1:] + else: + files = [fname] + else: + files = [fname] + + return (model, compat, files) + def build_fit(args): """Build the FIT from the provided files and arguments @@ -235,6 +266,7 @@ def build_fit(args): fsw = libfdt.FdtSw() setup_fit(fsw, args.name) entries = [] + fdts = {} # Handle the kernel with open(args.kernel, 'rb') as inf: @@ -243,12 +275,22 @@ def build_fit(args): write_kernel(fsw, comp_data, args) for fname in args.dtbs: - # Ignore overlay (.dtbo) files - if os.path.splitext(fname)[1] == '.dtb': - seq += 1 - size += os.path.getsize(fname) - model, compat = output_dtb(fsw, seq, fname, args.arch, args.compress) - entries.append([model, compat]) + # Ignore non-DTB (*.dtb) files + if os.path.splitext(fname)[1] != '.dtb': + continue + + (model, compat, files) = process_dtb(fname, args) + + for fn in files: + if fn not in fdts: + seq += 1 + size += os.path.getsize(fn) + output_dtb(fsw, seq, fn, args.arch, args.compress) + fdts[fn] = seq + + files_seq = [fdts[fn] for fn in files] + + entries.append([model, compat, files_seq]) finish_fit(fsw, entries) -- 2.45.2.627.g7a2c4fd464-goog