]> git.proxmox.com Git - mirror_edk2.git/blame - EmbeddedPkg/Ebl/Variable.c
ArmPlatformPkg: remove EblCmdLib implementation
[mirror_edk2.git] / EmbeddedPkg / Ebl / Variable.c
CommitLineData
a6caee65 1/** @file\r
2*\r
3* Copyright (c) 2011, ARM Limited. All rights reserved.\r
50c6a4d2 4* (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>\r
a6caee65 5*\r
3402aac7
RC
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
a6caee65 13*\r
14**/\r
fb334ef6 15\r
16#include "Ebl.h"\r
a6caee65 17\r
18#include <Guid/GlobalVariable.h>\r
fb334ef6 19\r
20EFI_STATUS\r
50c6a4d2 21EFIAPI\r
fb334ef6 22EblGetCmd (\r
23 IN UINTN Argc,\r
24 IN CHAR8 **Argv\r
25 )\r
26{\r
27 EFI_STATUS Status = EFI_INVALID_PARAMETER;\r
a6caee65 28 UINTN Size;\r
29 VOID* Value;\r
30 CHAR8* AsciiVariableName = NULL;\r
31 CHAR16* VariableName;\r
5140a6df 32 UINTN VariableNameLen;\r
a6caee65 33 UINT32 Index;\r
34\r
35 if (Argc == 1) {\r
36 AsciiPrint("Variable name is missing.\n");\r
37 return Status;\r
38 }\r
39\r
40 for (Index = 1; Index < Argc; Index++) {\r
41 if (Argv[Index][0] == '-') {\r
42 AsciiPrint("Warning: '%a' not recognized.\n",Argv[Index]);\r
43 } else {\r
44 AsciiVariableName = Argv[Index];\r
45 }\r
46 }\r
47\r
48 if (AsciiVariableName == NULL) {\r
49 AsciiPrint("Variable name is missing.\n");\r
50 return Status;\r
51 } else {\r
5140a6df
AB
52 VariableNameLen = AsciiStrLen (AsciiVariableName) + 1;\r
53 VariableName = AllocatePool (VariableNameLen * sizeof (CHAR16));\r
54 AsciiStrToUnicodeStrS (AsciiVariableName, VariableName, VariableNameLen);\r
a6caee65 55 }\r
56\r
57 // Try to get the variable size.\r
58 Value = NULL;\r
59 Size = 0;\r
60 Status = gRT->GetVariable (VariableName, &gEfiGlobalVariableGuid, NULL, &Size, Value);\r
61 if (Status == EFI_NOT_FOUND) {\r
62 AsciiPrint("Variable name '%s' not found.\n",VariableName);\r
63 } else if (Status == EFI_BUFFER_TOO_SMALL) {\r
64 // Get the environment variable value\r
65 Value = AllocatePool (Size);\r
66 if (Value == NULL) {\r
67 return EFI_OUT_OF_RESOURCES;\r
68 }\r
69\r
70 Status = gRT->GetVariable ((CHAR16 *)VariableName, &gEfiGlobalVariableGuid, NULL, &Size, Value);\r
71 if (EFI_ERROR (Status)) {\r
72 AsciiPrint("Error: '%r'\n",Status);\r
73 } else {\r
74 AsciiPrint("%a=%a\n",AsciiVariableName,Value);\r
75 }\r
76 FreePool(Value);\r
77 } else {\r
78 AsciiPrint("Error: '%r'\n",Status);\r
fb334ef6 79 }\r
80\r
81 FreePool(VariableName);\r
82 return Status;\r
83}\r
84\r
85EFI_STATUS\r
50c6a4d2 86EFIAPI\r
fb334ef6 87EblSetCmd (\r
88 IN UINTN Argc,\r
89 IN CHAR8 **Argv\r
90 )\r
91{\r
92 EFI_STATUS Status = EFI_INVALID_PARAMETER;\r
a6caee65 93 CHAR8* AsciiVariableSetting = NULL;\r
94 CHAR8* AsciiVariableName;\r
95 CHAR8* AsciiValue;\r
96 UINT32 AsciiValueLength;\r
97 CHAR16* VariableName;\r
5140a6df 98 UINTN VariableNameLen;\r
fb334ef6 99 UINT32 Index;\r
100 UINT32 EscapedQuotes = 0;\r
101 BOOLEAN Volatile = FALSE;\r
102\r
a6caee65 103 if (Argc == 1) {\r
104 AsciiPrint("Variable name is missing.\n");\r
105 return Status;\r
106 }\r
107\r
108 for (Index = 1; Index < Argc; Index++) {\r
109 if (AsciiStrCmp(Argv[Index],"-v") == 0) {\r
110 Volatile = 0;\r
111 } else if (Argv[Index][0] == '-') {\r
112 AsciiPrint("Warning: '%a' not recognized.\n",Argv[Index]);\r
113 } else {\r
114 AsciiVariableSetting = Argv[Index];\r
115 }\r
116 }\r
117\r
118 if (AsciiVariableSetting == NULL) {\r
119 AsciiPrint("Variable name is missing.\n");\r
120 return Status;\r
121 }\r
3402aac7 122\r
a6caee65 123 // Check if it is a valid variable setting\r
fb334ef6 124 AsciiValue = AsciiStrStr (AsciiVariableSetting,"=");\r
a6caee65 125 if (AsciiValue == NULL) {\r
126 //\r
127 // There is no value. It means this variable will be deleted\r
128 //\r
129\r
130 // Convert VariableName into Unicode\r
5140a6df
AB
131 VariableNameLen = AsciiStrLen (AsciiVariableSetting) + 1;\r
132 VariableName = AllocatePool (VariableNameLen * sizeof (CHAR16));\r
133 AsciiStrToUnicodeStrS (AsciiVariableSetting, VariableName, VariableNameLen);\r
a6caee65 134\r
135 Status = gRT->SetVariable (\r
136 VariableName,\r
137 &gEfiGlobalVariableGuid,\r
138 ( !Volatile ? EFI_VARIABLE_NON_VOLATILE : 0) |\r
139 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
140 0,\r
141 NULL\r
142 );\r
143 if (!EFI_ERROR(Status)) {\r
144 AsciiPrint("Variable '%s' deleted\n",VariableName);\r
145 } else {\r
146 AsciiPrint("Variable setting is incorrect. It should be VariableName=Value\n");\r
147 }\r
148 return Status;\r
fb334ef6 149 }\r
150\r
151 AsciiValue[0] = '\0';\r
152 AsciiVariableName = AsciiVariableSetting;\r
153 AsciiValue++;\r
154\r
155 // Clean AsciiValue from quote\r
156 if (AsciiValue[0] == '"') {\r
157 AsciiValue++;\r
158 }\r
159 AsciiValueLength = AsciiStrLen (AsciiValue);\r
160 if ((AsciiValue[AsciiValueLength-2] != '\\') && (AsciiValue[AsciiValueLength-1] == '"')) {\r
161 AsciiValue[AsciiValueLength-1] = '\0';\r
162 }\r
163\r
164 // Clean AsciiValue from escaped quotes\r
165 for (Index = 0; Index < AsciiValueLength; Index++) {\r
166 if ((Index > 0) && (AsciiValue[Index-1] == '\\') && (AsciiValue[Index] == '"')) {\r
167 EscapedQuotes++;\r
168 }\r
169 AsciiValue[Index-EscapedQuotes] = AsciiValue[Index];\r
170 }\r
171 // Fill the end of the value with '\0'\r
172 for (Index = 0; Index < EscapedQuotes; Index++) {\r
173 AsciiValue[AsciiValueLength-1-Index] = '\0';\r
174 }\r
175\r
176 // Convert VariableName into Unicode\r
5140a6df
AB
177 VariableNameLen = AsciiStrLen (AsciiVariableName) + 1;\r
178 VariableName = AllocatePool (VariableNameLen * sizeof (CHAR16));\r
179 AsciiStrToUnicodeStrS (AsciiVariableName, VariableName, VariableNameLen);\r
fb334ef6 180\r
a6caee65 181 Status = gRT->SetVariable (\r
182 VariableName,\r
183 &gEfiGlobalVariableGuid,\r
184 ( !Volatile ? EFI_VARIABLE_NON_VOLATILE : 0) |\r
185 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
186 AsciiStrLen (AsciiValue)+1,\r
187 AsciiValue\r
fb334ef6 188 );\r
20fd35dc 189 if (!EFI_ERROR(Status)) {\r
190 AsciiPrint("'%a'='%a'\n",AsciiVariableName,AsciiValue);\r
a6caee65 191 }\r
fb334ef6 192\r
193 return Status;\r
194}\r
195\r
196GLOBAL_REMOVE_IF_UNREFERENCED const EBL_COMMAND_TABLE mCmdVariableTemplate[] =\r
197{\r
198 {\r
199 "get",\r
200 " ; get UEFI variable\n\r [v]; verbose",\r
201 NULL,\r
202 EblGetCmd\r
203 },\r
204 {\r
205 "set",\r
206 " ; set UEFI variable\n\r [v]; create volatile variable",\r
207 NULL,\r
208 EblSetCmd\r
209 }\r
210};\r
211\r
212/**\r
213 Initialize the commands in this in this file\r
214**/\r
215VOID\r
216EblInitializeVariableCmds (\r
217 VOID\r
218 )\r
219{\r
220 EblAddCommands (mCmdVariableTemplate, sizeof (mCmdVariableTemplate)/sizeof (EBL_COMMAND_TABLE));\r
221}\r