sdhc_boot.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2011 Freescale Semiconductor, Inc.
  4. */
  5. #include <common.h>
  6. #include <mmc.h>
  7. #include <malloc.h>
  8. /*
  9. * The environment variables are written to just after the u-boot image
  10. * on SDCard, so we must read the MBR to get the start address and code
  11. * length of the u-boot image, then calculate the address of the env.
  12. */
  13. #define ESDHC_BOOT_IMAGE_SIZE 0x48
  14. #define ESDHC_BOOT_IMAGE_ADDR 0x50
  15. #define ESDHC_DEFAULT_ENVADDR 0x400
  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, 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. #ifdef CONFIG_ESDHC_HC_BLK_ADDR
  35. /*
  36. * On soc BSC9131, BSC9132:
  37. * In High Capacity SD Cards (> 2 GBytes), the 32-bit source address and
  38. * code length of these soc specify the memory address in block address
  39. * format. Block length is fixed to 512 bytes as per the SD High
  40. * Capacity specification.
  41. */
  42. u64 tmp;
  43. if (mmc->high_capacity) {
  44. tmp = (u64)code_offset * blklen;
  45. tmp += code_len * blklen;
  46. } else
  47. tmp = code_offset + code_len;
  48. if ((tmp + CONFIG_ENV_SIZE > mmc->capacity) ||
  49. (tmp > 0xFFFFFFFFU))
  50. *env_addr = ESDHC_DEFAULT_ENVADDR;
  51. else
  52. *env_addr = tmp;
  53. free(tmp_buf);
  54. return 0;
  55. #endif
  56. *env_addr = code_offset + code_len;
  57. free(tmp_buf);
  58. return 0;
  59. }