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