]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/C/Common/ParseGuidedSectionTools.c
BaseTools: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / BaseTools / Source / C / Common / ParseGuidedSectionTools.c
CommitLineData
30fdf114 1/** @file\r
97fa0ee9 2Helper functions for parsing GuidedSectionTools.txt\r
30fdf114 3\r
f7496d71 4Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>\r
2e351cbe 5SPDX-License-Identifier: BSD-2-Clause-Patent\r
30fdf114 6\r
30fdf114
LG
7**/\r
8\r
9#include <assert.h>\r
10#include <string.h>\r
11#include <ctype.h>\r
12#include <stdlib.h>\r
13#include "MemoryFile.h"\r
14#include "CommonLib.h"\r
15#include "EfiUtilityMsgs.h"\r
16#include "ParseInf.h"\r
17#include "ParseGuidedSectionTools.h"\r
18#include "StringFuncs.h"\r
19\r
20\r
21//\r
22// Local types / structures\r
23//\r
24\r
25typedef struct _GUID_SEC_TOOL_ENTRY {\r
26 EFI_GUID Guid;\r
27 CHAR8* Name;\r
28 CHAR8* Path;\r
29 struct _GUID_SEC_TOOL_ENTRY *Next;\r
30} GUID_SEC_TOOL_ENTRY;\r
31\r
32//\r
fb0b35e0 33// Function Implementation\r
30fdf114
LG
34//\r
35\r
36EFI_HANDLE\r
37ParseGuidedSectionToolsFile (\r
38 IN CHAR8 *InputFile\r
39 )\r
40/*++\r
41\r
42Routine Description:\r
43\r
44 This function parses the tools_def.txt file. It returns a\r
45 EFI_HANDLE object which can be used for the other library\r
46 functions and should be passed to FreeParsedGuidedSectionToolsHandle\r
47 to free resources when the tools_def.txt information is no\r
48 longer needed.\r
49\r
50Arguments:\r
51\r
52 InputFile Path name of file to read\r
53\r
54Returns:\r
55\r
56 NULL if error parsing\r
57 A non-NULL EFI_HANDLE otherwise\r
58\r
59--*/\r
60{\r
61 EFI_STATUS Status;\r
62 EFI_HANDLE MemoryFile;\r
63 EFI_HANDLE ParsedGuidedSectionTools;\r
64\r
65 Status = GetMemoryFile (InputFile, &MemoryFile);\r
66 if (EFI_ERROR (Status)) {\r
67 return NULL;\r
68 }\r
69\r
70 ParsedGuidedSectionTools = ParseGuidedSectionToolsMemoryFile (MemoryFile);\r
71\r
72 FreeMemoryFile (MemoryFile);\r
f7496d71 73\r
30fdf114
LG
74 return ParsedGuidedSectionTools;\r
75}\r
76\r
77\r
78EFI_HANDLE\r
79ParseGuidedSectionToolsMemoryFile (\r
80 IN EFI_HANDLE InputFile\r
81 )\r
82/*++\r
83\r
84Routine Description:\r
85\r
86 This function parses the tools_def.txt file. It returns a\r
87 EFI_HANDLE object which can be used for the other library\r
88 functions and should be passed to FreeParsedGuidedSectionToolsHandle\r
89 to free resources when the tools_def.txt information is no\r
90 longer needed.\r
91\r
92Arguments:\r
93\r
94 InputFile Memory file image.\r
95\r
96Returns:\r
97\r
98 NULL if error or EOF\r
99 InputBuffer otherwise\r
100\r
101--*/\r
102{\r
103 EFI_STATUS Status;\r
104 CHAR8 *NextLine;\r
105 STRING_LIST *Tool;\r
106 EFI_GUID Guid;\r
107 GUID_SEC_TOOL_ENTRY *FirstGuidTool;\r
108 GUID_SEC_TOOL_ENTRY *LastGuidTool;\r
109 GUID_SEC_TOOL_ENTRY *NewGuidTool;\r
110\r
111 FirstGuidTool = NULL;\r
fd171542 112 LastGuidTool = NULL;\r
30fdf114
LG
113\r
114 while (TRUE) {\r
115 NextLine = ReadMemoryFileLine (InputFile);\r
116 if (NextLine == NULL) {\r
117 break;\r
118 }\r
f7496d71 119\r
30fdf114
LG
120 Status = StripInfDscStringInPlace (NextLine);\r
121 if (EFI_ERROR (Status)) {\r
aee34651 122 free (NextLine);\r
30fdf114
LG
123 break;\r
124 }\r
125\r
126 if (NextLine[0] == '\0') {\r
aee34651 127 free (NextLine);\r
30fdf114
LG
128 continue;\r
129 }\r
130\r
131 Tool = SplitStringByWhitespace (NextLine);\r
132 if ((Tool != NULL) &&\r
133 (Tool->Count == 3)\r
134 ) {\r
135 Status = StringToGuid (Tool->Strings[0], &Guid);\r
136 if (!EFI_ERROR (Status)) {\r
137 NewGuidTool = malloc (sizeof (GUID_SEC_TOOL_ENTRY));\r
138 if (NewGuidTool != NULL) {\r
139 memcpy (&(NewGuidTool->Guid), &Guid, sizeof (Guid));\r
140 NewGuidTool->Name = CloneString(Tool->Strings[1]);\r
141 NewGuidTool->Path = CloneString(Tool->Strings[2]);\r
142 NewGuidTool->Next = NULL;\r
2ff3293d
HW
143\r
144 if (FirstGuidTool == NULL) {\r
145 FirstGuidTool = NewGuidTool;\r
146 } else {\r
147 LastGuidTool->Next = NewGuidTool;\r
148 }\r
149 LastGuidTool = NewGuidTool;\r
30fdf114 150 }\r
30fdf114 151 }\r
aee34651
HW
152 }\r
153\r
154 if (Tool != NULL) {\r
30fdf114
LG
155 FreeStringList (Tool);\r
156 }\r
aee34651 157 free (NextLine);\r
30fdf114
LG
158 }\r
159\r
160 return FirstGuidTool;\r
161}\r
162\r
163\r
164CHAR8*\r
165LookupGuidedSectionToolPath (\r
166 IN EFI_HANDLE ParsedGuidedSectionToolsHandle,\r
167 IN EFI_GUID *SectionGuid\r
168 )\r
169/*++\r
170\r
171Routine Description:\r
172\r
173 This function looks up the appropriate tool to use for extracting\r
174 a GUID defined FV section.\r
175\r
176Arguments:\r
177\r
178 ParsedGuidedSectionToolsHandle A parsed GUID section tools handle.\r
179 SectionGuid The GUID for the section.\r
180\r
181Returns:\r
182\r
183 NULL - if no tool is found or there is another error\r
184 Non-NULL - The tool to use to access the section contents. (The caller\r
185 must free the memory associated with this string.)\r
186\r
187--*/\r
188{\r
189 GUID_SEC_TOOL_ENTRY *GuidTool;\r
190\r
191 GuidTool = (GUID_SEC_TOOL_ENTRY*)ParsedGuidedSectionToolsHandle;\r
192 if (GuidTool == NULL) {\r
193 return NULL;\r
194 }\r
195\r
196 for ( ; GuidTool != NULL; GuidTool = GuidTool->Next) {\r
197 if (CompareGuid (&(GuidTool->Guid), SectionGuid) == 0) {\r
198 return CloneString (GuidTool->Path);\r
199 }\r
200 }\r
201\r
202 return NULL;\r
203}\r
204\r
205\r