diff --git a/BaseHdr/stdarg.h b/BaseHdr/stdarg.h deleted file mode 100644 index 72097b3b..00000000 --- a/BaseHdr/stdarg.h +++ /dev/null @@ -1,66 +0,0 @@ -/** -* BSD 2-Clause License -* -* Copyright (c) 2022, Manas Kamal Choudhury -* All rights reserved. -* -* Redistribution and use in source and binary forms, with or without -* modification, are permitted provided that the following conditions are met: -* -* 1. Redistributions of source code must retain the above copyright notice, this -* list of conditions and the following disclaimer. -* -* 2. Redistributions in binary form must reproduce the above copyright notice, -* this list of conditions and the following disclaimer in the documentation -* and/or other materials provided with the distribution. -* -* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE -* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, -* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -* -**/ - -#ifndef __STDARG_H__ -#define __STDARG_H__ - - -#include - - -#ifdef __cplusplus -extern "C" -{ -#endif - - /* width of stack == width of int */ -#define STACKITEM int64_t - - /* round up width of objects pushed on stack. The expression before the - & ensures that we get 0 for objects of size 0. */ -#define VA_SIZE(TYPE) \ - ((sizeof(TYPE)+sizeof(STACKITEM)-1) \ - & ~(sizeof(STACKITEM)-1)) - - /* &(LASTARG) points to the LEFTMOST argument of the function call - (before the ...) */ -#define va_start(AP, LASTARG) \ - (AP = ((va_list)&(LASTARG)+VA_SIZE(LASTARG))) - - /* nothing for va_end */ -#define va_end(AP) - -#define va_arg(AP, TYPE) \ - (AP += VA_SIZE(TYPE), *((TYPE *)(AP - VA_SIZE(TYPE)))) - -#ifdef __cplusplus -} -#endif - -#endif \ No newline at end of file diff --git a/BaseHdr/stdint.h b/BaseHdr/stdint.h deleted file mode 100644 index 080af0bf..00000000 --- a/BaseHdr/stdint.h +++ /dev/null @@ -1,235 +0,0 @@ -/** -* BSD 2-Clause License -* -* Copyright (c) 2022, Manas Kamal Choudhury -* All rights reserved. -* -* Redistribution and use in source and binary forms, with or without -* modification, are permitted provided that the following conditions are met: -* -* 1. Redistributions of source code must retain the above copyright notice, this -* list of conditions and the following disclaimer. -* -* 2. Redistributions in binary form must reproduce the above copyright notice, -* this list of conditions and the following disclaimer in the documentation -* and/or other materials provided with the distribution. -* -* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE -* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, -* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -* -**/ - -#ifndef __STDINT_H__ -#define __STDINT_H__ - - -#define __need_wint_t -#define __need_wchar_t - -typedef unsigned char BOOL; - -#ifdef ARCH_ARM64 -#ifndef __cplusplus -#ifndef bool -typedef BOOL bool; -#define true 1 -#define false 0 -#endif -#endif -#endif - - -/* 7.18.1.1 Exact-width integer types */ -typedef signed char int8_t; -typedef unsigned char uint8_t; -typedef short int16_t; -typedef unsigned short uint16_t; -typedef int int32_t; -typedef unsigned uint32_t; -typedef long long int64_t; -typedef unsigned long long uint64_t; - -typedef uint8_t uint8; -typedef uint16_t uint16; -typedef uint32_t uint32; -typedef uint64_t uint64; -typedef int8_t int8; -typedef int16_t int16; -typedef int32_t int32; -typedef int64_t int64; - - -/* 7.18.1.2 Minimum-width integer types */ -typedef signed char int_least8_t; -typedef unsigned char uint_least8_t; -typedef short int_least16_t; -typedef unsigned short uint_least16_t; -typedef int int_least32_t; -typedef unsigned uint_least32_t; -typedef long long int_least64_t; -typedef unsigned long long uint_least64_t; - -/* 7.18.1.3 Fastest minimum-width integer types -* Not actually guaranteed to be fastest for all purposes -* Here we use the exact-width types for 8 and 16-bit ints. -*/ -typedef char int_fast8_t; -typedef unsigned char uint_fast8_t; -typedef short int_fast16_t; -typedef unsigned short uint_fast16_t; -typedef int int_fast32_t; -typedef unsigned int uint_fast32_t; -typedef long long int_fast64_t; -typedef unsigned long long uint_fast64_t; - -/* 7.18.1.4 Integer types capable of holding object pointers */ -typedef int intptr_t; -typedef unsigned uintptr_t; - -/* 7.18.1.5 Greatest-width integer types */ -typedef long long intmax_t; -typedef unsigned long long uintmax_t; - -typedef uint64_t size_t; - -/* 7.18.2 Limits of specified-width integer types */ -#if defined ( __cplusplus) || defined (__STDC_LIMIT_MACROS) - -/* 7.18.2.1 Limits of exact-width integer types */ -#define INT8_MIN (-128) -#define INT16_MIN (-32768) -#define INT32_MIN (-2147483647 - 1) -#define INT64_MIN (-9223372036854775807LL - 1) - -#define INT8_MAX 127 -#define INT16_MAX 32767 -#define INT32_MAX 2147483647 -#define INT64_MAX 9223372036854775807LL - -#define UINT8_MAX 0xff /* 255U */ -#define UINT16_MAX 0xffff /* 65535U */ -#define UINT32_MAX 0xffffffff /* 4294967295U */ -#define UINT64_MAX 0xffffffffffffffffULL /* 18446744073709551615ULL */ - -/* 7.18.2.2 Limits of minimum-width integer types */ -#define INT_LEAST8_MIN INT8_MIN -#define INT_LEAST16_MIN INT16_MIN -#define INT_LEAST32_MIN INT32_MIN -#define INT_LEAST64_MIN INT64_MIN - -#define INT_LEAST8_MAX INT8_MAX -#define INT_LEAST16_MAX INT16_MAX -#define INT_LEAST32_MAX INT32_MAX -#define INT_LEAST64_MAX INT64_MAX - -#define UINT_LEAST8_MAX UINT8_MAX -#define UINT_LEAST16_MAX UINT16_MAX -#define UINT_LEAST32_MAX UINT32_MAX -#define UINT_LEAST64_MAX UINT64_MAX - -/* 7.18.2.3 Limits of fastest minimum-width integer types */ -#define INT_FAST8_MIN INT8_MIN -#define INT_FAST16_MIN INT16_MIN -#define INT_FAST32_MIN INT32_MIN -#define INT_FAST64_MIN INT64_MIN - -#define INT_FAST8_MAX INT8_MAX -#define INT_FAST16_MAX INT16_MAX -#define INT_FAST32_MAX INT32_MAX -#define INT_FAST64_MAX INT64_MAX - -#define UINT_FAST8_MAX UINT8_MAX -#define UINT_FAST16_MAX UINT16_MAX -#define UINT_FAST32_MAX UINT32_MAX -#define UINT_FAST64_MAX UINT64_MAX - -/* 7.18.2.4 Limits of integer types capable of holding -object pointers */ -#define INTPTR_MIN INT32_MIN -#define INTPTR_MAX INT32_MAX -#define UINTPTR_MAX UINT32_MAX - -/* 7.18.2.5 Limits of greatest-width integer types */ -#define INTMAX_MIN INT64_MIN -#define INTMAX_MAX INT64_MAX -#define UINTMAX_MAX UINT64_MAX - -/* 7.18.3 Limits of other integer types */ -#define PTRDIFF_MIN INT32_MIN -#define PTRDIFF_MAX INT32_MAX - -#define SIG_ATOMIC_MIN INT32_MIN -#define SIG_ATOMIC_MAX INT32_MAX - -#define SIZE_MAX 0xFFFFF //UINT32_MAX - -#ifndef WCHAR_MIN /* also in wchar.h */ -#define WCHAR_MIN 0 -#define WCHAR_MAX ((wchar_t)-1) /* UINT16_MAX */ -#endif - -/* -* wint_t is unsigned short for compatibility with MS runtime -*/ -#define WINT_MIN 0 -#define WINT_MAX ((wint_t)-1) /* UINT16_MAX */ - -#endif /* !defined ( __cplusplus) || defined __STDC_LIMIT_MACROS */ - - -/* 7.18.4 Macros for integer constants */ -#if !defined ( __cplusplus) || defined __STDC_LIMIT_MACROS - -/* 7.18.4.1 Macros for minimum-width integer constants */ - -#define INT8_C(val) ((int8_t) + (val)) -#define UINT8_C(val) ((uint8_t) + (val##U)) -#define INT16_C(val) ((int16_t) + (val)) -#define UINT16_C(val) ((uint16_t) + (val##U)) - -#define INT32_C(val) val##L -#define UINT32_C(val) val##UL -#define INT64_C(val) val##LL -#define UINT64_C(val) val##ULL - -/* 7.18.4.2 Macros for greatest-width integer constants */ -#define INTMAX_C(val) INT64_C(val) -#define UINTMAX_C(val) UINT64_C(val) - -#endif /* !defined ( __cplusplus) || defined __STDC_CONSTANT_MACROS */ - -#ifdef __cplusplus -extern "C++" { - template intptr_t raw_diff(T* p1, U* p2) - { - return (intptr_t)p1 - (intptr_t)p2; - }; - template T raw_offset(U p1, const intptr_t offset) - { - return (T)((size_t)p1 + offset); - }; - template T mem_after(U* p1) - { - return (T)(&p1[1]); - }; -}; -#endif - -#define RAW_OFFSET(type, x, offset) (type)((size_t)x + offset) -#define RAW_DIFF(p1,p2) ((intptr_t)p1 - (intptr_t)p2) -#define MEM_AFTER(type, p) ((type)(&(p)[1])) - -#define DIV_ROUND_UP(x, y) \ - ((x + y - 1) / y) - -#define ALIGN_UP(x, y) (DIV_ROUND_UP(x,y)*y) - -#endif \ No newline at end of file diff --git a/Boot/Boot.vcxproj b/Boot/Boot.vcxproj index f8cb5330..93807010 100644 --- a/Boot/Boot.vcxproj +++ b/Boot/Boot.vcxproj @@ -50,7 +50,7 @@ Application true - v142 + v143 Unicode @@ -109,6 +109,7 @@ BOOTx64 .efi false + ..\Build\EFI\BOOT\ true @@ -159,14 +160,15 @@ true false - + + false false StreamingSIMDExtensions2 ..\BaseHdr;$(ProjectDir)include;$(ProjectDir)include\AArch64;%(AdditionalIncludeDirectories) - true - Default - Default + false + stdc17 + stdcpp17 EFI Application @@ -178,7 +180,7 @@ true true false - UseLinkTimeCodeGeneration + Default false false false @@ -264,6 +266,7 @@ + diff --git a/Boot/Boot.vcxproj.filters b/Boot/Boot.vcxproj.filters index 0346270d..f3e8885f 100644 --- a/Boot/Boot.vcxproj.filters +++ b/Boot/Boot.vcxproj.filters @@ -10,6 +10,7 @@ + diff --git a/Boot/Makefile b/Boot/Makefile new file mode 100644 index 00000000..881a41aa --- /dev/null +++ b/Boot/Makefile @@ -0,0 +1,78 @@ +# Target EFI binary +TARGET_EFI = BOOTx64.efi +TARGET_SO = BOOTx64.so + +# Setting OBJCOPY +OBJCOPY = objcopy + +# Source files +CPP_SRCS = clib.cpp file.cpp mem.cpp paging.cpp pe.cpp physm.cpp video.cpp xnldr.cpp xnout.cpp +ASM_SRCS = lowlevel.asm + +# Object directory +OBJDIR = obj + +# GNU-EFI Source Directory +GNU_EFI_DIR = /home/cortexauth/Repos/gnu-efi-4.0.2 + +# Include directories +INCLUDES = -I../BaseHdr -Iinclude -Iinclude/x64 -I$(GNU_EFI_DIR)/inc + +# Compiler flags +CXXFLAGS = -Wno-error=pragmas -mno-red-zone -mno-avx -fPIE \ + -Wall -Wextra -Werror -funsigned-char -fshort-wchar \ + -fno-strict-aliasing -ffreestanding -fno-stack-protector -fno-stack-check \ + -fno-merge-all-constants -std=c++17 -DGNU_EFI_USE_MS_ABI -maccumulate-outgoing-args -DGNU_EFI_USE_MS_ABI $(INCLUDES) + +# NASM flags +NASMFLAGS = -f win64 + +# GNU-EFI link setup +LDSCRIPT = ${GNU_EFI_DIR}/gnuefi/elf_x86_64_efi.lds +CRT0 = ${GNU_EFI_DIR}/x86_64/gnuefi/crt0-efi-x86_64.o +LIBS = -lgnuefi -lefi +LIBDIRS = -L${GNU_EFI_DIR}/x86_64/gnuefi -L${GNU_EFI_DIR}//x86_64/lib + +# Tools +CXX = g++ +NASM = nasm +LD = ld +CP = cp + +# Output paths +SO_OUTPUT = $(TARGET_SO) +EFI_OUTPUT = Build/EFI/BOOT/$(TARGET_EFI) +COPY_DEST = $(XENEVA_BUILDS)/EFI/BOOT/$(TARGET_EFI) + +# Object files +OBJS = $(patsubst %.cpp,$(OBJDIR)/%.o,$(CPP_SRCS)) $(OBJDIR)/lowlevel.o + +.PHONY: all clean + +all: $(EFI_OUTPUT) + +# Link x64 EFI binary +$(SO_OUTPUT): $(CRT0) $(OBJS) + $(LD) -nostdlib --warn-common --no-undefined --fatal-warnings --build-id=sha1 \ + -z nocombreloc -shared -Bsymbolic $(LIBDIRS) -T$(LDSCRIPT) $(CRT0) $(OBJS) -o $@ $(LIBS) + +$(EFI_OUTPUT): $(SO_OUTPUT) + @mkdir -p $(dir $@) + $(OBJCOPY) -j .text -j .sdata -j .data -j .rodata -j .dynamic -j .dynsym -j .rel -j .rela \ + -j .rel.* -j .rela.* \ + -j .reloc --target=efi-app-x86_64 --subsystem=10 $< $@ +# $(CP) $@ $(COPY_DEST) + +# Compile C++ source files +$(OBJDIR)/%.o: %.cpp + @mkdir -p $(OBJDIR) + $(CXX) $(CXXFLAGS) -c $< -o $@ + +# Assemble NASM source +$(OBJDIR)/lowlevel.o: lowlevel.asm + @mkdir -p $(OBJDIR) + $(NASM) $(NASMFLAGS) -o $@ $< + +clean: + rm -rf $(OBJDIR) Build $(SO_OUTPUT) $(EFI_OUTPUT) + diff --git a/Boot/clib.cpp b/Boot/clib.cpp index 9501d282..5d7e6967 100644 --- a/Boot/clib.cpp +++ b/Boot/clib.cpp @@ -27,7 +27,7 @@ * **/ -#include +#include #include "clib.h" @@ -104,7 +104,7 @@ int is_digit(int c) { char* chars = (char*)"0123456789ABCDEF"; -char* sztoa(size_t value, char* str, int base) { +char* sztoa(std::size_t value, char* str, int base) { if (base < 2 || base > 16) return nullptr; @@ -124,8 +124,8 @@ char* sztoa(size_t value, char* str, int base) { } -size_t strlen(const char* s){ - size_t l = 0; +std::size_t strlen(const char* s){ + std::size_t l = 0; while (*s++)++l; return l; } diff --git a/Boot/clib.h b/Boot/clib.h index 102a38e4..5b61820a 100644 --- a/Boot/clib.h +++ b/Boot/clib.h @@ -31,31 +31,8 @@ #define __CLIB_H__ -#include - -#ifdef __cplusplus -extern "C" -{ -#endif - -#define STACKITEM int - -#define VA_SIZE(TYPE) \ - ((sizeof(TYPE) + sizeof(STACKITEM) - 1) \ - & ~(sizeof(STACKITEM) - 1)) - -#define va_start(AP, LASTARG) \ - (AP=((va_list)&(LASTARG) + VA_SIZE(LASTARG))) - -#define va_end(AP) - -#define va_arg(AP, TYPE) \ - (AP += VA_SIZE(TYPE), *((TYPE *)(AP - VA_SIZE(TYPE)))) - -#ifdef __cplusplus -} -#endif - +#include +#include extern void memset(void* targ, uint8_t val, uint32_t len); extern void memcpy(void* targ, void* src, uint32_t len); @@ -65,8 +42,6 @@ extern uint32_t wstrsize(wchar_t* s); extern int to_upper(int c); extern int to_lower(int c); extern int is_digit(int c); -extern char* sztoa(size_t value, char* str, int base); -extern size_t strlen(const char* s); -#endif - - +extern char* sztoa(std::size_t value, char* str, int base); +extern std::size_t strlen(const char* s); +#endif \ No newline at end of file diff --git a/Boot/file.cpp b/Boot/file.cpp index 5bf9dd90..f33be7c2 100644 --- a/Boot/file.cpp +++ b/Boot/file.cpp @@ -28,42 +28,6 @@ **/ #include "file.h" -#include -#include -#include -#include - -EFI_GUID FileSystemProtocol = { - 0x964E5B22, - 0x6459, - 0x11D2, - { - 0x8E, 0x39, 0x00, 0xA0, 0xC9, 0x69, 0x72, 0x3B - } -}; - -EFI_GUID LoadFileProtocol = { - 0x56EC3091, - 0x954C, - 0x11D2, - { - 0x8E, 0x3F, 0x00, 0xA0, 0xC9, 0x69, 0x72, 0x3B - } -}; - - -EFI_GUID GenericFileInfo = { - 0x9576E92, - 0x6D3F, - 0x11D2, - { - 0x8E, 0x39, 0x00, 0xA0, 0xC9, 0x69, 0x72, 0x3B - } -}; - -#define EFI_FILE_SYSTEM_INFO_ID \ -{0x9576e93, 0x6d3f, 0x11d2, {0x8e, 0x39, 0x0, 0xa0, 0xc9, 0x69, 0x72, 0x3b }} - /* * XEOpenAndReadFile -- open and reads a file @@ -110,7 +74,7 @@ XEFile* XEOpenAndReadFile(EFI_HANDLE ImageHandle,CHAR16* Filename) { return 0; } - Status = File->GetInfo(File, &GenericFileInfo, &FileInfoSize, NULL); + Status = File->GetInfo(File, &gEfiFileInfoGuid, &FileInfoSize, NULL); if (Status == EFI_BUFFER_TOO_SMALL) { Status = gBS->AllocatePool(EfiBootServicesData, FileInfoSize, (VOID**)&FileInfo); if (EFI_ERROR(Status)) { @@ -120,7 +84,7 @@ XEFile* XEOpenAndReadFile(EFI_HANDLE ImageHandle,CHAR16* Filename) { } } - Status = File->GetInfo(File, &GenericFileInfo, &FileInfoSize, FileInfo); + Status = File->GetInfo(File, &gEfiFileInfoGuid, &FileInfoSize, FileInfo); if (EFI_ERROR(Status)) { XEGuiPrint("Failed to get file metadata \n"); gBS->FreePool(FileInfo); diff --git a/Boot/file.h b/Boot/file.h index c61346f0..e0138259 100644 --- a/Boot/file.h +++ b/Boot/file.h @@ -33,7 +33,17 @@ #include "xnldr.h" #include "clib.h" #include "xnout.h" -#include +#ifdef _MSC_VER + #include + #include + #include + #include + #include + #include +#elif __GNUC__ + #include + #include +#endif typedef struct _XEFILE_ { diff --git a/Boot/guid.cpp b/Boot/guid.cpp new file mode 100644 index 00000000..fd0f6bae --- /dev/null +++ b/Boot/guid.cpp @@ -0,0 +1,4 @@ +#include +#include + +EFI_GUID gEfiFileInfoGuid = EFI_FILE_INFO_ID; \ No newline at end of file diff --git a/Boot/include/Common.hpp b/Boot/include/Common.hpp new file mode 100644 index 00000000..e962a830 --- /dev/null +++ b/Boot/include/Common.hpp @@ -0,0 +1,80 @@ +#ifndef __COMMON_HPP__ +#define __COMMON_HPP__ + +#include +#include +#include +#ifdef _MSC_VER + #include +#elif __GNUC__ + #include +#endif + +// We need UINT64 handling because of use cases +// like EFI_PHYSICAL_ADDRESS + +// Helper trait: true if T is pointer or uint64_t +template +struct is_ptr_or_uint64 : std::disjunction< + std::is_pointer, + std::is_same> +> {}; + +// raw_diff for pointers and uint64_t +template ::value, int> = 0> +std::ptrdiff_t raw_diff(T p1, T p2) +{ + if constexpr (std::is_pointer::value) + { + return reinterpret_cast(p1) - reinterpret_cast(p2); + } + else + { + return static_cast(p1 - p2); + } +} + +// raw_offset: add byte offset to pointer or integer address +template ::value && is_ptr_or_uint64::value, int> = 0> +T raw_offset(U p1, std::ptrdiff_t offset) +{ + if constexpr (std::is_pointer::value) + { + return reinterpret_cast( + reinterpret_cast(p1) + offset + ); + } + else + { + return static_cast(p1 + offset); + } +} + +// mem_after: get next element +template ::value, int> = 0> +T mem_after(T p1) +{ + if constexpr (std::is_pointer::value) + { + return p1 + 1; + } + else + { + return p1 + 1; + } +} + +// Macros for the same +#define RAW_OFFSET(type, x, offset) (type)((size_t)x + offset) +#define RAW_DIFF(p1,p2) ((intptr_t)p1 - (intptr_t)p2) +#define MEM_AFTER(type, p) ((type)(&(p)[1])) + +#define DIV_ROUND_UP(x, y) \ + ((x + y - 1) / y) + +#define ALIGN_UP(x, y) (DIV_ROUND_UP(x,y)*y) + +#endif // __COMMON_HPP__ \ No newline at end of file diff --git a/Boot/lowlevel.h b/Boot/lowlevel.h index 29f2a63d..27a012f9 100644 --- a/Boot/lowlevel.h +++ b/Boot/lowlevel.h @@ -30,7 +30,7 @@ #ifndef __LOW_LEVEL_H__ #define __LOW_LEVEL_H__ -#include +#include #ifdef __cplusplus extern "C" { diff --git a/Boot/mem.cpp b/Boot/mem.cpp index ede3e99b..c0d8229d 100644 --- a/Boot/mem.cpp +++ b/Boot/mem.cpp @@ -28,6 +28,9 @@ **/ #include "xnldr.h" +#ifdef __GNUC__ +#include +#endif /* * XEAllocatePool -- allocate pool memory @@ -35,7 +38,7 @@ */ void* XEAllocatePool(const uint64_t sz) { void* Buffer; - return (gBS->AllocatePool(EfiLoaderData, sz, &Buffer) < 0) ? NULL : Buffer; + return (gBS->AllocatePool(EfiLoaderData, sz, &Buffer) != EFI_SUCCESS) ? NULL : Buffer; } /* diff --git a/Boot/paging.cpp b/Boot/paging.cpp index a01eb25d..40d382bd 100644 --- a/Boot/paging.cpp +++ b/Boot/paging.cpp @@ -32,6 +32,7 @@ #include "lowlevel.h" #include "xnldr.h" #include "xnout.h" +#include void* pdptr; void* pml4ptr; @@ -75,7 +76,7 @@ static size_t decanonical(void* addr) typedef size_t* (*get_tab_ptr)(void*); typedef size_t(*get_tab_index)(void*); -static PML4_ENTRY* getPML4(void* addr) +static PML4_ENTRY* getPML4([[maybe_unused]] void* addr) { if (pml4ptr); else { @@ -105,7 +106,7 @@ static size_t getPDindex(void* addr) return (decanonical(addr) >> 21) & 0x1FF; } -static PD_ENTRY* getPDIR(void* addr) +[[maybe_unused]] static PD_ENTRY* getPDIR( [[maybe_unused]] void* addr) { if (pdptr); else @@ -117,12 +118,12 @@ static PD_ENTRY* getPDIR(void* addr) } -static size_t getPDIRindex(void* addr) +[[maybe_unused]] static size_t getPDIRindex(void* addr) { return ((size_t)addr >> 21) & 0x3FF; } -static PTAB_ENTRY* getPTAB(void* addr) +[[maybe_unused]] static PTAB_ENTRY* getPTAB(void* addr) { return (PD_ENTRY*)make_canonical((void*)((recursive_slot << 39) | ((decanonical(addr) >> 9) & 0x7FFFFFF000))); } @@ -190,7 +191,7 @@ void identity_map_4kb(uint64_t logical) int pdp_idx = (logical >> 30) & 0x1ff; int pd_idx = (logical >> 21) & 0x1ff; int pt_idx = (logical >> 12) & 0x1ff; - int p_idx = logical & 0x7ff; + [[maybe_unused]] int p_idx = logical & 0x7ff; if (!(pml4e[pml4_index] & PAGING_PRESENT)) { @@ -456,7 +457,7 @@ void* arch_get_pml4ptr() { } -void fill_arch_paging_info(void* info) +void fill_arch_paging_info([[maybe_unused]] void* info) { info = &paging_info; paging_info.recursive_slot = recursive_slot; diff --git a/Boot/pe.cpp b/Boot/pe.cpp index 2873e706..a0a76cce 100644 --- a/Boot/pe.cpp +++ b/Boot/pe.cpp @@ -32,6 +32,7 @@ #include "paging.h" #include "xnout.h" #include "physm.h" +#include static void copy_mem(void* dst, void* src, size_t length) { uint8_t* dstp = (uint8_t*)dst; @@ -58,8 +59,6 @@ void* XEPELoadImage(void* filebuf1){ PIMAGE_DOS_HEADER dosHeader = (PIMAGE_DOS_HEADER)filebuf; PIMAGE_NT_HEADERS ntHeaders = raw_offset(dosHeader, dosHeader->e_lfanew); - - PSECTION_HEADER sectionHeader = raw_offset(&ntHeaders->OptionalHeader, ntHeaders->FileHeader.SizeOfOptionaHeader); size_t ImageBase = ntHeaders->OptionalHeader.ImageBase; void* ImBase = (void*)ImageBase; diff --git a/Boot/pe.h b/Boot/pe.h index 8ca6844a..4feb183c 100644 --- a/Boot/pe.h +++ b/Boot/pe.h @@ -31,7 +31,7 @@ #define __PE_H__ -#include +#include #include "xnldr.h" #pragma pack(push, 1) diff --git a/Boot/physm.cpp b/Boot/physm.cpp index 9db884e4..37ab895f 100644 --- a/Boot/physm.cpp +++ b/Boot/physm.cpp @@ -29,11 +29,8 @@ #include "xnldr.h" #include "xnout.h" -#include #include "physm.h" - -typedef uint64_t EFI_PHYSICAL_ADDRESS; - +#include paddr_t* pagestack; paddr_t* stackptr; @@ -61,9 +58,8 @@ void XEInitialisePmmngr(const struct EfiMemoryMap memmap, void* buffer, size_t b allocatedCount = 1; EFI_MEMORY_DESCRIPTOR* current = memmap.memmap; - while (raw_diff(current, memmap.memmap) < memmap.MemMapSize) + while (static_cast(raw_diff(current, memmap.memmap)) < memmap.MemMapSize) { - ramSize += current->NumberOfPages * 4096; if (current->Type == EfiConventionalMemory){//|| current->Type == EfiPersistentMemory){ paddr_t addr = current->PhysicalStart; @@ -77,12 +73,12 @@ void XEInitialisePmmngr(const struct EfiMemoryMap memmap, void* buffer, size_t b numpages *= (EFI_PAGE_SIZE / PAGESIZE); mem_blocks++; - while (numpages > 0 && raw_diff(stackptr, pagestack) < bufsize){ + while (numpages > 0 && static_cast(raw_diff(stackptr, pagestack)) < bufsize){ *stackptr++ = addr; --numpages; addr += PAGESIZE; } - if (raw_diff(stackptr, pagestack) >= bufsize) + if (static_cast(raw_diff(stackptr, pagestack)) >= bufsize) break; } current = raw_offset(current, memmap.DescriptorSize); @@ -120,7 +116,6 @@ void XEPmmngrFree(paddr_t addr) { * XEPmmngrList -- list all available physical block */ void XEPmmngrList() { - int i = 10; while (allocatedCount) { uint64_t addr = *allocatedPtr--; XEGuiPrint("Address -> %x \n", addr); @@ -128,12 +123,12 @@ void XEPmmngrList() { } } -static struct _pmmngr_boot_info_ { +[[maybe_unused]] static struct _pmmngr_boot_info_ { paddr_t* pgstack; //paddr_t* pgstack; paddr_t* alstack; paddr_t* alstackptr; -}pmmngr_boot_info; +} pmmngr_boot_info; /* *XEGetAlstack -- return the allocated stack ptr diff --git a/Boot/physm.h b/Boot/physm.h index a482f832..f96ee75f 100644 --- a/Boot/physm.h +++ b/Boot/physm.h @@ -32,7 +32,11 @@ #include "xnldr.h" #include "xnout.h" -#include +#ifdef _MSC_VER + #include +#elif __GNUC__ + #include +#endif typedef EFI_PHYSICAL_ADDRESS paddr_t; #define PAGESIZE 4096 diff --git a/Boot/video.cpp b/Boot/video.cpp index 98aa1806..e3e4c8de 100644 --- a/Boot/video.cpp +++ b/Boot/video.cpp @@ -28,6 +28,7 @@ **/ #include "video.h" +#include FRAMEBUFFER_INFORMATION fbinfo; size_t xpos; @@ -91,24 +92,27 @@ EFI_STATUS XEInitialiseGraphics(EFI_GRAPHICS_OUTPUT_PROTOCOL* GraphicsOutput) { switch (GraphicsOutput->Mode->Info->PixelFormat) { - case PixelRedGreenBlueReserved8BitPerColor: - fbinfo.redmask = 0xFF; - fbinfo.greenmask = 0xFF00; - fbinfo.bluemask = 0xFF0000; - fbinfo.resvmask = 0xFF000000; - break; - case PixelBlueGreenRedReserved8BitPerColor: - fbinfo.redmask = 0xFF0000; - fbinfo.greenmask = 0xFF00; - fbinfo.bluemask = 0xFF; - fbinfo.resvmask = 0xFF000000; - break; - case PixelBitMask: - fbinfo.redmask = GraphicsOutput->Mode->Info->PixelInformation.RedMask; - fbinfo.greenmask = GraphicsOutput->Mode->Info->PixelInformation.GreenMask; - fbinfo.bluemask = GraphicsOutput->Mode->Info->PixelInformation.BlueMask; - fbinfo.resvmask = GraphicsOutput->Mode->Info->PixelInformation.ReservedMask; - break; + case PixelRedGreenBlueReserved8BitPerColor: + fbinfo.redmask = 0xFF; + fbinfo.greenmask = 0xFF00; + fbinfo.bluemask = 0xFF0000; + fbinfo.resvmask = 0xFF000000; + break; + case PixelBlueGreenRedReserved8BitPerColor: + fbinfo.redmask = 0xFF0000; + fbinfo.greenmask = 0xFF00; + fbinfo.bluemask = 0xFF; + fbinfo.resvmask = 0xFF000000; + break; + case PixelBitMask: + fbinfo.redmask = GraphicsOutput->Mode->Info->PixelInformation.RedMask; + fbinfo.greenmask = GraphicsOutput->Mode->Info->PixelInformation.GreenMask; + fbinfo.bluemask = GraphicsOutput->Mode->Info->PixelInformation.BlueMask; + fbinfo.resvmask = GraphicsOutput->Mode->Info->PixelInformation.ReservedMask; + break; + default: + // Do nothing? + break; } xpos = 0; @@ -154,7 +158,7 @@ void XEGraphicsPutC(char str) { for (size_t y = 0; y < 16; ++y) { for (size_t x = 0; x < 8; ++x) { - const bx_fontcharbitmap_t& entry = bx_vgafont[str]; + const bx_fontcharbitmap_t& entry = bx_vgafont[static_cast(str)]; if (entry.data[y] & (1 << x)) { XEPutPixel(x + xpos * 9, y + ypos * 16, foreground); } @@ -170,7 +174,7 @@ void XEGraphicsPutC(char str) { uint32_t* lfb = fbinfo.phyaddr; if (ypos + 1 > h_res / 16) { - for (int i = 16; i < h_res * v_res; i++) + for (size_t i = 16; i < h_res * v_res; i++) lfb[i] = lfb[i + v_res * 16]; ypos--; } @@ -184,11 +188,7 @@ void XEGraphicsPutC(char str) { void XEGraphicsPuts(const char* str){ while (*str){ - - if (*str > 0xFF){ - //unicode - } - else if (*str == '\n') { + if (*str == '\n') { ++ypos; xpos = 0; } @@ -199,8 +199,7 @@ void XEGraphicsPuts(const char* str){ --xpos; } else { - - const bx_fontcharbitmap_t entry = bx_vgafont[*str]; + const bx_fontcharbitmap_t entry = bx_vgafont[static_cast(*str)]; for (size_t y = 0; y < 16; ++y){ for (size_t x = 0; x < 8; ++x){ @@ -228,7 +227,7 @@ void XEGraphicsPuts(const char* str){ /* Scroll */ if (ypos + 1 > v_res / 16) { - for (int i = 16; i < v_res * h_res; i++) + for (size_t i = 16; i < v_res * h_res; i++) fbinfo.phyaddr[i] = fbinfo.phyaddr[i + h_res * 16]; ypos--; } diff --git a/Boot/video.h b/Boot/video.h index b60b273d..bdfe941e 100644 --- a/Boot/video.h +++ b/Boot/video.h @@ -32,9 +32,13 @@ #include "xnldr.h" #include "font.h" -#include -#include #include "clib.h" +#ifdef _MSC_VER + #include + #include +#elif __GNUC__ + #include +#endif #define RGB(r, g, b) \ diff --git a/Boot/xnldr.cpp b/Boot/xnldr.cpp index ea169de4..a1e7c8ee 100644 --- a/Boot/xnldr.cpp +++ b/Boot/xnldr.cpp @@ -27,14 +27,11 @@ * **/ -#include -#include -#include -#include #include "xnout.h" #include "xnldr.h" #include "video.h" #include "file.h" +#include "clib.h" #include "pe.h" #include "physm.h" #include "paging.h" @@ -43,14 +40,16 @@ /* global variable */ EFI_HANDLE gImageHandle; EFI_SYSTEM_TABLE* gSystemTable; + +// TODO : Look into this quirk between GNU-EFI and our build process on MSVC +#ifdef _MSC_VER EFI_BOOT_SERVICES* gBS; +#endif + EFI_RUNTIME_SERVICES* gRS; EFI_LOADED_IMAGE_PROTOCOL* xnldr2; EFI_GRAPHICS_OUTPUT_PROTOCOL* gop; - -#define ACPI_20_TABLE_GUID {0x8868e871, 0xe4f1, 0x11d3, 0xbc, 0x22, 0x00, 0x80, 0xc7, 0x3c, 0x88, 0x81} - /* * XEGUIDMatch -- compares two given GUID * @param guid1 -- GUID one @@ -76,13 +75,15 @@ bool XEGUIDMatch(EFI_GUID guid1, EFI_GUID guid2) { EFI_STATUS XEInitialiseLib(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE* SystemTable) { gImageHandle = ImageHandle; gSystemTable = SystemTable; +#ifdef _MSC_VER gBS = gSystemTable->BootServices; +#endif gRS = gSystemTable->RuntimeServices; EFI_STATUS Status; EFI_GUID loadedimageprot = EFI_LOADED_IMAGE_PROTOCOL_GUID; EFI_LOADED_IMAGE_PROTOCOL* loadedimage = nullptr; - if (Status = gBS->HandleProtocol(gImageHandle, &loadedimageprot, (void**)&loadedimage)) + if ((Status = gBS->HandleProtocol(gImageHandle, &loadedimageprot, (void**)&loadedimage)) != EFI_SUCCESS) { return Status; } @@ -96,15 +97,14 @@ typedef struct { }MENU_ITEM; MENU_ITEM MenuItem[] = { - {(CHAR16*)L"640x480"}, - {(CHAR16*)L"1024x768"}, - {(CHAR16*)L"1280x1024"}, - {(CHAR16*)L"1920x1080"} + {(CHAR16*)u"640x480"}, + {(CHAR16*)u"1024x768"}, + {(CHAR16*)u"1280x1024"}, + {(CHAR16*)u"1920x1080"} }; #define MENU_SIZE (sizeof(MenuItem)/sizeof(MenuItem[0])) - /* * XEGetScreenResolutionMode -- Provides a selection based menu * to user of supported screen resolution by XenevaOS @@ -117,11 +117,11 @@ int XEGetScreenResolutionMode(EFI_SYSTEM_TABLE* SystemTable) { while (1) { SystemTable->ConOut->SetCursorPosition(SystemTable->ConOut, 0, 0); XESetTextAttribute(0, EFI_WHITE); - XEPrintf(const_cast(L"XenevaOS loader (XNLDR) 2.0 \r\n")); + XEPrintf(u"XenevaOS loader (XNLDR) 2.0 \r\n"); XESetTextAttribute(0, EFI_LIGHTGRAY); - XEPrintf(const_cast(L"Copyright (C) Manas Kamal Choudhury 2020-2025 \r\n")); - XEPrintf(const_cast(L"Select a screen resolution:\r\n")); - XEPrintf(const_cast(L"\r\n")); + XEPrintf(u"Copyright (C) Manas Kamal Choudhury 2020-2025 \r\n"); + XEPrintf(u"Select a screen resolution:\r\n"); + XEPrintf(u"\r\n"); for (UINTN i = 0; i < MENU_SIZE; i++) { if (i == SelectedIndex) { SystemTable->ConOut->SetAttribute(SystemTable->ConOut, EFI_WHITE | EFI_BACKGROUND_BLUE); @@ -129,8 +129,7 @@ int XEGetScreenResolutionMode(EFI_SYSTEM_TABLE* SystemTable) { else { SystemTable->ConOut->SetAttribute(SystemTable->ConOut, EFI_LIGHTGRAY); } - XEPrintf(const_cast(L"%s\r\n"), MenuItem[i].Label); - + XEPrintf(u"%s\r\n", MenuItem[i].Label); } @@ -163,15 +162,15 @@ int XEGetScreenResolutionMode(EFI_SYSTEM_TABLE* SystemTable) { * @param SystemTable -- Pointer to EFI SYSTEM TABLE * @param index -- User selection index */ -UINTN XESetGraphicsMode(EFI_SYSTEM_TABLE* SystemTable, int index) { +UINTN XESetGraphicsMode( [[maybe_unused]] EFI_SYSTEM_TABLE* SystemTable, int index) { EFI_GRAPHICS_OUTPUT_PROTOCOL* GraphicsOutput; EFI_GUID gopguid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID; EFI_STATUS Status; + // TODO: Are these default values correct? UINTN Mode, MaxMode = 0; - EFI_INPUT_KEY Key; - int dwidth = 0; - int dheight = 0; + UINT32 dwidth = 0; + UINT32 dheight = 0; switch (index) { case 1: @@ -190,13 +189,12 @@ UINTN XESetGraphicsMode(EFI_SYSTEM_TABLE* SystemTable, int index) { Status = gBS->LocateProtocol(&gopguid, NULL, (VOID**)&GraphicsOutput); if (EFI_ERROR(Status)) { - XEPrintf(const_cast(L"XNLDR 2.0 Failed to locate Graphics Output protocol \r\n")); + XEPrintf(u"XNLDR 2.0 Failed to locate Graphics Output protocol \r\n"); return Status; } - MaxMode = GraphicsOutput->Mode->MaxMode; - XEPrintf(const_cast(L"Available Screen Resolution:\r\n")); + XEPrintf(u"Available Screen Resolution:\r\n"); for (UINTN i = 0; i < MaxMode; i++) { EFI_GRAPHICS_OUTPUT_MODE_INFORMATION* Info; UINTN Size; @@ -211,18 +209,21 @@ UINTN XESetGraphicsMode(EFI_SYSTEM_TABLE* SystemTable, int index) { return Mode; } +EFI_STATUS efi_main_handler(EFI_HANDLE, EFI_SYSTEM_TABLE*); +extern "C" EFI_STATUS efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE* SystemTable) +{ + return efi_main_handler(ImageHandle, SystemTable); - +} /* * efi_main -- main entry of XNLDR 2.0 * @param ImageHandle -- System parameter * @param SystemTable -- System parameter */ -EFI_STATUS efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE* SystemTable) { +EFI_STATUS efi_main_handler(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE* SystemTable) { EFI_STATUS Status; - Status = XEInitialiseLib(ImageHandle, SystemTable); XEClearScreen(); @@ -231,7 +232,7 @@ EFI_STATUS efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE* SystemTable) { /* Get user graphics resolution choice*/ int index = XEGetScreenResolutionMode(SystemTable); /* Set the graphics resolution based on user selection */ - UINTN Mode = XESetGraphicsMode(SystemTable, index); + [[maybe_unused]] UINTN Mode = XESetGraphicsMode(SystemTable, index); XEGuiPrint("XenevaOS Loader 2.0 (XNLDR) \n"); XEGuiPrint("Copyright (C) Manas Kamal Choudhury 2020-2025\n"); @@ -258,7 +259,11 @@ EFI_STATUS efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE* SystemTable) { *------------------------------------------------------------------- */ void* xdsp_address = NULL; - static EFI_GUID acpi_guid = ACPI_20_TABLE_GUID; +#ifdef __MSC_VER + static EFI_GUID acpi_guid = EFI_ACPI_20_TABLE_GUID; +#else + static EFI_GUID acpi_guid = ACPI_20_TABLE_GUID; +#endif for (unsigned i = 0; i < gSystemTable->NumberOfTableEntries; ++i) { if (XEGUIDMatch(acpi_guid, configuration_tables[i].VendorGuid)) { xdsp_address = configuration_tables[i].VendorTable; @@ -268,8 +273,14 @@ EFI_STATUS efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE* SystemTable) { const size_t EARLY_PAGE_STACK_SIZE = 1024 * 1024; EFI_PHYSICAL_ADDRESS earlyPhyPageStack = 0; - if ((SystemTable->BootServices->AllocatePages(AllocateAnyPages, EfiLoaderData, EARLY_PAGE_STACK_SIZE / EFI_PAGE_SIZE, (EFI_PHYSICAL_ADDRESS*)&earlyPhyPageStack))) - XEGuiPrint("Early Page Stack: allocation failed.....\n"); + if ( + (Status = SystemTable->BootServices->AllocatePages( + AllocateAnyPages, EfiLoaderData, + EARLY_PAGE_STACK_SIZE / EFI_PAGE_SIZE, (EFI_PHYSICAL_ADDRESS*)&earlyPhyPageStack)) != EFI_SUCCESS) + { + XEGuiPrint("Early Page Stack: allocation failed, %x", Status); + for(;;); + } /*Status = gSystemTable->BootServices->SetWatchdogTimer(0, 0, 0, 0); if (Status != EFI_SUCCESS) @@ -277,7 +288,6 @@ EFI_STATUS efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE* SystemTable) { struct EfiMemoryMap map; - EFI_MEMORY_DESCRIPTOR* desc_ptr = nullptr; map.MemMapSize = 0; map.MapKey = map.DescriptorSize = map.DescriptorVersion = 0; map.memmap = 0; @@ -294,6 +304,7 @@ EFI_STATUS efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE* SystemTable) { //give a nice bit of room to spare map.MemMapSize += 16 * map.DescriptorSize; //sizeof(EFI_MEMORY_DESCRIPTOR); + XEGuiPrint("Memory Map size -> %d \n", map.MemMapSize); map.memmap = (EFI_MEMORY_DESCRIPTOR*)XEAllocatePool(map.MemMapSize); @@ -303,7 +314,6 @@ EFI_STATUS efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE* SystemTable) { //XEGraphicsClearScreen(gop); - Status = SystemTable->BootServices->ExitBootServices(ImageHandle, map.MapKey); if (Status != EFI_SUCCESS) { XEGuiPrint("Exit Boot Service Failed \n"); @@ -316,17 +326,13 @@ EFI_STATUS efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE* SystemTable) { /* initilise paging */ XEInitialisePaging(); - - void* base = XEPELoadImage(krnl->kBuffer); - XEImageEntry kentry = (XEImageEntry)XEPEGetEntryPoint(krnl->kBuffer); - + [[maybe_unused]] void* base = XEPELoadImage(krnl->kBuffer); + [[maybe_unused]] XEImageEntry kentry = (XEImageEntry)XEPEGetEntryPoint(krnl->kBuffer); uint64_t ahciAddr = XEPELoadDLLImage(kAhci->kBuffer); uint64_t nvmeAddr = XEPELoadDLLImage(kNvme->kBuffer); //uint64_t xhciAddr = XEPELoadDLLImage(kXHCI->kBuffer); - - void* stackaddr = (void*)0xFFFFA00000000000; void* idtaddr = (void*)0xFFFFD80000000000; @@ -371,5 +377,3 @@ EFI_STATUS efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE* SystemTable) { for (;;); return EFI_SUCCESS; } - - diff --git a/Boot/xnldr.h b/Boot/xnldr.h index 0e49ffe7..5630f610 100644 --- a/Boot/xnldr.h +++ b/Boot/xnldr.h @@ -31,15 +31,17 @@ #ifndef __XNLDR2_H__ #define __XNLDR2_H__ -#include +#include +#include +#ifdef _MSC_VER #include - -#define SIZE_MAX 0xFFFFFFFF - - -#define EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID \ -{0x9042a9de,0x23dc,0x4a38,\ -{0x96,0xfb,0x7a,0xde,0xd0,0x80,0x51,0x6a}} +#include +#include +#include +#include +#elif __GNUC__ +#include +#endif #pragma pack(push,1) @@ -106,7 +108,7 @@ typedef void(*XEImageEntry)(XEBootInfo*); struct EfiMemoryMap { EFI_MEMORY_DESCRIPTOR* memmap; - uint64_t MemMapSize, MapKey, DescriptorSize; + UINT64 MemMapSize, MapKey, DescriptorSize; UINT32 DescriptorVersion; }; diff --git a/Boot/xnout.cpp b/Boot/xnout.cpp index 67a18fab..8724035f 100644 --- a/Boot/xnout.cpp +++ b/Boot/xnout.cpp @@ -31,8 +31,7 @@ #include "xnldr.h" #include "clib.h" #include "video.h" - -typedef unsigned char* va_list; +#include /* * XEClearScreen -- clears the screen @@ -83,7 +82,7 @@ void XEPutChar(const int ch) { unsigned short text[2]; text[0] = (unsigned short)ch; text[1] = 0; - gSystemTable->ConOut->OutputString(gSystemTable->ConOut, text); + gSystemTable->ConOut->OutputString(gSystemTable->ConOut, reinterpret_cast(text)); } /* @@ -91,45 +90,49 @@ void XEPutChar(const int ch) { * show output to the screen * @param fmt -- format to print */ -int XEPrintf(wchar_t* fmt, ...) { - va_list vl = (va_list)((uint8_t*)&fmt + sizeof(unsigned short*)); +int XEPrintf(const char16_t* fmt, ...) { + const char16_t* fmtPtr = fmt; + + va_list vl; + va_start(vl, fmt); - unsigned short out[1024]; + CHAR16 out[1024]; int o = 0; - int c, sign, width, precision, lmodifier; + char16_t c; + int sign, width, precision, lmodifier; unsigned char ljust, alt, lzeroes; - while (c = *fmt++) { - if (c != '%' || *fmt == '%') { + while ((c = *fmtPtr++)) { + if (c != u'%' || *fmtPtr == u'%') { out[o++] = c; - fmt += (c == '%'); + fmtPtr += (c == '%'); continue; } - if ((c = (uint8_t)*fmt++) == '\0') + if ((c = (uint8_t)*fmtPtr++) == '\0') return -1; ljust = alt = lzeroes = FALSE; sign = 0; for (;;) { - if (c == '-') { + if (c == u'-') { ljust = TRUE; lzeroes = FALSE; } - else if (c == '+') - sign = '+'; - else if (c == ' ') { + else if (c == u'+') + sign = u'+'; + else if (c == u' ') { if (!sign) - sign = ' '; + sign = u' '; } - else if (c == '#') + else if (c == u'#') alt = TRUE; - else if (c == '0') { + else if (c == u'0') { if (!ljust) lzeroes = TRUE; } else break; - if ((c = (uint8_t)*fmt++) == '\0') + if ((c = (uint8_t)*fmtPtr++) == u'\0') return -1; } @@ -137,26 +140,26 @@ int XEPrintf(wchar_t* fmt, ...) { if (is_digit(c)) { width = 0; while (is_digit(c)) { - width = width * 10 + (c - '0'); - if ((c = (uint8_t)*fmt++) == '\0') + width = width * 10 + (c - u'0'); + if ((c = (uint8_t)*fmtPtr++) == u'\0') return -1; } } - else if (c == '*') { - width = *(int*)vl; vl += sizeof(int); + else if (c == u'*') { + width = va_arg(vl, int); if (width < 0) { ljust = TRUE; lzeroes = FALSE; width = -width; } - if ((c = *fmt++) == '\0') + if ((c = *fmtPtr++) == u'\0') return -1; } - if (c == '[') { + if (c == u'[') { if (o > 0) { out[o] = 0; - gSystemTable->ConOut->OutputString(gSystemTable->ConOut, out); + gSystemTable->ConOut->OutputString(gSystemTable->ConOut, reinterpret_cast(out)); o = 0; } /**uint32 attr = *(unsigned long*)vl; @@ -165,10 +168,10 @@ int XEPrintf(wchar_t* fmt, ...) { continue; } //restore the default text attribute - if (c == ']') { + if (c == u']') { if (o > 0) { out[o] = 0; - gSystemTable->ConOut->OutputString(gSystemTable->ConOut, out); + gSystemTable->ConOut->OutputString(gSystemTable->ConOut, reinterpret_cast(out)); o = 0; } XESetTextAttribute(EFI_BACKGROUND_BLACK, EFI_LIGHTGRAY); @@ -176,64 +179,64 @@ int XEPrintf(wchar_t* fmt, ...) { } precision = -1; - if (c == '.') { - if ((c = (uint8_t)*fmt++) == '\0') + if (c == u'.') { + if ((c = (uint8_t)*fmtPtr++) == u'\0') return -1; precision = 0; lzeroes = FALSE; if (is_digit(c)) { while (is_digit(c)) { - precision = precision * 10 + (c - '0'); - if ((c = (uint8_t)*fmt++) == '\0') + precision = precision * 10 + (c - u'0'); + if ((c = (uint8_t)*fmtPtr++) == u'\0') return -1; } } - else if (c == '*') { - precision = *(int*)vl; vl += sizeof(int); - if ((c = *fmt++) == '\0') + else if (c == u'*') { + precision = va_arg(vl, int); + if ((c = *fmtPtr++) == u'\0') return -1; } } lmodifier = 0; - if (c == 'h') { - if (*fmt == 'h') { - fmt++; - lmodifier = 'H'; + if (c == u'h') { + if (*fmtPtr == u'h') { + fmtPtr++; + lmodifier = u'H'; } else lmodifier = c; } - else if (wstrchr((wchar_t*)L"jzt", c)) + else if (wstrchr((wchar_t*)u"jzt", c)) lmodifier = c; if (lmodifier) - if ((c = (uint8_t)*fmt++) == '\0') + if ((c = (uint8_t)*fmtPtr++) == u'\0') return -1; - if (c == 'i') - c = 'd'; - if (!wstrchr((wchar_t*)L"douxXcsp", c)) + if (c == u'i') + c = u'd'; + if (!wstrchr((wchar_t*)u"douxXcsp", c)) return -1; - if (c == 'c') { - int ch = (uint8_t) * (int*)vl; vl += sizeof(int); + if (c == u'c') { + char16_t ch = va_arg(vl, int); if (!ljust) while (width > 1) { - out[o++] = ' '; + out[o++] = u' '; width--; } out[o++] = ch; if (ljust) while (width > 1) { - out[o++] = ' '; + out[o++] = u' '; width--; } continue; } - else if (c == 's') { + else if (c == u's') { int len, i; - wchar_t* s = *(wchar_t**)vl; vl += sizeof(wchar_t*); + wchar_t* s = va_arg(vl, wchar_t*); if (precision < 0) len = wstrlen(s); @@ -246,7 +249,7 @@ int XEPrintf(wchar_t* fmt, ...) { if (!ljust) { while (width > len) { - out[o++] = ' '; + out[o++] = u' '; width--; } } @@ -257,53 +260,51 @@ int XEPrintf(wchar_t* fmt, ...) { if (ljust) { while (width > len) { - out[o++] = ' '; + out[o++] = u' '; width--; } } continue; } else { - unsigned v = *(unsigned*)vl, tmp; + unsigned v = va_arg(vl, unsigned); char s[11]; char* p = s + sizeof(s); - unsigned base = (c == 'p') ? 16 : 10; + unsigned base = (c == u'p') ? 16 : 10; char* digits = (char*)"0123456789abcdef"; char* hexpfx = (char*)NULL; int dcnt; int len; - vl += sizeof(unsigned); - if (c == 'o') + if (c == u'o') base = 8; - else if (to_upper(c) == 'X') { + else if (to_upper(c) == u'X') { base = 16; if (c == 'X') digits = (char*)"0123456789ABCDEF"; if (alt && v) - hexpfx = (char*)((c == 'X') ? "0X" : "0x"); + hexpfx = (char*)((c == u'X') ? "0X" : "0x"); } - if (c != 'd') { - if (lmodifier == 'H') + if (c != u'd') { + if (lmodifier == u'H') v = (uint8_t)v; - else if (lmodifier = 'h') + else if ((lmodifier = u'h')) v = (unsigned short)v; sign = 0; } else { - if (lmodifier = 'H') + if ((lmodifier = u'H')) v = (signed char)v; - else if (lmodifier == 'h') + else if ((lmodifier == u'h')) v = (short)v; if ((int)v < 0) { v = -v; - sign = '-'; + sign = u'-'; } } - - tmp = v; + auto tmp = v; do { *--p = digits[tmp % base]; tmp /= base; @@ -315,7 +316,7 @@ int XEPrintf(wchar_t* fmt, ...) { else if ((v == 0) && (precision == 0)) dcnt = 0; - if (alt && (c == 'o')) + if (alt && (c == u'o')) if (((v == 0) && (precision == 0)) || (v && (precision <= dcnt))) precision = dcnt + 1; @@ -327,7 +328,7 @@ int XEPrintf(wchar_t* fmt, ...) { if (!ljust && !lzeroes) while (width > len) { - out[o++] = ' '; + out[o++] = u' '; width--; } @@ -340,19 +341,19 @@ int XEPrintf(wchar_t* fmt, ...) { if (!ljust && lzeroes) while (width > len) { - out[o++] = '0'; + out[o++] = u'0'; width--; } while (precision-- > dcnt) - out[o++] = '0'; + out[o++] = u'0'; while (dcnt--) out[o++] = *p++; if (ljust) while (width > len) { - out[o++] = ' '; + out[o++] = u' '; width--; } @@ -360,14 +361,13 @@ int XEPrintf(wchar_t* fmt, ...) { } } + va_end(vl); + out[o++] = 0; - gSystemTable->ConOut->OutputString(gSystemTable->ConOut, out); + gSystemTable->ConOut->OutputString(gSystemTable->ConOut, reinterpret_cast(out)); return 0; } - - - /* * XEGuiPrint -- print formated text using graphics * @param format -- formated string @@ -397,7 +397,7 @@ void XEGuiPrint(const char* format, ...) { } else if (*format == 'c') { - char c = va_arg(args, char); + char16_t c = va_arg(args, int); XEGraphicsPutC(c); } else if (*format == 'x') { diff --git a/Boot/xnout.h b/Boot/xnout.h index 2edb493b..3052ce4d 100644 --- a/Boot/xnout.h +++ b/Boot/xnout.h @@ -30,7 +30,11 @@ #ifndef __XNOUT_H__ #define __XNOUT_H__ -#include +#ifdef _MSC_VER + #include +#elif __GNUC__ + #include +#endif /* * XEClearScreen -- clears the screen @@ -71,7 +75,7 @@ extern EFI_STATUS XESetTextAttribute(const int Back, const int Fore); * show output to the screen * @param fmt -- format to print */ -extern int XEPrintf(wchar_t* fmt, ...); +extern int XEPrintf(const char16_t* fmt, ...); /* * XEGuiPrint -- print formated text using graphics