Skip to content

Commit

Permalink
NetworkPkg/UefiPxeBcDxe: Mark the device as removed when it is unplugged
Browse files Browse the repository at this point in the history
Network stack fails when USB nic is removed after
initialization. This change marks it removed but
doesn't clean up memory allocations so that other
code holding onto and using that memory doesn't
cause an exception.

This change was split off the commit:
7a38833
  • Loading branch information
Bret Barkelew authored and Flickdm committed Jul 16, 2024
1 parent de0afe3 commit 6bdbfc3
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 36 deletions.
19 changes: 12 additions & 7 deletions NetworkPkg/UefiPxeBcDxe/PxeBcDhcp4.c
Original file line number Diff line number Diff line change
Expand Up @@ -1749,14 +1749,19 @@ PxeBcDhcp4Dora (
AsciiPrint ("\n");

ON_EXIT:
if (EFI_ERROR (Status)) {
Dhcp4->Stop (Dhcp4);
Dhcp4->Configure (Dhcp4, NULL);
} else {
ZeroMem (&Config, sizeof (EFI_DHCP4_CONFIG_DATA));
Dhcp4->Configure (Dhcp4, &Config);
Private->IsAddressOk = TRUE;
// MU_CHANGE [BEGIN] - 162958
if (!Private->DeviceDisconnected) {
if (EFI_ERROR (Status)) {
Dhcp4->Stop (Dhcp4);
Dhcp4->Configure (Dhcp4, NULL);
} else {
ZeroMem (&Config, sizeof (EFI_DHCP4_CONFIG_DATA));
Dhcp4->Configure (Dhcp4, &Config);
Private->IsAddressOk = TRUE;
}
}

// MU_CHANGE [END] - 162958

return Status;
}
6 changes: 4 additions & 2 deletions NetworkPkg/UefiPxeBcDxe/PxeBcDriver.c
Original file line number Diff line number Diff line change
Expand Up @@ -1515,7 +1515,8 @@ PxeBcStart (
}

if (FirstStart && (Private != NULL)) {
FreePool (Private);
// FreePool (Private); // MU_CHANGE - 162958
Private->DeviceDisconnected = TRUE; // MU_CHANGE - 162958
}

return Status;
Expand Down Expand Up @@ -1641,7 +1642,8 @@ PxeBcStop (
&gEfiCallerIdGuid,
&Private->Id
);
FreePool (Private);
// FreePool (Private); // MU_CHANGE - 162958
Private->DeviceDisconnected = TRUE; // MU_CHANGE - 162958
}

return EFI_SUCCESS;
Expand Down
57 changes: 38 additions & 19 deletions NetworkPkg/UefiPxeBcDxe/PxeBcImpl.c
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,15 @@ EfiPxeBcDhcp (
return EFI_INVALID_PARAMETER;
}

Status = EFI_SUCCESS;
Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
// MU_CHANGE [BEGIN] - 162958
Status = EFI_SUCCESS;
Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
if (Private->DeviceDisconnected) {
return EFI_DEVICE_ERROR;
}

// MU_CHANGE [END] - 162958

Mode = Private->PxeBc.Mode;
Mode->IcmpErrorReceived = FALSE;
Private->Function = EFI_PXE_BASE_CODE_FUNCTION_DHCP;
Expand Down Expand Up @@ -463,15 +470,20 @@ EfiPxeBcDhcp (
Status = PxeBcDhcp4Dora (Private, Private->Dhcp4);
}

//
// Reconfigure the UDP instance with the default configuration.
//
if (Mode->UsingIpv6) {
Private->Udp6Read->Configure (Private->Udp6Read, &Private->Udp6CfgData);
} else {
Private->Udp4Read->Configure (Private->Udp4Read, &Private->Udp4CfgData);
// MU_CHANGE [BEGIN] - 162958
if (!Private->DeviceDisconnected) {
//
// Reconfigure the UDP instance with the default configuration.
//
if (Mode->UsingIpv6) {
Private->Udp6Read->Configure (Private->Udp6Read, &Private->Udp6CfgData);
} else {
Private->Udp4Read->Configure (Private->Udp4Read, &Private->Udp4CfgData);
}
}

// MU_CHANGE [END] - 162958

//
// Dhcp(), Discover(), and Mtftp() set the IP filter, and return with the IP
// receive filter list emptied and the filter set to EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP.
Expand Down Expand Up @@ -1000,19 +1012,26 @@ EfiPxeBcMtftp (
break;
}

if (Status == EFI_ICMP_ERROR) {
Mode->IcmpErrorReceived = TRUE;
}

//
// Reconfigure the UDP instance with the default configuration.
//
if (Mode->UsingIpv6) {
Private->Udp6Read->Configure (Private->Udp6Read, &Private->Udp6CfgData);
// MU_CHANGE [BEGIN] - 162958
if (Private->DeviceDisconnected) {
Status = EFI_DEVICE_ERROR;
} else {
Private->Udp4Read->Configure (Private->Udp4Read, &Private->Udp4CfgData);
if (Status == EFI_ICMP_ERROR) {
Mode->IcmpErrorReceived = TRUE;
}

//
// Reconfigure the UDP instance with the default configuration.
//
if (Mode->UsingIpv6) {
Private->Udp6Read->Configure (Private->Udp6Read, &Private->Udp6CfgData);
} else {
Private->Udp4Read->Configure (Private->Udp4Read, &Private->Udp4CfgData);
}
}

// MU_CHANGE [END] - 162958

//
// Dhcp(), Discover(), and Mtftp() set the IP filter, and return with the IP
// receive filter list emptied and the filter set to EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP.
Expand Down
11 changes: 11 additions & 0 deletions NetworkPkg/UefiPxeBcDxe/PxeBcImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,17 @@ struct _PXEBC_PRIVATE_DATA {
UINT32 OfferNum;
UINT32 OfferCount[PxeOfferTypeMax];
UINT32 OfferIndex[PxeOfferTypeMax][PXEBC_OFFER_MAX_NUM];

// MU_CHANGE [BEGIN] - 162958
//
// The Network stack fails when a USB nic is removed
// after initialization. This change marks it removed
// but doesn't clean up memory allocations so that
// other code holding onto and using that memory
// doesn't cause an exception.
//
BOOLEAN DeviceDisconnected;
// MU_CHANGE [END] - 162958
};

extern EFI_PXE_BASE_CODE_PROTOCOL gPxeBcProtocolTemplate;
Expand Down
99 changes: 91 additions & 8 deletions NetworkPkg/UefiPxeBcDxe/PxeBcMtftp.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ PxeBcMtftp6CheckPacket (
EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL *Callback;
EFI_STATUS Status;

Private = (PXEBC_PRIVATE_DATA *)Token->Context;
// MU_CHANGE [BEGIN] - 162958
Private = (PXEBC_PRIVATE_DATA *)Token->Context;
if (Private->DeviceDisconnected) {
return EFI_DEVICE_ERROR;
}

// MU_CHANGE [END] - 162958
Callback = Private->PxeBcCallback;
Status = EFI_SUCCESS;

Expand Down Expand Up @@ -140,6 +146,12 @@ PxeBcMtftp6GetFileSize (
OptCnt = 1;
Config->InitialServerPort = PXEBC_BS_DOWNLOAD_PORT;

// MU_CHANGE [BEGIN] - 162958
if (Private->DeviceDisconnected) {
return EFI_DEVICE_ERROR;
}

// MU_CHANGE [END] - 162958
Status = Mtftp6->Configure (Mtftp6, Config);
if (EFI_ERROR (Status)) {
return Status;
Expand Down Expand Up @@ -274,6 +286,12 @@ PxeBcMtftp6ReadFile (
UINT8 WindowsizeBuf[10];
EFI_STATUS Status;

// MU_CHANGE [BEGIN] - 162958
if (Private->DeviceDisconnected) {
return EFI_DEVICE_ERROR;
}

// MU_CHANGE [END] - 162958
Status = EFI_DEVICE_ERROR;
Mtftp6 = Private->Mtftp6;
OptCnt = 0;
Expand Down Expand Up @@ -324,8 +342,12 @@ PxeBcMtftp6ReadFile (
//
*BufferSize = Token.BufferSize;

Mtftp6->Configure (Mtftp6, NULL);
// MU_CHANGE [BEGIN] - 162958 -- don't trust Mtftp6 after a surprise removal
if (!Private->DeviceDisconnected) {
Mtftp6->Configure (Mtftp6, NULL);
}

// MU_CHANGE [END] - 162958
return Status;
}

Expand Down Expand Up @@ -363,6 +385,12 @@ PxeBcMtftp6WriteFile (
UINT8 OptBuf[128];
EFI_STATUS Status;

// MU_CHANGE [BEGIN] - 162958
if (Private->DeviceDisconnected) {
return EFI_DEVICE_ERROR;
}

// MU_CHANGE [END] - 162958
Status = EFI_DEVICE_ERROR;
Mtftp6 = Private->Mtftp6;
OptCnt = 0;
Expand Down Expand Up @@ -398,7 +426,12 @@ PxeBcMtftp6WriteFile (
//
*BufferSize = Token.BufferSize;

Mtftp6->Configure (Mtftp6, NULL);
// MU_CHANGE [BEGIN] - 162958 -- don't trust Mtftp6 after a surprise removal
if (!Private->DeviceDisconnected) {
Mtftp6->Configure (Mtftp6, NULL);
}

// MU_CHANGE [END] - 162958

return Status;
}
Expand Down Expand Up @@ -489,8 +522,12 @@ PxeBcMtftp6ReadDirectory (
// Get the real size of received buffer.
//
*BufferSize = Token.BufferSize;
// MU_CHANGE [BEGIN] - 162958
if (!Private->DeviceDisconnected) {
Mtftp6->Configure (Mtftp6, NULL);
}

Mtftp6->Configure (Mtftp6, NULL);
// MU_CHANGE [END] - 162958

return Status;
}
Expand Down Expand Up @@ -526,7 +563,14 @@ PxeBcMtftp4CheckPacket (
EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL *Callback;
EFI_STATUS Status;

Private = (PXEBC_PRIVATE_DATA *)Token->Context;
Private = (PXEBC_PRIVATE_DATA *)Token->Context;

// MU_CHANGE [BEGIN] - 162958
if (Private->DeviceDisconnected) {
return EFI_DEVICE_ERROR;
}

// MU_CHANGE [END] - 162958
Callback = Private->PxeBcCallback;
Status = EFI_SUCCESS;

Expand Down Expand Up @@ -608,6 +652,13 @@ PxeBcMtftp4GetFileSize (
UINT32 OptCnt;
EFI_STATUS Status;

// MU_CHANGE [BEGIN] - 162958
if (Private->DeviceDisconnected) {
return EFI_DEVICE_ERROR;
}

// MU_CHANGE [END] - 162958

*BufferSize = 0;
Status = EFI_DEVICE_ERROR;
Mtftp4 = Private->Mtftp4;
Expand Down Expand Up @@ -752,6 +803,12 @@ PxeBcMtftp4ReadFile (
UINT8 WindowsizeBuf[10];
EFI_STATUS Status;

// MU_CHANGE [BEGIN] - 162958
if (Private->DeviceDisconnected) {
return EFI_DEVICE_ERROR;
}

// MU_CHANGE [END] - 162958
Status = EFI_DEVICE_ERROR;
Mtftp4 = Private->Mtftp4;
OptCnt = 0;
Expand Down Expand Up @@ -802,8 +859,12 @@ PxeBcMtftp4ReadFile (
//
*BufferSize = Token.BufferSize;

Mtftp4->Configure (Mtftp4, NULL);
// MU_CHANGE [BEGIN] - 162958 -- don't trust Mtftp6 after a surprise removal
if (!Private->DeviceDisconnected) {
Mtftp4->Configure (Mtftp4, NULL);
}

// MU_CHANGE [END] - 162958
return Status;
}

Expand Down Expand Up @@ -841,6 +902,12 @@ PxeBcMtftp4WriteFile (
UINT8 OptBuf[128];
EFI_STATUS Status;

// MU_CHANGE [BEGIN] - 162958
if (Private->DeviceDisconnected) {
return EFI_DEVICE_ERROR;
}

// MU_CHANGE [END] - 162958
Status = EFI_DEVICE_ERROR;
Mtftp4 = Private->Mtftp4;
OptCnt = 0;
Expand Down Expand Up @@ -876,7 +943,12 @@ PxeBcMtftp4WriteFile (
//
*BufferSize = Token.BufferSize;

Mtftp4->Configure (Mtftp4, NULL);
// MU_CHANGE [BEGIN] - 162958 -- don't trust Mtftp6 after a surprise removal
if (!Private->DeviceDisconnected) {
Mtftp4->Configure (Mtftp4, NULL);
}

// MU_CHANGE [END] - 162958

return Status;
}
Expand Down Expand Up @@ -918,6 +990,12 @@ PxeBcMtftp4ReadDirectory (
UINT8 WindowsizeBuf[10];
EFI_STATUS Status;

// MU_CHANGE [BEGIN] - 162958
if (Private->DeviceDisconnected) {
return EFI_DEVICE_ERROR;
}

// MU_CHANGE [END] - 162958
Status = EFI_DEVICE_ERROR;
Mtftp4 = Private->Mtftp4;
OptCnt = 0;
Expand Down Expand Up @@ -968,7 +1046,12 @@ PxeBcMtftp4ReadDirectory (
//
*BufferSize = Token.BufferSize;

Mtftp4->Configure (Mtftp4, NULL);
// MU_CHANGE [END] - 162958
if (!Private->DeviceDisconnected) {
Mtftp4->Configure (Mtftp4, NULL);
}

// MU_CHANGE [BEGIN] - 162958

return Status;
}
Expand Down

0 comments on commit 6bdbfc3

Please sign in to comment.