X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=Tools%2FSource%2FFrameworkTasks%2Forg%2Ftianocore%2Fframework%2Ftasks%2FGenFfsFileTask.java;h=cbe20f02960aa12b715422bc26ba9c33e0be7a04;hb=76c86b3ac42df05d2116346bb95cf9462eb2729c;hp=00712d8bb6875459c9a079a6cebb0cae0a4c861c;hpb=878ddf1fc3540a715f63594ed22b6929e881afb4;p=mirror_edk2.git diff --git a/Tools/Source/FrameworkTasks/org/tianocore/framework/tasks/GenFfsFileTask.java b/Tools/Source/FrameworkTasks/org/tianocore/framework/tasks/GenFfsFileTask.java index 00712d8bb6..cbe20f0296 100644 --- a/Tools/Source/FrameworkTasks/org/tianocore/framework/tasks/GenFfsFileTask.java +++ b/Tools/Source/FrameworkTasks/org/tianocore/framework/tasks/GenFfsFileTask.java @@ -27,6 +27,7 @@ import java.util.List; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; import org.apache.tools.ant.Task; +import org.tianocore.common.logger.EdkLog; /** GenFfsFileTask @@ -58,6 +59,10 @@ public class GenFfsFileTask extends Task implements EfiDefine, FfsTypes { /// String baseName = ""; /// + /// + /// + String moduleType; + /// /// module Guid /// String ffsFileGuid = ""; @@ -81,11 +86,11 @@ public class GenFfsFileTask extends Task implements EfiDefine, FfsTypes { /// boolean ffsAttribRecovery = false; /// - /// ffsAligenment value is used to set the corresponding bit in the output + /// ffsAttribDataAlignment value is used to set the corresponding bit in the output /// FFS file header.The specified FFS alignment must be a value between 0 /// and 7 inclusive /// - int ffsAlignment = 0; + int ffsAttribDataAlignment = 0; /// /// ffsAttribChecksum value is used to set the corresponding bit in the /// output FFS file header @@ -108,33 +113,34 @@ public class GenFfsFileTask extends Task implements EfiDefine, FfsTypes { /// The path of Framewor_Tools_Paht. /// static String path = ""; - + + /// + /// Gensection + /// + List genSectList = new ArrayList(); + /** execute GenFfsFileTask execute is to generate ffs file according to input section dscriptive information. - */ + **/ public void execute() throws BuildException { - Section sect; - int fileSize; - int fileDataSize; - File ffsFile; - FfsHeader ffsHeader = new FfsHeader(); + String ffsSuffix = ""; String outputPath = ""; - + // // Get Fraemwork_Tools_Path // Project pj = this.getOwningTarget().getProject(); - path = pj.getProperty("env.Framework_Tools_Path"); + path = pj.getProperty("env.FRAMEWORK_TOOLS_PATH"); // // Check does the BaseName, Guid, FileType set value. // if (this.baseName.equals("")) { - throw new BuildException ("Must set BaseName!\n"); + throw new BuildException ("Must set OutputFileBasename!\n"); } if (this.ffsFileGuid.equals("")) { @@ -149,7 +155,7 @@ public class GenFfsFileTask extends Task implements EfiDefine, FfsTypes { // Create ffs file. File name = FfsFileGuid + BaseName + ffsSuffix. // If outputDir's value was set, file will output to the outputDir. // - ffsSuffix = TypeToSuffix (this.ffsFileType); + ffsSuffix = TypeToSuffix (this.moduleType); if (!this.outputDir.equals("")) { String temp; outputPath = this.outputDir; @@ -161,189 +167,9 @@ public class GenFfsFileTask extends Task implements EfiDefine, FfsTypes { } - ffsFile = new File (outputPath + this.ffsFileGuid + '-' + this.baseName + ffsSuffix); - System.out.print("General Ffs file: file name is:\n"); - System.out.print(outputPath + this.ffsFileGuid + '-' + this.baseName + ffsSuffix); - System.out.print("\n"); - - // - // Create file output stream -- dataBuffer. - // - try { - FileOutputStream dataFs = new FileOutputStream (ffsFile.getAbsolutePath()); - DataOutputStream dataBuffer = new DataOutputStream (dataFs); - - // - // Search SectionList find earch section and call it's - // ToBuffer function. - // - Iterator sectionIter = this.sectionList.iterator(); - while (sectionIter.hasNext()) { - sect = (Section)sectionIter.next(); - - try { - // - // The last section don't need 4 byte ffsAligment. - // - sect.toBuffer((DataOutputStream)dataBuffer); - } catch (Exception e) { - throw new BuildException (e.getMessage()); - } - } - dataBuffer.close(); - } catch (Exception e) { - throw new BuildException (e.getMessage()); - } - - // - // Creat Ffs file header - // - try { - - // - // create input stream to read file data - // - byte[] fileBuffer = new byte[(int)ffsFile.length()]; - FileInputStream fi = new FileInputStream (ffsFile.getAbsolutePath()); - DataInputStream di = new DataInputStream (fi); - di.read(fileBuffer); - di.close(); - - - // - // Add GUID to header struct - // - if (this.ffsFileGuid != null) { - stringToGuid (this.ffsFileGuid, ffsHeader.name); - } - - ffsHeader.ffsAttributes = this.attributes; - if ((ffsHeader.fileType = stringToType(this.ffsFileType))== -1) { - throw new BuildException ("FFS_FILE_TYPE unknow!\n"); - } - - - // - // Adjust file size. The function is used to tripe the last - // section padding of 4 binary boundary. - // - // - if (ffsHeader.fileType != EFI_FV_FILETYPE_RAW) { - - fileDataSize = adjustFileSize (fileBuffer); - - } else { - fileDataSize = fileBuffer.length; - } - - // - // 1. add header size to file size - // - fileSize = fileDataSize + ffsHeader.getSize(); - - if ((ffsHeader.ffsAttributes & FFS_ATTRIB_TAIL_PRESENT) != 0) { - if (ffsHeader.fileType == EFI_FV_FILETYPE_FFS_PAD) { - - throw new BuildException ( - "FFS_ATTRIB_TAIL_PRESENT=TRUE is " + - "invalid for PAD files" - ); - } - if (fileSize == ffsHeader.getSize()) { - throw new BuildException ( - "FFS_ATTRIB_TAIL_PRESENT=TRUE is " + - "invalid for 0-length files" - ); - } - fileSize = fileSize + 2; - } - - // - // 2. set file size to header struct - // - ffsHeader.ffsFileSize[0] = (byte)(fileSize & 0x00FF); - ffsHeader.ffsFileSize[1] = (byte)((fileSize & 0x00FF00)>>8); - ffsHeader.ffsFileSize[2] = (byte)(((int)fileSize & 0xFF0000)>>16); - - // - // Fill in checksums and state, these must be zero for checksumming - // - ffsHeader.integrityCheck.header = calculateChecksum8 ( - ffsHeader.structToBuffer(), - ffsHeader.getSize() - ); - - if ((this.attributes & FFS_ATTRIB_CHECKSUM) != 0) { - if ((this.attributes & FFS_ATTRIB_TAIL_PRESENT) != 0) { - ffsHeader.integrityCheck.file = calculateChecksum8 ( - fileBuffer, - fileDataSize - ); - } else { - ffsHeader.integrityCheck.file = calculateChecksum8 ( - fileBuffer, - fileDataSize - ); - } - } else { - ffsHeader.integrityCheck.file = FFS_FIXED_CHECKSUM; - } - - // - // Set the state now. Spec says the checksum assumes the state is 0. - // - ffsHeader.ffsState = EFI_FILE_HEADER_CONSTRUCTION | - EFI_FILE_HEADER_VALID | - EFI_FILE_DATA_VALID; - - - // - // create output stream to first write header data in file, then write sect data in file. - // - FileOutputStream headerFfs = new FileOutputStream (ffsFile.getAbsolutePath()); - DataOutputStream ffsBuffer = new DataOutputStream (headerFfs); - - // - // Add header struct and file data to FFS file - // - ffsBuffer.write(ffsHeader.structToBuffer()); - for (int i = 0; i< fileDataSize; i++) { - ffsBuffer.write(fileBuffer[i]); - } - - // - // If there is a tail, then set it - // - if ((this.attributes & FFS_ATTRIB_TAIL_PRESENT) != 0) { - short tailValue ; - byte [] tailByte = new byte[2]; - - // - // reverse tailvalue , integritycheck.file as hight byte, and - // integritycheck.header as low byte. - // - tailValue = (short)(ffsHeader.integrityCheck.header & 0xff); - tailValue = (short)((tailValue) | ((ffsHeader.integrityCheck.file << 8) & 0xff00)); - tailValue = (short)~tailValue; - - // - // Change short to byte[2] - // - tailByte[0] = (byte)(tailValue & 0xff); - tailByte[1] = (byte)((tailValue & 0xff00)>>8); - ffsBuffer.write(tailByte[0]); - ffsBuffer.write(tailByte[1]); - } - - // - // close output stream. Note if don't close output stream - // the buffer can't be rewritten to file. - // - ffsBuffer.close(); - System.out.print ("Successful create ffs file!\n"); - } catch (Exception e) { - throw new BuildException (e.getMessage()); - } + String ffsFilePath = outputPath + this.ffsFileGuid + '-' + this.baseName + ffsSuffix; + File ffsFile = new File (ffsFilePath); + genFfs(ffsFile); } /** @@ -403,8 +229,8 @@ public class GenFfsFileTask extends Task implements EfiDefine, FfsTypes { This function is to get the ffsAligment @return The value of ffsAligment. **/ - public int getFfsAligment() { - return this.ffsAlignment; + public int getFfsAttribDataAlignment() { + return this.ffsAttribDataAlignment; } /** @@ -413,12 +239,12 @@ public class GenFfsFileTask extends Task implements EfiDefine, FfsTypes { This function is to set ffsAligment @param ffsAligment The value of ffsAligment. **/ - public void setFfsAligment(int ffsAligment) { - this.ffsAlignment = ffsAligment; - if (this.ffsAlignment > 7) { - throw new BuildException ("FFS_ALIGMENT Scope is 0-7"); + public void setFfsAttribDataAlignment(String ffsAligment) { + this.ffsAttribDataAlignment = stringToInt(ffsAligment.replaceAll(" ", "").toLowerCase()); + if (this.ffsAttribDataAlignment < 0 || this.ffsAttribDataAlignment > 7) { + throw new BuildException ("FFS_ATTRIB_DATA_ALIGMENT must be an integer value from 0 through 7, inclusive"); } else { - attributes |= (((byte)this.ffsAlignment) << 3); + attributes |= (((byte)this.ffsAttribDataAlignment) << 3); } } @@ -577,13 +403,13 @@ public class GenFfsFileTask extends Task implements EfiDefine, FfsTypes { byte[] buffer = new byte[16]; if (GuidStr.length()!=36) { - throw new BuildException ("Guid length is not correct!"); + throw new BuildException ("The GUID length [" + GuidStr.length() + "] is not correct!"); } SplitStr = GuidStr.split("-"); if (SplitStr.length != 5) { - throw new BuildException ("Guid type is not correct!"); + throw new BuildException ("The GUID format is not correct!"); } @@ -614,43 +440,21 @@ public class GenFfsFileTask extends Task implements EfiDefine, FfsTypes { @return The suffix of ffs file **/ private String TypeToSuffix (String ffsFileType){ - if (ffsFileType.equals("EFI_FV_FILETYPE_ALL")) { - return ""; - } - if (ffsFileType.equals("EFI_FV_FILETYPE_RAW")) { - return EFI_FV_FFS_FILETYPE_STR; - } - if (ffsFileType.equals("EFI_FV_FILETYPE_FREEFORM")) { - return EFI_FV_FFS_FILETYPE_STR; - } - if (ffsFileType.equals("EFI_FV_FILETYPE_SECURITY_CORE")) { - return EFI_FV_SEC_FILETYPE_STR; - } - if (ffsFileType.equals("EFI_FV_FILETYPE_PEI_CORE")) { - return EFI_FV_PEI_FILETYPE_STR; - } - if (ffsFileType.equals("EFI_FV_FILETYPE_DXE_CORE")) { - return EFI_FV_DXE_FILETYPE_STR; - } - if (ffsFileType.equals("EFI_FV_FILETYPE_PEIM")) { - return EFI_FV_PEI_FILETYPE_STR; - } - if (ffsFileType.equals("EFI_FV_FILETYPE_DRIVER")) { - return EFI_FV_DXE_FILETYPE_STR; - } - if (ffsFileType.equals("EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER")) { - return EFI_FV_PEI_FILETYPE_STR; - } - if (ffsFileType.equals("EFI_FV_FILETYPE_APPLICATION")) { - return EFI_FV_APP_FILETYPE_STR; - } - if (ffsFileType.equals("EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE")) { - return EFI_FV_FVI_FILETYPE_STR; - } - if (ffsFileType.equals("EFI_FV_FILETYPE_FFS_PAD")) { - return EFI_FV_FFS_FILETYPE_STR; + String[][] suffix = { { "BASE", ".FFS"}, + { "SEC", ".SEC" }, { "PEI_CORE", ".PEI" }, + { "PEIM", ".PEI" }, { "DXE_CORE", ".DXE" }, + { "DXE_DRIVER", ".DXE" }, { "DXE_RUNTIME_DRIVER", ".DXE" }, + { "DXE_SAL_DRIVER", ".DXE" }, { "DXE_SMM_DRIVER", ".DXE" }, + { "TOOL", ".FFS" }, { "UEFI_DRIVER", ".DXE" }, + { "UEFI_APPLICATION", ".APP" }, { "USER_DEFINED", ".FFS" } }; + + for (int i = 0; i < suffix.length; i++) { + if (suffix[i][0].equalsIgnoreCase(moduleType)) { + return suffix[i][1]; + } } - return ""; + + return ".FFS"; } @@ -672,7 +476,7 @@ public class GenFfsFileTask extends Task implements EfiDefine, FfsTypes { } if (ffsFileType.equals("EFI_FV_FILETYPE_FREEFORM")) { - return(byte)EFI_FV_FILETYPE_SECURITY_CORE; + return(byte)EFI_FV_FILETYPE_FREEFORM; } if (ffsFileType.equals("EFI_FV_FILETYPE_SECURITY_CORE")) { @@ -875,4 +679,272 @@ public class GenFfsFileTask extends Task implements EfiDefine, FfsTypes { public void setOutputDir(String outputDir) { this.outputDir = outputDir; } + + /** + getModuleTyp + + This function is to get string of module type. + + @return moduleType The string of module type. + **/ + public String getModuleType() { + return this.moduleType; + } + + /** + setModuleType + + This function is to set moduleType. + + @param moduleType The string of module type. + **/ + public void setModuleType(String moduleType) { + this.moduleType = moduleType; + } + + /** + Convert a string to a integer. + + @param intString The string representing a integer + + @retval int The value of integer represented by the + given string; -1 is returned if the format + of the string is wrong. + **/ + private int stringToInt(String intString) { + int value; + int hexPrefixPos = intString.indexOf("0x"); + int radix = 10; + String intStringNoPrefix; + + if (hexPrefixPos >= 0) { + radix = 16; + intStringNoPrefix = intString.substring(hexPrefixPos + 2, intString.length()); + } else { + intStringNoPrefix = intString; + } + + try { + value = Integer.parseInt(intStringNoPrefix, radix); + } catch (NumberFormatException e) { + log("Incorrect format of int [" + intString + "]. -1 is assumed"); + return -1; + } + + return value; + } + + /** + genFfs + + This function is to generate FFS file. + + @param ffsFile Name of FFS file. + @param isOrg Flag to indicate generate ORG ffs file or not. + **/ + private void genFfs(File ffsFile) { + Section sect; + int fileSize; + int fileDataSize; + FfsHeader ffsHeader = new FfsHeader(); + FfsHeader orgFfsHeader = new FfsHeader(); + + EdkLog.log(this, EdkLog.EDK_INFO, ffsFile.getName()); + + try { + // + // Create file output stream -- dataBuffer. + // + FileOutputStream dataFs = new FileOutputStream (ffsFile.getAbsolutePath()); + DataOutputStream dataBuffer = new DataOutputStream (dataFs); + + // + // Search SectionList find earch section and call it's + // ToBuffer function. + // + Iterator sectionIter = this.sectionList.iterator(); + while (sectionIter.hasNext()) { + sect = (Section)sectionIter.next(); + + try { + // + // The last section don't need 4 byte ffsAligment. + // + sect.toBuffer((DataOutputStream)dataBuffer); + } catch (Exception e) { + throw new BuildException (e.getMessage()); + } + } + dataBuffer.close(); + } catch (Exception e) { + throw new BuildException (e.getMessage()); + } + + // + // Creat Ffs file header + // + try { + + // + // create input stream to read file data + // + byte[] fileBuffer = new byte[(int)ffsFile.length()]; + FileInputStream fi = new FileInputStream (ffsFile.getAbsolutePath()); + DataInputStream di = new DataInputStream (fi); + di.read(fileBuffer); + di.close(); + + // + // Add GUID to header struct + // + if (this.ffsFileGuid != null) { + stringToGuid (this.ffsFileGuid, ffsHeader.name); + } + + ffsHeader.ffsAttributes = this.attributes; + if ((ffsHeader.fileType = stringToType(this.ffsFileType))== -1) { + throw new BuildException ("FFS_FILE_TYPE unknow!\n"); + } + + // + // Copy ffsHeader.ffsAttribute and fileType to orgFfsHeader.ffsAttribute + // and fileType + // + orgFfsHeader.ffsAttributes = ffsHeader.ffsAttributes; + orgFfsHeader.fileType = ffsHeader.fileType; + + // + // Adjust file size. The function is used to tripe the last + // section padding of 4 binary boundary. + // + // + if (ffsHeader.fileType != EFI_FV_FILETYPE_RAW) { + + fileDataSize = adjustFileSize (fileBuffer); + } else { + fileDataSize = fileBuffer.length; + } + + // + // 1. add header size to file size + // + fileSize = fileDataSize + ffsHeader.getSize(); + + if ((ffsHeader.ffsAttributes & FFS_ATTRIB_TAIL_PRESENT) != 0) { + if (ffsHeader.fileType == EFI_FV_FILETYPE_FFS_PAD) { + + throw new BuildException ( + "FFS_ATTRIB_TAIL_PRESENT=TRUE is " + + "invalid for PAD files" + ); + } + if (fileSize == ffsHeader.getSize()) { + throw new BuildException ( + "FFS_ATTRIB_TAIL_PRESENT=TRUE is " + + "invalid for 0-length files" + ); + } + fileSize = fileSize + 2; + } + + // + // 2. set file size to header struct + // + ffsHeader.ffsFileSize[0] = (byte)(fileSize & 0x00FF); + ffsHeader.ffsFileSize[1] = (byte)((fileSize & 0x00FF00)>>8); + ffsHeader.ffsFileSize[2] = (byte)(((int)fileSize & 0xFF0000)>>16); + + // + // Fill in checksums and state, these must be zero for checksumming + // + ffsHeader.integrityCheck.header = calculateChecksum8 ( + ffsHeader.structToBuffer(), + ffsHeader.getSize() + ); + + if ((this.attributes & FFS_ATTRIB_CHECKSUM) != 0) { + if ((this.attributes & FFS_ATTRIB_TAIL_PRESENT) != 0) { + ffsHeader.integrityCheck.file = calculateChecksum8 ( + fileBuffer, + fileDataSize + ); + } else { + ffsHeader.integrityCheck.file = calculateChecksum8 ( + fileBuffer, + fileDataSize + ); + } + } else { + ffsHeader.integrityCheck.file = FFS_FIXED_CHECKSUM; + orgFfsHeader.integrityCheck.file = FFS_FIXED_CHECKSUM; + } + + // + // Set the state now. Spec says the checksum assumes the state is 0. + // + ffsHeader.ffsState = EFI_FILE_HEADER_CONSTRUCTION | + EFI_FILE_HEADER_VALID | + EFI_FILE_DATA_VALID; + + // + // create output stream to first write header data in file, then write sect data in file. + // + FileOutputStream headerFfs = new FileOutputStream (ffsFile.getAbsolutePath()); + DataOutputStream ffsBuffer = new DataOutputStream (headerFfs); + + // + // Add header struct and file data to FFS file + // + ffsBuffer.write(ffsHeader.structToBuffer()); + ffsBuffer.write(fileBuffer, 0, fileDataSize); + + + + // + // If there is a tail, then set it + // + if ((this.attributes & FFS_ATTRIB_TAIL_PRESENT) != 0) { + short tailValue ; + byte [] tailByte = new byte[2]; + + // + // reverse tailvalue , integritycheck.file as hight byte, and + // integritycheck.header as low byte. + // + tailValue = (short)(ffsHeader.integrityCheck.header & 0xff); + tailValue = (short)((tailValue) | ((ffsHeader.integrityCheck.file << 8) & 0xff00)); + tailValue = (short)~tailValue; + + // + // Change short to byte[2] + // + tailByte[0] = (byte)(tailValue & 0xff); + tailByte[1] = (byte)((tailValue & 0xff00)>>8); + ffsBuffer.write(tailByte[0]); + ffsBuffer.write(tailByte[1]); + + } + + // + // close output stream. Note if don't close output stream + // the buffer can't be rewritten to file. + // + ffsBuffer.close(); + } catch (Exception e) { + log("genffsfile failed!"); + throw new BuildException (e.getMessage()); + } + + } + + /** + addGenSection + + This function is to add gensection instance to list + + @param task Instance of GenSectionTask + **/ + public void addGenSection (GenSectionTask task){ + this.sectionList.add(task); + } }