From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.5 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id AA247C4338F for ; Thu, 29 Jul 2021 15:34:26 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 7600560F42 for ; Thu, 29 Jul 2021 15:34:26 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 7600560F42 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=arm.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:List-Subscribe:List-Help: List-Post:List-Archive:List-Unsubscribe:List-Id:References:In-Reply-To: Message-Id:Date:Subject: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=l+I8NCH2uy6VBbV52w4sA5RYH9P13SkxW2aBuO6hvHw=; b=WZWfDJQIg6E5+k EBNUPs2FgtENPiRU8P0k1j4KMnyqqMrFA30mz60HRVUzzi0JeDI+shOX/q+5nSkYoAbQl5SY40bVE juR7rUvbzqjsB0KJ256bQfne6EziGQVUkD6Z237mBhe0fh3AD2HHJuxls/fjN3cVjfxbmPGQj62+M WG0v/DC00wnecouhKn5NCE64cyUWFw3yN+H1yY/yXFik6FhNvq96D8lD36I3w45vxFBGEKnGpujch uBFF80M5HJmge5WiTOqwSviorq+8ls+BfAa/3PGF7caK7KTUnbFiceO6o3vqXcD/FEiD2SU2CXHag aEOqgCT+6oqbDJY6Sj2Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1m980k-004jQp-FK; Thu, 29 Jul 2021 15:31:52 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1m97qR-004ge9-AE for linux-arm-kernel@lists.infradead.org; Thu, 29 Jul 2021 15:21:16 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 8205A11D4; Thu, 29 Jul 2021 08:21:06 -0700 (PDT) Received: from lakrids.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.121.207.14]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id B62773F73D; Thu, 29 Jul 2021 08:21:05 -0700 (PDT) From: Mark Rutland To: linux-arm-kernel@lists.infradead.org Cc: Andre.Przywara@arm.com, Jaxson.Han@arm.com, mark.rutland@arm.com, Wei.Chen@arm.com Subject: [boot-wrapper PATCH 07/12] aarch64: respect text offset Date: Thu, 29 Jul 2021 16:20:45 +0100 Message-Id: <20210729152050.23635-8-mark.rutland@arm.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20210729152050.23635-1-mark.rutland@arm.com> References: <20210729152050.23635-1-mark.rutland@arm.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210729_082111_558240_8FB38B81 X-CRM114-Status: GOOD ( 18.67 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org The boot-wrapper assumes that an AArch64 kernel's text offset is 0x80000 rather than reading the `text_offset` field from the Image header as the documentation says it should. Add a script to figure this out during the build process. As with FDT.pm the parsing of the Image (and common logic associated with this) is factored into a module that we may use in more scripts in future. Signed-off-by: Mark Rutland --- Makefile.am | 4 +-- scripts/AA64Image.pm | 87 +++++++++++++++++++++++++++++++++++++++++++++ scripts/aa64-load-offset.pl | 28 +++++++++++++++ 3 files changed, 117 insertions(+), 2 deletions(-) create mode 100755 scripts/AA64Image.pm create mode 100755 scripts/aa64-load-offset.pl diff --git a/Makefile.am b/Makefile.am index ad13dbc..5d34cc8 100644 --- a/Makefile.am +++ b/Makefile.am @@ -76,12 +76,12 @@ endif if KERNEL_32 MBOX_OFFSET := 0x7ff8 -KERNEL_OFFSET := 0x8000 TEXT_LIMIT := 0x3000 +KERNEL_OFFSET := 0x8000 else MBOX_OFFSET := 0xfff8 -KERNEL_OFFSET := 0x80000 TEXT_LIMIT := 0x80000 +KERNEL_OFFSET := $(shell perl -I $(SCRIPT_DIR) $(SCRIPT_DIR)/aa64-load-offset.pl $(KERNEL_IMAGE) $(TEXT_LIMIT)) endif LD_SCRIPT := model.lds.S diff --git a/scripts/AA64Image.pm b/scripts/AA64Image.pm new file mode 100755 index 0000000..36b2547 --- /dev/null +++ b/scripts/AA64Image.pm @@ -0,0 +1,87 @@ +#!/usr/bin/perl -w +# A simple Flattened Device Tree Blob (FDT/DTB) parser. +# +# Copyright (C) 2014 ARM Limited. All rights reserved. +# +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE.txt file. + +use warnings; +use strict; +use integer; + +package AA64Image; + +# Header definitions from v5.13 +# See https://www.kernel.org/doc/html/v5.13/arm64/booting.html#call-the-kernel-image + +use constant { + HEADER_LEN => 64, + HEADER_MAGIC => 0x644d5241, +}; + +sub parse +{ + my $class = shift; + my $fh = shift; + my $self = bless {}, $class; + + read($fh, my $raw_header, AA64Image::HEADER_LEN) == AA64Image::HEADER_LEN or goto failed; + + ( + $self->{code0}, + $self->{code1}, + $self->{text_offset}, + $self->{image_size}, + $self->{flags}, + $self->{res2}, + $self->{res3}, + $self->{res4}, + $self->{magic}, + $self->{res5} + ) = unpack("VVQ{magic} != AA64Image::HEADER_MAGIC) { + warn "Image header magic not found"; + goto failed; + } + + return $self; + +failed: + warn "Unable to parse header"; + return undef; +} + +sub get_text_offset +{ + my $self = shift; + + # Where image_size is 0, the load offset can be assumed to be 0x80000 + # See https://www.kernel.org/doc/html/v5.13/arm64/booting.html#call-the-kernel-image + if ($self->{image_size} == 0) { + return 0x80000; + } + + return $self->{text_offset}; +} + +sub get_load_offset +{ + my $self = shift; + my $min = shift; + my $offset = $self->get_text_offset(); + + if ($min < $offset) { + return $offset; + } + + # The image must be placed text_offset bytes from a 2MB aligned base address + my $size_2m = 2 * 1024 * 1024; + $min += $size_2m - 1; + $min &= ~($size_2m - 1); + + return $min + $offset; +} + +1; diff --git a/scripts/aa64-load-offset.pl b/scripts/aa64-load-offset.pl new file mode 100755 index 0000000..664b750 --- /dev/null +++ b/scripts/aa64-load-offset.pl @@ -0,0 +1,28 @@ +#!/usr/bin/perl -w +# Find the load address of an AArch64 Linux Image +# +# Usage: ./$0 +# +# Copyright (C) 2021 ARM Limited. All rights reserved. +# +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE.txt file. + +use warnings; +use strict; + +use AA64Image; + +my $filename = shift; +die("No filename provided") unless defined($filename); + +my $min = shift; +$min = oct($min) if $min =~ /^0/; + +open (my $fh, "<:raw", $filename) or die("Unable to open file '$filename'"); + +my $image = AA64Image->parse($fh) or die("Unable to parse Image"); + +my $offset = $image->get_load_offset($min); + +printf("0x%016x\n", $offset); -- 2.11.0 _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel