summaryrefslogtreecommitdiff
path: root/EdkCompatibilityPkg/Sample/Tools/Source/GenFvImage/GenFvImageExe.c
blob: ccd11994354c8241bc2e041cc75b7a5386c9722e (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
/*++

Copyright (c) 2004, 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.             

Module Name:

  GenFvImageExe.c

Abstract:

  This contains all code necessary to build the GenFvImage.exe utility.       
  This utility relies heavily on the GenFvImage Lib.  Definitions for both
  can be found in the Tiano Firmware Volume Generation Utility 
  Specification, review draft.

--*/

//
// File included in build
//
#include "GenFvImageExe.h"
#include "CommonLib.h"
#include "EfiUtilityMsgs.h"

VOID
PrintUtilityInfo (
  VOID
  )
/*++

Routine Description:

  Displays the standard utility information to SDTOUT

Arguments:

  None

Returns:

  None

--*/
{
  printf (
    "%s - Tiano Firmware Volume Generation Utility."" Version %i.%i\n\n",
    UTILITY_NAME,
    UTILITY_MAJOR_VERSION,
    UTILITY_MINOR_VERSION
    );
}

VOID
PrintUsage (
  VOID
  )
/*++

Routine Description:

  Displays the utility usage syntax to STDOUT

Arguments:

  None

Returns:

  None

--*/
{
  printf ("Usage: %s -I FvInfFileName\n", UTILITY_NAME);
  printf ("  Where:\n");
  printf ("\tFvInfFileName is the name of the image description file.\n\n");
}

EFI_STATUS
main (
  IN INTN   argc,
  IN CHAR8  **argv
  )
/*++

Routine Description:

  This utility uses GenFvImage.Lib to build a firmware volume image.

Arguments:

  FvInfFileName      The name of an FV image description file.

  Arguments come in pair in any order.
    -I FvInfFileName 

Returns:

  EFI_SUCCESS            No error conditions detected.
  EFI_INVALID_PARAMETER  One or more of the input parameters is invalid.
  EFI_OUT_OF_RESOURCES   A resource required by the utility was unavailable.  
                         Most commonly this will be memory allocation 
                         or file creation.
  EFI_LOAD_ERROR         GenFvImage.lib could not be loaded.
  EFI_ABORTED            Error executing the GenFvImage lib.

--*/
{
  EFI_STATUS  Status;
  CHAR8       InfFileName[_MAX_PATH];
  CHAR8       *InfFileImage;
  UINTN       InfFileSize;
  UINT8       *FvImage;
  UINTN       FvImageSize;
  UINT8       Index;
  CHAR8       FvFileNameBuffer[_MAX_PATH];
  CHAR8       *FvFileName;
  FILE        *FvFile;
  FILE        *SymFile;
  CHAR8       SymFileNameBuffer[_MAX_PATH];
  CHAR8       *SymFileName;
  UINT8       *SymImage;
  UINTN       SymImageSize;
  CHAR8       *CurrentSymString;

  FvFileName  = FvFileNameBuffer;
  SymFileName = SymFileNameBuffer;

  SetUtilityName (UTILITY_NAME);
  //
  // Display utility information
  //
  PrintUtilityInfo ();

  //
  // Verify the correct number of arguments
  //
  if (argc != MAX_ARGS) {
    Error (NULL, 0, 0, "invalid number of input parameters specified", NULL);
    PrintUsage ();
    return GetUtilityStatus ();
  }
  //
  // Initialize variables
  //
  strcpy (InfFileName, "");

  //
  // Parse the command line arguments
  //
  for (Index = 1; Index < MAX_ARGS; Index += 2) {
    //
    // Make sure argument pair begin with - or /
    //
    if (argv[Index][0] != '-' && argv[Index][0] != '/') {
      Error (NULL, 0, 0, argv[Index], "argument pair must begin with \"-\" or \"/\"");
      PrintUsage ();
      return GetUtilityStatus ();
    }
    //
    // Make sure argument specifier is only one letter
    //
    if (argv[Index][2] != 0) {
      Error (NULL, 0, 0, argv[Index], "unrecognized argument");
      PrintUsage ();
      return GetUtilityStatus ();
    }
    //
    // Determine argument to read
    //
    switch (argv[Index][1]) {

    case 'I':
    case 'i':
      if (strlen (InfFileName) == 0) {
        strcpy (InfFileName, argv[Index + 1]);
      } else {
        Error (NULL, 0, 0, argv[Index + 1], "FvInfFileName may only be specified once");
        PrintUsage ();
        return GetUtilityStatus ();
      }
      break;

    default:
      Error (NULL, 0, 0, argv[Index], "unrecognized argument");
      PrintUsage ();
      return GetUtilityStatus ();
      break;
    }
  }
  //
  // Read the INF file image
  //
  Status = GetFileImage (InfFileName, &InfFileImage, &InfFileSize);
  if (EFI_ERROR (Status)) {
    return STATUS_ERROR;
  }
  //
  // Call the GenFvImage lib
  //
  Status = GenerateFvImage (
            InfFileImage,
            InfFileSize,
            &FvImage,
            &FvImageSize,
            &FvFileName,
            &SymImage,
            &SymImageSize,
            &SymFileName
            );

  if (EFI_ERROR (Status)) {
    switch (Status) {

    case EFI_INVALID_PARAMETER:
      Error (NULL, 0, 0, "invalid parameter passed to GenFvImage Lib", NULL);
      return GetUtilityStatus ();
      break;

    case EFI_ABORTED:
      Error (NULL, 0, 0, "error detected while creating the file image", NULL);
      return GetUtilityStatus ();
      break;

    case EFI_OUT_OF_RESOURCES:
      Error (NULL, 0, 0, "GenFvImage Lib could not allocate required resources", NULL);
      return GetUtilityStatus ();
      break;

    case EFI_VOLUME_CORRUPTED:
      Error (NULL, 0, 0, "no base address was specified, but the FV.INF included a PEI or BSF file", NULL);
      return GetUtilityStatus ();
      break;

    case EFI_LOAD_ERROR:
      Error (NULL, 0, 0, "could not load FV image generation library", NULL);
      return GetUtilityStatus ();
      break;

    default:
      Error (NULL, 0, 0, "GenFvImage Lib returned unknown status", "status returned = 0x%X", Status);
      return GetUtilityStatus ();
      break;
    }
  }
  //
  // Write file
  //
  FvFile = fopen (FvFileName, "wb");
  if (FvFile == NULL) {
    Error (NULL, 0, 0, FvFileName, "could not open output file");
    free (FvImage);
    free (SymImage);
    return GetUtilityStatus ();
  }

  if (fwrite (FvImage, 1, FvImageSize, FvFile) != FvImageSize) {
    Error (NULL, 0, 0, FvFileName, "failed to write to output file");
    free (FvImage);
    free (SymImage);
    fclose (FvFile);
    return GetUtilityStatus ();
  }

  fclose (FvFile);
  free (FvImage);

  //
  // Write symbol file
  //
  if (strcmp (SymFileName, "")) {
    SymFile = fopen (SymFileName, "wt");
    if (SymFile == NULL) {
      Error (NULL, 0, 0, SymFileName, "could not open output symbol file");
      free (SymImage);
      return GetUtilityStatus ();
    }

    fprintf (SymFile, "TEXTSYM format | V1.0\n");

    CurrentSymString = SymImage;
    while (((UINTN) CurrentSymString - (UINTN) SymImage) < SymImageSize) {
      fprintf (SymFile, "%s", CurrentSymString);
      CurrentSymString = (CHAR8 *) (((UINTN) CurrentSymString) + strlen (CurrentSymString) + 1);
    }

    fclose (SymFile);
  }

  free (SymImage);

  return GetUtilityStatus ();
}