common: Drop image.h from common header
[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 <image.h>
8 #include <spl.h>
9
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
20 unsigned long __weak spl_nor_get_uboot_base(void)
21 {
22         return CONFIG_SYS_UBOOT_BASE;
23 }
24
25 static int spl_nor_load_image(struct spl_image_info *spl_image,
26                               struct spl_boot_device *bootdev)
27 {
28         __maybe_unused const struct image_header *header;
29         __maybe_unused struct spl_load_info load;
30
31         /*
32          * Loading of the payload to SDRAM is done with skipping of
33          * the mkimage header in this SPL NOR driver
34          */
35         spl_image->flags |= SPL_COPY_PAYLOAD_ONLY;
36
37 #ifdef CONFIG_SPL_OS_BOOT
38         if (!spl_start_uboot()) {
39                 /*
40                  * Load Linux from its location in NOR flash to its defined
41                  * location in SDRAM
42                  */
43                 header = (const struct image_header *)CONFIG_SYS_OS_BASE;
44 #ifdef CONFIG_SPL_LOAD_FIT
45                 if (image_get_magic(header) == FDT_MAGIC) {
46                         int ret;
47
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 #if defined CONFIG_SYS_SPL_ARGS_ADDR && defined CONFIG_CMD_SPL_NOR_OFS
57                         memcpy((void *)CONFIG_SYS_SPL_ARGS_ADDR,
58                                (void *)CONFIG_CMD_SPL_NOR_OFS,
59                                CONFIG_CMD_SPL_WRITE_SIZE);
60 #endif
61                         return ret;
62                 }
63 #endif
64                 if (image_get_os(header) == IH_OS_LINUX) {
65                         /* happy - was a Linux */
66                         int ret;
67
68                         ret = spl_parse_image_header(spl_image, header);
69                         if (ret)
70                                 return ret;
71
72                         memcpy((void *)spl_image->load_addr,
73                                (void *)(CONFIG_SYS_OS_BASE +
74                                         sizeof(struct image_header)),
75                                spl_image->size);
76 #ifdef CONFIG_SYS_FDT_BASE
77                         spl_image->arg = (void *)CONFIG_SYS_FDT_BASE;
78 #endif
79
80                         return 0;
81                 } else {
82                         puts("The Expected Linux image was not found.\n"
83                              "Please check your NOR configuration.\n"
84                              "Trying to start u-boot now...\n");
85                 }
86         }
87 #endif
88
89         /*
90          * Load real U-Boot from its location in NOR flash to its
91          * defined location in SDRAM
92          */
93 #ifdef CONFIG_SPL_LOAD_FIT
94         header = (const struct image_header *)spl_nor_get_uboot_base();
95         if (image_get_magic(header) == FDT_MAGIC) {
96                 debug("Found FIT format U-Boot\n");
97                 load.bl_len = 1;
98                 load.read = spl_nor_load_read;
99                 return spl_load_simple_fit(spl_image, &load,
100                                            spl_nor_get_uboot_base(),
101                                            (void *)header);
102         }
103 #endif
104         if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) {
105                 load.bl_len = 1;
106                 load.read = spl_nor_load_read;
107                 return spl_load_imx_container(spl_image, &load,
108                                               spl_nor_get_uboot_base());
109         }
110
111         /* Legacy image handling */
112         if (IS_ENABLED(CONFIG_SPL_LEGACY_IMAGE_SUPPORT)) {
113                 load.bl_len = 1;
114                 load.read = spl_nor_load_read;
115                 return spl_load_legacy_img(spl_image, &load,
116                                            spl_nor_get_uboot_base());
117         }
118
119         return 0;
120 }
121 SPL_LOAD_IMAGE_METHOD("NOR", 0, BOOT_DEVICE_NOR, spl_nor_load_image);