spl_sata.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2013
  4. * Texas Instruments, <www.ti.com>
  5. *
  6. * Dan Murphy <dmurphy@ti.com>
  7. *
  8. * Derived work from spl_usb.c
  9. */
  10. #include <common.h>
  11. #include <spl.h>
  12. #include <asm/u-boot.h>
  13. #include <sata.h>
  14. #include <scsi.h>
  15. #include <errno.h>
  16. #include <fat.h>
  17. #include <image.h>
  18. #ifndef CONFIG_SYS_SATA_FAT_BOOT_PARTITION
  19. #define CONFIG_SYS_SATA_FAT_BOOT_PARTITION 1
  20. #endif
  21. #ifndef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
  22. #define CONFIG_SPL_FS_LOAD_PAYLOAD_NAME "u-boot.img"
  23. #endif
  24. #ifndef CONFIG_SPL_SATA_RAW_U_BOOT_SECTOR
  25. /* Dummy value to make the compiler happy */
  26. #define CONFIG_SPL_SATA_RAW_U_BOOT_SECTOR 0x100
  27. #endif
  28. static int spl_sata_load_image_raw(struct spl_image_info *spl_image,
  29. struct blk_desc *stor_dev, unsigned long sector)
  30. {
  31. struct image_header *header;
  32. unsigned long count;
  33. u32 image_size_sectors;
  34. u32 image_offset_sectors;
  35. u32 image_offset;
  36. int ret;
  37. header = spl_get_load_buffer(-sizeof(*header), stor_dev->blksz);
  38. count = blk_dread(stor_dev, sector, 1, header);
  39. if (count == 0)
  40. return -EIO;
  41. ret = spl_parse_image_header(spl_image, header);
  42. if (ret)
  43. return ret;
  44. image_size_sectors = DIV_ROUND_UP(spl_image->size, stor_dev->blksz);
  45. image_offset_sectors = spl_image->offset / stor_dev->blksz;
  46. image_offset = spl_image->offset % stor_dev->blksz;
  47. count = blk_dread(stor_dev, sector + image_offset_sectors,
  48. image_size_sectors,
  49. (void *)spl_image->load_addr);
  50. if (count != image_size_sectors)
  51. return -EIO;
  52. if (image_offset)
  53. memmove((void *)spl_image->load_addr,
  54. (void *)spl_image->load_addr + image_offset,
  55. spl_image->size);
  56. return 0;
  57. }
  58. static int spl_sata_load_image(struct spl_image_info *spl_image,
  59. struct spl_boot_device *bootdev)
  60. {
  61. int err = 0;
  62. struct blk_desc *stor_dev;
  63. #if !defined(CONFIG_DM_SCSI) && !defined(CONFIG_AHCI)
  64. err = init_sata(CONFIG_SPL_SATA_BOOT_DEVICE);
  65. #endif
  66. if (err) {
  67. #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
  68. printf("spl: sata init failed: err - %d\n", err);
  69. #endif
  70. return err;
  71. } else {
  72. /* try to recognize storage devices immediately */
  73. scsi_scan(false);
  74. stor_dev = blk_get_devnum_by_type(IF_TYPE_SCSI, 0);
  75. if (!stor_dev)
  76. return -ENODEV;
  77. }
  78. #ifdef CONFIG_SPL_OS_BOOT
  79. if (spl_start_uboot() ||
  80. spl_load_image_fat_os(spl_image, stor_dev,
  81. CONFIG_SYS_SATA_FAT_BOOT_PARTITION))
  82. #endif
  83. {
  84. err = -ENOSYS;
  85. if (IS_ENABLED(CONFIG_SPL_FS_FAT)) {
  86. err = spl_load_image_fat(spl_image, stor_dev,
  87. CONFIG_SYS_SATA_FAT_BOOT_PARTITION,
  88. CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
  89. } else if (IS_ENABLED(CONFIG_SPL_SATA_RAW_U_BOOT_USE_SECTOR)) {
  90. err = spl_sata_load_image_raw(spl_image, stor_dev,
  91. CONFIG_SPL_SATA_RAW_U_BOOT_SECTOR);
  92. }
  93. }
  94. if (err) {
  95. puts("Error loading sata device\n");
  96. return err;
  97. }
  98. return 0;
  99. }
  100. SPL_LOAD_IMAGE_METHOD("SATA", 0, BOOT_DEVICE_SATA, spl_sata_load_image);