blob: 4158ed1c32d749e89804a9880d606e86ffb9d308 [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 *
Michal Simeka8c94362023-07-10 14:35:49 +02009 * Michal Simek <michal.simek@amd.com>
Stefan Agnercd01e642016-12-23 07:51:53 +010010 * 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{
Simon Glass95dbb412023-09-14 10:55:50 -060023 ulong addr = 0;
Philippe Reynese9d12f42022-03-28 22:57:01 +020024
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
Simon Glass95dbb412023-09-14 10:55:50 -060028 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT)) {
29 addr = IF_ENABLED_INT(CONFIG_SPL_LOAD_FIT,
30 CONFIG_SPL_LOAD_FIT_ADDRESS);
31 }
32 addr += sector;
Philippe Reynese9d12f42022-03-28 22:57:01 +020033 if (CONFIG_IS_ENABLED(IMAGE_PRE_LOAD))
34 addr += image_load_offset;
35
36 memcpy(buf, (void *)addr, count);
37
Stefan Agnercd01e642016-12-23 07:51:53 +010038 return count;
39}
40
41static int spl_ram_load_image(struct spl_image_info *spl_image,
42 struct spl_boot_device *bootdev)
43{
Simon Glassbb7d3bb2022-09-06 20:26:52 -060044 struct legacy_img_hdr *header;
Simon Glass95dbb412023-09-14 10:55:50 -060045 ulong addr = 0;
Nikita Shubinb5f2af82022-12-12 11:03:35 +030046 int ret;
Stefan Agnercd01e642016-12-23 07:51:53 +010047
Simon Glass95dbb412023-09-14 10:55:50 -060048 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT)) {
49 addr = IF_ENABLED_INT(CONFIG_SPL_LOAD_FIT,
50 CONFIG_SPL_LOAD_FIT_ADDRESS);
51 }
Stefan Agnercd01e642016-12-23 07:51:53 +010052
Philippe Reynese9d12f42022-03-28 22:57:01 +020053 if (CONFIG_IS_ENABLED(IMAGE_PRE_LOAD)) {
Nikita Shubinb5f2af82022-12-12 11:03:35 +030054 ret = image_pre_load(addr);
Philippe Reynese9d12f42022-03-28 22:57:01 +020055
56 if (ret)
57 return ret;
58
59 addr += image_load_offset;
Philippe Reynese9d12f42022-03-28 22:57:01 +020060 }
Simon Glass95dbb412023-09-14 10:55:50 -060061 header = map_sysmem(addr, 0);
Philippe Reynese9d12f42022-03-28 22:57:01 +020062
Andrew F. Davis6d932e62019-01-17 13:43:02 -060063#if CONFIG_IS_ENABLED(DFU)
Stefan Agnercd01e642016-12-23 07:51:53 +010064 if (bootdev->boot_device == BOOT_DEVICE_DFU)
65 spl_dfu_cmd(0, "dfu_alt_info_ram", "ram", "0");
66#endif
67
68 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
69 image_get_magic(header) == FDT_MAGIC) {
70 struct spl_load_info load;
71
72 debug("Found FIT\n");
73 load.bl_len = 1;
74 load.read = spl_ram_load_read;
Nikita Shubinb5f2af82022-12-12 11:03:35 +030075 ret = spl_load_simple_fit(spl_image, &load, 0, header);
Stefan Agnercd01e642016-12-23 07:51:53 +010076 } else {
Alper Nebi Yasak173321c2022-06-18 15:13:06 +030077 ulong u_boot_pos = spl_get_image_pos();
Simon Glass6767ec72017-11-13 18:55:04 -070078
Stefan Agnercd01e642016-12-23 07:51:53 +010079 debug("Legacy image\n");
80 /*
81 * Get the header. It will point to an address defined by
82 * handoff which will tell where the image located inside
Simon Glass6767ec72017-11-13 18:55:04 -070083 * the flash.
Stefan Agnercd01e642016-12-23 07:51:53 +010084 */
Simon Glass6767ec72017-11-13 18:55:04 -070085 debug("u_boot_pos = %lx\n", u_boot_pos);
86 if (u_boot_pos == BINMAN_SYM_MISSING) {
87 /*
88 * No binman support or no information. For now, fix it
89 * to the address pointed to by U-Boot.
90 */
Michal Simek15e12162018-10-04 09:29:20 +020091 u_boot_pos = (ulong)spl_get_load_buffer(-sizeof(*header),
92 sizeof(*header));
Simon Glass6767ec72017-11-13 18:55:04 -070093 }
Simon Glass95dbb412023-09-14 10:55:50 -060094 header = map_sysmem(u_boot_pos, 0);
Stefan Agnercd01e642016-12-23 07:51:53 +010095
Nikita Shubinb5f2af82022-12-12 11:03:35 +030096 ret = spl_parse_image_header(spl_image, bootdev, header);
Stefan Agnercd01e642016-12-23 07:51:53 +010097 }
98
Nikita Shubinb5f2af82022-12-12 11:03:35 +030099 return ret;
Stefan Agnercd01e642016-12-23 07:51:53 +0100100}
Marek Vasut2d608192018-04-07 17:03:28 +0200101#if CONFIG_IS_ENABLED(RAM_DEVICE)
Stefan Agnercd01e642016-12-23 07:51:53 +0100102SPL_LOAD_IMAGE_METHOD("RAM", 0, BOOT_DEVICE_RAM, spl_ram_load_image);
103#endif
Andrew F. Davis6d932e62019-01-17 13:43:02 -0600104#if CONFIG_IS_ENABLED(DFU)
Stefan Agnercd01e642016-12-23 07:51:53 +0100105SPL_LOAD_IMAGE_METHOD("DFU", 0, BOOT_DEVICE_DFU, spl_ram_load_image);
106#endif