Read the internal model name and load appropriate binary

This commit is contained in:
Takumi Sueda 2022-04-19 01:20:09 +09:00
parent 64868531ed
commit 0d0261e5f9
2 changed files with 67 additions and 17 deletions

View File

@ -30,7 +30,10 @@
* THE SOFTWARE. * THE SOFTWARE.
* *
*/ */
#include <fstream>
#include <regex>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h>
#include <windows.h> #include <windows.h>
#define FSNOTIFY_POWER_OFF 1 #define FSNOTIFY_POWER_OFF 1
@ -157,35 +160,82 @@ __attribute__((noreturn)) static DWORD EDNA2_callKernelEntryPoint()
EDNA2_runPhysicalInvoker(); 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() static bool doLinux()
{ {
TCHAR bootloaderFileName[128] = TEXT("\\Storage Card\\loader\\u-boot.bin"); wchar_t wcbuf[256];
HANDLE hFile; int i;
wchar_t buf[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; DWORD wReadSize;
OutputDebugString(L"BrainLILO: Opening Bootloader file..."); for (i = 0; i < int(sizeof(wcbuf) / sizeof(wcbuf[0])); i++)
hFile = CreateFile(bootloaderFileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{ {
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; return false;
} }
swprintf(buf, L"BrainLILO: Bootloader file handle 0x%08x\n", (int)(hFile));
OutputDebugString(buf);
FileSize = GetFileSize(hFile, NULL); OutputDebugString(L"BrainLILO: Opening Bootloader file...");
swprintf(buf, L"BrainLILO: Bootloader file size %d Byte\n", FileSize); fn += model + ".bin";
OutputDebugString(buf);
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..."); 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; return false;
} }
OutputDebugString(L"BrainLILO: Bootloader copied! Closing file handle..."); OutputDebugString(L"BrainLILO: Bootloader copied! Closing file handle...");
CloseHandle(hFile); CloseHandle(hUBoot);
OutputDebugString(L"BrainLILO: Notifying power off to filesystems..."); OutputDebugString(L"BrainLILO: Notifying power off to filesystems...");
if (FileSystemPowerFunction) if (FileSystemPowerFunction)

View File

@ -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 \ 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 \ -O0 -mcpu=arm926ej-s -D_WIN32_WCE=0x600 -D_LARGEFILE_SOURCE=1 -D_LARGEFILE64_SOURCE=1 \
-D_FILE_OFFSET_BITS=64 -DNDEBUG -Wall -static \ -D_FILE_OFFSET_BITS=64 -DNDEBUG -Wall -static \
-Wl,--image-base,0x100000 \ -Wl,--image-base,0x100000,--allow-multiple-definition \
-nostdlib -lcoredll -shared -lcoredll -shared
.PHONY: all clean .PHONY: all clean