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