From patchwork Thu Oct 12 21:16:08 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Lamparter X-Patchwork-Id: 825110 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.infradead.org (client-ip=65.50.211.133; helo=bombadil.infradead.org; envelope-from=lede-dev-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="slmHluMS"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="CuI4CvBP"; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3yCkF51Grzz9sNw for ; Fri, 13 Oct 2017 08:16:56 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:List-Subscribe:List-Help: List-Post:List-Archive:List-Unsubscribe:List-Id:Subject:References: In-Reply-To:Message-Id:Date:To:From:Reply-To:Cc:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=IRjPTtVN2cMzQq+V6M7DanFtvSC676QCPql6rOT516k=; b=slmHluMS8pi6o88SgPeW2PxXxS KoQhUz+xQOykYvyFyD/6XlWH0vF63Z2KbIgGM8FiszhD6QDAyGU49mxbfFCw32gGx0vbgsvNiJs98 zE3TZPNqGeNQrhSw1Yz2r1XFPmsecv6XWrSzfDA5vOvvB8ELQv60G598TUinWrDOB2jXLZnMEH71L YkpdG6fQOhZrQmqQ3nAtA6LLaTxQVa+XkpMeH9KJiypWGYSMnd6hgSS6c/3y7Q6sWhTaEbCHexCn8 COKAU/YBvh55FLXSXdFahMNKIEY2RtjC8yz9vczGw+Z5qeCzz+qX+B+H67g7bRynCvJD0U4operfO C++5V7DA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1e2kqL-0004d7-2X; Thu, 12 Oct 2017 21:16:37 +0000 Received: from mail-wm0-x244.google.com ([2a00:1450:400c:c09::244]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1e2kqH-0004Ku-Es for lede-dev@lists.infradead.org; Thu, 12 Oct 2017 21:16:35 +0000 Received: by mail-wm0-x244.google.com with SMTP id u138so16463948wmu.5 for ; Thu, 12 Oct 2017 14:16:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references; bh=PRagXp3ZP99tlbFHYNcJ6IVJ8kXWq4/QjBSZB1rr+xc=; b=CuI4CvBPh1y/+kF59LmylAO0RK6gKl4zHNwXLtOq6vv9OTvew7BmGcTKkHLeYqk0+u flCVWUSLVoNhgV4Qd22fdDazs3/edo7OOgXcgLOrhbebGCg90MtEURwvdNuHlbqYAiJ+ nNHodoe2h0+qKTGT+46pJGcS8VOrmNHacqC8TbWni7R9uz8Ay7eMSxr1PUHlyNJrBMlS eat0+1SUXM47d4lsRp7o7FWyjAcLh+5Jo0Ne78hMNZA+y4yJ/O0WrMPemMgTvDKzkWSm HgoxriGYOGE8FEaowvpVSRuOQXe6Y76RcEJOcteuIq+J5Yt+hEvSn17VigLbXJ9Y12Pj DaEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=PRagXp3ZP99tlbFHYNcJ6IVJ8kXWq4/QjBSZB1rr+xc=; b=o9u1YBWjYUAl1weGq8rwxKMHNo+qNLnsrADG6DMx9Oe1RCJxpwVkE1pwFLqGKuRcBy qS2lKT5KbuQ1qv+jZspGa7ec7+hPDUKivMVa8o1tCMJ6Re7vlYZOFUhygLIJzduli/Qb np6TyOoHSQovUFz2CQPvg/X0jy9GvutR6MEpQemTLOGjebPym8fSBkHK6TmltHaH94BP Z8pCgxECvxZad7KzQz7gUndC/Zeerv46ZH/siCB0UbbKMSg0ZblT9GqzYcndUwxLgwwu vS1G9dUCEbPvqeD1XR6MHE9ix+P3Zo3zt0vHqWlVIFnCLipajNrKAu9qTUAHkjwIVLc2 xZNA== X-Gm-Message-State: AMCzsaVjAuJk+cUMla90dNkr4r5vY3fb5tqWkTZMPRO+LWKtPtuBAOHi Mt8RipojSIBFUzeSlLWTI1J99/mr X-Google-Smtp-Source: AOwi7QCOXyAezj9xn+wWHmsarI7egETPmU5PV7FWq7nYn5dFBeuI0CHtbajLU71ay08ESN9ip3bkIw== X-Received: by 10.28.227.139 with SMTP id a133mr3074979wmh.104.1507842970393; Thu, 12 Oct 2017 14:16:10 -0700 (PDT) Received: from debian64.daheim (p200300D5FBC0EBFCA1A13610D2D4289E.dip0.t-ipconnect.de. [2003:d5:fbc0:ebfc:a1a1:3610:d2d4:289e]) by smtp.gmail.com with ESMTPSA id f89sm1396397wmh.18.2017.10.12.14.16.09 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 12 Oct 2017 14:16:09 -0700 (PDT) Received: from chuck by debian64.daheim with local (Exim 4.89) (envelope-from ) id 1e2kps-0007rh-RN for lede-dev@lists.infradead.org; Thu, 12 Oct 2017 23:16:08 +0200 From: Christian Lamparter To: lede-dev@lists.infradead.org Date: Thu, 12 Oct 2017 23:16:08 +0200 Message-Id: <20171012211608.30184-3-chunkeey@gmail.com> X-Mailer: git-send-email 2.15.0.rc0 In-Reply-To: <20171012211608.30184-1-chunkeey@gmail.com> References: <20171012211608.30184-1-chunkeey@gmail.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20171012_141633_687119_2779A4B2 X-CRM114-Status: GOOD ( 12.16 ) X-Spam-Score: -2.0 (--) X-Spam-Report: SpamAssassin version 3.4.1 on bombadil.infradead.org summary: Content analysis details: (-2.0 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 SPF_PASS SPF: sender matches SPF record 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider (chunkeey[at]gmail.com) -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain Subject: [LEDE-DEV] [RFC] base-files: upgrade: make get_partitions() endian agnostic X-BeenThere: lede-dev@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "Lede-dev" Errors-To: lede-dev-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org This patch fixes two issues with the current get_partitions() function. First: "Invalid partition table on $disk" will pop up on legitimate images on big endian system. This is because the little-endian representation of "55 AA" is assumed in the context of little-endian architectures. On these comparing it to the 16-bit word 0xAA55 does work as intented. Whereas on big-endian systems, this would have to be 0x55AA. This patch fixes the issue by replacing the integer conversion and value match check with just a string comparision. Second: The extraction of the type, start LBA and LBA num from the partition table has the same endianness issue. This has been fixed by using the new hex_le32_to_cpu() function. This function will translate the stored little-endian data to the correct byte-order if necessary. Signed-off-by: Christian Lamparter --- I stumbled upon this bug, when I was porting over the x86's sysupgrade code to the PPC MyBook Live. I'm not entirely happy with this yet. I'm looking for a better way that could to without the hex_le32_to_cpu. Any ideas? --- package/base-files/files/lib/upgrade/common.sh | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/package/base-files/files/lib/upgrade/common.sh b/package/base-files/files/lib/upgrade/common.sh index 428ec735d6..71cffc8587 100644 --- a/package/base-files/files/lib/upgrade/common.sh +++ b/package/base-files/files/lib/upgrade/common.sh @@ -160,6 +160,14 @@ export_partdevice() { return 1 } +hex_le32_to_cpu() { + [ "$(echo 01 | hexdump -v -n 2 -e '/2 "%x"')" == "3031" ] && { + echo "${1:0:2}${1:8:2}${1:6:2}${1:4:2}${1:2:2}" + return + } + echo "$@" +} + get_partitions() { # local disk="$1" local filename="$2" @@ -167,8 +175,8 @@ get_partitions() { # if [ -b "$disk" -o -f "$disk" ]; then v "Reading partition table from $filename..." - local magic="$(hexdump -v -n 2 -s 0x1FE -e '1/2 "0x%04X"' "$disk")" - if [ "$magic" != 0xAA55 ]; then + local magic=$(dd if="$disk" bs=2 count=1 skip=255 2>/dev/null) + if [ "$magic" != $'\x55\xAA' ]; then v "Invalid partition table on $disk" exit fi @@ -179,9 +187,9 @@ get_partitions() { # for part in 1 2 3 4; do set -- $(hexdump -v -n 12 -s "$((0x1B2 + $part * 16))" -e '3/4 "0x%08X "' "$disk") - local type="$(($1 % 256))" - local lba="$(($2))" - local num="$(($3))" + local type="$(( $(hex_le32_to_cpu $1) % 256))" + local lba="$(( $(hex_le32_to_cpu $2) ))" + local num="$(( $(hex_le32_to_cpu $3) ))" [ $type -gt 0 ] || continue