1 /*
2  * Copyright 2011 Freescale Semiconductor, Inc.
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <mmc.h>
9 #include <malloc.h>
10 
11 /*
12  * The environment variables are written to just after the u-boot image
13  * on SDCard, so we must read the MBR to get the start address and code
14  * length of the u-boot image, then calculate the address of the env.
15  */
16 #define ESDHC_BOOT_IMAGE_SIZE	0x48
17 #define ESDHC_BOOT_IMAGE_ADDR	0x50
18 
19 int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr)
20 {
21 	u8 *tmp_buf;
22 	u32 blklen, code_offset, code_len, n;
23 
24 	blklen = mmc->read_bl_len;
25 	tmp_buf = malloc(blklen);
26 	if (!tmp_buf)
27 		return 1;
28 
29 	/* read out the first block, get the config data information */
30 	n = mmc->block_dev.block_read(mmc->block_dev.dev, 0, 1, tmp_buf);
31 	if (!n) {
32 		free(tmp_buf);
33 		return 1;
34 	}
35 
36 	/* Get the Source Address, from offset 0x50 */
37 	code_offset = *(u32 *)(tmp_buf + ESDHC_BOOT_IMAGE_ADDR);
38 
39 	/* Get the code size from offset 0x48 */
40 	code_len = *(u32 *)(tmp_buf + ESDHC_BOOT_IMAGE_SIZE);
41 
42 	*env_addr = code_offset + code_len;
43 
44 	free(tmp_buf);
45 
46 	return 0;
47 }
48