summaryrefslogtreecommitdiff
path: root/Core/EM/SecurityPkg/SecFlashUpd/SecFlashUpdDxe.c
blob: 8ce7d3f8e30081dd744b5ffedca3524b579ccca0 (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
//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (C)Copyright 1985-2014, American Megatrends, Inc.         **
//**                                                                  **
//**                       All Rights Reserved.                       **
//**                                                                  **
//**        5555 Oakbrook Pkwy, Suite 200, Norcross, GA 30093         **
//**                                                                  **
//**                       Phone: (770)-246-8600                      **
//**                                                                  **
//**********************************************************************
//**********************************************************************
//**********************************************************************
// $Header: /Alaska/SOURCE/Modules/SecureFlashPkg/FlashUpdate/SecFlashUpdDxe.c 10    3/18/14 3:01p Alexp $
//
// $Revision: 10 $
//
// $Date: 3/18/14 3:01p $
//**********************************************************************
// Revision History
// ----------------
// $Log: /Alaska/SOURCE/Modules/SecureFlashPkg/FlashUpdate/SecFlashUpdDxe.c $
// 
// 10    3/18/14 3:01p Alexp
// set 2014 in file hdr & ftr
// 
// 9     5/09/13 10:37a Alexp
// EIP#123379 : Disabling FLASH_LOCK_EVENT_NOTIFY causes build error
// 
// 8     10/31/12 1:36p Alexp
// removed #ifdef for CAPSULE_SUPPORTbefore checks for capsule variable. 
// 
// 7     9/06/12 6:15p Alexp
// EIP#100418: Make SecureFlashPkg build independant from ReFlash-Support
// 
// 6     1/06/12 10:37a Alexp
// Streamlined the code for Setup.AutoFlash variable update if FlashUpdate
// is pending
// 
// 4     11/17/11 10:02a Alexp
// Use Core 4.6.5.2 provided name AMI_EVENT_FLASH_WRITE_LOCK as GUID
// define for Flash Lock event
// 
// 3     11/10/11 4:38p Alexp
// 1. UpdateAutoFlash() updates the AutoFlash Variable installed by
// ReFlash driver in Recovery/Flash Update flows
// 2. Issue FlashReadyToLock Event notification based on the Flash Update
// policy.
// 
// 2     7/20/11 7:16p Alexp
// remove dependency on Capsule module
// 
// 1     7/01/11 4:39p Alexp
// 
// 
//**********************************************************************
#include <Token.h>
#include <AmiDxeLib.h>
#include <AmiHobs.h>
#include <FlashUpd.h>

#include <Flash.h>
#if (defined ReFlash_SUPPORT) && (ReFlash_SUPPORT == 1)
#include "ReFlash.h"
#endif
#include "Capsule.h"

//
// Global variables
//
extern EFI_BOOT_SERVICES    *pBS;

#if FLASH_LOCK_EVENT_NOTIFY == 1

VOID     *gSbHobList     = NULL;
static EFI_GUID gHobListGuid = HOB_LIST_GUID;

// Core 4.6.5.2 sources GUID
#ifndef AMI_EVENT_FLASH_WRITE_LOCK
// {49D34AE7-1348-4551-8F71-467D8C0E4EF5}
#define AMI_EVENT_FLASH_WRITE_LOCK \
    { 0x49D34AE7, 0x9454, 0x4551, 0x8F, 0x71, 0x46, 0x7D, 0x8C, 0x0E, 0x4E, 0xF5 }
#endif

#define BDS_CONNECT_DRIVERS_PROTOCOL_GUID \
    { 0x3aa83745, 0x9454, 0x4f7a, 0xa7, 0xc0, 0x90, 0xdb, 0xd0, 0x2f, 0xab, 0x8e }

EFI_GUID  gBiosLockEnableEventGuid = AMI_EVENT_FLASH_WRITE_LOCK;
EFI_GUID  gBdsConnectDriversProtocolGuid = BDS_CONNECT_DRIVERS_PROTOCOL_GUID;

EFI_EVENT mSecureModEvent;
VOID      *mSecureModReg;
#endif //#if FLASH_LOCK_EVENT_NOTIFY == 1

//----------------------------------------------------------------------------
// Function definitions
//----------------------------------------------------------------------------

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure: ReadyToLockCallback
//
// Description: ready to Lock Flash part
//  Signal a event for ready to lock. 
//
// Input:   Event             - The event that triggered this notification function  
//          ParentImageHandle - Pointer to the notification functions context
//
// Output:  VOID
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
#if FLASH_LOCK_EVENT_NOTIFY == 1
VOID
ReadyToLockCallback (
  EFI_EVENT           Event,
  VOID                *ParentImageHandle
  )
{
    EFI_HANDLE  Handle = NULL;
    TRACE((-1,"\nSecure Fl Upd: FlashReadyToLock callback\n"));

// Signal Event.....
    pBS->InstallProtocolInterface (
        &Handle, &gBiosLockEnableEventGuid, EFI_NATIVE_INTERFACE,NULL
    );
    pBS->UninstallProtocolInterface (
        Handle,&gBiosLockEnableEventGuid, NULL
    );
    //
    //Kill the Event
	//
    pBS->CloseEvent(Event);
}

#endif //#if FLASH_LOCK_EVENT_NOTIFY == 1


//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   UpdateAutoFlash
//
// Description: This is the standard EFI driver entry point called for
//              Recovery flash module initlaization
// Input:       IN EFI_HANDLE ImageHandle - ImageHandle of the loaded driver
//              IN EFI_SYSTEM_TABLE SystemTable - Pointer to the System Table
//
// Output:      EFI_SUCCESS
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS UpdateAutoFlash ()
{
    EFI_STATUS              Status;
    UINT32                  Attributes;
    UINTN                   Size;
#if (defined ReFlash_SUPPORT) && (ReFlash_SUPPORT == 1)
    AUTOFLASH               AutoFlash;
    static EFI_GUID         guidReFlash     = RECOVERY_FORM_SET_GUID;
#endif //#if (defined ReFlash_SUPPORT) && (ReFlash_SUPPORT == 1)
    static EFI_GUID         FlashUpdGuid    = FLASH_UPDATE_GUID;
    static EFI_GUID         CapsuleBootModeGuid = EFI_CAPSULE_AMI_GUID;
    AMI_FLASH_UPDATE_BLOCK  FlashUpdDesc;

// Prep the FlashOp variable
    Attributes = 0;
    Size = sizeof(AMI_FLASH_UPDATE_BLOCK);
    Status = pRS->GetVariable( FLASH_UPDATE_VAR,&FlashUpdGuid,&Attributes,&Size, &FlashUpdDesc);
    if(!EFI_ERROR(Status))
    {
        // Erase Flash Var
        pRS->SetVariable (FLASH_UPDATE_VAR,&FlashUpdGuid,Attributes,0,NULL);
        // Clear pending Capsule Update Var
        // only if FlashOp is pending. We don't want to interfere with other types of Capsule Upd
        Size = 0;
        if(pRS->GetVariable(CAPSULE_UPDATE_VAR,&CapsuleBootModeGuid, &Attributes, &Size, NULL)==EFI_BUFFER_TOO_SMALL)
            pRS->SetVariable(CAPSULE_UPDATE_VAR,&CapsuleBootModeGuid, Attributes, 0, NULL);
#if (defined ReFlash_SUPPORT) && (ReFlash_SUPPORT == 1)
        // Update ReFlash parameters 
        Size = sizeof(AUTOFLASH);
        Status = pRS->GetVariable(L"Setup",&guidReFlash, &Attributes, &Size, &AutoFlash);   
        if(!EFI_ERROR(Status))
        {
        
            AutoFlash.UpdateNv = (FlashUpdDesc.ROMSection & (1<<FV_NV)) ? 1 : 0;
            AutoFlash.UpdateBb = (FlashUpdDesc.ROMSection & (1<<FV_BB)) ? 1 : 0;
            AutoFlash.UpdateMain=(FlashUpdDesc.ROMSection & (1<<FV_MAIN)) ? 1 : 0;
            // EC block
#if (defined EC_FIRMWARE_UPDATE_INTERFACE_SUPPORT) && (EC_FIRMWARE_UPDATE_INTERFACE_SUPPORT == 1)
            AutoFlash.UpdateEC  =(FlashUpdDesc.ROMSection & (1<<ROM_EC))   ? 1 : 0;
#endif       
            pRS->SetVariable(L"Setup",&guidReFlash, Attributes, sizeof(AUTOFLASH), &AutoFlash);
        }
#endif //#if (defined ReFlash_SUPPORT) && (ReFlash_SUPPORT == 1)
    }

    return EFI_SUCCESS;
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   SecFlashUpdDxe_Init
//
// Description: Entry point of Flash Update Policy driver
//
// Input:       EFI_HANDLE           ImageHandle,
//              EFI_SYSTEM_TABLE     *SystemTable
//
// Output:      EFI_STATUS
//
// Modified:
//
// Referrals:
//
// Notes:
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS
SecFlashUpdDxe_Init (
    IN EFI_HANDLE         ImageHandle,
    IN EFI_SYSTEM_TABLE   *SystemTable
)
{
    EFI_STATUS  Status;

    InitAmiLib(ImageHandle, SystemTable);

    Status = UpdateAutoFlash();
///////////////////////////////////////////////////////////////////////////////
//
// Create Flash Lock event.
//
///////////////////////////////////////////////////////////////////////////////
#if FLASH_LOCK_EVENT_NOTIFY == 1
    // Get Hob List
    gSbHobList = GetEfiConfigurationTable(SystemTable, &gHobListGuid);
    if (!gSbHobList)
        return EFI_INVALID_PARAMETER;

// Locking SPI before BDS Connect on normal boot
    if (((EFI_HOB_HANDOFF_INFO_TABLE*)gSbHobList)->BootMode!=BOOT_IN_RECOVERY_MODE && 
        ((EFI_HOB_HANDOFF_INFO_TABLE*)gSbHobList)->BootMode!=BOOT_ON_FLASH_UPDATE
    ) 
        Status = RegisterProtocolCallback ( &gBdsConnectDriversProtocolGuid, \
                                            ReadyToLockCallback, \
                                            NULL, \
                                            &mSecureModEvent, \
                                            &mSecureModReg );
// Locking SPI after ReFlash(BDS) if in Recoovery/Flash Upd mode
    else
        Status = CreateReadyToBootEvent ( TPL_CALLBACK,
                                        ReadyToLockCallback,
                                        NULL,
                                        &mSecureModEvent);

    ASSERT_EFI_ERROR (Status);
#endif

    return Status;
}

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