Merge tag 'tpm-030822' of https://source.denx.de/u-boot/custodians/u-boot-tpm
[platform/kernel/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 <log.h>
11 #include <part.h>
12 #include <spl.h>
13 #include <linux/compiler.h>
14 #include <errno.h>
15 #include <asm/u-boot.h>
16 #include <errno.h>
17 #include <mmc.h>
18 #include <image.h>
19
20 static int mmc_load_legacy(struct spl_image_info *spl_image,
21                            struct spl_boot_device *bootdev,
22                            struct mmc *mmc,
23                            ulong sector, struct image_header *header)
24 {
25         u32 image_offset_sectors;
26         u32 image_size_sectors;
27         unsigned long count;
28         u32 image_offset;
29         int ret;
30
31         ret = spl_parse_image_header(spl_image, bootdev, header);
32         if (ret)
33                 return ret;
34
35         /* convert offset to sectors - round down */
36         image_offset_sectors = spl_image->offset / mmc->read_bl_len;
37         /* calculate remaining offset */
38         image_offset = spl_image->offset % mmc->read_bl_len;
39
40         /* convert size to sectors - round up */
41         image_size_sectors = (spl_image->size + mmc->read_bl_len - 1) /
42                              mmc->read_bl_len;
43
44         /* Read the header too to avoid extra memcpy */
45         count = blk_dread(mmc_get_blk_desc(mmc),
46                           sector + image_offset_sectors,
47                           image_size_sectors,
48                           (void *)(ulong)spl_image->load_addr);
49         debug("read %x sectors to %lx\n", image_size_sectors,
50               spl_image->load_addr);
51         if (count != image_size_sectors)
52                 return -EIO;
53
54         if (image_offset)
55                 memmove((void *)(ulong)spl_image->load_addr,
56                         (void *)(ulong)spl_image->load_addr + image_offset,
57                         spl_image->size);
58
59         return 0;
60 }
61
62 static ulong h_spl_load_read(struct spl_load_info *load, ulong sector,
63                              ulong count, void *buf)
64 {
65         struct mmc *mmc = load->dev;
66
67         return blk_dread(mmc_get_blk_desc(mmc), sector, count, buf);
68 }
69
70 static __maybe_unused unsigned long spl_mmc_raw_uboot_offset(int part)
71 {
72 #if IS_ENABLED(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR)
73         if (part == 0)
74                 return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_DATA_PART_OFFSET;
75 #endif
76
77         return 0;
78 }
79
80 static __maybe_unused
81 int mmc_load_image_raw_sector(struct spl_image_info *spl_image,
82                               struct spl_boot_device *bootdev,
83                               struct mmc *mmc, unsigned long sector)
84 {
85         unsigned long count;
86         struct image_header *header;
87         struct blk_desc *bd = mmc_get_blk_desc(mmc);
88         int ret = 0;
89
90         header = spl_get_load_buffer(-sizeof(*header), bd->blksz);
91
92         /* read image header to find the image size & load address */
93         count = blk_dread(bd, sector, 1, header);
94         debug("hdr read sector %lx, count=%lu\n", sector, count);
95         if (count == 0) {
96                 ret = -EIO;
97                 goto end;
98         }
99
100         if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
101             image_get_magic(header) == FDT_MAGIC) {
102                 struct spl_load_info load;
103
104                 debug("Found FIT\n");
105                 load.dev = mmc;
106                 load.priv = NULL;
107                 load.filename = NULL;
108                 load.bl_len = mmc->read_bl_len;
109                 load.read = h_spl_load_read;
110                 ret = spl_load_simple_fit(spl_image, &load, sector, header);
111         } else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) {
112                 struct spl_load_info load;
113
114                 load.dev = mmc;
115                 load.priv = NULL;
116                 load.filename = NULL;
117                 load.bl_len = mmc->read_bl_len;
118                 load.read = h_spl_load_read;
119
120                 ret = spl_load_imx_container(spl_image, &load, sector);
121         } else {
122                 ret = mmc_load_legacy(spl_image, bootdev, mmc, sector, header);
123         }
124
125 end:
126         if (ret) {
127 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
128                 puts("mmc_load_image_raw_sector: mmc block read error\n");
129 #endif
130                 return -1;
131         }
132
133         return 0;
134 }
135
136 static int spl_mmc_get_device_index(u32 boot_device)
137 {
138         switch (boot_device) {
139         case BOOT_DEVICE_MMC1:
140                 return 0;
141         case BOOT_DEVICE_MMC2:
142         case BOOT_DEVICE_MMC2_2:
143                 return 1;
144         }
145
146 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
147         printf("spl: unsupported mmc boot device.\n");
148 #endif
149
150         return -ENODEV;
151 }
152
153 static int spl_mmc_find_device(struct mmc **mmcp, u32 boot_device)
154 {
155         int err, mmc_dev;
156
157         mmc_dev = spl_mmc_get_device_index(boot_device);
158         if (mmc_dev < 0)
159                 return mmc_dev;
160
161 #if CONFIG_IS_ENABLED(DM_MMC)
162         err = mmc_init_device(mmc_dev);
163 #else
164         err = mmc_initialize(NULL);
165 #endif /* DM_MMC */
166         if (err) {
167 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
168                 printf("spl: could not initialize mmc. error: %d\n", err);
169 #endif
170                 return err;
171         }
172         *mmcp = find_mmc_device(mmc_dev);
173         err = *mmcp ? 0 : -ENODEV;
174         if (err) {
175 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
176                 printf("spl: could not find mmc device %d. error: %d\n",
177                        mmc_dev, err);
178 #endif
179                 return err;
180         }
181
182         return 0;
183 }
184
185 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
186 static int mmc_load_image_raw_partition(struct spl_image_info *spl_image,
187                                         struct spl_boot_device *bootdev,
188                                         struct mmc *mmc, int partition,
189                                         unsigned long sector)
190 {
191         struct disk_partition info;
192         int err;
193
194 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION_TYPE
195         int type_part;
196         /* Only support MBR so DOS_ENTRY_NUMBERS */
197         for (type_part = 1; type_part <= DOS_ENTRY_NUMBERS; type_part++) {
198                 err = part_get_info(mmc_get_blk_desc(mmc), type_part, &info);
199                 if (err)
200                         continue;
201                 if (info.sys_ind ==
202                         CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION_TYPE) {
203                         partition = type_part;
204                         break;
205                 }
206         }
207 #endif
208
209         err = part_get_info(mmc_get_blk_desc(mmc), partition, &info);
210         if (err) {
211 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
212                 puts("spl: partition error\n");
213 #endif
214                 return -1;
215         }
216
217 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
218         return mmc_load_image_raw_sector(spl_image, bootdev, mmc, info.start + sector);
219 #else
220         return mmc_load_image_raw_sector(spl_image, bootdev, mmc, info.start);
221 #endif
222 }
223 #endif
224
225 #if CONFIG_IS_ENABLED(FALCON_BOOT_MMCSD)
226 static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
227                                  struct spl_boot_device *bootdev,
228                                  struct mmc *mmc)
229 {
230         int ret;
231
232 #if CONFIG_VAL(SYS_MMCSD_RAW_MODE_ARGS_SECTOR)
233         unsigned long count;
234
235         count = blk_dread(mmc_get_blk_desc(mmc),
236                 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
237                 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
238                 (void *) CONFIG_SYS_SPL_ARGS_ADDR);
239         if (count != CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS) {
240 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
241                 puts("mmc_load_image_raw_os: mmc block read error\n");
242 #endif
243                 return -1;
244         }
245 #endif  /* CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR */
246
247         ret = mmc_load_image_raw_sector(spl_image, bootdev, mmc,
248                 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
249         if (ret)
250                 return ret;
251
252         if (spl_image->os != IH_OS_LINUX && spl_image->os != IH_OS_TEE) {
253                 puts("Expected image is not found. Trying to start U-boot\n");
254                 return -ENOENT;
255         }
256
257         return 0;
258 }
259 #else
260 static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
261                                  struct spl_boot_device *bootdev,
262                                  struct mmc *mmc)
263 {
264         return -ENOSYS;
265 }
266 #endif
267
268 #ifndef CONFIG_SPL_OS_BOOT
269 int spl_start_uboot(void)
270 {
271         return 1;
272 }
273 #endif
274
275 #ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
276 static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image,
277                               struct spl_boot_device *bootdev,
278                               struct mmc *mmc,
279                               const char *filename)
280 {
281         int err = -ENOSYS;
282
283         __maybe_unused int partition = CONFIG_SYS_MMCSD_FS_BOOT_PARTITION;
284
285 #if CONFIG_SYS_MMCSD_FS_BOOT_PARTITION == -1
286         {
287                 struct disk_partition info;
288                 debug("Checking for the first MBR bootable partition\n");
289                 for (int type_part = 1; type_part <= DOS_ENTRY_NUMBERS; type_part++) {
290                         err = part_get_info(mmc_get_blk_desc(mmc), type_part, &info);
291                         if (err)
292                                 continue;
293                         debug("Partition %d is of type %d and bootable=%d\n", type_part, info.sys_ind, info.bootable);
294                         if (info.bootable != 0) {
295                                 debug("Partition %d is bootable, using it\n", type_part);
296                                 partition = type_part;
297                                 break;
298                         }
299                 }
300                 printf("Using first bootable partition: %d\n", partition);
301                 if (partition == CONFIG_SYS_MMCSD_FS_BOOT_PARTITION) {
302                         return -ENOSYS;
303                 }
304         }
305 #endif
306
307 #ifdef CONFIG_SPL_FS_FAT
308         if (!spl_start_uboot()) {
309                 err = spl_load_image_fat_os(spl_image, bootdev, mmc_get_blk_desc(mmc),
310                         partition);
311                 if (!err)
312                         return err;
313         }
314 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
315         err = spl_load_image_fat(spl_image, bootdev, mmc_get_blk_desc(mmc),
316                                  partition,
317                                  filename);
318         if (!err)
319                 return err;
320 #endif
321 #endif
322 #ifdef CONFIG_SPL_FS_EXT4
323         if (!spl_start_uboot()) {
324                 err = spl_load_image_ext_os(spl_image, bootdev, mmc_get_blk_desc(mmc),
325                         partition);
326                 if (!err)
327                         return err;
328         }
329 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
330         err = spl_load_image_ext(spl_image, bootdev, mmc_get_blk_desc(mmc),
331                                  partition,
332                                  filename);
333         if (!err)
334                 return err;
335 #endif
336 #endif
337
338 #if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
339         err = -ENOENT;
340 #endif
341
342         return err;
343 }
344 #else
345 static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image,
346                               struct spl_boot_device *bootdev,
347                               struct mmc *mmc,
348                               const char *filename)
349 {
350         return -ENOSYS;
351 }
352 #endif
353
354 u32 __weak spl_mmc_boot_mode(struct mmc *mmc, const u32 boot_device)
355 {
356 #if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
357         return MMCSD_MODE_FS;
358 #elif defined(CONFIG_SUPPORT_EMMC_BOOT)
359         return MMCSD_MODE_EMMCBOOT;
360 #else
361         return MMCSD_MODE_RAW;
362 #endif
363 }
364
365 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
366 int __weak spl_mmc_boot_partition(const u32 boot_device)
367 {
368         return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION;
369 }
370 #endif
371
372 unsigned long __weak spl_mmc_get_uboot_raw_sector(struct mmc *mmc,
373                                                   unsigned long raw_sect)
374 {
375         return raw_sect;
376 }
377
378 int default_spl_mmc_emmc_boot_partition(struct mmc *mmc)
379 {
380         int part;
381 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION
382         part = CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION;
383 #else
384         /*
385          * We need to check what the partition is configured to.
386          * 1 and 2 match up to boot0 / boot1 and 7 is user data
387          * which is the first physical partition (0).
388          */
389         part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
390         if (part == 7)
391                 part = 0;
392 #endif
393         return part;
394 }
395
396 int __weak spl_mmc_emmc_boot_partition(struct mmc *mmc)
397 {
398         return default_spl_mmc_emmc_boot_partition(mmc);
399 }
400
401 static int spl_mmc_get_mmc_devnum(struct mmc *mmc)
402 {
403         struct blk_desc *block_dev;
404 #if !CONFIG_IS_ENABLED(BLK)
405         block_dev = &mmc->block_dev;
406 #else
407         block_dev = dev_get_uclass_plat(mmc->dev);
408 #endif
409         return block_dev->devnum;
410 }
411
412 int spl_mmc_load(struct spl_image_info *spl_image,
413                  struct spl_boot_device *bootdev,
414                  const char *filename,
415                  int raw_part,
416                  unsigned long raw_sect)
417 {
418         static struct mmc *mmc;
419         u32 boot_mode;
420         int err = 0;
421         __maybe_unused int part = 0;
422         int mmc_dev;
423
424         /* Perform peripheral init only once for an mmc device */
425         mmc_dev = spl_mmc_get_device_index(bootdev->boot_device);
426         if (!mmc || spl_mmc_get_mmc_devnum(mmc) != mmc_dev) {
427                 err = spl_mmc_find_device(&mmc, bootdev->boot_device);
428                 if (err)
429                         return err;
430
431                 err = mmc_init(mmc);
432                 if (err) {
433                         mmc = NULL;
434 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
435                         printf("spl: mmc init failed with error: %d\n", err);
436 #endif
437                         return err;
438                 }
439         }
440
441         boot_mode = spl_mmc_boot_mode(mmc, bootdev->boot_device);
442         err = -EINVAL;
443         switch (boot_mode) {
444         case MMCSD_MODE_EMMCBOOT:
445                 part = spl_mmc_emmc_boot_partition(mmc);
446
447                 if (CONFIG_IS_ENABLED(MMC_TINY))
448                         err = mmc_switch_part(mmc, part);
449                 else
450                         err = blk_dselect_hwpart(mmc_get_blk_desc(mmc), part);
451
452                 if (err) {
453 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
454                         puts("spl: mmc partition switch failed\n");
455 #endif
456                         return err;
457                 }
458                 /* Fall through */
459         case MMCSD_MODE_RAW:
460                 debug("spl: mmc boot mode: raw\n");
461
462                 if (!spl_start_uboot()) {
463                         err = mmc_load_image_raw_os(spl_image, bootdev, mmc);
464                         if (!err)
465                                 return err;
466                 }
467
468                 raw_sect = spl_mmc_get_uboot_raw_sector(mmc, raw_sect);
469
470 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
471                 err = mmc_load_image_raw_partition(spl_image, bootdev,
472                                                    mmc, raw_part,
473                                                    raw_sect);
474                 if (!err)
475                         return err;
476 #endif
477 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
478                 err = mmc_load_image_raw_sector(spl_image, bootdev, mmc,
479                                 raw_sect + spl_mmc_raw_uboot_offset(part));
480                 if (!err)
481                         return err;
482 #endif
483                 /* If RAW mode fails, try FS mode. */
484         case MMCSD_MODE_FS:
485                 debug("spl: mmc boot mode: fs\n");
486
487                 err = spl_mmc_do_fs_boot(spl_image, bootdev, mmc, filename);
488                 if (!err)
489                         return err;
490
491                 break;
492 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
493         default:
494                 puts("spl: mmc: wrong boot mode\n");
495 #endif
496         }
497
498         return err;
499 }
500
501 int spl_mmc_load_image(struct spl_image_info *spl_image,
502                        struct spl_boot_device *bootdev)
503 {
504         return spl_mmc_load(spl_image, bootdev,
505 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
506                             CONFIG_SPL_FS_LOAD_PAYLOAD_NAME,
507 #else
508                             NULL,
509 #endif
510 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
511                             spl_mmc_boot_partition(bootdev->boot_device),
512 #else
513                             0,
514 #endif
515 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
516                             CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
517 #else
518                             0);
519 #endif
520 }
521
522 SPL_LOAD_IMAGE_METHOD("MMC1", 0, BOOT_DEVICE_MMC1, spl_mmc_load_image);
523 SPL_LOAD_IMAGE_METHOD("MMC2", 0, BOOT_DEVICE_MMC2, spl_mmc_load_image);
524 SPL_LOAD_IMAGE_METHOD("MMC2_2", 0, BOOT_DEVICE_MMC2_2, spl_mmc_load_image);