/** @file\r
The module to produce Usb Bus PPI.\r
\r
-Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>\r
- \r
+Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
+\r
This program and the accompanying materials\r
are licensed and made available under the terms and conditions\r
of the BSD License which accompanies this distribution. The\r
\r
/**\r
The enumeration routine to detect device change.\r
- \r
+\r
@param PeiServices Describes the list of possible PEI Services.\r
@param UsbHcPpi The pointer of PEI_USB_HOST_CONTROLLER_PPI instance.\r
@param Usb2HcPpi The pointer of PEI_USB2_HOST_CONTROLLER_PPI instance.\r
\r
/**\r
Configure new detected usb device.\r
- \r
+\r
@param PeiServices Describes the list of possible PEI Services.\r
@param PeiUsbDevice The pointer of PEI_USB_DEVICE instance.\r
@param Port The port to be configured.\r
\r
/**\r
Get all configurations from a detected usb device.\r
- \r
+\r
@param PeiServices Describes the list of possible PEI Services.\r
@param PeiUsbDevice The pointer of PEI_USB_DEVICE instance.\r
\r
\r
/**\r
Get the start position of next wanted descriptor.\r
- \r
+\r
@param Buffer Buffer containing data to parse.\r
@param Length Buffer length.\r
@param DescType Descriptor type.\r
\r
/**\r
The entrypoint of the module, it will enumerate all HCs.\r
- \r
+\r
@param FileHandle Handle of the file being invoked.\r
@param PeiServices Describes the list of possible PEI Services.\r
\r
}\r
\r
//\r
- // gPeiUsbHostControllerPpiGuid and gPeiUsb2HostControllerPpiGuid should not \r
+ // gPeiUsbHostControllerPpiGuid and gPeiUsb2HostControllerPpiGuid should not\r
// be produced at the same time\r
//\r
Index = 0;\r
Index,\r
NULL,\r
(VOID **) &Usb2HcPpi\r
- ); \r
+ );\r
if (EFI_ERROR (Status)) {\r
//\r
// No more host controller, break out\r
//\r
break;\r
- } \r
- PeiUsbEnumeration ((EFI_PEI_SERVICES **) PeiServices, NULL, Usb2HcPpi); \r
+ }\r
+ PeiUsbEnumeration ((EFI_PEI_SERVICES **) PeiServices, NULL, Usb2HcPpi);\r
Index++;\r
}\r
}\r
- \r
+\r
if (Index == 0) {\r
return EFI_UNSUPPORTED;\r
}\r
/**\r
The Hub Enumeration just scans the hub ports one time. It also\r
doesn't support hot-plug.\r
- \r
+\r
@param PeiServices Describes the list of possible PEI Services.\r
@param PeiUsbDevice The pointer of PEI_USB_DEVICE instance.\r
@param CurrentAddress The DeviceAddress of usb device.\r
NewPeiUsbDevice->IsHub = 0x0;\r
NewPeiUsbDevice->DownStreamPortNo = 0x0;\r
\r
- if (((PortStatus.PortChangeStatus & USB_PORT_STAT_C_RESET) == 0) || \r
+ if (((PortStatus.PortChangeStatus & USB_PORT_STAT_C_RESET) == 0) ||\r
((PortStatus.PortStatus & (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE)) == 0)) {\r
//\r
- // If the port already has reset change flag and is connected and enabled, skip the port reset logic. \r
+ // If the port already has reset change flag and is connected and enabled, skip the port reset logic.\r
//\r
PeiResetHubPort (PeiServices, UsbIoPpi, (UINT8)(Index + 1));\r
- \r
+\r
PeiHubGetPortStatus (\r
PeiServices,\r
UsbIoPpi,\r
\r
/**\r
The enumeration routine to detect device change.\r
- \r
+\r
@param PeiServices Describes the list of possible PEI Services.\r
@param UsbHcPpi The pointer of PEI_USB_HOST_CONTROLLER_PPI instance.\r
@param Usb2HcPpi The pointer of PEI_USB2_HOST_CONTROLLER_PPI instance.\r
PeiServices,\r
Usb2HcPpi,\r
(UINT8 *) &NumOfRootPort\r
- ); \r
+ );\r
} else if (UsbHcPpi != NULL) {\r
UsbHcPpi->GetRootHubPortNumber (\r
PeiServices,\r
Usb2HcPpi,\r
(UINT8) Index,\r
&PortStatus\r
- ); \r
+ );\r
} else {\r
UsbHcPpi->GetRootHubPortStatus (\r
PeiServices,\r
PeiUsbDevice->IsHub = 0x0;\r
PeiUsbDevice->DownStreamPortNo = 0x0;\r
\r
- if (((PortStatus.PortChangeStatus & USB_PORT_STAT_C_RESET) == 0) || \r
+ if (((PortStatus.PortChangeStatus & USB_PORT_STAT_C_RESET) == 0) ||\r
((PortStatus.PortStatus & (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE)) == 0)) {\r
//\r
- // If the port already has reset change flag and is connected and enabled, skip the port reset logic. \r
+ // If the port already has reset change flag and is connected and enabled, skip the port reset logic.\r
//\r
ResetRootPort (\r
PeiServices,\r
Usb2HcPpi,\r
(UINT8) Index,\r
EfiUsbPortResetChange\r
- ); \r
+ );\r
} else {\r
UsbHcPpi->ClearRootHubPortFeature (\r
PeiServices,\r
\r
/**\r
Configure new detected usb device.\r
- \r
+\r
@param PeiServices Describes the list of possible PEI Services.\r
@param PeiUsbDevice The pointer of PEI_USB_DEVICE instance.\r
@param Port The port to be configured.\r
\r
/**\r
Get all configurations from a detected usb device.\r
- \r
+\r
@param PeiServices Describes the list of possible PEI Services.\r
@param PeiUsbDevice The pointer of PEI_USB_DEVICE instance.\r
\r
\r
/**\r
Get the start position of next wanted descriptor.\r
- \r
+\r
@param Buffer Buffer containing data to parse.\r
@param Length Buffer length.\r
@param DescType Descriptor type.\r
OUT UINTN *ParsedBytes\r
)\r
{\r
- UINT16 DescriptorHeader;\r
- UINT8 Len;\r
- UINT8 *Ptr;\r
- UINTN Parsed;\r
+ USB_DESC_HEAD *Head;\r
+ UINTN Offset;\r
\r
- Parsed = 0;\r
- Ptr = Buffer;\r
+ //\r
+ // Total length is too small that cannot hold the single descriptor header plus data. \r
+ //\r
+ if (Length <= sizeof (USB_DESC_HEAD)) {\r
+ DEBUG ((DEBUG_ERROR, "GetExpectedDescriptor: met mal-format descriptor, total length = %d!\n", Length));\r
+ return EFI_DEVICE_ERROR;\r
+ }\r
\r
- while (TRUE) {\r
+ //\r
+ // All the descriptor has a common LTV (Length, Type, Value)\r
+ // format. Skip the descriptor that isn't of this Type\r
+ //\r
+ Offset = 0;\r
+ Head = (USB_DESC_HEAD *)Buffer;\r
+ while (Offset < Length - sizeof (USB_DESC_HEAD)) {\r
//\r
- // Buffer length should not less than Desc length\r
+ // Above condition make sure Head->Len and Head->Type are safe to access\r
//\r
- if (Length < DescLength) {\r
- return EFI_DEVICE_ERROR;\r
- }\r
-\r
- DescriptorHeader = (UINT16) (*Ptr + ((*(Ptr + 1)) << 8));\r
-\r
- Len = Buffer[0];\r
+ Head = (USB_DESC_HEAD *)&Buffer[Offset];\r
\r
- //\r
- // Check to see if it is a start of expected descriptor\r
- //\r
- if (DescriptorHeader == ((DescType << 8) | DescLength)) {\r
- break;\r
+ if (Head->Len == 0) {\r
+ DEBUG ((DEBUG_ERROR, "GetExpectedDescriptor: met mal-format descriptor, Head->Len = 0!\n"));\r
+ return EFI_DEVICE_ERROR;\r
}\r
\r
- if ((UINT8) (DescriptorHeader >> 8) == DescType) {\r
- if (Len > DescLength) {\r
- return EFI_DEVICE_ERROR;\r
- }\r
- }\r
//\r
- // Descriptor length should be at least 2\r
- // and should not exceed the buffer length\r
+ // Make sure no overflow when adding Head->Len to Offset.\r
//\r
- if (Len < 2) {\r
+ if (Head->Len > MAX_UINTN - Offset) {\r
+ DEBUG ((DEBUG_ERROR, "GetExpectedDescriptor: met mal-format descriptor, Head->Len = %d!\n", Head->Len));\r
return EFI_DEVICE_ERROR;\r
}\r
\r
- if (Len > Length) {\r
- return EFI_DEVICE_ERROR;\r
+ if (Head->Type == DescType) {\r
+ break;\r
}\r
- //\r
- // Skip this mismatch descriptor\r
- //\r
- Length -= Len;\r
- Ptr += Len;\r
- Parsed += Len;\r
+\r
+ Offset += Head->Len;\r
}\r
\r
- *ParsedBytes = Parsed;\r
+ //\r
+ // Head->Len is invalid resulting data beyond boundary, or\r
+ // Descriptor cannot be found: No such type.\r
+ //\r
+ if (Length < Offset) {\r
+ DEBUG ((DEBUG_ERROR, "GetExpectedDescriptor: met mal-format descriptor, Offset/Len = %d/%d!\n", Offset, Length));\r
+ return EFI_DEVICE_ERROR;\r
+ }\r
\r
+ if ((Head->Type != DescType) || (Head->Len < DescLength)) {\r
+ DEBUG ((DEBUG_ERROR, "GetExpectedDescriptor: descriptor cannot be found, Header(T/L) = %d/%d!\n", Head->Type, Head->Len));\r
+ return EFI_DEVICE_ERROR;\r
+ }\r
+\r
+ *ParsedBytes = Offset;\r
return EFI_SUCCESS;\r
}\r
\r
/**\r
Send reset signal over the given root hub port.\r
- \r
+\r
@param PeiServices Describes the list of possible PEI Services.\r
@param UsbHcPpi The pointer of PEI_USB_HOST_CONTROLLER_PPI instance.\r
@param Usb2HcPpi The pointer of PEI_USB2_HOST_CONTROLLER_PPI instance.\r
\r
if (Usb2HcPpi != NULL) {\r
MicroSecondDelay (200 * 1000);\r
- \r
+\r
//\r
// reset root port\r
//\r
PortNum,\r
EfiUsbPortReset\r
);\r
- \r
+\r
if (EFI_ERROR (Status)) {\r
DEBUG ((EFI_D_ERROR, "SetRootHubPortFeature EfiUsbPortReset Failed\n"));\r
return;\r
}\r
- \r
+\r
//\r
// Drive the reset signal for at least 50ms. Check USB 2.0 Spec\r
// section 7.1.7.5 for timing requirements.\r
PortNum,\r
EfiUsbPortReset\r
);\r
- \r
+\r
if (EFI_ERROR (Status)) {\r
DEBUG ((EFI_D_ERROR, "ClearRootHubPortFeature EfiUsbPortReset Failed\n"));\r
return;\r
Usb2HcPpi,\r
PortNum,\r
&PortStatus\r
- ); \r
+ );\r
if (EFI_ERROR (Status)) {\r
return;\r
}\r
PortNum,\r
EfiUsbPortConnectChange\r
);\r
- \r
+\r
//\r
// Set port enable\r
//\r
PortNum,\r
EfiUsbPortEnable\r
);\r
- \r
+\r
Usb2HcPpi->ClearRootHubPortFeature (\r
PeiServices,\r
Usb2HcPpi,\r
PortNum,\r
EfiUsbPortEnableChange\r
);\r
- \r
+\r
MicroSecondDelay ((RetryIndex + 1) * 50 * 1000);\r
} else {\r
MicroSecondDelay (200 * 1000);\r
- \r
+\r
//\r
// reset root port\r
//\r
PortNum,\r
EfiUsbPortReset\r
);\r
- \r
+\r
if (EFI_ERROR (Status)) {\r
DEBUG ((EFI_D_ERROR, "SetRootHubPortFeature EfiUsbPortReset Failed\n"));\r
return;\r
}\r
- \r
+\r
//\r
// Drive the reset signal for at least 50ms. Check USB 2.0 Spec\r
// section 7.1.7.5 for timing requirements.\r
//\r
MicroSecondDelay (USB_SET_ROOT_PORT_RESET_STALL);\r
- \r
+\r
//\r
// clear reset root port\r
//\r
PortNum,\r
EfiUsbPortReset\r
);\r
- \r
+\r
if (EFI_ERROR (Status)) {\r
DEBUG ((EFI_D_ERROR, "ClearRootHubPortFeature EfiUsbPortReset Failed\n"));\r
return;\r
}\r
- \r
+\r
MicroSecondDelay (USB_CLR_ROOT_PORT_RESET_STALL);\r
\r
//\r
UsbHcPpi,\r
PortNum,\r
&PortStatus\r
- ); \r
+ );\r
if (EFI_ERROR (Status)) {\r
return;\r
}\r
PortNum,\r
EfiUsbPortConnectChange\r
);\r
- \r
+\r
//\r
// Set port enable\r
//\r
PortNum,\r
EfiUsbPortEnable\r
);\r
- \r
+\r
UsbHcPpi->ClearRootHubPortFeature (\r
PeiServices,\r
UsbHcPpi,\r
PortNum,\r
EfiUsbPortEnableChange\r
);\r
- \r
+\r
MicroSecondDelay ((RetryIndex + 1) * 50 * 1000);\r
}\r
return;\r