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
{
35 private int alignment
= 0;
39 private final static String toolName
= "GenSection";
43 private FileArg inputFile
= new FileArg();
48 private FileArg outputFile
= new FileArg();
53 private ToolArg sectionType
= new ToolArg();
58 private ToolArg versionNum
= new ToolArg();
63 private ToolArg interfaceString
= new ToolArg();
68 private List
<Section
> sectFileList
= new ArrayList
<Section
>();
71 // flag indicated the <tool> element
73 private boolean haveTool
= false;
78 GenSectionTaks execute is to assemble tool command line & execute tool
81 @throws BuildException
83 public void execute() throws BuildException
{
85 Project project
= this.getOwningTarget().getProject();
87 // absolute path of efi tools
89 String path
= project
.getProperty("env.FRAMEWORK_TOOLS_PATH");
93 command
= path
+ File
.separator
+ toolName
;
98 String argument
= "" + inputFile
+ outputFile
+ sectionType
+ versionNum
+ interfaceString
;
100 // return value of gensection execution
105 Commandline cmdline
= new Commandline();
106 cmdline
.setExecutable(command
);
107 cmdline
.createArgument().setLine(argument
);
109 LogStreamHandler streamHandler
= new LogStreamHandler(this,
110 Project
.MSG_INFO
, Project
.MSG_WARN
);
111 Execute runner
= new Execute(streamHandler
, null);
113 runner
.setAntRun(project
);
114 runner
.setCommandline(cmdline
.getCommandline());
116 EdkLog
.log(this, inputFile
.toFileList() + versionNum
.getValue()
117 + interfaceString
.getValue() + " => " + outputFile
.toFileList());
118 EdkLog
.log(this, EdkLog
.EDK_VERBOSE
, Commandline
.toString(cmdline
.getCommandline()));
120 revl
= runner
.execute();
121 if (EFI_SUCCESS
== revl
) {
122 EdkLog
.log(this, EdkLog
.EDK_VERBOSE
, toolName
+ " succeeded!");
125 // command execution fail
127 EdkLog
.log(this, EdkLog
.EDK_INFO
, "ERROR = " + Integer
.toHexString(revl
));
128 throw new BuildException(toolName
+ " failed!");
130 } catch (Exception e
) {
131 throw new BuildException(e
.getMessage());
138 This function is to get class member "inputFile".
140 @return name of input file
142 public String
getInputFile() {
143 return this.inputFile
.getValue();
149 This function is to set class member "inputFile".
151 @param inputFile name of input file
153 public void setInputFile(String inputFile
) {
154 this.inputFile
.setArg(" -i ", inputFile
);
160 This function is to get class member "outputFile".
162 @return name of output file
164 public String
getOutputFile() {
165 return this.outputFile
.getValue();
171 This function is to set class member "outputFile".
172 @param outputFile name of output file
174 public void setOutputfile(String outputFile
) {
175 this.outputFile
.setArg(" -o ", outputFile
);
181 This function is to get class member "sectionType".
185 public String
getSectionType() {
186 return this.sectionType
.getValue();
192 This function is to set class member "sectionType".
194 @param sectionType section type
196 public void setSectionType(String sectionType
) {
197 this.sectionType
.setArg(" -s ", sectionType
);
203 This function is to get class member "versionNum".
204 @return version number
206 public String
getVersionNum() {
207 return this.versionNum
.getValue();
213 This function is to set class member "versionNum".
214 @param versionNum version number
216 public void setVersionNum(String versionNum
) {
217 this.versionNum
.setArg(" -v ", versionNum
);
223 This function is to get class member "interfaceString".
224 @return interface string
226 public String
getInterfaceString() {
227 return this.interfaceString
.getValue();
233 This funcion is to set class member "interfaceString".
234 @param interfaceString interface string
236 public void setInterfaceString(String interfaceString
) {
237 this.interfaceString
.setArg(" -a ", "\"" + interfaceString
+ "\"");
243 This function is to add sectFile to list.
245 @param sectFile instance of sectFile.
247 public void addSectFile(SectFile sectFile
){
248 this.sectFileList
.add(sectFile
);
254 This function is to set the class member "Tool";
258 public void addTool(Tool tool
) {
259 this.sectFileList
.add(tool
);
260 this.haveTool
= true;
266 This function is to add GenSectin element to list
267 @param task Instance of genSection
269 public void addGenSection(GenSectionTask task
){
270 this.sectFileList
.add(task
);
273 public int getAlignment() {
277 public void setAlignment(int alignment
) {
281 this.alignment
= alignment
;
285 public void toBuffer(DataOutputStream buffer
){
287 // Search SectionList find earch section and call it's
288 // ToBuffer function.
290 if (this.sectionType
.getValue().equalsIgnoreCase(
291 "EFI_SECTION_COMPRESSION")
296 // Get section file in compress node.
299 ByteArrayOutputStream bo
= new ByteArrayOutputStream();
300 DataOutputStream Do
= new DataOutputStream(bo
);
303 // Get each section which under the compress {};
304 // And add it is contains to File;
306 Iterator SectionIter
= this.sectFileList
.iterator();
307 while (SectionIter
.hasNext()) {
308 sect
= (Section
) SectionIter
.next();
311 // Call each section class's toBuffer function.
315 } catch (BuildException e
) {
316 System
.out
.print(e
.getMessage());
317 throw new BuildException(
318 "Compress.toBuffer failed at section");
328 byte[] fileBuffer
= bo
.toByteArray();
330 synchronized (CompressSection
.semaphore
) {
331 Compress myCompress
= new Compress(fileBuffer
,
335 // Add Compress header
337 CompressHeader Ch
= new CompressHeader();
338 Ch
.SectionHeader
.Size
[0] = (byte) ((myCompress
.outputBuffer
.length
+ Ch
340 Ch
.SectionHeader
.Size
[1] = (byte) (((myCompress
.outputBuffer
.length
+ Ch
341 .GetSize()) & 0xff00) >> 8);
342 Ch
.SectionHeader
.Size
[2] = (byte) (((myCompress
.outputBuffer
.length
+ Ch
343 .GetSize()) & 0xff0000) >> 16);
344 Ch
.SectionHeader
.type
= (byte) EFI_SECTION_COMPRESSION
;
347 // Note: The compressName was not efsfective now. Using the
348 // EFI_STANDARD_COMPRSSION for compressType .
349 // That is follow old Genffsfile tools. Some code will be
351 // the different compressName;
353 Ch
.UncompressLen
= fileBuffer
.length
;
354 Ch
.CompressType
= EFI_STANDARD_COMPRESSION
;
357 // Change header struct to byte buffer
359 byte[] headerBuffer
= new byte[Ch
.GetSize()];
360 Ch
.StructToBuffer(headerBuffer
);
363 // First add CompressHeader to Buffer, then add Compress
366 buffer
.write(headerBuffer
);
367 buffer
.write(myCompress
.outputBuffer
);
370 // Buffer 4 Byte aligment
372 int size
= Ch
.GetSize() + myCompress
.outputBuffer
.length
;
374 while ((size
& 0x03) != 0) {
379 } catch (Exception e
) {
380 throw new BuildException("GenSection<SectionType=EFI_SECTION_COMPRESSION> to Buffer failed!\n");
384 Iterator sectionIter
= this.sectFileList
.iterator();
385 while (sectionIter
.hasNext()) {
386 sect
= (Section
) sectionIter
.next();
389 // The last section don't need 4 byte ffsAligment.
391 sect
.toBuffer(buffer
);
392 } catch (Exception e
) {
393 throw new BuildException(e
.getMessage());