Home | History | Annotate | Download | only in spl
      1 // SPDX-License-Identifier: GPL-2.0+
      2 /*
      3  * (C) Copyright 2010
      4  * Texas Instruments, <www.ti.com>
      5  *
      6  * Aneesh V <aneesh (at) 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 	int ret = 0;
     59 
     60 	header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
     61 					 sizeof(struct image_header));
     62 
     63 	/* read image header to find the image size & load address */
     64 	count = blk_dread(mmc_get_blk_desc(mmc), 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 	unsigned long count;
    195 	int ret;
    196 
    197 	count = blk_dread(mmc_get_blk_desc(mmc),
    198 		CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
    199 		CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
    200 		(void *) CONFIG_SYS_SPL_ARGS_ADDR);
    201 	if (count != CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS) {
    202 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
    203 		puts("mmc_load_image_raw_os: mmc block read error\n");
    204 #endif
    205 		return -1;
    206 	}
    207 
    208 	ret = mmc_load_image_raw_sector(spl_image, mmc,
    209 		CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
    210 	if (ret)
    211 		return ret;
    212 
    213 	if (spl_image->os != IH_OS_LINUX) {
    214 		puts("Expected Linux image is not found. Trying to start U-boot\n");
    215 		return -ENOENT;
    216 	}
    217 
    218 	return 0;
    219 }
    220 #else
    221 int spl_start_uboot(void)
    222 {
    223 	return 1;
    224 }
    225 static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
    226 				 struct mmc *mmc)
    227 {
    228 	return -ENOSYS;
    229 }
    230 #endif
    231 
    232 #ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
    233 static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image, struct mmc *mmc)
    234 {
    235 	int err = -ENOSYS;
    236 
    237 #ifdef CONFIG_SPL_FAT_SUPPORT
    238 	if (!spl_start_uboot()) {
    239 		err = spl_load_image_fat_os(spl_image, mmc_get_blk_desc(mmc),
    240 			CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
    241 		if (!err)
    242 			return err;
    243 	}
    244 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
    245 	err = spl_load_image_fat(spl_image, mmc_get_blk_desc(mmc),
    246 				 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
    247 				 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
    248 	if (!err)
    249 		return err;
    250 #endif
    251 #endif
    252 #ifdef CONFIG_SPL_EXT_SUPPORT
    253 	if (!spl_start_uboot()) {
    254 		err = spl_load_image_ext_os(spl_image, mmc_get_blk_desc(mmc),
    255 			CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
    256 		if (!err)
    257 			return err;
    258 	}
    259 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
    260 	err = spl_load_image_ext(spl_image, mmc_get_blk_desc(mmc),
    261 				 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
    262 				 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
    263 	if (!err)
    264 		return err;
    265 #endif
    266 #endif
    267 
    268 #if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT)
    269 	err = -ENOENT;
    270 #endif
    271 
    272 	return err;
    273 }
    274 #else
    275 static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image, struct mmc *mmc)
    276 {
    277 	return -ENOSYS;
    278 }
    279 #endif
    280 
    281 u32 __weak spl_boot_mode(const u32 boot_device)
    282 {
    283 #if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT)
    284 	return MMCSD_MODE_FS;
    285 #elif defined(CONFIG_SUPPORT_EMMC_BOOT)
    286 	return MMCSD_MODE_EMMCBOOT;
    287 #else
    288 	return MMCSD_MODE_RAW;
    289 #endif
    290 }
    291 
    292 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
    293 __weak
    294 int spl_boot_partition(const u32 boot_device)
    295 {
    296 	return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION;
    297 }
    298 #endif
    299 
    300 int spl_mmc_load_image(struct spl_image_info *spl_image,
    301 		       struct spl_boot_device *bootdev)
    302 {
    303 	struct mmc *mmc = NULL;
    304 	u32 boot_mode;
    305 	int err = 0;
    306 	__maybe_unused int part;
    307 
    308 	err = spl_mmc_find_device(&mmc, bootdev->boot_device);
    309 	if (err)
    310 		return err;
    311 
    312 	err = mmc_init(mmc);
    313 	if (err) {
    314 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
    315 		printf("spl: mmc init failed with error: %d\n", err);
    316 #endif
    317 		return err;
    318 	}
    319 
    320 	boot_mode = spl_boot_mode(bootdev->boot_device);
    321 	err = -EINVAL;
    322 	switch (boot_mode) {
    323 	case MMCSD_MODE_EMMCBOOT:
    324 			/*
    325 			 * We need to check what the partition is configured to.
    326 			 * 1 and 2 match up to boot0 / boot1 and 7 is user data
    327 			 * which is the first physical partition (0).
    328 			 */
    329 			part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
    330 
    331 			if (part == 7)
    332 				part = 0;
    333 
    334 			if (CONFIG_IS_ENABLED(MMC_TINY))
    335 				err = mmc_switch_part(mmc, part);
    336 			else
    337 				err = blk_dselect_hwpart(mmc_get_blk_desc(mmc), part);
    338 
    339 			if (err) {
    340 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
    341 				puts("spl: mmc partition switch failed\n");
    342 #endif
    343 				return err;
    344 			}
    345 			/* Fall through */
    346 	case MMCSD_MODE_RAW:
    347 		debug("spl: mmc boot mode: raw\n");
    348 
    349 		if (!spl_start_uboot()) {
    350 			err = mmc_load_image_raw_os(spl_image, mmc);
    351 			if (!err)
    352 				return err;
    353 		}
    354 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
    355 		err = spl_boot_partition(bootdev->boot_device);
    356 		if (!err)
    357 			return err;
    358 
    359 		err = mmc_load_image_raw_partition(spl_image, mmc, err);
    360 		if (!err)
    361 			return err;
    362 #endif
    363 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
    364 		err = mmc_load_image_raw_sector(spl_image, mmc,
    365 			CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
    366 		if (!err)
    367 			return err;
    368 #endif
    369 		/* If RAW mode fails, try FS mode. */
    370 	case MMCSD_MODE_FS:
    371 		debug("spl: mmc boot mode: fs\n");
    372 
    373 		err = spl_mmc_do_fs_boot(spl_image, mmc);
    374 		if (!err)
    375 			return err;
    376 
    377 		break;
    378 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
    379 	default:
    380 		puts("spl: mmc: wrong boot mode\n");
    381 #endif
    382 	}
    383 
    384 	return err;
    385 }
    386 
    387 SPL_LOAD_IMAGE_METHOD("MMC1", 0, BOOT_DEVICE_MMC1, spl_mmc_load_image);
    388 SPL_LOAD_IMAGE_METHOD("MMC2", 0, BOOT_DEVICE_MMC2, spl_mmc_load_image);
    389 SPL_LOAD_IMAGE_METHOD("MMC2_2", 0, BOOT_DEVICE_MMC2_2, spl_mmc_load_image);
    390