spl_sata.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. int ret;
  35. header = spl_get_load_buffer(-sizeof(*header), stor_dev->blksz);
  36. count = blk_dread(stor_dev, sector, 1, header);
  37. if (count == 0)
  38. return -EIO;
  39. ret = spl_parse_image_header(spl_image, header);
  40. if (ret)
  41. return ret;
  42. image_size_sectors = DIV_ROUND_UP(spl_image->size, stor_dev->blksz);
  43. count = blk_dread(stor_dev, sector, image_size_sectors,
  44. (void *)spl_image->load_addr);
  45. if (count != image_size_sectors)
  46. return -EIO;
  47. return 0;
  48. }
  49. static int spl_sata_load_image(struct spl_image_info *spl_image,
  50. struct spl_boot_device *bootdev)
  51. {
  52. int err = 0;
  53. struct blk_desc *stor_dev;
  54. #if !defined(CONFIG_DM_SCSI) && !defined(CONFIG_AHCI)
  55. err = init_sata(CONFIG_SPL_SATA_BOOT_DEVICE);
  56. #endif
  57. if (err) {
  58. #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
  59. printf("spl: sata init failed: err - %d\n", err);
  60. #endif
  61. return err;
  62. } else {
  63. /* try to recognize storage devices immediately */
  64. scsi_scan(false);
  65. stor_dev = blk_get_devnum_by_type(IF_TYPE_SCSI, 0);
  66. if (!stor_dev)
  67. return -ENODEV;
  68. }
  69. #ifdef CONFIG_SPL_OS_BOOT
  70. if (spl_start_uboot() ||
  71. spl_load_image_fat_os(spl_image, stor_dev,
  72. CONFIG_SYS_SATA_FAT_BOOT_PARTITION))
  73. #endif
  74. {
  75. err = -ENOSYS;
  76. if (IS_ENABLED(CONFIG_SPL_FS_FAT)) {
  77. err = spl_load_image_fat(spl_image, stor_dev,
  78. CONFIG_SYS_SATA_FAT_BOOT_PARTITION,
  79. CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
  80. } else if (IS_ENABLED(CONFIG_SPL_SATA_RAW_U_BOOT_USE_SECTOR)) {
  81. err = spl_sata_load_image_raw(spl_image, stor_dev,
  82. CONFIG_SPL_SATA_RAW_U_BOOT_SECTOR);
  83. }
  84. }
  85. if (err) {
  86. puts("Error loading sata device\n");
  87. return err;
  88. }
  89. return 0;
  90. }
  91. SPL_LOAD_IMAGE_METHOD("SATA", 0, BOOT_DEVICE_SATA, spl_sata_load_image);