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