]> git.proxmox.com Git - mirror_edk2.git/blame - StdLib/LibC/Uefi/Devices/Utility/DevSearch.c
Add Socket Libraries.
[mirror_edk2.git] / StdLib / LibC / Uefi / Devices / Utility / DevSearch.c
CommitLineData
53e1e5c6 1/** @file\r
2 Device Abstraction: Search device list for a matching device.\r
3\r
4 Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>\r
5 This program and the accompanying materials are licensed and made available under\r
6 the terms and conditions of the BSD License that accompanies this distribution.\r
7 The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php.\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12**/\r
13#include <Uefi.h>\r
14#include <Library/BaseLib.h>\r
15#include <Library/BaseMemoryLib.h>\r
16\r
17#include <LibConfig.h>\r
18\r
19#include <errno.h>\r
20#include <kfile.h>\r
21#include <Device/Device.h>\r
22#include <MainData.h>\r
23\r
24/** Find a DeviceNode matching DevName or DevProto, or both.\r
25\r
26 If DevName is NULL, then the device name is not used in the search.\r
27 If DevProto is NULL, then the protocol GUID is not used in the search.\r
28 If both are NULL, then INVALID_PARAMETER is returned.\r
29 If both DevName and DevProto are specified, then both must match.\r
30 If both are specified but only one matches, then DEVICE_ERROR is returned.\r
31\r
32 @param DevName Name of the Device Abstraction to find.\r
33 @param DevProto GUID of the Protocol associated with the Device Abstraction.\r
34 @param Node Pointer to the pointer to receive the found Device Node's address.\r
35\r
36 @retval RETURN_SUCCESS The desired Device Node was found.\r
37 @retval RETURN_INVALID_PARAMETER Both DevName and DevProto are NULL or Node is NULL.\r
38 @retval RETURN_DEVICE_ERROR DevName matched but DevProto did not.\r
39 @retval RETURN_NOT_FOUND The desired device was not found.\r
40**/\r
41EFI_STATUS\r
42EFIAPI\r
43__DevSearch(\r
44 IN CHAR16 *DevName,\r
45 IN GUID *DevProto,\r
46 OUT DeviceNode **Node\r
47 )\r
48{\r
49 RETURN_STATUS Status = RETURN_NOT_FOUND;\r
50 DeviceNode *WorkNode;\r
51 INT32 DevMatched;\r
52\r
53 if(((DevName == NULL) && (DevProto == NULL)) || (Node == NULL)) {\r
54 Status = RETURN_INVALID_PARAMETER;\r
55 }\r
56 else {\r
57 if(IsListEmpty((LIST_ENTRY *)&daDeviceList)) {\r
58 Status = RETURN_NOT_FOUND;\r
59 }\r
60 else {\r
61 /* Traverse the list of Device Nodes hunting for a match */\r
62 WorkNode = (DeviceNode *)GetFirstNode((LIST_ENTRY *)&daDeviceList);\r
63 do {\r
64 /* Use DevMatched to keep track of the three match conditions. */\r
65 DevMatched = 0;\r
66 if(DevName != NULL) {\r
67 ++DevMatched;\r
68 if(wcsncmp(DevName, WorkNode->DevName, wcslen(WorkNode->DevName)) == 0) {\r
69 ++DevMatched;\r
70 }\r
71 }\r
72 /* At this point, DevMatched has one of the following values:\r
73 0 DevName == NULL, no name comparison\r
74 1 DevName did not match WorkNode's name\r
75 2 DevName MATCHED\r
76 */\r
77 if((DevMatched != 1) && (DevProto != NULL) && (WorkNode->DevProto != NULL)) {\r
78 /* Only bother with the GUID comparison if:\r
79 * There was NOT a name mismatch\r
80 * DevProto is NOT NULL -- there is a GUID to compare\r
81 * WorkNode->DevProto is NOT NULL\r
82 */\r
83 if(CompareGuid(DevProto, WorkNode->DevProto)) {\r
84 // GUIDs matched, we found it\r
85 Status = RETURN_SUCCESS;\r
86 *Node = WorkNode;\r
87 break;\r
88 }\r
89 else {\r
90 // GUIDs did not match\r
91 if(DevMatched == 2) {\r
92 // Name matched, GUID did not!\r
93 Status = RETURN_DEVICE_ERROR;\r
94 break; // Don't try any more, we have an internal problem\r
95 }\r
96 }\r
97 }\r
98 else {\r
99 if(DevMatched == 2) {\r
100 // Device Name matched, GUIDs skipped\r
101 Status = RETURN_SUCCESS;\r
102 *Node = WorkNode;\r
103 break;\r
104 }\r
105 }\r
106 // Check the next device in the list\r
107 WorkNode = (DeviceNode *)GetNextNode(&daDeviceList, (LIST_ENTRY *)WorkNode);\r
108 } while(&daDeviceList != (LIST_ENTRY *)WorkNode);\r
109 }\r
110 }\r
111 return Status;\r
112}\r