summaryrefslogtreecommitdiff
path: root/Tools/CCode/Source/SecFixup/SecFixup.c
blob: 5a36cdff5e44e73a7b517cd37319a7651a2d46d8 (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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*++

Copyright (c)  1999-2006 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:

    SecFixup.c

Abstract:

    This utility is part of build process for IA32 SEC FFS file.
    
    It fixup the reset vector data. The reset vector data binary file
    will be wrapped as a RAW section and be located immediately after
    the PE/TE section.

    The SEC EXE file can be either PE or TE file.
    
--*/

#include <stdio.h>

#include <Common/UefiBaseTypes.h>
#include <Common/EfiImage.h>
#include <Common/FirmwareVolumeImageFormat.h>

#include "EfiUtilityMsgs.c"
#include "SecFixup.h"

VOID
Version (
  VOID
  )
/*++

Routine Description:

  Displays the standard utility information to SDTOUT

Arguments:

  None

Returns:

  None

--*/
{
  printf ("%s v%d.%d -Tiano IA32 SEC Fixup Utility.\n", UTILITY_NAME, UTILITY_MAJOR_VERSION, UTILITY_MINOR_VERSION);
  printf ("Copyright (c) 1999-2007 Intel Corporation. All rights reserved.\n");
}

VOID
Usage (
  VOID
  )
/*++

Routine Description:

  Displays the utility usage syntax to STDOUT

Arguments:

  None

Returns:

  None

--*/
{
  Version();
  
  printf ("\nUsage: %s SecExeFile ResetVectorDataFile OutputFile\n", UTILITY_NAME);
  printf ("  Where:\n");
  printf ("     SecExeFile           - Name of the IA32 SEC EXE file.\n");
  printf ("     ResetVectorDataFile  - Name of the reset vector data binary file.\n");
  printf ("     OutputFileName       - Name of the output file.\n");
}

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

Routine Description:

  Main function.

Arguments:

  argc - Number of command line parameters.
  argv - Array of pointers to parameter strings.

Returns:
  STATUS_SUCCESS - Utility exits successfully.
  STATUS_ERROR   - Some error occurred during execution.

--*/
{
  FILE    *FpIn;

  FILE    *FpOut;
  UINT32  AddressOfEntryPoint;
  INT32   DestRel;
  STATUS  Status;
  UINT32  SecFileSize;

  SetUtilityName (UTILITY_NAME);

  if (argc == 1) {
    Usage();
    return STATUS_ERROR;
  }
    
  if ((strcmp(argv[1], "-h") == 0) || (strcmp(argv[1], "--help") == 0) ||
      (strcmp(argv[1], "-?") == 0) || (strcmp(argv[1], "/?") == 0)) {
    Usage();
    return STATUS_ERROR;
  }
  
  if ((strcmp(argv[1], "-V") == 0) || (strcmp(argv[1], "--version") == 0)) {
    Version();
    return STATUS_ERROR;
  }
  
  //
  // Verify the correct number of arguments
  //
  if (argc != MAX_ARGS) {
    Error (NULL, 0, 0, "invalid number of input parameters specified", NULL);
    Usage ();
    return STATUS_ERROR;
  }
  //
  // Open the SEC exe file
  //
  if ((FpIn = fopen (argv[1], "rb")) == NULL) {
    Error (NULL, 0, 0, "Unable to open file", argv[1]);
    return STATUS_ERROR;
  }
  //
  // Get the entry point of the EXE file
  //
  Status = GetEntryPoint (FpIn, &AddressOfEntryPoint);
  if (Status != STATUS_SUCCESS) {
    fclose (FpIn);
    return STATUS_ERROR;
  }
  //
  // Get the SEC file size
  //
  fseek (FpIn, 0, SEEK_END);
  SecFileSize = ftell (FpIn);

  //
  // Close the SEC file
  //
  fclose (FpIn);

  //
  // Open the reset vector data file
  //
  if ((FpIn = fopen (argv[2], "rb")) == NULL) {
    Error (NULL, 0, 0, "Unable to open file", argv[2]);
    return STATUS_ERROR;
  }
  //
  // Open the output file
  //
  if ((FpOut = fopen (argv[3], "w+b")) == NULL) {
    Error (NULL, 0, 0, "Unable to open file", argv[3]);
    fclose (FpIn);
    return STATUS_ERROR;
  }
  //
  // Copy the input file to the output file
  //
  if (CopyFile (FpIn, FpOut) != STATUS_SUCCESS) {
    fclose (FpIn);
    fclose (FpOut);
    return STATUS_ERROR;
  }
  //
  // Close the reset vector data file
  //
  fclose (FpIn);

  //
  // Fix the destination relative in the jmp instruction
  // in the reset vector data structure
  //
  fseek (FpOut, -DEST_REL_OFFSET, SEEK_END);
  DestRel = AddressOfEntryPoint - (SecFileSize + sizeof (EFI_COMMON_SECTION_HEADER) + (UINT32) (ftell (FpOut)) + 2);
  if (DestRel <= -65536) {
    Error (NULL, 0, 0, "The SEC EXE file size is too big", NULL);
    fclose (FpOut);
    return STATUS_ERROR;
  }

  if (fwrite (&DestRel, sizeof (UINT16), 1, FpOut) != 1) {
    Error (NULL, 0, 0, "Failed to write to the output file", NULL);
    fclose (FpOut);
    return STATUS_ERROR;
  }
  //
  // Close the output file
  //
  fclose (FpOut);

  return STATUS_SUCCESS;
}

STATUS
GetEntryPoint (
  IN  FILE   *ExeFile,
  OUT UINT32 *EntryPoint
  )
/*++

Routine Description:

  Get the address of the entry point of a PE/TE file.

Arguments:

  PeFile     - File pointer to the specified PE/TE file.
  EntryPoint - Buffer for the address of the entry point to be returned.

Returns:
  STATUS_SUCCESS - Function completed successfully.
  STATUS_ERROR   - Error occured.

--*/
// GC_TODO:    ExeFile - add argument and description to function comment
{
  EFI_IMAGE_DOS_HEADER    DosHeader;
  EFI_IMAGE_NT_HEADERS32  NtHeader;
  EFI_TE_IMAGE_HEADER     TeHeader;

  //
  // Check if it is a TE file
  //
  fseek (ExeFile, 0, SEEK_SET);
  //
  // Attempt to read the TE header
  //
  if (fread (&TeHeader, sizeof (TeHeader), 1, ExeFile) == 1) {
    if (TeHeader.Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) {
      if (TeHeader.Machine != EFI_IMAGE_MACHINE_IA32) {
        Error (NULL, 0, 0, "The SEC file is PE but is not PE32 for IA32", NULL);
        return STATUS_ERROR;
      }

      *EntryPoint = TeHeader.AddressOfEntryPoint + sizeof (EFI_TE_IMAGE_HEADER) - TeHeader.StrippedSize;
      return STATUS_SUCCESS;
    }
  }
  //
  // Check if it is a PE file
  //
  fseek (ExeFile, 0, SEEK_SET);
  //
  // Attempt to read the DOS header
  //
  if (fread (&DosHeader, sizeof (DosHeader), 1, ExeFile) != 1) {
    goto InvalidFile;
  }
  //
  // Check the magic number
  //
  if (DosHeader.e_magic != EFI_IMAGE_DOS_SIGNATURE) {
    goto InvalidFile;
  }
  //
  // Position into the file and read the NT PE header
  //
  fseek (ExeFile, (long) DosHeader.e_lfanew, SEEK_SET);
  if (fread (&NtHeader, sizeof (NtHeader), 1, ExeFile) != 1) {
    goto InvalidFile;
  }
  //
  // Check the PE signature in the header
  //
  if (NtHeader.Signature != EFI_IMAGE_NT_SIGNATURE) {
    goto InvalidFile;
  }
  //
  // Make sure the PE file is PE32 for IA32
  //
  if (NtHeader.FileHeader.Machine != EFI_IMAGE_MACHINE_IA32 ||
      NtHeader.OptionalHeader.Magic != EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
      ) {
    Error (NULL, 0, 0, "The SEC file is PE but is not PE32 for IA32", NULL);
    return STATUS_ERROR;
  }
  //
  // Get the entry point from the optional header
  //
  *EntryPoint = NtHeader.OptionalHeader.AddressOfEntryPoint;
  return STATUS_SUCCESS;

InvalidFile:
  Error (NULL, 0, 0, "The SEC file is neither PE nor TE file", NULL);
  return STATUS_ERROR;
}

STATUS
CopyFile (
  FILE    *FpIn,
  FILE    *FpOut
  )
/*++

Routine Description:

  Copy file.

Arguments:

  FpIn  - File pointer to the source file.
  FpOut - File pointer to the destination file.

Returns:
  STATUS_SUCCESS - Function completed successfully.
  STATUS_ERROR   - Error occured.

--*/
{
  INTN  FileSize;

  INTN  Offset;

  INTN  Length;
  UINT8 Buffer[BUF_SIZE];

  fseek (FpIn, 0, SEEK_END);
  FileSize = ftell (FpIn);

  fseek (FpIn, 0, SEEK_SET);
  fseek (FpOut, 0, SEEK_SET);

  Offset = 0;
  while (Offset < FileSize) {
    Length = sizeof (Buffer);
    if (FileSize - Offset < Length) {
      Length = FileSize - Offset;
    }

    if (fread (Buffer, Length, 1, FpIn) != 1 || fwrite (Buffer, Length, 1, FpOut) != 1) {
      Error (NULL, 0, 0, "Copy file error", NULL);
      return STATUS_ERROR;
    }

    Offset += Length;
  }

  return STATUS_SUCCESS;
}