3 Copyright (c) 2014, ARM Ltd. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
10 * Implementation of the FASTBOOT_TRANSPORT_PROTOCOL using the USB_DEVICE_PROTOCOL
13 #include <Protocol/UsbDevice.h>
14 #include <Protocol/AndroidFastbootTransport.h>
15 #include <Protocol/SimpleTextOut.h>
17 #include <Library/BaseLib.h>
18 #include <Library/BaseMemoryLib.h>
19 #include <Library/DebugLib.h>
20 #include <Library/MemoryAllocationLib.h>
21 #include <Library/UefiBootServicesTableLib.h>
22 #include <Library/UefiDriverEntryPoint.h>
24 STATIC USB_DEVICE_PROTOCOL
*mUsbDevice
;
26 // Configuration attributes:
27 // bit 7 reserved and must be 1, bit 6 means self-powered.
28 #define CONFIG_DESC_ATTRIBUTES (BIT7 | BIT6)
30 #define MAX_PACKET_SIZE_BULK 512
32 STATIC USB_DEVICE_PROTOCOL
*mUsbDevice
;
33 STATIC EFI_EVENT mReceiveEvent
= NULL
;
34 STATIC LIST_ENTRY mPacketList
;
36 // List type for queued received packets
37 typedef struct _FASTBOOT_USB_PACKET_LIST
{
41 } FASTBOOT_USB_PACKET_LIST
;
45 No string descriptors - all string descriptor members are set to 0
48 STATIC USB_DEVICE_DESCRIPTOR mDeviceDescriptor
= {
49 sizeof (USB_DEVICE_DESCRIPTOR
), //Length
50 USB_DESC_TYPE_DEVICE
, //DescriptorType
56 FixedPcdGet32 (PcdAndroidFastbootUsbVendorId
), //IdVendor
57 FixedPcdGet32 (PcdAndroidFastbootUsbProductId
), //IdProduct
66 We have one configuration, one interface, and two endpoints (one IN, one OUT)
69 // Lazy (compile-time) way to concatenate descriptors to pass to the USB device
74 USB_CONFIG_DESCRIPTOR ConfigDescriptor
;
75 USB_INTERFACE_DESCRIPTOR InterfaceDescriptor
;
76 USB_ENDPOINT_DESCRIPTOR EndpointDescriptor1
;
77 USB_ENDPOINT_DESCRIPTOR EndpointDescriptor2
;
78 } GET_CONFIG_DESCRIPTOR_RESPONSE
;
81 STATIC GET_CONFIG_DESCRIPTOR_RESPONSE mGetConfigDescriptorResponse
= {
82 { // USB_CONFIG_DESCRIPTOR
83 sizeof (USB_CONFIG_DESCRIPTOR
), //Length;
84 USB_DESC_TYPE_CONFIG
, //DescriptorType;
85 sizeof (GET_CONFIG_DESCRIPTOR_RESPONSE
), //TotalLength;
87 1, //ConfigurationValue;
89 CONFIG_DESC_ATTRIBUTES
, //Attributes;
92 { // USB_INTERFACE_DESCRIPTOR
93 sizeof (USB_INTERFACE_DESCRIPTOR
), //Length;
94 USB_DESC_TYPE_INTERFACE
, //DescriptorType;
96 0, //AlternateSetting;
98 0xFF, //InterfaceClass;
99 // Vendor specific interface subclass and protocol codes.
100 // I found these values in the Fastboot code
101 // (in match_fastboot_with_serial in fastboot.c).
102 0x42, //InterfaceSubClass;
103 0x03, //InterfaceProtocol;
106 { // USB_ENDPOINT_DESCRIPTOR (In Endpoint)
107 sizeof (USB_ENDPOINT_DESCRIPTOR
), //Length;
108 USB_DESC_TYPE_ENDPOINT
, //DescriptorType;
109 1 | BIT7
, //EndpointAddress;
111 MAX_PACKET_SIZE_BULK
, //MaxPacketSize;
114 { // STATIC USB_ENDPOINT_DESCRIPTOR (Out Endpoint)
115 sizeof (USB_ENDPOINT_DESCRIPTOR
), //Length;
116 USB_DESC_TYPE_ENDPOINT
, //DescriptorType;
117 1, //EndpointAddress;
119 MAX_PACKET_SIZE_BULK
, //MaxPacketSize;
131 FASTBOOT_USB_PACKET_LIST
*NewEntry
;
133 NewEntry
= AllocatePool (sizeof (*NewEntry
));
134 ASSERT (NewEntry
!= NULL
);
136 NewEntry
->Buffer
= Buffer
;
137 NewEntry
->BufferSize
= Size
;
139 InsertTailList (&mPacketList
, &NewEntry
->Link
);
142 gBS
->SignalEvent (mReceiveEvent
);
149 IN UINT8 EndpointIndex
156 Set up the transport system for use by Fastboot.
157 e.g. For USB this probably means making the device enumerable.
160 FastbootTransportUsbStart (
161 EFI_EVENT ReceiveEvent
164 GET_CONFIG_DESCRIPTOR_RESPONSE
*Responses
;
166 mReceiveEvent
= ReceiveEvent
;
168 mGetConfigDescriptorResponse
.ConfigDescriptor
.TotalLength
= sizeof (GET_CONFIG_DESCRIPTOR_RESPONSE
);
169 Responses
= &mGetConfigDescriptorResponse
;
171 InitializeListHead (&mPacketList
);
173 return mUsbDevice
->Start (&mDeviceDescriptor
, (VOID
**) &Responses
, DataReceived
, DataSent
);
177 Function to be called when all Fastboot transactions are finished, to
178 de-initialise the transport system.
179 e.g. A USB OTG system might want to get out of peripheral mode so it can be
183 FastbootTransportUsbStop (
187 // not yet implemented in USB
192 Send data. This function can be used both for command responses like "OKAY"
193 and for the data phase (the protocol doesn't describe any situation when the
194 latter might be necessary, but does allow it)
197 FastbootTransportUsbSend (
199 IN CONST VOID
*Buffer
,
200 IN EFI_EVENT
*FatalErrorEvent
203 // Current USB protocol is blocking, so ignore FatalErrorEvent
204 return mUsbDevice
->Send(1, BufferSize
, Buffer
);
208 When the event has been Signalled to say data is available from the host,
209 this function is used to get data. In order to handle the case where several
210 packets are received before ReceiveEvent's notify function is called, packets
211 received are queued, and each call to this function returns the next packet in
212 the queue. It should therefore be called in a loop, the exit condition being a
213 return of EFI_NOT_READY.
216 Buffer - The buffer in which to place data
217 BufferSize - The size of Buffer in bytes
219 Return EFI_NOT_READY if there is no data available
222 FastbootTransportUsbReceive (
223 OUT UINTN
*BufferSize
,
227 FASTBOOT_USB_PACKET_LIST
*Entry
;
229 if (IsListEmpty (&mPacketList
)) {
230 return EFI_NOT_READY
;
233 Entry
= (FASTBOOT_USB_PACKET_LIST
*) GetFirstNode (&mPacketList
);
235 *BufferSize
= Entry
->BufferSize
;
236 *Buffer
= Entry
->Buffer
;
238 RemoveEntryList (&Entry
->Link
);
244 STATIC FASTBOOT_TRANSPORT_PROTOCOL mTransportProtocol
= {
245 FastbootTransportUsbStart
,
246 FastbootTransportUsbStop
,
247 FastbootTransportUsbSend
,
248 FastbootTransportUsbReceive
252 FastbootTransportUsbEntryPoint (
253 IN EFI_HANDLE ImageHandle
,
254 IN EFI_SYSTEM_TABLE
*SystemTable
259 // Assume there's only one USB peripheral controller.
260 Status
= gBS
->LocateProtocol (&gUsbDeviceProtocolGuid
, NULL
, (VOID
**) &mUsbDevice
);
261 if (EFI_ERROR (Status
)) {
265 Status
= gBS
->InstallProtocolInterface (
267 &gAndroidFastbootTransportProtocolGuid
,
268 EFI_NATIVE_INTERFACE
,