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>
10 #include <image.h>
11 #include <mapmem.h>
12 #include <errno.h>
13 #include <asm/global_data.h>
14 #include <linux/sizes.h>
15 #include <linux/stddef.h>
16 
17 DECLARE_GLOBAL_DATA_PTR;
18 
19 /* ASCII version of "RSC\0x5" defined in Linux kernel */
20 #define LINUX_RISCV_IMAGE_MAGIC 0x05435352
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 */
27 	uint64_t	flags;		/* kernel flags (little endian) */
28 	uint32_t	version;	/* version of the header */
29 	uint32_t	res1;		/* reserved */
30 	uint64_t	res2;		/* reserved */
31 	uint64_t	res3;		/* reserved */
32 	uint32_t	magic;		/* Magic number */
33 	uint32_t	res4;		/* reserved */
34 };
35 
booti_setup(ulong image,ulong * relocated_addr,ulong * size,bool force_reloc)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;
53 	*relocated_addr = gd->ram_base + lhdr->text_offset;
54 
55 	unmap_sysmem(lhdr);
56 
57 	return 0;
58 }
59