Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Bin Chen | 6808ef9 | 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 | 6808ef9 | 2018-01-27 16:59:09 +1100 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <mapmem.h> |
| 9 | #include <linux/sizes.h> |
| 10 | |
| 11 | DECLARE_GLOBAL_DATA_PTR; |
| 12 | |
| 13 | #define LINUX_ARM64_IMAGE_MAGIC 0x644d5241 |
| 14 | |
| 15 | /* See Documentation/arm64/booting.txt in the Linux kernel */ |
| 16 | struct Image_header { |
Masahiro Yamada | 6becd9d | 2018-02-13 11:32:15 +0900 | [diff] [blame] | 17 | uint32_t code0; /* Executable code */ |
| 18 | uint32_t code1; /* Executable code */ |
| 19 | uint64_t text_offset; /* Image load offset, LE */ |
| 20 | uint64_t image_size; /* Effective Image size, LE */ |
| 21 | uint64_t flags; /* Kernel flags, LE */ |
| 22 | uint64_t res2; /* reserved */ |
| 23 | uint64_t res3; /* reserved */ |
| 24 | uint64_t res4; /* reserved */ |
| 25 | uint32_t magic; /* Magic number */ |
| 26 | uint32_t res5; |
Bin Chen | 6808ef9 | 2018-01-27 16:59:09 +1100 | [diff] [blame] | 27 | }; |
| 28 | |
| 29 | int booti_setup(ulong image, ulong *relocated_addr, ulong *size) |
| 30 | { |
Masahiro Yamada | 6becd9d | 2018-02-13 11:32:15 +0900 | [diff] [blame] | 31 | struct Image_header *ih; |
| 32 | uint64_t dst; |
| 33 | uint64_t image_size, text_offset; |
Bin Chen | 6808ef9 | 2018-01-27 16:59:09 +1100 | [diff] [blame] | 34 | |
Masahiro Yamada | 6becd9d | 2018-02-13 11:32:15 +0900 | [diff] [blame] | 35 | *relocated_addr = image; |
Bin Chen | 6808ef9 | 2018-01-27 16:59:09 +1100 | [diff] [blame] | 36 | |
Masahiro Yamada | 6becd9d | 2018-02-13 11:32:15 +0900 | [diff] [blame] | 37 | ih = (struct Image_header *)map_sysmem(image, 0); |
Bin Chen | 6808ef9 | 2018-01-27 16:59:09 +1100 | [diff] [blame] | 38 | |
Masahiro Yamada | 6becd9d | 2018-02-13 11:32:15 +0900 | [diff] [blame] | 39 | if (ih->magic != le32_to_cpu(LINUX_ARM64_IMAGE_MAGIC)) { |
| 40 | puts("Bad Linux ARM64 Image magic!\n"); |
| 41 | return 1; |
| 42 | } |
Bin Chen | 6808ef9 | 2018-01-27 16:59:09 +1100 | [diff] [blame] | 43 | |
Masahiro Yamada | 6becd9d | 2018-02-13 11:32:15 +0900 | [diff] [blame] | 44 | /* |
| 45 | * Prior to Linux commit a2c1d73b94ed, the text_offset field |
| 46 | * is of unknown endianness. In these cases, the image_size |
| 47 | * field is zero, and we can assume a fixed value of 0x80000. |
| 48 | */ |
| 49 | if (ih->image_size == 0) { |
| 50 | puts("Image lacks image_size field, assuming 16MiB\n"); |
| 51 | image_size = 16 << 20; |
| 52 | text_offset = 0x80000; |
| 53 | } else { |
| 54 | image_size = le64_to_cpu(ih->image_size); |
| 55 | text_offset = le64_to_cpu(ih->text_offset); |
| 56 | } |
Bin Chen | 6808ef9 | 2018-01-27 16:59:09 +1100 | [diff] [blame] | 57 | |
Masahiro Yamada | 6becd9d | 2018-02-13 11:32:15 +0900 | [diff] [blame] | 58 | *size = image_size; |
Bin Chen | 6808ef9 | 2018-01-27 16:59:09 +1100 | [diff] [blame] | 59 | |
Masahiro Yamada | 6becd9d | 2018-02-13 11:32:15 +0900 | [diff] [blame] | 60 | /* |
| 61 | * If bit 3 of the flags field is set, the 2MB aligned base of the |
| 62 | * kernel image can be anywhere in physical memory, so respect |
| 63 | * images->ep. Otherwise, relocate the image to the base of RAM |
| 64 | * since memory below it is not accessible via the linear mapping. |
| 65 | */ |
| 66 | if (le64_to_cpu(ih->flags) & BIT(3)) |
| 67 | dst = image - text_offset; |
| 68 | else |
| 69 | dst = gd->bd->bi_dram[0].start; |
Bin Chen | 6808ef9 | 2018-01-27 16:59:09 +1100 | [diff] [blame] | 70 | |
Masahiro Yamada | 6becd9d | 2018-02-13 11:32:15 +0900 | [diff] [blame] | 71 | *relocated_addr = ALIGN(dst, SZ_2M) + text_offset; |
Bin Chen | 6808ef9 | 2018-01-27 16:59:09 +1100 | [diff] [blame] | 72 | |
Masahiro Yamada | 6becd9d | 2018-02-13 11:32:15 +0900 | [diff] [blame] | 73 | unmap_sysmem(ih); |
Bin Chen | 6808ef9 | 2018-01-27 16:59:09 +1100 | [diff] [blame] | 74 | |
Masahiro Yamada | 6becd9d | 2018-02-13 11:32:15 +0900 | [diff] [blame] | 75 | return 0; |
Bin Chen | 6808ef9 | 2018-01-27 16:59:09 +1100 | [diff] [blame] | 76 | } |