linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: Mark Rutland <mark.rutland@arm.com>
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	[thread overview]
Message-ID: <20210729152050.23635-8-mark.rutland@arm.com> (raw)
In-Reply-To: <20210729152050.23635-1-mark.rutland@arm.com>

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 <mark.rutland@arm.com>
---
 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<Q<Q<Q<Q<Q<VV", $raw_header);
+
+	if ($self->{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 <Image> <min-offset>
+#
+# 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

  parent reply	other threads:[~2021-07-29 15:34 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-29 15:20 [boot-wrapper PATCH 00/12] Preparatory fixes and cleanup Mark Rutland
2021-07-29 15:20 ` [boot-wrapper PATCH 01/12] Ensure `kernel_address` is aligned Mark Rutland
2021-07-30 15:11   ` Andre Przywara
2021-07-29 15:20 ` [boot-wrapper PATCH 02/12] Output text separately from data Mark Rutland
2021-07-29 15:20 ` [boot-wrapper PATCH 03/12] Remove cache maintenance Mark Rutland
2021-07-30 15:12   ` Andre Przywara
2021-07-30 15:43     ` Mark Rutland
2021-07-29 15:20 ` [boot-wrapper PATCH 04/12] Remove `flag_no_el3` Mark Rutland
2021-07-30 15:13   ` Andre Przywara
2021-07-30 16:43     ` Mark Rutland
2021-08-02 14:43       ` Mark Rutland
2021-07-29 15:20 ` [boot-wrapper PATCH 05/12] Move PSCI triage to C Mark Rutland
2021-07-29 15:20 ` [boot-wrapper PATCH 06/12] Move scripts to a `scripts` directory Mark Rutland
2021-07-30 15:13   ` Andre Przywara
2021-07-29 15:20 ` Mark Rutland [this message]
2021-07-30 15:13   ` [boot-wrapper PATCH 07/12] aarch64: respect text offset Andre Przywara
2021-07-30 15:43     ` Mark Rutland
2021-07-29 15:20 ` [boot-wrapper PATCH 08/12] Consistently use logical CPU IDs Mark Rutland
2021-07-30 17:38   ` Andre Przywara
2021-07-29 15:20 ` [boot-wrapper PATCH 09/12] Cleanup `.globl` usage Mark Rutland
2021-07-30 17:39   ` Andre Przywara
2021-07-29 15:20 ` [boot-wrapper PATCH 10/12] aarch32: rename `_spin_dead` -> `err_invalid_id` Mark Rutland
2021-07-30 17:39   ` Andre Przywara
2021-07-29 15:20 ` [boot-wrapper PATCH 11/12] Rename `spin.h` -> `boot.h` Mark Rutland
2021-07-30 17:39   ` Andre Przywara
2021-07-29 15:20 ` [boot-wrapper PATCH 12/12] Move common source files to `common` directory Mark Rutland
2021-07-30 17:40   ` Andre Przywara

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210729152050.23635-8-mark.rutland@arm.com \
    --to=mark.rutland@arm.com \
    --cc=Andre.Przywara@arm.com \
    --cc=Jaxson.Han@arm.com \
    --cc=Wei.Chen@arm.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).