]> git.proxmox.com Git - mirror_edk2.git/blob - EmbeddedPkg/Drivers/AndroidFastbootTransportUsbDxe/FastbootTransportUsb.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / EmbeddedPkg / Drivers / AndroidFastbootTransportUsbDxe / FastbootTransportUsb.c
1 /** @file
2
3 Copyright (c) 2014, ARM Ltd. All rights reserved.<BR>
4
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 /*
10 * Implementation of the FASTBOOT_TRANSPORT_PROTOCOL using the USB_DEVICE_PROTOCOL
11 */
12
13 #include <Protocol/UsbDevice.h>
14 #include <Protocol/AndroidFastbootTransport.h>
15 #include <Protocol/SimpleTextOut.h>
16
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>
23
24 STATIC USB_DEVICE_PROTOCOL *mUsbDevice;
25
26 // Configuration attributes:
27 // bit 7 reserved and must be 1, bit 6 means self-powered.
28 #define CONFIG_DESC_ATTRIBUTES (BIT7 | BIT6)
29
30 #define MAX_PACKET_SIZE_BULK 512
31
32 STATIC USB_DEVICE_PROTOCOL *mUsbDevice;
33 STATIC EFI_EVENT mReceiveEvent = NULL;
34 STATIC LIST_ENTRY mPacketList;
35
36 // List type for queued received packets
37 typedef struct _FASTBOOT_USB_PACKET_LIST {
38 LIST_ENTRY Link;
39 VOID *Buffer;
40 UINTN BufferSize;
41 } FASTBOOT_USB_PACKET_LIST;
42
43 /*
44 No string descriptors - all string descriptor members are set to 0
45 */
46
47 STATIC USB_DEVICE_DESCRIPTOR mDeviceDescriptor = {
48 sizeof (USB_DEVICE_DESCRIPTOR), // Length
49 USB_DESC_TYPE_DEVICE, // DescriptorType
50 0x0200, // BcdUSB
51 0xFF, // DeviceClass
52 0, // DeviceSubClass
53 0, // DeviceProtocol
54 64, // MaxPacketSize0
55 FixedPcdGet32 (PcdAndroidFastbootUsbVendorId), // IdVendor
56 FixedPcdGet32 (PcdAndroidFastbootUsbProductId), // IdProduct
57 0, // BcdDevice
58 0, // StrManufacturer
59 0, // StrProduct
60 0, // StrSerialNumber
61 1 // NumConfigurations
62 };
63
64 /*
65 We have one configuration, one interface, and two endpoints (one IN, one OUT)
66 */
67
68 // Lazy (compile-time) way to concatenate descriptors to pass to the USB device
69 // protocol
70
71 #pragma pack(1)
72 typedef struct {
73 USB_CONFIG_DESCRIPTOR ConfigDescriptor;
74 USB_INTERFACE_DESCRIPTOR InterfaceDescriptor;
75 USB_ENDPOINT_DESCRIPTOR EndpointDescriptor1;
76 USB_ENDPOINT_DESCRIPTOR EndpointDescriptor2;
77 } GET_CONFIG_DESCRIPTOR_RESPONSE;
78 #pragma pack()
79
80 STATIC GET_CONFIG_DESCRIPTOR_RESPONSE mGetConfigDescriptorResponse = {
81 { // USB_CONFIG_DESCRIPTOR
82 sizeof (USB_CONFIG_DESCRIPTOR), // Length;
83 USB_DESC_TYPE_CONFIG, // DescriptorType;
84 sizeof (GET_CONFIG_DESCRIPTOR_RESPONSE), // TotalLength;
85 1, // NumInterfaces;
86 1, // ConfigurationValue;
87 0, // Configuration;
88 CONFIG_DESC_ATTRIBUTES, // Attributes;
89 0 // MaxPower;
90 },
91 { // USB_INTERFACE_DESCRIPTOR
92 sizeof (USB_INTERFACE_DESCRIPTOR), // Length;
93 USB_DESC_TYPE_INTERFACE, // DescriptorType;
94 0, // InterfaceNumber;
95 0, // AlternateSetting;
96 2, // NumEndpoints;
97 0xFF, // InterfaceClass;
98 // Vendor specific interface subclass and protocol codes.
99 // I found these values in the Fastboot code
100 // (in match_fastboot_with_serial in fastboot.c).
101 0x42, // InterfaceSubClass;
102 0x03, // InterfaceProtocol;
103 0 // Interface;
104 },
105 { // USB_ENDPOINT_DESCRIPTOR (In Endpoint)
106 sizeof (USB_ENDPOINT_DESCRIPTOR), // Length;
107 USB_DESC_TYPE_ENDPOINT, // DescriptorType;
108 1 | BIT7, // EndpointAddress;
109 0x2, // Attributes;
110 MAX_PACKET_SIZE_BULK, // MaxPacketSize;
111 16 // Interval;
112 },
113 { // STATIC USB_ENDPOINT_DESCRIPTOR (Out Endpoint)
114 sizeof (USB_ENDPOINT_DESCRIPTOR), // Length;
115 USB_DESC_TYPE_ENDPOINT, // DescriptorType;
116 1, // EndpointAddress;
117 0x2, // Attributes;
118 MAX_PACKET_SIZE_BULK, // MaxPacketSize;
119 16 // Interval;
120 }
121 };
122
123 STATIC
124 VOID
125 DataReceived (
126 IN UINTN Size,
127 IN VOID *Buffer
128 )
129 {
130 FASTBOOT_USB_PACKET_LIST *NewEntry;
131
132 NewEntry = AllocatePool (sizeof (*NewEntry));
133 ASSERT (NewEntry != NULL);
134
135 NewEntry->Buffer = Buffer;
136 NewEntry->BufferSize = Size;
137
138 InsertTailList (&mPacketList, &NewEntry->Link);
139
140 if (mReceiveEvent) {
141 gBS->SignalEvent (mReceiveEvent);
142 }
143 }
144
145 STATIC
146 VOID
147 DataSent (
148 IN UINT8 EndpointIndex
149 )
150 {
151 // Don't care.
152 }
153
154 /*
155 Set up the transport system for use by Fastboot.
156 e.g. For USB this probably means making the device enumerable.
157 */
158 EFI_STATUS
159 FastbootTransportUsbStart (
160 EFI_EVENT ReceiveEvent
161 )
162 {
163 GET_CONFIG_DESCRIPTOR_RESPONSE *Responses;
164
165 mReceiveEvent = ReceiveEvent;
166
167 mGetConfigDescriptorResponse.ConfigDescriptor.TotalLength = sizeof (GET_CONFIG_DESCRIPTOR_RESPONSE);
168 Responses = &mGetConfigDescriptorResponse;
169
170 InitializeListHead (&mPacketList);
171
172 return mUsbDevice->Start (&mDeviceDescriptor, (VOID **)&Responses, DataReceived, DataSent);
173 }
174
175 /*
176 Function to be called when all Fastboot transactions are finished, to
177 de-initialise the transport system.
178 e.g. A USB OTG system might want to get out of peripheral mode so it can be
179 a USB host.
180 */
181 EFI_STATUS
182 FastbootTransportUsbStop (
183 VOID
184 )
185 {
186 // not yet implemented in USB
187 return EFI_SUCCESS;
188 }
189
190 /*
191 Send data. This function can be used both for command responses like "OKAY"
192 and for the data phase (the protocol doesn't describe any situation when the
193 latter might be necessary, but does allow it)
194 */
195 EFI_STATUS
196 FastbootTransportUsbSend (
197 IN UINTN BufferSize,
198 IN CONST VOID *Buffer,
199 IN EFI_EVENT *FatalErrorEvent
200 )
201 {
202 // Current USB protocol is blocking, so ignore FatalErrorEvent
203 return mUsbDevice->Send (1, BufferSize, Buffer);
204 }
205
206 /*
207 When the event has been Signalled to say data is available from the host,
208 this function is used to get data. In order to handle the case where several
209 packets are received before ReceiveEvent's notify function is called, packets
210 received are queued, and each call to this function returns the next packet in
211 the queue. It should therefore be called in a loop, the exit condition being a
212 return of EFI_NOT_READY.
213
214 Parameters:
215 Buffer - The buffer in which to place data
216 BufferSize - The size of Buffer in bytes
217
218 Return EFI_NOT_READY if there is no data available
219 */
220 EFI_STATUS
221 FastbootTransportUsbReceive (
222 OUT UINTN *BufferSize,
223 OUT VOID **Buffer
224 )
225 {
226 FASTBOOT_USB_PACKET_LIST *Entry;
227
228 if (IsListEmpty (&mPacketList)) {
229 return EFI_NOT_READY;
230 }
231
232 Entry = (FASTBOOT_USB_PACKET_LIST *)GetFirstNode (&mPacketList);
233
234 *BufferSize = Entry->BufferSize;
235 *Buffer = Entry->Buffer;
236
237 RemoveEntryList (&Entry->Link);
238 FreePool (Entry);
239
240 return EFI_SUCCESS;
241 }
242
243 STATIC FASTBOOT_TRANSPORT_PROTOCOL mTransportProtocol = {
244 FastbootTransportUsbStart,
245 FastbootTransportUsbStop,
246 FastbootTransportUsbSend,
247 FastbootTransportUsbReceive
248 };
249
250 EFI_STATUS
251 FastbootTransportUsbEntryPoint (
252 IN EFI_HANDLE ImageHandle,
253 IN EFI_SYSTEM_TABLE *SystemTable
254 )
255 {
256 EFI_STATUS Status;
257
258 // Assume there's only one USB peripheral controller.
259 Status = gBS->LocateProtocol (&gUsbDeviceProtocolGuid, NULL, (VOID **)&mUsbDevice);
260 if (EFI_ERROR (Status)) {
261 return Status;
262 }
263
264 Status = gBS->InstallProtocolInterface (
265 &ImageHandle,
266 &gAndroidFastbootTransportProtocolGuid,
267 EFI_NATIVE_INTERFACE,
268 &mTransportProtocol
269 );
270 return Status;
271 }