spl_mmc.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * (C) Copyright 2010
  3. * Texas Instruments, <www.ti.com>
  4. *
  5. * Aneesh V <aneesh@ti.com>
  6. *
  7. * See file CREDITS for list of people who contributed to this
  8. * project.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. */
  25. #include <common.h>
  26. #include <spl.h>
  27. #include <asm/u-boot.h>
  28. #include <asm/utils.h>
  29. #include <mmc.h>
  30. #include <fat.h>
  31. #include <version.h>
  32. DECLARE_GLOBAL_DATA_PTR;
  33. static int mmc_load_image_raw(struct mmc *mmc, unsigned long sector)
  34. {
  35. unsigned long err;
  36. u32 image_size_sectors;
  37. struct image_header *header;
  38. header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
  39. sizeof(struct image_header));
  40. /* read image header to find the image size & load address */
  41. err = mmc->block_dev.block_read(0, sector, 1, header);
  42. if (err == 0)
  43. goto end;
  44. spl_parse_image_header(header);
  45. /* convert size to sectors - round up */
  46. image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) /
  47. mmc->read_bl_len;
  48. /* Read the header too to avoid extra memcpy */
  49. err = mmc->block_dev.block_read(0, sector, image_size_sectors,
  50. (void *)spl_image.load_addr);
  51. end:
  52. if (err == 0)
  53. printf("spl: mmc blk read err - %lu\n", err);
  54. return (err == 0);
  55. }
  56. #ifdef CONFIG_SPL_FAT_SUPPORT
  57. static int mmc_load_image_fat(struct mmc *mmc, const char *filename)
  58. {
  59. int err;
  60. struct image_header *header;
  61. header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
  62. sizeof(struct image_header));
  63. err = file_fat_read(filename, header, sizeof(struct image_header));
  64. if (err <= 0)
  65. goto end;
  66. spl_parse_image_header(header);
  67. err = file_fat_read(filename, (u8 *)spl_image.load_addr, 0);
  68. end:
  69. if (err <= 0)
  70. printf("spl: error reading image %s, err - %d\n",
  71. filename, err);
  72. return (err <= 0);
  73. }
  74. #ifdef CONFIG_SPL_OS_BOOT
  75. static int mmc_load_image_fat_os(struct mmc *mmc)
  76. {
  77. int err;
  78. err = file_fat_read(CONFIG_SPL_FAT_LOAD_ARGS_NAME,
  79. (void *)CONFIG_SYS_SPL_ARGS_ADDR, 0);
  80. if (err <= 0) {
  81. printf("spl: error reading image %s, err - %d\n",
  82. CONFIG_SPL_FAT_LOAD_ARGS_NAME, err);
  83. return -1;
  84. }
  85. return mmc_load_image_fat(mmc, CONFIG_SPL_FAT_LOAD_KERNEL_NAME);
  86. }
  87. #endif
  88. #endif
  89. void spl_mmc_load_image(void)
  90. {
  91. struct mmc *mmc;
  92. int err;
  93. u32 boot_mode;
  94. mmc_initialize(gd->bd);
  95. /* We register only one device. So, the dev id is always 0 */
  96. mmc = find_mmc_device(0);
  97. if (!mmc) {
  98. puts("spl: mmc device not found!!\n");
  99. hang();
  100. }
  101. err = mmc_init(mmc);
  102. if (err) {
  103. printf("spl: mmc init failed: err - %d\n", err);
  104. hang();
  105. }
  106. boot_mode = spl_boot_mode();
  107. if (boot_mode == MMCSD_MODE_RAW) {
  108. debug("boot mode - RAW\n");
  109. err = mmc_load_image_raw(mmc,
  110. CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
  111. #ifdef CONFIG_SPL_FAT_SUPPORT
  112. } else if (boot_mode == MMCSD_MODE_FAT) {
  113. debug("boot mode - FAT\n");
  114. err = fat_register_device(&mmc->block_dev,
  115. CONFIG_SYS_MMC_SD_FAT_BOOT_PARTITION);
  116. if (err) {
  117. printf("spl: fat register err - %d\n", err);
  118. hang();
  119. }
  120. #ifdef CONFIG_SPL_OS_BOOT
  121. if (spl_start_uboot() || mmc_load_image_fat_os(mmc))
  122. #endif
  123. err = mmc_load_image_fat(mmc, CONFIG_SPL_FAT_LOAD_PAYLOAD_NAME);
  124. #endif
  125. } else {
  126. puts("spl: wrong MMC boot mode\n");
  127. hang();
  128. }
  129. if (err)
  130. hang();
  131. }