Merge tag 'u-boot-atmel-fixes-2020.07-a' of https://gitlab.denx.de/u-boot/custodians...
[oweals/u-boot.git] / arch / arm / lib / image.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2000-2009
4  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5  */
6
7 #include <common.h>
8 #include <image.h>
9 #include <mapmem.h>
10 #include <linux/bitops.h>
11 #include <linux/sizes.h>
12
13 DECLARE_GLOBAL_DATA_PTR;
14
15 #define LINUX_ARM64_IMAGE_MAGIC 0x644d5241
16
17 /* See Documentation/arm64/booting.txt in the Linux kernel */
18 struct Image_header {
19         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;
29 };
30
31 int booti_setup(ulong image, ulong *relocated_addr, ulong *size,
32                 bool force_reloc)
33 {
34         struct Image_header *ih;
35         uint64_t dst;
36         uint64_t image_size, text_offset;
37
38         *relocated_addr = image;
39
40         ih = (struct Image_header *)map_sysmem(image, 0);
41
42         if (ih->magic != le32_to_cpu(LINUX_ARM64_IMAGE_MAGIC)) {
43                 puts("Bad Linux ARM64 Image magic!\n");
44                 return 1;
45         }
46
47         /*
48          * Prior to Linux commit a2c1d73b94ed, the text_offset field
49          * is of unknown endianness.  In these cases, the image_size
50          * field is zero, and we can assume a fixed value of 0x80000.
51          */
52         if (ih->image_size == 0) {
53                 puts("Image lacks image_size field, assuming 16MiB\n");
54                 image_size = 16 << 20;
55                 text_offset = 0x80000;
56         } else {
57                 image_size = le64_to_cpu(ih->image_size);
58                 text_offset = le64_to_cpu(ih->text_offset);
59         }
60
61         *size = image_size;
62
63         /*
64          * If bit 3 of the flags field is set, the 2MB aligned base of the
65          * kernel image can be anywhere in physical memory, so respect
66          * images->ep.  Otherwise, relocate the image to the base of RAM
67          * since memory below it is not accessible via the linear mapping.
68          */
69         if (!force_reloc && (le64_to_cpu(ih->flags) & BIT(3)))
70                 dst = image - text_offset;
71         else
72                 dst = gd->bd->bi_dram[0].start;
73
74         *relocated_addr = ALIGN(dst, SZ_2M) + text_offset;
75
76         unmap_sysmem(ih);
77
78         return 0;
79 }