]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/C/VfrCompile/VfrError.cpp
963bd2da42698659559d1920352cc4cd402eb39e
[mirror_edk2.git] / BaseTools / Source / C / VfrCompile / VfrError.cpp
1 /** @file
2
3 VfrCompiler error handler.
4
5 Copyright (c) 2004 - 2008, Intel Corporation
6 All rights reserved. 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_GET_EFIVARSTORE_ERROR, ": get efi varstore error"},
38 { VFR_RETURN_EFIVARSTORE_USE_ERROR, ": can not use the efi varstore like this" },
39 { VFR_RETURN_EFIVARSTORE_SIZE_ERROR, ": unsupport efi varstore size should be <= 8 bytes" },
40 { VFR_RETURN_GET_NVVARSTORE_ERROR, ": get name value varstore error" },
41 { VFR_RETURN_QVAR_REUSE, ": variable reused by more than one question" },
42 { VFR_RETURN_FLAGS_UNSUPPORTED, ": flags unsupported" },
43 { 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" },
44 { VFR_RETURN_DATA_STRING_ERROR, ": data field string error or not support"},
45 { VFR_RETURN_DEFAULT_VALUE_REDEFINED, ": default value re-defined with different value"},
46 { VFR_RETURN_CONSTANT_ONLY, ": only constant is allowed in the expression"},
47 { VFR_RETURN_CODEUNDEFINED, ": undefined Error Code" }
48 };
49
50 CVfrErrorHandle::CVfrErrorHandle (
51 VOID
52 )
53 {
54 mInputFileName = NULL;
55 mScopeRecordListHead = NULL;
56 mScopeRecordListTail = NULL;
57 mVfrErrorHandleTable = VFR_ERROR_HANDLE_TABLE;
58 }
59
60 CVfrErrorHandle::~CVfrErrorHandle (
61 VOID
62 )
63 {
64 SVfrFileScopeRecord *pNode = NULL;
65
66 if (mInputFileName != NULL) {
67 delete mInputFileName;
68 }
69
70 while (mScopeRecordListHead != NULL) {
71 pNode = mScopeRecordListHead;
72 mScopeRecordListHead = mScopeRecordListHead->mNext;
73 delete pNode;
74 }
75
76 mScopeRecordListHead = NULL;
77 mScopeRecordListTail = NULL;
78 mVfrErrorHandleTable = NULL;
79 }
80
81 VOID
82 CVfrErrorHandle::SetInputFile (
83 IN CHAR8 *InputFile
84 )
85 {
86 if (InputFile != NULL) {
87 mInputFileName = new CHAR8[strlen(InputFile) + 1];
88 strcpy (mInputFileName, InputFile);
89 }
90 }
91
92 SVfrFileScopeRecord::SVfrFileScopeRecord (
93 IN CHAR8 *Record,
94 IN UINT32 LineNum
95 )
96 {
97 UINT32 Index;
98 CHAR8 *FileName = NULL;
99 CHAR8 *Str = NULL;
100
101 mWholeScopeLine = LineNum;
102 mNext = NULL;
103
104 Str = strchr (Record, ' ');
105 mScopeLineStart = atoi (++Str);
106
107 Str = strchr (Str, '\"');
108 FileName = ++Str;
109
110 while((Str = strstr (FileName, "\\\\")) != NULL) {
111 FileName = Str + 2;
112 }
113 if ((mFileName = new CHAR8[strlen(FileName)]) != NULL) {
114 for (Index = 0; FileName[Index] != '\"'; Index++) {
115 mFileName[Index] = FileName[Index];
116 }
117 mFileName[Index] = '\0';
118 }
119
120 return;
121 }
122
123 SVfrFileScopeRecord::~SVfrFileScopeRecord (
124 VOID
125 )
126 {
127 if (mFileName != NULL) {
128 delete mFileName;
129 }
130 }
131
132 VOID
133 CVfrErrorHandle::ParseFileScopeRecord (
134 IN CHAR8 *Record,
135 IN UINT32 WholeScopeLine
136 )
137 {
138 CHAR8 *FullPathName = NULL;
139 SVfrFileScopeRecord *pNode = NULL;
140
141 if (Record == NULL) {
142 return;
143 }
144
145 if ((pNode = new SVfrFileScopeRecord(Record, WholeScopeLine)) == NULL) {
146 return;
147 }
148
149 if (mScopeRecordListHead == NULL) {
150 mScopeRecordListTail = mScopeRecordListHead = pNode;
151 } else {
152 mScopeRecordListTail->mNext = pNode;
153 mScopeRecordListTail = pNode;
154 }
155 }
156
157 VOID
158 CVfrErrorHandle::GetFileNameLineNum (
159 IN UINT32 LineNum,
160 OUT CHAR8 **FileName,
161 OUT UINT32 *FileLine
162 )
163 {
164 SVfrFileScopeRecord *pNode = NULL;
165
166 if ((FileName == NULL) || (FileLine == NULL)) {
167 return;
168 }
169
170 *FileName = NULL;
171 *FileLine = 0xFFFFFFFF;
172
173 //
174 // Some errors occur before scope record list been built.
175 //
176 if (mScopeRecordListHead == NULL) {
177 *FileLine = LineNum;
178 *FileName = mInputFileName;
179 return ;
180 }
181
182 for (pNode = mScopeRecordListHead; pNode->mNext != NULL; pNode = pNode->mNext) {
183 if ((LineNum > pNode->mWholeScopeLine) && (pNode->mNext->mWholeScopeLine > LineNum)) {
184 *FileName = pNode->mFileName;
185 *FileLine = LineNum - pNode->mWholeScopeLine + pNode->mScopeLineStart - 1;
186 return ;
187 }
188 }
189
190 *FileName = pNode->mFileName;
191 *FileLine = LineNum - pNode->mWholeScopeLine + pNode->mScopeLineStart - 1;
192 }
193
194 VOID
195 CVfrErrorHandle::PrintMsg (
196 IN UINT32 LineNum,
197 IN CHAR8 *TokName,
198 IN CHAR8 *MsgType,
199 IN CHAR8 *ErrorMsg
200 )
201 {
202 CHAR8 *FileName = NULL;
203 UINT32 FileLine;
204
205 if (strncmp ("Warning", MsgType, strlen ("Warning")) == 0) {
206 VerboseMsg (ErrorMsg);
207 return;
208 }
209 GetFileNameLineNum (LineNum, &FileName, &FileLine);
210 Error (FileName, FileLine, 0x3000, TokName, "\t%s\n", ErrorMsg);
211 }
212
213 UINT8
214 CVfrErrorHandle::HandleError (
215 IN EFI_VFR_RETURN_CODE ErrorCode,
216 IN UINT32 LineNum,
217 IN CHAR8 *TokName
218 )
219 {
220 UINT32 Index;
221 CHAR8 *FileName = NULL;
222 UINT32 FileLine;
223 CHAR8 *ErrorMsg = NULL;
224
225 if (mVfrErrorHandleTable == NULL) {
226 return 1;
227 }
228
229 for (Index = 0; mVfrErrorHandleTable[Index].mErrorCode != VFR_RETURN_CODEUNDEFINED; Index++) {
230 if (ErrorCode == mVfrErrorHandleTable[Index].mErrorCode) {
231 ErrorMsg = mVfrErrorHandleTable[Index].mErrorMsg;
232 break;
233 }
234 }
235
236 if (ErrorMsg != NULL) {
237 GetFileNameLineNum (LineNum, &FileName, &FileLine);
238 Error (FileName, FileLine, 0x3000, TokName, "\t%s\n", ErrorMsg);
239 return 1;
240 } else {
241 return 0;
242 }
243 }
244
245 CVfrErrorHandle gCVfrErrorHandle;