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
;
22 import java
.util
.ArrayList
;
23 import java
.util
.Iterator
;
24 import java
.util
.List
;
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
;
34 public class GenSectionTask
extends Task
implements EfiDefine
, Section
, FfsTypes
{
38 private final static String toolName
= "GenSection";
42 private FileArg inputFile
= new FileArg();
47 private FileArg outputFile
= new FileArg();
52 private ToolArg sectionType
= new ToolArg();
57 private ToolArg versionNum
= new ToolArg();
62 private ToolArg interfaceString
= new ToolArg();
67 private List
<Section
> sectFileList
= new ArrayList
<Section
>();
70 // flag indicated the <tool> element
72 private boolean haveTool
= false;
77 GenSectionTaks execute is to assemble tool command line & execute tool
80 @throws BuildException
82 public void execute() throws BuildException
{
84 Project project
= this.getOwningTarget().getProject();
86 // absolute path of efi tools
88 String path
= project
.getProperty("env.FRAMEWORK_TOOLS_PATH");
92 command
= path
+ File
.separator
+ toolName
;
97 String argument
= "" + inputFile
+ outputFile
+ sectionType
+ versionNum
+ interfaceString
;
99 // return value of gensection execution
104 Commandline cmdline
= new Commandline();
105 cmdline
.setExecutable(command
);
106 cmdline
.createArgument().setLine(argument
);
108 LogStreamHandler streamHandler
= new LogStreamHandler(this,
109 Project
.MSG_INFO
, Project
.MSG_WARN
);
110 Execute runner
= new Execute(streamHandler
, null);
112 runner
.setAntRun(project
);
113 runner
.setCommandline(cmdline
.getCommandline());
115 EdkLog
.log(this, inputFile
.toFileList() + versionNum
.getValue()
116 + interfaceString
.getValue() + " => " + outputFile
.toFileList());
117 EdkLog
.log(this, EdkLog
.EDK_VERBOSE
, Commandline
.toString(cmdline
.getCommandline()));
119 revl
= runner
.execute();
120 if (EFI_SUCCESS
== revl
) {
121 EdkLog
.log(this, EdkLog
.EDK_VERBOSE
, toolName
+ " succeeded!");
124 // command execution fail
126 EdkLog
.log(this, EdkLog
.EDK_INFO
, "ERROR = " + Integer
.toHexString(revl
));
127 throw new BuildException(toolName
+ " failed!");
129 } catch (Exception e
) {
130 throw new BuildException(e
.getMessage());
137 This function is to get class member "inputFile".
139 @return name of input file
141 public String
getInputFile() {
142 return this.inputFile
.getValue();
148 This function is to set class member "inputFile".
150 @param inputFile name of input file
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".
171 @param outputFile name of output file
173 public void setOutputfile(String outputFile
) {
174 this.outputFile
.setArg(" -o ", outputFile
);
180 This function is to get class member "sectionType".
184 public String
getSectionType() {
185 return this.sectionType
.getValue();
191 This function is to set class member "sectionType".
193 @param sectionType section type
195 public void setSectionType(String sectionType
) {
196 this.sectionType
.setArg(" -s ", sectionType
);
202 This function is to get class member "versionNum".
203 @return version number
205 public String
getVersionNum() {
206 return this.versionNum
.getValue();
212 This function is to set class member "versionNum".
213 @param versionNum version number
215 public void setVersionNum(String versionNum
) {
216 this.versionNum
.setArg(" -v ", versionNum
);
222 This function is to get class member "interfaceString".
223 @return interface string
225 public String
getInterfaceString() {
226 return this.interfaceString
.getValue();
232 This funcion is to set class member "interfaceString".
233 @param interfaceString interface string
235 public void setInterfaceString(String interfaceString
) {
236 this.interfaceString
.setArg(" -a ", "\"" + interfaceString
+ "\"");
242 This function is to add sectFile to list.
244 @param sectFile instance of sectFile.
246 public void addSectFile(SectFile sectFile
){
247 this.sectFileList
.add(sectFile
);
253 This function is to set the class member "Tool";
257 public void addTool(Tool tool
) {
258 this.sectFileList
.add(tool
);
259 this.haveTool
= true;
265 This function is to add GenSectin element to list
266 @param task Instance of genSection
268 public void addGenSection(GenSectionTask task
){
269 this.sectFileList
.add(task
);
272 public void toBuffer(DataOutputStream buffer
){
274 // Search SectionList find earch section and call it's
275 // ToBuffer function.
277 if (this.sectionType
.getValue().equalsIgnoreCase(
278 "EFI_SECTION_COMPRESSION")
283 // Get section file in compress node.
286 ByteArrayOutputStream bo
= new ByteArrayOutputStream();
287 DataOutputStream Do
= new DataOutputStream(bo
);
290 // Get each section which under the compress {};
291 // And add it is contains to File;
293 Iterator SectionIter
= this.sectFileList
.iterator();
294 while (SectionIter
.hasNext()) {
295 sect
= (Section
) SectionIter
.next();
298 // Call each section class's toBuffer function.
302 } catch (BuildException e
) {
303 System
.out
.print(e
.getMessage());
304 throw new BuildException(
305 "Compress.toBuffer failed at section");
314 byte[] fileBuffer
= bo
.toByteArray();
316 synchronized (CompressSection
.semaphore
) {
317 Compress myCompress
= new Compress(fileBuffer
,
321 // Add Compress header
323 CompressHeader Ch
= new CompressHeader();
324 Ch
.SectionHeader
.Size
[0] = (byte) ((myCompress
.outputBuffer
.length
+ Ch
326 Ch
.SectionHeader
.Size
[1] = (byte) (((myCompress
.outputBuffer
.length
+ Ch
327 .GetSize()) & 0xff00) >> 8);
328 Ch
.SectionHeader
.Size
[2] = (byte) (((myCompress
.outputBuffer
.length
+ Ch
329 .GetSize()) & 0xff0000) >> 16);
330 Ch
.SectionHeader
.type
= (byte) EFI_SECTION_COMPRESSION
;
333 // Note: The compressName was not efsfective now. Using the
334 // EFI_STANDARD_COMPRSSION for compressType .
335 // That is follow old Genffsfile tools. Some code will be
337 // the different compressName;
339 Ch
.UncompressLen
= fileBuffer
.length
;
340 Ch
.CompressType
= EFI_STANDARD_COMPRESSION
;
343 // Change header struct to byte buffer
345 byte[] headerBuffer
= new byte[Ch
.GetSize()];
346 Ch
.StructToBuffer(headerBuffer
);
349 // First add CompressHeader to Buffer, then add Compress
352 buffer
.write(headerBuffer
);
353 buffer
.write(myCompress
.outputBuffer
);
356 // Buffer 4 Byte aligment
358 int size
= Ch
.GetSize() + myCompress
.outputBuffer
.length
;
360 while ((size
& 0x03) != 0) {
365 } catch (Exception e
) {
366 throw new BuildException("compress.toBuffer failed!\n");
370 Iterator sectionIter
= this.sectFileList
.iterator();
371 while (sectionIter
.hasNext()) {
372 sect
= (Section
) sectionIter
.next();
375 // The last section don't need 4 byte ffsAligment.
377 sect
.toBuffer(buffer
);
378 } catch (Exception e
) {
379 throw new BuildException(e
.getMessage());