diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..4d84ce4 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,4 @@ +[*.{c,cpp,h}] +indent_style = space +indent_size = 4 +tab_width = 4 diff --git a/BrainLILODrv.cpp b/BrainLILODrv.cpp index e7caac4..028aa0e 100644 --- a/BrainLILODrv.cpp +++ b/BrainLILODrv.cpp @@ -30,7 +30,10 @@ * THE SOFTWARE. * */ +#include +#include #include +#include #include #define FSNOTIFY_POWER_OFF 1 @@ -157,35 +160,76 @@ __attribute__((noreturn)) static DWORD EDNA2_callKernelEntryPoint() EDNA2_runPhysicalInvoker(); } +static void ShowMessage(std::string msg, std::string title, UINT typ) +{ + void *bufMsg; + void *bufTitle; + bufMsg = LocalAlloc(LPTR, msg.length() * sizeof(wchar_t)); + bufTitle = LocalAlloc(LPTR, title.length() * sizeof(wchar_t)); + mbstowcs((wchar_t *)bufMsg, msg.c_str(), msg.length()); + mbstowcs((wchar_t *)bufTitle, title.c_str(), title.length()); + MessageBox(NULL, (wchar_t *)bufMsg, (wchar_t *)bufTitle, typ); + LocalFree(bufMsg); + LocalFree(bufTitle); +} + static bool doLinux() { - TCHAR bootloaderFileName[128] = TEXT("\\Storage Card\\loader\\u-boot.bin"); - HANDLE hFile; - wchar_t buf[256]; + wchar_t wcBuf[256] = {}; + + std::ifstream iVersion; + std::string line, model; + std::regex modelRe("[A-Z]{2}-[A-Z0-9]+"); + std::smatch match; + + std::string fn("\\Storage Card\\loader\\"); + HANDLE hUBoot; DWORD wReadSize; - OutputDebugString(L"BrainLILO: Opening Bootloader file..."); - hFile = CreateFile(bootloaderFileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); - if (hFile == INVALID_HANDLE_VALUE) + iVersion.open("\\NAND\\version.txt"); + while (getline(iVersion, line)) { - OutputDebugString(L"Cant open bootloader"); + if (regex_search(line, match, modelRe)) + { + model = match[0].str(); + break; + } + } + + if (model.length() == 0) + { + ShowMessage("Failed to match the model name", "BrainLILO", MB_ICONWARNING); return false; } - swprintf(buf, L"BrainLILO: Bootloader file handle 0x%08x\n", (int)(hFile)); - OutputDebugString(buf); - FileSize = GetFileSize(hFile, NULL); - swprintf(buf, L"BrainLILO: Bootloader file size %d Byte\n", FileSize); - OutputDebugString(buf); + OutputDebugString(L"BrainLILO: Opening Bootloader file..."); + fn += model + ".bin"; + + mbstowcs(wcBuf, fn.c_str(), fn.length()); + hUBoot = CreateFile(wcBuf, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + if (hUBoot == INVALID_HANDLE_VALUE) + { + OutputDebugString(L"Could not open the bootloader"); + ShowMessage("Could not open the bootloader: " + fn, "BrainLILO", MB_ICONWARNING); + return false; + } + + swprintf(wcBuf, L"BrainLILO: Bootloader file handle 0x%08x\n", (int)(hUBoot)); + OutputDebugString(wcBuf); + + FileSize = GetFileSize(hUBoot, NULL); + swprintf(wcBuf, L"BrainLILO: Bootloader file size %d Byte\n", FileSize); + OutputDebugString(wcBuf); OutputDebugString(L"BrainLILO: Preloading bootloader to 0xa0000000..."); - if (!ReadFile(hFile, (void *)0xa0000000, FileSize, &wReadSize, NULL)) + if (!ReadFile(hUBoot, (void *)0xa0000000, FileSize, &wReadSize, NULL)) { - OutputDebugString(L"Cant read bootloader"); + OutputDebugString(L"Could not read the bootloader"); + ShowMessage("Could not read the bootloader", "BrainLILO", MB_ICONWARNING); return false; } OutputDebugString(L"BrainLILO: Bootloader copied! Closing file handle..."); - CloseHandle(hFile); + CloseHandle(hUBoot); OutputDebugString(L"BrainLILO: Notifying power off to filesystems..."); if (FileSystemPowerFunction) diff --git a/Makefile b/Makefile index 91b7fb9..17395f2 100644 --- a/Makefile +++ b/Makefile @@ -28,8 +28,8 @@ DLLFLAGS=-DEV_PLATFORM_WIN32 -DUNICODE -D_UNICODE -DEV_UNSAFE_SWPRINTF -mwin32 \ DRVFLAGS= -DEV_PLATFORM_WIN32 -DUNICODE -D_UNICODE -DEV_UNSAFE_SWPRINTF -mwin32 \ -O0 -mcpu=arm926ej-s -D_WIN32_WCE=0x600 -D_LARGEFILE_SOURCE=1 -D_LARGEFILE64_SOURCE=1 \ -D_FILE_OFFSET_BITS=64 -DNDEBUG -Wall -static \ - -Wl,--image-base,0x100000 \ - -nostdlib -lcoredll -shared + -Wl,--image-base,0x100000,--allow-multiple-definition \ + -lcoredll -shared .PHONY: all clean diff --git a/bootloader.cpp b/bootloader.cpp index f80269f..845a91f 100644 --- a/bootloader.cpp +++ b/bootloader.cpp @@ -1,59 +1,12 @@ // This file is in public domain. -#include -#include -#include #include -#define MainWindowClassName L"SelectorMainWindowClass" - -#define DataTypeAlertError 0x1000 -#define DataTypeAlertInformation 0x1001 -#define DataTypeAlertWarning 0x1002 - -static void showAlertWarning(LPCWSTR message, LPCWSTR title) -{ - - HWND selectorMainWindow = FindWindow(MainWindowClassName, NULL); - - if (!selectorMainWindow) - { - MessageBox(NULL, message, title, MB_ICONWARNING); - return; - } - - wchar_t data[2048]; - wcscpy(data, message); - data[wcslen(message)] = 0; - wcscpy(data + wcslen(message) + 1, title); - - size_t dataLen = wcslen(message) + wcslen(title) + 1; - - COPYDATASTRUCT info; - info.dwData = DataTypeAlertWarning; - info.cbData = dataLen * sizeof(wchar_t); - - HGLOBAL global = GlobalAlloc(GPTR, info.cbData); - memcpy((LPVOID)global, data, info.cbData); - - info.lpData = (LPVOID)global; - - SendMessage(selectorMainWindow, WM_COPYDATA, NULL, (LPARAM)&info); - - GlobalFree(global); -} - int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPTSTR lpCmd, int nShow) { HINSTANCE lib = LoadLibrary(L"BrainLILO"); if (!lib) { - wchar_t buf[256]; - swprintf(buf, - L"Cannot perform a soft reset.\n" - L"BrainLILO was not loaded (0x%08x).", - GetLastError()); - showAlertWarning(buf, L"Error"); return 1; } @@ -61,23 +14,11 @@ int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPTSTR lpCmd, int nShow) RKDoSoftResetProc RKDoSoftReset = (RKDoSoftResetProc)GetProcAddress(lib, L"RKDoSoftReset"); if (!RKDoSoftReset) { - wchar_t buf[256]; - swprintf(buf, - L"Cannot perform a soft reset.\n" - L"RKDoSoftReset not found (0x%08x).", - GetLastError()); - showAlertWarning(buf, L"Error"); return 1; } if (!RKDoSoftReset()) { - wchar_t buf[256]; - swprintf(buf, - L"Cannot perform a soft reset.\n" - L"Operation failed (0x%08x).", - GetLastError()); - showAlertWarning(buf, L"Error"); return 1; } return 0;