Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 1 | /** @file |
| 2 | |
| 3 | Copyright (c) 2013-2014, ARM Ltd. All rights reserved.<BR> |
| 4 | |
| 5 | This program and the accompanying materials |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 6 | are licensed and made available under the terms and conditions of the BSD |
| 7 | License |
| 8 | which accompanies this distribution. The full text of the license may be |
| 9 | found at |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 10 | 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 | |
jianzhou | 894cb18 | 2019-09-16 16:54:51 +0800 | [diff] [blame] | 17 | /* Copyright (c) 2015-2020, The Linux Foundation. All rights reserved. |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 18 | * |
| 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 Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 46 | #include <Library/DebugLib.h> |
| 47 | #include <Library/FastbootMenu.h> |
| 48 | #include <Library/LinuxLoaderLib.h> |
| 49 | #include <Library/MenuKeysDetection.h> |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 50 | #include <Library/PcdLib.h> |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 51 | #include <Library/StackCanary.h> |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 52 | #include <Library/UefiApplicationEntryPoint.h> |
| 53 | #include <Library/UefiBootServicesTableLib.h> |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 54 | #include <Library/UefiLib.h> |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 55 | #include <Library/UefiRuntimeServicesTableLib.h> |
| 56 | #include <Protocol/EFIUsbDevice.h> |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 57 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 58 | #include "BootStats.h" |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 59 | #include "FastbootCmds.h" |
| 60 | #include "FastbootMain.h" |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 61 | #include "UsbDescriptors.h" |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 62 | |
Mayank Grover | 0727b7b | 2020-11-18 12:05:17 +0530 | [diff] [blame^] | 63 | #define USB_BUFF_SIZE USB_BUFFER_SIZE |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 64 | |
| 65 | /* Global fastboot data */ |
| 66 | static FastbootDeviceData Fbd; |
Channagoud Kadabi | c60eb38 | 2016-03-09 20:25:36 -0800 | [diff] [blame] | 67 | static USB_DEVICE_DESCRIPTOR_SET DescSet; |
| 68 | |
| 69 | STATIC |
| 70 | CONST |
| 71 | struct { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 72 | EFI_USB_BOS_DESCRIPTOR BosDescriptor; |
| 73 | EFI_USB_USB_20_EXTENSION_DESCRIPTOR Usb2ExtDescriptor; |
| 74 | EFI_USB_SUPERSPEED_USB_DESCRIPTOR SsUsbDescriptor; |
Jeevan Shriram | dc555ce | 2018-06-21 16:31:23 -0700 | [diff] [blame] | 75 | EFI_USB_SUPERSPEEDPLUS_USB_DESCRIPTOR SspUsbDescriptor; |
Channagoud Kadabi | c60eb38 | 2016-03-09 20:25:36 -0800 | [diff] [blame] | 76 | } BinaryObjectStore = { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 77 | // BOS Descriptor |
| 78 | { |
| 79 | sizeof (EFI_USB_BOS_DESCRIPTOR), // Descriptor Size |
| 80 | USB_DESC_TYPE_BOS, // Descriptor Type |
| 81 | sizeof (BinaryObjectStore), // Total Length |
Jeevan Shriram | dc555ce | 2018-06-21 16:31:23 -0700 | [diff] [blame] | 82 | 3 // Number of device capabilities |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 83 | }, |
| 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 Shriram | dc555ce | 2018-06-21 16:31:23 -0700 | [diff] [blame] | 101 | }, |
| 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 Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 114 | |
jianzhou | f44de78 | 2019-03-20 17:35:26 +0800 | [diff] [blame] | 115 | FastbootDeviceData *GetFastbootDeviceData (VOID) |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 116 | { |
jianzhou | f44de78 | 2019-03-20 17:35:26 +0800 | [diff] [blame] | 117 | return &Fbd; |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /* Dummy function needed for event notification callback */ |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 121 | STATIC VOID |
| 122 | DummyNotify (IN EFI_EVENT Event, IN VOID *Context) |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 123 | { |
| 124 | } |
| 125 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 126 | STATIC EFI_STATUS FastbootUsbDeviceStart (VOID) |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 127 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 128 | EFI_STATUS Status; |
Jeevan Shriram | dc555ce | 2018-06-21 16:31:23 -0700 | [diff] [blame] | 129 | EFI_USB_BUS_SPEED UsbMaxSupportSpeed; |
| 130 | UINTN UsbSpeedDataSize; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 131 | 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 Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 146 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 147 | 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 Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 152 | return Status; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 153 | } else { |
| 154 | gBS->SignalEvent (UsbConfigEvt); |
| 155 | gBS->CloseEvent (UsbConfigEvt); |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /* Locate the USBFastboot Protocol from DXE */ |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 159 | 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 Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 163 | return Status; |
| 164 | } |
| 165 | |
Channagoud Kadabi | b854cc4 | 2016-02-24 17:55:09 -0800 | [diff] [blame] | 166 | /* Register fastboot commands, allocate usb buffer*/ |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 167 | Status = FastbootCmdsInit (); |
| 168 | if (Status != EFI_SUCCESS) { |
| 169 | DEBUG ((EFI_D_ERROR, "couldnt init fastboot , exiting")); |
Channagoud Kadabi | b854cc4 | 2016-02-24 17:55:09 -0800 | [diff] [blame] | 170 | return Status; |
| 171 | } |
| 172 | |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 173 | /* Build the descriptor for fastboot */ |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 174 | BuildDefaultDescriptors (&DevDesc, &Descriptors, &SSDevDesc, &SSDescriptors); |
Jeevan Shriram | dc555ce | 2018-06-21 16:31:23 -0700 | [diff] [blame] | 175 | 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 Kadabi | c60eb38 | 2016-03-09 20:25:36 -0800 | [diff] [blame] | 186 | |
| 187 | DescSet.DeviceDescriptor = DevDesc; |
| 188 | DescSet.Descriptors = &Descriptors; |
| 189 | DescSet.SSDeviceDescriptor = SSDevDesc; |
| 190 | DescSet.SSDescriptors = &SSDescriptors; |
| 191 | DescSet.DeviceQualifierDescriptor = &DeviceQualifier; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 192 | DescSet.BinaryDeviceOjectStore = (VOID *)&BinaryObjectStore; |
Channagoud Kadabi | c60eb38 | 2016-03-09 20:25:36 -0800 | [diff] [blame] | 193 | DescSet.StringDescriptorCount = 5; |
| 194 | DescSet.StringDescritors = StrDescriptors; |
| 195 | |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 196 | /* Start the usb device */ |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 197 | 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; |
lijuang | b68034c | 2017-07-27 19:36:34 +0800 | [diff] [blame] | 202 | } |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 203 | |
| 204 | /* Allocate buffers required to receive the data from Host*/ |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 205 | 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 Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /* Allocate buffers required to send data from device to Host*/ |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 214 | 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 Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 220 | } |
| 221 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 222 | DEBUG ((EFI_D_INFO, "Fastboot: Processing commands\n")); |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 223 | |
| 224 | return Status; |
| 225 | } |
| 226 | |
| 227 | /* API to stop USB device when booting to kernel, used for "fastboot boot" */ |
| 228 | EFI_STATUS |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 229 | FastbootUsbDeviceStop (VOID) |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 230 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 231 | EFI_STATUS Status; |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 232 | |
| 233 | /* Free the Rx & Tx Buffers */ |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 234 | 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 Shriram | 9b77489 | 2016-10-11 12:44:29 -0700 | [diff] [blame] | 238 | } |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 239 | 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 Shriram | 9b77489 | 2016-10-11 12:44:29 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 245 | return Status; |
| 246 | } |
| 247 | |
| 248 | /* Process bulk transfer out come for Rx */ |
lijuang | f0bbcad | 2017-08-16 16:59:18 +0800 | [diff] [blame] | 249 | STATIC EFI_STATUS |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 250 | ProcessBulkXfrCompleteRx (IN USB_DEVICE_TRANSFER_OUTCOME *Uto) |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 251 | { |
| 252 | EFI_STATUS Status = EFI_SUCCESS; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 253 | |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 254 | // switch on the transfer status |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 255 | 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 Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 262 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 263 | 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 Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 268 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 269 | default: // Other statuses should not occur |
| 270 | Status = EFI_DEVICE_ERROR; |
| 271 | break; |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 272 | } |
| 273 | return Status; |
| 274 | } |
| 275 | |
| 276 | /* Process bulk transfer out come for Tx */ |
lijuang | f0bbcad | 2017-08-16 16:59:18 +0800 | [diff] [blame] | 277 | STATIC EFI_STATUS |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 278 | ProcessBulkXfrCompleteTx (IN USB_DEVICE_TRANSFER_OUTCOME *Uto) |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 279 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 280 | EFI_STATUS Status = EFI_SUCCESS; |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 281 | |
| 282 | // Switch on the transfer status |
| 283 | switch (Uto->Status) { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 284 | 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 Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 294 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 295 | case UsbDeviceTransferStatusCancelled: |
| 296 | DEBUG ((EFI_D_ERROR, "Bulk in xfr aborted")); |
| 297 | Status = EFI_ABORTED; |
| 298 | break; |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 299 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 300 | default: // Other statuses should not occur |
| 301 | DEBUG ((EFI_D_ERROR, "unhandled trasnfer status")); |
| 302 | Status = EFI_DEVICE_ERROR; |
| 303 | break; |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 304 | } |
| 305 | return Status; |
| 306 | } |
| 307 | |
| 308 | /* Handle USB events, this will keep looking for events from USB protocol */ |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 309 | EFI_STATUS HandleUsbEvents (VOID) |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 310 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 311 | EFI_STATUS Status = EFI_SUCCESS; |
| 312 | USB_DEVICE_EVENT Msg; |
| 313 | USB_DEVICE_EVENT_DATA Payload; |
| 314 | UINTN PayloadSize; |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 315 | |
| 316 | /* Look for Event from Usb device protocol */ |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 317 | Fbd.UsbDeviceProtocol->HandleEvent (&Msg, &PayloadSize, &Payload); |
| 318 | if (UsbDeviceEventDeviceStateChange == Msg) { |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 319 | if (UsbDeviceStateConnected == Payload.DeviceState) { |
Channagoud Kadabi | b5e102a | 2016-02-08 15:27:29 -0800 | [diff] [blame] | 320 | DEBUG ((EFI_D_VERBOSE, "Fastboot Device connected\n")); |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 321 | /* Queue receive buffer */ |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 322 | Status = Fbd.UsbDeviceProtocol->Send (0x1, 511, Fbd.gRxBuffer); |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 323 | } |
| 324 | if (UsbDeviceStateDisconnected == Payload.DeviceState) { |
Channagoud Kadabi | b5e102a | 2016-02-08 15:27:29 -0800 | [diff] [blame] | 325 | DEBUG ((EFI_D_VERBOSE, "Fastboot Device disconnected\n")); |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 326 | } |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 327 | } else if (UsbDeviceEventTransferNotification == Msg) { |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 328 | /* Check if the transfer notification is on the Bulk EP and process it*/ |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 329 | if (1 == USB_INDEX_TO_EP (Payload.TransferOutcome.EndpointIndex)) { |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 330 | /* If the direction is from host to device then process RX */ |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 331 | if (USB_ENDPOINT_DIRECTION_OUT == |
| 332 | USB_INDEX_TO_EPDIR (Payload.TransferOutcome.EndpointIndex)) { |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 333 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 334 | 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 Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 340 | } |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 341 | } else { |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 342 | /* Else the direction is from device to host, process TX */ |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 343 | 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 Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 349 | } |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | return Status; |
| 354 | } |
| 355 | |
lijuang | 6131138 | 2016-12-12 18:36:33 +0800 | [diff] [blame] | 356 | /* Initialize and start fastboot */ |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 357 | EFI_STATUS FastbootInitialize (VOID) |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 358 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 359 | EFI_STATUS Status = EFI_SUCCESS; |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 360 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 361 | DEBUG ((EFI_D_INFO, "Fastboot Build Info: %a %a\n", __DATE__, __TIME__)); |
| 362 | BootStatsSetTimeStamp (BS_BL_START); |
Channagoud Kadabi | 47c2300 | 2016-03-25 18:12:01 -0700 | [diff] [blame] | 363 | |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 364 | /* Start the USB device enumeration */ |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 365 | Status = FastbootUsbDeviceStart (); |
| 366 | if (Status != EFI_SUCCESS) { |
| 367 | DEBUG ((EFI_D_ERROR, "couldnt Start fastboot usb device, exiting")); |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 368 | return Status; |
| 369 | } |
| 370 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 371 | DisplayFastbootMenu (); |
lijuang | b1bb45d | 2016-07-27 20:03:52 +0800 | [diff] [blame] | 372 | |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 373 | /* Wait for USB events in tight loop */ |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 374 | 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")); |
lijuang | b68034c | 2017-07-27 19:36:34 +0800 | [diff] [blame] | 378 | break; |
| 379 | } |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 380 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 381 | if (FastbootFatal ()) { |
| 382 | DEBUG ((EFI_D_ERROR, "Continue detected, Exiting App...\n")); |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 383 | break; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | /* Close the fastboot app and stop USB device */ |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 388 | Status = FastbootCmdsUnInit (); |
| 389 | if (Status != EFI_SUCCESS) { |
| 390 | DEBUG ((EFI_D_ERROR, "couldnt uninit fastboot\n")); |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 391 | return Status; |
| 392 | } |
| 393 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 394 | ExitMenuKeysDetection (); |
lijuang | b1bb45d | 2016-07-27 20:03:52 +0800 | [diff] [blame] | 395 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 396 | Status = FastbootUsbDeviceStop (); |
Channagoud Kadabi | f8aa763 | 2015-11-12 14:27:01 -0800 | [diff] [blame] | 397 | return Status; |
| 398 | } |