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