blob: 7357d3b07d6cd02c920d43660e87408d91b29482 [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
9#include <common.h>
10#include <mapmem.h>
11#include <errno.h>
12#include <linux/sizes.h>
13#include <linux/stddef.h>
14
15DECLARE_GLOBAL_DATA_PTR;
16
Atish Patrab8745f02019-10-09 10:34:17 -070017/* ASCII version of "RSC\0x5" defined in Linux kernel */
18#define LINUX_RISCV_IMAGE_MAGIC 0x05435352
Atish Patra583b4092019-05-06 17:49:39 -070019
20struct linux_image_h {
21 uint32_t code0; /* Executable code */
22 uint32_t code1; /* Executable code */
23 uint64_t text_offset; /* Image load offset */
24 uint64_t image_size; /* Effective Image size */
Atish Patrab8745f02019-10-09 10:34:17 -070025 uint64_t flags; /* kernel flags (little endian) */
26 uint32_t version; /* version of the header */
27 uint32_t res1; /* reserved */
Atish Patra583b4092019-05-06 17:49:39 -070028 uint64_t res2; /* reserved */
29 uint64_t res3; /* reserved */
Atish Patrab8745f02019-10-09 10:34:17 -070030 uint32_t magic; /* Magic number */
Atish Patra583b4092019-05-06 17:49:39 -070031 uint32_t res4; /* reserved */
Atish Patra583b4092019-05-06 17:49:39 -070032};
33
34int booti_setup(ulong image, ulong *relocated_addr, ulong *size,
35 bool force_reloc)
36{
37 struct linux_image_h *lhdr;
38
39 lhdr = (struct linux_image_h *)map_sysmem(image, 0);
40
41 if (lhdr->magic != LINUX_RISCV_IMAGE_MAGIC) {
42 puts("Bad Linux RISCV Image magic!\n");
43 return -EINVAL;
44 }
45
46 if (lhdr->image_size == 0) {
47 puts("Image lacks image_size field, error!\n");
48 return -EINVAL;
49 }
50 *size = lhdr->image_size;
51 *relocated_addr = gd->ram_base + lhdr->text_offset;
52
53 unmap_sysmem(lhdr);
54
55 return 0;
56}