From patchwork Mon Apr 8 15:35:27 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eugeniu Rosca X-Patchwork-Id: 1081176 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=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=de.adit-jv.com Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 44dDz76g7sz9sPb for ; Tue, 9 Apr 2019 01:36:03 +1000 (AEST) Received: by lists.denx.de (Postfix, from userid 105) id A5594C21E0B; Mon, 8 Apr 2019 15:36:00 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=KHOP_BIG_TO_CC autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id BD01FC21DD4; Mon, 8 Apr 2019 15:35:57 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 97827C21DD4; Mon, 8 Apr 2019 15:35:56 +0000 (UTC) Received: from smtp1.de.adit-jv.com (smtp1.de.adit-jv.com [93.241.18.167]) by lists.denx.de (Postfix) with ESMTPS id 15571C21D4A for ; Mon, 8 Apr 2019 15:35:56 +0000 (UTC) Received: from localhost (smtp1.de.adit-jv.com [127.0.0.1]) by smtp1.de.adit-jv.com (Postfix) with ESMTP id 4DC9D3C00D1; Mon, 8 Apr 2019 17:35:55 +0200 (CEST) Received: from smtp1.de.adit-jv.com ([127.0.0.1]) by localhost (smtp1.de.adit-jv.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 9vs--ZpKHb2z; Mon, 8 Apr 2019 17:35:47 +0200 (CEST) Received: from HI2EXCH01.adit-jv.com (hi2exch01.adit-jv.com [10.72.92.24]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by smtp1.de.adit-jv.com (Postfix) with ESMTPS id D94013C00C6; Mon, 8 Apr 2019 17:35:47 +0200 (CEST) Received: from vmlxhi-102.adit-jv.com (10.72.93.184) by HI2EXCH01.adit-jv.com (10.72.92.24) with Microsoft SMTP Server (TLS) id 14.3.439.0; Mon, 8 Apr 2019 17:35:47 +0200 From: Eugeniu Rosca To: Tom Rini , Marek Vasut , Simon Glass , Alex Kiernan , Simon Goldschmidt , Alexander Graf , Heinrich Schuchardt , Bryan O'Donoghue , Peng Fan , Stefano Babic , Philippe Reynes , Fabio Estevam , Date: Mon, 8 Apr 2019 17:35:27 +0200 Message-ID: <20190408153528.23783-1-erosca@de.adit-jv.com> X-Mailer: git-send-email 2.21.0 MIME-Version: 1.0 X-Originating-IP: [10.72.93.184] Cc: Eugeniu Rosca , Eugeniu Rosca Subject: [U-Boot] [PATCH v2 1/2] image: android: allow booting lz4-compressed kernels X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 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" According to Android image format [1], kernel image resides at 1 page offset from the boot image address. Grab the magic number from there and allow U-Boot to handle LZ4-compressed KNL binaries instead of hardcoding compression type to IH_COMP_NONE. Other compression types, if needed, can be added later. Tested on H3ULCB-KF using the image detailed in [2]. [1] Excerpt from include/android_image.h +-----------------+ | boot header | 1 page +-----------------+ | kernel | n pages +-----------------+ | ramdisk | m pages +-----------------+ | second stage | o pages +-----------------+ [2] => iminfo 4c000000 ## Checking Image at 4c000000 ... Android image found kernel size: 85b9d1 kernel address: 48080000 ramdisk size: 54ddbc ramdisk addrress: 4a180000 second size: 0 second address: 48000800 tags address: 48000100 page size: 800 os_version: 1200012a (ver: 0.9.0, level: 2018.10) name: cmdline: buildvariant=userdebug Signed-off-by: Eugeniu Rosca --- v2: - [Marek] Relocate LZ4F_MAGIC from lib/lz4_wrapper.c to include/image.h. This could be factored out into a standalone non-functional change if needed (having it included in this patch reveals its true purpose though) - [Marek] Use get_unaligned() primitive in accordance with doc/README.unaligned-memory-access.txt. NOTE: get_unaligned_le32() looks more appropriate, since the lz4 magic is stored in LE32: https://github.com/lz4/lz4/blob/f66abc45d9b96/lib/lz4frame.c#L659 - Drop switch-case in favor of if-else, since the magic numbers of other compression types are not necessarily 32-bit wide (handling a void pointer looks to be more generic) - Keep android_image_get_kcomp() of type 'ulong' to please the eye (to stay in harmony with other android_image_get_* functions), even if its true return value merely needs a signed/unsigned int - Static analysis: sparse, smatch and `make W=1` are all silent - Runtime: boot-tested on H3ULCB-KF using an Android boot image containing a lz4-compressed kernel v1: - https://patchwork.ozlabs.org/patch/1076477/ --- common/bootm.c | 2 +- common/image-android.c | 11 +++++++++++ include/image.h | 2 ++ lib/lz4_wrapper.c | 3 +-- 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/common/bootm.c b/common/bootm.c index 3adbceaa38e3..bbae66df1001 100644 --- a/common/bootm.c +++ b/common/bootm.c @@ -154,7 +154,7 @@ static int bootm_find_os(cmd_tbl_t *cmdtp, int flag, int argc, #ifdef CONFIG_ANDROID_BOOT_IMAGE case IMAGE_FORMAT_ANDROID: images.os.type = IH_TYPE_KERNEL; - images.os.comp = IH_COMP_NONE; + images.os.comp = android_image_get_kcomp(os_hdr); images.os.os = IH_OS_LINUX; images.os.end = android_image_get_end(os_hdr); diff --git a/common/image-android.c b/common/image-android.c index 2f38c191e911..c31dcd446526 100644 --- a/common/image-android.c +++ b/common/image-android.c @@ -8,6 +8,7 @@ #include #include #include +#include #define ANDROID_IMAGE_DEFAULT_KERNEL_ADDR 0x10008000 @@ -126,6 +127,16 @@ ulong android_image_get_kload(const struct andr_img_hdr *hdr) return android_image_get_kernel_addr(hdr); } +ulong android_image_get_kcomp(const struct andr_img_hdr *hdr) +{ + const void *p = (void *)((uintptr_t)hdr + hdr->page_size); + + if (get_unaligned_le32(p) == LZ4F_MAGIC) + return IH_COMP_LZ4; + else + return IH_COMP_NONE; +} + int android_image_get_ramdisk(const struct andr_img_hdr *hdr, ulong *rd_data, ulong *rd_len) { diff --git a/include/image.h b/include/image.h index 765ffecee0a7..889305cbefdb 100644 --- a/include/image.h +++ b/include/image.h @@ -306,6 +306,7 @@ enum { IH_COMP_COUNT, }; +#define LZ4F_MAGIC 0x184D2204 /* LZ4 Magic Number */ #define IH_MAGIC 0x27051956 /* Image Magic Number */ #define IH_NMLEN 32 /* Image Name Length */ @@ -1312,6 +1313,7 @@ int android_image_get_second(const struct andr_img_hdr *hdr, ulong *second_data, ulong *second_len); ulong android_image_get_end(const struct andr_img_hdr *hdr); ulong android_image_get_kload(const struct andr_img_hdr *hdr); +ulong android_image_get_kcomp(const struct andr_img_hdr *hdr); void android_print_contents(const struct andr_img_hdr *hdr); #endif /* CONFIG_ANDROID_BOOT_IMAGE */ diff --git a/lib/lz4_wrapper.c b/lib/lz4_wrapper.c index 487d39ef0247..1c68e67452d9 100644 --- a/lib/lz4_wrapper.c +++ b/lib/lz4_wrapper.c @@ -5,6 +5,7 @@ #include #include +#include #include #include @@ -23,8 +24,6 @@ typedef uint64_t U64; /* Unaltered (except removing unrelated code) from github.com/Cyan4973/lz4. */ #include "lz4.c" /* #include for inlining, do not link! */ -#define LZ4F_MAGIC 0x184D2204 - struct lz4_frame_header { u32 magic; union {