blob: 236b07182833a88a150823f8cfd1ab4dd93ce3bb [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stefan Roese33d34642012-08-27 12:50:59 +02002/*
3 * Copyright (C) 2012 Stefan Roese <sr@denx.de>
Stefan Roese33d34642012-08-27 12:50:59 +02004 */
5
6#include <common.h>
Simon Glass4d72caa2020-05-10 11:40:01 -06007#include <image.h>
Sean Andersonab121792023-10-14 16:47:44 -04008#include <imx_container.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -06009#include <log.h>
Sean Andersonb02c4e92023-10-14 16:47:55 -040010#include <mapmem.h>
Stefan Roese33d34642012-08-27 12:50:59 +020011#include <spl.h>
12
York Sun6ecd8202018-06-26 10:10:03 -070013static ulong spl_nor_load_read(struct spl_load_info *load, ulong sector,
14 ulong count, void *buf)
15{
16 debug("%s: sector %lx, count %lx, buf %p\n",
17 __func__, sector, count, buf);
Sean Andersonb02c4e92023-10-14 16:47:55 -040018 memcpy(buf, map_sysmem(sector, count), count);
York Sun6ecd8202018-06-26 10:10:03 -070019
20 return count;
21}
York Sun6ecd8202018-06-26 10:10:03 -070022
Peng Fan07d900a2019-09-23 10:18:42 +080023unsigned long __weak spl_nor_get_uboot_base(void)
24{
Tom Rini65cc0e22022-11-16 13:10:41 -050025 return CFG_SYS_UBOOT_BASE;
Peng Fan07d900a2019-09-23 10:18:42 +080026}
27
Simon Glass2a2ee2a2016-09-24 18:20:13 -060028static int spl_nor_load_image(struct spl_image_info *spl_image,
29 struct spl_boot_device *bootdev)
Stefan Roese33d34642012-08-27 12:50:59 +020030{
Sean Anderson6cc21822023-10-14 16:47:38 -040031 struct legacy_img_hdr *header;
York Sun6ecd8202018-06-26 10:10:03 -070032 __maybe_unused struct spl_load_info load;
33
Stefan Roese33d34642012-08-27 12:50:59 +020034 /*
35 * Loading of the payload to SDRAM is done with skipping of
36 * the mkimage header in this SPL NOR driver
37 */
Simon Glass2a2ee2a2016-09-24 18:20:13 -060038 spl_image->flags |= SPL_COPY_PAYLOAD_ONLY;
Stefan Roese33d34642012-08-27 12:50:59 +020039
Tom Rini71150072021-10-30 23:03:48 -040040#if CONFIG_IS_ENABLED(OS_BOOT)
Masahiro Yamada9f9d8702015-01-08 19:23:35 +090041 if (!spl_start_uboot()) {
Stefan Roese33d34642012-08-27 12:50:59 +020042 /*
43 * Load Linux from its location in NOR flash to its defined
44 * location in SDRAM
45 */
Sean Anderson6cc21822023-10-14 16:47:38 -040046 header = (void *)CONFIG_SYS_OS_BASE;
York Sun6ecd8202018-06-26 10:10:03 -070047#ifdef CONFIG_SPL_LOAD_FIT
48 if (image_get_magic(header) == FDT_MAGIC) {
Stefan Roesef1b0f152020-04-21 09:28:44 +020049 int ret;
50
York Sun6ecd8202018-06-26 10:10:03 -070051 debug("Found FIT\n");
52 load.bl_len = 1;
53 load.read = spl_nor_load_read;
Stefan Roese33d34642012-08-27 12:50:59 +020054
York Sun6ecd8202018-06-26 10:10:03 -070055 ret = spl_load_simple_fit(spl_image, &load,
56 CONFIG_SYS_OS_BASE,
57 (void *)header);
58
Simon Glass9cbdc3a2023-09-26 08:14:17 -060059#if defined CONFIG_SPL_PAYLOAD_ARGS_ADDR && defined CONFIG_CMD_SPL_NOR_OFS
60 memcpy((void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR,
Lukasz Majewskia4a16c92019-10-15 10:28:45 +020061 (void *)CONFIG_CMD_SPL_NOR_OFS,
62 CONFIG_CMD_SPL_WRITE_SIZE);
63#endif
York Sun6ecd8202018-06-26 10:10:03 -070064 return ret;
65 }
66#endif
Masahiro Yamada9f9d8702015-01-08 19:23:35 +090067 if (image_get_os(header) == IH_OS_LINUX) {
68 /* happy - was a Linux */
Stefan Roesef1b0f152020-04-21 09:28:44 +020069 int ret;
Stefan Roese33d34642012-08-27 12:50:59 +020070
Pali Rohár2e0429b2022-01-14 14:31:38 +010071 ret = spl_parse_image_header(spl_image, bootdev, header);
Marek Vasut7e0f2262016-04-29 00:44:54 +020072 if (ret)
73 return ret;
Masahiro Yamada9f9d8702015-01-08 19:23:35 +090074
Simon Glass2a2ee2a2016-09-24 18:20:13 -060075 memcpy((void *)spl_image->load_addr,
Masahiro Yamada9f9d8702015-01-08 19:23:35 +090076 (void *)(CONFIG_SYS_OS_BASE +
Simon Glassf3543e62022-09-06 20:26:52 -060077 sizeof(struct legacy_img_hdr)),
Simon Glass2a2ee2a2016-09-24 18:20:13 -060078 spl_image->size);
Simon Glass9cbdc3a2023-09-26 08:14:17 -060079#ifdef CONFIG_SPL_PAYLOAD_ARGS_ADDR
80 spl_image->arg = (void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR;
York Sun14acea02018-06-26 10:10:04 -070081#endif
Masahiro Yamada9f9d8702015-01-08 19:23:35 +090082
Nikita Kiryanov36afd452015-11-08 17:11:49 +020083 return 0;
Masahiro Yamada9f9d8702015-01-08 19:23:35 +090084 } else {
85 puts("The Expected Linux image was not found.\n"
86 "Please check your NOR configuration.\n"
87 "Trying to start u-boot now...\n");
88 }
Stefan Roese33d34642012-08-27 12:50:59 +020089 }
Masahiro Yamada9f9d8702015-01-08 19:23:35 +090090#endif
91
92 /*
93 * Load real U-Boot from its location in NOR flash to its
94 * defined location in SDRAM
95 */
Sean Andersonb02c4e92023-10-14 16:47:55 -040096 header = map_sysmem(spl_nor_get_uboot_base(), sizeof(*header));
Sean Anderson6cc21822023-10-14 16:47:38 -040097#ifdef CONFIG_SPL_LOAD_FIT
York Sun6ecd8202018-06-26 10:10:03 -070098 if (image_get_magic(header) == FDT_MAGIC) {
99 debug("Found FIT format U-Boot\n");
100 load.bl_len = 1;
101 load.read = spl_nor_load_read;
Stefan Roesef1b0f152020-04-21 09:28:44 +0200102 return spl_load_simple_fit(spl_image, &load,
103 spl_nor_get_uboot_base(),
104 (void *)header);
York Sun6ecd8202018-06-26 10:10:03 -0700105 }
106#endif
Sean Andersonab121792023-10-14 16:47:44 -0400107 if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER) &&
108 valid_container_hdr((void *)header)) {
Peng Fan3bc4f1d2019-09-23 10:18:48 +0800109 load.bl_len = 1;
110 load.read = spl_nor_load_read;
111 return spl_load_imx_container(spl_image, &load,
112 spl_nor_get_uboot_base());
113 }
114
Stefan Roese2fc91ed2020-04-21 09:28:43 +0200115 /* Legacy image handling */
Andrew Davis6665ab12022-05-04 15:52:25 -0500116 if (IS_ENABLED(CONFIG_SPL_LEGACY_IMAGE_FORMAT)) {
Stefan Roese2fc91ed2020-04-21 09:28:43 +0200117 load.bl_len = 1;
118 load.read = spl_nor_load_read;
Pali Rohár2e0429b2022-01-14 14:31:38 +0100119 return spl_load_legacy_img(spl_image, bootdev, &load,
Roger Quadros06377c52022-09-29 13:11:28 +0300120 spl_nor_get_uboot_base(),
Sean Anderson6cc21822023-10-14 16:47:38 -0400121 header);
Stefan Roese2fc91ed2020-04-21 09:28:43 +0200122 }
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200123
Mario Kicherer1aeedef2023-01-30 10:21:43 +0100124 return -EINVAL;
Stefan Roese33d34642012-08-27 12:50:59 +0200125}
Simon Glassebc4ef62016-11-30 15:30:50 -0700126SPL_LOAD_IMAGE_METHOD("NOR", 0, BOOT_DEVICE_NOR, spl_nor_load_image);