4 GenSectionTask is to call GenSection.exe to generate Section.
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
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.
17 package org
.tianocore
.framework
.tasks
;
19 import java
.io
.ByteArrayOutputStream
;
20 import java
.io
.DataOutputStream
;
21 import java
.util
.ArrayList
;
22 import java
.util
.Iterator
;
23 import java
.util
.List
;
25 import org
.apache
.tools
.ant
.BuildException
;
26 import org
.apache
.tools
.ant
.Project
;
27 import org
.apache
.tools
.ant
.Task
;
28 import org
.apache
.tools
.ant
.taskdefs
.Execute
;
29 import org
.apache
.tools
.ant
.taskdefs
.LogStreamHandler
;
30 import org
.apache
.tools
.ant
.types
.Commandline
;
31 import org
.tianocore
.common
.logger
.EdkLog
;
33 public class GenSectionTask
extends Task
implements EfiDefine
, Section
,
38 private FileArg inputFile
= new FileArg();
43 private FileArg outputFile
= new FileArg();
48 private ToolArg sectionType
= new ToolArg();
53 private ToolArg versionNum
= new ToolArg();
58 private ToolArg interfaceString
= new ToolArg();
63 private List
<Section
> sectFileList
= new ArrayList
<Section
>();
66 // flag indicated the <tool> element
68 private boolean haveTool
= false;
73 * GenSectionTaks execute is to assemble tool command line & execute tool
76 * @throws BuildException
78 public void execute() throws BuildException
{
80 Project project
= this.getOwningTarget().getProject();
82 // absolute path of efi tools
84 String path
= project
.getProperty("env.FRAMEWORK_TOOLS_PATH");
86 command
= "GenSection";
88 command
= path
+ "/" + "GenSection";
93 String argument
= "" + inputFile
+ outputFile
+ sectionType
94 + versionNum
+ interfaceString
;
96 // return value of gensection execution
101 Commandline cmdline
= new Commandline();
102 cmdline
.setExecutable(command
);
103 cmdline
.createArgument().setLine(argument
);
105 LogStreamHandler streamHandler
= new LogStreamHandler(this,
106 Project
.MSG_INFO
, Project
.MSG_WARN
);
107 Execute runner
= new Execute(streamHandler
, null);
109 runner
.setAntRun(project
);
110 runner
.setCommandline(cmdline
.getCommandline());
112 EdkLog
.log(this, inputFile
.toFileList() + " => "
113 + outputFile
.toFileList());
114 EdkLog
.log(this, EdkLog
.EDK_VERBOSE
, Commandline
.toString(cmdline
117 revl
= runner
.execute();
118 if (EFI_SUCCESS
== revl
) {
119 EdkLog
.log(this, EdkLog
.EDK_VERBOSE
, "GenSection succeeded!");
122 // command execution fail
124 EdkLog
.log(this, EdkLog
.EDK_INFO
, "ERROR = "
125 + Integer
.toHexString(revl
));
126 throw new BuildException("GenSection failed!");
128 } catch (Exception e
) {
129 throw new BuildException(e
.getMessage());
136 * This function is to get class member "inputFile".
138 * @return name of input file
140 public String
getInputFile() {
141 return this.inputFile
.getValue();
147 * This function is to set class member "inputFile".
152 public void setInputFile(String inputFile
) {
153 this.inputFile
.setArg(" -i ", inputFile
);
159 * This function is to get class member "outputFile".
161 * @return name of output file
163 public String
getOutputFile() {
164 return this.outputFile
.getValue();
170 * This function is to set class member "outputFile".
173 * name of output file
175 public void setOutputfile(String outputFile
) {
176 this.outputFile
.setArg(" -o ", outputFile
);
182 * This function is to get class member "sectionType".
184 * @return sectoin type
186 public String
getSectionType() {
187 return this.sectionType
.getValue();
193 * This function is to set class member "sectionType".
198 public void setSectionType(String sectionType
) {
199 this.sectionType
.setArg(" -s ", sectionType
);
205 * This function is to get class member "versionNum".
207 * @return version number
209 public String
getVersionNum() {
210 return this.versionNum
.getValue();
216 * This function is to set class member "versionNum".
221 public void setVersionNum(String versionNum
) {
222 this.versionNum
.setArg(" -v ", versionNum
);
228 * This function is to get class member "interfaceString".
230 * @return interface string
232 public String
getInterfaceString() {
233 return this.interfaceString
.getValue();
239 * This funcion is to set class member "interfaceString".
241 * @param interfaceString
244 public void setInterfaceString(String interfaceString
) {
245 this.interfaceString
.setArg(" -a ", "\"" + interfaceString
+ "\"");
251 * This function is to add sectFile to list.
254 * instance of sectFile.
256 public void addSectFile(SectFile sectFile
) {
257 this.sectFileList
.add(sectFile
);
263 * This function is to set the class member "Tool";
267 public void addTool(Tool tool
) {
268 this.sectFileList
.add(tool
);
269 this.haveTool
= true;
275 * This function is to add GenSectin element to list
278 * Instance of genSection
280 public void addGenSection(GenSectionTask task
) {
281 this.sectFileList
.add(task
);
284 public void toBuffer(DataOutputStream buffer
) {
286 // Search SectionList find earch section and call it's
287 // ToBuffer function.
289 if (this.sectionType
.getValue().equalsIgnoreCase(
290 "EFI_SECTION_COMPRESSION")
295 // Get section file in compress node.
298 ByteArrayOutputStream bo
= new ByteArrayOutputStream();
299 DataOutputStream Do
= new DataOutputStream(bo
);
302 // Get each section which under the compress {};
303 // And add it is contains to File;
305 Iterator SectionIter
= this.sectFileList
.iterator();
306 while (SectionIter
.hasNext()) {
307 sect
= (Section
) SectionIter
.next();
310 // Call each section class's toBuffer function.
314 } catch (BuildException e
) {
315 System
.out
.print(e
.getMessage());
316 throw new BuildException(
317 "Compress.toBuffer failed at section");
326 byte[] fileBuffer
= bo
.toByteArray();
328 synchronized (CompressSection
.semaphore
) {
329 Compress myCompress
= new Compress(fileBuffer
,
333 // Add Compress header
335 CompressHeader Ch
= new CompressHeader();
336 Ch
.SectionHeader
.Size
[0] = (byte) ((myCompress
.outputBuffer
.length
+ Ch
338 Ch
.SectionHeader
.Size
[1] = (byte) (((myCompress
.outputBuffer
.length
+ Ch
339 .GetSize()) & 0xff00) >> 8);
340 Ch
.SectionHeader
.Size
[2] = (byte) (((myCompress
.outputBuffer
.length
+ Ch
341 .GetSize()) & 0xff0000) >> 16);
342 Ch
.SectionHeader
.type
= (byte) EFI_SECTION_COMPRESSION
;
345 // Note: The compressName was not efsfective now. Using the
346 // EFI_STANDARD_COMPRSSION for compressType .
347 // That is follow old Genffsfile tools. Some code will be
349 // the different compressName;
351 Ch
.UncompressLen
= fileBuffer
.length
;
352 Ch
.CompressType
= EFI_STANDARD_COMPRESSION
;
355 // Change header struct to byte buffer
357 byte[] headerBuffer
= new byte[Ch
.GetSize()];
358 Ch
.StructToBuffer(headerBuffer
);
361 // First add CompressHeader to Buffer, then add Compress
364 buffer
.write(headerBuffer
);
365 buffer
.write(myCompress
.outputBuffer
);
368 // Buffer 4 Byte aligment
370 int size
= Ch
.GetSize() + myCompress
.outputBuffer
.length
;
372 while ((size
& 0x03) != 0) {
377 } catch (Exception e
) {
378 throw new BuildException("compress.toBuffer failed!\n");
382 Iterator sectionIter
= this.sectFileList
.iterator();
383 while (sectionIter
.hasNext()) {
384 sect
= (Section
) sectionIter
.next();
387 // The last section don't need 4 byte ffsAligment.
389 sect
.toBuffer(buffer
);
390 } catch (Exception e
) {
391 throw new BuildException(e
.getMessage());