]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/C/VfrCompile/VfrError.cpp
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / BaseTools / Source / C / VfrCompile / VfrError.cpp
1 /** @file
2
3 VfrCompiler error handler.
4
5 Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10 #include "stdio.h"
11 #include "string.h"
12 #include "stdlib.h"
13 #include "VfrError.h"
14 #include "EfiUtilityMsgs.h"
15
16 static SVFR_ERROR_HANDLE VFR_ERROR_HANDLE_TABLE [] = {
17 { VFR_RETURN_SUCCESS, NULL },
18 { VFR_RETURN_ERROR_SKIPED, NULL },
19 { VFR_RETURN_FATAL_ERROR, ": fatal error!!" },
20
21 { VFR_RETURN_MISMATCHED, ": unexpected token" },
22 { VFR_RETURN_INVALID_PARAMETER, ": invalid parameter" },
23 { VFR_RETURN_OUT_FOR_RESOURCES, ": system out of memory" },
24 { VFR_RETURN_UNSUPPORTED, ": unsupported" },
25 { VFR_RETURN_REDEFINED, ": already defined" },
26 { VFR_RETURN_FORMID_REDEFINED, ": form id already defined" },
27 { VFR_RETURN_QUESTIONID_REDEFINED, ": question id already defined" },
28 { VFR_RETURN_VARSTOREID_REDEFINED, ": varstore id already defined" },
29 { VFR_RETURN_UNDEFINED, ": undefined" },
30 { VFR_RETURN_VAR_NOTDEFINED_BY_QUESTION, ": some variable has not defined by a question"},
31 { VFR_RETURN_VARSTORE_DATATYPE_REDEFINED_ERROR, ": Data Structure is defined by more than one varstores, it can't be referred as varstore, only varstore name could be used."},
32 { VFR_RETURN_GET_EFIVARSTORE_ERROR, ": get efi varstore error"},
33 { VFR_RETURN_EFIVARSTORE_USE_ERROR, ": can not use the efi varstore like this" },
34 { VFR_RETURN_EFIVARSTORE_SIZE_ERROR, ": unsupport efi varstore size should be <= 8 bytes" },
35 { VFR_RETURN_GET_NVVARSTORE_ERROR, ": get name value varstore error" },
36 { VFR_RETURN_QVAR_REUSE, ": variable reused by more than one question" },
37 { VFR_RETURN_FLAGS_UNSUPPORTED, ": flags unsupported" },
38 { VFR_RETURN_ERROR_ARRARY_NUM, ": array number error, the valid value is in (0 ~ MAX_INDEX-1) for UEFI vfr and in (1 ~ MAX_INDEX) for Framework Vfr" },
39 { VFR_RETURN_DATA_STRING_ERROR, ": data field string error or not support"},
40 { VFR_RETURN_DEFAULT_VALUE_REDEFINED, ": default value re-defined with different value"},
41 { VFR_RETURN_CONSTANT_ONLY, ": only constant is allowed in the expression"},
42 { VFR_RETURN_VARSTORE_NAME_REDEFINED_ERROR, ": Varstore name is defined by more than one varstores, it can't be referred as varstore, only varstore structure name could be used."},
43 { VFR_RETURN_BIT_WIDTH_ERROR, ": bit width must be <= sizeof (type) * 8 and the max width can not > 32" },
44 { VFR_RETURN_STRING_TO_UINT_OVERFLOW, ": String to UINT* Overflow"},
45 { VFR_RETURN_CODEUNDEFINED, ": undefined Error Code" }
46 };
47
48 static SVFR_WARNING_HANDLE VFR_WARNING_HANDLE_TABLE [] = {
49 { VFR_WARNING_DEFAULT_VALUE_REDEFINED, ": default value re-defined with different value"},
50 { VFR_WARNING_ACTION_WITH_TEXT_TWO, ": Action opcode should not have TextTwo part"},
51 { VFR_WARNING_OBSOLETED_FRAMEWORK_OPCODE, ": Not recommend to use obsoleted framework opcode"},
52 { VFR_WARNING_CODEUNDEFINED, ": undefined Warning Code" }
53 };
54
55 CVfrErrorHandle::CVfrErrorHandle (
56 VOID
57 )
58 {
59 mInputFileName = NULL;
60 mScopeRecordListHead = NULL;
61 mScopeRecordListTail = NULL;
62 mVfrErrorHandleTable = VFR_ERROR_HANDLE_TABLE;
63 mVfrWarningHandleTable = VFR_WARNING_HANDLE_TABLE;
64 mWarningAsError = FALSE;
65 }
66
67 CVfrErrorHandle::~CVfrErrorHandle (
68 VOID
69 )
70 {
71 SVfrFileScopeRecord *pNode = NULL;
72
73 if (mInputFileName != NULL) {
74 delete[] mInputFileName;
75 }
76
77 while (mScopeRecordListHead != NULL) {
78 pNode = mScopeRecordListHead;
79 mScopeRecordListHead = mScopeRecordListHead->mNext;
80 delete pNode;
81 }
82
83 mScopeRecordListHead = NULL;
84 mScopeRecordListTail = NULL;
85 mVfrErrorHandleTable = NULL;
86 mVfrWarningHandleTable = NULL;
87 }
88
89 VOID
90 CVfrErrorHandle::SetWarningAsError (
91 IN BOOLEAN WarningAsError
92 )
93 {
94 mWarningAsError = WarningAsError;
95 }
96
97 VOID
98 CVfrErrorHandle::SetInputFile (
99 IN CHAR8 *InputFile
100 )
101 {
102 if (InputFile != NULL) {
103 mInputFileName = new CHAR8[strlen(InputFile) + 1];
104 strcpy (mInputFileName, InputFile);
105 }
106 }
107
108 SVfrFileScopeRecord::SVfrFileScopeRecord (
109 IN CHAR8 *Record,
110 IN UINT32 LineNum
111 )
112 {
113 UINT32 Index;
114 CHAR8 *FileName = NULL;
115 CHAR8 *Str = NULL;
116
117 mWholeScopeLine = LineNum;
118 mNext = NULL;
119
120 Str = strchr (Record, ' ');
121 mScopeLineStart = atoi (++Str);
122
123 Str = strchr (Str, '\"');
124 FileName = ++Str;
125
126 while((Str = strstr (FileName, "\\\\")) != NULL) {
127 FileName = Str + 2;
128 }
129 if ((mFileName = new CHAR8[strlen(FileName)]) != NULL) {
130 for (Index = 0; FileName[Index] != '\"'; Index++) {
131 mFileName[Index] = FileName[Index];
132 }
133 mFileName[Index] = '\0';
134 }
135
136 return;
137 }
138
139 SVfrFileScopeRecord::~SVfrFileScopeRecord (
140 VOID
141 )
142 {
143 if (mFileName != NULL) {
144 delete[] mFileName;
145 }
146 }
147
148 VOID
149 CVfrErrorHandle::ParseFileScopeRecord (
150 IN CHAR8 *Record,
151 IN UINT32 WholeScopeLine
152 )
153 {
154 SVfrFileScopeRecord *pNode = NULL;
155
156 if (Record == NULL) {
157 return;
158 }
159
160 if ((pNode = new SVfrFileScopeRecord(Record, WholeScopeLine)) == NULL) {
161 return;
162 }
163
164 if (mScopeRecordListHead == NULL) {
165 mScopeRecordListTail = mScopeRecordListHead = pNode;
166 } else {
167 mScopeRecordListTail->mNext = pNode;
168 mScopeRecordListTail = pNode;
169 }
170 }
171
172 VOID
173 CVfrErrorHandle::GetFileNameLineNum (
174 IN UINT32 LineNum,
175 OUT CHAR8 **FileName,
176 OUT UINT32 *FileLine
177 )
178 {
179 SVfrFileScopeRecord *pNode = NULL;
180
181 if ((FileName == NULL) || (FileLine == NULL)) {
182 return;
183 }
184
185 *FileName = NULL;
186 *FileLine = 0xFFFFFFFF;
187
188 //
189 // Some errors occur before scope record list been built.
190 //
191 if (mScopeRecordListHead == NULL) {
192 *FileLine = LineNum;
193 *FileName = mInputFileName;
194 return ;
195 }
196
197 for (pNode = mScopeRecordListHead; pNode->mNext != NULL; pNode = pNode->mNext) {
198 if ((LineNum > pNode->mWholeScopeLine) && (pNode->mNext->mWholeScopeLine > LineNum)) {
199 *FileName = pNode->mFileName;
200 *FileLine = LineNum - pNode->mWholeScopeLine + pNode->mScopeLineStart - 1;
201 return ;
202 }
203 }
204
205 *FileName = pNode->mFileName;
206 *FileLine = LineNum - pNode->mWholeScopeLine + pNode->mScopeLineStart - 1;
207 }
208
209 VOID
210 CVfrErrorHandle::PrintMsg (
211 IN UINT32 LineNum,
212 IN CHAR8 *TokName,
213 IN CONST CHAR8 *MsgType,
214 IN CONST CHAR8 *ErrorMsg
215 )
216 {
217 CHAR8 *FileName = NULL;
218 UINT32 FileLine;
219
220 if (strncmp ("Warning", MsgType, strlen ("Warning")) == 0) {
221 VerboseMsg ((CHAR8 *) ErrorMsg);
222 return;
223 }
224 GetFileNameLineNum (LineNum, &FileName, &FileLine);
225 Error (FileName, FileLine, 0x3000, TokName, (CHAR8 *) "\t%s\n", (CHAR8 *) ErrorMsg);
226 }
227
228 UINT8
229 CVfrErrorHandle::HandleError (
230 IN EFI_VFR_RETURN_CODE ErrorCode,
231 IN UINT32 LineNum,
232 IN CHAR8 *TokName
233 )
234 {
235 UINT32 Index;
236 CHAR8 *FileName = NULL;
237 UINT32 FileLine;
238 CONST CHAR8 *ErrorMsg = NULL;
239
240 if (mVfrErrorHandleTable == NULL) {
241 return 1;
242 }
243
244 for (Index = 0; mVfrErrorHandleTable[Index].mErrorCode != VFR_RETURN_CODEUNDEFINED; Index++) {
245 if (ErrorCode == mVfrErrorHandleTable[Index].mErrorCode) {
246 ErrorMsg = mVfrErrorHandleTable[Index].mErrorMsg;
247 break;
248 }
249 }
250
251 if (ErrorMsg != NULL) {
252 GetFileNameLineNum (LineNum, &FileName, &FileLine);
253 Error (FileName, FileLine, 0x3000, TokName, (CHAR8 *) "\t%s\n", (CHAR8 *) ErrorMsg);
254 return 1;
255 } else {
256 return 0;
257 }
258 }
259
260 UINT8
261 CVfrErrorHandle::HandleWarning (
262 IN EFI_VFR_WARNING_CODE WarningCode,
263 IN UINT32 LineNum,
264 IN CHAR8 *TokName
265 )
266 {
267 UINT32 Index;
268 CHAR8 *FileName = NULL;
269 UINT32 FileLine;
270 CONST CHAR8 *WarningMsg = NULL;
271
272 if (mVfrWarningHandleTable == NULL) {
273 return 1;
274 }
275
276 GetFileNameLineNum (LineNum, &FileName, &FileLine);
277
278 if (mWarningAsError) {
279 Error (FileName, FileLine, 0x2220, (CHAR8 *) "warning treated as error", NULL);
280 }
281
282 for (Index = 0; mVfrWarningHandleTable[Index].mWarningCode != VFR_WARNING_CODEUNDEFINED; Index++) {
283 if (WarningCode == mVfrWarningHandleTable[Index].mWarningCode) {
284 WarningMsg = mVfrWarningHandleTable[Index].mWarningMsg;
285 break;
286 }
287 }
288
289 if (WarningMsg != NULL) {
290 Warning (FileName, FileLine, 0, TokName, (CHAR8 *) "\t%s\n", (CHAR8 *) WarningMsg);
291 return 1;
292 } else {
293 return 0;
294 }
295 }
296
297 CVfrErrorHandle gCVfrErrorHandle;