ParseInf.c 16 KB

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