From patchwork Mon Nov 29 09:25:30 2021 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: 1561065 X-Patchwork-Delegate: zajec5@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.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=21k44FWa; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20210112 header.b=XYXe2VO3; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.openwrt.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org; receiver=) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4J2gBt3TGtz9sVc for ; Mon, 29 Nov 2021 20:33:26 +1100 (AEDT) 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: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:In-Reply-To:References: List-Owner; bh=guwST3pxUalsJqS6NMPA1tBih9H3nt01PgAygsC0bas=; b=21k44FWaF1G6ou HjtUzAynwEwDwZzIl0R84SqRvF8w8M36inyW3G1VsG5kIzq1V9VLDCysjEvY42ewDFull7jVZ+Mup 0QHoIqJtEpI96/iCb0UwPk8XzEWOj7Pk98IpYMfoH7ofj61Oejfz7lBLI/Jexc77ILjWNrTLRF0tT Cdde6saACuFnASfPPEqwTXl0RmKqH2z2GvGxPuVee96kHdgtUZV1Af4ODHiZwJGUe+F8IZhCxcs0i vH8xNItHLjr3MnxVUXui41PSz39L3EOQ5qxZ0fLdliAjzUl+QEDAAsuf1gdUycMnJ7BeAODybIEZg phoZVA26O5Bxnd0QHSjQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mrcuo-000EUO-AC; Mon, 29 Nov 2021 09:25:38 +0000 Received: from mail-lf1-x135.google.com ([2a00:1450:4864:20::135]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mrcul-000ETa-9P for openwrt-devel@lists.openwrt.org; Mon, 29 Nov 2021 09:25:36 +0000 Received: by mail-lf1-x135.google.com with SMTP id bi37so42778191lfb.5 for ; Mon, 29 Nov 2021 01:25:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=9EnSKjwCq95BLy9EBfHBZC6Cs9353yadA9Zl7rpqNy0=; b=XYXe2VO3iVAAE+rN9GvDviEVNI2Ksv53nvZTgMF2QHmATQwEkcwhfrAPnWcefPI5es 0QS/nomt0LLiaZ6PpqEc2M8ylbd2GxedniNfazl0KVGenhAH0Fqka1cTIFLTyIZ4fCwr WJ849DHNCmtELoTAFi+m33Zm3REJ/tEeDhKNqIMe6ym+svmg4WSK4Oe1E3znC0f8yYbD VA1k80St6iUMGnhLzlZzJ8e17dFu0neGNof4GczHOS0vMwqBjF7yu8IOghimRyp9wWFQ /7SAQCd7WJYSiDfW5St1X9J1JKJPZ9065zZeilQpKuhlJwKh+KlrsruiMdNPwvWTTNR9 kYwQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=9EnSKjwCq95BLy9EBfHBZC6Cs9353yadA9Zl7rpqNy0=; b=MMDAGMAN/JjFolZienqVSZadw0kBLCkbcRcIZ2kp4amLHblXLOepp+qGYnyWl7tKNv BA/SlLWOh2DemDZc0zTSoMSvqep1lnU2rlyvA104ZaY9nIXGOhMVhDUKlmuSTnOgQaEE UiLvIgnacupbiid5AaMvWKTxP+DpNdYX+hWTr9nSKJcyr1q1FuAsdru+hqX5PvCH8e18 lHazvQYslm9jWJvYh1V0yXgQ384jhTlscoOWBoqk9cGCg0x9Ac3P6bSend4cloz3Zi46 O0/EG5u78pDVyphdExRszPy6JEwcL/1lOPHGoL307EdGPDmCSPEOQ5icKV7saVnwg8vT 2O9w== X-Gm-Message-State: AOAM530KvLbmHoTw1tJKzmY5zt8l1W43GH+NyoK62tVQsG/UuhPWh/k5 rRybaiCFczJ/pDNG/Swtt2KAMpc0H98= X-Google-Smtp-Source: ABdhPJxd9mCaJUZirvQlNMFv6S8s+MpsclcwQP7w072dbfvyv8v8VmZZZXYtu6akncGPnTKzMliUGQ== X-Received: by 2002:a19:4882:: with SMTP id v124mr46432542lfa.319.1638177932505; Mon, 29 Nov 2021 01:25:32 -0800 (PST) Received: from localhost.lan (ip-194-187-74-233.konfederacka.maverick.com.pl. [194.187.74.233]) by smtp.gmail.com with ESMTPSA id c11sm1277720lfj.34.2021.11.29.01.25.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Nov 2021 01:25:32 -0800 (PST) From: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= To: openwrt-devel@lists.openwrt.org Cc: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= Subject: [PATCH] bcm4908: sysupgrade: refactor handling different firmware formats Date: Mon, 29 Nov 2021 10:25:30 +0100 Message-Id: <20211129092530.14848-1-zajec5@gmail.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211129_012535_363205_FDDA2A2F X-CRM114-Status: GOOD ( 16.89 ) X-Spam-Score: 0.1 (/) 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_This_r?= =?unknown-8bit?q?esults_in_setting_format_specific?= =?unknown-8bit?q?_data_=28format_info=2C_extract_commands=29_in_a_single_fun?= =?unknown-8bit?q?ction=2E_It_should_help?= =?unknown-8bit?q?_maintaining_sysupgrade_code=2E_This_change_has_been_tested?= =?unknown-8bit?q?_on_Asus_GT-AC5300?= =?unknown-8bit?q?_and_Netgear_R8000P=2E_?= =?unknown-8bit?q?_?= =?unknown-8bit?q?_Content_analysis_details=3A___=280=2E1_points=2C_5=2E0_req?= =?unknown-8bit?q?uired=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?= =?unknown-8bit?q?_no_trust?= =?unknown-8bit?b?IFsyYTAwOjE0NTA6NDg2NDoyMDowOjA6MDoxMzUgbGlzdGVkIGluXQ==?= =?unknown-8bit?b?IFtsaXN0LmRuc3dsLm9yZ10=?= =?unknown-8bit?q?_-0=2E0_SPF=5FPASS_______________SPF=3A_sender_matches_SPF_?= =?unknown-8bit?q?record?= =?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_FREEMAIL=5FFROM__________Sender_email_is_commonly_ab?= =?unknown-8bit?q?used_enduser_mail?= =?unknown-8bit?q?_provider?= =?unknown-8bit?q?_=5Bzajec5=5Bat=5Dgmail=2Ecom=5D?= =?unknown-8bit?q?_0=2E2_FREEMAIL=5FENVFROM=5FEND=5FDIGIT_Envelope-from_freem?= =?unknown-8bit?q?ail_username_ends?= =?unknown-8bit?q?_in_digit?= =?unknown-8bit?q?_=5Bzajec5=5Bat=5Dgmail=2Ecom=5D?= =?unknown-8bit?q?_-0=2E1_DKIM=5FVALID=5FAU__________Message_has_a_valid_DKIM?= =?unknown-8bit?q?_or_DK_signature_from?= =?unknown-8bit?q?_author=27s_domain?= =?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=5FSIGNED____________Message_has_a_DKIM_or_DK_si?= =?unknown-8bit?q?gnature=2C_not_necessarily?= =?unknown-8bit?q?_valid?= =?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?= 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 This results in setting format specific data (format info, extract commands) in a single function. It should help maintaining sysupgrade code. This change has been tested on Asus GT-AC5300 and Netgear R8000P. Signed-off-by: Rafał Miłecki --- .../base-files/lib/upgrade/platform.sh | 108 ++++++++++++------ 1 file changed, 76 insertions(+), 32 deletions(-) diff --git a/target/linux/bcm4908/base-files/lib/upgrade/platform.sh b/target/linux/bcm4908/base-files/lib/upgrade/platform.sh index ee43e3a713..c057f2dc58 100644 --- a/target/linux/bcm4908/base-files/lib/upgrade/platform.sh +++ b/target/linux/bcm4908/base-files/lib/upgrade/platform.sh @@ -4,6 +4,10 @@ RAMFS_COPY_BIN="bcm4908img expr" PART_NAME=firmware +BCM4908_FW_FORMAT= +BCM4908_FW_BOARD_ID= +BCM4908_FW_INT_IMG_FORMAT= + # $(1): file to read from # $(2): offset in bytes # $(3): length in bytes @@ -34,7 +38,12 @@ platform_identify() { magic=$(get_hex_u32_be "$1" 0) case "$magic" in 2a23245e) - echo "chk" + local header_len=$((0x$(get_hex_u32_be "$1" 4))) + local board_id_len=$(($header_len - 40)) + + BCM4908_FW_FORMAT="chk" + BCM4908_FW_BOARD_ID=$(dd if="$1" skip=40 bs=1 count=$board_id_len 2>/dev/null | hexdump -v -e '1/1 "%c"') + BCM4908_FW_INT_IMG_FORMAT="bcm4908img" return ;; esac @@ -44,12 +53,22 @@ platform_identify() { magic=$(get_content "$1" $((size - 20 - 64 + 8)) 12) case "$magic" in GT-AC5300) - echo "asus" + local size=$(wc -c "$1" | cut -d ' ' -f 1) + + BCM4908_FW_FORMAT="asus" + BCM4908_FW_BOARD_ID=$(get_content "$1" $((size - 20 - 64 + 8)) 12) + BCM4908_FW_INT_IMG_FORMAT="bcm4908img" return ;; esac - echo "unknown" + # Detecting native format is a bit complex (it may start with CFE or + # JFFS2) so just use bcm4908img instead of bash hacks. + # Make it the last attempt as bcm4908img detects also vendor formats. + bcm4908img info -i "$1" > /dev/null && { + BCM4908_FW_FORMAT="bcm4908img" + return + } } platform_check_image() { @@ -58,43 +77,51 @@ platform_check_image() { local expected_image=$(platform_expected_image) local error=0 - bcm4908img info -i "$1" > /dev/null || { - echo "Failed to validate BCM4908 image" >&2 + platform_identify "$1" + [ -z "$BCM4908_FW_FORMAT" ] && { + echo "Invalid image type. Please use firmware specific for this device." notify_firmware_broken return 1 } + echo "Found $BCM4908_FW_FORMAT firmware for device ${BCM4908_FW_BOARD_ID:----}" - bcm4908img bootfs -i "$1" ls | grep -q "1-openwrt" || { - # OpenWrt images have 1-openwrt dummy file in the bootfs. - # Don't allow backup if it's missing - notify_firmware_no_backup + local expected_image="$(platform_expected_image)" + [ -n "$expected_image" -a -n "$BCM4908_FW_BOARD_ID" -a "$BCM4908_FW_FORMAT $BCM4908_FW_BOARD_ID" != "$expected_image" ] && { + echo "Firmware doesn't match device ($expected_image)" + error=1 } - case "$(platform_identify "$1")" in - asus) - local size=$(wc -c "$1" | cut -d ' ' -f 1) - local productid=$(get_content "$1" $((size - 20 - 64 + 8)) 12) - - [ -n "$expected_image" -a "asus $productid" != "$expected_image" ] && { - echo "Firmware productid mismatch ($productid)" >&2 - error=1 + case "$BCM4908_FW_FORMAT" in + "bcm4908img") + bcm4908img info -i "$1" > /dev/null || { + echo "Failed to validate BCM4908 image" >&2 + notify_firmware_broken + return 1 } - ;; - chk) - local header_len=$((0x$(get_hex_u32_be "$1" 4))) - local board_id_len=$(($header_len - 40)) - local board_id=$(dd if="$1" skip=40 bs=1 count=$board_id_len 2>/dev/null | hexdump -v -e '1/1 "%c"') - [ -n "$expected_image" -a "chk $board_id" != "$expected_image" ] && { - echo "Firmware board_id mismatch ($board_id)" >&2 - error=1 + bcm4908img bootfs -i "$1" ls | grep -q "1-openwrt" || { + # OpenWrt images have 1-openwrt dummy file in the bootfs. + # Don't allow backup if it's missing + notify_firmware_no_backup } - ;; + ;; *) - echo "Invalid image type. Please use firmware specific for this device." >&2 - notify_firmware_broken - error=1 - ;; + case "$BCM4908_FW_INT_IMG_FORMAT" in + "bcm4908img") + bcm4908img info -i "$1" > /dev/null || { + echo "Failed to validate BCM4908 image" >&2 + notify_firmware_broken + return 1 + } + + bcm4908img bootfs -i "$1" ls | grep -q "1-openwrt" || { + # OpenWrt images have 1-openwrt dummy file in the bootfs. + # Don't allow backup if it's missing + notify_firmware_no_backup + } + ;; + esac + ;; esac return $error @@ -189,10 +216,27 @@ platform_do_upgrade_ubi() { } platform_do_upgrade() { + platform_identify "$1" + # Try NAND aware UBI upgrade for OpenWrt images - # Below call will exit on success - bcm4908img bootfs -i "$1" ls | grep -q "1-openwrt" && platform_do_upgrade_ubi "$1" + case "$BCM4908_FW_FORMAT" in + "bcm4908img") + bcm4908img bootfs -i "$1" ls | grep -q "1-openwrt" && platform_do_upgrade_ubi "$1" + ;; + *) + case "$BCM4908_FW_INT_IMG_FORMAT" in + "bcm4908img") + bcm4908img bootfs -i "$1" ls | grep -q "1-openwrt" && platform_do_upgrade_ubi "$1" + ;; + *) + echo "NAND aware sysupgrade is unsupported for $BCM4908_FW_FORMAT format" + ;; + esac + ;; + esac + # Above calls exit on success. + # If we got here it isn't OpenWrt image or something went wrong. echo "Writing whole image to NAND flash. All erase counters will be lost." # Find cferam name for new firmware