Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #ifndef _IPXE_ATA_H
  2. #define _IPXE_ATA_H
  3. #include <stdint.h>
  4. #include <ipxe/uaccess.h>
  5. #include <ipxe/interface.h>
  6. /** @file
  7. *
  8. * ATA devices
  9. *
  10. */
  11. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  12. /**
  13. * An ATA Logical Block Address
  14. *
  15. * ATA controllers have three byte-wide registers for specifying the
  16. * block address: LBA Low, LBA Mid and LBA High. This allows for a
  17. * 24-bit address. Some devices support the "48-bit address feature
  18. * set" (LBA48), in which case each of these byte-wide registers is
  19. * actually a two-entry FIFO, and the "previous" byte pushed into the
  20. * FIFO is used as the corresponding high-order byte. So, to set up
  21. * the 48-bit address 0x123456abcdef, you would issue
  22. *
  23. * 0x56 -> LBA Low register
  24. * 0xef -> LBA Low register
  25. * 0x34 -> LBA Mid register
  26. * 0xcd -> LBA Mid register
  27. * 0x12 -> LBA High register
  28. * 0xab -> LBA High register
  29. *
  30. * This structure encapsulates this information by providing a single
  31. * 64-bit integer in native byte order, unioned with bytes named so
  32. * that the sequence becomes
  33. *
  34. * low_prev -> LBA Low register
  35. * low_cur -> LBA Low register
  36. * mid_prev -> LBA Mid register
  37. * mid_cur -> LBA Mid register
  38. * high_prev -> LBA High register
  39. * high_cur -> LBA High register
  40. *
  41. * Just to complicate matters further, in non-LBA48 mode it is
  42. * possible to have a 28-bit address, in which case bits 27:24 must be
  43. * written into the low four bits of the Device register.
  44. */
  45. union ata_lba {
  46. /** LBA as a 64-bit integer in native-endian order */
  47. uint64_t native;
  48. /** ATA registers */
  49. struct {
  50. #if __BYTE_ORDER == __LITTLE_ENDIAN
  51. uint8_t low_cur;
  52. uint8_t mid_cur;
  53. uint8_t high_cur;
  54. uint8_t low_prev;
  55. uint8_t mid_prev;
  56. uint8_t high_prev;
  57. uint16_t pad;
  58. #elif __BYTE_ORDER == __BIG_ENDIAN
  59. uint16_t pad;
  60. uint8_t high_prev;
  61. uint8_t mid_prev;
  62. uint8_t low_prev;
  63. uint8_t high_cur;
  64. uint8_t mid_cur;
  65. uint8_t low_cur;
  66. #else
  67. #error "I need a byte order"
  68. #endif
  69. } bytes;
  70. };
  71. /** An ATA 2-byte FIFO register */
  72. union ata_fifo {
  73. /** Value in native-endian order */
  74. uint16_t native;
  75. /** ATA registers */
  76. struct {
  77. #if __BYTE_ORDER == __LITTLE_ENDIAN
  78. uint8_t cur;
  79. uint8_t prev;
  80. #elif __BYTE_ORDER == __BIG_ENDIAN
  81. uint8_t prev;
  82. uint8_t cur;
  83. #else
  84. #error "I need a byte order"
  85. #endif
  86. } bytes;
  87. };
  88. /** ATA command block */
  89. struct ata_cb {
  90. /** Logical block address */
  91. union ata_lba lba;
  92. /** Sector count */
  93. union ata_fifo count;
  94. /** Error/feature register */
  95. union ata_fifo err_feat;
  96. /** Device register */
  97. uint8_t device;
  98. /** Command/status register */
  99. uint8_t cmd_stat;
  100. /** Use LBA48 extended addressing */
  101. int lba48;
  102. };
  103. /** Obsolete bits in the ATA device register */
  104. #define ATA_DEV_OBSOLETE 0xa0
  105. /** LBA flag in the ATA device register */
  106. #define ATA_DEV_LBA 0x40
  107. /** Slave ("device 1") flag in the ATA device register */
  108. #define ATA_DEV_SLAVE 0x10
  109. /** Master ("device 0") flag in the ATA device register */
  110. #define ATA_DEV_MASTER 0x00
  111. /** Mask of non-LBA portion of device register */
  112. #define ATA_DEV_MASK 0xf0
  113. /** "Read sectors" command */
  114. #define ATA_CMD_READ 0x20
  115. /** "Read sectors (ext)" command */
  116. #define ATA_CMD_READ_EXT 0x24
  117. /** "Write sectors" command */
  118. #define ATA_CMD_WRITE 0x30
  119. /** "Write sectors (ext)" command */
  120. #define ATA_CMD_WRITE_EXT 0x34
  121. /** "Identify" command */
  122. #define ATA_CMD_IDENTIFY 0xec
  123. /** Command completed in error */
  124. #define ATA_STAT_ERR 0x01
  125. /**
  126. * Structure returned by ATA IDENTIFY command
  127. *
  128. * This is a huge structure with many fields that we don't care about,
  129. * so we implement only a few fields.
  130. */
  131. struct ata_identity {
  132. uint16_t ignore_a[27]; /* words 0-26 */
  133. uint16_t model[20]; /* words 27-46 */
  134. uint16_t ignore_b[13]; /* words 47-59 */
  135. uint32_t lba_sectors; /* words 60-61 */
  136. uint16_t ignore_c[21]; /* words 62-82 */
  137. uint16_t supports_lba48; /* word 83 */
  138. uint16_t ignore_d[16]; /* words 84-99 */
  139. uint64_t lba48_sectors; /* words 100-103 */
  140. uint16_t ignore_e[152]; /* words 104-255 */
  141. };
  142. /** Supports LBA48 flag */
  143. #define ATA_SUPPORTS_LBA48 ( 1 << 10 )
  144. /** ATA sector size */
  145. #define ATA_SECTOR_SIZE 512
  146. /** An ATA command information unit */
  147. struct ata_cmd {
  148. /** ATA command block */
  149. struct ata_cb cb;
  150. /** Data-out buffer (may be NULL)
  151. *
  152. * If non-NULL, this buffer must be ata_command::cb::count
  153. * sectors in size.
  154. */
  155. userptr_t data_out;
  156. /** Data-out buffer length
  157. *
  158. * Must be zero if @c data_out is NULL
  159. */
  160. size_t data_out_len;
  161. /** Data-in buffer (may be NULL)
  162. *
  163. * If non-NULL, this buffer must be ata_command::cb::count
  164. * sectors in size.
  165. */
  166. userptr_t data_in;
  167. /** Data-in buffer length
  168. *
  169. * Must be zero if @c data_in is NULL
  170. */
  171. size_t data_in_len;
  172. };
  173. extern int ata_command ( struct interface *control, struct interface *data,
  174. struct ata_cmd *command );
  175. #define ata_command_TYPE( object_type ) \
  176. typeof ( int ( object_type, struct interface *data, \
  177. struct ata_cmd *command ) )
  178. extern int ata_open ( struct interface *block, struct interface *ata,
  179. unsigned int device, unsigned int max_count );
  180. #endif /* _IPXE_ATA_H */