summaryrefslogtreecommitdiff
path: root/Core/CORE_DXE/AmiDxeInit.c
blob: dbd41b5337df6e2bdc4864032e204da712eba071 (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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (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/Core/EDK/DxeMain/AmiDxeInit.c 24    5/05/11 3:47p Artems $
//
// $Revision: 24 $
//
// $Date: 5/05/11 3:47p $
//**********************************************************************
// Revision History
// ----------------
// $Log: /Alaska/SOURCE/Core/EDK/DxeMain/AmiDxeInit.c $
// 
// 24    5/05/11 3:47p Artems
// Added DevicePathToText and DevicePathFromText protocols
// 
// 23    2/05/11 4:09p Artems
// EDK library functions CopyMem and ZeroMem moved to EdkLib.c
// 
// 22    9/29/10 8:05a Felixp
// Enhancement: Initialization of the library TheImageHandle variable is
// added.
// 
// 21    4/22/10 10:43p Felixp
// Install dummy instance of the security protocol when 
// INSTALL_DUMMY_SECURITY_PROTOCOL SDL token is set to "on".
// 
// 20    2/23/10 10:01p Felixp
// Updated to use CORE_COMBINED_VERSION token.
// 
// 19    11/13/09 4:31p Felixp
// Buffer overflow protection is added (calls to Sprintf replaced with
// Sprintf_s).
// 
// 18    7/30/09 4:04p Felixp
// Bug fix:
// Symptoms: memory corruption during DXE Core initialization.
// Details: the corruption was caused by the HOBs created in AmiDxeInit1. 
// The HOB creation moved to an earlier point (before HOB data is used by
// CoreInitializeMemoryServices).
// 
// 17    5/04/09 3:10p Felixp
// Creation of CPU HOB moved from AmiPeiInit.c to AmiDxeInit.c
// 
// 16    3/05/09 4:39p Oleksiyy
// EIP 18486: 3 GUID EXTENSION HOBs created. 
// Code clean up performed. 
// 
// 15    4/18/08 6:28p Felixp
// checkpoint(0x60) removed. Corresponsing status code (DXE_CORE_ENTRY is
// now reported at the and of DXE IPL using status code infrastructure
// 
// 14    3/12/08 12:44p Felixp
// Progress/Error codes reporting added
// 
// 13    2/21/08 5:05p Felixp
// FirmwareRevision field of the EFI system table is updated to use
// CORE_BUILD_NUMBER.
// 
// 12    9/05/07 4:22p Felixp
// FirmwareVendor field of the EFI_SYSTEM_TABLE was initialized with a
// pointer to boot time memory. However, this is a runtime field.
// File updated to allocate runtime memory for the field.
// 
// 11    10/12/06 9:43p Felixp
// 
// 10    10/12/06 6:51p Felixp
// UEFI2.0 support: DevicePathUtilities protocol added
// 
// 9     10/07/06 10:24a Felixp
// UIEFI2.0 support.
// Updated to EDK 20060904
// 
// 8     8/24/06 10:11a Felixp
// x64 support: warning/error fixes
// 
// 7     5/23/06 4:16a Felixp
// 
// 6     5/22/06 11:57a Felixp
// 
// 5     5/22/06 12:12a Felixp
// Vendor & Revision fields of ST initialized
// 
// 4     5/20/06 9:37p Felixp
// checkpoint 0x60 added
// debug message for drivers startting/stopping
// clean up
// 
// 3     5/19/06 10:46p Felixp
// Updated to EDK 03.16.06
// Cleanup
// 
// 2     4/30/06 9:41p Felixp
// 
// 1     3/13/06 2:00a Felixp
// 
//**********************************************************************
//<AMI_FHDR_START>
//
// Name:	AmiDxeInit.c
//
// Description:	
//
//<AMI_FHDR_END>
//**********************************************************************
#include<EFI.h>
#include<Protocol/Decompress.h>
#include<Protocol/DriverBinding.h>
#include<Protocol/DevicePathUtilities.h>
#include<Protocol/DevicePathToText.h>
#include<Protocol/DevicePathFromText.h>
#include<Protocol/Security.h>
#include<AmiDxeLib.h>
#include<AmiHobs.h>
#include<Token.h>
#include<Guid/PeiFlushInstructionCache.h>
#include<Guid/PeiPeCoffLoader.h>
#include<Guid/PeiTransferControl.h>


EFI_GUID gEfiPeiFlushInstructionCacheGuid = EFI_PEI_FLUSH_INSTRUCTION_CACHE_GUID;
EFI_GUID gEfiPeiPeCoffLoaderGuid = EFI_PEI_PE_COFF_LOADER_GUID;
EFI_GUID gEfiPeiTransferControlGuid = EFI_PEI_TRANSFER_CONTROL_GUID;
EFI_GUID gEfiStatusCodeSpecificDataGuid = EFI_STATUS_CODE_SPECIFIC_DATA_GUID;
typedef VOID (*INIT_PARTS)(VOID* p1, VOID*p2);
VOID InitParts(VOID* p1, VOID*p2);
extern EFI_SYSTEM_TABLE *gST;

extern EFI_DEVICE_PATH_FROM_TEXT_PROTOCOL EfiDevicePathFromTextProtocol;
extern EFI_DEVICE_PATH_TO_TEXT_PROTOCOL EfiDevicePathToTextProtocol;

///////////////////////////////////////////////////////////////
////===================== Print Protocol ==================.
#define EFI_PRINT_PROTOCOL_GUID \
	{ 0xdf2d868e, 0x32fc, 0x4cf0, 0x8e, 0x6b, 0xff, 0xd9, 0x5d, 0x13, 0x43, 0xd0 }

typedef struct _EFI_PRINT_PROTOCOL EFI_PRINT_PROTOCOL;

typedef UINTN (EFIAPI *EFI_VSPRINT)(
	OUT CHAR16 *StartOfBuffer, IN UINTN BufferSize,
	IN CHAR16 *FormatString, IN va_list Marker
);

struct _EFI_PRINT_PROTOCOL{
	EFI_VSPRINT VSPrint;
};

EFI_GUID guidPrint = EFI_PRINT_PROTOCOL_GUID;
EFI_PRINT_PROTOCOL Print = {&Swprintf_s_va_list};

////===================== Decompress Protocol ==================.
EFI_GUID guidDecompress = EFI_DECOMPRESS_PROTOCOL_GUID;
EFI_STATUS DecomprGetInfo (
	IN VOID *This,
	IN VOID *Source, IN UINT32 SourceSize,
	OUT UINT32 *DestinationSize, OUT UINT32 *ScratchSize
)
{
	return GetInfo(Source,SourceSize,DestinationSize,ScratchSize);
}

EFI_STATUS DecomprDecompress (
	IN VOID *This,
	IN VOID* Source, IN UINT32 SourceSize,
	IN OUT VOID* Destination, IN UINT32 DestinationSize,
	IN OUT VOID* Scratch, IN UINT32 ScratchSize
)
{
	return Decompress(Source,SourceSize,Destination,DestinationSize,Scratch,ScratchSize);
}

EFI_DECOMPRESS_PROTOCOL DecompressInterface = {&DecomprGetInfo, &DecomprDecompress};
EFI_DECOMPRESS_PROTOCOL *pDecompressInterface=&DecompressInterface;

////===================== Device Path Utilities Protocol ==================
EFI_DEVICE_PATH_UTILITIES_PROTOCOL DevicePathUtilitiesInstance =
{
    (EFI_DEVICE_PATH_UTILS_GET_DEVICE_PATH_SIZE)DPLength,
    (EFI_DEVICE_PATH_UTILS_DUP_DEVICE_PATH)DPCopy,
    (EFI_DEVICE_PATH_UTILS_APPEND_PATH)DPAdd,
    (EFI_DEVICE_PATH_UTILS_APPEND_NODE)DPAddNode,
    (EFI_DEVICE_PATH_UTILS_APPEND_INSTANCE)DPAddInstance,
    (EFI_DEVICE_PATH_UTILS_GET_NEXT_INSTANCE)DPNextInstance,
    (EFI_DEVICE_PATH_UTILS_IS_MULTI_INSTANCE)DPIsMultiInstance,
    (EFI_DEVICE_PATH_UTILS_CREATE_NODE)DPCreateNode
};

////===================== Security Architectural Protocol =================
EFI_STATUS FileAuthenticationState(
	IN EFI_SECURITY_ARCH_PROTOCOL *This,
	IN UINT32 AuthenticationStatus,
	IN EFI_DEVICE_PATH_PROTOCOL *File
)
{
    if (File==NULL) return EFI_INVALID_PARAMETER;
    return EFI_SUCCESS;
}

EFI_SECURITY_ARCH_PROTOCOL Security = {FileAuthenticationState};

///////////////////////////////////////////////////////////////
extern EFI_GUID gEfiTianoDecompressProtocolGuid;
extern VOID  *mHobStart;
extern EFI_HANDLE gDxeCoreImageHandle;

extern EFI_PEI_FLUSH_INSTRUCTION_CACHE_PROTOCOL  mFlushInstructionCache;
extern EFI_PEI_PE_COFF_LOADER_PROTOCOL mPeCoffLoader;
extern EFI_PEI_TRANSFER_CONTROL_PROTOCOL mTransferControl;
extern BOOLEAN const InstallDummySecurityProtocol;

VOID *CoreAllocateRuntimeCopyPool (
    IN  UINTN   AllocationSize, CONST IN  VOID    *Buffer
);

typedef struct{
	EFI_GUID *pGuid;
	VOID* pInterface;
} DXE_MAIN_INTERFACES;

DXE_MAIN_INTERFACES DxeMainInterfaces[] = 
{
	{&gEfiPeiFlushInstructionCacheGuid,&mFlushInstructionCache},
	{&gEfiPeiPeCoffLoaderGuid,&mPeCoffLoader},
	{&gEfiPeiTransferControlGuid,&mTransferControl},
	{NULL,NULL}
};

EFI_STATUS CreateHob ( IN UINT16 Type, IN UINT16 Length, IN OUT VOID **Hob ){
	EFI_HOB_HANDOFF_INFO_TABLE           *HandOffHob;
	EFI_HOB_GENERIC_HEADER               *HobEnd;
	EFI_PHYSICAL_ADDRESS                 FreeMemory;
	
	if (mHobStart == NULL) return EFI_NOT_AVAILABLE_YET;
	if (Hob == NULL) return EFI_INVALID_PARAMETER;

	// first entry in the HOB list is the HOB handoff table
	HandOffHob = mHobStart;
	// make sure Hob length is a multiple of 8 bytes.
	Length = (UINT16)((Length + 0x7) & (~0x7));
	// calculate the amount of free memory
	FreeMemory =   HandOffHob->EfiFreeMemoryTop
	             - HandOffHob->EfiFreeMemoryBottom;
	if (FreeMemory < Length) return EFI_OUT_OF_RESOURCES;
	
	// add a HOB to the end of the HOB list
	*Hob = (VOID*)(UINTN) HandOffHob->EfiEndOfHobList;
	((EFI_HOB_GENERIC_HEADER*) *Hob)->HobType = Type;
	((EFI_HOB_GENERIC_HEADER*) *Hob)->HobLength = Length;
	((EFI_HOB_GENERIC_HEADER*) *Hob)->Reserved = 0;
	
	// move a new end of the HOB list past the newly created HOB
	HobEnd = (EFI_HOB_GENERIC_HEADER*) ((UINTN) *Hob + Length);
	HandOffHob->EfiEndOfHobList = (EFI_PHYSICAL_ADDRESS) (UINTN) HobEnd;
	
	// initialize the new end HOB and update the pointer to the bottom of free memory
	HobEnd->HobType   = EFI_HOB_TYPE_END_OF_HOB_LIST;
	HobEnd->HobLength = sizeof(EFI_HOB_GENERIC_HEADER);
	HobEnd->Reserved = 0;
	HobEnd++;
	HandOffHob->EfiFreeMemoryBottom = (EFI_PHYSICAL_ADDRESS) (UINTN) HobEnd;
	
	return EFI_SUCCESS;   
}

VOID AmiDxeInit0()
{
	DXE_MAIN_INTERFACES *p;
	EFI_HOB_CPU *CpuHob;

	//Create CPU HOB
    // NOTE: It is responsibility of CPU and/or NB PEIM to make sure
    // CPU HOB exists and contains valid data.
    // The HOB created here is just to prevent system from hanging
    // in case CPU HOB has not been created.
    // If CPU HOB already exists, the HOB crated here will be ignored 
    // by DXE Core because it uses only the first instance of the HOB.
    CreateHob(EFI_HOB_TYPE_CPU, sizeof(EFI_HOB_CPU), &CpuHob);
	CpuHob->SizeOfMemorySpace=32;
	CpuHob->SizeOfIoSpace=16;
	CpuHob->Reserved[0]=0; 
	CpuHob->Reserved[1]=0;
	CpuHob->Reserved[2]=0;
	CpuHob->Reserved[3]=0;
	CpuHob->Reserved[4]=0;
	CpuHob->Reserved[5]=0;

	// =========== Initialize DxeMain Pointers
	for(p=DxeMainInterfaces; p->pGuid; p++)
	{
		PEIM_HOB *PeimHob;
		CreateHob(EFI_HOB_TYPE_GUID_EXTENSION, sizeof(PEIM_HOB),&PeimHob);
		PeimHob->Header.Name = *p->pGuid;
		PeimHob->DxeEntryPoint= p->pInterface;
	}
}

VOID AmiDxeInit1()
{
	InitAmiLib(gDxeCoreImageHandle,gST);

	// =========== Initialize System Table Fields
    gST->FirmwareVendor = CoreAllocateRuntimeCopyPool(
        sizeof( CONVERT_TO_WSTRING(CORE_VENDOR)), CONVERT_TO_WSTRING(CORE_VENDOR)
    );
	gST->FirmwareRevision = CORE_COMBINED_VERSION;
}

VOID AmiDxeInit2()
{
	VOID *p=mHobStart;
	EFI_HANDLE Handle = NULL;

//  TheImageHandle is a library variable, which is initialized in the InitAmiLib library function
//	However, in our case InitAmiLib has been called too early (from AmiDxeInit1), before 
//  gDxeCoreImageHandle has beeen initialized. It is initialized now.
//  Let's update TheImageHandle variable.
    TheImageHandle = gDxeCoreImageHandle;
	// =========== Initialize modules inherited from PEI ======================= //	
#ifndef EFIx64 //no code sharing between PEI and DXE in x64 mode
	for(p = mHobStart; !EFI_ERROR(FindNextHobByType(EFI_HOB_TYPE_GUID_EXTENSION,&p)); )
	{
		PEIM_HOB *pHob = (PEIM_HOB*)p;
		static EFI_GUID guidAmiPeimHob = AMI_PEIM_HOB_GUID;
		if (guidcmp(&pHob->Header.Name,&guidAmiPeimHob)) continue;
		pHob->DxeEntryPoint(gDxeCoreImageHandle,gST);
	}
#endif
	// ========================================================================== //
	gST->BootServices->InstallMultipleProtocolInterfaces(
		&Handle,
		&guidDecompress, &DecompressInterface,
		&gEfiTianoDecompressProtocolGuid, &DecompressInterface,
		&guidPrint, &Print,
        &gEfiDevicePathUtilitiesProtocolGuid, &DevicePathUtilitiesInstance,
        &gEfiDevicePathFromTextProtocolGuid, &EfiDevicePathFromTextProtocol,
        &gEfiDevicePathToTextProtocolGuid, &EfiDevicePathToTextProtocol,
		NULL
	);
    if (InstallDummySecurityProtocol)
    	gST->BootServices->InstallMultipleProtocolInterfaces(
    		&Handle, &gEfiSecurityArchProtocolGuid, &Security, NULL
    	);
	InitParts(gDxeCoreImageHandle,gST);
}

EFI_STATUS AmiResetNotAvailableYet(
	IN EFI_RESET_TYPE ResetType, IN EFI_STATUS ResetStatus,
	IN UINTN DataSize, IN CHAR16 *ResetData OPTIONAL
)
{
    ERROR_CODE(DXE_RESET_NOT_AVAILABLE,EFI_ERROR_MINOR);
    return EFI_NOT_AVAILABLE_YET;;
}

VOID AmiReportArhcProtocolMissingError(){
    ERROR_CODE(DXE_ARCH_PROTOCOL_NOT_AVAILABLE,EFI_ERROR_MAJOR);
}

//GUID definitions

/// Library routines
#ifdef EFI_DEBUG
#define EFI_D_ERROR       0x80000000          // Error
#define EFI_DBUG_MASK   (EFI_D_ERROR)
#define EFI_D_LOAD        0x00000004          // Load events
UINTN gErrorLevel=EFI_DBUG_MASK | EFI_D_LOAD;

VOID
EfiDebugAssert (
  IN CHAR8    *FileName,
  IN INTN     LineNumber,
  IN CHAR8    *Description
  )
 {
	Trace(TRACE_ALWAYS,"ASSERT in %s on %i: %s\n",FileName, LineNumber, Description);
	EFI_DEADLOOP()
 }

VOID
EfiDebugVPrint (
  IN  UINTN   ErrorLevel,
  IN  CHAR8   *Format,
  IN  va_list ArgList
  )
  {
    if (!gST) return;
#if EFI_SPECIFICATION_VERSION<0x20000
	if (!(gST->RuntimeServices && gST->RuntimeServices->ReportStatusCode)) return;
#endif
	if (!(gErrorLevel & ErrorLevel)) return;
    PrintDebugMessageVaList(ErrorLevel, Format, ArgList);
  }

CHAR8* GetDriverName(EFI_DRIVER_BINDING_PROTOCOL *pDriver)
{
	static char sName[0x100];
	if (   !GetImageName(pDriver->DriverBindingHandle,sName)
		&& !GetImageName(pDriver->ImageHandle,sName)
	) Sprintf_s(sName,sizeof(sName),"Unknown");
	return sName;
}
#endif
//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (C)Copyright 1985-2011, American Megatrends, Inc.         **
//**                                                                  **
//**                       All Rights Reserved.                       **
//**                                                                  **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093        **
//**                                                                  **
//**                       Phone: (770)-246-8600                      **
//**                                                                  **
//**********************************************************************
//**********************************************************************