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