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