From 64868531edfc6d492a8318b3f4881e94522e59b3 Mon Sep 17 00:00:00 2001 From: Takumi Sueda Date: Tue, 19 Apr 2022 01:19:31 +0900 Subject: [PATCH 1/7] Add editorconfig --- .editorconfig | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 .editorconfig 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 From 0d0261e5f9cbed5a47be431f2ecd9f8b013ebaeb Mon Sep 17 00:00:00 2001 From: Takumi Sueda Date: Tue, 19 Apr 2022 01:20:09 +0900 Subject: [PATCH 2/7] Read the internal model name and load appropriate binary --- BrainLILODrv.cpp | 80 +++++++++++++++++++++++++++++++++++++++--------- Makefile | 4 +-- 2 files changed, 67 insertions(+), 17 deletions(-) diff --git a/BrainLILODrv.cpp b/BrainLILODrv.cpp index e7caac4..de6eafc 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,82 @@ __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]; + int i; + + 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) + for (i = 0; i < int(sizeof(wcbuf) / sizeof(wcbuf[0])); i++) { - OutputDebugString(L"Cant open bootloader"); + wcbuf[i] = '\0'; + } + + iVersion.open("\\NAND\\version.txt"); + while (getline(iVersion, line)) + { + 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(std::string("Could not open the bootloader: ") + fn, std::string("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(std::string("Could not read the bootloader"), std::string("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 From a128053742febd808d69763bccd5d266586810b8 Mon Sep 17 00:00:00 2001 From: Takumi Sueda Date: Tue, 19 Apr 2022 01:22:15 +0900 Subject: [PATCH 3/7] Don't call MessageBox in WinMain to prevent freezing --- bootloader.cpp | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/bootloader.cpp b/bootloader.cpp index f80269f..6c24ca7 100644 --- a/bootloader.cpp +++ b/bootloader.cpp @@ -48,12 +48,6 @@ 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 +55,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; From a7f9d3126bb2e52b4d7eef1efe8eae46925c60fe Mon Sep 17 00:00:00 2001 From: Takumi Sueda Date: Tue, 19 Apr 2022 01:24:24 +0900 Subject: [PATCH 4/7] Remove unused functions and headers --- bootloader.cpp | 41 ----------------------------------------- 1 file changed, 41 deletions(-) diff --git a/bootloader.cpp b/bootloader.cpp index 6c24ca7..845a91f 100644 --- a/bootloader.cpp +++ b/bootloader.cpp @@ -1,48 +1,7 @@ // 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"); From 78554fba0b6a60f005d0a13e00cdbc59f941ef14 Mon Sep 17 00:00:00 2001 From: Takumi Sueda Date: Tue, 19 Apr 2022 03:20:33 +0900 Subject: [PATCH 5/7] Initialize wcbuf with an empty initializer --- BrainLILODrv.cpp | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/BrainLILODrv.cpp b/BrainLILODrv.cpp index de6eafc..e8ac0fc 100644 --- a/BrainLILODrv.cpp +++ b/BrainLILODrv.cpp @@ -175,8 +175,7 @@ static void ShowMessage(std::string msg, std::string title, UINT typ) static bool doLinux() { - wchar_t wcbuf[256]; - int i; + wchar_t wcbuf[256] = {}; std::ifstream iVersion; std::string line, model; @@ -187,11 +186,6 @@ static bool doLinux() HANDLE hUBoot; DWORD wReadSize; - for (i = 0; i < int(sizeof(wcbuf) / sizeof(wcbuf[0])); i++) - { - wcbuf[i] = '\0'; - } - iVersion.open("\\NAND\\version.txt"); while (getline(iVersion, line)) { From dc1bb3a88d3c75a1dc73d103c81a9e7725e3b94e Mon Sep 17 00:00:00 2001 From: Takumi Sueda Date: Tue, 19 Apr 2022 03:21:52 +0900 Subject: [PATCH 6/7] Pass string literals to ShowMessage --- BrainLILODrv.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/BrainLILODrv.cpp b/BrainLILODrv.cpp index e8ac0fc..1265903 100644 --- a/BrainLILODrv.cpp +++ b/BrainLILODrv.cpp @@ -210,7 +210,7 @@ static bool doLinux() if (hUBoot == INVALID_HANDLE_VALUE) { OutputDebugString(L"Could not open the bootloader"); - ShowMessage(std::string("Could not open the bootloader: ") + fn, std::string("BrainLILO"), MB_ICONWARNING); + ShowMessage("Could not open the bootloader: " + fn, "BrainLILO", MB_ICONWARNING); return false; } @@ -225,7 +225,7 @@ static bool doLinux() if (!ReadFile(hUBoot, (void *)0xa0000000, FileSize, &wReadSize, NULL)) { OutputDebugString(L"Could not read the bootloader"); - ShowMessage(std::string("Could not read the bootloader"), std::string("BrainLILO"), MB_ICONWARNING); + ShowMessage("Could not read the bootloader", "BrainLILO", MB_ICONWARNING); return false; } OutputDebugString(L"BrainLILO: Bootloader copied! Closing file handle..."); From d1cb75e2f45a318e6d2ba2f78032809d9790ec9e Mon Sep 17 00:00:00 2001 From: Takumi Sueda Date: Tue, 19 Apr 2022 03:24:54 +0900 Subject: [PATCH 7/7] Obey the naming convention --- BrainLILODrv.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/BrainLILODrv.cpp b/BrainLILODrv.cpp index 1265903..028aa0e 100644 --- a/BrainLILODrv.cpp +++ b/BrainLILODrv.cpp @@ -175,7 +175,7 @@ static void ShowMessage(std::string msg, std::string title, UINT typ) static bool doLinux() { - wchar_t wcbuf[256] = {}; + wchar_t wcBuf[256] = {}; std::ifstream iVersion; std::string line, model; @@ -205,8 +205,8 @@ static bool doLinux() 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); + 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"); @@ -214,12 +214,12 @@ static bool doLinux() return false; } - swprintf(wcbuf, L"BrainLILO: Bootloader file handle 0x%08x\n", (int)(hUBoot)); - OutputDebugString(wcbuf); + 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); + swprintf(wcBuf, L"BrainLILO: Bootloader file size %d Byte\n", FileSize); + OutputDebugString(wcBuf); OutputDebugString(L"BrainLILO: Preloading bootloader to 0xa0000000..."); if (!ReadFile(hUBoot, (void *)0xa0000000, FileSize, &wReadSize, NULL))