blob: a82f48e9a5051e278cd389fbdfcabb0ca1cc6da6 [file] [log] [blame]
Atish Patra583b4092019-05-06 17:49:39 -07001// 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
Simon Glass2dc9c342020-05-10 11:40:01 -06009#include <image.h>
Atish Patra583b4092019-05-06 17:49:39 -070010#include <mapmem.h>
11#include <errno.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060012#include <asm/global_data.h>
Atish Patra583b4092019-05-06 17:49:39 -070013#include <linux/sizes.h>
14#include <linux/stddef.h>
15
16DECLARE_GLOBAL_DATA_PTR;
17
Atish Patrab8745f02019-10-09 10:34:17 -070018/* ASCII version of "RSC\0x5" defined in Linux kernel */
19#define LINUX_RISCV_IMAGE_MAGIC 0x05435352
Atish Patra583b4092019-05-06 17:49:39 -070020
21struct linux_image_h {
22 uint32_t code0; /* Executable code */
23 uint32_t code1; /* Executable code */
24 uint64_t text_offset; /* Image load offset */
25 uint64_t image_size; /* Effective Image size */
Atish Patrab8745f02019-10-09 10:34:17 -070026 uint64_t flags; /* kernel flags (little endian) */
27 uint32_t version; /* version of the header */
28 uint32_t res1; /* reserved */
Atish Patra583b4092019-05-06 17:49:39 -070029 uint64_t res2; /* reserved */
30 uint64_t res3; /* reserved */
Atish Patrab8745f02019-10-09 10:34:17 -070031 uint32_t magic; /* Magic number */
Atish Patra583b4092019-05-06 17:49:39 -070032 uint32_t res4; /* reserved */
Atish Patra583b4092019-05-06 17:49:39 -070033};
34
35int booti_setup(ulong image, ulong *relocated_addr, ulong *size,
36 bool force_reloc)
37{
38 struct linux_image_h *lhdr;
39
40 lhdr = (struct linux_image_h *)map_sysmem(image, 0);
41
42 if (lhdr->magic != LINUX_RISCV_IMAGE_MAGIC) {
43 puts("Bad Linux RISCV Image magic!\n");
44 return -EINVAL;
45 }
46
47 if (lhdr->image_size == 0) {
48 puts("Image lacks image_size field, error!\n");
49 return -EINVAL;
50 }
51 *size = lhdr->image_size;
Vitaly Wool51744fe2021-04-06 10:50:16 +030052 if (force_reloc ||
53 (gd->ram_base <= image && image < gd->ram_base + gd->ram_size)) {
54 *relocated_addr = gd->ram_base + lhdr->text_offset;
55 } else {
56 *relocated_addr = image;
57 }
Atish Patra583b4092019-05-06 17:49:39 -070058
59 unmap_sysmem(lhdr);
60
61 return 0;
62}