]>
Commit | Line | Data |
---|---|---|
878ddf1f | 1 | /** @file\r |
2 | File is FfsProcess class which is used to get the corresponding FFS layout\r | |
3 | information for driver module. \r | |
4 | \r | |
5 | Copyright (c) 2006, Intel Corporation\r | |
6 | All rights reserved. This program and the accompanying materials\r | |
7 | are licensed and made available under the terms and conditions of the BSD License\r | |
8 | which accompanies this distribution. The full text of the license may be found at\r | |
9 | http://opensource.org/licenses/bsd-license.php\r | |
10 | \r | |
11 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
12 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
13 | **/\r | |
14 | package org.tianocore.build;\r | |
15 | \r | |
16 | import java.io.File;\r | |
878ddf1f | 17 | import java.util.Vector;\r |
18 | \r | |
19 | import javax.xml.namespace.QName;\r | |
878ddf1f | 20 | \r |
21 | import org.apache.tools.ant.BuildException;\r | |
22 | import org.apache.tools.ant.Project;\r | |
23 | import org.apache.xmlbeans.XmlCursor;\r | |
a29c47e0 | 24 | import org.tianocore.BuildOptionsDocument;\r |
25 | import org.tianocore.build.global.GlobalData;\r | |
26 | import org.tianocore.build.global.SurfaceAreaQuery;\r | |
27 | import org.tianocore.build.id.FpdModuleIdentification;\r | |
878ddf1f | 28 | import org.w3c.dom.Document;\r |
29 | import org.w3c.dom.Element;\r | |
878ddf1f | 30 | \r |
31 | /** \r | |
32 | <p><code>FfsProcess</code> is a class to find the corresponding FFS layout. </p>\r | |
33 | \r | |
34 | <p>Property <code>COMMON_FILE</code> specified which file to search. The element\r | |
35 | in <code>COMMON_FILE</code> is like following: </p>\r | |
36 | \r | |
37 | <pre>\r | |
38 | <Ffs type="APPLICATION">\r | |
39 | <Attribute Name="FFS_FILETYPE" Value="EFI_FV_FILETYPE_APPLICATION" />\r | |
40 | <Attribute Name="FFS_ATTRIB_CHECKSUM" Value="TRUE" />\r | |
41 | <Sections EncapsulationType="Compress">\r | |
42 | <Sections EncapsulationType="Guid-Defined">\r | |
43 | <Section SectionType="EFI_SECTION_PE32" /> \r | |
44 | <Section SectionType="EFI_SECTION_USER_INTERFACE" />\r | |
45 | <Section SectionType="EFI_SECTION_VERSION" /> \r | |
46 | </Sections>\r | |
47 | </Sections>\r | |
48 | </Ffs>\r | |
49 | </pre>\r | |
50 | \r | |
51 | @since GenBuild 1.0\r | |
52 | **/\r | |
53 | public class FfsProcess {\r | |
54 | \r | |
a29c47e0 | 55 | private BuildOptionsDocument.BuildOptions.Ffs ffsXmlObject;\r |
878ddf1f | 56 | \r |
57 | ///\r | |
58 | /// ANT script to call GenFfs\r | |
59 | ///\r | |
60 | private Element ffsNode = null;\r | |
61 | \r | |
62 | ///\r | |
63 | /// Module base name\r | |
64 | ///\r | |
65 | private String basename;\r | |
66 | \r | |
67 | ///\r | |
68 | /// Sections type: normal\r | |
69 | ///\r | |
70 | private static int MODE_NONE = 0;\r | |
71 | \r | |
72 | ///\r | |
73 | /// Sections type: compress\r | |
74 | ///\r | |
75 | private static int MODE_COMPRESS = 1;\r | |
76 | \r | |
77 | ///\r | |
78 | /// Sections type: guid-define\r | |
79 | ///\r | |
80 | private static int MODE_GUID_DEFINED = 2;\r | |
81 | \r | |
82 | ///\r | |
83 | /// mapping from section type to section output file extension\r | |
84 | ///\r | |
85 | public static final String[][] sectionExt = { { "EFI_SECTION_FREEFORM_SUBTYPE_GUID", ".sec" },\r | |
86 | { "EFI_SECTION_VERSION", ".ver" },\r | |
87 | { "EFI_SECTION_USER_INTERFACE", ".ui" },\r | |
88 | { "EFI_SECTION_DXE_DEPEX", ".dpx" },\r | |
89 | { "EFI_SECTION_PEI_DEPEX", ".dpx" }, \r | |
90 | { "EFI_SECTION_PE32", ".pe32" },\r | |
91 | { "EFI_SECTION_PIC", ".pic" }, \r | |
92 | { "EFI_SECTION_TE", ".tes" },\r | |
93 | { "EFI_SECTION_RAW", ".sec" }, \r | |
94 | { "EFI_SECTION_COMPRESSION", ".sec" },\r | |
95 | { "EFI_SECTION_GUID_DEFINED", ".sec" },\r | |
96 | { "EFI_SECTION_COMPATIBILITY16", ".sec" },\r | |
97 | { "EFI_SECTION_FIRMWARE_VOLUME_IMAGE", ".sec" } };\r | |
98 | \r | |
99 | /**\r | |
100 | search in the type, if componentType is listed in type, return true; \r | |
101 | otherwise return false.\r | |
102 | \r | |
103 | @param type a list supported component type separated by comma\r | |
104 | @param componentType current module component type\r | |
105 | @return whether componentType is one of type \r | |
106 | **/\r | |
107 | private boolean isMatch(String type, String componentType) {\r | |
108 | String[] items = type.split("[ \t]*,[ \t]*");\r | |
109 | for (int i = 0; i < items.length; i++) {\r | |
110 | if (items[i].equalsIgnoreCase(componentType)) {\r | |
111 | return true;\r | |
112 | }\r | |
113 | }\r | |
114 | return false;\r | |
115 | }\r | |
116 | \r | |
117 | /**\r | |
118 | Find the corresponding FFS layout in <code>COMMON_FILE</code> if it\r | |
119 | does not specify in module's surface area. \r | |
120 | \r | |
121 | @param buildType Current module's component type\r | |
122 | @param project Ant project\r | |
123 | @return whether find the corresponding FFS layout\r | |
124 | @throws BuildException\r | |
125 | If specified COMMON_FILE XML file is not valide.\r | |
126 | **/\r | |
a29c47e0 | 127 | public boolean initSections(String buildType, Project project, FpdModuleIdentification fpdModuleId) throws BuildException {\r |
878ddf1f | 128 | //\r |
82516887 | 129 | // Try to find Ffs layout from FPD file\r |
a29c47e0 | 130 | //\r |
131 | SurfaceAreaQuery.push(GlobalData.getFpdBuildOptions());\r | |
132 | BuildOptionsDocument.BuildOptions.Ffs[] ffsArray = SurfaceAreaQuery.getFpdFfs();\r | |
133 | SurfaceAreaQuery.pop();\r | |
134 | for (int i = 0; i < ffsArray.length; i++) {\r | |
135 | if (isMatch(ffsArray[i].getFfsKey(), buildType)) {\r | |
136 | ffsXmlObject = ffsArray[i];\r | |
137 | return true;\r | |
138 | }\r | |
139 | }\r | |
140 | \r | |
878ddf1f | 141 | //\r |
e64c74dd | 142 | // If FfsFormatKey is not null, report exception and fail build\r |
143 | // Otherwise report warning message\r | |
878ddf1f | 144 | //\r |
e64c74dd | 145 | if (buildType == null) {\r |
146 | System.out.println("Warning: this module doesn't specify a FfsFormatKey. ");\r | |
82516887 | 147 | } else {\r |
391dbbb1 | 148 | throw new BuildException("Can't find the FfsFormatKey [" + buildType + "] attribute in the FPD file!"); \r |
878ddf1f | 149 | }\r |
e64c74dd | 150 | \r |
82516887 | 151 | return false;\r |
878ddf1f | 152 | }\r |
153 | \r | |
154 | /**\r | |
155 | Recursive parse the FFS layout. Find out all section type here used. \r | |
156 | \r | |
157 | @param document BaseName_build.xml Xml document\r | |
158 | @param basename Module's base name\r | |
159 | @param guid Module's GUID\r | |
160 | @param targetFilename Module's final file name (GUID-BaseName.APP)\r | |
161 | @return List of section type\r | |
162 | **/\r | |
163 | public String[] getGenSectionElements(Document document, String basename, String guid, String targetFilename) {\r | |
164 | this.basename = basename;\r | |
82516887 | 165 | if (ffsXmlObject == null) {\r |
878ddf1f | 166 | return new String[0];\r |
167 | }\r | |
168 | Vector<String> sectionList = new Vector<String>();\r | |
169 | XmlCursor cursor = null;\r | |
170 | try {\r | |
82516887 | 171 | cursor = ffsXmlObject.newCursor();\r |
878ddf1f | 172 | } catch (Exception e) {\r |
173 | return null;\r | |
174 | }\r | |
175 | int mode = MODE_NONE;\r | |
82516887 | 176 | Element genffsfileEle = document.createElement("genffsfile");\r |
177 | genffsfileEle.setAttribute("outputDir", "${BIN_DIR}");\r | |
178 | genffsfileEle.setAttribute("moduleType", "${MODULE_TYPE}");\r | |
179 | genffsfileEle.setAttribute("BaseName", basename);\r | |
180 | genffsfileEle.setAttribute("fileGuid", guid);\r | |
181 | \r | |
878ddf1f | 182 | if (cursor.toFirstChild()) {\r |
183 | do {\r | |
184 | if (cursor.getName().getLocalPart().equalsIgnoreCase("Attribute")) {\r | |
185 | String name = cursor.getAttributeText(new QName("Name"));\r | |
186 | String value = cursor.getAttributeText(new QName("Value"));\r | |
82516887 | 187 | genffsfileEle.setAttribute(changeAttributeName(name), value);\r |
878ddf1f | 188 | } else if (cursor.getName().getLocalPart().equalsIgnoreCase("Section")) {\r |
189 | cursor.push();\r | |
82516887 | 190 | dealSection(mode, document, genffsfileEle, cursor, sectionList);\r |
878ddf1f | 191 | cursor.pop();\r |
192 | } else if (cursor.getName().getLocalPart().equalsIgnoreCase("Sections")) {\r | |
193 | cursor.push();\r | |
82516887 | 194 | dealSections(mode, document, genffsfileEle, cursor, sectionList);\r |
878ddf1f | 195 | cursor.pop();\r |
196 | }\r | |
197 | } while (cursor.toNextSibling());\r | |
198 | }\r | |
199 | //\r | |
200 | // Check dependency \r | |
201 | //\r | |
202 | Element outofdateEle = document.createElement("OnDependency");\r | |
203 | Element sourceEle = document.createElement("sourcefiles");\r | |
204 | String[] result = new String[sectionList.size()];\r | |
205 | for (int i = 0; i < sectionList.size(); i++) {\r | |
206 | result[i] = (String) sectionList.get(i);\r | |
207 | Element pathEle = document.createElement("file");\r | |
208 | pathEle.setAttribute("name", "${DEST_DIR_OUTPUT}" + File.separatorChar + basename\r | |
209 | + getSectionExt(result[i]));\r | |
210 | sourceEle.appendChild(pathEle);\r | |
211 | }\r | |
212 | outofdateEle.appendChild(sourceEle);\r | |
213 | Element targetEle = document.createElement("targetfiles");\r | |
214 | Element fileEle = document.createElement("file");\r | |
52cbbdbc | 215 | fileEle.setAttribute("name", "${BIN_DIR}" + File.separatorChar + targetFilename);\r |
878ddf1f | 216 | targetEle.appendChild(fileEle);\r |
217 | outofdateEle.appendChild(targetEle);\r | |
218 | Element sequentialEle = document.createElement("sequential");\r | |
82516887 | 219 | sequentialEle.appendChild(genffsfileEle);\r |
878ddf1f | 220 | outofdateEle.appendChild(sequentialEle);\r |
221 | ffsNode = outofdateEle;\r | |
222 | return result;\r | |
223 | }\r | |
224 | \r | |
225 | /**\r | |
226 | Change the attribute name. For example: \r | |
227 | \r | |
228 | <pre>\r | |
229 | Before change: FFS_ATTRIB_CHECKSUM \r | |
230 | After change: ffsATTRIBCHECKSUM\r | |
231 | </pre>\r | |
232 | \r | |
233 | @param name Original attribute name\r | |
234 | @return Changed attribute name\r | |
235 | **/\r | |
236 | private String changeAttributeName(String name) {\r | |
237 | String[] strs = name.split("_");\r | |
238 | String str = strs[0].toLowerCase();\r | |
239 | for (int j = 1; j < strs.length; j++) {\r | |
240 | str += strs[j];\r | |
241 | }\r | |
242 | return str;\r | |
243 | }\r | |
244 | \r | |
245 | /**\r | |
246 | Recursively deal with Sections. If sections does not specify a type, then omit it.\r | |
247 | \r | |
248 | @param mode Current node mode (MODE_NONE | MODE_COMPREE | MODE_GUID_DEFINED)\r | |
249 | @param doc Xml Document\r | |
250 | @param root Root Node\r | |
251 | @param cursor Current FFS layout cursor\r | |
252 | @param list List of section type here used\r | |
253 | **/\r | |
254 | private void dealSections(int mode, Document doc, Element root, XmlCursor cursor, Vector<String> list) {\r | |
255 | String type = cursor.getAttributeText(new QName("EncapsulationType"));\r | |
256 | if (type == null) {\r | |
257 | if (cursor.toFirstChild()) {\r | |
258 | do {\r | |
259 | if (cursor.getName().getLocalPart().equalsIgnoreCase("Section")) {\r | |
260 | cursor.push();\r | |
261 | dealSection(mode, doc, root, cursor, list);\r | |
262 | cursor.pop();\r | |
263 | } else if (cursor.getName().getLocalPart().equalsIgnoreCase("Sections")) {\r | |
264 | cursor.push();\r | |
265 | dealSections(mode, doc, root, cursor, list);\r | |
266 | cursor.pop();\r | |
267 | }\r | |
268 | } while (cursor.toNextSibling());\r | |
269 | }\r | |
270 | return;\r | |
271 | }\r | |
272 | Element ele;\r | |
273 | if (type.equalsIgnoreCase("COMPRESS")) {\r | |
274 | mode = MODE_COMPRESS;\r | |
275 | //\r | |
276 | // <compress compressName = "dummy">\r | |
277 | //\r | |
278 | ele = doc.createElement("compress");\r | |
279 | ele.setAttribute("compressName", "dummy");\r | |
280 | } else {\r | |
281 | mode = MODE_GUID_DEFINED;\r | |
282 | //\r | |
283 | // <tool toolName="${OEMTOOLPATH}\toolname"\r | |
284 | // outputPath = "${DEST_DIR_OUTPUT}">\r | |
285 | //\r | |
286 | ele = doc.createElement("tool");\r | |
a33f3dd1 | 287 | ele.setAttribute("toolName", "${WORKSPACE_DIR}" + File.separatorChar + "Tools" + File.separatorChar + "bin"\r |
878ddf1f | 288 | + File.separatorChar + "GenCRC32Section");\r |
289 | ele.setAttribute("outputPath", "${DEST_DIR_OUTPUT}");\r | |
290 | }\r | |
291 | if (cursor.toFirstChild()) {\r | |
292 | do {\r | |
293 | if (cursor.getName().getLocalPart().equalsIgnoreCase("Section")) {\r | |
294 | cursor.push();\r | |
295 | dealSection(mode, doc, ele, cursor, list);\r | |
296 | cursor.pop();\r | |
297 | } else if (cursor.getName().getLocalPart().equalsIgnoreCase("Sections")) {\r | |
298 | cursor.push();\r | |
299 | dealSections(mode, doc, ele, cursor, list);\r | |
300 | cursor.pop();\r | |
301 | }\r | |
302 | } while (cursor.toNextSibling());\r | |
303 | }\r | |
304 | root.appendChild(ele);\r | |
305 | }\r | |
306 | \r | |
307 | /**\r | |
308 | Recursively deal with section.\r | |
309 | \r | |
310 | @param mode Current node mode (MODE_NONE | MODE_COMPREE | MODE_GUID_DEFINED)\r | |
311 | @param doc Xml Document\r | |
312 | @param root Root Node\r | |
313 | @param cursor Current FFS layout cursor\r | |
314 | @param list List of section type here used\r | |
315 | **/\r | |
316 | private void dealSection(int mode, Document doc, Element root, XmlCursor cursor, Vector<String> list) {\r | |
317 | String type = cursor.getAttributeText(new QName("SectionType"));\r | |
8a9783c1 | 318 | \r |
319 | //\r | |
320 | // Judge if file is specified? Yes, just use the file, else call Build Macro\r | |
321 | // If fileName is null, means without FileNames specify in FPD file\r | |
322 | //\r | |
323 | String fileName = null;\r | |
324 | cursor.push();\r | |
325 | if (cursor.toFirstChild()) {\r | |
326 | do {\r | |
327 | if (cursor.getName().getLocalPart().equalsIgnoreCase("Filenames")) {\r | |
328 | cursor.push();\r | |
329 | if (cursor.toFirstChild()) {\r | |
330 | do {\r | |
331 | if (cursor.getName().getLocalPart().equalsIgnoreCase("Filename")) {\r | |
332 | fileName = cursor.getTextValue();\r | |
333 | }\r | |
334 | } while (cursor.toNextSibling());\r | |
335 | }\r | |
336 | cursor.pop();\r | |
337 | }\r | |
338 | } while (cursor.toNextSibling());\r | |
339 | }\r | |
340 | \r | |
341 | cursor.pop();\r | |
342 | \r | |
343 | if (fileName == null) {\r | |
344 | list.addElement(type);\r | |
345 | }\r | |
878ddf1f | 346 | if (mode == MODE_GUID_DEFINED) {\r |
347 | //\r | |
348 | // <input file="${DEST_DIR_OUTPUT}\Bds.pe32"/>\r | |
349 | //\r | |
350 | Element ele = doc.createElement("input");\r | |
8a9783c1 | 351 | if (fileName == null) {\r |
352 | ele.setAttribute("file", "${DEST_DIR_OUTPUT}" + File.separatorChar + basename + getSectionExt(type));\r | |
353 | } else {\r | |
354 | ele.setAttribute("file", "${PLATFORM_DIR}" + File.separatorChar + fileName);\r | |
355 | }\r | |
878ddf1f | 356 | root.appendChild(ele);\r |
357 | } else {\r | |
358 | //\r | |
359 | // <sectFile fileName= "..."/>\r | |
360 | //\r | |
361 | Element ele = doc.createElement("sectFile");\r | |
8a9783c1 | 362 | if (fileName == null) {\r |
363 | ele.setAttribute("fileName", "${DEST_DIR_OUTPUT}" + File.separatorChar + basename + getSectionExt(type));\r | |
364 | } else {\r | |
365 | ele.setAttribute("fileName", "${PLATFORM_DIR}" + File.separatorChar + fileName);\r | |
366 | }\r | |
878ddf1f | 367 | root.appendChild(ele);\r |
368 | }\r | |
369 | }\r | |
370 | \r | |
371 | /**\r | |
a29c47e0 | 372 | Get the corresponding section file suffix.\r |
878ddf1f | 373 | \r |
374 | @param type Section type\r | |
375 | @return Corresponding section file extension\r | |
376 | **/\r | |
377 | private String getSectionExt(String type) {\r | |
378 | for (int i = 0; i < sectionExt.length; i++) {\r | |
379 | if (sectionExt[i][0].equalsIgnoreCase(type)) {\r | |
380 | return sectionExt[i][1];\r | |
381 | }\r | |
382 | }\r | |
383 | return ".sec";\r | |
384 | }\r | |
385 | \r | |
386 | /**\r | |
387 | Return the ANT script to call GenFfs Tool.\r | |
388 | \r | |
389 | @return ANT script to call GenFfs Tool\r | |
390 | **/\r | |
391 | public Element getFfsNode() {\r | |
392 | return ffsNode;\r | |
393 | }\r | |
394 | }\r |