blob: f6e2b2bbd6775d1a1928118246bfdf68e42b2e07 [file] [log] [blame]
Jerry Huang2c766ba2011-01-24 17:09:54 +00001/*
2 * Copyright 2011 Freescale Semiconductor, Inc.
3 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Jerry Huang2c766ba2011-01-24 17:09:54 +00005 */
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
Michael Heimpolda1d25a32013-04-10 10:36:19 +000019int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr)
Jerry Huang2c766ba2011-01-24 17:09:54 +000020{
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}