gnu-efi/lib/init.c

210 lines
4.0 KiB
C
Raw Normal View History

2013-01-31 01:25:25 +04:00
/*++
Copyright (c) 1998 Intel Corporation
Module Name:
Abstract:
Revision History
--*/
#include "lib.h"
VOID
EFIDebugVariable (
VOID
);
VOID
InitializeLib (
IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable
)
/*++
Routine Description:
Initializes EFI library for use
Arguments:
Firmware's EFI system table
Returns:
None
--*/
{
EFI_LOADED_IMAGE *LoadedImage;
EFI_STATUS Status;
CHAR8 *LangCode;
Make ELF constructors and destructors work This makes setup and teardown functions defined with __attribute__((__constructor__) and __attribute__((__destructor__)) work in normal circumstances in EFI binaries. A couple of notes: - it implements both the old-style .ctors/.dtors methods and the newer style .init_array/.fini_array ELF constructor and destructor arrays, processed in the order: .init_array[] .ctors[] efi_main() .dtors[] .fini_array[] - Destructors will only be called if efi_main() exits using "return"; any call to Exit() will still longjmp() past them. - InitializeLib() has already been called before constructors run, so they don't need to call it (and neither does anything else.) For compatibility, it has been altered so calling it more than once is safe. - No attempt is made to handle any constructor or destructor with a prototype other than "void func(void);", but note that InitializeLib has been called, so LibImageHandle, ST, BS, and RT are set. - The init_array/ctor/dtor/fini_array lists aren't the using the GNU "CONSTRUCTOR" output section command, so they don't start with a size. - The lists are individually sorted during the link stage via SORT_BY_NAME() in the linker script. - The default (empty) init_array/ctor/dtor/fini_array lists are padded out to 8-byte alignment with ".p2align 3, 0", and each list always has at least one ".long 0" at the end of it (even if it's completely empty). As a result, they can have NULLs that need to be skipped. The sections they're in are mergeable, so the NULLs don't have to be exclusively at the end. - The ia64 and mips64el arches have not been tested. Signed-off-by: Peter Jones <pjones@redhat.com>
2023-03-28 15:28:40 +03:00
if (LibInitialized)
return;
2013-01-31 01:25:25 +04:00
Make ELF constructors and destructors work This makes setup and teardown functions defined with __attribute__((__constructor__) and __attribute__((__destructor__)) work in normal circumstances in EFI binaries. A couple of notes: - it implements both the old-style .ctors/.dtors methods and the newer style .init_array/.fini_array ELF constructor and destructor arrays, processed in the order: .init_array[] .ctors[] efi_main() .dtors[] .fini_array[] - Destructors will only be called if efi_main() exits using "return"; any call to Exit() will still longjmp() past them. - InitializeLib() has already been called before constructors run, so they don't need to call it (and neither does anything else.) For compatibility, it has been altered so calling it more than once is safe. - No attempt is made to handle any constructor or destructor with a prototype other than "void func(void);", but note that InitializeLib has been called, so LibImageHandle, ST, BS, and RT are set. - The init_array/ctor/dtor/fini_array lists aren't the using the GNU "CONSTRUCTOR" output section command, so they don't start with a size. - The lists are individually sorted during the link stage via SORT_BY_NAME() in the linker script. - The default (empty) init_array/ctor/dtor/fini_array lists are padded out to 8-byte alignment with ".p2align 3, 0", and each list always has at least one ".long 0" at the end of it (even if it's completely empty). As a result, they can have NULLs that need to be skipped. The sections they're in are mergeable, so the NULLs don't have to be exclusively at the end. - The ia64 and mips64el arches have not been tested. Signed-off-by: Peter Jones <pjones@redhat.com>
2023-03-28 15:28:40 +03:00
LibInitialized = TRUE;
LibFwInstance = FALSE;
LibImageHandle = ImageHandle;
2013-01-31 01:25:25 +04:00
Make ELF constructors and destructors work This makes setup and teardown functions defined with __attribute__((__constructor__) and __attribute__((__destructor__)) work in normal circumstances in EFI binaries. A couple of notes: - it implements both the old-style .ctors/.dtors methods and the newer style .init_array/.fini_array ELF constructor and destructor arrays, processed in the order: .init_array[] .ctors[] efi_main() .dtors[] .fini_array[] - Destructors will only be called if efi_main() exits using "return"; any call to Exit() will still longjmp() past them. - InitializeLib() has already been called before constructors run, so they don't need to call it (and neither does anything else.) For compatibility, it has been altered so calling it more than once is safe. - No attempt is made to handle any constructor or destructor with a prototype other than "void func(void);", but note that InitializeLib has been called, so LibImageHandle, ST, BS, and RT are set. - The init_array/ctor/dtor/fini_array lists aren't the using the GNU "CONSTRUCTOR" output section command, so they don't start with a size. - The lists are individually sorted during the link stage via SORT_BY_NAME() in the linker script. - The default (empty) init_array/ctor/dtor/fini_array lists are padded out to 8-byte alignment with ".p2align 3, 0", and each list always has at least one ".long 0" at the end of it (even if it's completely empty). As a result, they can have NULLs that need to be skipped. The sections they're in are mergeable, so the NULLs don't have to be exclusively at the end. - The ia64 and mips64el arches have not been tested. Signed-off-by: Peter Jones <pjones@redhat.com>
2023-03-28 15:28:40 +03:00
//
// Set up global pointer to the system table, boot services table,
// and runtime services table
//
2013-01-31 01:25:25 +04:00
Make ELF constructors and destructors work This makes setup and teardown functions defined with __attribute__((__constructor__) and __attribute__((__destructor__)) work in normal circumstances in EFI binaries. A couple of notes: - it implements both the old-style .ctors/.dtors methods and the newer style .init_array/.fini_array ELF constructor and destructor arrays, processed in the order: .init_array[] .ctors[] efi_main() .dtors[] .fini_array[] - Destructors will only be called if efi_main() exits using "return"; any call to Exit() will still longjmp() past them. - InitializeLib() has already been called before constructors run, so they don't need to call it (and neither does anything else.) For compatibility, it has been altered so calling it more than once is safe. - No attempt is made to handle any constructor or destructor with a prototype other than "void func(void);", but note that InitializeLib has been called, so LibImageHandle, ST, BS, and RT are set. - The init_array/ctor/dtor/fini_array lists aren't the using the GNU "CONSTRUCTOR" output section command, so they don't start with a size. - The lists are individually sorted during the link stage via SORT_BY_NAME() in the linker script. - The default (empty) init_array/ctor/dtor/fini_array lists are padded out to 8-byte alignment with ".p2align 3, 0", and each list always has at least one ".long 0" at the end of it (even if it's completely empty). As a result, they can have NULLs that need to be skipped. The sections they're in are mergeable, so the NULLs don't have to be exclusively at the end. - The ia64 and mips64el arches have not been tested. Signed-off-by: Peter Jones <pjones@redhat.com>
2023-03-28 15:28:40 +03:00
ST = SystemTable;
BS = SystemTable->BootServices;
RT = SystemTable->RuntimeServices;
// ASSERT (CheckCrc(0, &ST->Hdr));
// ASSERT (CheckCrc(0, &BS->Hdr));
// ASSERT (CheckCrc(0, &RT->Hdr));
//
// Initialize pool allocation type
//
if (ImageHandle) {
Status = uefi_call_wrapper(
BS->HandleProtocol,
3,
ImageHandle,
&LoadedImageProtocol,
(VOID*)&LoadedImage
);
if (!EFI_ERROR(Status)) {
PoolAllocationType = LoadedImage->ImageDataType;
}
EFIDebugVariable ();
2013-01-31 01:25:25 +04:00
}
//
Make ELF constructors and destructors work This makes setup and teardown functions defined with __attribute__((__constructor__) and __attribute__((__destructor__)) work in normal circumstances in EFI binaries. A couple of notes: - it implements both the old-style .ctors/.dtors methods and the newer style .init_array/.fini_array ELF constructor and destructor arrays, processed in the order: .init_array[] .ctors[] efi_main() .dtors[] .fini_array[] - Destructors will only be called if efi_main() exits using "return"; any call to Exit() will still longjmp() past them. - InitializeLib() has already been called before constructors run, so they don't need to call it (and neither does anything else.) For compatibility, it has been altered so calling it more than once is safe. - No attempt is made to handle any constructor or destructor with a prototype other than "void func(void);", but note that InitializeLib has been called, so LibImageHandle, ST, BS, and RT are set. - The init_array/ctor/dtor/fini_array lists aren't the using the GNU "CONSTRUCTOR" output section command, so they don't start with a size. - The lists are individually sorted during the link stage via SORT_BY_NAME() in the linker script. - The default (empty) init_array/ctor/dtor/fini_array lists are padded out to 8-byte alignment with ".p2align 3, 0", and each list always has at least one ".long 0" at the end of it (even if it's completely empty). As a result, they can have NULLs that need to be skipped. The sections they're in are mergeable, so the NULLs don't have to be exclusively at the end. - The ia64 and mips64el arches have not been tested. Signed-off-by: Peter Jones <pjones@redhat.com>
2023-03-28 15:28:40 +03:00
// Initialize Guid table
2013-01-31 01:25:25 +04:00
//
Make ELF constructors and destructors work This makes setup and teardown functions defined with __attribute__((__constructor__) and __attribute__((__destructor__)) work in normal circumstances in EFI binaries. A couple of notes: - it implements both the old-style .ctors/.dtors methods and the newer style .init_array/.fini_array ELF constructor and destructor arrays, processed in the order: .init_array[] .ctors[] efi_main() .dtors[] .fini_array[] - Destructors will only be called if efi_main() exits using "return"; any call to Exit() will still longjmp() past them. - InitializeLib() has already been called before constructors run, so they don't need to call it (and neither does anything else.) For compatibility, it has been altered so calling it more than once is safe. - No attempt is made to handle any constructor or destructor with a prototype other than "void func(void);", but note that InitializeLib has been called, so LibImageHandle, ST, BS, and RT are set. - The init_array/ctor/dtor/fini_array lists aren't the using the GNU "CONSTRUCTOR" output section command, so they don't start with a size. - The lists are individually sorted during the link stage via SORT_BY_NAME() in the linker script. - The default (empty) init_array/ctor/dtor/fini_array lists are padded out to 8-byte alignment with ".p2align 3, 0", and each list always has at least one ".long 0" at the end of it (even if it's completely empty). As a result, they can have NULLs that need to be skipped. The sections they're in are mergeable, so the NULLs don't have to be exclusively at the end. - The ia64 and mips64el arches have not been tested. Signed-off-by: Peter Jones <pjones@redhat.com>
2023-03-28 15:28:40 +03:00
InitializeGuid();
InitializeLibPlatform(ImageHandle,SystemTable);
2013-01-31 01:25:25 +04:00
if (ImageHandle && UnicodeInterface == &LibStubUnicodeInterface) {
LangCode = LibGetVariable (VarLanguage, &EfiGlobalVariable);
InitializeUnicodeSupport (LangCode);
if (LangCode) {
FreePool (LangCode);
}
}
}
VOID
InitializeUnicodeSupport (
CHAR8 *LangCode
)
{
EFI_UNICODE_COLLATION_INTERFACE *Ui;
EFI_STATUS Status;
CHAR8 *Languages;
UINTN Index, Position, Length;
UINTN NoHandles;
EFI_HANDLE *Handles;
//
// If we don't know it, lookup the current language code
//
LibLocateHandle (ByProtocol, &UnicodeCollationProtocol, NULL, &NoHandles, &Handles);
if (!LangCode || !NoHandles) {
goto Done;
}
//
// Check all driver's for a matching language code
//
for (Index=0; Index < NoHandles; Index++) {
Status = uefi_call_wrapper(BS->HandleProtocol, 3, Handles[Index], &UnicodeCollationProtocol, (VOID*)&Ui);
if (EFI_ERROR(Status)) {
continue;
}
//
// Check for a matching language code
//
Languages = Ui->SupportedLanguages;
Length = strlena(Languages);
for (Position=0; Position < Length; Position += ISO_639_2_ENTRY_SIZE) {
//
// If this code matches, use this driver
//
if (CompareMem (Languages+Position, LangCode, ISO_639_2_ENTRY_SIZE) == 0) {
UnicodeInterface = Ui;
goto Done;
}
}
}
Done:
//
// Cleanup
//
if (Handles) {
FreePool (Handles);
}
}
VOID
EFIDebugVariable (
VOID
)
{
EFI_STATUS Status;
UINT32 Attributes;
UINTN DataSize;
UINTN NewEFIDebug;
DataSize = sizeof(EFIDebug);
Status = uefi_call_wrapper(RT->GetVariable, 5, u"EFIDebug", &EfiGlobalVariable, &Attributes, &DataSize, &NewEFIDebug);
2013-01-31 01:25:25 +04:00
if (!EFI_ERROR(Status)) {
EFIDebug = NewEFIDebug;
}
}
/*
* Calls to memset/memcpy may be emitted implicitly by GCC or MSVC
* even when -ffreestanding or /NODEFAULTLIB are in effect.
*/
#ifndef __SIZE_TYPE__
#define __SIZE_TYPE__ UINTN
#endif
void *memset(void *s, int c, __SIZE_TYPE__ n)
{
unsigned char *p = s;
while (n--)
*p++ = c;
return s;
}
void *memcpy(void *dest, const void *src, __SIZE_TYPE__ n)
{
const unsigned char *q = src;
unsigned char *p = dest;
while (n--)
*p++ = *q++;
return dest;
2013-01-31 01:25:25 +04:00
}