--- /dev/null
+/** @file\r
+ GenFfsFileTask class.\r
+\r
+ GenFfsFileTaks is to generate ffs file.\r
+ \r
+ Copyright (c) 2006, Intel Corporation\r
+ All rights reserved. This program and the accompanying materials\r
+ are licensed and made available under the terms and conditions of the BSD License\r
+ which accompanies this distribution. The full text of the license may be found at\r
+ http://opensource.org/licenses/bsd-license.php\r
+ \r
+ 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
+package org.tianocore.framework.tasks;\r
+\r
+import java.io.DataInputStream;\r
+import java.io.DataOutputStream;\r
+import java.io.File;\r
+import java.io.FileInputStream;\r
+import java.io.FileOutputStream;\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
+import org.apache.tools.ant.Task;\r
+import org.tianocore.common.logger.EdkLog;\r
+\r
+/**\r
+ GenFfsFileTask\r
+ \r
+ GenFfsFileTaks is to generate ffs file.\r
+\r
+**/\r
+public class GenFfsFileTask extends Task implements EfiDefine, FfsTypes {\r
+ /**\r
+ * GenFfsFile Task Class\r
+ * class member \r
+ * -baseName : module baseName\r
+ * -ffsFileGuid : module Guid.\r
+ * -ffsFileType : Ffs file type. \r
+ * -ffsAttributeRecovery : The file is required for recovery.\r
+ * -ffsAligment : The file data alignment (0 if none required). See FFS \r
+ * specification for supported alignments (0-7 are only possible \r
+ * values). *\r
+ * -ffsAttributeCheckSum : The file data is checksummed. If this is FALSE a \r
+ * value of 0x5A will be inserted in the file \r
+ * checksum field of the file header. *\r
+ * -sectFileDir : specifies the full path to the component build directory.\r
+ * Required.\r
+ * -ffsAttrib : Data recorde attribute added result.\r
+ * -sectionList : List recorded all section elemet in task.\r
+ */\r
+ ///\r
+ /// module baseName\r
+ ///\r
+ String baseName = "";\r
+ ///\r
+ /// \r
+ /// \r
+ String moduleType;\r
+ ///\r
+ /// module Guid\r
+ ///\r
+ String ffsFileGuid = "";\r
+ ///\r
+ /// Ffs file type\r
+ ///\r
+ String ffsFileType = "";\r
+ ///\r
+ /// ffsAttribHeaderExtension value is used to set the corresponding bit in \r
+ /// the output FFS file header \r
+ ///\r
+ boolean ffsAttribHeaderExtension = false;\r
+ ///\r
+ /// ffsAttribTailPresent value is used to set the corresponding bit in the \r
+ /// output FFS file header\r
+ ///\r
+ boolean ffsAttribTailPresent = false;\r
+ ///\r
+ /// ffsAttribRecovery value is used to set the corresponding bit in the \r
+ /// output FFS file header\r
+ ///\r
+ boolean ffsAttribRecovery = false;\r
+ ///\r
+ /// ffsAttribDataAlignment value is used to set the corresponding bit in the output \r
+ /// FFS file header.The specified FFS alignment must be a value between 0 \r
+ /// and 7 inclusive\r
+ ///\r
+ int ffsAttribDataAlignment = 0;\r
+ ///\r
+ /// ffsAttribChecksum value is used to set the corresponding bit in the \r
+ /// output FFS file header\r
+ ///\r
+ boolean FfsAttribChecksum = false;\r
+ ///\r
+ /// Attribute is used to record the sum of all bit in the output FFS file.\r
+ ///\r
+ byte attributes = 0;\r
+ ///\r
+ /// The output directory of ffs file.\r
+ ///\r
+ String outputDir = "";\r
+ ///\r
+ /// List of section.\r
+ ///\r
+ List<Object> sectionList = new ArrayList<Object>();\r
+\r
+ ///\r
+ /// The path of Framewor_Tools_Paht.\r
+ ///\r
+ static String path = ""; \r
+ \r
+ ///\r
+ /// Gensection\r
+ ///\r
+ List<GenSectionTask> genSectList = new ArrayList<GenSectionTask>();\r
+ \r
+ /**\r
+ execute\r
+ \r
+ GenFfsFileTask execute is to generate ffs file according to input section \r
+ dscriptive information.\r
+ **/\r
+ public void execute() throws BuildException {\r
+\r
+ String ffsSuffix = "";\r
+ String outputPath = "";\r
+ \r
+ //\r
+ // Get Fraemwork_Tools_Path\r
+ //\r
+ Project pj = this.getOwningTarget().getProject();\r
+ path = pj.getProperty("env.FRAMEWORK_TOOLS_PATH");\r
+\r
+ //\r
+ // Check does the BaseName, Guid, FileType set value.\r
+ //\r
+ if (this.baseName.equals("")) {\r
+ throw new BuildException ("Must set OutputFileBasename!\n");\r
+ }\r
+\r
+ if (this.ffsFileGuid.equals("")) {\r
+ throw new BuildException ("Must set ffsFileGuid!\n");\r
+ }\r
+\r
+ if (this.ffsFileType.equals("")) {\r
+ throw new BuildException ("Must set ffsFileType!\n");\r
+ }\r
+ //\r
+ // Create ffs file. File name = FfsFileGuid + BaseName + ffsSuffix.\r
+ // If outputDir's value was set, file will output to the outputDir.\r
+ //\r
+ ffsSuffix = TypeToSuffix (this.moduleType);\r
+ if (!this.outputDir.equals("")) {\r
+ String temp;\r
+ outputPath = this.outputDir;\r
+ temp = outputPath.replace('\\', File.separatorChar);\r
+ outputPath = temp.replace('/', File.separatorChar);\r
+ if (outputPath.charAt(outputPath.length()-1) != File.separatorChar) {\r
+ outputPath = outputPath + File.separator;\r
+ }\r
+\r
+ }\r
+\r
+ String ffsFilePath = outputPath + this.ffsFileGuid + '-' + this.baseName + ffsSuffix;\r
+ File ffsFile = new File (ffsFilePath);\r
+ try{\r
+ genFfs(ffsFile);\r
+ }catch (BuildException e){\r
+ if (ffsFile != null && ffsFile.exists()){\r
+ ffsFile.deleteOnExit();\r
+ }\r
+ throw new BuildException(e.getMessage());\r
+ \r
+ }\r
+ \r
+ } \r
+\r
+ /**\r
+ addCompress\r
+ \r
+ This function is to add compress section to section list.\r
+ @param compress Section of compress \r
+ **/\r
+ public void addCompress(CompressSection compress) {\r
+ this.sectionList.add(compress);\r
+ }\r
+\r
+ /**\r
+ addTool\r
+ \r
+ This function is to add tool section to section list.\r
+ @param tool Section of tool\r
+ **/\r
+ public void addTool(Tool tool) {\r
+ this.sectionList.add(tool);\r
+ }\r
+\r
+ /**\r
+ addSectionFile\r
+ \r
+ This function is to add sectFile section to section list.\r
+ @param sectFile Section of sectFile.\r
+ **/\r
+ public void addSectFile (SectFile sectFile) {\r
+ this.sectionList.add(sectFile); \r
+ }\r
+\r
+ /**\r
+ getBaseName\r
+ \r
+ This function is to get basename\r
+ \r
+ @return String of base name\r
+ **/\r
+ public String getBaseName() {\r
+ return this.baseName;\r
+ }\r
+\r
+ /**\r
+ setBaseName\r
+ \r
+ This function is to set base name.\r
+ @param baseName\r
+ **/\r
+ public void setBaseName(String baseName) {\r
+ this.baseName = baseName.trim();\r
+ }\r
+\r
+ /**\r
+ getFfsAligment\r
+ \r
+ This function is to get the ffsAligment\r
+ @return The value of ffsAligment.\r
+ **/\r
+ public int getFfsAttribDataAlignment() {\r
+ return this.ffsAttribDataAlignment;\r
+ }\r
+\r
+ /**\r
+ setFfsAligment\r
+ \r
+ This function is to set ffsAligment \r
+ @param ffsAligment The value of ffsAligment.\r
+ **/\r
+ public void setFfsAttribDataAlignment(String ffsAligment) {\r
+ this.ffsAttribDataAlignment = stringToInt(ffsAligment.replaceAll(" ", "").toLowerCase());\r
+ if (this.ffsAttribDataAlignment < 0 || this.ffsAttribDataAlignment > 7) {\r
+ throw new BuildException ("FFS_ATTRIB_DATA_ALIGMENT must be an integer value from 0 through 7, inclusive");\r
+ } else {\r
+ attributes |= (((byte)this.ffsAttribDataAlignment) << 3);\r
+ }\r
+ }\r
+\r
+ /**\r
+ getFfsAttribCheckSum\r
+ \r
+ This function is to get ffsAttribCheckSum\r
+ \r
+ @return Value of ffsAttribChecksum \r
+ **/\r
+ public boolean getFfsAttribChecksum() {\r
+ return this.FfsAttribChecksum;\r
+ }\r
+\r
+ /**\r
+ setFfsAttribChecksum\r
+ \r
+ This function is to set ffsAttribChecksum\r
+ @param ffsAttributeCheckSum Value of ffsAttribCheckSum\r
+ **/\r
+ public void setFfsAttribChecksum(boolean ffsAttributeCheckSum) {\r
+ this.FfsAttribChecksum = ffsAttributeCheckSum;\r
+ if (ffsAttributeCheckSum) {\r
+ attributes |= FFS_ATTRIB_CHECKSUM;\r
+ }\r
+ }\r
+\r
+ /**\r
+ getFfsAttribRecovery\r
+ \r
+ This function is to get ffsAttribRecovery\r
+ @return Value of ffsAttribRecovery\r
+ **/\r
+ public boolean getFfsAttribRecovery() {\r
+ return this.ffsAttribRecovery;\r
+ }\r
+\r
+ /**\r
+ setRecovery\r
+ \r
+ This function is to set ffsAttributeRecovery\r
+ \r
+ @param ffsAttributeRecovery Value of ffsAttributeRecovery\r
+ **/\r
+ public void setRecovery(boolean ffsAttributeRecovery) {\r
+ this.ffsAttribRecovery = ffsAttributeRecovery;\r
+ if (ffsAttributeRecovery) {\r
+ attributes |= FFS_ATTRIB_RECOVERY;\r
+ }\r
+ }\r
+\r
+ /**\r
+ getFileGuid\r
+ \r
+ This function is to get fileGuid\r
+ @return Guid\r
+ **/\r
+ public String getFileGuid() {\r
+ return this.ffsFileGuid;\r
+ }\r
+\r
+ /**\r
+ setFileGuid\r
+ \r
+ This function is to set fileGuid\r
+ @param ffsFileGuid String of GUID\r
+ **/\r
+ public void setFileGuid(String ffsFileGuid) {\r
+ this.ffsFileGuid = ffsFileGuid.trim();\r
+ }\r
+\r
+ /**\r
+ getFfsFileType\r
+ \r
+ This function is to get ffsFileType.\r
+ \r
+ @return value of ffsFileType\r
+ **/\r
+ public String getFfsFileType() {\r
+ return this.ffsFileType;\r
+ }\r
+\r
+ /**\r
+ setFfsFileType\r
+ \r
+ This function is to set ffsFileType.\r
+ \r
+ @param ffsFileType \r
+ **/\r
+ public void setFfsFileType(String ffsFileType) {\r
+ this.ffsFileType = ffsFileType.trim();\r
+ }\r
+\r
+ /**\r
+ ffsAttribHeaderExtension\r
+ \r
+ This function is to get ffsAttribHeaderExtension\r
+ \r
+ @return Value of ffsAttribHeaderExtension\r
+ **/\r
+ public boolean isFfsAttribHeaderExtension() {\r
+ return this.ffsAttribHeaderExtension;\r
+ }\r
+\r
+ /**\r
+ setHeaderExension\r
+ \r
+ This function is to set headerExtension\r
+ @param headerExtension Value of headerExension\r
+ **/\r
+ public void setHeaderExtension(boolean headerExtension) {\r
+ this.ffsAttribHeaderExtension = headerExtension;\r
+ if (headerExtension) {\r
+ attributes |= FFS_ATTRIB_HEADER_EXTENSION;\r
+ }\r
+ }\r
+\r
+ /**\r
+ isFfsAttribTailPresent\r
+ \r
+ This function is to get ffsAttribTailPresent value.\r
+ @return Value of ffsAttribTailPresent.\r
+ **/\r
+ public boolean isFfsAttribTailPresent() {\r
+ return this.ffsAttribTailPresent;\r
+ }\r
+\r
+ /**\r
+ setFfsAttribTailPresent\r
+ \r
+ This function is to set ffsAttribTailPresent.\r
+ @param tailPresent Value of ffsAttribTailPresent.\r
+ **/\r
+ public void setFfsAttribTailPresent(boolean tailPresent) {\r
+ this.ffsAttribTailPresent = tailPresent;\r
+ if (tailPresent) {\r
+ attributes |= FFS_ATTRIB_TAIL_PRESENT;\r
+ }\r
+ } \r
+\r
+\r
+ /**\r
+ stringToGuid\r
+ \r
+ This function is to convert string to GUID.\r
+ * @param GuidStr String of GUID.\r
+ * @param Guid GUID form.\r
+ */\r
+ private void stringToGuid (String GuidStr, FfsHeader.FfsGuid Guid){ \r
+\r
+ int i = 0;\r
+ int j = 0;\r
+ int k = 0;\r
+ char [] charArry;\r
+ String [] SplitStr;\r
+\r
+ byte[] buffer = new byte[16];\r
+ if (GuidStr.length()!=36) {\r
+ throw new BuildException ("The GUID length [" + GuidStr.length() + "] is not correct!");\r
+ }\r
+\r
+\r
+ SplitStr = GuidStr.split("-");\r
+ if (SplitStr.length != 5) {\r
+ throw new BuildException ("The GUID format is not correct!");\r
+ }\r
+\r
+\r
+\r
+ for (i= 0; i < SplitStr.length; i++) {\r
+ String str = SplitStr[i]; \r
+ charArry = str.toCharArray();\r
+\r
+ for (j =0; j < (str.toCharArray().length)/2; j++) {\r
+\r
+ buffer[k] = hexCharToByte (charArry[j*2]); \r
+ buffer[k] = (byte)( buffer[k]& 0x0f); \r
+ buffer[k] = (byte)((buffer[k]<< 4));\r
+ buffer[k] = (byte)( buffer[k]& 0xf0); \r
+ buffer[k] = (byte)( buffer[k]|hexCharToByte(charArry[j*2+1]));\r
+ k++; \r
+ }\r
+ }\r
+ Guid.bufferToStruct(buffer);\r
+ }\r
+\r
+ /**\r
+ typeToSuffix\r
+ \r
+ This function is to get suffix of ffs file according to ffsFileType.\r
+ \r
+ @param ffsFileType ffsFileType\r
+ @return The suffix of ffs file\r
+ **/\r
+ private String TypeToSuffix (String ffsFileType){\r
+ String[][] suffix = { { "BASE", ".FFS"},\r
+ { "SEC", ".SEC" }, { "PEI_CORE", ".PEI" }, \r
+ { "PEIM", ".PEI" }, { "DXE_CORE", ".DXE" },\r
+ { "DXE_DRIVER", ".DXE" }, { "DXE_RUNTIME_DRIVER", ".DXE" }, \r
+ { "DXE_SAL_DRIVER", ".DXE" }, { "DXE_SMM_DRIVER", ".DXE" }, \r
+ { "TOOL", ".FFS" }, { "UEFI_DRIVER", ".DXE" },\r
+ { "UEFI_APPLICATION", ".APP" }, { "USER_DEFINED", ".FFS" } };\r
+\r
+ for (int i = 0; i < suffix.length; i++) {\r
+ if (suffix[i][0].equalsIgnoreCase(moduleType)) {\r
+ return suffix[i][1];\r
+ }\r
+ }\r
+\r
+ return ".FFS";\r
+ }\r
+\r
+\r
+ /**\r
+ stringToType\r
+ \r
+ This function is to get ffsFileType integer value according to ffsFileType.\r
+ @param ffsFileType String value of ffsFileType\r
+ @return Integer value of ffsFileType.\r
+ **/\r
+ private byte stringToType (String ffsFileType){\r
+\r
+ if (ffsFileType.equals("EFI_FV_FILETYPE_ALL")) {\r
+ return(byte)EFI_FV_FILETYPE_ALL;\r
+ }\r
+\r
+ if (ffsFileType.equals("EFI_FV_FILETYPE_RAW")) {\r
+ return(byte)EFI_FV_FILETYPE_RAW;\r
+ }\r
+\r
+ if (ffsFileType.equals("EFI_FV_FILETYPE_FREEFORM")) {\r
+ return(byte)EFI_FV_FILETYPE_FREEFORM;\r
+ }\r
+\r
+ if (ffsFileType.equals("EFI_FV_FILETYPE_SECURITY_CORE")) {\r
+ return(byte)EFI_FV_FILETYPE_SECURITY_CORE;\r
+ }\r
+\r
+ if (ffsFileType.equals("EFI_FV_FILETYPE_PEI_CORE")) {\r
+ return(byte) EFI_FV_FILETYPE_PEI_CORE;\r
+ }\r
+\r
+ if (ffsFileType.equals("EFI_FV_FILETYPE_DXE_CORE")) {\r
+ return(byte)EFI_FV_FILETYPE_DXE_CORE;\r
+ }\r
+\r
+ if (ffsFileType.equals("EFI_FV_FILETYPE_PEIM")) {\r
+ return(byte)EFI_FV_FILETYPE_PEIM;\r
+ }\r
+\r
+ if (ffsFileType.equals("EFI_FV_FILETYPE_DRIVER")) {\r
+ return(byte) EFI_FV_FILETYPE_DRIVER;\r
+ }\r
+\r
+ if (ffsFileType.equals("EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER")) {\r
+ return(byte)EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER;\r
+ }\r
+\r
+ if (ffsFileType.equals("EFI_FV_FILETYPE_APPLICATION")) {\r
+ return(byte)EFI_FV_FILETYPE_APPLICATION;\r
+ }\r
+\r
+ if (ffsFileType.equals("EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE")) {\r
+ return(byte)EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE;\r
+ }\r
+ if (ffsFileType.equals("EFI_FV_FILETYPE_FFS_PAD")) {\r
+ return(byte) EFI_FV_FILETYPE_FFS_PAD;\r
+ }\r
+\r
+ return -1;\r
+ }\r
+\r
+\r
+\r
+ /**\r
+ calculateCheckSum8\r
+ \r
+ This function is to calculate the value needed for a valid UINT8 checksum\r
+ @param buffer Byte buffer containing byte data of component.\r
+ @param size Size of the buffer.\r
+ @return The 8 bit checksum value needed.\r
+ **/\r
+ private byte calculateChecksum8 (byte[] buffer, int size){\r
+ return(byte) (0x100 - calculateSum8 (buffer, size));\r
+ }\r
+\r
+\r
+ /**\r
+ calculateSum8\r
+ \r
+ This function is to calculate the UINT8 sum for the requested region.\r
+ @param buffer Byte buffer containing byte data of component\r
+ @param size Size of the buffer.\r
+ @return The 8 bit checksum value needed.\r
+ **/ \r
+ private short calculateSum8 (byte[] buffer, int size){\r
+ int Index;\r
+ byte Sum;\r
+ Sum = 0;\r
+\r
+ //\r
+ // Perform the word sum for buffer\r
+ //\r
+ for (Index = 0; Index < size; Index++) {\r
+ Sum = (byte) (Sum + buffer[Index]); \r
+ }\r
+\r
+ return(byte) Sum; \r
+ }\r
+\r
+ /**\r
+ hexCharToByte\r
+ \r
+ This function is to convert hex character to byte\r
+ \r
+ @param hexChar hex character\r
+ @return Byte which corresponding to the character.\r
+ **/\r
+ private byte hexCharToByte (char hexChar){\r
+ switch (hexChar) {\r
+ case '0':\r
+ return(byte)0x00;\r
+ case '1':\r
+ return(byte)0x01;\r
+ case '2':\r
+ return(byte)0x02;\r
+ case '3':\r
+ return(byte)0x03;\r
+ case '4':\r
+ return(byte)0x04;\r
+ case '5':\r
+ return(byte)0x05;\r
+ case '6':\r
+ return(byte)0x06;\r
+ case '7':\r
+ return(byte)0x07;\r
+ case '8':\r
+ return(byte)0x08;\r
+ case '9':\r
+ return(byte)0x09;\r
+ case 'a':\r
+ case 'A':\r
+ return(byte)0x0a;\r
+ case 'b':\r
+ case 'B':\r
+ return(byte)0x0b;\r
+ case 'c':\r
+ case 'C':\r
+ return(byte)0x0c;\r
+\r
+ case 'd':\r
+ case 'D': \r
+ return(byte)0x0d;\r
+\r
+ case 'e':\r
+ case 'E':\r
+ return(byte)0x0e;\r
+ case 'f':\r
+ case 'F':\r
+ return(byte)0x0f;\r
+\r
+ default:\r
+ return(byte)0xff; \r
+ }\r
+ }\r
+\r
+ /**\r
+ adjustFileSize\r
+ \r
+ This function is used to adjusts file size to insure sectioned file is exactly the right length such\r
+ that it ends on exactly the last byte of the last section. ProcessScript()\r
+ may have padded beyond the end of the last section out to a 4 byte boundary.\r
+ This padding is stripped. \r
+ \r
+ @param buffer Byte buffer contains a section stream\r
+ @return Corrected size of file.\r
+ **/\r
+ private int adjustFileSize (byte[] buffer){ \r
+\r
+ int orignalLen = buffer.length;\r
+ int adjustLen = 0;\r
+ int sectionPoint = 0;\r
+ int nextSectionPoint = 0;\r
+ int sectionLen = 0;\r
+ int totalLen = 0;\r
+ int firstSectionHeader = 0;\r
+\r
+\r
+ firstSectionHeader = buffer[0]& 0xff;\r
+ firstSectionHeader = ((buffer[1]&0xff)<<8) | firstSectionHeader;\r
+ firstSectionHeader = ((buffer[2]&0xff)<<16)| firstSectionHeader;\r
+\r
+\r
+ while (sectionPoint < buffer.length) {\r
+ sectionLen = buffer[0 + sectionPoint]& 0xff;\r
+ sectionLen = ((buffer[1 + sectionPoint]&0xff)<<8)| sectionLen;\r
+ sectionLen = ((buffer[2 + sectionPoint]&0xff)<<16)| sectionLen; \r
+ totalLen = totalLen + sectionLen;\r
+\r
+ if (totalLen == orignalLen) {\r
+ return totalLen;\r
+ }\r
+\r
+ sectionPoint = sectionPoint + sectionLen;\r
+ adjustLen = sectionPoint;\r
+\r
+ nextSectionPoint = (sectionPoint + 0x03) & (~0x03);\r
+ totalLen = totalLen + nextSectionPoint - sectionLen;\r
+ sectionPoint = nextSectionPoint;\r
+ }\r
+ return adjustLen;\r
+ }\r
+\r
+ /**\r
+ getOutputDir\r
+ \r
+ This function is to get output directory.\r
+ \r
+ @return Path of output directory.\r
+ **/\r
+ public String getOutputDir() {\r
+ return outputDir;\r
+ }\r
+\r
+ /**\r
+ setOutputDir\r
+ \r
+ This function is to set output directory.\r
+ \r
+ @param outputDir The output direcotry.\r
+ **/\r
+ public void setOutputDir(String outputDir) {\r
+ this.outputDir = outputDir;\r
+ }\r
+\r
+ /**\r
+ getModuleTyp\r
+ \r
+ This function is to get string of module type.\r
+ \r
+ @return moduleType The string of module type.\r
+ **/\r
+ public String getModuleType() {\r
+ return this.moduleType;\r
+ }\r
+\r
+ /**\r
+ setModuleType\r
+ \r
+ This function is to set moduleType.\r
+ \r
+ @param moduleType The string of module type.\r
+ **/\r
+ public void setModuleType(String moduleType) {\r
+ this.moduleType = moduleType;\r
+ }\r
+ \r
+ /**\r
+ Convert a string to a integer.\r
+ \r
+ @param intString The string representing a integer\r
+ \r
+ @retval int The value of integer represented by the\r
+ given string; -1 is returned if the format\r
+ of the string is wrong.\r
+ **/\r
+ private int stringToInt(String intString) {\r
+ int value;\r
+ int hexPrefixPos = intString.indexOf("0x");\r
+ int radix = 10;\r
+ String intStringNoPrefix;\r
+\r
+ if (hexPrefixPos >= 0) {\r
+ radix = 16;\r
+ intStringNoPrefix = intString.substring(hexPrefixPos + 2, intString.length());\r
+ } else {\r
+ intStringNoPrefix = intString;\r
+ }\r
+\r
+ try {\r
+ value = Integer.parseInt(intStringNoPrefix, radix);\r
+ } catch (NumberFormatException e) {\r
+ log("Incorrect format of int [" + intString + "]. -1 is assumed");\r
+ return -1;\r
+ }\r
+\r
+ return value;\r
+ }\r
+ \r
+ /**\r
+ genFfs\r
+ \r
+ This function is to generate FFS file.\r
+ \r
+ @param ffsFile Name of FFS file.\r
+ @param isOrg Flag to indicate generate ORG ffs file or not.\r
+ **/\r
+ private void genFfs(File ffsFile) throws BuildException {\r
+ Section sect;\r
+ int fileSize;\r
+ int fileDataSize;\r
+ FfsHeader ffsHeader = new FfsHeader(); \r
+ FfsHeader orgFfsHeader = new FfsHeader();\r
+ \r
+ EdkLog.log(this, EdkLog.EDK_INFO, ffsFile.getName());\r
+ \r
+ try {\r
+ //\r
+ // Create file output stream -- dataBuffer.\r
+ //\r
+ FileOutputStream dataFs = new FileOutputStream (ffsFile.getAbsolutePath());\r
+ DataOutputStream dataBuffer = new DataOutputStream (dataFs);\r
+ \r
+ //\r
+ // Search SectionList find earch section and call it's \r
+ // ToBuffer function.\r
+ //\r
+ Iterator sectionIter = this.sectionList.iterator();\r
+ while (sectionIter.hasNext()) {\r
+ sect = (Section)sectionIter.next(); \r
+\r
+ try {\r
+ //\r
+ // The last section don't need 4 byte ffsAligment.\r
+ //\r
+ sect.toBuffer((DataOutputStream)dataBuffer);\r
+ } catch (Exception e) {\r
+ if (dataBuffer != null){\r
+ dataBuffer.close();\r
+ }\r
+ throw new BuildException (e.getMessage());\r
+ }\r
+ }\r
+ dataBuffer.close();\r
+ } catch (Exception e) {\r
+ throw new BuildException (e.getMessage());\r
+ }\r
+\r
+ //\r
+ // Creat Ffs file header\r
+ //\r
+ try {\r
+\r
+ //\r
+ // create input stream to read file data\r
+ //\r
+ byte[] fileBuffer = new byte[(int)ffsFile.length()];\r
+ FileInputStream fi = new FileInputStream (ffsFile.getAbsolutePath());\r
+ DataInputStream di = new DataInputStream (fi);\r
+ di.read(fileBuffer);\r
+ di.close();\r
+ \r
+ //\r
+ // Add GUID to header struct\r
+ //\r
+ if (this.ffsFileGuid != null) {\r
+ stringToGuid (this.ffsFileGuid, ffsHeader.name);\r
+ }\r
+\r
+ ffsHeader.ffsAttributes = this.attributes;\r
+ if ((ffsHeader.fileType = stringToType(this.ffsFileType))== -1) {\r
+ throw new BuildException ("FFS_FILE_TYPE unknow!\n");\r
+ }\r
+ \r
+ //\r
+ // Copy ffsHeader.ffsAttribute and fileType to orgFfsHeader.ffsAttribute\r
+ // and fileType\r
+ // \r
+ orgFfsHeader.ffsAttributes = ffsHeader.ffsAttributes;\r
+ orgFfsHeader.fileType = ffsHeader.fileType;\r
+ \r
+ //\r
+ // Adjust file size. The function is used to tripe the last \r
+ // section padding of 4 binary boundary. \r
+ // \r
+ //\r
+ if (ffsHeader.fileType != EFI_FV_FILETYPE_RAW) {\r
+\r
+ fileDataSize = adjustFileSize (fileBuffer);\r
+ } else {\r
+ fileDataSize = fileBuffer.length;\r
+ }\r
+\r
+ //\r
+ // 1. add header size to file size\r
+ //\r
+ fileSize = fileDataSize + ffsHeader.getSize();\r
+\r
+ if ((ffsHeader.ffsAttributes & FFS_ATTRIB_TAIL_PRESENT) != 0) {\r
+ if (ffsHeader.fileType == EFI_FV_FILETYPE_FFS_PAD) {\r
+\r
+ throw new BuildException (\r
+ "FFS_ATTRIB_TAIL_PRESENT=TRUE is " +\r
+ "invalid for PAD files"\r
+ );\r
+ }\r
+ if (fileSize == ffsHeader.getSize()) {\r
+ throw new BuildException (\r
+ "FFS_ATTRIB_TAIL_PRESENT=TRUE is " +\r
+ "invalid for 0-length files"\r
+ ); \r
+ }\r
+ fileSize = fileSize + 2;\r
+ }\r
+\r
+ //\r
+ // 2. set file size to header struct\r
+ //\r
+ ffsHeader.ffsFileSize[0] = (byte)(fileSize & 0x00FF);\r
+ ffsHeader.ffsFileSize[1] = (byte)((fileSize & 0x00FF00)>>8);\r
+ ffsHeader.ffsFileSize[2] = (byte)(((int)fileSize & 0xFF0000)>>16);\r
+ \r
+ //\r
+ // Fill in checksums and state, these must be zero for checksumming\r
+ //\r
+ ffsHeader.integrityCheck.header = calculateChecksum8 (\r
+ ffsHeader.structToBuffer(),\r
+ ffsHeader.getSize()\r
+ );\r
+ \r
+ if ((this.attributes & FFS_ATTRIB_CHECKSUM) != 0) {\r
+ if ((this.attributes & FFS_ATTRIB_TAIL_PRESENT) != 0) {\r
+ ffsHeader.integrityCheck.file = calculateChecksum8 (\r
+ fileBuffer, \r
+ fileDataSize\r
+ );\r
+ } else {\r
+ ffsHeader.integrityCheck.file = calculateChecksum8 (\r
+ fileBuffer,\r
+ fileDataSize\r
+ );\r
+ }\r
+ } else {\r
+ ffsHeader.integrityCheck.file = FFS_FIXED_CHECKSUM;\r
+ orgFfsHeader.integrityCheck.file = FFS_FIXED_CHECKSUM;\r
+ }\r
+\r
+ //\r
+ // Set the state now. Spec says the checksum assumes the state is 0.\r
+ //\r
+ ffsHeader.ffsState = EFI_FILE_HEADER_CONSTRUCTION | \r
+ EFI_FILE_HEADER_VALID | \r
+ EFI_FILE_DATA_VALID;\r
+ \r
+ //\r
+ // create output stream to first write header data in file, then write sect data in file.\r
+ //\r
+ FileOutputStream headerFfs = new FileOutputStream (ffsFile.getAbsolutePath());\r
+ DataOutputStream ffsBuffer = new DataOutputStream (headerFfs);\r
+ \r
+ //\r
+ // Add header struct and file data to FFS file\r
+ //\r
+ ffsBuffer.write(ffsHeader.structToBuffer());\r
+ ffsBuffer.write(fileBuffer, 0, fileDataSize);\r
+ \r
+\r
+\r
+ //\r
+ // If there is a tail, then set it\r
+ //\r
+ if ((this.attributes & FFS_ATTRIB_TAIL_PRESENT) != 0) {\r
+ short tailValue ;\r
+ byte [] tailByte = new byte[2];\r
+\r
+ //\r
+ // reverse tailvalue , integritycheck.file as hight byte, and \r
+ // integritycheck.header as low byte.\r
+ //\r
+ tailValue = (short)(ffsHeader.integrityCheck.header & 0xff);\r
+ tailValue = (short)((tailValue) | ((ffsHeader.integrityCheck.file << 8) & 0xff00)); \r
+ tailValue = (short)~tailValue;\r
+\r
+ //\r
+ // Change short to byte[2]\r
+ //\r
+ tailByte[0] = (byte)(tailValue & 0xff);\r
+ tailByte[1] = (byte)((tailValue & 0xff00)>>8); \r
+ ffsBuffer.write(tailByte[0]);\r
+ ffsBuffer.write(tailByte[1]);\r
+\r
+ }\r
+\r
+ //\r
+ // close output stream. Note if don't close output stream \r
+ // the buffer can't be rewritten to file. \r
+ //\r
+ ffsBuffer.close();\r
+ } catch (Exception e) {\r
+ log("genffsfile failed!");\r
+ throw new BuildException (e.getMessage());\r
+ }\r
+\r
+ }\r
+ \r
+ /**\r
+ addGenSection\r
+ \r
+ This function is to add gensection instance to list\r
+ \r
+ @param task Instance of GenSectionTask\r
+ **/\r
+ public void addGenSection (GenSectionTask task){\r
+ this.sectionList.add(task);\r
+ }\r
+}\r