+/**\r
+ Get the mac address string from the device path.\r
+ if the device path has the vlan, get the vanid also.\r
+ \r
+ @param MacAddressNode Device path begin with mac address \r
+ @param PBuffer Output string buffer contain mac address.\r
+\r
+**/\r
+BOOLEAN \r
+GetMacAddressString(\r
+ IN MAC_ADDR_DEVICE_PATH *MacAddressNode,\r
+ OUT CHAR16 **PBuffer\r
+ )\r
+{\r
+ UINTN HwAddressSize;\r
+ UINTN Index;\r
+ UINT8 *HwAddress;\r
+ EFI_DEVICE_PATH_PROTOCOL *Node;\r
+ UINT16 VlanId;\r
+ CHAR16 *String;\r
+ UINTN BufferLen;\r
+\r
+ VlanId = 0;\r
+ String = NULL;\r
+ ASSERT(MacAddressNode != NULL);\r
+\r
+ HwAddressSize = sizeof (EFI_MAC_ADDRESS);\r
+ if (MacAddressNode->IfType == 0x01 || MacAddressNode->IfType == 0x00) {\r
+ HwAddressSize = 6;\r
+ }\r
+\r
+ //\r
+ // The output format is MAC:XX:XX:XX:...\XXXX\r
+ // The size is the Number size + ":" size + Vlan size(\XXXX) + End\r
+ //\r
+ BufferLen = (4 + 2 * HwAddressSize + (HwAddressSize - 1) + 5 + 1) * sizeof (CHAR16);\r
+ String = AllocateZeroPool (BufferLen);\r
+ if (String == NULL) {\r
+ return FALSE;\r
+ }\r
+\r
+ *PBuffer = String;\r
+ StrCpy(String, L"MAC:");\r
+ String += 4;\r
+ \r
+ //\r
+ // Convert the MAC address into a unicode string.\r
+ //\r
+ HwAddress = &MacAddressNode->MacAddress.Addr[0];\r
+ for (Index = 0; Index < HwAddressSize; Index++) {\r
+ String += UnicodeValueToString (String, PREFIX_ZERO | RADIX_HEX, *(HwAddress++), 2);\r
+ if (Index < HwAddressSize - 1) {\r
+ *String++ = L':';\r
+ }\r
+ }\r
+ \r
+ //\r
+ // If VLAN is configured, it will need extra 5 characters like "\0005".\r
+ // Plus one unicode character for the null-terminator.\r
+ //\r
+ Node = (EFI_DEVICE_PATH_PROTOCOL *)MacAddressNode;\r
+ while (!IsDevicePathEnd (Node)) {\r
+ if (Node->Type == MESSAGING_DEVICE_PATH && Node->SubType == MSG_VLAN_DP) {\r
+ VlanId = ((VLAN_DEVICE_PATH *) Node)->VlanId;\r
+ }\r
+ Node = NextDevicePathNode (Node);\r
+ }\r
+\r
+ if (VlanId != 0) {\r
+ *String++ = L'\\';\r
+ String += UnicodeValueToString (String, PREFIX_ZERO | RADIX_HEX, VlanId, 4);\r
+ }\r
+\r
+ //\r
+ // Null terminate the Unicode string\r
+ //\r
+ *String = L'\0';\r
+\r
+ return TRUE;\r
+}\r
+\r
+/**\r
+ Save question id and prompt id to the mac device list.\r
+ If the same mac address has saved yet, no need to add more.\r
+\r
+ @param MacAddrString Mac address string.\r
+\r
+ @retval EFI_SUCCESS Add the item is successful.\r
+ @return Other values if failed to Add the item.\r
+**/\r
+BOOLEAN \r
+AddIdToMacDeviceList (\r
+ IN EFI_STRING MacAddrString\r
+ )\r
+{\r
+ MENU_INFO_ITEM *TempDeviceList;\r
+ UINTN Index;\r
+ EFI_STRING StoredString;\r
+ EFI_STRING_ID PromptId;\r
+ EFI_HII_HANDLE HiiHandle;\r
+\r
+ HiiHandle = gDeviceManagerPrivate.HiiHandle;\r
+ TempDeviceList = NULL;\r
+\r
+ for (Index = 0; Index < mMacDeviceList.CurListLen; Index ++) {\r
+ StoredString = HiiGetString (HiiHandle, mMacDeviceList.NodeList[Index].PromptId, NULL);\r
+ if (StoredString == NULL) {\r
+ return FALSE;\r
+ }\r
+\r
+ //\r
+ // Already has save the same mac address to the list.\r
+ //\r
+ if (StrCmp (MacAddrString, StoredString) == 0) {\r
+ return FALSE;\r
+ }\r
+ }\r
+\r
+ PromptId = HiiSetString(HiiHandle, 0, MacAddrString, NULL);\r
+ //\r
+ // If not in the list, save it.\r
+ //\r
+ if (mMacDeviceList.MaxListLen > mMacDeviceList.CurListLen + 1) {\r
+ mMacDeviceList.NodeList[mMacDeviceList.CurListLen].PromptId = PromptId;\r
+ mMacDeviceList.NodeList[mMacDeviceList.CurListLen].QuestionId = (EFI_QUESTION_ID) (mMacDeviceList.CurListLen + NETWORK_DEVICE_LIST_KEY_OFFSET);\r
+ } else {\r
+ mMacDeviceList.MaxListLen += MAX_MAC_ADDRESS_NODE_LIST_LEN;\r
+ if (mMacDeviceList.CurListLen != 0) {\r
+ TempDeviceList = (MENU_INFO_ITEM *)AllocateCopyPool (sizeof (MENU_INFO_ITEM) * mMacDeviceList.MaxListLen, (VOID *)mMacDeviceList.NodeList);\r
+ } else {\r
+ TempDeviceList = (MENU_INFO_ITEM *)AllocatePool (sizeof (MENU_INFO_ITEM) * mMacDeviceList.MaxListLen);\r
+ }\r
+ \r
+ if (TempDeviceList == NULL) {\r
+ return FALSE;\r
+ }\r
+ TempDeviceList[mMacDeviceList.CurListLen].PromptId = PromptId; \r
+ TempDeviceList[mMacDeviceList.CurListLen].QuestionId = (EFI_QUESTION_ID) (mMacDeviceList.CurListLen + NETWORK_DEVICE_LIST_KEY_OFFSET);\r
+ \r
+ if (mMacDeviceList.CurListLen > 0) {\r
+ FreePool(mMacDeviceList.NodeList);\r
+ }\r
+ \r
+ mMacDeviceList.NodeList = TempDeviceList;\r
+ }\r
+ mMacDeviceList.CurListLen ++;\r
+\r
+ return TRUE;\r
+}\r
+\r
+/**\r
+ Check the devcie path, try to find whether it has mac address path.\r
+\r
+ In this function, first need to check whether this path has mac address path.\r
+ second, when the mac address device path has find, also need to deicide whether\r
+ need to add this mac address relate info to the menu.\r
+\r
+ @param *Node Input device which need to be check.\r
+ @param *NeedAddItem Whether need to add the menu in the network device list.\r
+ \r
+ @retval TRUE Has mac address device path.\r
+ @retval FALSE NOT Has mac address device path. \r
+\r
+**/\r
+BOOLEAN\r
+IsMacAddressDevicePath (\r
+ IN VOID *Node,\r
+ OUT BOOLEAN *NeedAddItem\r
+ )\r
+{\r
+ EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
+ CHAR16 *Buffer;\r
+ BOOLEAN ReturnVal;\r
+ \r
+ ASSERT (Node != NULL);\r
+ *NeedAddItem = FALSE;\r
+ ReturnVal = FALSE;\r
+ Buffer = NULL;\r
+\r
+ DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) Node;\r
+\r
+ //\r
+ // find the partition device path node\r
+ //\r
+ while (!IsDevicePathEnd (DevicePath)) {\r
+ if ((DevicePathType (DevicePath) == MESSAGING_DEVICE_PATH) &&\r
+ (DevicePathSubType (DevicePath) == MSG_MAC_ADDR_DP)) {\r
+ ReturnVal = TRUE;\r
+ \r
+ if (DEVICE_MANAGER_FORM_ID == mNextShowFormId) {\r
+ *NeedAddItem = TRUE;\r
+ break;\r
+ } \r
+ \r
+ if (!GetMacAddressString((MAC_ADDR_DEVICE_PATH*)DevicePath, &Buffer)) {\r
+ break;\r
+ }\r
+\r
+ if (NETWORK_DEVICE_FORM_ID == mNextShowFormId) {\r
+ if (StrCmp (Buffer, mSelectedMacAddrString) == 0) {\r
+ *NeedAddItem = TRUE;\r
+ }\r
+ break;\r
+ }\r
+\r
+ if (NETWORK_DEVICE_LIST_FORM_ID == mNextShowFormId) {\r
+ //\r
+ // Same handle may has two network child handle, so the questionid \r
+ // has the offset of SAME_HANDLE_KEY_OFFSET.\r
+ //\r
+ if (AddIdToMacDeviceList (Buffer)) {\r
+ *NeedAddItem = TRUE;\r
+ }\r
+ break;\r
+ }\r
+ } \r
+ DevicePath = NextDevicePathNode (DevicePath);\r
+ }\r
+\r
+ if (Buffer != NULL) {\r
+ FreePool (Buffer);\r
+ }\r
+\r
+ return ReturnVal;\r
+}\r
+\r
+/**\r
+ Check to see if the device path is for the network device.\r
+\r
+ @param Handle The HII handle which include the mac address device path.\r
+ @param ItemCount The new add Mac address item count.\r
+\r
+ @retval TRUE Need to add new item in the menu.\r
+ @return FALSE Do not need to add the menu about the network.\r
+\r
+**/\r
+BOOLEAN \r
+IsNeedAddNetworkMenu (\r
+ IN EFI_HII_HANDLE Handle,\r
+ OUT UINTN *ItemCount\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ UINTN EntryCount;\r
+ UINTN Index; \r
+ EFI_HANDLE DriverHandle;\r
+ EFI_HANDLE ControllerHandle;\r
+ EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
+ EFI_DEVICE_PATH_PROTOCOL *TmpDevicePath;\r
+ EFI_DEVICE_PATH_PROTOCOL *ChildDevicePath;\r
+ EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInfoBuffer;\r
+ BOOLEAN IsNeedAdd;\r
+\r
+ IsNeedAdd = FALSE;\r
+ OpenInfoBuffer = NULL;\r
+ if ((Handle == NULL) || (ItemCount == NULL)) {\r
+ return FALSE;\r
+ }\r
+ *ItemCount = 0;\r
+\r
+ Status = gHiiDatabase->GetPackageListHandle (gHiiDatabase, Handle, &DriverHandle);\r
+ if (EFI_ERROR (Status)) {\r
+ return FALSE;\r
+ }\r
+ //\r
+ // Get the device path by the got Driver handle .\r
+ //\r
+ Status = gBS->HandleProtocol (DriverHandle, &gEfiDevicePathProtocolGuid, (VOID **) &DevicePath);\r
+ if (EFI_ERROR (Status)) {\r
+ return FALSE;\r
+ }\r
+ TmpDevicePath = DevicePath;\r
+\r
+ // \r
+ // Check whether this device path include mac address device path.\r
+ // If this path has mac address path, get the value whether need \r
+ // add this info to the menu and return.\r
+ // Else check more about the child handle devcie path.\r
+ //\r
+ if (IsMacAddressDevicePath(TmpDevicePath, &IsNeedAdd)) {\r
+ if ((NETWORK_DEVICE_LIST_FORM_ID == mNextShowFormId) && IsNeedAdd) {\r
+ (*ItemCount) = 1;\r
+ }\r
+ return IsNeedAdd;\r
+ }\r
+\r
+ //\r
+ // Search whether this path is the controller path, not he child handle path.\r
+ // And the child handle has the network devcie connected.\r
+ //\r
+ TmpDevicePath = DevicePath;\r
+ Status = gBS->LocateDevicePath(&gEfiDevicePathProtocolGuid, &TmpDevicePath, &ControllerHandle);\r
+ if (EFI_ERROR (Status)) {\r
+ return FALSE;\r
+ }\r
+\r
+ if (!IsDevicePathEnd (TmpDevicePath)) {\r
+ return FALSE; \r
+ }\r
+\r
+ //\r
+ // Retrieve the list of agents that are consuming the specific protocol\r
+ // on ControllerHandle.\r
+ // The buffer point by OpenInfoBuffer need be free at this function.\r
+ //\r
+ Status = gBS->OpenProtocolInformation (\r
+ ControllerHandle,\r
+ &gEfiPciIoProtocolGuid,\r
+ &OpenInfoBuffer,\r
+ &EntryCount\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ return FALSE;\r
+ }\r
+\r
+ //\r
+ // Inspect if ChildHandle is one of the agents.\r
+ //\r
+ Status = EFI_UNSUPPORTED;\r
+ for (Index = 0; Index < EntryCount; Index++) {\r
+ //\r
+ // Query all the children created by the controller handle's driver\r
+ //\r
+ if ((OpenInfoBuffer[Index].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) != 0) {\r
+ Status = gBS->OpenProtocol (\r
+ OpenInfoBuffer[Index].ControllerHandle,\r
+ &gEfiDevicePathProtocolGuid,\r
+ (VOID **) &ChildDevicePath,\r
+ NULL,\r
+ NULL,\r
+ EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ continue;\r
+ }\r
+\r
+ // \r
+ // Check whether this device path include mac address device path.\r
+ //\r
+ if (!IsMacAddressDevicePath(ChildDevicePath, &IsNeedAdd)) {\r
+ //\r
+ // If this path not has mac address path, check the other.\r
+ //\r
+ continue;\r
+ } else {\r
+ //\r
+ // If need to update the NETWORK_DEVICE_LIST_FORM, try to get more.\r
+ //\r
+ if ((NETWORK_DEVICE_LIST_FORM_ID == mNextShowFormId)) {\r
+ if (IsNeedAdd) {\r
+ (*ItemCount) += 1;\r
+ }\r
+ continue;\r
+ } else {\r
+ //\r
+ // If need to update other form, return whether need to add to the menu.\r
+ // \r
+ goto Done;\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+Done:\r
+ if (OpenInfoBuffer != NULL) {\r
+ FreePool (OpenInfoBuffer); \r
+ }\r
+ return IsNeedAdd; \r
+}\r
+\r