prepare to leave boot services, a real printf library, soon™️ threads
This commit is contained in:
1 parent
e65b6be64b
commit
1523b581bb
13 files changed
+1323
-171
No files matched your search
Binary file not shown.
@@ -1,10 +1,11 @@
|
||||
@echo off
|
||||
ml64 /c /nologo /Brepro helpers.asm
|
||||
cl printf.c /c /Brepro /GS- /GR- /Oi /EHs-c- /nologo /Zc:threadSafeInit- /I include || pause && exit /b
|
||||
cl main.cpp /c /Brepro /GS- /GR- /Oi /EHs-c- /nologo /Zc:threadSafeInit- /I include || pause && exit /b
|
||||
cl take_care_of_bink.cpp /c /Brepro /GS- /GR- /Oi /EHs-c- /nologo /Zc:threadSafeInit- /I include || pause && exit /b
|
||||
cl user32.cpp /c /Brepro /GS- /GR- /Oi /EHs-c- /nologo /Zc:threadSafeInit- /I include || pause && exit /b
|
||||
cl ole32.cpp /c /Brepro /GS- /GR- /Oi /EHs-c- /nologo /Zc:threadSafeInit- /I include || pause && exit /b
|
||||
cl kernel32.cpp /Brepro /c /GS- /GR- /Oi /EHs-c- /nologo /Zc:threadSafeInit- /I include || pause && exit /b
|
||||
cl winmm.cpp /c /Brepro /GS- /GR- /Oi /EHs-c- /nologo /Zc:threadSafeInit- /I include || pause && exit /b
|
||||
link helpers.obj main.obj take_care_of_bink.obj user32.obj ole32.obj kernel32.obj winmm.obj /Brepro /STACK:0x400000,0x100000 /NODEFAULTLIB /ENTRY:efi_main /NOLOGO /SUBSYSTEM:EFI_APPLICATION /OUT:BootX64.efi efilib.lib /MACHINE:X64 || pause && exit /b
|
||||
link helpers.obj printf.obj main.obj take_care_of_bink.obj user32.obj ole32.obj kernel32.obj winmm.obj /Brepro /STACK:0x400000,0x100000 /NODEFAULTLIB /ENTRY:efi_main /NOLOGO /SUBSYSTEM:EFI_APPLICATION /OUT:BootX64.efi efilib.lib /MACHINE:X64 || pause && exit /b
|
||||
copy /B /Y BootX64.efi fatroot\EFI\Boot\
|
||||
Binary file not shown.
Binary file not shown.
|
After Width: | Height: | Size: 16 MiB |
+104
-104
@@ -170,29 +170,29 @@ extern void Free(void* ptr);
|
||||
|
||||
// --- Memory ---
|
||||
HANDLE HeapCreate(DWORD, SIZE_T, SIZE_T) {
|
||||
PRINT(L"Called: HeapCreate\n");
|
||||
PRINT("Called: HeapCreate\n");
|
||||
return reinterpret_cast<HANDLE>(1);
|
||||
}
|
||||
|
||||
BOOL HeapDestroy(HANDLE) {
|
||||
PRINT(L"Called: HeapDestroy\n");
|
||||
PRINT("Called: HeapDestroy\n");
|
||||
return TRUE; // nothing to do
|
||||
}
|
||||
|
||||
LPVOID HeapAlloc(HANDLE, DWORD, SIZE_T size) {
|
||||
PRINT(L"Called: HeapAlloc with size: %ull\n", size);
|
||||
PRINT("Called: HeapAlloc with size: %ull\n", size);
|
||||
return Malloc(size);
|
||||
}
|
||||
|
||||
BOOL HeapFree(HANDLE, DWORD, LPVOID ptr) {
|
||||
PRINT(L"Called: HeapFree with ptr: %p\n", ptr);
|
||||
PRINT("Called: HeapFree with ptr: %p\n", ptr);
|
||||
if (ptr)
|
||||
Free(ptr);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
LPVOID HeapReAlloc(HANDLE, DWORD, LPVOID ptr, SIZE_T size) {
|
||||
PRINT(L"Called: HeapReAlloc with size: %ull and ptr: %p\n", size, ptr);
|
||||
PRINT("Called: HeapReAlloc with size: %ull and ptr: %p\n", size, ptr);
|
||||
if (!ptr) return Malloc(size);
|
||||
LPVOID newPtr = Malloc(size);
|
||||
if (!newPtr) return nullptr;
|
||||
@@ -203,23 +203,23 @@ LPVOID HeapReAlloc(HANDLE, DWORD, LPVOID ptr, SIZE_T size) {
|
||||
}
|
||||
|
||||
SIZE_T HeapSize(HANDLE, DWORD, LPCVOID) {
|
||||
PRINT(L"Called: HeapSize\n");
|
||||
PRINT("Called: HeapSize\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// --- Threads (fake, UEFI is single-threaded at boot) ---
|
||||
HANDLE GetCurrentThread() {
|
||||
PRINT(L"Called: GetCurrentThread\n");
|
||||
PRINT("Called: GetCurrentThread\n");
|
||||
return reinterpret_cast<HANDLE>(1);
|
||||
}
|
||||
|
||||
HANDLE CreateThread(void*, SIZE_T, LPTHREAD_START_ROUTINE, void*, DWORD, DWORD*) {
|
||||
PRINT(L"Called: CreateThread\n");
|
||||
PRINT("Called: CreateThread\n");
|
||||
return reinterpret_cast<HANDLE>(1);
|
||||
}
|
||||
|
||||
DWORD WaitForSingleObject(HANDLE, DWORD) {
|
||||
PRINT(L"Called: WaitForSingleObject\n");
|
||||
PRINT("Called: WaitForSingleObject\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -287,66 +287,66 @@ DWORD GetTickCount() {
|
||||
|
||||
// --- Module / File ---
|
||||
HMODULE GetModuleHandleA(LPCSTR name) {
|
||||
PRINT(L"Called: GetModuleHandleA with name: %a\n", name);
|
||||
PRINT("Called: GetModuleHandleA with name: %s\n", name);
|
||||
return reinterpret_cast<HMODULE>(1);
|
||||
}
|
||||
|
||||
void SleepConditionVariableCS(void*, void*, unsigned) {
|
||||
PRINT(L"Called: SleepConditionVariableCS\n");
|
||||
PRINT("Called: SleepConditionVariableCS\n");
|
||||
}
|
||||
|
||||
BOOL TryEnterCriticalSection(LPCRITICAL_SECTION lpCriticalSection) {
|
||||
PRINT(L"Called: TryEnterCriticalSection\n");
|
||||
PRINT("Called: TryEnterCriticalSection\n");
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
BOOL GetLogicalProcessorInformation(void*, void*) {
|
||||
PRINT(L"Called: GetLogicalProcessorInformation\n");
|
||||
PRINT("Called: GetLogicalProcessorInformation\n");
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
void InitializeConditionVariable(void* cond) {
|
||||
PRINT(L"Called: InitializeConditionVariable\n");
|
||||
PRINT("Called: InitializeConditionVariable\n");
|
||||
// No-op stub
|
||||
}
|
||||
|
||||
int timeBeginPeriod(UINT period) {
|
||||
PRINT(L"Called: timeBeginPeriod\n");
|
||||
PRINT("Called: timeBeginPeriod\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int timeEndPeriod(UINT period) {
|
||||
PRINT(L"Called: timeEndPeriod\n");
|
||||
PRINT("Called: timeEndPeriod\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
void WakeConditionVariable(void* cond) {
|
||||
PRINT(L"Called: WakeConditionVariable\n");
|
||||
PRINT("Called: WakeConditionVariable\n");
|
||||
// No-op stub
|
||||
}
|
||||
|
||||
void WakeAllConditionVariable(void* cond) {
|
||||
PRINT(L"Called: WakeAllConditionVariable\n");
|
||||
PRINT("Called: WakeAllConditionVariable\n");
|
||||
// No-op stub
|
||||
}
|
||||
|
||||
BOOL InitializeCriticalSectionAndSpinCount(LPCRITICAL_SECTION lpCriticalSection, DWORD dwSpinCount) {
|
||||
PRINT(L"Called: InitializeCriticalSectionAndSpinCount\n");
|
||||
PRINT("Called: InitializeCriticalSectionAndSpinCount\n");
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
int waveOutOpen() { PRINT(L"Called: waveOutOpen\n"); return 0; }
|
||||
int waveOutPause() { PRINT(L"Called: waveOutPause\n"); return 0; }
|
||||
int waveOutPrepareHeader() { PRINT(L"Called: waveOutPrepareHeader\n"); return 0; }
|
||||
int waveOutUnprepareHeader() { PRINT(L"Called: waveOutUnprepareHeader\n"); return 0; }
|
||||
int waveOutWrite() { PRINT(L"Called: waveOutWrite\n"); return 0; }
|
||||
int waveOutReset() { PRINT(L"Called: waveOutReset\n"); return 0; }
|
||||
int waveOutClose() { PRINT(L"Called: waveOutClose\n"); return 0; }
|
||||
int waveOutRestart() { PRINT(L"Called: waveOutRestart\n"); return 0; }
|
||||
int waveOutSetVolume() { PRINT(L"Called: waveOutSetVolume\n"); return 0; }
|
||||
int waveOutOpen() { PRINT("Called: waveOutOpen\n"); return 0; }
|
||||
int waveOutPause() { PRINT("Called: waveOutPause\n"); return 0; }
|
||||
int waveOutPrepareHeader() { PRINT("Called: waveOutPrepareHeader\n"); return 0; }
|
||||
int waveOutUnprepareHeader() { PRINT("Called: waveOutUnprepareHeader\n"); return 0; }
|
||||
int waveOutWrite() { PRINT("Called: waveOutWrite\n"); return 0; }
|
||||
int waveOutReset() { PRINT("Called: waveOutReset\n"); return 0; }
|
||||
int waveOutClose() { PRINT("Called: waveOutClose\n"); return 0; }
|
||||
int waveOutRestart() { PRINT("Called: waveOutRestart\n"); return 0; }
|
||||
int waveOutSetVolume() { PRINT("Called: waveOutSetVolume\n"); return 0; }
|
||||
|
||||
FARPROC GetProcAddress(HMODULE, LPCSTR func) {
|
||||
PRINT(L"Called: GetProcAddress with func: %a\n", func);
|
||||
PRINT("Called: GetProcAddress with func: %s\n", func);
|
||||
if (stricmp(func, "waveOutOpen") == 0) {
|
||||
return reinterpret_cast<FARPROC>(waveOutOpen);
|
||||
}
|
||||
@@ -401,28 +401,28 @@ FARPROC GetProcAddress(HMODULE, LPCSTR func) {
|
||||
else if (stricmp(func, "WakeAllConditionVariable") == 0) {
|
||||
return reinterpret_cast<FARPROC>(WakeAllConditionVariable);
|
||||
}
|
||||
PRINT(L"Unknown function requested!\n");
|
||||
PRINT("Unknown function requested!\n");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
HMODULE LoadLibraryA(LPCSTR name) {
|
||||
PRINT(L"Called: LoadLibraryA with name: %a\n", name);
|
||||
PRINT("Called: LoadLibraryA with name: %s\n", name);
|
||||
return reinterpret_cast<HMODULE>(1);
|
||||
}
|
||||
|
||||
// --- Process ---
|
||||
DWORD GetCurrentProcessId() {
|
||||
PRINT(L"Called: GetCurrentProcessId\n");
|
||||
PRINT("Called: GetCurrentProcessId\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
void ExitProcess(UINT uExitCode) {
|
||||
PRINT(L"Called: ExitProcess\n");
|
||||
PRINT("Called: ExitProcess\n");
|
||||
while(1); // halt
|
||||
}
|
||||
|
||||
DWORD GetSystemDirectoryA(LPSTR lpBuffer, DWORD uSize) {
|
||||
PRINT(L"Called: GetSystemDirectoryA\n");
|
||||
PRINT("Called: GetSystemDirectoryA\n");
|
||||
if (lpBuffer && uSize > 0) {
|
||||
*lpBuffer = '\0';
|
||||
return 1;
|
||||
@@ -431,7 +431,7 @@ DWORD GetSystemDirectoryA(LPSTR lpBuffer, DWORD uSize) {
|
||||
}
|
||||
|
||||
DWORD GetWindowsDirectoryA(LPSTR lpBuffer, DWORD uSize) {
|
||||
PRINT(L"Called: GetWindowsDirectoryA\n");
|
||||
PRINT("Called: GetWindowsDirectoryA\n");
|
||||
if (lpBuffer && uSize > 0) {
|
||||
*lpBuffer = '\0';
|
||||
return 1;
|
||||
@@ -440,7 +440,7 @@ DWORD GetWindowsDirectoryA(LPSTR lpBuffer, DWORD uSize) {
|
||||
}
|
||||
|
||||
DWORD GetModuleFileNameA(HMODULE hModule, LPSTR lpFilename, DWORD nSize) {
|
||||
PRINT(L"Called: GetModuleFileNameA\n");
|
||||
PRINT("Called: GetModuleFileNameA\n");
|
||||
if (lpFilename && nSize > 0) {
|
||||
*lpFilename = '\0';
|
||||
return 1;
|
||||
@@ -449,255 +449,255 @@ DWORD GetModuleFileNameA(HMODULE hModule, LPSTR lpFilename, DWORD nSize) {
|
||||
}
|
||||
|
||||
void Sleep(DWORD dwMilliseconds) {
|
||||
PRINT(L"Called: Sleep\n");
|
||||
PRINT("Called: Sleep\n");
|
||||
UINTN microseconds = (UINTN)dwMilliseconds * 1000;
|
||||
BS->Stall(microseconds);
|
||||
}
|
||||
|
||||
DWORD SetErrorMode(DWORD uMode) {
|
||||
PRINT(L"Called: SetErrorMode\n");
|
||||
PRINT("Called: SetErrorMode\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
DWORD GetEnvironmentVariableA(LPCTSTR lpName, LPSTR lpBuffer, DWORD nSize) {
|
||||
PRINT(L"Called: GetEnvironmentVariableA\n");
|
||||
PRINT("Called: GetEnvironmentVariableA\n");
|
||||
if (lpBuffer && nSize > 0) lpBuffer[0] = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
BOOL CloseHandle(HANDLE hObject) {
|
||||
PRINT(L"Called: CloseHandle\n");
|
||||
PRINT("Called: CloseHandle\n");
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
DWORD GetLastError() {
|
||||
PRINT(L"Called: GetLastError\n");
|
||||
PRINT("Called: GetLastError\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
DWORD GetFileSize(HANDLE hFile, LPDWORD lpFileSizeHigh) {
|
||||
PRINT(L"Called: GetFileSize\n");
|
||||
PRINT("Called: GetFileSize\n");
|
||||
if (lpFileSizeHigh) *lpFileSizeHigh = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
HANDLE CreateFileA(LPCTSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile) {
|
||||
PRINT(L"Called: CreateFileA\n");
|
||||
PRINT("Called: CreateFileA\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
BOOL ReadFile(HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead, LPDWORD lpNumberOfBytesRead, LPOVERLAPPED lpOverlapped) {
|
||||
PRINT(L"Called: ReadFile\n");
|
||||
PRINT("Called: ReadFile\n");
|
||||
*lpNumberOfBytesRead = 0;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
DWORD SetFilePointer(HANDLE hFile, LONG lDistanceToMove, PLONG lpDistanceToMoveHigh, DWORD dwMoveMethod) {
|
||||
PRINT(L"Called: SetFilePointer\n");
|
||||
PRINT("Called: SetFilePointer\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
BOOL FreeLibrary(HMODULE hLibModule) {
|
||||
PRINT(L"Called: FreeLibrary\n");
|
||||
PRINT("Called: FreeLibrary\n");
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
int GetThreadPriority(HANDLE hThread) {
|
||||
PRINT(L"Called: GetThreadPriority\n");
|
||||
PRINT("Called: GetThreadPriority\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
PVOID RemoveVectoredExceptionHandler(PVOID Handle) {
|
||||
PRINT(L"Called: RemoveVectoredExceptionHandler\n");
|
||||
PRINT("Called: RemoveVectoredExceptionHandler\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void RaiseException(DWORD dwExceptionCode, DWORD dwExceptionFlags, DWORD nNumberOfArguments, const ULONG_PTR* lpArguments) {
|
||||
PRINT(L"Called: RaiseException\n");
|
||||
PRINT("Called: RaiseException\n");
|
||||
return;
|
||||
}
|
||||
|
||||
PVOID AddVectoredExceptionHandler(ULONG FirstHandler, PVECTORED_EXCEPTION_HANDLER VectoredHandler) {
|
||||
PRINT(L"Called: AddVectoredExceptionHandler\n");
|
||||
PRINT("Called: AddVectoredExceptionHandler\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
DWORD_PTR SetThreadAffinityMask(HANDLE hThread, DWORD_PTR dwThreadAffinityMask) {
|
||||
PRINT(L"Called: SetThreadAffinityMask\n");
|
||||
PRINT("Called: SetThreadAffinityMask\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
DWORD ResumeThread(HANDLE hThread) {
|
||||
PRINT(L"Called: ResumeThread\n");
|
||||
PRINT("Called: ResumeThread\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
BOOL SetThreadPriority(HANDLE hThread, int nPriority) {
|
||||
PRINT(L"Called: SetThreadPriority\n");
|
||||
PRINT("Called: SetThreadPriority\n");
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
void InitializeCriticalSection(LPCRITICAL_SECTION lpCriticalSection) {
|
||||
PRINT(L"Called: InitializeCriticalSection\n");
|
||||
PRINT("Called: InitializeCriticalSection\n");
|
||||
}
|
||||
|
||||
void DeleteCriticalSection(LPCRITICAL_SECTION lpCriticalSection) {
|
||||
PRINT(L"Called: DeleteCriticalSection\n");
|
||||
PRINT("Called: DeleteCriticalSection\n");
|
||||
}
|
||||
|
||||
void EnterCriticalSection(LPCRITICAL_SECTION lpCriticalSection) {
|
||||
PRINT(L"Called: EnterCriticalSection\n");
|
||||
PRINT("Called: EnterCriticalSection\n");
|
||||
}
|
||||
|
||||
void LeaveCriticalSection(LPCRITICAL_SECTION lpCriticalSection) {
|
||||
PRINT(L"Called: LeaveCriticalSection\n");
|
||||
PRINT("Called: LeaveCriticalSection\n");
|
||||
}
|
||||
|
||||
DWORD GetCurrentThreadId() {
|
||||
PRINT(L"Called: GetCurrentThreadId\n");
|
||||
PRINT("Called: GetCurrentThreadId\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
HANDLE CreateMutexA(LPSECURITY_ATTRIBUTES lpMutexAttributes, BOOL bInitialOwner, LPCTSTR lpName) {
|
||||
PRINT(L"Called: CreateMutexA with name: lpName: %a\n", lpName);
|
||||
PRINT("Called: CreateMutexA with name: lpName: %s\n", lpName);
|
||||
return reinterpret_cast<HANDLE>(1);
|
||||
}
|
||||
|
||||
BOOL ReleaseMutex(HANDLE hMutex) {
|
||||
PRINT(L"Called: ReleaseMutex\n");
|
||||
PRINT("Called: ReleaseMutex\n");
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
HANDLE CreateSemaphoreA(LPSECURITY_ATTRIBUTES lpSemaphoreAttributes, LONG lInitialCount, LONG lMaximumCount, LPCTSTR lpName) {
|
||||
PRINT(L"Called: CreateSemaphoreA with name: lpName: %a\n", lpName);
|
||||
PRINT("Called: CreateSemaphoreA with name: lpName: %s\n", lpName);
|
||||
return reinterpret_cast<HANDLE>(1);
|
||||
}
|
||||
|
||||
BOOL ReleaseSemaphore(HANDLE hSemaphore, LONG lReleaseCount, LPLONG lpPreviousCount) {
|
||||
PRINT(L"Called: ReleaseSemaphore\n");
|
||||
PRINT("Called: ReleaseSemaphore\n");
|
||||
if (lpPreviousCount) *lpPreviousCount = 0;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
BOOL TerminateProcess(HANDLE hProcess, UINT uExitCode) {
|
||||
PRINT(L"Called: TerminateProcess\n");
|
||||
PRINT("Called: TerminateProcess\n");
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
HANDLE GetCurrentProcess() {
|
||||
PRINT(L"Called: GetCurrentProcess\n");
|
||||
PRINT("Called: GetCurrentProcess\n");
|
||||
return reinterpret_cast<HANDLE>(1);
|
||||
}
|
||||
|
||||
LPTOP_LEVEL_EXCEPTION_FILTER UnhandledExceptionFilter(LPTOP_LEVEL_EXCEPTION_FILTER lpTopLevelExceptionFilter) {
|
||||
PRINT(L"Called: UnhandledExceptionFilter\n");
|
||||
PRINT("Called: UnhandledExceptionFilter\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
LPTOP_LEVEL_EXCEPTION_FILTER SetUnhandledExceptionFilter(LPTOP_LEVEL_EXCEPTION_FILTER lpTopLevelExceptionFilter) {
|
||||
PRINT(L"Called: SetUnhandledExceptionFilter\n");
|
||||
PRINT("Called: SetUnhandledExceptionFilter\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
BOOL IsDebuggerPresent() {
|
||||
PRINT(L"Called: IsDebuggerPresent\n");
|
||||
PRINT("Called: IsDebuggerPresent\n");
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
PVOID RtlVirtualUnwind(ULONG HandlerType, ULONG64 ImageBase, ULONG64 ControlPc, PRUNTIME_FUNCTION FunctionEntry, PCONTEXT ContextRecord, PVOID* HandlerData, PULONG64 EstablisherFrame, PCONTEXT ContextRecord2) {
|
||||
PRINT(L"Called: RtlVirtualUnwind\n");
|
||||
PRINT("Called: RtlVirtualUnwind\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
PRUNTIME_FUNCTION RtlLookupFunctionEntry(ULONG64 ControlPc, PULONG64 ImageBase, PVOID HistoryTable) {
|
||||
PRINT(L"Called: RtlLookupFunctionEntry\n");
|
||||
PRINT("Called: RtlLookupFunctionEntry\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void RtlCaptureContext(PCONTEXT ContextRecord) {
|
||||
PRINT(L"Called: RtlCaptureContext\n");
|
||||
PRINT("Called: RtlCaptureContext\n");
|
||||
}
|
||||
|
||||
void RtlUnwindEx(PVOID TargetFrame, PVOID TargetIp, PVOID ExceptionRecord, PVOID ReturnValue, PCONTEXT ContextRecord, PVOID HistoryTable) {
|
||||
PRINT(L"Called: RtlUnwindEx\n");
|
||||
PRINT("Called: RtlUnwindEx\n");
|
||||
}
|
||||
|
||||
BOOL WriteFile(HANDLE hFile, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite, LPDWORD lpNumberOfBytesWritten, LPOVERLAPPED lpOverlapped) {
|
||||
PRINT(L"Called: WriteFile\n");
|
||||
PRINT("Called: WriteFile\n");
|
||||
if (lpNumberOfBytesWritten) *lpNumberOfBytesWritten = 0;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
HMODULE GetModuleHandleW(LPCWSTR lpModuleName) {
|
||||
PRINT(L"Called: GetModuleHandleW with name: %s\n", lpModuleName);
|
||||
PRINT("Called: GetModuleHandleW with name: %s\n", lpModuleName);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int SetHandleCount(int n) {
|
||||
PRINT(L"Called: SetHandleCount\n");
|
||||
PRINT("Called: SetHandleCount\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
HANDLE GetStdHandle(DWORD nStdHandle) {
|
||||
PRINT(L"Called: GetStdHandle\n");
|
||||
PRINT("Called: GetStdHandle\n");
|
||||
return reinterpret_cast<HANDLE>(1);
|
||||
}
|
||||
|
||||
DWORD GetFileType(HANDLE hFile) {
|
||||
PRINT(L"Called: GetFileType\n");
|
||||
PRINT("Called: GetFileType\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
void GetStartupInfoA(LPSTARTUPINFOA lpStartupInfo) {
|
||||
PRINT(L"Called: GetStartupInfoA\n");
|
||||
PRINT("Called: GetStartupInfoA\n");
|
||||
}
|
||||
|
||||
LPCH GetEnvironmentStrings() {
|
||||
PRINT(L"Called: GetEnvironmentStrings\n");
|
||||
PRINT("Called: GetEnvironmentStrings\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
BOOL FreeEnvironmentStringsA(LPCH lpszString) {
|
||||
PRINT(L"Called: FreeEnvironmentStringsA\n");
|
||||
PRINT("Called: FreeEnvironmentStringsA\n");
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
LPWCH GetEnvironmentStringsW() {
|
||||
PRINT(L"Called: GetEnvironmentStringsW\n");
|
||||
PRINT("Called: GetEnvironmentStringsW\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
BOOL FreeEnvironmentStringsW(LPWCH lpszString) {
|
||||
PRINT(L"Called: FreeEnvironmentStringsW\n");
|
||||
PRINT("Called: FreeEnvironmentStringsW\n");
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
void GetSystemTimeAsFileTime(LPFILETIME lpSystemTimeAsFileTime) {
|
||||
PRINT(L"Called: GetSystemTimeAsFileTime\n");
|
||||
PRINT("Called: GetSystemTimeAsFileTime\n");
|
||||
}
|
||||
|
||||
int GetACP() {
|
||||
PRINT(L"Called: GetACP\n");
|
||||
PRINT("Called: GetACP\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
BOOL GetCPInfo(UINT CodePage, LPCPINFO lpCPInfo) {
|
||||
PRINT(L"Called: GetCPInfo\n");
|
||||
PRINT("Called: GetCPInfo\n");
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
UINT GetOEMCP() {
|
||||
PRINT(L"Called: GetOEMCP\n");
|
||||
PRINT("Called: GetOEMCP\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
BOOL IsValidCodePage(UINT CodePage) {
|
||||
PRINT(L"Called: IsValidCodePage\n");
|
||||
PRINT("Called: IsValidCodePage\n");
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
int WideCharToMultiByte(UINT CodePage, DWORD dwFlags, LPCWSTR lpWideCharStr, int cchWideChar, LPSTR lpMultiByteStr, int cbMultiByte, LPCSTR lpDefaultChar, LPBOOL lpUsedDefaultChar) {
|
||||
PRINT(L"Called: WideCharToMultiByte\n");
|
||||
PRINT("Called: WideCharToMultiByte\n");
|
||||
if (lpWideCharStr == NULL || cchWideChar <= 0) {
|
||||
return 0; // Return 0 for invalid input
|
||||
}
|
||||
@@ -719,77 +719,77 @@ int WideCharToMultiByte(UINT CodePage, DWORD dwFlags, LPCWSTR lpWideCharStr, int
|
||||
}
|
||||
|
||||
int LCMapStringA(LCID Locale, DWORD dwMapFlags, LPCSTR lpSrcStr, int cchSrc, LPSTR lpDestStr, int cchDest) {
|
||||
PRINT(L"Called: LCMapStringA\n");
|
||||
PRINT("Called: LCMapStringA\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int LCMapStringW(LCID Locale, DWORD dwMapFlags, LPCWSTR lpSrcStr, int cchSrc, LPWSTR lpDestStr, int cchDest) {
|
||||
PRINT(L"Called: LCMapStringW\n");
|
||||
PRINT("Called: LCMapStringW\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
BOOL GetStringTypeA(LCID Locale, DWORD dwInfoType, LPCSTR lpSrcStr, int cchSrc, LPWORD lpCharType) {
|
||||
PRINT(L"Called: GetStringTypeA\n");
|
||||
PRINT("Called: GetStringTypeA\n");
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
BOOL GetStringTypeW(DWORD dwInfoType, LPCWSTR lpSrcStr, int cchSrc, LPWORD lpCharType) {
|
||||
PRINT(L"Called: GetStringTypeW\n");
|
||||
PRINT("Called: GetStringTypeW\n");
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
int GetLocaleInfoA(LCID Locale, LCTYPE LCType, LPSTR lpLCData, int cchData) {
|
||||
PRINT(L"Called: GetLocaleInfoA\n");
|
||||
PRINT("Called: GetLocaleInfoA\n");
|
||||
*lpLCData = '\0';
|
||||
return 1;
|
||||
}
|
||||
|
||||
BOOL FlsSetValue(DWORD dwFlsIndex, PVOID lpFlsData) {
|
||||
PRINT(L"Called: FlsSetValue\n");
|
||||
PRINT("Called: FlsSetValue\n");
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
PSTR GetCommandLineA() {
|
||||
PRINT(L"Called: GetCommandLineA\n");
|
||||
PRINT("Called: GetCommandLineA\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
LPVOID EncodePointer(LPVOID Ptr) {
|
||||
PRINT(L"Called: EncodePointer\n");
|
||||
PRINT("Called: EncodePointer\n");
|
||||
return Ptr;
|
||||
}
|
||||
|
||||
LPVOID DecodePointer(LPVOID Ptr) {
|
||||
PRINT(L"Called: DecodePointer\n");
|
||||
PRINT("Called: DecodePointer\n");
|
||||
return Ptr;
|
||||
}
|
||||
|
||||
BOOL FlsGetValue(DWORD dwFlsIndex) {
|
||||
PRINT(L"Called: FlsGetValue\n");
|
||||
PRINT("Called: FlsGetValue\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
BOOL FlsFree(DWORD dwFlsIndex) {
|
||||
PRINT(L"Called: FlsFree\n");
|
||||
PRINT("Called: FlsFree\n");
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
void SetLastError(DWORD dwErrCode) {
|
||||
PRINT(L"Called: SetLastError\n");
|
||||
PRINT("Called: SetLastError\n");
|
||||
}
|
||||
|
||||
DWORD FlsAlloc(PFLS_CALLBACK_FUNCTION lpCallback) {
|
||||
PRINT(L"Called: FlsAlloc\n");
|
||||
PRINT("Called: FlsAlloc\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
BOOL HeapSetInformation(HANDLE HeapHandle, HEAP_INFORMATION_CLASS HeapInformationClass, PVOID HeapInformation, SIZE_T HeapInformationLength) {
|
||||
PRINT(L"Called: HeapSetInformation\n");
|
||||
PRINT("Called: HeapSetInformation\n");
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
void GetSystemInfo(LPSYSTEM_INFO lpSystemInfo) {
|
||||
PRINT(L"Called: GetSystemInfo\n");
|
||||
PRINT("Called: GetSystemInfo\n");
|
||||
if (lpSystemInfo) {
|
||||
// Fill the struct with dummy values consistent with an x64 environment
|
||||
lpSystemInfo->DUMMYUNIONNAME.DUMMYSTRUCTNAME.wProcessorArchitecture = 9; // PROCESSOR_ARCHITECTURE_AMD64
|
||||
@@ -806,7 +806,7 @@ void GetSystemInfo(LPSYSTEM_INFO lpSystemInfo) {
|
||||
}
|
||||
|
||||
int MultiByteToWideChar(UINT CodePage, DWORD dwFlags, LPCSTR lpMultiByteStr, int cbMultiByte, LPWSTR lpWideCharStr, int cchWideChar) {
|
||||
PRINT(L"Called: MultiByteToWideChar\n");
|
||||
PRINT("Called: MultiByteToWideChar\n");
|
||||
if (lpMultiByteStr == NULL || cchWideChar <= 0) {
|
||||
return 0; // Return 0 for invalid input
|
||||
}
|
||||
@@ -1012,6 +1012,6 @@ void* ResolveKernel32(const char* functionName) {
|
||||
return MultiByteToWideChar;
|
||||
}
|
||||
|
||||
Print(L"Unknown function: %a\n", functionName);
|
||||
printf("Unknown function: %s\n", functionName);
|
||||
return NULL;
|
||||
}
|
||||
@@ -1,10 +1,33 @@
|
||||
#include <efi.h>
|
||||
#include <efilib.h>
|
||||
#include <intrin.h>
|
||||
#include <immintrin.h>
|
||||
#include <compiler_defines.h>
|
||||
#include "print.h"
|
||||
#include "take_care_of_bink.h"
|
||||
|
||||
int SerialPutc(char c) {
|
||||
// Define a timeout limit to prevent infinite hanging
|
||||
uint32_t timeout = 1000000;
|
||||
|
||||
// 0x3FD is the Line Status Register (LSR)
|
||||
// 0x20 is the Transmit Holding Register Empty (THRE) bit
|
||||
while (!(__inbyte(0x3FD) & 0x20)) {
|
||||
if (--timeout == 0) {
|
||||
return 0; // Hardware failure or busy timeout
|
||||
}
|
||||
}
|
||||
|
||||
// 0x3F8 is the Transmit Holding Register (THR)
|
||||
__outbyte(0x3F8, (unsigned char)c);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void _putchar(char c) {
|
||||
SerialPutc(c);
|
||||
}
|
||||
|
||||
EFI_GRAPHICS_OUTPUT_PROTOCOL* gop;
|
||||
EFI_GRAPHICS_OUTPUT_MODE_INFORMATION* mode_info;
|
||||
|
||||
@@ -14,7 +37,7 @@ EFI_STATUS InitializeGop() {
|
||||
// 1. Locate the Graphics Output Protocol
|
||||
Status = LibLocateProtocol(&gEfiGraphicsOutputProtocolGuid, (VOID**)&gop);
|
||||
if (EFI_ERROR(Status)) {
|
||||
Print(L"Failed to locate Graphics Output Protocol: %r\n", Status);
|
||||
printf("Failed to locate Graphics Output Protocol: %r\n", Status);
|
||||
return Status;
|
||||
}
|
||||
|
||||
@@ -53,7 +76,7 @@ void* LoadFile(EFI_FILE_HANDLE volume, const wchar_t* filename) {
|
||||
EFI_STATUS file_loading_status = volume->Open(volume, &file, (wchar_t*)filename,
|
||||
EFI_FILE_MODE_READ, 0);
|
||||
if (EFI_ERROR(file_loading_status)) {
|
||||
Print(L"Failed to open file: %r\n", file_loading_status);
|
||||
printf("Failed to open file: %r\n", file_loading_status);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -75,14 +98,14 @@ void* LoadFile(EFI_FILE_HANDLE volume, const wchar_t* filename) {
|
||||
file->Close(file);
|
||||
|
||||
if (EFI_ERROR(file_loading_status)) {
|
||||
Print(L"Failed to read file: %r\n", file_loading_status);
|
||||
printf("Failed to read file: %r\n", file_loading_status);
|
||||
// Free the allocated memory if the read fails
|
||||
Free(file_buffer);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (read_size != file_size) {
|
||||
Print(L"Read size mismatch: expected %llu, got %llu\n", file_size, read_size);
|
||||
printf("Read size mismatch: expected %llu, got %llu\n", file_size, read_size);
|
||||
Free(file_buffer);
|
||||
return NULL;
|
||||
}
|
||||
@@ -104,7 +127,7 @@ bool LoadBinkDLL(EFI_FILE_HANDLE volume) {
|
||||
|
||||
// Grab exports
|
||||
#define GRAB(name) p##name = (name##_t)GetExportByName(binkBase, #name); \
|
||||
if (!p##name) { Print(L"Missing export: %a\n", #name); return false; }
|
||||
if (!p##name) { printf("Missing export: %s\n", #name); return false; }
|
||||
|
||||
GRAB(BinkLogoAddress)
|
||||
GRAB(BinkSetError)
|
||||
@@ -235,19 +258,19 @@ EFI_STATUS SetMaxResolution(EFI_GRAPHICS_OUTPUT_PROTOCOL *Gop) {
|
||||
}
|
||||
|
||||
if (MaxModeInfo) {
|
||||
Print(L"Found max resolution mode: %u x %u at mode %u\n",
|
||||
printf("Found max resolution mode: %u x %u at mode %u\n",
|
||||
MaxModeInfo->HorizontalResolution, MaxModeInfo->VerticalResolution, MaxModeNumber);
|
||||
|
||||
// Set the mode to the maximum resolution
|
||||
Status = Gop->SetMode(Gop, MaxModeNumber);
|
||||
if (EFI_ERROR(Status)) {
|
||||
Print(L"Failed to set mode: %r\n", Status);
|
||||
printf("Failed to set mode: %r\n", Status);
|
||||
} else {
|
||||
Print(L"Successfully set max resolution mode.\n");
|
||||
printf("Successfully set max resolution mode.\n");
|
||||
}
|
||||
FreePool(MaxModeInfo); // Free the memory for MaxModeInfo
|
||||
} else {
|
||||
Print(L"Could not find a valid mode.\n");
|
||||
printf("Could not find a valid mode.\n");
|
||||
Status = EFI_NOT_FOUND;
|
||||
}
|
||||
|
||||
@@ -256,10 +279,93 @@ EFI_STATUS SetMaxResolution(EFI_GRAPHICS_OUTPUT_PROTOCOL *Gop) {
|
||||
|
||||
extern "C" void enable_avx2();
|
||||
|
||||
#pragma pack(push, 1)
|
||||
typedef struct {
|
||||
uint16_t type;
|
||||
uint32_t size;
|
||||
uint16_t reserved1;
|
||||
uint16_t reserved2;
|
||||
uint32_t offset;
|
||||
uint32_t dib_size;
|
||||
int32_t width;
|
||||
int32_t height;
|
||||
uint16_t planes;
|
||||
uint16_t bpp;
|
||||
uint32_t compression;
|
||||
uint32_t image_size;
|
||||
int32_t x_ppm;
|
||||
int32_t y_ppm;
|
||||
uint32_t colors;
|
||||
uint32_t important_colors;
|
||||
} BMP_HEADER;
|
||||
#pragma pack(pop)
|
||||
|
||||
void SavePhysicalScreen(EFI_FILE_HANDLE volume, EFI_GRAPHICS_OUTPUT_PROTOCOL* Gop) {
|
||||
UINT32 width = Gop->Mode->Info->HorizontalResolution;
|
||||
UINT32 height = Gop->Mode->Info->VerticalResolution;
|
||||
UINT32 stride = Gop->Mode->Info->PixelsPerScanLine;
|
||||
uint32_t* fb_base = (uint32_t*)Gop->Mode->FrameBufferBase;
|
||||
|
||||
// 1. Allocate a single contiguous buffer for the BMP (Header + Pixels)
|
||||
UINTN total_pixel_bytes = width * height * 4;
|
||||
UINTN total_file_size = sizeof(BMP_HEADER) + total_pixel_bytes;
|
||||
uint8_t* file_buffer = (uint8_t*)AllocatePool(total_file_size);
|
||||
|
||||
if (!file_buffer) {
|
||||
printf("Failed to allocate memory for screenshot!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. Setup Header
|
||||
BMP_HEADER* header = (BMP_HEADER*)file_buffer;
|
||||
SetMem(header, sizeof(BMP_HEADER), 0);
|
||||
header->type = 0x4D42;
|
||||
header->offset = sizeof(BMP_HEADER);
|
||||
header->size = (uint32_t)total_file_size;
|
||||
header->dib_size = 40;
|
||||
header->width = (int32_t)width;
|
||||
header->height = -(int32_t)height; // Top-down
|
||||
header->planes = 1;
|
||||
header->bpp = 32;
|
||||
|
||||
// 3. Copy pixels to buffer while stripping Stride and fixing Alpha
|
||||
uint32_t* dst_pixels = (uint32_t*)(file_buffer + sizeof(BMP_HEADER));
|
||||
__m256i alpha_mask = _mm256_set1_epi32(0xFF000000); // Mask to force Alpha to 255
|
||||
|
||||
for (UINT32 y = 0; y < height; y++) {
|
||||
uint32_t* src_row = fb_base + (y * stride);
|
||||
uint32_t* dst_row = dst_pixels + (y * width);
|
||||
|
||||
UINT32 x = 0;
|
||||
// AVX2 Loop: Fix 8 pixels at a time
|
||||
for (; x + 7 < width; x += 8) {
|
||||
__m256i pixels = _mm256_loadu_si256((__m256i*)(src_row + x));
|
||||
pixels = _mm256_or_si256(pixels, alpha_mask); // Force Alpha channel to Opaque
|
||||
_mm256_storeu_si256((__m256i*)(dst_row + x), pixels);
|
||||
}
|
||||
// Cleanup loop for remaining pixels
|
||||
for (; x < width; x++) {
|
||||
dst_row[x] = src_row[x] | 0xFF000000;
|
||||
}
|
||||
}
|
||||
|
||||
// 4. One single disk write (Massively faster)
|
||||
EFI_FILE_HANDLE file;
|
||||
EFI_STATUS status = volume->Open(volume, &file, (CHAR16*)L"screen.bmp",
|
||||
EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE, 0);
|
||||
if (!EFI_ERROR(status)) {
|
||||
UINTN write_size = total_file_size;
|
||||
file->Write(file, &write_size, file_buffer);
|
||||
file->Close(file);
|
||||
}
|
||||
|
||||
FreePool(file_buffer);
|
||||
}
|
||||
|
||||
EFI_STATUS efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE* SystemTable) {
|
||||
InitializeLib(ImageHandle, SystemTable);
|
||||
SystemTable->BootServices->SetWatchdogTimer(0, 0, 0, NULL);
|
||||
Print(L"Hello, World!\n");
|
||||
printf("Hello, World!\n");
|
||||
|
||||
VOID* HeapMemory = NULL;
|
||||
UINTN AllocationSize = 512 * 1024 * 1024; // 512 MB
|
||||
@@ -273,72 +379,73 @@ EFI_STATUS efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE* SystemTable) {
|
||||
);
|
||||
|
||||
if (EFI_ERROR(Status)) {
|
||||
Print(L"Failed to allocate memory pool pages: %r\n", Status);
|
||||
printf("Failed to allocate memory pool pages: %r\n", Status);
|
||||
gBS->Stall(5 * 1000 * 1000);
|
||||
gST->RuntimeServices->ResetSystem(EfiResetShutdown, EFI_SUCCESS, 0, NULL);
|
||||
return Status;
|
||||
}
|
||||
|
||||
InitAllocatorFromBuffer(HeapMemory, AllocationSize);
|
||||
Print(L"Custom allocator initialized\n");
|
||||
printf("Custom allocator initialized\n");
|
||||
|
||||
InitializeGop();
|
||||
Print(L"GOP initialized\n");
|
||||
printf("GOP initialized\n");
|
||||
|
||||
SetMaxResolution(gop);
|
||||
|
||||
enable_avx2();
|
||||
|
||||
EFI_FILE_HANDLE volume = GetVolume(ImageHandle);
|
||||
Print(L"Got volume\n");
|
||||
printf("Got volume\n");
|
||||
|
||||
if (!LoadBinkDLL(volume)) {
|
||||
Print(L"Failed to load Bink Video DLL\n");
|
||||
printf("Failed to load Bink Video DLL\n");
|
||||
gBS->Stall(5 * 1000 * 1000);
|
||||
gST->RuntimeServices->ResetSystem(EfiResetShutdown, EFI_SUCCESS, 0, NULL);
|
||||
return 1;
|
||||
}
|
||||
|
||||
Print(L"Bink function pointers initialized!\n");
|
||||
printf("Bink function pointers initialized!\n");
|
||||
|
||||
//pBinkSetSoundSystem(pBinkOpenWaveOut, 0);
|
||||
pBinkSetMemory((BINKMEMALLOC)Malloc, (BINKMEMFREE)Free);
|
||||
pBinkSetIO(0); // no io
|
||||
|
||||
Print(L"BinkSet* called\n");
|
||||
printf("BinkSet* called\n");
|
||||
|
||||
void* video_file = LoadFile(volume, L"idk.bk2");
|
||||
|
||||
Print(L"Video file loaded into memory\n");
|
||||
printf("Video file loaded into memory\n");
|
||||
|
||||
HBINK bink = NULL;
|
||||
bink = pBinkOpen((const char*)video_file, BINKFROMMEMORY | BINKNOSKIP | BINKNOTHREADEDIO);
|
||||
Print(L"Opened Bink!\n");
|
||||
printf("Opened Bink!\n");
|
||||
|
||||
int width = bink->Width, height = bink->Height;
|
||||
Print(L"Width: %d, Height: %d\n", width, height);
|
||||
printf("Width: %d, Height: %d\n", width, height);
|
||||
|
||||
// Allocate framebuffer
|
||||
uint32_t* framebuffer = (uint32_t*)Malloc(width * height * sizeof(uint32_t));
|
||||
if (!framebuffer) {
|
||||
Print(L"Failed to allocate framebuffer!\n");
|
||||
printf("Failed to allocate framebuffer!\n");
|
||||
gBS->Stall(5 * 1000 * 1000);
|
||||
pBinkClose(bink);
|
||||
Free(video_file);
|
||||
return 1;
|
||||
}
|
||||
|
||||
Print(L"Framebuffer allocated!\n");
|
||||
printf("Framebuffer allocated!\n");
|
||||
|
||||
EFI_INPUT_KEY key;
|
||||
bool paused = false;
|
||||
uint64_t skipped = 0;
|
||||
|
||||
// start the decode loop
|
||||
while (true) {
|
||||
EFI_STATUS key_status = gST->ConIn->ReadKeyStroke(gST->ConIn, &key);
|
||||
if (key_status == EFI_SUCCESS) {
|
||||
if (key.UnicodeChar == 'q') {
|
||||
Print(L"\nExiting");
|
||||
printf("\nExiting");
|
||||
break;
|
||||
}
|
||||
else if (key.UnicodeChar == 'r') {
|
||||
@@ -348,11 +455,14 @@ EFI_STATUS efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE* SystemTable) {
|
||||
pBinkPause(bink, paused);
|
||||
paused = !paused;
|
||||
}
|
||||
else if (key.UnicodeChar == 's' ) {
|
||||
SavePhysicalScreen(volume, gop);
|
||||
}
|
||||
}
|
||||
|
||||
// Check if the video is done
|
||||
if (bink->FrameNum >= bink->Frames) {
|
||||
Print(L"\nVideo playback finished!\n");
|
||||
printf("\nVideo playback finished!");
|
||||
break; // exit the loop if the video is done
|
||||
}
|
||||
|
||||
@@ -365,12 +475,13 @@ EFI_STATUS efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE* SystemTable) {
|
||||
// Check if we should skip the frame
|
||||
if (pBinkShouldSkip(bink)) {
|
||||
pBinkNextFrame(bink);
|
||||
skipped++;
|
||||
continue; // skip this frame
|
||||
}
|
||||
|
||||
int code = pBinkDoFrame(bink);
|
||||
if (code != 0) {
|
||||
Print(L"BinkDoFrame failed!\n");
|
||||
printf("BinkDoFrame failed!\n");
|
||||
pBinkClose(bink);
|
||||
Free(video_file);
|
||||
gBS->Stall(5 * 1000 * 1000);
|
||||
@@ -389,7 +500,7 @@ EFI_STATUS efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE* SystemTable) {
|
||||
BINKSURFACE32 | BINKCOPYALL
|
||||
);
|
||||
if (code != 0 && code != 1) { // who knows what they mean
|
||||
Print(L"BinkCopyToBuffer failed!\n");
|
||||
printf("BinkCopyToBuffer failed!\n");
|
||||
pBinkClose(bink);
|
||||
Free(video_file);
|
||||
gBS->Stall(5 * 1000 * 1000);
|
||||
@@ -402,16 +513,20 @@ EFI_STATUS efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE* SystemTable) {
|
||||
Blit_AVX2_NT(gop, framebuffer, width, height);
|
||||
|
||||
if (bink->FrameNum % 10 == 0)
|
||||
Print(L"\rframe %d done ", bink->FrameNum);
|
||||
printf("\rframe %d done ", bink->FrameNum);
|
||||
}
|
||||
|
||||
printf("\nTotal skipped frames: %llu\n", skipped);
|
||||
if (skipped > 100)
|
||||
printf("oof\n");
|
||||
|
||||
pBinkClose(bink);
|
||||
|
||||
Free(video_file);
|
||||
Free(framebuffer);
|
||||
|
||||
Print(L"Exiting...\n");
|
||||
gBS->Stall(4 * 1000 * 1000);
|
||||
printf("Exiting...\n");
|
||||
gBS->Stall(2 * 1000 * 1000);
|
||||
gST->RuntimeServices->ResetSystem(EfiResetShutdown, EFI_SUCCESS, 0, NULL);
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -14,14 +14,14 @@ typedef void* HMODULE;
|
||||
|
||||
// COM stubs
|
||||
HRESULT CoCreateInstance(REFCLSID, LPUNKNOWN, DWORD, REFIID, LPVOID* ppv) {
|
||||
PRINT(L"CoCreateInstance called\n");
|
||||
printf("CoCreateInstance called\n");
|
||||
if (ppv) *ppv = nullptr;
|
||||
return S_OK;
|
||||
}
|
||||
HRESULT CoInitializeEx(LPVOID, DWORD) { PRINT(L"CoInitializeEx called\n"); return S_OK; }
|
||||
void CoUninitialize() { PRINT(L"CoUninitialize called\n"); }
|
||||
void CoTaskMemFree(LPVOID) { PRINT(L"CoTaskMemFree called\n"); }
|
||||
HRESULT PropVariantClear(LPVOID) { PRINT(L"PropVariantClear called\n"); return S_OK; }
|
||||
HRESULT CoInitializeEx(LPVOID, DWORD) { printf("CoInitializeEx called\n"); return S_OK; }
|
||||
void CoUninitialize() { printf("CoUninitialize called\n"); }
|
||||
void CoTaskMemFree(LPVOID) { printf("CoTaskMemFree called\n"); }
|
||||
HRESULT PropVariantClear(LPVOID) { printf("PropVariantClear called\n"); return S_OK; }
|
||||
|
||||
extern int stricmp(const char *s1, const char *s2);
|
||||
|
||||
@@ -38,6 +38,6 @@ void* ResolveOle32(const char* functionName) {
|
||||
return PropVariantClear;
|
||||
}
|
||||
|
||||
Print(L"Unknown function: %a\n", functionName);
|
||||
printf("Unknown function: %s\n", functionName);
|
||||
return NULL;
|
||||
}
|
||||
@@ -1,2 +1,7 @@
|
||||
#define PRINT Print
|
||||
//#define PRINT(...)
|
||||
#pragma once
|
||||
#include "printf.h"
|
||||
#if 0
|
||||
#define PRINT(...)
|
||||
#else
|
||||
#define PRINT printf
|
||||
#endif
|
||||
@@ -0,0 +1,914 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// \author (c) Marco Paland (info@paland.com)
|
||||
// 2014-2019, PALANDesign Hannover, Germany
|
||||
//
|
||||
// \license The MIT License (MIT)
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
//
|
||||
// \brief Tiny printf, sprintf and (v)snprintf implementation, optimized for speed on
|
||||
// embedded systems with a very limited resources. These routines are thread
|
||||
// safe and reentrant!
|
||||
// Use this instead of the bloated standard/newlib printf cause these use
|
||||
// malloc for printf (and may not be thread safe).
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "printf.h"
|
||||
|
||||
|
||||
// define this globally (e.g. gcc -DPRINTF_INCLUDE_CONFIG_H ...) to include the
|
||||
// printf_config.h header file
|
||||
// default: undefined
|
||||
#ifdef PRINTF_INCLUDE_CONFIG_H
|
||||
#include "printf_config.h"
|
||||
#endif
|
||||
|
||||
|
||||
// 'ntoa' conversion buffer size, this must be big enough to hold one converted
|
||||
// numeric number including padded zeros (dynamically created on stack)
|
||||
// default: 32 byte
|
||||
#ifndef PRINTF_NTOA_BUFFER_SIZE
|
||||
#define PRINTF_NTOA_BUFFER_SIZE 32U
|
||||
#endif
|
||||
|
||||
// 'ftoa' conversion buffer size, this must be big enough to hold one converted
|
||||
// float number including padded zeros (dynamically created on stack)
|
||||
// default: 32 byte
|
||||
#ifndef PRINTF_FTOA_BUFFER_SIZE
|
||||
#define PRINTF_FTOA_BUFFER_SIZE 32U
|
||||
#endif
|
||||
|
||||
// support for the floating point type (%f)
|
||||
// default: activated
|
||||
#ifndef PRINTF_DISABLE_SUPPORT_FLOAT
|
||||
#define PRINTF_SUPPORT_FLOAT
|
||||
#endif
|
||||
|
||||
// support for exponential floating point notation (%e/%g)
|
||||
// default: activated
|
||||
#ifndef PRINTF_DISABLE_SUPPORT_EXPONENTIAL
|
||||
#define PRINTF_SUPPORT_EXPONENTIAL
|
||||
#endif
|
||||
|
||||
// define the default floating point precision
|
||||
// default: 6 digits
|
||||
#ifndef PRINTF_DEFAULT_FLOAT_PRECISION
|
||||
#define PRINTF_DEFAULT_FLOAT_PRECISION 6U
|
||||
#endif
|
||||
|
||||
// define the largest float suitable to print with %f
|
||||
// default: 1e9
|
||||
#ifndef PRINTF_MAX_FLOAT
|
||||
#define PRINTF_MAX_FLOAT 1e9
|
||||
#endif
|
||||
|
||||
// support for the long long types (%llu or %p)
|
||||
// default: activated
|
||||
#ifndef PRINTF_DISABLE_SUPPORT_LONG_LONG
|
||||
#define PRINTF_SUPPORT_LONG_LONG
|
||||
#endif
|
||||
|
||||
// support for the ptrdiff_t type (%t)
|
||||
// ptrdiff_t is normally defined in <stddef.h> as long or long long type
|
||||
// default: activated
|
||||
#ifndef PRINTF_DISABLE_SUPPORT_PTRDIFF_T
|
||||
#define PRINTF_SUPPORT_PTRDIFF_T
|
||||
#endif
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// internal flag definitions
|
||||
#define FLAGS_ZEROPAD (1U << 0U)
|
||||
#define FLAGS_LEFT (1U << 1U)
|
||||
#define FLAGS_PLUS (1U << 2U)
|
||||
#define FLAGS_SPACE (1U << 3U)
|
||||
#define FLAGS_HASH (1U << 4U)
|
||||
#define FLAGS_UPPERCASE (1U << 5U)
|
||||
#define FLAGS_CHAR (1U << 6U)
|
||||
#define FLAGS_SHORT (1U << 7U)
|
||||
#define FLAGS_LONG (1U << 8U)
|
||||
#define FLAGS_LONG_LONG (1U << 9U)
|
||||
#define FLAGS_PRECISION (1U << 10U)
|
||||
#define FLAGS_ADAPT_EXP (1U << 11U)
|
||||
|
||||
|
||||
// import float.h for DBL_MAX
|
||||
#if defined(PRINTF_SUPPORT_FLOAT)
|
||||
#include <float.h>
|
||||
#endif
|
||||
|
||||
|
||||
// output function type
|
||||
typedef void (*out_fct_type)(char character, void* buffer, size_t idx, size_t maxlen);
|
||||
|
||||
|
||||
// wrapper (used as buffer) for output function type
|
||||
typedef struct {
|
||||
void (*fct)(char character, void* arg);
|
||||
void* arg;
|
||||
} out_fct_wrap_type;
|
||||
|
||||
|
||||
// internal buffer output
|
||||
static inline void _out_buffer(char character, void* buffer, size_t idx, size_t maxlen)
|
||||
{
|
||||
if (idx < maxlen) {
|
||||
((char*)buffer)[idx] = character;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// internal null output
|
||||
static inline void _out_null(char character, void* buffer, size_t idx, size_t maxlen)
|
||||
{
|
||||
(void)character; (void)buffer; (void)idx; (void)maxlen;
|
||||
}
|
||||
|
||||
|
||||
// internal _putchar wrapper
|
||||
static inline void _out_char(char character, void* buffer, size_t idx, size_t maxlen)
|
||||
{
|
||||
(void)buffer; (void)idx; (void)maxlen;
|
||||
if (character) {
|
||||
_putchar(character);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// internal output function wrapper
|
||||
static inline void _out_fct(char character, void* buffer, size_t idx, size_t maxlen)
|
||||
{
|
||||
(void)idx; (void)maxlen;
|
||||
if (character) {
|
||||
// buffer is the output fct pointer
|
||||
((out_fct_wrap_type*)buffer)->fct(character, ((out_fct_wrap_type*)buffer)->arg);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// internal secure strlen
|
||||
// \return The length of the string (excluding the terminating 0) limited by 'maxsize'
|
||||
static inline unsigned int _strnlen_s(const char* str, size_t maxsize)
|
||||
{
|
||||
const char* s;
|
||||
for (s = str; *s && maxsize--; ++s);
|
||||
return (unsigned int)(s - str);
|
||||
}
|
||||
|
||||
|
||||
// internal test if char is a digit (0-9)
|
||||
// \return true if char is a digit
|
||||
static inline bool _is_digit(char ch)
|
||||
{
|
||||
return (ch >= '0') && (ch <= '9');
|
||||
}
|
||||
|
||||
|
||||
// internal ASCII string to unsigned int conversion
|
||||
static unsigned int _atoi(const char** str)
|
||||
{
|
||||
unsigned int i = 0U;
|
||||
while (_is_digit(**str)) {
|
||||
i = i * 10U + (unsigned int)(*((*str)++) - '0');
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
|
||||
// output the specified string in reverse, taking care of any zero-padding
|
||||
static size_t _out_rev(out_fct_type out, char* buffer, size_t idx, size_t maxlen, const char* buf, size_t len, unsigned int width, unsigned int flags)
|
||||
{
|
||||
const size_t start_idx = idx;
|
||||
|
||||
// pad spaces up to given width
|
||||
if (!(flags & FLAGS_LEFT) && !(flags & FLAGS_ZEROPAD)) {
|
||||
for (size_t i = len; i < width; i++) {
|
||||
out(' ', buffer, idx++, maxlen);
|
||||
}
|
||||
}
|
||||
|
||||
// reverse string
|
||||
while (len) {
|
||||
out(buf[--len], buffer, idx++, maxlen);
|
||||
}
|
||||
|
||||
// append pad spaces up to given width
|
||||
if (flags & FLAGS_LEFT) {
|
||||
while (idx - start_idx < width) {
|
||||
out(' ', buffer, idx++, maxlen);
|
||||
}
|
||||
}
|
||||
|
||||
return idx;
|
||||
}
|
||||
|
||||
|
||||
// internal itoa format
|
||||
static size_t _ntoa_format(out_fct_type out, char* buffer, size_t idx, size_t maxlen, char* buf, size_t len, bool negative, unsigned int base, unsigned int prec, unsigned int width, unsigned int flags)
|
||||
{
|
||||
// pad leading zeros
|
||||
if (!(flags & FLAGS_LEFT)) {
|
||||
if (width && (flags & FLAGS_ZEROPAD) && (negative || (flags & (FLAGS_PLUS | FLAGS_SPACE)))) {
|
||||
width--;
|
||||
}
|
||||
while ((len < prec) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
|
||||
buf[len++] = '0';
|
||||
}
|
||||
while ((flags & FLAGS_ZEROPAD) && (len < width) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
|
||||
buf[len++] = '0';
|
||||
}
|
||||
}
|
||||
|
||||
// handle hash
|
||||
if (flags & FLAGS_HASH) {
|
||||
if (!(flags & FLAGS_PRECISION) && len && ((len == prec) || (len == width))) {
|
||||
len--;
|
||||
if (len && (base == 16U)) {
|
||||
len--;
|
||||
}
|
||||
}
|
||||
if ((base == 16U) && !(flags & FLAGS_UPPERCASE) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
|
||||
buf[len++] = 'x';
|
||||
}
|
||||
else if ((base == 16U) && (flags & FLAGS_UPPERCASE) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
|
||||
buf[len++] = 'X';
|
||||
}
|
||||
else if ((base == 2U) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
|
||||
buf[len++] = 'b';
|
||||
}
|
||||
if (len < PRINTF_NTOA_BUFFER_SIZE) {
|
||||
buf[len++] = '0';
|
||||
}
|
||||
}
|
||||
|
||||
if (len < PRINTF_NTOA_BUFFER_SIZE) {
|
||||
if (negative) {
|
||||
buf[len++] = '-';
|
||||
}
|
||||
else if (flags & FLAGS_PLUS) {
|
||||
buf[len++] = '+'; // ignore the space if the '+' exists
|
||||
}
|
||||
else if (flags & FLAGS_SPACE) {
|
||||
buf[len++] = ' ';
|
||||
}
|
||||
}
|
||||
|
||||
return _out_rev(out, buffer, idx, maxlen, buf, len, width, flags);
|
||||
}
|
||||
|
||||
|
||||
// internal itoa for 'long' type
|
||||
static size_t _ntoa_long(out_fct_type out, char* buffer, size_t idx, size_t maxlen, unsigned long value, bool negative, unsigned long base, unsigned int prec, unsigned int width, unsigned int flags)
|
||||
{
|
||||
char buf[PRINTF_NTOA_BUFFER_SIZE];
|
||||
size_t len = 0U;
|
||||
|
||||
// no hash for 0 values
|
||||
if (!value) {
|
||||
flags &= ~FLAGS_HASH;
|
||||
}
|
||||
|
||||
// write if precision != 0 and value is != 0
|
||||
if (!(flags & FLAGS_PRECISION) || value) {
|
||||
do {
|
||||
const char digit = (char)(value % base);
|
||||
buf[len++] = digit < 10 ? '0' + digit : (flags & FLAGS_UPPERCASE ? 'A' : 'a') + digit - 10;
|
||||
value /= base;
|
||||
} while (value && (len < PRINTF_NTOA_BUFFER_SIZE));
|
||||
}
|
||||
|
||||
return _ntoa_format(out, buffer, idx, maxlen, buf, len, negative, (unsigned int)base, prec, width, flags);
|
||||
}
|
||||
|
||||
|
||||
// internal itoa for 'long long' type
|
||||
#if defined(PRINTF_SUPPORT_LONG_LONG)
|
||||
static size_t _ntoa_long_long(out_fct_type out, char* buffer, size_t idx, size_t maxlen, unsigned long long value, bool negative, unsigned long long base, unsigned int prec, unsigned int width, unsigned int flags)
|
||||
{
|
||||
char buf[PRINTF_NTOA_BUFFER_SIZE];
|
||||
size_t len = 0U;
|
||||
|
||||
// no hash for 0 values
|
||||
if (!value) {
|
||||
flags &= ~FLAGS_HASH;
|
||||
}
|
||||
|
||||
// write if precision != 0 and value is != 0
|
||||
if (!(flags & FLAGS_PRECISION) || value) {
|
||||
do {
|
||||
const char digit = (char)(value % base);
|
||||
buf[len++] = digit < 10 ? '0' + digit : (flags & FLAGS_UPPERCASE ? 'A' : 'a') + digit - 10;
|
||||
value /= base;
|
||||
} while (value && (len < PRINTF_NTOA_BUFFER_SIZE));
|
||||
}
|
||||
|
||||
return _ntoa_format(out, buffer, idx, maxlen, buf, len, negative, (unsigned int)base, prec, width, flags);
|
||||
}
|
||||
#endif // PRINTF_SUPPORT_LONG_LONG
|
||||
|
||||
|
||||
#if defined(PRINTF_SUPPORT_FLOAT)
|
||||
|
||||
#if defined(PRINTF_SUPPORT_EXPONENTIAL)
|
||||
// forward declaration so that _ftoa can switch to exp notation for values > PRINTF_MAX_FLOAT
|
||||
static size_t _etoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width, unsigned int flags);
|
||||
#endif
|
||||
|
||||
|
||||
// internal ftoa for fixed decimal floating point
|
||||
static size_t _ftoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width, unsigned int flags)
|
||||
{
|
||||
char buf[PRINTF_FTOA_BUFFER_SIZE];
|
||||
size_t len = 0U;
|
||||
double diff = 0.0;
|
||||
|
||||
// powers of 10
|
||||
static const double pow10[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 };
|
||||
|
||||
// test for special values
|
||||
if (value != value)
|
||||
return _out_rev(out, buffer, idx, maxlen, "nan", 3, width, flags);
|
||||
if (value < -DBL_MAX)
|
||||
return _out_rev(out, buffer, idx, maxlen, "fni-", 4, width, flags);
|
||||
if (value > DBL_MAX)
|
||||
return _out_rev(out, buffer, idx, maxlen, (flags & FLAGS_PLUS) ? "fni+" : "fni", (flags & FLAGS_PLUS) ? 4U : 3U, width, flags);
|
||||
|
||||
// test for very large values
|
||||
// standard printf behavior is to print EVERY whole number digit -- which could be 100s of characters overflowing your buffers == bad
|
||||
if ((value > PRINTF_MAX_FLOAT) || (value < -PRINTF_MAX_FLOAT)) {
|
||||
#if defined(PRINTF_SUPPORT_EXPONENTIAL)
|
||||
return _etoa(out, buffer, idx, maxlen, value, prec, width, flags);
|
||||
#else
|
||||
return 0U;
|
||||
#endif
|
||||
}
|
||||
|
||||
// test for negative
|
||||
bool negative = false;
|
||||
if (value < 0) {
|
||||
negative = true;
|
||||
value = 0 - value;
|
||||
}
|
||||
|
||||
// set default precision, if not set explicitly
|
||||
if (!(flags & FLAGS_PRECISION)) {
|
||||
prec = PRINTF_DEFAULT_FLOAT_PRECISION;
|
||||
}
|
||||
// limit precision to 9, cause a prec >= 10 can lead to overflow errors
|
||||
while ((len < PRINTF_FTOA_BUFFER_SIZE) && (prec > 9U)) {
|
||||
buf[len++] = '0';
|
||||
prec--;
|
||||
}
|
||||
|
||||
int whole = (int)value;
|
||||
double tmp = (value - whole) * pow10[prec];
|
||||
unsigned long frac = (unsigned long)tmp;
|
||||
diff = tmp - frac;
|
||||
|
||||
if (diff > 0.5) {
|
||||
++frac;
|
||||
// handle rollover, e.g. case 0.99 with prec 1 is 1.0
|
||||
if (frac >= pow10[prec]) {
|
||||
frac = 0;
|
||||
++whole;
|
||||
}
|
||||
}
|
||||
else if (diff < 0.5) {
|
||||
}
|
||||
else if ((frac == 0U) || (frac & 1U)) {
|
||||
// if halfway, round up if odd OR if last digit is 0
|
||||
++frac;
|
||||
}
|
||||
|
||||
if (prec == 0U) {
|
||||
diff = value - (double)whole;
|
||||
if ((!(diff < 0.5) || (diff > 0.5)) && (whole & 1)) {
|
||||
// exactly 0.5 and ODD, then round up
|
||||
// 1.5 -> 2, but 2.5 -> 2
|
||||
++whole;
|
||||
}
|
||||
}
|
||||
else {
|
||||
unsigned int count = prec;
|
||||
// now do fractional part, as an unsigned number
|
||||
while (len < PRINTF_FTOA_BUFFER_SIZE) {
|
||||
--count;
|
||||
buf[len++] = (char)(48U + (frac % 10U));
|
||||
if (!(frac /= 10U)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
// add extra 0s
|
||||
while ((len < PRINTF_FTOA_BUFFER_SIZE) && (count-- > 0U)) {
|
||||
buf[len++] = '0';
|
||||
}
|
||||
if (len < PRINTF_FTOA_BUFFER_SIZE) {
|
||||
// add decimal
|
||||
buf[len++] = '.';
|
||||
}
|
||||
}
|
||||
|
||||
// do whole part, number is reversed
|
||||
while (len < PRINTF_FTOA_BUFFER_SIZE) {
|
||||
buf[len++] = (char)(48 + (whole % 10));
|
||||
if (!(whole /= 10)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// pad leading zeros
|
||||
if (!(flags & FLAGS_LEFT) && (flags & FLAGS_ZEROPAD)) {
|
||||
if (width && (negative || (flags & (FLAGS_PLUS | FLAGS_SPACE)))) {
|
||||
width--;
|
||||
}
|
||||
while ((len < width) && (len < PRINTF_FTOA_BUFFER_SIZE)) {
|
||||
buf[len++] = '0';
|
||||
}
|
||||
}
|
||||
|
||||
if (len < PRINTF_FTOA_BUFFER_SIZE) {
|
||||
if (negative) {
|
||||
buf[len++] = '-';
|
||||
}
|
||||
else if (flags & FLAGS_PLUS) {
|
||||
buf[len++] = '+'; // ignore the space if the '+' exists
|
||||
}
|
||||
else if (flags & FLAGS_SPACE) {
|
||||
buf[len++] = ' ';
|
||||
}
|
||||
}
|
||||
|
||||
return _out_rev(out, buffer, idx, maxlen, buf, len, width, flags);
|
||||
}
|
||||
|
||||
|
||||
#if defined(PRINTF_SUPPORT_EXPONENTIAL)
|
||||
// internal ftoa variant for exponential floating-point type, contributed by Martijn Jasperse <m.jasperse@gmail.com>
|
||||
static size_t _etoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width, unsigned int flags)
|
||||
{
|
||||
// check for NaN and special values
|
||||
if ((value != value) || (value > DBL_MAX) || (value < -DBL_MAX)) {
|
||||
return _ftoa(out, buffer, idx, maxlen, value, prec, width, flags);
|
||||
}
|
||||
|
||||
// determine the sign
|
||||
const bool negative = value < 0;
|
||||
if (negative) {
|
||||
value = -value;
|
||||
}
|
||||
|
||||
// default precision
|
||||
if (!(flags & FLAGS_PRECISION)) {
|
||||
prec = PRINTF_DEFAULT_FLOAT_PRECISION;
|
||||
}
|
||||
|
||||
// determine the decimal exponent
|
||||
// based on the algorithm by David Gay (https://www.ampl.com/netlib/fp/dtoa.c)
|
||||
union {
|
||||
uint64_t U;
|
||||
double F;
|
||||
} conv;
|
||||
|
||||
conv.F = value;
|
||||
int exp2 = (int)((conv.U >> 52U) & 0x07FFU) - 1023; // effectively log2
|
||||
conv.U = (conv.U & ((1ULL << 52U) - 1U)) | (1023ULL << 52U); // drop the exponent so conv.F is now in [1,2)
|
||||
// now approximate log10 from the log2 integer part and an expansion of ln around 1.5
|
||||
int expval = (int)(0.1760912590558 + exp2 * 0.301029995663981 + (conv.F - 1.5) * 0.289529654602168);
|
||||
// now we want to compute 10^expval but we want to be sure it won't overflow
|
||||
exp2 = (int)(expval * 3.321928094887362 + 0.5);
|
||||
const double z = expval * 2.302585092994046 - exp2 * 0.6931471805599453;
|
||||
const double z2 = z * z;
|
||||
conv.U = (uint64_t)(exp2 + 1023) << 52U;
|
||||
// compute exp(z) using continued fractions, see https://en.wikipedia.org/wiki/Exponential_function#Continued_fractions_for_ex
|
||||
conv.F *= 1 + 2 * z / (2 - z + (z2 / (6 + (z2 / (10 + z2 / 14)))));
|
||||
// correct for rounding errors
|
||||
if (value < conv.F) {
|
||||
expval--;
|
||||
conv.F /= 10;
|
||||
}
|
||||
|
||||
// the exponent format is "%+03d" and largest value is "307", so set aside 4-5 characters
|
||||
unsigned int minwidth = ((expval < 100) && (expval > -100)) ? 4U : 5U;
|
||||
|
||||
// in "%g" mode, "prec" is the number of *significant figures* not decimals
|
||||
if (flags & FLAGS_ADAPT_EXP) {
|
||||
// do we want to fall-back to "%f" mode?
|
||||
if ((value >= 1e-4) && (value < 1e6)) {
|
||||
if ((int)prec > expval) {
|
||||
prec = (unsigned)((int)prec - expval - 1);
|
||||
}
|
||||
else {
|
||||
prec = 0;
|
||||
}
|
||||
flags |= FLAGS_PRECISION; // make sure _ftoa respects precision
|
||||
// no characters in exponent
|
||||
minwidth = 0U;
|
||||
expval = 0;
|
||||
}
|
||||
else {
|
||||
// we use one sigfig for the whole part
|
||||
if ((prec > 0) && (flags & FLAGS_PRECISION)) {
|
||||
--prec;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// will everything fit?
|
||||
unsigned int fwidth = width;
|
||||
if (width > minwidth) {
|
||||
// we didn't fall-back so subtract the characters required for the exponent
|
||||
fwidth -= minwidth;
|
||||
} else {
|
||||
// not enough characters, so go back to default sizing
|
||||
fwidth = 0U;
|
||||
}
|
||||
if ((flags & FLAGS_LEFT) && minwidth) {
|
||||
// if we're padding on the right, DON'T pad the floating part
|
||||
fwidth = 0U;
|
||||
}
|
||||
|
||||
// rescale the float value
|
||||
if (expval) {
|
||||
value /= conv.F;
|
||||
}
|
||||
|
||||
// output the floating part
|
||||
const size_t start_idx = idx;
|
||||
idx = _ftoa(out, buffer, idx, maxlen, negative ? -value : value, prec, fwidth, flags & ~FLAGS_ADAPT_EXP);
|
||||
|
||||
// output the exponent part
|
||||
if (minwidth) {
|
||||
// output the exponential symbol
|
||||
out((flags & FLAGS_UPPERCASE) ? 'E' : 'e', buffer, idx++, maxlen);
|
||||
// output the exponent value
|
||||
idx = _ntoa_long(out, buffer, idx, maxlen, (expval < 0) ? -expval : expval, expval < 0, 10, 0, minwidth-1, FLAGS_ZEROPAD | FLAGS_PLUS);
|
||||
// might need to right-pad spaces
|
||||
if (flags & FLAGS_LEFT) {
|
||||
while (idx - start_idx < width) out(' ', buffer, idx++, maxlen);
|
||||
}
|
||||
}
|
||||
return idx;
|
||||
}
|
||||
#endif // PRINTF_SUPPORT_EXPONENTIAL
|
||||
#endif // PRINTF_SUPPORT_FLOAT
|
||||
|
||||
|
||||
// internal vsnprintf
|
||||
static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const char* format, va_list va)
|
||||
{
|
||||
unsigned int flags, width, precision, n;
|
||||
size_t idx = 0U;
|
||||
|
||||
if (!buffer) {
|
||||
// use null output function
|
||||
out = _out_null;
|
||||
}
|
||||
|
||||
while (*format)
|
||||
{
|
||||
// format specifier? %[flags][width][.precision][length]
|
||||
if (*format != '%') {
|
||||
// no
|
||||
out(*format, buffer, idx++, maxlen);
|
||||
format++;
|
||||
continue;
|
||||
}
|
||||
else {
|
||||
// yes, evaluate it
|
||||
format++;
|
||||
}
|
||||
|
||||
// evaluate flags
|
||||
flags = 0U;
|
||||
do {
|
||||
switch (*format) {
|
||||
case '0': flags |= FLAGS_ZEROPAD; format++; n = 1U; break;
|
||||
case '-': flags |= FLAGS_LEFT; format++; n = 1U; break;
|
||||
case '+': flags |= FLAGS_PLUS; format++; n = 1U; break;
|
||||
case ' ': flags |= FLAGS_SPACE; format++; n = 1U; break;
|
||||
case '#': flags |= FLAGS_HASH; format++; n = 1U; break;
|
||||
default : n = 0U; break;
|
||||
}
|
||||
} while (n);
|
||||
|
||||
// evaluate width field
|
||||
width = 0U;
|
||||
if (_is_digit(*format)) {
|
||||
width = _atoi(&format);
|
||||
}
|
||||
else if (*format == '*') {
|
||||
const int w = va_arg(va, int);
|
||||
if (w < 0) {
|
||||
flags |= FLAGS_LEFT; // reverse padding
|
||||
width = (unsigned int)-w;
|
||||
}
|
||||
else {
|
||||
width = (unsigned int)w;
|
||||
}
|
||||
format++;
|
||||
}
|
||||
|
||||
// evaluate precision field
|
||||
precision = 0U;
|
||||
if (*format == '.') {
|
||||
flags |= FLAGS_PRECISION;
|
||||
format++;
|
||||
if (_is_digit(*format)) {
|
||||
precision = _atoi(&format);
|
||||
}
|
||||
else if (*format == '*') {
|
||||
const int prec = (int)va_arg(va, int);
|
||||
precision = prec > 0 ? (unsigned int)prec : 0U;
|
||||
format++;
|
||||
}
|
||||
}
|
||||
|
||||
// evaluate length field
|
||||
switch (*format) {
|
||||
case 'l' :
|
||||
flags |= FLAGS_LONG;
|
||||
format++;
|
||||
if (*format == 'l') {
|
||||
flags |= FLAGS_LONG_LONG;
|
||||
format++;
|
||||
}
|
||||
break;
|
||||
case 'h' :
|
||||
flags |= FLAGS_SHORT;
|
||||
format++;
|
||||
if (*format == 'h') {
|
||||
flags |= FLAGS_CHAR;
|
||||
format++;
|
||||
}
|
||||
break;
|
||||
#if defined(PRINTF_SUPPORT_PTRDIFF_T)
|
||||
case 't' :
|
||||
flags |= (sizeof(ptrdiff_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG);
|
||||
format++;
|
||||
break;
|
||||
#endif
|
||||
case 'j' :
|
||||
flags |= (sizeof(intmax_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG);
|
||||
format++;
|
||||
break;
|
||||
case 'z' :
|
||||
flags |= (sizeof(size_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG);
|
||||
format++;
|
||||
break;
|
||||
default :
|
||||
break;
|
||||
}
|
||||
|
||||
// evaluate specifier
|
||||
switch (*format) {
|
||||
case 'd' :
|
||||
case 'i' :
|
||||
case 'u' :
|
||||
case 'x' :
|
||||
case 'X' :
|
||||
case 'o' :
|
||||
case 'b' : {
|
||||
// set the base
|
||||
unsigned int base;
|
||||
if (*format == 'x' || *format == 'X') {
|
||||
base = 16U;
|
||||
}
|
||||
else if (*format == 'o') {
|
||||
base = 8U;
|
||||
}
|
||||
else if (*format == 'b') {
|
||||
base = 2U;
|
||||
}
|
||||
else {
|
||||
base = 10U;
|
||||
flags &= ~FLAGS_HASH; // no hash for dec format
|
||||
}
|
||||
// uppercase
|
||||
if (*format == 'X') {
|
||||
flags |= FLAGS_UPPERCASE;
|
||||
}
|
||||
|
||||
// no plus or space flag for u, x, X, o, b
|
||||
if ((*format != 'i') && (*format != 'd')) {
|
||||
flags &= ~(FLAGS_PLUS | FLAGS_SPACE);
|
||||
}
|
||||
|
||||
// ignore '0' flag when precision is given
|
||||
if (flags & FLAGS_PRECISION) {
|
||||
flags &= ~FLAGS_ZEROPAD;
|
||||
}
|
||||
|
||||
// convert the integer
|
||||
if ((*format == 'i') || (*format == 'd')) {
|
||||
// signed
|
||||
if (flags & FLAGS_LONG_LONG) {
|
||||
#if defined(PRINTF_SUPPORT_LONG_LONG)
|
||||
const long long value = va_arg(va, long long);
|
||||
idx = _ntoa_long_long(out, buffer, idx, maxlen, (unsigned long long)(value > 0 ? value : 0 - value), value < 0, base, precision, width, flags);
|
||||
#endif
|
||||
}
|
||||
else if (flags & FLAGS_LONG) {
|
||||
const long value = va_arg(va, long);
|
||||
idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned long)(value > 0 ? value : 0 - value), value < 0, base, precision, width, flags);
|
||||
}
|
||||
else {
|
||||
const int value = (flags & FLAGS_CHAR) ? (char)va_arg(va, int) : (flags & FLAGS_SHORT) ? (short int)va_arg(va, int) : va_arg(va, int);
|
||||
idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned int)(value > 0 ? value : 0 - value), value < 0, base, precision, width, flags);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// unsigned
|
||||
if (flags & FLAGS_LONG_LONG) {
|
||||
#if defined(PRINTF_SUPPORT_LONG_LONG)
|
||||
idx = _ntoa_long_long(out, buffer, idx, maxlen, va_arg(va, unsigned long long), false, base, precision, width, flags);
|
||||
#endif
|
||||
}
|
||||
else if (flags & FLAGS_LONG) {
|
||||
idx = _ntoa_long(out, buffer, idx, maxlen, va_arg(va, unsigned long), false, base, precision, width, flags);
|
||||
}
|
||||
else {
|
||||
const unsigned int value = (flags & FLAGS_CHAR) ? (unsigned char)va_arg(va, unsigned int) : (flags & FLAGS_SHORT) ? (unsigned short int)va_arg(va, unsigned int) : va_arg(va, unsigned int);
|
||||
idx = _ntoa_long(out, buffer, idx, maxlen, value, false, base, precision, width, flags);
|
||||
}
|
||||
}
|
||||
format++;
|
||||
break;
|
||||
}
|
||||
#if defined(PRINTF_SUPPORT_FLOAT)
|
||||
case 'f' :
|
||||
case 'F' :
|
||||
if (*format == 'F') flags |= FLAGS_UPPERCASE;
|
||||
idx = _ftoa(out, buffer, idx, maxlen, va_arg(va, double), precision, width, flags);
|
||||
format++;
|
||||
break;
|
||||
#if defined(PRINTF_SUPPORT_EXPONENTIAL)
|
||||
case 'e':
|
||||
case 'E':
|
||||
case 'g':
|
||||
case 'G':
|
||||
if ((*format == 'g')||(*format == 'G')) flags |= FLAGS_ADAPT_EXP;
|
||||
if ((*format == 'E')||(*format == 'G')) flags |= FLAGS_UPPERCASE;
|
||||
idx = _etoa(out, buffer, idx, maxlen, va_arg(va, double), precision, width, flags);
|
||||
format++;
|
||||
break;
|
||||
#endif // PRINTF_SUPPORT_EXPONENTIAL
|
||||
#endif // PRINTF_SUPPORT_FLOAT
|
||||
case 'c' : {
|
||||
unsigned int l = 1U;
|
||||
// pre padding
|
||||
if (!(flags & FLAGS_LEFT)) {
|
||||
while (l++ < width) {
|
||||
out(' ', buffer, idx++, maxlen);
|
||||
}
|
||||
}
|
||||
// char output
|
||||
out((char)va_arg(va, int), buffer, idx++, maxlen);
|
||||
// post padding
|
||||
if (flags & FLAGS_LEFT) {
|
||||
while (l++ < width) {
|
||||
out(' ', buffer, idx++, maxlen);
|
||||
}
|
||||
}
|
||||
format++;
|
||||
break;
|
||||
}
|
||||
|
||||
case 's' : {
|
||||
const char* p = va_arg(va, char*);
|
||||
unsigned int l = _strnlen_s(p, precision ? precision : (size_t)-1);
|
||||
// pre padding
|
||||
if (flags & FLAGS_PRECISION) {
|
||||
l = (l < precision ? l : precision);
|
||||
}
|
||||
if (!(flags & FLAGS_LEFT)) {
|
||||
while (l++ < width) {
|
||||
out(' ', buffer, idx++, maxlen);
|
||||
}
|
||||
}
|
||||
// string output
|
||||
while ((*p != 0) && (!(flags & FLAGS_PRECISION) || precision--)) {
|
||||
out(*(p++), buffer, idx++, maxlen);
|
||||
}
|
||||
// post padding
|
||||
if (flags & FLAGS_LEFT) {
|
||||
while (l++ < width) {
|
||||
out(' ', buffer, idx++, maxlen);
|
||||
}
|
||||
}
|
||||
format++;
|
||||
break;
|
||||
}
|
||||
|
||||
case 'p' : {
|
||||
width = sizeof(void*) * 2U;
|
||||
flags |= FLAGS_ZEROPAD | FLAGS_UPPERCASE;
|
||||
#if defined(PRINTF_SUPPORT_LONG_LONG)
|
||||
const bool is_ll = sizeof(uintptr_t) == sizeof(long long);
|
||||
if (is_ll) {
|
||||
idx = _ntoa_long_long(out, buffer, idx, maxlen, (uintptr_t)va_arg(va, void*), false, 16U, precision, width, flags);
|
||||
}
|
||||
else {
|
||||
#endif
|
||||
idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned long)((uintptr_t)va_arg(va, void*)), false, 16U, precision, width, flags);
|
||||
#if defined(PRINTF_SUPPORT_LONG_LONG)
|
||||
}
|
||||
#endif
|
||||
format++;
|
||||
break;
|
||||
}
|
||||
|
||||
case '%' :
|
||||
out('%', buffer, idx++, maxlen);
|
||||
format++;
|
||||
break;
|
||||
|
||||
default :
|
||||
out(*format, buffer, idx++, maxlen);
|
||||
format++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// termination
|
||||
out((char)0, buffer, idx < maxlen ? idx : maxlen - 1U, maxlen);
|
||||
|
||||
// return written chars without terminating \0
|
||||
return (int)idx;
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
int printf_(const char* format, ...)
|
||||
{
|
||||
va_list va;
|
||||
va_start(va, format);
|
||||
char buffer[1];
|
||||
const int ret = _vsnprintf(_out_char, buffer, (size_t)-1, format, va);
|
||||
va_end(va);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
int sprintf_(char* buffer, const char* format, ...)
|
||||
{
|
||||
va_list va;
|
||||
va_start(va, format);
|
||||
const int ret = _vsnprintf(_out_buffer, buffer, (size_t)-1, format, va);
|
||||
va_end(va);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
int snprintf_(char* buffer, size_t count, const char* format, ...)
|
||||
{
|
||||
va_list va;
|
||||
va_start(va, format);
|
||||
const int ret = _vsnprintf(_out_buffer, buffer, count, format, va);
|
||||
va_end(va);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
int vprintf_(const char* format, va_list va)
|
||||
{
|
||||
char buffer[1];
|
||||
return _vsnprintf(_out_char, buffer, (size_t)-1, format, va);
|
||||
}
|
||||
|
||||
|
||||
int vsnprintf_(char* buffer, size_t count, const char* format, va_list va)
|
||||
{
|
||||
return _vsnprintf(_out_buffer, buffer, count, format, va);
|
||||
}
|
||||
|
||||
|
||||
int fctprintf(void (*out)(char character, void* arg), void* arg, const char* format, ...)
|
||||
{
|
||||
va_list va;
|
||||
va_start(va, format);
|
||||
const out_fct_wrap_type out_fct_wrap = { out, arg };
|
||||
const int ret = _vsnprintf(_out_fct, (char*)(uintptr_t)&out_fct_wrap, (size_t)-1, format, va);
|
||||
va_end(va);
|
||||
return ret;
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// \author (c) Marco Paland (info@paland.com)
|
||||
// 2014-2019, PALANDesign Hannover, Germany
|
||||
//
|
||||
// \license The MIT License (MIT)
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
//
|
||||
// \brief Tiny printf, sprintf and snprintf implementation, optimized for speed on
|
||||
// embedded systems with a very limited resources.
|
||||
// Use this instead of bloated standard/newlib printf.
|
||||
// These routines are thread safe and reentrant.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef _PRINTF_H_
|
||||
#define _PRINTF_H_
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
/**
|
||||
* Output a character to a custom device like UART, used by the printf() function
|
||||
* This function is declared here only. You have to write your custom implementation somewhere
|
||||
* \param character Character to output
|
||||
*/
|
||||
void _putchar(char character);
|
||||
|
||||
|
||||
/**
|
||||
* Tiny printf implementation
|
||||
* You have to implement _putchar if you use printf()
|
||||
* To avoid conflicts with the regular printf() API it is overridden by macro defines
|
||||
* and internal underscore-appended functions like printf_() are used
|
||||
* \param format A string that specifies the format of the output
|
||||
* \return The number of characters that are written into the array, not counting the terminating null character
|
||||
*/
|
||||
#define printf printf_
|
||||
int printf_(const char* format, ...);
|
||||
|
||||
|
||||
/**
|
||||
* Tiny sprintf implementation
|
||||
* Due to security reasons (buffer overflow) YOU SHOULD CONSIDER USING (V)SNPRINTF INSTEAD!
|
||||
* \param buffer A pointer to the buffer where to store the formatted string. MUST be big enough to store the output!
|
||||
* \param format A string that specifies the format of the output
|
||||
* \return The number of characters that are WRITTEN into the buffer, not counting the terminating null character
|
||||
*/
|
||||
#define sprintf sprintf_
|
||||
int sprintf_(char* buffer, const char* format, ...);
|
||||
|
||||
|
||||
/**
|
||||
* Tiny snprintf/vsnprintf implementation
|
||||
* \param buffer A pointer to the buffer where to store the formatted string
|
||||
* \param count The maximum number of characters to store in the buffer, including a terminating null character
|
||||
* \param format A string that specifies the format of the output
|
||||
* \param va A value identifying a variable arguments list
|
||||
* \return The number of characters that COULD have been written into the buffer, not counting the terminating
|
||||
* null character. A value equal or larger than count indicates truncation. Only when the returned value
|
||||
* is non-negative and less than count, the string has been completely written.
|
||||
*/
|
||||
#define snprintf snprintf_
|
||||
#define vsnprintf vsnprintf_
|
||||
int snprintf_(char* buffer, size_t count, const char* format, ...);
|
||||
int vsnprintf_(char* buffer, size_t count, const char* format, va_list va);
|
||||
|
||||
|
||||
/**
|
||||
* Tiny vprintf implementation
|
||||
* \param format A string that specifies the format of the output
|
||||
* \param va A value identifying a variable arguments list
|
||||
* \return The number of characters that are WRITTEN into the buffer, not counting the terminating null character
|
||||
*/
|
||||
#define vprintf vprintf_
|
||||
int vprintf_(const char* format, va_list va);
|
||||
|
||||
|
||||
/**
|
||||
* printf with output function
|
||||
* You may use this as dynamic alternative to printf() with its fixed _putchar() output
|
||||
* \param out An output function which takes one character and an argument pointer
|
||||
* \param arg An argument pointer for user data passed to output function
|
||||
* \param format A string that specifies the format of the output
|
||||
* \return The number of characters that are sent to the output function, not counting the terminating null character
|
||||
*/
|
||||
int fctprintf(void (*out)(char character, void* arg), void* arg, const char* format, ...);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif // _PRINTF_H_
|
||||
+18
-18
@@ -22,9 +22,9 @@ static struct BlockHeader* g_freeList = 0;
|
||||
#define ALIGN_UP(x, a) (((x) + ((a) - 1)) & ~((a) - 1))
|
||||
|
||||
void InitAllocatorFromBuffer(void* buffer, size_t size) {
|
||||
PRINT(L"Initializing custom allocator with buffer %p of size %ull\n", buffer, size);
|
||||
printf("Initializing custom allocator with buffer %p of size %ull\n", buffer, size);
|
||||
if (!buffer || size <= sizeof(struct BlockHeader)) {
|
||||
Print(L"Invalid buffer or size for allocator initialization\n");
|
||||
printf("Invalid buffer or size for allocator initialization\n");
|
||||
gBS->Stall(5 * 1000000); // 5 seconds
|
||||
gRT->ResetSystem(EfiResetShutdown, EFI_SUCCESS, 0, NULL);
|
||||
}
|
||||
@@ -45,7 +45,7 @@ static bool IsPointerInPool(void* ptr) {
|
||||
}
|
||||
|
||||
void* Malloc(size_t size) {
|
||||
PRINT(L"Allocating memory of size %llu, ptr = ", size);
|
||||
printf("Allocating memory of size %llu, ptr = ", size);
|
||||
size = ALIGN_UP(size, AVX_ALIGN); // ensure AVX alignment
|
||||
struct BlockHeader* best = 0;
|
||||
struct BlockHeader* bestPrev = 0;
|
||||
@@ -64,7 +64,7 @@ void* Malloc(size_t size) {
|
||||
}
|
||||
|
||||
if (!best) {
|
||||
PRINT(L"NULL\n");
|
||||
printf("NULL\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -74,7 +74,7 @@ void* Malloc(size_t size) {
|
||||
|
||||
size_t totalNeeded = size + padding;
|
||||
if (best->size < totalNeeded) {
|
||||
PRINT(L"NULL not enough space\n");
|
||||
printf("NULL not enough space\n");
|
||||
return 0; // not enough space
|
||||
}
|
||||
|
||||
@@ -93,12 +93,12 @@ void* Malloc(size_t size) {
|
||||
best->free = false;
|
||||
best->size = totalNeeded;
|
||||
|
||||
PRINT(L"%p\n", (void*)alignedAddr);
|
||||
printf("%p\n", (void*)alignedAddr);
|
||||
return (void*)alignedAddr;
|
||||
}
|
||||
|
||||
void Free(void* ptr) {
|
||||
PRINT(L"Freeing memory at %p\n", ptr);
|
||||
printf("Freeing memory at %p\n", ptr);
|
||||
if (!ptr || !IsPointerInPool(ptr)) return;
|
||||
|
||||
struct BlockHeader* block = (struct BlockHeader*)((uint8_t*)ptr - sizeof(struct BlockHeader));
|
||||
@@ -108,7 +108,7 @@ void Free(void* ptr) {
|
||||
}
|
||||
|
||||
if (!IsPointerInPool(block) || block->magic != MAGIC_ALLOCATED) {
|
||||
Print(L"Invalid pointer passed to Free: %p\n", ptr);
|
||||
printf("Invalid pointer passed to Free: %p\n", ptr);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -244,7 +244,7 @@ void* ResolveImport(const char* dllName, const char* functionName) {
|
||||
}
|
||||
|
||||
// If the import is not a known dependency, print an error
|
||||
Print(L"Unknown dll: %a\n", dllName);
|
||||
printf("Unknown dll: %s\n", dllName);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -288,18 +288,18 @@ void* LoadPeImage(void* buffer) {
|
||||
uint8_t* data = (uint8_t*)buffer;
|
||||
IMAGE_DOS_HEADER* dos = (IMAGE_DOS_HEADER*)data;
|
||||
if (dos->e_magic != IMAGE_DOS_SIGNATURE) {
|
||||
Print(L"Invalid MZ header\n");
|
||||
printf("Invalid MZ header\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
IMAGE_NT_HEADERS64* pe = (IMAGE_NT_HEADERS64*)(data + dos->e_lfanew);
|
||||
if (pe->Signature != IMAGE_NT_SIGNATURE) {
|
||||
Print(L"Invalid PE header\n");
|
||||
printf("Invalid PE header\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (pe->OptionalHeader.Magic != IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
|
||||
Print(L"Not PE32+\n");
|
||||
printf("Not PE32+\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -307,7 +307,7 @@ void* LoadPeImage(void* buffer) {
|
||||
uint64_t imageSize = pe->OptionalHeader.SizeOfImage;
|
||||
uint64_t imageBase = (uint64_t)Malloc(imageSize);
|
||||
if (!imageBase) {
|
||||
Print(L"Failed to allocate image memory\n");
|
||||
printf("Failed to allocate image memory\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -338,7 +338,7 @@ void* LoadPeImage(void* buffer) {
|
||||
while (relocBase < relocEnd) {
|
||||
IMAGE_BASE_RELOCATION* rel = (IMAGE_BASE_RELOCATION*)relocBase;
|
||||
if (rel->SizeOfBlock < sizeof(IMAGE_BASE_RELOCATION)) {
|
||||
Print(L"Relocation block too small at %p\n", rel);
|
||||
printf("Relocation block too small at %p\n", rel);
|
||||
return NULL;
|
||||
}
|
||||
uint64_t entryCount = (rel->SizeOfBlock - sizeof(IMAGE_BASE_RELOCATION)) / sizeof(uint16_t);
|
||||
@@ -353,12 +353,12 @@ void* LoadPeImage(void* buffer) {
|
||||
uint64_t* patchAddr = (uint64_t*)(imageBase + rel->VirtualAddress + offset);
|
||||
// Check patchAddr is within image
|
||||
if ((uintptr_t)patchAddr < imageBase || (uintptr_t)patchAddr >= imageBase + imageSize) {
|
||||
Print(L"Relocation address out of bounds: %p\n", patchAddr);
|
||||
printf("Relocation address out of bounds: %p\n", patchAddr);
|
||||
return NULL;
|
||||
}
|
||||
*patchAddr += imageDelta;
|
||||
} else if (type != 0) {
|
||||
Print(L"Unknown relocation type %u at RVA %x\n", type, (unsigned)(rel->VirtualAddress + offset));
|
||||
printf("Unknown relocation type %u at RVA %x\n", type, (unsigned)(rel->VirtualAddress + offset));
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
@@ -383,7 +383,7 @@ void* LoadPeImage(void* buffer) {
|
||||
void* funcPtr = NULL;
|
||||
if (thunkINT->u1.Ordinal & IMAGE_ORDINAL_FLAG64) {
|
||||
// Import by ordinal - your ResolveImport doesn't support this
|
||||
Print(L"Import by ordinal is not supported.\n");
|
||||
printf("Import by ordinal is not supported.\n");
|
||||
return NULL;
|
||||
} else {
|
||||
// Import by name
|
||||
@@ -392,7 +392,7 @@ void* LoadPeImage(void* buffer) {
|
||||
funcPtr = ResolveImport(dllName, funcName);
|
||||
|
||||
if (!funcPtr) {
|
||||
Print(L"Failed to resolve import: %a -> %a\n", dllName, importByName->Name);
|
||||
printf("Failed to resolve import: %s -> %s\n", dllName, importByName->Name);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
+10
-10
@@ -10,18 +10,18 @@ typedef long LONG_PTR;
|
||||
typedef const char* LPCSTR;
|
||||
|
||||
// Window stubs
|
||||
HWND GetDesktopWindow() { PRINT(L"GetDesktopWindow called\n"); return (HWND)1; }
|
||||
HWND GetActiveWindow() { PRINT(L"GetActiveWindow called\n"); return (HWND)1; }
|
||||
HWND GetTopWindow(HWND) { PRINT(L"GetTopWindow called\n"); return (HWND)1; }
|
||||
HWND GetWindow(HWND, UINT) { PRINT(L"GetWindow called\n"); return (HWND)1; }
|
||||
LONG GetWindowLongA(HWND, int) { PRINT(L"GetWindowLongA called\n"); return 0; }
|
||||
LONG_PTR GetWindowLongPtrA(HWND, int) { PRINT(L"GetWindowLongPtrA called\n"); return 0; }
|
||||
void GetWindowThreadProcessId(HWND, DWORD*) { PRINT(L"GetWindowThreadProcessId called\n"); }
|
||||
HWND GetDesktopWindow() { printf("GetDesktopWindow called\n"); return (HWND)1; }
|
||||
HWND GetActiveWindow() { printf("GetActiveWindow called\n"); return (HWND)1; }
|
||||
HWND GetTopWindow(HWND) { printf("GetTopWindow called\n"); return (HWND)1; }
|
||||
HWND GetWindow(HWND, UINT) { printf("GetWindow called\n"); return (HWND)1; }
|
||||
LONG GetWindowLongA(HWND, int) { printf("GetWindowLongA called\n"); return 0; }
|
||||
LONG_PTR GetWindowLongPtrA(HWND, int) { printf("GetWindowLongPtrA called\n"); return 0; }
|
||||
void GetWindowThreadProcessId(HWND, DWORD*) { printf("GetWindowThreadProcessId called\n"); }
|
||||
|
||||
// MessageBox
|
||||
int MessageBoxA(HWND, LPCSTR text, LPCSTR caption, UINT) {
|
||||
PRINT(L"MessageBoxA called\n");
|
||||
PRINT(L"[MessageBox] %s: %s\n", caption, text);
|
||||
printf("MessageBoxA called\n");
|
||||
printf("[MessageBox] %s: %s\n", caption, text);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -46,6 +46,6 @@ void* ResolveUser32(const char* functionName) {
|
||||
return MessageBoxA;
|
||||
}
|
||||
|
||||
Print(L"Unknown function: %a\n", functionName);
|
||||
printf("Unknown function: %s\n", functionName);
|
||||
return NULL;
|
||||
}
|
||||
Reference in new issue
Block a user