3 Copyright (c) 2014, ARM Ltd. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 * Implementation of the FASTBOOT_TRANSPORT_PROTOCOL using the USB_DEVICE_PROTOCOL
19 #include <Protocol/UsbDevice.h>
20 #include <Protocol/AndroidFastbootTransport.h>
21 #include <Protocol/SimpleTextOut.h>
23 #include <Library/BaseLib.h>
24 #include <Library/BaseMemoryLib.h>
25 #include <Library/DebugLib.h>
26 #include <Library/MemoryAllocationLib.h>
27 #include <Library/UefiBootServicesTableLib.h>
28 #include <Library/UefiDriverEntryPoint.h>
30 STATIC USB_DEVICE_PROTOCOL
*mUsbDevice
;
32 // Configuration attributes:
33 // bit 7 reserved and must be 1, bit 6 means self-powered.
34 #define CONFIG_DESC_ATTRIBUTES (BIT7 | BIT6)
36 #define MAX_PACKET_SIZE_BULK 512
38 STATIC USB_DEVICE_PROTOCOL
*mUsbDevice
;
39 STATIC EFI_EVENT mReceiveEvent
= NULL
;
40 STATIC LIST_ENTRY mPacketList
;
42 // List type for queued received packets
43 typedef struct _FASTBOOT_USB_PACKET_LIST
{
47 } FASTBOOT_USB_PACKET_LIST
;
51 No string descriptors - all string descriptor members are set to 0
54 STATIC USB_DEVICE_DESCRIPTOR mDeviceDescriptor
= {
55 sizeof (USB_DEVICE_DESCRIPTOR
), //Length
56 USB_DESC_TYPE_DEVICE
, //DescriptorType
62 FixedPcdGet32 (PcdAndroidFastbootUsbVendorId
), //IdVendor
63 FixedPcdGet32 (PcdAndroidFastbootUsbProductId
), //IdProduct
72 We have one configuration, one interface, and two endpoints (one IN, one OUT)
75 // Lazy (compile-time) way to concatenate descriptors to pass to the USB device
80 USB_CONFIG_DESCRIPTOR ConfigDescriptor
;
81 USB_INTERFACE_DESCRIPTOR InterfaceDescriptor
;
82 USB_ENDPOINT_DESCRIPTOR EndpointDescriptor1
;
83 USB_ENDPOINT_DESCRIPTOR EndpointDescriptor2
;
84 } GET_CONFIG_DESCRIPTOR_RESPONSE
;
87 STATIC GET_CONFIG_DESCRIPTOR_RESPONSE mGetConfigDescriptorResponse
= {
88 { // USB_CONFIG_DESCRIPTOR
89 sizeof (USB_CONFIG_DESCRIPTOR
), //Length;
90 USB_DESC_TYPE_CONFIG
, //DescriptorType;
91 sizeof (GET_CONFIG_DESCRIPTOR_RESPONSE
), //TotalLength;
93 1, //ConfigurationValue;
95 CONFIG_DESC_ATTRIBUTES
, //Attributes;
98 { // USB_INTERFACE_DESCRIPTOR
99 sizeof (USB_INTERFACE_DESCRIPTOR
), //Length;
100 USB_DESC_TYPE_INTERFACE
, //DescriptorType;
101 0, //InterfaceNumber;
102 0, //AlternateSetting;
104 0xFF, //InterfaceClass;
105 // Vendor specific interface subclass and protocol codes.
106 // I found these values in the Fastboot code
107 // (in match_fastboot_with_serial in fastboot.c).
108 0x42, //InterfaceSubClass;
109 0x03, //InterfaceProtocol;
112 { // USB_ENDPOINT_DESCRIPTOR (In Endpoint)
113 sizeof (USB_ENDPOINT_DESCRIPTOR
), //Length;
114 USB_DESC_TYPE_ENDPOINT
, //DescriptorType;
115 1 | BIT7
, //EndpointAddress;
117 MAX_PACKET_SIZE_BULK
, //MaxPacketSize;
120 { // STATIC USB_ENDPOINT_DESCRIPTOR (Out Endpoint)
121 sizeof (USB_ENDPOINT_DESCRIPTOR
), //Length;
122 USB_DESC_TYPE_ENDPOINT
, //DescriptorType;
123 1, //EndpointAddress;
125 MAX_PACKET_SIZE_BULK
, //MaxPacketSize;
137 FASTBOOT_USB_PACKET_LIST
*NewEntry
;
139 NewEntry
= AllocatePool (sizeof (*NewEntry
));
140 ASSERT (NewEntry
!= NULL
);
142 NewEntry
->Buffer
= Buffer
;
143 NewEntry
->BufferSize
= Size
;
145 InsertTailList (&mPacketList
, &NewEntry
->Link
);
148 gBS
->SignalEvent (mReceiveEvent
);
155 IN UINT8 EndpointIndex
162 Set up the transport system for use by Fastboot.
163 e.g. For USB this probably means making the device enumerable.
166 FastbootTransportUsbStart (
167 EFI_EVENT ReceiveEvent
170 GET_CONFIG_DESCRIPTOR_RESPONSE
*Responses
;
172 mReceiveEvent
= ReceiveEvent
;
174 mGetConfigDescriptorResponse
.ConfigDescriptor
.TotalLength
= sizeof (GET_CONFIG_DESCRIPTOR_RESPONSE
);
175 Responses
= &mGetConfigDescriptorResponse
;
177 InitializeListHead (&mPacketList
);
179 return mUsbDevice
->Start (&mDeviceDescriptor
, (VOID
**) &Responses
, DataReceived
, DataSent
);
183 Function to be called when all Fastboot transactions are finished, to
184 de-initialise the transport system.
185 e.g. A USB OTG system might want to get out of peripheral mode so it can be
189 FastbootTransportUsbStop (
193 // not yet implemented in USB
198 Send data. This function can be used both for command responses like "OKAY"
199 and for the data phase (the protocol doesn't describe any situation when the
200 latter might be necessary, but does allow it)
203 FastbootTransportUsbSend (
205 IN CONST VOID
*Buffer
,
206 IN EFI_EVENT
*FatalErrorEvent
209 // Current USB protocol is blocking, so ignore FatalErrorEvent
210 return mUsbDevice
->Send(1, BufferSize
, Buffer
);
214 When the event has been Signalled to say data is available from the host,
215 this function is used to get data. In order to handle the case where several
216 packets are received before ReceiveEvent's notify function is called, packets
217 received are queued, and each call to this function returns the next packet in
218 the queue. It should therefore be called in a loop, the exit condition being a
219 return of EFI_NOT_READY.
222 Buffer - The buffer in which to place data
223 BufferSize - The size of Buffer in bytes
225 Return EFI_NOT_READY if there is no data available
228 FastbootTransportUsbReceive (
229 OUT UINTN
*BufferSize
,
233 FASTBOOT_USB_PACKET_LIST
*Entry
;
235 if (IsListEmpty (&mPacketList
)) {
236 return EFI_NOT_READY
;
239 Entry
= (FASTBOOT_USB_PACKET_LIST
*) GetFirstNode (&mPacketList
);
241 *BufferSize
= Entry
->BufferSize
;
242 *Buffer
= Entry
->Buffer
;
244 RemoveEntryList (&Entry
->Link
);
250 STATIC FASTBOOT_TRANSPORT_PROTOCOL mTransportProtocol
= {
251 FastbootTransportUsbStart
,
252 FastbootTransportUsbStop
,
253 FastbootTransportUsbSend
,
254 FastbootTransportUsbReceive
258 FastbootTransportUsbEntryPoint (
259 IN EFI_HANDLE ImageHandle
,
260 IN EFI_SYSTEM_TABLE
*SystemTable
265 // Assume there's only one USB peripheral controller.
266 Status
= gBS
->LocateProtocol (&gUsbDeviceProtocolGuid
, NULL
, (VOID
**) &mUsbDevice
);
267 if (EFI_ERROR (Status
)) {
271 Status
= gBS
->InstallProtocolInterface (
273 &gAndroidFastbootTransportProtocolGuid
,
274 EFI_NATIVE_INTERFACE
,