2020-10-03 19:02:31 +02:00
/*
* Self . cpp
*
* Created on : Sep 28 , 2020
* Author : jief
*/
# include <Platform.h>
2021-04-28 20:30:34 +02:00
# include "../Settings/SelfOem.h"
# include "../Settings/Self.h"
2020-10-03 19:02:31 +02:00
# include "../refit/lib.h"
2021-04-28 20:30:34 +02:00
# define DONT_DEFINE_GLOBALS
2020-10-03 19:02:31 +02:00
# ifndef DEBUG_ALL
# define DEBUG_SELF 1
# else
# define DEBUG_SELF DEBUG_ALL
# endif
# if DEBUG_SELF == 0
# define DBG(...)
# else
# define DBG(...) DebugLog(DEBUG_SELF, __VA_ARGS__)
# endif
2021-04-08 17:07:05 +02:00
constexpr const LStringW KEXTS_DIRNAME = L " Kexts " _XSW ;
2020-10-03 19:02:31 +02:00
SelfOem selfOem ;
2020-10-17 15:01:33 +02:00
//EFI_STATUS SelfOem::_openDir(const XStringW& path, bool* b, EFI_FILE** efiDir)
//{
// EFI_STATUS Status;
// Status = self.getCloverDir().Open(&self.getCloverDir(), efiDir, path.wc_str(), EFI_FILE_MODE_READ, 0);
// if ( EFI_ERROR(Status) ) {
// DBG("Error when opening dir '%ls\\%ls' : %s\n", self.getCloverDirFullPath().wc_str(), path.wc_str(), efiStrError(Status));
// *efiDir = NULL;
// *b = false;
// }else{
// *b = true;
// }
// return Status;
//}
2020-10-03 19:02:31 +02:00
bool SelfOem : : _checkOEMPath ( )
{
EFI_STATUS Status ;
// if ( !selfOem.oemDirExists() ) return false;
2020-10-17 15:01:33 +02:00
// EFI_FILE* efiDir;
Status = self . getCloverDir ( ) . Open ( & self . getCloverDir ( ) , & m_OemDir , m_OemPathRelToSelfDir . wc_str ( ) , EFI_FILE_MODE_READ , 0 ) ;
2020-10-03 19:02:31 +02:00
if ( Status = = EFI_NOT_FOUND ) {
2020-10-17 15:01:33 +02:00
DBG ( " _checkOEMPath Look for oem dir at path '%ls \\ %ls'. Dir doesn't exist. \n " , self . getCloverDirFullPath ( ) . wc_str ( ) , m_OemPathRelToSelfDir . wc_str ( ) ) ;
m_OemDir = NULL ;
2020-10-03 19:02:31 +02:00
return false ;
}
if ( EFI_ERROR ( Status ) ! = EFI_SUCCESS ) {
2020-10-17 15:01:33 +02:00
DBG ( " Cannot open dir at '%ls \\ %ls' dir : %s \n " , self . getCloverDirFullPath ( ) . wc_str ( ) , m_OemPathRelToSelfDir . wc_str ( ) , efiStrError ( Status ) ) ;
m_OemDir = NULL ;
2020-10-03 19:02:31 +02:00
return false ;
}
2020-10-17 15:01:33 +02:00
BOOLEAN res2 = FileExists ( m_OemDir , SWPrintf ( " %s.plist " , m_ConfName . c_str ( ) ) ) ;
2020-10-03 19:02:31 +02:00
if ( ! res2 ) {
2020-10-17 15:01:33 +02:00
DBG ( " _checkOEMPath looked for config file at '%ls \\ %ls \\ %s.plist'. File doesn't exist. \n " , self . getCloverDirFullPath ( ) . wc_str ( ) , m_OemPathRelToSelfDir . wc_str ( ) , m_ConfName . c_str ( ) ) ;
m_OemDir - > Close ( m_OemDir ) ;
m_OemDir = NULL ;
2020-10-03 19:02:31 +02:00
return false ;
}
2020-10-17 15:01:33 +02:00
DBG ( " _checkOEMPath: set OEMPath: '%ls \\ %ls' \n " , self . getCloverDirFullPath ( ) . wc_str ( ) , m_OemPathRelToSelfDir . wc_str ( ) ) ;
2020-10-03 19:02:31 +02:00
return true ;
}
2021-04-28 20:30:34 +02:00
bool SelfOem : : _setOemPathRelToSelfDir ( bool isFirmwareClover , const XString8 & OEMBoard , const XString8 & OEMProduct , INT32 frequency , const XObjArray < LanCardClass > & lanCardArray )
2020-10-03 19:02:31 +02:00
{
2021-04-28 20:30:34 +02:00
for ( size_t idx = 0 ; idx < lanCardArray . size ( ) ; + + idx ) {
m_OemPathRelToSelfDir . SWPrintf ( " OEM \\ %s--%02X-%02X-%02X-%02X-%02X-%02X " , OEMProduct . c_str ( ) , lanCardArray [ idx ] . MacAddress [ 0 ] , lanCardArray [ idx ] . MacAddress [ 1 ] , lanCardArray [ idx ] . MacAddress [ 2 ] , lanCardArray [ idx ] . MacAddress [ 3 ] , lanCardArray [ idx ] . MacAddress [ 4 ] , lanCardArray [ idx ] . MacAddress [ 5 ] ) ;
2020-10-03 19:02:31 +02:00
if ( _checkOEMPath ( ) ) return true ;
}
if ( ! isFirmwareClover ) {
m_OemPathRelToSelfDir . SWPrintf ( " OEM \\ %s \\ UEFI " , OEMBoard . c_str ( ) ) ;
if ( _checkOEMPath ( ) ) return true ;
}
m_OemPathRelToSelfDir . SWPrintf ( " OEM \\ %s " , OEMProduct . c_str ( ) ) ;
if ( _checkOEMPath ( ) ) return true ;
m_OemPathRelToSelfDir . SWPrintf ( " OEM \\ %s-%d " , OEMProduct . c_str ( ) , frequency ) ;
if ( _checkOEMPath ( ) ) return true ;
m_OemPathRelToSelfDir . SWPrintf ( " OEM \\ %s " , OEMBoard . c_str ( ) ) ;
if ( _checkOEMPath ( ) ) return true ;
m_OemPathRelToSelfDir . SWPrintf ( " OEM \\ %s-%d " , OEMBoard . c_str ( ) , frequency ) ;
if ( _checkOEMPath ( ) ) return true ;
2020-10-17 15:01:33 +02:00
// m_OemPathRelToSelfDir.takeValueFrom(".");
// DBG("set OEMPath to \".\"\n");
m_OemPathRelToSelfDir . setEmpty ( ) ;
2020-10-03 19:02:31 +02:00
return false ;
}
EFI_STATUS SelfOem : : _initialize ( )
{
EFI_STATUS Status ;
2020-10-17 15:01:33 +02:00
if ( oemDirExists ( ) ) {
m_OemFulPath = SWPrintf ( " %ls \\ %ls " , self . getCloverDirFullPath ( ) . wc_str ( ) , m_OemPathRelToSelfDir . wc_str ( ) ) ;
m_configDirPathRelToSelfDir = getOemPathRelToSelfDir ( ) ;
m_configDirPathRelToSelfDirWithTrailingSlash . SWPrintf ( " %ls \\ " , m_configDirPathRelToSelfDir . wc_str ( ) ) ;
} else {
m_OemFulPath . setEmpty ( ) ;
m_configDirPathRelToSelfDir . setEmpty ( ) ;
m_configDirPathRelToSelfDirWithTrailingSlash . setEmpty ( ) ;
}
if ( m_OemDir = = NULL ) {
assert ( m_OemPathRelToSelfDir . isEmpty ( ) ) ;
assert ( m_OemFulPath . isEmpty ( ) ) ;
} else {
assert ( m_OemPathRelToSelfDir . notEmpty ( ) ) ;
assert ( m_OemFulPath . notEmpty ( ) ) ;
}
2021-04-23 14:20:48 +02:00
if ( m_KextsDir ! = NULL ) {
log_technical_bug ( " %s : Kexts dir != NULL. " , __PRETTY_FUNCTION__ ) ;
m_KextsDir = NULL ;
}
2020-10-17 15:01:33 +02:00
if ( oemDirExists ( ) ) {
Status = m_OemDir - > Open ( m_OemDir , & m_KextsDir , KEXTS_DIRNAME . wc_str ( ) , EFI_FILE_MODE_READ , 0 ) ;
if ( ! EFI_ERROR ( Status ) ) {
m_KextsPathRelToSelfDir . SWPrintf ( " %ls \\ %ls " , getOemPathRelToSelfDir ( ) . wc_str ( ) , KEXTS_DIRNAME . wc_str ( ) ) ;
m_KextsFullPath . SWPrintf ( " %ls \\ %ls " , getOemFullPath ( ) . wc_str ( ) , KEXTS_DIRNAME . wc_str ( ) ) ;
} else {
DBG ( " Cannot open %ls \\ %ls : %s " , getOemFullPath ( ) . wc_str ( ) , KEXTS_DIRNAME . wc_str ( ) , efiStrError ( Status ) ) ;
m_KextsDir = NULL ;
}
2020-10-03 19:02:31 +02:00
}
2020-10-05 11:14:35 +02:00
// if ( Status != EFI_SUCCESS && Status != EFI_NOT_FOUND ) {
// panic("Cannot open kexts dir %ls\\%ls : %s", getOemFullPath().wc_str(), KEXTS_DIRNAME.wc_str(), efiStrError(Status));
// }
2020-10-17 15:01:33 +02:00
if ( m_KextsDir = = NULL ) {
2020-10-03 19:02:31 +02:00
Status = self . getCloverDir ( ) . Open ( & self . getCloverDir ( ) , & m_KextsDir , KEXTS_DIRNAME . wc_str ( ) , EFI_FILE_MODE_READ , 0 ) ;
2020-10-05 11:14:35 +02:00
if ( EFI_ERROR ( Status ) ) {
2020-10-17 15:01:33 +02:00
DBG ( " Cannot open %ls \\ %ls : %s " , self . getCloverDirFullPath ( ) . wc_str ( ) , KEXTS_DIRNAME . wc_str ( ) , efiStrError ( Status ) ) ;
//panic("Cannot open kexts dir at '%ls\\%ls'", self.getCloverDirFullPath().wc_str(), KEXTS_DIRNAME.wc_str());
2020-10-05 11:14:35 +02:00
m_KextsDir = NULL ;
m_KextsPathRelToSelfDir . setEmpty ( ) ;
m_KextsFullPath . setEmpty ( ) ;
} else {
m_KextsPathRelToSelfDir = KEXTS_DIRNAME ;
2020-10-17 15:01:33 +02:00
m_KextsFullPath . SWPrintf ( " %ls \\ %ls " , self . getCloverDirFullPath ( ) . wc_str ( ) , KEXTS_DIRNAME . wc_str ( ) ) ;
2020-10-05 11:14:35 +02:00
}
2020-10-03 19:02:31 +02:00
} else {
}
2020-10-17 15:01:33 +02:00
if ( m_KextsDir = = NULL ) {
2021-04-23 14:20:48 +02:00
if ( m_KextsPathRelToSelfDir . notEmpty ( ) ) log_technical_bug ( " %s : m_KextsPathRelToSelfDir.notEmpty() " , __PRETTY_FUNCTION__ ) ;
if ( m_KextsFullPath . notEmpty ( ) ) log_technical_bug ( " %s : m_KextsFullPath.notEmpty() " , __PRETTY_FUNCTION__ ) ;
2020-10-17 15:01:33 +02:00
} else {
2021-04-23 14:20:48 +02:00
if ( m_KextsPathRelToSelfDir . isEmpty ( ) ) log_technical_bug ( " %s : m_KextsPathRelToSelfDir.isEmpty() " , __PRETTY_FUNCTION__ ) ;
if ( m_KextsFullPath . isEmpty ( ) ) log_technical_bug ( " %s : m_KextsFullPath.isEmpty() " , __PRETTY_FUNCTION__ ) ;
2020-10-17 15:01:33 +02:00
}
2020-10-23 20:48:31 +02:00
# ifdef JIEF_DEBUG
2020-10-17 15:01:33 +02:00
if ( isKextsDirFound ( ) ) {
DBG ( " Kexts dir = '%ls' \n " , getKextsFullPath ( ) . wc_str ( ) ) ;
} else {
DBG ( " Kexts dir = none \n " ) ;
}
2020-10-23 20:48:31 +02:00
# endif
2020-10-03 19:02:31 +02:00
return EFI_SUCCESS ;
}
2021-04-28 20:30:34 +02:00
EFI_STATUS SelfOem : : initialize ( const XString8 & confName , bool isFirmwareClover , const XString8 & OEMBoard , const XString8 & OEMProduct , INT32 frequency , const XObjArray < LanCardClass > & lanCardArray )
2020-10-03 19:02:31 +02:00
{
2021-04-23 14:20:48 +02:00
if ( m_ConfName . notEmpty ( ) ) {
log_technical_bug ( " %s : cannot be called twice. Use reinitialize. " , __PRETTY_FUNCTION__ ) ;
return EFI_SUCCESS ;
}
// confName must not be empty as it serves as a flag to know if it's initialized or not.
// {confName}.plist will also be used to detect OEM dir.
if ( confName . isEmpty ( ) ) {
log_technical_bug ( " %s : cannot be called with empty config name. " , __PRETTY_FUNCTION__ ) ;
return EFI_SUCCESS ;
}
2020-10-23 08:04:01 +02:00
2020-10-03 19:02:31 +02:00
m_ConfName = confName ;
2020-10-17 15:01:33 +02:00
// Initialise m_OemPathRelToSelfDir and leave m_OemDir opened.
2021-04-28 20:30:34 +02:00
_setOemPathRelToSelfDir ( isFirmwareClover , OEMBoard , OEMProduct , frequency , lanCardArray ) ;
2020-10-17 15:01:33 +02:00
2020-10-23 08:04:01 +02:00
EFI_STATUS Status = _initialize ( ) ;
return Status ;
}
void SelfOem : : unInitialize ( )
{
2021-04-23 14:20:48 +02:00
if ( m_ConfName . isEmpty ( ) ) {
log_technical_bug ( " %s : Already uninitialized. " , __PRETTY_FUNCTION__ ) ;
return ;
}
2020-10-23 08:04:01 +02:00
closeHandle ( ) ;
m_ConfName . setEmpty ( ) ;
2020-10-03 19:02:31 +02:00
}
EFI_STATUS SelfOem : : reInitialize ( )
{
2021-04-23 14:20:48 +02:00
if ( m_ConfName . isEmpty ( ) ) {
log_technical_bug ( " %s : initialize() must called once first " , __PRETTY_FUNCTION__ ) ;
return EFI_LOAD_ERROR ;
}
2020-10-03 19:02:31 +02:00
closeHandle ( ) ;
2020-10-17 15:01:33 +02:00
// No need to call _setOemPathRelToSelfDir again, but need to open m_OemDir, if it exists
if ( oemDirExists ( ) ) {
EFI_STATUS Status = self . getCloverDir ( ) . Open ( & self . getCloverDir ( ) , & m_OemDir , m_OemPathRelToSelfDir . wc_str ( ) , EFI_FILE_MODE_READ , 0 ) ;
if ( EFI_ERROR ( Status ) ) {
2021-04-23 14:20:48 +02:00
log_technical_bug ( " Impossible to reopen dir '%ls \\ %ls', although it was opened the first time : %s " , self . getCloverDirFullPath ( ) . wc_str ( ) , m_OemPathRelToSelfDir . wc_str ( ) , efiStrError ( Status ) ) ;
return Status ;
2020-10-17 15:01:33 +02:00
}
}
2020-10-23 08:04:01 +02:00
EFI_STATUS Status = _initialize ( ) ;
return Status ;
2020-10-03 19:02:31 +02:00
}
2020-10-23 08:04:01 +02:00
void SelfOem : : closeHandle ( )
2020-10-03 19:02:31 +02:00
{
2020-10-23 08:04:01 +02:00
//DBG("%s : enter.\n", __FUNCTION__);
2020-10-17 15:01:33 +02:00
if ( m_KextsDir ! = NULL ) {
m_KextsDir - > Close ( m_KextsDir ) ;
m_KextsDir = NULL ;
m_KextsPathRelToSelfDir . setEmpty ( ) ;
m_KextsFullPath . setEmpty ( ) ;
}
2020-10-03 19:02:31 +02:00
if ( m_OemDir ! = NULL ) {
m_OemDir - > Close ( m_OemDir ) ;
m_OemDir = NULL ;
2020-10-17 15:01:33 +02:00
// m_OemPathRelToSelfDir.setEmpty(); // do not empty m_OemPathRelToSelfDir, we need it in reInitialize()
// m_OemFulPath.setEmpty(); // doesn't matter, it'll be re-initialised in _initialize()
2020-10-03 19:02:31 +02:00
}
2020-10-23 08:04:01 +02:00
//DBG("%s : leave.\n", __FUNCTION__);
2020-10-03 19:02:31 +02:00
}