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