]> git.proxmox.com Git - mirror_edk2.git/blame - EmbeddedPkg/Ebl/Variable.c
ArmPlatformPkg: Remove PcdStandalone from Sec module and Introduce ArmPlatformSecExtr...
[mirror_edk2.git] / EmbeddedPkg / Ebl / Variable.c
CommitLineData
a6caee65 1/** @file\r
2*\r
3* Copyright (c) 2011, ARM Limited. All rights reserved.\r
4* \r
5* This program and the accompanying materials \r
6* are licensed and made available under the terms and conditions of the BSD License \r
7* which accompanies this distribution. The full text of the license may be found at \r
8* http://opensource.org/licenses/bsd-license.php \r
9*\r
10* THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
11* WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
12*\r
13**/\r
fb334ef6 14\r
15#include "Ebl.h"\r
a6caee65 16\r
17#include <Guid/GlobalVariable.h>\r
fb334ef6 18\r
19EFI_STATUS\r
20EblGetCmd (\r
21 IN UINTN Argc,\r
22 IN CHAR8 **Argv\r
23 )\r
24{\r
25 EFI_STATUS Status = EFI_INVALID_PARAMETER;\r
a6caee65 26 UINTN Size;\r
27 VOID* Value;\r
28 CHAR8* AsciiVariableName = NULL;\r
29 CHAR16* VariableName;\r
30 UINT32 Index;\r
31\r
32 if (Argc == 1) {\r
33 AsciiPrint("Variable name is missing.\n");\r
34 return Status;\r
35 }\r
36\r
37 for (Index = 1; Index < Argc; Index++) {\r
38 if (Argv[Index][0] == '-') {\r
39 AsciiPrint("Warning: '%a' not recognized.\n",Argv[Index]);\r
40 } else {\r
41 AsciiVariableName = Argv[Index];\r
42 }\r
43 }\r
44\r
45 if (AsciiVariableName == NULL) {\r
46 AsciiPrint("Variable name is missing.\n");\r
47 return Status;\r
48 } else {\r
49 VariableName = AllocatePool((AsciiStrLen (AsciiVariableName) + 1) * sizeof (CHAR16));\r
50 AsciiStrToUnicodeStr (AsciiVariableName,VariableName);\r
51 }\r
52\r
53 // Try to get the variable size.\r
54 Value = NULL;\r
55 Size = 0;\r
56 Status = gRT->GetVariable (VariableName, &gEfiGlobalVariableGuid, NULL, &Size, Value);\r
57 if (Status == EFI_NOT_FOUND) {\r
58 AsciiPrint("Variable name '%s' not found.\n",VariableName);\r
59 } else if (Status == EFI_BUFFER_TOO_SMALL) {\r
60 // Get the environment variable value\r
61 Value = AllocatePool (Size);\r
62 if (Value == NULL) {\r
63 return EFI_OUT_OF_RESOURCES;\r
64 }\r
65\r
66 Status = gRT->GetVariable ((CHAR16 *)VariableName, &gEfiGlobalVariableGuid, NULL, &Size, Value);\r
67 if (EFI_ERROR (Status)) {\r
68 AsciiPrint("Error: '%r'\n",Status);\r
69 } else {\r
70 AsciiPrint("%a=%a\n",AsciiVariableName,Value);\r
71 }\r
72 FreePool(Value);\r
73 } else {\r
74 AsciiPrint("Error: '%r'\n",Status);\r
fb334ef6 75 }\r
76\r
77 FreePool(VariableName);\r
78 return Status;\r
79}\r
80\r
81EFI_STATUS\r
82EblSetCmd (\r
83 IN UINTN Argc,\r
84 IN CHAR8 **Argv\r
85 )\r
86{\r
87 EFI_STATUS Status = EFI_INVALID_PARAMETER;\r
a6caee65 88 CHAR8* AsciiVariableSetting = NULL;\r
89 CHAR8* AsciiVariableName;\r
90 CHAR8* AsciiValue;\r
91 UINT32 AsciiValueLength;\r
92 CHAR16* VariableName;\r
fb334ef6 93 UINT32 Index;\r
94 UINT32 EscapedQuotes = 0;\r
95 BOOLEAN Volatile = FALSE;\r
96\r
a6caee65 97 if (Argc == 1) {\r
98 AsciiPrint("Variable name is missing.\n");\r
99 return Status;\r
100 }\r
101\r
102 for (Index = 1; Index < Argc; Index++) {\r
103 if (AsciiStrCmp(Argv[Index],"-v") == 0) {\r
104 Volatile = 0;\r
105 } else if (Argv[Index][0] == '-') {\r
106 AsciiPrint("Warning: '%a' not recognized.\n",Argv[Index]);\r
107 } else {\r
108 AsciiVariableSetting = Argv[Index];\r
109 }\r
110 }\r
111\r
112 if (AsciiVariableSetting == NULL) {\r
113 AsciiPrint("Variable name is missing.\n");\r
114 return Status;\r
115 }\r
116 \r
117 // Check if it is a valid variable setting\r
fb334ef6 118 AsciiValue = AsciiStrStr (AsciiVariableSetting,"=");\r
a6caee65 119 if (AsciiValue == NULL) {\r
120 //\r
121 // There is no value. It means this variable will be deleted\r
122 //\r
123\r
124 // Convert VariableName into Unicode\r
125 VariableName = AllocatePool((AsciiStrLen (AsciiVariableSetting) + 1) * sizeof (CHAR16));\r
126 AsciiStrToUnicodeStr (AsciiVariableSetting,VariableName);\r
127\r
128 Status = gRT->SetVariable (\r
129 VariableName,\r
130 &gEfiGlobalVariableGuid,\r
131 ( !Volatile ? EFI_VARIABLE_NON_VOLATILE : 0) |\r
132 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
133 0,\r
134 NULL\r
135 );\r
136 if (!EFI_ERROR(Status)) {\r
137 AsciiPrint("Variable '%s' deleted\n",VariableName);\r
138 } else {\r
139 AsciiPrint("Variable setting is incorrect. It should be VariableName=Value\n");\r
140 }\r
141 return Status;\r
fb334ef6 142 }\r
143\r
144 AsciiValue[0] = '\0';\r
145 AsciiVariableName = AsciiVariableSetting;\r
146 AsciiValue++;\r
147\r
148 // Clean AsciiValue from quote\r
149 if (AsciiValue[0] == '"') {\r
150 AsciiValue++;\r
151 }\r
152 AsciiValueLength = AsciiStrLen (AsciiValue);\r
153 if ((AsciiValue[AsciiValueLength-2] != '\\') && (AsciiValue[AsciiValueLength-1] == '"')) {\r
154 AsciiValue[AsciiValueLength-1] = '\0';\r
155 }\r
156\r
157 // Clean AsciiValue from escaped quotes\r
158 for (Index = 0; Index < AsciiValueLength; Index++) {\r
159 if ((Index > 0) && (AsciiValue[Index-1] == '\\') && (AsciiValue[Index] == '"')) {\r
160 EscapedQuotes++;\r
161 }\r
162 AsciiValue[Index-EscapedQuotes] = AsciiValue[Index];\r
163 }\r
164 // Fill the end of the value with '\0'\r
165 for (Index = 0; Index < EscapedQuotes; Index++) {\r
166 AsciiValue[AsciiValueLength-1-Index] = '\0';\r
167 }\r
168\r
169 // Convert VariableName into Unicode\r
a6caee65 170 VariableName = AllocatePool((AsciiStrLen (AsciiVariableName) + 1) * sizeof (CHAR16));\r
fb334ef6 171 AsciiStrToUnicodeStr (AsciiVariableName,VariableName);\r
172\r
a6caee65 173 Status = gRT->SetVariable (\r
174 VariableName,\r
175 &gEfiGlobalVariableGuid,\r
176 ( !Volatile ? EFI_VARIABLE_NON_VOLATILE : 0) |\r
177 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
178 AsciiStrLen (AsciiValue)+1,\r
179 AsciiValue\r
fb334ef6 180 );\r
20fd35dc 181 if (!EFI_ERROR(Status)) {\r
182 AsciiPrint("'%a'='%a'\n",AsciiVariableName,AsciiValue);\r
a6caee65 183 }\r
fb334ef6 184\r
185 return Status;\r
186}\r
187\r
188GLOBAL_REMOVE_IF_UNREFERENCED const EBL_COMMAND_TABLE mCmdVariableTemplate[] =\r
189{\r
190 {\r
191 "get",\r
192 " ; get UEFI variable\n\r [v]; verbose",\r
193 NULL,\r
194 EblGetCmd\r
195 },\r
196 {\r
197 "set",\r
198 " ; set UEFI variable\n\r [v]; create volatile variable",\r
199 NULL,\r
200 EblSetCmd\r
201 }\r
202};\r
203\r
204/**\r
205 Initialize the commands in this in this file\r
206**/\r
207VOID\r
208EblInitializeVariableCmds (\r
209 VOID\r
210 )\r
211{\r
212 EblAddCommands (mCmdVariableTemplate, sizeof (mCmdVariableTemplate)/sizeof (EBL_COMMAND_TABLE));\r
213}\r