blob: d4733ea90a6ac3b39645293b1766a34b9de114ae [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stefan Roese900c1772012-08-27 12:50:59 +02002/*
3 * Copyright (C) 2012 Stefan Roese <sr@denx.de>
Stefan Roese900c1772012-08-27 12:50:59 +02004 */
5
6#include <common.h>
Simon Glass2dc9c342020-05-10 11:40:01 -06007#include <image.h>
Stefan Roese900c1772012-08-27 12:50:59 +02008#include <spl.h>
9
York Sun79757252018-06-26 10:10:03 -070010static ulong spl_nor_load_read(struct spl_load_info *load, ulong sector,
11 ulong count, void *buf)
12{
13 debug("%s: sector %lx, count %lx, buf %p\n",
14 __func__, sector, count, buf);
15 memcpy(buf, (void *)sector, count);
16
17 return count;
18}
York Sun79757252018-06-26 10:10:03 -070019
Peng Fan6e773f92019-09-23 10:18:42 +080020unsigned long __weak spl_nor_get_uboot_base(void)
21{
22 return CONFIG_SYS_UBOOT_BASE;
23}
24
Simon Glassee306792016-09-24 18:20:13 -060025static int spl_nor_load_image(struct spl_image_info *spl_image,
26 struct spl_boot_device *bootdev)
Stefan Roese900c1772012-08-27 12:50:59 +020027{
York Sun79757252018-06-26 10:10:03 -070028 __maybe_unused const struct image_header *header;
29 __maybe_unused struct spl_load_info load;
30
Stefan Roese900c1772012-08-27 12:50:59 +020031 /*
32 * Loading of the payload to SDRAM is done with skipping of
33 * the mkimage header in this SPL NOR driver
34 */
Simon Glassee306792016-09-24 18:20:13 -060035 spl_image->flags |= SPL_COPY_PAYLOAD_ONLY;
Stefan Roese900c1772012-08-27 12:50:59 +020036
Masahiro Yamada225581b2015-01-08 19:23:35 +090037#ifdef CONFIG_SPL_OS_BOOT
38 if (!spl_start_uboot()) {
Stefan Roese900c1772012-08-27 12:50:59 +020039 /*
40 * Load Linux from its location in NOR flash to its defined
41 * location in SDRAM
42 */
Masahiro Yamada225581b2015-01-08 19:23:35 +090043 header = (const struct image_header *)CONFIG_SYS_OS_BASE;
York Sun79757252018-06-26 10:10:03 -070044#ifdef CONFIG_SPL_LOAD_FIT
45 if (image_get_magic(header) == FDT_MAGIC) {
Stefan Roese4ee4cf92020-04-21 09:28:44 +020046 int ret;
47
York Sun79757252018-06-26 10:10:03 -070048 debug("Found FIT\n");
49 load.bl_len = 1;
50 load.read = spl_nor_load_read;
Stefan Roese900c1772012-08-27 12:50:59 +020051
York Sun79757252018-06-26 10:10:03 -070052 ret = spl_load_simple_fit(spl_image, &load,
53 CONFIG_SYS_OS_BASE,
54 (void *)header);
55
Lukasz Majewski55ce92e2019-10-15 10:28:45 +020056#if defined CONFIG_SYS_SPL_ARGS_ADDR && defined CONFIG_CMD_SPL_NOR_OFS
57 memcpy((void *)CONFIG_SYS_SPL_ARGS_ADDR,
58 (void *)CONFIG_CMD_SPL_NOR_OFS,
59 CONFIG_CMD_SPL_WRITE_SIZE);
60#endif
York Sun79757252018-06-26 10:10:03 -070061 return ret;
62 }
63#endif
Masahiro Yamada225581b2015-01-08 19:23:35 +090064 if (image_get_os(header) == IH_OS_LINUX) {
65 /* happy - was a Linux */
Stefan Roese4ee4cf92020-04-21 09:28:44 +020066 int ret;
Stefan Roese900c1772012-08-27 12:50:59 +020067
Simon Glassee306792016-09-24 18:20:13 -060068 ret = spl_parse_image_header(spl_image, header);
Marek Vasut02266e22016-04-29 00:44:54 +020069 if (ret)
70 return ret;
Masahiro Yamada225581b2015-01-08 19:23:35 +090071
Simon Glassee306792016-09-24 18:20:13 -060072 memcpy((void *)spl_image->load_addr,
Masahiro Yamada225581b2015-01-08 19:23:35 +090073 (void *)(CONFIG_SYS_OS_BASE +
74 sizeof(struct image_header)),
Simon Glassee306792016-09-24 18:20:13 -060075 spl_image->size);
York Sunf0370c02018-06-26 10:10:04 -070076#ifdef CONFIG_SYS_FDT_BASE
Vikas Manocha62b021c2017-04-07 15:38:13 -070077 spl_image->arg = (void *)CONFIG_SYS_FDT_BASE;
York Sunf0370c02018-06-26 10:10:04 -070078#endif
Masahiro Yamada225581b2015-01-08 19:23:35 +090079
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020080 return 0;
Masahiro Yamada225581b2015-01-08 19:23:35 +090081 } else {
82 puts("The Expected Linux image was not found.\n"
83 "Please check your NOR configuration.\n"
84 "Trying to start u-boot now...\n");
85 }
Stefan Roese900c1772012-08-27 12:50:59 +020086 }
Masahiro Yamada225581b2015-01-08 19:23:35 +090087#endif
88
89 /*
90 * Load real U-Boot from its location in NOR flash to its
91 * defined location in SDRAM
92 */
York Sun79757252018-06-26 10:10:03 -070093#ifdef CONFIG_SPL_LOAD_FIT
Peng Fan6e773f92019-09-23 10:18:42 +080094 header = (const struct image_header *)spl_nor_get_uboot_base();
York Sun79757252018-06-26 10:10:03 -070095 if (image_get_magic(header) == FDT_MAGIC) {
96 debug("Found FIT format U-Boot\n");
97 load.bl_len = 1;
98 load.read = spl_nor_load_read;
Stefan Roese4ee4cf92020-04-21 09:28:44 +020099 return spl_load_simple_fit(spl_image, &load,
100 spl_nor_get_uboot_base(),
101 (void *)header);
York Sun79757252018-06-26 10:10:03 -0700102 }
103#endif
Peng Fan3d52a6f2019-09-23 10:18:48 +0800104 if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) {
105 load.bl_len = 1;
106 load.read = spl_nor_load_read;
107 return spl_load_imx_container(spl_image, &load,
108 spl_nor_get_uboot_base());
109 }
110
Stefan Roese84ae9d82020-04-21 09:28:43 +0200111 /* Legacy image handling */
112 if (IS_ENABLED(CONFIG_SPL_LEGACY_IMAGE_SUPPORT)) {
113 load.bl_len = 1;
114 load.read = spl_nor_load_read;
115 return spl_load_legacy_img(spl_image, &load,
116 spl_nor_get_uboot_base());
117 }
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200118
119 return 0;
Stefan Roese900c1772012-08-27 12:50:59 +0200120}
Simon Glass4fc1f252016-11-30 15:30:50 -0700121SPL_LOAD_IMAGE_METHOD("NOR", 0, BOOT_DEVICE_NOR, spl_nor_load_image);