From patchwork Fri Jul 12 09:36:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= X-Patchwork-Id: 1959735 X-Patchwork-Delegate: zajec5@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=QcGwVtMb; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=hV/FV9Rl; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.openwrt.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org; receiver=patchwork.ozlabs.org) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WL63t50QTz1xqc for ; Fri, 12 Jul 2024 19:38:50 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=07JMqm4PRDpmo4uGemJ8iXemk66HWEb4JZRKv38nYw0=; b=QcGwVtMbXm1rxx KWaf/vCGuORgBDmJCpOznUpC/LFuO9/6bByESqM7RJUW+UWJSo7/A4kJe4g6atrvIE8blIwp/MyYd 9BGxjf4R3qBHzS63K/5V08jYzP88x0qSgcffkwM+fbiUyMiy9B79XH4yV7kiSrjvarYIaHfaJe/YG CBwQqB78mst0uvVm63uux9fCbdZuEditZKfBA7BpPFJBd3DMmO02vP4SyS4SiJks+IV9pp4/KpOUg CjzTLq3QdT/yA/vjx6bfOgr3uTfhETD77DSY7h/YdSUCUzkUwX30cZK1f39mGp3+LVv8sA7hkKZK9 ByWSK0p49s9dtXEKnyRg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sSCi6-0000000H89r-25p7; Fri, 12 Jul 2024 09:37:02 +0000 Received: from mail-lf1-x132.google.com ([2a00:1450:4864:20::132]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sSCi3-0000000H88n-3RB8 for openwrt-devel@lists.openwrt.org; Fri, 12 Jul 2024 09:37:01 +0000 Received: by mail-lf1-x132.google.com with SMTP id 2adb3069b0e04-52ea79e689eso2464867e87.1 for ; Fri, 12 Jul 2024 02:36:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720777017; x=1721381817; darn=lists.openwrt.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=9l2pWfYoB8+ok8jqj4/eEQQRAiPz+R2oRHVJyVw5Oss=; b=hV/FV9RlYlyT6nm565psv15LTkkASNKvl8YjACjtRJ0XkwkjCzu9nf+xYyoehzQjhK xxafh46G0aiYwQoeXaOikKq4xj9O1IzM/bAcaX+RABFNl8v8Vx+g8psOKSTrfmPS4zYI GSPSkdfPTfjTw4SDcBf2niCx1q808BcGaxh7xVjFrjNAKbkMxIfwhqw9tOVlmSPPhx6Y gpsTrLA1ixtj/Wy6LSHtuxjE7AfeOIbGzrwGl6RA1vW67Hc5ZHUAREGHjOQeyBi+Q4Lm QqQ61JrRKS1qpsH/Tb+Veo6e+AqCdxdCh8xL9/kiGFewlYLKx0cGgCI0dAJWJU6SnCfb 1S5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720777017; x=1721381817; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=9l2pWfYoB8+ok8jqj4/eEQQRAiPz+R2oRHVJyVw5Oss=; b=E56uaxygtzhO5PxNLf6xUBiU76E9+lAoUQC9N7YltBtXjxdG6Cc03GgRXN6PiQTL8i ZqRiOvSj//hMSFHDDXFaWB4WN0Ko2zM9V4fgk6BDDQDMLTy34N8wZ7JlgSdk4AjLX0tI gXe0bTSUMGcg4JIqRLrYybtbBJMOi2RuvOU7v7pVYO9JVa7J/cyQ6wwTVD94FpAeLk7+ sDv0tXhsAAWeiAqEhFCbd/Y/OCrYbXcQvqXf7s5X/76JXGGmt4FBDvApaULc3WMqN6Ei TQKcfQcLC0PqMnUS+kmDAPMcYdtCvZ4MpNPR/gkfVKimYifiyOyz3xHHvrN4d4lLsKmo il/w== X-Gm-Message-State: AOJu0YxfSCo/ePm062/opmgRzRkwxaK6pNZYQVW7N2jxv8BSKCDk7r05 eqxCLnOvSsHks77Z+a+bnHINz8p8rhXxW3HWIEijvzarjMzSrkB2isBDQA== X-Google-Smtp-Source: AGHT+IEwujhdomXu3b6VkSCLhgiTdhv/8u7ORwyn1LHH/EI1ugXsaD9O6LIQq6F4pO6uOjzVx5TyXw== X-Received: by 2002:a05:6512:e96:b0:52c:df55:e110 with SMTP id 2adb3069b0e04-52eb9991e14mr10521199e87.12.1720777016378; Fri, 12 Jul 2024 02:36:56 -0700 (PDT) Received: from localhost.lan (031011218106.poznan.vectranet.pl. [31.11.218.106]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a780a87213dsm327228066b.221.2024.07.12.02.36.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Jul 2024 02:36:56 -0700 (PDT) From: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= To: openwrt-devel@lists.openwrt.org Cc: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= Subject: [PATCH 2/3] base-files: upgrade: nand: use "cmd" argument for extracting command Date: Fri, 12 Jul 2024 11:36:47 +0200 Message-Id: <20240712093648.20748-2-zajec5@gmail.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20240712093648.20748-1-zajec5@gmail.com> References: <20240712093648.20748-1-zajec5@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240712_023659_895434_C62EC5F0 X-CRM114-Status: GOOD ( 14.63 ) X-Spam-Score: -1.8 (-) X-Spam-Report: =?unknown-8bit?q?Spam_detection_software=2C_running_on_the_sy?= =?unknown-8bit?q?stem_=22bombadil=2Einfradead=2Eorg=22=2C?= =?unknown-8bit?q?_has_NOT_identified_this_incoming_email_as_spam=2E__The_ori?= =?unknown-8bit?q?ginal?= =?unknown-8bit?q?_message_has_been_attached_to_this_so_you_can_view_it_or_la?= =?unknown-8bit?q?bel?= =?unknown-8bit?q?_similar_future_email=2E__If_you_have_any_questions=2C_see?= =?unknown-8bit?q?_the_administrator_of_that_system_for_details=2E?= =?unknown-8bit?q?_?= =?unknown-8bit?q?_Content_preview=3A__From=3A_Rafa=C5=82_Mi=C5=82ecki_NAND_c?= =?unknown-8bit?q?ode_uses_either_=22cat=22_or_=22zcat=22?= =?unknown-8bit?q?_for_getting_firmware_image_content=2E_Code_was_full_of_dup?= =?unknown-8bit?q?licated_=24=7Bgz=7Dcat?= =?unknown-8bit?q?_calls=2E_Use_=22cmd=22_variable_that_is_determined_by_a_ca?= =?unknown-8bit?q?ller_and_passed_to_lower?= =?unknown-8bit?b?IGxldmVsIGZ1bmN0aSBbLi4uXSA=?= =?unknown-8bit?q?_?= =?unknown-8bit?q?_Content_analysis_details=3A___=28-1=2E8_points=2C_5=2E0_re?= =?unknown-8bit?q?quired=29?= =?unknown-8bit?q?_?= =?unknown-8bit?q?_pts_rule_name______________description?= =?unknown-8bit?q?_----_----------------------_------------------------------?= =?unknown-8bit?q?--------------------?= =?unknown-8bit?q?_-0=2E0_RCVD=5FIN=5FDNSWL=5FNONE_____RBL=3A_Sender_listed_a?= =?unknown-8bit?q?t_https=3A//www=2Ednswl=2Eorg/=2C_no?= =?unknown-8bit?q?_trust?= =?unknown-8bit?b?IFsyYTAwOjE0NTA6NDg2NDoyMDowOjA6MDoxMzIgbGlzdGVkIGluXQ==?= =?unknown-8bit?b?IFtsaXN0LmRuc3dsLm9yZ10=?= =?unknown-8bit?q?_0=2E0_SPF=5FHELO=5FNONE__________SPF=3A_HELO_does_not_publ?= =?unknown-8bit?q?ish_an_SPF_Record?= =?unknown-8bit?q?_-0=2E0_SPF=5FPASS_______________SPF=3A_sender_matches_SPF_?= =?unknown-8bit?q?record?= =?unknown-8bit?q?_-0=2E1_DKIM=5FVALID=5FEF__________Message_has_a_valid_DKIM?= =?unknown-8bit?q?_or_DK_signature_from?= =?unknown-8bit?q?_envelope-from_domain?= =?unknown-8bit?q?_0=2E1_DKIM=5FSIGNED____________Message_has_a_DKIM_or_DK_si?= =?unknown-8bit?q?gnature=2C_not_necessarily_valid?= =?unknown-8bit?q?_-0=2E1_DKIM=5FVALID_____________Message_has_at_least_one_v?= =?unknown-8bit?q?alid_DKIM_or_DK_signature?= =?unknown-8bit?q?_-0=2E1_DKIM=5FVALID=5FAU__________Message_has_a_valid_DKIM?= =?unknown-8bit?q?_or_DK_signature_from_author=27s?= =?unknown-8bit?q?_domain?= =?unknown-8bit?q?_-1=2E9_BAYES=5F00_______________BODY=3A_Bayes_spam_probabi?= =?unknown-8bit?q?lity_is_0_to_1=25?= =?unknown-8bit?q?_=5Bscore=3A_0=2E0000=5D?= =?unknown-8bit?q?_0=2E0_FREEMAIL=5FFROM__________Sender_email_is_commonly_ab?= =?unknown-8bit?q?used_enduser_mail_provider?= =?unknown-8bit?q?_=5Bzajec5=28at=29gmail=2Ecom=5D?= =?unknown-8bit?q?_0=2E2_FREEMAIL=5FENVFROM=5FEND=5FDIGIT_Envelope-from_freem?= =?unknown-8bit?q?ail_username_ends_in?= =?unknown-8bit?q?_digit?= =?unknown-8bit?q?_=5Bzajec5=28at=29gmail=2Ecom=5D?= X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: OpenWrt Development List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "openwrt-devel" Errors-To: openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org From: Rafał Miłecki NAND code uses either "cat" or "zcat" for getting firmware image content. Code was full of duplicated ${gz}cat calls. Use "cmd" variable that is determined by a caller and passed to lower level functions. This avoids code duplication and allows adding support for more formats. Signed-off-by: Rafał Miłecki --- package/base-files/files/lib/upgrade/nand.sh | 82 ++++++++++---------- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/package/base-files/files/lib/upgrade/nand.sh b/package/base-files/files/lib/upgrade/nand.sh index 7a6873e224..0b76486ee7 100644 --- a/package/base-files/files/lib/upgrade/nand.sh +++ b/package/base-files/files/lib/upgrade/nand.sh @@ -57,11 +57,11 @@ nand_find_ubi() { } nand_get_magic_long() { - (${3}cat "$1" | dd bs=4 "skip=${2:-0}" count=1 | hexdump -v -n 4 -e '1/1 "%02x"') 2> /dev/null + ($2 < "$1" | dd bs=4 "skip=${3:-0}" count=1 | hexdump -v -n 4 -e '1/1 "%02x"') 2> /dev/null } get_magic_long_tar() { - (tar xO${3}f "$1" "$2" | dd bs=4 count=1 | hexdump -v -n 4 -e '1/1 "%02x"') 2> /dev/null + ($2 < "$1" | tar xOf - "$3" | dd bs=4 count=1 | hexdump -v -n 4 -e '1/1 "%02x"') 2> /dev/null } identify() { @@ -73,7 +73,7 @@ identify_tar() { } identify_if_gzip() { - if [ "$(identify "$1")" = gzip ]; then echo -n z; fi + if [ "$(identify "$1" "cat")" = gzip ]; then echo -n z; fi } nand_restore_config() { @@ -259,64 +259,64 @@ nand_upgrade_prepare_ubi() { # Write the UBI image to MTD ubi partition nand_upgrade_ubinized() { local ubi_file="$1" - local gz="$2" + local cmd="$2" - local ubi_length=$( (${gz}cat "$ubi_file" | wc -c) 2> /dev/null) + local ubi_length=$( ($cmd < "$ubi_file" | wc -c) 2> /dev/null) nand_detach_ubi "$CI_UBIPART" || return 1 local mtdnum="$( find_mtd_index "$CI_UBIPART" )" - ${gz}cat "$ubi_file" | ubiformat "/dev/mtd$mtdnum" -S "$ubi_length" -y -f - && ubiattach -m "$mtdnum" + $cmd < "$ubi_file" | ubiformat "/dev/mtd$mtdnum" -S "$ubi_length" -y -f - && ubiattach -m "$mtdnum" } # Write the UBIFS image to UBI rootfs volume nand_upgrade_ubifs() { local ubifs_file="$1" - local gz="$2" + local cmd="$2" - local ubifs_length=$( (${gz}cat "$ubifs_file" | wc -c) 2> /dev/null) + local ubifs_length=$( ($cmd < "$ubifs_file" | wc -c) 2> /dev/null) nand_upgrade_prepare_ubi "$ubifs_length" "ubifs" "" "" || return 1 local ubidev="$( nand_find_ubi "$CI_UBIPART" )" local root_ubivol="$(nand_find_volume $ubidev "$CI_ROOTPART")" - ${gz}cat "$ubifs_file" | ubiupdatevol /dev/$root_ubivol -s "$ubifs_length" - + $cmd < "$ubifs_file" | ubiupdatevol /dev/$root_ubivol -s "$ubifs_length" - } # Write the FIT image to UBI kernel volume nand_upgrade_fit() { local fit_file="$1" - local gz="$2" + local cmd="$2" - local fit_length=$( (${gz}cat "$fit_file" | wc -c) 2> /dev/null) + local fit_length=$( ($cmd < "$fit_file" | wc -c) 2> /dev/null) nand_upgrade_prepare_ubi "" "" "$fit_length" "1" || return 1 local fit_ubidev="$(nand_find_ubi "$CI_UBIPART")" local fit_ubivol="$(nand_find_volume $fit_ubidev "$CI_KERNPART")" - ${gz}cat "$fit_file" | ubiupdatevol /dev/$fit_ubivol -s "$fit_length" - + $cmd < "$fit_file" | ubiupdatevol /dev/$fit_ubivol -s "$fit_length" - } # Write images in the TAR file to MTD partitions and/or UBI volumes as required nand_upgrade_tar() { local tar_file="$1" - local gz="$2" + local cmd="$2" local jffs2_markers="${CI_JFFS2_CLEAN_MARKERS:-0}" # WARNING: This fails if tar contains more than one 'sysupgrade-*' directory. - local board_dir="$(tar t${gz}f "$tar_file" | grep -m 1 '^sysupgrade-.*/$')" + local board_dir="$($cmd < "$tar_file" | tar tf - | grep -m 1 '^sysupgrade-.*/$')" board_dir="${board_dir%/}" local kernel_mtd kernel_length if [ "$CI_KERNPART" != "none" ]; then kernel_mtd="$(find_mtd_index "$CI_KERNPART")" - kernel_length=$( (tar xO${gz}f "$tar_file" "$board_dir/kernel" | wc -c) 2> /dev/null) + kernel_length=$( ($cmd < "$tar_file" | tar xOf - "$board_dir/kernel" | wc -c) 2> /dev/null) [ "$kernel_length" = 0 ] && kernel_length= fi - local rootfs_length=$( (tar xO${gz}f "$tar_file" "$board_dir/root" | wc -c) 2> /dev/null) + local rootfs_length=$( ($cmd < "$tar_file" | tar xOf - "$board_dir/root" | wc -c) 2> /dev/null) [ "$rootfs_length" = 0 ] && rootfs_length= local rootfs_type - [ "$rootfs_length" ] && rootfs_type="$(identify_tar "$tar_file" "$board_dir/root" "$gz")" + [ "$rootfs_length" ] && rootfs_type="$(identify_tar "$tar_file" "$cmd" "$board_dir/root")" local ubi_kernel_length if [ "$kernel_length" ]; then @@ -337,23 +337,23 @@ nand_upgrade_tar() { if [ "$rootfs_length" ]; then local ubidev="$( nand_find_ubi "${CI_ROOT_UBIPART:-$CI_UBIPART}" )" local root_ubivol="$( nand_find_volume $ubidev "$CI_ROOTPART" )" - tar xO${gz}f "$tar_file" "$board_dir/root" | \ + $cmd < "$tar_file" | tar xOf - "$board_dir/root" | \ ubiupdatevol /dev/$root_ubivol -s "$rootfs_length" - fi if [ "$kernel_length" ]; then if [ "$kernel_mtd" ]; then if [ "$jffs2_markers" = 1 ]; then flash_erase -j "/dev/mtd${kernel_mtd}" 0 0 - tar xO${gz}f "$tar_file" "$board_dir/kernel" | \ + $cmd < "$tar_file" | tar xOf - "$board_dir/kernel" | \ nandwrite "/dev/mtd${kernel_mtd}" - else - tar xO${gz}f "$tar_file" "$board_dir/kernel" | \ + $cmd < "$tar_file" | tar xOf - "$board_dir/kernel" | \ mtd write - "$CI_KERNPART" fi else local ubidev="$( nand_find_ubi "${CI_KERN_UBIPART:-$CI_UBIPART}" )" local kern_ubivol="$( nand_find_volume $ubidev "$CI_KERNPART" )" - tar xO${gz}f "$tar_file" "$board_dir/kernel" | \ + $cmd < "$tar_file" | tar xOf - "$board_dir/kernel" | \ ubiupdatevol /dev/$kern_ubivol -s "$kernel_length" - fi fi @@ -363,9 +363,9 @@ nand_upgrade_tar() { nand_verify_if_gzip_file() { local file="$1" - local gz="$2" + local cmd="$2" - if [ "$gz" = z ]; then + if [ "$cmd" = zcat ]; then echo "verifying compressed sysupgrade file integrity" if ! gzip -t "$file"; then echo "corrupted compressed sysupgrade file" @@ -376,10 +376,10 @@ nand_verify_if_gzip_file() { nand_verify_tar_file() { local file="$1" - local gz="$2" + local cmd="$2" echo "verifying sysupgrade tar file integrity" - if ! tar xO${gz}f "$file" > /dev/null; then + if ! $cmd < "$file" | tar xOf - > /dev/null; then echo "corrupted sysupgrade tar file" return 1 fi @@ -388,27 +388,27 @@ nand_verify_tar_file() { nand_do_flash_file() { local file="$1" - local gz="$(identify_if_gzip "$file")" - local file_type="$(identify "$file" "" "$gz")" + local cmd="$(identify_if_gzip "$file")cat" + local file_type="$(identify "$file" "$cmd" "")" [ ! "$(find_mtd_index "$CI_UBIPART")" ] && CI_UBIPART=rootfs case "$file_type" in "fit") - nand_verify_if_gzip_file "$file" "$gz" || return 1 - nand_upgrade_fit "$file" "$gz" + nand_verify_if_gzip_file "$file" "$cmd" || return 1 + nand_upgrade_fit "$file" "$cmd" ;; "ubi") - nand_verify_if_gzip_file "$file" "$gz" || return 1 - nand_upgrade_ubinized "$file" "$gz" + nand_verify_if_gzip_file "$file" "$cmd" || return 1 + nand_upgrade_ubinized "$file" "$cmd" ;; "ubifs") - nand_verify_if_gzip_file "$file" "$gz" || return 1 - nand_upgrade_ubifs "$file" "$gz" + nand_verify_if_gzip_file "$file" "$cmd" || return 1 + nand_upgrade_ubifs "$file" "$cmd" ;; *) - nand_verify_tar_file "$file" "$gz" || return 1 - nand_upgrade_tar "$file" "$gz" + nand_verify_tar_file "$file" "$cmd" || return 1 + nand_upgrade_tar "$file" "$cmd" ;; esac } @@ -470,18 +470,18 @@ nand_do_platform_check() { local board_name="$1" local file="$2" - local gz="$(identify_if_gzip "$file")" - local file_type="$(identify "$file" "" "$gz")" - local control_length=$( (tar xO${gz}f "$file" "sysupgrade-${board_name//,/_}/CONTROL" | wc -c) 2> /dev/null) + local cmd="$(identify_if_gzip "$file")cat" + local file_type="$(identify "$file" "$cmd" "")" + local control_length=$( ($cmd < "$file" | tar xOf - "sysupgrade-${board_name//,/_}/CONTROL" | wc -c) 2> /dev/null) if [ "$control_length" = 0 ]; then - control_length=$( (tar xO${gz}f "$file" "sysupgrade-${board_name//_/,}/CONTROL" | wc -c) 2> /dev/null) + control_length=$( ($cmd < "$file" | tar xOf - "sysupgrade-${board_name//_/,}/CONTROL" | wc -c) 2> /dev/null) fi if [ "$control_length" != 0 ]; then - nand_verify_tar_file "$file" "$gz" || return 1 + nand_verify_tar_file "$file" "$cmd" || return 1 else - nand_verify_if_gzip_file "$file" "$gz" || return 1 + nand_verify_if_gzip_file "$file" "$cmd" || return 1 if [ "$file_type" != "fit" -a "$file_type" != "ubi" -a "$file_type" != "ubifs" ]; then echo "invalid sysupgrade file" return 1