blob: 8296459257186108e20af60dd2373d0a6e882d1b [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
20#ifndef CONFIG_SPL_LOAD_FIT_ADDRESS
21# define CONFIG_SPL_LOAD_FIT_ADDRESS 0
22#endif
23
24static ulong spl_ram_load_read(struct spl_load_info *load, ulong sector,
25 ulong count, void *buf)
26{
Philippe Reynese9d12f42022-03-28 22:57:01 +020027 ulong addr;
28
Stefan Agnercd01e642016-12-23 07:51:53 +010029 debug("%s: sector %lx, count %lx, buf %lx\n",
30 __func__, sector, count, (ulong)buf);
Philippe Reynese9d12f42022-03-28 22:57:01 +020031
32 addr = (ulong)CONFIG_SPL_LOAD_FIT_ADDRESS + sector;
33 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{
44 struct image_header *header;
45
46 header = (struct image_header *)CONFIG_SPL_LOAD_FIT_ADDRESS;
47
Philippe Reynese9d12f42022-03-28 22:57:01 +020048 if (CONFIG_IS_ENABLED(IMAGE_PRE_LOAD)) {
49 unsigned long addr = (unsigned long)header;
50 int ret = image_pre_load(addr);
51
52 if (ret)
53 return ret;
54
55 addr += image_load_offset;
56 header = (struct image_header *)addr;
57 }
58
Andrew F. Davis6d932e62019-01-17 13:43:02 -060059#if CONFIG_IS_ENABLED(DFU)
Stefan Agnercd01e642016-12-23 07:51:53 +010060 if (bootdev->boot_device == BOOT_DEVICE_DFU)
61 spl_dfu_cmd(0, "dfu_alt_info_ram", "ram", "0");
62#endif
63
64 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
65 image_get_magic(header) == FDT_MAGIC) {
66 struct spl_load_info load;
67
68 debug("Found FIT\n");
69 load.bl_len = 1;
70 load.read = spl_ram_load_read;
71 spl_load_simple_fit(spl_image, &load, 0, header);
72 } else {
Simon Glass9dcc8612018-08-01 15:22:42 -060073 ulong u_boot_pos = binman_sym(ulong, u_boot_any, image_pos);
Simon Glass6767ec72017-11-13 18:55:04 -070074
Stefan Agnercd01e642016-12-23 07:51:53 +010075 debug("Legacy image\n");
76 /*
77 * Get the header. It will point to an address defined by
78 * handoff which will tell where the image located inside
Simon Glass6767ec72017-11-13 18:55:04 -070079 * the flash.
Stefan Agnercd01e642016-12-23 07:51:53 +010080 */
Simon Glass6767ec72017-11-13 18:55:04 -070081 debug("u_boot_pos = %lx\n", u_boot_pos);
82 if (u_boot_pos == BINMAN_SYM_MISSING) {
83 /*
84 * No binman support or no information. For now, fix it
85 * to the address pointed to by U-Boot.
86 */
Michal Simek15e12162018-10-04 09:29:20 +020087 u_boot_pos = (ulong)spl_get_load_buffer(-sizeof(*header),
88 sizeof(*header));
Simon Glass6767ec72017-11-13 18:55:04 -070089 }
90 header = (struct image_header *)map_sysmem(u_boot_pos, 0);
Stefan Agnercd01e642016-12-23 07:51:53 +010091
Pali Rohárdda8f882022-01-14 14:31:38 +010092 spl_parse_image_header(spl_image, bootdev, header);
Stefan Agnercd01e642016-12-23 07:51:53 +010093 }
94
95 return 0;
96}
Marek Vasut2d608192018-04-07 17:03:28 +020097#if CONFIG_IS_ENABLED(RAM_DEVICE)
Stefan Agnercd01e642016-12-23 07:51:53 +010098SPL_LOAD_IMAGE_METHOD("RAM", 0, BOOT_DEVICE_RAM, spl_ram_load_image);
99#endif
Andrew F. Davis6d932e62019-01-17 13:43:02 -0600100#if CONFIG_IS_ENABLED(DFU)
Stefan Agnercd01e642016-12-23 07:51:53 +0100101SPL_LOAD_IMAGE_METHOD("DFU", 0, BOOT_DEVICE_DFU, spl_ram_load_image);
102#endif