spl: nor: introduce spl_nor_get_uboot_base
[platform/kernel/u-boot.git] / common / spl / spl_nor.c
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 <spl.h>
8
9 #ifdef CONFIG_SPL_LOAD_FIT
10 static ulong spl_nor_load_read(struct spl_load_info *load, ulong sector,
11                                ulong count, void *buf)
12 {
13         debug("%s: sector %lx, count %lx, buf %p\n",
14               __func__, sector, count, buf);
15         memcpy(buf, (void *)sector, count);
16
17         return count;
18 }
19 #endif
20
21 unsigned long __weak spl_nor_get_uboot_base(void)
22 {
23         return CONFIG_SYS_UBOOT_BASE;
24 }
25
26 static int spl_nor_load_image(struct spl_image_info *spl_image,
27                               struct spl_boot_device *bootdev)
28 {
29         int ret;
30         __maybe_unused const struct image_header *header;
31         __maybe_unused struct spl_load_info load;
32
33         /*
34          * Loading of the payload to SDRAM is done with skipping of
35          * the mkimage header in this SPL NOR driver
36          */
37         spl_image->flags |= SPL_COPY_PAYLOAD_ONLY;
38
39 #ifdef CONFIG_SPL_OS_BOOT
40         if (!spl_start_uboot()) {
41                 /*
42                  * Load Linux from its location in NOR flash to its defined
43                  * location in SDRAM
44                  */
45                 header = (const struct image_header *)CONFIG_SYS_OS_BASE;
46 #ifdef CONFIG_SPL_LOAD_FIT
47                 if (image_get_magic(header) == FDT_MAGIC) {
48                         debug("Found FIT\n");
49                         load.bl_len = 1;
50                         load.read = spl_nor_load_read;
51
52                         ret = spl_load_simple_fit(spl_image, &load,
53                                                   CONFIG_SYS_OS_BASE,
54                                                   (void *)header);
55
56                         return ret;
57                 }
58 #endif
59                 if (image_get_os(header) == IH_OS_LINUX) {
60                         /* happy - was a Linux */
61
62                         ret = spl_parse_image_header(spl_image, header);
63                         if (ret)
64                                 return ret;
65
66                         memcpy((void *)spl_image->load_addr,
67                                (void *)(CONFIG_SYS_OS_BASE +
68                                         sizeof(struct image_header)),
69                                spl_image->size);
70 #ifdef CONFIG_SYS_FDT_BASE
71                         spl_image->arg = (void *)CONFIG_SYS_FDT_BASE;
72 #endif
73
74                         return 0;
75                 } else {
76                         puts("The Expected Linux image was not found.\n"
77                              "Please check your NOR configuration.\n"
78                              "Trying to start u-boot now...\n");
79                 }
80         }
81 #endif
82
83         /*
84          * Load real U-Boot from its location in NOR flash to its
85          * defined location in SDRAM
86          */
87 #ifdef CONFIG_SPL_LOAD_FIT
88         header = (const struct image_header *)spl_nor_get_uboot_base();
89         if (image_get_magic(header) == FDT_MAGIC) {
90                 debug("Found FIT format U-Boot\n");
91                 load.bl_len = 1;
92                 load.read = spl_nor_load_read;
93                 ret = spl_load_simple_fit(spl_image, &load,
94                                           spl_nor_get_uboot_base(),
95                                           (void *)header);
96
97                 return ret;
98         }
99 #endif
100         ret = spl_parse_image_header(spl_image,
101                         (const struct image_header *)spl_nor_get_uboot_base());
102         if (ret)
103                 return ret;
104
105         memcpy((void *)(unsigned long)spl_image->load_addr,
106                (void *)(spl_nor_get_uboot_base() + sizeof(struct image_header)),
107                spl_image->size);
108
109         return 0;
110 }
111 SPL_LOAD_IMAGE_METHOD("NOR", 0, BOOT_DEVICE_NOR, spl_nor_load_image);