summaryrefslogtreecommitdiff
path: root/Platform/Intel/MinPlatformPkg/Library/PeiLib/PeiLib.c
blob: 96bf91c408ec260bb3c81149418601c05da821d7 (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
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
/** @file

Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials are licensed and made available under
the terms and conditions of the BSD License that accompanies this distribution.
The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php.

THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

**/

#include <PiPei.h>
#include <Library/DebugLib.h>
#include <Library/PeiServicesLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Ppi/ReadOnlyVariable2.h>

/**
  Returns the status whether get the variable success. The function retrieves 
  variable  through the ReadOnlyVariable2 PPI GetVariable().  The 
  returned buffer is allocated using AllocatePool().  The caller is responsible
  for freeing this buffer with FreePool().

  If Name  is NULL, then ASSERT().
  If Guid  is NULL, then ASSERT().
  If Value is NULL, then ASSERT().

  @param[in]  Name  The pointer to a Null-terminated Unicode string.
  @param[in]  Guid  The pointer to an EFI_GUID structure
  @param[out] Value The buffer point saved the variable info.
  @param[out] Size  The buffer size of the variable.

  @return EFI_OUT_OF_RESOURCES      Allocate buffer failed.
  @return EFI_SUCCESS               Find the specified variable.
  @return Others Errors             Return errors from call to gRT->GetVariable.

**/
EFI_STATUS
EFIAPI
PeiGetVariable (
  IN CONST CHAR16    *Name,
  IN CONST EFI_GUID  *Guid,
  OUT VOID           **Value,
  OUT UINTN          *Size  OPTIONAL
  )
{
  EFI_STATUS                        Status;
  EFI_PEI_READ_ONLY_VARIABLE2_PPI   *VariableServices;
  UINTN                             VariableSize;
  VOID                              *VariableData;

  ASSERT (Name != NULL);
  ASSERT (Guid != NULL);
  ASSERT (Value != NULL);

  Status = PeiServicesLocatePpi (
             &gEfiPeiReadOnlyVariable2PpiGuid,
             0,
             NULL,
             &VariableServices
             );
  ASSERT_EFI_ERROR (Status);
  if (EFI_ERROR(Status)) {
    return EFI_NOT_READY;
  }

  VariableSize = 0;
  VariableData = NULL;
  Status = VariableServices->GetVariable (
                               VariableServices,
                               Name,
                               Guid,
                               NULL,
                               &VariableSize,
                               VariableData
                               );
  if (Status != EFI_BUFFER_TOO_SMALL) {
    return Status;
  }

  if ((Size == NULL) || (*Size == 0) || (*Value == NULL)) {
    VariableData = AllocatePool (VariableSize);
    ASSERT (VariableData != NULL);
    if (VariableData == NULL) {
      return EFI_OUT_OF_RESOURCES;
    }
  } else {
    if (*Size < VariableSize) {
      return EFI_BUFFER_TOO_SMALL;
    }
    VariableData = *Value;
  }

  Status = VariableServices->GetVariable (
                               VariableServices,
                               Name,
                               Guid,
                               NULL,
                               &VariableSize,
                               VariableData
                               );
  if (EFI_ERROR (Status)) {
    FreePool (VariableData);
    VariableData = NULL;
    VariableSize = 0;
  }

  *Value = VariableData;
  if (Size != NULL) {
    *Size = VariableSize;
  }
  return Status;
}

EFI_PEI_FILE_HANDLE
InternalGetFfsHandleFromAnyFv (
  IN CONST  EFI_GUID           *NameGuid
  )
{
  EFI_STATUS                 Status;
  UINTN                      FvInstance;
  EFI_PEI_FV_HANDLE          FvHandle;
  EFI_PEI_FILE_HANDLE        FfsHandle;

  FvInstance  = 0;
  FvHandle    = NULL;
  FfsHandle   = NULL;
  while (TRUE) {
    Status = PeiServicesFfsFindNextVolume (FvInstance, &FvHandle);
    if (EFI_ERROR(Status)) {
      break;
    }
    Status = PeiServicesFfsFindFileByName (NameGuid, FvHandle, &FfsHandle);
    if (Status == EFI_SUCCESS) {
      break;
    }
    FfsHandle = NULL;
    FvInstance ++;
  }

  return FfsHandle;
}

/**
  Finds the file in any FV and gets file Address and Size
  
  @param[in]  NameGuid             File GUID
  @param[out] Address              Pointer to the File Address
  @param[out] Size                 Pointer to File Size

  @retval EFI_SUCCESS                Successfull in reading the file from any FV
**/
EFI_STATUS
EFIAPI
PeiGetFfsFromAnyFv (
  IN CONST  EFI_GUID           *NameGuid,
  OUT       VOID               **Address,
  OUT       UINTN              *Size
  )
{
  EFI_STATUS                 Status;
  EFI_FV_FILE_INFO           FvFileInfo;
  EFI_PEI_FILE_HANDLE        FfsHandle;

  FfsHandle = InternalGetFfsHandleFromAnyFv (NameGuid);
  if (FfsHandle == NULL) {
    return EFI_NOT_FOUND;
  }
  
  //
  // Need get size
  //
  Status = PeiServicesFfsGetFileInfo (FfsHandle, &FvFileInfo);
  if (EFI_ERROR(Status)) {
    return EFI_NOT_FOUND;
  }
  *Address = FvFileInfo.Buffer;
  *Size    = FvFileInfo.BufferSize;

  return Status;
}

/**
  Get Section buffer pointer by SectionType and SectionInstance.

  @param[in]   SectionBuffer     The buffer of section
  @param[in]   SectionBufferSize The size of SectionBuffer in bytes
  @param[in]   SectionType       The SectionType of Section to be found
  @param[in]   SectionInstance   The Instance of Section to be found
  @param[out]  OutSectionBuffer  The section found, including SECTION_HEADER
  @param[out]  OutSectionSize    The size of section found, including SECTION_HEADER
  
  @retval EFI_SUCCESS                Successfull in reading the section from FV
**/
EFI_STATUS
InternalGetSectionByType (
  IN VOID                  *SectionBuffer,
  IN UINTN                 SectionBufferSize,
  IN EFI_SECTION_TYPE      SectionType,
  IN UINTN                 SectionInstance,
  OUT VOID                 **OutSectionBuffer,
  OUT UINTN                *OutSectionSize
  )
{
  EFI_COMMON_SECTION_HEADER             *SectionHeader;
  UINTN                                 SectionSize;
  UINTN                                 Instance;

  //
  // Find Section
  //
  SectionHeader = SectionBuffer;

  Instance = 0;
  while ((UINTN)SectionHeader < (UINTN)SectionBuffer + SectionBufferSize) {
    if (IS_SECTION2(SectionHeader)) {
      SectionSize = SECTION2_SIZE(SectionHeader);
    } else {
      SectionSize = SECTION_SIZE(SectionHeader);
    }

    if (SectionHeader->Type == SectionType) {
      if (Instance == SectionInstance) {
        *OutSectionBuffer = (UINT8 *)SectionHeader;
        *OutSectionSize = SectionSize;
        return EFI_SUCCESS;
      } else {
        Instance++;
      }
    } else {
      //
      // Skip other section type
      //
    }

    //
    // Next Section
    //
    SectionHeader = (EFI_COMMON_SECTION_HEADER *)((UINTN)SectionHeader + ALIGN_VALUE(SectionSize, 4));
  }

  return EFI_NOT_FOUND;
}

/**
  Finds the section in any FV and gets section Address and Size

  @param[in]  NameGuid             File GUID
  @param[in]  SectionType          The SectionType of Section to be found
  @param[in]  SectionInstance      The Instance of Section to be found
  @param[out] Address              Pointer to the section Address
  @param[out] Size                 Pointer to section Size

  @retval EFI_SUCCESS                Successfull in reading the section from any FV
**/
EFI_STATUS
EFIAPI
PeiGetSectionFromAnyFv  (
  IN CONST  EFI_GUID           *NameGuid,
  IN        EFI_SECTION_TYPE   SectionType,
  IN        UINTN              SectionInstance,
  OUT       VOID               **Address,
  OUT       UINTN              *Size
  )
{
  EFI_STATUS                 Status;
  EFI_COMMON_SECTION_HEADER  *Section;
  VOID                       *FileBuffer;
  UINTN                      FileBufferSize;
  
  Status = PeiGetFfsFromAnyFv (NameGuid, &FileBuffer, &FileBufferSize);
  if (EFI_ERROR(Status)) {
    return Status;
  }

  Status = InternalGetSectionByType (FileBuffer, FileBufferSize, SectionType, SectionInstance, Address, Size);
  if (EFI_ERROR(Status)) {
    return Status;
  }
  Section = *Address;
  if (IS_SECTION2(Section)) {
    ASSERT(SECTION2_SIZE(Section) > 0x00FFFFFF);
    *Size = SECTION2_SIZE(Section) - sizeof (EFI_COMMON_SECTION_HEADER2);
    *Address = (UINT8 *)*Address + sizeof (EFI_COMMON_SECTION_HEADER2);
  } else {
    *Size = SECTION_SIZE(Section) - sizeof (EFI_COMMON_SECTION_HEADER);
    *Address = (UINT8 *)*Address + sizeof (EFI_COMMON_SECTION_HEADER);
  }
  
  return EFI_SUCCESS;
}