/* Determine whether a given device path is valid. */
BOOLEAN IsDevicePathValid(CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath, UINTN MaxSize)
/* Determine whether a given device path is valid. */
BOOLEAN IsDevicePathValid(CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath, UINTN MaxSize)
{
  UINTN Count;
  UINTN Size;
  UINTN NodeLength;
  if (DevicePath == NULL || (MaxSize > 0 && MaxSize < END_DEVICE_PATH_LENGTH)) {
    return FALSE;
  }
  if (MaxSize == 0) {
    MaxSize = MAX_UINT32;
  }
  for (Count = 0, Size = 0; !IsDevicePathEnd (DevicePath); DevicePath = NextDevicePathNode (DevicePath)) {
    NodeLength = DevicePathNodeLength (DevicePath);
    if (NodeLength < sizeof (EFI_DEVICE_PATH_PROTOCOL)) {
      return FALSE;
    }
    if (NodeLength > MAX_UINT32 - Size) {
      return FALSE;
    }
    Size += NodeLength;
    if (Size > MaxSize - END_DEVICE_PATH_LENGTH ) {
      return FALSE;
    }
    Count++;
    if (Count >= MAX_DEVICE_PATH_NODE_COUNT) {
      return FALSE;
      }
  }
  return (BOOLEAN) (DevicePathNodeLength (DevicePath) == END_DEVICE_PATH_LENGTH);
}