blob: b1e79b9ded6258f44070e4e4d64bcddd8ba184a3 [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>
7#include <spl.h>
8
York Sun79757252018-06-26 10:10:03 -07009static ulong spl_nor_load_read(struct spl_load_info *load, ulong sector,
10 ulong count, void *buf)
11{
12 debug("%s: sector %lx, count %lx, buf %p\n",
13 __func__, sector, count, buf);
14 memcpy(buf, (void *)sector, count);
15
16 return count;
17}
York Sun79757252018-06-26 10:10:03 -070018
Peng Fan6e773f92019-09-23 10:18:42 +080019unsigned long __weak spl_nor_get_uboot_base(void)
20{
21 return CONFIG_SYS_UBOOT_BASE;
22}
23
Simon Glassee306792016-09-24 18:20:13 -060024static int spl_nor_load_image(struct spl_image_info *spl_image,
25 struct spl_boot_device *bootdev)
Stefan Roese900c1772012-08-27 12:50:59 +020026{
Marek Vasut02266e22016-04-29 00:44:54 +020027 int ret;
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) {
46 debug("Found FIT\n");
47 load.bl_len = 1;
48 load.read = spl_nor_load_read;
Stefan Roese900c1772012-08-27 12:50:59 +020049
York Sun79757252018-06-26 10:10:03 -070050 ret = spl_load_simple_fit(spl_image, &load,
51 CONFIG_SYS_OS_BASE,
52 (void *)header);
53
Lukasz Majewski55ce92e2019-10-15 10:28:45 +020054#if defined CONFIG_SYS_SPL_ARGS_ADDR && defined CONFIG_CMD_SPL_NOR_OFS
55 memcpy((void *)CONFIG_SYS_SPL_ARGS_ADDR,
56 (void *)CONFIG_CMD_SPL_NOR_OFS,
57 CONFIG_CMD_SPL_WRITE_SIZE);
58#endif
York Sun79757252018-06-26 10:10:03 -070059 return ret;
60 }
61#endif
Masahiro Yamada225581b2015-01-08 19:23:35 +090062 if (image_get_os(header) == IH_OS_LINUX) {
63 /* happy - was a Linux */
Stefan Roese900c1772012-08-27 12:50:59 +020064
Simon Glassee306792016-09-24 18:20:13 -060065 ret = spl_parse_image_header(spl_image, header);
Marek Vasut02266e22016-04-29 00:44:54 +020066 if (ret)
67 return ret;
Masahiro Yamada225581b2015-01-08 19:23:35 +090068
Simon Glassee306792016-09-24 18:20:13 -060069 memcpy((void *)spl_image->load_addr,
Masahiro Yamada225581b2015-01-08 19:23:35 +090070 (void *)(CONFIG_SYS_OS_BASE +
71 sizeof(struct image_header)),
Simon Glassee306792016-09-24 18:20:13 -060072 spl_image->size);
York Sunf0370c02018-06-26 10:10:04 -070073#ifdef CONFIG_SYS_FDT_BASE
Vikas Manocha62b021c2017-04-07 15:38:13 -070074 spl_image->arg = (void *)CONFIG_SYS_FDT_BASE;
York Sunf0370c02018-06-26 10:10:04 -070075#endif
Masahiro Yamada225581b2015-01-08 19:23:35 +090076
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020077 return 0;
Masahiro Yamada225581b2015-01-08 19:23:35 +090078 } else {
79 puts("The Expected Linux image was not found.\n"
80 "Please check your NOR configuration.\n"
81 "Trying to start u-boot now...\n");
82 }
Stefan Roese900c1772012-08-27 12:50:59 +020083 }
Masahiro Yamada225581b2015-01-08 19:23:35 +090084#endif
85
86 /*
87 * Load real U-Boot from its location in NOR flash to its
88 * defined location in SDRAM
89 */
York Sun79757252018-06-26 10:10:03 -070090#ifdef CONFIG_SPL_LOAD_FIT
Peng Fan6e773f92019-09-23 10:18:42 +080091 header = (const struct image_header *)spl_nor_get_uboot_base();
York Sun79757252018-06-26 10:10:03 -070092 if (image_get_magic(header) == FDT_MAGIC) {
93 debug("Found FIT format U-Boot\n");
94 load.bl_len = 1;
95 load.read = spl_nor_load_read;
96 ret = spl_load_simple_fit(spl_image, &load,
Peng Fan6e773f92019-09-23 10:18:42 +080097 spl_nor_get_uboot_base(),
York Sun79757252018-06-26 10:10:03 -070098 (void *)header);
99
100 return ret;
101 }
102#endif
Peng Fan3d52a6f2019-09-23 10:18:48 +0800103 if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) {
104 load.bl_len = 1;
105 load.read = spl_nor_load_read;
106 return spl_load_imx_container(spl_image, &load,
107 spl_nor_get_uboot_base());
108 }
109
Simon Glassee306792016-09-24 18:20:13 -0600110 ret = spl_parse_image_header(spl_image,
Peng Fan6e773f92019-09-23 10:18:42 +0800111 (const struct image_header *)spl_nor_get_uboot_base());
Marek Vasut02266e22016-04-29 00:44:54 +0200112 if (ret)
113 return ret;
Masahiro Yamada225581b2015-01-08 19:23:35 +0900114
Simon Glassee306792016-09-24 18:20:13 -0600115 memcpy((void *)(unsigned long)spl_image->load_addr,
Peng Fan6e773f92019-09-23 10:18:42 +0800116 (void *)(spl_nor_get_uboot_base() + sizeof(struct image_header)),
Simon Glassee306792016-09-24 18:20:13 -0600117 spl_image->size);
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);