1
0
Fork 0
mirror of https://github.com/eliboa/TegraRcmGUI.git synced 2024-11-28 13:12:05 +00:00
TegraRcmGUI/kourou/libs/rcm_device.h

187 lines
5.7 KiB
C
Raw Normal View History

2020-06-24 19:31:54 +01:00
/*
* Copyright (c) 2020 eliboa
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope 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/>.
*/
2020-06-24 19:30:39 +01:00
#ifndef RCMDEVICE_H
#define RCMDEVICE_H
#include <windows.h>
#include <setupapi.h>
#include <stdio.h>
#include <tchar.h>
2020-06-24 19:30:39 +01:00
#include "libusbk_int.h"
#include <string>
2020-06-24 19:31:54 +01:00
#include "../../types.h"
2020-06-24 19:30:39 +01:00
#define PACKET_SIZE 0x1000
2020-06-24 19:31:54 +01:00
#define PAYLOAD_MAX_SIZE 0x1ED58
2020-06-24 19:30:39 +01:00
static int RCM_VID = 0x0955;
static int RCM_PID = 0x7321;
2020-06-24 19:31:54 +01:00
static UCHAR READ_PIPE_ID = 0x81;
static UCHAR WRITE_PIPE_ID = 0x01;
2020-06-24 19:30:39 +01:00
2020-06-24 19:31:54 +01:00
typedef enum _RESULT : DWORD
2020-06-24 19:30:39 +01:00
{
SUCCESS = 0x000,
LIBUSBK_WRONG_DRIVER = 0x001,
DEVICE_NOT_FOUND = 0x002,
DEVICE_HANDLE_FAILED = 0x003,
MISSING_LIBUSBK_DRIVER = 0x004,
OPEN_FILE_FAILED = 0x005,
PAYLOAD_TOO_LARGE = 0x006,
USB_WRITE_FAILED = 0x007,
SW_HIGHBUFF_FAILED = 0x008,
STACK_SMASH_FAILED = 0x009,
2020-06-24 19:31:54 +01:00
DEVICEID_READ_FAILED = 0x00A,
DEVICE_NOT_READY = 0x00B,
DEVICE_NOT_SET = 0x00C,
WRONG_DEVICE_VID_PID = 0x00D,
DEVICE_DISCONNECTED = 0x00E
2020-06-24 19:30:39 +01:00
} RRESULT;
2020-06-24 19:31:54 +01:00
typedef enum _DEVICE_STATUS {
DISCONNECTED,
CONNECTED
} DEVICE_STATUS;
2020-06-24 19:30:39 +01:00
class RcmDevice
{
public:
2020-06-24 19:31:54 +01:00
//! Rcm device constructor
2020-06-24 19:30:39 +01:00
RcmDevice();
2020-06-24 19:31:54 +01:00
~RcmDevice();
////////////////////////////////////////////
///// PUBLIC METHODS /////
////////////////////////////////////////////
2020-06-24 19:30:39 +01:00
/*!
* \brief Initialize a RCM device. This function must be called at least on time before any other method call
* \param[in] deviceInfo
* Pointer to a KLST_DEVINFO struct. If not provided, the system will try to detect a plugged RCM device (a.k.a Nintendo Switch in RCM mode)
*
* \returns
* - on success, true.
2020-06-24 19:31:54 +01:00
* - on failure, false (use GetLastError() to get error code)
2020-06-24 19:30:39 +01:00
*/
bool initDevice(KLST_DEVINFO_HANDLE deviceInfo = nullptr);
/*!
* \brief Constructs a RCM payload and smash the device stack (fusée gelée exploit)
* \param payload_path
* Path to the user payload file to build final payload with
* \returns
* - on success, SUCCESS (0)
* - on failure, see enum RRESULT
*/
RRESULT hack(const char* payload_path);
2020-06-24 19:31:54 +01:00
/*!
* \brief Constructs a RCM payload and smash the device stack (fusée gelée exploit)
* \param payload_buff
* Pointer to the payload data
* \param buff_size
* Payload size
* \returns
* - on success, SUCCESS (0)
* - on failure, see enum RRESULT
*/
RRESULT hack(u8 *payload_buff, u32 buff_size);
2020-06-24 19:30:39 +01:00
/*!
* \brief Read a buffer from USB pipe
* \param buffer
* \param bufferSize
* \returns
* - on success, number of bytes read from pipe
* - on failure, error code < 0
*/
int read(u8* buffer, size_t bufferSize);
/*!
* \brief Write a buffer to USB pipe
* \param buffer
* \param bufferSize
* \returns
* - on success, number of bytes written to pipe
* - on failure, error code < 0
*/
int write(const u8* buffer, size_t bufferSize);
/*!
2020-06-24 19:31:54 +01:00
* \brief Check whether the device is ready to receive RCM commands
2020-06-24 19:30:39 +01:00
* \return true if device is ready
*/
2020-06-24 19:31:54 +01:00
bool deviceIsReady();
//! Get the RCM device status (CONNECTED / DISCONNECTED)
DEVICE_STATUS getStatus() { return m_devStatus; }
//! Set the RCM device status to DISCONNECTED
void disconnect() { m_devStatus = DISCONNECTED; }
// API Getters
bool flushPipe(UCHAR pipeId) { return m_usbApi.FlushPipe(m_usbHandle, pipeId); }
// Reset the current buffer to lower one. Use this with caution.
// Current buffer should not be reset manually, except after a controlled RCM reboot er disconnection.
// disconnect() method should be preferred
void resetCurrentBuffer() { m_currentBuffer = 0; }
2020-06-24 19:30:39 +01:00
private:
bool m_is_devicePlugged;
KLST_DEVINFO_HANDLE m_devInfo = nullptr;
KUSB_HANDLE m_usbHandle;
KUSB_DRIVER_API m_usbApi;
KLST_HANDLE m_devList = nullptr;
u32 m_currentBuffer = 0;
2020-06-24 19:31:54 +01:00
bool m_devIsInitialized = false;
bool m_usbAPI_loaded = false;
DEVICE_STATUS m_devStatus;
2020-06-24 19:30:39 +01:00
private:
2020-06-24 19:31:54 +01:00
bool getPluggedDevice(KLST_DEVINFO_HANDLE *devinfo);
2020-06-24 19:30:39 +01:00
int switchToHighBuffer();
u32 getCurrentBufferAddress() const { return m_currentBuffer == 0 ? 0x40005000u : 0x40009000u; }
int Ioctl(DWORD ioctlCode, const void* inputBytes, size_t numInputBytes, void* outputBytes, size_t numOutputBytes);
};
class WinHandle
{
public:
WinHandle(HANDLE srcHandle = INVALID_HANDLE_VALUE) noexcept : _handle(srcHandle) {}
~WinHandle()
{
if (_handle != INVALID_HANDLE_VALUE)
{
::CloseHandle(_handle);
_handle = INVALID_HANDLE_VALUE;
}
}
void swap(WinHandle&& other) noexcept { std::swap(_handle, other._handle); }
WinHandle(WinHandle&& moved) noexcept : _handle(INVALID_HANDLE_VALUE) { swap(std::move(moved)); }
WinHandle(const WinHandle& copied) = delete;
WinHandle& operator=(WinHandle&& moved) { swap(std::move(moved)); return *this; }
WinHandle& operator=(const WinHandle& copied) = delete;
HANDLE get() const { return _handle; }
HANDLE release() { HANDLE retHandle = _handle; _handle = INVALID_HANDLE_VALUE; return retHandle; }
protected:
HANDLE _handle;
};
#endif // RCMDEVICE_H