Atish Patra | 3cedc97 | 2019-05-06 17:49:39 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2019 Western Digital Corporation or its affiliates. |
| 4 | * Authors: |
| 5 | * Atish Patra <atish.patra@wdc.com> |
| 6 | * Based on arm/lib/image.c |
| 7 | */ |
| 8 | |
| 9 | #include <common.h> |
Simon Glass | 4d72caa | 2020-05-10 11:40:01 -0600 | [diff] [blame] | 10 | #include <image.h> |
Atish Patra | 3cedc97 | 2019-05-06 17:49:39 -0700 | [diff] [blame] | 11 | #include <mapmem.h> |
| 12 | #include <errno.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 13 | #include <asm/global_data.h> |
Atish Patra | 3cedc97 | 2019-05-06 17:49:39 -0700 | [diff] [blame] | 14 | #include <linux/sizes.h> |
| 15 | #include <linux/stddef.h> |
| 16 | |
| 17 | DECLARE_GLOBAL_DATA_PTR; |
| 18 | |
Atish Patra | 70d64a4 | 2019-10-09 10:34:17 -0700 | [diff] [blame] | 19 | /* ASCII version of "RSC\0x5" defined in Linux kernel */ |
| 20 | #define LINUX_RISCV_IMAGE_MAGIC 0x05435352 |
Atish Patra | 3cedc97 | 2019-05-06 17:49:39 -0700 | [diff] [blame] | 21 | |
| 22 | struct linux_image_h { |
| 23 | uint32_t code0; /* Executable code */ |
| 24 | uint32_t code1; /* Executable code */ |
| 25 | uint64_t text_offset; /* Image load offset */ |
| 26 | uint64_t image_size; /* Effective Image size */ |
Atish Patra | 70d64a4 | 2019-10-09 10:34:17 -0700 | [diff] [blame] | 27 | uint64_t flags; /* kernel flags (little endian) */ |
| 28 | uint32_t version; /* version of the header */ |
| 29 | uint32_t res1; /* reserved */ |
Atish Patra | 3cedc97 | 2019-05-06 17:49:39 -0700 | [diff] [blame] | 30 | uint64_t res2; /* reserved */ |
| 31 | uint64_t res3; /* reserved */ |
Atish Patra | 70d64a4 | 2019-10-09 10:34:17 -0700 | [diff] [blame] | 32 | uint32_t magic; /* Magic number */ |
Atish Patra | 3cedc97 | 2019-05-06 17:49:39 -0700 | [diff] [blame] | 33 | uint32_t res4; /* reserved */ |
Atish Patra | 3cedc97 | 2019-05-06 17:49:39 -0700 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | int booti_setup(ulong image, ulong *relocated_addr, ulong *size, |
| 37 | bool force_reloc) |
| 38 | { |
| 39 | struct linux_image_h *lhdr; |
| 40 | |
| 41 | lhdr = (struct linux_image_h *)map_sysmem(image, 0); |
| 42 | |
| 43 | if (lhdr->magic != LINUX_RISCV_IMAGE_MAGIC) { |
| 44 | puts("Bad Linux RISCV Image magic!\n"); |
| 45 | return -EINVAL; |
| 46 | } |
| 47 | |
| 48 | if (lhdr->image_size == 0) { |
| 49 | puts("Image lacks image_size field, error!\n"); |
| 50 | return -EINVAL; |
| 51 | } |
| 52 | *size = lhdr->image_size; |
Vitaly Wool | 6ba8eeb | 2021-04-06 10:50:16 +0300 | [diff] [blame] | 53 | if (force_reloc || |
| 54 | (gd->ram_base <= image && image < gd->ram_base + gd->ram_size)) { |
| 55 | *relocated_addr = gd->ram_base + lhdr->text_offset; |
| 56 | } else { |
| 57 | *relocated_addr = image; |
| 58 | } |
Atish Patra | 3cedc97 | 2019-05-06 17:49:39 -0700 | [diff] [blame] | 59 | |
| 60 | unmap_sysmem(lhdr); |
| 61 | |
| 62 | return 0; |
| 63 | } |