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