1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
|
/** @file
BOT Transportation implementation.
Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include "UsbBotPeim.h"
#include "BotPeim.h"
#include "PeiUsbLib.h"
/**
Reset the given usb device.
@param PeiServices The pointer of EFI_PEI_SERVICES.
@param PeiBotDev The instance to PEI_BOT_DEVICE.
@retval EFI_INVALID_PARAMETER Can not get usb io ppi.
@retval EFI_SUCCESS Failed to reset the given usb device.
**/
EFI_STATUS
BotRecoveryReset (
IN EFI_PEI_SERVICES **PeiServices,
IN PEI_BOT_DEVICE *PeiBotDev
)
{
EFI_USB_DEVICE_REQUEST DevReq;
UINT32 Timeout;
PEI_USB_IO_PPI *UsbIoPpi;
UINT8 EndpointAddr;
EFI_STATUS Status;
UsbIoPpi = PeiBotDev->UsbIoPpi;
if (UsbIoPpi == NULL) {
return EFI_INVALID_PARAMETER;
}
ZeroMem (&DevReq, sizeof (EFI_USB_DEVICE_REQUEST));
DevReq.RequestType = 0x21;
DevReq.Request = 0xFF;
DevReq.Value = 0;
DevReq.Index = 0;
DevReq.Length = 0;
Timeout = 3000;
Status = UsbIoPpi->UsbControlTransfer (
PeiServices,
UsbIoPpi,
&DevReq,
EfiUsbNoData,
Timeout,
NULL,
0
);
//
// clear bulk in endpoint stall feature
//
EndpointAddr = (PeiBotDev->BulkInEndpoint)->EndpointAddress;
PeiUsbClearEndpointHalt (PeiServices, UsbIoPpi, EndpointAddr);
//
// clear bulk out endpoint stall feature
//
EndpointAddr = (PeiBotDev->BulkOutEndpoint)->EndpointAddress;
PeiUsbClearEndpointHalt (PeiServices, UsbIoPpi, EndpointAddr);
return Status;
}
/**
Send the command to the device using Bulk-Out endpoint.
This function sends the command to the device using Bulk-Out endpoint.
BOT transfer is composed of three phases: Command, Data, and Status.
This is the Command phase.
@param PeiServices The pointer of EFI_PEI_SERVICES.
@param PeiBotDev The instance to PEI_BOT_DEVICE.
@param Command The command to transfer to device.
@param CommandSize The length of the command.
@param DataTransferLength The expected length of the data.
@param Direction The direction of the data.
@param Timeout Indicates the maximum time, in millisecond, which the
transfer is allowed to complete.
@retval EFI_DEVICE_ERROR Successful to send the command to device.
@retval EFI_SUCCESS Failed to send the command to device.
**/
EFI_STATUS
BotCommandPhase (
IN EFI_PEI_SERVICES **PeiServices,
IN PEI_BOT_DEVICE *PeiBotDev,
IN VOID *Command,
IN UINT8 CommandSize,
IN UINT32 DataTransferLength,
IN EFI_USB_DATA_DIRECTION Direction,
IN UINT16 Timeout
)
{
CBW Cbw;
EFI_STATUS Status;
PEI_USB_IO_PPI *UsbIoPpi;
UINTN DataSize;
UsbIoPpi = PeiBotDev->UsbIoPpi;
ZeroMem (&Cbw, sizeof (CBW));
//
// Fill the command block, detailed see BOT spec
//
Cbw.Signature = CBWSIG;
Cbw.Tag = 0x01;
Cbw.DataTransferLength = DataTransferLength;
Cbw.Flags = (UINT8) ((Direction == EfiUsbDataIn) ? 0x80 : 0);
Cbw.Lun = 0;
Cbw.CmdLen = CommandSize;
CopyMem (Cbw.CmdBlock, Command, CommandSize);
DataSize = sizeof (CBW);
Status = UsbIoPpi->UsbBulkTransfer (
PeiServices,
UsbIoPpi,
(PeiBotDev->BulkOutEndpoint)->EndpointAddress,
(UINT8 *) &Cbw,
&DataSize,
Timeout
);
if (EFI_ERROR (Status)) {
//
// Command phase fail, we need to recovery reset this device
//
BotRecoveryReset (PeiServices, PeiBotDev);
return EFI_DEVICE_ERROR;
}
return EFI_SUCCESS;
}
/**
Transfer the data between the device and host.
This function transfers the data between the device and host.
BOT transfer is composed of three phases: Command, Data, and Status.
This is the Data phase.
@param PeiServices The pointer of EFI_PEI_SERVICES.
@param PeiBotDev The instance to PEI_BOT_DEVICE.
@param DataSize The length of the data.
@param DataBuffer The pointer to the data.
@param Direction The direction of the data.
@param Timeout Indicates the maximum time, in millisecond, which the
transfer is allowed to complete.
@retval EFI_DEVICE_ERROR Successful to send the data to device.
@retval EFI_SUCCESS Failed to send the data to device.
**/
EFI_STATUS
BotDataPhase (
IN EFI_PEI_SERVICES **PeiServices,
IN PEI_BOT_DEVICE *PeiBotDev,
IN UINT32 *DataSize,
IN OUT VOID *DataBuffer,
IN EFI_USB_DATA_DIRECTION Direction,
IN UINT16 Timeout
)
{
EFI_STATUS Status;
PEI_USB_IO_PPI *UsbIoPpi;
UINT8 EndpointAddr;
UINTN Remain;
UINTN Increment;
UINT32 MaxPacketLen;
UINT8 *BufferPtr;
UINTN TransferredSize;
UsbIoPpi = PeiBotDev->UsbIoPpi;
Remain = *DataSize;
BufferPtr = (UINT8 *) DataBuffer;
TransferredSize = 0;
//
// retrieve the max packet length of the given endpoint
//
if (Direction == EfiUsbDataIn) {
MaxPacketLen = (PeiBotDev->BulkInEndpoint)->MaxPacketSize;
EndpointAddr = (PeiBotDev->BulkInEndpoint)->EndpointAddress;
} else {
MaxPacketLen = (PeiBotDev->BulkOutEndpoint)->MaxPacketSize;
EndpointAddr = (PeiBotDev->BulkOutEndpoint)->EndpointAddress;
}
while (Remain > 0) {
//
// Using 15 packets to avoid Bitstuff error
//
if (Remain > 16 * MaxPacketLen) {
Increment = 16 * MaxPacketLen;
} else {
Increment = Remain;
}
Status = UsbIoPpi->UsbBulkTransfer (
PeiServices,
UsbIoPpi,
EndpointAddr,
BufferPtr,
&Increment,
Timeout
);
TransferredSize += Increment;
if (EFI_ERROR (Status)) {
PeiUsbClearEndpointHalt (PeiServices, UsbIoPpi, EndpointAddr);
return Status;
}
BufferPtr += Increment;
Remain -= Increment;
}
*DataSize = (UINT32) TransferredSize;
return EFI_SUCCESS;
}
/**
Get the command execution status from device.
This function gets the command execution status from device.
BOT transfer is composed of three phases: Command, Data, and Status.
This is the Status phase.
@param PeiServices The pointer of EFI_PEI_SERVICES.
@param PeiBotDev The instance to PEI_BOT_DEVICE.
@param TransferStatus The status of the transaction.
@param Timeout Indicates the maximum time, in millisecond, which the
transfer is allowed to complete.
@retval EFI_DEVICE_ERROR Successful to get the status of device.
@retval EFI_SUCCESS Failed to get the status of device.
**/
EFI_STATUS
BotStatusPhase (
IN EFI_PEI_SERVICES **PeiServices,
IN PEI_BOT_DEVICE *PeiBotDev,
OUT UINT8 *TransferStatus,
IN UINT16 Timeout
)
{
CSW Csw;
EFI_STATUS Status;
PEI_USB_IO_PPI *UsbIoPpi;
UINT8 EndpointAddr;
UINTN DataSize;
UsbIoPpi = PeiBotDev->UsbIoPpi;
ZeroMem (&Csw, sizeof (CSW));
EndpointAddr = (PeiBotDev->BulkInEndpoint)->EndpointAddress;
DataSize = sizeof (CSW);
//
// Get the status field from bulk transfer
//
Status = UsbIoPpi->UsbBulkTransfer (
PeiServices,
UsbIoPpi,
EndpointAddr,
&Csw,
&DataSize,
Timeout
);
if (EFI_ERROR (Status)) {
return Status;
}
if (Csw.Signature == CSWSIG) {
*TransferStatus = Csw.Status;
} else {
return EFI_DEVICE_ERROR;
}
return EFI_SUCCESS;
}
/**
Send ATAPI command using BOT protocol.
@param PeiServices The pointer of EFI_PEI_SERVICES.
@param PeiBotDev The instance to PEI_BOT_DEVICE.
@param Command The command to be sent to ATAPI device.
@param CommandSize The length of the data to be sent.
@param DataBuffer The pointer to the data.
@param BufferLength The length of the data.
@param Direction The direction of the data.
@param TimeOutInMilliSeconds Indicates the maximum time, in millisecond, which the
transfer is allowed to complete.
@retval EFI_DEVICE_ERROR Successful to get the status of device.
@retval EFI_SUCCESS Failed to get the status of device.
**/
EFI_STATUS
PeiAtapiCommand (
IN EFI_PEI_SERVICES **PeiServices,
IN PEI_BOT_DEVICE *PeiBotDev,
IN VOID *Command,
IN UINT8 CommandSize,
IN VOID *DataBuffer,
IN UINT32 BufferLength,
IN EFI_USB_DATA_DIRECTION Direction,
IN UINT16 TimeOutInMilliSeconds
)
{
EFI_STATUS Status;
EFI_STATUS BotDataStatus;
UINT8 TransferStatus;
UINT32 BufferSize;
BotDataStatus = EFI_SUCCESS;
//
// First send ATAPI command through Bot
//
Status = BotCommandPhase (
PeiServices,
PeiBotDev,
Command,
CommandSize,
BufferLength,
Direction,
TimeOutInMilliSeconds
);
if (EFI_ERROR (Status)) {
return EFI_DEVICE_ERROR;
}
//
// Send/Get Data if there is a Data Stage
//
switch (Direction) {
case EfiUsbDataIn:
case EfiUsbDataOut:
BufferSize = BufferLength;
BotDataStatus = BotDataPhase (
PeiServices,
PeiBotDev,
&BufferSize,
DataBuffer,
Direction,
TimeOutInMilliSeconds
);
break;
case EfiUsbNoData:
break;
}
//
// Status Phase
//
Status = BotStatusPhase (
PeiServices,
PeiBotDev,
&TransferStatus,
TimeOutInMilliSeconds
);
if (EFI_ERROR (Status)) {
BotRecoveryReset (PeiServices, PeiBotDev);
return EFI_DEVICE_ERROR;
}
if (TransferStatus == 0x01) {
return EFI_DEVICE_ERROR;
}
return BotDataStatus;
}
|