summaryrefslogtreecommitdiff
path: root/Core/EM/OFBD/SecureFlash/SecureFlash.c
blob: f787d3364b4a259dd796ea16ba09aa4f47690408 (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
//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (C)Copyright 1985-2011, American Megatrends, Inc.         **
//**                                                                  **
//**                       All Rights Reserved.                       **
//**                                                                  **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093        **
//**                                                                  **
//**                       Phone: (770)-246-8600                      **
//**                                                                  **
//**********************************************************************
//**********************************************************************

//**********************************************************************
// $Header: /Alaska/SOURCE/Modules/OFBD Secure Flash/SecureFlash.c 7     1/17/12 10:24p Lawrencechang $
//
// $Revision: 7 $
//
// $Date: 1/17/12 10:24p $
//**********************************************************************
// Revision History
// ----------------
// $Log: /Alaska/SOURCE/Modules/OFBD Secure Flash/SecureFlash.c $
// 
// 7     1/17/12 10:24p Lawrencechang
// Remove unnecessary header file.
// 
// 6     1/04/12 7:14a Lawrencechang
// Remove runtime flash codes, which will be done by SMIFlash protocol.
// 
// 5     12/21/11 8:17a Lawrencechang
// 1. Check if SMIFlash module exists or enables before install SMIFlash
// protocol.
// 2. Add WriteBlockEx() for SMIFlash protocol compatibility.
// WriteBlockEx() will be only used in SMIFlash protocol but no exposed to
// flash ROM interface.
// 
// 4     10/29/11 5:26a Lawrencechang
// Modified for adding SMIFlash protocol compatibility.
// 
// 3     10/28/11 4:28a Lawrencechang
// Add GetFlashInfo() for compatibility with SMIFlash protocol. Only works
// with 4.6.3.2_OFBD.1.0.2.
//
// 2     10/18/11 3:11a Lawrencechang
// Force LoadFirmwareImage(), GetFlashUpdatePolicy() and
// SetFlashUpdateMethod() to return correct error code.
//
// 1     9/16/11 4:25a Lawrencechang
// Initial check-in.
//
//**********************************************************************
//<AMI_FHDR_START>
//
// Name:	SecureFlash.c
//
// Description: This file provides OFBD Secure Flash function.
//
//<AMI_FHDR_END>
//**********************************************************************
#include <EFI.h>
#include <Token.h>
#include <AmiDxeLib.h>
#include <Protocol/SecSmiFlash.h>
#include <Protocol/FlashProtocol.h>

#include <OFBD.h>
#include <SecureFlash.h>

// GUIDs of consumed protocols
static EFI_GUID		gEfiSmiFlashProtocolGuid		= EFI_SMI_FLASH_GUID;
static EFI_GUID		gEfiSecSmiFlashProtocolGuid		= EFI_SEC_SMI_FLASH_GUID;

// Consumed protocols
EFI_SEC_SMI_FLASH_PROTOCOL		*SecSmiFlash	= NULL;
FLASH_PROTOCOL					*Flash			= NULL;

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure: LoadFirmwareImage
//
// Description:	Transfer ROM image from AFU's buffer to Secure Flash Module
//				through EFI_SEC_SMI_FLASH_PROTOCOL.
//
// Input:
//		IN VOID		*Data	The data is a memory buffer. Its content is FUNC_BLOCK
//							defined in SmiFlash.h. The member function
//							LOAD_FLASH_IMAGE in EFI_SEC_SMI_FLASH_PROTOCOL only
//							accepts this type of parameter.
//
// Output:
//		EFI_STATUS
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS LoadFirmwareImage (
	VOID		*Data
)
{
	EFI_STATUS		Status;

	if (NULL == SecSmiFlash) return EFI_UNSUPPORTED;

	Status = SecSmiFlash->LoadFwImage((FUNC_BLOCK *)Data);

	((FUNC_BLOCK *)Data)->ErrorCode = (EFI_ERROR(Status) ? 1 : 0);

	return Status;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure: GetFlashUpdatePolicy
//
// Description:	Get Flash Update Policy from Secure Flash module.
//
// Input:
//		IN VOID		*Data	The data is a memory buffer. Its content is
//							FLASH_POLICY_INFO_BLOCK defined in SecSmiFlash.h.
//
// Output:
//		EFI_STATUS
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS GetFlashUpdatePolicy (
	VOID	*Data
)
{
	EFI_STATUS					Status;
	FLASH_POLICY_INFO_BLOCK		*pFlashPolicy = (FLASH_POLICY_INFO_BLOCK *)Data;

	if (NULL == SecSmiFlash) return EFI_UNSUPPORTED;

	Status = SecSmiFlash->GetFlUpdPolicy(pFlashPolicy);

	pFlashPolicy->ErrorCode = (EFI_ERROR(Status) ? 1 : 0);

	return Status;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure: SetFlashUpdateMethod
//
// Description:	Set Flash Update Policy to Secure Flash module.
//
// Input:
//		IN VOID		*Data	The data is a memory buffer. Its content is
//							FUNC_FLASH_SESSION_BLOCK defined in SecSmiFlash.h.
//
// Output:
//		EFI_STATUS
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS SetFlashUpdateMethod (
	VOID		*Data
)
{
	EFI_STATUS					Status;
	FUNC_FLASH_SESSION_BLOCK	*pFlashSessionBlock = (FUNC_FLASH_SESSION_BLOCK *)Data;

	if (NULL == SecSmiFlash) return EFI_UNSUPPORTED;

	Status = SecSmiFlash->SetFlUpdMethod(pFlashSessionBlock);

	pFlashSessionBlock->ErrorCode = (EFI_ERROR(Status) ? 1 : 0);

	return Status;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	SecureFlashProtocolCallback
//
// Description:	Locate SecSmiFlash protocol callback
//
// Input:
//		IN EFI_EVENT		Event
//		IN VOID				*Context
//
// Output:
//		VOID
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID SecureFlashProtocolCallback (
	IN	EFI_EVENT		Event,
	IN	VOID			*Context
)
{
	pBS->LocateProtocol(&gEfiSecSmiFlashProtocolGuid, NULL, &SecSmiFlash);
	pBS->CloseEvent(Event);
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	SmmFlashProtocolCallback
//
// Description:	Locate Flash protocol callback
//
// Input:
//		IN EFI_EVENT		Event
//		IN VOID				*Context
//
// Output:
//		VOID
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID SmmFlashProtocolCallback (
	IN	EFI_EVENT		Event,
	IN	VOID			*Context
)
{
	pBS->LocateProtocol(&gFlashSmmProtocolGuid, NULL, &Flash);
	pBS->CloseEvent(Event);
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------
// Procedure:   ASFUInSmm
//
// Description: This function installs the ASFU Protocol.
//
// Input:
//		VOID
//
// Output:
//		VOID
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>
VOID ASFUInSmm (
	VOID
)
{
	EFI_STATUS	Status;
	EFI_HANDLE	DummyHandle = NULL;

	Status = pBS->LocateProtocol(&gEfiSecSmiFlashProtocolGuid, NULL, &SecSmiFlash);
	if (EFI_ERROR(Status)) {
		EFI_EVENT	SecFlashCallbackEvt;
		VOID		*Reg;
		RegisterProtocolCallback(
			&gEfiSecSmiFlashProtocolGuid,
			SecureFlashProtocolCallback,
			NULL,
			&SecFlashCallbackEvt,
			&Reg);
	}

	Status = pBS->LocateProtocol(&gFlashSmmProtocolGuid, NULL, &Flash);
	if (EFI_ERROR(Status)) {
		EFI_EVENT	SmmFlashCallbackEvt;
		VOID		*Reg;
		RegisterProtocolCallback(
			&gFlashSmmProtocolGuid,
			SmmFlashProtocolCallback,
			NULL,
			&SmmFlashCallbackEvt,
			&Reg);
	}

	return;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	ASFUEntry
//
// Description:	OFBD Secure Flash Update Entry point
//
// Input:
//		IN VOID				*Buffer
//		IN OUT UINT8		*pOFBDDataHandled
//
// Output:
//		VOID
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID ASFUEntry (
	IN		VOID				*Buffer,
	IN OUT	UINT8		*pOFBDDataHandled
)
{
	OFBD_HDR					*pOFBDHdr;
	OFBD_EXT_HDR				*pOFBDExtHdr;
	VOID						*pOFBDTblEnd;
	OFBD_TC_70_SFU_STRUCT		*ASFUStructPtr;
	EFI_STATUS					Status = EFI_SUCCESS;

	if (*pOFBDDataHandled == 0) {
		pOFBDHdr = (OFBD_HDR *)Buffer;
		pOFBDExtHdr = (OFBD_EXT_HDR *)((UINT8 *)Buffer + (pOFBDHdr->OFBD_HDR_SIZE));
		ASFUStructPtr = (OFBD_TC_70_SFU_STRUCT *)((UINT8 *)pOFBDExtHdr + sizeof(OFBD_EXT_HDR));
		pOFBDTblEnd = (VOID *)((UINT8 *)Buffer + (pOFBDHdr->OFBD_Size));

		if (pOFBDHdr->OFBD_FS & OFBD_FS_SFU) {
			//Check Type Code ID
			if (pOFBDExtHdr->TypeCodeID == OFBD_EXT_TC_SFU) {
				switch (ASFUStructPtr->Command) {
					case OFBD_TC_SFU_LOAD_FIRMWARE_IMAGE :
						Status = LoadFirmwareImage(pOFBDTblEnd);
						break;
					case OFBD_TC_SFU_GET_FLASH_UPDATE_POLICY :
						Status = GetFlashUpdatePolicy(pOFBDTblEnd);
						break;
					case OFBD_TC_SFU_SET_FLASH_UPDATE_METHOD :
						Status = SetFlashUpdateMethod(pOFBDTblEnd);
						break;
					default :
						Status = EFI_UNSUPPORTED;
						break;
				}
				if (EFI_ERROR(Status)) {
					*pOFBDDataHandled = 0xFE;
					ASFUStructPtr->Status = OFBD_TC_SFU_NOT_SUPPORTED;
				} else {
					*pOFBDDataHandled = 0xFF;
					ASFUStructPtr->Status = OFBD_TC_SFU_OK;
				}
			}
		}
	}
}

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