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