blob: eaa95fb9b590ffed6b816a540e6d933cbc16a9c0 [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>
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
Stefan Roese900c1772012-08-27 12:50:59 +02009#include <spl.h>
10
York Sun79757252018-06-26 10:10:03 -070011static ulong spl_nor_load_read(struct spl_load_info *load, ulong sector,
12 ulong count, void *buf)
13{
14 debug("%s: sector %lx, count %lx, buf %p\n",
15 __func__, sector, count, buf);
16 memcpy(buf, (void *)sector, count);
17
18 return count;
19}
York Sun79757252018-06-26 10:10:03 -070020
Peng Fan6e773f92019-09-23 10:18:42 +080021unsigned long __weak spl_nor_get_uboot_base(void)
22{
23 return CONFIG_SYS_UBOOT_BASE;
24}
25
Simon Glassee306792016-09-24 18:20:13 -060026static int spl_nor_load_image(struct spl_image_info *spl_image,
27 struct spl_boot_device *bootdev)
Stefan Roese900c1772012-08-27 12:50:59 +020028{
Simon Glassbb7d3bb2022-09-06 20:26:52 -060029 __maybe_unused const struct legacy_img_hdr *header;
York Sun79757252018-06-26 10:10:03 -070030 __maybe_unused struct spl_load_info load;
31
Stefan Roese900c1772012-08-27 12:50:59 +020032 /*
33 * Loading of the payload to SDRAM is done with skipping of
34 * the mkimage header in this SPL NOR driver
35 */
Simon Glassee306792016-09-24 18:20:13 -060036 spl_image->flags |= SPL_COPY_PAYLOAD_ONLY;
Stefan Roese900c1772012-08-27 12:50:59 +020037
Tom Rinic2e78882021-10-30 23:03:48 -040038#if CONFIG_IS_ENABLED(OS_BOOT)
Masahiro Yamada225581b2015-01-08 19:23:35 +090039 if (!spl_start_uboot()) {
Stefan Roese900c1772012-08-27 12:50:59 +020040 /*
41 * Load Linux from its location in NOR flash to its defined
42 * location in SDRAM
43 */
Simon Glassbb7d3bb2022-09-06 20:26:52 -060044 header = (const struct legacy_img_hdr *)CONFIG_SYS_OS_BASE;
York Sun79757252018-06-26 10:10:03 -070045#ifdef CONFIG_SPL_LOAD_FIT
46 if (image_get_magic(header) == FDT_MAGIC) {
Stefan Roese4ee4cf92020-04-21 09:28:44 +020047 int ret;
48
York Sun79757252018-06-26 10:10:03 -070049 debug("Found FIT\n");
50 load.bl_len = 1;
51 load.read = spl_nor_load_read;
Stefan Roese900c1772012-08-27 12:50:59 +020052
York Sun79757252018-06-26 10:10:03 -070053 ret = spl_load_simple_fit(spl_image, &load,
54 CONFIG_SYS_OS_BASE,
55 (void *)header);
56
Lukasz Majewski55ce92e2019-10-15 10:28:45 +020057#if defined CONFIG_SYS_SPL_ARGS_ADDR && defined CONFIG_CMD_SPL_NOR_OFS
58 memcpy((void *)CONFIG_SYS_SPL_ARGS_ADDR,
59 (void *)CONFIG_CMD_SPL_NOR_OFS,
60 CONFIG_CMD_SPL_WRITE_SIZE);
61#endif
York Sun79757252018-06-26 10:10:03 -070062 return ret;
63 }
64#endif
Masahiro Yamada225581b2015-01-08 19:23:35 +090065 if (image_get_os(header) == IH_OS_LINUX) {
66 /* happy - was a Linux */
Stefan Roese4ee4cf92020-04-21 09:28:44 +020067 int ret;
Stefan Roese900c1772012-08-27 12:50:59 +020068
Pali Rohárdda8f882022-01-14 14:31:38 +010069 ret = spl_parse_image_header(spl_image, bootdev, header);
Marek Vasut02266e22016-04-29 00:44:54 +020070 if (ret)
71 return ret;
Masahiro Yamada225581b2015-01-08 19:23:35 +090072
Simon Glassee306792016-09-24 18:20:13 -060073 memcpy((void *)spl_image->load_addr,
Masahiro Yamada225581b2015-01-08 19:23:35 +090074 (void *)(CONFIG_SYS_OS_BASE +
Simon Glassbb7d3bb2022-09-06 20:26:52 -060075 sizeof(struct legacy_img_hdr)),
Simon Glassee306792016-09-24 18:20:13 -060076 spl_image->size);
Tom Rini2c9d2b52022-05-27 22:06:52 -040077#ifdef CONFIG_SYS_SPL_ARGS_ADDR
78 spl_image->arg = (void *)CONFIG_SYS_SPL_ARGS_ADDR;
York Sunf0370c02018-06-26 10:10:04 -070079#endif
Masahiro Yamada225581b2015-01-08 19:23:35 +090080
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020081 return 0;
Masahiro Yamada225581b2015-01-08 19:23:35 +090082 } else {
83 puts("The Expected Linux image was not found.\n"
84 "Please check your NOR configuration.\n"
85 "Trying to start u-boot now...\n");
86 }
Stefan Roese900c1772012-08-27 12:50:59 +020087 }
Masahiro Yamada225581b2015-01-08 19:23:35 +090088#endif
89
90 /*
91 * Load real U-Boot from its location in NOR flash to its
92 * defined location in SDRAM
93 */
York Sun79757252018-06-26 10:10:03 -070094#ifdef CONFIG_SPL_LOAD_FIT
Simon Glassbb7d3bb2022-09-06 20:26:52 -060095 header = (const struct legacy_img_hdr *)spl_nor_get_uboot_base();
York Sun79757252018-06-26 10:10:03 -070096 if (image_get_magic(header) == FDT_MAGIC) {
97 debug("Found FIT format U-Boot\n");
98 load.bl_len = 1;
99 load.read = spl_nor_load_read;
Stefan Roese4ee4cf92020-04-21 09:28:44 +0200100 return spl_load_simple_fit(spl_image, &load,
101 spl_nor_get_uboot_base(),
102 (void *)header);
York Sun79757252018-06-26 10:10:03 -0700103 }
104#endif
Peng Fan3d52a6f2019-09-23 10:18:48 +0800105 if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) {
106 load.bl_len = 1;
107 load.read = spl_nor_load_read;
108 return spl_load_imx_container(spl_image, &load,
109 spl_nor_get_uboot_base());
110 }
111
Stefan Roese84ae9d82020-04-21 09:28:43 +0200112 /* Legacy image handling */
Andrew Davis360ec6a2022-05-04 15:52:25 -0500113 if (IS_ENABLED(CONFIG_SPL_LEGACY_IMAGE_FORMAT)) {
Roger Quadros237f0182022-09-29 13:11:28 +0300114 struct legacy_img_hdr hdr;
115
Stefan Roese84ae9d82020-04-21 09:28:43 +0200116 load.bl_len = 1;
117 load.read = spl_nor_load_read;
Roger Quadros237f0182022-09-29 13:11:28 +0300118 spl_nor_load_read(&load, spl_nor_get_uboot_base(), sizeof(hdr), &hdr);
Pali Rohárdda8f882022-01-14 14:31:38 +0100119 return spl_load_legacy_img(spl_image, bootdev, &load,
Roger Quadros237f0182022-09-29 13:11:28 +0300120 spl_nor_get_uboot_base(),
121 &hdr);
Stefan Roese84ae9d82020-04-21 09:28:43 +0200122 }
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200123
124 return 0;
Stefan Roese900c1772012-08-27 12:50:59 +0200125}
Simon Glass4fc1f252016-11-30 15:30:50 -0700126SPL_LOAD_IMAGE_METHOD("NOR", 0, BOOT_DEVICE_NOR, spl_nor_load_image);