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