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