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