X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=MdeModulePkg%2FLibrary%2FDxeNetLib%2FDxeNetLib.c;h=1743e9bdfa0dc28d28934da742e00f16a2ea0d36;hp=ffcd70ca844109d3171ef01b10e11d5d2bf4efdd;hb=501793fad4084c43828ba2f4e2d9355d29abe9f7;hpb=1204fe8319e5e6f77df68c375ef403e9ffa9227e diff --git a/MdeModulePkg/Library/DxeNetLib/DxeNetLib.c b/MdeModulePkg/Library/DxeNetLib/DxeNetLib.c index ffcd70ca84..1743e9bdfa 100644 --- a/MdeModulePkg/Library/DxeNetLib/DxeNetLib.c +++ b/MdeModulePkg/Library/DxeNetLib/DxeNetLib.c @@ -1,8 +1,8 @@ /** @file Network library. -Copyright (c) 2005 - 2009, Intel Corporation.
-All rights reserved. This program and the accompanying materials +Copyright (c) 2005 - 2011, Intel Corporation. All rights reserved.
+This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at http://opensource.org/licenses/bsd-license.php @@ -13,6 +13,8 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include +#include + #include #include #include @@ -20,8 +22,10 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include #include #include +#include #include +#include #include #include @@ -33,6 +37,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include #include #include +#include #define NIC_ITEM_CONFIG_SIZE sizeof (NIC_IP4_CONFIG_INFO) + sizeof (EFI_IP4_ROUTE_TABLE) * MAX_IP4_CONFIG_IN_VARIABLE @@ -111,6 +116,21 @@ GLOBAL_REMOVE_IF_UNREFERENCED CHAR8 *mMonthName[] = { "Dec" }; +// +// VLAN device path node template +// +GLOBAL_REMOVE_IF_UNREFERENCED VLAN_DEVICE_PATH mNetVlanDevicePathTemplate = { + { + MESSAGING_DEVICE_PATH, + MSG_VLAN_DP, + { + (UINT8) (sizeof (VLAN_DEVICE_PATH)), + (UINT8) ((sizeof (VLAN_DEVICE_PATH)) >> 8) + } + }, + 0 +}; + /** Locate the handles that support SNP, then open one of them to send the syslog packets. The caller isn't required to close @@ -360,6 +380,7 @@ SyslogBuildPacket ( // Pri = ((NET_SYSLOG_FACILITY & 31) << 3) | (Level & 7); gRT->GetTime (&Time, NULL); + ASSERT ((Time.Month <= 12) && (Time.Month >= 1)); // // Use %a to format the ASCII strings, %s to format UNICODE strings @@ -427,6 +448,7 @@ SyslogBuildPacket ( **/ CHAR8 * +EFIAPI NetDebugASPrint ( IN CHAR8 *Format, ... @@ -466,6 +488,7 @@ NetDebugASPrint ( than the mNetDebugLevelMax. Or, it has been sent out. **/ EFI_STATUS +EFIAPI NetDebugOutput ( IN UINT32 Level, IN UINT8 *Module, @@ -660,6 +683,7 @@ NetIp4IsUnicast ( **/ BOOLEAN +EFIAPI NetIp6IsValidUnicast ( IN EFI_IPv6_ADDRESS *Ip6 ) @@ -696,6 +720,7 @@ NetIp6IsValidUnicast ( **/ BOOLEAN +EFIAPI NetIp6IsUnspecifiedAddr ( IN EFI_IPv6_ADDRESS *Ip6 ) @@ -721,6 +746,7 @@ NetIp6IsUnspecifiedAddr ( **/ BOOLEAN +EFIAPI NetIp6IsLinkLocalAddr ( IN EFI_IPv6_ADDRESS *Ip6 ) @@ -758,6 +784,7 @@ NetIp6IsLinkLocalAddr ( **/ BOOLEAN +EFIAPI NetIp6IsNetEqual ( EFI_IPv6_ADDRESS *Ip1, EFI_IPv6_ADDRESS *Ip2, @@ -768,7 +795,7 @@ NetIp6IsNetEqual ( UINT8 Bit; UINT8 Mask; - ASSERT (Ip1 != NULL && Ip2 != NULL); + ASSERT ((Ip1 != NULL) && (Ip2 != NULL) && (PrefixLength < IP6_PREFIX_NUM)); if (PrefixLength == 0) { return TRUE; @@ -784,6 +811,7 @@ NetIp6IsNetEqual ( if (Bit > 0) { Mask = (UINT8) (0xFF << (8 - Bit)); + ASSERT (Byte < 16); if ((Ip1->Addr[Byte] & Mask) != (Ip2->Addr[Byte] & Mask)) { return FALSE; } @@ -806,6 +834,7 @@ NetIp6IsNetEqual ( **/ EFI_IPv6_ADDRESS * +EFIAPI Ip6Swap128 ( EFI_IPv6_ADDRESS *Ip6 ) @@ -884,7 +913,7 @@ NetGetUint32 ( byte stream. @param[in, out] Buf The buffer to put the UINT32. - @param[in] Data The data to put. + @param[in] Data The data to be converted and put into the byte stream. **/ VOID @@ -1515,7 +1544,7 @@ EFIAPI NetMapIterate ( IN NET_MAP *Map, IN NET_MAP_CALLBACK CallBack, - IN VOID *Arg + IN VOID *Arg OPTIONAL ) { @@ -1546,6 +1575,86 @@ NetMapIterate ( } +/** + Internal function to get the child handle of the NIC handle. + + @param[in] Controller NIC controller handle. + @param[out] ChildHandle Returned child handle. + + @retval EFI_SUCCESS Successfully to get child handle. + @retval Others Failed to get child handle. + +**/ +EFI_STATUS +NetGetChildHandle ( + IN EFI_HANDLE Controller, + OUT EFI_HANDLE *ChildHandle + ) +{ + EFI_STATUS Status; + EFI_HANDLE *Handles; + UINTN HandleCount; + UINTN Index; + EFI_DEVICE_PATH_PROTOCOL *ChildDeviceDevicePath; + VENDOR_DEVICE_PATH *VendorDeviceNode; + + // + // Locate all EFI Hii Config Access protocols + // + Status = gBS->LocateHandleBuffer ( + ByProtocol, + &gEfiHiiConfigAccessProtocolGuid, + NULL, + &HandleCount, + &Handles + ); + if (EFI_ERROR (Status) || (HandleCount == 0)) { + return Status; + } + + Status = EFI_NOT_FOUND; + + for (Index = 0; Index < HandleCount; Index++) { + + Status = EfiTestChildHandle (Controller, Handles[Index], &gEfiManagedNetworkServiceBindingProtocolGuid); + if (!EFI_ERROR (Status)) { + // + // Get device path on the child handle + // + Status = gBS->HandleProtocol ( + Handles[Index], + &gEfiDevicePathProtocolGuid, + (VOID **) &ChildDeviceDevicePath + ); + + if (!EFI_ERROR (Status)) { + while (!IsDevicePathEnd (ChildDeviceDevicePath)) { + ChildDeviceDevicePath = NextDevicePathNode (ChildDeviceDevicePath); + // + // Parse one instance + // + if (ChildDeviceDevicePath->Type == HARDWARE_DEVICE_PATH && + ChildDeviceDevicePath->SubType == HW_VENDOR_DP) { + VendorDeviceNode = (VENDOR_DEVICE_PATH *) ChildDeviceDevicePath; + if (CompareMem (&VendorDeviceNode->Guid, &gEfiNicIp4ConfigVariableGuid, sizeof (EFI_GUID)) == 0) { + // + // Found item matched gEfiNicIp4ConfigVariableGuid + // + *ChildHandle = Handles[Index]; + FreePool (Handles); + return EFI_SUCCESS; + } + } + } + } + } + } + + FreePool (Handles); + return Status; +} + + /** This is the default unload handle for all the network drivers. @@ -1772,18 +1881,291 @@ NetLibDestroyServiceChild ( return Status; } +/** + Get handle with Simple Network Protocol installed on it. + + There should be MNP Service Binding Protocol installed on the input ServiceHandle. + If Simple Network Protocol is already installed on the ServiceHandle, the + ServiceHandle will be returned. If SNP is not installed on the ServiceHandle, + try to find its parent handle with SNP installed. + + @param[in] ServiceHandle The handle where network service binding protocols are + installed on. + @param[out] Snp The pointer to store the address of the SNP instance. + This is an optional parameter that may be NULL. + + @return The SNP handle, or NULL if not found. + +**/ +EFI_HANDLE +EFIAPI +NetLibGetSnpHandle ( + IN EFI_HANDLE ServiceHandle, + OUT EFI_SIMPLE_NETWORK_PROTOCOL **Snp OPTIONAL + ) +{ + EFI_STATUS Status; + EFI_SIMPLE_NETWORK_PROTOCOL *SnpInstance; + EFI_DEVICE_PATH_PROTOCOL *DevicePath; + EFI_HANDLE SnpHandle; + + // + // Try to open SNP from ServiceHandle + // + SnpInstance = NULL; + Status = gBS->HandleProtocol (ServiceHandle, &gEfiSimpleNetworkProtocolGuid, (VOID **) &SnpInstance); + if (!EFI_ERROR (Status)) { + if (Snp != NULL) { + *Snp = SnpInstance; + } + return ServiceHandle; + } + + // + // Failed to open SNP, try to get SNP handle by LocateDevicePath() + // + DevicePath = DevicePathFromHandle (ServiceHandle); + if (DevicePath == NULL) { + return NULL; + } + + SnpHandle = NULL; + Status = gBS->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid, &DevicePath, &SnpHandle); + if (EFI_ERROR (Status)) { + // + // Failed to find SNP handle + // + return NULL; + } + + Status = gBS->HandleProtocol (SnpHandle, &gEfiSimpleNetworkProtocolGuid, (VOID **) &SnpInstance); + if (!EFI_ERROR (Status)) { + if (Snp != NULL) { + *Snp = SnpInstance; + } + return SnpHandle; + } + + return NULL; +} + +/** + Retrieve VLAN ID of a VLAN device handle. + + Search VLAN device path node in Device Path of specified ServiceHandle and + return its VLAN ID. If no VLAN device path node found, then this ServiceHandle + is not a VLAN device handle, and 0 will be returned. + + @param[in] ServiceHandle The handle where network service binding protocols are + installed on. + + @return VLAN ID of the device handle, or 0 if not a VLAN device. + +**/ +UINT16 +EFIAPI +NetLibGetVlanId ( + IN EFI_HANDLE ServiceHandle + ) +{ + EFI_DEVICE_PATH_PROTOCOL *DevicePath; + EFI_DEVICE_PATH_PROTOCOL *Node; + + DevicePath = DevicePathFromHandle (ServiceHandle); + if (DevicePath == NULL) { + return 0; + } + + Node = DevicePath; + while (!IsDevicePathEnd (Node)) { + if (Node->Type == MESSAGING_DEVICE_PATH && Node->SubType == MSG_VLAN_DP) { + return ((VLAN_DEVICE_PATH *) Node)->VlanId; + } + Node = NextDevicePathNode (Node); + } + + return 0; +} + +/** + Find VLAN device handle with specified VLAN ID. + + The VLAN child device handle is created by VLAN Config Protocol on ControllerHandle. + This function will append VLAN device path node to the parent device path, + and then use LocateDevicePath() to find the correct VLAN device handle. + + @param[in] ControllerHandle The handle where network service binding protocols are + installed on. + @param[in] VlanId The configured VLAN ID for the VLAN device. + + @return The VLAN device handle, or NULL if not found. + +**/ +EFI_HANDLE +EFIAPI +NetLibGetVlanHandle ( + IN EFI_HANDLE ControllerHandle, + IN UINT16 VlanId + ) +{ + EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath; + EFI_DEVICE_PATH_PROTOCOL *VlanDevicePath; + EFI_DEVICE_PATH_PROTOCOL *DevicePath; + VLAN_DEVICE_PATH VlanNode; + EFI_HANDLE Handle; + + ParentDevicePath = DevicePathFromHandle (ControllerHandle); + if (ParentDevicePath == NULL) { + return NULL; + } + + // + // Construct VLAN device path + // + CopyMem (&VlanNode, &mNetVlanDevicePathTemplate, sizeof (VLAN_DEVICE_PATH)); + VlanNode.VlanId = VlanId; + VlanDevicePath = AppendDevicePathNode ( + ParentDevicePath, + (EFI_DEVICE_PATH_PROTOCOL *) &VlanNode + ); + if (VlanDevicePath == NULL) { + return NULL; + } + + // + // Find VLAN device handle + // + Handle = NULL; + DevicePath = VlanDevicePath; + gBS->LocateDevicePath ( + &gEfiDevicePathProtocolGuid, + &DevicePath, + &Handle + ); + if (!IsDevicePathEnd (DevicePath)) { + // + // Device path is not exactly match + // + Handle = NULL; + } + + FreePool (VlanDevicePath); + return Handle; +} /** - Convert the mac address of the simple network protocol installed on - SnpHandle to a unicode string. Callers are responsible for freeing the - string storage. + Get MAC address associated with the network service handle. + + There should be MNP Service Binding Protocol installed on the input ServiceHandle. + If SNP is installed on the ServiceHandle or its parent handle, MAC address will + be retrieved from SNP. If no SNP found, try to get SNP mode data use MNP. + + @param[in] ServiceHandle The handle where network service binding protocols are + installed on. + @param[out] MacAddress The pointer to store the returned MAC address. + @param[out] AddressSize The length of returned MAC address. + + @retval EFI_SUCCESS MAC address is returned successfully. + @retval Others Failed to get SNP mode data. + +**/ +EFI_STATUS +EFIAPI +NetLibGetMacAddress ( + IN EFI_HANDLE ServiceHandle, + OUT EFI_MAC_ADDRESS *MacAddress, + OUT UINTN *AddressSize + ) +{ + EFI_STATUS Status; + EFI_SIMPLE_NETWORK_PROTOCOL *Snp; + EFI_SIMPLE_NETWORK_MODE *SnpMode; + EFI_SIMPLE_NETWORK_MODE SnpModeData; + EFI_MANAGED_NETWORK_PROTOCOL *Mnp; + EFI_SERVICE_BINDING_PROTOCOL *MnpSb; + EFI_HANDLE *SnpHandle; + EFI_HANDLE MnpChildHandle; + + ASSERT (MacAddress != NULL); + ASSERT (AddressSize != NULL); + + // + // Try to get SNP handle + // + Snp = NULL; + SnpHandle = NetLibGetSnpHandle (ServiceHandle, &Snp); + if (SnpHandle != NULL) { + // + // SNP found, use it directly + // + SnpMode = Snp->Mode; + } else { + // + // Failed to get SNP handle, try to get MAC address from MNP + // + MnpChildHandle = NULL; + Status = gBS->HandleProtocol ( + ServiceHandle, + &gEfiManagedNetworkServiceBindingProtocolGuid, + (VOID **) &MnpSb + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Create a MNP child + // + Status = MnpSb->CreateChild (MnpSb, &MnpChildHandle); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Open MNP protocol + // + Status = gBS->HandleProtocol ( + MnpChildHandle, + &gEfiManagedNetworkProtocolGuid, + (VOID **) &Mnp + ); + if (EFI_ERROR (Status)) { + MnpSb->DestroyChild (MnpSb, MnpChildHandle); + return Status; + } - Get the mac address of the Simple Network protocol from the SnpHandle. Then convert - the mac address into a unicode string. It takes 2 unicode characters to represent - a 1 byte binary buffer. Plus one unicode character for the null-terminator. + // + // Try to get SNP mode from MNP + // + Status = Mnp->GetModeData (Mnp, NULL, &SnpModeData); + if (EFI_ERROR (Status)) { + MnpSb->DestroyChild (MnpSb, MnpChildHandle); + return Status; + } + SnpMode = &SnpModeData; + + // + // Destroy the MNP child + // + MnpSb->DestroyChild (MnpSb, MnpChildHandle); + } + *AddressSize = SnpMode->HwAddressSize; + CopyMem (MacAddress->Addr, SnpMode->CurrentAddress.Addr, SnpMode->HwAddressSize); - @param[in] SnpHandle The handle where the simple network protocol is + return EFI_SUCCESS; +} + +/** + Convert MAC address of the NIC associated with specified Service Binding Handle + to a unicode string. Callers are responsible for freeing the string storage. + + Locate simple network protocol associated with the Service Binding Handle and + get the mac address from SNP. Then convert the mac address into a unicode + string. It takes 2 unicode characters to represent a 1 byte binary buffer. + Plus one unicode character for the null-terminator. + + @param[in] ServiceHandle The handle where network service binding protocol is installed on. @param[in] ImageHandle The image handle used to act as the agent handle to get the simple network protocol. @@ -1798,144 +2180,362 @@ NetLibDestroyServiceChild ( EFI_STATUS EFIAPI NetLibGetMacString ( - IN EFI_HANDLE SnpHandle, + IN EFI_HANDLE ServiceHandle, IN EFI_HANDLE ImageHandle, OUT CHAR16 **MacString ) { EFI_STATUS Status; - EFI_SIMPLE_NETWORK_PROTOCOL *Snp; - EFI_SIMPLE_NETWORK_MODE *Mode; - CHAR16 *MacAddress; + EFI_MAC_ADDRESS MacAddress; UINT8 *HwAddress; + UINTN HwAddressSize; + UINT16 VlanId; + CHAR16 *String; UINTN Index; - *MacString = NULL; + ASSERT (MacString != NULL); // - // Get the Simple Network protocol from the SnpHandle. + // Get MAC address of the network device // - Status = gBS->OpenProtocol ( - SnpHandle, - &gEfiSimpleNetworkProtocolGuid, - (VOID **) &Snp, - ImageHandle, - SnpHandle, - EFI_OPEN_PROTOCOL_GET_PROTOCOL - ); + Status = NetLibGetMacAddress (ServiceHandle, &MacAddress, &HwAddressSize); if (EFI_ERROR (Status)) { return Status; } - Mode = Snp->Mode; - // // It takes 2 unicode characters to represent a 1 byte binary buffer. + // If VLAN is configured, it will need extra 5 characters like "\0005". // Plus one unicode character for the null-terminator. // - MacAddress = AllocatePool ((2 * Mode->HwAddressSize + 1) * sizeof (CHAR16)); - if (MacAddress == NULL) { + String = AllocateZeroPool ((2 * HwAddressSize + 5 + 1) * sizeof (CHAR16)); + if (String == NULL) { return EFI_OUT_OF_RESOURCES; } - *MacString = MacAddress; + *MacString = String; // - // Convert the mac address into a unicode string. + // Convert the MAC address into a unicode string. // - HwAddress = Mode->CurrentAddress.Addr; - for (Index = 0; Index < Mode->HwAddressSize; Index++) { - MacAddress += UnicodeValueToString (MacAddress, PREFIX_ZERO | RADIX_HEX, *(HwAddress++), 2); + HwAddress = &MacAddress.Addr[0]; + for (Index = 0; Index < HwAddressSize; Index++) { + String += UnicodeValueToString (String, PREFIX_ZERO | RADIX_HEX, *(HwAddress++), 2); } - MacAddress[Mode->HwAddressSize * 2] = L'\0'; + // + // Append VLAN ID if any + // + VlanId = NetLibGetVlanId (ServiceHandle); + if (VlanId != 0) { + *String++ = L'\\'; + String += UnicodeValueToString (String, PREFIX_ZERO | RADIX_HEX, VlanId, 4); + } + // + // Null terminate the Unicode string + // + *String = L'\0'; return EFI_SUCCESS; } /** - Check the default address used by the IPv4 driver is static or dynamic (acquired - from DHCP). - - If the controller handle does not have the NIC Ip4 Config Protocol installed, the - default address is static. If the EFI variable to save the configuration is not found, - the default address is static. Otherwise, get the result from the EFI variable which - saving the configuration. - - @param[in] Controller The controller handle which has the NIC Ip4 Config Protocol - relative with the default address to judge. - - @retval TRUE If the default address is static. - @retval FALSE If the default address is acquired from DHCP. + Detect media status for specified network device. + + The underlying UNDI driver may or may not support reporting media status from + GET_STATUS command (PXE_STATFLAGS_GET_STATUS_NO_MEDIA_SUPPORTED). This routine + will try to invoke Snp->GetStatus() to get the media status: if media already + present, it return directly; if media not present, it will stop SNP and then + restart SNP to get the latest media status, this give chance to get the correct + media status for old UNDI driver which doesn't support reporting media status + from GET_STATUS command. + Note: there will be two limitations for current algorithm: + 1) for UNDI with this capability, in case of cable is not attached, there will + be an redundant Stop/Start() process; + 2) for UNDI without this capability, in case that network cable is attached when + Snp->Initialize() is invoked while network cable is unattached later, + NetLibDetectMedia() will report MediaPresent as TRUE, causing upper layer + apps to wait for timeout time. + + @param[in] ServiceHandle The handle where network service binding protocols are + installed on. + @param[out] MediaPresent The pointer to store the media status. + + @retval EFI_SUCCESS Media detection success. + @retval EFI_INVALID_PARAMETER ServiceHandle is not valid network device handle. + @retval EFI_UNSUPPORTED Network device does not support media detection. + @retval EFI_DEVICE_ERROR SNP is in unknown state. **/ -BOOLEAN -NetLibDefaultAddressIsStatic ( - IN EFI_HANDLE Controller +EFI_STATUS +EFIAPI +NetLibDetectMedia ( + IN EFI_HANDLE ServiceHandle, + OUT BOOLEAN *MediaPresent ) { - EFI_STATUS Status; - EFI_HII_CONFIG_ROUTING_PROTOCOL *HiiConfigRouting; - UINTN Len; - NIC_IP4_CONFIG_INFO *ConfigInfo; - BOOLEAN IsStatic; - EFI_STRING ConfigHdr; - EFI_STRING ConfigResp; - EFI_STRING AccessProgress; - EFI_STRING AccessResults; - EFI_STRING String; - - ConfigInfo = NULL; - ConfigHdr = NULL; - ConfigResp = NULL; - AccessProgress = NULL; - AccessResults = NULL; - IsStatic = TRUE; + EFI_STATUS Status; + EFI_HANDLE SnpHandle; + EFI_SIMPLE_NETWORK_PROTOCOL *Snp; + UINT32 InterruptStatus; + UINT32 OldState; + EFI_MAC_ADDRESS *MCastFilter; + UINT32 MCastFilterCount; + UINT32 EnableFilterBits; + UINT32 DisableFilterBits; + BOOLEAN ResetMCastFilters; - Status = gBS->LocateProtocol ( - &gEfiHiiConfigRoutingProtocolGuid, - NULL, - (VOID **) &HiiConfigRouting - ); - if (EFI_ERROR (Status)) { - return TRUE; - } + ASSERT (MediaPresent != NULL); // - // Construct config request string header + // Get SNP handle // - ConfigHdr = HiiConstructConfigHdr (&gEfiNicIp4ConfigVariableGuid, EFI_NIC_IP4_CONFIG_VARIABLE, Controller); - if (ConfigHdr == NULL) { - return TRUE; + Snp = NULL; + SnpHandle = NetLibGetSnpHandle (ServiceHandle, &Snp); + if (SnpHandle == NULL) { + return EFI_INVALID_PARAMETER; } - Len = StrLen (ConfigHdr); - ConfigResp = AllocateZeroPool ((Len + NIC_ITEM_CONFIG_SIZE * 2 + 100) * sizeof (CHAR16)); - if (ConfigResp == NULL) { - goto ON_EXIT; + // + // Check whether SNP support media detection + // + if (!Snp->Mode->MediaPresentSupported) { + return EFI_UNSUPPORTED; } - StrCpy (ConfigResp, ConfigHdr); - - String = ConfigResp + Len; - UnicodeSPrint ( - String, - (8 + 4 + 7 + 4 + 1) * sizeof (CHAR16), - L"&OFFSET=%04X&WIDTH=%04X", - OFFSET_OF (NIC_IP4_CONFIG_INFO, Source), - sizeof (UINT32) - ); - Status = HiiConfigRouting->ExtractConfig ( - HiiConfigRouting, - ConfigResp, - &AccessProgress, - &AccessResults - ); + // + // Invoke Snp->GetStatus() to refresh MediaPresent field in SNP mode data + // + Status = Snp->GetStatus (Snp, &InterruptStatus, NULL); if (EFI_ERROR (Status)) { - goto ON_EXIT; + return Status; } - ConfigInfo = AllocateZeroPool (sizeof (NIC_ITEM_CONFIG_SIZE)); + if (Snp->Mode->MediaPresent) { + // + // Media is present, return directly + // + *MediaPresent = TRUE; + return EFI_SUCCESS; + } + + // + // Till now, GetStatus() report no media; while, in case UNDI not support + // reporting media status from GetStatus(), this media status may be incorrect. + // So, we will stop SNP and then restart it to get the correct media status. + // + OldState = Snp->Mode->State; + if (OldState >= EfiSimpleNetworkMaxState) { + return EFI_DEVICE_ERROR; + } + + MCastFilter = NULL; + + if (OldState == EfiSimpleNetworkInitialized) { + // + // SNP is already in use, need Shutdown/Stop and then Start/Initialize + // + + // + // Backup current SNP receive filter settings + // + EnableFilterBits = Snp->Mode->ReceiveFilterSetting; + DisableFilterBits = Snp->Mode->ReceiveFilterMask ^ EnableFilterBits; + + ResetMCastFilters = TRUE; + MCastFilterCount = Snp->Mode->MCastFilterCount; + if (MCastFilterCount != 0) { + MCastFilter = AllocateCopyPool ( + MCastFilterCount * sizeof (EFI_MAC_ADDRESS), + Snp->Mode->MCastFilter + ); + ASSERT (MCastFilter != NULL); + + ResetMCastFilters = FALSE; + } + + // + // Shutdown/Stop the simple network + // + Status = Snp->Shutdown (Snp); + if (!EFI_ERROR (Status)) { + Status = Snp->Stop (Snp); + } + if (EFI_ERROR (Status)) { + goto Exit; + } + + // + // Start/Initialize the simple network + // + Status = Snp->Start (Snp); + if (!EFI_ERROR (Status)) { + Status = Snp->Initialize (Snp, 0, 0); + } + if (EFI_ERROR (Status)) { + goto Exit; + } + + // + // Here we get the correct media status + // + *MediaPresent = Snp->Mode->MediaPresent; + + // + // Restore SNP receive filter settings + // + Status = Snp->ReceiveFilters ( + Snp, + EnableFilterBits, + DisableFilterBits, + ResetMCastFilters, + MCastFilterCount, + MCastFilter + ); + + if (MCastFilter != NULL) { + FreePool (MCastFilter); + } + + return Status; + } + + // + // SNP is not in use, it's in state of EfiSimpleNetworkStopped or EfiSimpleNetworkStarted + // + if (OldState == EfiSimpleNetworkStopped) { + // + // SNP not start yet, start it + // + Status = Snp->Start (Snp); + if (EFI_ERROR (Status)) { + goto Exit; + } + } + + // + // Initialize the simple network + // + Status = Snp->Initialize (Snp, 0, 0); + if (EFI_ERROR (Status)) { + Status = EFI_DEVICE_ERROR; + goto Exit; + } + + // + // Here we get the correct media status + // + *MediaPresent = Snp->Mode->MediaPresent; + + // + // Shut down the simple network + // + Snp->Shutdown (Snp); + +Exit: + if (OldState == EfiSimpleNetworkStopped) { + // + // Original SNP sate is Stopped, restore to original state + // + Snp->Stop (Snp); + } + + if (MCastFilter != NULL) { + FreePool (MCastFilter); + } + + return Status; +} + +/** + Check the default address used by the IPv4 driver is static or dynamic (acquired + from DHCP). + + If the controller handle does not have the NIC Ip4 Config Protocol installed, the + default address is static. If the EFI variable to save the configuration is not found, + the default address is static. Otherwise, get the result from the EFI variable which + saving the configuration. + + @param[in] Controller The controller handle which has the NIC Ip4 Config Protocol + relative with the default address to judge. + + @retval TRUE If the default address is static. + @retval FALSE If the default address is acquired from DHCP. + +**/ +BOOLEAN +NetLibDefaultAddressIsStatic ( + IN EFI_HANDLE Controller + ) +{ + EFI_STATUS Status; + EFI_HII_CONFIG_ROUTING_PROTOCOL *HiiConfigRouting; + UINTN Len; + NIC_IP4_CONFIG_INFO *ConfigInfo; + BOOLEAN IsStatic; + EFI_STRING ConfigHdr; + EFI_STRING ConfigResp; + EFI_STRING AccessProgress; + EFI_STRING AccessResults; + EFI_STRING String; + EFI_HANDLE ChildHandle; + + ConfigInfo = NULL; + ConfigHdr = NULL; + ConfigResp = NULL; + AccessProgress = NULL; + AccessResults = NULL; + IsStatic = TRUE; + + Status = gBS->LocateProtocol ( + &gEfiHiiConfigRoutingProtocolGuid, + NULL, + (VOID **) &HiiConfigRouting + ); + if (EFI_ERROR (Status)) { + return TRUE; + } + + Status = NetGetChildHandle (Controller, &ChildHandle); + if (EFI_ERROR (Status)) { + return TRUE; + } + + // + // Construct config request string header + // + ConfigHdr = HiiConstructConfigHdr (&gEfiNicIp4ConfigVariableGuid, EFI_NIC_IP4_CONFIG_VARIABLE, ChildHandle); + if (ConfigHdr == NULL) { + return TRUE; + } + + Len = StrLen (ConfigHdr); + ConfigResp = AllocateZeroPool ((Len + NIC_ITEM_CONFIG_SIZE * 2 + 100) * sizeof (CHAR16)); + if (ConfigResp == NULL) { + goto ON_EXIT; + } + StrCpy (ConfigResp, ConfigHdr); + + String = ConfigResp + Len; + UnicodeSPrint ( + String, + (8 + 4 + 7 + 4 + 1) * sizeof (CHAR16), + L"&OFFSET=%04X&WIDTH=%04X", + OFFSET_OF (NIC_IP4_CONFIG_INFO, Source), + sizeof (UINT32) + ); + + Status = HiiConfigRouting->ExtractConfig ( + HiiConfigRouting, + ConfigResp, + &AccessProgress, + &AccessResults + ); + if (EFI_ERROR (Status)) { + goto ON_EXIT; + } + + ConfigInfo = AllocateZeroPool (NIC_ITEM_CONFIG_SIZE); if (ConfigInfo == NULL) { goto ON_EXIT; } @@ -1978,7 +2578,6 @@ ON_EXIT: The header type of IPv4 device path node is MESSAGING_DEVICE_PATH. The header subtype of IPv4 device path node is MSG_IPv4_DP. - The length of the IPv4 device path node in bytes is 19. Get other info from parameters to make up the whole IPv4 device path node. @param[in, out] Node Pointer to the IPv4 device path node. @@ -2006,7 +2605,7 @@ NetLibCreateIPv4DPathNode ( { Node->Header.Type = MESSAGING_DEVICE_PATH; Node->Header.SubType = MSG_IPv4_DP; - SetDevicePathNodeLength (&Node->Header, 19); + SetDevicePathNodeLength (&Node->Header, sizeof (IPv4_DEVICE_PATH)); CopyMem (&Node->LocalIpAddress, &LocalIp, sizeof (EFI_IPv4_ADDRESS)); CopyMem (&Node->RemoteIpAddress, &RemoteIp, sizeof (EFI_IPv4_ADDRESS)); @@ -2021,6 +2620,14 @@ NetLibCreateIPv4DPathNode ( } else { Node->StaticIpAddress = NetLibDefaultAddressIsStatic (Controller); } + + // + // Set the Gateway IP address to default value 0:0:0:0. + // Set the Subnet mask to default value 255:255:255:0. + // + ZeroMem (&Node->GatewayIpAddress, sizeof (EFI_IPv4_ADDRESS)); + SetMem (&Node->SubnetMask, sizeof (EFI_IPv4_ADDRESS), 0xff); + Node->SubnetMask.Addr[3] = 0; } /** @@ -2062,7 +2669,14 @@ NetLibCreateIPv6DPathNode ( Node->RemotePort = RemotePort; Node->Protocol = Protocol; - Node->StaticIpAddress = FALSE; + + // + // Set default value to IPAddressOrigin, PrefixLength. + // Set the Gateway IP address to unspecified address. + // + Node->IpAddressOrigin = 0; + Node->PrefixLength = IP6_PREFIX_LENGTH; + ZeroMem (&Node->GatewayIpAddress, sizeof (EFI_IPv6_ADDRESS)); } /** @@ -2109,7 +2723,7 @@ NetLibGetNicHandle ( Handle = NULL; for (Index = 0; Index < OpenCount; Index++) { - if (OpenBuffer[Index].Attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) { + if ((OpenBuffer[Index].Attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) != 0) { Handle = OpenBuffer[Index].ControllerHandle; break; } @@ -2118,3 +2732,528 @@ NetLibGetNicHandle ( gBS->FreePool (OpenBuffer); return Handle; } + +/** + Convert one Null-terminated ASCII string (decimal dotted) to EFI_IPv4_ADDRESS. + + @param[in] String The pointer to the Ascii string. + @param[out] Ip4Address The pointer to the converted IPv4 address. + + @retval EFI_SUCCESS Convert to IPv4 address successfully. + @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip4Address is NULL. + +**/ +EFI_STATUS +EFIAPI +NetLibAsciiStrToIp4 ( + IN CONST CHAR8 *String, + OUT EFI_IPv4_ADDRESS *Ip4Address + ) +{ + UINT8 Index; + CHAR8 *Ip4Str; + CHAR8 *TempStr; + UINTN NodeVal; + + if ((String == NULL) || (Ip4Address == NULL)) { + return EFI_INVALID_PARAMETER; + } + + Ip4Str = (CHAR8 *) String; + + for (Index = 0; Index < 4; Index++) { + TempStr = Ip4Str; + + while ((*Ip4Str != '\0') && (*Ip4Str != '.')) { + Ip4Str++; + } + + // + // The IPv4 address is X.X.X.X + // + if (*Ip4Str == '.') { + if (Index == 3) { + return EFI_INVALID_PARAMETER; + } + } else { + if (Index != 3) { + return EFI_INVALID_PARAMETER; + } + } + + // + // Convert the string to IPv4 address. AsciiStrDecimalToUintn stops at the + // first character that is not a valid decimal character, '.' or '\0' here. + // + NodeVal = AsciiStrDecimalToUintn (TempStr); + if (NodeVal > 0xFF) { + return EFI_INVALID_PARAMETER; + } + + Ip4Address->Addr[Index] = (UINT8) NodeVal; + + Ip4Str++; + } + + return EFI_SUCCESS; +} + + +/** + Convert one Null-terminated ASCII string to EFI_IPv6_ADDRESS. The format of the + string is defined in RFC 4291 - Text Pepresentation of Addresses. + + @param[in] String The pointer to the Ascii string. + @param[out] Ip6Address The pointer to the converted IPv6 address. + + @retval EFI_SUCCESS Convert to IPv6 address successfully. + @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip6Address is NULL. + +**/ +EFI_STATUS +EFIAPI +NetLibAsciiStrToIp6 ( + IN CONST CHAR8 *String, + OUT EFI_IPv6_ADDRESS *Ip6Address + ) +{ + UINT8 Index; + CHAR8 *Ip6Str; + CHAR8 *TempStr; + CHAR8 *TempStr2; + UINT8 NodeCnt; + UINT8 TailNodeCnt; + UINT8 AllowedCnt; + UINTN NodeVal; + BOOLEAN Short; + BOOLEAN Update; + BOOLEAN LeadZero; + UINT8 LeadZeroCnt; + UINT8 Cnt; + + if ((String == NULL) || (Ip6Address == NULL)) { + return EFI_INVALID_PARAMETER; + } + + Ip6Str = (CHAR8 *) String; + AllowedCnt = 6; + LeadZeroCnt = 0; + + // + // An IPv6 address leading with : looks strange. + // + if (*Ip6Str == ':') { + if (*(Ip6Str + 1) != ':') { + return EFI_INVALID_PARAMETER; + } else { + AllowedCnt = 7; + } + } + + ZeroMem (Ip6Address, sizeof (EFI_IPv6_ADDRESS)); + + NodeCnt = 0; + TailNodeCnt = 0; + Short = FALSE; + Update = FALSE; + LeadZero = FALSE; + + for (Index = 0; Index < 15; Index = (UINT8) (Index + 2)) { + TempStr = Ip6Str; + + while ((*Ip6Str != '\0') && (*Ip6Str != ':')) { + Ip6Str++; + } + + if ((*Ip6Str == '\0') && (Index != 14)) { + return EFI_INVALID_PARAMETER; + } + + if (*Ip6Str == ':') { + if (*(Ip6Str + 1) == ':') { + if ((NodeCnt > 6) || + ((*(Ip6Str + 2) != '\0') && (AsciiStrHexToUintn (Ip6Str + 2) == 0))) { + // + // ::0 looks strange. report error to user. + // + return EFI_INVALID_PARAMETER; + } + if ((NodeCnt == 6) && (*(Ip6Str + 2) != '\0') && + (AsciiStrHexToUintn (Ip6Str + 2) != 0)) { + return EFI_INVALID_PARAMETER; + } + + // + // Skip the abbreviation part of IPv6 address. + // + TempStr2 = Ip6Str + 2; + while ((*TempStr2 != '\0')) { + if (*TempStr2 == ':') { + if (*(TempStr2 + 1) == ':') { + // + // :: can only appear once in IPv6 address. + // + return EFI_INVALID_PARAMETER; + } + + TailNodeCnt++; + if (TailNodeCnt >= (AllowedCnt - NodeCnt)) { + // + // :: indicates one or more groups of 16 bits of zeros. + // + return EFI_INVALID_PARAMETER; + } + } + + TempStr2++; + } + + Short = TRUE; + Update = TRUE; + + Ip6Str = Ip6Str + 2; + } else { + if (*(Ip6Str + 1) == '\0') { + return EFI_INVALID_PARAMETER; + } + Ip6Str++; + NodeCnt++; + if ((Short && (NodeCnt > 6)) || (!Short && (NodeCnt > 7))) { + // + // There are more than 8 groups of 16 bits of zeros. + // + return EFI_INVALID_PARAMETER; + } + } + } + + // + // Convert the string to IPv6 address. AsciiStrHexToUintn stops at the first + // character that is not a valid hexadecimal character, ':' or '\0' here. + // + NodeVal = AsciiStrHexToUintn (TempStr); + if ((NodeVal > 0xFFFF) || (Index > 14)) { + return EFI_INVALID_PARAMETER; + } + if (NodeVal != 0) { + if ((*TempStr == '0') && + ((*(TempStr + 2) == ':') || (*(TempStr + 3) == ':') || + (*(TempStr + 2) == '\0') || (*(TempStr + 3) == '\0'))) { + return EFI_INVALID_PARAMETER; + } + if ((*TempStr == '0') && (*(TempStr + 4) != '\0') && + (*(TempStr + 4) != ':')) { + return EFI_INVALID_PARAMETER; + } + } else { + if (((*TempStr == '0') && (*(TempStr + 1) == '0') && + ((*(TempStr + 2) == ':') || (*(TempStr + 2) == '\0'))) || + ((*TempStr == '0') && (*(TempStr + 1) == '0') && (*(TempStr + 2) == '0') && + ((*(TempStr + 3) == ':') || (*(TempStr + 3) == '\0')))) { + return EFI_INVALID_PARAMETER; + } + } + + Cnt = 0; + while ((TempStr[Cnt] != ':') && (TempStr[Cnt] != '\0')) { + Cnt++; + } + if (LeadZeroCnt == 0) { + if ((Cnt == 4) && (*TempStr == '0')) { + LeadZero = TRUE; + LeadZeroCnt++; + } + if ((Cnt != 0) && (Cnt < 4)) { + LeadZero = FALSE; + LeadZeroCnt++; + } + } else { + if ((Cnt == 4) && (*TempStr == '0') && !LeadZero) { + return EFI_INVALID_PARAMETER; + } + if ((Cnt != 0) && (Cnt < 4) && LeadZero) { + return EFI_INVALID_PARAMETER; + } + } + + Ip6Address->Addr[Index] = (UINT8) (NodeVal >> 8); + Ip6Address->Addr[Index + 1] = (UINT8) (NodeVal & 0xFF); + + // + // Skip the groups of zeros by :: + // + if (Short && Update) { + Index = (UINT8) (16 - (TailNodeCnt + 2) * 2); + Update = FALSE; + } + } + + if ((!Short && Index != 16) || (*Ip6Str != '\0')) { + return EFI_INVALID_PARAMETER; + } + + return EFI_SUCCESS; +} + + +/** + Convert one Null-terminated Unicode string (decimal dotted) to EFI_IPv4_ADDRESS. + + @param[in] String The pointer to the Ascii string. + @param[out] Ip4Address The pointer to the converted IPv4 address. + + @retval EFI_SUCCESS Convert to IPv4 address successfully. + @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip4Address is NULL. + @retval EFI_OUT_OF_RESOURCES Fail to perform the operation due to lack of resource. + +**/ +EFI_STATUS +EFIAPI +NetLibStrToIp4 ( + IN CONST CHAR16 *String, + OUT EFI_IPv4_ADDRESS *Ip4Address + ) +{ + CHAR8 *Ip4Str; + EFI_STATUS Status; + + if ((String == NULL) || (Ip4Address == NULL)) { + return EFI_INVALID_PARAMETER; + } + + Ip4Str = (CHAR8 *) AllocatePool ((StrLen (String) + 1) * sizeof (CHAR8)); + if (Ip4Str == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + UnicodeStrToAsciiStr (String, Ip4Str); + + Status = NetLibAsciiStrToIp4 (Ip4Str, Ip4Address); + + FreePool (Ip4Str); + + return Status; +} + + +/** + Convert one Null-terminated Unicode string to EFI_IPv6_ADDRESS. The format of + the string is defined in RFC 4291 - Text Pepresentation of Addresses. + + @param[in] String The pointer to the Ascii string. + @param[out] Ip6Address The pointer to the converted IPv6 address. + + @retval EFI_SUCCESS Convert to IPv6 address successfully. + @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip6Address is NULL. + @retval EFI_OUT_OF_RESOURCES Fail to perform the operation due to lack of resource. + +**/ +EFI_STATUS +EFIAPI +NetLibStrToIp6 ( + IN CONST CHAR16 *String, + OUT EFI_IPv6_ADDRESS *Ip6Address + ) +{ + CHAR8 *Ip6Str; + EFI_STATUS Status; + + if ((String == NULL) || (Ip6Address == NULL)) { + return EFI_INVALID_PARAMETER; + } + + Ip6Str = (CHAR8 *) AllocatePool ((StrLen (String) + 1) * sizeof (CHAR8)); + if (Ip6Str == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + UnicodeStrToAsciiStr (String, Ip6Str); + + Status = NetLibAsciiStrToIp6 (Ip6Str, Ip6Address); + + FreePool (Ip6Str); + + return Status; +} + +/** + Convert one Null-terminated Unicode string to EFI_IPv6_ADDRESS and prefix length. + The format of the string is defined in RFC 4291 - Text Pepresentation of Addresses + Prefixes: ipv6-address/prefix-length. + + @param[in] String The pointer to the Ascii string. + @param[out] Ip6Address The pointer to the converted IPv6 address. + @param[out] PrefixLength The pointer to the converted prefix length. + + @retval EFI_SUCCESS Convert to IPv6 address successfully. + @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip6Address is NULL. + @retval EFI_OUT_OF_RESOURCES Fail to perform the operation due to lack of resource. + +**/ +EFI_STATUS +EFIAPI +NetLibStrToIp6andPrefix ( + IN CONST CHAR16 *String, + OUT EFI_IPv6_ADDRESS *Ip6Address, + OUT UINT8 *PrefixLength + ) +{ + CHAR8 *Ip6Str; + CHAR8 *PrefixStr; + CHAR8 *TempStr; + EFI_STATUS Status; + UINT8 Length; + + if ((String == NULL) || (Ip6Address == NULL) || (PrefixLength == NULL)) { + return EFI_INVALID_PARAMETER; + } + + Ip6Str = (CHAR8 *) AllocatePool ((StrLen (String) + 1) * sizeof (CHAR8)); + if (Ip6Str == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + UnicodeStrToAsciiStr (String, Ip6Str); + + // + // Get the sub string describing prefix length. + // + TempStr = Ip6Str; + while (*TempStr != '\0' && (*TempStr != '/')) { + TempStr++; + } + + if (*TempStr == '/') { + PrefixStr = TempStr + 1; + } else { + PrefixStr = NULL; + } + + // + // Get the sub string describing IPv6 address and convert it. + // + *TempStr = '\0'; + + Status = NetLibAsciiStrToIp6 (Ip6Str, Ip6Address); + if (EFI_ERROR (Status)) { + goto Exit; + } + + // + // If input string doesn't indicate the prefix length, return 0xff. + // + Length = 0xFF; + + // + // Convert the string to prefix length + // + if (PrefixStr != NULL) { + + Status = EFI_INVALID_PARAMETER; + Length = 0; + while (*PrefixStr != '\0') { + if (NET_IS_DIGIT (*PrefixStr)) { + Length = (UINT8) (Length * 10 + (*PrefixStr - '0')); + if (Length >= IP6_PREFIX_NUM) { + goto Exit; + } + } else { + goto Exit; + } + + PrefixStr++; + } + } + + *PrefixLength = Length; + Status = EFI_SUCCESS; + +Exit: + + FreePool (Ip6Str); + return Status; +} + + + +/** + This function obtains the system guid from the smbios table. + + @param[out] SystemGuid The pointer of the returned system guid. + + @retval EFI_SUCCESS Successfully obtained the system guid. + @retval EFI_NOT_FOUND Did not find the SMBIOS table. + +**/ +EFI_STATUS +EFIAPI +NetLibGetSystemGuid ( + OUT EFI_GUID *SystemGuid + ) +{ + EFI_STATUS Status; + SMBIOS_TABLE_ENTRY_POINT *SmbiosTable; + SMBIOS_STRUCTURE_POINTER Smbios; + SMBIOS_STRUCTURE_POINTER SmbiosEnd; + CHAR8 *String; + + SmbiosTable = NULL; + Status = EfiGetSystemConfigurationTable (&gEfiSmbiosTableGuid, (VOID **) &SmbiosTable); + + if (EFI_ERROR (Status) || SmbiosTable == NULL) { + return EFI_NOT_FOUND; + } + + Smbios.Hdr = (SMBIOS_STRUCTURE *) (UINTN) SmbiosTable->TableAddress; + SmbiosEnd.Raw = (UINT8 *) (UINTN) (SmbiosTable->TableAddress + SmbiosTable->TableLength); + + do { + if (Smbios.Hdr->Type == 1) { + if (Smbios.Hdr->Length < 0x19) { + // + // Older version did not support UUID. + // + return EFI_NOT_FOUND; + } + + // + // SMBIOS tables are byte packed so we need to do a byte copy to + // prevend alignment faults on Itanium-based platform. + // + CopyMem (SystemGuid, &Smbios.Type1->Uuid, sizeof (EFI_GUID)); + return EFI_SUCCESS; + } + + // + // Go to the next SMBIOS structure. Each SMBIOS structure may include 2 parts: + // 1. Formatted section; 2. Unformatted string section. So, 2 steps are needed + // to skip one SMBIOS structure. + // + + // + // Step 1: Skip over formatted section. + // + String = (CHAR8 *) (Smbios.Raw + Smbios.Hdr->Length); + + // + // Step 2: Skip over unformated string section. + // + do { + // + // Each string is terminated with a NULL(00h) BYTE and the sets of strings + // is terminated with an additional NULL(00h) BYTE. + // + for ( ; *String != 0; String++) { + } + + if (*(UINT8*)++String == 0) { + // + // Pointer to the next SMBIOS structure. + // + Smbios.Raw = (UINT8 *)++String; + break; + } + } while (TRUE); + } while (Smbios.Raw < SmbiosEnd.Raw); + return EFI_NOT_FOUND; +}