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