1 /*
2  * Copyright 2011 Freescale Semiconductor, Inc.
3  *
4  * See file CREDITS for list of people who contributed to this
5  * project.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of
10  * the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20  * MA 02111-1307 USA
21  */
22 
23 #include <common.h>
24 #include <mmc.h>
25 #include <malloc.h>
26 
27 /*
28  * The environment variables are written to just after the u-boot image
29  * on SDCard, so we must read the MBR to get the start address and code
30  * length of the u-boot image, then calculate the address of the env.
31  */
32 #define ESDHC_BOOT_IMAGE_SIZE	0x48
33 #define ESDHC_BOOT_IMAGE_ADDR	0x50
34 
35 int mmc_get_env_addr(struct mmc *mmc, u32 *env_addr)
36 {
37 	u8 *tmp_buf;
38 	u32 blklen, code_offset, code_len, n;
39 
40 	blklen = mmc->read_bl_len;
41 	tmp_buf = malloc(blklen);
42 	if (!tmp_buf)
43 		return 1;
44 
45 	/* read out the first block, get the config data information */
46 	n = mmc->block_dev.block_read(mmc->block_dev.dev, 0, 1, tmp_buf);
47 	if (!n) {
48 		free(tmp_buf);
49 		return 1;
50 	}
51 
52 	/* Get the Source Address, from offset 0x50 */
53 	code_offset = *(u32 *)(tmp_buf + ESDHC_BOOT_IMAGE_ADDR);
54 
55 	/* Get the code size from offset 0x48 */
56 	code_len = *(u32 *)(tmp_buf + ESDHC_BOOT_IMAGE_SIZE);
57 
58 	*env_addr = code_offset + code_len;
59 
60 	free(tmp_buf);
61 
62 	return 0;
63 }
64