ata.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. #include <grub/disk.h>
  24. /* XXX: For now this only works on i386. */
  25. #include <grub/cpu/io.h>
  26. typedef enum
  27. {
  28. GRUB_ATA_CHS,
  29. GRUB_ATA_LBA,
  30. GRUB_ATA_LBA48
  31. } grub_ata_addressing_t;
  32. #define GRUB_ATA_CH0_PORT1 0x1f0
  33. #define GRUB_ATA_CH1_PORT1 0x170
  34. #define GRUB_ATA_CH0_PORT2 0x3f6
  35. #define GRUB_ATA_CH1_PORT2 0x376
  36. #define GRUB_ATA_REG_DATA 0
  37. #define GRUB_ATA_REG_ERROR 1
  38. #define GRUB_ATA_REG_FEATURES 1
  39. #define GRUB_ATA_REG_SECTORS 2
  40. #define GRUB_ATAPI_REG_IREASON 2
  41. #define GRUB_ATA_REG_SECTNUM 3
  42. #define GRUB_ATA_REG_CYLLSB 4
  43. #define GRUB_ATA_REG_CYLMSB 5
  44. #define GRUB_ATA_REG_LBALOW 3
  45. #define GRUB_ATA_REG_LBAMID 4
  46. #define GRUB_ATAPI_REG_CNTLOW 4
  47. #define GRUB_ATA_REG_LBAHIGH 5
  48. #define GRUB_ATAPI_REG_CNTHIGH 5
  49. #define GRUB_ATA_REG_DISK 6
  50. #define GRUB_ATA_REG_CMD 7
  51. #define GRUB_ATA_REG_STATUS 7
  52. #define GRUB_ATA_REG2_CONTROL 0
  53. #define GRUB_ATA_STATUS_ERR 0x01
  54. #define GRUB_ATA_STATUS_INDEX 0x02
  55. #define GRUB_ATA_STATUS_ECC 0x04
  56. #define GRUB_ATA_STATUS_DRQ 0x08
  57. #define GRUB_ATA_STATUS_SEEK 0x10
  58. #define GRUB_ATA_STATUS_WRERR 0x20
  59. #define GRUB_ATA_STATUS_READY 0x40
  60. #define GRUB_ATA_STATUS_BUSY 0x80
  61. /* ATAPI interrupt reason values (I/O, D/C bits). */
  62. #define GRUB_ATAPI_IREASON_MASK 0x3
  63. #define GRUB_ATAPI_IREASON_DATA_OUT 0x0
  64. #define GRUB_ATAPI_IREASON_CMD_OUT 0x1
  65. #define GRUB_ATAPI_IREASON_DATA_IN 0x2
  66. #define GRUB_ATAPI_IREASON_ERROR 0x3
  67. enum grub_ata_commands
  68. {
  69. GRUB_ATA_CMD_CHECK_POWER_MODE = 0xe5,
  70. GRUB_ATA_CMD_IDENTIFY_DEVICE = 0xec,
  71. GRUB_ATA_CMD_IDENTIFY_PACKET_DEVICE = 0xa1,
  72. GRUB_ATA_CMD_IDLE = 0xe3,
  73. GRUB_ATA_CMD_PACKET = 0xa0,
  74. GRUB_ATA_CMD_READ_SECTORS = 0x20,
  75. GRUB_ATA_CMD_READ_SECTORS_EXT = 0x24,
  76. GRUB_ATA_CMD_READ_SECTORS_DMA = 0xc8,
  77. GRUB_ATA_CMD_READ_SECTORS_DMA_EXT = 0x25,
  78. GRUB_ATA_CMD_SECURITY_FREEZE_LOCK = 0xf5,
  79. GRUB_ATA_CMD_SET_FEATURES = 0xef,
  80. GRUB_ATA_CMD_SLEEP = 0xe6,
  81. GRUB_ATA_CMD_SMART = 0xb0,
  82. GRUB_ATA_CMD_STANDBY_IMMEDIATE = 0xe0,
  83. GRUB_ATA_CMD_WRITE_SECTORS = 0x30,
  84. GRUB_ATA_CMD_WRITE_SECTORS_EXT = 0x34,
  85. GRUB_ATA_CMD_WRITE_SECTORS_DMA_EXT = 0x35,
  86. GRUB_ATA_CMD_WRITE_SECTORS_DMA = 0xca,
  87. };
  88. enum grub_ata_timeout_milliseconds
  89. {
  90. GRUB_ATA_TOUT_STD = 1000, /* 1s standard timeout. */
  91. GRUB_ATA_TOUT_DATA = 10000, /* 10s DATA I/O timeout. */
  92. GRUB_ATA_TOUT_SPINUP = 10000, /* Give the device 10s on first try to spinon. */
  93. };
  94. typedef union
  95. {
  96. grub_uint8_t raw[11];
  97. struct
  98. {
  99. union
  100. {
  101. grub_uint8_t features;
  102. grub_uint8_t error;
  103. };
  104. union
  105. {
  106. grub_uint8_t sectors;
  107. grub_uint8_t atapi_ireason;
  108. };
  109. union
  110. {
  111. grub_uint8_t lba_low;
  112. grub_uint8_t sectnum;
  113. };
  114. union
  115. {
  116. grub_uint8_t lba_mid;
  117. grub_uint8_t cyllsb;
  118. grub_uint8_t atapi_cntlow;
  119. };
  120. union
  121. {
  122. grub_uint8_t lba_high;
  123. grub_uint8_t cylmsb;
  124. grub_uint8_t atapi_cnthigh;
  125. };
  126. grub_uint8_t disk;
  127. union
  128. {
  129. grub_uint8_t cmd;
  130. grub_uint8_t status;
  131. };
  132. grub_uint8_t sectors48;
  133. grub_uint8_t lba48_low;
  134. grub_uint8_t lba48_mid;
  135. grub_uint8_t lba48_high;
  136. };
  137. } grub_ata_regs_t;
  138. /* ATA pass through parameters and function. */
  139. struct grub_disk_ata_pass_through_parms
  140. {
  141. grub_ata_regs_t taskfile;
  142. void * buffer;
  143. grub_size_t size;
  144. int write;
  145. void *cmd;
  146. int cmdsize;
  147. int dma;
  148. };
  149. struct grub_ata
  150. {
  151. /* Addressing methods available for accessing this device. If CHS
  152. is only available, use that. Otherwise use LBA, except for the
  153. high sectors. In that case use LBA48. */
  154. grub_ata_addressing_t addr;
  155. /* Sector count. */
  156. grub_uint64_t size;
  157. grub_uint32_t log_sector_size;
  158. /* CHS maximums. */
  159. grub_uint16_t cylinders;
  160. grub_uint16_t heads;
  161. grub_uint16_t sectors_per_track;
  162. /* Set to 0 for ATA, set to 1 for ATAPI. */
  163. int atapi;
  164. int dma;
  165. grub_size_t maxbuffer;
  166. int *present;
  167. void *data;
  168. struct grub_ata_dev *dev;
  169. };
  170. typedef struct grub_ata *grub_ata_t;
  171. typedef int (*grub_ata_dev_iterate_hook_t) (int id, int bus, void *data);
  172. struct grub_ata_dev
  173. {
  174. /* Call HOOK with each device name, until HOOK returns non-zero. */
  175. int (*iterate) (grub_ata_dev_iterate_hook_t hook, void *hook_data,
  176. grub_disk_pull_t pull);
  177. /* Open the device named NAME, and set up SCSI. */
  178. grub_err_t (*open) (int id, int bus, struct grub_ata *scsi);
  179. /* Close the scsi device SCSI. */
  180. void (*close) (struct grub_ata *ata);
  181. /* Read SIZE bytes from the device SCSI into BUF after sending the
  182. command CMD of size CMDSIZE. */
  183. grub_err_t (*readwrite) (struct grub_ata *ata,
  184. struct grub_disk_ata_pass_through_parms *parms,
  185. int spinup);
  186. /* The next scsi device. */
  187. struct grub_ata_dev *next;
  188. };
  189. typedef struct grub_ata_dev *grub_ata_dev_t;
  190. void grub_ata_dev_register (grub_ata_dev_t dev);
  191. void grub_ata_dev_unregister (grub_ata_dev_t dev);
  192. #endif /* ! GRUB_ATA_HEADER */