Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Bin Chen | dac184b | 2018-01-27 16:59:09 +1100 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2000-2009 |
| 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
Bin Chen | dac184b | 2018-01-27 16:59:09 +1100 | [diff] [blame] | 5 | */ |
| 6 | |
Simon Glass | 2dc9c34 | 2020-05-10 11:40:01 -0600 | [diff] [blame] | 7 | #include <image.h> |
Bin Chen | dac184b | 2018-01-27 16:59:09 +1100 | [diff] [blame] | 8 | #include <mapmem.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 9 | #include <asm/global_data.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 10 | #include <linux/bitops.h> |
Bin Chen | dac184b | 2018-01-27 16:59:09 +1100 | [diff] [blame] | 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 { |
Masahiro Yamada | 764acb0 | 2018-02-13 11:32:15 +0900 | [diff] [blame] | 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; |
Bin Chen | dac184b | 2018-01-27 16:59:09 +1100 | [diff] [blame] | 29 | }; |
| 30 | |
Marek Vasut | 85ed289 | 2018-06-13 06:13:32 +0200 | [diff] [blame] | 31 | int booti_setup(ulong image, ulong *relocated_addr, ulong *size, |
| 32 | bool force_reloc) |
Bin Chen | dac184b | 2018-01-27 16:59:09 +1100 | [diff] [blame] | 33 | { |
Masahiro Yamada | 764acb0 | 2018-02-13 11:32:15 +0900 | [diff] [blame] | 34 | struct Image_header *ih; |
| 35 | uint64_t dst; |
| 36 | uint64_t image_size, text_offset; |
Bin Chen | dac184b | 2018-01-27 16:59:09 +1100 | [diff] [blame] | 37 | |
Masahiro Yamada | 764acb0 | 2018-02-13 11:32:15 +0900 | [diff] [blame] | 38 | *relocated_addr = image; |
Bin Chen | dac184b | 2018-01-27 16:59:09 +1100 | [diff] [blame] | 39 | |
Masahiro Yamada | 764acb0 | 2018-02-13 11:32:15 +0900 | [diff] [blame] | 40 | ih = (struct Image_header *)map_sysmem(image, 0); |
Bin Chen | dac184b | 2018-01-27 16:59:09 +1100 | [diff] [blame] | 41 | |
Masahiro Yamada | 764acb0 | 2018-02-13 11:32:15 +0900 | [diff] [blame] | 42 | if (ih->magic != le32_to_cpu(LINUX_ARM64_IMAGE_MAGIC)) { |
| 43 | puts("Bad Linux ARM64 Image magic!\n"); |
| 44 | return 1; |
| 45 | } |
Bin Chen | dac184b | 2018-01-27 16:59:09 +1100 | [diff] [blame] | 46 | |
Masahiro Yamada | 764acb0 | 2018-02-13 11:32:15 +0900 | [diff] [blame] | 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 | } |
Bin Chen | dac184b | 2018-01-27 16:59:09 +1100 | [diff] [blame] | 60 | |
Masahiro Yamada | 764acb0 | 2018-02-13 11:32:15 +0900 | [diff] [blame] | 61 | *size = image_size; |
Bin Chen | dac184b | 2018-01-27 16:59:09 +1100 | [diff] [blame] | 62 | |
Masahiro Yamada | 764acb0 | 2018-02-13 11:32:15 +0900 | [diff] [blame] | 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 | */ |
Marek Vasut | 85ed289 | 2018-06-13 06:13:32 +0200 | [diff] [blame] | 69 | if (!force_reloc && (le64_to_cpu(ih->flags) & BIT(3))) |
Masahiro Yamada | 764acb0 | 2018-02-13 11:32:15 +0900 | [diff] [blame] | 70 | dst = image - text_offset; |
| 71 | else |
| 72 | dst = gd->bd->bi_dram[0].start; |
Bin Chen | dac184b | 2018-01-27 16:59:09 +1100 | [diff] [blame] | 73 | |
Masahiro Yamada | 764acb0 | 2018-02-13 11:32:15 +0900 | [diff] [blame] | 74 | *relocated_addr = ALIGN(dst, SZ_2M) + text_offset; |
Bin Chen | dac184b | 2018-01-27 16:59:09 +1100 | [diff] [blame] | 75 | |
Masahiro Yamada | 764acb0 | 2018-02-13 11:32:15 +0900 | [diff] [blame] | 76 | unmap_sysmem(ih); |
Bin Chen | dac184b | 2018-01-27 16:59:09 +1100 | [diff] [blame] | 77 | |
Masahiro Yamada | 764acb0 | 2018-02-13 11:32:15 +0900 | [diff] [blame] | 78 | return 0; |
Bin Chen | dac184b | 2018-01-27 16:59:09 +1100 | [diff] [blame] | 79 | } |