blob: d78d704cb58558268d0486a0907d005ca0c390d6 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Bin Chendac184b2018-01-27 16:59:09 +11002/*
3 * (C) Copyright 2000-2009
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Bin Chendac184b2018-01-27 16:59:09 +11005 */
6
Simon Glass2dc9c342020-05-10 11:40:01 -06007#include <image.h>
Bin Chendac184b2018-01-27 16:59:09 +11008#include <mapmem.h>
Simon Glass3ba929a2020-10-30 21:38:53 -06009#include <asm/global_data.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060010#include <linux/bitops.h>
Bin Chendac184b2018-01-27 16:59:09 +110011#include <linux/sizes.h>
12
13DECLARE_GLOBAL_DATA_PTR;
14
15#define LINUX_ARM64_IMAGE_MAGIC 0x644d5241
16
17/* See Documentation/arm64/booting.txt in the Linux kernel */
18struct Image_header {
Masahiro Yamada764acb02018-02-13 11:32:15 +090019 uint32_t code0; /* Executable code */
20 uint32_t code1; /* Executable code */
21 uint64_t text_offset; /* Image load offset, LE */
22 uint64_t image_size; /* Effective Image size, LE */
23 uint64_t flags; /* Kernel flags, LE */
24 uint64_t res2; /* reserved */
25 uint64_t res3; /* reserved */
26 uint64_t res4; /* reserved */
27 uint32_t magic; /* Magic number */
28 uint32_t res5;
Bin Chendac184b2018-01-27 16:59:09 +110029};
30
Simon Glass5047fcc2025-03-05 17:25:09 -070031bool booti_is_valid(const void *img)
32{
33 const struct Image_header *ih = img;
34
35 return ih->magic == le32_to_cpu(LINUX_ARM64_IMAGE_MAGIC);
36}
37
Marek Vasut85ed2892018-06-13 06:13:32 +020038int booti_setup(ulong image, ulong *relocated_addr, ulong *size,
39 bool force_reloc)
Bin Chendac184b2018-01-27 16:59:09 +110040{
Masahiro Yamada764acb02018-02-13 11:32:15 +090041 struct Image_header *ih;
42 uint64_t dst;
43 uint64_t image_size, text_offset;
Bin Chendac184b2018-01-27 16:59:09 +110044
Masahiro Yamada764acb02018-02-13 11:32:15 +090045 *relocated_addr = image;
Bin Chendac184b2018-01-27 16:59:09 +110046
Masahiro Yamada764acb02018-02-13 11:32:15 +090047 ih = (struct Image_header *)map_sysmem(image, 0);
Bin Chendac184b2018-01-27 16:59:09 +110048
Simon Glass5047fcc2025-03-05 17:25:09 -070049 if (!booti_is_valid(ih)) {
Masahiro Yamada764acb02018-02-13 11:32:15 +090050 puts("Bad Linux ARM64 Image magic!\n");
51 return 1;
52 }
Bin Chendac184b2018-01-27 16:59:09 +110053
Masahiro Yamada764acb02018-02-13 11:32:15 +090054 /*
55 * Prior to Linux commit a2c1d73b94ed, the text_offset field
56 * is of unknown endianness. In these cases, the image_size
57 * field is zero, and we can assume a fixed value of 0x80000.
58 */
59 if (ih->image_size == 0) {
60 puts("Image lacks image_size field, assuming 16MiB\n");
61 image_size = 16 << 20;
62 text_offset = 0x80000;
63 } else {
64 image_size = le64_to_cpu(ih->image_size);
65 text_offset = le64_to_cpu(ih->text_offset);
66 }
Bin Chendac184b2018-01-27 16:59:09 +110067
Masahiro Yamada764acb02018-02-13 11:32:15 +090068 *size = image_size;
Bin Chendac184b2018-01-27 16:59:09 +110069
Masahiro Yamada764acb02018-02-13 11:32:15 +090070 /*
71 * If bit 3 of the flags field is set, the 2MB aligned base of the
72 * kernel image can be anywhere in physical memory, so respect
73 * images->ep. Otherwise, relocate the image to the base of RAM
74 * since memory below it is not accessible via the linear mapping.
75 */
Marek Vasut85ed2892018-06-13 06:13:32 +020076 if (!force_reloc && (le64_to_cpu(ih->flags) & BIT(3)))
Masahiro Yamada764acb02018-02-13 11:32:15 +090077 dst = image - text_offset;
78 else
79 dst = gd->bd->bi_dram[0].start;
Bin Chendac184b2018-01-27 16:59:09 +110080
Masahiro Yamada764acb02018-02-13 11:32:15 +090081 *relocated_addr = ALIGN(dst, SZ_2M) + text_offset;
Bin Chendac184b2018-01-27 16:59:09 +110082
Masahiro Yamada764acb02018-02-13 11:32:15 +090083 unmap_sysmem(ih);
Bin Chendac184b2018-01-27 16:59:09 +110084
Masahiro Yamada764acb02018-02-13 11:32:15 +090085 return 0;
Bin Chendac184b2018-01-27 16:59:09 +110086}