]> git.proxmox.com Git - mirror_edk2.git/blobdiff - Tools/Source/FrameworkTasks/org/tianocore/framework/tasks/GenSectionTask.java
(Customized Compression)If setting the EncapsulationType="Compress", Tool will transf...
[mirror_edk2.git] / Tools / Source / FrameworkTasks / org / tianocore / framework / tasks / GenSectionTask.java
index 4d335e0ba6c706144cfdfd87c7e2f5c63adb4de0..8396f707ab3bc2772007b17c9b6e75e0ae4e9f3f 100644 (file)
  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
  \r
-**/\r
+ **/\r
 \r
 package org.tianocore.framework.tasks;\r
 \r
 import java.io.ByteArrayOutputStream;\r
 import java.io.DataOutputStream;\r
-import java.io.File;\r
 import java.util.ArrayList;\r
 import java.util.Iterator;\r
 import java.util.List;\r
@@ -31,40 +30,51 @@ import org.apache.tools.ant.taskdefs.LogStreamHandler;
 import org.apache.tools.ant.types.Commandline;\r
 import org.tianocore.common.logger.EdkLog;\r
 \r
-public class GenSectionTask extends Task implements EfiDefine, Section, FfsTypes {\r
+public class GenSectionTask extends Task implements EfiDefine, Section,\r
+        FfsTypes {\r
     //\r
     // inputfile name\r
     //\r
     private FileArg inputFile = new FileArg();\r
+\r
     //\r
     // outputfile name\r
     //\r
     private FileArg outputFile = new FileArg();\r
+\r
     //\r
     // section type\r
     //\r
     private ToolArg sectionType = new ToolArg();\r
+\r
     //\r
     // version number\r
     //\r
     private ToolArg versionNum = new ToolArg();\r
+\r
     //\r
     // interface string\r
     //\r
     private ToolArg interfaceString = new ToolArg();\r
+\r
     //\r
     // Section file list\r
     //\r
     private List<Section> sectFileList = new ArrayList<Section>();\r
-   \r
+\r
+    //\r
+    // flag indicated the <tool> element\r
+    //\r
+    private boolean haveTool = false;\r
+\r
     /**\r
-      execute\r
-      \r
-      GenSectionTaks execute is to assemble tool command line & execute tool\r
-      command line.\r
-      \r
-      @throws BuildException\r
-    **/\r
+     * execute\r
+     * \r
+     * GenSectionTaks execute is to assemble tool command line & execute tool\r
+     * command line.\r
+     * \r
+     * @throws BuildException\r
+     */\r
     public void execute() throws BuildException {\r
         String command;\r
         Project project = this.getOwningTarget().getProject();\r
@@ -80,7 +90,8 @@ public class GenSectionTask extends Task implements EfiDefine, Section, FfsTypes
         //\r
         // argument of tools\r
         //\r
-        String argument = "" + inputFile + outputFile + sectionType + versionNum + interfaceString;\r
+        String argument = "" + inputFile + outputFile + sectionType\r
+                + versionNum + interfaceString;\r
         //\r
         // return value of gensection execution\r
         //\r
@@ -98,8 +109,10 @@ public class GenSectionTask extends Task implements EfiDefine, Section, FfsTypes
             runner.setAntRun(project);\r
             runner.setCommandline(cmdline.getCommandline());\r
 \r
-            EdkLog.log(this, inputFile.toFileList() + " => " + outputFile.toFileList());\r
-            EdkLog.log(this, EdkLog.EDK_VERBOSE, Commandline.toString(cmdline.getCommandline()));\r
+            EdkLog.log(this, inputFile.toFileList() + " => "\r
+                    + outputFile.toFileList());\r
+            EdkLog.log(this, EdkLog.EDK_VERBOSE, Commandline.toString(cmdline\r
+                    .getCommandline()));\r
 \r
             revl = runner.execute();\r
             if (EFI_SUCCESS == revl) {\r
@@ -108,7 +121,8 @@ public class GenSectionTask extends Task implements EfiDefine, Section, FfsTypes
                 //\r
                 // command execution fail\r
                 //\r
-                EdkLog.log(this, EdkLog.EDK_INFO, "ERROR = " + Integer.toHexString(revl));\r
+                EdkLog.log(this, EdkLog.EDK_INFO, "ERROR = "\r
+                        + Integer.toHexString(revl));\r
                 throw new BuildException("GenSection failed!");\r
             }\r
         } catch (Exception e) {\r
@@ -117,252 +131,264 @@ public class GenSectionTask extends Task implements EfiDefine, Section, FfsTypes
     }\r
 \r
     /**\r
-      getInputFile\r
-      \r
-      This function is to get class member "inputFile".\r
-      \r
-      @return                    name of input file\r
-    **/\r
+     * getInputFile\r
+     * \r
+     * This function is to get class member "inputFile".\r
+     * \r
+     * @return name of input file\r
+     */\r
     public String getInputFile() {\r
         return this.inputFile.getValue();\r
     }\r
 \r
     /**\r
-      setInputFile\r
-      \r
-      This function is to set class member "inputFile".\r
-      \r
-      @param inputFile            name of input file\r
-    **/\r
+     * setInputFile\r
+     * \r
+     * This function is to set class member "inputFile".\r
+     * \r
+     * @param inputFile\r
+     *            name of input file\r
+     */\r
     public void setInputFile(String inputFile) {\r
         this.inputFile.setArg(" -i ", inputFile);\r
     }\r
 \r
     /**\r
-      getOutputFile\r
-      \r
-      This function is to get class member "outputFile".\r
-      \r
-      @return                      name of output file\r
-    **/\r
+     * getOutputFile\r
+     * \r
+     * This function is to get class member "outputFile".\r
+     * \r
+     * @return name of output file\r
+     */\r
     public String getOutputFile() {\r
         return this.outputFile.getValue();\r
     }\r
 \r
     /**\r
-      setOutputfile\r
-      \r
-      This function is to set class member "outputFile".\r
-      @param  outputFile            name of output file\r
-    **/\r
+     * setOutputfile\r
+     * \r
+     * This function is to set class member "outputFile".\r
+     * \r
+     * @param outputFile\r
+     *            name of output file\r
+     */\r
     public void setOutputfile(String outputFile) {\r
         this.outputFile.setArg(" -o ", outputFile);\r
     }\r
 \r
     /**\r
-      getSectionType\r
-      \r
-      This function is to get class member "sectionType".\r
-      \r
-      @return                        sectoin type\r
-    **/\r
+     * getSectionType\r
+     * \r
+     * This function is to get class member "sectionType".\r
+     * \r
+     * @return sectoin type\r
+     */\r
     public String getSectionType() {\r
         return this.sectionType.getValue();\r
     }\r
 \r
     /**\r
-      setSectionType\r
-      \r
-      This function is to set class member "sectionType".\r
-      \r
-      @param sectionType              section type\r
-    **/\r
+     * setSectionType\r
+     * \r
+     * This function is to set class member "sectionType".\r
+     * \r
+     * @param sectionType\r
+     *            section type\r
+     */\r
     public void setSectionType(String sectionType) {\r
         this.sectionType.setArg(" -s ", sectionType);\r
     }\r
 \r
     /**\r
-      getVersionNum\r
-      \r
-      This function is to get class member "versionNum".\r
-      @return                         version number\r
-    **/\r
+     * getVersionNum\r
+     * \r
+     * This function is to get class member "versionNum".\r
+     * \r
+     * @return version number\r
+     */\r
     public String getVersionNum() {\r
         return this.versionNum.getValue();\r
     }\r
 \r
     /**\r
-      setVersionNume\r
-      \r
-      This function is to set class member "versionNum".\r
-      @param versionNum               version number\r
-    **/\r
+     * setVersionNume\r
+     * \r
+     * This function is to set class member "versionNum".\r
+     * \r
+     * @param versionNum\r
+     *            version number\r
+     */\r
     public void setVersionNum(String versionNum) {\r
         this.versionNum.setArg(" -v ", versionNum);\r
     }\r
 \r
     /**\r
-      getInterfaceString\r
-      \r
-      This function is to get class member "interfaceString".\r
-      @return                         interface string\r
-    **/\r
+     * getInterfaceString\r
+     * \r
+     * This function is to get class member "interfaceString".\r
+     * \r
+     * @return interface string\r
+     */\r
     public String getInterfaceString() {\r
         return this.interfaceString.getValue();\r
     }\r
 \r
     /**\r
-      setInterfaceString\r
-      \r
-      This funcion is to set class member "interfaceString".\r
-      @param interfaceString            interface string\r
-    **/\r
+     * setInterfaceString\r
+     * \r
+     * This funcion is to set class member "interfaceString".\r
+     * \r
+     * @param interfaceString\r
+     *            interface string\r
+     */\r
     public void setInterfaceString(String interfaceString) {\r
         this.interfaceString.setArg(" -a ", "\"" + interfaceString + "\"");\r
     }\r
-    \r
+\r
     /**\r
-      addSectFile\r
-      \r
-      This function is to add sectFile to list.\r
-      \r
-      @param sectFile     instance of sectFile.\r
-    **/\r
-    public void addSectFile(SectFile sectFile){\r
+     * addSectFile\r
+     * \r
+     * This function is to add sectFile to list.\r
+     * \r
+     * @param sectFile\r
+     *            instance of sectFile.\r
+     */\r
+    public void addSectFile(SectFile sectFile) {\r
         this.sectFileList.add(sectFile);\r
     }\r
 \r
     /**\r
-      setTool\r
-      \r
-      This function is to set the class member "Tool";\r
-      \r
-      @param tool \r
-    **/\r
+     * setTool\r
+     * \r
+     * This function is to set the class member "Tool";\r
+     * \r
+     * @param tool\r
+     */\r
     public void addTool(Tool tool) {\r
         this.sectFileList.add(tool);\r
+        this.haveTool = true;\r
     }\r
-    \r
+\r
     /**\r
-      addGenSection\r
-      \r
-      This function is to add GenSectin element to list\r
-      @param task         Instance of genSection\r
-    **/\r
-    public void addGenSection(GenSectionTask task){\r
+     * addGenSection\r
+     * \r
+     * This function is to add GenSectin element to list\r
+     * \r
+     * @param task\r
+     *            Instance of genSection\r
+     */\r
+    public void addGenSection(GenSectionTask task) {\r
         this.sectFileList.add(task);\r
     }\r
-    \r
-    public void toBuffer(DataOutputStream buffer){\r
+\r
+    public void toBuffer(DataOutputStream buffer) {\r
         //\r
-        //  Search SectionList find earch section and call it's \r
-        //  ToBuffer function.\r
+        // Search SectionList find earch section and call it's\r
+        // ToBuffer function.\r
         //\r
-        if (this.sectionType.getValue().equalsIgnoreCase("EFI_SECTION_COMPRESSION")){\r
-            Section    sect;\r
-            \r
+        if (this.sectionType.getValue().equalsIgnoreCase(\r
+                "EFI_SECTION_COMPRESSION")\r
+                && !this.haveTool) {\r
+            Section sect;\r
+\r
             //\r
-            //  Get section file in compress node.\r
+            // Get section file in compress node.\r
             //\r
-            try{\r
-                ByteArrayOutputStream bo = new ByteArrayOutputStream ();\r
-                DataOutputStream Do = new DataOutputStream (bo);\r
-                \r
+            try {\r
+                ByteArrayOutputStream bo = new ByteArrayOutputStream();\r
+                DataOutputStream Do = new DataOutputStream(bo);\r
+\r
                 //\r
-                //  Get each section which under the compress {};\r
-                //  And add it is contains to File;\r
+                // Get each section which under the compress {};\r
+                // And add it is contains to File;\r
                 //\r
                 Iterator SectionIter = this.sectFileList.iterator();\r
-                while (SectionIter.hasNext()){\r
-                    sect = (Section)SectionIter.next();\r
-                    \r
+                while (SectionIter.hasNext()) {\r
+                    sect = (Section) SectionIter.next();\r
+\r
                     //\r
-                    //  Call each section class's toBuffer function.\r
+                    // Call each section class's toBuffer function.\r
                     //\r
                     try {\r
                         sect.toBuffer(Do);\r
-                    }\r
-                    catch (BuildException e) {\r
+                    } catch (BuildException e) {\r
                         System.out.print(e.getMessage());\r
-                        throw new BuildException ("Compress.toBuffer failed at section");\r
-                    }    \r
-                                \r
+                        throw new BuildException(\r
+                                "Compress.toBuffer failed at section");\r
+                    }\r
+\r
                 }\r
-                Do.close();    \r
-                \r
+                Do.close();\r
+\r
                 //\r
-                //  Call compress\r
+                // Call compress\r
                 //\r
                 byte[] fileBuffer = bo.toByteArray();\r
-                \r
+\r
                 synchronized (CompressSection.semaphore) {\r
-                Compress myCompress = new Compress(fileBuffer, fileBuffer.length);            \r
-                \r
-                //\r
-                //  Add Compress header\r
-                //\r
-                CompressHeader Ch        = new CompressHeader();\r
-                Ch.SectionHeader.Size[0] = (byte)((myCompress.outputBuffer.length +\r
-                                                   Ch.GetSize()) &\r
-                                                   0xff\r
-                                                  );\r
-                Ch.SectionHeader.Size[1] = (byte)(((myCompress.outputBuffer.length + \r
-                                                    Ch.GetSize())&\r
-                                                   0xff00) >> 8\r
-                                                  );\r
-                Ch.SectionHeader.Size[2] = (byte)(((myCompress.outputBuffer.length + \r
-                                                    Ch.GetSize()) & \r
-                                                   0xff0000) >> 16\r
-                                                 );\r
-                Ch.SectionHeader.type    = (byte) EFI_SECTION_COMPRESSION;\r
-                \r
-                //\r
-                //  Note: The compressName was not efsfective now. Using the\r
-                //  EFI_STANDARD_COMPRSSION for compressType .\r
-                //  That is follow old Genffsfile tools. Some code will be added for \r
-                //  the different compressName;\r
-                //\r
-                Ch.UncompressLen         = fileBuffer.length;\r
-                Ch.CompressType          = EFI_STANDARD_COMPRESSION; \r
-                \r
-                //\r
-                //  Change header struct to byte buffer\r
-                //\r
-                byte [] headerBuffer = new byte[Ch.GetSize()];\r
-                Ch.StructToBuffer(headerBuffer);\r
-                \r
-                //\r
-                //  First add CompressHeader to Buffer, then add Compress data.\r
-                //\r
-                buffer.write (headerBuffer);\r
-                buffer.write(myCompress.outputBuffer);            \r
-                    \r
-                //\r
-                //  Buffer 4 Byte aligment \r
-                //\r
-                int size = Ch.GetSize() + myCompress.outputBuffer.length;\r
-                \r
-                while ((size & 0x03) != 0){\r
-                    size ++;\r
-                    buffer.writeByte(0);\r
-                }\r
+                    Compress myCompress = new Compress(fileBuffer,\r
+                            fileBuffer.length);\r
+\r
+                    //\r
+                    // Add Compress header\r
+                    //\r
+                    CompressHeader Ch = new CompressHeader();\r
+                    Ch.SectionHeader.Size[0] = (byte) ((myCompress.outputBuffer.length + Ch\r
+                            .GetSize()) & 0xff);\r
+                    Ch.SectionHeader.Size[1] = (byte) (((myCompress.outputBuffer.length + Ch\r
+                            .GetSize()) & 0xff00) >> 8);\r
+                    Ch.SectionHeader.Size[2] = (byte) (((myCompress.outputBuffer.length + Ch\r
+                            .GetSize()) & 0xff0000) >> 16);\r
+                    Ch.SectionHeader.type = (byte) EFI_SECTION_COMPRESSION;\r
+\r
+                    //\r
+                    // Note: The compressName was not efsfective now. Using the\r
+                    // EFI_STANDARD_COMPRSSION for compressType .\r
+                    // That is follow old Genffsfile tools. Some code will be\r
+                    // added for\r
+                    // the different compressName;\r
+                    //\r
+                    Ch.UncompressLen = fileBuffer.length;\r
+                    Ch.CompressType = EFI_STANDARD_COMPRESSION;\r
+\r
+                    //\r
+                    // Change header struct to byte buffer\r
+                    //\r
+                    byte[] headerBuffer = new byte[Ch.GetSize()];\r
+                    Ch.StructToBuffer(headerBuffer);\r
+\r
+                    //\r
+                    // First add CompressHeader to Buffer, then add Compress\r
+                    // data.\r
+                    //\r
+                    buffer.write(headerBuffer);\r
+                    buffer.write(myCompress.outputBuffer);\r
+\r
+                    //\r
+                    // Buffer 4 Byte aligment\r
+                    //\r
+                    int size = Ch.GetSize() + myCompress.outputBuffer.length;\r
+\r
+                    while ((size & 0x03) != 0) {\r
+                        size++;\r
+                        buffer.writeByte(0);\r
+                    }\r
                 }\r
-            }\r
-            catch (Exception e){\r
+            } catch (Exception e) {\r
                 throw new BuildException("compress.toBuffer failed!\n");\r
-            }    \r
+            }\r
         } else {\r
             Section sect;\r
             Iterator sectionIter = this.sectFileList.iterator();\r
             while (sectionIter.hasNext()) {\r
-                sect = (Section)sectionIter.next(); \r
+                sect = (Section) sectionIter.next();\r
                 try {\r
                     //\r
-                    //  The last section don't need 4 byte ffsAligment.\r
+                    // The last section don't need 4 byte ffsAligment.\r
                     //\r
                     sect.toBuffer(buffer);\r
                 } catch (Exception e) {\r
-                    throw new BuildException (e.getMessage());\r
+                    throw new BuildException(e.getMessage());\r
                 }\r
             }\r
         }\r