summaryrefslogtreecommitdiff
path: root/Core/EM/SecurityPkg/SecFlashUpd/IsSecRecovery.c
blob: 112f6550a21d2afc03f2ce7791ec3b64d7282e41 (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
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
//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (C)Copyright 1985-2014, American Megatrends, Inc.         **
//**                                                                  **
//**                       All Rights Reserved.                       **
//**                                                                  **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093        **
//**                                                                  **
//**                       Phone: (770)-246-8600                      **
//**                                                                  **
//**********************************************************************
//**********************************************************************
//**********************************************************************
// $Header: /Alaska/SOURCE/Modules/SecureFlashPkg/FlashUpdate/IsSecRecovery.c 18    10/29/14 5:11p Alexp $
//
// $Revision: 18 $
//
// $Date: 10/29/14 5:11p $
//**********************************************************************
// Revision History
// ----------------
// $Log: /Alaska/SOURCE/Modules/SecureFlashPkg/FlashUpdate/IsSecRecovery.c $
// 
// 18    10/29/14 5:11p Alexp
// GetFlashUpdateVar()  - set default value of CounterHi= -1
// for cases when MC variable is available only in pre-boot.
// 
// 15    3/18/14 3:02p Alexp
// year '2014' in file hdr & ftr
// 
// 14    8/12/13 4:36p Alexp
// fix ChangeBootModeAfterEndofMrc() to change BootMode and install PPI if
// CApsule Upd is pending 
// 
// 13    4/01/13 9:37a Alexp
// exclude capsule recovery code if FWCAPSULE_RECOVERY_SUPPORT is 0
// 
// 12    2/27/13 6:43p Alexp
// GetFlashUpdateVar() to check for AMI Capsule Variable 
// if Ami FwCapsule is pending and update boot mode to
// BOOT_ON_FLASH_UPDATE
// 
// 11    7/09/12 5:30p Alexp
//  IsFlashUpdateS3Capsule
//  Install Capsule Recovery Event whether by checking FlashUpd variable 
//  or if BOOT_ON_FLASH_UPDATE.  It may've already been set by Capsule PEI
// 
// 
// 9     6/01/12 10:44a Alexp
// EIP74625:New Capsule PPI required by latest Intel's MRC code
//     New PPI GUID; 
//     Capsule_2_0 Mailbox does not need extra Cap Hdr
// EIP90678: MonotonicCounter variable guid changed
//    Use AMI_GLOBAL_VARIABLE_GUID
// 
// 7     12/08/11 4:51p Alexp
// Change method of calling IsSecRecovery init routine.
// Replaced IsSecRecovery PEI with function call IsFlashUpdateRecovery
// from PeiCoreInitialize
// 
// 6     11/10/11 7:10p Alexp
// minor optimizations 
// 
// 5     11/03/11 6:41p Alexp
// Add provision to build file as .FFS with new function to install
// callback on gAmiPeiAfterMrcGuid event
// Update BootMode to BOOT_ON_FLASH_UPDATE if Capsule update is detected
// 
// 4     10/17/11 11:33a Alexp
// Replace old method to detect FlashUpd with single IsFlashUpdate() hook
// to be called from SbPei.c ->GetBootMode
// 
// 3     10/11/11 12:23p Alexp
// removed comments
// 
// 2     7/20/11 7:16p Alexp
// remove dependency on Capsule module
// 
// 1     7/01/11 4:39p Alexp
// 
// 
//<AMI_FHDR_START>
//----------------------------------------------------------------------
//
// Name: IsSecRecovery.c - PEI recovery services
//
// Description:   File contains hook to determine if BootMode needs to follow 
//                recovery path due to pending Flash Update
//
//----------------------------------------------------------------------
//<AMI_FHDR_END>
// Module specific Includes
#include "Efi.h"
#include "Pei.h"
#include "token.h"
#include <AmiPeiLib.h>
#include <Hob.h>
#include <Ppi\ReadOnlyVariable.h>
#include <FlashUpd.h>
#if  FWCAPSULE_RECOVERY_SUPPORT == 1
#include "AmiCertificate.h"
#include <Capsule.h>

// Definitions
static EFI_GUID gFWCapsuleGuid          = APTIO_FW_CAPSULE_GUID;
static EFI_GUID gEfiCapsuleVendorGuid   = EFI_CAPSULE_AMI_GUID;
#endif
static EFI_GUID gFlashUpdBootModeGuid   = EFI_PEI_BOOT_IN_FLASH_UPDATE_MODE_PEIM_PPI;
// PPI that are installed
static EFI_PEI_PPI_DESCRIPTOR mFlashUpdateModePpi[] =
  {
   (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
    &gFlashUpdBootModeGuid,
    NULL
};

static EFI_GUID gPeiReadOnlyVariablePpiGuid = EFI_PEI_READ_ONLY_VARIABLE_PPI_GUID;
static EFI_GUID gFlashUpdGuid               = FLASH_UPDATE_GUID;
// must be defined in AmiLib.h (Core 4.6.5.4 +)
#if defined(AMI_GLOBAL_VARIABLE_GUID)
static EFI_GUID guidVar                     = AMI_GLOBAL_VARIABLE_GUID;    
#else
static EFI_GUID guidVar                     = EFI_GLOBAL_VARIABLE; 
#endif

#if  FWCAPSULE_RECOVERY_SUPPORT == 1
///////////////////////////////////////////////////////
// {64C96700-6B4C-480C-A3E1-B8BDE8F602B2}
#define AMI_PEI_AFTER_MRC_GUID \
 {0x64c96700, 0x6b4c, 0x480c, 0xa3, 0xe1, 0xb8, 0xbd, 0xe8, 0xf6, 0x2, 0xb2}

static EFI_GUID gAmiPeiAfterMrcGuid         = AMI_PEI_AFTER_MRC_GUID;

EFI_STATUS ChangeBootModeAfterEndofMrc (
    IN EFI_PEI_SERVICES         **PeiServices,
    IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,
    IN VOID                     *InvokePpi
);

static EFI_PEI_NOTIFY_DESCRIPTOR EndOfMrcNotifyList[] = {
    { EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | \
      EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST, \
      &gAmiPeiAfterMrcGuid, ChangeBootModeAfterEndofMrc },
};
#endif
//--------------------------------------------------------------------------
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   GetFlashUpdateVar
//
// Description:
//  This function checks if recovery flow is needed due to pending Flash Update
//
// Input:
//  PeiServices Pointer to the PEI services table
//
// Output:
//  TRUE - recovery is requested
//  FALSE - recovery is not requested
//
// Notes:
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
BOOLEAN 
GetFlashUpdateVar (
    EFI_PEI_SERVICES    **PeiServices,
    AMI_FLASH_UPDATE_BLOCK *FlashUpdDesc,
    UINT32                  *CounterHi
){
    EFI_STATUS      Status;
    UINTN           Size;
    EFI_PEI_READ_ONLY_VARIABLE_PPI  *ReadOnlyVariable;
#if  FWCAPSULE_RECOVERY_SUPPORT == 1
    EFI_PHYSICAL_ADDRESS          IoData;
    EFI_CAPSULE_HEADER           *CapsuleHeader;
    EFI_CAPSULE_BLOCK_DESCRIPTOR *pCapsuleMailboxPtr;
#endif
// Check FlashOp variable for Recovery Flash operation 
// Detect if we are in Flash Update mode and set some recovery global variables
// Read "FlashOp" Variable to update global RecoveryFileName, Size
    Status = (*PeiServices)->LocatePpi( PeiServices,
                                &gPeiReadOnlyVariablePpiGuid,
                                0,
                                NULL,
                                &ReadOnlyVariable );
    if(EFI_ERROR(Status)) 
        return FALSE;

    Size = sizeof(AMI_FLASH_UPDATE_BLOCK);
    Status = ReadOnlyVariable->GetVariable( PeiServices,
                                    FLASH_UPDATE_VAR,
                                    &gFlashUpdGuid,
                                    NULL,
                                    &Size,
                                    FlashUpdDesc );
    if (!EFI_ERROR(Status)) 
    {
        Size = sizeof(UINT32);
        Status = ReadOnlyVariable->GetVariable(PeiServices,
                                        L"MonotonicCounter", 
                                        &guidVar,
                                        NULL, 
                                        &Size,
                                        CounterHi);
        if (EFI_ERROR(Status) || FlashUpdDesc->MonotonicCounter == 0xffffffff)
            *CounterHi = 0xffffffff;

        return TRUE;
#if  FWCAPSULE_RECOVERY_SUPPORT == 1
    } else {
        Size=sizeof(EFI_PHYSICAL_ADDRESS);
        Status = ReadOnlyVariable->GetVariable( PeiServices,
                                        CAPSULE_UPDATE_VAR, &gEfiCapsuleVendorGuid, 
                                        NULL,
                                        &Size,
                                        &IoData );
        if (!EFI_ERROR(Status)) 
        {
            // verify the FW capsule is in memory. May first check if pCapsuleMailboxPtr == IoData
            pCapsuleMailboxPtr = (EFI_CAPSULE_BLOCK_DESCRIPTOR*)IoData;
            CapsuleHeader = (EFI_CAPSULE_HEADER*)pCapsuleMailboxPtr[0].DataBlock;
            //
            // Compare GUID with APTIO_FW_CAPSULE_GUID 
            //
            if (guidcmp (&CapsuleHeader->CapsuleGuid, &gFWCapsuleGuid)==0) {
                FlashUpdDesc->FlashOpType = FlCapsule;
                return TRUE;  
            }
        }
#endif
    }

    return FALSE;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   IsFlashUpdate
//
// Description:
//  This function returns proper BootMode if Flash Update mode is pending
//
// Input:
//  **PeiServices - Pointer to the PEI services table
//  *BootMode     - Pointer to a BootMode variable
//
// Output:
//  BOOLEAN 
//      TRUE - recovery is requested
//      FALSE - recovery is not requested
//  BootMode - updated BootMode value if TRUE
//
// Notes:
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
BOOLEAN 
IsFlashUpdate(
    EFI_PEI_SERVICES    **PeiServices,
    EFI_BOOT_MODE       *BootMode
){
    UINT32          CounterHi = 0;
    AMI_FLASH_UPDATE_BLOCK FlashUpdDesc;

    FlashUpdDesc.MonotonicCounter = 0;
    FlashUpdDesc.FlashOpType = FlDisabled;

//SetMode should have set FlashUpd var even if no MC var detected.
// MC check should fail Recovery
    if(/**BootMode != BOOT_IN_RECOVERY_MODE && */
        GetFlashUpdateVar(PeiServices, &FlashUpdDesc, &CounterHi) &&
        CounterHi==(UINT32)FlashUpdDesc.MonotonicCounter)
    {
        if(FlashUpdDesc.FlashOpType == FlRecovery)
        {
            *BootMode = BOOT_IN_RECOVERY_MODE;
            return TRUE;
        }
        if(FlashUpdDesc.FlashOpType == FlCapsule/* && *BootMode == BOOT_ON_S3_RESUME*/)
        {
            *BootMode = BOOT_ON_FLASH_UPDATE;
            return TRUE;  
        }
    }
    return FALSE;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   IsFlashUpdateRecovery
//
// Description:
//  This function checks if recovery flow is needed due to pending Flash Update
//
// Input:
//  PeiServices Pointer to the PEI services table
//
// Output:
//  TRUE - recovery is requested
//  FALSE - recovery is not requested
//
// Notes:
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
BOOLEAN 
IsFlashUpdateRecovery (
    EFI_PEI_SERVICES    **PeiServices
){
    EFI_BOOT_MODE       BootMode;
    if(IsFlashUpdate(PeiServices, &BootMode) && BootMode == BOOT_IN_RECOVERY_MODE)
        return TRUE;
    return FALSE;
}

#if  FWCAPSULE_RECOVERY_SUPPORT == 1

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   ChangeBootModeAfterEndofMrc
//
// Description: Change BootMode to BOOT_ON_FLASH_UPDATE if Capsule update is pending
//
// Input:       PeiServices      - Pointer to the PEI services table
//              NotifyDescriptor - Pointer to the descriptor for the
//                                 notification event.
//              InvokePpi        - Pointer to the PPI that was installed
//
// Output:      EFI_STATUS
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS ChangeBootModeAfterEndofMrc (
    IN EFI_PEI_SERVICES             **PeiServices,
    IN EFI_PEI_NOTIFY_DESCRIPTOR    *NotifyDescriptor,
    IN VOID                         *InvokePpi )
{
    EFI_STATUS                      Status;
    EFI_BOOT_MODE        BootMode;

    Status = (*PeiServices)->GetBootMode( PeiServices, &BootMode );
//    if (!EFI_ERROR(Status) &&  (BootMode != BOOT_IN_RECOVERY_MODE))
// BOOT_ON_FLASH_UPDATE may already be set by Capsule PEI
    if(!EFI_ERROR(Status) && (BootMode == BOOT_ON_S3_RESUME || BootMode == BOOT_ON_FLASH_UPDATE))
    {
    // Change the Boot Mode to Recovery from S3_RESUME/BOOT_ON_FLASH_UPDATE
       if (IsFlashUpdate(PeiServices, &BootMode) && BootMode == BOOT_ON_FLASH_UPDATE)
           (*PeiServices)->SetBootMode(PeiServices, BootMode);
    }
    //install Flash Update Boot Mode PPI
    if(BootMode == BOOT_ON_FLASH_UPDATE)
        (*PeiServices)->InstallPpi( PeiServices, mFlashUpdateModePpi);

    return EFI_SUCCESS;
}
#endif
//<AMI_PHDR_START>
//----------------------------------------------------------------------
//
// Procedure:    IsFlashUpdateCapsuleInit
//
// Description:    Installs callback to ChangeBootModeAfterEndofMrc
//
//---------------------------------------------------------------------- 
//<AMI_PHDR_END>
EFI_STATUS
IsFlashUpdateCapsuleInit (
    IN    EFI_FFS_FILE_HEADER        *FfsHeader,
    IN    EFI_PEI_SERVICES        **PeiServices
)
{
#if  FWCAPSULE_RECOVERY_SUPPORT == 1
    return (*PeiServices)->NotifyPpi( PeiServices, EndOfMrcNotifyList);
#else 
    return EFI_SUCCESS;
#endif

}
//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (C)Copyright 1985-2014, American Megatrends, Inc.         **
//**                                                                  **
//**                       All Rights Reserved.                       **
//**                                                                  **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093        **
//**                                                                  **
//**                       Phone: (770)-246-8600                      **
//**                                                                  **
//**********************************************************************
//**********************************************************************