RCM (Fusée Gelée) support, numerous UI updates and a lot of things for version 2.

This commit is contained in:
Dmitry Isaenko 2020-02-10 02:19:39 +03:00
parent 3d3fb56f9e
commit 010c33c593
36 changed files with 1572 additions and 92 deletions

View file

@ -0,0 +1,33 @@
# Compiler
CC=gcc
# Flags
CFLAGS=-O2
MKDIR_P = mkdir -p
APP_NAME = smashlib.so
all: x86 amd64
x86:
$(MKDIR_P) ./x86
$(CC) ${CFLAGS} -m32 -c -fPIC -I"${JAVA_HOME}/include" -I"${JAVA_HOME}/include/linux" smashlib.c -o smashlib_x86.o
$(CC) ${CFLAGS} -m32 -shared -fPIC -o ./x86/${APP_NAME} smashlib_x86.o -lc
amd64:
$(MKDIR_P) ./amd64
$(CC) ${CFLAGS} -m64 -c -fPIC -I"${JAVA_HOME}/include" -I"${JAVA_HOME}/include/linux" smashlib.c -o smashlib_amd64.o
$(CC) ${CFLAGS} -m64 -shared -fPIC -o ./amd64/${APP_NAME} smashlib_amd64.o -lc
clean:
rm -rf \
smashlib_amd64.o \
smashlib_x86.o \
./x86 \
./amd64
install: x86 amd64
install ./x86/${APP_NAME} ../../src/main/resources/native/linux/x86/
install ./amd64/${APP_NAME} ../../src/main/resources/native/linux/amd64/
uninstall:
rm ../../src/main/resources/native/linux/x86/${APP_NAME}
rm ../../src/main/resources/native/linux/amd64/${APP_NAME}

View file

@ -0,0 +1,29 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class nsusbloader_Utilities_RcmSmash */
#ifndef _Included_nsusbloader_Utilities_RcmSmash
#define _Included_nsusbloader_Utilities_RcmSmash
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: nsusbloader_Utilities_RcmSmash
* Method: smashLinux
* Signature: (II)I
*/
JNIEXPORT jint JNICALL Java_nsusbloader_Utilities_RcmSmash_smashLinux
(JNIEnv *, jclass, jint, jint);
/*
* Class: nsusbloader_Utilities_RcmSmash
* Method: smashWindows
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_nsusbloader_Utilities_RcmSmash_smashWindows
(JNIEnv *, jclass);
#ifdef __cplusplus
}
#endif
#endif

88
JNI sources/linux/smashlib.c Executable file
View file

@ -0,0 +1,88 @@
/* NS-USBloader - native libraries for 'special purposes'
* Copyright (C) 2020 Dmitry Isaenko
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/usbdevice_fs.h>
#include <linux/usb/ch9.h>
#include <errno.h>
#include "nsusbloader_Utilities_RcmSmash.h"
struct usbdevfs_urb urb;
JNIEXPORT jint JNICALL Java_nsusbloader_Utilities_RcmSmash_smashLinux
(JNIEnv * jni_env, jclass this_class, jint bus_id, jint device_addr){
int ret_value;
char *usb_path = (char*)malloc(24 * sizeof(char));
sprintf(usb_path, "/dev/bus/usb/%03d/%03d", bus_id, device_addr);
int fd = open(usb_path, O_RDWR);
if (fd == -1)
return -1;
struct usb_ctrlrequest* ctrl_req;
__u8* buf[0x7000+sizeof(ctrl_req)];
ctrl_req = (struct usb_ctrlrequest *) buf;
ctrl_req->bRequestType = 0x82;
ctrl_req->bRequest = USB_REQ_GET_STATUS;
ctrl_req->wValue = 0;
ctrl_req->wIndex = 0;
ctrl_req->wLength = 0x7000;
memset(&urb, 0, sizeof(urb));
urb.type = USBDEVFS_URB_TYPE_CONTROL;
urb.endpoint = USB_DIR_IN | 0;
urb.buffer = buf;
urb.buffer_length = sizeof(buf);
//Submit request
ret_value = ioctl(fd, USBDEVFS_SUBMITURB, &urb);
// If we failed on this step, it's a VERY bad sign. Nothing to do, let's report failure.
if (ret_value != 0)
return ret_value;
// Wait 1/4 sec
usleep(250000);
struct usbdevfs_urb urb1;
// Let's pick reply (everybody does it, right? In non-blocking manner.)
ret_value = ioctl(fd, USBDEVFS_REAPURBNDELAY, &urb1);
if (ret_value < 0){
if (errno == EAGAIN){ // In case of resource temporarily unavailable
// Wired.. so much time left. Let's cancel it!
ret_value = ioctl(fd, USBDEVFS_DISCARDURB, &urb);
// And wait a bit more..
usleep(40000);
// And try to pick reply. Yes, it's still possible. See /usr/src/linux/drivers/usb/core/devio.c
ret_value = ioctl(fd, USBDEVFS_REAPURBNDELAY, &urb1);
}
}
// Leftovers.
free(usb_path);
// Let's try to close device, but even if we fail with this, nvm.
close(fd); // So we won't even write returned value somewhere.
return 0;
}
JNIEXPORT jint JNICALL Java_nsusbloader_Utilities_RcmSmash_smashWindows
(JNIEnv * jni_env, jclass this_class){
return -1;
}

View file

@ -0,0 +1,34 @@
# Compiler
CC32='C:/MinGW/bin/gcc'
CC64='C:/Program Files/mingw-w64/x86_64-8.1.0-win32-seh-rt_v6-rev0/mingw64/bin/gcc'
# Flags
CFLAGS=-O2
MKDIR_P=mkdir
APP_NAME=smashlib.dll
all: x86 amd64
#$(CC) ${CFLAGS} -m32 -c -fPIC -I "C:/MinGW/include/ddk" -I "${JAVA_HOME}/include" -I "${JAVA_HOME}/include/win32" smashlib.c -o ./x86/smashlib.o # MinGw-32 version
x86:
$(MKDIR_P) ./x86
export PATH="C/MinGW/bin/:${PATH}"
$(CC32) ${CFLAGS} -m32 -c -fPIC -I "C:/MinGW/include/ddk" -I "${JAVA_HOME}/include" -I "${JAVA_HOME}/include/win32" smashlib.c -o ./smashlib_x86.o
$(CC32) ${CFLAGS} -shared -o ./x86/${APP_NAME} ./smashlib_x86.o -lsetupapi -lhid -Wl,--add-stdcall-alias
#$(CC) ${CFLAGS} -m64 -c -fPIC -I "C:/MinGW/include/ddk" -I "${JAVA_HOME}/include" -I "${JAVA_HOME}/include/win32" smashlib.c -o ./amd64/smashlib.o # MinGw-32 version
amd64:
$(MKDIR_P) ./amd64
export PATH="C/Program Files/mingw-w64/x86_64-8.1.0-win32-seh-rt_v6-rev0/mingw64/bin/:${PATH}"
$(CC64) ${CFLAGS} -m64 -c -fPIC -I "${JAVA_HOME}/include" -I "${JAVA_HOME}/include/win32" smashlib.c -o ./smashlib_amd64.o
$(CC64) ${CFLAGS} -shared -o ./amd64/${APP_NAME} ./smashlib_amd64.o -lsetupapi -lhid -Wl,--add-stdcall-alias
clean:
rm -rf ./smashlib_x86.o ./smashlib_amd64.o ./x86 ./amd64
install: x86 amd64
install ./x86/${APP_NAME} ../../src/main/resources/native/windows/x86/
install ./amd64/${APP_NAME} ../../src/main/resources/native/windows/amd64/
uninstall:
rm ../../src/main/resources/native/windows/x86/${APP_NAME}
rm ../../src/main/resources/native/windows/amd64/${APP_NAME}

View file

@ -0,0 +1,29 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class nsusbloader_Utilities_RcmSmash */
#ifndef _Included_nsusbloader_Utilities_RcmSmash
#define _Included_nsusbloader_Utilities_RcmSmash
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: nsusbloader_Utilities_RcmSmash
* Method: smashLinux
* Signature: (II)I
*/
JNIEXPORT jint JNICALL Java_nsusbloader_Utilities_RcmSmash_smashLinux
(JNIEnv *, jclass, jint, jint);
/*
* Class: nsusbloader_Utilities_RcmSmash
* Method: smashWindows
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_nsusbloader_Utilities_RcmSmash_smashWindows
(JNIEnv *, jclass);
#ifdef __cplusplus
}
#endif
#endif

View file

@ -0,0 +1,168 @@
/*
* Derivative & modified code based on awesome example from here: https://www.velleman.eu/images/tmp/usbfind.c
* And MSDN documentation :)
*
* return
* -2 device not connected
* -1 Unable to open handler
* 0 maybe we're all set
*/
#ifdef __cplusplus
extern "C" {
#endif
#define _DEBUG
#define _BUILD_FOR_X86
#include <windows.h>
#include <tchar.h>
#include <setupapi.h>
#ifdef DEBUG
#include <stdio.h>
#endif
#ifdef BUILD_FOR_X86
#include <ntddser.h>
#endif
#include "nsusbloader_Utilities_RcmSmash.h"
#define LIBUSB_IOCTL_GET_STATUS CTL_CODE(FILE_DEVICE_UNKNOWN, 0x807, METHOD_BUFFERED, FILE_ANY_ACCESS)
static GUID GUID_DEVINTERFACE_USB_DEVICE = {0xA5DCBF10L, 0x6530, 0x11D2, {0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED}};
// NOTE: CHANGE TO NV DEVICE!
//static TCHAR VID_PID_PAIR[] = _T("vid_1a86&pid_7523"); // UNCOMMENT FOR TESTS
static TCHAR VID_PID_PAIR[] = _T("vid_0955&pid_7321"); // UNCOMMENT ON RELEASE
typedef struct
{
unsigned int timeout;
unsigned int recipient;
unsigned int index;
unsigned int status;
unsigned int ___zeroes_hold_0; // made it for better understanding. Literally useless.
unsigned int ___zeroes_hold_1; // Just consider each int as 4 bytes and calculate size =)
} simple_status_req;
int win32_magic(LPCSTR lpFileName){
unsigned char reqBuf[24] = {0};
simple_status_req* request;
request = (simple_status_req *) &reqBuf;
request->timeout = 1000;
request->recipient = 0x02;
request->index = 0;
request->status = 0;
#ifdef DEBUG
printf("Device path: %s\nStatus: %x\nIn buffer size: %d\nIn buffer content: ",
lpFileName,
LIBUSB_IOCTL_GET_STATUS,
sizeof(reqBuf) ); // Path and what is our IOCTL request looks like
for (int i = 0; i < sizeof(reqBuf); i++)
printf("%x ", reqBuf[i]);
printf("\n");
#endif
unsigned char outBuffer[28672];
OVERLAPPED ovrlpd;
memset(&ovrlpd, 0, sizeof(ovrlpd));
// Fucking finally let's open this
HANDLE handler = CreateFile(
lpFileName,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
NULL
);
if ( handler == INVALID_HANDLE_VALUE )
return -1;
BOOL ret_val = DeviceIoControl(
handler,
LIBUSB_IOCTL_GET_STATUS,
(LPVOID) &reqBuf,
24,
(LPVOID) &outBuffer,
28672,
NULL,
&ovrlpd
);
#ifdef DEBUG
printf("\nDeviceIoControl reports: %d\nLast Error Code: %d\n", ret_val, GetLastError());
#endif
Sleep(250);
DWORD bReceived = 0;
ret_val = GetOverlappedResult(handler, &ovrlpd, &bReceived, FALSE);
#ifdef DEBUG
if (! ret_val) {
// we won't report any issues since there is no workaround.
printf("\nLast Error Code: %d\n\n", GetLastError());
}
#endif
CloseHandle(handler);
return 0;
}
JNIEXPORT jint JNICALL Java_nsusbloader_Utilities_RcmSmash_smashWindows
(JNIEnv * jnie_enb, jclass this_class) {
int found = 0;
int ret_val = -2;
HDEVINFO hDevInfo;
SP_DEVICE_INTERFACE_DATA DevIntfData;
PSP_DEVICE_INTERFACE_DETAIL_DATA DevIntfDetailData;
SP_DEVINFO_DATA DevData;
DWORD dwSize, dwType, dwMemberIdx;
HKEY hKey;
BYTE lpData[1024];
hDevInfo = SetupDiGetClassDevs(&GUID_DEVINTERFACE_USB_DEVICE, NULL, 0, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);
if (hDevInfo != INVALID_HANDLE_VALUE){
DevIntfData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
dwMemberIdx = 0;
SetupDiEnumDeviceInterfaces(hDevInfo, NULL, &GUID_DEVINTERFACE_USB_DEVICE, dwMemberIdx, &DevIntfData);
while(GetLastError() != ERROR_NO_MORE_ITEMS) {
DevData.cbSize = sizeof(DevData);
SetupDiGetDeviceInterfaceDetail(hDevInfo, &DevIntfData, NULL, 0, &dwSize, NULL);
DevIntfDetailData = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwSize);
DevIntfDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
if (SetupDiGetDeviceInterfaceDetail(hDevInfo, &DevIntfData, DevIntfDetailData, dwSize, &dwSize, &DevData)) {
if (NULL != _tcsstr((TCHAR*)DevIntfDetailData->DevicePath, VID_PID_PAIR)) {
found = 1;
ret_val = win32_magic(DevIntfDetailData->DevicePath);
}
}
HeapFree(GetProcessHeap(), 0, DevIntfDetailData);
if (found)
break;
// Continue looping
SetupDiEnumDeviceInterfaces(hDevInfo, NULL, &GUID_DEVINTERFACE_USB_DEVICE, ++dwMemberIdx, &DevIntfData);
}
SetupDiDestroyDeviceInfoList(hDevInfo);
}
#ifdef DEBUG
printf("Returning value: %d\n", ret_val);
#endif
return ret_val;
}
JNIEXPORT jint JNICALL Java_nsusbloader_Utilities_RcmSmash_smashLinux
(JNIEnv * jnie_env, jclass this_class, jint bus_id, jint device_addr){
return -1;
}