summaryrefslogtreecommitdiff
path: root/Tools/CodeTools/Source/SecApResetVectorFixup/SecApResetVectorFixup.c
blob: 442645720eed03fe56fb7ec256e77f43e59675c2 (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
/*++

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

    SecApResetVectorFixup.c

Abstract:

    This utility is part of build process for IA32 Fvrecovery.fv whose total size
    is larger than 128kB so that we cannot use GenFvImage utility to put Ap reset 
    vector at the zero vector of Fv header.
    
      PEI FV after using the tool
    
    -------------------------
    |zzz                    |
    |                       |
    |                       |
    |      FFS              |
    |                       |
    |                       |
    |                       |
    |---------------------- |
    |       PAD             |
    |                       |
    |.......................|  --- 
    |                       |   |
    |xxx                    |   | 128K    
    |---------------------- |   | 
    |       VTF (SEC)       |   |
    -------------------------  ---
    
    1. zzz --> Zero vector, which is beyond the 128K limited address space
    2. xxx --> AP reset vector at 4K alignment below 128K and it is in the PAD
       file area.
    3. After the build process ,the PAD guid is changed to a new GUID to avoid 
       the PAD definition confusing. If there is some problem, try to disable
       UpdatePadFileGuid
    
     
    
--*/

#include "SecApResetVectorFixup.h"


EFI_GUID  DefaultFvPadFileNameGuid = { 0x78f54d4, 0xcc22, 0x4048, 0x9e, 0x94, 0x87, 0x9c, 0x21, 0x4d, 0x56, 0x2f };
EFI_GUID  NewFvPadFileNameGuid     = { 0x145372bc, 0x66b9, 0x476d, 0x81, 0xbc, 0x21, 0x27, 0xc3, 0x76, 0xbb, 0x66 };

//
// jmp 0xf000:0xffd0 (0xFFFFFFD0)
//
UINT8 ApResetVector[5] = {0xEA, 0xD0, 0xFF, 0x00, 0xF0};

VOID
PrintUtilityInfo (
  VOID
  )
/*++

Routine Description:

  Displays the standard utility information to SDTOUT

Arguments:

  None

Returns:

  None

--*/
{
  printf (
    "%s - Tiano IA32 SEC Ap Reset Vector Fixup 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 InputFvrecoveryFile OutputFvrecoveryFile\n", UTILITY_NAME);
  printf ("  Where:\n");
  printf ("\tInputFvrecoveryFile   - Name of the IA32 input Fvrecovery.fv file.\n");
  printf ("\tOutputFvrecoveryFile  - Name of the IA32 output Fvrecovery.fv file.\n");
}


VOID 
UpdatePadFileGuid (
  IN     EFI_FIRMWARE_VOLUME_HEADER  *FvHeader,
  IN     EFI_FFS_FILE_HEADER         *FileHeader,
  IN     UINT32                      FileLength,
  IN OUT EFI_GUID                    *Guid
  )
/*++

Routine Description:

  Update the Pad File Guid to change it to other guid and update
  the checksum

Arguments:
  FvHeader   - EFI_FIRMWARE_VOLUME_HEADER 
  FileHeader - The FFS PAD file header.
  FileLength - The FFS PAD file length.
  Guid       - The Guid to compare and if it is PAD Guid, update it to new Guid
Returns:
  VOID
--*/

{
  if ((CompareGuid (Guid, (EFI_GUID *)&DefaultFvPadFileNameGuid)) == 0) {
    //
    // Set new Pad file guid
    // 
    memcpy (Guid, &NewFvPadFileNameGuid, sizeof (EFI_GUID));



    FileHeader->Type       = EFI_FV_FILETYPE_FFS_PAD;
    FileHeader->Attributes = 0;
    //
    // Fill in checksums and state, must be zero during checksum calculation.
    //
    FileHeader->IntegrityCheck.Checksum.Header = 0;
    FileHeader->IntegrityCheck.Checksum.File   = 0;
    FileHeader->State                          = 0;
    FileHeader->IntegrityCheck.Checksum.Header = CalculateChecksum8 ((UINT8 *) FileHeader, sizeof (EFI_FFS_FILE_HEADER));
    if (FileHeader->Attributes & FFS_ATTRIB_CHECKSUM) {
      FileHeader->IntegrityCheck.Checksum.File = CalculateChecksum8 ((UINT8 *) FileHeader, FileLength);
    } else {
      FileHeader->IntegrityCheck.Checksum.File = FFS_FIXED_CHECKSUM;
    }

    FileHeader->State = EFI_FILE_HEADER_CONSTRUCTION | EFI_FILE_HEADER_VALID | EFI_FILE_DATA_VALID;

    if (FvHeader->Attributes & EFI_FVB_ERASE_POLARITY) {
      FileHeader->State = (UINT8)~(FileHeader->State);
    }    
  }
  
}


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                      FvrecoveryFileSize;
  UINT8                       *FileBuffer;
  UINT8                       *FileBufferRaw;
  UINT64                      FvLength;
  UINT32                      Offset;
  UINT32                      FileLength;
  UINT32                      FileOccupiedSize;
  EFI_FIRMWARE_VOLUME_HEADER  *FvHeader;
  EFI_FFS_FILE_HEADER         *FileHeader;
  EFI_GUID                    *TempGuid;
  UINT8                       *FixPoint;
  UINT32                      TempResult;
  UINT32                      Index;
  UINT32                      IpiVector;

  TempGuid = NULL;
  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 STATUS_ERROR;
  }
  //
  // Open the Input Fvrecovery.fv file
  //
  if ((FpIn = fopen (argv[1], "rb")) == NULL) {
    Error (NULL, 0, 0, "Unable to open file", argv[1]);
    return STATUS_ERROR;
  }
  //
  // Get the Input Fvrecovery.fv file size
  //
  fseek (FpIn, 0, SEEK_END);
  FvrecoveryFileSize = ftell (FpIn);
  //
  // Read the contents of input file to memory buffer
  //
  FileBuffer    = NULL;
  FileBufferRaw = NULL;
  FileBufferRaw = (UINT8 *) malloc (FvrecoveryFileSize + 0x10000);
  if (NULL == FileBufferRaw) {
    Error (NULL, 0, 0, "No sufficient memory to allocate!", NULL);
    fclose (FpIn);
    return STATUS_ERROR;
  }
  TempResult = 0x10000 - ((UINT32)FileBufferRaw & 0x0FFFF);
  FileBuffer = (UINT8 *)((UINT32)FileBufferRaw + TempResult);
  fseek (FpIn, 0, SEEK_SET);
  TempResult = fread (FileBuffer, 1, FvrecoveryFileSize, FpIn);
  if (TempResult != FvrecoveryFileSize) {
    Error (NULL, 0, 0, "Read input file error!", NULL);
    free ((VOID *)FileBufferRaw);
    fclose (FpIn);
    return STATUS_ERROR;
  }
  //
  // Close the input Fvrecovery.fv file
  //
  fclose (FpIn);
  //
  // Find the pad FFS file
  //
  FvHeader         = (EFI_FIRMWARE_VOLUME_HEADER *)FileBuffer;
  FvLength         = FvHeader->FvLength;
  FileHeader       = (EFI_FFS_FILE_HEADER *)(FileBuffer + FvHeader->HeaderLength);
  FileLength       = (*(UINT32 *)(FileHeader->Size)) & 0x00FFFFFF;
  FileOccupiedSize = GETOCCUPIEDSIZE(FileLength, 8);
  Offset           = (UINT32)FileHeader - (UINT32)FileBuffer;
  
  while (Offset < FvLength) {
    TempGuid = (EFI_GUID *)&(FileHeader->Name);
    FileLength = (*(UINT32 *)(FileHeader->Size)) & 0x00FFFFFF;
    FileOccupiedSize = GETOCCUPIEDSIZE(FileLength, 8);
    if ((CompareGuid (TempGuid, (EFI_GUID *)&DefaultFvPadFileNameGuid)) == 0) {
      break;
    }
    FileHeader = (EFI_FFS_FILE_HEADER *)((UINT32)FileHeader + FileOccupiedSize);
    Offset = (UINT32)FileHeader - (UINT32)FileBuffer;
  }

  if (Offset >= FvLength) {
    Error (NULL, 0, 0, "No pad file found!", NULL);
    free ((VOID *)FileBufferRaw);
    return STATUS_ERROR;
  }
  //
  // Find the position to place Ap reset vector, the offset
  // between the position and the end of Fvrecovery.fv file
  // should not exceed 128kB to prevent Ap reset vector from
  // outside legacy E and F segment
  //
  FixPoint = (UINT8 *)(FileHeader + sizeof(EFI_FFS_FILE_HEADER));
  TempResult = 0x1000 - ((UINT32)FixPoint & 0x0FFF);
  FixPoint +=TempResult;
  if (((UINT32)FixPoint - (UINT32)FileHeader + 5) > FileOccupiedSize) {
    Error (NULL, 0, 0, "No appropriate space in pad file to add Ap reset vector!", NULL);
    free ((VOID *)FileBufferRaw);
    return STATUS_ERROR;  
  }
  while (((UINT32)FixPoint - (UINT32)FileHeader + 5) <= FileOccupiedSize) {
    FixPoint += 0x1000;
  }
  FixPoint -= 0x1000;
  if ((UINT32)FvHeader + FvLength - (UINT32)FixPoint > 0x20000) {
    Error (NULL, 0, 0, "The position to place Ap reset vector is not in E and F segment!", NULL);
    free ((VOID *)FileBufferRaw);
    return STATUS_ERROR; 
  }    
  //
  // Fix up Ap reset vector and calculate the IPI vector
  //
  for (Index = 0; Index < 5; Index++) {
    FixPoint[Index] = ApResetVector[Index];
  } 
  TempResult = 0x0FFFFFFFF - ((UINT32)FvHeader + (UINT32)FvLength - 1 - (UINT32)FixPoint);
  TempResult >>= 12;
  IpiVector = TempResult & 0x0FF;
  
  
  UpdatePadFileGuid (FvHeader, FileHeader, FileLength, TempGuid);

  //
  // Open the output Fvrecovery.fv file
  //
  if ((FpOut = fopen (argv[2], "w+b")) == NULL) {
    Error (NULL, 0, 0, "Unable to open file", argv[2]);
    free ((VOID *)FileBufferRaw);
    return STATUS_ERROR;
  }
  //
  // Write the output Fvrecovery.fv file
  //
  if ((fwrite (FileBuffer, 1, FvrecoveryFileSize, FpOut)) != FvrecoveryFileSize) {
    Error (NULL, 0, 0, "Write output file error!", NULL);
    free ((VOID *)FileBufferRaw);
    return STATUS_ERROR;   
  }
  //
  //
  //
  fseek (FpOut, -8, SEEK_END);
  if ((fwrite (&IpiVector, 1, sizeof(UINT32), FpOut)) != sizeof(UINT32)) {
    Error (NULL, 0, 0, "Write output file error!", NULL);
    free ((VOID *)FileBufferRaw);
    return STATUS_ERROR;
  }  
  //
  // Close the output Fvrecovery.fv file
  //
  fclose (FpOut);
  free ((VOID *)FileBufferRaw);
  return STATUS_SUCCESS;
}