]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/VlanConfigDxe/VlanConfigImpl.c
refine the code and add more security check.
[mirror_edk2.git] / MdeModulePkg / Universal / Network / VlanConfigDxe / VlanConfigImpl.c
1 /** @file
2 HII Config Access protocol implementation of VLAN configuration module.
3
4 Copyright (c) 2009 - 2010, Intel Corporation.<BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions
7 of the BSD License which accompanies this distribution. The full
8 text of the license may be found at<BR>
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include "VlanConfigImpl.h"
17
18 EFI_GUID mVlanFormSetGuid = VLAN_CONFIG_PRIVATE_GUID;
19 CHAR16 mVlanStorageName[] = L"VlanNvData";
20 EFI_HII_CONFIG_ROUTING_PROTOCOL *mHiiConfigRouting = NULL;
21
22 VLAN_CONFIG_PRIVATE_DATA mVlanConfigPrivateDateTemplate = {
23 VLAN_CONFIG_PRIVATE_DATA_SIGNATURE,
24 {
25 VlanExtractConfig,
26 VlanRouteConfig,
27 VlanCallback
28 }
29 };
30
31 VENDOR_DEVICE_PATH mHiiVendorDevicePathNode = {
32 {
33 HARDWARE_DEVICE_PATH,
34 HW_VENDOR_DP,
35 {
36 (UINT8) (sizeof (VENDOR_DEVICE_PATH)),
37 (UINT8) ((sizeof (VENDOR_DEVICE_PATH)) >> 8)
38 }
39 },
40 VLAN_CONFIG_PRIVATE_GUID
41 };
42
43 /**
44 This function allows a caller to extract the current configuration for one
45 or more named elements from the target driver.
46
47 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
48 @param[in] Request A null-terminated Unicode string in
49 <ConfigRequest> format.
50 @param[out] Progress On return, points to a character in the Request
51 string. Points to the string's null terminator if
52 request was successful. Points to the most recent
53 '&' before the first failing name/value pair (or
54 the beginning of the string if the failure is in
55 the first name/value pair) if the request was not
56 successful.
57 @param[out] Results A null-terminated Unicode string in
58 <ConfigAltResp> format which has all values filled
59 in for the names in the Request string. String to
60 be allocated by the called function.
61
62 @retval EFI_SUCCESS The Results is filled with the requested values.
63 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the results.
64 @retval EFI_INVALID_PARAMETER Request is NULL, illegal syntax, or unknown name.
65 @retval EFI_NOT_FOUND Routing data doesn't match any storage in this
66 driver.
67
68 **/
69 EFI_STATUS
70 EFIAPI
71 VlanExtractConfig (
72 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,
73 IN CONST EFI_STRING Request,
74 OUT EFI_STRING *Progress,
75 OUT EFI_STRING *Results
76 )
77 {
78 EFI_STATUS Status;
79 UINTN BufferSize;
80 VLAN_CONFIGURATION Configuration;
81
82 if (Request == NULL) {
83 return EFI_INVALID_PARAMETER;
84 }
85
86 //
87 // Retrieve the pointer to the UEFI HII Config Routing Protocol
88 //
89 if (mHiiConfigRouting == NULL) {
90 gBS->LocateProtocol (&gEfiHiiConfigRoutingProtocolGuid, NULL, (VOID **) &mHiiConfigRouting);
91 }
92 ASSERT (mHiiConfigRouting != NULL);
93
94 //
95 // Convert buffer data to <ConfigResp> by helper function BlockToConfig()
96 //
97 ZeroMem (&Configuration, sizeof (VLAN_CONFIGURATION));
98 BufferSize = sizeof (VLAN_CONFIG_PRIVATE_DATA);
99 Status = mHiiConfigRouting->BlockToConfig (
100 mHiiConfigRouting,
101 Request,
102 (UINT8 *) &Configuration,
103 BufferSize,
104 Results,
105 Progress
106 );
107 return Status;
108 }
109
110
111 /**
112 This function processes the results of changes in configuration.
113
114 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
115 @param[in] Configuration A null-terminated Unicode string in <ConfigResp>
116 format.
117 @param[out] Progress A pointer to a string filled in with the offset of
118 the most recent '&' before the first failing
119 name/value pair (or the beginning of the string if
120 the failure is in the first name/value pair) or
121 the terminating NULL if all was successful.
122
123 @retval EFI_SUCCESS The Results is processed successfully.
124 @retval EFI_INVALID_PARAMETER Configuration is NULL.
125 @retval EFI_NOT_FOUND Routing data doesn't match any storage in this
126 driver.
127
128 **/
129 EFI_STATUS
130 EFIAPI
131 VlanRouteConfig (
132 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,
133 IN CONST EFI_STRING Configuration,
134 OUT EFI_STRING *Progress
135 )
136 {
137 if (Configuration == NULL || Progress == NULL) {
138 return EFI_INVALID_PARAMETER;
139 }
140
141 *Progress = Configuration;
142 if (!HiiIsConfigHdrMatch (Configuration, &mVlanFormSetGuid, mVlanStorageName)) {
143 return EFI_NOT_FOUND;
144 }
145
146 *Progress = Configuration + StrLen (Configuration);
147 return EFI_SUCCESS;
148 }
149
150 /**
151 This function processes the results of changes in configuration.
152
153 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
154 @param[in] Action Specifies the type of action taken by the browser.
155 @param[in] QuestionId A unique value which is sent to the original
156 exporting driver so that it can identify the type
157 of data to expect.
158 @param[in] Type The type of value for the question.
159 @param[in] Value A pointer to the data being sent to the original
160 exporting driver.
161 @param[out] ActionRequest On return, points to the action requested by the
162 callback function.
163
164 @retval EFI_SUCCESS The callback successfully handled the action.
165 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the
166 variable and its data.
167 @retval EFI_DEVICE_ERROR The variable could not be saved.
168 @retval EFI_UNSUPPORTED The specified Action is not supported by the
169 callback.
170
171 **/
172 EFI_STATUS
173 EFIAPI
174 VlanCallback (
175 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,
176 IN EFI_BROWSER_ACTION Action,
177 IN EFI_QUESTION_ID QuestionId,
178 IN UINT8 Type,
179 IN EFI_IFR_TYPE_VALUE *Value,
180 OUT EFI_BROWSER_ACTION_REQUEST *ActionRequest
181 )
182 {
183 VLAN_CONFIG_PRIVATE_DATA *PrivateData;
184 VLAN_CONFIGURATION *Configuration;
185 EFI_VLAN_CONFIG_PROTOCOL *VlanConfig;
186 UINTN Index;
187 EFI_HANDLE VlanHandle;
188
189 PrivateData = VLAN_CONFIG_PRIVATE_DATA_FROM_THIS (This);
190
191 if (Action == EFI_BROWSER_ACTION_FORM_OPEN) {
192 //
193 // On FORM_OPEN event, update current VLAN list
194 //
195 VlanUpdateForm (PrivateData);
196
197 return EFI_SUCCESS;
198 }
199
200 //
201 // Get Browser data
202 //
203 Configuration = AllocateZeroPool (sizeof (VLAN_CONFIGURATION));
204 ASSERT (Configuration != NULL);
205 HiiGetBrowserData (&mVlanFormSetGuid, mVlanStorageName, sizeof (VLAN_CONFIGURATION), (UINT8 *) Configuration);
206
207 VlanConfig = PrivateData->VlanConfig;
208
209 switch (QuestionId) {
210 case VLAN_ADD_QUESTION_ID:
211 //
212 // Add a VLAN
213 //
214 VlanConfig->Set (VlanConfig, Configuration->VlanId, Configuration->Priority);
215 VlanUpdateForm (PrivateData);
216
217 //
218 // Connect the newly created VLAN device
219 //
220 VlanHandle = NetLibGetVlanHandle (PrivateData->ControllerHandle, Configuration->VlanId);
221 if (VlanHandle == NULL) {
222 //
223 // There may be no child handle created for VLAN ID 0, connect the parent handle
224 //
225 VlanHandle = PrivateData->ControllerHandle;
226 }
227 gBS->ConnectController (VlanHandle, NULL, NULL, TRUE);
228
229 //
230 // Clear UI data
231 //
232 *ActionRequest = EFI_BROWSER_ACTION_REQUEST_SUBMIT;
233 Configuration->VlanId = 0;
234 Configuration->Priority = 0;
235 break;
236
237 case VLAN_REMOVE_QUESTION_ID:
238 //
239 // Remove VLAN
240 //
241 ASSERT (PrivateData->NumberOfVlan <= MAX_VLAN_NUMBER);
242 for (Index = 0; Index < PrivateData->NumberOfVlan; Index++) {
243 if (Configuration->VlanList[Index] != 0) {
244 //
245 // Checkbox is selected, need remove this VLAN
246 //
247 VlanConfig->Remove (VlanConfig, PrivateData->VlanId[Index]);
248 }
249 }
250
251 VlanUpdateForm (PrivateData);
252 if (PrivateData->NumberOfVlan == 0) {
253 //
254 // No VLAN device now, connect the physical NIC handle.
255 // Note: PrivateData->NumberOfVlan has been updated by VlanUpdateForm()
256 //
257 gBS->ConnectController (PrivateData->ControllerHandle, NULL, NULL, TRUE);
258 }
259
260 *ActionRequest = EFI_BROWSER_ACTION_REQUEST_SUBMIT;
261 ZeroMem (Configuration->VlanList, MAX_VLAN_NUMBER);
262 break;
263
264 default:
265 break;
266 }
267
268 HiiSetBrowserData (&mVlanFormSetGuid, mVlanStorageName, sizeof (VLAN_CONFIGURATION), (UINT8 *) Configuration, NULL);
269 FreePool (Configuration);
270 return EFI_SUCCESS;
271 }
272
273
274 /**
275 This function update VLAN list in the VLAN configuration Form.
276
277 @param[in, out] PrivateData Points to VLAN configuration private data.
278
279 **/
280 VOID
281 VlanUpdateForm (
282 IN OUT VLAN_CONFIG_PRIVATE_DATA *PrivateData
283 )
284 {
285 EFI_VLAN_CONFIG_PROTOCOL *VlanConfig;
286 UINT16 NumberOfVlan;
287 UINTN Index;
288 EFI_VLAN_FIND_DATA *VlanData;
289 VOID *StartOpCodeHandle;
290 EFI_IFR_GUID_LABEL *StartLabel;
291 VOID *EndOpCodeHandle;
292 EFI_IFR_GUID_LABEL *EndLabel;
293 CHAR16 *String;
294 CHAR16 VlanStr[30];
295 CHAR16 VlanIdStr[6];
296 UINTN DigitalCount;
297 EFI_STRING_ID StringId;
298
299 //
300 // Find current VLAN configuration
301 //
302 VlanData = NULL;
303 NumberOfVlan = 0;
304 VlanConfig = PrivateData->VlanConfig;
305 VlanConfig->Find (VlanConfig, NULL, &NumberOfVlan, &VlanData);
306
307 //
308 // Update VLAN configuration in PrivateData
309 //
310 if (NumberOfVlan > MAX_VLAN_NUMBER) {
311 NumberOfVlan = MAX_VLAN_NUMBER;
312 }
313 PrivateData->NumberOfVlan = NumberOfVlan;
314
315 //
316 // Init OpCode Handle
317 //
318 StartOpCodeHandle = HiiAllocateOpCodeHandle ();
319 ASSERT (StartOpCodeHandle != NULL);
320
321 EndOpCodeHandle = HiiAllocateOpCodeHandle ();
322 ASSERT (EndOpCodeHandle != NULL);
323
324 //
325 // Create Hii Extend Label OpCode as the start opcode
326 //
327 StartLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode (
328 StartOpCodeHandle,
329 &gEfiIfrTianoGuid,
330 NULL,
331 sizeof (EFI_IFR_GUID_LABEL)
332 );
333 StartLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;
334 StartLabel->Number = LABEL_VLAN_LIST;
335
336 //
337 // Create Hii Extend Label OpCode as the end opcode
338 //
339 EndLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode (
340 EndOpCodeHandle,
341 &gEfiIfrTianoGuid,
342 NULL,
343 sizeof (EFI_IFR_GUID_LABEL)
344 );
345 EndLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;
346 EndLabel->Number = LABEL_END;
347
348 ZeroMem (PrivateData->VlanId, MAX_VLAN_NUMBER);
349 for (Index = 0; Index < NumberOfVlan; Index++) {
350 String = VlanStr;
351
352 StrCpy (String, L" VLAN ID:");
353 String += 10;
354 //
355 // Pad VlanId string up to 4 characters with space
356 //
357 DigitalCount = UnicodeValueToString (VlanIdStr, 0, VlanData[Index].VlanId, 5);
358 SetMem16 (String, (4 - DigitalCount) * sizeof (CHAR16), L' ');
359 StrCpy (String + 4 - DigitalCount, VlanIdStr);
360 String += 4;
361
362 StrCpy (String, L", Priority:");
363 String += 11;
364 String += UnicodeValueToString (String, 0, VlanData[Index].Priority, 4);
365 *String = 0;
366
367 StringId = HiiSetString (PrivateData->HiiHandle, 0, VlanStr, NULL);
368 ASSERT (StringId != 0);
369
370 HiiCreateCheckBoxOpCode (
371 StartOpCodeHandle,
372 (EFI_QUESTION_ID) (VLAN_LIST_VAR_OFFSET + Index),
373 VLAN_CONFIGURATION_VARSTORE_ID,
374 (UINT16) (VLAN_LIST_VAR_OFFSET + Index),
375 StringId,
376 STRING_TOKEN (STR_VLAN_VLAN_LIST_HELP),
377 0,
378 0,
379 NULL
380 );
381
382 //
383 // Save VLAN id to private data
384 //
385 PrivateData->VlanId[Index] = VlanData[Index].VlanId;
386 }
387
388 HiiUpdateForm (
389 PrivateData->HiiHandle, // HII handle
390 &mVlanFormSetGuid, // Formset GUID
391 VLAN_CONFIGURATION_FORM_ID, // Form ID
392 StartOpCodeHandle, // Label for where to insert opcodes
393 EndOpCodeHandle // Replace data
394 );
395
396 HiiFreeOpCodeHandle (StartOpCodeHandle);
397 HiiFreeOpCodeHandle (EndOpCodeHandle);
398
399 if (VlanData != NULL) {
400 FreePool (VlanData);
401 }
402 }
403
404
405 /**
406 This function publish the VLAN configuration Form for a network device. The
407 HII Config Access protocol will be installed on a child handle of the network
408 device.
409
410 @param[in, out] PrivateData Points to VLAN configuration private data.
411
412 @retval EFI_SUCCESS HII Form is installed for this network device.
413 @retval EFI_OUT_OF_RESOURCES Not enough resource for HII Form installation.
414 @retval Others Other errors as indicated.
415
416 **/
417 EFI_STATUS
418 InstallVlanConfigForm (
419 IN OUT VLAN_CONFIG_PRIVATE_DATA *PrivateData
420 )
421 {
422 EFI_STATUS Status;
423 EFI_HII_HANDLE HiiHandle;
424 EFI_HANDLE DriverHandle;
425 CHAR16 Str[26 + sizeof (EFI_MAC_ADDRESS) * 2 + 1];
426 CHAR16 *MacString;
427 EFI_DEVICE_PATH_PROTOCOL *ChildDevicePath;
428 EFI_HII_CONFIG_ACCESS_PROTOCOL *ConfigAccess;
429
430 //
431 // Create child handle and install HII Config Access Protocol
432 //
433 ChildDevicePath = AppendDevicePathNode (
434 PrivateData->ParentDevicePath,
435 (CONST EFI_DEVICE_PATH_PROTOCOL *) &mHiiVendorDevicePathNode
436 );
437 if (ChildDevicePath == NULL) {
438 return EFI_OUT_OF_RESOURCES;
439 }
440 PrivateData->ChildDevicePath = ChildDevicePath;
441
442 DriverHandle = NULL;
443 ConfigAccess = &PrivateData->ConfigAccess;
444 Status = gBS->InstallMultipleProtocolInterfaces (
445 &DriverHandle,
446 &gEfiDevicePathProtocolGuid,
447 ChildDevicePath,
448 &gEfiHiiConfigAccessProtocolGuid,
449 ConfigAccess,
450 NULL
451 );
452 if (EFI_ERROR (Status)) {
453 return Status;
454 }
455 PrivateData->DriverHandle = DriverHandle;
456
457 //
458 // Publish the HII package list
459 //
460 HiiHandle = HiiAddPackages (
461 &mVlanFormSetGuid,
462 DriverHandle,
463 VlanConfigDxeStrings,
464 VlanConfigBin,
465 NULL
466 );
467 if (HiiHandle == NULL) {
468 return EFI_OUT_OF_RESOURCES;
469 }
470 PrivateData->HiiHandle = HiiHandle;
471
472 //
473 // Update formset title
474 //
475 MacString = NULL;
476 Status = NetLibGetMacString (PrivateData->ControllerHandle, PrivateData->ImageHandle, &MacString);
477 if (EFI_ERROR (Status)) {
478 return Status;
479 }
480 PrivateData->MacString = MacString;
481
482 StrCpy (Str, L"VLAN Configuration (MAC:");
483 ASSERT (StrLen (MacString) <= (sizeof (EFI_MAC_ADDRESS) * 2));
484 StrCat (Str, MacString);
485 StrCat (Str, L")");
486 HiiSetString (
487 HiiHandle,
488 STRING_TOKEN (STR_VLAN_FORM_SET_TITLE),
489 Str,
490 NULL
491 );
492
493 //
494 // Update form title
495 //
496 HiiSetString (
497 HiiHandle,
498 STRING_TOKEN (STR_VLAN_FORM_TITLE),
499 Str,
500 NULL
501 );
502
503 return EFI_SUCCESS;
504 }
505
506 /**
507 This function remove the VLAN configuration Form for a network device. The
508 child handle for HII Config Access protocol will be destroyed.
509
510 @param[in, out] PrivateData Points to VLAN configuration private data.
511
512 **/
513 VOID
514 UninstallVlanConfigForm (
515 IN OUT VLAN_CONFIG_PRIVATE_DATA *PrivateData
516 )
517 {
518 //
519 // Free MAC string
520 //
521 if (PrivateData->MacString != NULL) {
522 FreePool (PrivateData->MacString);
523 PrivateData->MacString = NULL;
524 }
525
526 //
527 // Uninstall HII package list
528 //
529 if (PrivateData->HiiHandle != NULL) {
530 HiiRemovePackages (PrivateData->HiiHandle);
531 PrivateData->HiiHandle = NULL;
532 }
533
534 //
535 // Uninstall HII Config Access Protocol
536 //
537 if (PrivateData->DriverHandle != NULL) {
538 gBS->UninstallMultipleProtocolInterfaces (
539 PrivateData->DriverHandle,
540 &gEfiDevicePathProtocolGuid,
541 PrivateData->ChildDevicePath,
542 &gEfiHiiConfigAccessProtocolGuid,
543 &PrivateData->ConfigAccess,
544 NULL
545 );
546 PrivateData->DriverHandle = NULL;
547
548 if (PrivateData->ChildDevicePath != NULL) {
549 FreePool (PrivateData->ChildDevicePath);
550 PrivateData->ChildDevicePath = NULL;
551 }
552 }
553 }