1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2011 Freescale Semiconductor, Inc.
11 * The environment variables are written to just after the u-boot image
12 * on SDCard, so we must read the MBR to get the start address and code
13 * length of the u-boot image, then calculate the address of the env.
15 #define ESDHC_BOOT_IMAGE_SIZE 0x48
16 #define ESDHC_BOOT_IMAGE_ADDR 0x50
18 #define ESDHC_DEFAULT_ENVADDR 0x400
20 int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr)
23 u32 blklen, code_offset, code_len, n;
25 blklen = mmc->read_bl_len;
26 tmp_buf = malloc(blklen);
30 /* read out the first block, get the config data information */
31 n = mmc->block_dev.block_read(&mmc->block_dev, 0, 1, tmp_buf);
37 /* Get the Source Address, from offset 0x50 */
38 code_offset = *(u32 *)(tmp_buf + ESDHC_BOOT_IMAGE_ADDR);
40 /* Get the code size from offset 0x48 */
41 code_len = *(u32 *)(tmp_buf + ESDHC_BOOT_IMAGE_SIZE);
43 #ifdef CONFIG_ESDHC_HC_BLK_ADDR
45 * On soc BSC9131, BSC9132:
46 * In High Capacity SD Cards (> 2 GBytes), the 32-bit source address and
47 * code length of these soc specify the memory address in block address
48 * format. Block length is fixed to 512 bytes as per the SD High
49 * Capacity specification.
53 if (mmc->high_capacity) {
54 tmp = (u64)code_offset * blklen;
55 tmp += code_len * blklen;
57 tmp = code_offset + code_len;
59 if ((tmp + CONFIG_ENV_SIZE > mmc->capacity) ||
61 *env_addr = ESDHC_DEFAULT_ENVADDR;
70 *env_addr = code_offset + code_len;