]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/FrameworkTasks/org/tianocore/framework/tasks/GenSectionTask.java
Support calling customized compression tool in FrameworkTask.
[mirror_edk2.git] / Tools / Source / FrameworkTasks / org / tianocore / framework / tasks / GenSectionTask.java
1 /** @file
2 GenSectionTask class.
3
4 GenSectionTask is to call GenSection.exe to generate Section.
5
6 Copyright (c) 2006, Intel Corporation
7 All rights reserved. This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
11
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14
15 **/
16
17 package org.tianocore.framework.tasks;
18
19 import java.io.ByteArrayOutputStream;
20 import java.io.DataOutputStream;
21 import java.io.File;
22 import java.util.ArrayList;
23 import java.util.Iterator;
24 import java.util.List;
25
26 import org.apache.tools.ant.BuildException;
27 import org.apache.tools.ant.Project;
28 import org.apache.tools.ant.Task;
29 import org.apache.tools.ant.taskdefs.Execute;
30 import org.apache.tools.ant.taskdefs.LogStreamHandler;
31 import org.apache.tools.ant.types.Commandline;
32 import org.tianocore.common.logger.EdkLog;
33
34 public class GenSectionTask extends Task implements EfiDefine, Section,FfsTypes {
35 ///
36 /// inputfile name
37 ///
38 private String inputFile = "";
39 ///
40 ///
41 ///
42 private String inputFileName = "";
43 ///
44 /// outputfile name
45 ///
46 private String outputFile = "";
47 ///
48 /// section type
49 ///
50 private String sectionType = "";
51 ///
52 /// version number
53 ///
54 private String versionNum = "";
55 ///
56 /// interface string
57 ///
58 private String interfaceString = "";
59 ///
60 /// Section file list
61 ///
62 private List<Section> sectFileList = new ArrayList<Section>();
63
64 /**
65 execute
66
67 GenSectionTaks execute is to assemble tool command line & execute tool
68 command line.
69
70 @throws BuildException
71 **/
72 public void execute() throws BuildException {
73 String command;
74 Project project = this.getOwningTarget().getProject();
75 //
76 // absolute path of efi tools
77 //
78 String path = project.getProperty("env.FRAMEWORK_TOOLS_PATH");
79 if (path == null) {
80 command = "GenSection";
81 } else {
82 command = path + "/" + "GenSection";
83 }
84 //
85 // argument of tools
86 //
87 String argument = inputFile + outputFile + " -s "+ sectionType + versionNum
88 + interfaceString;
89 //
90 // return value of gensection execution
91 //
92 int revl = -1;
93
94 try {
95 Commandline cmdline = new Commandline();
96 cmdline.setExecutable(command);
97 cmdline.createArgument().setLine(argument);
98
99 LogStreamHandler streamHandler = new LogStreamHandler(this,
100 Project.MSG_INFO, Project.MSG_WARN);
101 Execute runner = new Execute(streamHandler, null);
102
103 runner.setAntRun(project);
104 runner.setCommandline(cmdline.getCommandline());
105
106 EdkLog.log(this, EdkLog.EDK_INFO, inputFileName);
107 EdkLog.log(this, EdkLog.EDK_DEBUG, Commandline.toString(cmdline.getCommandline()));
108 revl = runner.execute();
109 if (EFI_SUCCESS == revl) {
110 log("GenSection succeeded!", Project.MSG_VERBOSE);
111 } else {
112 //
113 // command execution fail
114 //
115 log("ERROR = " + Integer.toHexString(revl));
116 throw new BuildException("GenSection failed!");
117 }
118 } catch (Exception e) {
119 throw new BuildException(e.getMessage());
120 }
121 }
122
123 /**
124 getInputFile
125
126 This function is to get class member "inputFile".
127
128 @return name of input file
129 **/
130 public String getInputFile() {
131 return this.inputFile;
132 }
133
134 /**
135 setInputFile
136
137 This function is to set class member "inputFile".
138
139 @param inputFile name of input file
140 **/
141 public void setInputFile(String inputFile) {
142 this.inputFileName = (new File(inputFile)).getName();
143 this.inputFile = " -i " + inputFile;
144 }
145
146 /**
147 getOutputFile
148
149 This function is to get class member "outputFile".
150
151 @return name of output file
152 **/
153 public String getOutputFile() {
154 return this.outputFile;
155 }
156
157 /**
158 setOutputfile
159
160 This function is to set class member "outputFile".
161 @param outputFile name of output file
162 **/
163 public void setOutputfile(String outputFile) {
164 this.outputFile = " -o " + outputFile;
165 }
166
167 /**
168 getSectionType
169
170 This function is to get class member "sectionType".
171
172 @return sectoin type
173 **/
174 public String getSectionType() {
175 return this.sectionType;
176 }
177
178 /**
179 setSectionType
180
181 This function is to set class member "sectionType".
182
183 @param sectionType section type
184 **/
185 public void setSectionType(String sectionType) {
186 this.sectionType = sectionType;
187 }
188
189 /**
190 getVersionNum
191
192 This function is to get class member "versionNum".
193 @return version number
194 **/
195 public String getVersionNum() {
196 return this.versionNum;
197 }
198
199 /**
200 setVersionNume
201
202 This function is to set class member "versionNum".
203 @param versionNum version number
204 **/
205 public void setVersionNum(String versionNum) {
206 this.versionNum = " -v " + versionNum;
207 }
208
209 /**
210 getInterfaceString
211
212 This function is to get class member "interfaceString".
213 @return interface string
214 **/
215 public String getInterfaceString() {
216 return this.interfaceString;
217 }
218
219 /**
220 setInterfaceString
221
222 This funcion is to set class member "interfaceString".
223 @param interfaceString interface string
224 **/
225 public void setInterfaceString(String interfaceString) {
226 this.interfaceString = " -a " + "\"" + interfaceString + "\"";
227 }
228
229 /**
230 addSectFile
231
232 This function is to add sectFile to list.
233
234 @param sectFile instance of sectFile.
235 **/
236 public void addSectFile(SectFile sectFile){
237 this.sectFileList.add(sectFile);
238 }
239
240 /**
241 setTool
242
243 This function is to set the class member "Tool";
244
245 @param tool
246 **/
247 public void addTool(Tool tool) {
248 this.sectFileList.add(tool);
249 }
250
251 /**
252 addGenSection
253
254 This function is to add GenSectin element to list
255 @param task Instance of genSection
256 **/
257 public void addGenSection(GenSectionTask task){
258 this.sectFileList.add(task);
259 }
260
261 public void toBuffer(DataOutputStream buffer){
262 //
263 // Search SectionList find earch section and call it's
264 // ToBuffer function.
265 //
266 if (this.sectionType.equalsIgnoreCase("EFI_SECTION_COMPRESSION")){
267 Section sect;
268
269 //
270 // Get section file in compress node.
271 //
272 try{
273 ByteArrayOutputStream bo = new ByteArrayOutputStream ();
274 DataOutputStream Do = new DataOutputStream (bo);
275
276 //
277 // Get each section which under the compress {};
278 // And add it is contains to File;
279 //
280 Iterator SectionIter = this.sectFileList.iterator();
281 while (SectionIter.hasNext()){
282 sect = (Section)SectionIter.next();
283
284 //
285 // Call each section class's toBuffer function.
286 //
287 try {
288 sect.toBuffer(Do);
289 }
290 catch (BuildException e) {
291 System.out.print(e.getMessage());
292 throw new BuildException ("Compress.toBuffer failed at section");
293 }
294
295 }
296 Do.close();
297
298 //
299 // Call compress
300 //
301 byte[] fileBuffer = bo.toByteArray();
302 Compress myCompress = new Compress(fileBuffer, fileBuffer.length);
303
304 //
305 // Add Compress header
306 //
307 CompressHeader Ch = new CompressHeader();
308 Ch.SectionHeader.Size[0] = (byte)((myCompress.outputBuffer.length +
309 Ch.GetSize()) &
310 0xff
311 );
312 Ch.SectionHeader.Size[1] = (byte)(((myCompress.outputBuffer.length +
313 Ch.GetSize())&
314 0xff00) >> 8
315 );
316 Ch.SectionHeader.Size[2] = (byte)(((myCompress.outputBuffer.length +
317 Ch.GetSize()) &
318 0xff0000) >> 16
319 );
320 Ch.SectionHeader.type = (byte) EFI_SECTION_COMPRESSION;
321
322 //
323 // Note: The compressName was not efsfective now. Using the
324 // EFI_STANDARD_COMPRSSION for compressType .
325 // That is follow old Genffsfile tools. Some code will be added for
326 // the different compressName;
327 //
328 Ch.UncompressLen = fileBuffer.length;
329 Ch.CompressType = EFI_STANDARD_COMPRESSION;
330
331 //
332 // Change header struct to byte buffer
333 //
334 byte [] headerBuffer = new byte[Ch.GetSize()];
335 Ch.StructToBuffer(headerBuffer);
336
337 //
338 // First add CompressHeader to Buffer, then add Compress data.
339 //
340 buffer.write (headerBuffer);
341 buffer.write(myCompress.outputBuffer);
342
343 //
344 // Buffer 4 Byte aligment
345 //
346 int size = Ch.GetSize() + myCompress.outputBuffer.length;
347
348 while ((size & 0x03) != 0){
349 size ++;
350 buffer.writeByte(0);
351 }
352 }
353 catch (Exception e){
354 throw new BuildException("compress.toBuffer failed!\n");
355 }
356 } else {
357 Section sect;
358 Iterator sectionIter = this.sectFileList.iterator();
359 while (sectionIter.hasNext()) {
360 sect = (Section)sectionIter.next();
361 try {
362 //
363 // The last section don't need 4 byte ffsAligment.
364 //
365 sect.toBuffer(buffer);
366 } catch (Exception e) {
367 throw new BuildException (e.getMessage());
368 }
369 }
370 }
371 }
372 }