From patchwork Wed Jan 20 17:36:52 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Bj=C3=B8rn_Mork?= X-Patchwork-Id: 1429376 X-Patchwork-Delegate: ynezz@true.cz Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.openwrt.org (client-ip=2001:8b0:10b:1231::1; helo=merlin.infradead.org; envelope-from=openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=merlin.20170209 header.b=u7Gj0xhr; dkim=fail reason="signature verification failed" (1024-bit key; secure) header.d=mork.no header.i=@mork.no header.a=rsa-sha256 header.s=b header.b=i0Q0cKr5; dkim-atps=neutral Received: from merlin.infradead.org (merlin.infradead.org [IPv6:2001:8b0:10b:1231::1]) (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 ozlabs.org (Postfix) with ESMTPS id 4DLY5Y0cL6z9sRR for ; Thu, 21 Jan 2021 04:52:49 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To:Message-Id:Date: Subject:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=I+QVAnjJ6lpo+ynLmdkfSGQ+Ur13jl8JQODOQrJfEwU=; b=u7Gj0xhr+PNeN/WWjcB7Amuz5 B4yfn16e6cWakRGRvAhR4inZV+o7kuTM3PF/cfKHzSfUHahv6zQBjqCAYOvA8K3AcCYTNoCkwMm5w cxCgF0jYe/vWgYcI1zeZLBW0AYVQDwrQyaQMdoRuj3qImyepHl5BerUJ4xXyV5RpZ/OhK3YTTwZlj sW1DqkW/F6TQfEKP4/P4bXwcIiEERsAXghpYiQFNXW6vrrk0Yb6o+GTb/bqcybihN9BncAxgIARGW 6H4RnXhTKf05Uuudvaew7DygDoxz8Hk0Xp0i4KugDks/OBQJnIOfv5n59AXiamhooID8tZ8BSje+e IlORRfnnA==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1l2Hd9-00012N-UH; Wed, 20 Jan 2021 17:50:56 +0000 Received: from canardo.mork.no ([2001:4641::1]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1l2Hd5-0000zf-Mw for openwrt-devel@lists.openwrt.org; Wed, 20 Jan 2021 17:50:53 +0000 Received: from canardo.mork.no (ip6-localhost [IPv6:0:0:0:0:0:0:0:1]) by canardo.mork.no (8.15.2/8.15.2) with ESMTPS id 10KHb57I028944 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=OK); Wed, 20 Jan 2021 18:37:05 +0100 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mork.no; s=b; t=1611164225; bh=xPEIR9ZREDdsw4/DYjsni00fMKrcyvgxTi7EypUXuBk=; h=From:To:Cc:Subject:Date:Message-Id:References:From; b=i0Q0cKr58rP/+uCyEO87fqVawrqKzG2xnWDjtz24niUVr35TTQUXl/H0kr+YSsbdV ZtuMWUG1UqLb9PI87mOWYK6SSmuJMjsdWs88tpWYFA6dhCJyzCB5rQXao7XBK2mJJ7 GhbeE0RKrnhSl1TJLpZMdb0qpXsjjUTGQlUgXE1k= Received: (from bjorn@localhost) by canardo.mork.no (8.15.2/8.15.2/Submit) id 10KHb5D3028937; Wed, 20 Jan 2021 18:37:05 +0100 From: =?utf-8?q?Bj=C3=B8rn_Mork?= To: openwrt-devel@lists.openwrt.org Subject: [PATCH v4 09/10] kernel: mtdsplit_uimage: add "openwrt, offset" and "openwrt, partition-magic" Date: Wed, 20 Jan 2021 18:36:52 +0100 Message-Id: <20210120173653.28784-10-bjorn@mork.no> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210120173653.28784-1-bjorn@mork.no> References: <20210120173653.28784-1-bjorn@mork.no> MIME-Version: 1.0 X-Spam-Status: No, score=-1.0 required=5.0 tests=ALL_TRUSTED,SHORTCIRCUIT shortcircuit=ham autolearn=disabled version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on canardo.mork.no X-Virus-Scanned: clamav-milter 0.102.4 at canardo X-Virus-Status: Clean X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210120_125052_060111_D41DADC4 X-CRM114-Status: GOOD ( 15.21 ) X-Spam-Score: -2.5 (--) X-Spam-Report: SpamAssassin version 3.4.4 on merlin.infradead.org summary: Content analysis details: (-2.5 points) pts rule name description ---- ---------------------- -------------------------------------------------- -2.3 RCVD_IN_DNSWL_MED RBL: Sender listed at https://www.dnswl.org/, medium trust [2001:4641:0:0:0:0:0:1 listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record -0.0 SPF_HELO_PASS SPF: HELO matches SPF record 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: OpenWrt Development List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: =?utf-8?q?Bj=C3=B8rn_Mork?= Sender: "openwrt-devel" Errors-To: openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org Some devices prepend a standard U-Boot Image with a vendor specific header, having its own magic. Adding two new properties will support validation of such images, including the additional magic. Signed-off-by: Bjørn Mork --- .../drivers/mtd/mtdsplit/mtdsplit_uimage.c | 40 +++++++++++++------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/target/linux/generic/files/drivers/mtd/mtdsplit/mtdsplit_uimage.c b/target/linux/generic/files/drivers/mtd/mtdsplit/mtdsplit_uimage.c index c29eb6234bc2..7a8ccdf8f59a 100644 --- a/target/linux/generic/files/drivers/mtd/mtdsplit/mtdsplit_uimage.c +++ b/target/linux/generic/files/drivers/mtd/mtdsplit/mtdsplit_uimage.c @@ -70,7 +70,8 @@ read_uimage_header(struct mtd_info *mtd, size_t offset, u_char *buf, } static void uimage_parse_dt(struct mtd_info *master, int *extralen, - u32 *ih_magic, u32 *ih_type) + u32 *ih_magic, u32 *ih_type, + u32 *header_offset, u32 *part_magic) { struct device_node *np = mtd_get_of_node(master); @@ -83,6 +84,10 @@ static void uimage_parse_dt(struct mtd_info *master, int *extralen, pr_debug("got openwrt,ih-magic=%08x from device-tree\n", *ih_magic); if (!of_property_read_u32(np, "openwrt,ih-type", ih_type)) pr_debug("got openwrt,ih-type=%08x from device-tree\n", *ih_type); + if (!of_property_read_u32(np, "openwrt,offset", header_offset)) + pr_debug("got ih-start=%u from device-tree\n", *header_offset); + if (!of_property_read_u32(np, "openwrt,partition-magic", part_magic)) + pr_debug("got openwrt,partition-magic=%08x from device-tree\n", *part_magic); } /** @@ -92,9 +97,9 @@ static void uimage_parse_dt(struct mtd_info *master, int *extralen, * and tail padding length of a valid uImage header if found */ static int __mtdsplit_parse_uimage(struct mtd_info *master, - const struct mtd_partition **pparts, - struct mtd_part_parser_data *data, - ssize_t (*find_header)(u_char *buf, size_t len, u32 ih_magic, u32 ih_type)) + const struct mtd_partition **pparts, + struct mtd_part_parser_data *data, + ssize_t (*find_header)(u_char *buf, size_t len, u32 ih_magic, u32 ih_type)) { struct mtd_partition *parts; u_char *buf; @@ -104,11 +109,14 @@ static int __mtdsplit_parse_uimage(struct mtd_info *master, size_t uimage_size = 0; size_t rootfs_offset; size_t rootfs_size = 0; + size_t buflen; int uimage_part, rf_part; int ret; int extralen = 0; u32 ih_magic = IH_MAGIC; u32 ih_type = IH_TYPE_KERNEL; + u32 header_offset = 0; + u32 part_magic = 0; enum mtdsplit_part_type type; nr_parts = 2; @@ -116,34 +124,42 @@ static int __mtdsplit_parse_uimage(struct mtd_info *master, if (!parts) return -ENOMEM; - buf = vmalloc(MAX_HEADER_LEN); + uimage_parse_dt(master, &extralen, &ih_magic, &ih_type, &header_offset, &part_magic); + buflen = MAX_HEADER_LEN; + buf = vmalloc(buflen); if (!buf) { ret = -ENOMEM; goto err_free_parts; } - uimage_parse_dt(master, &extralen, &ih_magic, &ih_type); - /* find uImage on erase block boundaries */ for (offset = 0; offset < master->size; offset += master->erasesize) { struct uimage_header *header; uimage_size = 0; - ret = read_uimage_header(master, offset, buf, MAX_HEADER_LEN); + ret = read_uimage_header(master, offset, buf, buflen); if (ret) continue; - ret = find_header(buf, MAX_HEADER_LEN, ih_magic, ih_type); + /* verify optional partition magic before uimage header */ + if (header_offset && part_magic && (be32_to_cpu(*(u32 *)buf) != part_magic)) + continue; + + ret = find_header(buf + header_offset, buflen, ih_magic, ih_type); if (ret < 0) { pr_debug("no valid uImage found in \"%s\" at offset %llx\n", master->name, (unsigned long long) offset); continue; } - header = (struct uimage_header *)(buf + ret); + + /* let uimage_find_edimax override the offset */ + if (ret > 0) + header_offset = ret; + header = (struct uimage_header *)(buf + header_offset); uimage_size = sizeof(*header) + - be32_to_cpu(header->ih_size) + ret + extralen; + be32_to_cpu(header->ih_size) + header_offset + extralen; if ((offset + uimage_size) > master->size) { pr_debug("uImage exceeds MTD device \"%s\"\n", @@ -254,7 +270,7 @@ mtdsplit_uimage_parse_generic(struct mtd_info *master, struct mtd_part_parser_data *data) { return __mtdsplit_parse_uimage(master, pparts, data, - uimage_verify_default); + uimage_verify_default); } static const struct of_device_id mtdsplit_uimage_of_match_table[] = {