brainlilo/BrainLILO.cpp

195 lines
4.7 KiB
C++
Raw Permalink Normal View History

2020-09-28 03:19:37 +09:00
/*
* BrainLILO
* U-Boot loader for electric dictionary.
2022-01-24 01:52:45 +09:00
*
2020-09-28 03:19:37 +09:00
* Copyright (C) 2019 C. Shirasaka <holly_programmer@outlook.com>
* based on
** ResetKitHelper
** Soft/hard reset the electronic dictionary.
2022-01-24 01:52:45 +09:00
**
2020-09-28 03:19:37 +09:00
** Copyright (C) 2012 T. Kawada <tcppjp [ at ] gmail.com>
*
* This file is licensed in MIT license.
*
2022-01-24 01:52:45 +09:00
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the "Software"),
2020-09-28 03:19:37 +09:00
* to deal in the Software without restriction, including without limitation the
2022-01-24 01:52:45 +09:00
* 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
2020-09-28 03:19:37 +09:00
* furnished to do so, subject to the following conditions:
*
2022-01-24 01:52:45 +09:00
* The above copyright notice and this permission notice shall be included in
2020-09-28 03:19:37 +09:00
* all copies or substantial portions of the Software.
*
2022-01-24 01:52:45 +09:00
* 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
2020-09-28 03:19:37 +09:00
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2022-01-24 01:52:45 +09:00
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2020-09-28 03:19:37 +09:00
* THE SOFTWARE.
*
*/
#define BRAINLILO_API extern "C" __declspec(dllexport)
#include "BrainLILO.h"
#include "BrainLILODrv.h"
#include <string.h>
2022-01-24 01:52:45 +09:00
static bool g_initialized = false;
static HANDLE g_helperHandle = NULL;
2020-09-28 03:19:37 +09:00
static HINSTANCE g_hInstance;
2022-01-24 01:52:45 +09:00
static void initialize()
{
if (g_initialized)
2020-09-28 03:19:37 +09:00
return;
2022-01-24 01:52:45 +09:00
g_initialized = true;
2020-09-28 03:19:37 +09:00
}
2022-01-24 01:52:45 +09:00
static void getThisDllDirectoryPath(LPWSTR buffer)
{
// retrive the path of the application.
GetModuleFileName(g_hInstance, buffer, 512);
const size_t notFound = (size_t)-1;
size_t i = 0;
size_t j = notFound;
while (buffer[i])
{
if (buffer[i] == L'/' || buffer[i] == L'\\')
{
j = i;
2020-09-28 03:19:37 +09:00
}
i++;
}
2022-01-24 01:52:45 +09:00
if (j == notFound)
2020-09-28 03:19:37 +09:00
return;
2022-01-24 01:52:45 +09:00
buffer[j] = 0;
2020-09-28 03:19:37 +09:00
}
2022-01-24 01:52:45 +09:00
static bool isDriverLoaded()
{
2020-09-28 03:19:37 +09:00
HANDLE handle;
2022-01-24 01:52:45 +09:00
handle =
CreateFile(L"LIN0:", GENERIC_READ | GENERIC_WRITE | FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING, 0, NULL);
if (handle == INVALID_HANDLE_VALUE)
handle = NULL;
if (handle)
{
2020-09-28 03:19:37 +09:00
CloseHandle(handle);
return true;
}
return false;
}
2022-01-24 01:52:45 +09:00
static void loadDriverIfNeeded()
{
if (isDriverLoaded())
2020-09-28 03:19:37 +09:00
return;
2022-01-24 01:52:45 +09:00
OutputDebugString(L"BrainLILO: installing LILODriver...");
DWORD err;
HANDLE driver = NULL;
2020-09-28 03:19:37 +09:00
wchar_t driverPath[1024];
2022-01-24 01:52:45 +09:00
getThisDllDirectoryPath(driverPath);
TCHAR newDriverPath[128];
getThisDllDirectoryPath(driverPath);
wcscat(driverPath, L"\\BrainLILODrv.dll");
2022-01-24 01:52:45 +09:00
wcscpy(newDriverPath, L"\\Windows\\BrainLILODrv.dll");
2020-09-28 03:19:37 +09:00
{
wchar_t buf[1024];
2022-01-24 01:52:45 +09:00
swprintf(buf, L"BrainLILO: copying \"%ls\" to \"%ls\"", driverPath, newDriverPath);
2020-09-28 03:19:37 +09:00
OutputDebugString(buf);
}
2022-01-24 01:52:45 +09:00
if (!CopyFile(driverPath, newDriverPath, FALSE))
{
if (GetFileAttributes(newDriverPath) == (DWORD)-1)
{
OutputDebugString(L"BrainLILO: failed to copy");
return;
}
}
OutputDebugString(L"BrainLILO: registering BrainLILODriver...");
try
{
driver = RegisterDevice(L"LIN", 0, L"\\Windows\\BrainLILODrv.dll", 0);
if (driver == INVALID_HANDLE_VALUE)
driver = NULL;
if (!driver)
{
driver = RegisterDevice(L"LIN", 0, L"BrainLILODrv.dll", 0);
if (driver == INVALID_HANDLE_VALUE)
driver = NULL;
}
err = GetLastError();
}
catch (DWORD e)
{
err = e;
}
if (!driver && (err != 0x964))
{
OutputDebugString(L"BrainLILO: failed to install...");
return;
}
2020-09-28 03:19:37 +09:00
}
2022-01-24 01:52:45 +09:00
static void openDriver()
{
if (g_helperHandle)
2020-09-28 03:19:37 +09:00
return;
2022-01-24 01:52:45 +09:00
g_helperHandle =
CreateFile(L"LIN0:", GENERIC_READ | GENERIC_WRITE | FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING, 0, NULL);
if (g_helperHandle == INVALID_HANDLE_VALUE)
g_helperHandle = NULL;
2020-09-28 03:19:37 +09:00
}
2022-01-24 01:52:45 +09:00
extern "C" BOOL APIENTRY DllMain(HINSTANCE hInstance, DWORD ul_reason_for_call, LPVOID lpReserved)
2020-09-28 03:19:37 +09:00
{
switch (ul_reason_for_call)
2022-01-24 01:52:45 +09:00
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
g_hInstance = hInstance;
initialize();
loadDriverIfNeeded();
openDriver();
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
2020-09-28 03:19:37 +09:00
}
return TRUE;
}
2022-01-24 01:52:45 +09:00
BRAINLILO_API BOOL RKDoSoftReset()
{
initialize();
2020-09-28 03:19:37 +09:00
loadDriverIfNeeded();
openDriver();
2022-01-24 01:52:45 +09:00
return DeviceIoControl(g_helperHandle, IOCTL_LIN_DO_LINUX, NULL, 0, NULL, 0, NULL, NULL);
2020-09-28 03:19:37 +09:00
}