]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/FrameworkTasks/org/tianocore/framework/tasks/GenSectionTask.java
1) Applied ToolArg and FileArg class to represent tool arguments
[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.io.file;
23 import java.util.ArrayList;
24 import java.util.Iterator;
25 import java.util.List;
26
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;
34
35 public class GenSectionTask extends Task implements EfiDefine, Section, FfsTypes {
36 //
37 // Tool name
38 //
39 private final static String toolName = "GenSection";
40 //
41 // inputfile name
42 //
43 private FileArg inputFile = new FileArg();
44
45 //
46 // outputfile name
47 //
48 private FileArg outputFile = new FileArg();
49
50 //
51 // section type
52 //
53 private ToolArg sectionType = new ToolArg();
54
55 //
56 // version number
57 //
58 private ToolArg versionNum = new ToolArg();
59
60 //
61 // interface string
62 //
63 private ToolArg interfaceString = new ToolArg();
64
65 //
66 // Section file list
67 //
68 private List<Section> sectFileList = new ArrayList<Section>();
69
70 //
71 // flag indicated the <tool> element
72 //
73 private boolean haveTool = false;
74
75 /**
76 execute
77
78 GenSectionTaks execute is to assemble tool command line & execute tool
79 command line.
80
81 @throws BuildException
82 **/
83 public void execute() throws BuildException {
84 String command;
85 Project project = this.getOwningTarget().getProject();
86 //
87 // absolute path of efi tools
88 //
89 String path = project.getProperty("env.FRAMEWORK_TOOLS_PATH");
90 if (path == null) {
91 command = toolName;
92 } else {
93 command = path + File.separator + toolName;
94 }
95 //
96 // argument of tools
97 //
98 String argument = "" + inputFile + outputFile + sectionType + versionNum + interfaceString;
99 //
100 // return value of gensection execution
101 //
102 int revl = -1;
103
104 try {
105 Commandline cmdline = new Commandline();
106 cmdline.setExecutable(command);
107 cmdline.createArgument().setLine(argument);
108
109 LogStreamHandler streamHandler = new LogStreamHandler(this,
110 Project.MSG_INFO, Project.MSG_WARN);
111 Execute runner = new Execute(streamHandler, null);
112
113 runner.setAntRun(project);
114 runner.setCommandline(cmdline.getCommandline());
115
116 EdkLog.log(this, inputFile.toFileList() + versionNum.getValue()
117 + interfaceString.getValue() + " => " + outputFile.toFileList());
118 EdkLog.log(this, EdkLog.EDK_VERBOSE, Commandline.toString(cmdline.getCommandline()));
119
120 revl = runner.execute();
121 if (EFI_SUCCESS == revl) {
122 EdkLog.log(this, EdkLog.EDK_VERBOSE, toolName + " succeeded!");
123 } else {
124 //
125 // command execution fail
126 //
127 EdkLog.log(this, EdkLog.EDK_INFO, "ERROR = " + Integer.toHexString(revl));
128 throw new BuildException(toolName + " failed!");
129 }
130 } catch (Exception e) {
131 throw new BuildException(e.getMessage());
132 }
133 }
134
135 /**
136 getInputFile
137
138 This function is to get class member "inputFile".
139
140 @return name of input file
141 **/
142 public String getInputFile() {
143 return this.inputFile.getValue();
144 }
145
146 /**
147 setInputFile
148
149 This function is to set class member "inputFile".
150
151 @param inputFile name of input file
152 **/
153 public void setInputFile(String inputFile) {
154 this.inputFile.setArg(" -i ", inputFile);
155 }
156
157 /**
158 getOutputFile
159
160 This function is to get class member "outputFile".
161
162 @return name of output file
163 **/
164 public String getOutputFile() {
165 return this.outputFile.getValue();
166 }
167
168 /**
169 setOutputfile
170
171 This function is to set class member "outputFile".
172 @param outputFile name of output file
173 **/
174 public void setOutputfile(String outputFile) {
175 this.outputFile.setArg(" -o ", outputFile);
176 }
177
178 /**
179 getSectionType
180
181 This function is to get class member "sectionType".
182
183 @return sectoin type
184 **/
185 public String getSectionType() {
186 return this.sectionType.getValue();
187 }
188
189 /**
190 setSectionType
191
192 This function is to set class member "sectionType".
193
194 @param sectionType section type
195 **/
196 public void setSectionType(String sectionType) {
197 this.sectionType.setArg(" -s ", sectionType);
198 }
199
200 /**
201 getVersionNum
202
203 This function is to get class member "versionNum".
204 @return version number
205 **/
206 public String getVersionNum() {
207 return this.versionNum.getValue();
208 }
209
210 /**
211 setVersionNume
212
213 This function is to set class member "versionNum".
214 @param versionNum version number
215 **/
216 public void setVersionNum(String versionNum) {
217 this.versionNum.setArg(" -v ", versionNum);
218 }
219
220 /**
221 getInterfaceString
222
223 This function is to get class member "interfaceString".
224 @return interface string
225 **/
226 public String getInterfaceString() {
227 return this.interfaceString.getValue();
228 }
229
230 /**
231 setInterfaceString
232
233 This funcion is to set class member "interfaceString".
234 @param interfaceString interface string
235 **/
236 public void setInterfaceString(String interfaceString) {
237 this.interfaceString.setArg(" -a ", "\"" + interfaceString + "\"");
238 }
239
240 /**
241 addSectFile
242
243 This function is to add sectFile to list.
244
245 @param sectFile instance of sectFile.
246 **/
247 public void addSectFile(SectFile sectFile){
248 this.sectFileList.add(sectFile);
249 }
250
251 /**
252 setTool
253
254 This function is to set the class member "Tool";
255
256 @param tool
257 **/
258 public void addTool(Tool tool) {
259 this.sectFileList.add(tool);
260 this.haveTool = true;
261 }
262
263 /**
264 addGenSection
265
266 This function is to add GenSectin element to list
267 @param task Instance of genSection
268 **/
269 public void addGenSection(GenSectionTask task){
270 this.sectFileList.add(task);
271 }
272
273 public void toBuffer(DataOutputStream buffer){
274 //
275 // Search SectionList find earch section and call it's
276 // ToBuffer function.
277 //
278 if (this.sectionType.getValue().equalsIgnoreCase(
279 "EFI_SECTION_COMPRESSION")
280 && !this.haveTool) {
281 Section sect;
282
283 //
284 // Get section file in compress node.
285 //
286 try {
287 ByteArrayOutputStream bo = new ByteArrayOutputStream();
288 DataOutputStream Do = new DataOutputStream(bo);
289
290 //
291 // Get each section which under the compress {};
292 // And add it is contains to File;
293 //
294 Iterator SectionIter = this.sectFileList.iterator();
295 while (SectionIter.hasNext()) {
296 sect = (Section) SectionIter.next();
297
298 //
299 // Call each section class's toBuffer function.
300 //
301 try {
302 sect.toBuffer(Do);
303 } catch (BuildException e) {
304 System.out.print(e.getMessage());
305 throw new BuildException(
306 "Compress.toBuffer failed at section");
307 }
308
309 }
310 Do.close();
311
312 //
313 // Call compress
314 //
315 byte[] fileBuffer = bo.toByteArray();
316
317 synchronized (CompressSection.semaphore) {
318 Compress myCompress = new Compress(fileBuffer,
319 fileBuffer.length);
320
321 //
322 // Add Compress header
323 //
324 CompressHeader Ch = new CompressHeader();
325 Ch.SectionHeader.Size[0] = (byte) ((myCompress.outputBuffer.length + Ch
326 .GetSize()) & 0xff);
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;
332
333 //
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
337 // added for
338 // the different compressName;
339 //
340 Ch.UncompressLen = fileBuffer.length;
341 Ch.CompressType = EFI_STANDARD_COMPRESSION;
342
343 //
344 // Change header struct to byte buffer
345 //
346 byte[] headerBuffer = new byte[Ch.GetSize()];
347 Ch.StructToBuffer(headerBuffer);
348
349 //
350 // First add CompressHeader to Buffer, then add Compress
351 // data.
352 //
353 buffer.write(headerBuffer);
354 buffer.write(myCompress.outputBuffer);
355
356 //
357 // Buffer 4 Byte aligment
358 //
359 int size = Ch.GetSize() + myCompress.outputBuffer.length;
360
361 while ((size & 0x03) != 0) {
362 size++;
363 buffer.writeByte(0);
364 }
365 }
366 } catch (Exception e) {
367 throw new BuildException("compress.toBuffer failed!\n");
368 }
369 } else {
370 Section sect;
371 Iterator sectionIter = this.sectFileList.iterator();
372 while (sectionIter.hasNext()) {
373 sect = (Section) sectionIter.next();
374 try {
375 //
376 // The last section don't need 4 byte ffsAligment.
377 //
378 sect.toBuffer(buffer);
379 } catch (Exception e) {
380 throw new BuildException(e.getMessage());
381 }
382 }
383 }
384 }
385 }