1 /** @file
2   Implementation of EFI_COMPONENT_NAME_PROTOCOL and
3   EFI_COMPONENT_NAME2_PROTOCOL protocol.
4 
5   Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>
6 
7   This program and the accompanying materials
8   are licensed and made available under the terms and conditions of the BSD License
9   which accompanies this distribution.  The full text of the license may be found at
10   http://opensource.org/licenses/bsd-license.php.
11 
12   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14 
15 **/
16 
17 #include "Ip6Impl.h"
18 
19 //
20 // EFI Component Name Functions
21 //
22 
23 /**
24   Retrieves a Unicode string that is the user-readable name of the driver.
25 
26   This function retrieves the user-readable name of a driver in the form of a
27   Unicode string. If the driver specified by This has a user-readable name in
28   the language specified by Language, then a pointer to the driver name is
29   returned in DriverName, and EFI_SUCCESS is returned. If the driver specified
30   by This does not support the language specified by Language,
31   then EFI_UNSUPPORTED is returned.
32 
33   @param[in]  This              A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
34                                 EFI_COMPONENT_NAME_PROTOCOL instance.
35 
36   @param[in]  Language          A pointer to a Null-terminated ASCII string
37                                 array indicating the language. This is the
38                                 language of the driver name that the caller is
39                                 requesting, and it must match one of the
40                                 languages specified in SupportedLanguages. The
41                                 number of languages supported by a driver is up
42                                 to the driver writer. Language is specified
43                                 in RFC 4646 or ISO 639-2 language code format.
44 
45   @param[out]  DriverName       A pointer to the Unicode string to return.
46                                 This Unicode string is the name of the
47                                 driver specified by This in the language
48                                 specified by Language.
49 
50   @retval EFI_SUCCESS           The Unicode string for the Driver specified by
51                                 This and the language specified by Language was
52                                 returned in DriverName.
53 
54   @retval EFI_INVALID_PARAMETER Language is NULL.
55 
56   @retval EFI_INVALID_PARAMETER DriverName is NULL.
57 
58   @retval EFI_UNSUPPORTED       The driver specified by This does not support
59                                 the language specified by Language.
60 
61 **/
62 EFI_STATUS
63 EFIAPI
64 Ip6ComponentNameGetDriverName (
65   IN  EFI_COMPONENT_NAME_PROTOCOL  *This,
66   IN  CHAR8                        *Language,
67   OUT CHAR16                       **DriverName
68   );
69 
70 /**
71   Retrieves a Unicode string that is the user-readable name of the controller
72   that is managed by a driver.
73 
74   This function retrieves the user-readable name of the controller specified by
75   ControllerHandle and ChildHandle in the form of a Unicode string. If the
76   driver specified by This has a user-readable name in the language specified by
77   Language, then a pointer to the controller name is returned in ControllerName,
78   and EFI_SUCCESS is returned.  If the driver specified by This is not currently
79   managing the controller specified by ControllerHandle and ChildHandle,
80   then EFI_UNSUPPORTED is returned.  If the driver specified by This does not
81   support the language specified by Language, then EFI_UNSUPPORTED is returned.
82 
83   @param[in]  This              A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
84                                 EFI_COMPONENT_NAME_PROTOCOL instance.
85 
86   @param[in]  ControllerHandle  The handle of a controller that the driver
87                                 specified by This is managing.  This handle
88                                 specifies the controller whose name is to be
89                                 returned.
90 
91   @param[in]  ChildHandle       The handle of the child controller to retrieve
92                                 the name of.  This is an optional parameter that
93                                 may be NULL.  It will be NULL for device
94                                 drivers.  It will also be NULL for a bus drivers
95                                 that wish to retrieve the name of the bus
96                                 controller.  It will not be NULL for a bus
97                                 driver that wishes to retrieve the name of a
98                                 child controller.
99 
100   @param[in]  Language          A pointer to a Null-terminated ASCII string
101                                 array indicating the language.  This is the
102                                 language of the driver name that the caller is
103                                 requesting, and it must match one of the
104                                 languages specified in SupportedLanguages. The
105                                 number of languages supported by a driver is up
106                                 to the driver writer. Language is specified in
107                                 RFC 4646 or ISO 639-2 language code format.
108 
109   @param[out]  ControllerName   A pointer to the Unicode string to return.
110                                 This Unicode string is the name of the
111                                 controller specified by ControllerHandle and
112                                 ChildHandle in the language specified by
113                                 Language from the point of view of the driver
114                                 specified by This.
115 
116   @retval EFI_SUCCESS           The Unicode string for the user-readable name in
117                                 the language specified by Language for the
118                                 driver specified by This was returned in
119                                 DriverName.
120 
121   @retval EFI_INVALID_PARAMETER ControllerHandle is NULL.
122 
123   @retval EFI_INVALID_PARAMETER ChildHandle is not NULL, and it is not a valid
124                                 EFI_HANDLE.
125 
126   @retval EFI_INVALID_PARAMETER Language is NULL.
127 
128   @retval EFI_INVALID_PARAMETER ControllerName is NULL.
129 
130   @retval EFI_UNSUPPORTED       The driver specified by This is not currently
131                                 managing the controller specified by
132                                 ControllerHandle and ChildHandle.
133 
134   @retval EFI_UNSUPPORTED       The driver specified by This does not support
135                                 the language specified by Language.
136 
137 **/
138 EFI_STATUS
139 EFIAPI
140 Ip6ComponentNameGetControllerName (
141   IN  EFI_COMPONENT_NAME_PROTOCOL                     *This,
142   IN  EFI_HANDLE                                      ControllerHandle,
143   IN  EFI_HANDLE                                      ChildHandle        OPTIONAL,
144   IN  CHAR8                                           *Language,
145   OUT CHAR16                                          **ControllerName
146   );
147 
148 //
149 // EFI Component Name Protocol.
150 //
151 GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME_PROTOCOL   gIp6ComponentName = {
152   Ip6ComponentNameGetDriverName,
153   Ip6ComponentNameGetControllerName,
154   "eng"
155 };
156 
157 //
158 // EFI Component Name 2 Protocol.
159 //
160 GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME2_PROTOCOL  gIp6ComponentName2 = {
161   (EFI_COMPONENT_NAME2_GET_DRIVER_NAME) Ip6ComponentNameGetDriverName,
162   (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME) Ip6ComponentNameGetControllerName,
163   "en"
164 };
165 
166 GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE      mIp6DriverNameTable[] = {
167   {
168     "eng;en",
169     L"IP6 Network Service Driver"
170   },
171   {
172     NULL,
173     NULL
174   }
175 };
176 
177 GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE      *gIp6ControllerNameTable = NULL;
178 
179 /**
180   Retrieves a Unicode string that is the user-readable name of the driver.
181 
182   This function retrieves the user-readable name of a driver in the form of a
183   Unicode string. If the driver specified by This has a user-readable name in
184   the language specified by Language, then a pointer to the driver name is
185   returned in DriverName, and EFI_SUCCESS is returned. If the driver specified
186   by This does not support the language specified by Language,
187   then EFI_UNSUPPORTED is returned.
188 
189   @param[in]  This              A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
190                                 EFI_COMPONENT_NAME_PROTOCOL instance.
191 
192   @param[in]  Language          A pointer to a Null-terminated ASCII string
193                                 array indicating the language. This is the
194                                 language of the driver name that the caller is
195                                 requesting, and it must match one of the
196                                 languages specified in SupportedLanguages. The
197                                 number of languages supported by a driver is up
198                                 to the driver writer. Language is specified
199                                 in RFC 4646 or ISO 639-2 language code format.
200 
201   @param[out]  DriverName       A pointer to the Unicode string to return.
202                                 This Unicode string is the name of the
203                                 driver specified by This in the language
204                                 specified by Language.
205 
206   @retval EFI_SUCCESS           The Unicode string for the Driver specified by
207                                 This and the language specified by Language was
208                                 returned in DriverName.
209 
210   @retval EFI_INVALID_PARAMETER Language is NULL.
211 
212   @retval EFI_INVALID_PARAMETER DriverName is NULL.
213 
214   @retval EFI_UNSUPPORTED       The driver specified by This does not support
215                                 the language specified by Language.
216 
217 **/
218 EFI_STATUS
219 EFIAPI
Ip6ComponentNameGetDriverName(IN EFI_COMPONENT_NAME_PROTOCOL * This,IN CHAR8 * Language,OUT CHAR16 ** DriverName)220 Ip6ComponentNameGetDriverName (
221   IN  EFI_COMPONENT_NAME_PROTOCOL  *This,
222   IN  CHAR8                        *Language,
223   OUT CHAR16                       **DriverName
224   )
225 {
226   return LookupUnicodeString2 (
227           Language,
228           This->SupportedLanguages,
229           mIp6DriverNameTable,
230           DriverName,
231           (BOOLEAN) (This == &gIp6ComponentName)
232           );
233 
234 }
235 
236 /**
237   Update the component name for the IP6 child handle.
238 
239   @param  Ip6[in]                   A pointer to the EFI_IP6_PROTOCOL.
240 
241 
242   @retval EFI_SUCCESS               Update the ControllerNameTable of this instance successfully.
243   @retval EFI_INVALID_PARAMETER     The input parameter is invalid.
244 
245 **/
246 EFI_STATUS
UpdateName(IN EFI_IP6_PROTOCOL * Ip6)247 UpdateName (
248   IN    EFI_IP6_PROTOCOL         *Ip6
249   )
250 {
251   EFI_STATUS                       Status;
252   CHAR16                           HandleName[128];
253   EFI_IP6_MODE_DATA                Ip6ModeData;
254   UINTN                            Offset;
255   CHAR16                           Address[sizeof"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"];
256 
257   if (Ip6 == NULL) {
258     return EFI_INVALID_PARAMETER;
259   }
260 
261   //
262   // Format the child name into the string buffer.
263   //
264   Offset = 0;
265   Status = Ip6->GetModeData (Ip6, &Ip6ModeData, NULL, NULL);
266   if (!EFI_ERROR (Status) && Ip6ModeData.IsStarted) {
267     Status = NetLibIp6ToStr (&Ip6ModeData.ConfigData.StationAddress, Address, sizeof(Address));
268     if (EFI_ERROR (Status)) {
269       return Status;
270     }
271     Offset += UnicodeSPrint (
272                 HandleName,
273                 sizeof(HandleName),
274                 L"IPv6(StationAddress=%s, ",
275                 Address
276                 );
277     Status = NetLibIp6ToStr (&Ip6ModeData.ConfigData.DestinationAddress, Address, sizeof(Address));
278     if (EFI_ERROR (Status)) {
279       return Status;
280     }
281     UnicodeSPrint (
282       HandleName + Offset,
283       sizeof(HandleName) - Offset * sizeof (CHAR16),
284       L"DestinationAddress=%s)",
285       Address
286       );
287   } else if (!Ip6ModeData.IsStarted) {
288     UnicodeSPrint (HandleName, sizeof(HandleName), L"IPv6(Not started)");
289   } else {
290     UnicodeSPrint (HandleName, sizeof(HandleName), L"IPv6(%r)", Status);
291   }
292 
293   if (gIp6ControllerNameTable != NULL) {
294       FreeUnicodeStringTable (gIp6ControllerNameTable);
295       gIp6ControllerNameTable = NULL;
296   }
297 
298   Status = AddUnicodeString2 (
299              "eng",
300              gIp6ComponentName.SupportedLanguages,
301              &gIp6ControllerNameTable,
302              HandleName,
303              TRUE
304              );
305   if (EFI_ERROR (Status)) {
306     return Status;
307   }
308 
309   return AddUnicodeString2 (
310            "en",
311            gIp6ComponentName2.SupportedLanguages,
312            &gIp6ControllerNameTable,
313            HandleName,
314            FALSE
315            );
316 }
317 
318 /**
319   Retrieves a Unicode string that is the user-readable name of the controller
320   that is being managed by a driver.
321 
322   This function retrieves the user-readable name of the controller specified by
323   ControllerHandle and ChildHandle in the form of a Unicode string. If the
324   driver specified by This has a user-readable name in the language specified by
325   Language, then a pointer to the controller name is returned in ControllerName,
326   and EFI_SUCCESS is returned.  If the driver specified by This is not currently
327   managing the controller specified by ControllerHandle and ChildHandle,
328   then EFI_UNSUPPORTED is returned.  If the driver specified by This does not
329   support the language specified by Language, then EFI_UNSUPPORTED is returned.
330 
331   @param[in]  This              A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
332                                 EFI_COMPONENT_NAME_PROTOCOL instance.
333 
334   @param[in]  ControllerHandle  The handle of a controller that the driver
335                                 specified by This is managing.  This handle
336                                 specifies the controller whose name is to be
337                                 returned.
338 
339   @param[in]  ChildHandle       The handle of the child controller to retrieve
340                                 the name of.  This is an optional parameter that
341                                 may be NULL.  It will be NULL for device
342                                 drivers.  It will also be NULL for a bus drivers
343                                 that wish to retrieve the name of the bus
344                                 controller.  It will not be NULL for a bus
345                                 driver that wishes to retrieve the name of a
346                                 child controller.
347 
348   @param[in]  Language          A pointer to a Null-terminated ASCII string
349                                 array indicating the language.  This is the
350                                 language of the driver name that the caller is
351                                 requesting, and it must match one of the
352                                 languages specified in SupportedLanguages. The
353                                 number of languages supported by a driver is up
354                                 to the driver writer. Language is specified in
355                                 RFC 4646 or ISO 639-2 language code format.
356 
357   @param[out]  ControllerName   A pointer to the Unicode string to return.
358                                 This Unicode string is the name of the
359                                 controller specified by ControllerHandle and
360                                 ChildHandle in the language specified by
361                                 Language from the point of view of the driver
362                                 specified by This.
363 
364   @retval EFI_SUCCESS           The Unicode string for the user-readable name in
365                                 the language specified by Language for the
366                                 driver specified by This was returned in
367                                 DriverName.
368 
369   @retval EFI_INVALID_PARAMETER ControllerHandle is NULL.
370 
371   @retval EFI_INVALID_PARAMETER ChildHandle is not NULL, and it is not a valid
372                                 EFI_HANDLE.
373 
374   @retval EFI_INVALID_PARAMETER Language is NULL.
375 
376   @retval EFI_INVALID_PARAMETER ControllerName is NULL.
377 
378   @retval EFI_UNSUPPORTED       The driver specified by This is not currently
379                                 managing the controller specified by
380                                 ControllerHandle and ChildHandle.
381 
382   @retval EFI_UNSUPPORTED       The driver specified by This does not support
383                                 the language specified by Language.
384 
385 **/
386 EFI_STATUS
387 EFIAPI
Ip6ComponentNameGetControllerName(IN EFI_COMPONENT_NAME_PROTOCOL * This,IN EFI_HANDLE ControllerHandle,IN EFI_HANDLE ChildHandle OPTIONAL,IN CHAR8 * Language,OUT CHAR16 ** ControllerName)388 Ip6ComponentNameGetControllerName (
389   IN  EFI_COMPONENT_NAME_PROTOCOL                     *This,
390   IN  EFI_HANDLE                                      ControllerHandle,
391   IN  EFI_HANDLE                                      ChildHandle        OPTIONAL,
392   IN  CHAR8                                           *Language,
393   OUT CHAR16                                          **ControllerName
394   )
395 {
396   EFI_STATUS                    Status;
397   EFI_IP6_PROTOCOL              *Ip6;
398 
399   //
400   // Only provide names for child handles.
401   //
402   if (ChildHandle == NULL) {
403     return EFI_UNSUPPORTED;
404   }
405 
406   //
407   // Make sure this driver produced ChildHandle
408   //
409   Status = EfiTestChildHandle (
410              ControllerHandle,
411              ChildHandle,
412              &gEfiManagedNetworkProtocolGuid
413              );
414   if (EFI_ERROR (Status)) {
415     return Status;
416   }
417 
418   //
419   // Retrieve an instance of a produced protocol from ChildHandle
420   //
421   Status = gBS->OpenProtocol (
422                   ChildHandle,
423                   &gEfiIp6ProtocolGuid,
424                   (VOID **)&Ip6,
425                   NULL,
426                   NULL,
427                   EFI_OPEN_PROTOCOL_GET_PROTOCOL
428                   );
429   if (EFI_ERROR (Status)) {
430     return Status;
431   }
432 
433   //
434   // Update the component name for this child handle.
435   //
436   Status = UpdateName (Ip6);
437   if (EFI_ERROR (Status)) {
438     return Status;
439   }
440 
441   return LookupUnicodeString2 (
442            Language,
443            This->SupportedLanguages,
444            gIp6ControllerNameTable,
445            ControllerName,
446            (BOOLEAN)(This == &gIp6ComponentName)
447            );
448 }
449