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