]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/VlanConfigDxe/VlanConfigImpl.c
Refine some call back function to return EFI_UNSUPPORTED for all unsupported call...
[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 - 2011, Intel Corporation. All rights reserved.<BR>
5 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 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 VLAN_CONFIG_PRIVATE_DATA *PrivateData;
82 EFI_STRING ConfigRequestHdr;
83 EFI_STRING ConfigRequest;
84 BOOLEAN AllocatedRequest;
85 UINTN Size;
86
87 if (Progress == NULL || Results == NULL) {
88 return EFI_INVALID_PARAMETER;
89 }
90
91 *Progress = Request;
92 if ((Request != NULL) && !HiiIsConfigHdrMatch (Request, &mVlanFormSetGuid, mVlanStorageName)) {
93 return EFI_NOT_FOUND;
94 }
95
96 ConfigRequestHdr = NULL;
97 ConfigRequest = NULL;
98 AllocatedRequest = FALSE;
99 Size = 0;
100
101 //
102 // Retrieve the pointer to the UEFI HII Config Routing Protocol
103 //
104 if (mHiiConfigRouting == NULL) {
105 gBS->LocateProtocol (&gEfiHiiConfigRoutingProtocolGuid, NULL, (VOID **) &mHiiConfigRouting);
106 }
107 ASSERT (mHiiConfigRouting != NULL);
108
109 //
110 // Convert buffer data to <ConfigResp> by helper function BlockToConfig()
111 //
112 PrivateData = VLAN_CONFIG_PRIVATE_DATA_FROM_THIS (This);
113 ZeroMem (&Configuration, sizeof (VLAN_CONFIGURATION));
114 BufferSize = sizeof (Configuration);
115 ConfigRequest = Request;
116 if ((Request == NULL) || (StrStr (Request, L"OFFSET") == NULL)) {
117 //
118 // Request has no request element, construct full request string.
119 // Allocate and fill a buffer large enough to hold the <ConfigHdr> template
120 // followed by "&OFFSET=0&WIDTH=WWWWWWWWWWWWWWWW" followed by a Null-terminator
121 //
122 ConfigRequestHdr = HiiConstructConfigHdr (&mVlanFormSetGuid, mVlanStorageName, PrivateData->DriverHandle);
123 Size = (StrLen (ConfigRequestHdr) + 32 + 1) * sizeof (CHAR16);
124 ConfigRequest = AllocateZeroPool (Size);
125 ASSERT (ConfigRequest != NULL);
126 AllocatedRequest = TRUE;
127 UnicodeSPrint (ConfigRequest, Size, L"%s&OFFSET=0&WIDTH=%016LX", ConfigRequestHdr, (UINT64)BufferSize);
128 FreePool (ConfigRequestHdr);
129 }
130
131 Status = mHiiConfigRouting->BlockToConfig (
132 mHiiConfigRouting,
133 ConfigRequest,
134 (UINT8 *) &Configuration,
135 BufferSize,
136 Results,
137 Progress
138 );
139 //
140 // Free the allocated config request string.
141 //
142 if (AllocatedRequest) {
143 FreePool (ConfigRequest);
144 ConfigRequest = NULL;
145 }
146 //
147 // Set Progress string to the original request string.
148 //
149 if (Request == NULL) {
150 *Progress = NULL;
151 } else if (StrStr (Request, L"OFFSET") == NULL) {
152 *Progress = Request + StrLen (Request);
153 }
154
155 return Status;
156 }
157
158
159 /**
160 This function processes the results of changes in configuration.
161
162 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
163 @param[in] Configuration A null-terminated Unicode string in <ConfigResp>
164 format.
165 @param[out] Progress A pointer to a string filled in with the offset of
166 the most recent '&' before the first failing
167 name/value pair (or the beginning of the string if
168 the failure is in the first name/value pair) or
169 the terminating NULL if all was successful.
170
171 @retval EFI_SUCCESS The Results is processed successfully.
172 @retval EFI_INVALID_PARAMETER Configuration is NULL.
173 @retval EFI_NOT_FOUND Routing data doesn't match any storage in this
174 driver.
175
176 **/
177 EFI_STATUS
178 EFIAPI
179 VlanRouteConfig (
180 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,
181 IN CONST EFI_STRING Configuration,
182 OUT EFI_STRING *Progress
183 )
184 {
185 if (Configuration == NULL || Progress == NULL) {
186 return EFI_INVALID_PARAMETER;
187 }
188
189 *Progress = Configuration;
190 if (!HiiIsConfigHdrMatch (Configuration, &mVlanFormSetGuid, mVlanStorageName)) {
191 return EFI_NOT_FOUND;
192 }
193
194 *Progress = Configuration + StrLen (Configuration);
195 return EFI_SUCCESS;
196 }
197
198 /**
199 This function processes the results of changes in configuration.
200
201 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
202 @param[in] Action Specifies the type of action taken by the browser.
203 @param[in] QuestionId A unique value which is sent to the original
204 exporting driver so that it can identify the type
205 of data to expect.
206 @param[in] Type The type of value for the question.
207 @param[in] Value A pointer to the data being sent to the original
208 exporting driver.
209 @param[out] ActionRequest On return, points to the action requested by the
210 callback function.
211
212 @retval EFI_SUCCESS The callback successfully handled the action.
213 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the
214 variable and its data.
215 @retval EFI_DEVICE_ERROR The variable could not be saved.
216 @retval EFI_UNSUPPORTED The specified Action is not supported by the
217 callback.
218
219 **/
220 EFI_STATUS
221 EFIAPI
222 VlanCallback (
223 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,
224 IN EFI_BROWSER_ACTION Action,
225 IN EFI_QUESTION_ID QuestionId,
226 IN UINT8 Type,
227 IN EFI_IFR_TYPE_VALUE *Value,
228 OUT EFI_BROWSER_ACTION_REQUEST *ActionRequest
229 )
230 {
231 VLAN_CONFIG_PRIVATE_DATA *PrivateData;
232 VLAN_CONFIGURATION *Configuration;
233 EFI_VLAN_CONFIG_PROTOCOL *VlanConfig;
234 UINTN Index;
235 EFI_HANDLE VlanHandle;
236
237 PrivateData = VLAN_CONFIG_PRIVATE_DATA_FROM_THIS (This);
238
239 if (Action == EFI_BROWSER_ACTION_FORM_OPEN) {
240 if (QuestionId == VLAN_ADD_QUESTION_ID) {
241 //
242 // Update current VLAN list into Form when Form is opened.
243 // This will be done only in FORM_OPEN CallBack of question with VLAN_ADD_QUESTION_ID.
244 //
245 VlanUpdateForm (PrivateData);
246 }
247 return EFI_SUCCESS;
248 }
249
250 if (Action == EFI_BROWSER_ACTION_FORM_CLOSE) {
251 //
252 // Do nothing for UEFI FORM_CLOSE action
253 //
254 return EFI_SUCCESS;
255 }
256
257 if (Action == EFI_BROWSER_ACTION_CHANGING) {
258 //
259 // Get Browser data
260 //
261 Configuration = AllocateZeroPool (sizeof (VLAN_CONFIGURATION));
262 ASSERT (Configuration != NULL);
263 HiiGetBrowserData (&mVlanFormSetGuid, mVlanStorageName, sizeof (VLAN_CONFIGURATION), (UINT8 *) Configuration);
264
265 VlanConfig = PrivateData->VlanConfig;
266
267 switch (QuestionId) {
268 case VLAN_ADD_QUESTION_ID:
269 //
270 // Add a VLAN
271 //
272 VlanConfig->Set (VlanConfig, Configuration->VlanId, Configuration->Priority);
273 VlanUpdateForm (PrivateData);
274
275 //
276 // Connect the newly created VLAN device
277 //
278 VlanHandle = NetLibGetVlanHandle (PrivateData->ControllerHandle, Configuration->VlanId);
279 if (VlanHandle == NULL) {
280 //
281 // There may be no child handle created for VLAN ID 0, connect the parent handle
282 //
283 VlanHandle = PrivateData->ControllerHandle;
284 }
285 gBS->ConnectController (VlanHandle, NULL, NULL, TRUE);
286
287 //
288 // Clear UI data
289 //
290 *ActionRequest = EFI_BROWSER_ACTION_REQUEST_SUBMIT;
291 Configuration->VlanId = 0;
292 Configuration->Priority = 0;
293 break;
294
295 case VLAN_REMOVE_QUESTION_ID:
296 //
297 // Remove VLAN
298 //
299 ASSERT (PrivateData->NumberOfVlan <= MAX_VLAN_NUMBER);
300 for (Index = 0; Index < PrivateData->NumberOfVlan; Index++) {
301 if (Configuration->VlanList[Index] != 0) {
302 //
303 // Checkbox is selected, need remove this VLAN
304 //
305 VlanConfig->Remove (VlanConfig, PrivateData->VlanId[Index]);
306 }
307 }
308
309 VlanUpdateForm (PrivateData);
310 if (PrivateData->NumberOfVlan == 0) {
311 //
312 // No VLAN device now, connect the physical NIC handle.
313 // Note: PrivateData->NumberOfVlan has been updated by VlanUpdateForm()
314 //
315 gBS->ConnectController (PrivateData->ControllerHandle, NULL, NULL, TRUE);
316 }
317
318 *ActionRequest = EFI_BROWSER_ACTION_REQUEST_SUBMIT;
319 ZeroMem (Configuration->VlanList, MAX_VLAN_NUMBER);
320 break;
321
322 default:
323 break;
324 }
325
326 HiiSetBrowserData (&mVlanFormSetGuid, mVlanStorageName, sizeof (VLAN_CONFIGURATION), (UINT8 *) Configuration, NULL);
327 FreePool (Configuration);
328 return EFI_SUCCESS;
329 }
330
331 //
332 // All other action return unsupported.
333 //
334 return EFI_UNSUPPORTED;
335 }
336
337
338 /**
339 This function update VLAN list in the VLAN configuration Form.
340
341 @param[in, out] PrivateData Points to VLAN configuration private data.
342
343 **/
344 VOID
345 VlanUpdateForm (
346 IN OUT VLAN_CONFIG_PRIVATE_DATA *PrivateData
347 )
348 {
349 EFI_VLAN_CONFIG_PROTOCOL *VlanConfig;
350 UINT16 NumberOfVlan;
351 UINTN Index;
352 EFI_VLAN_FIND_DATA *VlanData;
353 VOID *StartOpCodeHandle;
354 EFI_IFR_GUID_LABEL *StartLabel;
355 VOID *EndOpCodeHandle;
356 EFI_IFR_GUID_LABEL *EndLabel;
357 CHAR16 *String;
358 CHAR16 VlanStr[30];
359 CHAR16 VlanIdStr[6];
360 UINTN DigitalCount;
361 EFI_STRING_ID StringId;
362
363 //
364 // Find current VLAN configuration
365 //
366 VlanData = NULL;
367 NumberOfVlan = 0;
368 VlanConfig = PrivateData->VlanConfig;
369 VlanConfig->Find (VlanConfig, NULL, &NumberOfVlan, &VlanData);
370
371 //
372 // Update VLAN configuration in PrivateData
373 //
374 if (NumberOfVlan > MAX_VLAN_NUMBER) {
375 NumberOfVlan = MAX_VLAN_NUMBER;
376 }
377 PrivateData->NumberOfVlan = NumberOfVlan;
378
379 //
380 // Init OpCode Handle
381 //
382 StartOpCodeHandle = HiiAllocateOpCodeHandle ();
383 ASSERT (StartOpCodeHandle != NULL);
384
385 EndOpCodeHandle = HiiAllocateOpCodeHandle ();
386 ASSERT (EndOpCodeHandle != NULL);
387
388 //
389 // Create Hii Extend Label OpCode as the start opcode
390 //
391 StartLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode (
392 StartOpCodeHandle,
393 &gEfiIfrTianoGuid,
394 NULL,
395 sizeof (EFI_IFR_GUID_LABEL)
396 );
397 StartLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;
398 StartLabel->Number = LABEL_VLAN_LIST;
399
400 //
401 // Create Hii Extend Label OpCode as the end opcode
402 //
403 EndLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode (
404 EndOpCodeHandle,
405 &gEfiIfrTianoGuid,
406 NULL,
407 sizeof (EFI_IFR_GUID_LABEL)
408 );
409 EndLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;
410 EndLabel->Number = LABEL_END;
411
412 ZeroMem (PrivateData->VlanId, MAX_VLAN_NUMBER);
413 for (Index = 0; Index < NumberOfVlan; Index++) {
414 String = VlanStr;
415
416 StrCpy (String, L" VLAN ID:");
417 String += 10;
418 //
419 // Pad VlanId string up to 4 characters with space
420 //
421 DigitalCount = UnicodeValueToString (VlanIdStr, 0, VlanData[Index].VlanId, 5);
422 SetMem16 (String, (4 - DigitalCount) * sizeof (CHAR16), L' ');
423 StrCpy (String + 4 - DigitalCount, VlanIdStr);
424 String += 4;
425
426 StrCpy (String, L", Priority:");
427 String += 11;
428 String += UnicodeValueToString (String, 0, VlanData[Index].Priority, 4);
429 *String = 0;
430
431 StringId = HiiSetString (PrivateData->HiiHandle, 0, VlanStr, NULL);
432 ASSERT (StringId != 0);
433
434 HiiCreateCheckBoxOpCode (
435 StartOpCodeHandle,
436 (EFI_QUESTION_ID) (VLAN_LIST_VAR_OFFSET + Index),
437 VLAN_CONFIGURATION_VARSTORE_ID,
438 (UINT16) (VLAN_LIST_VAR_OFFSET + Index),
439 StringId,
440 STRING_TOKEN (STR_VLAN_VLAN_LIST_HELP),
441 0,
442 0,
443 NULL
444 );
445
446 //
447 // Save VLAN id to private data
448 //
449 PrivateData->VlanId[Index] = VlanData[Index].VlanId;
450 }
451
452 HiiUpdateForm (
453 PrivateData->HiiHandle, // HII handle
454 &mVlanFormSetGuid, // Formset GUID
455 VLAN_CONFIGURATION_FORM_ID, // Form ID
456 StartOpCodeHandle, // Label for where to insert opcodes
457 EndOpCodeHandle // Replace data
458 );
459
460 HiiFreeOpCodeHandle (StartOpCodeHandle);
461 HiiFreeOpCodeHandle (EndOpCodeHandle);
462
463 if (VlanData != NULL) {
464 FreePool (VlanData);
465 }
466 }
467
468
469 /**
470 This function publish the VLAN configuration Form for a network device. The
471 HII Config Access protocol will be installed on a child handle of the network
472 device.
473
474 @param[in, out] PrivateData Points to VLAN configuration private data.
475
476 @retval EFI_SUCCESS HII Form is installed for this network device.
477 @retval EFI_OUT_OF_RESOURCES Not enough resource for HII Form installation.
478 @retval Others Other errors as indicated.
479
480 **/
481 EFI_STATUS
482 InstallVlanConfigForm (
483 IN OUT VLAN_CONFIG_PRIVATE_DATA *PrivateData
484 )
485 {
486 EFI_STATUS Status;
487 EFI_HII_HANDLE HiiHandle;
488 EFI_HANDLE DriverHandle;
489 CHAR16 Str[26 + sizeof (EFI_MAC_ADDRESS) * 2 + 1];
490 CHAR16 *MacString;
491 EFI_DEVICE_PATH_PROTOCOL *ChildDevicePath;
492 EFI_HII_CONFIG_ACCESS_PROTOCOL *ConfigAccess;
493
494 //
495 // Create child handle and install HII Config Access Protocol
496 //
497 ChildDevicePath = AppendDevicePathNode (
498 PrivateData->ParentDevicePath,
499 (CONST EFI_DEVICE_PATH_PROTOCOL *) &mHiiVendorDevicePathNode
500 );
501 if (ChildDevicePath == NULL) {
502 return EFI_OUT_OF_RESOURCES;
503 }
504 PrivateData->ChildDevicePath = ChildDevicePath;
505
506 DriverHandle = NULL;
507 ConfigAccess = &PrivateData->ConfigAccess;
508 Status = gBS->InstallMultipleProtocolInterfaces (
509 &DriverHandle,
510 &gEfiDevicePathProtocolGuid,
511 ChildDevicePath,
512 &gEfiHiiConfigAccessProtocolGuid,
513 ConfigAccess,
514 NULL
515 );
516 if (EFI_ERROR (Status)) {
517 return Status;
518 }
519 PrivateData->DriverHandle = DriverHandle;
520
521 //
522 // Publish the HII package list
523 //
524 HiiHandle = HiiAddPackages (
525 &mVlanFormSetGuid,
526 DriverHandle,
527 VlanConfigDxeStrings,
528 VlanConfigBin,
529 NULL
530 );
531 if (HiiHandle == NULL) {
532 return EFI_OUT_OF_RESOURCES;
533 }
534 PrivateData->HiiHandle = HiiHandle;
535
536 //
537 // Update formset title help string.
538 //
539 MacString = NULL;
540 Status = NetLibGetMacString (PrivateData->ControllerHandle, PrivateData->ImageHandle, &MacString);
541 if (EFI_ERROR (Status)) {
542 return Status;
543 }
544 PrivateData->MacString = MacString;
545
546 StrCpy (Str, L"VLAN Configuration (MAC:");
547 StrnCat (Str, MacString, sizeof (EFI_MAC_ADDRESS) * 2);
548 StrCat (Str, L")");
549 HiiSetString (
550 HiiHandle,
551 STRING_TOKEN (STR_VLAN_FORM_SET_TITLE_HELP),
552 Str,
553 NULL
554 );
555
556 //
557 // Update form title help string.
558 //
559 HiiSetString (
560 HiiHandle,
561 STRING_TOKEN (STR_VLAN_FORM_HELP),
562 Str,
563 NULL
564 );
565
566 return EFI_SUCCESS;
567 }
568
569 /**
570 This function remove the VLAN configuration Form for a network device. The
571 child handle for HII Config Access protocol will be destroyed.
572
573 @param[in, out] PrivateData Points to VLAN configuration private data.
574
575 **/
576 VOID
577 UninstallVlanConfigForm (
578 IN OUT VLAN_CONFIG_PRIVATE_DATA *PrivateData
579 )
580 {
581 //
582 // Free MAC string
583 //
584 if (PrivateData->MacString != NULL) {
585 FreePool (PrivateData->MacString);
586 PrivateData->MacString = NULL;
587 }
588
589 //
590 // Uninstall HII package list
591 //
592 if (PrivateData->HiiHandle != NULL) {
593 HiiRemovePackages (PrivateData->HiiHandle);
594 PrivateData->HiiHandle = NULL;
595 }
596
597 //
598 // Uninstall HII Config Access Protocol
599 //
600 if (PrivateData->DriverHandle != NULL) {
601 gBS->UninstallMultipleProtocolInterfaces (
602 PrivateData->DriverHandle,
603 &gEfiDevicePathProtocolGuid,
604 PrivateData->ChildDevicePath,
605 &gEfiHiiConfigAccessProtocolGuid,
606 &PrivateData->ConfigAccess,
607 NULL
608 );
609 PrivateData->DriverHandle = NULL;
610
611 if (PrivateData->ChildDevicePath != NULL) {
612 FreePool (PrivateData->ChildDevicePath);
613 PrivateData->ChildDevicePath = NULL;
614 }
615 }
616 }