dc4b43faae0f8764dcc54a0a4ba005c919a50e43
[mirror_edk2.git] / MdeModulePkg / Bus / Usb / UsbMassStorageDxe / UsbMassBoot.h
1 /** @file
2
3 Copyright (c) 2007, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 Module Name:
13
14 UsbMassBoot.h
15
16 Abstract:
17
18 The definition of command and data of the USB mass storage for
19 bootability command set.
20
21 Revision History
22
23
24 **/
25
26 #ifndef _EFI_USB_MASS_BOOT_H_
27 #define _EFI_USB_MASS_BOOT_H_
28
29 enum {
30 //
31 // The opcodes of various usb boot commands:
32 // INQUIRY/REQUEST_SENSE are "No Timeout Commands" as specified
33 // by MMC command set. Others are "Group 1 Timeout Commands". That
34 // is they should be retried if driver is ready.
35 // We can't use the Peripheral Device Type in Inquiry data to
36 // determine the timeout used. For example, both floppy and flash
37 // are likely set their PDT to 0, or Direct Access Device.
38 //
39 USB_BOOT_INQUIRY_OPCODE = 0x12,
40 USB_BOOT_REQUEST_SENSE_OPCODE = 0x03,
41
42 USB_BOOT_MODE_SENSE10_OPCODE = 0x5a,
43 USB_BOOT_READ_CAPACITY_OPCODE = 0x25,
44 USB_BOOT_TEST_UNIT_READY_OPCODE = 0x00,
45 USB_BOOT_READ10_OPCODE = 0x28,
46 USB_BOOT_WRITE10_OPCODE = 0x2a,
47
48 //
49 // The Sense Key part of the sense data. Sense data has three levels:
50 // Sense key, Additional Sense Code and Additional Sense Code Qualifier
51 //
52 USB_BOOT_SENSE_NO_SENSE = 0x00, // No sense key
53 USB_BOOT_SENSE_RECOVERED = 0x01, // Last command succeed with recovery actions
54 USB_BOOT_SENSE_NOT_READY = 0x02, // Device not ready
55 USB_BOOT_SNESE_MEDIUM_ERROR = 0X03, // Failed probably because flaw in the media
56 USB_BOOT_SENSE_HARDWARE_ERROR = 0X04, // Non-recoverable hardware failure
57 USB_BOOT_SENSE_ILLEGAL_REQUEST = 0X05, // Illegal parameters in the request
58 USB_BOOT_SENSE_UNIT_ATTENTION = 0X06, // Removable medium may have been changed
59 USB_BOOT_SNESE_DATA_PROTECT = 0X07, // Write protected
60 USB_BOOT_SENSE_BLANK_CHECK = 0X08, // Blank/non-blank medium while reading/writing
61 USB_BOOT_SENSE_VENDOR = 0X09, // Vendor specific sense key
62 USB_BOOT_SENSE_ABORTED = 0X0B, // Command aborted by the device
63 USB_BOOT_SENSE_VOLUME_OVERFLOW = 0x0D, // Partition overflow
64 USB_BOOT_SENSE_MISCOMPARE = 0x0E, // Source data mis-match while verfying.
65
66 USB_BOOT_ASC_NOT_READY = 0x04,
67 USB_BOOT_ASC_MEDIA_UPSIDE_DOWN = 0x06,
68 USB_BOOT_ASC_NO_MEDIA = 0x3A,
69 USB_BOOT_ASC_MEDIA_CHANGE = 0x28,
70
71 USB_BOOT_ASCQ_IN_PROGRESS = 0x01,
72 USB_BOOT_ASCQ_DEVICE_BUSY = 0xFF,
73
74 //
75 // Other parameters
76 //
77 USB_BOOT_IO_BLOCKS = 64,
78
79 //
80 // Boot Retry times
81 //
82 USB_BOOT_COMMAND_RETRY = 5,
83 USB_BOOT_WAIT_RETRY = 5,
84
85 //
86 // Boot Stall time
87 //
88 USB_BOOT_UNIT_READY_STALL = 50 * USB_MASS_STALL_1_MS,
89
90 //
91 // Boot Transfer timeout
92 //
93 USB_BOOT_GENERAL_BLOCK_TIMEOUT = 200 * USB_MASS_STALL_1_MS,
94 USB_BOOT_OPTICAL_BLOCK_TIMEOUT = 1 * USB_MASS_STALL_1_S,
95 USB_BOOT_GENERAL_CMD_TIMEOUT = 1 * USB_MASS_STALL_1_S,
96 USB_BOOT_INQUIRY_CMD_TIMEOUT = 3 * USB_MASS_STALL_1_S,
97
98 //
99 // Supported PDT codes, or Peripheral Device Type
100 //
101 USB_PDT_DIRECT_ACCESS = 0x00, // Direct access device
102 USB_PDT_CDROM = 0x05, // CDROM
103 USB_PDT_OPTICAL = 0x07, // Non-CD optical disks
104 USB_PDT_SIMPLE_DIRECT = 0x0E // Simplified direct access device
105 };
106
107 //
108 // The required commands are INQUIRY, READ CAPACITY, TEST UNIT READY,
109 // READ10, WRITE10, and REQUEST SENSE. The BLOCK_IO protocol uses LBA
110 // so it isn't necessary to issue MODE SENSE / READ FORMAT CAPACITY
111 // command to retrieve the disk gemotrics.
112 //
113 #pragma pack(1)
114 typedef struct {
115 UINT8 OpCode;
116 UINT8 Lun; // Lun (high 3 bits)
117 UINT8 Reserved0[2];
118 UINT8 AllocLen;
119 UINT8 Reserved1;
120 UINT8 Pad[6];
121 } USB_BOOT_INQUIRY_CMD;
122
123 typedef struct {
124 UINT8 Pdt; // Peripheral Device Type (low 5 bits)
125 UINT8 Removable; // Removable Media (highest bit)
126 UINT8 Reserved0[2];
127 UINT8 AddLen; // Additional length
128 UINT8 Reserved1[3];
129 UINT8 VendorID[8];
130 UINT8 ProductID[16];
131 UINT8 ProductRevision[4];
132 } USB_BOOT_INQUIRY_DATA;
133
134 typedef struct {
135 UINT8 OpCode;
136 UINT8 Lun;
137 UINT8 Reserved0[8];
138 UINT8 Pad[2];
139 } USB_BOOT_READ_CAPACITY_CMD;
140
141 typedef struct {
142 UINT8 LastLba[4];
143 UINT8 BlockLen[4];
144 } USB_BOOT_READ_CAPACITY_DATA;
145
146 typedef struct {
147 UINT8 OpCode;
148 UINT8 Lun;
149 UINT8 Reserved[4];
150 UINT8 Pad[6];
151 } USB_BOOT_TEST_UNIT_READY_CMD;
152
153 typedef struct {
154 UINT8 OpCode;
155 UINT8 Lun;
156 UINT8 PageCode;
157 UINT8 Reserved0[4];
158 UINT8 ParaListLenMsb;
159 UINT8 ParaListLenLsb;
160 UINT8 Reserved1;
161 UINT8 Pad[2];
162 } USB_BOOT_MODE_SENSE_CMD;
163
164 typedef struct {
165 UINT8 ModeDataLenMsb;
166 UINT8 ModeDataLenLsb;
167 UINT8 Reserved0[4];
168 UINT8 BlkDesLenMsb;
169 UINT8 BlkDesLenLsb;
170 } USB_BOOT_MODE_PARA_HEADER;
171
172 typedef struct {
173 UINT8 OpCode;
174 UINT8 Lun; // Lun (High 3 bits)
175 UINT8 Lba[4]; // Logical block address
176 UINT8 Reserved0;
177 UINT8 TransferLen[2]; // Transfer length
178 UINT8 Reserverd1;
179 UINT8 Pad[2];
180 } USB_BOOT_READ10_CMD;
181
182 typedef struct {
183 UINT8 OpCode;
184 UINT8 Lun;
185 UINT8 Lba[4];
186 UINT8 Reserved0;
187 UINT8 TransferLen[2];
188 UINT8 Reserverd1;
189 UINT8 Pad[2];
190 } USB_BOOT_WRITE10_CMD;
191
192 typedef struct {
193 UINT8 OpCode;
194 UINT8 Lun; // Lun (High 3 bits)
195 UINT8 Reserved0[2];
196 UINT8 AllocLen; // Allocation length
197 UINT8 Reserved1;
198 UINT8 Pad[6];
199 } USB_BOOT_REQUEST_SENSE_CMD;
200
201 typedef struct {
202 UINT8 ErrorCode;
203 UINT8 Reserved0;
204 UINT8 SenseKey; // Sense key (low 4 bits)
205 UINT8 Infor[4];
206 UINT8 AddLen; // Additional Sense length, 10
207 UINT8 Reserved1[4];
208 UINT8 ASC; // Additional Sense Code
209 UINT8 ASCQ; // Additional Sense Code Qualifier
210 UINT8 Reserverd2[4];
211 } USB_BOOT_REQUEST_SENSE_DATA;
212 #pragma pack()
213
214 //
215 // Convert a LUN number to that in the command
216 //
217 #define USB_BOOT_LUN(Lun) ((Lun) << 5)
218
219 //
220 // Get the removable, PDT, and sense key bits from the command data
221 //
222 #define USB_BOOT_REMOVABLE(RmbByte) (((RmbByte) & 0x80) != 0)
223 #define USB_BOOT_PDT(Pdt) ((Pdt) & 0x1f)
224 #define USB_BOOT_SENSE_KEY(Key) ((Key) & 0x0f)
225
226 //
227 // Swap the byte sequence of a UINT32. Intel CPU uses little endian
228 // in UEFI environment, but USB boot uses big endian.
229 //
230 #define USB_BOOT_SWAP32(Data32) \
231 ((((Data32) & 0x000000ff) << 24) | (((Data32) & 0xff000000) >> 24) | \
232 (((Data32) & 0x0000ff00) << 8) | (((Data32) & 0x00ff0000) >> 8))
233
234 #define USB_BOOT_SWAP16(Data16) \
235 ((((Data16) & 0x00ff) << 8) | (((Data16) & 0xff00) >> 8))
236
237 EFI_STATUS
238 UsbBootGetParams (
239 IN USB_MASS_DEVICE *UsbMass
240 );
241
242 EFI_STATUS
243 UsbBootIsUnitReady (
244 IN USB_MASS_DEVICE *UsbMass
245 );
246
247 EFI_STATUS
248 UsbBootDetectMedia (
249 IN USB_MASS_DEVICE *UsbMass
250 );
251
252 EFI_STATUS
253 UsbBootReadBlocks (
254 IN USB_MASS_DEVICE *UsbMass,
255 IN UINT32 Lba,
256 IN UINTN TotalBlock,
257 OUT UINT8 *Buffer
258 );
259
260 EFI_STATUS
261 UsbBootWriteBlocks (
262 IN USB_MASS_DEVICE *UsbMass,
263 IN UINT32 Lba,
264 IN UINTN TotalBlock,
265 OUT UINT8 *Buffer
266 );
267 #endif
268