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