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