ParseGuidedSectionTools.c 4.4 KB

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