summaryrefslogtreecommitdiff
path: root/Core/EM/Recovery/CapsuleRecovery.c
blob: f38c11e7ac5e8e79a50b21370231c4a3d80c71c8 (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
//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (C)Copyright 1985-2009, American Megatrends, Inc.         **
//**                                                                  **
//**                       All Rights Reserved.                       **
//**                                                                  **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093        **
//**                                                                  **
//**                       Phone: (770)-246-8600                      **
//**                                                                  **
//**********************************************************************
//**********************************************************************
// $Header: /Alaska/Projects/Intel/Haswell/LynxPoint_SharkBay-DT_Crb_1AQQW/Core/EM/Recovery/CapsuleRecovery.c 2     4/16/13 5:51a Thomaschen $
//
// $Revision: 2 $
//
// $Date: 4/16/13 5:51a $
//**********************************************************************
// Revision History
// ----------------
// $Log: /Alaska/Projects/Intel/Haswell/LynxPoint_SharkBay-DT_Crb_1AQQW/Core/EM/Recovery/CapsuleRecovery.c $
// 
// 2     4/16/13 5:51a Thomaschen
// Fixed for EIP106722.
//
// 4     2/07/13 5:17p Artems
// [TAG]  		EIP106722
// [Category]  	Bug Fix
// [Severity]  	Critical
// [Symptom]  	Win8 firmware update doesn't work
// [RootCause]  	For Win8 update capsule CapsuleRecovery device didn't
// skip capsule header
// [Solution]  	Added unique GUID to distinguish between AFU and Win8
// capsule update
// [Files]  		Capsule2_0.c CapsuleRecovery.c Capsule.h
// 
// 3     11/15/12 4:28p Artems
// [TAG]  		EIP103898
// [Category]  	Bug Fix
// [Severity]  	Important
// [Symptom]  	After updating Core 4.6.5.4, the Capsule mode of Secure
// Flash can't work.
// [RootCause]  	Capsule recovery device was stripping capsule header from
// provided image, 
// but verification code requires header to be present
// [Solution]  	Keep header together with recovery image for verification
// purposes
// [Files]  		CapsuleRecovery.c
// 
// 2     9/17/12 2:50p Artems
// [TAG]  		EIP N/A
// [Category]  	Improvement
// [Description]  	Removed unnecessary debug messages
// [Files]  		CapsuleRecovery.c
// 
// 1     8/02/12 12:04p Artems
// [TAG]  		EIP93520
// [Category]  	New Feature
// [Description]  	Windows 8 firmware update spec
// [Files]  		Recovery.h Recovery.sdl Recovery.c Reflash.c ReflashWorker.c
// Esrt.c CapsuleRecovery.c
// 
//**********************************************************************

//<AMI_FHDR_START>
//----------------------------------------------------------------------
//
// Name: CapsuleRecovery.c
//
// Description:	Recovery from Capsule support
//
//----------------------------------------------------------------------
//<AMI_FHDR_END>

#include <AmiPeiLib.h>
#include <AmiHobs.h>
#include <Capsule.h>
#include <Token.h>
#include "Recovery.h"
#include "RecoveryCsp.h"


/************************************************************************/
/*                 Device Recovery Module PPI                           */
/************************************************************************/
//<AMI_PHDR_START>
//----------------------------------------------------------------------
//
// Procedure:	GetNumberRecoveryCapsules
//
// Description:	
//  GetNumberRecoveryCapsules function of ppi 
//  EFI_PEI_DEVICE_RECOVERY_MODULE_PPI.
//
// Input:
//
// Output:
//  EFI_SUCCESS - number of recovery capsules returned
//  EFI_INVALID_PARAMETER - the pointer NumberRecoveryCapsules is NULL
//----------------------------------------------------------------------
//<AMI_PHDR_END>

static EFI_STATUS GetNumberRecoveryCapsules(
    IN EFI_PEI_SERVICES                   **PeiServices,
    IN EFI_PEI_DEVICE_RECOVERY_MODULE_PPI *This,
    OUT UINTN                             *NumberRecoveryCapsules 
)
{
    if ( !NumberRecoveryCapsules ) {
        return EFI_INVALID_PARAMETER;
    }
    *NumberRecoveryCapsules = 1;
    return EFI_SUCCESS;
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------
//
// Procedure:	GetRecoveryCapsuleInfo
//
// Description:	
//  GetRecoveryCapsuleInfo function of ppi EFI_PEI_DEVICE_RECOVERY_MODULE_PPI 
//  for any block devices including floppies, USB keys, CD-ROMs and HDDs.
//
// Input:
//  IN EFI_PEI_SERVICES **PeiServices - pointer to PeiServices Structure
//  IN EFI_PEI_DEVICE_RECOVERY_MODULE_PPI *This - pointer to the PPI structure
//  IN UINTN CapsuleInstance - value indicating the instance of the PPI
//  OUT UINTN *Size - Size of the recovery capsule
//  OUT EFI_GUID *CapsuleType OPTIONAL - Type of recovery capsule
//
// Output:
//  EFI_SUCCESS - Parameters are valid and output parameters are updated
//  EFI_INVALID_PARAMETER - Size pointer is NULL
//  EFI_NOT_FOUND - asking for a 1 or greater instance of the PPI
//----------------------------------------------------------------------
//<AMI_PHDR_END>
static EFI_STATUS GetRecoveryCapsuleInfo(
    IN EFI_PEI_SERVICES                   **PeiServices,
    IN EFI_PEI_DEVICE_RECOVERY_MODULE_PPI *This,
    IN UINTN                              CapsuleInstance,
    OUT UINTN                             *Size,
    OUT EFI_GUID                          *CapsuleType )
{
    EFI_STATUS Status;
    if ( !Size ) {
        return EFI_INVALID_PARAMETER;
    }

    if ( CapsuleInstance > 0 ) {
        return EFI_NOT_FOUND;
    }

    Status = GetRecoveryFileInfo(PeiServices, NULL, Size, NULL);
    if(EFI_ERROR(Status))
        return Status;

    if ( CapsuleType ) {
        *CapsuleType = guidOemCapsule;
    }
    return EFI_SUCCESS;
}

#define NUMBER_OF_RETRIES 3

//<AMI_PHDR_START>
//----------------------------------------------------------------------
//
// Procedure:	LoadRecoveryCapsule
//
// Description:	
//  Locates all EFI_PEI_RECOVERY_BLOCK_IO_PPI PPIs.  Calls function 
//  GetNumberOfBlockDevices.  For each block device, calls the function 
//  FsRecoveryRead, to find the recovery image named in var sAmiRomFile.
//
// Input:
//  IN EFI_PEI_SERVICES **PeiServices - pointer to PeiServices Structure 
//  IN EFI_PEI_DEVICE_RECOVERY_MODULE_PPI *This - pointer to the PPI structure
//  IN UINTN CapsuleInstance - value indicating the instance of the PPI
//  OUT VOID *Buffer - contains information read from the block device
//
// Output:
//  EFI_SUCCESS - File read from recovery media
//  EFI_INVALID_PARAMETER - Buffer is a NULL pointer
//  EFI_NOT_FOUND - asking for a 1 or greater instance of the PPI
//  Other - return error values from LocatePpi or FsRecoveryRead
//----------------------------------------------------------------------
//<AMI_PHDR_END>
static EFI_STATUS LoadRecoveryCapsule(
    IN EFI_PEI_SERVICES                   **PeiServices,
    IN EFI_PEI_DEVICE_RECOVERY_MODULE_PPI *This,
    IN UINTN                              CapsuleInstance,
    OUT VOID                              *Buffer )
{
    EFI_STATUS Status;
    AMI_CAPSULE_HOB *CapsuleHob;
    static EFI_GUID CapsuleHobGuid = AMI_CAPSULE_HOB_GUID;
    static EFI_GUID AptioFwCapsuleGuid = APTIO_FW_CAPSULE_GUID;
    static EFI_GUID W8FwUpdateImageCapsuleGuid = W8_FW_UPDATE_IMAGE_CAPSULE_GUID;
    EFI_CAPSULE_HEADER *Capsule;
    VOID *Data;
    UINTN CapsuleSize;

    Status = (*PeiServices)->GetHobList(PeiServices, &CapsuleHob);
    if(EFI_ERROR(Status))
        return EFI_SUCCESS;

    do {
        Status = FindNextHobByGuid(&CapsuleHobGuid, &CapsuleHob);
        if(!EFI_ERROR(Status)) {
            Capsule = (EFI_CAPSULE_HEADER *)(VOID *)(UINTN)(CapsuleHob->CapsuleData);
            if(!guidcmp(&AptioFwCapsuleGuid, &(CapsuleHob->CapsuleGuid))) { //we found AFU recovery capsule, let's keep capsule header and put into provided buffer
                MemCpy(Buffer, Capsule, Capsule->CapsuleImageSize);
                return EFI_SUCCESS;
            }
            if(!guidcmp(&W8FwUpdateImageCapsuleGuid, &(CapsuleHob->CapsuleGuid))) { //we found Win8 recovery capsule, let's skip capsule header and put into provided buffer
                CapsuleSize = Capsule->CapsuleImageSize - Capsule->HeaderSize;
                Data = (VOID *)((UINT8 *)Capsule + Capsule->HeaderSize);
                MemCpy(Buffer, Data, CapsuleSize);
                return EFI_SUCCESS;
            }                
        }
    } while(!EFI_ERROR(Status));

    return Status;
}

/************************************************************************/
/*				Entry Point												*/
/************************************************************************/
EFI_PEI_DEVICE_RECOVERY_MODULE_PPI CapsuleDeviceRecoveryModule = {
    GetNumberRecoveryCapsules, GetRecoveryCapsuleInfo, LoadRecoveryCapsule
};

// PPI to be installed
static EFI_PEI_PPI_DESCRIPTOR CapsuleDeviceRecoveryPpiList[] = {
    {
        EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,
        &guidRecoveryDevice, &CapsuleDeviceRecoveryModule
    }
};


//<AMI_PHDR_START>
//----------------------------------------------------------------------
//
// Procedure:	CapsuleDeviceRecoveryEntry
//
// Description:	
//  Installs EFI_PEI_DEVICE_RECOVERY_MODULE_PPI for loading recovery 
//  images from APTIO_FW_CAPSULE_GUID capsule
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS CapsuleDeviceRecoveryEntry(
    IN EFI_FFS_FILE_HEADER *FfsHeader,
    IN EFI_PEI_SERVICES    **PeiServices )
{
    return (*PeiServices)->InstallPpi( PeiServices, CapsuleDeviceRecoveryPpiList );
}



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