summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Furby Haxx furbyhaxx@gmail.com2026-09-19 13:37:34 +0200
committerGravatar Nikias Bassen2026-11-15 23:22:07 +0100
commitbeef3dfa74e8b7a489aea5e212087bc18e57bb64 (patch)
tree49b1e0bcd0918228add56e46c0664fe5dcbbebc3
parentbfad7145785525b9e0c1e1490f3d39ef364b6b18 (diff)
downloadlibirecovery-beef3dfa74e8b7a489aea5e212087bc18e57bb64.tar.gz
libirecovery-beef3dfa74e8b7a489aea5e212087bc18e57bb64.tar.bz2
Propagate recovery command failures
Co-authored-by: Nikias Bassen <nikias@gmx.li>
-rw-r--r--include/libirecovery.h1
-rw-r--r--src/libirecovery.c105
-rw-r--r--tools/irecovery.c81
3 files changed, 142 insertions, 45 deletions
diff --git a/include/libirecovery.h b/include/libirecovery.h
index 7c3323c..69f0f94 100644
--- a/include/libirecovery.h
+++ b/include/libirecovery.h
@@ -178,6 +178,7 @@ IRECV_API irecv_error_t irecv_event_unsubscribe(irecv_client_t client, irecv_eve
IRECV_API irecv_error_t irecv_send_file(irecv_client_t client, const char* filename, unsigned int options);
IRECV_API irecv_error_t irecv_send_command(irecv_client_t client, const char* command);
IRECV_API irecv_error_t irecv_send_command_breq(irecv_client_t client, const char* command, uint8_t b_request);
+IRECV_API int irecv_command_is_breq(const char* command);
IRECV_API irecv_error_t irecv_send_buffer(irecv_client_t client, unsigned char* buffer, unsigned long length, unsigned int options);
IRECV_API irecv_error_t irecv_recv_buffer(irecv_client_t client, char* buffer, unsigned long length);
diff --git a/src/libirecovery.c b/src/libirecovery.c
index 63658cc..4654eb5 100644
--- a/src/libirecovery.c
+++ b/src/libirecovery.c
@@ -3737,7 +3737,82 @@ const char* irecv_version()
}
+int irecv_command_is_breq(const char* command)
+{
+ static const char* const commands[] = { "go", "bootx", "reboot", "memboot" };
+ size_t token_length;
+ size_t i;
+
+ if (command == NULL) {
+ return 0;
+ }
+
+ command += strspn(command, " \t\r\n");
+ token_length = strcspn(command, " \t\r\n");
+ for (i = 0; i < sizeof(commands) / sizeof(commands[0]); i++) {
+ if (strlen(commands[i]) == token_length
+ && strncmp(command, commands[i], token_length) == 0) {
+ return 1;
+ }
+ }
+
+ return 0;
+}
+
#ifndef USE_DUMMY
+static int irecv_command_error_is_accepted(irecv_error_t error, const char* command)
+{
+ if (error == IRECV_E_PIPE) {
+ return 1;
+ }
+
+ return irecv_command_is_breq(command)
+ && (error == IRECV_E_NO_DEVICE
+ || error == IRECV_E_TIMEOUT
+ || error == IRECV_E_USB_STATUS);
+}
+
+static irecv_error_t irecv_control_transfer_error(int result)
+{
+#ifdef HAVE_IOKIT
+ /* The IOKit backend already returns public irecv_error_t values. */
+ switch (result) {
+ case IRECV_E_NO_DEVICE:
+ case IRECV_E_OUT_OF_MEMORY:
+ case IRECV_E_UNABLE_TO_CONNECT:
+ case IRECV_E_INVALID_INPUT:
+ case IRECV_E_FILE_NOT_FOUND:
+ case IRECV_E_USB_UPLOAD:
+ case IRECV_E_USB_STATUS:
+ case IRECV_E_USB_INTERFACE:
+ case IRECV_E_USB_CONFIGURATION:
+ case IRECV_E_PIPE:
+ case IRECV_E_TIMEOUT:
+ case IRECV_E_UNSUPPORTED:
+ case IRECV_E_UNKNOWN_ERROR:
+ return (irecv_error_t)result;
+ default:
+ return IRECV_E_USB_STATUS;
+ }
+#elif defined(_WIN32)
+ /* The Windows backend exposes only an undifferentiated negative result. */
+ return IRECV_E_USB_STATUS;
+#else
+ switch (result) {
+ case LIBUSB_ERROR_PIPE:
+ return IRECV_E_PIPE;
+ case LIBUSB_ERROR_TIMEOUT:
+ return IRECV_E_TIMEOUT;
+ case LIBUSB_ERROR_NO_DEVICE:
+ return IRECV_E_NO_DEVICE;
+ case LIBUSB_ERROR_NO_MEM:
+ return IRECV_E_OUT_OF_MEMORY;
+ default:
+ return IRECV_E_USB_STATUS;
+ }
+#endif
+}
+
static irecv_error_t irecv_send_command_raw(irecv_client_t client, const char* command, uint8_t b_request)
{
unsigned int length = strlen(command);
@@ -3775,7 +3850,13 @@ static irecv_error_t irecv_send_command_raw(irecv_client_t client, const char* c
}
if (length > 0) {
- irecv_usb_control_transfer(client, 0x40, b_request, 0, 0, (unsigned char*) command, length + 1, USB_TIMEOUT);
+ int transferred = irecv_usb_control_transfer(client, 0x40, b_request, 0, 0, (unsigned char*) command, length + 1, USB_TIMEOUT);
+ if (transferred < 0) {
+ return irecv_control_transfer_error(transferred);
+ }
+ if ((unsigned int)transferred != length + 1) {
+ return IRECV_E_USB_STATUS;
+ }
}
return IRECV_E_SUCCESS;
@@ -3810,8 +3891,9 @@ irecv_error_t irecv_send_command_breq(irecv_client_t client, const char* command
error = irecv_send_command_raw(client, command, b_request);
if (error != IRECV_E_SUCCESS) {
debug("Failed to send command %s\n", command);
- if (error != IRECV_E_PIPE)
+ if (!irecv_command_error_is_accepted(error, command)) {
return error;
+ }
}
if (client->postcommand_callback != NULL) {
@@ -4283,10 +4365,6 @@ irecv_error_t irecv_getenv(irecv_client_t client, const char* variable, char** v
memset(command, 0, sizeof(command));
snprintf(command, sizeof(command)-1, "getenv %s", variable);
irecv_error_t error = irecv_send_command_raw(client, command, 0);
- if (error == IRECV_E_PIPE) {
- return IRECV_E_SUCCESS;
- }
-
if (error != IRECV_E_SUCCESS) {
return error;
}
@@ -4297,9 +4375,18 @@ irecv_error_t irecv_getenv(irecv_client_t client, const char* variable, char** v
return IRECV_E_OUT_OF_MEMORY;
}
- memset(response, 0, rsize);
- irecv_usb_control_transfer(client, 0xC0, 0, 0, 0, (unsigned char*) response, rsize-1, USB_TIMEOUT);
+ int received = irecv_usb_control_transfer(client, 0xC0, 0, 0, 0, (unsigned char*) response, rsize-1, USB_TIMEOUT);
+ if (received < 0) {
+ free(response);
+ return irecv_control_transfer_error(received);
+ }
+ if (received >= rsize) {
+ free(response);
+ return IRECV_E_USB_STATUS;
+ }
+ response[received] = '\0';
+ /* A zero-length response is a successful getenv with an empty value. */
*value = response;
return IRECV_E_SUCCESS;
@@ -4529,7 +4616,7 @@ irecv_error_t irecv_reboot(irecv_client_t client)
return IRECV_E_UNSUPPORTED;
#else
irecv_error_t error = irecv_send_command_raw(client, "reboot", 0);
- if (error != IRECV_E_SUCCESS) {
+ if (error != IRECV_E_SUCCESS && !irecv_command_error_is_accepted(error, "reboot")) {
return error;
}
diff --git a/tools/irecovery.c b/tools/irecovery.c
index b293324..ee3d907 100644
--- a/tools/irecovery.c
+++ b/tools/irecovery.c
@@ -33,6 +33,7 @@
#include <inttypes.h>
#include <ctype.h>
#include <libirecovery.h>
+
#ifdef HAVE_READLINE
#include <readline/readline.h>
#include <readline/history.h>
@@ -219,16 +220,6 @@ static void print_devices()
}
}
-static int _is_breq_command(const char* cmd)
-{
- return (
- !strcmp(cmd, "go")
- || !strcmp(cmd, "bootx")
- || !strcmp(cmd, "reboot")
- || !strcmp(cmd, "memboot")
- );
-}
-
static void parse_command(irecv_client_t client, unsigned char* command, unsigned int size)
{
char* cmd = strdup((char*)command);
@@ -344,11 +335,7 @@ static void init_shell(irecv_client_t client)
get_input(cmdbuf, sizeof(cmdbuf));
#endif
if (cmd && *cmd) {
- if (_is_breq_command(cmd)) {
- error = irecv_send_command_breq(client, cmd, 1);
- } else {
- error = irecv_send_command(client, cmd);
- }
+ error = irecv_send_command_breq(client, cmd, irecv_command_is_breq(cmd));
if (error != IRECV_E_SUCCESS) {
quit = 1;
}
@@ -382,6 +369,43 @@ int precommand_cb(irecv_client_t client, const irecv_event_t* event)
parse_command(client, (unsigned char*)event->data, event->size);
return -1;
}
+
+ char* command = strdup(event->data);
+ if (command == NULL) {
+ fprintf(stderr, "irecovery: %s\n", irecv_strerror(IRECV_E_OUT_OF_MEMORY));
+ return -1;
+ }
+ char* action = strtok(command, " \t");
+ if (action && !strcmp(action, "getenv")) {
+ char* argument = strtok(NULL, " \t");
+ char* trailing = strtok(NULL, " \t");
+ if (argument == NULL) {
+ fprintf(stderr, "getenv: missing variable name\n");
+ free(command);
+ return -1;
+ }
+ if (trailing != NULL) {
+ fprintf(stderr, "getenv: unexpected trailing argument '%s'\n", trailing);
+ free(command);
+ return -1;
+ }
+
+ char* value = NULL;
+ irecv_error_t error = irecv_getenv(client, argument, &value);
+ if (error == IRECV_E_UNSUPPORTED) {
+ free(command);
+ return 0;
+ }
+ if (error != IRECV_E_SUCCESS) {
+ fprintf(stderr, "getenv: %s\n", irecv_strerror(error));
+ } else {
+ printf("%s\n", value);
+ free(value);
+ }
+ free(command);
+ return -1;
+ }
+ free(command);
}
return 0;
@@ -389,28 +413,17 @@ int precommand_cb(irecv_client_t client, const irecv_event_t* event)
int postcommand_cb(irecv_client_t client, const irecv_event_t* event)
{
- char* value = NULL;
char* action = NULL;
char* command = NULL;
- char* argument = NULL;
- irecv_error_t error = IRECV_E_SUCCESS;
if (event->type == IRECV_POSTCOMMAND) {
command = strdup(event->data);
- action = strtok(command, " ");
- if (!strcmp(action, "getenv")) {
- argument = strtok(NULL, " ");
- error = irecv_getenv(client, argument, &value);
- if (error != IRECV_E_SUCCESS) {
- debug("%s\n", irecv_strerror(error));
- free(command);
- return error;
- }
- printf("%s\n", value);
- free(value);
+ if (command == NULL) {
+ debug("Out of memory\n");
+ return 0;
}
-
- if (!strcmp(action, "reboot")) {
+ action = strtok(command, " ");
+ if (action && !strcmp(action, "reboot")) {
quit = 1;
}
}
@@ -658,11 +671,7 @@ int main(int argc, char* argv[])
printf("Shell is not available in Debug USB (KIS) mode.\n");
break;
}
- if (_is_breq_command(argument)) {
- error = irecv_send_command_breq(client, argument, 1);
- } else {
- error = irecv_send_command(client, argument);
- }
+ error = irecv_send_command_breq(client, argument, irecv_command_is_breq(argument));
debug("%s\n", irecv_strerror(error));
break;