Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 3B560C636CC for ; Sat, 4 Feb 2023 22:25:36 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id C0BEF857FB; Sat, 4 Feb 2023 23:24:51 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="fbUmVNEH"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 1FB8C858ED; Sat, 4 Feb 2023 23:24:46 +0100 (CET) Received: from mail-il1-x134.google.com (mail-il1-x134.google.com [IPv6:2607:f8b0:4864:20::134]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id EE0DC8583D for ; Sat, 4 Feb 2023 23:24:40 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sjg@chromium.org Received: by mail-il1-x134.google.com with SMTP id m15so3444984ilh.9 for ; Sat, 04 Feb 2023 14:24:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=d4C0bkBd1Flitna57QJ/pJYl0apUOezq/Q3jqY4pD1U=; b=fbUmVNEH5r1P6uOMphqU0gaWsLRySB3cxRQhOg+cNIpOxCuB1w+TqAMokD2y/xZhHG 3XYWnego/E2+D7tYnfrb221C7SfsidiNz/WjbfcpJeYVWo2pkosu+Fb62VTp1pkVdTZK kG+TnOxWQddiUqwnoaVgywE4/ERhE2UlAy448= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=d4C0bkBd1Flitna57QJ/pJYl0apUOezq/Q3jqY4pD1U=; b=nSLqLuSxiYeob11KqZsfsLk4GOOB3jPwxlx854tnTtuQpIoLjwgSv31eiWdxBZru4O wX8rxA3jGdy89eEgBewDTUDr6GrfQRvmSdDGiBDCkQTCTMqn/bbJQ4+WKsjpmUL369zT ikSva3EYl+4by+uh5FXthwk2NKTuNvXoo5Dps7ieIRQiZYIoUFiti/SEp6XI8YYxscTC cmSZysvI0hJS0ofzHIJT/iyv/DQCRYZC4Bo4kAsN09KJOP6tGU4Uim6eF6TAn/oPKF2W zEVdwsAAzB3EsudNYAPXwgb8RTPmrJlC2aHmW4MnZHW83k3pFBwOdsY+iVBAjyq4oC63 EahA== X-Gm-Message-State: AO0yUKXsfJSF7w/zR7IE1PmelroSt4K5tyU7F4jtBqtrSRg5OuWTWaQU x7sRyZ9P2v6VwVon/xMEUwl+FEeAQtL48ryN X-Google-Smtp-Source: AK7set8R2Rm8VvXk2Yli3vPeeJwVJCYHYbilb2IKEcLN08xXSxOiu3kzxWrOtXP5JQvIZT6T3oZt1w== X-Received: by 2002:a92:c563:0:b0:311:199f:cc7a with SMTP id b3-20020a92c563000000b00311199fcc7amr12555939ilj.21.1675549456476; Sat, 04 Feb 2023 14:24:16 -0800 (PST) Received: from sjg1.lan (c-73-14-173-85.hsd1.co.comcast.net. [73.14.173.85]) by smtp.gmail.com with ESMTPSA id b89-20020a0295e2000000b003a9962a24d1sm2115583jai.122.2023.02.04.14.24.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 04 Feb 2023 14:24:16 -0800 (PST) From: Simon Glass To: U-Boot Mailing List Cc: Ivan Mikhaylov , Roman Kopytin , Alper Nebi Yasak , Neha Malcom Francis , Stefan Herbrechtsmeier , Philippe Reynes , Peng Fan , Jan Kiszka , Simon Glass Subject: [PATCH] binman: Support updating section contents Date: Sat, 4 Feb 2023 15:24:13 -0700 Message-Id: <20230204222413.116740-1-sjg@chromium.org> X-Mailer: git-send-email 2.39.1.519.gcb327c4b5f-goog MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.6 at phobos.denx.de X-Virus-Status: Clean Implement this feature since it is useful for updating FITs within an image. Signed-off-by: Simon Glass --- tools/binman/binman.rst | 16 ++++++++++ tools/binman/etype/section.py | 28 ++++++++++++----- tools/binman/ftest.py | 58 ++++++++++++++++++++++++++++++++--- 3 files changed, 90 insertions(+), 12 deletions(-) diff --git a/tools/binman/binman.rst b/tools/binman/binman.rst index 03a99a19bc6..ece2c05128a 100644 --- a/tools/binman/binman.rst +++ b/tools/binman/binman.rst @@ -1327,6 +1327,22 @@ You can also replace just a selection of entries:: $ binman replace -i image.bin "*u-boot*" -I indir +It is possible to replace whole sections as well, but in that case any +information about entries within the section may become outdated. This is +because Binman cannot know whether things have moved around or resized within +the section, once you have updated its data. + +Technical note: With 'allow-repack', Binman writes information about the +original offset and size properties of each entry, if any were specified, in +the 'orig-offset' and 'orig-size' properties. This allows Binman to distinguish +between an entry which ended up being packed at an offset (or assigned a size) +and an entry which had a particular offset / size requested in the Binman +configuration. Where are particular offset / size was requested, this is treated +as set in stone, so Binman will ensure it doesn't change. Without this feature, +repacking an entry might cause it to disobey the original constraints provided +when it was created. + + Repacking an image involves .. _`BinmanLogging`: diff --git a/tools/binman/etype/section.py b/tools/binman/etype/section.py index 57b91ff726c..1c03d357201 100644 --- a/tools/binman/etype/section.py +++ b/tools/binman/etype/section.py @@ -168,6 +168,9 @@ class Entry_section(Entry): self._end_4gb = False self._ignore_missing = False self._filename = None + # Indicates that self.data should be used as is, without calling + # BuildSectionData() + self._build_done = False def IsSpecialSubnode(self, node): """Check if a node is a special one used by the section itself @@ -397,10 +400,13 @@ class Entry_section(Entry): This excludes any padding. If the section is compressed, the compressed data is returned """ - data = self.BuildSectionData(required) - if data is None: - return None - self.SetContents(data) + if not self._build_done: + data = self.BuildSectionData(required) + if data is None: + return None + self.SetContents(data) + else: + data = self.data if self._filename: tools.write_file(tools.get_output_filename(self._filename), data) return data @@ -427,8 +433,11 @@ class Entry_section(Entry): self._SortEntries() self._extend_entries() - data = self.BuildSectionData(True) - self.SetContents(data) + if self._build_done: + self.size = None + else: + data = self.BuildSectionData(True) + self.SetContents(data) self.CheckSize() @@ -866,7 +875,12 @@ class Entry_section(Entry): return data def WriteData(self, data, decomp=True): - self.Raise("Replacing sections is not implemented yet") + ok = super().WriteData(data, decomp) + + # The section contents are now fixed and cannot be rebuilt from the + # containing entries. + self._build_done = True + return ok def WriteChildData(self, child): return True diff --git a/tools/binman/ftest.py b/tools/binman/ftest.py index 6b203dfb644..2ec4864290a 100644 --- a/tools/binman/ftest.py +++ b/tools/binman/ftest.py @@ -5819,13 +5819,61 @@ fdt fdtmap Extract the devicetree blob from the fdtmap self.assertEqual(expected_fdtmap, fdtmap) def testReplaceSectionSimple(self): - """Test replacing a simple section with arbitrary data""" + """Test replacing a simple section with same-sized data""" new_data = b'w' * len(COMPRESS_DATA + U_BOOT_DATA) - with self.assertRaises(ValueError) as exc: - self._RunReplaceCmd('section', new_data, - dts='241_replace_section_simple.dts') + data, expected_fdtmap, image = self._RunReplaceCmd('section', + new_data, dts='241_replace_section_simple.dts') + self.assertEqual(new_data, data) + + entries = image.GetEntries() + self.assertIn('section', entries) + entry = entries['section'] + self.assertEqual(len(new_data), entry.size) + + def testReplaceSectionLarger(self): + """Test replacing a simple section with larger data""" + new_data = b'w' * (len(COMPRESS_DATA + U_BOOT_DATA) + 1) + data, expected_fdtmap, image = self._RunReplaceCmd('section', + new_data, dts='241_replace_section_simple.dts') + self.assertEqual(new_data, data) + + entries = image.GetEntries() + self.assertIn('section', entries) + entry = entries['section'] + self.assertEqual(len(new_data), entry.size) + fentry = entries['fdtmap'] + self.assertEqual(entry.offset + entry.size, fentry.offset) + + def testReplaceSectionSmaller(self): + """Test replacing a simple section with smaller data""" + new_data = b'w' * (len(COMPRESS_DATA + U_BOOT_DATA) - 1) + b'\0' + data, expected_fdtmap, image = self._RunReplaceCmd('section', + new_data, dts='241_replace_section_simple.dts') + self.assertEqual(new_data, data) + + # The new size is the same as the old, just with a pad byte at the end + entries = image.GetEntries() + self.assertIn('section', entries) + entry = entries['section'] + self.assertEqual(len(new_data), entry.size) + + def testReplaceSectionSmallerAllow(self): + """Test failing to replace a simple section with smaller data""" + new_data = b'w' * (len(COMPRESS_DATA + U_BOOT_DATA) - 1) + try: + state.SetAllowEntryContraction(True) + with self.assertRaises(ValueError) as exc: + self._RunReplaceCmd('section', new_data, + dts='241_replace_section_simple.dts') + finally: + state.SetAllowEntryContraction(False) + + # Since we have no information about the position of things within the + # section, we cannot adjust the position of /section-u-boot so it ends + # up outside the section self.assertIn( - "Node '/section': Replacing sections is not implemented yet", + "Node '/section/u-boot': Offset 0x24 (36) size 0x4 (4) is outside " + "the section '/section' starting at 0x0 (0) of size 0x27 (39)", str(exc.exception)) def testMkimageImagename(self): -- 2.39.1.519.gcb327c4b5f-goog