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