summaryrefslogtreecommitdiffstats
path: root/BaseTools/Source/C/FCE/BinaryCreate.c
blob: 2f36bc2ef34bd706fb6d25eaa086936658436dae (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
/** @file

 The API to create the binary.

 Copyright (c) 2011-2019, Intel Corporation. All rights reserved.<BR>
 SPDX-License-Identifier: BSD-2-Clause-Patent

**/

#include "BinaryCreate.h"
#ifndef __GNUC__
#define GENSEC_RAW      "GenSec -s %s \"%s\" -o \"%s\" > NUL"
#else
#define GENSEC_RAW      "GenSec -s %s \"%s\" -o \"%s\" > /dev/null"
#endif

//
// The guid is to for FFS of BFV.
//
EFI_GUID gEfiFfsBfvForMultiPlatformGuid = EFI_FFS_BFV_FOR_MULTIPLATFORM_GUID;
EFI_GUID gEfiFfsBfvForMultiPlatformGuid2 = EFI_FFS_BFV_FOR_MULTIPLATFORM_GUID2;

/**
  Convert a GUID to a string.

  @param[in]   Guid    Pointer to GUID to print.

  @return The string after convert.
**/
static
CHAR8 *
LibBfmGuidToStr (
  IN  EFI_GUID  *Guid
)
{
  CHAR8 * Buffer;

  Buffer = NULL;

  if (Guid == NULL) {
    return NULL;
  }

  Buffer = (CHAR8 *) malloc (36 + 1);

  if (Buffer == NULL) {
    return NULL;
  }
  memset (Buffer, '\0', 36 + 1);

  sprintf (
      Buffer,
      "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
      Guid->Data1,
      Guid->Data2,
      Guid->Data3,
      Guid->Data4[0],
      Guid->Data4[1],
      Guid->Data4[2],
      Guid->Data4[3],
      Guid->Data4[4],
      Guid->Data4[5],
      Guid->Data4[6],
      Guid->Data4[7]
      );

  return Buffer;
}

/**
  Create the Ras section in FFS

  @param[in]   InputFilePath   .efi file, it's optional unless process PE/TE section.
  @param[in]   OutputFilePath  .te or .pe file

  @retval EFI_SUCCESS

**/
EFI_STATUS
CreateRawSection (
  IN CHAR8*     InputFilePath,
  IN CHAR8*     OutputFilePath
  )
{
  INT32        ReturnValue;
  CHAR8*       SystemCommand;

  SystemCommand             = NULL;
  SystemCommand = malloc (
    strlen (GENSEC_RAW) +
    strlen ("EFI_SECTION_RAW") +
    strlen (InputFilePath) +
    strlen (OutputFilePath) +
    1
  );
  if (NULL == SystemCommand) {
    return EFI_OUT_OF_RESOURCES;
  }
  sprintf (
    SystemCommand,
    GENSEC_RAW,
    "EFI_SECTION_RAW",
    InputFilePath,
    OutputFilePath
  );
  ReturnValue = system (SystemCommand);
  free(SystemCommand);

  if (ReturnValue != 0) {
    printf ("Error. Call GenSec failed.\n");
    return EFI_ABORTED;
  }
  return EFI_SUCCESS;
}

/**
  Create the Ras type of FFS

  @param[in]   InputFilePath   .efi file, it's optional unless process PE/TE section.
  @param[in]   OutputFilePath  .te or .pe file

  @retval EFI_SUCCESS

**/
EFI_STATUS
CreateRawFfs (
  IN CHAR8**    InputFilePaths,
  IN CHAR8*     OutputFilePath,
  IN BOOLEAN    SizeOptimized
  )
{
  INT32        ReturnValue;
  CHAR8*       SystemCommandFormatString;
  CHAR8*       SystemCommand;
  CHAR8*       GuidStr;
  CHAR8*       FilePathFormatStr;
  CHAR8*       FilePathStr;
  UINT32       Index;
  UINT32       StrLen;
  UINT32       Size;

  SystemCommand    = NULL;
  GuidStr          = NULL;
  FilePathStr      = NULL;
  StrLen           = 0;

  FilePathFormatStr = " -i \"";

  for (Index = 0; InputFilePaths[Index] != NULL; Index++) {
    Size = strlen (FilePathFormatStr) + strlen (InputFilePaths[Index]) + 2; // 2 menas "" "
    if (FilePathStr == NULL) {
      FilePathStr = malloc (Size);
      if (NULL == FilePathStr) {
        return EFI_OUT_OF_RESOURCES;
      }
    } else {
      FilePathStr = realloc (FilePathStr, StrLen + Size);
      if (NULL == FilePathStr) {
        return EFI_OUT_OF_RESOURCES;
      }
    }
    memset (FilePathStr + StrLen, ' ', Size);
    memcpy (FilePathStr + StrLen, FilePathFormatStr, strlen(FilePathFormatStr));
    memcpy(FilePathStr + StrLen + strlen(FilePathFormatStr), InputFilePaths[Index], strlen(InputFilePaths[Index]));
    StrLen += Size;
    *(FilePathStr + StrLen - 2) = '\"';
  }
   if (FilePathStr == NULL) {
    return EFI_ABORTED;
  }
  *(FilePathStr + StrLen - 1)= '\0';


  if (SizeOptimized) {
    GuidStr = LibBfmGuidToStr(&gEfiFfsBfvForMultiPlatformGuid2);
  } else {
    GuidStr  = LibBfmGuidToStr(&gEfiFfsBfvForMultiPlatformGuid);
  }
  if (NULL == GuidStr) {
    free (FilePathStr);
    return EFI_OUT_OF_RESOURCES;
  }
  SystemCommandFormatString = "GenFfs -t %s %s -g %s -o \"%s\"";
  SystemCommand = malloc (
    strlen (SystemCommandFormatString) +
    strlen ("EFI_FV_FILETYPE_FREEFORM") +
    strlen (FilePathStr) +
    strlen (GuidStr) +
    strlen (OutputFilePath) +
    1
    );
  if (NULL == SystemCommand) {
    free (GuidStr);
    free (FilePathStr);
    return EFI_OUT_OF_RESOURCES;
  }
  sprintf (
    SystemCommand,
    "GenFfs -t %s %s -g %s -o \"%s\"",
    "EFI_FV_FILETYPE_FREEFORM",// -t
    FilePathStr,               // -i
    GuidStr,                   // -g
    OutputFilePath             // -o
    );
  ReturnValue = system (SystemCommand);
  free(SystemCommand);
  free (FilePathStr);
  free (GuidStr);

  if (ReturnValue != 0) {
    printf ("Error. Call GenFfs failed.\n");
    return EFI_ABORTED;
  }
  return EFI_SUCCESS;
}