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