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