]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/VlanConfigDxe/VlanConfigImpl.c
8d72f585d040a0f9056face6c2022f19dcc737ea
[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, 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 for (Index = 0; Index < PrivateData->NumberOfVlan; Index++) {
242 if (Configuration->VlanList[Index] != 0) {
243 //
244 // Checkbox is selected, need remove this VLAN
245 //
246 VlanConfig->Remove (VlanConfig, PrivateData->VlanId[Index]);
247 }
248 }
249
250 VlanUpdateForm (PrivateData);
251 if (PrivateData->NumberOfVlan == 0) {
252 //
253 // No VLAN device now, connect the physical NIC handle.
254 // Note: PrivateData->NumberOfVlan has been updated by VlanUpdateForm()
255 //
256 gBS->ConnectController (PrivateData->ControllerHandle, NULL, NULL, TRUE);
257 }
258
259 *ActionRequest = EFI_BROWSER_ACTION_REQUEST_SUBMIT;
260 ZeroMem (Configuration->VlanList, MAX_VLAN_NUMBER);
261 break;
262
263 default:
264 break;
265 }
266
267 HiiSetBrowserData (&mVlanFormSetGuid, mVlanStorageName, sizeof (VLAN_CONFIGURATION), (UINT8 *) Configuration, NULL);
268 FreePool (Configuration);
269 return EFI_SUCCESS;
270 }
271
272
273 /**
274 This function update VLAN list in the VLAN configuration Form.
275
276 @param[in, out] PrivateData Points to VLAN configuration private data.
277
278 **/
279 VOID
280 VlanUpdateForm (
281 IN OUT VLAN_CONFIG_PRIVATE_DATA *PrivateData
282 )
283 {
284 EFI_VLAN_CONFIG_PROTOCOL *VlanConfig;
285 UINT16 NumberOfVlan;
286 UINTN Index;
287 EFI_VLAN_FIND_DATA *VlanData;
288 VOID *StartOpCodeHandle;
289 EFI_IFR_GUID_LABEL *StartLabel;
290 VOID *EndOpCodeHandle;
291 EFI_IFR_GUID_LABEL *EndLabel;
292 CHAR16 *String;
293 CHAR16 VlanStr[30];
294 CHAR16 VlanIdStr[6];
295 UINTN DigitalCount;
296 EFI_STRING_ID StringId;
297
298 //
299 // Find current VLAN configuration
300 //
301 VlanData = NULL;
302 NumberOfVlan = 0;
303 VlanConfig = PrivateData->VlanConfig;
304 VlanConfig->Find (VlanConfig, NULL, &NumberOfVlan, &VlanData);
305
306 //
307 // Update VLAN configuration in PrivateData
308 //
309 if (NumberOfVlan > MAX_VLAN_NUMBER) {
310 NumberOfVlan = MAX_VLAN_NUMBER;
311 }
312 PrivateData->NumberOfVlan = NumberOfVlan;
313
314 //
315 // Init OpCode Handle
316 //
317 StartOpCodeHandle = HiiAllocateOpCodeHandle ();
318 ASSERT (StartOpCodeHandle != NULL);
319
320 EndOpCodeHandle = HiiAllocateOpCodeHandle ();
321 ASSERT (EndOpCodeHandle != NULL);
322
323 //
324 // Create Hii Extend Label OpCode as the start opcode
325 //
326 StartLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode (
327 StartOpCodeHandle,
328 &gEfiIfrTianoGuid,
329 NULL,
330 sizeof (EFI_IFR_GUID_LABEL)
331 );
332 StartLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;
333 StartLabel->Number = LABEL_VLAN_LIST;
334
335 //
336 // Create Hii Extend Label OpCode as the end opcode
337 //
338 EndLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode (
339 EndOpCodeHandle,
340 &gEfiIfrTianoGuid,
341 NULL,
342 sizeof (EFI_IFR_GUID_LABEL)
343 );
344 EndLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;
345 EndLabel->Number = LABEL_END;
346
347 ZeroMem (PrivateData->VlanId, MAX_VLAN_NUMBER);
348 for (Index = 0; Index < NumberOfVlan; Index++) {
349 String = VlanStr;
350
351 StrCpy (String, L" VLAN ID:");
352 String += 10;
353 //
354 // Pad VlanId string up to 4 characters with space
355 //
356 DigitalCount = UnicodeValueToString (VlanIdStr, 0, VlanData[Index].VlanId, 5);
357 SetMem16 (String, (4 - DigitalCount) * sizeof (CHAR16), L' ');
358 StrCpy (String + 4 - DigitalCount, VlanIdStr);
359 String += 4;
360
361 StrCpy (String, L", Priority:");
362 String += 11;
363 String += UnicodeValueToString (String, 0, VlanData[Index].Priority, 4);
364 *String = 0;
365
366 StringId = HiiSetString (PrivateData->HiiHandle, 0, VlanStr, NULL);
367 ASSERT (StringId != 0);
368
369 HiiCreateCheckBoxOpCode (
370 StartOpCodeHandle,
371 (EFI_QUESTION_ID) (VLAN_LIST_VAR_OFFSET + Index),
372 VLAN_CONFIGURATION_VARSTORE_ID,
373 (UINT16) (VLAN_LIST_VAR_OFFSET + Index),
374 StringId,
375 STRING_TOKEN (STR_VLAN_VLAN_LIST_HELP),
376 0,
377 0,
378 NULL
379 );
380
381 //
382 // Save VLAN id to private data
383 //
384 PrivateData->VlanId[Index] = VlanData[Index].VlanId;
385 }
386
387 HiiUpdateForm (
388 PrivateData->HiiHandle, // HII handle
389 &mVlanFormSetGuid, // Formset GUID
390 VLAN_CONFIGURATION_FORM_ID, // Form ID
391 StartOpCodeHandle, // Label for where to insert opcodes
392 EndOpCodeHandle // Replace data
393 );
394
395 HiiFreeOpCodeHandle (StartOpCodeHandle);
396 HiiFreeOpCodeHandle (EndOpCodeHandle);
397
398 if (VlanData != NULL) {
399 FreePool (VlanData);
400 }
401 }
402
403
404 /**
405 This function publish the VLAN configuration Form for a network device. The
406 HII Config Access protocol will be installed on a child handle of the network
407 device.
408
409 @param[in, out] PrivateData Points to VLAN configuration private data.
410
411 @retval EFI_SUCCESS HII Form is installed for this network device.
412 @retval EFI_OUT_OF_RESOURCES Not enough resource for HII Form installation.
413 @retval Others Other errors as indicated.
414
415 **/
416 EFI_STATUS
417 InstallVlanConfigForm (
418 IN OUT VLAN_CONFIG_PRIVATE_DATA *PrivateData
419 )
420 {
421 EFI_STATUS Status;
422 EFI_HII_HANDLE HiiHandle;
423 EFI_HANDLE DriverHandle;
424 CHAR16 Str[40];
425 CHAR16 *MacString;
426 EFI_DEVICE_PATH_PROTOCOL *ChildDevicePath;
427 EFI_HII_CONFIG_ACCESS_PROTOCOL *ConfigAccess;
428
429 //
430 // Create child handle and install HII Config Access Protocol
431 //
432 ChildDevicePath = AppendDevicePathNode (
433 PrivateData->ParentDevicePath,
434 (CONST EFI_DEVICE_PATH_PROTOCOL *) &mHiiVendorDevicePathNode
435 );
436 if (ChildDevicePath == NULL) {
437 return EFI_OUT_OF_RESOURCES;
438 }
439 PrivateData->ChildDevicePath = ChildDevicePath;
440
441 DriverHandle = NULL;
442 ConfigAccess = &PrivateData->ConfigAccess;
443 Status = gBS->InstallMultipleProtocolInterfaces (
444 &DriverHandle,
445 &gEfiDevicePathProtocolGuid,
446 ChildDevicePath,
447 &gEfiHiiConfigAccessProtocolGuid,
448 ConfigAccess,
449 NULL
450 );
451 if (EFI_ERROR (Status)) {
452 return Status;
453 }
454 PrivateData->DriverHandle = DriverHandle;
455
456 //
457 // Publish the HII package list
458 //
459 HiiHandle = HiiAddPackages (
460 &mVlanFormSetGuid,
461 DriverHandle,
462 VlanConfigDxeStrings,
463 VlanConfigBin,
464 NULL
465 );
466 if (HiiHandle == NULL) {
467 return EFI_OUT_OF_RESOURCES;
468 }
469 PrivateData->HiiHandle = HiiHandle;
470
471 //
472 // Update formset title
473 //
474 MacString = NULL;
475 Status = NetLibGetMacString (PrivateData->ControllerHandle, PrivateData->ImageHandle, &MacString);
476 if (EFI_ERROR (Status)) {
477 return Status;
478 }
479 PrivateData->MacString = MacString;
480
481 StrCpy (Str, L"VLAN Configuration (MAC:");
482 StrCat (Str, MacString);
483 StrCat (Str, L")");
484 HiiSetString (
485 HiiHandle,
486 STRING_TOKEN (STR_VLAN_FORM_SET_TITLE),
487 Str,
488 NULL
489 );
490
491 //
492 // Update form title
493 //
494 HiiSetString (
495 HiiHandle,
496 STRING_TOKEN (STR_VLAN_FORM_TITLE),
497 Str,
498 NULL
499 );
500
501 return EFI_SUCCESS;
502 }
503
504 /**
505 This function remove the VLAN configuration Form for a network device. The
506 child handle for HII Config Access protocol will be destroyed.
507
508 @param[in, out] PrivateData Points to VLAN configuration private data.
509
510 **/
511 VOID
512 UninstallVlanConfigForm (
513 IN OUT VLAN_CONFIG_PRIVATE_DATA *PrivateData
514 )
515 {
516 //
517 // Free MAC string
518 //
519 if (PrivateData->MacString != NULL) {
520 FreePool (PrivateData->MacString);
521 PrivateData->MacString = NULL;
522 }
523
524 //
525 // Uninstall HII package list
526 //
527 if (PrivateData->HiiHandle != NULL) {
528 HiiRemovePackages (PrivateData->HiiHandle);
529 PrivateData->HiiHandle = NULL;
530 }
531
532 //
533 // Uninstall HII Config Access Protocol
534 //
535 if (PrivateData->DriverHandle != NULL) {
536 gBS->UninstallMultipleProtocolInterfaces (
537 PrivateData->DriverHandle,
538 &gEfiDevicePathProtocolGuid,
539 PrivateData->ChildDevicePath,
540 &gEfiHiiConfigAccessProtocolGuid,
541 &PrivateData->ConfigAccess,
542 NULL
543 );
544 PrivateData->DriverHandle = NULL;
545
546 if (PrivateData->ChildDevicePath != NULL) {
547 FreePool (PrivateData->ChildDevicePath);
548 PrivateData->ChildDevicePath = NULL;
549 }
550 }
551 }