]> git.proxmox.com Git - mirror_edk2.git/blob - EdkCompatibilityPkg/Sample/Tools/Source/EfildrImage/efildrimage.c
comment CpuRuntimeDxe driver to not break Nt32Pkg build
[mirror_edk2.git] / EdkCompatibilityPkg / Sample / Tools / Source / EfildrImage / efildrimage.c
1 /*++
2
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
8
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.
11
12 Module Name:
13
14 efildrimage.c
15
16 Abstract:
17
18 Creates and EFILDR image.
19 This tool combines several PE Image files together using following format denoted as EBNF:
20 FILE := EFILDR_HEADER
21 EFILDR_IMAGE +
22 <PeImageFileContent> +
23 The order of EFILDR_IMAGE is same as the order of placing PeImageFileContent.
24
25 Revision History
26
27 --*/
28
29
30 #include <windows.h>
31 #include <stdio.h>
32 #include "Tiano.h"
33
34 #define MAX_PE_IMAGES 63
35 #define FILE_TYPE_FIXED_LOADER 0
36 #define FILE_TYPE_RELOCATABLE_PE_IMAGE 1
37
38 typedef struct {
39 UINT32 CheckSum;
40 UINT32 Offset;
41 UINT32 Length;
42 UINT8 FileName[52];
43 } EFILDR_IMAGE;
44
45 typedef struct {
46 UINT32 Signature;
47 UINT32 HeaderCheckSum;
48 UINT32 FileLength;
49 UINT32 NumberOfImages;
50 } EFILDR_HEADER;
51
52
53
54 VOID
55 Usage (
56 VOID
57 )
58 {
59 printf ("Usage: EfiLdrImage OutImage LoaderImage PeImage1 PeImage2 ... PeImageN");
60 exit (1);
61 }
62
63 ULONG
64 FCopyFile (
65 FILE *in,
66 FILE *out
67 )
68 /*++
69 Routine Description:
70 Write all the content of input file to output file.
71
72 Arguments:
73 in - input file pointer
74 out - output file pointer
75
76 Return:
77 ULONG : file size of input file
78 --*/
79 {
80 ULONG filesize, offset, length;
81 UCHAR Buffer[8*1024];
82
83 fseek (in, 0, SEEK_END);
84 filesize = ftell(in);
85
86 fseek (in, 0, SEEK_SET);
87
88 offset = 0;
89 while (offset < filesize) {
90 length = sizeof(Buffer);
91 if (filesize-offset < length) {
92 length = filesize-offset;
93 }
94
95 fread (Buffer, length, 1, in);
96 fwrite (Buffer, length, 1, out);
97 offset += length;
98 }
99
100 return filesize;
101 }
102
103
104 int
105 main (
106 int argc,
107 char *argv[]
108 )
109 /*++
110
111 Routine Description:
112
113
114 Arguments:
115
116
117 Returns:
118
119
120 --*/
121 {
122 ULONG i;
123 ULONG filesize;
124 FILE *fpIn, *fpOut;
125 EFILDR_HEADER EfiLdrHeader;
126 EFILDR_IMAGE EfiLdrImage[MAX_PE_IMAGES];
127
128 if (argc < 4) {
129 Usage();
130 }
131
132 //
133 // Open output file for write
134 //
135 fpOut = fopen(argv[1], "w+b");
136 if (!fpOut) {
137 printf ("efildrimage: Could not open output file %s\n", argv[1]);
138 exit(1);
139 }
140
141 memset (&EfiLdrHeader, 0, sizeof (EfiLdrHeader));
142 memset (&EfiLdrImage, 0, sizeof (EFILDR_IMAGE) * (argc - 2));
143
144 memcpy (&EfiLdrHeader.Signature, "EFIL", 4);
145 EfiLdrHeader.FileLength = sizeof(EFILDR_HEADER) + sizeof(EFILDR_IMAGE)*(argc-2);
146
147 //
148 // Skip the file header first
149 //
150 fseek (fpOut, EfiLdrHeader.FileLength, SEEK_SET);
151
152 //
153 // copy all the input files to the output file
154 //
155 for(i=2;i<(ULONG)argc;i++) {
156 //
157 // Copy the content of PeImage file to output file
158 //
159 fpIn = fopen (argv[i], "rb");
160 if (!fpIn) {
161 printf ("efildrimage: Could not open input file %s\n", argv[i-2]);
162 exit(1);
163 }
164 filesize = FCopyFile (fpIn, fpOut);
165 fclose(fpIn);
166
167 //
168 // And in the same time update the EfiLdrHeader and EfiLdrImage array
169 //
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++;
175 }
176
177 //
178 // Write the image header to the output file finally
179 //
180 fseek (fpOut, 0, SEEK_SET);
181 fwrite (&EfiLdrHeader, sizeof(EFILDR_HEADER) , 1, fpOut);
182 fwrite (&EfiLdrImage , sizeof(EFILDR_IMAGE)*(argc-2), 1, fpOut);
183
184 fclose (fpOut);
185 printf ("Created %s\n", argv[1]);
186 return 0;
187 }
188