]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/C/FCE/BinaryCreate.c
BaseTools: Fix various typos
[mirror_edk2.git] / BaseTools / Source / C / FCE / BinaryCreate.c
1 /** @file
2
3 The API to create the binary.
4
5 Copyright (c) 2011-2019, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10 #include "BinaryCreate.h"
11 #ifndef __GNUC__
12 #define GENSEC_RAW "GenSec -s %s \"%s\" -o \"%s\" > NUL"
13 #else
14 #define GENSEC_RAW "GenSec -s %s \"%s\" -o \"%s\" > /dev/null"
15 #endif
16
17 //
18 // The guid is to for FFS of BFV.
19 //
20 EFI_GUID gEfiFfsBfvForMultiPlatformGuid = EFI_FFS_BFV_FOR_MULTIPLATFORM_GUID;
21 EFI_GUID gEfiFfsBfvForMultiPlatformGuid2 = EFI_FFS_BFV_FOR_MULTIPLATFORM_GUID2;
22
23 /**
24 Convert a GUID to a string.
25
26 @param[in] Guid Pointer to GUID to print.
27
28 @return The string after convert.
29 **/
30 static
31 CHAR8 *
32 LibBfmGuidToStr (
33 IN EFI_GUID *Guid
34 )
35 {
36 CHAR8 * Buffer;
37
38 Buffer = NULL;
39
40 if (Guid == NULL) {
41 return NULL;
42 }
43
44 Buffer = (CHAR8 *) malloc (36 + 1);
45
46 if (Buffer == NULL) {
47 return NULL;
48 }
49 memset (Buffer, '\0', 36 + 1);
50
51 sprintf (
52 Buffer,
53 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
54 Guid->Data1,
55 Guid->Data2,
56 Guid->Data3,
57 Guid->Data4[0],
58 Guid->Data4[1],
59 Guid->Data4[2],
60 Guid->Data4[3],
61 Guid->Data4[4],
62 Guid->Data4[5],
63 Guid->Data4[6],
64 Guid->Data4[7]
65 );
66
67 return Buffer;
68 }
69
70 /**
71 Create the Ras section in FFS
72
73 @param[in] InputFilePath .efi file, it's optional unless process PE/TE section.
74 @param[in] OutputFilePath .te or .pe file
75
76 @retval EFI_SUCCESS
77
78 **/
79 EFI_STATUS
80 CreateRawSection (
81 IN CHAR8* InputFilePath,
82 IN CHAR8* OutputFilePath
83 )
84 {
85 INT32 ReturnValue;
86 CHAR8* SystemCommand;
87
88 SystemCommand = NULL;
89 SystemCommand = malloc (
90 strlen (GENSEC_RAW) +
91 strlen ("EFI_SECTION_RAW") +
92 strlen (InputFilePath) +
93 strlen (OutputFilePath) +
94 1
95 );
96 if (NULL == SystemCommand) {
97 return EFI_OUT_OF_RESOURCES;
98 }
99 sprintf (
100 SystemCommand,
101 GENSEC_RAW,
102 "EFI_SECTION_RAW",
103 InputFilePath,
104 OutputFilePath
105 );
106 ReturnValue = system (SystemCommand);
107 free(SystemCommand);
108
109 if (ReturnValue != 0) {
110 printf ("Error. Call GenSec failed.\n");
111 return EFI_ABORTED;
112 }
113 return EFI_SUCCESS;
114 }
115
116 /**
117 Create the Ras type of FFS
118
119 @param[in] InputFilePath .efi file, it's optional unless process PE/TE section.
120 @param[in] OutputFilePath .te or .pe file
121
122 @retval EFI_SUCCESS
123
124 **/
125 EFI_STATUS
126 CreateRawFfs (
127 IN CHAR8** InputFilePaths,
128 IN CHAR8* OutputFilePath,
129 IN BOOLEAN SizeOptimized
130 )
131 {
132 INT32 ReturnValue;
133 CHAR8* SystemCommandFormatString;
134 CHAR8* SystemCommand;
135 CHAR8* GuidStr;
136 CHAR8* FilePathFormatStr;
137 CHAR8* FilePathStr;
138 UINT32 Index;
139 UINT32 StrLen;
140 UINT32 Size;
141
142 SystemCommand = NULL;
143 GuidStr = NULL;
144 FilePathStr = NULL;
145 StrLen = 0;
146
147 FilePathFormatStr = " -i \"";
148
149 for (Index = 0; InputFilePaths[Index] != NULL; Index++) {
150 Size = strlen (FilePathFormatStr) + strlen (InputFilePaths[Index]) + 2; // 2 menas "" "
151 if (FilePathStr == NULL) {
152 FilePathStr = malloc (Size);
153 if (NULL == FilePathStr) {
154 return EFI_OUT_OF_RESOURCES;
155 }
156 } else {
157 FilePathStr = realloc (FilePathStr, StrLen + Size);
158 if (NULL == FilePathStr) {
159 return EFI_OUT_OF_RESOURCES;
160 }
161 }
162 memset (FilePathStr + StrLen, ' ', Size);
163 memcpy (FilePathStr + StrLen, FilePathFormatStr, strlen(FilePathFormatStr));
164 memcpy(FilePathStr + StrLen + strlen(FilePathFormatStr), InputFilePaths[Index], strlen(InputFilePaths[Index]));
165 StrLen += Size;
166 *(FilePathStr + StrLen - 2) = '\"';
167 }
168 if (FilePathStr == NULL) {
169 return EFI_ABORTED;
170 }
171 *(FilePathStr + StrLen - 1)= '\0';
172
173
174 if (SizeOptimized) {
175 GuidStr = LibBfmGuidToStr(&gEfiFfsBfvForMultiPlatformGuid2);
176 } else {
177 GuidStr = LibBfmGuidToStr(&gEfiFfsBfvForMultiPlatformGuid);
178 }
179 if (NULL == GuidStr) {
180 free (FilePathStr);
181 return EFI_OUT_OF_RESOURCES;
182 }
183 SystemCommandFormatString = "GenFfs -t %s %s -g %s -o \"%s\"";
184 SystemCommand = malloc (
185 strlen (SystemCommandFormatString) +
186 strlen ("EFI_FV_FILETYPE_FREEFORM") +
187 strlen (FilePathStr) +
188 strlen (GuidStr) +
189 strlen (OutputFilePath) +
190 1
191 );
192 if (NULL == SystemCommand) {
193 free (GuidStr);
194 free (FilePathStr);
195 return EFI_OUT_OF_RESOURCES;
196 }
197 sprintf (
198 SystemCommand,
199 "GenFfs -t %s %s -g %s -o \"%s\"",
200 "EFI_FV_FILETYPE_FREEFORM",// -t
201 FilePathStr, // -i
202 GuidStr, // -g
203 OutputFilePath // -o
204 );
205 ReturnValue = system (SystemCommand);
206 free(SystemCommand);
207 free (FilePathStr);
208 free (GuidStr);
209
210 if (ReturnValue != 0) {
211 printf ("Error. Call GenFfs failed.\n");
212 return EFI_ABORTED;
213 }
214 return EFI_SUCCESS;
215 }
216