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