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