sdhc_boot.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /*
  2. * Copyright 2011 Freescale Semiconductor, Inc.
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <mmc.h>
  8. #include <malloc.h>
  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. int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr)
  17. {
  18. u8 *tmp_buf;
  19. u32 blklen, code_offset, code_len, n;
  20. blklen = mmc->read_bl_len;
  21. tmp_buf = malloc(blklen);
  22. if (!tmp_buf)
  23. return 1;
  24. /* read out the first block, get the config data information */
  25. n = mmc->block_dev.block_read(mmc->block_dev.dev, 0, 1, tmp_buf);
  26. if (!n) {
  27. free(tmp_buf);
  28. return 1;
  29. }
  30. /* Get the Source Address, from offset 0x50 */
  31. code_offset = *(u32 *)(tmp_buf + ESDHC_BOOT_IMAGE_ADDR);
  32. /* Get the code size from offset 0x48 */
  33. code_len = *(u32 *)(tmp_buf + ESDHC_BOOT_IMAGE_SIZE);
  34. *env_addr = code_offset + code_len;
  35. free(tmp_buf);
  36. return 0;
  37. }