]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/SnpDxe/ComponentName.c
1. Fix buffer overflow bugs in SNP, MNP and IP6 driver.
[mirror_edk2.git] / MdeModulePkg / Universal / Network / SnpDxe / ComponentName.c
1 /** @file
2 UEFI Component Name(2) protocol implementation for SnpDxe driver.
3
4 Copyright (c) 2004 - 2012, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials are licensed
6 and made available under the terms and conditions of the BSD License which
7 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
16 #include "Snp.h"
17
18 //
19 // EFI Component Name Functions
20 //
21 /**
22 Retrieves a Unicode string that is the user readable name of the driver.
23
24 This function retrieves the user readable name of a driver in the form of a
25 Unicode string. If the driver specified by This has a user readable name in
26 the language specified by Language, then a pointer to the driver name is
27 returned in DriverName, and EFI_SUCCESS is returned. If the driver specified
28 by This does not support the language specified by Language,
29 then EFI_UNSUPPORTED is returned.
30
31 @param This[in] A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
32 EFI_COMPONENT_NAME_PROTOCOL instance.
33
34 @param Language[in] A pointer to a Null-terminated ASCII string
35 array indicating the language. This is the
36 language of the driver name that the caller is
37 requesting, and it must match one of the
38 languages specified in SupportedLanguages. The
39 number of languages supported by a driver is up
40 to the driver writer. Language is specified
41 in RFC 4646 or ISO 639-2 language code format.
42
43 @param DriverName[out] A pointer to the Unicode string to return.
44 This Unicode string is the name of the
45 driver specified by This in the language
46 specified by Language.
47
48 @retval EFI_SUCCESS The Unicode string for the Driver specified by
49 This and the language specified by Language was
50 returned in DriverName.
51
52 @retval EFI_INVALID_PARAMETER Language is NULL.
53
54 @retval EFI_INVALID_PARAMETER 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 SimpleNetworkComponentNameGetDriverName (
63 IN EFI_COMPONENT_NAME_PROTOCOL *This,
64 IN CHAR8 *Language,
65 OUT CHAR16 **DriverName
66 );
67
68
69 /**
70 Retrieves a Unicode string that is the user readable name of the controller
71 that is being managed by a driver.
72
73 This function retrieves the user readable name of the controller specified by
74 ControllerHandle and ChildHandle in the form of a Unicode string. If the
75 driver specified by This has a user readable name in the language specified by
76 Language, then a pointer to the controller name is returned in ControllerName,
77 and EFI_SUCCESS is returned. If the driver specified by This is not currently
78 managing the controller specified by ControllerHandle and ChildHandle,
79 then EFI_UNSUPPORTED is returned. If the driver specified by This does not
80 support the language specified by Language, then EFI_UNSUPPORTED is returned.
81
82 @param This[in] A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
83 EFI_COMPONENT_NAME_PROTOCOL instance.
84
85 @param ControllerHandle[in] The handle of a controller that the driver
86 specified by This is managing. This handle
87 specifies the controller whose name is to be
88 returned.
89
90 @param ChildHandle[in] The handle of the child controller to retrieve
91 the name of. This is an optional parameter that
92 may be NULL. It will be NULL for device
93 drivers. It will also be NULL for a bus drivers
94 that wish to retrieve the name of the bus
95 controller. It will not be NULL for a bus
96 driver that wishes to retrieve the name of a
97 child controller.
98
99 @param Language[in] A pointer to a Null-terminated ASCII string
100 array indicating the language. This is the
101 language of the driver name that the caller is
102 requesting, and it must match one of the
103 languages specified in SupportedLanguages. The
104 number of languages supported by a driver is up
105 to the driver writer. Language is specified in
106 RFC 4646 or ISO 639-2 language code format.
107
108 @param ControllerName[out] A pointer to the Unicode string to return.
109 This Unicode string is the name of the
110 controller specified by ControllerHandle and
111 ChildHandle in the language specified by
112 Language from the point of view of the driver
113 specified by This.
114
115 @retval EFI_SUCCESS The Unicode string for the user readable name in
116 the language specified by Language for the
117 driver specified by This was returned in
118 DriverName.
119
120 @retval EFI_INVALID_PARAMETER ControllerHandle is NULL.
121
122 @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid
123 EFI_HANDLE.
124
125 @retval EFI_INVALID_PARAMETER Language is NULL.
126
127 @retval EFI_INVALID_PARAMETER ControllerName is NULL.
128
129 @retval EFI_UNSUPPORTED The driver specified by This is not currently
130 managing the controller specified by
131 ControllerHandle and ChildHandle.
132
133 @retval EFI_UNSUPPORTED The driver specified by This does not support
134 the language specified by Language.
135
136 **/
137 EFI_STATUS
138 EFIAPI
139 SimpleNetworkComponentNameGetControllerName (
140 IN EFI_COMPONENT_NAME_PROTOCOL *This,
141 IN EFI_HANDLE ControllerHandle,
142 IN EFI_HANDLE ChildHandle OPTIONAL,
143 IN CHAR8 *Language,
144 OUT CHAR16 **ControllerName
145 );
146
147
148 //
149 // EFI Component Name Protocol
150 //
151 GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME_PROTOCOL gSimpleNetworkComponentName = {
152 SimpleNetworkComponentNameGetDriverName,
153 SimpleNetworkComponentNameGetControllerName,
154 "eng"
155 };
156
157 //
158 // EFI Component Name 2 Protocol
159 //
160 GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME2_PROTOCOL gSimpleNetworkComponentName2 = {
161 (EFI_COMPONENT_NAME2_GET_DRIVER_NAME) SimpleNetworkComponentNameGetDriverName,
162 (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME) SimpleNetworkComponentNameGetControllerName,
163 "en"
164 };
165
166
167 GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE mSimpleNetworkDriverNameTable[] = {
168 {
169 "eng;en",
170 L"Simple Network Protocol Driver"
171 },
172 {
173 NULL,
174 NULL
175 }
176 };
177
178 GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE *gSimpleNetworkControllerNameTable = NULL;
179
180 /**
181 Retrieves a Unicode string that is the user readable name of the driver.
182
183 This function retrieves the user readable name of a driver in the form of a
184 Unicode string. If the driver specified by This has a user readable name in
185 the language specified by Language, then a pointer to the driver name is
186 returned in DriverName, and EFI_SUCCESS is returned. If the driver specified
187 by This does not support the language specified by Language,
188 then EFI_UNSUPPORTED is returned.
189
190 @param This[in] A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
191 EFI_COMPONENT_NAME_PROTOCOL instance.
192
193 @param Language[in] A pointer to a Null-terminated ASCII string
194 array indicating the language. This is the
195 language of the driver name that the caller is
196 requesting, and it must match one of the
197 languages specified in SupportedLanguages. The
198 number of languages supported by a driver is up
199 to the driver writer. Language is specified
200 in RFC 4646 or ISO 639-2 language code format.
201
202 @param DriverName[out] A pointer to the Unicode string to return.
203 This Unicode string is the name of the
204 driver specified by This in the language
205 specified by Language.
206
207 @retval EFI_SUCCESS The Unicode string for the Driver specified by
208 This and the language specified by Language was
209 returned in DriverName.
210
211 @retval EFI_INVALID_PARAMETER Language is NULL.
212
213 @retval EFI_INVALID_PARAMETER DriverName is NULL.
214
215 @retval EFI_UNSUPPORTED The driver specified by This does not support
216 the language specified by Language.
217
218 **/
219 EFI_STATUS
220 EFIAPI
221 SimpleNetworkComponentNameGetDriverName (
222 IN EFI_COMPONENT_NAME_PROTOCOL *This,
223 IN CHAR8 *Language,
224 OUT CHAR16 **DriverName
225 )
226 {
227 return LookupUnicodeString2 (
228 Language,
229 This->SupportedLanguages,
230 mSimpleNetworkDriverNameTable,
231 DriverName,
232 (BOOLEAN)(This == &gSimpleNetworkComponentName)
233 );
234 }
235
236 /**
237 Update the component name for the Snp child handle.
238
239 @param Snp[in] A pointer to the EFI_SIMPLE_NETWORK_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
247 UpdateName (
248 IN EFI_SIMPLE_NETWORK_PROTOCOL *Snp
249 )
250 {
251 EFI_STATUS Status;
252 CHAR16 HandleName[80];
253 UINTN OffSet;
254 UINTN Index;
255
256 if (Snp == NULL) {
257 return EFI_INVALID_PARAMETER;
258 }
259
260 OffSet = 0;
261 OffSet += UnicodeSPrint (
262 HandleName,
263 sizeof (HandleName),
264 L"SNP (MAC="
265 );
266 for (Index = 0; Index < Snp->Mode->HwAddressSize; Index++) {
267 OffSet += UnicodeSPrint (
268 HandleName + OffSet,
269 sizeof (HandleName) - OffSet * sizeof (CHAR16),
270 L"%02X-",
271 Snp->Mode->CurrentAddress.Addr[Index]
272 );
273 }
274 //
275 // Remove the last '-'
276 //
277 OffSet--;
278 OffSet += UnicodeSPrint (
279 HandleName + OffSet,
280 sizeof (HandleName) - OffSet * sizeof (CHAR16),
281 L")"
282 );
283 if (gSimpleNetworkControllerNameTable != NULL) {
284 FreeUnicodeStringTable (gSimpleNetworkControllerNameTable);
285 gSimpleNetworkControllerNameTable = NULL;
286 }
287
288 Status = AddUnicodeString2 (
289 "eng",
290 gSimpleNetworkComponentName.SupportedLanguages,
291 &gSimpleNetworkControllerNameTable,
292 HandleName,
293 TRUE
294 );
295 if (EFI_ERROR (Status)) {
296 return Status;
297 }
298
299 return AddUnicodeString2 (
300 "en",
301 gSimpleNetworkComponentName2.SupportedLanguages,
302 &gSimpleNetworkControllerNameTable,
303 HandleName,
304 FALSE
305 );
306 }
307
308 /**
309 Retrieves a Unicode string that is the user readable name of the controller
310 that is being managed by a driver.
311
312 This function retrieves the user readable name of the controller specified by
313 ControllerHandle and ChildHandle in the form of a Unicode string. If the
314 driver specified by This has a user readable name in the language specified by
315 Language, then a pointer to the controller name is returned in ControllerName,
316 and EFI_SUCCESS is returned. If the driver specified by This is not currently
317 managing the controller specified by ControllerHandle and ChildHandle,
318 then EFI_UNSUPPORTED is returned. If the driver specified by This does not
319 support the language specified by Language, then EFI_UNSUPPORTED is returned.
320 Currently not implemented.
321
322 @param This[in] A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
323 EFI_COMPONENT_NAME_PROTOCOL instance.
324
325 @param ControllerHandle[in] The handle of a controller that the driver
326 specified by This is managing. This handle
327 specifies the controller whose name is to be
328 returned.
329
330 @param ChildHandle[in] The handle of the child controller to retrieve
331 the name of. This is an optional parameter that
332 may be NULL. It will be NULL for device
333 drivers. It will also be NULL for a bus drivers
334 that wish to retrieve the name of the bus
335 controller. It will not be NULL for a bus
336 driver that wishes to retrieve the name of a
337 child controller.
338
339 @param Language[in] A pointer to a Null-terminated ASCII string
340 array indicating the language. This is the
341 language of the driver name that the caller is
342 requesting, and it must match one of the
343 languages specified in SupportedLanguages. The
344 number of languages supported by a driver is up
345 to the driver writer. Language is specified in
346 RFC 4646 or ISO 639-2 language code format.
347
348 @param ControllerName[out] A pointer to the Unicode string to return.
349 This Unicode string is the name of the
350 controller specified by ControllerHandle and
351 ChildHandle in the language specified by
352 Language from the point of view of the driver
353 specified by This.
354
355 @retval EFI_SUCCESS The Unicode string for the user readable name in
356 the language specified by Language for the
357 driver specified by This was returned in
358 DriverName.
359
360 @retval EFI_INVALID_PARAMETER ControllerHandle is NULL.
361
362 @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid
363 EFI_HANDLE.
364
365 @retval EFI_INVALID_PARAMETER Language is NULL.
366
367 @retval EFI_INVALID_PARAMETER ControllerName is NULL.
368
369 @retval EFI_UNSUPPORTED The driver specified by This is not currently
370 managing the controller specified by
371 ControllerHandle and ChildHandle.
372
373 @retval EFI_UNSUPPORTED The driver specified by This does not support
374 the language specified by Language.
375
376 **/
377 EFI_STATUS
378 EFIAPI
379 SimpleNetworkComponentNameGetControllerName (
380 IN EFI_COMPONENT_NAME_PROTOCOL *This,
381 IN EFI_HANDLE ControllerHandle,
382 IN EFI_HANDLE ChildHandle OPTIONAL,
383 IN CHAR8 *Language,
384 OUT CHAR16 **ControllerName
385 )
386 {
387 EFI_STATUS Status;
388 EFI_SIMPLE_NETWORK_PROTOCOL *Snp;
389
390 if (ChildHandle != NULL) {
391 return EFI_UNSUPPORTED;
392 }
393
394 //
395 // Make sure this driver is currently managing ControllHandle
396 //
397 Status = EfiTestManagedDevice (
398 ControllerHandle,
399 gSimpleNetworkDriverBinding.DriverBindingHandle,
400 &gEfiSimpleNetworkProtocolGuid
401 );
402 if (EFI_ERROR (Status)) {
403 return Status;
404 }
405
406 //
407 // Retrieve an instance of a produced protocol from ControllerHandle
408 //
409 Status = gBS->OpenProtocol (
410 ControllerHandle,
411 &gEfiSimpleNetworkProtocolGuid,
412 (VOID **)&Snp,
413 NULL,
414 NULL,
415 EFI_OPEN_PROTOCOL_GET_PROTOCOL
416 );
417 if (EFI_ERROR (Status)) {
418 return Status;
419 }
420 //
421 // Update the component name for this child handle.
422 //
423 Status = UpdateName (Snp);
424 if (EFI_ERROR (Status)) {
425 return Status;
426 }
427
428 return LookupUnicodeString2 (
429 Language,
430 This->SupportedLanguages,
431 gSimpleNetworkControllerNameTable,
432 ControllerName,
433 (BOOLEAN)(This == &gSimpleNetworkComponentName)
434 );
435 }