1 /** @file
2   UEFI Component Name(2) protocol implementation for MnpDxe driver.
3 
4 Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6 
7 **/
8 
9 #include  "MnpImpl.h"
10 
11 //
12 // EFI Component Name Protocol
13 //
14 GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME_PROTOCOL   gMnpComponentName = {
15   MnpComponentNameGetDriverName,
16   MnpComponentNameGetControllerName,
17   "eng"
18 };
19 
20 //
21 // EFI Component Name 2 Protocol
22 //
23 GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME2_PROTOCOL  gMnpComponentName2 = {
24   (EFI_COMPONENT_NAME2_GET_DRIVER_NAME) MnpComponentNameGetDriverName,
25   (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME) MnpComponentNameGetControllerName,
26   "en"
27 };
28 
29 GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE      mMnpDriverNameTable[] = {
30   {
31     "eng;en",
32     L"MNP Network Service Driver"
33   },
34   {
35     NULL,
36     NULL
37   }
38 };
39 
40 GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE    *gMnpControllerNameTable = NULL;
41 
42 /**
43   Retrieves a Unicode string that is the user readable name of the driver.
44 
45   This function retrieves the user readable name of a driver in the form of a
46   Unicode string. If the driver specified by This has a user readable name in
47   the language specified by Language, then a pointer to the driver name is
48   returned in DriverName, and EFI_SUCCESS is returned. If the driver specified
49   by This does not support the language specified by Language,
50   then EFI_UNSUPPORTED is returned.
51 
52   @param[in]   This             A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
53                                 EFI_COMPONENT_NAME_PROTOCOL instance.
54 
55   @param[in]   Language         A pointer to a Null-terminated ASCII string
56                                 array indicating the language. This is the
57                                 language of the driver name that the caller is
58                                 requesting, and it must match one of the
59                                 languages specified in SupportedLanguages. The
60                                 number of languages supported by a driver is up
61                                 to the driver writer. Language is specified
62                                 in RFC 4646 or ISO 639-2 language code format.
63 
64   @param[out]  DriverName       A pointer to the Unicode string to return.
65                                 This Unicode string is the name of the
66                                 driver specified by This in the language
67                                 specified by Language.
68 
69   @retval EFI_SUCCESS           The Unicode string for the Driver specified by
70                                 This and the language specified by Language was
71                                 returned in DriverName.
72 
73   @retval EFI_INVALID_PARAMETER Language is NULL.
74 
75   @retval EFI_INVALID_PARAMETER DriverName is NULL.
76 
77   @retval EFI_UNSUPPORTED       The driver specified by This does not support
78                                 the language specified by Language.
79 
80 **/
81 EFI_STATUS
82 EFIAPI
MnpComponentNameGetDriverName(IN EFI_COMPONENT_NAME_PROTOCOL * This,IN CHAR8 * Language,OUT CHAR16 ** DriverName)83 MnpComponentNameGetDriverName (
84   IN     EFI_COMPONENT_NAME_PROTOCOL   *This,
85   IN     CHAR8                         *Language,
86      OUT CHAR16                        **DriverName
87   )
88 {
89   return LookupUnicodeString2 (
90            Language,
91            This->SupportedLanguages,
92            mMnpDriverNameTable,
93            DriverName,
94            (BOOLEAN) (This == &gMnpComponentName)
95            );
96 }
97 
98 /**
99   Update the component name for the MNP child handle.
100 
101   @param  Mnp[in]                 A pointer to the EFI_MANAGED_NETWORK_PROTOCOL.
102 
103 
104   @retval EFI_SUCCESS             Update the ControllerNameTable of this instance successfully.
105   @retval EFI_INVALID_PARAMETER   The input parameter is invalid.
106 
107 **/
108 EFI_STATUS
UpdateName(IN EFI_MANAGED_NETWORK_PROTOCOL * Mnp)109 UpdateName (
110   IN   EFI_MANAGED_NETWORK_PROTOCOL     *Mnp
111   )
112 {
113   EFI_STATUS                       Status;
114   MNP_INSTANCE_DATA                *Instance;
115   CHAR16                           HandleName[80];
116   EFI_MANAGED_NETWORK_CONFIG_DATA  MnpConfigData;
117   EFI_SIMPLE_NETWORK_MODE          SnpModeData;
118   UINTN                            OffSet;
119   UINTN                            Index;
120 
121   if (Mnp == NULL) {
122     return EFI_INVALID_PARAMETER;
123   }
124 
125   Instance = MNP_INSTANCE_DATA_FROM_THIS (Mnp);
126   //
127   // Format the child name into the string buffer as:
128   // MNP (MAC=FF-FF-FF-FF-FF-FF, ProtocolType=0x0800, VlanId=0)
129   //
130   Status = Mnp->GetModeData (Mnp, &MnpConfigData, &SnpModeData);
131   if (!EFI_ERROR (Status)) {
132     OffSet = 0;
133     //
134     // Print the MAC address.
135     //
136     OffSet += UnicodeSPrint (
137                 HandleName,
138                 sizeof (HandleName),
139                 L"MNP (MAC="
140                 );
141     for (Index = 0; Index < SnpModeData.HwAddressSize; Index++) {
142       OffSet += UnicodeSPrint (
143                   HandleName + OffSet,
144                   sizeof (HandleName) - OffSet * sizeof (CHAR16),
145                   L"%02X-",
146                   SnpModeData.CurrentAddress.Addr[Index]
147                   );
148     }
149     ASSERT (OffSet > 0);
150     //
151     // Remove the last '-'
152     //
153     OffSet--;
154     //
155     // Print the ProtocolType and VLAN ID for this instance.
156     //
157     OffSet += UnicodeSPrint (
158                 HandleName + OffSet,
159                 sizeof (HandleName) - OffSet * sizeof (CHAR16),
160                 L", ProtocolType=0x%X, VlanId=%d)",
161                 MnpConfigData.ProtocolTypeFilter,
162                 Instance->MnpServiceData->VlanId
163                 );
164   } else if (Status == EFI_NOT_STARTED) {
165     UnicodeSPrint (
166       HandleName,
167       sizeof (HandleName),
168       L"MNP (Not started)"
169       );
170   } else {
171     return Status;
172   }
173 
174   if (gMnpControllerNameTable != NULL) {
175     FreeUnicodeStringTable (gMnpControllerNameTable);
176     gMnpControllerNameTable = NULL;
177   }
178 
179   Status = AddUnicodeString2 (
180              "eng",
181              gMnpComponentName.SupportedLanguages,
182              &gMnpControllerNameTable,
183              HandleName,
184              TRUE
185              );
186   if (EFI_ERROR (Status)) {
187     return Status;
188   }
189 
190   return AddUnicodeString2 (
191            "en",
192            gMnpComponentName2.SupportedLanguages,
193            &gMnpControllerNameTable,
194            HandleName,
195            FALSE
196            );
197 }
198 
199 /**
200   Retrieves a Unicode string that is the user readable name of the controller
201   that is being managed by a driver.
202 
203   This function retrieves the user readable name of the controller specified by
204   ControllerHandle and ChildHandle in the form of a Unicode string. If the
205   driver specified by This has a user readable name in the language specified by
206   Language, then a pointer to the controller name is returned in ControllerName,
207   and EFI_SUCCESS is returned.  If the driver specified by This is not currently
208   managing the controller specified by ControllerHandle and ChildHandle,
209   then EFI_UNSUPPORTED is returned.  If the driver specified by This does not
210   support the language specified by Language, then EFI_UNSUPPORTED is returned.
211   Currently not implemented.
212 
213   @param[in]   This             A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
214                                 EFI_COMPONENT_NAME_PROTOCOL instance.
215 
216   @param[in]   ControllerHandle The handle of a controller that the driver
217                                 specified by This is managing.  This handle
218                                 specifies the controller whose name is to be
219                                 returned.
220 
221   @param[in]   ChildHandle      The handle of the child controller to retrieve
222                                 the name of.  This is an optional parameter that
223                                 may be NULL.  It will be NULL for device
224                                 drivers.  It will also be NULL for a bus drivers
225                                 that wish to retrieve the name of the bus
226                                 controller.  It will not be NULL for a bus
227                                 driver that wishes to retrieve the name of a
228                                 child controller.
229 
230   @param[in]   Language         A pointer to a Null-terminated ASCII string
231                                 array indicating the language.  This is the
232                                 language of the driver name that the caller is
233                                 requesting, and it must match one of the
234                                 languages specified in SupportedLanguages. The
235                                 number of languages supported by a driver is up
236                                 to the driver writer. Language is specified in
237                                 RFC 4646 or ISO 639-2 language code format.
238 
239   @param[out]  ControllerName   A pointer to the Unicode string to return.
240                                 This Unicode string is the name of the
241                                 controller specified by ControllerHandle and
242                                 ChildHandle in the language specified by
243                                 Language from the point of view of the driver
244                                 specified by This.
245 
246   @retval EFI_SUCCESS           The Unicode string for the user readable name
247                                 specified by This, ControllerHandle, ChildHandle,
248                                 and Language was returned in ControllerName.
249 
250   @retval EFI_INVALID_PARAMETER ControllerHandle is NULL.
251 
252   @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid
253                                 EFI_HANDLE.
254 
255   @retval EFI_INVALID_PARAMETER Language is NULL.
256 
257   @retval EFI_INVALID_PARAMETER ControllerName is NULL.
258 
259   @retval EFI_UNSUPPORTED       The driver specified by This is not currently
260                                 managing the controller specified by
261                                 ControllerHandle and ChildHandle.
262 
263   @retval EFI_UNSUPPORTED       The driver specified by This does not support
264                                 the language specified by Language.
265 
266 **/
267 EFI_STATUS
268 EFIAPI
MnpComponentNameGetControllerName(IN EFI_COMPONENT_NAME_PROTOCOL * This,IN EFI_HANDLE ControllerHandle,IN EFI_HANDLE ChildHandle OPTIONAL,IN CHAR8 * Language,OUT CHAR16 ** ControllerName)269 MnpComponentNameGetControllerName (
270   IN     EFI_COMPONENT_NAME_PROTOCOL   *This,
271   IN     EFI_HANDLE                    ControllerHandle,
272   IN     EFI_HANDLE                    ChildHandle        OPTIONAL,
273   IN     CHAR8                         *Language,
274      OUT CHAR16                        **ControllerName
275   )
276 {
277   EFI_STATUS                    Status;
278   EFI_MANAGED_NETWORK_PROTOCOL  *Mnp;
279 
280   //
281   // Only provide names for MNP child handles.
282   //
283   if (ChildHandle == NULL) {
284     return EFI_UNSUPPORTED;
285   }
286 
287   //
288   // Make sure this driver is currently managing ControllerHandle
289   //
290   Status = EfiTestManagedDevice (
291              ControllerHandle,
292              gMnpDriverBinding.DriverBindingHandle,
293              &gEfiSimpleNetworkProtocolGuid
294              );
295   if (EFI_ERROR (Status)) {
296     return Status;
297   }
298 
299   //
300   // Make sure this driver produced ChildHandle
301   //
302   Status = EfiTestChildHandle (
303              ControllerHandle,
304              ChildHandle,
305              &gEfiManagedNetworkServiceBindingProtocolGuid
306              );
307   if (EFI_ERROR (Status)) {
308     return Status;
309   }
310 
311   //
312   // Retrieve an instance of a produced protocol from ChildHandle
313   //
314   Status = gBS->OpenProtocol (
315                   ChildHandle,
316                   &gEfiManagedNetworkProtocolGuid,
317                   (VOID **)&Mnp,
318                   NULL,
319                   NULL,
320                   EFI_OPEN_PROTOCOL_GET_PROTOCOL
321                   );
322   if (EFI_ERROR (Status)) {
323     return Status;
324   }
325 
326   //
327   // Update the component name for this child handle.
328   //
329   Status = UpdateName (Mnp);
330   if (EFI_ERROR (Status)) {
331     return Status;
332   }
333 
334   return LookupUnicodeString2 (
335            Language,
336            This->SupportedLanguages,
337            gMnpControllerNameTable,
338            ControllerName,
339            (BOOLEAN)(This == &gMnpComponentName)
340            );
341 }
342