2023-09-04 21:09:31 +02:00
|
|
|
// NOTE: This code doesn't implement EverDrive-64 USB protocol.
|
|
|
|
// Main use of these functions is to aid menu development
|
|
|
|
// (for example replace files on the SD card or reboot menu).
|
|
|
|
|
2024-04-24 02:45:09 +02:00
|
|
|
#include <stdio.h>
|
2023-09-04 21:09:31 +02:00
|
|
|
#include <string.h>
|
2024-04-24 02:45:09 +02:00
|
|
|
|
2023-09-04 21:09:31 +02:00
|
|
|
#include <usb.h>
|
|
|
|
|
|
|
|
#include "usb_comm.h"
|
|
|
|
#include "utils/utils.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define MAX_FILE_SIZE MiB(4)
|
|
|
|
|
2024-01-17 15:43:02 +01:00
|
|
|
|
2023-11-10 17:39:09 +01:00
|
|
|
/** @brief The supported USB commands structure. */
|
2023-09-04 21:09:31 +02:00
|
|
|
typedef struct {
|
2023-11-10 17:39:09 +01:00
|
|
|
/** @brief The command identifier. */
|
2023-09-04 21:09:31 +02:00
|
|
|
const char *id;
|
2024-01-17 15:43:02 +01:00
|
|
|
|
2023-11-10 17:39:09 +01:00
|
|
|
/** @brief The command operation. */
|
2023-09-04 21:09:31 +02:00
|
|
|
void (*op) (menu_t *menu);
|
|
|
|
} usb_comm_command_t;
|
|
|
|
|
|
|
|
|
|
|
|
static int usb_comm_get_char (void) {
|
|
|
|
char c;
|
|
|
|
|
|
|
|
if (USBHEADER_GETSIZE(usb_poll()) <= 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
usb_read(&c, sizeof(c));
|
|
|
|
|
|
|
|
return (int) (c);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool usb_comm_read_string (char *string, int length, char end) {
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
int c = usb_comm_get_char();
|
|
|
|
|
|
|
|
if (c < 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
string[i] = (char) (c);
|
|
|
|
|
|
|
|
if (c == '\0' || c == end) {
|
|
|
|
string[i] = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == (length - 1)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usb_comm_send_error (const char *message) {
|
|
|
|
usb_purge();
|
|
|
|
usb_write(DATATYPE_TEXT, message, strlen(message));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void command_reboot (menu_t *menu) {
|
|
|
|
menu->next_mode = MENU_MODE_BOOT;
|
|
|
|
|
|
|
|
menu->boot_params->device_type = BOOT_DEVICE_TYPE_ROM;
|
|
|
|
menu->boot_params->tv_type = BOOT_TV_TYPE_PASSTHROUGH;
|
|
|
|
menu->boot_params->detect_cic_seed = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void command_send_file (menu_t *menu) {
|
2024-04-24 02:45:09 +02:00
|
|
|
FILE *f;
|
|
|
|
char buffer[256];
|
2023-09-04 21:09:31 +02:00
|
|
|
uint8_t data[8192];
|
2024-04-24 02:45:09 +02:00
|
|
|
char length[8];
|
2023-09-04 21:09:31 +02:00
|
|
|
|
2024-04-24 02:45:09 +02:00
|
|
|
if (usb_comm_read_string(buffer, sizeof(buffer), ' ')) {
|
2023-09-04 21:09:31 +02:00
|
|
|
return usb_comm_send_error("Invalid path argument\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (usb_comm_get_char() != '@') {
|
|
|
|
return usb_comm_send_error("Invalid argument\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (usb_comm_read_string(length, sizeof(length), '@')) {
|
|
|
|
return usb_comm_send_error("Invalid file length argument\n");
|
|
|
|
}
|
|
|
|
|
2024-04-24 02:45:09 +02:00
|
|
|
path_t *path = path_init(menu->storage_prefix, buffer);
|
|
|
|
|
|
|
|
if ((f = fopen(path_get(path), "wb")) == NULL) {
|
|
|
|
path_free(path);
|
2023-09-04 21:09:31 +02:00
|
|
|
return usb_comm_send_error("Couldn't create file\n");
|
|
|
|
}
|
2024-04-24 02:45:09 +02:00
|
|
|
setbuf(f, NULL);
|
|
|
|
path_free(path);
|
2023-09-04 21:09:31 +02:00
|
|
|
|
2024-04-24 02:45:09 +02:00
|
|
|
int remaining = atoi(length);
|
2023-09-04 21:09:31 +02:00
|
|
|
|
|
|
|
if (remaining > MAX_FILE_SIZE) {
|
|
|
|
return usb_comm_send_error("File size too big\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
while (remaining > 0) {
|
|
|
|
int block_size = MIN(remaining, sizeof(data));
|
|
|
|
usb_read(data, block_size);
|
2024-04-24 02:45:09 +02:00
|
|
|
if (fwrite(data, 1, block_size, f) != block_size) {
|
|
|
|
fclose(f);
|
2023-09-04 21:09:31 +02:00
|
|
|
return usb_comm_send_error("Couldn't write all required data to the file\n");
|
|
|
|
}
|
|
|
|
remaining -= block_size;
|
|
|
|
}
|
|
|
|
|
2024-04-24 02:45:09 +02:00
|
|
|
if (fclose(f)) {
|
2023-09-04 21:09:31 +02:00
|
|
|
return usb_comm_send_error("Couldn't flush data to the file\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (usb_comm_get_char() != '\0') {
|
|
|
|
return usb_comm_send_error("Invalid token at the end of data stream\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static usb_comm_command_t commands[] = {
|
|
|
|
{ .id = "reboot", .op = command_reboot },
|
|
|
|
{ .id = "send-file", .op = command_send_file },
|
|
|
|
{ .id = NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void usb_comm_poll (menu_t *menu) {
|
|
|
|
uint32_t header = usb_poll();
|
|
|
|
|
|
|
|
if (USBHEADER_GETTYPE(header) != DATATYPE_TEXT) {
|
|
|
|
usb_purge();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (USBHEADER_GETSIZE(header) > 0) {
|
|
|
|
char cmd_id[32];
|
|
|
|
|
|
|
|
if (usb_comm_read_string(cmd_id, sizeof(cmd_id), ' ')) {
|
|
|
|
usb_comm_send_error("Command id too long\n");
|
|
|
|
} else {
|
|
|
|
usb_comm_command_t *cmd = commands;
|
|
|
|
|
|
|
|
while (cmd->id != NULL) {
|
|
|
|
if (strcmp(cmd->id, cmd_id) == 0) {
|
|
|
|
cmd->op(menu);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
cmd++;
|
|
|
|
}
|
|
|
|
|
|
|
|
usb_purge();
|
|
|
|
|
|
|
|
if (cmd->id == NULL) {
|
|
|
|
usb_comm_send_error("Unknown command\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|