Received: by 2002:ab2:6f44:0:b0:1fd:c486:4f03 with SMTP id l4csp224768lqq; Thu, 13 Jun 2024 00:45:53 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUh/wVpbYw4NTGtRM4QumJWb47rww5YnoLK19ce4DwaW0UNBItm3tMS5wcURgJKaR3anSj7b77R0oqQJNGQfxCCNjdxvCPCEyUj3z3riA== X-Google-Smtp-Source: AGHT+IEjHxYH0iHjb88/nDfAwBAplXhSY13UdCUDkq+KNGaO03VDslEqQzMKOFnHzccdHaqsieZk X-Received: by 2002:a17:902:d50b:b0:1f7:2ba:4c1f with SMTP id d9443c01a7336-1f83b567833mr49474945ad.22.1718264752918; Thu, 13 Jun 2024 00:45:52 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718264752; cv=pass; d=google.com; s=arc-20160816; b=LCC8Rovb7YEYeFvArYDLHnZe9i3QJGzgRLdHllI7/Onubiu8YWcD+z5rRSdLNPDvHt /Whb5Mh0hY4Y1S+X/RDxh0vHoHd59Ti9kONgmuIojuTLkDDnS5b+tfl7/FHImLefoH03 gm+m2mv6juSogScRNFDqvBxHhVkasgGQ4Y2lvN/ZxKxZW3xU4LCHNJsb7TuZZGATQx/k Ne/9uZRaA5miXXybV6iEYEyA0DFUJeMKPnkU/bE+xpsku2JDxqo7XHNRaFiRTUes96Wi AmrDqN5dFK7Mcm/AUvTZZX3iaR3NAJuHAeM+LYgqskZfPNrH/WgFMIubBkhRVsy/+XVz DBjg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=DoUfCuEwCpKD3p7FxzACAXcXvlyOy49T5PY4S52/dYw=; fh=CK0WsvxMhAGJOLd//BSmdnPVTWwDHJHQSUYIb1hRjns=; b=jdo0o1klLJ++JdsEkfeiYh6/F+vpn91xgwigIZBazr8xodvy0ewD2MffgeRbCWpf62 dYlGOOw4285lKhcEBY4Z99AQrwIeO770z55q13lQfpDvKqkdlsJBkwqmlm83WrUEeCxl 8Mpf4eSUVSTpJWK9/cLf5LEr8zmki+KMp4sVLrhQtvNtQVK5spuUgqx0I5/vwnHwOFov xxsv2c0YVi5bM+Yuof3Ej3TLo8TEjg7aiBGWX9qfQEsNATWrMrG+hncnKUNKEjmRfjbq UBQqBeNlnI16dX66ddtS0fv9/J3cXjBQpwme5YQiN2xhlfKe0HlGN/ZAz11pA4gZ2ypG FEsA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=aRGa2+TB; 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-212738-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-212738-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id d9443c01a7336-1f855efe055si7680125ad.448.2024.06.13.00.45.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Jun 2024 00:45:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-212738-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=aRGa2+TB; 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-212738-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-212738-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 8128A28129A for ; Thu, 13 Jun 2024 07:45:52 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D58AB136660; Thu, 13 Jun 2024 07:45:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="aRGa2+TB" Received: from mail-lf1-f48.google.com (mail-lf1-f48.google.com [209.85.167.48]) (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 B2811130E44 for ; Thu, 13 Jun 2024 07:45:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.48 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718264744; cv=none; b=PEGdPjui7GveHQbqtvYrTk1buZ/iRmFY1FhJCujOoE9P6b3o0WXv6tuKLnww8jbxcHoF7bAhKzM5tDaMCV774vLwC6/fZSwLldVzqrpPbfcLd9WUipSZGwJWobIZIXYUcicMfpWXgCVf78gfTaJc3MLHrhu4ySV/3gbwgCvrP+g= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718264744; c=relaxed/simple; bh=zpSZlBDNL9kD22wlVaHzbNHGdJH3kmVNXx0DVn3Zo3Y=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=CRKDjl7+oFAAmCEf0RaZ3nmauFi+SQSK2R5RB2Sm3zF+ftuWCnYb4VuWiHDhJyTXNmmT36eaRTprsZN4DtGuV8Lg7atBAILw9nLHCn53/3ZI4BhNLcxdssDuEukqo+0V61hkAHEN3e6UZglp8Hy1fjuNaOihvq4haLJbCuRO9SA= 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=aRGa2+TB; arc=none smtp.client-ip=209.85.167.48 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-lf1-f48.google.com with SMTP id 2adb3069b0e04-52bd48cf36bso961730e87.3 for ; Thu, 13 Jun 2024 00:45:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1718264741; x=1718869541; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=DoUfCuEwCpKD3p7FxzACAXcXvlyOy49T5PY4S52/dYw=; b=aRGa2+TBRGzI+whXmimQkuuMNYQr8dyJReIpnNWOBBv0gNAv+2fXbp1xxXQB8KDsoH Ozi10JJ825H465GsTQBw3Lnnin/HjjI+hce+aag5+IvNoS6hTQ6Oa6EKpxBiKW24vp3Z RFAe/boN0YYb+vzjz9zZjFWnDRI/dfpZkUklw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718264741; x=1718869541; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=DoUfCuEwCpKD3p7FxzACAXcXvlyOy49T5PY4S52/dYw=; b=pQkm1hADdDovx33LmW4kvi2jdetyFboIdgZEwwAvtKALmLmItT6nK6rEuiY1vUyd3m il6ysRc6WyWQrapLwpeBLRdp0tzGfeQm8ImP+3CD4MZhusapHxowsTy+831ODtLlJ98I KcaYTvEjqSviYT24eXWKhdmSNPQXXBKW2Blb4XYTog4Rdo0wQG1ga7ZI9KTca2fvASvv YOjctwfovvuv+k/9W5KwC6cn49ysfZA2g3VTBqz6saHjm7H8Ksmqd1TsAYMwcSJRLtxO E9SNJE4qv3N9lIWoaQQPJNNmyIxfR/Z0bwamWdSN3J0Kt204Y2dM21h9SkHr7/X5J0aZ sdtA== X-Forwarded-Encrypted: i=1; AJvYcCU1h0b+2j/NTBxAix7YFv0kFb7rfKnls4p010Oc1Xcd3uGOZQPz2/9JQfx3+f6zF2J0WwQlQMHCfdHHlwEDgvJKzOnr5yRgXduvdqIK X-Gm-Message-State: AOJu0YyAwUDySg+GduGp7nZbJYRpSUoN+WIStV2/WI/z4ggPpI8wG9qB NULOFqdhwovbAIqe0lyBtqq7+2/mWn9d0UWHbwDL5SAYN5R0W1cOvNcpl4EI38m6Sd8CmF/s0sK /pqmIh+ySEdEEN6pF7zCxXzebyjW5TyUpEAGk X-Received: by 2002:a05:6512:3687:b0:52c:983f:8ad with SMTP id 2adb3069b0e04-52c9a3fd4camr2542032e87.47.1718264740824; Thu, 13 Jun 2024 00:45:40 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240605094843.4141730-1-wenst@chromium.org> In-Reply-To: From: Chen-Yu Tsai Date: Thu, 13 Jun 2024 15:45:29 +0800 Message-ID: Subject: Re: [PATCH] scripts/make_fit: Support decomposing DTBs To: Simon Glass Cc: Masahiro Yamada , Nathan Chancellor , Nicolas Schier , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-kbuild@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Wed, Jun 12, 2024 at 4:01=E2=80=AFAM Simon Glass wrot= e: > > Hi Chen-Yu, > > On Wed, 5 Jun 2024 at 03:48, Chen-Yu Tsai wrote: > > > > The kernel tree builds some "composite" DTBs, where the final DTB is th= e > > 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 t= o > > 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 possibl= e > > 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 > > --- > > 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 developmen= t > > board vendors in binary form (without upstream) in an image or in > > SPI flash on the board that were built with OF_LIBFDT_OVERLAY=3Dn. > > 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@googl= e.com/ > > [2] > > > > scripts/Makefile.lib | 1 + > > scripts/make_fit.py | 70 ++++++++++++++++++++++++++++++-------------- > > 2 files changed, 49 insertions(+), 22 deletions(-) > > Reviewed-by: Simon Glass > > Some possible nits / changes below > > > > > 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 =3D FIT $@ > > cmd_fit =3D $(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..120f13e1323c 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 th= e > > +DTBs to be sourced from the kernel build directory, as the implementat= ion > > +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=3D'Specifies the architecture') > > parser.add_argument('-c', '--compress', type=3Dstr, default=3D'non= e', > > help=3D'Specifies the compression') > > + parser.add_argument('-d', '--decompose-dtbs', action=3D'store_true= ', > > + help=3D'Decompose composite DTBs into base DTB and overlays'= ) > > I wonder if we should reserve -d for --debug? I don't have a strong > opinion though. Seems reasonable. I'll make it use the capital -D then. > > parser.add_argument('-E', '--external', action=3D'store_true', > > help=3D'Convert the FIT to use external data') > > parser.add_argument('-n', '--name', type=3Dstr, required=3DTrue, > > @@ -140,12 +147,12 @@ def finish_fit(fsw, entries): > > fsw.end_node() > > seq =3D 0 > > with fsw.add_node('configurations'): > > - for model, compat in entries: > > + for model, compat, files in entries: > > seq +=3D 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', b''.join([b'fdt-%d\x00' % x for x = in files])) > > This looks right to me. It would be nice to use an f string but I > don't know how to do that with bytes. Me neither. Switching the format to an f string doesn't work: File "/ssd1/wenst/linux/src/scripts/make_fit.py", line 324, in sys.exit(run_make_fit()) ^^^^^^^^^^^^^^ File "/ssd1/wenst/linux/src/scripts/make_fit.py", line 298, in run_make_f= it out_data, count, size =3D build_fit(args) ^^^^^^^^^^^^^^^ File "/ssd1/wenst/linux/src/scripts/make_fit.py", line 288, in build_fit finish_fit(fsw, entries) File "/ssd1/wenst/linux/src/scripts/make_fit.py", line 161, in finish_fit fsw.property('fdt', b''.join([f'fdt-%d\x00' % x for x in files])) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: sequence item 0: expected a bytes-like object, str found > But do you need the inner [] ? Nope. Will remove. > > > 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 =3D inf.read() > > - fdt =3D libfdt.FdtRo(data) > > - model =3D fdt.getprop(0, 'model').as_str() > > - compat =3D 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,7 +210,6 @@ def output_dtb(fsw, seq, fname, arch, compress): > > with open(fname, 'rb') as inf: > > compressed =3D compress_data(inf, compress) > > fsw.property('data', compressed) > > - return model, compat > > > > > > def build_fit(args): > > @@ -235,6 +229,7 @@ def build_fit(args): > > fsw =3D libfdt.FdtSw() > > setup_fit(fsw, args.name) > > entries =3D [] > > + fdts =3D collections.OrderedDict() > > > > # Handle the kernel > > with open(args.kernel, 'rb') as inf: > > @@ -243,12 +238,43 @@ 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] =3D=3D '.dtb': > > - seq +=3D 1 > > - size +=3D os.path.getsize(fname) > > - model, compat =3D output_dtb(fsw, seq, fname, args.arch, a= rgs.compress) > > - entries.append([model, compat]) > > + # Ignore non-DTB (*.dtb) files > > + if os.path.splitext(fname)[1] !=3D '.dtb': > > + continue > > + > > + # Get the compatible / model information > > + with open(fname, 'rb') as inf: > > + data =3D inf.read() > > + fdt =3D libfdt.FdtRo(data) > > + model =3D fdt.getprop(0, 'model').as_str() > > + compat =3D fdt.getprop(0, 'compatible') > > + > > + if args.decompose_dtbs: > > + # Check if the DTB needs to be decomposed > > + path, basename =3D os.path.split(fname) > > + cmd_fname =3D os.path.join(path, f'.{basename}.cmd') > > + with open(cmd_fname, 'r', encoding=3D'ascii') as inf: > > + cmd =3D inf.read() > > + > > + if 'scripts/dtc/fdtoverlay' in cmd: > > + # This depends on the structure of the composite DTB c= ommand > > + files =3D cmd.split() > > + files =3D files[files.index('-i')+1:] > > spaces around + Will fix. > > + else: > > + files =3D [fname] > > + else: > > + files =3D [fname] > > I do wonder if the code from '# Get the compatible' to here would be > better in a separate, documented function, to keep things easier to > understand? I'll see what I can do. In that case I'll drop your Reviewed-by. Thanks ChenYu > > + > > + for fn in files: > > + if fn not in fdts: > > + seq +=3D 1 > > + size +=3D os.path.getsize(fn) > > + output_dtb(fsw, seq, fn, args.arch, args.compress) > > + fdts[fn] =3D seq > > + > > + files_seq =3D [fdts[fn] for fn in files] > > + > > + entries.append([model, compat, files_seq]) > > > > finish_fit(fsw, entries) > > > > -- > > 2.45.1.288.g0e0cd299f1-goog > > > > Regards, > Simon