Enhance peirebase tool to get base address from the corresponding fv.inf file, which...
[mirror_edk2.git] / Tools / CCode / Source / GenFvImage / GenFvImageExe.c
CommitLineData
878ddf1f 1/*++\r
2\r
3Copyright (c) 2004, Intel Corporation \r
4All rights reserved. This program and the accompanying materials \r
5are licensed and made available under the terms and conditions of the BSD License \r
6which accompanies this distribution. The full text of the license may be found at \r
7http://opensource.org/licenses/bsd-license.php \r
8 \r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
11\r
12Module Name:\r
13\r
14 GenFvImageExe.c\r
15\r
16Abstract:\r
17\r
18 This contains all code necessary to build the GenFvImage.exe utility. \r
19 This utility relies heavily on the GenFvImage Lib. Definitions for both\r
20 can be found in the Tiano Firmware Volume Generation Utility \r
21 Specification, review draft.\r
22\r
23--*/\r
24\r
25//\r
26// File included in build\r
27//\r
28#include "GenFvImageExe.h"\r
29#include "CommonLib.h"\r
30#include "EfiUtilityMsgs.h"\r
31\r
32VOID\r
33PrintUtilityInfo (\r
34 VOID\r
35 )\r
36/*++\r
37\r
38Routine Description:\r
39\r
40 Displays the standard utility information to SDTOUT\r
41\r
42Arguments:\r
43\r
44 None\r
45\r
46Returns:\r
47\r
48 None\r
49\r
50--*/\r
51{\r
52 printf (\r
53 "%s - Tiano Firmware Volume Generation Utility."" Version %i.%i\n\n",\r
54 UTILITY_NAME,\r
55 UTILITY_MAJOR_VERSION,\r
56 UTILITY_MINOR_VERSION\r
57 );\r
58}\r
59\r
60VOID\r
61PrintUsage (\r
62 VOID\r
63 )\r
64/*++\r
65\r
66Routine Description:\r
67\r
68 Displays the utility usage syntax to STDOUT\r
69\r
70Arguments:\r
71\r
72 None\r
73\r
74Returns:\r
75\r
76 None\r
77\r
78--*/\r
79{\r
80 printf ("Usage: %s -I FvInfFileName\n", UTILITY_NAME);\r
81 printf (" Where:\n");\r
82 printf ("\tFvInfFileName is the name of the image description file.\n\n");\r
83}\r
84\r
85int\r
86main (\r
87 IN INTN argc,\r
88 IN CHAR8 **argv\r
89 )\r
90/*++\r
91\r
92Routine Description:\r
93\r
94 This utility uses GenFvImage.Lib to build a firmware volume image.\r
95\r
96Arguments:\r
97\r
98 FvInfFileName The name of an FV image description file.\r
99\r
100 Arguments come in pair in any order.\r
101 -I FvInfFileName \r
102\r
103Returns:\r
104\r
105 EFI_SUCCESS No error conditions detected.\r
106 EFI_INVALID_PARAMETER One or more of the input parameters is invalid.\r
107 EFI_OUT_OF_RESOURCES A resource required by the utility was unavailable. \r
108 Most commonly this will be memory allocation \r
109 or file creation.\r
110 EFI_LOAD_ERROR GenFvImage.lib could not be loaded.\r
111 EFI_ABORTED Error executing the GenFvImage lib.\r
112\r
113--*/\r
114{\r
115 EFI_STATUS Status;\r
116 CHAR8 InfFileName[_MAX_PATH];\r
117 CHAR8 *InfFileImage;\r
118 UINTN InfFileSize;\r
119 UINT8 *FvImage;\r
120 UINTN FvImageSize;\r
121 UINT8 Index;\r
122 CHAR8 FvFileNameBuffer[_MAX_PATH];\r
123 CHAR8 *FvFileName;\r
124 FILE *FvFile;\r
125 FILE *SymFile;\r
126 CHAR8 SymFileNameBuffer[_MAX_PATH];\r
127 CHAR8 *SymFileName;\r
128 UINT8 *SymImage;\r
1d940d05 129 UINTN SymImageSize = 0;\r
878ddf1f 130 CHAR8 *CurrentSymString;\r
131\r
132 FvFileName = FvFileNameBuffer;\r
133 SymFileName = SymFileNameBuffer;\r
134\r
135 SetUtilityName (UTILITY_NAME);\r
136 //\r
137 // Display utility information\r
138 //\r
139 PrintUtilityInfo ();\r
140\r
141 //\r
142 // Verify the correct number of arguments\r
143 //\r
144 if (argc != MAX_ARGS) {\r
145 Error (NULL, 0, 0, "invalid number of input parameters specified", NULL);\r
146 PrintUsage ();\r
147 return GetUtilityStatus ();\r
148 }\r
149 //\r
150 // Initialize variables\r
151 //\r
152 strcpy (InfFileName, "");\r
153\r
154 //\r
155 // Parse the command line arguments\r
156 //\r
157 for (Index = 1; Index < MAX_ARGS; Index += 2) {\r
158 //\r
159 // Make sure argument pair begin with - or /\r
160 //\r
161 if (argv[Index][0] != '-' && argv[Index][0] != '/') {\r
162 Error (NULL, 0, 0, argv[Index], "argument pair must begin with \"-\" or \"/\"");\r
163 PrintUsage ();\r
164 return GetUtilityStatus ();\r
165 }\r
166 //\r
167 // Make sure argument specifier is only one letter\r
168 //\r
169 if (argv[Index][2] != 0) {\r
170 Error (NULL, 0, 0, argv[Index], "unrecognized argument");\r
171 PrintUsage ();\r
172 return GetUtilityStatus ();\r
173 }\r
174 //\r
175 // Determine argument to read\r
176 //\r
177 switch (argv[Index][1]) {\r
178\r
179 case 'I':\r
180 case 'i':\r
181 if (strlen (InfFileName) == 0) {\r
182 strcpy (InfFileName, argv[Index + 1]);\r
183 } else {\r
184 Error (NULL, 0, 0, argv[Index + 1], "FvInfFileName may only be specified once");\r
185 PrintUsage ();\r
186 return GetUtilityStatus ();\r
187 }\r
188 break;\r
189\r
190 default:\r
191 Error (NULL, 0, 0, argv[Index], "unrecognized argument");\r
192 PrintUsage ();\r
193 return GetUtilityStatus ();\r
194 break;\r
195 }\r
196 }\r
197 //\r
198 // Read the INF file image\r
199 //\r
200 Status = GetFileImage (InfFileName, &InfFileImage, &InfFileSize);\r
201 if (EFI_ERROR (Status)) {\r
202 return STATUS_ERROR;\r
203 }\r
204 //\r
205 // Call the GenFvImage lib\r
206 //\r
207 Status = GenerateFvImage (\r
208 InfFileImage,\r
209 InfFileSize,\r
210 &FvImage,\r
211 &FvImageSize,\r
212 &FvFileName,\r
213 &SymImage,\r
214 &SymImageSize,\r
215 &SymFileName\r
216 );\r
217\r
4c50c885
LG
218 //\r
219 // free InfFileImage memory\r
220 //\r
221 free (InfFileImage);\r
222\r
878ddf1f 223 if (EFI_ERROR (Status)) {\r
224 switch (Status) {\r
225\r
226 case EFI_INVALID_PARAMETER:\r
227 Error (NULL, 0, 0, "invalid parameter passed to GenFvImage Lib", NULL);\r
228 return GetUtilityStatus ();\r
229 break;\r
230\r
231 case EFI_ABORTED:\r
232 Error (NULL, 0, 0, "error detected while creating the file image", NULL);\r
233 return GetUtilityStatus ();\r
234 break;\r
235\r
236 case EFI_OUT_OF_RESOURCES:\r
237 Error (NULL, 0, 0, "GenFvImage Lib could not allocate required resources", NULL);\r
238 return GetUtilityStatus ();\r
239 break;\r
240\r
241 case EFI_VOLUME_CORRUPTED:\r
242 Error (NULL, 0, 0, "no base address was specified, but the FV.INF included a PEI or BSF file", NULL);\r
243 return GetUtilityStatus ();\r
244 break;\r
245\r
246 case EFI_LOAD_ERROR:\r
247 Error (NULL, 0, 0, "could not load FV image generation library", NULL);\r
248 return GetUtilityStatus ();\r
249 break;\r
250\r
251 default:\r
252 Error (NULL, 0, 0, "GenFvImage Lib returned unknown status", "status returned = 0x%X", Status);\r
253 return GetUtilityStatus ();\r
254 break;\r
255 }\r
256 }\r
257 //\r
258 // Write file\r
259 //\r
260 FvFile = fopen (FvFileName, "wb");\r
261 if (FvFile == NULL) {\r
262 Error (NULL, 0, 0, FvFileName, "could not open output file");\r
263 free (FvImage);\r
264 free (SymImage);\r
265 return GetUtilityStatus ();\r
266 }\r
267\r
268 if (fwrite (FvImage, 1, FvImageSize, FvFile) != FvImageSize) {\r
269 Error (NULL, 0, 0, FvFileName, "failed to write to output file");\r
270 free (FvImage);\r
271 free (SymImage);\r
272 fclose (FvFile);\r
273 return GetUtilityStatus ();\r
274 }\r
275\r
276 fclose (FvFile);\r
277 free (FvImage);\r
278\r
279 //\r
280 // Write symbol file\r
281 //\r
282 if (strcmp (SymFileName, "")) {\r
283 SymFile = fopen (SymFileName, "wt");\r
284 if (SymFile == NULL) {\r
285 Error (NULL, 0, 0, SymFileName, "could not open output symbol file");\r
286 free (SymImage);\r
287 return GetUtilityStatus ();\r
288 }\r
289\r
290 fprintf (SymFile, "TEXTSYM format | V1.0\n");\r
291\r
292 CurrentSymString = SymImage;\r
293 while (((UINTN) CurrentSymString - (UINTN) SymImage) < SymImageSize) {\r
294 fprintf (SymFile, "%s", CurrentSymString);\r
295 CurrentSymString = (CHAR8 *) (((UINTN) CurrentSymString) + strlen (CurrentSymString) + 1);\r
296 }\r
297\r
298 fclose (SymFile);\r
299 }\r
300\r
301 free (SymImage);\r
302\r
303 return GetUtilityStatus ();\r
304}\r