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