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