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