blob: e06cf3bceb245360fc6495b01543d3847aa5ccaa [file] [log] [blame]
Channagoud Kadabif8aa7632015-11-12 14:27:01 -08001/** @file
2
3 Copyright (c) 2013-2014, ARM Ltd. All rights reserved.<BR>
4
5 This program and the accompanying materials
Jeevan Shriram17f173d2017-10-24 22:11:07 -07006 are licensed and made available under the terms and conditions of the BSD
7License
8 which accompanies this distribution. The full text of the license may be
9found at
Channagoud Kadabif8aa7632015-11-12 14:27:01 -080010 http://opensource.org/licenses/bsd-license.php
11
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14
15**/
16
jianzhou894cb182019-09-16 16:54:51 +080017/* Copyright (c) 2015-2020, The Linux Foundation. All rights reserved.
Channagoud Kadabif8aa7632015-11-12 14:27:01 -080018 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions are
21 * met:
22 * * Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * * Redistributions in binary form must reproduce the above
25 * copyright notice, this list of conditions and the following
26 * disclaimer in the documentation and/or other materials provided
27 * with the distribution.
28 * * Neither the name of The Linux Foundation nor the names of its
29 * contributors may be used to endorse or promote products derived
30 * from this software without specific prior written permission.
31 *
32 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
33 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
34 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
35 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
36 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
37 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
38 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
39 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
40 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
41 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
42 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43*/
44
45#include <Uefi.h>
Jeevan Shriram17f173d2017-10-24 22:11:07 -070046#include <Library/DebugLib.h>
47#include <Library/FastbootMenu.h>
48#include <Library/LinuxLoaderLib.h>
49#include <Library/MenuKeysDetection.h>
Channagoud Kadabif8aa7632015-11-12 14:27:01 -080050#include <Library/PcdLib.h>
Jeevan Shriram17f173d2017-10-24 22:11:07 -070051#include <Library/StackCanary.h>
Channagoud Kadabif8aa7632015-11-12 14:27:01 -080052#include <Library/UefiApplicationEntryPoint.h>
53#include <Library/UefiBootServicesTableLib.h>
Jeevan Shriram17f173d2017-10-24 22:11:07 -070054#include <Library/UefiLib.h>
Channagoud Kadabif8aa7632015-11-12 14:27:01 -080055#include <Library/UefiRuntimeServicesTableLib.h>
56#include <Protocol/EFIUsbDevice.h>
Channagoud Kadabif8aa7632015-11-12 14:27:01 -080057
Jeevan Shriram17f173d2017-10-24 22:11:07 -070058#include "BootStats.h"
Channagoud Kadabif8aa7632015-11-12 14:27:01 -080059#include "FastbootCmds.h"
60#include "FastbootMain.h"
Jeevan Shriram17f173d2017-10-24 22:11:07 -070061#include "UsbDescriptors.h"
Channagoud Kadabif8aa7632015-11-12 14:27:01 -080062
Mayank Grover0727b7b2020-11-18 12:05:17 +053063#define USB_BUFF_SIZE USB_BUFFER_SIZE
Channagoud Kadabif8aa7632015-11-12 14:27:01 -080064
65/* Global fastboot data */
66static FastbootDeviceData Fbd;
Channagoud Kadabic60eb382016-03-09 20:25:36 -080067static USB_DEVICE_DESCRIPTOR_SET DescSet;
68
69STATIC
70CONST
71struct {
Jeevan Shriram17f173d2017-10-24 22:11:07 -070072 EFI_USB_BOS_DESCRIPTOR BosDescriptor;
73 EFI_USB_USB_20_EXTENSION_DESCRIPTOR Usb2ExtDescriptor;
74 EFI_USB_SUPERSPEED_USB_DESCRIPTOR SsUsbDescriptor;
Jeevan Shriramdc555ce2018-06-21 16:31:23 -070075 EFI_USB_SUPERSPEEDPLUS_USB_DESCRIPTOR SspUsbDescriptor;
Channagoud Kadabic60eb382016-03-09 20:25:36 -080076} BinaryObjectStore = {
Jeevan Shriram17f173d2017-10-24 22:11:07 -070077 // BOS Descriptor
78 {
79 sizeof (EFI_USB_BOS_DESCRIPTOR), // Descriptor Size
80 USB_DESC_TYPE_BOS, // Descriptor Type
81 sizeof (BinaryObjectStore), // Total Length
Jeevan Shriramdc555ce2018-06-21 16:31:23 -070082 3 // Number of device capabilities
Jeevan Shriram17f173d2017-10-24 22:11:07 -070083 },
84 // USB2 Extension Desc
85 {
86 sizeof (EFI_USB_USB_20_EXTENSION_DESCRIPTOR), // Descriptor Size
87 USB_DESC_TYPE_DEVICE_CAPABILITY, // Device Capability Type descriptor
88 USB_DEV_CAP_TYPE_USB_20_EXTENSION, // USB 2.0 Extension Capability Type
89 0x6 // Supported device level features
90 },
91 // Super Speed Device Capability Desc
92 {
93 sizeof (EFI_USB_SUPERSPEED_USB_DESCRIPTOR), // Descriptor Size
94 USB_DESC_TYPE_DEVICE_CAPABILITY, // Device Capability Type descriptor
95 USB_DEV_CAP_TYPE_SUPERSPEED_USB, // SuperSpeed Device Capability Type
96 0x00, // Supported device level features
97 0x0E, // Speeds Supported by the device: SS, HS and FS
98 0x01, // Functionality support
99 0x07, // U1 Device Exit Latency
100 0x65 // U2 Device Exit Latency
Jeevan Shriramdc555ce2018-06-21 16:31:23 -0700101 },
102 // Super Speed Plus Device Capability Desc
103 {
104 sizeof (EFI_USB_SUPERSPEEDPLUS_USB_DESCRIPTOR), // Descriptor Size
105 USB_DESC_TYPE_DEVICE_CAPABILITY, // Device Capability Type descriptor
106 USB_DEV_CAP_TYPE_SUPERSPEEDPLUS_USB, //SuperSpeedPlus Device Capability
107 0x00, // Reserved
108 0x00000001, // Attributes
109 0x1100, // Functionality Support
110 0x00, // Reserved
111 {0x000A4030, 0x000A40B0}, // Sublink Speed Attribute
112 }
113};
Channagoud Kadabif8aa7632015-11-12 14:27:01 -0800114
jianzhouf44de782019-03-20 17:35:26 +0800115FastbootDeviceData *GetFastbootDeviceData (VOID)
Channagoud Kadabif8aa7632015-11-12 14:27:01 -0800116{
jianzhouf44de782019-03-20 17:35:26 +0800117 return &Fbd;
Channagoud Kadabif8aa7632015-11-12 14:27:01 -0800118}
119
120/* Dummy function needed for event notification callback */
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700121STATIC VOID
122DummyNotify (IN EFI_EVENT Event, IN VOID *Context)
Channagoud Kadabif8aa7632015-11-12 14:27:01 -0800123{
124}
125
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700126STATIC EFI_STATUS FastbootUsbDeviceStart (VOID)
Channagoud Kadabif8aa7632015-11-12 14:27:01 -0800127{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700128 EFI_STATUS Status;
Jeevan Shriramdc555ce2018-06-21 16:31:23 -0700129 EFI_USB_BUS_SPEED UsbMaxSupportSpeed;
130 UINTN UsbSpeedDataSize;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700131 USB_DEVICE_DESCRIPTOR *DevDesc;
132 USB_DEVICE_DESCRIPTOR *SSDevDesc;
133 VOID *Descriptors;
134 VOID *SSDescriptors;
135 EFI_EVENT UsbConfigEvt;
136 EFI_GUID UsbDeviceProtolGuid = {
137 0xd9d9ce48,
138 0x44b8,
139 0x4f49,
140 {0x8e, 0x3e, 0x2a, 0x3b, 0x92, 0x7d, 0xc6, 0xc1}};
141 EFI_GUID InitUsbControllerGuid = {
142 0x1c0cffce,
143 0xfc8d,
144 0x4e44,
145 {0x8c, 0x78, 0x9c, 0x9e, 0x5b, 0x53, 0xd, 0x36}};
Channagoud Kadabif8aa7632015-11-12 14:27:01 -0800146
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700147 Status = gBS->CreateEventEx (EVT_NOTIFY_SIGNAL, TPL_CALLBACK, DummyNotify,
148 NULL, &InitUsbControllerGuid, &UsbConfigEvt);
149 if (EFI_ERROR (Status)) {
150 DEBUG (
151 (EFI_D_ERROR, "Usb controller init event not signaled: %r\n", Status));
Channagoud Kadabif8aa7632015-11-12 14:27:01 -0800152 return Status;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700153 } else {
154 gBS->SignalEvent (UsbConfigEvt);
155 gBS->CloseEvent (UsbConfigEvt);
Channagoud Kadabif8aa7632015-11-12 14:27:01 -0800156 }
157
158 /* Locate the USBFastboot Protocol from DXE */
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700159 Status = gBS->LocateProtocol (&UsbDeviceProtolGuid, NULL,
160 (VOID **)&Fbd.UsbDeviceProtocol);
161 if (Status != EFI_SUCCESS) {
162 DEBUG ((EFI_D_ERROR, "couldnt find USB device protocol, exiting now"));
Channagoud Kadabif8aa7632015-11-12 14:27:01 -0800163 return Status;
164 }
165
Channagoud Kadabib854cc42016-02-24 17:55:09 -0800166 /* Register fastboot commands, allocate usb buffer*/
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700167 Status = FastbootCmdsInit ();
168 if (Status != EFI_SUCCESS) {
169 DEBUG ((EFI_D_ERROR, "couldnt init fastboot , exiting"));
Channagoud Kadabib854cc42016-02-24 17:55:09 -0800170 return Status;
171 }
172
Channagoud Kadabif8aa7632015-11-12 14:27:01 -0800173 /* Build the descriptor for fastboot */
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700174 BuildDefaultDescriptors (&DevDesc, &Descriptors, &SSDevDesc, &SSDescriptors);
Jeevan Shriramdc555ce2018-06-21 16:31:23 -0700175 UsbSpeedDataSize = sizeof (UsbMaxSupportSpeed);
176 Status = gRT->GetVariable ((CHAR16 *)L"UsbfnMaxSpeed",
177 &gQcomTokenSpaceGuid,
178 NULL,
179 &UsbSpeedDataSize,
180 &UsbMaxSupportSpeed);
181
182 if ((!EFI_ERROR (Status)) &&
183 (UsbMaxSupportSpeed == UsbBusSpeedSuperPlus)) {
184 SSDevDesc->BcdUSB = 0x0310;
185 }
Channagoud Kadabic60eb382016-03-09 20:25:36 -0800186
187 DescSet.DeviceDescriptor = DevDesc;
188 DescSet.Descriptors = &Descriptors;
189 DescSet.SSDeviceDescriptor = SSDevDesc;
190 DescSet.SSDescriptors = &SSDescriptors;
191 DescSet.DeviceQualifierDescriptor = &DeviceQualifier;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700192 DescSet.BinaryDeviceOjectStore = (VOID *)&BinaryObjectStore;
Channagoud Kadabic60eb382016-03-09 20:25:36 -0800193 DescSet.StringDescriptorCount = 5;
194 DescSet.StringDescritors = StrDescriptors;
195
Channagoud Kadabif8aa7632015-11-12 14:27:01 -0800196 /* Start the usb device */
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700197 Status = Fbd.UsbDeviceProtocol->StartEx (&DescSet);
198 if (EFI_ERROR (Status)) {
199 DEBUG ((EFI_D_ERROR,
200 "Error start the usb device, cannot enter fastboot mode\n"));
201 return EFI_NOT_STARTED;
lijuangb68034c2017-07-27 19:36:34 +0800202 }
Channagoud Kadabif8aa7632015-11-12 14:27:01 -0800203
204 /* Allocate buffers required to receive the data from Host*/
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700205 Status = Fbd.UsbDeviceProtocol->AllocateTransferBuffer (USB_BUFF_SIZE,
206 &Fbd.gRxBuffer);
207 if (EFI_ERROR (Status)) {
208 DEBUG ((EFI_D_ERROR,
209 "Error Allocate RX buffer, cannot enter fastboot mode\n"));
210 return EFI_OUT_OF_RESOURCES;
Channagoud Kadabif8aa7632015-11-12 14:27:01 -0800211 }
212
213 /* Allocate buffers required to send data from device to Host*/
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700214 Status = Fbd.UsbDeviceProtocol->AllocateTransferBuffer (USB_BUFF_SIZE,
215 &Fbd.gTxBuffer);
216 if (EFI_ERROR (Status)) {
217 DEBUG ((EFI_D_ERROR,
218 "Error Allocate TX buffer, cannot enter fastboot mode\n"));
219 return EFI_OUT_OF_RESOURCES;
Channagoud Kadabif8aa7632015-11-12 14:27:01 -0800220 }
221
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700222 DEBUG ((EFI_D_INFO, "Fastboot: Processing commands\n"));
Channagoud Kadabif8aa7632015-11-12 14:27:01 -0800223
224 return Status;
225}
226
227/* API to stop USB device when booting to kernel, used for "fastboot boot" */
228EFI_STATUS
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700229FastbootUsbDeviceStop (VOID)
Channagoud Kadabif8aa7632015-11-12 14:27:01 -0800230{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700231 EFI_STATUS Status;
Channagoud Kadabif8aa7632015-11-12 14:27:01 -0800232
233 /* Free the Rx & Tx Buffers */
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700234 Status = Fbd.UsbDeviceProtocol->FreeTransferBuffer (Fbd.gTxBuffer);
235 if (EFI_ERROR (Status)) {
236 DEBUG ((EFI_D_ERROR, "Fastboot USB: Unable to free Tx Buffer\n"));
237 return Status;
Jeevan Shriram9b774892016-10-11 12:44:29 -0700238 }
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700239 Status = Fbd.UsbDeviceProtocol->FreeTransferBuffer (Fbd.gRxBuffer);
240 if (EFI_ERROR (Status)) {
241 DEBUG ((EFI_D_ERROR, "Fastboot USB: Unable to free Rx Buffer\n"));
242 return Status;
Jeevan Shriram9b774892016-10-11 12:44:29 -0700243 }
244
Channagoud Kadabif8aa7632015-11-12 14:27:01 -0800245 return Status;
246}
247
248/* Process bulk transfer out come for Rx */
lijuangf0bbcad2017-08-16 16:59:18 +0800249STATIC EFI_STATUS
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700250ProcessBulkXfrCompleteRx (IN USB_DEVICE_TRANSFER_OUTCOME *Uto)
Channagoud Kadabif8aa7632015-11-12 14:27:01 -0800251{
252 EFI_STATUS Status = EFI_SUCCESS;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700253
Channagoud Kadabif8aa7632015-11-12 14:27:01 -0800254 // switch on the transfer status
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700255 switch (Uto->Status) {
256 case UsbDeviceTransferStatusCompleteOK:
257 if (FastbootCurrentState () == ExpectDataState)
258 DataReady (Uto->BytesCompleted, FastbootDloadBuffer ());
259 else
260 DataReady (Uto->BytesCompleted, Fbd.gRxBuffer);
261 break;
Channagoud Kadabif8aa7632015-11-12 14:27:01 -0800262
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700263 case UsbDeviceTransferStatusCancelled:
264 // if usb connected, retry, otherwise wait to get connected, then retry
265 DEBUG ((EFI_D_ERROR, "Bulk in XFR aborted\n"));
266 Status = EFI_ABORTED;
267 break;
Channagoud Kadabif8aa7632015-11-12 14:27:01 -0800268
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700269 default: // Other statuses should not occur
270 Status = EFI_DEVICE_ERROR;
271 break;
Channagoud Kadabif8aa7632015-11-12 14:27:01 -0800272 }
273 return Status;
274}
275
276/* Process bulk transfer out come for Tx */
lijuangf0bbcad2017-08-16 16:59:18 +0800277STATIC EFI_STATUS
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700278ProcessBulkXfrCompleteTx (IN USB_DEVICE_TRANSFER_OUTCOME *Uto)
Channagoud Kadabif8aa7632015-11-12 14:27:01 -0800279{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700280 EFI_STATUS Status = EFI_SUCCESS;
Channagoud Kadabif8aa7632015-11-12 14:27:01 -0800281
282 // Switch on the transfer status
283 switch (Uto->Status) {
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700284 case UsbDeviceTransferStatusCompleteOK:
285 DEBUG ((EFI_D_VERBOSE, "UsbDeviceTransferStatusCompleteOK\n"));
286 /* Just Queue the next recieve, must be a Command */
287 if (FastbootCurrentState () == ExpectDataState)
288 Status = Fbd.UsbDeviceProtocol->Send (ENDPOINT_IN, GetXfrSize (),
289 FastbootDloadBuffer ());
290 else
291 Status = Fbd.UsbDeviceProtocol->Send (ENDPOINT_IN, GetXfrSize (),
292 Fbd.gRxBuffer);
293 break;
Channagoud Kadabif8aa7632015-11-12 14:27:01 -0800294
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700295 case UsbDeviceTransferStatusCancelled:
296 DEBUG ((EFI_D_ERROR, "Bulk in xfr aborted"));
297 Status = EFI_ABORTED;
298 break;
Channagoud Kadabif8aa7632015-11-12 14:27:01 -0800299
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700300 default: // Other statuses should not occur
301 DEBUG ((EFI_D_ERROR, "unhandled trasnfer status"));
302 Status = EFI_DEVICE_ERROR;
303 break;
Channagoud Kadabif8aa7632015-11-12 14:27:01 -0800304 }
305 return Status;
306}
307
308/* Handle USB events, this will keep looking for events from USB protocol */
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700309EFI_STATUS HandleUsbEvents (VOID)
Channagoud Kadabif8aa7632015-11-12 14:27:01 -0800310{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700311 EFI_STATUS Status = EFI_SUCCESS;
312 USB_DEVICE_EVENT Msg;
313 USB_DEVICE_EVENT_DATA Payload;
314 UINTN PayloadSize;
Channagoud Kadabif8aa7632015-11-12 14:27:01 -0800315
316 /* Look for Event from Usb device protocol */
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700317 Fbd.UsbDeviceProtocol->HandleEvent (&Msg, &PayloadSize, &Payload);
318 if (UsbDeviceEventDeviceStateChange == Msg) {
Channagoud Kadabif8aa7632015-11-12 14:27:01 -0800319 if (UsbDeviceStateConnected == Payload.DeviceState) {
Channagoud Kadabib5e102a2016-02-08 15:27:29 -0800320 DEBUG ((EFI_D_VERBOSE, "Fastboot Device connected\n"));
Channagoud Kadabif8aa7632015-11-12 14:27:01 -0800321 /* Queue receive buffer */
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700322 Status = Fbd.UsbDeviceProtocol->Send (0x1, 511, Fbd.gRxBuffer);
Channagoud Kadabif8aa7632015-11-12 14:27:01 -0800323 }
324 if (UsbDeviceStateDisconnected == Payload.DeviceState) {
Channagoud Kadabib5e102a2016-02-08 15:27:29 -0800325 DEBUG ((EFI_D_VERBOSE, "Fastboot Device disconnected\n"));
Channagoud Kadabif8aa7632015-11-12 14:27:01 -0800326 }
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700327 } else if (UsbDeviceEventTransferNotification == Msg) {
Channagoud Kadabif8aa7632015-11-12 14:27:01 -0800328 /* Check if the transfer notification is on the Bulk EP and process it*/
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700329 if (1 == USB_INDEX_TO_EP (Payload.TransferOutcome.EndpointIndex)) {
Channagoud Kadabif8aa7632015-11-12 14:27:01 -0800330 /* If the direction is from host to device then process RX */
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700331 if (USB_ENDPOINT_DIRECTION_OUT ==
332 USB_INDEX_TO_EPDIR (Payload.TransferOutcome.EndpointIndex)) {
Channagoud Kadabif8aa7632015-11-12 14:27:01 -0800333
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700334 Status = ProcessBulkXfrCompleteRx (&Payload.TransferOutcome);
335 if (EFI_ERROR (Status)) {
336 /* Should not happen, even if it happens we keep waiting for USB to be
337 * connected */
338 DEBUG ((EFI_D_ERROR,
339 "Error, should not happen! Check your USB connection"));
Channagoud Kadabif8aa7632015-11-12 14:27:01 -0800340 }
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700341 } else {
Channagoud Kadabif8aa7632015-11-12 14:27:01 -0800342 /* Else the direction is from device to host, process TX */
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700343 Status = ProcessBulkXfrCompleteTx (&Payload.TransferOutcome);
344 if (EFI_ERROR (Status)) {
345 /* Should not happen, even if it happens we keep waiting for USB to be
346 * connected */
347 DEBUG ((EFI_D_ERROR,
348 "Error, should not happen! Check your USB connection"));
Channagoud Kadabif8aa7632015-11-12 14:27:01 -0800349 }
350 }
351 }
352 }
353 return Status;
354}
355
lijuang61311382016-12-12 18:36:33 +0800356/* Initialize and start fastboot */
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700357EFI_STATUS FastbootInitialize (VOID)
Channagoud Kadabif8aa7632015-11-12 14:27:01 -0800358{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700359 EFI_STATUS Status = EFI_SUCCESS;
Channagoud Kadabif8aa7632015-11-12 14:27:01 -0800360
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700361 DEBUG ((EFI_D_INFO, "Fastboot Build Info: %a %a\n", __DATE__, __TIME__));
362 BootStatsSetTimeStamp (BS_BL_START);
Channagoud Kadabi47c23002016-03-25 18:12:01 -0700363
Channagoud Kadabif8aa7632015-11-12 14:27:01 -0800364 /* Start the USB device enumeration */
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700365 Status = FastbootUsbDeviceStart ();
366 if (Status != EFI_SUCCESS) {
367 DEBUG ((EFI_D_ERROR, "couldnt Start fastboot usb device, exiting"));
Channagoud Kadabif8aa7632015-11-12 14:27:01 -0800368 return Status;
369 }
370
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700371 DisplayFastbootMenu ();
lijuangb1bb45d2016-07-27 20:03:52 +0800372
Channagoud Kadabif8aa7632015-11-12 14:27:01 -0800373 /* Wait for USB events in tight loop */
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700374 while (1) {
375 Status = HandleUsbEvents ();
376 if (EFI_ERROR (Status) && (Status != EFI_ABORTED)) {
377 DEBUG ((EFI_D_ERROR, "Error, failed to handle USB event\n"));
lijuangb68034c2017-07-27 19:36:34 +0800378 break;
379 }
Channagoud Kadabif8aa7632015-11-12 14:27:01 -0800380
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700381 if (FastbootFatal ()) {
382 DEBUG ((EFI_D_ERROR, "Continue detected, Exiting App...\n"));
Channagoud Kadabif8aa7632015-11-12 14:27:01 -0800383 break;
384 }
385 }
386
387 /* Close the fastboot app and stop USB device */
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700388 Status = FastbootCmdsUnInit ();
389 if (Status != EFI_SUCCESS) {
390 DEBUG ((EFI_D_ERROR, "couldnt uninit fastboot\n"));
Channagoud Kadabif8aa7632015-11-12 14:27:01 -0800391 return Status;
392 }
393
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700394 ExitMenuKeysDetection ();
lijuangb1bb45d2016-07-27 20:03:52 +0800395
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700396 Status = FastbootUsbDeviceStop ();
Channagoud Kadabif8aa7632015-11-12 14:27:01 -0800397 return Status;
398}