3 Copyright 2006 - 2007, 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
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.
18 Creates and EFILDR image.
19 This tool combines several PE Image files together using following format denoted as EBNF:
22 <PeImageFileContent> +
23 The order of EFILDR_IMAGE is same as the order of placing PeImageFileContent.
34 #define MAX_PE_IMAGES 63
35 #define FILE_TYPE_FIXED_LOADER 0
36 #define FILE_TYPE_RELOCATABLE_PE_IMAGE 1
47 UINT32 HeaderCheckSum
;
49 UINT32 NumberOfImages
;
59 printf ("Usage: EfiLdrImage OutImage LoaderImage PeImage1 PeImage2 ... PeImageN");
70 Write all the content of input file to output file.
73 in - input file pointer
74 out - output file pointer
77 ULONG : file size of input file
80 ULONG filesize
, offset
, length
;
83 fseek (in
, 0, SEEK_END
);
86 fseek (in
, 0, SEEK_SET
);
89 while (offset
< filesize
) {
90 length
= sizeof(Buffer
);
91 if (filesize
-offset
< length
) {
92 length
= filesize
-offset
;
95 fread (Buffer
, length
, 1, in
);
96 fwrite (Buffer
, length
, 1, out
);
125 EFILDR_HEADER EfiLdrHeader
;
126 EFILDR_IMAGE EfiLdrImage
[MAX_PE_IMAGES
];
133 // Open output file for write
135 fpOut
= fopen(argv
[1], "w+b");
137 printf ("efildrimage: Could not open output file %s\n", argv
[1]);
141 memset (&EfiLdrHeader
, 0, sizeof (EfiLdrHeader
));
142 memset (&EfiLdrImage
, 0, sizeof (EFILDR_IMAGE
) * (argc
- 2));
144 memcpy (&EfiLdrHeader
.Signature
, "EFIL", 4);
145 EfiLdrHeader
.FileLength
= sizeof(EFILDR_HEADER
) + sizeof(EFILDR_IMAGE
)*(argc
-2);
148 // Skip the file header first
150 fseek (fpOut
, EfiLdrHeader
.FileLength
, SEEK_SET
);
153 // copy all the input files to the output file
155 for(i
=2;i
<(ULONG
)argc
;i
++) {
157 // Copy the content of PeImage file to output file
159 fpIn
= fopen (argv
[i
], "rb");
161 printf ("efildrimage: Could not open input file %s\n", argv
[i
-2]);
164 filesize
= FCopyFile (fpIn
, fpOut
);
168 // And in the same time update the EfiLdrHeader and EfiLdrImage array
170 EfiLdrImage
[i
-2].Offset
= EfiLdrHeader
.FileLength
;
171 EfiLdrImage
[i
-2].Length
= filesize
;
172 strncpy (EfiLdrImage
[i
-2].FileName
, argv
[i
], sizeof (EfiLdrImage
[i
-2].FileName
) - 1);
173 EfiLdrHeader
.FileLength
+= filesize
;
174 EfiLdrHeader
.NumberOfImages
++;
178 // Write the image header to the output file finally
180 fseek (fpOut
, 0, SEEK_SET
);
181 fwrite (&EfiLdrHeader
, sizeof(EFILDR_HEADER
) , 1, fpOut
);
182 fwrite (&EfiLdrImage
, sizeof(EFILDR_IMAGE
)*(argc
-2), 1, fpOut
);
185 printf ("Created %s\n", argv
[1]);