xref: /openbmc/u-boot/common/spl/spl_ram.c (revision b08c8c4870831c9315dcae237772238e80035bd5)
122802f4eSStefan Agner /*
222802f4eSStefan Agner  * (C) Copyright 2016
322802f4eSStefan Agner  * Xilinx, Inc.
422802f4eSStefan Agner  *
522802f4eSStefan Agner  * (C) Copyright 2016
622802f4eSStefan Agner  * Toradex AG
722802f4eSStefan Agner  *
822802f4eSStefan Agner  * Michal Simek <michal.simek@xilinx.com>
922802f4eSStefan Agner  * Stefan Agner <stefan.agner@toradex.com>
1022802f4eSStefan Agner  *
1122802f4eSStefan Agner  * SPDX-License-Identifier:	GPL-2.0+
1222802f4eSStefan Agner  */
1322802f4eSStefan Agner #include <common.h>
14dfce1799SSimon Glass #include <binman_sym.h>
15dfce1799SSimon Glass #include <mapmem.h>
1622802f4eSStefan Agner #include <spl.h>
17*b08c8c48SMasahiro Yamada #include <linux/libfdt.h>
1822802f4eSStefan Agner 
1922802f4eSStefan Agner #ifndef CONFIG_SPL_LOAD_FIT_ADDRESS
2022802f4eSStefan Agner # define CONFIG_SPL_LOAD_FIT_ADDRESS	0
2122802f4eSStefan Agner #endif
2222802f4eSStefan Agner 
2322802f4eSStefan Agner static ulong spl_ram_load_read(struct spl_load_info *load, ulong sector,
2422802f4eSStefan Agner 			       ulong count, void *buf)
2522802f4eSStefan Agner {
2622802f4eSStefan Agner 	debug("%s: sector %lx, count %lx, buf %lx\n",
2722802f4eSStefan Agner 	      __func__, sector, count, (ulong)buf);
2822802f4eSStefan Agner 	memcpy(buf, (void *)(CONFIG_SPL_LOAD_FIT_ADDRESS + sector), count);
2922802f4eSStefan Agner 	return count;
3022802f4eSStefan Agner }
3122802f4eSStefan Agner 
3222802f4eSStefan Agner static int spl_ram_load_image(struct spl_image_info *spl_image,
3322802f4eSStefan Agner 			      struct spl_boot_device *bootdev)
3422802f4eSStefan Agner {
3522802f4eSStefan Agner 	struct image_header *header;
3622802f4eSStefan Agner 
3722802f4eSStefan Agner 	header = (struct image_header *)CONFIG_SPL_LOAD_FIT_ADDRESS;
3822802f4eSStefan Agner 
3922802f4eSStefan Agner #if defined(CONFIG_SPL_DFU_SUPPORT)
4022802f4eSStefan Agner 	if (bootdev->boot_device == BOOT_DEVICE_DFU)
4122802f4eSStefan Agner 		spl_dfu_cmd(0, "dfu_alt_info_ram", "ram", "0");
4222802f4eSStefan Agner #endif
4322802f4eSStefan Agner 
4422802f4eSStefan Agner 	if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
4522802f4eSStefan Agner 	    image_get_magic(header) == FDT_MAGIC) {
4622802f4eSStefan Agner 		struct spl_load_info load;
4722802f4eSStefan Agner 
4822802f4eSStefan Agner 		debug("Found FIT\n");
4922802f4eSStefan Agner 		load.bl_len = 1;
5022802f4eSStefan Agner 		load.read = spl_ram_load_read;
5122802f4eSStefan Agner 		spl_load_simple_fit(spl_image, &load, 0, header);
5222802f4eSStefan Agner 	} else {
53dfce1799SSimon Glass 		ulong u_boot_pos = binman_sym(ulong, u_boot_any, pos);
54dfce1799SSimon Glass 
5522802f4eSStefan Agner 		debug("Legacy image\n");
5622802f4eSStefan Agner 		/*
5722802f4eSStefan Agner 		 * Get the header.  It will point to an address defined by
5822802f4eSStefan Agner 		 * handoff which will tell where the image located inside
59dfce1799SSimon Glass 		 * the flash.
6022802f4eSStefan Agner 		 */
61dfce1799SSimon Glass 		debug("u_boot_pos = %lx\n", u_boot_pos);
62dfce1799SSimon Glass 		if (u_boot_pos == BINMAN_SYM_MISSING) {
63dfce1799SSimon Glass 			/*
64dfce1799SSimon Glass 			 * No binman support or no information. For now, fix it
65dfce1799SSimon Glass 			 * to the address pointed to by U-Boot.
66dfce1799SSimon Glass 			 */
67dfce1799SSimon Glass 			u_boot_pos = CONFIG_SYS_TEXT_BASE -
68dfce1799SSimon Glass 					sizeof(struct image_header);
69dfce1799SSimon Glass 		}
70dfce1799SSimon Glass 		header = (struct image_header *)map_sysmem(u_boot_pos, 0);
7122802f4eSStefan Agner 
7222802f4eSStefan Agner 		spl_parse_image_header(spl_image, header);
7322802f4eSStefan Agner 	}
7422802f4eSStefan Agner 
7522802f4eSStefan Agner 	return 0;
7622802f4eSStefan Agner }
7722802f4eSStefan Agner #if defined(CONFIG_SPL_RAM_DEVICE)
7822802f4eSStefan Agner SPL_LOAD_IMAGE_METHOD("RAM", 0, BOOT_DEVICE_RAM, spl_ram_load_image);
7922802f4eSStefan Agner #endif
8022802f4eSStefan Agner #if defined(CONFIG_SPL_DFU_SUPPORT)
8122802f4eSStefan Agner SPL_LOAD_IMAGE_METHOD("DFU", 0, BOOT_DEVICE_DFU, spl_ram_load_image);
8222802f4eSStefan Agner #endif
8322802f4eSStefan Agner 
8422802f4eSStefan Agner 
85