blob: 8139a203273c88e21b5ce75463a078877b25a7af [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stefan Agnercd01e642016-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 Agnercd01e642016-12-23 07:51:53 +010011 */
12#include <common.h>
Simon Glass6767ec72017-11-13 18:55:04 -070013#include <binman_sym.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060014#include <image.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Simon Glass6767ec72017-11-13 18:55:04 -070016#include <mapmem.h>
Stefan Agnercd01e642016-12-23 07:51:53 +010017#include <spl.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090018#include <linux/libfdt.h>
Stefan Agnercd01e642016-12-23 07:51:53 +010019
Stefan Agnercd01e642016-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 Reynese9d12f42022-03-28 22:57:01 +020023 ulong addr;
24
Stefan Agnercd01e642016-12-23 07:51:53 +010025 debug("%s: sector %lx, count %lx, buf %lx\n",
26 __func__, sector, count, (ulong)buf);
Philippe Reynese9d12f42022-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 Agnercd01e642016-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 Glassbb7d3bb2022-09-06 20:26:52 -060040 struct legacy_img_hdr *header;
Nikita Shubinb5f2af82022-12-12 11:03:35 +030041 int ret;
Stefan Agnercd01e642016-12-23 07:51:53 +010042
Simon Glassbb7d3bb2022-09-06 20:26:52 -060043 header = (struct legacy_img_hdr *)CONFIG_SPL_LOAD_FIT_ADDRESS;
Stefan Agnercd01e642016-12-23 07:51:53 +010044
Philippe Reynese9d12f42022-03-28 22:57:01 +020045 if (CONFIG_IS_ENABLED(IMAGE_PRE_LOAD)) {
46 unsigned long addr = (unsigned long)header;
Nikita Shubinb5f2af82022-12-12 11:03:35 +030047 ret = image_pre_load(addr);
Philippe Reynese9d12f42022-03-28 22:57:01 +020048
49 if (ret)
50 return ret;
51
52 addr += image_load_offset;
Simon Glassbb7d3bb2022-09-06 20:26:52 -060053 header = (struct legacy_img_hdr *)addr;
Philippe Reynese9d12f42022-03-28 22:57:01 +020054 }
55
Andrew F. Davis6d932e62019-01-17 13:43:02 -060056#if CONFIG_IS_ENABLED(DFU)
Stefan Agnercd01e642016-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 Shubinb5f2af82022-12-12 11:03:35 +030068 ret = spl_load_simple_fit(spl_image, &load, 0, header);
Stefan Agnercd01e642016-12-23 07:51:53 +010069 } else {
Alper Nebi Yasak173321c2022-06-18 15:13:06 +030070 ulong u_boot_pos = spl_get_image_pos();
Simon Glass6767ec72017-11-13 18:55:04 -070071
Stefan Agnercd01e642016-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 Glass6767ec72017-11-13 18:55:04 -070076 * the flash.
Stefan Agnercd01e642016-12-23 07:51:53 +010077 */
Simon Glass6767ec72017-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 Simek15e12162018-10-04 09:29:20 +020084 u_boot_pos = (ulong)spl_get_load_buffer(-sizeof(*header),
85 sizeof(*header));
Simon Glass6767ec72017-11-13 18:55:04 -070086 }
Simon Glassbb7d3bb2022-09-06 20:26:52 -060087 header = (struct legacy_img_hdr *)map_sysmem(u_boot_pos, 0);
Stefan Agnercd01e642016-12-23 07:51:53 +010088
Nikita Shubinb5f2af82022-12-12 11:03:35 +030089 ret = spl_parse_image_header(spl_image, bootdev, header);
Stefan Agnercd01e642016-12-23 07:51:53 +010090 }
91
Nikita Shubinb5f2af82022-12-12 11:03:35 +030092 return ret;
Stefan Agnercd01e642016-12-23 07:51:53 +010093}
Marek Vasut2d608192018-04-07 17:03:28 +020094#if CONFIG_IS_ENABLED(RAM_DEVICE)
Stefan Agnercd01e642016-12-23 07:51:53 +010095SPL_LOAD_IMAGE_METHOD("RAM", 0, BOOT_DEVICE_RAM, spl_ram_load_image);
96#endif
Andrew F. Davis6d932e62019-01-17 13:43:02 -060097#if CONFIG_IS_ENABLED(DFU)
Stefan Agnercd01e642016-12-23 07:51:53 +010098SPL_LOAD_IMAGE_METHOD("DFU", 0, BOOT_DEVICE_DFU, spl_ram_load_image);
99#endif