blob: 92bfb8fb662475b2aca60eb081d1d34cad896d0e [file] [log] [blame]
Bin Chen6808ef92018-01-27 16:59:09 +11001/*
2 * (C) Copyright 2000-2009
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#include <common.h>
9#include <mapmem.h>
10#include <linux/sizes.h>
11
12DECLARE_GLOBAL_DATA_PTR;
13
14#define LINUX_ARM64_IMAGE_MAGIC 0x644d5241
15
16/* See Documentation/arm64/booting.txt in the Linux kernel */
17struct Image_header {
Masahiro Yamada6becd9d2018-02-13 11:32:15 +090018 uint32_t code0; /* Executable code */
19 uint32_t code1; /* Executable code */
20 uint64_t text_offset; /* Image load offset, LE */
21 uint64_t image_size; /* Effective Image size, LE */
22 uint64_t flags; /* Kernel flags, LE */
23 uint64_t res2; /* reserved */
24 uint64_t res3; /* reserved */
25 uint64_t res4; /* reserved */
26 uint32_t magic; /* Magic number */
27 uint32_t res5;
Bin Chen6808ef92018-01-27 16:59:09 +110028};
29
30int booti_setup(ulong image, ulong *relocated_addr, ulong *size)
31{
Masahiro Yamada6becd9d2018-02-13 11:32:15 +090032 struct Image_header *ih;
33 uint64_t dst;
34 uint64_t image_size, text_offset;
Bin Chen6808ef92018-01-27 16:59:09 +110035
Masahiro Yamada6becd9d2018-02-13 11:32:15 +090036 *relocated_addr = image;
Bin Chen6808ef92018-01-27 16:59:09 +110037
Masahiro Yamada6becd9d2018-02-13 11:32:15 +090038 ih = (struct Image_header *)map_sysmem(image, 0);
Bin Chen6808ef92018-01-27 16:59:09 +110039
Masahiro Yamada6becd9d2018-02-13 11:32:15 +090040 if (ih->magic != le32_to_cpu(LINUX_ARM64_IMAGE_MAGIC)) {
41 puts("Bad Linux ARM64 Image magic!\n");
42 return 1;
43 }
Bin Chen6808ef92018-01-27 16:59:09 +110044
Masahiro Yamada6becd9d2018-02-13 11:32:15 +090045 /*
46 * Prior to Linux commit a2c1d73b94ed, the text_offset field
47 * is of unknown endianness. In these cases, the image_size
48 * field is zero, and we can assume a fixed value of 0x80000.
49 */
50 if (ih->image_size == 0) {
51 puts("Image lacks image_size field, assuming 16MiB\n");
52 image_size = 16 << 20;
53 text_offset = 0x80000;
54 } else {
55 image_size = le64_to_cpu(ih->image_size);
56 text_offset = le64_to_cpu(ih->text_offset);
57 }
Bin Chen6808ef92018-01-27 16:59:09 +110058
Masahiro Yamada6becd9d2018-02-13 11:32:15 +090059 *size = image_size;
Bin Chen6808ef92018-01-27 16:59:09 +110060
Masahiro Yamada6becd9d2018-02-13 11:32:15 +090061 /*
62 * If bit 3 of the flags field is set, the 2MB aligned base of the
63 * kernel image can be anywhere in physical memory, so respect
64 * images->ep. Otherwise, relocate the image to the base of RAM
65 * since memory below it is not accessible via the linear mapping.
66 */
67 if (le64_to_cpu(ih->flags) & BIT(3))
68 dst = image - text_offset;
69 else
70 dst = gd->bd->bi_dram[0].start;
Bin Chen6808ef92018-01-27 16:59:09 +110071
Masahiro Yamada6becd9d2018-02-13 11:32:15 +090072 *relocated_addr = ALIGN(dst, SZ_2M) + text_offset;
Bin Chen6808ef92018-01-27 16:59:09 +110073
Masahiro Yamada6becd9d2018-02-13 11:32:15 +090074 unmap_sysmem(ih);
Bin Chen6808ef92018-01-27 16:59:09 +110075
Masahiro Yamada6becd9d2018-02-13 11:32:15 +090076 return 0;
Bin Chen6808ef92018-01-27 16:59:09 +110077}