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