1 /*++ 2 3 Copyright (c) 2004 - 2010, 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 9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, 10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. 11 12 Module Name: 13 14 VfrError.cpp 15 16 Abstract: 17 18 --*/ 19 20 #include "stdio.h" 21 #include "string.h" 22 #include "stdlib.h" 23 #include "VfrError.h" 24 25 static SVFR_ERROR_HANDLE VFR_ERROR_HANDLE_TABLE [] = { 26 { VFR_RETURN_SUCCESS, NULL }, 27 { VFR_RETURN_ERROR_SKIPED, NULL }, 28 { VFR_RETURN_FATAL_ERROR, ": fatal error!!" }, 29 30 { VFR_RETURN_MISMATCHED, ": unexpected token" }, 31 { VFR_RETURN_INVALID_PARAMETER, ": invalid parameter" }, 32 { VFR_RETURN_OUT_FOR_RESOURCES, ": system out of memory" }, 33 { VFR_RETURN_UNSUPPORTED, ": unsupported" }, 34 { VFR_RETURN_REDEFINED, ": already defined" }, 35 { VFR_RETURN_FORMID_REDEFINED, ": form id already defined" }, 36 { VFR_RETURN_QUESTIONID_REDEFINED, ": question id already defined" }, 37 { VFR_RETURN_VARSTOREID_REDEFINED, ": varstore id already defined" }, 38 { VFR_RETURN_UNDEFINED, ": undefined" }, 39 { VFR_RETURN_VAR_NOTDEFINED_BY_QUESTION, ": some variable has not defined by a question"}, 40 { VFR_RETURN_GET_EFIVARSTORE_ERROR, ": get efi varstore error"}, 41 { VFR_RETURN_EFIVARSTORE_USE_ERROR, ": can not use the efi varstore like this" }, 42 { VFR_RETURN_EFIVARSTORE_SIZE_ERROR, ": unsupport efi varstore size should be <= 8 bytes" }, 43 { VFR_RETURN_GET_NVVARSTORE_ERROR, ": get name value varstore error" }, 44 { VFR_RETURN_QVAR_REUSE, ": variable reused by more than one question" }, 45 { VFR_RETURN_FLAGS_UNSUPPORTED, ": flags unsupported" }, 46 { VFR_RETURN_ERROR_ARRARY_NUM, ": array number error" }, 47 { VFR_RETURN_DATA_STRING_ERROR, ": data field string error or not support"}, 48 { VFR_RETURN_DEFAULT_VALUE_REDEFINED, ": default value re-defined with different value"}, 49 { VFR_RETURN_CONSTANT_ONLY, ": only constant is allowed in the expression"}, 50 { VFR_RETURN_CODEUNDEFINED, ": undefined Error Code" } 51 }; 52 53 CVfrErrorHandle::CVfrErrorHandle ( 54 VOID 55 ) 56 { 57 mInputFileName = NULL; 58 mScopeRecordListHead = NULL; 59 mScopeRecordListTail = NULL; 60 mVfrErrorHandleTable = VFR_ERROR_HANDLE_TABLE; 61 } 62 63 CVfrErrorHandle::~CVfrErrorHandle ( 64 VOID 65 ) 66 { 67 SVfrFileScopeRecord *pNode = NULL; 68 69 if (mInputFileName != NULL) { 70 delete mInputFileName; 71 } 72 73 while (mScopeRecordListHead != NULL) { 74 pNode = mScopeRecordListHead; 75 mScopeRecordListHead = mScopeRecordListHead->mNext; 76 delete pNode; 77 } 78 79 mScopeRecordListHead = NULL; 80 mScopeRecordListTail = NULL; 81 mVfrErrorHandleTable = NULL; 82 } 83 84 VOID 85 CVfrErrorHandle::SetInputFile ( 86 IN INT8 *InputFile 87 ) 88 { 89 if (InputFile != NULL) { 90 mInputFileName = new INT8[strlen(InputFile) + 1]; 91 strcpy (mInputFileName, InputFile); 92 } 93 } 94 95 SVfrFileScopeRecord::SVfrFileScopeRecord ( 96 IN INT8 *Record, 97 IN UINT32 LineNum 98 ) 99 { 100 UINT32 Index; 101 INT8 *FileName = NULL; 102 INT8 *Str = NULL; 103 104 mWholeScopeLine = LineNum; 105 mNext = NULL; 106 107 Str = strchr (Record, ' '); 108 mScopeLineStart = atoi (++Str); 109 110 Str = strchr (Str, '\"'); 111 FileName = ++Str; 112 113 while((Str = strstr (FileName, "\\\\")) != NULL) { 114 FileName = Str + 2; 115 } 116 if ((mFileName = new INT8[strlen(FileName)]) != NULL) { 117 for (Index = 0; FileName[Index] != '\"'; Index++) { 118 mFileName[Index] = FileName[Index]; 119 } 120 mFileName[Index] = '\0'; 121 } 122 123 return; 124 } 125 126 SVfrFileScopeRecord::~SVfrFileScopeRecord ( 127 VOID 128 ) 129 { 130 if (mFileName != NULL) { 131 delete mFileName; 132 } 133 } 134 135 VOID 136 CVfrErrorHandle::ParseFileScopeRecord ( 137 IN INT8 *Record, 138 IN UINT32 WholeScopeLine 139 ) 140 { 141 INT8 *FullPathName = NULL; 142 SVfrFileScopeRecord *pNode = NULL; 143 144 if (Record == NULL) { 145 return; 146 } 147 148 if ((pNode = new SVfrFileScopeRecord(Record, WholeScopeLine)) == NULL) { 149 return; 150 } 151 152 if (mScopeRecordListHead == NULL) { 153 mScopeRecordListTail = mScopeRecordListHead = pNode; 154 } else { 155 mScopeRecordListTail->mNext = pNode; 156 mScopeRecordListTail = pNode; 157 } 158 } 159 160 VOID 161 CVfrErrorHandle::GetFileNameLineNum ( 162 IN UINT32 LineNum, 163 OUT INT8 **FileName, 164 OUT UINT32 *FileLine 165 ) 166 { 167 SVfrFileScopeRecord *pNode = NULL; 168 169 if ((FileName == NULL) || (FileLine == NULL)) { 170 return; 171 } 172 173 *FileName = NULL; 174 *FileLine = 0xFFFFFFFF; 175 176 // 177 // Some errors occur before scope record list been built. 178 // 179 if (mScopeRecordListHead == NULL) { 180 *FileLine = LineNum; 181 *FileName = mInputFileName; 182 return ; 183 } 184 185 for (pNode = mScopeRecordListHead; pNode->mNext != NULL; pNode = pNode->mNext) { 186 if ((LineNum > pNode->mWholeScopeLine) && (pNode->mNext->mWholeScopeLine > LineNum)) { 187 *FileName = pNode->mFileName; 188 *FileLine = LineNum - pNode->mWholeScopeLine + pNode->mScopeLineStart - 1; 189 return ; 190 } 191 } 192 193 *FileName = pNode->mFileName; 194 *FileLine = LineNum - pNode->mWholeScopeLine + pNode->mScopeLineStart - 1; 195 } 196 197 VOID 198 CVfrErrorHandle::PrintMsg ( 199 IN UINT32 LineNum, 200 IN INT8 *TokName, 201 IN INT8 *MsgType, 202 IN INT8 *ErrorMsg 203 ) 204 { 205 INT8 *FileName = NULL; 206 UINT32 FileLine; 207 208 GetFileNameLineNum (LineNum, &FileName, &FileLine); 209 printf ("%s line %d: %s %s %s\n", FileName, FileLine, MsgType, TokName, ErrorMsg); 210 } 211 212 UINT8 213 CVfrErrorHandle::HandleError ( 214 IN EFI_VFR_RETURN_CODE ErrorCode, 215 IN UINT32 LineNum, 216 IN INT8 *TokName 217 ) 218 { 219 UINT32 Index; 220 INT8 *FileName = NULL; 221 UINT32 FileLine; 222 INT8 *ErrorMsg = NULL; 223 224 if (mVfrErrorHandleTable == NULL) { 225 return 1; 226 } 227 228 for (Index = 0; mVfrErrorHandleTable[Index].mErrorCode != VFR_RETURN_CODEUNDEFINED; Index++) { 229 if (ErrorCode == mVfrErrorHandleTable[Index].mErrorCode) { 230 ErrorMsg = mVfrErrorHandleTable[Index].mErrorMsg; 231 break; 232 } 233 } 234 235 if (ErrorMsg != NULL) { 236 GetFileNameLineNum (LineNum, &FileName, &FileLine); 237 printf ("%s line %d: error %s %s\n", FileName, FileLine, TokName, ErrorMsg); 238 return 1; 239 } else { 240 return 0; 241 } 242 } 243 244 CVfrErrorHandle gCVfrErrorHandle; 245