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