1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (C) 2012 Stefan Roese <sr@denx.de>
4 */
5
6 #include <common.h>
7 #include <image.h>
8 #include <log.h>
9 #include <spl.h>
10
spl_nor_load_read(struct spl_load_info * load,ulong sector,ulong count,void * buf)11 static 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 }
20
spl_nor_get_uboot_base(void)21 unsigned long __weak spl_nor_get_uboot_base(void)
22 {
23 return CFG_SYS_UBOOT_BASE;
24 }
25
spl_nor_load_image(struct spl_image_info * spl_image,struct spl_boot_device * bootdev)26 static int spl_nor_load_image(struct spl_image_info *spl_image,
27 struct spl_boot_device *bootdev)
28 {
29 __maybe_unused const struct legacy_img_hdr *header;
30 __maybe_unused struct spl_load_info load;
31
32 /*
33 * Loading of the payload to SDRAM is done with skipping of
34 * the mkimage header in this SPL NOR driver
35 */
36 spl_image->flags |= SPL_COPY_PAYLOAD_ONLY;
37
38 #if CONFIG_IS_ENABLED(OS_BOOT)
39 if (!spl_start_uboot()) {
40 /*
41 * Load Linux from its location in NOR flash to its defined
42 * location in SDRAM
43 */
44 header = (const struct legacy_img_hdr *)CONFIG_SYS_OS_BASE;
45 #ifdef CONFIG_SPL_LOAD_FIT
46 if (image_get_magic(header) == FDT_MAGIC) {
47 int ret;
48
49 debug("Found FIT\n");
50 load.bl_len = 1;
51 load.read = spl_nor_load_read;
52
53 ret = spl_load_simple_fit(spl_image, &load,
54 CONFIG_SYS_OS_BASE,
55 (void *)header);
56
57 #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
62 return ret;
63 }
64 #endif
65 if (image_get_os(header) == IH_OS_LINUX) {
66 /* happy - was a Linux */
67 int ret;
68
69 ret = spl_parse_image_header(spl_image, bootdev, header);
70 if (ret)
71 return ret;
72
73 memcpy((void *)spl_image->load_addr,
74 (void *)(CONFIG_SYS_OS_BASE +
75 sizeof(struct legacy_img_hdr)),
76 spl_image->size);
77 #ifdef CONFIG_SYS_SPL_ARGS_ADDR
78 spl_image->arg = (void *)CONFIG_SYS_SPL_ARGS_ADDR;
79 #endif
80
81 return 0;
82 } 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 }
87 }
88 #endif
89
90 /*
91 * Load real U-Boot from its location in NOR flash to its
92 * defined location in SDRAM
93 */
94 #ifdef CONFIG_SPL_LOAD_FIT
95 header = (const struct legacy_img_hdr *)spl_nor_get_uboot_base();
96 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;
100 return spl_load_simple_fit(spl_image, &load,
101 spl_nor_get_uboot_base(),
102 (void *)header);
103 }
104 #endif
105 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
112 /* Legacy image handling */
113 if (IS_ENABLED(CONFIG_SPL_LEGACY_IMAGE_FORMAT)) {
114 struct legacy_img_hdr hdr;
115
116 load.bl_len = 1;
117 load.read = spl_nor_load_read;
118 spl_nor_load_read(&load, spl_nor_get_uboot_base(), sizeof(hdr), &hdr);
119 return spl_load_legacy_img(spl_image, bootdev, &load,
120 spl_nor_get_uboot_base(),
121 &hdr);
122 }
123
124 return 0;
125 }
126 SPL_LOAD_IMAGE_METHOD("NOR", 0, BOOT_DEVICE_NOR, spl_nor_load_image);
127