1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * (C) Copyright 2016
4 * Xilinx, Inc.
5 *
6 * (C) Copyright 2016
7 * Toradex AG
8 *
9 * Michal Simek <michal.simek@amd.com>
10 * Stefan Agner <stefan.agner@toradex.com>
11 */
12 #include <binman_sym.h>
13 #include <image.h>
14 #include <log.h>
15 #include <mapmem.h>
16 #include <spl.h>
17 #include <linux/libfdt.h>
18
spl_ram_load_read(struct spl_load_info * load,ulong sector,ulong count,void * buf)19 static ulong spl_ram_load_read(struct spl_load_info *load, ulong sector,
20 ulong count, void *buf)
21 {
22 ulong addr = 0;
23
24 debug("%s: sector %lx, count %lx, buf %lx\n",
25 __func__, sector, count, (ulong)buf);
26
27 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT)) {
28 addr = IF_ENABLED_INT(CONFIG_SPL_LOAD_FIT,
29 CONFIG_SPL_LOAD_FIT_ADDRESS);
30 }
31 addr += sector;
32 if (CONFIG_IS_ENABLED(IMAGE_PRE_LOAD))
33 addr += image_load_offset;
34
35 memcpy(buf, (void *)addr, count);
36
37 return count;
38 }
39
spl_ram_load_image(struct spl_image_info * spl_image,struct spl_boot_device * bootdev)40 static int spl_ram_load_image(struct spl_image_info *spl_image,
41 struct spl_boot_device *bootdev)
42 {
43 struct legacy_img_hdr *header;
44 ulong addr = 0;
45 int ret;
46
47 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT)) {
48 addr = IF_ENABLED_INT(CONFIG_SPL_LOAD_FIT,
49 CONFIG_SPL_LOAD_FIT_ADDRESS);
50 }
51
52 if (CONFIG_IS_ENABLED(IMAGE_PRE_LOAD)) {
53 ret = image_pre_load(addr);
54
55 if (ret)
56 return ret;
57
58 addr += image_load_offset;
59 }
60 header = map_sysmem(addr, 0);
61
62 #if CONFIG_IS_ENABLED(DFU)
63 if (bootdev->boot_device == BOOT_DEVICE_DFU)
64 spl_dfu_cmd(0, "dfu_alt_info_ram", "ram", "0");
65 #endif
66
67 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
68 image_get_magic(header) == FDT_MAGIC) {
69 struct spl_load_info load;
70
71 debug("Found FIT\n");
72 spl_load_init(&load, spl_ram_load_read, NULL, 1);
73 ret = spl_load_simple_fit(spl_image, &load, 0, header);
74 } else {
75 ulong u_boot_pos = spl_get_image_pos();
76
77 debug("Legacy image\n");
78 /*
79 * Get the header. It will point to an address defined by
80 * handoff which will tell where the image located inside
81 * the flash.
82 */
83 debug("u_boot_pos = %lx\n", u_boot_pos);
84 if (u_boot_pos == BINMAN_SYM_MISSING) {
85 /*
86 * No binman support or no information. For now, fix it
87 * to the address pointed to by U-Boot.
88 */
89 u_boot_pos = (ulong)spl_get_load_buffer(-sizeof(*header),
90 sizeof(*header));
91 }
92 header = map_sysmem(u_boot_pos, 0);
93
94 ret = spl_parse_image_header(spl_image, bootdev, header);
95 }
96
97 return ret;
98 }
99 #if CONFIG_IS_ENABLED(RAM_DEVICE)
100 SPL_LOAD_IMAGE_METHOD("RAM", 0, BOOT_DEVICE_RAM, spl_ram_load_image);
101 #endif
102 #if CONFIG_IS_ENABLED(DFU)
103 SPL_LOAD_IMAGE_METHOD("DFU", 0, BOOT_DEVICE_DFU, spl_ram_load_image);
104 #endif
105