]> git.proxmox.com Git - mirror_edk2.git/blame - NetworkPkg/Application/VConfig/VConfig.c
Add NetworkPkg (P.UDK2010.UP3.Network.P1)
[mirror_edk2.git] / NetworkPkg / Application / VConfig / VConfig.c
CommitLineData
a3bcde70
HT
1/** @file\r
2 Shell application for VLAN configuration.\r
3\r
4 Copyright (C) 2009 - 2010, Intel Corporation. All rights reserved.<BR>\r
5\r
6 This program and the accompanying materials\r
7 are licensed and made available under the terms and conditions of the BSD License\r
8 which accompanies this distribution. The full text of the license may be found at\r
9 http://opensource.org/licenses/bsd-license.php.\r
10\r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include <Uefi.h>\r
17\r
18#include <Protocol/VlanConfig.h>\r
19\r
20#include <Library/UefiApplicationEntryPoint.h>\r
21#include <Library/UefiLib.h>\r
22#include <Library/ShellLib.h>\r
23#include <Library/MemoryAllocationLib.h>\r
24#include <Library/HiiLib.h>\r
25#include <Library/UefiBootServicesTableLib.h>\r
26#include <Library/NetLib.h>\r
27\r
28#define INVALID_NIC_INDEX 0xffff\r
29#define INVALID_VLAN_ID 0xffff\r
30\r
31//\r
32// This is the generated String package data for all .UNI files.\r
33// This data array is ready to be used as input of HiiAddPackages() to\r
34// create a packagelist (which contains Form packages, String packages, etc).\r
35//\r
36extern UINT8 VConfigStrings[];\r
37\r
38EFI_HANDLE mImageHandle = NULL;\r
39EFI_HII_HANDLE mHiiHandle = NULL;\r
40\r
41SHELL_PARAM_ITEM mParamList[] = {\r
42 {\r
43 L"-l",\r
44 TypeValue\r
45 },\r
46 {\r
47 L"-a",\r
48 TypeMaxValue\r
49 },\r
50 {\r
51 L"-d",\r
52 TypeValue\r
53 },\r
54 {\r
55 NULL,\r
56 TypeMax\r
57 }\r
58};\r
59\r
60/**\r
61 Locate the network interface handle buffer.\r
62\r
63 @param[out] NumberOfHandles Pointer to the number of handles.\r
64 @param[out] HandleBuffer Pointer to the buffer to store the returned handles.\r
65\r
66**/\r
67VOID\r
68LocateNicHandleBuffer (\r
69 OUT UINTN *NumberOfHandles,\r
70 OUT EFI_HANDLE **HandleBuffer\r
71 )\r
72{\r
73 EFI_STATUS Status;\r
74\r
75 *NumberOfHandles = 0;\r
76 *HandleBuffer = NULL;\r
77\r
78 Status = gBS->LocateHandleBuffer (\r
79 ByProtocol,\r
80 &gEfiVlanConfigProtocolGuid,\r
81 NULL,\r
82 NumberOfHandles,\r
83 HandleBuffer\r
84 );\r
85 if (EFI_ERROR (Status)) {\r
86 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_LOCATE_FAIL), mHiiHandle, Status);\r
87 }\r
88}\r
89\r
90/**\r
91 Extract the decimal index from the network interface name.\r
92\r
93 @param[in] Name Name of the network interface.\r
94\r
95 @retval INVALID_NIC_INDEX Failed to extract the network interface index.\r
96 @return others The network interface index.\r
97\r
98**/\r
99UINTN\r
100NicNameToIndex (\r
101 IN CHAR16 *Name\r
102 )\r
103{\r
104 CHAR16 *Str;\r
105\r
106 Str = Name + 3;\r
107 if ((StrnCmp (Name, L"eth", 3) != 0) || (*Str == 0)) {\r
108 return INVALID_NIC_INDEX;\r
109 }\r
110\r
111 while (*Str != 0) {\r
112 if ((*Str < L'0') || (*Str > L'9')) {\r
113 return INVALID_NIC_INDEX;\r
114 }\r
115\r
116 Str++;\r
117 }\r
118\r
119 return (UINT16) StrDecimalToUintn (Name + 3);\r
120}\r
121\r
122/**\r
123 Find network interface device handle by its name.\r
124\r
125 @param[in] Name Name of the network interface.\r
126\r
127 @retval NULL Cannot find the network interface.\r
128 @return others Handle of the network interface.\r
129\r
130**/\r
131EFI_HANDLE\r
132NicNameToHandle (\r
133 IN CHAR16 *Name\r
134 )\r
135{\r
136 UINTN NumberOfHandles;\r
137 EFI_HANDLE *HandleBuffer;\r
138 UINTN Index;\r
139 EFI_HANDLE Handle;\r
140\r
141 //\r
142 // Find all NIC handles.\r
143 //\r
144 LocateNicHandleBuffer (&NumberOfHandles, &HandleBuffer);\r
145 if (NumberOfHandles == 0) {\r
146 return NULL;\r
147 }\r
148\r
149 Index = NicNameToIndex (Name);\r
150 if (Index >= NumberOfHandles) {\r
151 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_INVALID_IF), mHiiHandle, Name);\r
152 Handle = NULL;\r
153 } else {\r
154 Handle = HandleBuffer[Index];\r
155 }\r
156\r
157 FreePool (HandleBuffer);\r
158 return Handle;\r
159}\r
160\r
161/**\r
162 Open VlanConfig protocol from a handle.\r
163\r
164 @param[in] Handle The handle to open the VlanConfig protocol.\r
165\r
166 @return The VlanConfig protocol interface.\r
167\r
168**/\r
169EFI_VLAN_CONFIG_PROTOCOL *\r
170OpenVlanConfigProtocol (\r
171 IN EFI_HANDLE Handle\r
172 )\r
173{\r
174 EFI_VLAN_CONFIG_PROTOCOL *VlanConfig;\r
175\r
176 VlanConfig = NULL;\r
177 gBS->OpenProtocol (\r
178 Handle,\r
179 &gEfiVlanConfigProtocolGuid,\r
180 (VOID **) &VlanConfig,\r
181 mImageHandle,\r
182 Handle,\r
183 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
184 );\r
185\r
186 return VlanConfig;\r
187}\r
188\r
189/**\r
190 Close VlanConfig protocol of a handle.\r
191\r
192 @param[in] Handle The handle to close the VlanConfig protocol.\r
193\r
194**/\r
195VOID\r
196CloseVlanConfigProtocol (\r
197 IN EFI_HANDLE Handle\r
198 )\r
199{\r
200 gBS->CloseProtocol (\r
201 Handle,\r
202 &gEfiVlanConfigProtocolGuid,\r
203 mImageHandle,\r
204 Handle\r
205 );\r
206}\r
207\r
208/**\r
209 Display VLAN configuration of a network interface.\r
210\r
211 @param[in] Handle Handle of the network interface.\r
212 @param[in] NicIndex Index of the network interface.\r
213\r
214**/\r
215VOID\r
216ShowNicVlanInfo (\r
217 IN EFI_HANDLE Handle,\r
218 IN UINTN NicIndex\r
219 )\r
220{\r
221 CHAR16 *MacStr;\r
222 EFI_STATUS Status;\r
223 UINTN Index;\r
224 EFI_VLAN_CONFIG_PROTOCOL *VlanConfig;\r
225 UINT16 NumberOfVlan;\r
226 EFI_VLAN_FIND_DATA *VlanData;\r
227\r
228 VlanConfig = OpenVlanConfigProtocol (Handle);\r
229 if (VlanConfig == NULL) {\r
230 return ;\r
231 }\r
232\r
233 MacStr = NULL;\r
234 Status = NetLibGetMacString (Handle, mImageHandle, &MacStr);\r
235 if (EFI_ERROR (Status)) {\r
236 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_MAC_FAIL), mHiiHandle, Status);\r
237 goto Exit;\r
238 }\r
239\r
240 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_ETH_MAC), mHiiHandle, NicIndex, MacStr);\r
241\r
242 Status = VlanConfig->Find (VlanConfig, NULL, &NumberOfVlan, &VlanData);\r
243 if (EFI_ERROR (Status)) {\r
244 if (Status == EFI_NOT_FOUND) {\r
245 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_NO_VLAN), mHiiHandle);\r
246 } else {\r
247 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_FIND_FAIL), mHiiHandle, Status);\r
248 }\r
249\r
250 goto Exit;\r
251 }\r
252\r
253 for (Index = 0; Index < NumberOfVlan; Index++) {\r
254 ShellPrintHiiEx (\r
255 -1,\r
256 -1,\r
257 NULL,\r
258 STRING_TOKEN (STR_VCONFIG_VLAN_DISPLAY),\r
259 mHiiHandle,\r
260 VlanData[Index].VlanId,\r
261 VlanData[Index].Priority\r
262 );\r
263 }\r
264\r
265 FreePool (VlanData);\r
266\r
267Exit:\r
268 CloseVlanConfigProtocol (Handle);\r
269\r
270 if (MacStr != NULL) {\r
271 FreePool (MacStr);\r
272 }\r
273}\r
274\r
275/**\r
276 Display the VLAN configuration of all, or a specified network interface.\r
277\r
278 @param[in] Name Name of the network interface. If NULL, the VLAN\r
279 configuration of all network will be displayed.\r
280\r
281**/\r
282VOID\r
283DisplayVlan (\r
284 IN CHAR16 *Name OPTIONAL\r
285 )\r
286{\r
287 UINTN NumberOfHandles;\r
288 EFI_HANDLE *HandleBuffer;\r
289 UINTN Index;\r
290 EFI_HANDLE NicHandle;\r
291\r
292 if (Name != NULL) {\r
293 //\r
294 // Display specified NIC\r
295 //\r
296 NicHandle = NicNameToHandle (Name);\r
297 if (NicHandle == NULL) {\r
298 return ;\r
299 }\r
300\r
301 ShowNicVlanInfo (NicHandle, 0);\r
302 return ;\r
303 }\r
304\r
305 //\r
306 // Find all NIC handles\r
307 //\r
308 LocateNicHandleBuffer (&NumberOfHandles, &HandleBuffer);\r
309 if (NumberOfHandles == 0) {\r
310 return ;\r
311 }\r
312\r
313 for (Index = 0; Index < NumberOfHandles; Index++) {\r
314 ShowNicVlanInfo (HandleBuffer[Index], Index);\r
315 }\r
316\r
317 FreePool (HandleBuffer);\r
318}\r
319\r
320/**\r
321 Convert a NULL-terminated unicode decimal VLAN ID string to VLAN ID.\r
322\r
323 @param[in] String Pointer to VLAN ID string from user input.\r
324\r
325 @retval Value translated from String, or INVALID_VLAN_ID is string is invalid.\r
326\r
327**/\r
328UINT16\r
329StrToVlanId (\r
330 IN CHAR16 *String\r
331 )\r
332{\r
333 CHAR16 *Str;\r
334\r
335 if (String == NULL) {\r
336 return INVALID_VLAN_ID;\r
337 }\r
338\r
339 Str = String;\r
340 while ((*Str >= '0') && (*Str <= '9')) {\r
341 Str++;\r
342 }\r
343\r
344 if (*Str != 0) {\r
345 return INVALID_VLAN_ID;\r
346 }\r
347\r
348 return (UINT16) StrDecimalToUintn (String);\r
349}\r
350\r
351/**\r
352 Add a VLAN device.\r
353\r
354 @param[in] ParamStr Parameter string from user input.\r
355\r
356**/\r
357VOID\r
358AddVlan (\r
359 IN CHAR16 *ParamStr\r
360 )\r
361{\r
362 CHAR16 *Name;\r
363 CHAR16 *VlanIdStr;\r
364 CHAR16 *PriorityStr;\r
365 CHAR16 *StrPtr;\r
366 BOOLEAN IsSpace;\r
367 UINTN VlanId;\r
368 UINTN Priority;\r
369 EFI_HANDLE Handle;\r
370 EFI_HANDLE VlanHandle;\r
371 EFI_VLAN_CONFIG_PROTOCOL *VlanConfig;\r
372 EFI_STATUS Status;\r
373\r
374 VlanConfig = NULL;\r
375 Priority = 0;\r
376\r
377 if (ParamStr == NULL) {\r
378 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_NO_IF), mHiiHandle);\r
379 return ;\r
380 }\r
381\r
382 StrPtr = AllocateCopyPool (StrSize (ParamStr), ParamStr);\r
383 if (StrPtr == NULL) {\r
384 return ;\r
385 }\r
386\r
387 Name = StrPtr;\r
388 VlanIdStr = NULL;\r
389 PriorityStr = NULL;\r
390 IsSpace = FALSE;\r
391 while (*StrPtr != 0) {\r
392 if (*StrPtr == L' ') {\r
393 *StrPtr = 0;\r
394 IsSpace = TRUE;\r
395 } else {\r
396 if (IsSpace) {\r
397 //\r
398 // Start of a parameter.\r
399 //\r
400 if (VlanIdStr == NULL) {\r
401 //\r
402 // 2nd parameter is VLAN ID.\r
403 //\r
404 VlanIdStr = StrPtr;\r
405 } else if (PriorityStr == NULL) {\r
406 //\r
407 // 3rd parameter is Priority.\r
408 //\r
409 PriorityStr = StrPtr;\r
410 } else {\r
411 //\r
412 // Ignore else parameters.\r
413 //\r
414 break;\r
415 }\r
416 }\r
417\r
418 IsSpace = FALSE;\r
419 }\r
420\r
421 StrPtr++;\r
422 }\r
423\r
424 Handle = NicNameToHandle (Name);\r
425 if (Handle == NULL) {\r
426 goto Exit;\r
427 }\r
428\r
429 VlanConfig = OpenVlanConfigProtocol (Handle);\r
430 if (VlanConfig == NULL) {\r
431 goto Exit;\r
432 }\r
433\r
434 //\r
435 // Check VLAN ID.\r
436 //\r
437 if ((VlanIdStr == NULL) || (*VlanIdStr == 0)) {\r
438 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_NO_VID), mHiiHandle);\r
439 goto Exit;\r
440 }\r
441\r
442 VlanId = StrToVlanId (VlanIdStr);\r
443 if (VlanId > 4094) {\r
444 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_INVALID_VID), mHiiHandle, VlanIdStr);\r
445 goto Exit;\r
446 }\r
447\r
448 //\r
449 // Check Priority.\r
450 //\r
451 if ((PriorityStr != NULL) && (*PriorityStr != 0)) {\r
452 Priority = StrDecimalToUintn (PriorityStr);\r
453 if (Priority > 7) {\r
454 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_INVALID_PRIORITY), mHiiHandle, PriorityStr);\r
455 goto Exit;\r
456 }\r
457 }\r
458\r
459 //\r
460 // Set VLAN\r
461 //\r
462 Status = VlanConfig->Set (VlanConfig, (UINT16) VlanId, (UINT8) Priority);\r
463 if (EFI_ERROR (Status)) {\r
464 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_SET_FAIL), mHiiHandle, Status);\r
465 goto Exit;\r
466 }\r
467\r
468 //\r
469 // Connect the VLAN device.\r
470 //\r
471 VlanHandle = NetLibGetVlanHandle (Handle, (UINT16) VlanId);\r
472 if (VlanHandle != NULL) {\r
473 gBS->ConnectController (VlanHandle, NULL, NULL, TRUE);\r
474 }\r
475\r
476 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_SET_SUCCESS), mHiiHandle);\r
477\r
478Exit:\r
479 if (VlanConfig != NULL) {\r
480 CloseVlanConfigProtocol (Handle);\r
481 }\r
482\r
483 FreePool (Name);\r
484}\r
485\r
486/**\r
487 Remove a VLAN device.\r
488\r
489 @param[in] ParamStr Parameter string from user input.\r
490\r
491**/\r
492VOID\r
493DeleteVlan (\r
494 CHAR16 *ParamStr\r
495 )\r
496{\r
497 CHAR16 *Name;\r
498 CHAR16 *VlanIdStr;\r
499 CHAR16 *StrPtr;\r
500 UINTN VlanId;\r
501 EFI_HANDLE Handle;\r
502 EFI_VLAN_CONFIG_PROTOCOL *VlanConfig;\r
503 EFI_STATUS Status;\r
504 UINT16 NumberOfVlan;\r
505 EFI_VLAN_FIND_DATA *VlanData;\r
506\r
507 VlanConfig = NULL;\r
508\r
509 if (ParamStr == NULL) {\r
510 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_NO_IF), mHiiHandle);\r
511 return ;\r
512 }\r
513\r
514 StrPtr = AllocateCopyPool (StrSize (ParamStr), ParamStr);\r
515 if (StrPtr == NULL) {\r
516 return ;\r
517 }\r
518\r
519 Name = StrPtr;\r
520 VlanIdStr = NULL;\r
521 while (*StrPtr != 0) {\r
522 if (*StrPtr == L'.') {\r
523 *StrPtr = 0;\r
524 VlanIdStr = StrPtr + 1;\r
525 break;\r
526 }\r
527\r
528 StrPtr++;\r
529 }\r
530\r
531 Handle = NicNameToHandle (Name);\r
532 if (Handle == NULL) {\r
533 goto Exit;\r
534 }\r
535\r
536 VlanConfig = OpenVlanConfigProtocol (Handle);\r
537 if (VlanConfig == NULL) {\r
538 goto Exit;\r
539 }\r
540\r
541 //\r
542 // Check VLAN ID\r
543 //\r
544 if (VlanIdStr == NULL || *VlanIdStr == 0) {\r
545 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_NO_VID), mHiiHandle);\r
546 goto Exit;\r
547 }\r
548\r
549 VlanId = StrToVlanId (VlanIdStr);\r
550 if (VlanId > 4094) {\r
551 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_INVALID_VID), mHiiHandle, VlanIdStr);\r
552 goto Exit;\r
553 }\r
554\r
555 //\r
556 // Delete VLAN.\r
557 //\r
558 Status = VlanConfig->Remove (VlanConfig, (UINT16) VlanId);\r
559 if (EFI_ERROR (Status)) {\r
560 if (Status == EFI_NOT_FOUND) {\r
561 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_NOT_FOUND), mHiiHandle);\r
562 } else {\r
563 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_REMOVE_FAIL), mHiiHandle, Status);\r
564 }\r
565\r
566 goto Exit;\r
567 }\r
568\r
569 //\r
570 // Check whether this is the last VLAN to remove.\r
571 //\r
572 Status = VlanConfig->Find (VlanConfig, NULL, &NumberOfVlan, &VlanData);\r
573 if (EFI_ERROR (Status)) {\r
574 //\r
575 // This is the last VLAN to remove, try to connect the controller handle.\r
576 //\r
577 gBS->ConnectController (Handle, NULL, NULL, TRUE);\r
578 } else {\r
579 FreePool (VlanData);\r
580 }\r
581\r
582 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_REMOVE_SUCCESS), mHiiHandle);\r
583\r
584Exit:\r
585 if (VlanConfig != NULL) {\r
586 CloseVlanConfigProtocol (Handle);\r
587 }\r
588\r
589 FreePool (Name);\r
590}\r
591\r
592/**\r
593 The actual entry point for the application.\r
594\r
595 @param[in] ImageHandle The firmware allocated handle for the EFI image.\r
596 @param[in] SystemTable A pointer to the EFI System Table.\r
597\r
598 @retval EFI_SUCCESS The entry point executed successfully.\r
599 @retval other Some error occur when executing this entry point.\r
600\r
601**/\r
602EFI_STATUS\r
603EFIAPI\r
604VlanConfigMain (\r
605 IN EFI_HANDLE ImageHandle,\r
606 IN EFI_SYSTEM_TABLE *SystemTable\r
607 )\r
608{\r
609 LIST_ENTRY *List;\r
610 CONST CHAR16 *Str;\r
611\r
612 mImageHandle = ImageHandle;\r
613\r
614 //\r
615 // Register our string package to HII database.\r
616 //\r
617 mHiiHandle = HiiAddPackages (&gEfiCallerIdGuid, ImageHandle, VConfigStrings, NULL);\r
618 if (mHiiHandle == NULL) {\r
619 return EFI_SUCCESS;\r
620 }\r
621\r
622 List = NULL;\r
623 ShellCommandLineParseEx (mParamList, &List, NULL, FALSE, FALSE);\r
624 if (List == NULL) {\r
625 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_NO_ARG), mHiiHandle);\r
626 goto Exit;\r
627 }\r
628\r
629 if (ShellCommandLineGetFlag (List, L"-?")) {\r
630 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_HELP), mHiiHandle);\r
631 goto Exit;\r
632 }\r
633\r
634 if (ShellCommandLineGetFlag (List, L"-l")) {\r
635 Str = ShellCommandLineGetValue (List, L"-l");\r
636 DisplayVlan ((CHAR16 *) Str);\r
637 goto Exit;\r
638 }\r
639\r
640 if (ShellCommandLineGetFlag (List, L"-a")) {\r
641 Str = ShellCommandLineGetValue (List, L"-a");\r
642 AddVlan ((CHAR16 *) Str);\r
643 goto Exit;\r
644 }\r
645\r
646 if (ShellCommandLineGetFlag (List, L"-d")) {\r
647 Str = ShellCommandLineGetValue (List, L"-d");\r
648 DeleteVlan ((CHAR16 *) Str);\r
649 goto Exit;\r
650 }\r
651\r
652 //\r
653 // No valid argument till now.\r
654 //\r
655 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_VCONFIG_NO_ARG), mHiiHandle);\r
656\r
657Exit:\r
658 if (List != NULL) {\r
659 ShellCommandLineFreeVarList (List);\r
660 }\r
661\r
662 //\r
663 // Remove our string package from HII database.\r
664 //\r
665 HiiRemovePackages (mHiiHandle);\r
666\r
667 return EFI_SUCCESS;\r
668}\r