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