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