ParseGuidedSectionTools.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /** @file
  2. Helper functions for parsing GuidedSectionTools.txt
  3. Copyright (c) 2007 - 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 "MemoryFile.h"
  16. #include "CommonLib.h"
  17. #include "EfiUtilityMsgs.h"
  18. #include "ParseInf.h"
  19. #include "ParseGuidedSectionTools.h"
  20. #include "StringFuncs.h"
  21. //
  22. // Local types / structures
  23. //
  24. typedef struct _GUID_SEC_TOOL_ENTRY {
  25. EFI_GUID Guid;
  26. CHAR8* Name;
  27. CHAR8* Path;
  28. struct _GUID_SEC_TOOL_ENTRY *Next;
  29. } GUID_SEC_TOOL_ENTRY;
  30. //
  31. // Function Implementation
  32. //
  33. EFI_HANDLE
  34. ParseGuidedSectionToolsFile (
  35. IN CHAR8 *InputFile
  36. )
  37. /*++
  38. Routine Description:
  39. This function parses the tools_def.txt file. It returns a
  40. EFI_HANDLE object which can be used for the other library
  41. functions and should be passed to FreeParsedGuidedSectionToolsHandle
  42. to free resources when the tools_def.txt information is no
  43. longer needed.
  44. Arguments:
  45. InputFile Path name of file to read
  46. Returns:
  47. NULL if error parsing
  48. A non-NULL EFI_HANDLE otherwise
  49. --*/
  50. {
  51. EFI_STATUS Status;
  52. EFI_HANDLE MemoryFile;
  53. EFI_HANDLE ParsedGuidedSectionTools;
  54. Status = GetMemoryFile (InputFile, &MemoryFile);
  55. if (EFI_ERROR (Status)) {
  56. return NULL;
  57. }
  58. ParsedGuidedSectionTools = ParseGuidedSectionToolsMemoryFile (MemoryFile);
  59. FreeMemoryFile (MemoryFile);
  60. return ParsedGuidedSectionTools;
  61. }
  62. EFI_HANDLE
  63. ParseGuidedSectionToolsMemoryFile (
  64. IN EFI_HANDLE InputFile
  65. )
  66. /*++
  67. Routine Description:
  68. This function parses the tools_def.txt file. It returns a
  69. EFI_HANDLE object which can be used for the other library
  70. functions and should be passed to FreeParsedGuidedSectionToolsHandle
  71. to free resources when the tools_def.txt information is no
  72. longer needed.
  73. Arguments:
  74. InputFile Memory file image.
  75. Returns:
  76. NULL if error or EOF
  77. InputBuffer otherwise
  78. --*/
  79. {
  80. EFI_STATUS Status;
  81. CHAR8 *NextLine;
  82. STRING_LIST *Tool;
  83. EFI_GUID Guid;
  84. GUID_SEC_TOOL_ENTRY *FirstGuidTool;
  85. GUID_SEC_TOOL_ENTRY *LastGuidTool;
  86. GUID_SEC_TOOL_ENTRY *NewGuidTool;
  87. FirstGuidTool = NULL;
  88. LastGuidTool = NULL;
  89. while (TRUE) {
  90. NextLine = ReadMemoryFileLine (InputFile);
  91. if (NextLine == NULL) {
  92. break;
  93. }
  94. Status = StripInfDscStringInPlace (NextLine);
  95. if (EFI_ERROR (Status)) {
  96. free (NextLine);
  97. break;
  98. }
  99. if (NextLine[0] == '\0') {
  100. free (NextLine);
  101. continue;
  102. }
  103. Tool = SplitStringByWhitespace (NextLine);
  104. if ((Tool != NULL) &&
  105. (Tool->Count == 3)
  106. ) {
  107. Status = StringToGuid (Tool->Strings[0], &Guid);
  108. if (!EFI_ERROR (Status)) {
  109. NewGuidTool = malloc (sizeof (GUID_SEC_TOOL_ENTRY));
  110. if (NewGuidTool != NULL) {
  111. memcpy (&(NewGuidTool->Guid), &Guid, sizeof (Guid));
  112. NewGuidTool->Name = CloneString(Tool->Strings[1]);
  113. NewGuidTool->Path = CloneString(Tool->Strings[2]);
  114. NewGuidTool->Next = NULL;
  115. if (FirstGuidTool == NULL) {
  116. FirstGuidTool = NewGuidTool;
  117. } else {
  118. LastGuidTool->Next = NewGuidTool;
  119. }
  120. LastGuidTool = NewGuidTool;
  121. }
  122. }
  123. }
  124. if (Tool != NULL) {
  125. FreeStringList (Tool);
  126. }
  127. free (NextLine);
  128. }
  129. return FirstGuidTool;
  130. }
  131. CHAR8*
  132. LookupGuidedSectionToolPath (
  133. IN EFI_HANDLE ParsedGuidedSectionToolsHandle,
  134. IN EFI_GUID *SectionGuid
  135. )
  136. /*++
  137. Routine Description:
  138. This function looks up the appropriate tool to use for extracting
  139. a GUID defined FV section.
  140. Arguments:
  141. ParsedGuidedSectionToolsHandle A parsed GUID section tools handle.
  142. SectionGuid The GUID for the section.
  143. Returns:
  144. NULL - if no tool is found or there is another error
  145. Non-NULL - The tool to use to access the section contents. (The caller
  146. must free the memory associated with this string.)
  147. --*/
  148. {
  149. GUID_SEC_TOOL_ENTRY *GuidTool;
  150. GuidTool = (GUID_SEC_TOOL_ENTRY*)ParsedGuidedSectionToolsHandle;
  151. if (GuidTool == NULL) {
  152. return NULL;
  153. }
  154. for ( ; GuidTool != NULL; GuidTool = GuidTool->Next) {
  155. if (CompareGuid (&(GuidTool->Guid), SectionGuid) == 0) {
  156. return CloneString (GuidTool->Path);
  157. }
  158. }
  159. return NULL;
  160. }