spl_sata.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. static int spl_sata_load_image_raw(struct spl_image_info *spl_image,
  19. struct spl_boot_device *bootdev,
  20. struct blk_desc *stor_dev, unsigned long sector)
  21. {
  22. struct legacy_img_hdr *header;
  23. unsigned long count;
  24. u32 image_size_sectors;
  25. u32 image_offset_sectors;
  26. u32 image_offset;
  27. int ret;
  28. header = spl_get_load_buffer(-sizeof(*header), stor_dev->blksz);
  29. count = blk_dread(stor_dev, sector, 1, header);
  30. if (count == 0)
  31. return -EIO;
  32. ret = spl_parse_image_header(spl_image, bootdev, header);
  33. if (ret)
  34. return ret;
  35. image_size_sectors = DIV_ROUND_UP(spl_image->size, stor_dev->blksz);
  36. image_offset_sectors = spl_image->offset / stor_dev->blksz;
  37. image_offset = spl_image->offset % stor_dev->blksz;
  38. count = blk_dread(stor_dev, sector + image_offset_sectors,
  39. image_size_sectors,
  40. (void *)spl_image->load_addr);
  41. if (count != image_size_sectors)
  42. return -EIO;
  43. if (image_offset)
  44. memmove((void *)spl_image->load_addr,
  45. (void *)spl_image->load_addr + image_offset,
  46. spl_image->size);
  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 = -ENOSYS;
  53. struct blk_desc *stor_dev;
  54. /* try to recognize storage devices immediately */
  55. scsi_scan(false);
  56. stor_dev = blk_get_devnum_by_uclass_id(UCLASS_SCSI, 0);
  57. if (!stor_dev)
  58. return -ENODEV;
  59. #if CONFIG_IS_ENABLED(OS_BOOT)
  60. if (spl_start_uboot() ||
  61. spl_load_image_fat_os(spl_image, bootdev, stor_dev,
  62. CONFIG_SYS_SATA_FAT_BOOT_PARTITION))
  63. #endif
  64. {
  65. #ifdef CONFIG_SPL_FS_FAT
  66. err = spl_load_image_fat(spl_image, bootdev, stor_dev,
  67. CONFIG_SYS_SATA_FAT_BOOT_PARTITION,
  68. CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
  69. #elif defined(CONFIG_SPL_SATA_RAW_U_BOOT_USE_SECTOR)
  70. err = spl_sata_load_image_raw(spl_image, bootdev, stor_dev,
  71. CONFIG_SPL_SATA_RAW_U_BOOT_SECTOR);
  72. #endif
  73. }
  74. if (err) {
  75. puts("Error loading sata device\n");
  76. return err;
  77. }
  78. return 0;
  79. }
  80. SPL_LOAD_IMAGE_METHOD("SATA", 0, BOOT_DEVICE_SATA, spl_sata_load_image);