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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
|
/*++
Copyright (c) 1999 - 2006 Intel Corporation. All rights reserved
This software and associated documentation (if any) is furnished
under a license and may only be used or copied in accordance
with the terms of the license. Except as permitted by such
license, no part of this software or documentation may be
reproduced, stored in a retrieval system, or transmitted in any
form or by any means without the express written consent of
Intel Corporation.
Module Name:
GenBsfFixup.c
Abstract:
Utility to Fixup the SEC component for IA32. This is an
interim tool in place of the full GenBsfImage support for
IA32.
This tool supports the Synch3 update
--*/
#include "BaseTypes.h"
#include "UefiBaseTypes.h"
#include "EfiImage.h"
#include "FirmwareVolumeHeader.h"
#include "FirmwareVolumeImageFormat.h"
#include "ParseInf.h"
#include "CommonLib.h"
#include "FirmwareFileSystem.h"
#include "FvLib.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
//
// BugBug -- this port to the new FFS is really weird.
// A lot of the file-header stuff has been ported, but
// not the section information.
//
#define UTILITY_NAME "GenBsfFixup"
#define UTILITY_MAJOR_VERSION 0
#define UTILITY_MINOR_VERSION 1
UINT32 gFixup;
UINT32
GetOccupiedSize (
IN UINT32 ActualSize,
IN UINT32 Alignment
)
/*++
Routine Description:
GC_TODO: Add function description
Arguments:
ActualSize - GC_TODO: add argument description
Alignment - GC_TODO: add argument description
Returns:
GC_TODO: add return values
--*/
{
UINT32 OccupiedSize;
OccupiedSize = ActualSize;
while ((OccupiedSize & (Alignment - 1)) != 0) {
OccupiedSize++;
}
return OccupiedSize;
}
static
void
Version (
VOID
)
{
printf ("%s v%d.%d -EDK Utility to Fixup the SEC component for IA32.\n", UTILITY_NAME, UTILITY_MAJOR_VERSION, UTILITY_MINOR_VERSION);
printf ("Copyright (c) 1999-2006 Intel Corporation. All rights reserved.\n");
}
VOID
Usage (
VOID
)
{
Version();
printf ("\nUsage: " UTILITY_NAME " FvVolumeImageFile AddressOfFvInMemory OffsetOfFixup OutputFileName \n");
}
int
ReadHeader (
FILE *In,
UINT32 *FvSize
)
/*++
Routine Description:
Reads in Firmware Volume information from the volume header file.
Arguments:
In: Firmware Volume header file to read from
FvSize: Size of Firmware Volume
Returns:
int: Number of bytes read
--*/
{
EFI_FIRMWARE_VOLUME_HEADER VolumeHeader;
EFI_FV_BLOCK_MAP_ENTRY BlockMap;
INT32 SigTemp[2];
INT32 Invert;
INT32 bytesread;
UINT32 size;
size = 0;
Invert = 0;
bytesread = 0;
if (In == NULL) {
printf ("Error: Input file is NULL.\n");
return -1;
}
fread (&VolumeHeader, sizeof (EFI_FIRMWARE_VOLUME_HEADER) - sizeof (EFI_FV_BLOCK_MAP_ENTRY), 1, In);
bytesread = sizeof (EFI_FIRMWARE_VOLUME_HEADER) - sizeof (EFI_FV_BLOCK_MAP_ENTRY);
SigTemp[0] = VolumeHeader.Signature;
SigTemp[1] = 0;
if (VolumeHeader.Attributes & EFI_FVB_ERASE_POLARITY) {
Invert = 1;
}
do {
fread (&BlockMap, sizeof (EFI_FV_BLOCK_MAP_ENTRY), 1, In);
bytesread += sizeof (EFI_FV_BLOCK_MAP_ENTRY);
if (BlockMap.NumBlocks != 0) {
size += BlockMap.NumBlocks * BlockMap.BlockLength;
}
} while (BlockMap.NumBlocks != 0);
*FvSize = size;
rewind (In);
if (Invert == 1) {
bytesread *= -1;
}
return bytesread;
}
UINT32
GetSectionLength (
IN UINT32 *Length
)
/*++
Routine Description:
Converts a UINT8[3] array to a UINT32
Arguments:
Length A pointer to a 3 byte array
Returns:
UINT32: The length.
--*/
{
return *Length & 0x00FFFFFF;
}
int
Readfile (
UINT8 *FvImage,
int bytes,
int Invert
)
/*++
Routine Description:
GC_TODO: Add function description
Arguments:
FvImage - GC_TODO: add argument description
bytes - GC_TODO: add argument description
Invert - GC_TODO: add argument description
Returns:
GC_TODO: add return values
--*/
{
UINT32 FileLength;
UINT32 OccupiedFileLength;
EFI_FFS_FILE_HEADER *FileHeader;
UINT8 FileState;
UINT8 Checksum;
UINT8 *Ptr;
UINT32 SectionLength;
EFI_COMMON_SECTION_HEADER *SectionHeader;
EFI_IMAGE_NT_HEADERS *PeHeader;
UINT32 PeiCoreOffset;
Ptr = FvImage + bytes;
FileHeader = (EFI_FFS_FILE_HEADER *) Ptr;
FileState = GetFileState ((UINT8) Invert, FileHeader);
switch (FileState) {
case EFI_FILE_HEADER_CONSTRUCTION:
case EFI_FILE_HEADER_INVALID:
return sizeof (EFI_FFS_FILE_HEADER);
case EFI_FILE_HEADER_VALID:
Checksum = CalculateSum8 ((UINT8 *) FileHeader, sizeof (EFI_FFS_FILE_HEADER));
Checksum = (UINT8) (Checksum - FileHeader->IntegrityCheck.Checksum.File);
Checksum = (UINT8) (Checksum - FileHeader->State);
if (Checksum != 0) {
return -1;
}
//
// Now do the fixup stuff - begin
//
if (FileHeader->Type == EFI_FV_FILETYPE_PEI_CORE) {
SectionHeader = (EFI_COMMON_SECTION_HEADER *) FileHeader + sizeof (EFI_FFS_FILE_HEADER);
SectionLength = GetSectionLength ((UINT32 *) &SectionHeader->Size[0]);
printf ("Section length is 0x%X\n", SectionLength);
if (SectionHeader->Type == EFI_SECTION_PE32) {
gFixup = bytes + sizeof (EFI_FFS_FILE_HEADER) + sizeof (EFI_COMMON_SECTION_HEADER);
PeHeader = (EFI_IMAGE_NT_HEADERS *) Ptr + sizeof (EFI_FFS_FILE_HEADER) + sizeof (EFI_COMMON_SECTION_HEADER);
if (((EFI_IMAGE_DOS_HEADER *) PeHeader)->e_magic == EFI_IMAGE_DOS_SIGNATURE) {
//
// DOS image header is present, so read the PE header after the DOS image header
//
PeHeader = (EFI_IMAGE_NT_HEADERS *) ((UINTN) PeHeader + (UINTN) ((((EFI_IMAGE_DOS_HEADER *) PeHeader)->e_lfanew) & 0x0ffff));
}
PeiCoreOffset = (UINTN) ((UINTN) (PeHeader->OptionalHeader.AddressOfEntryPoint & 0x0ffffffff));
gFixup += PeiCoreOffset;
}
}
FileLength = GetLength (FileHeader->Size);
OccupiedFileLength = GetOccupiedSize (FileLength, 8);
return OccupiedFileLength;
case EFI_FILE_DATA_VALID:
//
// Calculate header checksum
//
Checksum = CalculateSum8 ((UINT8 *) FileHeader, sizeof (EFI_FFS_FILE_HEADER));
Checksum = (UINT8) (Checksum - FileHeader->IntegrityCheck.Checksum.File);
Checksum = (UINT8) (Checksum - FileHeader->State);
if (Checksum != 0) {
return -1;
}
//
// Determine file length
//
FileLength = GetLength (FileHeader->Size);
OccupiedFileLength = GetOccupiedSize (FileLength, 8);
//
// Determine if file checksum is valid or fixed
//
if (FileHeader->Attributes & FFS_ATTRIB_CHECKSUM) {
Checksum = CalculateSum8 (Ptr, FileLength);
Checksum = (UINT8) (Checksum - FileHeader->State);
if (Checksum != 0) {
return -1;
}
} else {
if (FileHeader->IntegrityCheck.Checksum.File != FFS_FIXED_CHECKSUM) {
return -1;
}
}
break;
case EFI_FILE_MARKED_FOR_UPDATE:
case EFI_FILE_DELETED:
//
// Calculate header checksum
//
Checksum = CalculateSum8 ((UINT8 *) FileHeader, sizeof (EFI_FFS_FILE_HEADER));
Checksum = (UINT8) (Checksum - FileHeader->IntegrityCheck.Checksum.File);
Checksum = (UINT8) (Checksum - FileHeader->State);
if (Checksum != 0) {
return -1;
}
//
// Determine file length
//
FileLength = GetLength (FileHeader->Size);
OccupiedFileLength = GetOccupiedSize (FileLength, 8);
//
// Determine if file checksum is valid or fixed
//
if (FileHeader->Attributes & FFS_ATTRIB_CHECKSUM) {
Checksum = CalculateSum8 (Ptr, FileLength);
Checksum = (UINT8) (Checksum - FileHeader->State);
if (Checksum != 0) {
return -1;
}
} else {
if (FileHeader->IntegrityCheck.Checksum.File != FFS_FIXED_CHECKSUM) {
return -1;
}
}
return OccupiedFileLength;
default:
return sizeof (EFI_FFS_FILE_HEADER);
}
return OccupiedFileLength;
}
int
main (
int argc,
char*argv[]
)
/*++
Routine Description:
Runs GenBsfFixup
Arguments:
argc: number of command line arguments
arg[0] = This file name
arg[1] = Firmware Volume Name
arg[2] = Base Address to relocate
arg[3] = Relative offset of the fixup to perform
arg[4] = Output File Name
Returns:
int: 0 code success, -1 code failure
--*/
{
FILE *In;
FILE *Out;
int ByteStart;
int Invert;
int Index;
int cnt;
UINT8 *FvImage;
int ByteRead;
UINT32 FvSize;
UINT64 delta;
UINT32 Idx;
UINT64 FvOffset;
EFI_STATUS Status;
Index = 0;
Invert = 0;
if (argc < 1) {
Usage();
return -1;
}
if ((strcmp(argv[1], "-h") == 0) || (strcmp(argv[1], "--help") == 0) ||
(strcmp(argv[1], "-?") == 0) || (strcmp(argv[1], "/?") == 0)) {
Usage();
return -1;
}
if ((strcmp(argv[1], "-V") == 0) || (strcmp(argv[1], "--version") == 0)) {
Version();
return -1;
}
if (argc != 5) {
Usage();
return -1;
}
In = fopen (argv[1], "rb");
if (In == NULL) {
printf ("Unable to open FV image file \"%s\"\n", argv[1]);
return -1;
}
ByteStart = ReadHeader (In, &FvSize);
if (ByteStart < 0) {
Invert = 1;
ByteStart *= -1;
}
FvImage = malloc (FvSize);
if (FvImage == NULL) {
printf ("Cannot allocate memory\n");
fclose (In);
return -1;
}
ByteRead = fread (FvImage, 1, FvSize, In);
if ((unsigned int) ByteRead != FvSize) {
printf ("Read File error\n");
fclose (In);
return -1;
}
cnt = 0;
while ((unsigned int) ByteStart < FvSize && cnt != -1) {
cnt = Readfile (FvImage, ByteStart, Invert);
if (cnt != -1) {
ByteStart += cnt;
}
if (cnt != sizeof (EFI_FFS_FILE_HEADER)) {
Index++;
}
}
if (cnt == -1) {
printf ("Firmware Volume image corrupted\n");
return -1;
}
fclose (In);
Out = fopen (argv[4], "wb");
if (Out == NULL) {
printf ("Unable to open FV image file \"%s\"\n", argv[4]);
return -1;
}
In = fopen (argv[1], "rb");
if (In == NULL) {
printf ("Unable to open FV image file \"%s\"\n", argv[1]);
return -1;
}
if (gFixup != 0) {
printf ("Fixup of 0x%X\n", gFixup);
Status = AsciiStringToUint64 (argv[2], TRUE, &FvOffset);
gFixup += (UINT32) FvOffset;
ByteStart = ReadHeader (In, &FvSize);
Readfile (FvImage, ByteStart, Invert);
cnt = 0;
Status = AsciiStringToUint64 (argv[3], TRUE, &delta);
fclose (In);
In = fopen (argv[1], "rb");
if (In == NULL) {
printf ("Unable to open FV image file \"%s\"\n", argv[1]);
return -1;
}
for (Idx = 0; Idx < delta - FvOffset; Idx++) {
fputc (fgetc (In), Out);
}
fwrite (&gFixup, sizeof (UINT32), 1, Out);
fseek (In, sizeof (UINT32), SEEK_CUR);
for (Idx = 0; Idx < FvSize - (delta - FvOffset) - sizeof (UINT32); Idx++) {
fputc (fgetc (In), Out);
}
fclose (In);
fclose (Out);
} else {
printf ("There was no fixup to perform\n");
}
free (FvImage);
return 0;
}
|