From patchwork Fri Mar 6 00:24:22 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Patra X-Patchwork-Id: 1249934 X-Patchwork-Delegate: trini@ti.com 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.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=wdc.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=wdc.com header.i=@wdc.com header.a=rsa-sha256 header.s=dkim.wdc.com header.b=LEj7zJOY; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 48YT1S4Wjgz9sPJ for ; Fri, 6 Mar 2020 11:26:04 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id DF6C981269; Fri, 6 Mar 2020 01:25:42 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=wdc.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=wdc.com header.i=@wdc.com header.b="LEj7zJOY"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 4CEFA81289; Fri, 6 Mar 2020 01:25:40 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,SPF_HELO_NONE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from esa5.hgst.iphmx.com (esa5.hgst.iphmx.com [216.71.153.144]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 72EAB802DB for ; Fri, 6 Mar 2020 01:25:35 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=wdc.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=prvs=3272f33ed=atish.patra@wdc.com DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=wdc.com; i=@wdc.com; q=dns/txt; s=dkim.wdc.com; t=1583454336; x=1614990336; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=oyBv9VpMW3GhR7nP2mh1GArS0UhQWkTk+CTyGM4ISdE=; b=LEj7zJOYJZpT2Z7lQsrIa3DpS1eHlBHWpVayCNjpmNKonN8fAuNGlGaP gy/7eNhLx0eAUX9I4SB+vaXWWrQexd/TzrWLY96bLKgQXbXDxtizerF4E VG0leRlu76RrgufLy4GzwOecRGQv5rlqFL/gPJFSG2ALnpta5T0Eim6e7 o1NBfvms2EH9Tq6bRzl896JPonHCMV+VrsM9lEhdAQQItkeOu1JaR6r4q QbZWp6W8ZjqAXcoEb8I+QeVP3wL/miDeuimXo/HWSJ0fDN7QUYlSrJpb/ oe51lL3i4ZCwyzwGM0RcNRWAXLKnYMVP+aBL+aHmhaxaSumE+vy4YqMRP Q==; IronPort-SDR: WlecTG9E4CfnX0RAUDe5dDlvA2W5N1kaPsgwhK42jh93fA1syO0CAFi9iQJRaYu1YVpBX0DzV6 w5eb4A65EM4ssTl//EEQTODEmT9egEp+MAIHV1sVFQgTwrr1h08qR4XPYUaMg0l+7ypv0zJoBL H/NSyVbgF3AuEbZo8ZMphEVbnL3Am+DkFCfJ6k+vb90t182+V/BZQJCFkt7MiNKEoIiewZLBAf ktqzhYtN4HejSySWbMJH2jKcGnh5KBvOfOz8V/RLUb1cVBwUF6Tk24J5fXFMkZDtadI8Euxdwm 6pc= X-IronPort-AV: E=Sophos;i="5.70,520,1574092800"; d="scan'208";a="132152827" Received: from h199-255-45-14.hgst.com (HELO uls-op-cesaep01.wdc.com) ([199.255.45.14]) by ob1.hgst.iphmx.com with ESMTP; 06 Mar 2020 08:25:14 +0800 IronPort-SDR: y7b8CtZfghavNO5pPOxrr2eceP5ubICJX0GLQORjvtzhA6k31XFSHig3gwJvR5QMCvNn58PhWW /2I+4kyJvAFYsNXv+dfOaWq/Z87nXUm3yFwEtd/9jRSo89SzYlSzp2/QWrArKCnGTcN+J7NuS0 r5obClHCngGm3pTQZUW9GJxU4kcYasi1ohGYNdcvr3Q4JfGzXekGE/gh8yEYjkGHGsGWxqNaR3 60qvTKdtA5R+8RrM77ZvjsyREt0xMUfpIAal7ArbTwkgFWDZiSjhH8BRFpiU3ltKySXZOB3LTO gv1kVxxbi621wzqbv+7id7ol Received: from uls-op-cesaip02.wdc.com ([10.248.3.37]) by uls-op-cesaep01.wdc.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Mar 2020 16:17:24 -0800 IronPort-SDR: HnF1+qiuwYkJ7ALvhPFeY4m4LFntaZ7ibkkq/CgVZzGZmB0WCczSzj7MB3ZeLOQP5as7r3ORLc usvUpDDIu4TSQVi5f50Rd8+/twLYgxHIdW/ChIagScNsVQDXJwyNBqKaf8NHCSlQejN+jfwSTD KXn2Oxyj/FsbgGs92j+LyA6XyzPAdxamxRmVXbKXSegwwbx4gGR6+8NbDUcaKkSf527IvzngAy o8wwytPV8s5+2m7MxclkxebsjsA6I8YML0BTlgyI2hFddEQUAzY5bptRLsf1Q5WQxw7eoBnW0g SC8= WDCIronportException: Internal Received: from jedi-01.sdcorp.global.sandisk.com (HELO jedi-01.int.fusionio.com) ([10.11.143.218]) by uls-op-cesaip02.wdc.com with ESMTP; 05 Mar 2020 16:25:14 -0800 From: Atish Patra To: u-boot@lists.denx.de Cc: Atish Patra , Tom Rini , Anup Patel , Bin Meng , Heinrich Schuchardt , Jagan Teki , Marek Vasut , Simon Goldschmidt , David Abdurachmanov Subject: [RFT PATCH v5 2/3] image: Add a common compression type detection function. Date: Thu, 5 Mar 2020 16:24:22 -0800 Message-Id: <20200306002423.3954-3-atish.patra@wdc.com> X-Mailer: git-send-email 2.24.0 In-Reply-To: <20200306002423.3954-1-atish.patra@wdc.com> References: <20200306002423.3954-1-atish.patra@wdc.com> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.30rc1 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.102.2 at phobos.denx.de X-Virus-Status: Clean Currently, there is no method that can detect compression types given a file. This is very useful where a compressed kernel image is loaded directly to the memory. Inspect initial few bytes to figure out compression type of the image. It will be used in booti method for now but can be reused any other function in future as well. Signed-off-by: Atish Patra Reviewed-by: Tom Rini --- common/image.c | 23 +++++++++++++++++++++++ include/image.h | 21 +++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/common/image.c b/common/image.c index 94873cb6ed50..d8d14e871c64 100644 --- a/common/image.c +++ b/common/image.c @@ -202,6 +202,14 @@ struct table_info { const table_entry_t *table; }; +static const struct comp_magic_map image_comp[] = { + { IH_COMP_BZIP2, "bzip2", {0x42, 0x5a},}, + { IH_COMP_GZIP, "gzip", {0x1f, 0x8b},}, + { IH_COMP_LZMA, "lzma", {0x5d, 0x00},}, + { IH_COMP_LZO, "lzo", {0x89, 0x4c},}, + { IH_COMP_NONE, "none", {}, }, +}; + static const struct table_info table_info[IH_COUNT] = { { "architecture", IH_ARCH_COUNT, uimage_arch }, { "compression", IH_COMP_COUNT, uimage_comp }, @@ -407,6 +415,21 @@ static void print_decomp_msg(int comp_type, int type, bool is_xip) printf(" Uncompressing %s\n", name); } +int image_decomp_type(const unsigned char *buf, ulong len) +{ + const struct comp_magic_map *cmagic = image_comp; + + if (len < 2) + return -EINVAL; + + for (; cmagic->comp_id > 0; cmagic++) { + if (!memcmp(buf, cmagic->magic, 2)) + break; + } + + return cmagic->comp_id; +} + int image_decomp(int comp, ulong load, ulong image_start, int type, void *load_buf, void *image_buf, ulong image_len, uint unc_len, ulong *load_end) diff --git a/include/image.h b/include/image.h index b316d167d8d7..9bbb1acfb3af 100644 --- a/include/image.h +++ b/include/image.h @@ -452,6 +452,15 @@ typedef struct table_entry { char *lname; /* long (output) name to print for messages */ } table_entry_t; +/* + * Compression type and magic number mapping table. + */ +struct comp_magic_map { + int comp_id; + const char *name; + unsigned char magic[2]; +}; + /* * get_table_entry_id() scans the translation table trying to find an * entry that matches the given short name. If a matching entry is @@ -868,6 +877,18 @@ static inline int image_check_target_arch(const image_header_t *hdr) } #endif /* USE_HOSTCC */ +/** + * image_decomp_type() - Find out compression type of an image + * + * @buf: Address in U-Boot memory where image is loaded. + * @len: Length of the compressed image. + * @return compression type or IH_COMP_NONE if not compressed. + * + * Note: Only following compression types are supported now. + * lzo, lzma, gzip, bzip2 + */ +int image_decomp_type(const unsigned char *buf, ulong len); + /** * image_decomp() - decompress an image *