efi_loader: remove superfluous check in efi_setup_loaded_image()
[oweals/u-boot.git] / common / spl / spl_mmc.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2010
4  * Texas Instruments, <www.ti.com>
5  *
6  * Aneesh V <aneesh@ti.com>
7  */
8 #include <common.h>
9 #include <dm.h>
10 #include <spl.h>
11 #include <linux/compiler.h>
12 #include <errno.h>
13 #include <asm/u-boot.h>
14 #include <errno.h>
15 #include <mmc.h>
16 #include <image.h>
17
18 static int mmc_load_legacy(struct spl_image_info *spl_image, struct mmc *mmc,
19                            ulong sector, struct image_header *header)
20 {
21         u32 image_size_sectors;
22         unsigned long count;
23         int ret;
24
25         ret = spl_parse_image_header(spl_image, header);
26         if (ret)
27                 return ret;
28
29         /* convert size to sectors - round up */
30         image_size_sectors = (spl_image->size + mmc->read_bl_len - 1) /
31                              mmc->read_bl_len;
32
33         /* Read the header too to avoid extra memcpy */
34         count = blk_dread(mmc_get_blk_desc(mmc), sector, image_size_sectors,
35                           (void *)(ulong)spl_image->load_addr);
36         debug("read %x sectors to %lx\n", image_size_sectors,
37               spl_image->load_addr);
38         if (count != image_size_sectors)
39                 return -EIO;
40
41         return 0;
42 }
43
44 static ulong h_spl_load_read(struct spl_load_info *load, ulong sector,
45                              ulong count, void *buf)
46 {
47         struct mmc *mmc = load->dev;
48
49         return blk_dread(mmc_get_blk_desc(mmc), sector, count, buf);
50 }
51
52 static __maybe_unused
53 int mmc_load_image_raw_sector(struct spl_image_info *spl_image,
54                               struct mmc *mmc, unsigned long sector)
55 {
56         unsigned long count;
57         struct image_header *header;
58         struct blk_desc *bd = mmc_get_blk_desc(mmc);
59         int ret = 0;
60
61         header = spl_get_load_buffer(-sizeof(*header), bd->blksz);
62
63         /* read image header to find the image size & load address */
64         count = blk_dread(bd, sector, 1, header);
65         debug("hdr read sector %lx, count=%lu\n", sector, count);
66         if (count == 0) {
67                 ret = -EIO;
68                 goto end;
69         }
70
71         if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
72             image_get_magic(header) == FDT_MAGIC) {
73                 struct spl_load_info load;
74
75                 debug("Found FIT\n");
76                 load.dev = mmc;
77                 load.priv = NULL;
78                 load.filename = NULL;
79                 load.bl_len = mmc->read_bl_len;
80                 load.read = h_spl_load_read;
81                 ret = spl_load_simple_fit(spl_image, &load, sector, header);
82         } else {
83                 ret = mmc_load_legacy(spl_image, mmc, sector, header);
84         }
85
86 end:
87         if (ret) {
88 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
89                 puts("mmc_load_image_raw_sector: mmc block read error\n");
90 #endif
91                 return -1;
92         }
93
94         return 0;
95 }
96
97 static int spl_mmc_get_device_index(u32 boot_device)
98 {
99         switch (boot_device) {
100         case BOOT_DEVICE_MMC1:
101                 return 0;
102         case BOOT_DEVICE_MMC2:
103         case BOOT_DEVICE_MMC2_2:
104                 return 1;
105         }
106
107 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
108         printf("spl: unsupported mmc boot device.\n");
109 #endif
110
111         return -ENODEV;
112 }
113
114 static int spl_mmc_find_device(struct mmc **mmcp, u32 boot_device)
115 {
116 #if CONFIG_IS_ENABLED(DM_MMC)
117         struct udevice *dev;
118 #endif
119         int err, mmc_dev;
120
121         mmc_dev = spl_mmc_get_device_index(boot_device);
122         if (mmc_dev < 0)
123                 return mmc_dev;
124
125         err = mmc_initialize(NULL);
126         if (err) {
127 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
128                 printf("spl: could not initialize mmc. error: %d\n", err);
129 #endif
130                 return err;
131         }
132
133 #if CONFIG_IS_ENABLED(DM_MMC)
134         err = uclass_get_device(UCLASS_MMC, mmc_dev, &dev);
135         if (!err)
136                 *mmcp = mmc_get_mmc_dev(dev);
137 #else
138         *mmcp = find_mmc_device(mmc_dev);
139         err = *mmcp ? 0 : -ENODEV;
140 #endif
141         if (err) {
142 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
143                 printf("spl: could not find mmc device %d. error: %d\n",
144                        mmc_dev, err);
145 #endif
146                 return err;
147         }
148
149         return 0;
150 }
151
152 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
153 static int mmc_load_image_raw_partition(struct spl_image_info *spl_image,
154                                         struct mmc *mmc, int partition)
155 {
156         disk_partition_t info;
157         int err;
158
159 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION_TYPE
160         int type_part;
161         /* Only support MBR so DOS_ENTRY_NUMBERS */
162         for (type_part = 1; type_part <= DOS_ENTRY_NUMBERS; type_part++) {
163                 err = part_get_info(mmc_get_blk_desc(mmc), type_part, &info);
164                 if (err)
165                         continue;
166                 if (info.sys_ind == 
167                         CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION_TYPE) {
168                         partition = type_part;
169                         break;
170                 }
171         }
172 #endif
173
174         err = part_get_info(mmc_get_blk_desc(mmc), partition, &info);
175         if (err) {
176 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
177                 puts("spl: partition error\n");
178 #endif
179                 return -1;
180         }
181
182 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
183         return mmc_load_image_raw_sector(spl_image, mmc,
184                         info.start + CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
185 #else
186         return mmc_load_image_raw_sector(spl_image, mmc, info.start);
187 #endif
188 }
189 #endif
190
191 #ifdef CONFIG_SPL_OS_BOOT
192 static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
193                                  struct mmc *mmc)
194 {
195         int ret;
196
197 #if defined(CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR)
198         unsigned long count;
199
200         count = blk_dread(mmc_get_blk_desc(mmc),
201                 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
202                 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
203                 (void *) CONFIG_SYS_SPL_ARGS_ADDR);
204         if (count != CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS) {
205 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
206                 puts("mmc_load_image_raw_os: mmc block read error\n");
207 #endif
208                 return -1;
209         }
210 #endif  /* CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR */
211
212         ret = mmc_load_image_raw_sector(spl_image, mmc,
213                 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
214         if (ret)
215                 return ret;
216
217         if (spl_image->os != IH_OS_LINUX) {
218                 puts("Expected Linux image is not found. Trying to start U-boot\n");
219                 return -ENOENT;
220         }
221
222         return 0;
223 }
224 #else
225 int spl_start_uboot(void)
226 {
227         return 1;
228 }
229 static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
230                                  struct mmc *mmc)
231 {
232         return -ENOSYS;
233 }
234 #endif
235
236 #ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
237 static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image, struct mmc *mmc)
238 {
239         int err = -ENOSYS;
240
241 #ifdef CONFIG_SPL_FS_FAT
242         if (!spl_start_uboot()) {
243                 err = spl_load_image_fat_os(spl_image, mmc_get_blk_desc(mmc),
244                         CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
245                 if (!err)
246                         return err;
247         }
248 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
249         err = spl_load_image_fat(spl_image, mmc_get_blk_desc(mmc),
250                                  CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
251                                  CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
252         if (!err)
253                 return err;
254 #endif
255 #endif
256 #ifdef CONFIG_SPL_FS_EXT4
257         if (!spl_start_uboot()) {
258                 err = spl_load_image_ext_os(spl_image, mmc_get_blk_desc(mmc),
259                         CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
260                 if (!err)
261                         return err;
262         }
263 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
264         err = spl_load_image_ext(spl_image, mmc_get_blk_desc(mmc),
265                                  CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
266                                  CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
267         if (!err)
268                 return err;
269 #endif
270 #endif
271
272 #if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
273         err = -ENOENT;
274 #endif
275
276         return err;
277 }
278 #else
279 static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image, struct mmc *mmc)
280 {
281         return -ENOSYS;
282 }
283 #endif
284
285 u32 __weak spl_boot_mode(const u32 boot_device)
286 {
287 #if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
288         return MMCSD_MODE_FS;
289 #elif defined(CONFIG_SUPPORT_EMMC_BOOT)
290         return MMCSD_MODE_EMMCBOOT;
291 #else
292         return MMCSD_MODE_RAW;
293 #endif
294 }
295
296 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
297 __weak
298 int spl_boot_partition(const u32 boot_device)
299 {
300         return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION;
301 }
302 #endif
303
304 int spl_mmc_load_image(struct spl_image_info *spl_image,
305                        struct spl_boot_device *bootdev)
306 {
307         struct mmc *mmc = NULL;
308         u32 boot_mode;
309         int err = 0;
310         __maybe_unused int part;
311
312         err = spl_mmc_find_device(&mmc, bootdev->boot_device);
313         if (err)
314                 return err;
315
316         err = mmc_init(mmc);
317         if (err) {
318 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
319                 printf("spl: mmc init failed with error: %d\n", err);
320 #endif
321                 return err;
322         }
323
324         boot_mode = spl_boot_mode(bootdev->boot_device);
325         err = -EINVAL;
326         switch (boot_mode) {
327         case MMCSD_MODE_EMMCBOOT:
328                         /*
329                          * We need to check what the partition is configured to.
330                          * 1 and 2 match up to boot0 / boot1 and 7 is user data
331                          * which is the first physical partition (0).
332                          */
333                         part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
334
335                         if (part == 7)
336                                 part = 0;
337
338                         if (CONFIG_IS_ENABLED(MMC_TINY))
339                                 err = mmc_switch_part(mmc, part);
340                         else
341                                 err = blk_dselect_hwpart(mmc_get_blk_desc(mmc), part);
342
343                         if (err) {
344 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
345                                 puts("spl: mmc partition switch failed\n");
346 #endif
347                                 return err;
348                         }
349                         /* Fall through */
350         case MMCSD_MODE_RAW:
351                 debug("spl: mmc boot mode: raw\n");
352
353                 if (!spl_start_uboot()) {
354                         err = mmc_load_image_raw_os(spl_image, mmc);
355                         if (!err)
356                                 return err;
357                 }
358 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
359                 err = spl_boot_partition(bootdev->boot_device);
360                 if (!err)
361                         return err;
362
363                 err = mmc_load_image_raw_partition(spl_image, mmc, err);
364                 if (!err)
365                         return err;
366 #endif
367 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
368                 err = mmc_load_image_raw_sector(spl_image, mmc,
369                         CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
370                 if (!err)
371                         return err;
372 #endif
373                 /* If RAW mode fails, try FS mode. */
374         case MMCSD_MODE_FS:
375                 debug("spl: mmc boot mode: fs\n");
376
377                 err = spl_mmc_do_fs_boot(spl_image, mmc);
378                 if (!err)
379                         return err;
380
381                 break;
382 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
383         default:
384                 puts("spl: mmc: wrong boot mode\n");
385 #endif
386         }
387
388         return err;
389 }
390
391 SPL_LOAD_IMAGE_METHOD("MMC1", 0, BOOT_DEVICE_MMC1, spl_mmc_load_image);
392 SPL_LOAD_IMAGE_METHOD("MMC2", 0, BOOT_DEVICE_MMC2, spl_mmc_load_image);
393 SPL_LOAD_IMAGE_METHOD("MMC2_2", 0, BOOT_DEVICE_MMC2_2, spl_mmc_load_image);