1/** @file
2  UEFI Component Name(2) protocol implementation for iSCSI.
3
4Copyright (c) 2004 - 2015, Intel Corporation. All rights reserved.<BR>
5This program and the accompanying materials
6are licensed and made available under the terms and conditions of the BSD License
7which accompanies this distribution.  The full text of the license may be found at
8http://opensource.org/licenses/bsd-license.php
9
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13**/
14
15#include "IScsiImpl.h"
16
17//
18// EFI Component Name Protocol
19//
20GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME_PROTOCOL     gIScsiComponentName = {
21  IScsiComponentNameGetDriverName,
22  IScsiComponentNameGetControllerName,
23  "eng"
24};
25
26//
27// EFI Component Name 2 Protocol
28//
29GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME2_PROTOCOL    gIScsiComponentName2 = {
30  (EFI_COMPONENT_NAME2_GET_DRIVER_NAME) IScsiComponentNameGetDriverName,
31  (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME) IScsiComponentNameGetControllerName,
32  "en"
33};
34
35GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE mIScsiDriverNameTable[] = {
36  {"eng;en", L"iSCSI Driver"},
37  {NULL, NULL}
38};
39
40GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE  *mIScsiControllerNameTable = NULL;
41
42/**
43  Retrieves a Unicode string that is the user readable name of the EFI Driver.
44
45  This function retrieves the user readable name of a driver in the form of a
46  Unicode string. If the driver specified by This has a user readable name in
47  the language specified by Language, then a pointer to the driver name is
48  returned in DriverName, and EFI_SUCCESS is returned. If the driver specified
49  by This does not support the language specified by Language,
50  then EFI_UNSUPPORTED is returned.
51
52  @param[in]  This        A pointer to the EFI_COMPONENT_NAME_PROTOCOL instance.
53  @param[in]  Language    A pointer to a three character ISO 639-2 language identifier.
54                          This is the language of the driver name that that the caller
55                          is requesting, and it must match one of the languages specified
56                          in SupportedLanguages.  The number of languages supported by a
57                          driver is up to the driver writer.
58  @param[out]  DriverName A pointer to the Unicode string to return.  This Unicode string
59                          is the name of the driver specified by This in the language
60                          specified by Language.
61
62  @retval EFI_SUCCESS           The Unicode string for the Driver specified by This
63                                and the language specified by Language was returned
64                                in DriverName.
65  @retval EFI_INVALID_PARAMETER Language is NULL.
66  @retval EFI_INVALID_PARAMETER DriverName is NULL.
67  @retval EFI_UNSUPPORTED       The driver specified by This does not support the
68                                language specified by Language.
69**/
70EFI_STATUS
71EFIAPI
72IScsiComponentNameGetDriverName (
73  IN  EFI_COMPONENT_NAME_PROTOCOL   *This,
74  IN  CHAR8                         *Language,
75  OUT CHAR16                        **DriverName
76  )
77{
78  return LookupUnicodeString2 (
79          Language,
80          This->SupportedLanguages,
81          mIScsiDriverNameTable,
82          DriverName,
83          (BOOLEAN)(This == &gIScsiComponentName)
84          );
85}
86
87/**
88  Update the component name for the iSCSI instance.
89
90  @param[in]  IScsiExtScsiPassThru  A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
91
92  @retval EFI_SUCCESS               Update the ControllerNameTable of this instance successfully.
93  @retval EFI_INVALID_PARAMETER     The input parameter is invalid.
94  @retval EFI_UNSUPPORTED           Can't get the corresponding NIC info from the Controller handle.
95
96**/
97EFI_STATUS
98UpdateName (
99  IN   EFI_EXT_SCSI_PASS_THRU_PROTOCOL *IScsiExtScsiPassThru
100  )
101{
102  EFI_STATUS                       Status;
103  CHAR16                           HandleName[150];
104  ISCSI_DRIVER_DATA                *Private;
105  EFI_MAC_ADDRESS                  MacAddress;
106  UINTN                            HwAddressSize;
107  UINT16                           VlanId;
108  CHAR16                           MacString[70];
109
110  if (IScsiExtScsiPassThru == NULL) {
111    return EFI_INVALID_PARAMETER;
112  }
113
114  Private  = ISCSI_DRIVER_DATA_FROM_EXT_SCSI_PASS_THRU (IScsiExtScsiPassThru);
115
116  //
117  // Get the mac string, it's the name of various variable
118  //
119  Status = NetLibGetMacAddress (Private->Controller, &MacAddress, &HwAddressSize);
120  if (EFI_ERROR (Status)) {
121    return Status;
122  }
123  VlanId = NetLibGetVlanId (Private->Controller);
124  IScsiMacAddrToStr (&MacAddress, (UINT32) HwAddressSize, VlanId, MacString);
125
126  UnicodeSPrint (
127    HandleName,
128    sizeof (HandleName),
129    L"iSCSI IPv4 (MacString=%s)",
130    MacString
131  );
132
133  if (mIScsiControllerNameTable != NULL) {
134    FreeUnicodeStringTable (mIScsiControllerNameTable);
135    mIScsiControllerNameTable = NULL;
136  }
137
138  Status = AddUnicodeString2 (
139             "eng",
140             gIScsiComponentName.SupportedLanguages,
141             &mIScsiControllerNameTable,
142             HandleName,
143             TRUE
144             );
145  if (EFI_ERROR (Status)) {
146    return Status;
147  }
148
149  return AddUnicodeString2 (
150           "en",
151           gIScsiComponentName2.SupportedLanguages,
152           &mIScsiControllerNameTable,
153           HandleName,
154           FALSE
155           );
156}
157
158/**
159  Retrieves a Unicode string that is the user readable name of the controller
160  that is being managed by an EFI Driver.Currently not implemented.
161
162  @param[in]  This             A pointer to the EFI_COMPONENT_NAME_PROTOCOL instance.
163  @param[in]  ControllerHandle The handle of a controller that the driver specified by
164                               This is managing.  This handle specifies the controller
165                               whose name is to be returned.
166  @param[in]  ChildHandle      The handle of the child controller to retrieve the name
167                               of.  This is an optional parameter that may be NULL.  It
168                               will be NULL for device drivers.  It will also be NULL
169                               for a bus drivers that wish to retrieve the name of the
170                               bus controller.  It will not be NULL for a bus driver
171                               that wishes to retrieve the name of a child controller.
172  @param[in]  Language         A pointer to a three character ISO 639-2 language
173                               identifier.  This is the language of the controller name
174                               that that the caller is requesting, and it must match one
175                               of the languages specified in SupportedLanguages.  The
176                               number of languages supported by a driver is up to the
177                               driver writer.
178  @param[out]  ControllerName  A pointer to the Unicode string to return.  This Unicode
179                               string is the name of the controller specified by
180                               ControllerHandle and ChildHandle in the language specified
181                               by Language from the point of view of the driver specified
182                               by This.
183
184  @retval EFI_SUCCESS           The Unicode string for the user readable name in the
185                                language specified by Language for the driver
186                                specified by This was returned in DriverName.
187  @retval EFI_INVALID_PARAMETER ControllerHandle is NULL.
188  @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid EFI_HANDLE.
189  @retval EFI_INVALID_PARAMETER Language is NULL.
190  @retval EFI_INVALID_PARAMETER ControllerName is NULL.
191  @retval EFI_UNSUPPORTED       The driver specified by This is not currently managing
192                                the controller specified by ControllerHandle and
193                                ChildHandle.
194  @retval EFI_UNSUPPORTED       The driver specified by This does not support the
195                                language specified by Language.
196**/
197EFI_STATUS
198EFIAPI
199IScsiComponentNameGetControllerName (
200  IN  EFI_COMPONENT_NAME_PROTOCOL   *This,
201  IN  EFI_HANDLE                    ControllerHandle,
202  IN  EFI_HANDLE                    ChildHandle        OPTIONAL,
203  IN  CHAR8                         *Language,
204  OUT CHAR16                        **ControllerName
205  )
206{
207  EFI_STATUS                      Status;
208
209  EFI_HANDLE                      IScsiController;
210  ISCSI_PRIVATE_PROTOCOL          *IScsiIdentifier;
211
212  EFI_EXT_SCSI_PASS_THRU_PROTOCOL *IScsiExtScsiPassThru;
213
214  if (ControllerHandle == NULL) {
215    return EFI_UNSUPPORTED;
216  }
217
218  //
219  // Get the handle of the controller we are controling.
220  //
221  IScsiController = NetLibGetNicHandle (ControllerHandle, &gEfiTcp4ProtocolGuid);
222  if (IScsiController == NULL) {
223    return EFI_UNSUPPORTED;
224  }
225
226  Status = gBS->OpenProtocol (
227                  IScsiController,
228                  &gEfiCallerIdGuid,
229                  (VOID **)&IScsiIdentifier,
230                  NULL,
231                  NULL,
232                  EFI_OPEN_PROTOCOL_GET_PROTOCOL
233                  );
234  if (EFI_ERROR (Status)) {
235    return Status;
236  }
237
238  if (ChildHandle != NULL) {
239    //
240    // Make sure this driver produced ChildHandle
241    //
242    Status = EfiTestChildHandle (
243               ControllerHandle,
244               ChildHandle,
245               &gEfiTcp4ProtocolGuid
246               );
247    if (!EFI_ERROR (Status)) {
248      //
249      // Retrieve an instance of a produced protocol from ChildHandle
250      //
251      Status = gBS->OpenProtocol (
252                      ChildHandle,
253                      &gEfiExtScsiPassThruProtocolGuid,
254                     (VOID **)&IScsiExtScsiPassThru,
255                      NULL,
256                      NULL,
257                      EFI_OPEN_PROTOCOL_GET_PROTOCOL
258                      );
259      if (EFI_ERROR (Status)) {
260        return Status;
261      }
262
263      //
264      // Update the component name for this child handle.
265      //
266      Status = UpdateName (IScsiExtScsiPassThru);
267      if (EFI_ERROR (Status)) {
268        return Status;
269      }
270    } else {
271      return Status;
272    }
273  }
274
275  return LookupUnicodeString2 (
276           Language,
277           This->SupportedLanguages,
278           mIScsiControllerNameTable,
279           ControllerName,
280           (BOOLEAN)(This == &gIScsiComponentName)
281           );
282}
283
284