/* Check the validity of the device path of a ATA AHCI host controller. */
EFI_STATUS AhciIsHcDevicePathValid(IN EFI_DEVICE_PATH_PROTOCOL *DevicePath, IN UINTN DevicePathLength)
/* Check the validity of the device path of a ATA AHCI host controller. */
EFI_STATUS AhciIsHcDevicePathValid(IN EFI_DEVICE_PATH_PROTOCOL *DevicePath, IN UINTN DevicePathLength)
{
  EFI_DEVICE_PATH_PROTOCOL  *Start;
  UINTN                     Size;
  if (DevicePath == NULL) {
    return EFI_INVALID_PARAMETER;
  }
  if (DevicePathLength < sizeof (EFI_DEVICE_PATH_PROTOCOL)) {
    return EFI_INVALID_PARAMETER;
  }
  Start = DevicePath;
  while (!(DevicePath->Type == END_DEVICE_PATH_TYPE &&
           DevicePath->SubType == END_ENTIRE_DEVICE_PATH_SUBTYPE))
  {
    DevicePath = NextDevicePathNode (DevicePath);
    if ((UINTN)DevicePath <= (UINTN)Start) {
      return EFI_INVALID_PARAMETER;
    }
    if ((UINTN)DevicePath - (UINTN)Start >
        DevicePathLength - sizeof (EFI_DEVICE_PATH_PROTOCOL))
    {
      return EFI_INVALID_PARAMETER;
    }
  }
  Size = ((UINTN)DevicePath - (UINTN)Start) + sizeof (EFI_DEVICE_PATH_PROTOCOL);
  if (Size != DevicePathLength) {
    return EFI_INVALID_PARAMETER;
  }
  return EFI_SUCCESS;
}