blob: 8139a203273c88e21b5ce75463a078877b25a7af [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stefan Agner22802f42016-12-23 07:51:53 +01002/*
3 * (C) Copyright 2016
4 * Xilinx, Inc.
5 *
6 * (C) Copyright 2016
7 * Toradex AG
8 *
9 * Michal Simek <michal.simek@xilinx.com>
10 * Stefan Agner <stefan.agner@toradex.com>
Stefan Agner22802f42016-12-23 07:51:53 +010011 */
12#include <common.h>
Simon Glassdfce1792017-11-13 18:55:04 -070013#include <binman_sym.h>
Simon Glass4d72caa2020-05-10 11:40:01 -060014#include <image.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060015#include <log.h>
Simon Glassdfce1792017-11-13 18:55:04 -070016#include <mapmem.h>
Stefan Agner22802f42016-12-23 07:51:53 +010017#include <spl.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090018#include <linux/libfdt.h>
Stefan Agner22802f42016-12-23 07:51:53 +010019
Stefan Agner22802f42016-12-23 07:51:53 +010020static ulong spl_ram_load_read(struct spl_load_info *load, ulong sector,
21 ulong count, void *buf)
22{
Philippe Reynes2404a012022-03-28 22:57:01 +020023 ulong addr;
24
Stefan Agner22802f42016-12-23 07:51:53 +010025 debug("%s: sector %lx, count %lx, buf %lx\n",
26 __func__, sector, count, (ulong)buf);
Philippe Reynes2404a012022-03-28 22:57:01 +020027
28 addr = (ulong)CONFIG_SPL_LOAD_FIT_ADDRESS + sector;
29 if (CONFIG_IS_ENABLED(IMAGE_PRE_LOAD))
30 addr += image_load_offset;
31
32 memcpy(buf, (void *)addr, count);
33
Stefan Agner22802f42016-12-23 07:51:53 +010034 return count;
35}
36
37static int spl_ram_load_image(struct spl_image_info *spl_image,
38 struct spl_boot_device *bootdev)
39{
Simon Glassf3543e62022-09-06 20:26:52 -060040 struct legacy_img_hdr *header;
Nikita Shubincf7aa032022-12-12 11:03:35 +030041 int ret;
Stefan Agner22802f42016-12-23 07:51:53 +010042
Simon Glassf3543e62022-09-06 20:26:52 -060043 header = (struct legacy_img_hdr *)CONFIG_SPL_LOAD_FIT_ADDRESS;
Stefan Agner22802f42016-12-23 07:51:53 +010044
Philippe Reynes2404a012022-03-28 22:57:01 +020045 if (CONFIG_IS_ENABLED(IMAGE_PRE_LOAD)) {
46 unsigned long addr = (unsigned long)header;
Nikita Shubincf7aa032022-12-12 11:03:35 +030047 ret = image_pre_load(addr);
Philippe Reynes2404a012022-03-28 22:57:01 +020048
49 if (ret)
50 return ret;
51
52 addr += image_load_offset;
Simon Glassf3543e62022-09-06 20:26:52 -060053 header = (struct legacy_img_hdr *)addr;
Philippe Reynes2404a012022-03-28 22:57:01 +020054 }
55
Andrew F. Davis6536ca42019-01-17 13:43:02 -060056#if CONFIG_IS_ENABLED(DFU)
Stefan Agner22802f42016-12-23 07:51:53 +010057 if (bootdev->boot_device == BOOT_DEVICE_DFU)
58 spl_dfu_cmd(0, "dfu_alt_info_ram", "ram", "0");
59#endif
60
61 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
62 image_get_magic(header) == FDT_MAGIC) {
63 struct spl_load_info load;
64
65 debug("Found FIT\n");
66 load.bl_len = 1;
67 load.read = spl_ram_load_read;
Nikita Shubincf7aa032022-12-12 11:03:35 +030068 ret = spl_load_simple_fit(spl_image, &load, 0, header);
Stefan Agner22802f42016-12-23 07:51:53 +010069 } else {
Alper Nebi Yasakd7f07172022-06-18 15:13:06 +030070 ulong u_boot_pos = spl_get_image_pos();
Simon Glassdfce1792017-11-13 18:55:04 -070071
Stefan Agner22802f42016-12-23 07:51:53 +010072 debug("Legacy image\n");
73 /*
74 * Get the header. It will point to an address defined by
75 * handoff which will tell where the image located inside
Simon Glassdfce1792017-11-13 18:55:04 -070076 * the flash.
Stefan Agner22802f42016-12-23 07:51:53 +010077 */
Simon Glassdfce1792017-11-13 18:55:04 -070078 debug("u_boot_pos = %lx\n", u_boot_pos);
79 if (u_boot_pos == BINMAN_SYM_MISSING) {
80 /*
81 * No binman support or no information. For now, fix it
82 * to the address pointed to by U-Boot.
83 */
Michal Simek83a64562018-10-04 09:29:20 +020084 u_boot_pos = (ulong)spl_get_load_buffer(-sizeof(*header),
85 sizeof(*header));
Simon Glassdfce1792017-11-13 18:55:04 -070086 }
Simon Glassf3543e62022-09-06 20:26:52 -060087 header = (struct legacy_img_hdr *)map_sysmem(u_boot_pos, 0);
Stefan Agner22802f42016-12-23 07:51:53 +010088
Nikita Shubincf7aa032022-12-12 11:03:35 +030089 ret = spl_parse_image_header(spl_image, bootdev, header);
Stefan Agner22802f42016-12-23 07:51:53 +010090 }
91
Nikita Shubincf7aa032022-12-12 11:03:35 +030092 return ret;
Stefan Agner22802f42016-12-23 07:51:53 +010093}
Marek Vasut49132612018-04-07 17:03:28 +020094#if CONFIG_IS_ENABLED(RAM_DEVICE)
Stefan Agner22802f42016-12-23 07:51:53 +010095SPL_LOAD_IMAGE_METHOD("RAM", 0, BOOT_DEVICE_RAM, spl_ram_load_image);
96#endif
Andrew F. Davis6536ca42019-01-17 13:43:02 -060097#if CONFIG_IS_ENABLED(DFU)
Stefan Agner22802f42016-12-23 07:51:53 +010098SPL_LOAD_IMAGE_METHOD("DFU", 0, BOOT_DEVICE_DFU, spl_ram_load_image);
99#endif