C to C++ migration and refactoring #17

Merged
harraiken merged 67 commits from cxxtest into master 2025-09-24 20:18:35 +02:00
3 changed files with 70 additions and 1 deletions
Showing only changes of commit 6c66028800 - Show all commits

View File

@@ -362,6 +362,13 @@ Configuration::InitializeBootMenuList(IN ULONG MaxNameLength,
return STATUS_EFI_SUCCESS; return STATUS_EFI_SUCCESS;
} }
/**
* Initializes the XTLDR configuration subsystem.
*
* @return This routine does not return any value.
*
* @since XT 1.0
*/
XTCDECL XTCDECL
VOID VOID
Configuration::InitializeConfiguration() Configuration::InitializeConfiguration()

View File

@@ -387,9 +387,16 @@ Debug::PutChar(IN WCHAR Character)
return HL::ComPort::WriteComPort(&SerialPort, Buffer[0]); return HL::ComPort::WriteComPort(&SerialPort, Buffer[0]);
} }
/**
* Determines if the serial port has been successfully initialized and is ready for communication.
*
* @return This routine returns TRUE if the serial port is initialized and ready, FALSE otherwise.
*
* @since XT 1.0
*/
XTCDECL XTCDECL
BOOLEAN BOOLEAN
Debug::SerialPortReady() Debug::SerialPortReady()
{ {
return (BOOLEAN)(SerialPort.Flags & COMPORT_FLAG_INIT); return (BOOLEAN)(SerialPort.Flags & COMPORT_FLAG_INIT);
belliash marked this conversation as resolved Outdated

Do we need to typecast each bool operation? In C this returned integer value and compiler knew how to cast it into our BOOLEAN enum. In C++, result value is of bool type that cannot be automatically typecasted into enum.

Do we need to typecast each bool operation? In C this returned integer value and compiler knew how to cast it into our BOOLEAN enum. In C++, result value is of bool type that cannot be automatically typecasted into enum.

No, we do not have to. Alternatively, I can suggest moving the BOOLEAN type definition from xttypes.h to xtcompat.h:

#ifdef __cplusplus
    /* C++ definitions */
    #define XTCLINK         extern "C"
    #define NULLPTR         nullptr

    /* C++ boolean type */
    typedef bool BOOLEAN, *PBOOLEAN;
    #define TRUE true
    #define FALSE false

    /* C++ widechar type */
    typedef wchar_t wchar;
#else
    /* C definitions */
    #define XTCLINK
    #define NULLPTR         ((void *)0)

    /* C boolean type */
    typedef enum _BOOLEAN
    {
        FALSE = 0,
        TRUE = 1
    } BOOLEAN, *PBOOLEAN;

    /* C widechar type */
    typedef unsigned short wchar;
#endif

This will allow us to maintain backward compatibility with C and get rid of explicit typecasts at the same time.
This should also work in the case of the quoted fragment, even though the result of the operation is not of type bool but int. The result of the expression will be 0 if the flag is not set, or the value of COMPORT_FLAG_INIT if it is. It is only the comparison of this result, for example (SerialPort.Flags & COMPORT_FLAG_INIT) != 0, that actually yields a bool type.

Please let me know if you accept this, then I will commit the proposed change.

No, we do not have to. Alternatively, I can suggest moving the BOOLEAN type definition from xttypes.h to xtcompat.h: ``` #ifdef __cplusplus /* C++ definitions */ #define XTCLINK extern "C" #define NULLPTR nullptr /* C++ boolean type */ typedef bool BOOLEAN, *PBOOLEAN; #define TRUE true #define FALSE false /* C++ widechar type */ typedef wchar_t wchar; #else /* C definitions */ #define XTCLINK #define NULLPTR ((void *)0) /* C boolean type */ typedef enum _BOOLEAN { FALSE = 0, TRUE = 1 } BOOLEAN, *PBOOLEAN; /* C widechar type */ typedef unsigned short wchar; #endif ``` This will allow us to maintain backward compatibility with C and get rid of explicit typecasts at the same time. This should also work in the case of the quoted fragment, even though the result of the operation is not of type bool but int. The result of the expression will be 0 if the flag is not set, or the value of COMPORT_FLAG_INIT if it is. It is only the comparison of this result, for example (SerialPort.Flags & COMPORT_FLAG_INIT) != 0, that actually yields a bool type. Please let me know if you accept this, then I will commit the proposed change.

You are right, this results in int, not bool. There are more such changes like this in the diff, and I added a comment to the first one, as its the only one that fits on first page. unfortunately. Never mind, I think the proposed solution will be OK.

You are right, this results in int, not bool. There are more such changes like this in the diff, and I added a comment to the first one, as its the only one that fits on first page. unfortunately. Never mind, I think the proposed solution will be OK.
} }

View File

@@ -9,6 +9,13 @@
#include <xtldr.hh> #include <xtldr.hh>
/**
* Disables access to EFI Boot Services.
*
* @return This routine does not return any value.
*
* @since XT 1.0
*/
XTCDECL XTCDECL
VOID VOID
XtLoader::DisableBootServices() XtLoader::DisableBootServices()
@@ -17,6 +24,13 @@ XtLoader::DisableBootServices()
} }
/**
* Queries the availability of EFI Boot Services.
*
* @return This routine returns TRUE if EFI Boot Services are available, FALSE otherwise.
*
* @since XT 1.0
*/
XTCDECL XTCDECL
BOOLEAN BOOLEAN
XtLoader::GetBootServicesStatus() XtLoader::GetBootServicesStatus()
@@ -24,6 +38,13 @@ XtLoader::GetBootServicesStatus()
return LoaderStatus.BootServices; return LoaderStatus.BootServices;
} }
/**
* Retrieves the EFI image handle.
*
* @return This routine returns a handle to the EFI-loaded image.
*
* @since XT 1.0
*/
XTCDECL XTCDECL
EFI_HANDLE EFI_HANDLE
XtLoader::GetEfiImageHandle() XtLoader::GetEfiImageHandle()
@@ -31,6 +52,13 @@ XtLoader::GetEfiImageHandle()
return XtLoader::EfiImageHandle; return XtLoader::EfiImageHandle;
} }
/**
* Retrieves the EFI system table pointer.
*
* @return This routine returns a pointer to the EFI system table.
*
* @since XT 1.0
*/
XTCDECL XTCDECL
PEFI_SYSTEM_TABLE PEFI_SYSTEM_TABLE
XtLoader::GetEfiSystemTable() XtLoader::GetEfiSystemTable()
@@ -38,6 +66,19 @@ XtLoader::GetEfiSystemTable()
return XtLoader::EfiSystemTable; return XtLoader::EfiSystemTable;
} }
/**
* Provides base address and size of the XTLDR image.
*
* @param LoaderBase
* Supplies a pointer to a variable that receives the base address of the XTLDR image.
*
* @param LoaderSize
* Supplies a pointer to a variable that receives the size of the XTLDR image.
*
* @return This routine does not return any value.
*
* @since XT 1.0
*/
XTCDECL XTCDECL
VOID VOID
XtLoader::GetLoaderImageInformation(PVOID *LoaderBase, XtLoader::GetLoaderImageInformation(PVOID *LoaderBase,
@@ -47,6 +88,13 @@ XtLoader::GetLoaderImageInformation(PVOID *LoaderBase,
*LoaderSize = XtLoader::LoaderStatus.LoaderSize; *LoaderSize = XtLoader::LoaderStatus.LoaderSize;
} }
/**
* Retrieves the Secure Boot status.
*
* @return This routine returns SecureBoot status.
*
* @since XT 1.0
*/
XTCDECL XTCDECL
INT_PTR INT_PTR
XtLoader::GetSecureBootStatus() XtLoader::GetSecureBootStatus()
@@ -143,6 +191,13 @@ XtLoader::RegisterBootMenu(IN PVOID BootMenuRoutine)
BootMenu = (PBL_XT_BOOT_MENU)BootMenuRoutine; BootMenu = (PBL_XT_BOOT_MENU)BootMenuRoutine;
} }
/**
* Invokes either a custom boot menu handler, if one has been registered, or displays the default boot menu.
*
* @return This routine does not return any value.
*
* @since XT 1.0
*/
XTCDECL XTCDECL
VOID VOID
XtLoader::ShowBootMenu() XtLoader::ShowBootMenu()