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
;
23 import java
.util
.ArrayList
;
24 import java
.util
.Iterator
;
25 import java
.util
.List
;
27 import org
.apache
.tools
.ant
.BuildException
;
28 import org
.apache
.tools
.ant
.Project
;
29 import org
.apache
.tools
.ant
.Task
;
30 import org
.apache
.tools
.ant
.taskdefs
.Execute
;
31 import org
.apache
.tools
.ant
.taskdefs
.LogStreamHandler
;
32 import org
.apache
.tools
.ant
.types
.Commandline
;
33 import org
.tianocore
.common
.logger
.EdkLog
;
35 public class GenSectionTask
extends Task
implements EfiDefine
, Section
, FfsTypes
{
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 void toBuffer(DataOutputStream buffer
){
275 // Search SectionList find earch section and call it's
276 // ToBuffer function.
278 if (this.sectionType
.getValue().equalsIgnoreCase(
279 "EFI_SECTION_COMPRESSION")
284 // Get section file in compress node.
287 ByteArrayOutputStream bo
= new ByteArrayOutputStream();
288 DataOutputStream Do
= new DataOutputStream(bo
);
291 // Get each section which under the compress {};
292 // And add it is contains to File;
294 Iterator SectionIter
= this.sectFileList
.iterator();
295 while (SectionIter
.hasNext()) {
296 sect
= (Section
) SectionIter
.next();
299 // Call each section class's toBuffer function.
303 } catch (BuildException e
) {
304 System
.out
.print(e
.getMessage());
305 throw new BuildException(
306 "Compress.toBuffer failed at section");
315 byte[] fileBuffer
= bo
.toByteArray();
317 synchronized (CompressSection
.semaphore
) {
318 Compress myCompress
= new Compress(fileBuffer
,
322 // Add Compress header
324 CompressHeader Ch
= new CompressHeader();
325 Ch
.SectionHeader
.Size
[0] = (byte) ((myCompress
.outputBuffer
.length
+ Ch
327 Ch
.SectionHeader
.Size
[1] = (byte) (((myCompress
.outputBuffer
.length
+ Ch
328 .GetSize()) & 0xff00) >> 8);
329 Ch
.SectionHeader
.Size
[2] = (byte) (((myCompress
.outputBuffer
.length
+ Ch
330 .GetSize()) & 0xff0000) >> 16);
331 Ch
.SectionHeader
.type
= (byte) EFI_SECTION_COMPRESSION
;
334 // Note: The compressName was not efsfective now. Using the
335 // EFI_STANDARD_COMPRSSION for compressType .
336 // That is follow old Genffsfile tools. Some code will be
338 // the different compressName;
340 Ch
.UncompressLen
= fileBuffer
.length
;
341 Ch
.CompressType
= EFI_STANDARD_COMPRESSION
;
344 // Change header struct to byte buffer
346 byte[] headerBuffer
= new byte[Ch
.GetSize()];
347 Ch
.StructToBuffer(headerBuffer
);
350 // First add CompressHeader to Buffer, then add Compress
353 buffer
.write(headerBuffer
);
354 buffer
.write(myCompress
.outputBuffer
);
357 // Buffer 4 Byte aligment
359 int size
= Ch
.GetSize() + myCompress
.outputBuffer
.length
;
361 while ((size
& 0x03) != 0) {
366 } catch (Exception e
) {
367 throw new BuildException("compress.toBuffer failed!\n");
371 Iterator sectionIter
= this.sectFileList
.iterator();
372 while (sectionIter
.hasNext()) {
373 sect
= (Section
) sectionIter
.next();
376 // The last section don't need 4 byte ffsAligment.
378 sect
.toBuffer(buffer
);
379 } catch (Exception e
) {
380 throw new BuildException(e
.getMessage());