]> git.proxmox.com Git - mirror_edk2.git/blobdiff - Tools/Source/FrameworkTasks/org/tianocore/framework/tasks/GenSectionTask.java
Support calling customized compression tool in FrameworkTask.
[mirror_edk2.git] / Tools / Source / FrameworkTasks / org / tianocore / framework / tasks / GenSectionTask.java
index a631d93de506bda10c736768620f947306314dbd..bb477f031e2f518302b2578e764becc015f08a5c 100644 (file)
 \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
 \r
 import org.apache.tools.ant.BuildException;\r
 import org.apache.tools.ant.Project;\r
@@ -24,8 +29,9 @@ import org.apache.tools.ant.Task;
 import org.apache.tools.ant.taskdefs.Execute;\r
 import org.apache.tools.ant.taskdefs.LogStreamHandler;\r
 import org.apache.tools.ant.types.Commandline;\r
+import org.tianocore.common.logger.EdkLog;\r
 \r
-public class GenSectionTask extends Task implements EfiDefine {\r
+public class GenSectionTask extends Task implements EfiDefine, Section,FfsTypes {\r
     ///\r
     /// inputfile name\r
     ///\r
@@ -50,7 +56,11 @@ public class GenSectionTask extends Task implements EfiDefine {
     /// interface string\r
     ///\r
     private String interfaceString = "";\r
-\r
+    ///\r
+    /// Section file list\r
+    ///\r
+    private List<Section> sectFileList = new ArrayList<Section>();\r
+   \r
     /**\r
       execute\r
       \r
@@ -74,7 +84,7 @@ public class GenSectionTask extends Task implements EfiDefine {
         //\r
         // argument of tools\r
         //\r
-        String argument = inputFile + outputFile + sectionType + versionNum\r
+        String argument = inputFile + outputFile + " -s "+ sectionType + versionNum\r
                 + interfaceString;\r
         //\r
         // return value of gensection execution\r
@@ -93,8 +103,8 @@ public class GenSectionTask extends Task implements EfiDefine {
             runner.setAntRun(project);\r
             runner.setCommandline(cmdline.getCommandline());\r
 \r
-            log(inputFileName);\r
-            log(Commandline.toString(cmdline.getCommandline()), Project.MSG_VERBOSE);\r
+            EdkLog.log(this, EdkLog.EDK_INFO, inputFileName);\r
+            EdkLog.log(this, EdkLog.EDK_DEBUG, Commandline.toString(cmdline.getCommandline()));\r
             revl = runner.execute();\r
             if (EFI_SUCCESS == revl) {\r
                 log("GenSection succeeded!", Project.MSG_VERBOSE);\r
@@ -173,7 +183,7 @@ public class GenSectionTask extends Task implements EfiDefine {
       @param sectionType              section type\r
     **/\r
     public void setSectionType(String sectionType) {\r
-        this.sectionType = " -s " + sectionType;\r
+        this.sectionType = sectionType;\r
     }\r
 \r
     /**\r
@@ -215,4 +225,148 @@ public class GenSectionTask extends Task implements EfiDefine {
     public void setInterfaceString(String interfaceString) {\r
         this.interfaceString = " -a " + "\"" + interfaceString + "\"";\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
+        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
+    public void addTool(Tool tool) {\r
+        this.sectFileList.add(tool);\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
+        this.sectFileList.add(task);\r
+    }\r
+    \r
+    public void toBuffer(DataOutputStream buffer){\r
+        //\r
+        //  Search SectionList find earch section and call it's \r
+        //  ToBuffer function.\r
+        //\r
+        if (this.sectionType.equalsIgnoreCase("EFI_SECTION_COMPRESSION")){\r
+            Section    sect;\r
+            \r
+            //\r
+            //  Get section file in compress node.\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
+                //\r
+                Iterator SectionIter = this.sectFileList.iterator();\r
+                while (SectionIter.hasNext()){\r
+                    sect = (Section)SectionIter.next();\r
+                    \r
+                    //\r
+                    //  Call each section class's toBuffer function.\r
+                    //\r
+                    try {\r
+                        sect.toBuffer(Do);\r
+                    }\r
+                    catch (BuildException e) {\r
+                        System.out.print(e.getMessage());\r
+                        throw new BuildException ("Compress.toBuffer failed at section");\r
+                    }    \r
+                                \r
+                }\r
+                Do.close();    \r
+                \r
+                //\r
+                //  Call compress\r
+                //\r
+                byte[] fileBuffer = bo.toByteArray();\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
+            }\r
+            catch (Exception e){\r
+                throw new BuildException("compress.toBuffer failed!\n");\r
+            }    \r
+        } else {\r
+            Section sect;\r
+            Iterator sectionIter = this.sectFileList.iterator();\r
+            while (sectionIter.hasNext()) {\r
+                sect = (Section)sectionIter.next(); \r
+                try {\r
+                    //\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
+                }\r
+            }\r
+        }\r
+    }\r
 }\r