hc
2024-03-25 edb30157bad0c0001c32b854271ace01d3b9a16a
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/** @file
  FIT based microcode shadow PEIM.
 
Copyright (c) 2020 - 2021, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
 
**/
 
#include <PiPei.h>
#include <Ppi/ShadowMicrocode.h>
#include <Library/PeiServicesLib.h>
#include <Library/HobLib.h>
#include <Library/DebugLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/MicrocodeLib.h>
#include <IndustryStandard/FirmwareInterfaceTable.h>
#include <Register/Intel/Microcode.h>
#include <Register/Intel/Cpuid.h>
#include <Guid/MicrocodeShadowInfoHob.h>
//
// Data structure for microcode patch information
//
typedef struct {
  UINTN    Address;
  UINTN    Size;
} MICROCODE_PATCH_INFO;
 
/**
  Shadow microcode update patches to memory.
 
  The function is used for shadowing microcode update patches to a continuous memory.
  It shall allocate memory buffer and only shadow the microcode patches for those
  processors specified by MicrocodeCpuId array. The checksum verification may be
  skiped in this function so the caller must perform checksum verification before
  using the microcode patches in returned memory buffer.
 
  @param[in]  This                 The PPI instance pointer.
  @param[in]  CpuIdCount           Number of elements in MicrocodeCpuId array.
  @param[in]  MicrocodeCpuId       A pointer to an array of EDKII_PEI_MICROCODE_CPU_ID
                                   structures.
  @param[out] BufferSize           Pointer to receive the total size of Buffer.
  @param[out] Buffer               Pointer to receive address of allocated memory
                                   with microcode patches data in it.
 
  @retval EFI_SUCCESS              The microcode has been shadowed to memory.
  @retval EFI_OUT_OF_RESOURCES     The operation fails due to lack of resources.
 
**/
EFI_STATUS
EFIAPI
ShadowMicrocode (
  IN  EDKII_PEI_SHADOW_MICROCODE_PPI        *This,
  IN  UINTN                                 CpuIdCount,
  IN  EDKII_PEI_MICROCODE_CPU_ID            *MicrocodeCpuId,
  OUT UINTN                                 *BufferSize,
  OUT VOID                                  **Buffer
  );
 
 
EDKII_PEI_SHADOW_MICROCODE_PPI   mPeiShadowMicrocodePpi = {
  ShadowMicrocode
};
 
 
EFI_PEI_PPI_DESCRIPTOR           mPeiShadowMicrocodePpiList[] = {
  {
    EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,
    &gEdkiiPeiShadowMicrocodePpiGuid,
    &mPeiShadowMicrocodePpi
  }
};
 
/**
  Actual worker function that shadows the required microcode patches into memory.
 
  @param[in]       Patches          The pointer to an array of information on
                                    the microcode patches that will be loaded
                                    into memory.
  @param[in]       PatchCount       The number of microcode patches that will
                                    be loaded into memory.
  @param[in]       TotalLoadSize    The total size of all the microcode patches
                                    to be loaded.
  @param[out] BufferSize            Pointer to receive the total size of Buffer.
  @param[out] Buffer                Pointer to receive address of allocated memory
                                    with microcode patches data in it.
**/
VOID
ShadowMicrocodePatchWorker (
  IN  MICROCODE_PATCH_INFO       *Patches,
  IN  UINTN                      PatchCount,
  IN  UINTN                      TotalLoadSize,
  OUT UINTN                      *BufferSize,
  OUT VOID                       **Buffer
  )
{
  UINTN                                     Index;
  VOID                                      *MicrocodePatchInRam;
  UINT8                                     *Walker;
  EDKII_MICROCODE_SHADOW_INFO_HOB           *MicrocodeShadowHob;
  UINTN                                     HobDataLength;
  UINT64                                    *MicrocodeAddressInMemory;
  EFI_MICROCODE_STORAGE_TYPE_FLASH_CONTEXT  *Flashcontext;
 
  ASSERT ((Patches != NULL) && (PatchCount != 0));
 
  //
  // Init microcode shadow info HOB content.
  //
  HobDataLength = sizeof (EDKII_MICROCODE_SHADOW_INFO_HOB) +
                  sizeof (UINT64) * PatchCount * 2;
  MicrocodeShadowHob  = AllocatePool (HobDataLength);
  if (MicrocodeShadowHob == NULL) {
    ASSERT (FALSE);
    return;
  }
  MicrocodeShadowHob->MicrocodeCount = PatchCount;
  CopyGuid (
    &MicrocodeShadowHob->StorageType,
    &gEdkiiMicrocodeStorageTypeFlashGuid
    );
  MicrocodeAddressInMemory = (UINT64 *) (MicrocodeShadowHob + 1);
  Flashcontext = (EFI_MICROCODE_STORAGE_TYPE_FLASH_CONTEXT *) (MicrocodeAddressInMemory + PatchCount);
 
  //
  // Allocate memory for microcode shadow operation.
  //
  MicrocodePatchInRam = AllocatePages (EFI_SIZE_TO_PAGES (TotalLoadSize));
  if (MicrocodePatchInRam == NULL) {
    ASSERT (FALSE);
    return;
  }
 
  //
  // Shadow all the required microcode patches into memory
  //
  for (Walker = MicrocodePatchInRam, Index = 0; Index < PatchCount; Index++) {
    CopyMem (
      Walker,
      (VOID *) Patches[Index].Address,
      Patches[Index].Size
      );
    MicrocodeAddressInMemory[Index] = (UINT64) (UINTN) Walker;
    Flashcontext->MicrocodeAddressInFlash[Index]  = (UINT64) Patches[Index].Address;
    Walker += Patches[Index].Size;
  }
 
  //
  // Update the microcode patch related fields in CpuMpData
  //
  *Buffer     = (VOID *) (UINTN) MicrocodePatchInRam;
  *BufferSize = TotalLoadSize;
 
  BuildGuidDataHob (
    &gEdkiiMicrocodeShadowInfoHobGuid,
    MicrocodeShadowHob,
    HobDataLength
    );
 
  DEBUG ((
    DEBUG_INFO,
    "%a: Required microcode patches have been loaded at 0x%lx, with size 0x%lx.\n",
    __FUNCTION__, *Buffer, *BufferSize
    ));
 
  return;
}
 
/**
  Check if FIT table content is valid according to FIT BIOS specification.
 
**/
BOOLEAN
IsValidFitTable (
  IN  UINT64               FitPointer
  )
{
  UINT64                            FitEnding;
  FIRMWARE_INTERFACE_TABLE_ENTRY    *FitEntry;
  UINT32                            EntryNum;
  UINT32                            Type0Count;
  UINT32                            Index;
 
  //
  // The entire FIT table must reside with in the firmware address range
  // of (4GB-16MB) to (4GB-40h).
  //
  if ((FitPointer < (SIZE_4GB - SIZE_16MB)) || (FitPointer >= (SIZE_4GB - 0x40))) {
    //
    // Invalid FIT address, treat it as no FIT table.
    //
    DEBUG ((DEBUG_ERROR, "Error: Invalid FIT pointer 0x%p.\n", FitPointer));
    return FALSE;
  }
 
  //
  // Check FIT header.
  //
  FitEntry = (FIRMWARE_INTERFACE_TABLE_ENTRY *) (UINTN) FitPointer;
  if ((FitEntry[0].Type != FIT_TYPE_00_HEADER) ||
      (FitEntry[0].Address != FIT_TYPE_00_SIGNATURE)) {
    DEBUG ((DEBUG_ERROR, "Error: Invalid FIT header.\n"));
    return FALSE;
  }
 
  //
  // Check FIT version.
  //
  if (FitEntry[0].Version != FIT_TYPE_VERSION) {
    DEBUG ((DEBUG_ERROR, "Error: Unsupported FIT version.\n"));
    return FALSE;
  }
 
  //
  // Check FIT ending address in valid range.
  //
  EntryNum = *(UINT32 *)(&FitEntry[0].Size[0]) & 0xFFFFFF;
  FitEnding = FitPointer + sizeof (FIRMWARE_INTERFACE_TABLE_ENTRY) * EntryNum;
  if (FitEnding  > (SIZE_4GB - 0x40)) {
    DEBUG ((DEBUG_ERROR, "Error: FIT table exceeds valid range.\n"));
    return FALSE;
  }
 
  //
  // Calculate FIT checksum if Checksum Valid bit is set.
  //
  if (FitEntry[0].C_V &&
      CalculateSum8 ((UINT8*) FitEntry, sizeof (FIRMWARE_INTERFACE_TABLE_ENTRY) * EntryNum) != 0) {
    DEBUG ((DEBUG_ERROR, "Error: Invalid FIT checksum.\n"));
    return FALSE;
  }
 
  //
  // Check type 0 entry count.
  //
  Type0Count = 0;
  for (Index = 0; Index < EntryNum; Index++) {
    if (FitEntry[Index].Type == FIT_TYPE_00_HEADER) {
      Type0Count++;
    }
  }
  if (Type0Count != 1) {
    DEBUG ((DEBUG_ERROR, "Error: There can be only one Type 0 entry in FIT.\n"));
    return FALSE;
  }
 
  return TRUE;
}
 
 
/**
  Shadow microcode update patches to memory.
 
  The function is used for shadowing microcode update patches to a continuous memory.
  It shall allocate memory buffer and only shadow the microcode patches for those
  processors specified by MicrocodeCpuId array. The checksum verification may be
  skiped in this function so the caller must perform checksum verification before
  using the microcode patches in returned memory buffer.
 
  @param[in]  This                 The PPI instance pointer.
  @param[in]  CpuIdCount           Number of elements in MicrocodeCpuId array.
  @param[in]  MicrocodeCpuId       A pointer to an array of EDKII_PEI_MICROCODE_CPU_ID
                                   structures.
  @param[out] BufferSize           Pointer to receive the total size of Buffer.
  @param[out] Buffer               Pointer to receive address of allocated memory
                                   with microcode patches data in it.
 
  @retval EFI_SUCCESS              The microcode has been shadowed to memory.
  @retval EFI_INVALID_PARAMETER    BufferSize or Buffer is NULL.
  @retval EFI_INVALID_PARAMETER    CpuIdCount not equal to 0 and MicrocodeCpuId is NULL.
  @retval EFI_NOT_FOUND            No valid microcode found.
  @retval EFI_OUT_OF_RESOURCES     The operation fails due to lack of resources.
 
**/
EFI_STATUS
EFIAPI
ShadowMicrocode (
  IN  EDKII_PEI_SHADOW_MICROCODE_PPI        *This,
  IN  UINTN                                 CpuIdCount,
  IN  EDKII_PEI_MICROCODE_CPU_ID            *MicrocodeCpuId,
  OUT UINTN                                 *BufferSize,
  OUT VOID                                  **Buffer
  )
{
  EFI_STATUS                        Status;
  UINT64                            FitPointer;
  FIRMWARE_INTERFACE_TABLE_ENTRY    *FitEntry;
  UINT32                            EntryNum;
  UINT32                            Index;
  MICROCODE_PATCH_INFO              *PatchInfoBuffer;
  UINTN                             MaxPatchNumber;
  CPU_MICROCODE_HEADER              *MicrocodeEntryPoint;
  UINTN                             PatchCount;
  UINTN                             TotalSize;
  UINTN                             TotalLoadSize;
 
  if (BufferSize == NULL || Buffer == NULL) {
    return EFI_INVALID_PARAMETER;
  }
 
  if (CpuIdCount != 0 && MicrocodeCpuId == NULL) {
    return EFI_INVALID_PARAMETER;
  }
 
  FitPointer = *(UINT64 *) (UINTN) FIT_POINTER_ADDRESS;
  if (!IsValidFitTable (FitPointer)) {
    return EFI_NOT_FOUND;
  }
 
  //
  // Calculate microcode entry number
  //
  FitEntry = (FIRMWARE_INTERFACE_TABLE_ENTRY *) (UINTN) FitPointer;
  EntryNum = *(UINT32 *)(&FitEntry[0].Size[0]) & 0xFFFFFF;
  MaxPatchNumber = 0;
  for (Index = 0; Index < EntryNum; Index++) {
    if (FitEntry[Index].Type == FIT_TYPE_01_MICROCODE) {
      MaxPatchNumber++;
    }
  }
  if (MaxPatchNumber == 0) {
    return EFI_NOT_FOUND;
  }
 
  PatchInfoBuffer = AllocatePool (MaxPatchNumber * sizeof (MICROCODE_PATCH_INFO));
  if (PatchInfoBuffer == NULL) {
    return EFI_OUT_OF_RESOURCES;
  }
 
  if (FeaturePcdGet (PcdShadowAllMicrocode)) {
    MicrocodeCpuId = NULL;
    CpuIdCount     = 0;
  }
 
  //
  // Fill up microcode patch info buffer according to FIT table.
  //
  PatchCount = 0;
  TotalLoadSize = 0;
  for (Index = 0; Index < EntryNum; Index++) {
    if (FitEntry[Index].Type == FIT_TYPE_01_MICROCODE) {
      MicrocodeEntryPoint = (CPU_MICROCODE_HEADER *) (UINTN) FitEntry[Index].Address;
      TotalSize = GetMicrocodeLength (MicrocodeEntryPoint);
      if (IsValidMicrocode (MicrocodeEntryPoint, TotalSize, 0, MicrocodeCpuId, CpuIdCount, FALSE)) {
        PatchInfoBuffer[PatchCount].Address = (UINTN) MicrocodeEntryPoint;
        PatchInfoBuffer[PatchCount].Size    = TotalSize;
        TotalLoadSize += TotalSize;
        PatchCount++;
      }
    }
  }
 
  if (PatchCount != 0) {
    DEBUG ((
      DEBUG_INFO,
      "%a: 0x%x microcode patches will be loaded into memory, with size 0x%x.\n",
      __FUNCTION__, PatchCount, TotalLoadSize
      ));
 
    ShadowMicrocodePatchWorker (PatchInfoBuffer, PatchCount, TotalLoadSize, BufferSize, Buffer);
    Status = EFI_SUCCESS;
  } else {
    Status = EFI_NOT_FOUND;
  }
 
  FreePool (PatchInfoBuffer);
  return Status;
}
 
 
/**
  Platform Init PEI module entry point
 
  @param[in]  FileHandle           Not used.
  @param[in]  PeiServices          General purpose services available to every PEIM.
 
  @retval     EFI_SUCCESS          The function completes successfully
  @retval     EFI_OUT_OF_RESOURCES Insufficient resources to create database
**/
EFI_STATUS
EFIAPI
ShadowMicrocodePeimInit (
  IN       EFI_PEI_FILE_HANDLE  FileHandle,
  IN CONST EFI_PEI_SERVICES     **PeiServices
  )
{
  EFI_STATUS                       Status;
 
  //
  // Install EDKII Shadow Microcode PPI
  //
  Status = PeiServicesInstallPpi(mPeiShadowMicrocodePpiList);
  ASSERT_EFI_ERROR (Status);
 
  return Status;
}