blob: 5ee730cefd044ba9ee187bde7657a83ff5bcc767 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Jerry Huang2c766ba2011-01-24 17:09:54 +00002/*
3 * Copyright 2011 Freescale Semiconductor, Inc.
Jerry Huang2c766ba2011-01-24 17:09:54 +00004 */
5
Jerry Huang2c766ba2011-01-24 17:09:54 +00006#include <mmc.h>
7#include <malloc.h>
8
9/*
10 * The environment variables are written to just after the u-boot image
11 * on SDCard, so we must read the MBR to get the start address and code
12 * length of the u-boot image, then calculate the address of the env.
13 */
14#define ESDHC_BOOT_IMAGE_SIZE 0x48
15#define ESDHC_BOOT_IMAGE_ADDR 0x50
16
Haijun.Zhang22e3c422014-01-10 13:52:19 +080017#define ESDHC_DEFAULT_ENVADDR 0x400
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 */
Yinbo Zhuea3ff842019-10-15 17:20:49 +080030#ifdef CONFIG_BLK
31 n = blk_dread(mmc_get_blk_desc(mmc), 0, 1, tmp_buf);
32#else
Stephen Warrene73f2962015-12-07 11:38:48 -070033 n = mmc->block_dev.block_read(&mmc->block_dev, 0, 1, tmp_buf);
Yinbo Zhuea3ff842019-10-15 17:20:49 +080034#endif
Jerry Huang2c766ba2011-01-24 17:09:54 +000035 if (!n) {
36 free(tmp_buf);
37 return 1;
38 }
39
40 /* Get the Source Address, from offset 0x50 */
41 code_offset = *(u32 *)(tmp_buf + ESDHC_BOOT_IMAGE_ADDR);
42
43 /* Get the code size from offset 0x48 */
44 code_len = *(u32 *)(tmp_buf + ESDHC_BOOT_IMAGE_SIZE);
45
Haijun.Zhang22e3c422014-01-10 13:52:19 +080046#ifdef CONFIG_ESDHC_HC_BLK_ADDR
47 /*
48 * On soc BSC9131, BSC9132:
49 * In High Capacity SD Cards (> 2 GBytes), the 32-bit source address and
50 * code length of these soc specify the memory address in block address
51 * format. Block length is fixed to 512 bytes as per the SD High
52 * Capacity specification.
53 */
54 u64 tmp;
55
56 if (mmc->high_capacity) {
57 tmp = (u64)code_offset * blklen;
58 tmp += code_len * blklen;
59 } else
60 tmp = code_offset + code_len;
61
62 if ((tmp + CONFIG_ENV_SIZE > mmc->capacity) ||
63 (tmp > 0xFFFFFFFFU))
64 *env_addr = ESDHC_DEFAULT_ENVADDR;
65 else
66 *env_addr = tmp;
67
68 free(tmp_buf);
69
70 return 0;
71#endif
72
Jerry Huang2c766ba2011-01-24 17:09:54 +000073 *env_addr = code_offset + code_len;
74
75 free(tmp_buf);
76
77 return 0;
78}