2020-09-28 17:57:50 +02:00
|
|
|
/*
|
|
|
|
* Self.cpp
|
|
|
|
*
|
|
|
|
* Created on: Sep 28, 2020
|
|
|
|
* Author: jief
|
|
|
|
*/
|
|
|
|
|
2021-04-28 20:30:34 +02:00
|
|
|
#include "../Settings/Self.h"
|
2020-09-28 17:57:50 +02:00
|
|
|
#include <Platform.h>
|
|
|
|
#include "../refit/lib.h"
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
|
|
|
|
|
|
Self self;
|
|
|
|
|
2021-04-08 17:07:05 +02:00
|
|
|
constexpr const LStringW THEMES_DIRNAME = L"Themes"_XSW;
|
2020-10-03 19:02:31 +02:00
|
|
|
|
|
|
|
EFI_STATUS Self::_openDir(const XStringW& path, bool* b, EFI_FILE** efiDir)
|
2020-09-28 17:57:50 +02:00
|
|
|
{
|
|
|
|
EFI_STATUS Status;
|
2020-11-24 13:41:47 +01:00
|
|
|
Status = m_CloverDir->Open(m_CloverDir, efiDir, path.wc_str(), EFI_FILE_MODE_READ, 0);
|
2020-10-03 19:02:31 +02:00
|
|
|
if ( EFI_ERROR(Status) ) {
|
2020-11-24 13:41:47 +01:00
|
|
|
DBG("Error when opening dir '%ls\\%ls' : %s\n", m_CloverDirFullPath.wc_str(), path.wc_str(), efiStrError(Status));
|
2020-10-03 19:02:31 +02:00
|
|
|
*efiDir = NULL;
|
|
|
|
*b = false;
|
|
|
|
}else{
|
|
|
|
*b = true;
|
|
|
|
}
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
EFI_STATUS Self::_initialize()
|
|
|
|
{
|
|
|
|
EFI_STATUS Status;
|
|
|
|
|
2020-11-24 13:41:47 +01:00
|
|
|
Status = gBS->HandleProtocol(m_SelfImageHandle, &gEfiLoadedImageProtocolGuid, (void **)&m_SelfLoadedImage);
|
2021-02-21 14:49:09 +01:00
|
|
|
#ifdef DEBUG
|
2020-09-28 17:57:50 +02:00
|
|
|
if ( EFI_ERROR(Status) ) panic("Cannot get SelfLoadedImage");
|
2020-11-24 13:41:47 +01:00
|
|
|
if ( m_SelfLoadedImage->DeviceHandle == NULL ) panic("m_SelfLoadedImage->DeviceHandle == NULL");
|
|
|
|
m_SelfDevicePath = DuplicateDevicePath(DevicePathFromHandle(m_SelfLoadedImage->DeviceHandle));
|
2020-09-28 17:57:50 +02:00
|
|
|
if ( m_SelfDevicePath == NULL ) panic("m_SelfDevicePath == NULL");
|
2021-02-21 14:49:09 +01:00
|
|
|
#else
|
|
|
|
if ( EFI_ERROR(Status) ) return Status;
|
|
|
|
if ( m_SelfLoadedImage->DeviceHandle == NULL ) return EFI_NOT_FOUND;
|
|
|
|
m_SelfDevicePath = DuplicateDevicePath(DevicePathFromHandle(m_SelfLoadedImage->DeviceHandle));
|
|
|
|
if ( m_SelfDevicePath == NULL ) return EFI_NOT_FOUND;
|
|
|
|
#endif
|
|
|
|
|
2020-10-23 20:48:31 +02:00
|
|
|
#ifdef JIEF_DEBUG
|
2020-11-24 13:41:47 +01:00
|
|
|
DBG("Self DevicePath()=%ls @%llX\n", FileDevicePathToXStringW(m_SelfDevicePath).wc_str(), (uintptr_t)m_SelfLoadedImage->DeviceHandle);
|
2020-10-23 20:48:31 +02:00
|
|
|
#endif
|
2020-11-24 13:41:47 +01:00
|
|
|
Status = gBS->HandleProtocol(m_SelfLoadedImage->DeviceHandle, &gEfiSimpleFileSystemProtocolGuid, (void**)&m_SelfSimpleVolume);
|
2021-02-21 14:49:09 +01:00
|
|
|
#ifdef DEBUG
|
2020-10-03 19:02:31 +02:00
|
|
|
if ( EFI_ERROR(Status) ) panic("Cannot get m_SelfSimpleVolume");
|
2020-11-24 13:41:47 +01:00
|
|
|
Status = m_SelfSimpleVolume->OpenVolume(m_SelfSimpleVolume, &m_SelfVolumeRootDir);
|
2020-10-03 19:02:31 +02:00
|
|
|
if ( EFI_ERROR(Status) ) panic("Cannot get m_SelfRootDir");
|
2021-02-21 14:49:09 +01:00
|
|
|
#else
|
|
|
|
if ( EFI_ERROR(Status) ) return Status;
|
|
|
|
Status = m_SelfSimpleVolume->OpenVolume(m_SelfSimpleVolume, &m_SelfVolumeRootDir);
|
|
|
|
if ( EFI_ERROR(Status) ) return Status;
|
|
|
|
#endif
|
2020-10-03 19:02:31 +02:00
|
|
|
// find the current directory
|
2020-11-24 13:41:47 +01:00
|
|
|
m_CloverDirFullPath = FileDevicePathToXStringW(m_SelfLoadedImage->FilePath);
|
2020-10-18 15:54:55 +02:00
|
|
|
|
|
|
|
// Do this before the next check.
|
|
|
|
if ( !m_CloverDirFullPath.startWith('\\') ) {
|
2020-11-24 13:41:47 +01:00
|
|
|
//CHAR16* f = ConvertDevicePathToText(m_SelfLoadedImage->FilePath, TRUE, TRUE);
|
2020-10-18 15:54:55 +02:00
|
|
|
//panic("Bad format for m_CloverDirFullPath(%ls). It must start with a '\\'.\nConvertDevicePathToText=%ls", m_CloverDirFullPath.wc_str(), f);
|
|
|
|
//
|
|
|
|
// Somefirmware seems to not put a '\' at the begining. Do not panic anymore, just add it.
|
|
|
|
m_CloverDirFullPath.insertAtPos('\\', 0);
|
|
|
|
}
|
|
|
|
|
2020-11-24 14:14:48 +01:00
|
|
|
m_efiFileName = m_CloverDirFullPath.basename();
|
|
|
|
|
2020-10-03 19:02:31 +02:00
|
|
|
// History : if this Clover was started as BootX64.efi, redirect to /EFI/CLOVER
|
2021-04-11 07:18:52 +02:00
|
|
|
if ( m_CloverDirFullPath.isEqualIC("\\EFI\\Boot\\BootX64.efi") ) {
|
2020-10-17 15:01:33 +02:00
|
|
|
m_CloverDirFullPath.takeValueFrom("\\EFI\\CLOVER\\CloverX64.efi");
|
2020-10-03 19:02:31 +02:00
|
|
|
}
|
2021-02-21 14:49:09 +01:00
|
|
|
#ifdef DEBUG
|
2020-10-17 15:01:33 +02:00
|
|
|
if ( m_CloverDirFullPath.isEmpty() ) panic("m_CloverDirFullPath.isEmpty()");
|
2021-02-21 14:49:09 +01:00
|
|
|
#else
|
|
|
|
if ( m_CloverDirFullPath.isEmpty() ) return EFI_NOT_FOUND;
|
|
|
|
#endif
|
2020-11-24 13:41:47 +01:00
|
|
|
m_SelfDevicePath = FileDevicePath(m_SelfLoadedImage->DeviceHandle, m_CloverDirFullPath);
|
2020-10-03 19:02:31 +02:00
|
|
|
m_SelfDevicePathAsXStringW = FileDevicePathToXStringW(m_SelfDevicePath);
|
|
|
|
|
2021-02-21 14:49:09 +01:00
|
|
|
#ifdef DEBUG
|
2020-10-17 15:01:33 +02:00
|
|
|
if ( m_CloverDirFullPath.lastChar() == U'\\' ) panic("m_CloverDirFullPath.lastChar() == U'\\'");
|
|
|
|
//if ( m_CloverDirFullPath.endsWith('\\') ) panic("m_CloverDirFullPath.endsWith('\\')");
|
2021-02-21 14:49:09 +01:00
|
|
|
#else
|
|
|
|
if ( m_CloverDirFullPath.lastChar() == U'\\' ) return EFI_NOT_FOUND;
|
|
|
|
#endif
|
|
|
|
|
2020-10-03 19:02:31 +02:00
|
|
|
|
2020-10-17 15:01:33 +02:00
|
|
|
size_t i = m_CloverDirFullPath.rindexOf(U'\\', SIZE_T_MAX-1);
|
|
|
|
if ( i != SIZE_T_MAX && i > 0 ) m_CloverDirFullPath.deleteCharsAtPos(i, SIZE_T_MAX);
|
2020-11-24 13:41:47 +01:00
|
|
|
|
2020-10-23 20:48:31 +02:00
|
|
|
#ifdef JIEF_DEBUG
|
2020-10-17 15:01:33 +02:00
|
|
|
DBG("SelfDirPath = %ls\n", m_CloverDirFullPath.wc_str());
|
2020-10-23 20:48:31 +02:00
|
|
|
#endif
|
2020-11-24 13:41:47 +01:00
|
|
|
Status = m_SelfVolumeRootDir->Open(m_SelfVolumeRootDir, &m_CloverDir, m_CloverDirFullPath.wc_str(), EFI_FILE_MODE_READ, 0);
|
2020-10-03 19:02:31 +02:00
|
|
|
if ( EFI_ERROR(Status) ) panic("Cannot open getSelfRootDir()");
|
|
|
|
|
|
|
|
|
|
|
|
_openDir(THEMES_DIRNAME, &m_ThemesDirExists, &m_ThemesDir); // don't need to check returned status as it's ok to not have a themes dir.
|
|
|
|
|
2020-09-28 17:57:50 +02:00
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2020-10-03 19:02:31 +02:00
|
|
|
EFI_STATUS Self::initialize(EFI_HANDLE ImageHandle)
|
|
|
|
{
|
|
|
|
m_SelfImageHandle = ImageHandle;
|
|
|
|
return _initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
EFI_STATUS Self::reInitialize()
|
|
|
|
{
|
|
|
|
closeHandle();
|
|
|
|
return _initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Self::closeHandle(void)
|
|
|
|
{
|
|
|
|
// called before running external programs to close open file handles
|
|
|
|
|
|
|
|
if (m_CloverDir != NULL) {
|
|
|
|
m_CloverDir->Close(m_CloverDir);
|
|
|
|
m_CloverDir = NULL;
|
|
|
|
}
|
|
|
|
|
2020-11-24 13:41:47 +01:00
|
|
|
if (m_SelfVolumeRootDir != NULL) {
|
|
|
|
m_SelfVolumeRootDir->Close(m_SelfVolumeRootDir);
|
|
|
|
m_SelfVolumeRootDir = NULL;
|
2020-10-03 19:02:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|