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