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