From patchwork Sun Nov 19 16:19:21 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Lamparter X-Patchwork-Id: 839336 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="C+m61tJ2"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="AaFOSq1X"; 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 3yfxs06D8Wz9rxm for ; Mon, 20 Nov 2017 03:20:00 +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: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=ce2SEmZv2WB8NE5N8VztmD79q70g5T2aba2h+dM0gkc=; b=C+m61tJ2ZVmXe5z0GwPhVigTJA 2uk87fD/iEMnAhxNSrAZu69+11I9vXm2WnaaB6UgIIztcpuPnAoxv/HBaHjv5HgdqEWadGN5YOQ54 oi9k4XohyQEtcckzj15fagZ9e35t7NddQCELCCnHVUUyrzRgpNU5YQKYqCjFntyigOCf+ydbpSEby pN/2ErqeYeYlNxXxw3aDdKO7yGZ/xHgaHR8Di1YQ+TTttAn+5M7qX0Ijq7bs2cytswAJ3jCAxt3Dq ZZwEuC083bUzWV/Xz1bZr1CWOM2/vftMiUQEBctrjl2VQAupDskPDLkLKDxtOVB5h7AAlmjZi6fPV JqViLTjw==; 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 1eGSK1-0005sC-26; Sun, 19 Nov 2017 16:19:53 +0000 Received: from mail-wm0-x242.google.com ([2a00:1450:400c:c09::242]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1eGSJw-0005pt-Ty for lede-dev@lists.infradead.org; Sun, 19 Nov 2017 16:19:50 +0000 Received: by mail-wm0-x242.google.com with SMTP id 5so13319280wmk.1 for ; Sun, 19 Nov 2017 08:19:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=sK8alkZcupJnWSW83oQWcAMUWywag3RATfIL9VLzBjw=; b=AaFOSq1Xor1WF+Z5C6A2Zl7HCFVFg+hHu0EbF/iHGLlV2Z0OoNQDYVKrAlYg899sBx a8lv83VIgLTExfyQCB7XHB5HWYE6WJ1VkOGNn7JaKpuTC+4J840+nyBpjhu6FYh1B1L3 ZlkWBLa5RYRYNwPd7WZ7+2OVGTC5dqeT0oqYfcgfMZO30mO/tdBzPrfKFmfVRSdrlitX kucxZ91E0SkFqe67L8RaGhfprv6EMybZqSw5iCHzo7Zcg0gKsszKWADxCQcGxH7QaiWP 5vff1Ex8otISFRExnlfjZ2ULUFzh7NkD26vU27JMKqmQuGzgltGajnHNo0ZwrUKgOBSB cVug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=sK8alkZcupJnWSW83oQWcAMUWywag3RATfIL9VLzBjw=; b=R8WKewva8t5FdU64bKCEVc7VyycKsxNKPGRYckPYpqdy5R5Gr03VQ5gnD70ykfs+Wy MQhwGksMxleiSw3JysoNFKbLdkRyDF6xjM1neLJ8CEMMFOfu364Nn/ErNgYJwvYy4Wzh gsRsFegvGS8j2y5bvsmY3kKfjEVAAKW69aGLil9Kvn3VxwPqST3+eADyEtjdEv2ufPK1 JPsNeuV9QOARVk4Ssczp6ktmHCRaPdF9TtDGkCIDLTfMtpSYzI72tuqjyN2C0vEO4miY 6/pQlxuxB3hWXLJPAuiutbFd6S22b8UklhXKhxZQsgUHfRRUo8u8mTipGaLyLqRz+jD3 riSQ== X-Gm-Message-State: AJaThX7T1PRcfxMYhNdWiQ0Q9ipuWJ3iRSEHc34xdH6dDinePt8a7P53 KsJD4HCYxOyU7Lsx9R+lYbjvKmUL X-Google-Smtp-Source: AGs4zMbuqPxz4igwIGSaaC7wZ+QZIngs8X+tyb+L+7vP0LwUIwvvGSOEOJGe8P/2nI9el1y2kayGbg== X-Received: by 10.28.61.87 with SMTP id k84mr7898587wma.48.1511108366143; Sun, 19 Nov 2017 08:19:26 -0800 (PST) Received: from debian64.daheim (p200300D5FBD775FC0000000000000830.dip0.t-ipconnect.de. [2003:d5:fbd7:75fc::830]) by smtp.gmail.com with ESMTPSA id w204sm10153895wmw.23.2017.11.19.08.19.24 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sun, 19 Nov 2017 08:19:24 -0800 (PST) Received: from chuck by debian64.daheim with local (Exim 4.89) (envelope-from ) id 1eGSJX-0004b5-M9 for lede-dev@lists.infradead.org; Sun, 19 Nov 2017 17:19:23 +0100 From: Christian Lamparter To: Cc: lede-dev@lists.infradead.org Date: Sun, 19 Nov 2017 17:19:21 +0100 Message-Id: <4d3355c63e097909ac35bce0dfee85e11e25b11e.1511108316.git.chunkeey@gmail.com> X-Mailer: git-send-email 2.15.0 In-Reply-To: <1b83b7cb314ff2a5f56bd666f585e999faed52d0.1511108316.git.chunkeey@gmail.com> References: <1b83b7cb314ff2a5f56bd666f585e999faed52d0.1511108316.git.chunkeey@gmail.com> In-Reply-To: <1b83b7cb314ff2a5f56bd666f585e999faed52d0.1511108316.git.chunkeey@gmail.com> References: <1b83b7cb314ff2a5f56bd666f585e999faed52d0.1511108316.git.chunkeey@gmail.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20171119_081949_153525_EA08216C X-CRM114-Status: GOOD ( 11.31 ) 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 RCVD_IN_DNSWL_NONE RBL: Sender listed at http://www.dnswl.org/, no trust [2a00:1450:400c:c09:0:0:0:242 listed in] [list.dnswl.org] -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_AU Message has a valid DKIM or DK signature from author's domain -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 Subject: [LEDE-DEV] [PATCH 2/4] 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 --- 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