From patchwork Thu Mar 9 17:09:43 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 9613695 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 55F4A60414 for ; Thu, 9 Mar 2017 17:10:09 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 41056286A9 for ; Thu, 9 Mar 2017 17:10:09 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3229E286BC; Thu, 9 Mar 2017 17:10:09 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id A1920286A9 for ; Thu, 9 Mar 2017 17:10:08 +0000 (UTC) Received: from localhost ([::1]:35368 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cm1Zn-0004tE-Oe for patchwork-qemu-devel@patchwork.kernel.org; Thu, 09 Mar 2017 12:10:07 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46512) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cm1Za-0004sx-CO for qemu-devel@nongnu.org; Thu, 09 Mar 2017 12:09:55 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cm1ZW-0002gG-BU for qemu-devel@nongnu.org; Thu, 09 Mar 2017 12:09:54 -0500 Received: from mail-wm0-x22f.google.com ([2a00:1450:400c:c09::22f]:37775) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cm1ZW-0002fd-4R for qemu-devel@nongnu.org; Thu, 09 Mar 2017 12:09:50 -0500 Received: by mail-wm0-x22f.google.com with SMTP id n11so61268774wma.0 for ; Thu, 09 Mar 2017 09:09:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=MHpk0k9T73lsvfE0Vkv8Ij5QLRM1/uaw+a3COzk1/qI=; b=PpKMpuPdN24pnaz5O4jrqD2YodGAZo9cpHerCwmFGfHZgviev2KIIDwzAODf6Kn1fn M2RbVNBKHrQbSfv1kxInjDKXb00NqKyxK5SW8bmW6UO14/tksTUmLi6QmnyUi4X0V5SE WPIS0MsFfIU0NzzurfQJE1z0c6xL+YbvDHCF4= 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; bh=MHpk0k9T73lsvfE0Vkv8Ij5QLRM1/uaw+a3COzk1/qI=; b=SYkvaSmoqy4edxX2Q6+I63Q82nDb11kcLV/P6VQEgbiem4o+kKAMoj6pFdBLFFx4bR k4lsv0szlph5sYzAwpCud1Deq6BQUf6S4Lm1AlIBHNkXWFpYgW9jW72gy1fYJtSRupOK sR7it55mhk/yHyoIh2psiaN1zATQwOBKZ8lCV36QGP85JrQgxpIw+1RFXbeE7lS8xKlG ZjKUmJehJONajpDR0g2Sma0a3RYWR4arMeX+AroPCO4Q+jjl0PEAmNBxZo44dH+Pr7zB dbMp4UyilPLzVEtytRvshIiVqwYaA02pzbIX9oxQSIHwvQw6YdyYB/UcrNj9OA2W91rf O/RQ== X-Gm-Message-State: AMke39mReZ+LToWCg6ozpnMCqH2P08BSK+6yJ6fNjD9oTUysJE84KgABjKWd4uqgkbz9Ps5h X-Received: by 10.28.134.70 with SMTP id i67mr13364769wmd.124.1489079387271; Thu, 09 Mar 2017 09:09:47 -0800 (PST) Received: from ards-macbook-pro.c.hoisthospitality.com ([109.74.48.129]) by smtp.gmail.com with ESMTPSA id 94sm8917826wrl.50.2017.03.09.09.09.46 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 09 Mar 2017 09:09:46 -0800 (PST) From: Ard Biesheuvel To: qemu-devel@nongnu.org, peter.maydell@linaro.org Date: Thu, 9 Mar 2017 18:09:43 +0100 Message-Id: <1489079383-11162-1-git-send-email-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.7.4 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::22f Subject: [Qemu-devel] [PATCH] hw/arm/boot: take Linux/arm64 TEXT_OFFSET header field into account X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Ard Biesheuvel Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP The arm64 boot protocol stipulates that the kernel must be loaded TEXT_OFFSET bytes beyond a 2 MB aligned base address, where TEXT_OFFSET could be any 4 KB multiple between 0 and 2 MB, and whose value can be found in the header of the Image file. So after attempts to load the kernel image as an ELF file or as a U-Boot image (both of which have their own way of specifying the load offset) have failed, try to determine the TEXT_OFFSET from the image before proceeding with loading it as a gzipped or raw image. Signed-off-by: Ard Biesheuvel --- hw/arm/boot.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/hw/arm/boot.c b/hw/arm/boot.c index ff621e4b6a4b..0824f74c697f 100644 --- a/hw/arm/boot.c +++ b/hw/arm/boot.c @@ -31,6 +31,11 @@ #define KERNEL_LOAD_ADDR 0x00010000 #define KERNEL64_LOAD_ADDR 0x00080000 +#define ARM64_IMAGE_HEADER_SIZE 64 +#define ARM64_TEXT_OFFSET_OFFSET 8 +#define ARM64_IMAGE_SIZE_OFFSET 16 +#define ARM64_MAGIC_OFFSET 56 + typedef enum { FIXUP_NONE = 0, /* do nothing */ FIXUP_TERMINATOR, /* end of insns */ @@ -768,6 +773,51 @@ static uint64_t arm_load_elf(struct arm_boot_info *info, uint64_t *pentry, return ret; } +static void aarch64_get_text_offset(struct arm_boot_info *info, + hwaddr *text_offset) +{ + uint8_t *buffer; + uint64_t headerval; + int size; + + size = load_image_gzipped_buffer(info->kernel_filename, + LOAD_IMAGE_MAX_GUNZIP_BYTES, + &buffer); + + if (size < 0) { + int fd, bytes; + + fd = open(info->kernel_filename, O_RDONLY | O_BINARY); + if (fd < 0) + return; + + buffer = g_malloc(ARM64_IMAGE_HEADER_SIZE); + bytes = read(fd, buffer, ARM64_IMAGE_HEADER_SIZE); + close(fd); + + if (bytes < ARM64_IMAGE_HEADER_SIZE) + goto free_buffer; + } + + /* check the arm64 magic header value */ + if (memcmp(buffer + ARM64_MAGIC_OFFSET, "ARM\x64", 4) != 0) + goto free_buffer; + + /* The arm64 Image header has text_offset and image_size fields at 8 and + * 16 bytes into the Image header, respectively. The text_offset field is + * only valid if the image_size is non-zero. + */ + memcpy(&headerval, buffer + ARM64_IMAGE_SIZE_OFFSET, sizeof(headerval)); + if (headerval == 0) + goto free_buffer; + + memcpy(&headerval, buffer + ARM64_TEXT_OFFSET_OFFSET, sizeof(headerval)); + *text_offset = le64_to_cpu(headerval); + +free_buffer: + g_free(buffer); +} + static void arm_load_kernel_notify(Notifier *notifier, void *data) { CPUState *cs; @@ -900,6 +950,12 @@ static void arm_load_kernel_notify(Notifier *notifier, void *data) kernel_size = load_uimage(info->kernel_filename, &entry, NULL, &is_linux, NULL, NULL); } + + /* A bare Linux/arm64 kernel carries the load offset in the Image header */ + if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64) && kernel_size < 0) { + aarch64_get_text_offset(info, &kernel_load_offset); + } + /* On aarch64, it's the bootloader's job to uncompress the kernel. */ if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64) && kernel_size < 0) { entry = info->loader_start + kernel_load_offset;