diff options
author | Mark Rutland <mark.rutland@arm.com> | 2021-07-26 14:10:38 +0100 |
---|---|---|
committer | Mark Rutland <mark.rutland@arm.com> | 2021-08-02 15:36:52 +0100 |
commit | 4a50f69c550473b989f0d38f096d4d9a3a6804c7 (patch) | |
tree | 331525a04ae36409fb33474e38dd247658ea5c72 | |
parent | 9a730aac690fe329dddbb401d66e7c41d57889c2 (diff) | |
download | boot-wrapper-aarch64-4a50f69c550473b989f0d38f096d4d9a3a6804c7.tar.gz |
aarch64: respect text offset
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>
-rw-r--r-- | Makefile.am | 4 | ||||
-rwxr-xr-x | scripts/AA64Image.pm | 87 | ||||
-rwxr-xr-x | scripts/aa64-load-offset.pl | 28 |
3 files changed, 117 insertions, 2 deletions
diff --git a/Makefile.am b/Makefile.am index 30804fa..bb6079d 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..8c441ec --- /dev/null +++ b/scripts/AA64Image.pm @@ -0,0 +1,87 @@ +#!/usr/bin/perl -w +# A simple AArch64 Linux Image header parser +# +# 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 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); |