ata_pthru.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* ata_pthru.c - ATA pass through for ata.mod. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2009 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/ata.h>
  20. #include <grub/disk.h>
  21. #include <grub/dl.h>
  22. #include <grub/mm.h>
  23. /* ATA pass through support, used by hdparm.mod. */
  24. static grub_err_t
  25. grub_ata_pass_through (grub_disk_t disk,
  26. struct grub_disk_ata_pass_through_parms *parms)
  27. {
  28. if (disk->dev->id != GRUB_DISK_DEVICE_ATA_ID)
  29. return grub_error (GRUB_ERR_BAD_DEVICE,
  30. "device not accessed via ata.mod");
  31. struct grub_ata_device *dev = (struct grub_ata_device *) disk->data;
  32. if (! (parms->size == 0 || parms->size == GRUB_DISK_SECTOR_SIZE))
  33. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  34. "ATA multi-sector read and DATA OUT not implemented");
  35. grub_dprintf ("ata", "ata_pass_through: cmd=0x%x, features=0x%x, sectors=0x%x\n",
  36. parms->taskfile[GRUB_ATA_REG_CMD],
  37. parms->taskfile[GRUB_ATA_REG_FEATURES],
  38. parms->taskfile[GRUB_ATA_REG_SECTORS]);
  39. grub_dprintf ("ata", "lba_high=0x%x, lba_mid=0x%x, lba_low=0x%x, size=%d\n",
  40. parms->taskfile[GRUB_ATA_REG_LBAHIGH],
  41. parms->taskfile[GRUB_ATA_REG_LBAMID],
  42. parms->taskfile[GRUB_ATA_REG_LBALOW], parms->size);
  43. /* Set registers. */
  44. grub_ata_regset (dev, GRUB_ATA_REG_DISK, 0xE0 | dev->device << 4
  45. | (parms->taskfile[GRUB_ATA_REG_DISK] & 0xf));
  46. if (grub_ata_check_ready (dev))
  47. return grub_errno;
  48. int i;
  49. for (i = GRUB_ATA_REG_FEATURES; i <= GRUB_ATA_REG_LBAHIGH; i++)
  50. grub_ata_regset (dev, i, parms->taskfile[i]);
  51. /* Start command. */
  52. grub_ata_regset (dev, GRUB_ATA_REG_CMD, parms->taskfile[GRUB_ATA_REG_CMD]);
  53. /* Wait for !BSY. */
  54. if (grub_ata_wait_not_busy (dev, GRUB_ATA_TOUT_DATA))
  55. return grub_errno;
  56. /* Check status. */
  57. grub_int8_t sts = grub_ata_regget (dev, GRUB_ATA_REG_STATUS);
  58. grub_dprintf ("ata", "status=0x%x\n", sts);
  59. /* Transfer data. */
  60. if ((sts & (GRUB_ATA_STATUS_DRQ | GRUB_ATA_STATUS_ERR)) == GRUB_ATA_STATUS_DRQ)
  61. {
  62. if (parms->size != GRUB_DISK_SECTOR_SIZE)
  63. return grub_error (GRUB_ERR_READ_ERROR, "DRQ unexpected");
  64. grub_ata_pio_read (dev, parms->buffer, GRUB_DISK_SECTOR_SIZE);
  65. }
  66. /* Return registers. */
  67. for (i = GRUB_ATA_REG_ERROR; i <= GRUB_ATA_REG_STATUS; i++)
  68. parms->taskfile[i] = grub_ata_regget (dev, i);
  69. grub_dprintf ("ata", "status=0x%x, error=0x%x, sectors=0x%x\n",
  70. parms->taskfile[GRUB_ATA_REG_STATUS],
  71. parms->taskfile[GRUB_ATA_REG_ERROR],
  72. parms->taskfile[GRUB_ATA_REG_SECTORS]);
  73. if (parms->taskfile[GRUB_ATA_REG_STATUS]
  74. & (GRUB_ATA_STATUS_DRQ | GRUB_ATA_STATUS_ERR))
  75. return grub_error (GRUB_ERR_READ_ERROR, "ATA passthrough failed");
  76. return GRUB_ERR_NONE;
  77. }
  78. GRUB_MOD_INIT(ata_pthru)
  79. {
  80. /* Register ATA pass through function. */
  81. grub_disk_ata_pass_through = grub_ata_pass_through;
  82. }
  83. GRUB_MOD_FINI(ata_pthru)
  84. {
  85. if (grub_disk_ata_pass_through == grub_ata_pass_through)
  86. grub_disk_ata_pass_through = NULL;
  87. }