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