1 /** @file
2   Implementation of protocols EFI_COMPONENT_NAME_PROTOCOL and
3   EFI_COMPONENT_NAME2_PROTOCOL.
4 
5   Copyright (c) 2009 - 2015, 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 "TcpMain.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 or DriverName is NULL.
55 
56   @retval EFI_UNSUPPORTED       The driver specified by This does not support
57                                 the language specified by Language.
58 
59 **/
60 EFI_STATUS
61 EFIAPI
62 TcpComponentNameGetDriverName (
63   IN  EFI_COMPONENT_NAME_PROTOCOL  *This,
64   IN  CHAR8                        *Language,
65   OUT CHAR16                       **DriverName
66   );
67 
68 /**
69   Retrieves a Unicode string that is the user-readable name of the controller
70   that is being managed by a driver.
71 
72   This function retrieves the user-readable name of the controller specified by
73   ControllerHandle and ChildHandle in the form of a Unicode string. If the
74   driver specified by This has a user-readable name in the language specified by
75   Language, then a pointer to the controller name is returned in ControllerName,
76   and EFI_SUCCESS is returned.  If the driver specified by This is not currently
77   managing the controller specified by ControllerHandle and ChildHandle,
78   then EFI_UNSUPPORTED is returned.  If the driver specified by This does not
79   support the language specified by Language, then EFI_UNSUPPORTED is returned.
80 
81   @param[in]  This              A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
82                                 EFI_COMPONENT_NAME_PROTOCOL instance.
83 
84   @param[in]  ControllerHandle  The handle of a controller that the driver
85                                 specified by This is managing.  This handle
86                                 specifies the controller whose name is to be
87                                 returned.
88 
89   @param[in]  ChildHandle       The handle of the child controller to retrieve
90                                 the name of.  This is an optional parameter that
91                                 may be NULL.  It will be NULL for device
92                                 drivers.  It will also be NULL for a bus drivers
93                                 that wish to retrieve the name of the bus
94                                 controller.  It will not be NULL for a bus
95                                 driver that wishes to retrieve the name of a
96                                 child controller.
97 
98   @param[in]  Language          A pointer to a Null-terminated ASCII string
99                                 array indicating the language.  This is the
100                                 language of the driver name that the caller is
101                                 requesting, and it must match one of the
102                                 languages specified in SupportedLanguages. The
103                                 number of languages supported by a driver is up
104                                 to the driver writer. Language is specified in
105                                 RFC 4646 or ISO 639-2 language code format.
106 
107   @param[out]  ControllerName   A pointer to the Unicode string to return.
108                                 This Unicode string is the name of the
109                                 controller specified by ControllerHandle and
110                                 ChildHandle in the language specified by
111                                 Language, from the point of view of the driver
112                                 specified by This.
113 
114   @retval EFI_SUCCESS           The Unicode string for the user-readable name in
115                                 the language specified by Language for the
116                                 driver specified by This was returned in
117                                 DriverName.
118 
119   @retval EFI_INVALID_PARAMETER ControllerHandle is NULL.
120 
121   @retval EFI_INVALID_PARAMETER ChildHandle is not NULL, and it is not a valid
122                                 EFI_HANDLE.
123 
124   @retval EFI_INVALID_PARAMETER Language or ControllerName is NULL.
125 
126   @retval EFI_UNSUPPORTED       The driver specified by This is not currently
127                                 managing the controller specified by
128                                 ControllerHandle and ChildHandle.
129 
130   @retval EFI_UNSUPPORTED       The driver specified by This does not support
131                                 the language specified by Language.
132 
133 **/
134 EFI_STATUS
135 EFIAPI
136 TcpComponentNameGetControllerName (
137   IN  EFI_COMPONENT_NAME_PROTOCOL  *This,
138   IN  EFI_HANDLE                   ControllerHandle,
139   IN  EFI_HANDLE                   ChildHandle  OPTIONAL,
140   IN  CHAR8                        *Language,
141   OUT CHAR16                       **ControllerName
142   );
143 
144 ///
145 /// EFI Component Name Protocol
146 ///
147 GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME_PROTOCOL     gTcpComponentName = {
148   TcpComponentNameGetDriverName,
149   TcpComponentNameGetControllerName,
150   "eng"
151 };
152 
153 ///
154 /// EFI Component Name 2 Protocol
155 ///
156 GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME2_PROTOCOL    gTcpComponentName2 = {
157   (EFI_COMPONENT_NAME2_GET_DRIVER_NAME) TcpComponentNameGetDriverName,
158   (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME) TcpComponentNameGetControllerName,
159   "en"
160 };
161 
162 GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE        mTcpDriverNameTable[] = {
163   {
164     "eng;en",
165     L"TCP Network Service Driver"
166   },
167   {
168     NULL,
169     NULL
170   }
171 };
172 
173 GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE        *gTcpControllerNameTable = NULL;
174 
175 /**
176   Retrieves a Unicode string that is the user-readable name of the driver.
177 
178   This function retrieves the user-readable name of a driver in the form of a
179   Unicode string. If the driver specified by This has a user-readable name in
180   the language specified by Language, then a pointer to the driver name is
181   returned in DriverName, and EFI_SUCCESS is returned. If the driver specified
182   by This does not support the language specified by Language,
183   then EFI_UNSUPPORTED is returned.
184 
185   @param[in]  This              A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
186                                 EFI_COMPONENT_NAME_PROTOCOL instance.
187 
188   @param[in]  Language          A pointer to a Null-terminated ASCII string
189                                 array indicating the language. This is the
190                                 language of the driver name that the caller is
191                                 requesting, and it must match one of the
192                                 languages specified in SupportedLanguages. The
193                                 number of languages supported by a driver is up
194                                 to the driver writer. Language is specified
195                                 in RFC 4646 or ISO 639-2 language code format.
196 
197   @param[out]  DriverName       A pointer to the Unicode string to return.
198                                 This Unicode string is the name of the
199                                 driver specified by This in the language
200                                 specified by Language.
201 
202   @retval EFI_SUCCESS           The Unicode string for the Driver specified by
203                                 This, and the language specified by Language was
204                                 returned in DriverName.
205 
206   @retval EFI_INVALID_PARAMETER Language or DriverName is NULL.
207 
208   @retval EFI_UNSUPPORTED       The driver specified by This does not support
209                                 the language specified by Language.
210 
211 **/
212 EFI_STATUS
213 EFIAPI
TcpComponentNameGetDriverName(IN EFI_COMPONENT_NAME_PROTOCOL * This,IN CHAR8 * Language,OUT CHAR16 ** DriverName)214 TcpComponentNameGetDriverName (
215   IN  EFI_COMPONENT_NAME_PROTOCOL  *This,
216   IN  CHAR8                        *Language,
217   OUT CHAR16                       **DriverName
218   )
219 {
220   return LookupUnicodeString2 (
221            Language,
222            This->SupportedLanguages,
223            mTcpDriverNameTable,
224            DriverName,
225            (BOOLEAN) (This == &gTcpComponentName)
226            );
227 }
228 
229 /**
230   Update the component name for the Tcp4 child handle.
231 
232   @param  Tcp4[in]                   A pointer to the EFI_TCP4_PROTOCOL.
233 
234 
235   @retval EFI_SUCCESS                Update the ControllerNameTable of this instance successfully.
236   @retval EFI_INVALID_PARAMETER      The input parameter is invalid.
237 
238 **/
239 EFI_STATUS
UpdateTcp4Name(IN EFI_TCP4_PROTOCOL * Tcp4)240 UpdateTcp4Name (
241   IN    EFI_TCP4_PROTOCOL             *Tcp4
242   )
243 {
244   EFI_STATUS                       Status;
245   CHAR16                           HandleName[80];
246   EFI_TCP4_CONFIG_DATA             Tcp4ConfigData;
247 
248   if (Tcp4 == NULL) {
249     return EFI_INVALID_PARAMETER;
250   }
251 
252   //
253   // Format the child name into the string buffer as:
254   // TCPv4 (SrcPort=59, DestPort=60, ActiveFlag=TRUE)
255   //
256   ZeroMem (&Tcp4ConfigData, sizeof (Tcp4ConfigData));
257   Status = Tcp4->GetModeData (Tcp4, NULL, &Tcp4ConfigData, NULL, NULL, NULL);
258   if (!EFI_ERROR (Status)) {
259     UnicodeSPrint (HandleName, sizeof (HandleName),
260       L"TCPv4 (SrcPort=%d, DestPort=%d, ActiveFlag=%s)",
261       Tcp4ConfigData.AccessPoint.StationPort,
262       Tcp4ConfigData.AccessPoint.RemotePort,
263       (Tcp4ConfigData.AccessPoint.ActiveFlag ? L"TRUE" : L"FALSE")
264       );
265   } else if (Status == EFI_NOT_STARTED) {
266     UnicodeSPrint (
267       HandleName,
268       sizeof (HandleName),
269       L"TCPv4 (Not started)"
270       );
271   } else {
272     return Status;
273   }
274 
275   if (gTcpControllerNameTable != NULL) {
276     FreeUnicodeStringTable (gTcpControllerNameTable);
277     gTcpControllerNameTable = NULL;
278   }
279 
280   Status = AddUnicodeString2 (
281              "eng",
282              gTcpComponentName.SupportedLanguages,
283              &gTcpControllerNameTable,
284              HandleName,
285              TRUE
286              );
287   if (EFI_ERROR (Status)) {
288     return Status;
289   }
290 
291   return AddUnicodeString2 (
292            "en",
293            gTcpComponentName2.SupportedLanguages,
294            &gTcpControllerNameTable,
295            HandleName,
296            FALSE
297            );
298 }
299 
300 /**
301   Update the component name for the Tcp6 child handle.
302 
303   @param  Tcp6[in]                   A pointer to the EFI_TCP6_PROTOCOL.
304 
305 
306   @retval EFI_SUCCESS                Update the ControllerNameTable of this instance successfully.
307   @retval EFI_INVALID_PARAMETER      The input parameter is invalid.
308 
309 **/
310 EFI_STATUS
UpdateTcp6Name(IN EFI_TCP6_PROTOCOL * Tcp6)311 UpdateTcp6Name (
312   IN    EFI_TCP6_PROTOCOL             *Tcp6
313   )
314 {
315   EFI_STATUS                       Status;
316   CHAR16                           HandleName[80];
317   EFI_TCP6_CONFIG_DATA             Tcp6ConfigData;
318 
319   if (Tcp6 == NULL) {
320     return EFI_INVALID_PARAMETER;
321   }
322 
323   //
324   // Format the child name into the string buffer.
325   //
326   ZeroMem (&Tcp6ConfigData, sizeof (Tcp6ConfigData));
327   Status = Tcp6->GetModeData (Tcp6, NULL, &Tcp6ConfigData, NULL, NULL, NULL);
328   if (!EFI_ERROR (Status)) {
329     UnicodeSPrint (HandleName, sizeof (HandleName),
330       L"TCPv6(SrcPort=%d, DestPort=%d, ActiveFlag=%d)",
331       Tcp6ConfigData.AccessPoint.StationPort,
332       Tcp6ConfigData.AccessPoint.RemotePort,
333       Tcp6ConfigData.AccessPoint.ActiveFlag
334       );
335   } else if (Status == EFI_NOT_STARTED) {
336     UnicodeSPrint (HandleName, sizeof (HandleName), L"TCPv6(Not started)");
337   } else {
338     return Status;
339   }
340 
341 
342   if (gTcpControllerNameTable != NULL) {
343     FreeUnicodeStringTable (gTcpControllerNameTable);
344     gTcpControllerNameTable = NULL;
345   }
346 
347   Status = AddUnicodeString2 (
348              "eng",
349              gTcpComponentName.SupportedLanguages,
350              &gTcpControllerNameTable,
351              HandleName,
352              TRUE
353              );
354   if (EFI_ERROR (Status)) {
355     return Status;
356   }
357 
358   return AddUnicodeString2 (
359            "en",
360            gTcpComponentName2.SupportedLanguages,
361            &gTcpControllerNameTable,
362            HandleName,
363            FALSE
364            );
365 }
366 
367 /**
368   Retrieves a Unicode string that is the user-readable name of the controller
369   that is being managed by a driver.
370 
371   This function retrieves the user-readable name of the controller specified by
372   ControllerHandle and ChildHandle in the form of a Unicode string. If the
373   driver specified by This has a user-readable name in the language specified by
374   Language, then a pointer to the controller name is returned in ControllerName,
375   and EFI_SUCCESS is returned.  If the driver specified by This is not currently
376   managing the controller specified by ControllerHandle and ChildHandle,
377   then EFI_UNSUPPORTED is returned.  If the driver specified by This does not
378   support the language specified by Language, then EFI_UNSUPPORTED is returned.
379 
380   @param[in]  This              A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
381                                 EFI_COMPONENT_NAME_PROTOCOL instance.
382 
383   @param[in]  ControllerHandle  The handle of a controller that the driver
384                                 specified by This is managing.  This handle
385                                 specifies the controller whose name is to be
386                                 returned.
387 
388   @param[in]  ChildHandle       The handle of the child controller to retrieve
389                                 the name of.  This is an optional parameter that
390                                 may be NULL.  It will be NULL for device
391                                 drivers.  It will also be NULL for a bus drivers
392                                 that wish to retrieve the name of the bus
393                                 controller.  It will not be NULL for a bus
394                                 driver that wishes to retrieve the name of a
395                                 child controller.
396 
397   @param[in]  Language          A pointer to a Null-terminated ASCII string
398                                 array indicating the language.  This is the
399                                 language of the driver name that the caller is
400                                 requesting, and it must match one of the
401                                 languages specified in SupportedLanguages. The
402                                 number of languages supported by a driver is up
403                                 to the driver writer. Language is specified in
404                                 RFC 4646 or ISO 639-2 language code format.
405 
406   @param[out]  ControllerName   A pointer to the Unicode string to return.
407                                 This Unicode string is the name of the
408                                 controller specified by ControllerHandle and
409                                 ChildHandle in the language specified by
410                                 Language, from the point of view of the driver
411                                 specified by This.
412 
413   @retval EFI_SUCCESS           The Unicode string for the user-readable name in
414                                 the language specified by Language for the
415                                 driver specified by This was returned in
416                                 DriverName.
417 
418   @retval EFI_INVALID_PARAMETER ControllerHandle is NULL.
419 
420   @retval EFI_INVALID_PARAMETER ChildHandle is not NULL, and it is not a valid
421                                 EFI_HANDLE.
422 
423   @retval EFI_INVALID_PARAMETER Language or ControllerName is NULL.
424 
425   @retval EFI_UNSUPPORTED       The driver specified by This is not currently
426                                 managing the controller specified by
427                                 ControllerHandle and ChildHandle.
428 
429   @retval EFI_UNSUPPORTED       The driver specified by This does not support
430                                 the language specified by Language.
431 
432 **/
433 EFI_STATUS
434 EFIAPI
TcpComponentNameGetControllerName(IN EFI_COMPONENT_NAME_PROTOCOL * This,IN EFI_HANDLE ControllerHandle,IN EFI_HANDLE ChildHandle OPTIONAL,IN CHAR8 * Language,OUT CHAR16 ** ControllerName)435 TcpComponentNameGetControllerName (
436   IN  EFI_COMPONENT_NAME_PROTOCOL  *This,
437   IN  EFI_HANDLE                   ControllerHandle,
438   IN  EFI_HANDLE                   ChildHandle  OPTIONAL,
439   IN  CHAR8                        *Language,
440   OUT CHAR16                       **ControllerName
441   )
442 {
443   EFI_STATUS                    Status;
444   EFI_TCP4_PROTOCOL             *Tcp4;
445   EFI_TCP6_PROTOCOL             *Tcp6;
446 
447   //
448   // Only provide names for child handles.
449   //
450   if (ChildHandle == NULL) {
451     return EFI_UNSUPPORTED;
452   }
453 
454   //
455   // Make sure this driver produced ChildHandle
456   //
457   Status = EfiTestChildHandle (
458              ControllerHandle,
459              ChildHandle,
460              &gEfiIp6ProtocolGuid
461              );
462   if (!EFI_ERROR (Status)) {
463     //
464     // Retrieve an instance of a produced protocol from ChildHandle
465     //
466     Status = gBS->OpenProtocol (
467                     ChildHandle,
468                     &gEfiTcp6ProtocolGuid,
469                    (VOID **)&Tcp6,
470                     NULL,
471                     NULL,
472                     EFI_OPEN_PROTOCOL_GET_PROTOCOL
473                     );
474     if (EFI_ERROR (Status)) {
475       return Status;
476     }
477 
478     //
479     // Update the component name for this child handle.
480     //
481     Status = UpdateTcp6Name (Tcp6);
482     if (EFI_ERROR (Status)) {
483       return Status;
484     }
485   }
486 
487   //
488   // Make sure this driver is currently managing ControllHandle
489   //
490   Status = EfiTestChildHandle (
491              ControllerHandle,
492              ChildHandle,
493              &gEfiIp4ProtocolGuid
494              );
495   if (!EFI_ERROR (Status)) {
496     //
497     // Retrieve an instance of a produced protocol from ChildHandle
498     //
499     Status = gBS->OpenProtocol (
500                ChildHandle,
501                &gEfiTcp4ProtocolGuid,
502               (VOID **)&Tcp4,
503                NULL,
504                NULL,
505                EFI_OPEN_PROTOCOL_GET_PROTOCOL
506                );
507     if (EFI_ERROR (Status)) {
508       return Status;
509     }
510 
511     //
512     // Update the component name for this child handle.
513     //
514     Status = UpdateTcp4Name (Tcp4);
515     if (EFI_ERROR (Status)) {
516       return Status;
517     }
518   }
519 
520   return LookupUnicodeString2 (
521            Language,
522            This->SupportedLanguages,
523            gTcpControllerNameTable,
524            ControllerName,
525            (BOOLEAN)(This == &gTcpComponentName)
526            );
527 }
528 
529