summaryrefslogtreecommitdiffstats
path: root/tools/irecovery.c
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 /tools/irecovery.c
parentbfad7145785525b9e0c1e1490f3d39ef364b6b18 (diff)
downloadlibirecovery-beef3dfa74e8b7a489aea5e212087bc18e57bb64.tar.gz
libirecovery-beef3dfa74e8b7a489aea5e212087bc18e57bb64.tar.bz2
Propagate recovery command failures
Co-authored-by: Nikias Bassen <nikias@gmx.li>
Diffstat (limited to 'tools/irecovery.c')
-rw-r--r--tools/irecovery.c81
1 files changed, 45 insertions, 36 deletions
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;