1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * (C) Copyright 2016
3*4882a593Smuzhiyun * Xilinx, Inc.
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * (C) Copyright 2016
6*4882a593Smuzhiyun * Toradex AG
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * Michal Simek <michal.simek@xilinx.com>
9*4882a593Smuzhiyun * Stefan Agner <stefan.agner@toradex.com>
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0+
12*4882a593Smuzhiyun */
13*4882a593Smuzhiyun #include <common.h>
14*4882a593Smuzhiyun #include <spl.h>
15*4882a593Smuzhiyun #include <linux/libfdt.h>
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun #ifndef CONFIG_SPL_LOAD_FIT_ADDRESS
18*4882a593Smuzhiyun # define CONFIG_SPL_LOAD_FIT_ADDRESS 0
19*4882a593Smuzhiyun #endif
20*4882a593Smuzhiyun
spl_ram_load_read(struct spl_load_info * load,ulong sector,ulong count,void * buf)21*4882a593Smuzhiyun static ulong spl_ram_load_read(struct spl_load_info *load, ulong sector,
22*4882a593Smuzhiyun ulong count, void *buf)
23*4882a593Smuzhiyun {
24*4882a593Smuzhiyun debug("%s: sector %lx, count %lx, buf %lx\n",
25*4882a593Smuzhiyun __func__, sector, count, (ulong)buf);
26*4882a593Smuzhiyun memcpy(buf, (void *)(CONFIG_SPL_LOAD_FIT_ADDRESS + sector), count);
27*4882a593Smuzhiyun return count;
28*4882a593Smuzhiyun }
29*4882a593Smuzhiyun
spl_ram_load_image(struct spl_image_info * spl_image,struct spl_boot_device * bootdev)30*4882a593Smuzhiyun static int spl_ram_load_image(struct spl_image_info *spl_image,
31*4882a593Smuzhiyun struct spl_boot_device *bootdev)
32*4882a593Smuzhiyun {
33*4882a593Smuzhiyun struct image_header *header;
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun header = (struct image_header *)CONFIG_SPL_LOAD_FIT_ADDRESS;
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun #if CONFIG_IS_ENABLED(DFU)
38*4882a593Smuzhiyun if (bootdev->boot_device == BOOT_DEVICE_DFU)
39*4882a593Smuzhiyun spl_dfu_cmd(0, "dfu_alt_info_ram", "ram", "0");
40*4882a593Smuzhiyun #endif
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun #ifdef CONFIG_SPL_FIT_IMAGE_MULTIPLE
43*4882a593Smuzhiyun if ((IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
44*4882a593Smuzhiyun image_get_magic(header) == FDT_MAGIC) ||
45*4882a593Smuzhiyun CONFIG_SPL_FIT_IMAGE_MULTIPLE > 1) {
46*4882a593Smuzhiyun #else
47*4882a593Smuzhiyun if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
48*4882a593Smuzhiyun image_get_magic(header) == FDT_MAGIC) {
49*4882a593Smuzhiyun #endif
50*4882a593Smuzhiyun struct spl_load_info load;
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun debug("Found FIT\n");
53*4882a593Smuzhiyun load.bl_len = 1;
54*4882a593Smuzhiyun load.read = spl_ram_load_read;
55*4882a593Smuzhiyun spl_load_simple_fit(spl_image, &load, 0, header);
56*4882a593Smuzhiyun } else {
57*4882a593Smuzhiyun debug("Legacy image\n");
58*4882a593Smuzhiyun /*
59*4882a593Smuzhiyun * Get the header. It will point to an address defined by
60*4882a593Smuzhiyun * handoff which will tell where the image located inside
61*4882a593Smuzhiyun * the flash. For now, it will temporary fixed to address
62*4882a593Smuzhiyun * pointed by U-Boot.
63*4882a593Smuzhiyun */
64*4882a593Smuzhiyun header = (struct image_header *)
65*4882a593Smuzhiyun (CONFIG_SYS_TEXT_BASE - sizeof(struct image_header));
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun spl_parse_image_header(spl_image, header);
68*4882a593Smuzhiyun }
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun return 0;
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun #if defined(CONFIG_SPL_RAM_DEVICE)
73*4882a593Smuzhiyun SPL_LOAD_IMAGE_METHOD("RAM", 0, BOOT_DEVICE_RAM, spl_ram_load_image);
74*4882a593Smuzhiyun #endif
75*4882a593Smuzhiyun #if CONFIG_IS_ENABLED(DFU)
76*4882a593Smuzhiyun SPL_LOAD_IMAGE_METHOD("DFU", 0, BOOT_DEVICE_DFU, spl_ram_load_image);
77*4882a593Smuzhiyun #endif
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun
80