From 0d0261e5f9cbed5a47be431f2ecd9f8b013ebaeb Mon Sep 17 00:00:00 2001 From: Takumi Sueda Date: Tue, 19 Apr 2022 01:20:09 +0900 Subject: [PATCH] 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