2 Main file for BCFG command.
4 (C) Copyright 2014-2015 Hewlett-Packard Development Company, L.P.<BR>
5 Copyright (c) 2010 - 2015, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
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.
18 #include <ShellBase.h>
20 #include <Guid/GlobalVariable.h>
21 #include <Guid/ShellLibHiiGuid.h>
23 #include <Protocol/EfiShell.h>
24 #include <Protocol/EfiShellParameters.h>
25 #include <Protocol/DevicePath.h>
26 #include <Protocol/LoadedImage.h>
27 #include <Protocol/UnicodeCollation.h>
29 #include <Library/BaseLib.h>
30 #include <Library/BaseMemoryLib.h>
31 #include <Library/DebugLib.h>
32 #include <Library/MemoryAllocationLib.h>
33 #include <Library/PcdLib.h>
34 #include <Library/ShellCommandLib.h>
35 #include <Library/ShellLib.h>
36 #include <Library/SortLib.h>
37 #include <Library/UefiLib.h>
38 #include <Library/UefiRuntimeServicesTableLib.h>
39 #include <Library/UefiBootServicesTableLib.h>
40 #include <Library/HiiLib.h>
41 #include <Library/FileHandleLib.h>
42 #include <Library/PrintLib.h>
43 #include <Library/HandleParsingLib.h>
44 #include <Library/DevicePathLib.h>
46 STATIC CONST CHAR16 mFileName
[] = L
"ShellCommands";
47 STATIC EFI_HANDLE gShellBcfgHiiHandle
= NULL
;
50 BcfgTargetBootOrder
= 0,
51 BcfgTargetDriverOrder
= 1,
53 } BCFG_OPERATION_TARGET
;
64 } BCFG_OPERATION_TYPE
;
67 BCFG_OPERATION_TARGET Target
;
68 BCFG_OPERATION_TYPE Type
;
75 CONST CHAR16
*OptData
;
79 Update the optional data for a boot or driver option.
81 If optional data exists it will be changed.
83 @param[in] Index The boot or driver option index update.
84 @param[in] DataSize The size in bytes of Data.
85 @param[in] Data The buffer for the optioanl data.
86 @param[in] Target The target of the operation.
88 @retval EFI_SUCCESS The data was sucessfully updated.
89 @retval other A error occured.
97 IN CONST BCFG_OPERATION_TARGET Target
101 CHAR16 VariableName
[12];
106 UINTN OriginalOptionDataSize
;
108 UnicodeSPrint(VariableName
, sizeof(VariableName
), L
"%s%04x", Target
== BcfgTargetBootOrder
?L
"Boot":L
"Driver", Index
);
115 Status
= gRT
->GetVariable(
117 (EFI_GUID
*)&gEfiGlobalVariableGuid
,
121 if (Status
== EFI_BUFFER_TOO_SMALL
) {
122 OriginalData
= AllocateZeroPool(OriginalSize
);
123 if (OriginalData
== NULL
) {
124 return (EFI_OUT_OF_RESOURCES
);
126 Status
= gRT
->GetVariable(
128 (EFI_GUID
*)&gEfiGlobalVariableGuid
,
134 if (!EFI_ERROR(Status
)) {
136 // Allocate new struct and discard old optional data.
138 ASSERT (OriginalData
!= NULL
);
139 OriginalOptionDataSize
= sizeof(UINT32
) + sizeof(UINT16
) + StrSize(((CHAR16
*)(OriginalData
+ sizeof(UINT32
) + sizeof(UINT16
))));
140 OriginalOptionDataSize
+= (*(UINT16
*)(OriginalData
+ sizeof(UINT32
)));
141 OriginalOptionDataSize
-= OriginalSize
;
142 NewSize
= OriginalSize
- OriginalOptionDataSize
+ DataSize
;
143 NewData
= AllocateCopyPool(NewSize
, OriginalData
);
144 if (NewData
== NULL
) {
145 Status
= EFI_OUT_OF_RESOURCES
;
147 CopyMem(NewData
+ OriginalSize
- OriginalOptionDataSize
, Data
, DataSize
);
151 if (!EFI_ERROR(Status
)) {
153 // put the data back under the variable
155 Status
= gRT
->SetVariable(
157 (EFI_GUID
*)&gEfiGlobalVariableGuid
,
158 EFI_VARIABLE_NON_VOLATILE
|EFI_VARIABLE_BOOTSERVICE_ACCESS
|EFI_VARIABLE_RUNTIME_ACCESS
,
163 SHELL_FREE_NON_NULL(OriginalData
);
164 SHELL_FREE_NON_NULL(NewData
);
169 This function will get a CRC for a boot option.
171 @param[in, out] Crc The CRC value to return.
172 @param[in] BootIndex The boot option index to CRC.
174 @retval EFI_SUCCESS The CRC was sucessfully returned.
175 @retval other A error occured.
184 CHAR16 VariableName
[12];
193 // Get the data Buffer
195 UnicodeSPrint(VariableName
, sizeof(VariableName
), L
"%Boot%04x", BootIndex
);
196 Status
= gRT
->GetVariable(
198 (EFI_GUID
*)&gEfiGlobalVariableGuid
,
202 if (Status
== EFI_BUFFER_TOO_SMALL
) {
203 Buffer
= AllocateZeroPool(BufferSize
);
204 Status
= gRT
->GetVariable(
206 (EFI_GUID
*)&gEfiGlobalVariableGuid
,
213 // Get the CRC computed
215 if (!EFI_ERROR(Status
)) {
216 Status
= gBS
->CalculateCrc32 (Buffer
, BufferSize
, Crc
);
219 SHELL_FREE_NON_NULL(Buffer
);
224 This function will populate the device path protocol parameter based on TheHandle.
226 @param[in] TheHandle Driver handle.
227 @param[in, out] FilePath On a sucessful return the device path to the handle.
229 @retval EFI_SUCCESS The device path was sucessfully returned.
230 @retval other A error from gBS->HandleProtocol.
236 GetDevicePathForDriverHandle (
237 IN EFI_HANDLE TheHandle
,
238 IN OUT EFI_DEVICE_PATH_PROTOCOL
**FilePath
242 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
243 EFI_DEVICE_PATH_PROTOCOL
*ImageDevicePath
;
245 Status
= gBS
->OpenProtocol (
247 &gEfiLoadedImageProtocolGuid
,
248 (VOID
**)&LoadedImage
,
251 EFI_OPEN_PROTOCOL_GET_PROTOCOL
253 if (!EFI_ERROR (Status
)) {
254 Status
= gBS
->OpenProtocol (
255 LoadedImage
->DeviceHandle
,
256 &gEfiDevicePathProtocolGuid
,
257 (VOID
**)&ImageDevicePath
,
260 EFI_OPEN_PROTOCOL_GET_PROTOCOL
262 if (!EFI_ERROR (Status
)) {
263 // *DevPath = DuplicateDevicePath (ImageDevicePath);
264 // *FilePath = DuplicateDevicePath (LoadedImage->FilePath);
265 *FilePath
= AppendDevicePath(ImageDevicePath
,LoadedImage
->FilePath
);
267 LoadedImage
->DeviceHandle
,
268 &gEfiDevicePathProtocolGuid
,
274 &gEfiLoadedImageProtocolGuid
,
282 Function to add a option.
284 @param[in] Position The position to add Target at.
285 @param[in] File The file to make the target.
286 @param[in] Desc The description text.
287 @param[in] CurrentOrder The pointer to the current order of items.
288 @param[in] OrderCount The number if items in CurrentOrder.
289 @param[in] Target The info on the option to add.
290 @param[in] UseHandle TRUE to use HandleNumber, FALSE to use File and Desc.
291 @param[in] UsePath TRUE to convert to devicepath.
292 @param[in] HandleNumber The handle number to add.
294 @retval SHELL_SUCCESS The operation was successful.
295 @retval SHELL_INVALID_PARAMETER A parameter was invalid.
301 IN CONST CHAR16
*File
,
302 IN CONST CHAR16
*Desc
,
303 IN CONST UINT16
*CurrentOrder
,
304 IN CONST UINTN OrderCount
,
305 IN CONST BCFG_OPERATION_TARGET Target
,
306 IN CONST BOOLEAN UseHandle
,
307 IN CONST BOOLEAN UsePath
,
308 IN CONST UINTN HandleNumber
312 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
313 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
314 EFI_DEVICE_PATH_PROTOCOL
*FilePath
;
316 UINT8
*TempByteBuffer
;
317 UINT8
*TempByteStart
;
318 EFI_SHELL_FILE_INFO
*Arg
;
319 EFI_SHELL_FILE_INFO
*FileList
;
320 CHAR16 OptionStr
[40];
321 UINTN DescSize
, FilePathSize
;
323 UINTN TargetLocation
;
326 EFI_HANDLE CurHandle
;
327 UINTN DriverBindingHandleCount
;
328 UINTN ParentControllerHandleCount
;
329 UINTN ChildControllerHandleCount
;
330 SHELL_STATUS ShellStatus
;
334 if (File
== NULL
|| Desc
== NULL
) {
335 return (SHELL_INVALID_PARAMETER
);
338 if (HandleNumber
== 0) {
339 return (SHELL_INVALID_PARAMETER
);
343 if (Position
> OrderCount
) {
344 Position
= OrderCount
;
351 ShellStatus
= SHELL_SUCCESS
;
352 TargetLocation
= 0xFFFF;
355 CurHandle
= ConvertHandleIndexToHandle(HandleNumber
);
356 if (CurHandle
== NULL
) {
357 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
), gShellBcfgHiiHandle
, L
"bcfg", L
"Handle Number");
358 ShellStatus
= SHELL_INVALID_PARAMETER
;
360 if (Target
== BcfgTargetBootOrder
) {
362 //Make sure that the handle should point to a real controller
364 Status
= PARSE_HANDLE_DATABASE_UEFI_DRIVERS (
366 &DriverBindingHandleCount
,
369 Status
= PARSE_HANDLE_DATABASE_PARENTS (
371 &ParentControllerHandleCount
,
374 Status
= ParseHandleDatabaseForChildControllers (
376 &ChildControllerHandleCount
,
379 if (DriverBindingHandleCount
> 0
380 || ParentControllerHandleCount
> 0
381 || ChildControllerHandleCount
> 0) {
383 Status
= gBS
->HandleProtocol (
385 &gEfiDevicePathProtocolGuid
,
388 if (EFI_ERROR (Status
)) {
389 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_BCFG_HANDLE
), gShellBcfgHiiHandle
, L
"bcfg", HandleNumber
);
390 ShellStatus
= SHELL_INVALID_PARAMETER
;
394 //Make sure that the handle should point to driver, not a controller.
396 Status
= PARSE_HANDLE_DATABASE_UEFI_DRIVERS (
398 &DriverBindingHandleCount
,
401 Status
= PARSE_HANDLE_DATABASE_PARENTS (
403 &ParentControllerHandleCount
,
406 Status
= ParseHandleDatabaseForChildControllers (
408 &ChildControllerHandleCount
,
411 Status
= gBS
->HandleProtocol (
413 &gEfiDevicePathProtocolGuid
,
416 if (DriverBindingHandleCount
> 0
417 || ParentControllerHandleCount
> 0
418 || ChildControllerHandleCount
> 0
419 || !EFI_ERROR(Status
) ) {
420 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
), gShellBcfgHiiHandle
, L
"bcfg", L
"Handle Number");
421 ShellStatus
= SHELL_INVALID_PARAMETER
;
424 // Get the DevicePath from the loaded image information.
426 Status
= GetDevicePathForDriverHandle(CurHandle
, &FilePath
);
434 ShellOpenFileMetaArg ((CHAR16
*)File
, EFI_FILE_MODE_READ
, &FileList
);
436 if (FileList
== NULL
) {
438 // If filename matched nothing fail
440 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL
), gShellBcfgHiiHandle
, L
"bcfg", File
);
441 ShellStatus
= SHELL_INVALID_PARAMETER
;
442 } else if (FileList
->Link
.ForwardLink
!= FileList
->Link
.BackLink
) {
444 // If filename expanded to multiple names, fail
446 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_BCFG_FILE
), gShellBcfgHiiHandle
, L
"bcfg", File
);
447 ShellStatus
= SHELL_INVALID_PARAMETER
;
449 Arg
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&FileList
->Link
);
450 if (EFI_ERROR(Arg
->Status
)) {
451 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_BCFG_FILE_OPEN
), gShellBcfgHiiHandle
, L
"bcfg", File
);
452 ShellStatus
= SHELL_INVALID_PARAMETER
;
455 // Build FilePath to the filename
459 // get the device path
461 DevicePath
= gEfiShellProtocol
->GetDevicePathFromFilePath(Arg
->FullName
);
462 if (DevicePath
== NULL
) {
463 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_BCFG_FILE_DP
), gShellBcfgHiiHandle
, L
"bcfg", Arg
->FullName
);
464 ShellStatus
= SHELL_UNSUPPORTED
;
467 DevPath
= DevicePath
;
468 ShellStatus
= SHELL_INVALID_PARAMETER
;
469 while (!IsDevicePathEnd(DevPath
)) {
470 if ((DevicePathType(DevPath
) == MEDIA_DEVICE_PATH
) &&
471 (DevicePathSubType(DevPath
) == MEDIA_HARDDRIVE_DP
)) {
474 // If we find it use it instead
476 ShellStatus
= SHELL_SUCCESS
;
477 FilePath
= DuplicateDevicePath (DevPath
);
480 DevPath
= NextDevicePathNode(DevPath
);
483 FilePath
= DuplicateDevicePath(DevicePath
);
485 FreePool(DevicePath
);
492 if (ShellStatus
== SHELL_SUCCESS
) {
494 // Find a free target ,a brute force implementation
497 for (TargetLocation
=0; TargetLocation
< 0xFFFF; TargetLocation
++) {
499 for (Index
=0; Index
< OrderCount
; Index
++) {
500 if (CurrentOrder
[Index
] == TargetLocation
) {
511 if (TargetLocation
== 0xFFFF) {
512 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_BCFG_TARGET_NF
), gShellBcfgHiiHandle
, L
"bcfg");
514 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_BCFG_TARGET
), gShellBcfgHiiHandle
, TargetLocation
);
518 if (ShellStatus
== SHELL_SUCCESS
) {
522 DescSize
= StrSize(Desc
);
523 FilePathSize
= GetDevicePathSize (FilePath
);
525 TempByteBuffer
= AllocateZeroPool(sizeof(UINT32
) + sizeof(UINT16
) + DescSize
+ FilePathSize
);
526 if (TempByteBuffer
!= NULL
) {
527 TempByteStart
= TempByteBuffer
;
528 *((UINT32
*) TempByteBuffer
) = LOAD_OPTION_ACTIVE
; // Attributes
529 TempByteBuffer
+= sizeof (UINT32
);
531 *((UINT16
*) TempByteBuffer
) = (UINT16
)FilePathSize
; // FilePathListLength
532 TempByteBuffer
+= sizeof (UINT16
);
534 CopyMem (TempByteBuffer
, Desc
, DescSize
);
535 TempByteBuffer
+= DescSize
;
536 ASSERT (FilePath
!= NULL
);
537 CopyMem (TempByteBuffer
, FilePath
, FilePathSize
);
539 UnicodeSPrint (OptionStr
, sizeof(OptionStr
), L
"%s%04x", Target
== BcfgTargetBootOrder
?L
"Boot":L
"Driver", TargetLocation
);
540 Status
= gRT
->SetVariable (
542 &gEfiGlobalVariableGuid
,
543 EFI_VARIABLE_NON_VOLATILE
|EFI_VARIABLE_BOOTSERVICE_ACCESS
|EFI_VARIABLE_RUNTIME_ACCESS
,
544 sizeof(UINT32
) + sizeof(UINT16
) + DescSize
+ FilePathSize
,
548 FreePool(TempByteStart
);
550 Status
= EFI_OUT_OF_RESOURCES
;
553 if (EFI_ERROR(Status
)) {
554 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_BCFG_SET_VAR_FAIL
), gShellBcfgHiiHandle
, L
"bcfg", OptionStr
);
556 NewOrder
= AllocateZeroPool((OrderCount
+1)*sizeof(NewOrder
[0]));
557 ASSERT(NewOrder
!= NULL
);
558 CopyMem(NewOrder
, CurrentOrder
, (OrderCount
)*sizeof(NewOrder
[0]));
561 // Insert target into order list
563 for (Index
=OrderCount
; Index
> Position
; Index
--) {
564 NewOrder
[Index
] = NewOrder
[Index
-1];
567 NewOrder
[Position
] = (UINT16
) TargetLocation
;
568 Status
= gRT
->SetVariable (
569 Target
== BcfgTargetBootOrder
?L
"BootOrder":L
"DriverOrder",
570 &gEfiGlobalVariableGuid
,
571 EFI_VARIABLE_NON_VOLATILE
|EFI_VARIABLE_BOOTSERVICE_ACCESS
|EFI_VARIABLE_RUNTIME_ACCESS
,
572 (OrderCount
+1) * sizeof(UINT16
),
578 if (EFI_ERROR(Status
)) {
579 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_BCFG_WRITE_FAIL
), gShellBcfgHiiHandle
, L
"bcfg", Target
== BcfgTargetBootOrder
?L
"BootOrder":L
"DriverOrder");
580 ShellStatus
= SHELL_INVALID_PARAMETER
;
582 Print (L
"bcfg: Add %s as %x\n", OptionStr
, Position
);
588 //If always Free FilePath, will free devicepath in system when use "addh"
590 if (FilePath
!=NULL
&& !UseHandle
) {
598 if (Handles
!= NULL
) {
602 if (FileList
!= NULL
) {
603 ShellCloseFileMetaArg (&FileList
);
606 return (ShellStatus
);
610 Funciton to remove an item.
612 @param[in] Target The target item to move.
613 @param[in] CurrentOrder The pointer to the current order of items.
614 @param[in] OrderCount The number if items in CurrentOrder.
615 @param[in] Location The current location of the Target.
617 @retval SHELL_SUCCESS The operation was successful.
618 @retval SHELL_INVALID_PARAMETER A parameter was invalid.
623 IN CONST BCFG_OPERATION_TARGET Target
,
624 IN CONST UINT16
*CurrentOrder
,
625 IN CONST UINTN OrderCount
,
626 IN CONST UINT16 Location
629 CHAR16 VariableName
[12];
634 UnicodeSPrint(VariableName
, sizeof(VariableName
), L
"%s%04x", Target
== BcfgTargetBootOrder
?L
"Boot":L
"Driver", CurrentOrder
[Location
]);
635 Status
= gRT
->SetVariable(
637 (EFI_GUID
*)&gEfiGlobalVariableGuid
,
638 EFI_VARIABLE_NON_VOLATILE
|EFI_VARIABLE_BOOTSERVICE_ACCESS
|EFI_VARIABLE_RUNTIME_ACCESS
,
641 if (EFI_ERROR(Status
)) {
642 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_BCFG_WRITE_FAIL
), gShellBcfgHiiHandle
, L
"bcfg", VariableName
);
643 return (SHELL_INVALID_PARAMETER
);
645 NewOrder
= AllocateZeroPool(OrderCount
*sizeof(CurrentOrder
[0]));
646 if (NewOrder
!= NULL
) {
647 NewCount
= OrderCount
;
648 CopyMem(NewOrder
, CurrentOrder
, OrderCount
*sizeof(CurrentOrder
[0]));
649 CopyMem(NewOrder
+Location
, NewOrder
+Location
+1, (OrderCount
- Location
- 1)*sizeof(CurrentOrder
[0]));
652 Status
= gRT
->SetVariable(
653 Target
== BcfgTargetBootOrder
?(CHAR16
*)L
"BootOrder":(CHAR16
*)L
"DriverOrder",
654 (EFI_GUID
*)&gEfiGlobalVariableGuid
,
655 EFI_VARIABLE_NON_VOLATILE
|EFI_VARIABLE_BOOTSERVICE_ACCESS
|EFI_VARIABLE_RUNTIME_ACCESS
,
656 NewCount
*sizeof(NewOrder
[0]),
660 Status
= EFI_OUT_OF_RESOURCES
;
662 if (EFI_ERROR(Status
)) {
663 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_BCFG_WRITE_FAIL
), gShellBcfgHiiHandle
, L
"bcfg", Target
== BcfgTargetBootOrder
?(CHAR16
*)L
"BootOrder":(CHAR16
*)L
"DriverOrder");
664 return (SHELL_INVALID_PARAMETER
);
666 return (SHELL_SUCCESS
);
670 Funciton to move a item to another location.
672 @param[in] Target The target item to move.
673 @param[in] CurrentOrder The pointer to the current order of items.
674 @param[in] OrderCount The number if items in CurrentOrder.
675 @param[in] OldLocation The current location of the Target.
676 @param[in] NewLocation The desired location of the Target.
678 @retval SHELL_SUCCESS The operation was successful.
679 @retval SHELL_INVALID_PARAMETER A parameter was invalid.
684 IN CONST BCFG_OPERATION_TARGET Target
,
685 IN CONST UINT16
*CurrentOrder
,
686 IN CONST UINTN OrderCount
,
687 IN CONST UINT16 OldLocation
,
688 IN UINT16 NewLocation
695 NewOrder
= AllocateCopyPool(OrderCount
*sizeof(CurrentOrder
[0]), CurrentOrder
);
696 if (NewOrder
== NULL
) {
697 return (SHELL_OUT_OF_RESOURCES
);
701 // correct the new location
703 if (NewLocation
>= OrderCount
) {
704 if (OrderCount
> 0) {
705 NewLocation
= (UINT16
)OrderCount
- 1;
711 Temp
= CurrentOrder
[OldLocation
];
712 CopyMem(NewOrder
+OldLocation
, NewOrder
+OldLocation
+1, (OrderCount
- OldLocation
- 1)*sizeof(CurrentOrder
[0]));
713 CopyMem(NewOrder
+NewLocation
+1, NewOrder
+NewLocation
, (OrderCount
- NewLocation
- 1)*sizeof(CurrentOrder
[0]));
714 NewOrder
[NewLocation
] = Temp
;
716 Status
= gRT
->SetVariable(
717 Target
== BcfgTargetBootOrder
?(CHAR16
*)L
"BootOrder":(CHAR16
*)L
"DriverOrder",
718 (EFI_GUID
*)&gEfiGlobalVariableGuid
,
719 EFI_VARIABLE_NON_VOLATILE
|EFI_VARIABLE_BOOTSERVICE_ACCESS
|EFI_VARIABLE_RUNTIME_ACCESS
,
720 OrderCount
*sizeof(CurrentOrder
[0]),
725 if (EFI_ERROR(Status
)) {
726 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_BCFG_WRITE_FAIL
), gShellBcfgHiiHandle
, L
"bcfg", Target
== BcfgTargetBootOrder
?(CHAR16
*)L
"BootOrder":(CHAR16
*)L
"DriverOrder");
727 return (SHELL_INVALID_PARAMETER
);
729 return (SHELL_SUCCESS
);
733 Function to add optional data to an option.
735 @param[in] OptData The optional data to add.
736 @param[in] CurrentOrder The pointer to the current order of items.
737 @param[in] OrderCount The number if items in CurrentOrder.
738 @param[in] Target The target of the operation.
740 @retval SHELL_SUCCESS The operation was succesful.
745 IN CONST CHAR16
*OptData
,
746 IN CONST UINT16
*CurrentOrder
,
747 IN CONST UINTN OrderCount
,
748 IN CONST BCFG_OPERATION_TARGET Target
751 EFI_KEY_OPTION NewKeyOption
;
752 EFI_KEY_OPTION
*KeyOptionBuffer
;
753 SHELL_STATUS ShellStatus
;
759 CONST CHAR16
*Walker
;
764 CHAR16 VariableName
[12];
767 SHELL_FILE_HANDLE FileHandle
;
769 Status
= EFI_SUCCESS
;
770 ShellStatus
= SHELL_SUCCESS
;
774 KeyOptionBuffer
= NULL
;
777 ZeroMem(&NewKeyOption
, sizeof(EFI_KEY_OPTION
));
778 ZeroMem(VariableName
, sizeof(VariableName
));
780 while(Walker
[0] == L
' ') {
785 // Get the index of the variable we are changing.
787 Status
= ShellConvertStringToUint64(Walker
, &Intermediate
, FALSE
, TRUE
);
788 if (EFI_ERROR(Status
) || (((UINT16
)Intermediate
) != Intermediate
) || StrStr(Walker
, L
" ") == NULL
|| ((UINT16
)Intermediate
) > ((UINT16
)OrderCount
)) {
789 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
), gShellBcfgHiiHandle
, L
"bcfg", L
"Option Index");
790 ShellStatus
= SHELL_INVALID_PARAMETER
;
791 return (ShellStatus
);
793 OptionIndex
= (UINT16
)Intermediate
;
795 Temp
= StrStr(Walker
, L
" ");
799 while(Walker
[0] == L
' ') {
804 // determine whether we have file with data, quote delimited information, or a hot-key
806 if (Walker
[0] == L
'\"') {
808 // quoted filename or quoted information.
810 Temp
= StrStr(Walker
+1, L
"\"");
811 if (Temp
== NULL
|| StrLen(Temp
) != 1) {
812 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
), gShellBcfgHiiHandle
, L
"bcfg", Walker
);
813 ShellStatus
= SHELL_INVALID_PARAMETER
;
815 FileName
= StrnCatGrow(&FileName
, NULL
, Walker
+1, 0);
816 if (FileName
== NULL
) {
817 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_OUT_MEM
), gShellBcfgHiiHandle
, L
"bcfg");
818 ShellStatus
= SHELL_OUT_OF_RESOURCES
;
819 return (ShellStatus
);
821 Temp2
= StrStr(FileName
, L
"\"");
822 ASSERT(Temp2
!= NULL
);
823 Temp2
[0] = CHAR_NULL
;
825 if (StrLen(Temp2
)>0) {
826 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
), gShellBcfgHiiHandle
, L
"bcfg", Walker
);
827 ShellStatus
= SHELL_INVALID_PARAMETER
;
829 if (EFI_ERROR(ShellFileExists(Walker
))) {
831 // Not a file. must be misc information.
836 FileName
= StrnCatGrow(&FileName
, NULL
, Walker
, 0);
841 // filename or hot key information.
843 if (StrStr(Walker
, L
" ") == NULL
) {
847 if (EFI_ERROR(ShellFileExists(Walker
))) {
848 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_FIND_FAIL
), gShellBcfgHiiHandle
, L
"bcfg", Walker
);
849 ShellStatus
= SHELL_INVALID_PARAMETER
;
851 FileName
= StrnCatGrow(&FileName
, NULL
, Walker
, 0);
854 if (Target
!= BcfgTargetBootOrder
) {
855 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_BOOT_ONLY
), gShellBcfgHiiHandle
, L
"bcfg");
856 ShellStatus
= SHELL_INVALID_PARAMETER
;
859 if (ShellStatus
== SHELL_SUCCESS
) {
861 // Get hot key information
863 Status
= ShellConvertStringToUint64(Walker
, &Intermediate
, FALSE
, TRUE
);
864 if (EFI_ERROR(Status
) || (((UINT32
)Intermediate
) != Intermediate
) || StrStr(Walker
, L
" ") == NULL
) {
865 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
), gShellBcfgHiiHandle
, L
"bcfg", Walker
);
866 ShellStatus
= SHELL_INVALID_PARAMETER
;
868 NewKeyOption
.KeyData
.PackedValue
= (UINT32
)Intermediate
;
869 Temp
= StrStr(Walker
, L
" ");
873 while(Walker
[0] == L
' ') {
878 if (ShellStatus
== SHELL_SUCCESS
) {
880 // Now we know how many EFI_INPUT_KEY structs we need to attach to the end of the EFI_KEY_OPTION struct.
881 // Re-allocate with the added information.
883 KeyOptionBuffer
= AllocateCopyPool(sizeof(EFI_KEY_OPTION
) + (sizeof(EFI_INPUT_KEY
) * NewKeyOption
.KeyData
.Options
.InputKeyCount
), &NewKeyOption
);
884 if (KeyOptionBuffer
== NULL
) {
885 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_NO_MEM
), gShellBcfgHiiHandle
, L
"bcfg");
886 ShellStatus
= SHELL_OUT_OF_RESOURCES
;
889 for (LoopCounter
= 0 ; ShellStatus
== SHELL_SUCCESS
&& LoopCounter
< NewKeyOption
.KeyData
.Options
.InputKeyCount
; LoopCounter
++) {
893 Status
= ShellConvertStringToUint64(Walker
, &Intermediate
, FALSE
, TRUE
);
894 if (EFI_ERROR(Status
) || (((UINT16
)Intermediate
) != Intermediate
) || StrStr(Walker
, L
" ") == NULL
) {
895 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
), gShellBcfgHiiHandle
, L
"bcfg", Walker
);
896 ShellStatus
= SHELL_INVALID_PARAMETER
;
898 ((EFI_INPUT_KEY
*)(((UINT8
*)KeyOptionBuffer
) + sizeof(EFI_KEY_OPTION
)))[LoopCounter
].ScanCode
= (UINT16
)Intermediate
;
899 Temp
= StrStr(Walker
, L
" ");
903 while(Walker
[0] == L
' ') {
910 Status
= ShellConvertStringToUint64(Walker
, &Intermediate
, FALSE
, TRUE
);
911 if (EFI_ERROR(Status
) || (((UINT16
)Intermediate
) != Intermediate
)) {
912 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
), gShellBcfgHiiHandle
, L
"bcfg", Walker
);
913 ShellStatus
= SHELL_INVALID_PARAMETER
;
915 ((EFI_INPUT_KEY
*)(((UINT8
*)KeyOptionBuffer
) + sizeof(EFI_KEY_OPTION
)))[LoopCounter
].UnicodeChar
= (UINT16
)Intermediate
;
916 Temp
= StrStr(Walker
, L
" ");
920 while(Walker
[0] == L
' ') {
925 if (ShellStatus
== SHELL_SUCCESS
) {
927 // Now do the BootOption / BootOptionCrc
929 ASSERT (OptionIndex
<= OrderCount
);
930 KeyOptionBuffer
->BootOption
= CurrentOrder
[OptionIndex
];
931 Status
= GetBootOptionCrc(&(KeyOptionBuffer
->BootOptionCrc
), KeyOptionBuffer
->BootOption
);
932 if (EFI_ERROR(Status
)) {
933 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
), gShellBcfgHiiHandle
, L
"bcfg", L
"Option Index");
934 ShellStatus
= SHELL_INVALID_PARAMETER
;
938 if (ShellStatus
== SHELL_SUCCESS
) {
939 for (Temp2
= NULL
, KeyIndex
= 0 ; KeyIndex
<= 0xFFFF ; KeyIndex
++) {
940 UnicodeSPrint(VariableName
, sizeof(VariableName
), L
"Key%04x", KeyIndex
);
941 Status
= GetEfiGlobalVariable2 (VariableName
, &VariableData
, NULL
);
942 if (Status
== EFI_NOT_FOUND
) {
945 if (!EFI_ERROR(Status
)) {
946 SHELL_FREE_NON_NULL(VariableData
);
949 if (KeyIndex
<= 0xFFFF) {
950 Status
= gRT
->SetVariable(
952 (EFI_GUID
*)&gEfiGlobalVariableGuid
,
953 EFI_VARIABLE_NON_VOLATILE
|EFI_VARIABLE_BOOTSERVICE_ACCESS
|EFI_VARIABLE_RUNTIME_ACCESS
,
954 sizeof(EFI_KEY_OPTION
) + (sizeof(EFI_INPUT_KEY
) * NewKeyOption
.KeyData
.Options
.InputKeyCount
),
956 if (EFI_ERROR(Status
)) {
957 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_BCFG_SET_VAR_FAIL
), gShellBcfgHiiHandle
, L
"bcfg", VariableName
);
958 ShellStatus
= SHELL_INVALID_PARAMETER
;
961 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_BCFG_VAR_NO_NUM
), gShellBcfgHiiHandle
, L
"bcfg");
962 ShellStatus
= SHELL_INVALID_PARAMETER
;
964 ASSERT(FileName
== NULL
&& Data
== NULL
);
970 // Shouldn't be possible to have have both. Neither is ok though.
972 ASSERT(FileName
== NULL
|| Data
== NULL
);
974 if (ShellStatus
== SHELL_SUCCESS
&& (FileName
!= NULL
|| Data
!= NULL
)) {
975 if (FileName
!= NULL
) {
977 // Open the file and populate the data buffer.
979 Status
= ShellOpenFileByName(
984 if (!EFI_ERROR(Status
)) {
985 Status
= ShellGetFileSize(FileHandle
, &Intermediate
);
987 Data
= AllocateZeroPool((UINTN
)Intermediate
);
989 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_NO_MEM
), gShellBcfgHiiHandle
, L
"bcfg");
990 ShellStatus
= SHELL_OUT_OF_RESOURCES
;
992 if (!EFI_ERROR(Status
)) {
993 Status
= ShellReadFile(FileHandle
, (UINTN
*)&Intermediate
, Data
);
996 Intermediate
= StrSize(Data
);
999 if (!EFI_ERROR(Status
) && ShellStatus
== SHELL_SUCCESS
&& Data
!= NULL
) {
1000 Status
= UpdateOptionalData(CurrentOrder
[OptionIndex
], (UINTN
)Intermediate
, (UINT8
*)Data
, Target
);
1001 if (EFI_ERROR(Status
)) {
1002 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_BCFG_SET_VAR_FAIL
), gShellBcfgHiiHandle
, L
"bcfg", VariableName
);
1003 ShellStatus
= SHELL_INVALID_PARAMETER
;
1006 if (EFI_ERROR(Status
) && ShellStatus
== SHELL_SUCCESS
) {
1007 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_BCFG_SET_VAR_FAIL
), gShellBcfgHiiHandle
, L
"bcfg", VariableName
);
1008 ShellStatus
= SHELL_INVALID_PARAMETER
;
1012 SHELL_FREE_NON_NULL(Data
);
1013 SHELL_FREE_NON_NULL(KeyOptionBuffer
);
1014 SHELL_FREE_NON_NULL(FileName
);
1019 Function to dump the Bcfg information.
1021 @param[in] Op The operation.
1022 @param[in] OrderCount How many to dump.
1023 @param[in] CurrentOrder The pointer to the current order of items.
1024 @param[in] VerboseOutput TRUE for extra output. FALSE otherwise.
1026 @retval SHELL_SUCCESS The dump was successful.
1027 @retval SHELL_INVALID_PARAMETER A parameter was invalid.
1032 IN CONST CHAR16
*Op
,
1033 IN CONST UINTN OrderCount
,
1034 IN CONST UINT16
*CurrentOrder
,
1035 IN CONST BOOLEAN VerboseOutput
1041 CHAR16 VariableName
[12];
1043 CHAR16
*DevPathString
;
1046 EFI_LOAD_OPTION
*LoadOption
;
1047 CHAR16
*Description
;
1048 UINTN DescriptionSize
;
1049 UINTN OptionalDataOffset
;
1051 if (OrderCount
== 0) {
1052 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN(STR_BCFG_NONE
), gShellBcfgHiiHandle
, L
"bcfg");
1053 return (SHELL_SUCCESS
);
1058 for (LoopVar
= 0 ; LoopVar
< OrderCount
; LoopVar
++) {
1061 DevPathString
= NULL
;
1063 UnicodeSPrint(VariableName
, sizeof(VariableName
), L
"%s%04x", Op
, CurrentOrder
[LoopVar
]);
1065 Status
= gRT
->GetVariable(
1067 (EFI_GUID
*)&gEfiGlobalVariableGuid
,
1071 if (Status
== EFI_BUFFER_TOO_SMALL
) {
1072 Buffer
= AllocateZeroPool(BufferSize
);
1073 Status
= gRT
->GetVariable(
1075 (EFI_GUID
*)&gEfiGlobalVariableGuid
,
1081 if (EFI_ERROR(Status
) || Buffer
== NULL
) {
1082 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_BCFG_READ_FAIL
), gShellBcfgHiiHandle
, L
"bcfg", VariableName
);
1088 // We expect the Attributes, FilePathListLength, and L'\0'-terminated
1089 // Description fields to be present.
1091 if (BufferSize
< sizeof *LoadOption
+ sizeof (CHAR16
)) {
1096 STRING_TOKEN (STR_BCFG_VAR_CORRUPT
),
1097 gShellBcfgHiiHandle
,
1105 LoadOption
= (EFI_LOAD_OPTION
*)Buffer
;
1106 Description
= (CHAR16
*)(Buffer
+ sizeof (EFI_LOAD_OPTION
));
1107 DescriptionSize
= StrSize (Description
);
1109 if (LoadOption
->FilePathListLength
!= 0) {
1110 FilePathList
= (UINT8
*)Description
+ DescriptionSize
;
1111 DevPathString
= ConvertDevicePathToText(FilePathList
, TRUE
, FALSE
);
1114 OptionalDataOffset
= sizeof *LoadOption
+ DescriptionSize
+
1115 LoadOption
->FilePathListLength
;
1121 STRING_TOKEN(STR_BCFG_LOAD_OPTIONS
),
1122 gShellBcfgHiiHandle
,
1127 OptionalDataOffset
>= BufferSize
? L
'N' : L
'Y'
1129 if (VerboseOutput
&& (OptionalDataOffset
< BufferSize
)) {
1132 0, // Offset (displayed)
1133 BufferSize
- OptionalDataOffset
, // DataSize
1134 Buffer
+ OptionalDataOffset
// UserData
1139 if (Buffer
!= NULL
) {
1142 if (DevPathString
!= NULL
) {
1143 FreePool(DevPathString
);
1146 return (Errors
> 0) ? SHELL_INVALID_PARAMETER
: SHELL_SUCCESS
;
1150 Function to initialize the BCFG operation structure.
1152 @param[in] Struct The stuct to initialize.
1157 IN BGFG_OPERATION
*Struct
1160 ASSERT(Struct
!= NULL
);
1161 Struct
->Target
= BcfgTargetMax
;
1162 Struct
->Type
= BcfgTypeMax
;
1163 Struct
->Number1
= 0;
1164 Struct
->Number2
= 0;
1165 Struct
->HandleIndex
= 0;
1166 Struct
->FileName
= NULL
;
1167 Struct
->Description
= NULL
;
1168 Struct
->Order
= NULL
;
1169 Struct
->OptData
= NULL
;
1173 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
1175 {L
"-opt", TypeMaxValue
},
1180 Function for 'bcfg' command.
1182 @param[in] ImageHandle Handle to the Image (NULL if Internal).
1183 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
1187 ShellCommandRunBcfg (
1188 IN EFI_HANDLE ImageHandle
,
1189 IN EFI_SYSTEM_TABLE
*SystemTable
1193 LIST_ENTRY
*Package
;
1194 CHAR16
*ProblemParam
;
1195 SHELL_STATUS ShellStatus
;
1197 CONST CHAR16
*CurrentParam
;
1198 BGFG_OPERATION CurrentOperation
;
1200 UINT64 Intermediate
;
1204 ProblemParam
= NULL
;
1206 ShellStatus
= SHELL_SUCCESS
;
1208 InitBcfgStruct(&CurrentOperation
);
1211 // initialize the shell lib (we must be in non-auto-init...)
1213 Status
= ShellInitialize();
1214 ASSERT_EFI_ERROR(Status
);
1216 Status
= CommandInit();
1217 ASSERT_EFI_ERROR(Status
);
1220 // parse the command line
1222 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
1223 if (EFI_ERROR(Status
)) {
1224 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
1225 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellBcfgHiiHandle
, L
"bcfg", ProblemParam
);
1226 FreePool(ProblemParam
);
1227 ShellStatus
= SHELL_INVALID_PARAMETER
;
1233 // Read in if we are doing -OPT
1235 if (ShellCommandLineGetFlag(Package
, L
"-opt")) {
1236 CurrentOperation
.OptData
= ShellCommandLineGetValue(Package
, L
"-opt");
1237 if (CurrentOperation
.OptData
== NULL
) {
1238 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_NO_VALUE
), gShellBcfgHiiHandle
, L
"bcfg", L
"-opt");
1239 ShellStatus
= SHELL_INVALID_PARAMETER
;
1241 CurrentOperation
.Type
= BcfgTypeOpt
;
1245 // small block to read the target of the operation
1247 if ((ShellCommandLineGetCount(Package
) < 3 && CurrentOperation
.Type
!= BcfgTypeOpt
) ||
1248 (ShellCommandLineGetCount(Package
) < 2 && CurrentOperation
.Type
== BcfgTypeOpt
)
1250 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellBcfgHiiHandle
, L
"bcfg");
1251 ShellStatus
= SHELL_INVALID_PARAMETER
;
1252 } else if (gUnicodeCollation
->StriColl(gUnicodeCollation
, (CHAR16
*)ShellCommandLineGetRawValue(Package
, 1), L
"driver") == 0) {
1253 CurrentOperation
.Target
= BcfgTargetDriverOrder
;
1254 } else if (gUnicodeCollation
->StriColl(gUnicodeCollation
, (CHAR16
*)ShellCommandLineGetRawValue(Package
, 1), L
"boot") == 0) {
1255 CurrentOperation
.Target
= BcfgTargetBootOrder
;
1257 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_NO_DRIVER_BOOT
), gShellBcfgHiiHandle
, L
"bcfg");
1258 ShellStatus
= SHELL_INVALID_PARAMETER
;
1263 // Read in the boot or driver order environment variable (not needed for opt)
1265 if (ShellStatus
== SHELL_SUCCESS
&& CurrentOperation
.Target
< BcfgTargetMax
) {
1267 Status
= gRT
->GetVariable(
1268 CurrentOperation
.Target
== BcfgTargetBootOrder
?(CHAR16
*)L
"BootOrder":(CHAR16
*)L
"DriverOrder",
1269 (EFI_GUID
*)&gEfiGlobalVariableGuid
,
1272 CurrentOperation
.Order
);
1273 if (Status
== EFI_BUFFER_TOO_SMALL
) {
1274 CurrentOperation
.Order
= AllocateZeroPool(Length
+(4*sizeof(CurrentOperation
.Order
[0])));
1275 if (CurrentOperation
.Order
== NULL
) {
1276 ShellStatus
= SHELL_OUT_OF_RESOURCES
;
1278 Status
= gRT
->GetVariable(
1279 CurrentOperation
.Target
== BcfgTargetBootOrder
?(CHAR16
*)L
"BootOrder":(CHAR16
*)L
"DriverOrder",
1280 (EFI_GUID
*)&gEfiGlobalVariableGuid
,
1283 CurrentOperation
.Order
);
1288 Count
= (UINT16
) (Length
/ sizeof(CurrentOperation
.Order
[0]));
1291 // large block to read the type of operation and verify parameter types for the info.
1293 if (ShellStatus
== SHELL_SUCCESS
&& CurrentOperation
.Target
< BcfgTargetMax
) {
1294 for (ParamNumber
= 2 ; ParamNumber
< ShellCommandLineGetCount(Package
) && ShellStatus
== SHELL_SUCCESS
; ParamNumber
++) {
1295 CurrentParam
= ShellCommandLineGetRawValue(Package
, ParamNumber
);
1296 if (gUnicodeCollation
->StriColl(gUnicodeCollation
, (CHAR16
*)CurrentParam
, L
"dump") == 0) {
1297 CurrentOperation
.Type
= BcfgTypeDump
;
1298 } else if (ShellCommandLineGetFlag(Package
, L
"-v")) {
1299 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
), gShellBcfgHiiHandle
, L
"bcfg", L
"-v (without dump)");
1300 ShellStatus
= SHELL_INVALID_PARAMETER
;
1301 } else if (gUnicodeCollation
->StriColl(gUnicodeCollation
, (CHAR16
*)CurrentParam
, L
"add") == 0) {
1302 if ((ParamNumber
+ 3) >= ShellCommandLineGetCount(Package
)) {
1303 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellBcfgHiiHandle
, L
"bcfg");
1304 ShellStatus
= SHELL_INVALID_PARAMETER
;
1306 CurrentOperation
.Type
= BcfgTypeAdd
;
1307 CurrentParam
= ShellCommandLineGetRawValue(Package
, ++ParamNumber
);
1308 if (CurrentParam
== NULL
|| !ShellIsHexOrDecimalNumber(CurrentParam
, TRUE
, FALSE
)) {
1309 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
), gShellBcfgHiiHandle
, L
"bcfg", CurrentParam
);
1310 ShellStatus
= SHELL_INVALID_PARAMETER
;
1312 Status
= ShellConvertStringToUint64(CurrentParam
, &Intermediate
, TRUE
, FALSE
);
1313 CurrentOperation
.Number1
= (UINT16
)Intermediate
;
1314 ASSERT(CurrentOperation
.FileName
== NULL
);
1315 CurrentOperation
.FileName
= StrnCatGrow(&CurrentOperation
.FileName
, NULL
, ShellCommandLineGetRawValue(Package
, ++ParamNumber
), 0);
1316 ASSERT(CurrentOperation
.Description
== NULL
);
1317 CurrentOperation
.Description
= StrnCatGrow(&CurrentOperation
.Description
, NULL
, ShellCommandLineGetRawValue(Package
, ++ParamNumber
), 0);
1319 } else if (gUnicodeCollation
->StriColl(gUnicodeCollation
, (CHAR16
*)CurrentParam
, L
"addp") == 0) {
1320 if ((ParamNumber
+ 3) >= ShellCommandLineGetCount(Package
)) {
1321 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellBcfgHiiHandle
, L
"bcfg");
1322 ShellStatus
= SHELL_INVALID_PARAMETER
;
1324 CurrentOperation
.Type
= BcfgTypeAddp
;
1325 CurrentParam
= ShellCommandLineGetRawValue(Package
, ++ParamNumber
);
1326 if (CurrentParam
== NULL
|| !ShellIsHexOrDecimalNumber(CurrentParam
, TRUE
, FALSE
)) {
1327 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
), gShellBcfgHiiHandle
, L
"bcfg", CurrentParam
);
1328 ShellStatus
= SHELL_INVALID_PARAMETER
;
1330 Status
= ShellConvertStringToUint64(CurrentParam
, &Intermediate
, TRUE
, FALSE
);
1331 CurrentOperation
.Number1
= (UINT16
)Intermediate
;
1332 ASSERT(CurrentOperation
.FileName
== NULL
);
1333 CurrentOperation
.FileName
= StrnCatGrow(&CurrentOperation
.FileName
, NULL
, ShellCommandLineGetRawValue(Package
, ++ParamNumber
), 0);
1334 ASSERT(CurrentOperation
.Description
== NULL
);
1335 CurrentOperation
.Description
= StrnCatGrow(&CurrentOperation
.Description
, NULL
, ShellCommandLineGetRawValue(Package
, ++ParamNumber
), 0);
1337 } else if (gUnicodeCollation
->StriColl(gUnicodeCollation
, (CHAR16
*)CurrentParam
, L
"addh") == 0) {
1338 if ((ParamNumber
+ 3) >= ShellCommandLineGetCount(Package
)) {
1339 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellBcfgHiiHandle
, L
"bcfg");
1340 ShellStatus
= SHELL_INVALID_PARAMETER
;
1342 CurrentOperation
.Type
= BcfgTypeAddh
;
1343 CurrentParam
= ShellCommandLineGetRawValue(Package
, ++ParamNumber
);
1344 if (CurrentParam
== NULL
|| !ShellIsHexOrDecimalNumber(CurrentParam
, TRUE
, FALSE
)) {
1345 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
), gShellBcfgHiiHandle
, L
"bcfg", CurrentParam
);
1346 ShellStatus
= SHELL_INVALID_PARAMETER
;
1348 Status
= ShellConvertStringToUint64(CurrentParam
, &Intermediate
, TRUE
, FALSE
);
1349 CurrentOperation
.Number1
= (UINT16
)Intermediate
;
1350 CurrentParam
= ShellCommandLineGetRawValue(Package
, ++ParamNumber
);
1351 if (CurrentParam
== NULL
|| !ShellIsHexOrDecimalNumber(CurrentParam
, TRUE
, FALSE
)) {
1352 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
), gShellBcfgHiiHandle
, L
"bcfg", CurrentParam
);
1353 ShellStatus
= SHELL_INVALID_PARAMETER
;
1355 Status
= ShellConvertStringToUint64(CurrentParam
, &Intermediate
, TRUE
, FALSE
);
1356 CurrentOperation
.HandleIndex
= (UINT16
)Intermediate
;
1357 ASSERT(CurrentOperation
.Description
== NULL
);
1358 CurrentOperation
.Description
= StrnCatGrow(&CurrentOperation
.Description
, NULL
, ShellCommandLineGetRawValue(Package
, ++ParamNumber
), 0);
1361 } else if (gUnicodeCollation
->StriColl(gUnicodeCollation
, (CHAR16
*)CurrentParam
, L
"rm") == 0) {
1362 if ((ParamNumber
+ 1) >= ShellCommandLineGetCount(Package
)) {
1363 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellBcfgHiiHandle
, L
"bcfg");
1364 ShellStatus
= SHELL_INVALID_PARAMETER
;
1366 CurrentOperation
.Type
= BcfgTypeRm
;
1367 CurrentParam
= ShellCommandLineGetRawValue(Package
, ++ParamNumber
);
1368 if (CurrentParam
== NULL
|| !ShellIsHexOrDecimalNumber(CurrentParam
, TRUE
, FALSE
)) {
1369 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
), gShellBcfgHiiHandle
, L
"bcfg", CurrentParam
);
1370 ShellStatus
= SHELL_INVALID_PARAMETER
;
1372 Status
= ShellConvertStringToUint64(CurrentParam
, &Intermediate
, TRUE
, FALSE
);
1373 CurrentOperation
.Number1
= (UINT16
)Intermediate
;
1374 if (CurrentOperation
.Number1
>= Count
){
1375 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_BCFG_NUMB_RANGE
), gShellBcfgHiiHandle
, L
"bcfg", Count
);
1376 ShellStatus
= SHELL_INVALID_PARAMETER
;
1379 } else if (gUnicodeCollation
->StriColl(gUnicodeCollation
, (CHAR16
*)CurrentParam
, L
"mv") == 0) {
1380 if ((ParamNumber
+ 2) >= ShellCommandLineGetCount(Package
)) {
1381 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellBcfgHiiHandle
, L
"bcfg");
1382 ShellStatus
= SHELL_INVALID_PARAMETER
;
1384 CurrentOperation
.Type
= BcfgTypeMv
;
1385 CurrentParam
= ShellCommandLineGetRawValue(Package
, ++ParamNumber
);
1386 if (CurrentParam
== NULL
|| !ShellIsHexOrDecimalNumber(CurrentParam
, TRUE
, FALSE
)) {
1387 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
), gShellBcfgHiiHandle
, L
"bcfg", CurrentParam
);
1388 ShellStatus
= SHELL_INVALID_PARAMETER
;
1390 Status
= ShellConvertStringToUint64(CurrentParam
, &Intermediate
, TRUE
, FALSE
);
1391 CurrentOperation
.Number1
= (UINT16
)Intermediate
;
1392 if (CurrentOperation
.Number1
>= Count
){
1393 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_BCFG_NUMB_RANGE
), gShellBcfgHiiHandle
, L
"bcfg", Count
);
1394 ShellStatus
= SHELL_INVALID_PARAMETER
;
1396 CurrentParam
= ShellCommandLineGetRawValue(Package
, ++ParamNumber
);
1397 if (CurrentParam
== NULL
|| !ShellIsHexOrDecimalNumber(CurrentParam
, TRUE
, FALSE
)) {
1398 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
), gShellBcfgHiiHandle
, L
"bcfg", CurrentParam
);
1399 ShellStatus
= SHELL_INVALID_PARAMETER
;
1401 Status
= ShellConvertStringToUint64(CurrentParam
, &Intermediate
, TRUE
, FALSE
);
1402 CurrentOperation
.Number2
= (UINT16
)Intermediate
;
1404 if (CurrentOperation
.Number2
== CurrentOperation
.Number1
1405 ||CurrentOperation
.Number2
>= Count
1407 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_BCFG_NUMB_RANGE
), gShellBcfgHiiHandle
, L
"bcfg", Count
);
1408 ShellStatus
= SHELL_INVALID_PARAMETER
;
1413 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
), gShellBcfgHiiHandle
, L
"bcfg", CurrentParam
);
1414 ShellStatus
= SHELL_INVALID_PARAMETER
;
1418 if (ShellStatus
== SHELL_SUCCESS
&& CurrentOperation
.Target
< BcfgTargetMax
&& CurrentOperation
.Type
< BcfgTypeMax
) {
1420 // we have all the info. Do the work
1422 switch (CurrentOperation
.Type
) {
1424 ShellStatus
= BcfgDisplayDump(
1425 CurrentOperation
.Target
== BcfgTargetBootOrder
?L
"Boot":L
"Driver",
1427 CurrentOperation
.Order
,
1428 ShellCommandLineGetFlag(Package
, L
"-v"));
1431 ShellStatus
= BcfgMove(
1432 CurrentOperation
.Target
,
1433 CurrentOperation
.Order
,
1435 CurrentOperation
.Number1
,
1436 CurrentOperation
.Number2
);
1439 ShellStatus
= BcfgRemove(
1440 CurrentOperation
.Target
,
1441 CurrentOperation
.Order
,
1443 CurrentOperation
.Number1
);
1448 ShellStatus
= BcfgAdd(
1449 CurrentOperation
.Number1
,
1450 CurrentOperation
.FileName
,
1451 CurrentOperation
.Description
==NULL
?L
"":CurrentOperation
.Description
,
1452 CurrentOperation
.Order
,
1454 CurrentOperation
.Target
,
1455 (BOOLEAN
)(CurrentOperation
.Type
== BcfgTypeAddh
),
1456 (BOOLEAN
)(CurrentOperation
.Type
== BcfgTypeAddp
),
1457 CurrentOperation
.HandleIndex
);
1460 ShellStatus
= BcfgAddOpt(
1461 CurrentOperation
.OptData
,
1462 CurrentOperation
.Order
,
1464 CurrentOperation
.Target
);
1472 if (Package
!= NULL
) {
1473 ShellCommandLineFreeVarList (Package
);
1475 if (CurrentOperation
.FileName
!= NULL
) {
1476 FreePool(CurrentOperation
.FileName
);
1478 if (CurrentOperation
.Description
!= NULL
) {
1479 FreePool(CurrentOperation
.Description
);
1481 if (CurrentOperation
.Order
!= NULL
) {
1482 FreePool(CurrentOperation
.Order
);
1485 return (ShellStatus
);
1490 Function to get the filename with help context if HII will not be used.
1492 @return The filename with help text in it.
1496 ShellCommandGetManFileNameBcfg (
1504 "Constructor" for the library.
1506 This will register the handler for the bcfg command.
1508 @param[in] ImageHandle the image handle of the process
1509 @param[in] SystemTable the EFI System Table pointer
1510 @param[in] Name the profile name to use
1512 @retval EFI_SUCCESS the shell command handlers were installed sucessfully
1513 @retval EFI_UNSUPPORTED the shell level required was not found.
1517 BcfgLibraryRegisterBcfgCommand (
1518 IN EFI_HANDLE ImageHandle
,
1519 IN EFI_SYSTEM_TABLE
*SystemTable
,
1520 IN CONST CHAR16
*Name
1523 if (gShellBcfgHiiHandle
!= NULL
) {
1524 return (EFI_SUCCESS
);
1527 gShellBcfgHiiHandle
= HiiAddPackages (&gShellBcfgHiiGuid
, gImageHandle
, UefiShellBcfgCommandLibStrings
, NULL
);
1528 if (gShellBcfgHiiHandle
== NULL
) {
1529 return (EFI_DEVICE_ERROR
);
1533 // install our shell command handler
1535 ShellCommandRegisterCommandName(L
"bcfg", ShellCommandRunBcfg
, ShellCommandGetManFileNameBcfg
, 0, Name
, FALSE
, gShellBcfgHiiHandle
, STRING_TOKEN(STR_GET_HELP_BCFG
));
1537 return (EFI_SUCCESS
);
1541 Destructor for the library. free any resources.
1543 @param ImageHandle The image handle of the process.
1544 @param SystemTable The EFI System Table pointer.
1548 BcfgLibraryUnregisterBcfgCommand (
1549 IN EFI_HANDLE ImageHandle
,
1550 IN EFI_SYSTEM_TABLE
*SystemTable
1553 if (gShellBcfgHiiHandle
!= NULL
) {
1554 HiiRemovePackages(gShellBcfgHiiHandle
);
1556 gShellBcfgHiiHandle
= NULL
;
1557 return (EFI_SUCCESS
);