ata.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* ata.h - ATA disk access. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2007, 2008, 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. #ifndef GRUB_ATA_HEADER
  20. #define GRUB_ATA_HEADER 1
  21. #include <grub/misc.h>
  22. #include <grub/symbol.h>
  23. /* XXX: For now this only works on i386. */
  24. #include <grub/cpu/io.h>
  25. typedef enum
  26. {
  27. GRUB_ATA_CHS,
  28. GRUB_ATA_LBA,
  29. GRUB_ATA_LBA48
  30. } grub_ata_addressing_t;
  31. #define GRUB_ATA_REG_DATA 0
  32. #define GRUB_ATA_REG_ERROR 1
  33. #define GRUB_ATA_REG_FEATURES 1
  34. #define GRUB_ATA_REG_SECTORS 2
  35. #define GRUB_ATAPI_REG_IREASON 2
  36. #define GRUB_ATA_REG_SECTNUM 3
  37. #define GRUB_ATA_REG_CYLLSB 4
  38. #define GRUB_ATA_REG_CYLMSB 5
  39. #define GRUB_ATA_REG_LBALOW 3
  40. #define GRUB_ATA_REG_LBAMID 4
  41. #define GRUB_ATAPI_REG_CNTLOW 4
  42. #define GRUB_ATA_REG_LBAHIGH 5
  43. #define GRUB_ATAPI_REG_CNTHIGH 5
  44. #define GRUB_ATA_REG_DISK 6
  45. #define GRUB_ATA_REG_CMD 7
  46. #define GRUB_ATA_REG_STATUS 7
  47. #define GRUB_ATA_REG2_CONTROL 0
  48. #define GRUB_ATA_STATUS_ERR 0x01
  49. #define GRUB_ATA_STATUS_INDEX 0x02
  50. #define GRUB_ATA_STATUS_ECC 0x04
  51. #define GRUB_ATA_STATUS_DRQ 0x08
  52. #define GRUB_ATA_STATUS_SEEK 0x10
  53. #define GRUB_ATA_STATUS_WRERR 0x20
  54. #define GRUB_ATA_STATUS_READY 0x40
  55. #define GRUB_ATA_STATUS_BUSY 0x80
  56. /* ATAPI interrupt reason values (I/O, D/C bits). */
  57. #define GRUB_ATAPI_IREASON_MASK 0x3
  58. #define GRUB_ATAPI_IREASON_DATA_OUT 0x0
  59. #define GRUB_ATAPI_IREASON_CMD_OUT 0x1
  60. #define GRUB_ATAPI_IREASON_DATA_IN 0x2
  61. #define GRUB_ATAPI_IREASON_ERROR 0x3
  62. enum grub_ata_commands
  63. {
  64. GRUB_ATA_CMD_CHECK_POWER_MODE = 0xe5,
  65. GRUB_ATA_CMD_IDENTIFY_DEVICE = 0xec,
  66. GRUB_ATA_CMD_IDENTIFY_PACKET_DEVICE = 0xa1,
  67. GRUB_ATA_CMD_IDLE = 0xe3,
  68. GRUB_ATA_CMD_PACKET = 0xa0,
  69. GRUB_ATA_CMD_READ_SECTORS = 0x20,
  70. GRUB_ATA_CMD_READ_SECTORS_EXT = 0x24,
  71. GRUB_ATA_CMD_SECURITY_FREEZE_LOCK = 0xf5,
  72. GRUB_ATA_CMD_SET_FEATURES = 0xef,
  73. GRUB_ATA_CMD_SLEEP = 0xe6,
  74. GRUB_ATA_CMD_SMART = 0xb0,
  75. GRUB_ATA_CMD_STANDBY_IMMEDIATE = 0xe0,
  76. GRUB_ATA_CMD_WRITE_SECTORS = 0x30,
  77. GRUB_ATA_CMD_WRITE_SECTORS_EXT = 0x34,
  78. };
  79. enum grub_ata_timeout_milliseconds
  80. {
  81. GRUB_ATA_TOUT_STD = 1000, /* 1s standard timeout. */
  82. GRUB_ATA_TOUT_DATA = 10000 /* 10s DATA I/O timeout. */
  83. };
  84. struct grub_ata_device
  85. {
  86. /* IDE port to use. */
  87. int port;
  88. /* IO addresses on which the registers for this device can be
  89. found. */
  90. grub_port_t ioaddress;
  91. grub_port_t ioaddress2;
  92. /* Two devices can be connected to a single cable. Use this field
  93. to select device 0 (commonly known as "master") or device 1
  94. (commonly known as "slave"). */
  95. int device;
  96. /* Addressing methods available for accessing this device. If CHS
  97. is only available, use that. Otherwise use LBA, except for the
  98. high sectors. In that case use LBA48. */
  99. grub_ata_addressing_t addr;
  100. /* Sector count. */
  101. grub_uint64_t size;
  102. /* CHS maximums. */
  103. grub_uint16_t cylinders;
  104. grub_uint16_t heads;
  105. grub_uint16_t sectors_per_track;
  106. /* Set to 0 for ATA, set to 1 for ATAPI. */
  107. int atapi;
  108. struct grub_ata_device *next;
  109. };
  110. grub_err_t grub_ata_wait_not_busy (struct grub_ata_device *dev,
  111. int milliseconds);
  112. grub_err_t grub_ata_wait_drq (struct grub_ata_device *dev,
  113. int rw, int milliseconds);
  114. void grub_ata_pio_read (struct grub_ata_device *dev,
  115. char *buf, grub_size_t size);
  116. static inline void
  117. grub_ata_regset (struct grub_ata_device *dev, int reg, int val)
  118. {
  119. grub_outb (val, dev->ioaddress + reg);
  120. }
  121. static inline grub_uint8_t
  122. grub_ata_regget (struct grub_ata_device *dev, int reg)
  123. {
  124. return grub_inb (dev->ioaddress + reg);
  125. }
  126. static inline void
  127. grub_ata_regset2 (struct grub_ata_device *dev, int reg, int val)
  128. {
  129. grub_outb (val, dev->ioaddress2 + reg);
  130. }
  131. static inline grub_uint8_t
  132. grub_ata_regget2 (struct grub_ata_device *dev, int reg)
  133. {
  134. return grub_inb (dev->ioaddress2 + reg);
  135. }
  136. static inline grub_err_t
  137. grub_ata_check_ready (struct grub_ata_device *dev)
  138. {
  139. if (grub_ata_regget (dev, GRUB_ATA_REG_STATUS) & GRUB_ATA_STATUS_BUSY)
  140. return grub_ata_wait_not_busy (dev, GRUB_ATA_TOUT_STD);
  141. return GRUB_ERR_NONE;
  142. }
  143. #endif /* ! GRUB_ATA_HEADER */