1 /** @file
2 Helper functions for parsing GuidedSectionTools.txt
3 
4 Copyright (c) 2007 - 2014, 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
ParseGuidedSectionToolsFile(IN CHAR8 * InputFile)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
ParseGuidedSectionToolsMemoryFile(IN EFI_HANDLE InputFile)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       break;
129     }
130 
131     if (NextLine[0] == '\0') {
132       continue;
133     }
134 
135     Tool = SplitStringByWhitespace (NextLine);
136     if ((Tool != NULL) &&
137         (Tool->Count == 3)
138        ) {
139       Status = StringToGuid (Tool->Strings[0], &Guid);
140       if (!EFI_ERROR (Status)) {
141         NewGuidTool = malloc (sizeof (GUID_SEC_TOOL_ENTRY));
142         if (NewGuidTool != NULL) {
143           memcpy (&(NewGuidTool->Guid), &Guid, sizeof (Guid));
144           NewGuidTool->Name = CloneString(Tool->Strings[1]);
145           NewGuidTool->Path = CloneString(Tool->Strings[2]);
146           NewGuidTool->Next = NULL;
147         }
148         if (FirstGuidTool == NULL) {
149           FirstGuidTool = NewGuidTool;
150         } else {
151           LastGuidTool->Next = NewGuidTool;
152         }
153         LastGuidTool = NewGuidTool;
154       }
155       FreeStringList (Tool);
156     }
157   }
158 
159   return FirstGuidTool;
160 }
161 
162 
163 CHAR8*
LookupGuidedSectionToolPath(IN EFI_HANDLE ParsedGuidedSectionToolsHandle,IN EFI_GUID * SectionGuid)164 LookupGuidedSectionToolPath (
165   IN EFI_HANDLE ParsedGuidedSectionToolsHandle,
166   IN EFI_GUID   *SectionGuid
167   )
168 /*++
169 
170 Routine Description:
171 
172   This function looks up the appropriate tool to use for extracting
173   a GUID defined FV section.
174 
175 Arguments:
176 
177   ParsedGuidedSectionToolsHandle    A parsed GUID section tools handle.
178   SectionGuid                       The GUID for the section.
179 
180 Returns:
181 
182   NULL     - if no tool is found or there is another error
183   Non-NULL - The tool to use to access the section contents.  (The caller
184              must free the memory associated with this string.)
185 
186 --*/
187 {
188   GUID_SEC_TOOL_ENTRY *GuidTool;
189 
190   GuidTool = (GUID_SEC_TOOL_ENTRY*)ParsedGuidedSectionToolsHandle;
191   if (GuidTool == NULL) {
192     return NULL;
193   }
194 
195   for ( ; GuidTool != NULL; GuidTool = GuidTool->Next) {
196     if (CompareGuid (&(GuidTool->Guid), SectionGuid) == 0) {
197       return CloneString (GuidTool->Path);
198     }
199   }
200 
201   return NULL;
202 }
203 
204 
205