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