summaryrefslogtreecommitdiff
path: root/Tools/Java/Source/FrameworkTasks/org/tianocore/framework/tasks/FlashMapTask.java
blob: 9a3c61e6c879c5765b8530af61f3f83388dafcbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
/** @file
 FlashMapTask class.

 FlashMapTask is used to call FlashMap.exe to lay out the flash.


 Copyright (c) 2006, Intel Corporation
 All rights reserved. This program and the accompanying materials
 are licensed and made available under the terms and conditions of the BSD License
 which accompanies this distribution.  The full text of the license may be found at
 http://opensource.org/licenses/bsd-license.php

 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

 **/
package org.tianocore.framework.tasks;

import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;

import java.util.List;
import java.util.ArrayList;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

import org.apache.tools.ant.Task;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.taskdefs.Execute;
import org.apache.tools.ant.taskdefs.LogStreamHandler;
import org.apache.tools.ant.types.Commandline;

import org.tianocore.common.logger.EdkLog;

/**
 * FlashMapTask class.
 *
 * FlashMapTask is used to call FlashMap.exe to generate flash map defition files and fd files.
 */
public class FlashMapTask extends Task implements EfiDefine {
    //
    // tool name
    //
    private static final String toolName = "FlashMap";

    //
    // 
    // 
    private static Pattern fileBlock = Pattern.compile("\\s*File\\s*\\{([^\\{\\}]+)\\}");
    private static Pattern fileNameDef = Pattern.compile("\\bName\\s*=\\s*\"([^\"]+)\"");
    
    //
    // Flash definition file
    //
    private FileArg flashDefFile = new FileArg();

    //
    // Flash device
    //
    private ToolArg flashDevice = new ToolArg();

    //
    // Flash device Image
    //
    private ToolArg flashDeviceImage = new ToolArg();

    //
    // MCI file
    //
    private FileArg mciFile = new FileArg();

    //
    // MCO file
    //
    private FileArg mcoFile = new FileArg();

    //
    // Discover FD image
    //
    private ToolArg fdImage = new ToolArg();

    //
    // Dsc file
    //
    private FileArg dscFile = new FileArg();

    //
    // Asm INC file
    //
    private FileArg asmIncFile = new FileArg();

    //
    // Image out file
    //
    private FileArg imageOutFile = new FileArg();

    //
    // Header file
    //
    private FileArg headerFile = new FileArg();

    //
    // Input string file
    //
    private String inStrFile = "";

    //
    // Output string file
    //
    private String outStrFile = "";

    //
    //
    //
    private FileArg strFile = new FileArg();
    //
    // Base address
    //
    private ToolArg baseAddr = new ToolArg();

    //
    // Aligment
    //
    private ToolArg aligment = new ToolArg();

    //
    // Padding value
    //
    private ToolArg padValue = new ToolArg();

    //
    // output directory
    //
    private String outputDir = ".";

    //
    // MCI file array
    //
    FileArg mciFileArray = new FileArg();

    /**
      execute

      FlashMapTask execute function is to assemble tool command line & execute
      tool command line

      @throws BuidException
     **/
    public void execute() throws BuildException {
        if (isUptodate()) {
            EdkLog.log(this, EdkLog.EDK_VERBOSE, headerFile.toFileList()
                                                 + imageOutFile.toFileList()
                                                 + mcoFile.toFileList()
                                                 + dscFile.toFileList()
                                                 + asmIncFile.toFileList()
                                                 + outStrFile
                                                 + " is up-to-date!");
            return;
        }

        Project project = this.getOwningTarget().getProject();
        //
        // absolute path of efi tools
        //
        String path = project.getProperty("env.FRAMEWORK_TOOLS_PATH");
        String command;
        if (path == null) {
            command = toolName;
        } else {
            command = path + File.separator + toolName;
        }

        //
        // add substituted input file and output file
        //
        if (this.inStrFile != null && this.outStrFile != null
            && this.inStrFile.length() > 0 && this.outStrFile.length() > 0) {
            strFile.setPrefix(" -strsub ");
            strFile.insValue(this.inStrFile);
            strFile.insValue(this.outStrFile);
        }

        String argument = "" + flashDefFile + flashDevice + flashDeviceImage
                             + mciFile + mcoFile + fdImage + dscFile + asmIncFile
                             + imageOutFile + headerFile + strFile + baseAddr
                             + aligment + padValue + mciFileArray;


        //
        // lauch the program
        //
        // ProcessBuilder pb = new ProcessBuilder(argList);
        // pb.directory(new File(outputDir));
        int exitCode = 0;
        try {
            Commandline cmdline = new Commandline();
            cmdline.setExecutable(command);
            cmdline.createArgument().setLine(argument);

            LogStreamHandler streamHandler = new LogStreamHandler(this,
                    Project.MSG_INFO, Project.MSG_WARN);
            Execute runner = new Execute(streamHandler, null);

            runner.setAntRun(project);
            runner.setCommandline(cmdline.getCommandline());

            if (outputDir != null) {
                runner.setWorkingDirectory(new File(outputDir));
            }
            //
            // log command line string.
            //
            EdkLog.log(this, EdkLog.EDK_VERBOSE, Commandline.toString(cmdline.getCommandline()));
            EdkLog.log(this, flashDefFile.toFileList()
                             + mciFile.toFileList()
                             + mciFileArray.toFileList()
                             + fdImage.toFileList()
                             + inStrFile
                             + " => "
                             + headerFile.toFileList()
                             + imageOutFile.toFileList()
                             + mcoFile.toFileList()
                             + dscFile.toFileList()
                             + asmIncFile.toFileList()
                             + outStrFile);

            exitCode = runner.execute();
            if (exitCode != 0) {
                EdkLog.log(this, "ERROR = " + Integer.toHexString(exitCode));
            } else {
                EdkLog.log(this, EdkLog.EDK_VERBOSE, "FlashMap succeeded!");
            }
        } catch (Exception e) {
            throw new BuildException(e.getMessage());
        } finally {
            if (exitCode != 0) {
                throw new BuildException("FlashMap failed!");
            }
        }
    }

    /**
      getFlashDefFile

      This function is to get class member "flashDefFile"

      @return flashDeFile Name of flash definition file.
     **/
    public String getFlashDefFile() {
        return this.flashDefFile.getValue();
    }

    /**
      setFlashDefFile

      This function is to set class member "flashDefFile"

      @param flashDefFile
                 Name of flash definition file.
     **/
    public void setFlashDefFile(String flashDefFile) {
        this.flashDefFile.setArg(" -fdf ", flashDefFile);
    }

    /**
      getAligment

      This function is to get class member "aligment"

      @return aligment String of aligment value.
     **/
    public String getAligment() {
        return this.aligment.getValue();
    }

    /**
      setAligment

      This function is to set class member "aligment"

      @param aligment
                 String of aligment value.
     **/
    public void setAligment(String aligment) {
        this.aligment.setArg(" -align ", aligment);
    }

    /**
      getAsmIncFile

      This function is to get class member "asmIncFile"

      @return asmIncFile String of ASM include file.
     **/
    public String getAsmIncFile() {
        return this.asmIncFile.getValue();
    }

    /**
      setAsmIncFile

      This function is to set class member "asmIncFile"

      @param asmIncFile
                 String of ASM include file.
     **/
    public void setAsmIncFile(String asmIncFile) {
        this.asmIncFile.setArg(" -asmincfile ", asmIncFile);
    }

    /**
      getBaseAddr

      This function is to get class member "baseAddr"

      @return baseAddr String of base address value.
     **/
    public String getBaseAddr() {
        return this.baseAddr.getValue();
    }

    /**
      setBaseAddr

      This function is to set class member "baseAddr"

      @param baseAddr
                 String of base address value.
     **/
    public void setBaseAddr(String baseAddr) {
        this.baseAddr.setArg(" -baseaddr ", baseAddr);
    }

    /**
      getDscFile

      This function is to get class member "dscFile"

      @return dscFile name of DSC file
     **/
    public String getDscFile() {
        return this.dscFile.getValue();
    }

    /**
      setDscFile

      This function is to set class member "dscFile"

      @param dscFile
                 name of DSC file
     **/
    public void setDscFile(String dscFile) {
        this.dscFile.setArg(" -dsc ", dscFile);
    }

    /**
      getFdImage

      This function is to get class member "fdImage"

      @return fdImage name of input FDI image file.
     **/
    public String getFdImage() {
        return this.fdImage.getValue();
    }

    /**
      setFdImage

      This function is to set class member "fdImage"

      @param fdImage
                 name of input FDI image file.
     **/
    public void setFdImage(String fdImage) {
        this.fdImage.setArg(" -discover ", fdImage);
    }

    /**
      getFlashDevice

      This function is to get class member "flashDevice".

      @return flashDevice name of flash device.
     **/
    public String getFlashDevice() {
        return this.flashDevice.getValue();
    }

    /**
      setFlashDevice

      This function is to set class member "flashDevice"

      @param flashDevice
                 name of flash device.
     **/
    public void setFlashDevice(String flashDevice) {
        this.flashDevice.setArg(" -flashdevice ", flashDevice);
    }

    /**
      getFlashDeviceImage

      This function is to get class member "flashDeviceImage"

      @return flashDeviceImage name of flash device image
     **/
    public String getFlashDeviceImage() {
        return this.flashDeviceImage.getValue();
    }

    /**
      setFlashDeviceImage

      This function is to set class member "flashDeviceImage"

      @param flashDeviceImage
                 name of flash device image
     **/
    public void setFlashDeviceImage(String flashDeviceImage) {
        this.flashDeviceImage.setArg(" -flashdeviceimage ", flashDeviceImage);

    }

    /**
      getHeaderFile

      This function is to get class member "headerFile"

      @return headerFile name of include file
     **/
    public String getHeaderFile() {
        return this.headerFile.getValue();
    }

    /**
      setHeaderFile

      This function is to set class member "headerFile"

      @param headerFile
                 name of include file
     **/
    public void setHeaderFile(String headerFile) {
        this.headerFile.setArg(" -hfile ", headerFile);
    }

    /**
      getImageOutFile

      This function is to get class member "imageOutFile"

      @return imageOutFile name of output image file
     **/
    public String getImageOutFile() {
        return this.imageOutFile.getValue();
    }

    /**
      setImageOutFile

      This function is to set class member "ImageOutFile"

      @param imageOutFile
                 name of output image file
     **/
    public void setImageOutFile(String imageOutFile) {
        this.imageOutFile.setArg(" -imageout ", imageOutFile);
    }

    /**
      getInStrFile

      This function is to get class member "inStrFile"

      @return inStrFile name of input file which used to replace symbol names.
     **/
    public String getInStrFile() {
        return this.inStrFile;
    }

    /**
      setInStrFile

      This function is to set class member "inStrFile"

      @param inStrFile
                 name of input file which used to replace symbol names.
     **/
    public void setInStrFile(String inStrFile) {
        this.inStrFile = inStrFile;
    }

    /**
      getMciFile

      This function is to get class member "mciFile"

      @return mciFile name of input microcode file
     **/
    public String getMciFile() {
        return this.mciFile.getValue();
    }

    /**
      setMciFile

      This function is to set class member "mciFile"

      @param mciFile
                 name of input microcode file
     **/
    public void setMciFile(String mciFile) {
        this.mciFile.setArg(" -mci ", mciFile);
    }

    /**
      getMcoFile

      This function is to get class member "mcoFile"

      @return mcoFile name of output binary microcode image
     **/
    public String getMcoFile() {
        return this.mcoFile.getValue();
    }

    /**
      setMcoFile

      This function is to set class member "mcoFile"

      @param mcoFile
                 name of output binary microcode image
     **/
    public void setMcoFile(String mcoFile) {
        this.mcoFile.setArg(" -mco ", mcoFile);
    }

    /**
      getOutStrFile

      This function is to get class member "outStrFile"

      @return outStrFile name of output string substitution file
     **/
    public String getOutStrFile() {
        return this.outStrFile;
    }

    /**
      setOutStrFile

      This function is to set class member "outStrFile"

      @param outStrFile
                 name of output string substitution file
     **/
    public void setOutStrFile(String outStrFile) {
        this.outStrFile = outStrFile;
    }

    /**
      getPadValue

      This function is to get class member "padValue"

      @return padValue string of byte value to use as padding
     **/
    public String getPadValue() {
        return this.padValue.getValue();
    }

    /**
      setPadValue

      This function is to set class member "padValue"

      @param padValue
                 string of byte value to use as padding
     **/
    public void setPadValue(String padValue) {
        this.padValue.setArg(" -padvalue ", padValue);
    }

    /**
      addMciFile

      This function is to add Microcode binary file

      @param mciFile
                 instance of input class
     **/
    public void addConfiguredMciFile(FileArg mciFile) {
        this.mciFileArray.setPrefix(" -mcmerge ");
        this.mciFileArray.insert(mciFile);
    }

    /**
      getOutputDir

      This function is to get class member "outputDir"

      @return outputDir string of output directory
     **/
    public String getOutputDir() {
        return outputDir;
    }

    /**
      setOutputDir

      This function is to set class member "outputDir"

      @param outputDir
                 string of output directory
     **/
    public void setOutputDir(String outputDir) {
        this.outputDir = outputDir;
    }

    //
    // Dependency check
    //
    private boolean isUptodate() {
        long srcTimeStamp = 0;
        String srcName = "";
        String dstName = "";
        long timeStamp = 0;

        if (!flashDefFile.isEmpty()) {
            srcName = flashDefFile.getValue();
            timeStamp = new File(srcName).lastModified();
            if (timeStamp > srcTimeStamp) {
                srcTimeStamp = timeStamp;
            }
        }

        if (!mciFile.isEmpty()) {
            srcName = mciFile.getValue();
            timeStamp = new File(srcName).lastModified();
            if (timeStamp > srcTimeStamp) {
                srcTimeStamp = timeStamp;
            }
        }

        if (!fdImage.isEmpty()) {
            srcName = fdImage.getValue();
            timeStamp = new File(srcName).lastModified();
            if (timeStamp > srcTimeStamp) {
                srcTimeStamp = timeStamp;
            }
        }

        if (inStrFile.length() != 0) {
            srcName = inStrFile;
            timeStamp = new File(srcName).lastModified();
            if (timeStamp > srcTimeStamp) {
                srcTimeStamp = timeStamp;
            }
        }

        if (!mciFileArray.isEmpty()) {
            for (int i = 0; i < mciFileArray.nameList.size(); ++i) {
                srcName += mciFileArray.nameList.get(i) + " ";
                timeStamp = new File(mciFileArray.nameList.get(i)).lastModified();
                if (timeStamp > srcTimeStamp) {
                    srcTimeStamp = timeStamp;
                }
            }
        }

        if (!headerFile.isEmpty()) {
            dstName = headerFile.getValue();
            File dstFile = new File(dstName);
            if (!dstFile.isAbsolute()) {
                dstName = outputDir + File.separator + dstName;
                dstFile = new File(dstName);
            }

            if (srcTimeStamp > dstFile.lastModified()) {
                EdkLog.log(this, EdkLog.EDK_VERBOSE, srcName + " has been changed since last build!");
                return false;
            }
        }

        if (!imageOutFile.isEmpty()) {
            dstName = imageOutFile.getValue();
            File dstFile = new File(dstName);
            if (!dstFile.isAbsolute()) {
                dstName = outputDir + File.separator + dstName;
                dstFile = new File(dstName);
            }

            if (srcTimeStamp > dstFile.lastModified()) {
                EdkLog.log(this, EdkLog.EDK_VERBOSE, srcName + " has been changed since last build!");
                return false;
            }

            //
            // we need to check the time stamp of each FV file specified in fdf file
            // 
            if (!isFdUptodate(dstName, getFvFiles(flashDefFile.getValue()))) {
                return false;
            }
        }

        if (!mcoFile.isEmpty()) {
            dstName = mcoFile.getValue();
            File dstFile = new File(dstName);
            if (!dstFile.isAbsolute()) {
                dstName = outputDir + File.separator + dstName;
                dstFile = new File(dstName);
            }

            if (srcTimeStamp > dstFile.lastModified()) {
                EdkLog.log(this, EdkLog.EDK_VERBOSE, srcName + " has been changed since last build!");
                return false;
            }
        }

        if (!dscFile.isEmpty()) {
            dstName = dscFile.getValue();
            File dstFile = new File(dstName);
            if (!dstFile.isAbsolute()) {
                dstName = outputDir + File.separator + dstName;
                dstFile = new File(dstName);
            }

            if (srcTimeStamp > dstFile.lastModified()) {
                EdkLog.log(this, EdkLog.EDK_VERBOSE, srcName + " has been changed since last build!");
                return false;
            }
        }

        if (!asmIncFile.isEmpty()) {
            dstName = asmIncFile.getValue();
            File dstFile = new File(dstName);
            if (!dstFile.isAbsolute()) {
                dstName = outputDir + File.separator + dstName;
                dstFile = new File(dstName);
            }

            if (srcTimeStamp > dstFile.lastModified()) {
                EdkLog.log(this, EdkLog.EDK_VERBOSE, srcName + " has been changed since last build!");
                return false;
            }
        }

        if (outStrFile.length() != 0) {
            dstName = outStrFile;
            File dstFile = new File(dstName);
            if (!dstFile.isAbsolute()) {
                dstName = outputDir + File.separator + dstName;
                dstFile = new File(dstName);
            }

            if (srcTimeStamp > dstFile.lastModified()) {
                EdkLog.log(this, EdkLog.EDK_VERBOSE, srcName + " has been changed since last build!");
                return false;
            }
        }

        return true;
    }

    //
    // Parse the flash definition file and find out the FV file names
    // 
    private List<String> getFvFiles(String fdfFileName) {
        File fdfFile = new File(fdfFileName);
        int fileLength = (int)fdfFile.length();
        char[] fdfContent = new char[fileLength];
        List<String> fileList = new ArrayList<String>();

        try {
            FileReader reader = new FileReader(fdfFile);
            BufferedReader in = new BufferedReader(reader);

            in.read(fdfContent, 0, fileLength);
            String str = new String(fdfContent);

            //
            // match the 
            //      File {
            //        ...
            //      }
            // block
            // 
            Matcher matcher = fileBlock.matcher(str);
            while (matcher.find()) {
                String fileBlockContent = str.substring(matcher.start(1), matcher.end(1));
                //
                // match the definition like
                //      Name = "..."
                //  
                Matcher nameMatcher = fileNameDef.matcher(fileBlockContent);
                if (nameMatcher.find()) {
                    fileList.add(fileBlockContent.substring(nameMatcher.start(1), nameMatcher.end(1)));
                }
            }

            in.close();
            reader.close();
        } catch (Exception ex) {
            throw new BuildException(ex.getMessage());
        }

        return fileList;
    }

    private boolean isFdUptodate(String fdFile, List<String> fvFileList) {
        String fvDir = ".";
        File fd = new File(fdFile);

        if (outputDir.equals(".")) {
            if (!fd.isAbsolute()) {
                //
                // If we cannot get the absolute path of fd file, we caanot
                // get its time stamp. Re-generate it always in such situation.
                // 
                EdkLog.log(this, EdkLog.EDK_VERBOSE, "Cannot retrieve the time stamp of " + fdFile);
                return false;
            }
            fvDir = fd.getParent();
        } else {
            fvDir = outputDir;
            if (!fd.isAbsolute()) {
                fd = new File(fvDir + File.separator + fdFile);
            }
        }

        long fdTimeStamp = fd.lastModified();
        for (int i = 0; i < fvFileList.size(); ++i) {
            File fv = new File(fvDir + File.separator + fvFileList.get(i));
            if (fv.lastModified() > fdTimeStamp) {
                EdkLog.log(this, EdkLog.EDK_VERBOSE, fv.getPath() + " has been changed since last build!");
                return false;
            }
        }

        return true;
    }
}