sdhc_boot.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright 2011 Freescale Semiconductor, Inc.
  3. *
  4. * See file CREDITS for list of people who contributed to this
  5. * project.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  20. * MA 02111-1307 USA
  21. */
  22. #include <common.h>
  23. #include <mmc.h>
  24. #include <malloc.h>
  25. /*
  26. * The environment variables are written to just after the u-boot image
  27. * on SDCard, so we must read the MBR to get the start address and code
  28. * length of the u-boot image, then calculate the address of the env.
  29. */
  30. #define ESDHC_BOOT_IMAGE_SIZE 0x48
  31. #define ESDHC_BOOT_IMAGE_ADDR 0x50
  32. int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr)
  33. {
  34. u8 *tmp_buf;
  35. u32 blklen, code_offset, code_len, n;
  36. blklen = mmc->read_bl_len;
  37. tmp_buf = malloc(blklen);
  38. if (!tmp_buf)
  39. return 1;
  40. /* read out the first block, get the config data information */
  41. n = mmc->block_dev.block_read(&mmc->block_dev, 0, 1, tmp_buf);
  42. if (!n) {
  43. free(tmp_buf);
  44. return 1;
  45. }
  46. /* Get the Source Address, from offset 0x50 */
  47. code_offset = *(u32 *)(tmp_buf + ESDHC_BOOT_IMAGE_ADDR);
  48. /* Get the code size from offset 0x48 */
  49. code_len = *(u32 *)(tmp_buf + ESDHC_BOOT_IMAGE_SIZE);
  50. *env_addr = code_offset + code_len;
  51. free(tmp_buf);
  52. return 0;
  53. }