1f8a4d1f3c899286a0b07d4b3496173994c91ec4
[mirror_edk2.git] / Tools / Java / Source / FrameworkTasks / org / tianocore / framework / tasks / PeiReBaseTask.java
1 /** @file
2 PeiReBaseTask class.
3
4 PeiReBaseTask is used to call PeiReBase.exe to rebase efi fv file.
5
6
7 Copyright (c) 2006, Intel Corporation
8 All rights reserved. This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
12
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15
16 **/
17 package org.tianocore.framework.tasks;
18
19 import java.io.File;
20
21 import org.apache.tools.ant.Task;
22 import org.apache.tools.ant.Project;
23 import org.apache.tools.ant.BuildException;
24 import org.apache.tools.ant.taskdefs.Execute;
25 import org.apache.tools.ant.taskdefs.LogStreamHandler;
26 import org.apache.tools.ant.types.Commandline;
27
28 import org.tianocore.common.logger.EdkLog;
29
30 /**
31 PeiReBaseTask class.
32
33 PeiReBaseTask is used to call PeiReBase.exe to rebase efi fv file.
34 **/
35 public class PeiReBaseTask extends Task implements EfiDefine {
36 //
37 // tool name
38 //
39 private String toolName = "PeiReBase";
40 //
41 // Input file
42 //
43 private FileArg inputFile = new FileArg();
44 //
45 // Output file
46 //
47 private FileArg outputFile = new FileArg();
48 //
49 // Base address
50 //
51 private ToolArg baseAddr = new ToolArg();
52 //
53 //
54 //
55 private FileArg mapFile = new FileArg();
56 //
57 // Architecture
58 //
59 private String arch = "IA32";
60
61 /**
62 execute
63
64 PeiReBaseTask execute function is to assemble tool command line & execute
65 tool command line
66
67 @throws BuidException
68 **/
69 public void execute() throws BuildException {
70 if (isUptodate()) {
71 EdkLog.log(this, EdkLog.EDK_VERBOSE, outputFile.toFileList() + " is up-to-date!");
72 return;
73 }
74
75 Project project = this.getOwningTarget().getProject();
76
77 //
78 // absolute path of efi tools
79 //
80 String path = project.getProperty("env.FRAMEWORK_TOOLS_PATH");
81 String command;
82 String argument;
83 if (this.arch.equalsIgnoreCase("IA32")){
84 command = toolName + "_IA32";
85 } else if (this.arch.equalsIgnoreCase("X64")){
86 command = toolName + "_X64";
87 } else if (this.arch.equalsIgnoreCase("IPF")){
88 command = toolName + "_IPF";
89 } else {
90 command = toolName + "_IA32";
91 }
92 if (path != null) {
93 command = path + File.separator + command;
94 }
95
96 //
97 // argument of tools
98 //
99 if (mapFile.getValue().length() == 0) {
100 mapFile.setArg(" -M ", outputFile.getValue() + ".map");
101 }
102 argument = "" + inputFile + outputFile + baseAddr + mapFile;
103
104 //
105 // return value of fwimage execution
106 //
107 int revl = -1;
108
109 try {
110 Commandline cmdline = new Commandline();
111 cmdline.setExecutable(command);
112 cmdline.createArgument().setLine(argument);
113
114 LogStreamHandler streamHandler = new LogStreamHandler(this,
115 Project.MSG_INFO, Project.MSG_WARN);
116 Execute runner = new Execute(streamHandler, null);
117
118 runner.setAntRun(project);
119 runner.setCommandline(cmdline.getCommandline());
120 //
121 // Set debug log information.
122 //
123 EdkLog.log(this, EdkLog.EDK_VERBOSE, Commandline.toString(cmdline.getCommandline()));
124 EdkLog.log(this, EdkLog.EDK_INFO, inputFile.toFileList() + " => "
125 + outputFile.toFileList()
126 + mapFile.toFileList());
127
128 revl = runner.execute();
129
130 if (EFI_SUCCESS == revl) {
131 //
132 // command execution success
133 //
134 EdkLog.log(this, EdkLog.EDK_VERBOSE, "PeiReBase succeeded!");
135 } else {
136 //
137 // command execution fail
138 //
139 EdkLog.log(this, EdkLog.EDK_INFO, "ERROR = " + Integer.toHexString(revl));
140 throw new BuildException("PeiReBase failed!");
141 }
142 } catch (Exception e) {
143 throw new BuildException(e.getMessage());
144 }
145 }
146
147 /**
148 getInputFile
149
150 This function is to get class member "inputFile".
151
152 @return string of input file name.
153 **/
154 public String getInputFile() {
155 return inputFile.getValue();
156 }
157
158 /**
159 setComponentType
160
161 This function is to set class member "inputFile".
162
163 @param inputFile
164 string of input file name.
165 **/
166 public void setInputFile(String inputFile) {
167 this.inputFile.setArg(" -I ", inputFile);
168 }
169
170 /**
171 getOutputFile
172
173 This function is to get class member "outputFile"
174
175 @return outputFile string of output file name.
176 **/
177 public String getOutputFile() {
178 return outputFile.getValue();
179 }
180
181 /**
182 setOutputFile
183
184 This function is to set class member "outputFile"
185
186 @param outputFile
187 string of output file name.
188 **/
189 public void setOutputFile(String outputFile) {
190 this.outputFile.setArg(" -O ", outputFile);
191 }
192
193 /**
194 getBaseAddr
195
196 This function is to get class member "baseAddr"
197
198 @return baseAddr string of base address.
199 **/
200 public String getBaseAddr() {
201 return baseAddr.getValue();
202 }
203
204 /**
205 setBaseAddr
206
207 This function is to set class member "baseAddr"
208
209 @param baseAddr string of base address
210 **/
211 public void setBaseAddr(String baseAddr) {
212 this.baseAddr.setArg(" -B ", baseAddr);
213 }
214
215 /**
216 getArch
217
218 This function is to get class member "arch".
219
220 @return arch Architecture
221 **/
222 public String getArch() {
223 return arch;
224 }
225
226 /**
227 setArch
228
229 This function is to set class member "arch"
230
231 @param arch Architecture
232 **/
233 public void setArch(String arch) {
234 this.arch = arch;
235 }
236
237 /**
238 Get the value of map file
239
240 @return String The map file path
241 **/
242 public String getMapFile() {
243 return mapFile.getValue();
244 }
245
246 /**
247 Set "-M MapFile" argument
248
249 @param mapFile The path of map file
250 **/
251 public void setMapFile(String mapFile) {
252 this.mapFile.setArg(" -M ", mapFile);
253 }
254
255 //
256 // Dependency check
257 //
258 private boolean isUptodate() {
259 File srcFile = new File(inputFile.getValue());
260 File dstFile = new File(outputFile.getValue());
261
262 if (srcFile.lastModified() > dstFile.lastModified()) {
263 return false;
264 }
265
266 return true;
267 }
268 }