blob: 5a23841f6981589344499bfdbd98611904379f31 [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");
Sean Anderson35f15fe2023-11-08 11:48:43 -050072 spl_set_bl_len(&load, 1);
Stefan Agnercd01e642016-12-23 07:51:53 +010073 load.read = spl_ram_load_read;
Nikita Shubinb5f2af82022-12-12 11:03:35 +030074 ret = spl_load_simple_fit(spl_image, &load, 0, header);
Stefan Agnercd01e642016-12-23 07:51:53 +010075 } else {
Alper Nebi Yasak173321c2022-06-18 15:13:06 +030076 ulong u_boot_pos = spl_get_image_pos();
Simon Glass6767ec72017-11-13 18:55:04 -070077
Stefan Agnercd01e642016-12-23 07:51:53 +010078 debug("Legacy image\n");
79 /*
80 * Get the header. It will point to an address defined by
81 * handoff which will tell where the image located inside
Simon Glass6767ec72017-11-13 18:55:04 -070082 * the flash.
Stefan Agnercd01e642016-12-23 07:51:53 +010083 */
Simon Glass6767ec72017-11-13 18:55:04 -070084 debug("u_boot_pos = %lx\n", u_boot_pos);
85 if (u_boot_pos == BINMAN_SYM_MISSING) {
86 /*
87 * No binman support or no information. For now, fix it
88 * to the address pointed to by U-Boot.
89 */
Michal Simek15e12162018-10-04 09:29:20 +020090 u_boot_pos = (ulong)spl_get_load_buffer(-sizeof(*header),
91 sizeof(*header));
Simon Glass6767ec72017-11-13 18:55:04 -070092 }
Simon Glass95dbb412023-09-14 10:55:50 -060093 header = map_sysmem(u_boot_pos, 0);
Stefan Agnercd01e642016-12-23 07:51:53 +010094
Nikita Shubinb5f2af82022-12-12 11:03:35 +030095 ret = spl_parse_image_header(spl_image, bootdev, header);
Stefan Agnercd01e642016-12-23 07:51:53 +010096 }
97
Nikita Shubinb5f2af82022-12-12 11:03:35 +030098 return ret;
Stefan Agnercd01e642016-12-23 07:51:53 +010099}
Marek Vasut2d608192018-04-07 17:03:28 +0200100#if CONFIG_IS_ENABLED(RAM_DEVICE)
Stefan Agnercd01e642016-12-23 07:51:53 +0100101SPL_LOAD_IMAGE_METHOD("RAM", 0, BOOT_DEVICE_RAM, spl_ram_load_image);
102#endif
Andrew F. Davis6d932e62019-01-17 13:43:02 -0600103#if CONFIG_IS_ENABLED(DFU)
Stefan Agnercd01e642016-12-23 07:51:53 +0100104SPL_LOAD_IMAGE_METHOD("DFU", 0, BOOT_DEVICE_DFU, spl_ram_load_image);
105#endif