blob: 71b7a8374bbbf534e1b2c28e02205af035fcfd8b [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 */
Simon Glass6767ec72017-11-13 18:55:04 -070012#include <binman_sym.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060013#include <image.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Simon Glass6767ec72017-11-13 18:55:04 -070015#include <mapmem.h>
Stefan Agnercd01e642016-12-23 07:51:53 +010016#include <spl.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090017#include <linux/libfdt.h>
Stefan Agnercd01e642016-12-23 07:51:53 +010018
Stefan Agnercd01e642016-12-23 07:51:53 +010019static ulong spl_ram_load_read(struct spl_load_info *load, ulong sector,
20 ulong count, void *buf)
21{
Simon Glass95dbb412023-09-14 10:55:50 -060022 ulong addr = 0;
Philippe Reynese9d12f42022-03-28 22:57:01 +020023
Stefan Agnercd01e642016-12-23 07:51:53 +010024 debug("%s: sector %lx, count %lx, buf %lx\n",
25 __func__, sector, count, (ulong)buf);
Philippe Reynese9d12f42022-03-28 22:57:01 +020026
Simon Glass95dbb412023-09-14 10:55:50 -060027 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT)) {
28 addr = IF_ENABLED_INT(CONFIG_SPL_LOAD_FIT,
29 CONFIG_SPL_LOAD_FIT_ADDRESS);
30 }
31 addr += sector;
Philippe Reynese9d12f42022-03-28 22:57:01 +020032 if (CONFIG_IS_ENABLED(IMAGE_PRE_LOAD))
33 addr += image_load_offset;
34
35 memcpy(buf, (void *)addr, count);
36
Stefan Agnercd01e642016-12-23 07:51:53 +010037 return count;
38}
39
40static int spl_ram_load_image(struct spl_image_info *spl_image,
41 struct spl_boot_device *bootdev)
42{
Simon Glassbb7d3bb2022-09-06 20:26:52 -060043 struct legacy_img_hdr *header;
Simon Glass95dbb412023-09-14 10:55:50 -060044 ulong addr = 0;
Nikita Shubinb5f2af82022-12-12 11:03:35 +030045 int ret;
Stefan Agnercd01e642016-12-23 07:51:53 +010046
Simon Glass95dbb412023-09-14 10:55:50 -060047 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT)) {
48 addr = IF_ENABLED_INT(CONFIG_SPL_LOAD_FIT,
49 CONFIG_SPL_LOAD_FIT_ADDRESS);
50 }
Stefan Agnercd01e642016-12-23 07:51:53 +010051
Philippe Reynese9d12f42022-03-28 22:57:01 +020052 if (CONFIG_IS_ENABLED(IMAGE_PRE_LOAD)) {
Nikita Shubinb5f2af82022-12-12 11:03:35 +030053 ret = image_pre_load(addr);
Philippe Reynese9d12f42022-03-28 22:57:01 +020054
55 if (ret)
56 return ret;
57
58 addr += image_load_offset;
Philippe Reynese9d12f42022-03-28 22:57:01 +020059 }
Simon Glass95dbb412023-09-14 10:55:50 -060060 header = map_sysmem(addr, 0);
Philippe Reynese9d12f42022-03-28 22:57:01 +020061
Andrew F. Davis6d932e62019-01-17 13:43:02 -060062#if CONFIG_IS_ENABLED(DFU)
Stefan Agnercd01e642016-12-23 07:51:53 +010063 if (bootdev->boot_device == BOOT_DEVICE_DFU)
64 spl_dfu_cmd(0, "dfu_alt_info_ram", "ram", "0");
65#endif
66
67 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
68 image_get_magic(header) == FDT_MAGIC) {
69 struct spl_load_info load;
70
71 debug("Found FIT\n");
Simon Glasse8066862024-08-22 07:55:02 -060072 spl_load_init(&load, spl_ram_load_read, NULL, 1);
Nikita Shubinb5f2af82022-12-12 11:03:35 +030073 ret = spl_load_simple_fit(spl_image, &load, 0, header);
Stefan Agnercd01e642016-12-23 07:51:53 +010074 } else {
Alper Nebi Yasak173321c2022-06-18 15:13:06 +030075 ulong u_boot_pos = spl_get_image_pos();
Simon Glass6767ec72017-11-13 18:55:04 -070076
Stefan Agnercd01e642016-12-23 07:51:53 +010077 debug("Legacy image\n");
78 /*
79 * Get the header. It will point to an address defined by
80 * handoff which will tell where the image located inside
Simon Glass6767ec72017-11-13 18:55:04 -070081 * the flash.
Stefan Agnercd01e642016-12-23 07:51:53 +010082 */
Simon Glass6767ec72017-11-13 18:55:04 -070083 debug("u_boot_pos = %lx\n", u_boot_pos);
84 if (u_boot_pos == BINMAN_SYM_MISSING) {
85 /*
86 * No binman support or no information. For now, fix it
87 * to the address pointed to by U-Boot.
88 */
Michal Simek15e12162018-10-04 09:29:20 +020089 u_boot_pos = (ulong)spl_get_load_buffer(-sizeof(*header),
90 sizeof(*header));
Simon Glass6767ec72017-11-13 18:55:04 -070091 }
Simon Glass95dbb412023-09-14 10:55:50 -060092 header = map_sysmem(u_boot_pos, 0);
Stefan Agnercd01e642016-12-23 07:51:53 +010093
Nikita Shubinb5f2af82022-12-12 11:03:35 +030094 ret = spl_parse_image_header(spl_image, bootdev, header);
Stefan Agnercd01e642016-12-23 07:51:53 +010095 }
96
Nikita Shubinb5f2af82022-12-12 11:03:35 +030097 return ret;
Stefan Agnercd01e642016-12-23 07:51:53 +010098}
Marek Vasut2d608192018-04-07 17:03:28 +020099#if CONFIG_IS_ENABLED(RAM_DEVICE)
Stefan Agnercd01e642016-12-23 07:51:53 +0100100SPL_LOAD_IMAGE_METHOD("RAM", 0, BOOT_DEVICE_RAM, spl_ram_load_image);
101#endif
Andrew F. Davis6d932e62019-01-17 13:43:02 -0600102#if CONFIG_IS_ENABLED(DFU)
Stefan Agnercd01e642016-12-23 07:51:53 +0100103SPL_LOAD_IMAGE_METHOD("DFU", 0, BOOT_DEVICE_DFU, spl_ram_load_image);
104#endif