ParseInf.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. /** @file
  2. This contains some useful functions for parsing INF files.
  3. Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <assert.h>
  7. #include <string.h>
  8. #include <ctype.h>
  9. #include <stdlib.h>
  10. #include "EfiUtilityMsgs.h"
  11. #include "ParseInf.h"
  12. #include "CommonLib.h"
  13. CHAR8 *
  14. ReadLine (
  15. IN MEMORY_FILE *InputFile,
  16. IN OUT CHAR8 *InputBuffer,
  17. IN UINTN MaxLength
  18. )
  19. /*++
  20. Routine Description:
  21. This function reads a line, stripping any comments.
  22. The function reads a string from the input stream argument and stores it in
  23. the input string. ReadLine reads characters from the current file position
  24. to and including the first newline character, to the end of the stream, or
  25. until the number of characters read is equal to MaxLength - 1, whichever
  26. comes first. The newline character, if read, is replaced with a \0.
  27. Arguments:
  28. InputFile Memory file image.
  29. InputBuffer Buffer to read into, must be MaxLength size.
  30. MaxLength The maximum size of the input buffer.
  31. Returns:
  32. NULL if error or EOF
  33. InputBuffer otherwise
  34. --*/
  35. {
  36. CHAR8 *CharPtr;
  37. CHAR8 *EndOfLine;
  38. UINTN CharsToCopy;
  39. //
  40. // Verify input parameters are not null
  41. //
  42. assert (InputBuffer);
  43. assert (InputFile->FileImage);
  44. assert (InputFile->Eof);
  45. assert (InputFile->CurrentFilePointer);
  46. //
  47. // Check for end of file condition
  48. //
  49. if (InputFile->CurrentFilePointer >= InputFile->Eof) {
  50. return NULL;
  51. }
  52. //
  53. // Find the next newline char
  54. //
  55. EndOfLine = strchr (InputFile->CurrentFilePointer, '\n');
  56. //
  57. // Determine the number of characters to copy.
  58. //
  59. if (EndOfLine == 0) {
  60. //
  61. // If no newline found, copy to the end of the file.
  62. //
  63. CharsToCopy = InputFile->Eof - InputFile->CurrentFilePointer;
  64. } else if (EndOfLine >= InputFile->Eof) {
  65. //
  66. // If the newline found was beyond the end of file, copy to the eof.
  67. //
  68. CharsToCopy = InputFile->Eof - InputFile->CurrentFilePointer;
  69. } else {
  70. //
  71. // Newline found in the file.
  72. //
  73. CharsToCopy = EndOfLine - InputFile->CurrentFilePointer;
  74. }
  75. //
  76. // If the end of line is too big for the current buffer, set it to the max
  77. // size of the buffer (leaving room for the \0.
  78. //
  79. if (CharsToCopy > MaxLength - 1) {
  80. CharsToCopy = MaxLength - 1;
  81. }
  82. //
  83. // Copy the line.
  84. //
  85. memcpy (InputBuffer, InputFile->CurrentFilePointer, CharsToCopy);
  86. //
  87. // Add the null termination over the 0x0D
  88. //
  89. if (InputBuffer[CharsToCopy - 1] == '\r') {
  90. InputBuffer[CharsToCopy - 1] = '\0';
  91. } else {
  92. InputBuffer[CharsToCopy] = '\0';
  93. }
  94. //
  95. // Increment the current file pointer (include the 0x0A)
  96. //
  97. InputFile->CurrentFilePointer += CharsToCopy + 1;
  98. //
  99. // Strip any comments
  100. //
  101. CharPtr = strstr (InputBuffer, "//");
  102. if (CharPtr != 0) {
  103. CharPtr[0] = 0;
  104. }
  105. //
  106. // Return the string
  107. //
  108. return InputBuffer;
  109. }
  110. BOOLEAN
  111. FindSection (
  112. IN MEMORY_FILE *InputFile,
  113. IN CHAR8 *Section
  114. )
  115. /*++
  116. Routine Description:
  117. This function parses a file from the beginning to find a section.
  118. The section string may be anywhere within a line.
  119. Arguments:
  120. InputFile Memory file image.
  121. Section Section to search for
  122. Returns:
  123. FALSE if error or EOF
  124. TRUE if section found
  125. --*/
  126. {
  127. CHAR8 InputBuffer[MAX_LONG_FILE_PATH];
  128. CHAR8 *CurrentToken;
  129. //
  130. // Verify input is not NULL
  131. //
  132. assert (InputFile->FileImage);
  133. assert (InputFile->Eof);
  134. assert (InputFile->CurrentFilePointer);
  135. assert (Section);
  136. //
  137. // Rewind to beginning of file
  138. //
  139. InputFile->CurrentFilePointer = InputFile->FileImage;
  140. //
  141. // Read lines until the section is found
  142. //
  143. while (InputFile->CurrentFilePointer < InputFile->Eof) {
  144. //
  145. // Read a line
  146. //
  147. ReadLine (InputFile, InputBuffer, MAX_LONG_FILE_PATH);
  148. //
  149. // Check if the section is found
  150. //
  151. CurrentToken = strstr (InputBuffer, Section);
  152. if (CurrentToken != NULL) {
  153. return TRUE;
  154. }
  155. }
  156. return FALSE;
  157. }
  158. EFI_STATUS
  159. FindToken (
  160. IN MEMORY_FILE *InputFile,
  161. IN CHAR8 *Section,
  162. IN CHAR8 *Token,
  163. IN UINTN Instance,
  164. OUT CHAR8 *Value
  165. )
  166. /*++
  167. Routine Description:
  168. Finds a token value given the section and token to search for.
  169. Arguments:
  170. InputFile Memory file image.
  171. Section The section to search for, a string within [].
  172. Token The token to search for, e.g. EFI_PEIM_RECOVERY, followed by an = in the INF file.
  173. Instance The instance of the token to search for. Zero is the first instance.
  174. Value The string that holds the value following the =. Must be MAX_LONG_FILE_PATH in size.
  175. Returns:
  176. EFI_SUCCESS Value found.
  177. EFI_ABORTED Format error detected in INF file.
  178. EFI_INVALID_PARAMETER Input argument was null.
  179. EFI_LOAD_ERROR Error reading from the file.
  180. EFI_NOT_FOUND Section/Token/Value not found.
  181. --*/
  182. {
  183. CHAR8 InputBuffer[MAX_LONG_FILE_PATH];
  184. CHAR8 *CurrentToken;
  185. CHAR8 *Delimiter;
  186. BOOLEAN ParseError;
  187. BOOLEAN ReadError;
  188. UINTN Occurrence;
  189. //
  190. // Check input parameters
  191. //
  192. if (InputFile->FileImage == NULL ||
  193. InputFile->Eof == NULL ||
  194. InputFile->CurrentFilePointer == NULL ||
  195. Section == NULL ||
  196. strlen (Section) == 0 ||
  197. Token == NULL ||
  198. strlen (Token) == 0 ||
  199. Value == NULL
  200. ) {
  201. return EFI_INVALID_PARAMETER;
  202. }
  203. //
  204. // Initialize error codes
  205. //
  206. ParseError = FALSE;
  207. ReadError = FALSE;
  208. //
  209. // Initialize our instance counter for the search token
  210. //
  211. Occurrence = 0;
  212. if (FindSection (InputFile, Section)) {
  213. //
  214. // Found the desired section, find and read the desired token
  215. //
  216. do {
  217. //
  218. // Read a line from the file
  219. //
  220. if (ReadLine (InputFile, InputBuffer, MAX_LONG_FILE_PATH) == NULL) {
  221. //
  222. // Error reading from input file
  223. //
  224. ReadError = TRUE;
  225. break;
  226. }
  227. //
  228. // Get the first non-whitespace string
  229. //
  230. Delimiter = strchr (InputBuffer, '=');
  231. if (Delimiter != NULL) {
  232. *Delimiter = 0;
  233. }
  234. CurrentToken = strtok (InputBuffer, " \t\n");
  235. if (CurrentToken == NULL || Delimiter == NULL) {
  236. //
  237. // Whitespace line found (or comment) so continue
  238. //
  239. CurrentToken = InputBuffer;
  240. continue;
  241. }
  242. //
  243. // Make sure we have not reached the end of the current section
  244. //
  245. if (CurrentToken[0] == '[') {
  246. break;
  247. }
  248. //
  249. // Compare the current token with the desired token
  250. //
  251. if (strcmp (CurrentToken, Token) == 0) {
  252. //
  253. // Found it
  254. //
  255. //
  256. // Check if it is the correct instance
  257. //
  258. if (Instance == Occurrence) {
  259. //
  260. // Copy the contents following the =
  261. //
  262. CurrentToken = Delimiter + 1;
  263. if (*CurrentToken == 0) {
  264. //
  265. // Nothing found, parsing error
  266. //
  267. ParseError = TRUE;
  268. } else {
  269. //
  270. // Strip leading white space
  271. //
  272. while (*CurrentToken == ' ' || *CurrentToken == '\t') {
  273. CurrentToken++;
  274. }
  275. //
  276. // Copy the current token to the output value
  277. //
  278. strcpy (Value, CurrentToken);
  279. //
  280. // Strip trailing white space
  281. //
  282. while (strlen(Value) > 0 && (*(Value + strlen(Value) - 1) == ' ' || *(Value + strlen(Value) - 1) == '\t')) {
  283. *(Value + strlen(Value) - 1) = 0;
  284. }
  285. return EFI_SUCCESS;
  286. }
  287. } else {
  288. //
  289. // Increment the occurrence found
  290. //
  291. Occurrence++;
  292. }
  293. }
  294. } while (
  295. !ParseError &&
  296. !ReadError &&
  297. InputFile->CurrentFilePointer < InputFile->Eof &&
  298. CurrentToken[0] != '[' &&
  299. Occurrence <= Instance
  300. );
  301. }
  302. //
  303. // Distinguish between read errors and INF file format errors.
  304. //
  305. if (ReadError) {
  306. return EFI_LOAD_ERROR;
  307. }
  308. if (ParseError) {
  309. return EFI_ABORTED;
  310. }
  311. return EFI_NOT_FOUND;
  312. }
  313. EFI_STATUS
  314. StringToGuid (
  315. IN CHAR8 *AsciiGuidBuffer,
  316. OUT EFI_GUID *GuidBuffer
  317. )
  318. /*++
  319. Routine Description:
  320. Converts a string to an EFI_GUID. The string must be in the
  321. xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx format.
  322. Arguments:
  323. AsciiGuidBuffer - pointer to ascii string
  324. GuidBuffer - pointer to destination Guid
  325. Returns:
  326. EFI_ABORTED Could not convert the string
  327. EFI_SUCCESS The string was successfully converted
  328. EFI_INVALID_PARAMETER Input parameter is invalid.
  329. --*/
  330. {
  331. INT32 Index;
  332. int Data1;
  333. int Data2;
  334. int Data3;
  335. int Data4[8];
  336. if (AsciiGuidBuffer == NULL || GuidBuffer == NULL) {
  337. return EFI_INVALID_PARAMETER;
  338. }
  339. //
  340. // Check Guid Format strictly xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
  341. //
  342. for (Index = 0; AsciiGuidBuffer[Index] != '\0' && Index < 37; Index ++) {
  343. if (Index == 8 || Index == 13 || Index == 18 || Index == 23) {
  344. if (AsciiGuidBuffer[Index] != '-') {
  345. break;
  346. }
  347. } else {
  348. if (((AsciiGuidBuffer[Index] >= '0') && (AsciiGuidBuffer[Index] <= '9')) ||
  349. ((AsciiGuidBuffer[Index] >= 'a') && (AsciiGuidBuffer[Index] <= 'f')) ||
  350. ((AsciiGuidBuffer[Index] >= 'A') && (AsciiGuidBuffer[Index] <= 'F'))) {
  351. continue;
  352. } else {
  353. break;
  354. }
  355. }
  356. }
  357. if (Index < 36 || AsciiGuidBuffer[36] != '\0') {
  358. Error (NULL, 0, 1003, "Invalid option value", "Incorrect GUID \"%s\"\n Correct Format \"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\"", AsciiGuidBuffer);
  359. return EFI_ABORTED;
  360. }
  361. //
  362. // Scan the guid string into the buffer
  363. //
  364. Index = sscanf (
  365. AsciiGuidBuffer,
  366. "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
  367. &Data1,
  368. &Data2,
  369. &Data3,
  370. &Data4[0],
  371. &Data4[1],
  372. &Data4[2],
  373. &Data4[3],
  374. &Data4[4],
  375. &Data4[5],
  376. &Data4[6],
  377. &Data4[7]
  378. );
  379. //
  380. // Verify the correct number of items were scanned.
  381. //
  382. if (Index != 11) {
  383. Error (NULL, 0, 1003, "Invalid option value", "Incorrect GUID \"%s\"\n Correct Format \"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\"", AsciiGuidBuffer);
  384. return EFI_ABORTED;
  385. }
  386. //
  387. // Copy the data into our GUID.
  388. //
  389. GuidBuffer->Data1 = (UINT32) Data1;
  390. GuidBuffer->Data2 = (UINT16) Data2;
  391. GuidBuffer->Data3 = (UINT16) Data3;
  392. GuidBuffer->Data4[0] = (UINT8) Data4[0];
  393. GuidBuffer->Data4[1] = (UINT8) Data4[1];
  394. GuidBuffer->Data4[2] = (UINT8) Data4[2];
  395. GuidBuffer->Data4[3] = (UINT8) Data4[3];
  396. GuidBuffer->Data4[4] = (UINT8) Data4[4];
  397. GuidBuffer->Data4[5] = (UINT8) Data4[5];
  398. GuidBuffer->Data4[6] = (UINT8) Data4[6];
  399. GuidBuffer->Data4[7] = (UINT8) Data4[7];
  400. return EFI_SUCCESS;
  401. }
  402. EFI_STATUS
  403. AsciiStringToUint64 (
  404. IN CONST CHAR8 *AsciiString,
  405. IN BOOLEAN IsHex,
  406. OUT UINT64 *ReturnValue
  407. )
  408. /*++
  409. Routine Description:
  410. Converts a null terminated ascii string that represents a number into a
  411. UINT64 value. A hex number may be preceded by a 0x, but may not be
  412. succeeded by an h. A number without 0x or 0X is considered to be base 10
  413. unless the IsHex input is true.
  414. Arguments:
  415. AsciiString The string to convert.
  416. IsHex Force the string to be treated as a hex number.
  417. ReturnValue The return value.
  418. Returns:
  419. EFI_SUCCESS Number successfully converted.
  420. EFI_ABORTED Invalid character encountered.
  421. --*/
  422. {
  423. UINT8 Index;
  424. UINT64 Value;
  425. CHAR8 CurrentChar;
  426. //
  427. // Initialize the result
  428. //
  429. Value = 0;
  430. Index = 0;
  431. //
  432. // Check input parameter
  433. //
  434. if (AsciiString == NULL || ReturnValue == NULL || strlen(AsciiString) > 0xFF) {
  435. return EFI_INVALID_PARAMETER;
  436. }
  437. while (AsciiString[Index] == ' ') {
  438. Index ++;
  439. }
  440. //
  441. // Add each character to the result
  442. //
  443. //
  444. // Skip first two chars only if the string starts with '0x' or '0X'
  445. //
  446. if (AsciiString[Index] == '0' && (AsciiString[Index + 1] == 'x' || AsciiString[Index + 1] == 'X')) {
  447. IsHex = TRUE;
  448. Index += 2;
  449. }
  450. if (IsHex) {
  451. //
  452. // Convert the hex string.
  453. //
  454. for (; AsciiString[Index] != '\0'; Index++) {
  455. CurrentChar = AsciiString[Index];
  456. if (CurrentChar == ' ') {
  457. break;
  458. }
  459. //
  460. // Verify Hex string
  461. //
  462. if (isxdigit ((int)CurrentChar) == 0) {
  463. return EFI_ABORTED;
  464. }
  465. //
  466. // Add hex value
  467. //
  468. Value *= 16;
  469. if (CurrentChar >= '0' && CurrentChar <= '9') {
  470. Value += CurrentChar - '0';
  471. } else if (CurrentChar >= 'a' && CurrentChar <= 'f') {
  472. Value += CurrentChar - 'a' + 10;
  473. } else if (CurrentChar >= 'A' && CurrentChar <= 'F') {
  474. Value += CurrentChar - 'A' + 10;
  475. }
  476. }
  477. *ReturnValue = Value;
  478. } else {
  479. //
  480. // Convert dec string is a number
  481. //
  482. for (; Index < strlen (AsciiString); Index++) {
  483. CurrentChar = AsciiString[Index];
  484. if (CurrentChar == ' ') {
  485. break;
  486. }
  487. //
  488. // Verify Dec string
  489. //
  490. if (isdigit ((int)CurrentChar) == 0) {
  491. return EFI_ABORTED;
  492. }
  493. //
  494. // Add dec value
  495. //
  496. Value = Value * 10;
  497. Value += CurrentChar - '0';
  498. }
  499. *ReturnValue = Value;
  500. }
  501. return EFI_SUCCESS;
  502. }
  503. CHAR8 *
  504. ReadLineInStream (
  505. IN FILE *InputFile,
  506. IN OUT CHAR8 *InputBuffer
  507. )
  508. /*++
  509. Routine Description:
  510. This function reads a line, stripping any comments.
  511. // BUGBUG: This is obsolete once genmake goes away...
  512. Arguments:
  513. InputFile Stream pointer.
  514. InputBuffer Buffer to read into, must be MAX_LONG_FILE_PATH size.
  515. Returns:
  516. NULL if error or EOF
  517. InputBuffer otherwise
  518. --*/
  519. {
  520. CHAR8 *CharPtr;
  521. //
  522. // Verify input parameters are not null
  523. //
  524. assert (InputFile);
  525. assert (InputBuffer);
  526. //
  527. // Read a line
  528. //
  529. if (fgets (InputBuffer, MAX_LONG_FILE_PATH, InputFile) == NULL) {
  530. return NULL;
  531. }
  532. //
  533. // Strip any comments
  534. //
  535. CharPtr = strstr (InputBuffer, "//");
  536. if (CharPtr != 0) {
  537. CharPtr[0] = 0;
  538. }
  539. CharPtr = strstr (InputBuffer, "#");
  540. if (CharPtr != 0) {
  541. CharPtr[0] = 0;
  542. }
  543. //
  544. // Return the string
  545. //
  546. return InputBuffer;
  547. }
  548. BOOLEAN
  549. FindSectionInStream (
  550. IN FILE *InputFile,
  551. IN CHAR8 *Section
  552. )
  553. /*++
  554. Routine Description:
  555. This function parses a stream file from the beginning to find a section.
  556. The section string may be anywhere within a line.
  557. // BUGBUG: This is obsolete once genmake goes away...
  558. Arguments:
  559. InputFile Stream pointer.
  560. Section Section to search for
  561. Returns:
  562. FALSE if error or EOF
  563. TRUE if section found
  564. --*/
  565. {
  566. CHAR8 InputBuffer[MAX_LONG_FILE_PATH];
  567. CHAR8 *CurrentToken;
  568. //
  569. // Verify input is not NULL
  570. //
  571. assert (InputFile);
  572. assert (Section);
  573. //
  574. // Rewind to beginning of file
  575. //
  576. if (fseek (InputFile, 0, SEEK_SET) != 0) {
  577. return FALSE;
  578. }
  579. //
  580. // Read lines until the section is found
  581. //
  582. while (feof (InputFile) == 0) {
  583. //
  584. // Read a line
  585. //
  586. ReadLineInStream (InputFile, InputBuffer);
  587. //
  588. // Check if the section is found
  589. //
  590. CurrentToken = strstr (InputBuffer, Section);
  591. if (CurrentToken != NULL) {
  592. return TRUE;
  593. }
  594. }
  595. return FALSE;
  596. }