Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2010 |
| 4 | * Texas Instruments, <www.ti.com> |
| 5 | * |
| 6 | * Aneesh V <aneesh@ti.com> |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 7 | */ |
| 8 | #include <common.h> |
Simon Glass | f22851a | 2015-06-23 15:38:51 -0600 | [diff] [blame] | 9 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Simon Glass | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 11 | #include <part.h> |
Tom Rini | 28591df | 2012-08-13 12:03:19 -0700 | [diff] [blame] | 12 | #include <spl.h> |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 13 | #include <linux/compiler.h> |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 14 | #include <errno.h> |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 15 | #include <asm/u-boot.h> |
Nikita Kiryanov | 022ddb8 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 16 | #include <errno.h> |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 17 | #include <mmc.h> |
Tom Rini | 1530e8a | 2013-06-28 14:43:01 -0400 | [diff] [blame] | 18 | #include <image.h> |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 19 | |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 20 | static int mmc_load_legacy(struct spl_image_info *spl_image, |
| 21 | struct spl_boot_device *bootdev, |
| 22 | struct mmc *mmc, |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 23 | ulong sector, struct image_header *header) |
Simon Glass | b52c7fc | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 24 | { |
Pali Rohár | 1104167 | 2021-07-23 11:14:27 +0200 | [diff] [blame] | 25 | u32 image_offset_sectors; |
Simon Glass | b52c7fc | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 26 | u32 image_size_sectors; |
| 27 | unsigned long count; |
Pali Rohár | 1104167 | 2021-07-23 11:14:27 +0200 | [diff] [blame] | 28 | u32 image_offset; |
Marek Vasut | 02266e2 | 2016-04-29 00:44:54 +0200 | [diff] [blame] | 29 | int ret; |
| 30 | |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 31 | ret = spl_parse_image_header(spl_image, bootdev, header); |
Marek Vasut | 02266e2 | 2016-04-29 00:44:54 +0200 | [diff] [blame] | 32 | if (ret) |
| 33 | return ret; |
Simon Glass | b52c7fc | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 34 | |
Pali Rohár | 1104167 | 2021-07-23 11:14:27 +0200 | [diff] [blame] | 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 | |
Simon Glass | b52c7fc | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 40 | /* convert size to sectors - round up */ |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 41 | image_size_sectors = (spl_image->size + mmc->read_bl_len - 1) / |
Simon Glass | b52c7fc | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 42 | mmc->read_bl_len; |
| 43 | |
| 44 | /* Read the header too to avoid extra memcpy */ |
Pali Rohár | 1104167 | 2021-07-23 11:14:27 +0200 | [diff] [blame] | 45 | count = blk_dread(mmc_get_blk_desc(mmc), |
| 46 | sector + image_offset_sectors, |
| 47 | image_size_sectors, |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 48 | (void *)(ulong)spl_image->load_addr); |
Andre Przywara | 6c52607 | 2017-01-02 11:48:31 +0000 | [diff] [blame] | 49 | debug("read %x sectors to %lx\n", image_size_sectors, |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 50 | spl_image->load_addr); |
Simon Glass | b52c7fc | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 51 | if (count != image_size_sectors) |
| 52 | return -EIO; |
| 53 | |
Pali Rohár | 1104167 | 2021-07-23 11:14:27 +0200 | [diff] [blame] | 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 | |
Simon Glass | b52c7fc | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 59 | return 0; |
| 60 | } |
| 61 | |
Simon Glass | b52c7fc | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 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 | |
Simon Glass | 670d304 | 2016-05-14 14:03:06 -0600 | [diff] [blame] | 67 | return blk_dread(mmc_get_blk_desc(mmc), sector, count, buf); |
Simon Glass | b52c7fc | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 68 | } |
Simon Glass | b52c7fc | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 69 | |
Baruch Siach | fef81be | 2020-01-15 09:08:08 +0200 | [diff] [blame] | 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 | |
Seung-Woo Kim | c0a4e2d | 2017-09-19 13:29:45 +0900 | [diff] [blame] | 80 | static __maybe_unused |
| 81 | int mmc_load_image_raw_sector(struct spl_image_info *spl_image, |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 82 | struct spl_boot_device *bootdev, |
Seung-Woo Kim | c0a4e2d | 2017-09-19 13:29:45 +0900 | [diff] [blame] | 83 | struct mmc *mmc, unsigned long sector) |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 84 | { |
Paul Kocialkowski | 2d69f25 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 85 | unsigned long count; |
Peter Korsgaard | f247129 | 2013-03-21 04:55:17 +0000 | [diff] [blame] | 86 | struct image_header *header; |
Marek Vasut | 1dda46f | 2018-08-14 11:27:02 +0200 | [diff] [blame] | 87 | struct blk_desc *bd = mmc_get_blk_desc(mmc); |
Simon Glass | b52c7fc | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 88 | int ret = 0; |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 89 | |
Marek Vasut | 1dda46f | 2018-08-14 11:27:02 +0200 | [diff] [blame] | 90 | header = spl_get_load_buffer(-sizeof(*header), bd->blksz); |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 91 | |
| 92 | /* read image header to find the image size & load address */ |
Marek Vasut | 1dda46f | 2018-08-14 11:27:02 +0200 | [diff] [blame] | 93 | count = blk_dread(bd, sector, 1, header); |
Simon Glass | b52c7fc | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 94 | debug("hdr read sector %lx, count=%lu\n", sector, count); |
| 95 | if (count == 0) { |
| 96 | ret = -EIO; |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 97 | goto end; |
Simon Glass | b52c7fc | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 98 | } |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 99 | |
Masahiro Yamada | 802f1c3 | 2016-03-16 12:10:00 +0900 | [diff] [blame] | 100 | if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) && |
| 101 | image_get_magic(header) == FDT_MAGIC) { |
Simon Glass | b52c7fc | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 102 | struct spl_load_info load; |
| 103 | |
| 104 | debug("Found FIT\n"); |
| 105 | load.dev = mmc; |
| 106 | load.priv = NULL; |
Lokesh Vutla | f62cef1 | 2016-05-24 10:34:38 +0530 | [diff] [blame] | 107 | load.filename = NULL; |
Simon Glass | b52c7fc | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 108 | load.bl_len = mmc->read_bl_len; |
| 109 | load.read = h_spl_load_read; |
Simon Glass | 43a734f | 2016-09-24 18:20:16 -0600 | [diff] [blame] | 110 | ret = spl_load_simple_fit(spl_image, &load, sector, header); |
Peng Fan | d867076 | 2019-08-22 07:42:38 +0000 | [diff] [blame] | 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); |
Masahiro Yamada | 802f1c3 | 2016-03-16 12:10:00 +0900 | [diff] [blame] | 121 | } else { |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 122 | ret = mmc_load_legacy(spl_image, bootdev, mmc, sector, header); |
Simon Glass | 5614db8 | 2015-06-23 15:38:47 -0600 | [diff] [blame] | 123 | } |
Tom Rini | 1530e8a | 2013-06-28 14:43:01 -0400 | [diff] [blame] | 124 | |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 125 | end: |
Simon Glass | b52c7fc | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 126 | if (ret) { |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 127 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Fabio Estevam | 37342ab | 2016-04-20 23:12:04 -0300 | [diff] [blame] | 128 | puts("mmc_load_image_raw_sector: mmc block read error\n"); |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 129 | #endif |
Paul Kocialkowski | 2d69f25 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 130 | return -1; |
Paul Kocialkowski | 01c723f | 2015-06-03 18:48:51 +0200 | [diff] [blame] | 131 | } |
Paul Kocialkowski | 2d69f25 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 132 | |
| 133 | return 0; |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 134 | } |
| 135 | |
Masahiro Yamada | 2a4a20c | 2017-06-22 16:52:36 +0900 | [diff] [blame] | 136 | static int spl_mmc_get_device_index(u32 boot_device) |
Nikita Kiryanov | e63b2dd | 2015-11-08 17:11:54 +0200 | [diff] [blame] | 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 | |
Simon Glass | 4faf63b | 2015-12-02 10:59:12 -0700 | [diff] [blame] | 153 | static int spl_mmc_find_device(struct mmc **mmcp, u32 boot_device) |
Nikita Kiryanov | 022ddb8 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 154 | { |
Nikita Kiryanov | e63b2dd | 2015-11-08 17:11:54 +0200 | [diff] [blame] | 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; |
Nikita Kiryanov | 022ddb8 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 160 | |
Lokesh Vutla | c59b41c | 2019-09-09 14:40:36 +0530 | [diff] [blame] | 161 | #if CONFIG_IS_ENABLED(DM_MMC) |
| 162 | err = mmc_init_device(mmc_dev); |
| 163 | #else |
Nikita Kiryanov | 022ddb8 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 164 | err = mmc_initialize(NULL); |
Lokesh Vutla | c59b41c | 2019-09-09 14:40:36 +0530 | [diff] [blame] | 165 | #endif /* DM_MMC */ |
Nikita Kiryanov | 022ddb8 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 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 | } |
Simon Glass | 51ace32 | 2015-12-02 10:59:13 -0700 | [diff] [blame] | 172 | *mmcp = find_mmc_device(mmc_dev); |
| 173 | err = *mmcp ? 0 : -ENODEV; |
Nikita Kiryanov | 022ddb8 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 174 | if (err) { |
| 175 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Alex Kiernan | f5920d0 | 2018-09-18 08:11:55 +0000 | [diff] [blame] | 176 | printf("spl: could not find mmc device %d. error: %d\n", |
| 177 | mmc_dev, err); |
Nikita Kiryanov | 022ddb8 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 178 | #endif |
| 179 | return err; |
| 180 | } |
| 181 | |
Nikita Kiryanov | 022ddb8 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 182 | return 0; |
| 183 | } |
Nikita Kiryanov | 022ddb8 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 184 | |
Dalon Westergreen | f5ba985 | 2017-02-10 17:15:35 -0800 | [diff] [blame] | 185 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 186 | static int mmc_load_image_raw_partition(struct spl_image_info *spl_image, |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 187 | struct spl_boot_device *bootdev, |
Andreas Dannenberg | 8d9f7f1 | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 188 | struct mmc *mmc, int partition, |
| 189 | unsigned long sector) |
Paul Kocialkowski | 17675c8 | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 190 | { |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 191 | struct disk_partition info; |
Paul Kocialkowski | 2d69f25 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 192 | int err; |
Paul Kocialkowski | 17675c8 | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 193 | |
Dalon Westergreen | 8d770f4 | 2017-02-10 17:15:34 -0800 | [diff] [blame] | 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; |
Wolfgang Denk | 9d328a6 | 2021-09-27 17:42:38 +0200 | [diff] [blame] | 201 | if (info.sys_ind == |
Dalon Westergreen | 8d770f4 | 2017-02-10 17:15:34 -0800 | [diff] [blame] | 202 | CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION_TYPE) { |
| 203 | partition = type_part; |
| 204 | break; |
| 205 | } |
| 206 | } |
| 207 | #endif |
| 208 | |
Simon Glass | 55877f9 | 2016-07-05 17:10:18 -0600 | [diff] [blame] | 209 | err = part_get_info(mmc_get_blk_desc(mmc), partition, &info); |
Paul Kocialkowski | 2d69f25 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 210 | if (err) { |
Paul Kocialkowski | 17675c8 | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 211 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Paul Kocialkowski | 01c723f | 2015-06-03 18:48:51 +0200 | [diff] [blame] | 212 | puts("spl: partition error\n"); |
Paul Kocialkowski | 17675c8 | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 213 | #endif |
| 214 | return -1; |
| 215 | } |
| 216 | |
Semen Protsenko | 7e3ba48 | 2016-11-16 19:19:05 +0200 | [diff] [blame] | 217 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 218 | return mmc_load_image_raw_sector(spl_image, bootdev, mmc, info.start + sector); |
Stefan Roese | d0503cd | 2015-07-20 11:20:39 +0200 | [diff] [blame] | 219 | #else |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 220 | return mmc_load_image_raw_sector(spl_image, bootdev, mmc, info.start); |
Stefan Roese | d0503cd | 2015-07-20 11:20:39 +0200 | [diff] [blame] | 221 | #endif |
Paul Kocialkowski | 17675c8 | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 222 | } |
| 223 | #endif |
| 224 | |
Alexandru Gagniuc | 99426c8 | 2021-12-30 10:39:59 -0600 | [diff] [blame] | 225 | #if CONFIG_IS_ENABLED(FALCON_BOOT_MMCSD) |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 226 | static int mmc_load_image_raw_os(struct spl_image_info *spl_image, |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 227 | struct spl_boot_device *bootdev, |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 228 | struct mmc *mmc) |
Peter Korsgaard | 01b542f | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 229 | { |
Lokesh Vutla | 9dee77f | 2016-04-11 15:24:50 +0530 | [diff] [blame] | 230 | int ret; |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 231 | |
York Sun | cea8821 | 2018-06-26 09:44:39 -0700 | [diff] [blame] | 232 | #if defined(CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR) |
| 233 | unsigned long count; |
| 234 | |
Simon Glass | 70061ba | 2016-06-12 23:30:32 -0600 | [diff] [blame] | 235 | count = blk_dread(mmc_get_blk_desc(mmc), |
Paul Kocialkowski | 2d69f25 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 236 | CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR, |
| 237 | CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS, |
| 238 | (void *) CONFIG_SYS_SPL_ARGS_ADDR); |
Tom Rini | 195bc4b | 2017-08-14 21:01:30 -0400 | [diff] [blame] | 239 | if (count != CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS) { |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 240 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Fabio Estevam | 37342ab | 2016-04-20 23:12:04 -0300 | [diff] [blame] | 241 | puts("mmc_load_image_raw_os: mmc block read error\n"); |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 242 | #endif |
Peter Korsgaard | 01b542f | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 243 | return -1; |
| 244 | } |
York Sun | cea8821 | 2018-06-26 09:44:39 -0700 | [diff] [blame] | 245 | #endif /* CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR */ |
Peter Korsgaard | 01b542f | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 246 | |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 247 | ret = mmc_load_image_raw_sector(spl_image, bootdev, mmc, |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 248 | CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR); |
Lokesh Vutla | 9dee77f | 2016-04-11 15:24:50 +0530 | [diff] [blame] | 249 | if (ret) |
| 250 | return ret; |
| 251 | |
Alexandru Gagniuc | 36fd3ba | 2021-07-15 14:19:23 -0500 | [diff] [blame] | 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"); |
Lokesh Vutla | 9dee77f | 2016-04-11 15:24:50 +0530 | [diff] [blame] | 254 | return -ENOENT; |
| 255 | } |
| 256 | |
| 257 | return 0; |
Peter Korsgaard | 01b542f | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 258 | } |
Nikita Kiryanov | cc49f55 | 2015-11-08 17:11:45 +0200 | [diff] [blame] | 259 | #else |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 260 | static int mmc_load_image_raw_os(struct spl_image_info *spl_image, |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 261 | struct spl_boot_device *bootdev, |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 262 | struct mmc *mmc) |
Nikita Kiryanov | cc49f55 | 2015-11-08 17:11:45 +0200 | [diff] [blame] | 263 | { |
| 264 | return -ENOSYS; |
| 265 | } |
Peter Korsgaard | 01b542f | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 266 | #endif |
| 267 | |
Alexandru Gagniuc | 99426c8 | 2021-12-30 10:39:59 -0600 | [diff] [blame] | 268 | #ifndef CONFIG_SPL_OS_BOOT |
| 269 | int spl_start_uboot(void) |
| 270 | { |
| 271 | return 1; |
| 272 | } |
| 273 | #endif |
| 274 | |
Nikita Kiryanov | 642648c | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 275 | #ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 276 | static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image, |
| 277 | struct spl_boot_device *bootdev, |
| 278 | struct mmc *mmc, |
Andreas Dannenberg | 8d9f7f1 | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 279 | const char *filename) |
Nikita Kiryanov | 642648c | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 280 | { |
| 281 | int err = -ENOSYS; |
| 282 | |
Tien Fong Chee | 6fd0a71 | 2019-01-23 14:20:03 +0800 | [diff] [blame] | 283 | #ifdef CONFIG_SPL_FS_FAT |
Nikita Kiryanov | 642648c | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 284 | if (!spl_start_uboot()) { |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 285 | err = spl_load_image_fat_os(spl_image, bootdev, mmc_get_blk_desc(mmc), |
Nikita Kiryanov | 642648c | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 286 | CONFIG_SYS_MMCSD_FS_BOOT_PARTITION); |
| 287 | if (!err) |
| 288 | return err; |
| 289 | } |
| 290 | #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 291 | err = spl_load_image_fat(spl_image, bootdev, mmc_get_blk_desc(mmc), |
Nikita Kiryanov | 642648c | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 292 | CONFIG_SYS_MMCSD_FS_BOOT_PARTITION, |
Andreas Dannenberg | 8d9f7f1 | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 293 | filename); |
Nikita Kiryanov | 642648c | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 294 | if (!err) |
| 295 | return err; |
| 296 | #endif |
| 297 | #endif |
Tien Fong Chee | 6091dd1 | 2019-01-23 14:20:05 +0800 | [diff] [blame] | 298 | #ifdef CONFIG_SPL_FS_EXT4 |
Nikita Kiryanov | 642648c | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 299 | if (!spl_start_uboot()) { |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 300 | err = spl_load_image_ext_os(spl_image, bootdev, mmc_get_blk_desc(mmc), |
Nikita Kiryanov | 642648c | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 301 | CONFIG_SYS_MMCSD_FS_BOOT_PARTITION); |
| 302 | if (!err) |
| 303 | return err; |
| 304 | } |
| 305 | #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 306 | err = spl_load_image_ext(spl_image, bootdev, mmc_get_blk_desc(mmc), |
Nikita Kiryanov | 642648c | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 307 | CONFIG_SYS_MMCSD_FS_BOOT_PARTITION, |
Andreas Dannenberg | 8d9f7f1 | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 308 | filename); |
Nikita Kiryanov | 642648c | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 309 | if (!err) |
| 310 | return err; |
| 311 | #endif |
| 312 | #endif |
| 313 | |
Tien Fong Chee | 6091dd1 | 2019-01-23 14:20:05 +0800 | [diff] [blame] | 314 | #if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4) |
Nikita Kiryanov | 642648c | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 315 | err = -ENOENT; |
| 316 | #endif |
| 317 | |
| 318 | return err; |
| 319 | } |
| 320 | #else |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 321 | static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image, |
| 322 | struct spl_boot_device *bootdev, |
| 323 | struct mmc *mmc, |
Andreas Dannenberg | 8d9f7f1 | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 324 | const char *filename) |
Nikita Kiryanov | 642648c | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 325 | { |
| 326 | return -ENOSYS; |
| 327 | } |
| 328 | #endif |
| 329 | |
Harald Seiler | 0bf7ab1 | 2020-04-15 11:33:30 +0200 | [diff] [blame] | 330 | u32 __weak spl_mmc_boot_mode(const u32 boot_device) |
Lukasz Majewski | 7c88d10 | 2018-02-03 08:29:52 +0100 | [diff] [blame] | 331 | { |
Tien Fong Chee | 6091dd1 | 2019-01-23 14:20:05 +0800 | [diff] [blame] | 332 | #if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4) |
Lukasz Majewski | 7c88d10 | 2018-02-03 08:29:52 +0100 | [diff] [blame] | 333 | return MMCSD_MODE_FS; |
| 334 | #elif defined(CONFIG_SUPPORT_EMMC_BOOT) |
| 335 | return MMCSD_MODE_EMMCBOOT; |
| 336 | #else |
| 337 | return MMCSD_MODE_RAW; |
| 338 | #endif |
| 339 | } |
| 340 | |
Patrick Delaunay | 113aa50 | 2018-03-20 10:54:51 +0100 | [diff] [blame] | 341 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION |
Harald Seiler | bf16c30 | 2020-04-15 11:33:31 +0200 | [diff] [blame] | 342 | int __weak spl_mmc_boot_partition(const u32 boot_device) |
Patrick Delaunay | 113aa50 | 2018-03-20 10:54:51 +0100 | [diff] [blame] | 343 | { |
| 344 | return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION; |
| 345 | } |
| 346 | #endif |
| 347 | |
Faiz Abbas | 0bdf563 | 2020-02-26 13:44:35 +0530 | [diff] [blame] | 348 | unsigned long __weak spl_mmc_get_uboot_raw_sector(struct mmc *mmc, |
| 349 | unsigned long raw_sect) |
Peng Fan | 315cf59 | 2019-09-23 10:18:40 +0800 | [diff] [blame] | 350 | { |
Faiz Abbas | 0bdf563 | 2020-02-26 13:44:35 +0530 | [diff] [blame] | 351 | return raw_sect; |
Peng Fan | 315cf59 | 2019-09-23 10:18:40 +0800 | [diff] [blame] | 352 | } |
| 353 | |
Marek Vasut | de1ce23 | 2021-07-03 04:55:32 +0200 | [diff] [blame] | 354 | int default_spl_mmc_emmc_boot_partition(struct mmc *mmc) |
| 355 | { |
| 356 | int part; |
| 357 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION |
| 358 | part = CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION; |
| 359 | #else |
| 360 | /* |
| 361 | * We need to check what the partition is configured to. |
| 362 | * 1 and 2 match up to boot0 / boot1 and 7 is user data |
| 363 | * which is the first physical partition (0). |
| 364 | */ |
| 365 | part = (mmc->part_config >> 3) & PART_ACCESS_MASK; |
| 366 | if (part == 7) |
| 367 | part = 0; |
| 368 | #endif |
| 369 | return part; |
| 370 | } |
| 371 | |
| 372 | int __weak spl_mmc_emmc_boot_partition(struct mmc *mmc) |
| 373 | { |
| 374 | return default_spl_mmc_emmc_boot_partition(mmc); |
| 375 | } |
| 376 | |
Andreas Dannenberg | 8d9f7f1 | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 377 | int spl_mmc_load(struct spl_image_info *spl_image, |
| 378 | struct spl_boot_device *bootdev, |
| 379 | const char *filename, |
| 380 | int raw_part, |
| 381 | unsigned long raw_sect) |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 382 | { |
Andreas Dannenberg | 8d9f7f1 | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 383 | static struct mmc *mmc; |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 384 | u32 boot_mode; |
Simon Glass | f22851a | 2015-06-23 15:38:51 -0600 | [diff] [blame] | 385 | int err = 0; |
Baruch Siach | fef81be | 2020-01-15 09:08:08 +0200 | [diff] [blame] | 386 | __maybe_unused int part = 0; |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 387 | |
Andreas Dannenberg | 8d9f7f1 | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 388 | /* Perform peripheral init only once */ |
| 389 | if (!mmc) { |
| 390 | err = spl_mmc_find_device(&mmc, bootdev->boot_device); |
| 391 | if (err) |
| 392 | return err; |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 393 | |
Andreas Dannenberg | 8d9f7f1 | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 394 | err = mmc_init(mmc); |
| 395 | if (err) { |
| 396 | mmc = NULL; |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 397 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Andreas Dannenberg | 8d9f7f1 | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 398 | printf("spl: mmc init failed with error: %d\n", err); |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 399 | #endif |
Andreas Dannenberg | 8d9f7f1 | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 400 | return err; |
| 401 | } |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 402 | } |
Peter Korsgaard | 5e5483d | 2013-05-13 08:36:25 +0000 | [diff] [blame] | 403 | |
Harald Seiler | 0bf7ab1 | 2020-04-15 11:33:30 +0200 | [diff] [blame] | 404 | boot_mode = spl_mmc_boot_mode(bootdev->boot_device); |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 405 | err = -EINVAL; |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 406 | switch (boot_mode) { |
Nikita Kiryanov | 2b3ae5f | 2015-11-08 17:11:48 +0200 | [diff] [blame] | 407 | case MMCSD_MODE_EMMCBOOT: |
Marek Vasut | de1ce23 | 2021-07-03 04:55:32 +0200 | [diff] [blame] | 408 | part = spl_mmc_emmc_boot_partition(mmc); |
Nikita Kiryanov | 2b3ae5f | 2015-11-08 17:11:48 +0200 | [diff] [blame] | 409 | |
Mans Rullgard | ec046a5 | 2019-09-03 16:17:17 +0200 | [diff] [blame] | 410 | if (CONFIG_IS_ENABLED(MMC_TINY)) |
| 411 | err = mmc_switch_part(mmc, part); |
| 412 | else |
| 413 | err = blk_dselect_hwpart(mmc_get_blk_desc(mmc), part); |
Marek Vasut | f537e39 | 2016-12-01 02:06:33 +0100 | [diff] [blame] | 414 | |
Mans Rullgard | ec046a5 | 2019-09-03 16:17:17 +0200 | [diff] [blame] | 415 | if (err) { |
Nikita Kiryanov | 2b3ae5f | 2015-11-08 17:11:48 +0200 | [diff] [blame] | 416 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Mans Rullgard | ec046a5 | 2019-09-03 16:17:17 +0200 | [diff] [blame] | 417 | puts("spl: mmc partition switch failed\n"); |
Nikita Kiryanov | 2b3ae5f | 2015-11-08 17:11:48 +0200 | [diff] [blame] | 418 | #endif |
Mans Rullgard | ec046a5 | 2019-09-03 16:17:17 +0200 | [diff] [blame] | 419 | return err; |
| 420 | } |
| 421 | /* Fall through */ |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 422 | case MMCSD_MODE_RAW: |
| 423 | debug("spl: mmc boot mode: raw\n"); |
| 424 | |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 425 | if (!spl_start_uboot()) { |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 426 | err = mmc_load_image_raw_os(spl_image, bootdev, mmc); |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 427 | if (!err) |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 428 | return err; |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 429 | } |
Peng Fan | dc7d322 | 2019-10-23 01:43:30 +0000 | [diff] [blame] | 430 | |
Faiz Abbas | 0bdf563 | 2020-02-26 13:44:35 +0530 | [diff] [blame] | 431 | raw_sect = spl_mmc_get_uboot_raw_sector(mmc, raw_sect); |
Peng Fan | dc7d322 | 2019-10-23 01:43:30 +0000 | [diff] [blame] | 432 | |
Dalon Westergreen | f5ba985 | 2017-02-10 17:15:35 -0800 | [diff] [blame] | 433 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 434 | err = mmc_load_image_raw_partition(spl_image, bootdev, |
| 435 | mmc, raw_part, |
Andreas Dannenberg | 8d9f7f1 | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 436 | raw_sect); |
Paul Kocialkowski | 4bd66e7 | 2015-06-08 23:05:09 +0200 | [diff] [blame] | 437 | if (!err) |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 438 | return err; |
Dalon Westergreen | f5ba985 | 2017-02-10 17:15:35 -0800 | [diff] [blame] | 439 | #endif |
Semen Protsenko | 7e3ba48 | 2016-11-16 19:19:05 +0200 | [diff] [blame] | 440 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 441 | err = mmc_load_image_raw_sector(spl_image, bootdev, mmc, |
Baruch Siach | fef81be | 2020-01-15 09:08:08 +0200 | [diff] [blame] | 442 | raw_sect + spl_mmc_raw_uboot_offset(part)); |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 443 | if (!err) |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 444 | return err; |
Paul Kocialkowski | 4bd66e7 | 2015-06-08 23:05:09 +0200 | [diff] [blame] | 445 | #endif |
Guillaume GARDET | 834dd88 | 2016-02-18 18:17:36 +0100 | [diff] [blame] | 446 | /* If RAW mode fails, try FS mode. */ |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 447 | case MMCSD_MODE_FS: |
| 448 | debug("spl: mmc boot mode: fs\n"); |
| 449 | |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 450 | err = spl_mmc_do_fs_boot(spl_image, bootdev, mmc, filename); |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 451 | if (!err) |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 452 | return err; |
Nikita Kiryanov | 642648c | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 453 | |
Nikita Kiryanov | ed52f4d | 2015-11-08 17:11:43 +0200 | [diff] [blame] | 454 | break; |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 455 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Nikita Kiryanov | ed52f4d | 2015-11-08 17:11:43 +0200 | [diff] [blame] | 456 | default: |
| 457 | puts("spl: mmc: wrong boot mode\n"); |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 458 | #endif |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 459 | } |
Nikita Kiryanov | ed52f4d | 2015-11-08 17:11:43 +0200 | [diff] [blame] | 460 | |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 461 | return err; |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 462 | } |
Simon Glass | d449cb8 | 2016-09-24 18:20:00 -0600 | [diff] [blame] | 463 | |
Andreas Dannenberg | 8d9f7f1 | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 464 | int spl_mmc_load_image(struct spl_image_info *spl_image, |
| 465 | struct spl_boot_device *bootdev) |
| 466 | { |
| 467 | return spl_mmc_load(spl_image, bootdev, |
| 468 | #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME |
| 469 | CONFIG_SPL_FS_LOAD_PAYLOAD_NAME, |
| 470 | #else |
| 471 | NULL, |
| 472 | #endif |
| 473 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION |
Harald Seiler | bf16c30 | 2020-04-15 11:33:31 +0200 | [diff] [blame] | 474 | spl_mmc_boot_partition(bootdev->boot_device), |
Andreas Dannenberg | 8d9f7f1 | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 475 | #else |
| 476 | 0, |
| 477 | #endif |
| 478 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR |
| 479 | CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR); |
| 480 | #else |
| 481 | 0); |
| 482 | #endif |
| 483 | } |
| 484 | |
Simon Glass | 4fc1f25 | 2016-11-30 15:30:50 -0700 | [diff] [blame] | 485 | SPL_LOAD_IMAGE_METHOD("MMC1", 0, BOOT_DEVICE_MMC1, spl_mmc_load_image); |
| 486 | SPL_LOAD_IMAGE_METHOD("MMC2", 0, BOOT_DEVICE_MMC2, spl_mmc_load_image); |
| 487 | SPL_LOAD_IMAGE_METHOD("MMC2_2", 0, BOOT_DEVICE_MMC2_2, spl_mmc_load_image); |