blob: dd44798207185c750d2ca67b8c650d8226faa123 [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>
Sean Anderson952ed672023-10-14 16:47:44 -04008#include <imx_container.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Stefan Roese900c1772012-08-27 12:50:59 +020010#include <spl.h>
11
York Sun79757252018-06-26 10:10:03 -070012static ulong spl_nor_load_read(struct spl_load_info *load, ulong sector,
13 ulong count, void *buf)
14{
15 debug("%s: sector %lx, count %lx, buf %p\n",
16 __func__, sector, count, buf);
17 memcpy(buf, (void *)sector, count);
18
19 return count;
20}
York Sun79757252018-06-26 10:10:03 -070021
Peng Fan6e773f92019-09-23 10:18:42 +080022unsigned long __weak spl_nor_get_uboot_base(void)
23{
Tom Rini6a5dccc2022-11-16 13:10:41 -050024 return CFG_SYS_UBOOT_BASE;
Peng Fan6e773f92019-09-23 10:18:42 +080025}
26
Simon Glassee306792016-09-24 18:20:13 -060027static int spl_nor_load_image(struct spl_image_info *spl_image,
28 struct spl_boot_device *bootdev)
Stefan Roese900c1772012-08-27 12:50:59 +020029{
Sean Andersonfab09a52023-10-14 16:47:38 -040030 struct legacy_img_hdr *header;
York Sun79757252018-06-26 10:10:03 -070031 __maybe_unused struct spl_load_info load;
32
Stefan Roese900c1772012-08-27 12:50:59 +020033 /*
34 * Loading of the payload to SDRAM is done with skipping of
35 * the mkimage header in this SPL NOR driver
36 */
Simon Glassee306792016-09-24 18:20:13 -060037 spl_image->flags |= SPL_COPY_PAYLOAD_ONLY;
Stefan Roese900c1772012-08-27 12:50:59 +020038
Tom Rinic2e78882021-10-30 23:03:48 -040039#if CONFIG_IS_ENABLED(OS_BOOT)
Masahiro Yamada225581b2015-01-08 19:23:35 +090040 if (!spl_start_uboot()) {
Stefan Roese900c1772012-08-27 12:50:59 +020041 /*
42 * Load Linux from its location in NOR flash to its defined
43 * location in SDRAM
44 */
Sean Andersonfab09a52023-10-14 16:47:38 -040045 header = (void *)CONFIG_SYS_OS_BASE;
York Sun79757252018-06-26 10:10:03 -070046#ifdef CONFIG_SPL_LOAD_FIT
47 if (image_get_magic(header) == FDT_MAGIC) {
Stefan Roese4ee4cf92020-04-21 09:28:44 +020048 int ret;
49
York Sun79757252018-06-26 10:10:03 -070050 debug("Found FIT\n");
51 load.bl_len = 1;
52 load.read = spl_nor_load_read;
Stefan Roese900c1772012-08-27 12:50:59 +020053
York Sun79757252018-06-26 10:10:03 -070054 ret = spl_load_simple_fit(spl_image, &load,
55 CONFIG_SYS_OS_BASE,
56 (void *)header);
57
Simon Glass259cdb42023-09-26 08:14:17 -060058#if defined CONFIG_SPL_PAYLOAD_ARGS_ADDR && defined CONFIG_CMD_SPL_NOR_OFS
59 memcpy((void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR,
Lukasz Majewski55ce92e2019-10-15 10:28:45 +020060 (void *)CONFIG_CMD_SPL_NOR_OFS,
61 CONFIG_CMD_SPL_WRITE_SIZE);
62#endif
York Sun79757252018-06-26 10:10:03 -070063 return ret;
64 }
65#endif
Masahiro Yamada225581b2015-01-08 19:23:35 +090066 if (image_get_os(header) == IH_OS_LINUX) {
67 /* happy - was a Linux */
Stefan Roese4ee4cf92020-04-21 09:28:44 +020068 int ret;
Stefan Roese900c1772012-08-27 12:50:59 +020069
Pali Rohárdda8f882022-01-14 14:31:38 +010070 ret = spl_parse_image_header(spl_image, bootdev, header);
Marek Vasut02266e22016-04-29 00:44:54 +020071 if (ret)
72 return ret;
Masahiro Yamada225581b2015-01-08 19:23:35 +090073
Simon Glassee306792016-09-24 18:20:13 -060074 memcpy((void *)spl_image->load_addr,
Masahiro Yamada225581b2015-01-08 19:23:35 +090075 (void *)(CONFIG_SYS_OS_BASE +
Simon Glassbb7d3bb2022-09-06 20:26:52 -060076 sizeof(struct legacy_img_hdr)),
Simon Glassee306792016-09-24 18:20:13 -060077 spl_image->size);
Simon Glass259cdb42023-09-26 08:14:17 -060078#ifdef CONFIG_SPL_PAYLOAD_ARGS_ADDR
79 spl_image->arg = (void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR;
York Sunf0370c02018-06-26 10:10:04 -070080#endif
Masahiro Yamada225581b2015-01-08 19:23:35 +090081
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020082 return 0;
Masahiro Yamada225581b2015-01-08 19:23:35 +090083 } else {
84 puts("The Expected Linux image was not found.\n"
85 "Please check your NOR configuration.\n"
86 "Trying to start u-boot now...\n");
87 }
Stefan Roese900c1772012-08-27 12:50:59 +020088 }
Masahiro Yamada225581b2015-01-08 19:23:35 +090089#endif
90
91 /*
92 * Load real U-Boot from its location in NOR flash to its
93 * defined location in SDRAM
94 */
Simon Glassbb7d3bb2022-09-06 20:26:52 -060095 header = (const struct legacy_img_hdr *)spl_nor_get_uboot_base();
Sean Andersonfab09a52023-10-14 16:47:38 -040096#ifdef CONFIG_SPL_LOAD_FIT
York Sun79757252018-06-26 10:10:03 -070097 if (image_get_magic(header) == FDT_MAGIC) {
98 debug("Found FIT format U-Boot\n");
99 load.bl_len = 1;
100 load.read = spl_nor_load_read;
Stefan Roese4ee4cf92020-04-21 09:28:44 +0200101 return spl_load_simple_fit(spl_image, &load,
102 spl_nor_get_uboot_base(),
103 (void *)header);
York Sun79757252018-06-26 10:10:03 -0700104 }
105#endif
Sean Anderson952ed672023-10-14 16:47:44 -0400106 if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER) &&
107 valid_container_hdr((void *)header)) {
Peng Fan3d52a6f2019-09-23 10:18:48 +0800108 load.bl_len = 1;
109 load.read = spl_nor_load_read;
110 return spl_load_imx_container(spl_image, &load,
111 spl_nor_get_uboot_base());
112 }
113
Stefan Roese84ae9d82020-04-21 09:28:43 +0200114 /* Legacy image handling */
Andrew Davis360ec6a2022-05-04 15:52:25 -0500115 if (IS_ENABLED(CONFIG_SPL_LEGACY_IMAGE_FORMAT)) {
Stefan Roese84ae9d82020-04-21 09:28:43 +0200116 load.bl_len = 1;
117 load.read = spl_nor_load_read;
Pali Rohárdda8f882022-01-14 14:31:38 +0100118 return spl_load_legacy_img(spl_image, bootdev, &load,
Roger Quadros237f0182022-09-29 13:11:28 +0300119 spl_nor_get_uboot_base(),
Sean Andersonfab09a52023-10-14 16:47:38 -0400120 header);
Stefan Roese84ae9d82020-04-21 09:28:43 +0200121 }
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200122
Mario Kicherer4d1ad122023-01-30 10:21:43 +0100123 return -EINVAL;
Stefan Roese900c1772012-08-27 12:50:59 +0200124}
Simon Glass4fc1f252016-11-30 15:30:50 -0700125SPL_LOAD_IMAGE_METHOD("NOR", 0, BOOT_DEVICE_NOR, spl_nor_load_image);