VfrError.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /** @file
  2. VfrCompiler error handler.
  3. Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>
  4. This program and the accompanying materials
  5. are licensed and made available under the terms and conditions of the BSD License
  6. which accompanies this distribution. The full text of the license may be found at
  7. http://opensource.org/licenses/bsd-license.php
  8. THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
  10. **/
  11. #include "stdio.h"
  12. #include "string.h"
  13. #include "stdlib.h"
  14. #include "VfrError.h"
  15. #include "EfiUtilityMsgs.h"
  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. { VFR_RETURN_MISMATCHED, ": unexpected token" },
  21. { VFR_RETURN_INVALID_PARAMETER, ": invalid parameter" },
  22. { VFR_RETURN_OUT_FOR_RESOURCES, ": system out of memory" },
  23. { VFR_RETURN_UNSUPPORTED, ": unsupported" },
  24. { VFR_RETURN_REDEFINED, ": already defined" },
  25. { VFR_RETURN_FORMID_REDEFINED, ": form id already defined" },
  26. { VFR_RETURN_QUESTIONID_REDEFINED, ": question id already defined" },
  27. { VFR_RETURN_VARSTOREID_REDEFINED, ": varstore id already defined" },
  28. { VFR_RETURN_UNDEFINED, ": undefined" },
  29. { VFR_RETURN_VAR_NOTDEFINED_BY_QUESTION, ": some variable has not defined by a question"},
  30. { 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."},
  31. { VFR_RETURN_GET_EFIVARSTORE_ERROR, ": get efi varstore error"},
  32. { VFR_RETURN_EFIVARSTORE_USE_ERROR, ": can not use the efi varstore like this" },
  33. { VFR_RETURN_EFIVARSTORE_SIZE_ERROR, ": unsupport efi varstore size should be <= 8 bytes" },
  34. { VFR_RETURN_GET_NVVARSTORE_ERROR, ": get name value varstore error" },
  35. { VFR_RETURN_QVAR_REUSE, ": variable reused by more than one question" },
  36. { VFR_RETURN_FLAGS_UNSUPPORTED, ": flags unsupported" },
  37. { 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" },
  38. { VFR_RETURN_DATA_STRING_ERROR, ": data field string error or not support"},
  39. { VFR_RETURN_DEFAULT_VALUE_REDEFINED, ": default value re-defined with different value"},
  40. { VFR_RETURN_CONSTANT_ONLY, ": only constant is allowed in the expression"},
  41. { 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."},
  42. { VFR_RETURN_BIT_WIDTH_ERROR, ": bit width must be <= sizeof (type) * 8 and the max width can not > 32" },
  43. { VFR_RETURN_STRING_TO_UINT_OVERFLOW, ": String to UINT* Overflow"},
  44. { VFR_RETURN_CODEUNDEFINED, ": undefined Error Code" }
  45. };
  46. static SVFR_WARNING_HANDLE VFR_WARNING_HANDLE_TABLE [] = {
  47. { VFR_WARNING_DEFAULT_VALUE_REDEFINED, ": default value re-defined with different value"},
  48. { VFR_WARNING_ACTION_WITH_TEXT_TWO, ": Action opcode should not have TextTwo part"},
  49. { VFR_WARNING_OBSOLETED_FRAMEWORK_OPCODE, ": Not recommend to use obsoleted framework opcode"},
  50. { VFR_WARNING_CODEUNDEFINED, ": undefined Warning Code" }
  51. };
  52. CVfrErrorHandle::CVfrErrorHandle (
  53. VOID
  54. )
  55. {
  56. mInputFileName = NULL;
  57. mScopeRecordListHead = NULL;
  58. mScopeRecordListTail = NULL;
  59. mVfrErrorHandleTable = VFR_ERROR_HANDLE_TABLE;
  60. mVfrWarningHandleTable = VFR_WARNING_HANDLE_TABLE;
  61. mWarningAsError = FALSE;
  62. }
  63. CVfrErrorHandle::~CVfrErrorHandle (
  64. VOID
  65. )
  66. {
  67. SVfrFileScopeRecord *pNode = NULL;
  68. if (mInputFileName != NULL) {
  69. delete[] mInputFileName;
  70. }
  71. while (mScopeRecordListHead != NULL) {
  72. pNode = mScopeRecordListHead;
  73. mScopeRecordListHead = mScopeRecordListHead->mNext;
  74. delete pNode;
  75. }
  76. mScopeRecordListHead = NULL;
  77. mScopeRecordListTail = NULL;
  78. mVfrErrorHandleTable = NULL;
  79. mVfrWarningHandleTable = NULL;
  80. }
  81. VOID
  82. CVfrErrorHandle::SetWarningAsError (
  83. IN BOOLEAN WarningAsError
  84. )
  85. {
  86. mWarningAsError = WarningAsError;
  87. }
  88. VOID
  89. CVfrErrorHandle::SetInputFile (
  90. IN CHAR8 *InputFile
  91. )
  92. {
  93. if (InputFile != NULL) {
  94. mInputFileName = new CHAR8[strlen(InputFile) + 1];
  95. strcpy (mInputFileName, InputFile);
  96. }
  97. }
  98. SVfrFileScopeRecord::SVfrFileScopeRecord (
  99. IN CHAR8 *Record,
  100. IN UINT32 LineNum
  101. )
  102. {
  103. UINT32 Index;
  104. CHAR8 *FileName = NULL;
  105. CHAR8 *Str = NULL;
  106. mWholeScopeLine = LineNum;
  107. mNext = NULL;
  108. Str = strchr (Record, ' ');
  109. mScopeLineStart = atoi (++Str);
  110. Str = strchr (Str, '\"');
  111. FileName = ++Str;
  112. while((Str = strstr (FileName, "\\\\")) != NULL) {
  113. FileName = Str + 2;
  114. }
  115. if ((mFileName = new CHAR8[strlen(FileName)]) != NULL) {
  116. for (Index = 0; FileName[Index] != '\"'; Index++) {
  117. mFileName[Index] = FileName[Index];
  118. }
  119. mFileName[Index] = '\0';
  120. }
  121. return;
  122. }
  123. SVfrFileScopeRecord::~SVfrFileScopeRecord (
  124. VOID
  125. )
  126. {
  127. if (mFileName != NULL) {
  128. delete[] mFileName;
  129. }
  130. }
  131. VOID
  132. CVfrErrorHandle::ParseFileScopeRecord (
  133. IN CHAR8 *Record,
  134. IN UINT32 WholeScopeLine
  135. )
  136. {
  137. SVfrFileScopeRecord *pNode = NULL;
  138. if (Record == NULL) {
  139. return;
  140. }
  141. if ((pNode = new SVfrFileScopeRecord(Record, WholeScopeLine)) == NULL) {
  142. return;
  143. }
  144. if (mScopeRecordListHead == NULL) {
  145. mScopeRecordListTail = mScopeRecordListHead = pNode;
  146. } else {
  147. mScopeRecordListTail->mNext = pNode;
  148. mScopeRecordListTail = pNode;
  149. }
  150. }
  151. VOID
  152. CVfrErrorHandle::GetFileNameLineNum (
  153. IN UINT32 LineNum,
  154. OUT CHAR8 **FileName,
  155. OUT UINT32 *FileLine
  156. )
  157. {
  158. SVfrFileScopeRecord *pNode = NULL;
  159. if ((FileName == NULL) || (FileLine == NULL)) {
  160. return;
  161. }
  162. *FileName = NULL;
  163. *FileLine = 0xFFFFFFFF;
  164. //
  165. // Some errors occur before scope record list been built.
  166. //
  167. if (mScopeRecordListHead == NULL) {
  168. *FileLine = LineNum;
  169. *FileName = mInputFileName;
  170. return ;
  171. }
  172. for (pNode = mScopeRecordListHead; pNode->mNext != NULL; pNode = pNode->mNext) {
  173. if ((LineNum > pNode->mWholeScopeLine) && (pNode->mNext->mWholeScopeLine > LineNum)) {
  174. *FileName = pNode->mFileName;
  175. *FileLine = LineNum - pNode->mWholeScopeLine + pNode->mScopeLineStart - 1;
  176. return ;
  177. }
  178. }
  179. *FileName = pNode->mFileName;
  180. *FileLine = LineNum - pNode->mWholeScopeLine + pNode->mScopeLineStart - 1;
  181. }
  182. VOID
  183. CVfrErrorHandle::PrintMsg (
  184. IN UINT32 LineNum,
  185. IN CHAR8 *TokName,
  186. IN CONST CHAR8 *MsgType,
  187. IN CONST CHAR8 *ErrorMsg
  188. )
  189. {
  190. CHAR8 *FileName = NULL;
  191. UINT32 FileLine;
  192. if (strncmp ("Warning", MsgType, strlen ("Warning")) == 0) {
  193. VerboseMsg ((CHAR8 *) ErrorMsg);
  194. return;
  195. }
  196. GetFileNameLineNum (LineNum, &FileName, &FileLine);
  197. Error (FileName, FileLine, 0x3000, TokName, (CHAR8 *) "\t%s\n", (CHAR8 *) ErrorMsg);
  198. }
  199. UINT8
  200. CVfrErrorHandle::HandleError (
  201. IN EFI_VFR_RETURN_CODE ErrorCode,
  202. IN UINT32 LineNum,
  203. IN CHAR8 *TokName
  204. )
  205. {
  206. UINT32 Index;
  207. CHAR8 *FileName = NULL;
  208. UINT32 FileLine;
  209. CONST CHAR8 *ErrorMsg = NULL;
  210. if (mVfrErrorHandleTable == NULL) {
  211. return 1;
  212. }
  213. for (Index = 0; mVfrErrorHandleTable[Index].mErrorCode != VFR_RETURN_CODEUNDEFINED; Index++) {
  214. if (ErrorCode == mVfrErrorHandleTable[Index].mErrorCode) {
  215. ErrorMsg = mVfrErrorHandleTable[Index].mErrorMsg;
  216. break;
  217. }
  218. }
  219. if (ErrorMsg != NULL) {
  220. GetFileNameLineNum (LineNum, &FileName, &FileLine);
  221. Error (FileName, FileLine, 0x3000, TokName, (CHAR8 *) "\t%s\n", (CHAR8 *) ErrorMsg);
  222. return 1;
  223. } else {
  224. return 0;
  225. }
  226. }
  227. UINT8
  228. CVfrErrorHandle::HandleWarning (
  229. IN EFI_VFR_WARNING_CODE WarningCode,
  230. IN UINT32 LineNum,
  231. IN CHAR8 *TokName
  232. )
  233. {
  234. UINT32 Index;
  235. CHAR8 *FileName = NULL;
  236. UINT32 FileLine;
  237. CONST CHAR8 *WarningMsg = NULL;
  238. if (mVfrWarningHandleTable == NULL) {
  239. return 1;
  240. }
  241. GetFileNameLineNum (LineNum, &FileName, &FileLine);
  242. if (mWarningAsError) {
  243. Error (FileName, FileLine, 0x2220, (CHAR8 *) "warning treated as error", NULL);
  244. }
  245. for (Index = 0; mVfrWarningHandleTable[Index].mWarningCode != VFR_WARNING_CODEUNDEFINED; Index++) {
  246. if (WarningCode == mVfrWarningHandleTable[Index].mWarningCode) {
  247. WarningMsg = mVfrWarningHandleTable[Index].mWarningMsg;
  248. break;
  249. }
  250. }
  251. if (WarningMsg != NULL) {
  252. Warning (FileName, FileLine, 0, TokName, (CHAR8 *) "\t%s\n", (CHAR8 *) WarningMsg);
  253. return 1;
  254. } else {
  255. return 0;
  256. }
  257. }
  258. CVfrErrorHandle gCVfrErrorHandle;