]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Source/FrameworkTasks/org/tianocore/framework/tasks/GenFfsFileTask.java
Using ModuleType to determine FFS file extension.
[mirror_edk2.git] / Tools / Source / FrameworkTasks / org / tianocore / framework / tasks / GenFfsFileTask.java
CommitLineData
878ddf1f 1/** @file\r
2 GenFfsFileTask class.\r
3\r
4 GenFfsFileTaks is to generate ffs file.\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
16package org.tianocore.framework.tasks;\r
17\r
18import java.io.DataInputStream;\r
19import java.io.DataOutputStream;\r
20import java.io.File;\r
21import java.io.FileInputStream;\r
22import java.io.FileOutputStream;\r
23import java.util.ArrayList;\r
24import java.util.Iterator;\r
25import java.util.List;\r
26\r
27import org.apache.tools.ant.BuildException;\r
28import org.apache.tools.ant.Project;\r
29import org.apache.tools.ant.Task;\r
30\r
31/**\r
32 GenFfsFileTask\r
33 \r
34 GenFfsFileTaks is to generate ffs file.\r
35\r
36**/\r
37public class GenFfsFileTask extends Task implements EfiDefine, FfsTypes {\r
38 /**\r
39 * GenFfsFile Task Class\r
40 * class member \r
41 * -baseName : module baseName\r
42 * -ffsFileGuid : module Guid.\r
43 * -ffsFileType : Ffs file type. \r
44 * -ffsAttributeRecovery : The file is required for recovery.\r
45 * -ffsAligment : The file data alignment (0 if none required). See FFS \r
46 * specification for supported alignments (0-7 are only possible \r
47 * values). *\r
48 * -ffsAttributeCheckSum : The file data is checksummed. If this is FALSE a \r
49 * value of 0x5A will be inserted in the file \r
50 * checksum field of the file header. *\r
51 * -sectFileDir : specifies the full path to the component build directory.\r
52 * Required.\r
53 * -ffsAttrib : Data recorde attribute added result.\r
54 * -sectionList : List recorded all section elemet in task.\r
55 */\r
56 ///\r
57 /// module baseName\r
58 ///\r
59 String baseName = "";\r
60 ///\r
275d78c5 61 /// \r
62 /// \r
63 String moduleType;\r
64 ///\r
878ddf1f 65 /// module Guid\r
66 ///\r
67 String ffsFileGuid = "";\r
68 ///\r
69 /// Ffs file type\r
70 ///\r
71 String ffsFileType = "";\r
72 ///\r
73 /// ffsAttribHeaderExtension value is used to set the corresponding bit in \r
74 /// the output FFS file header \r
75 ///\r
76 boolean ffsAttribHeaderExtension = false;\r
77 ///\r
78 /// ffsAttribTailPresent value is used to set the corresponding bit in the \r
79 /// output FFS file header\r
80 ///\r
81 boolean ffsAttribTailPresent = false;\r
82 ///\r
83 /// ffsAttribRecovery value is used to set the corresponding bit in the \r
84 /// output FFS file header\r
85 ///\r
86 boolean ffsAttribRecovery = false;\r
87 ///\r
88 /// ffsAligenment value is used to set the corresponding bit in the output \r
89 /// FFS file header.The specified FFS alignment must be a value between 0 \r
90 /// and 7 inclusive\r
91 ///\r
92 int ffsAlignment = 0;\r
93 ///\r
94 /// ffsAttribChecksum value is used to set the corresponding bit in the \r
95 /// output FFS file header\r
96 ///\r
97 boolean FfsAttribChecksum = false;\r
98 ///\r
99 /// Attribute is used to record the sum of all bit in the output FFS file.\r
100 ///\r
101 byte attributes = 0;\r
102 ///\r
103 /// The output directory of ffs file.\r
104 ///\r
105 String outputDir = "";\r
106 ///\r
107 /// List of section.\r
108 ///\r
109 List<Object> sectionList = new ArrayList<Object>();\r
110\r
111 ///\r
112 /// The path of Framewor_Tools_Paht.\r
113 ///\r
114 static String path = ""; \r
115\r
116 /**\r
117 execute\r
118 \r
119 GenFfsFileTask execute is to generate ffs file according to input section \r
120 dscriptive information.\r
2da8968b 121 **/\r
878ddf1f 122 public void execute() throws BuildException {\r
123 Section sect;\r
124 int fileSize;\r
2da8968b 125 int orgFileSize;\r
878ddf1f 126 int fileDataSize;\r
2da8968b 127 int orgFileDataSize;\r
878ddf1f 128 File ffsFile;\r
2da8968b 129 File ffsOrgFile;\r
878ddf1f 130 FfsHeader ffsHeader = new FfsHeader(); \r
2da8968b 131 FfsHeader orgFfsHeader = new FfsHeader();\r
878ddf1f 132 String ffsSuffix = "";\r
133 String outputPath = "";\r
134\r
135 //\r
136 // Get Fraemwork_Tools_Path\r
137 //\r
138 Project pj = this.getOwningTarget().getProject();\r
2da8968b 139 path = pj.getProperty("env.FRAMEWORK_TOOLS_PATH");\r
878ddf1f 140\r
141 //\r
142 // Check does the BaseName, Guid, FileType set value.\r
143 //\r
144 if (this.baseName.equals("")) {\r
145 throw new BuildException ("Must set BaseName!\n");\r
146 }\r
147\r
148 if (this.ffsFileGuid.equals("")) {\r
149 throw new BuildException ("Must set ffsFileGuid!\n");\r
150 }\r
151\r
152 if (this.ffsFileType.equals("")) {\r
153 throw new BuildException ("Must set ffsFileType!\n");\r
154 }\r
155\r
156 //\r
157 // Create ffs file. File name = FfsFileGuid + BaseName + ffsSuffix.\r
158 // If outputDir's value was set, file will output to the outputDir.\r
159 //\r
275d78c5 160 ffsSuffix = TypeToSuffix (this.moduleType);\r
878ddf1f 161 if (!this.outputDir.equals("")) {\r
162 String temp;\r
163 outputPath = this.outputDir;\r
164 temp = outputPath.replace('\\', File.separatorChar);\r
165 outputPath = temp.replace('/', File.separatorChar);\r
166 if (outputPath.charAt(outputPath.length()-1) != File.separatorChar) {\r
167 outputPath = outputPath + File.separator;\r
168 }\r
169\r
170 }\r
171\r
172 ffsFile = new File (outputPath + this.ffsFileGuid + '-' + this.baseName + ffsSuffix); \r
173 System.out.print("General Ffs file: file name is:\n");\r
174 System.out.print(outputPath + this.ffsFileGuid + '-' + this.baseName + ffsSuffix);\r
175 System.out.print("\n");\r
2da8968b 176 \r
878ddf1f 177 //\r
2da8968b 178 // Create ffs ORG file. fileName = FfsFileGuid + BaseName + ffsSuffix +\r
179 // ".org".\r
180 //\r
181 ffsOrgFile = new File(outputPath + this.ffsFileGuid + '-' + this.baseName + ffsSuffix + ".org");\r
182 \r
878ddf1f 183 try {\r
2da8968b 184 //\r
185 // Create file output stream -- dataBuffer.\r
186 //\r
878ddf1f 187 FileOutputStream dataFs = new FileOutputStream (ffsFile.getAbsolutePath());\r
188 DataOutputStream dataBuffer = new DataOutputStream (dataFs);\r
2da8968b 189 \r
190 //\r
191 // Create org file output stream -- orgDataBuffer\r
192 //\r
193 FileOutputStream orgDataFs = new FileOutputStream (ffsOrgFile.getAbsolutePath());\r
194 DataOutputStream orgDataBuffer = new DataOutputStream (orgDataFs);\r
195 \r
878ddf1f 196 //\r
197 // Search SectionList find earch section and call it's \r
198 // ToBuffer function.\r
199 //\r
200 Iterator sectionIter = this.sectionList.iterator();\r
201 while (sectionIter.hasNext()) {\r
202 sect = (Section)sectionIter.next(); \r
203\r
204 try {\r
205 //\r
206 // The last section don't need 4 byte ffsAligment.\r
207 //\r
2da8968b 208 sect.toBuffer((DataOutputStream)dataBuffer, (DataOutputStream) orgDataBuffer);\r
878ddf1f 209 } catch (Exception e) {\r
210 throw new BuildException (e.getMessage());\r
211 }\r
212 }\r
213 dataBuffer.close();\r
164803cb 214 orgDataBuffer.close();\r
878ddf1f 215 } catch (Exception e) {\r
216 throw new BuildException (e.getMessage());\r
217 }\r
218\r
219 //\r
220 // Creat Ffs file header\r
221 //\r
222 try {\r
223\r
224 //\r
225 // create input stream to read file data\r
226 //\r
227 byte[] fileBuffer = new byte[(int)ffsFile.length()];\r
228 FileInputStream fi = new FileInputStream (ffsFile.getAbsolutePath());\r
229 DataInputStream di = new DataInputStream (fi);\r
230 di.read(fileBuffer);\r
2da8968b 231 di.close();\r
232 \r
233 //\r
234 // create input org stream to read file data\r
235 //\r
236 byte[] orgFileBuffer = new byte[(int)ffsOrgFile.length()];\r
237 FileInputStream ofi = new FileInputStream (ffsOrgFile.getAbsolutePath());\r
238 DataInputStream odi = new DataInputStream (ofi);\r
239 odi.read(orgFileBuffer);\r
240 odi.close();\r
878ddf1f 241\r
242 //\r
243 // Add GUID to header struct\r
244 //\r
245 if (this.ffsFileGuid != null) {\r
246 stringToGuid (this.ffsFileGuid, ffsHeader.name);\r
2da8968b 247 //\r
248 // Add Guid to org header struct\r
249 //\r
250 stringToGuid (this.ffsFileGuid, orgFfsHeader.name);\r
878ddf1f 251 }\r
252\r
253 ffsHeader.ffsAttributes = this.attributes;\r
254 if ((ffsHeader.fileType = stringToType(this.ffsFileType))== -1) {\r
255 throw new BuildException ("FFS_FILE_TYPE unknow!\n");\r
256 }\r
2da8968b 257 \r
258 //\r
259 // Copy ffsHeader.ffsAttribute and fileType to orgFfsHeader.ffsAttribute\r
260 // and fileType\r
261 // \r
262 orgFfsHeader.ffsAttributes = ffsHeader.ffsAttributes;\r
263 orgFfsHeader.fileType = ffsHeader.fileType;\r
264 \r
878ddf1f 265 //\r
266 // Adjust file size. The function is used to tripe the last \r
267 // section padding of 4 binary boundary. \r
268 // \r
269 //\r
270 if (ffsHeader.fileType != EFI_FV_FILETYPE_RAW) {\r
271\r
272 fileDataSize = adjustFileSize (fileBuffer);\r
2da8968b 273 orgFileDataSize = adjustFileSize (orgFileBuffer);\r
878ddf1f 274\r
275 } else {\r
276 fileDataSize = fileBuffer.length;\r
2da8968b 277 orgFileDataSize = orgFileBuffer.length;\r
878ddf1f 278 }\r
279\r
280 //\r
281 // 1. add header size to file size\r
282 //\r
283 fileSize = fileDataSize + ffsHeader.getSize();\r
2da8968b 284 //\r
285 // add header size to org file size\r
286 //\r
287 orgFileSize = orgFileDataSize + ffsHeader.getSize();\r
878ddf1f 288\r
289 if ((ffsHeader.ffsAttributes & FFS_ATTRIB_TAIL_PRESENT) != 0) {\r
290 if (ffsHeader.fileType == EFI_FV_FILETYPE_FFS_PAD) {\r
291\r
292 throw new BuildException (\r
293 "FFS_ATTRIB_TAIL_PRESENT=TRUE is " +\r
294 "invalid for PAD files"\r
295 );\r
296 }\r
297 if (fileSize == ffsHeader.getSize()) {\r
298 throw new BuildException (\r
299 "FFS_ATTRIB_TAIL_PRESENT=TRUE is " +\r
300 "invalid for 0-length files"\r
301 ); \r
302 }\r
303 fileSize = fileSize + 2;\r
2da8968b 304 orgFileSize = orgFileSize + 2;\r
878ddf1f 305 }\r
306\r
307 //\r
308 // 2. set file size to header struct\r
309 //\r
310 ffsHeader.ffsFileSize[0] = (byte)(fileSize & 0x00FF);\r
311 ffsHeader.ffsFileSize[1] = (byte)((fileSize & 0x00FF00)>>8);\r
312 ffsHeader.ffsFileSize[2] = (byte)(((int)fileSize & 0xFF0000)>>16);\r
2da8968b 313 \r
314 //\r
315 // set file size to org header struct\r
316 //\r
317 orgFfsHeader.ffsFileSize[0] = (byte)(orgFileSize & 0x00FF);\r
318 orgFfsHeader.ffsFileSize[1] = (byte)((orgFileSize & 0x00FF00)>>8);\r
319 orgFfsHeader.ffsFileSize[2] = (byte)(((int)orgFileSize & 0xFF0000)>>16);\r
320 \r
878ddf1f 321 //\r
322 // Fill in checksums and state, these must be zero for checksumming\r
323 //\r
324 ffsHeader.integrityCheck.header = calculateChecksum8 (\r
325 ffsHeader.structToBuffer(),\r
326 ffsHeader.getSize()\r
327 );\r
2da8968b 328 //\r
329 // Fill in org file's header check sum and state\r
330 //\r
331 orgFfsHeader.integrityCheck.header = calculateChecksum8 (\r
332 orgFfsHeader.structToBuffer(),\r
333 orgFfsHeader.getSize()\r
334 );\r
335 \r
878ddf1f 336 if ((this.attributes & FFS_ATTRIB_CHECKSUM) != 0) {\r
337 if ((this.attributes & FFS_ATTRIB_TAIL_PRESENT) != 0) {\r
338 ffsHeader.integrityCheck.file = calculateChecksum8 (\r
339 fileBuffer, \r
340 fileDataSize\r
341 );\r
2da8968b 342 //\r
343 // Add org file header\r
344 //\r
345 orgFfsHeader.integrityCheck.file = calculateChecksum8 (\r
346 orgFileBuffer,\r
347 orgFileDataSize\r
348 );\r
878ddf1f 349 } else {\r
350 ffsHeader.integrityCheck.file = calculateChecksum8 (\r
351 fileBuffer,\r
352 fileDataSize\r
353 );\r
2da8968b 354 //\r
355 // Add org file header\r
356 //\r
357 orgFfsHeader.integrityCheck.file = calculateChecksum8 (\r
358 orgFileBuffer,\r
359 orgFileDataSize\r
360 );\r
878ddf1f 361 }\r
362 } else {\r
363 ffsHeader.integrityCheck.file = FFS_FIXED_CHECKSUM;\r
2da8968b 364 orgFfsHeader.integrityCheck.file = FFS_FIXED_CHECKSUM;\r
878ddf1f 365 }\r
366\r
367 //\r
368 // Set the state now. Spec says the checksum assumes the state is 0.\r
369 //\r
370 ffsHeader.ffsState = EFI_FILE_HEADER_CONSTRUCTION | \r
371 EFI_FILE_HEADER_VALID | \r
372 EFI_FILE_DATA_VALID;\r
c965d3af 373 orgFfsHeader.ffsState = ffsHeader.ffsState;\r
2da8968b 374 \r
878ddf1f 375 //\r
376 // create output stream to first write header data in file, then write sect data in file.\r
377 //\r
378 FileOutputStream headerFfs = new FileOutputStream (ffsFile.getAbsolutePath());\r
379 DataOutputStream ffsBuffer = new DataOutputStream (headerFfs);\r
2da8968b 380 \r
381 FileOutputStream orgHeaderFfs = new FileOutputStream (ffsOrgFile.getAbsolutePath());\r
382 DataOutputStream orgFfsBuffer = new DataOutputStream (orgHeaderFfs);\r
383 \r
878ddf1f 384 //\r
385 // Add header struct and file data to FFS file\r
386 //\r
387 ffsBuffer.write(ffsHeader.structToBuffer());\r
2da8968b 388 orgFfsBuffer.write(orgFfsHeader.structToBuffer());\r
389 \r
878ddf1f 390 for (int i = 0; i< fileDataSize; i++) {\r
391 ffsBuffer.write(fileBuffer[i]);\r
392 }\r
2da8968b 393 \r
394 for (int i = 0; i < orgFileDataSize; i++){\r
395 orgFfsBuffer.write(orgFileBuffer[i]);\r
396 }\r
878ddf1f 397\r
398 //\r
399 // If there is a tail, then set it\r
400 //\r
401 if ((this.attributes & FFS_ATTRIB_TAIL_PRESENT) != 0) {\r
402 short tailValue ;\r
403 byte [] tailByte = new byte[2];\r
404\r
405 //\r
406 // reverse tailvalue , integritycheck.file as hight byte, and \r
407 // integritycheck.header as low byte.\r
408 //\r
409 tailValue = (short)(ffsHeader.integrityCheck.header & 0xff);\r
410 tailValue = (short)((tailValue) | ((ffsHeader.integrityCheck.file << 8) & 0xff00)); \r
411 tailValue = (short)~tailValue;\r
412\r
413 //\r
414 // Change short to byte[2]\r
415 //\r
416 tailByte[0] = (byte)(tailValue & 0xff);\r
417 tailByte[1] = (byte)((tailValue & 0xff00)>>8); \r
418 ffsBuffer.write(tailByte[0]);\r
419 ffsBuffer.write(tailByte[1]);\r
2da8968b 420 \r
421 orgFfsBuffer.write(tailByte[0]);\r
422 orgFfsBuffer.write(tailByte[1]);\r
878ddf1f 423 }\r
424\r
425 //\r
426 // close output stream. Note if don't close output stream \r
427 // the buffer can't be rewritten to file. \r
428 //\r
429 ffsBuffer.close();\r
2da8968b 430 orgFfsBuffer.close();\r
878ddf1f 431 System.out.print ("Successful create ffs file!\n");\r
432 } catch (Exception e) {\r
433 throw new BuildException (e.getMessage());\r
434 }\r
435 } \r
436\r
437 /**\r
438 addCompress\r
439 \r
440 This function is to add compress section to section list.\r
441 @param compress Section of compress \r
442 **/\r
443 public void addCompress(CompressSection compress) {\r
444 this.sectionList.add(compress);\r
445 }\r
446\r
447 /**\r
448 addTool\r
449 \r
450 This function is to add tool section to section list.\r
451 @param tool Section of tool\r
452 **/\r
453 public void addTool(Tool tool) {\r
454 this.sectionList.add(tool);\r
455 }\r
456\r
457 /**\r
458 addSectionFile\r
459 \r
460 This function is to add sectFile section to section list.\r
461 @param sectFile Section of sectFile.\r
462 **/\r
463 public void addSectFile (SectFile sectFile) {\r
464 this.sectionList.add(sectFile); \r
465 }\r
466\r
467 /**\r
468 getBaseName\r
469 \r
470 This function is to get basename\r
471 \r
472 @return String of base name\r
473 **/\r
474 public String getBaseName() {\r
475 return this.baseName;\r
476 }\r
477\r
478 /**\r
479 setBaseName\r
480 \r
481 This function is to set base name.\r
482 @param baseName\r
483 **/\r
484 public void setBaseName(String baseName) {\r
485 this.baseName = baseName.trim();\r
486 }\r
487\r
488 /**\r
489 getFfsAligment\r
490 \r
491 This function is to get the ffsAligment\r
492 @return The value of ffsAligment.\r
493 **/\r
494 public int getFfsAligment() {\r
495 return this.ffsAlignment;\r
496 }\r
497\r
498 /**\r
499 setFfsAligment\r
500 \r
501 This function is to set ffsAligment \r
502 @param ffsAligment The value of ffsAligment.\r
503 **/\r
504 public void setFfsAligment(int ffsAligment) {\r
505 this.ffsAlignment = ffsAligment;\r
506 if (this.ffsAlignment > 7) {\r
507 throw new BuildException ("FFS_ALIGMENT Scope is 0-7");\r
508 } else {\r
509 attributes |= (((byte)this.ffsAlignment) << 3);\r
510 }\r
511 }\r
512\r
513 /**\r
514 getFfsAttribCheckSum\r
515 \r
516 This function is to get ffsAttribCheckSum\r
517 \r
518 @return Value of ffsAttribChecksum \r
519 **/\r
520 public boolean getFfsAttribChecksum() {\r
521 return this.FfsAttribChecksum;\r
522 }\r
523\r
524 /**\r
525 setFfsAttribChecksum\r
526 \r
527 This function is to set ffsAttribChecksum\r
528 @param ffsAttributeCheckSum Value of ffsAttribCheckSum\r
529 **/\r
530 public void setFfsAttribChecksum(boolean ffsAttributeCheckSum) {\r
531 this.FfsAttribChecksum = ffsAttributeCheckSum;\r
532 if (ffsAttributeCheckSum) {\r
533 attributes |= FFS_ATTRIB_CHECKSUM;\r
534 }\r
535 }\r
536\r
537 /**\r
538 getFfsAttribRecovery\r
539 \r
540 This function is to get ffsAttribRecovery\r
541 @return Value of ffsAttribRecovery\r
542 **/\r
543 public boolean getFfsAttribRecovery() {\r
544 return this.ffsAttribRecovery;\r
545 }\r
546\r
547 /**\r
548 setRecovery\r
549 \r
550 This function is to set ffsAttributeRecovery\r
551 \r
552 @param ffsAttributeRecovery Value of ffsAttributeRecovery\r
553 **/\r
554 public void setRecovery(boolean ffsAttributeRecovery) {\r
555 this.ffsAttribRecovery = ffsAttributeRecovery;\r
556 if (ffsAttributeRecovery) {\r
557 attributes |= FFS_ATTRIB_RECOVERY;\r
558 }\r
559 }\r
560\r
561 /**\r
562 getFileGuid\r
563 \r
564 This function is to get fileGuid\r
565 @return Guid\r
566 **/\r
567 public String getFileGuid() {\r
568 return this.ffsFileGuid;\r
569 }\r
570\r
571 /**\r
572 setFileGuid\r
573 \r
574 This function is to set fileGuid\r
575 @param ffsFileGuid String of GUID\r
576 **/\r
577 public void setFileGuid(String ffsFileGuid) {\r
578 this.ffsFileGuid = ffsFileGuid.trim();\r
579 }\r
580\r
581 /**\r
582 getFfsFileType\r
583 \r
584 This function is to get ffsFileType.\r
585 \r
586 @return value of ffsFileType\r
587 **/\r
588 public String getFfsFileType() {\r
589 return this.ffsFileType;\r
590 }\r
591\r
592 /**\r
593 setFfsFileType\r
594 \r
595 This function is to set ffsFileType.\r
596 \r
597 @param ffsFileType \r
598 **/\r
599 public void setFfsFileType(String ffsFileType) {\r
600 this.ffsFileType = ffsFileType.trim();\r
601 }\r
602\r
603 /**\r
604 ffsAttribHeaderExtension\r
605 \r
606 This function is to get ffsAttribHeaderExtension\r
607 \r
608 @return Value of ffsAttribHeaderExtension\r
609 **/\r
610 public boolean isFfsAttribHeaderExtension() {\r
611 return this.ffsAttribHeaderExtension;\r
612 }\r
613\r
614 /**\r
615 setHeaderExension\r
616 \r
617 This function is to set headerExtension\r
618 @param headerExtension Value of headerExension\r
619 **/\r
620 public void setHeaderExtension(boolean headerExtension) {\r
621 this.ffsAttribHeaderExtension = headerExtension;\r
622 if (headerExtension) {\r
623 attributes |= FFS_ATTRIB_HEADER_EXTENSION;\r
624 }\r
625 }\r
626\r
627 /**\r
628 isFfsAttribTailPresent\r
629 \r
630 This function is to get ffsAttribTailPresent value.\r
631 @return Value of ffsAttribTailPresent.\r
632 **/\r
633 public boolean isFfsAttribTailPresent() {\r
634 return this.ffsAttribTailPresent;\r
635 }\r
636\r
637 /**\r
638 setFfsAttribTailPresent\r
639 \r
640 This function is to set ffsAttribTailPresent.\r
641 @param tailPresent Value of ffsAttribTailPresent.\r
642 **/\r
643 public void setFfsAttribTailPresent(boolean tailPresent) {\r
644 this.ffsAttribTailPresent = tailPresent;\r
645 if (tailPresent) {\r
646 attributes |= FFS_ATTRIB_TAIL_PRESENT;\r
647 }\r
648 } \r
649\r
650\r
651 /**\r
652 stringToGuid\r
653 \r
654 This function is to convert string to GUID.\r
655 * @param GuidStr String of GUID.\r
656 * @param Guid GUID form.\r
657 */\r
658 private void stringToGuid (String GuidStr, FfsHeader.FfsGuid Guid){ \r
659\r
660 int i = 0;\r
661 int j = 0;\r
662 int k = 0;\r
663 char [] charArry;\r
664 String [] SplitStr;\r
665\r
666 byte[] buffer = new byte[16];\r
667 if (GuidStr.length()!=36) {\r
668 throw new BuildException ("Guid length is not correct!");\r
669 }\r
670\r
671\r
672 SplitStr = GuidStr.split("-");\r
673 if (SplitStr.length != 5) {\r
674 throw new BuildException ("Guid type is not correct!");\r
675 }\r
676\r
677\r
678\r
679 for (i= 0; i < SplitStr.length; i++) {\r
680 String str = SplitStr[i]; \r
681 charArry = str.toCharArray();\r
682\r
683 for (j =0; j < (str.toCharArray().length)/2; j++) {\r
684\r
685 buffer[k] = hexCharToByte (charArry[j*2]); \r
686 buffer[k] = (byte)( buffer[k]& 0x0f); \r
687 buffer[k] = (byte)((buffer[k]<< 4));\r
688 buffer[k] = (byte)( buffer[k]& 0xf0); \r
689 buffer[k] = (byte)( buffer[k]|hexCharToByte(charArry[j*2+1]));\r
690 k++; \r
691 }\r
692 }\r
693 Guid.bufferToStruct(buffer);\r
694 }\r
695\r
696 /**\r
697 typeToSuffix\r
698 \r
699 This function is to get suffix of ffs file according to ffsFileType.\r
700 \r
701 @param ffsFileType ffsFileType\r
702 @return The suffix of ffs file\r
703 **/\r
704 private String TypeToSuffix (String ffsFileType){\r
275d78c5 705 String[][] suffix = { { "BASE", ".FFS"},\r
706 { "SEC", ".SEC" }, { "PEI_CORE", ".PEI" }, \r
707 { "PEIM", ".PEI" }, { "DXE_CORE", ".DXE" },\r
708 { "DXE_DRIVER", ".DXE" }, { "DXE_RUNTIME_DRIVER", ".DXE" }, \r
709 { "DXE_SAL_DRIVER", ".DXE" }, { "DXE_SMM_DRIVER", ".DXE" }, \r
710 { "TOOL", ".FFS" }, { "UEFI_DRIVER", ".DXE" },\r
711 { "UEFI_APPLICATION", ".APP" }, { "USER_DEFINED", ".FFS" } };\r
712\r
713 for (int i = 0; i < suffix.length; i++) {\r
714 if (suffix[i][0].equalsIgnoreCase(moduleType)) {\r
715 return suffix[i][1];\r
716 }\r
878ddf1f 717 }\r
275d78c5 718\r
719 return ".FFS";\r
878ddf1f 720 }\r
721\r
722\r
723 /**\r
724 stringToType\r
725 \r
726 This function is to get ffsFileType integer value according to ffsFileType.\r
727 @param ffsFileType String value of ffsFileType\r
728 @return Integer value of ffsFileType.\r
729 **/\r
730 private byte stringToType (String ffsFileType){\r
731\r
732 if (ffsFileType.equals("EFI_FV_FILETYPE_ALL")) {\r
733 return(byte)EFI_FV_FILETYPE_ALL;\r
734 }\r
735\r
736 if (ffsFileType.equals("EFI_FV_FILETYPE_RAW")) {\r
737 return(byte)EFI_FV_FILETYPE_RAW;\r
738 }\r
739\r
740 if (ffsFileType.equals("EFI_FV_FILETYPE_FREEFORM")) {\r
2da8968b 741 return(byte)EFI_FV_FILETYPE_FREEFORM;\r
878ddf1f 742 }\r
743\r
744 if (ffsFileType.equals("EFI_FV_FILETYPE_SECURITY_CORE")) {\r
745 return(byte)EFI_FV_FILETYPE_SECURITY_CORE;\r
746 }\r
747\r
748 if (ffsFileType.equals("EFI_FV_FILETYPE_PEI_CORE")) {\r
749 return(byte) EFI_FV_FILETYPE_PEI_CORE;\r
750 }\r
751\r
752 if (ffsFileType.equals("EFI_FV_FILETYPE_DXE_CORE")) {\r
753 return(byte)EFI_FV_FILETYPE_DXE_CORE;\r
754 }\r
755\r
756 if (ffsFileType.equals("EFI_FV_FILETYPE_PEIM")) {\r
757 return(byte)EFI_FV_FILETYPE_PEIM;\r
758 }\r
759\r
760 if (ffsFileType.equals("EFI_FV_FILETYPE_DRIVER")) {\r
761 return(byte) EFI_FV_FILETYPE_DRIVER;\r
762 }\r
763\r
764 if (ffsFileType.equals("EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER")) {\r
765 return(byte)EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER;\r
766 }\r
767\r
768 if (ffsFileType.equals("EFI_FV_FILETYPE_APPLICATION")) {\r
769 return(byte)EFI_FV_FILETYPE_APPLICATION;\r
770 }\r
771\r
772 if (ffsFileType.equals("EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE")) {\r
773 return(byte)EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE;\r
774 }\r
775 if (ffsFileType.equals("EFI_FV_FILETYPE_FFS_PAD")) {\r
776 return(byte) EFI_FV_FILETYPE_FFS_PAD;\r
777 }\r
778\r
779 return -1;\r
780 }\r
781\r
782\r
783\r
784 /**\r
785 calculateCheckSum8\r
786 \r
787 This function is to calculate the value needed for a valid UINT8 checksum\r
788 @param buffer Byte buffer containing byte data of component.\r
789 @param size Size of the buffer.\r
790 @return The 8 bit checksum value needed.\r
791 **/\r
792 private byte calculateChecksum8 (byte[] buffer, int size){\r
793 return(byte) (0x100 - calculateSum8 (buffer, size));\r
794 }\r
795\r
796\r
797 /**\r
798 calculateSum8\r
799 \r
800 This function is to calculate the UINT8 sum for the requested region.\r
801 @param buffer Byte buffer containing byte data of component\r
802 @param size Size of the buffer.\r
803 @return The 8 bit checksum value needed.\r
804 **/ \r
805 private short calculateSum8 (byte[] buffer, int size){\r
806 int Index;\r
807 byte Sum;\r
808 Sum = 0;\r
809\r
810 //\r
811 // Perform the word sum for buffer\r
812 //\r
813 for (Index = 0; Index < size; Index++) {\r
814 Sum = (byte) (Sum + buffer[Index]); \r
815 }\r
816\r
817 return(byte) Sum; \r
818 }\r
819\r
820 /**\r
821 hexCharToByte\r
822 \r
823 This function is to convert hex character to byte\r
824 \r
825 @param hexChar hex character\r
826 @return Byte which corresponding to the character.\r
827 **/\r
828 private byte hexCharToByte (char hexChar){\r
829 switch (hexChar) {\r
830 case '0':\r
831 return(byte)0x00;\r
832 case '1':\r
833 return(byte)0x01;\r
834 case '2':\r
835 return(byte)0x02;\r
836 case '3':\r
837 return(byte)0x03;\r
838 case '4':\r
839 return(byte)0x04;\r
840 case '5':\r
841 return(byte)0x05;\r
842 case '6':\r
843 return(byte)0x06;\r
844 case '7':\r
845 return(byte)0x07;\r
846 case '8':\r
847 return(byte)0x08;\r
848 case '9':\r
849 return(byte)0x09;\r
850 case 'a':\r
851 case 'A':\r
852 return(byte)0x0a;\r
853 case 'b':\r
854 case 'B':\r
855 return(byte)0x0b;\r
856 case 'c':\r
857 case 'C':\r
858 return(byte)0x0c;\r
859\r
860 case 'd':\r
861 case 'D': \r
862 return(byte)0x0d;\r
863\r
864 case 'e':\r
865 case 'E':\r
866 return(byte)0x0e;\r
867 case 'f':\r
868 case 'F':\r
869 return(byte)0x0f;\r
870\r
871 default:\r
872 return(byte)0xff; \r
873 }\r
874 }\r
875\r
876 /**\r
877 adjustFileSize\r
878 \r
879 This function is used to adjusts file size to insure sectioned file is exactly the right length such\r
880 that it ends on exactly the last byte of the last section. ProcessScript()\r
881 may have padded beyond the end of the last section out to a 4 byte boundary.\r
882 This padding is stripped. \r
883 \r
884 @param buffer Byte buffer contains a section stream\r
885 @return Corrected size of file.\r
886 **/\r
887 private int adjustFileSize (byte[] buffer){ \r
888\r
889 int orignalLen = buffer.length;\r
890 int adjustLen = 0;\r
891 int sectionPoint = 0;\r
892 int nextSectionPoint = 0;\r
893 int sectionLen = 0;\r
894 int totalLen = 0;\r
895 int firstSectionHeader = 0;\r
896\r
897\r
898 firstSectionHeader = buffer[0]& 0xff;\r
899 firstSectionHeader = ((buffer[1]&0xff)<<8) | firstSectionHeader;\r
900 firstSectionHeader = ((buffer[2]&0xff)<<16)| firstSectionHeader;\r
901\r
902\r
903 while (sectionPoint < buffer.length) {\r
904 sectionLen = buffer[0 + sectionPoint]& 0xff;\r
905 sectionLen = ((buffer[1 + sectionPoint]&0xff)<<8)| sectionLen;\r
906 sectionLen = ((buffer[2 + sectionPoint]&0xff)<<16)| sectionLen; \r
907 totalLen = totalLen + sectionLen;\r
908\r
909 if (totalLen == orignalLen) {\r
910 return totalLen;\r
911 }\r
912\r
913 sectionPoint = sectionPoint + sectionLen;\r
914 adjustLen = sectionPoint;\r
915\r
916 nextSectionPoint = (sectionPoint + 0x03) & (~0x03);\r
917 totalLen = totalLen + nextSectionPoint - sectionLen;\r
918 sectionPoint = nextSectionPoint;\r
919 }\r
920 return adjustLen;\r
921 }\r
922\r
923 /**\r
924 getOutputDir\r
925 \r
926 This function is to get output directory.\r
927 \r
928 @return Path of output directory.\r
929 **/\r
930 public String getOutputDir() {\r
931 return outputDir;\r
932 }\r
933\r
934 /**\r
935 setOutputDir\r
936 \r
937 This function is to set output directory.\r
938 \r
939 @param outputDir The output direcotry.\r
940 **/\r
941 public void setOutputDir(String outputDir) {\r
942 this.outputDir = outputDir;\r
943 }\r
275d78c5 944\r
945 public String getModuleType() {\r
946 return this.moduleType;\r
947 }\r
948\r
949 public void setModuleType(String moduleType) {\r
950 this.moduleType = moduleType;\r
951 }\r
878ddf1f 952}\r