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. error: %d\n", err); 144 #endif 145 return err; 146 } 147 148 return 0; 149 } 150 151 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION 152 static int mmc_load_image_raw_partition(struct spl_image_info *spl_image, 153 struct mmc *mmc, int partition) 154 { 155 disk_partition_t info; 156 int err; 157 158 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION_TYPE 159 int type_part; 160 /* Only support MBR so DOS_ENTRY_NUMBERS */ 161 for (type_part = 1; type_part <= DOS_ENTRY_NUMBERS; type_part++) { 162 err = part_get_info(mmc_get_blk_desc(mmc), type_part, &info); 163 if (err) 164 continue; 165 if (info.sys_ind == 166 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION_TYPE) { 167 partition = type_part; 168 break; 169 } 170 } 171 #endif 172 173 err = part_get_info(mmc_get_blk_desc(mmc), partition, &info); 174 if (err) { 175 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT 176 puts("spl: partition error\n"); 177 #endif 178 return -1; 179 } 180 181 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR 182 return mmc_load_image_raw_sector(spl_image, mmc, 183 info.start + CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR); 184 #else 185 return mmc_load_image_raw_sector(spl_image, mmc, info.start); 186 #endif 187 } 188 #endif 189 190 #ifdef CONFIG_SPL_OS_BOOT 191 static int mmc_load_image_raw_os(struct spl_image_info *spl_image, 192 struct mmc *mmc) 193 { 194 int ret; 195 196 #if defined(CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR) 197 unsigned long count; 198 199 count = blk_dread(mmc_get_blk_desc(mmc), 200 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR, 201 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS, 202 (void *) CONFIG_SYS_SPL_ARGS_ADDR); 203 if (count != CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS) { 204 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT 205 puts("mmc_load_image_raw_os: mmc block read error\n"); 206 #endif 207 return -1; 208 } 209 #endif /* CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR */ 210 211 ret = mmc_load_image_raw_sector(spl_image, mmc, 212 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR); 213 if (ret) 214 return ret; 215 216 if (spl_image->os != IH_OS_LINUX) { 217 puts("Expected Linux image is not found. Trying to start U-boot\n"); 218 return -ENOENT; 219 } 220 221 return 0; 222 } 223 #else 224 int spl_start_uboot(void) 225 { 226 return 1; 227 } 228 static int mmc_load_image_raw_os(struct spl_image_info *spl_image, 229 struct mmc *mmc) 230 { 231 return -ENOSYS; 232 } 233 #endif 234 235 #ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION 236 static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image, struct mmc *mmc) 237 { 238 int err = -ENOSYS; 239 240 #ifdef CONFIG_SPL_FAT_SUPPORT 241 if (!spl_start_uboot()) { 242 err = spl_load_image_fat_os(spl_image, mmc_get_blk_desc(mmc), 243 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION); 244 if (!err) 245 return err; 246 } 247 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME 248 err = spl_load_image_fat(spl_image, mmc_get_blk_desc(mmc), 249 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION, 250 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME); 251 if (!err) 252 return err; 253 #endif 254 #endif 255 #ifdef CONFIG_SPL_EXT_SUPPORT 256 if (!spl_start_uboot()) { 257 err = spl_load_image_ext_os(spl_image, mmc_get_blk_desc(mmc), 258 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION); 259 if (!err) 260 return err; 261 } 262 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME 263 err = spl_load_image_ext(spl_image, mmc_get_blk_desc(mmc), 264 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION, 265 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME); 266 if (!err) 267 return err; 268 #endif 269 #endif 270 271 #if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT) 272 err = -ENOENT; 273 #endif 274 275 return err; 276 } 277 #else 278 static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image, struct mmc *mmc) 279 { 280 return -ENOSYS; 281 } 282 #endif 283 284 u32 __weak spl_boot_mode(const u32 boot_device) 285 { 286 #if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT) 287 return MMCSD_MODE_FS; 288 #elif defined(CONFIG_SUPPORT_EMMC_BOOT) 289 return MMCSD_MODE_EMMCBOOT; 290 #else 291 return MMCSD_MODE_RAW; 292 #endif 293 } 294 295 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION 296 __weak 297 int spl_boot_partition(const u32 boot_device) 298 { 299 return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION; 300 } 301 #endif 302 303 int spl_mmc_load_image(struct spl_image_info *spl_image, 304 struct spl_boot_device *bootdev) 305 { 306 struct mmc *mmc = NULL; 307 u32 boot_mode; 308 int err = 0; 309 __maybe_unused int part; 310 311 err = spl_mmc_find_device(&mmc, bootdev->boot_device); 312 if (err) 313 return err; 314 315 err = mmc_init(mmc); 316 if (err) { 317 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT 318 printf("spl: mmc init failed with error: %d\n", err); 319 #endif 320 return err; 321 } 322 323 boot_mode = spl_boot_mode(bootdev->boot_device); 324 err = -EINVAL; 325 switch (boot_mode) { 326 case MMCSD_MODE_EMMCBOOT: 327 /* 328 * We need to check what the partition is configured to. 329 * 1 and 2 match up to boot0 / boot1 and 7 is user data 330 * which is the first physical partition (0). 331 */ 332 part = (mmc->part_config >> 3) & PART_ACCESS_MASK; 333 334 if (part == 7) 335 part = 0; 336 337 if (CONFIG_IS_ENABLED(MMC_TINY)) 338 err = mmc_switch_part(mmc, part); 339 else 340 err = blk_dselect_hwpart(mmc_get_blk_desc(mmc), part); 341 342 if (err) { 343 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT 344 puts("spl: mmc partition switch failed\n"); 345 #endif 346 return err; 347 } 348 /* Fall through */ 349 case MMCSD_MODE_RAW: 350 debug("spl: mmc boot mode: raw\n"); 351 352 if (!spl_start_uboot()) { 353 err = mmc_load_image_raw_os(spl_image, mmc); 354 if (!err) 355 return err; 356 } 357 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION 358 err = spl_boot_partition(bootdev->boot_device); 359 if (!err) 360 return err; 361 362 err = mmc_load_image_raw_partition(spl_image, mmc, err); 363 if (!err) 364 return err; 365 #endif 366 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR 367 err = mmc_load_image_raw_sector(spl_image, mmc, 368 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR); 369 if (!err) 370 return err; 371 #endif 372 /* If RAW mode fails, try FS mode. */ 373 case MMCSD_MODE_FS: 374 debug("spl: mmc boot mode: fs\n"); 375 376 err = spl_mmc_do_fs_boot(spl_image, mmc); 377 if (!err) 378 return err; 379 380 break; 381 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT 382 default: 383 puts("spl: mmc: wrong boot mode\n"); 384 #endif 385 } 386 387 return err; 388 } 389 390 SPL_LOAD_IMAGE_METHOD("MMC1", 0, BOOT_DEVICE_MMC1, spl_mmc_load_image); 391 SPL_LOAD_IMAGE_METHOD("MMC2", 0, BOOT_DEVICE_MMC2, spl_mmc_load_image); 392 SPL_LOAD_IMAGE_METHOD("MMC2_2", 0, BOOT_DEVICE_MMC2_2, spl_mmc_load_image); 393