Merge tag 'rpi-next-2019.10' of https://github.com/mbgg/u-boot
[oweals/u-boot.git] / arch / riscv / lib / image.c
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 <mapmem.h>
11 #include <errno.h>
12 #include <linux/sizes.h>
13 #include <linux/stddef.h>
14
15 DECLARE_GLOBAL_DATA_PTR;
16
17 /* ASCII version of "RISCV" defined in Linux kernel */
18 #define LINUX_RISCV_IMAGE_MAGIC 0x5643534952
19
20 struct 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 */
25         uint64_t        res1;           /* reserved */
26         uint64_t        res2;           /* reserved */
27         uint64_t        res3;           /* reserved */
28         uint64_t        magic;          /* Magic number */
29         uint32_t        res4;           /* reserved */
30         uint32_t        res5;           /* reserved */
31 };
32
33 int booti_setup(ulong image, ulong *relocated_addr, ulong *size,
34                 bool force_reloc)
35 {
36         struct linux_image_h *lhdr;
37
38         lhdr = (struct linux_image_h *)map_sysmem(image, 0);
39
40         if (lhdr->magic != LINUX_RISCV_IMAGE_MAGIC) {
41                 puts("Bad Linux RISCV Image magic!\n");
42                 return -EINVAL;
43         }
44
45         if (lhdr->image_size == 0) {
46                 puts("Image lacks image_size field, error!\n");
47                 return -EINVAL;
48         }
49         *size = lhdr->image_size;
50         *relocated_addr = gd->ram_base + lhdr->text_offset;
51
52         unmap_sysmem(lhdr);
53
54         return 0;
55 }