From 03a42f6c6afc69919544c6dbe0fce49637ec17df Mon Sep 17 00:00:00 2001 From: Ethan Yonker Date: Fri, 8 Aug 2014 11:03:51 -0500 Subject: Add command line capabilities Allows sending openrecoveryscript commands to TWRP via shell. This may be handy for visually impaired users, for various one click utilities to drive TWRP commands from a computer, for using TWRP when a catastrophic hardware failure like a shattered screen prevents you from being able to use touch, or even on devices like a TV stick where touch and USB mouse input is unavailable. This patch also includes a few minor changes to openrecoveryscript including proper support for rebooting via the script and for decrypting the device via the command line. Change-Id: I796ad168efdd2da9c25334ac93d1079daaa7b0bc --- gui/gui.cpp | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 98 insertions(+), 1 deletion(-) (limited to 'gui/gui.cpp') diff --git a/gui/gui.cpp b/gui/gui.cpp index b7680909c..80063399f 100644 --- a/gui/gui.cpp +++ b/gui/gui.cpp @@ -53,6 +53,8 @@ extern "C" #include "../variables.h" #include "../partitions.hpp" #include "../twrp-functions.hpp" +#include "../openrecoveryscript.hpp" +#include "../orscmd/orscmd.h" #ifndef TW_NO_SCREEN_TIMEOUT #include "blanktimer.hpp" #endif @@ -73,6 +75,7 @@ static int gForceRender = 0; pthread_mutex_t gForceRendermutex; static int gNoAnimation = 1; static int gGuiInputRunning = 0; +static int gCmdLineRunning = 0; #ifndef TW_NO_SCREEN_TIMEOUT blanktimer blankTimer; #endif @@ -434,6 +437,85 @@ static void * input_thread(void *cookie) return NULL; } +static void * command_thread(void *cookie) +{ + int read_fd; + FILE* orsout; + char command[1024], result[512]; + + LOGINFO("Starting command line thread\n"); + + unlink(ORS_INPUT_FILE); + if (mkfifo(ORS_INPUT_FILE, 06660) != 0) { + LOGINFO("Unable to mkfifo %s\n", ORS_INPUT_FILE); + return 0; + } + unlink(ORS_OUTPUT_FILE); + if (mkfifo(ORS_OUTPUT_FILE, 06666) != 0) { + LOGINFO("Unable to mkfifo %s\n", ORS_OUTPUT_FILE); + unlink(ORS_INPUT_FILE); + return 0; + } + + read_fd = open(ORS_INPUT_FILE, O_RDONLY); + if (read_fd < 0) { + LOGINFO("Unable to open %s\n", ORS_INPUT_FILE); + unlink(ORS_INPUT_FILE); + unlink(ORS_OUTPUT_FILE); + return 0; + } + + while (!gGuiRunning) + sleep(1); + + for (;;) { + while (read(read_fd, &command, sizeof(command)) > 0) { + command[1022] = '\n'; + command[1023] = '\0'; + LOGINFO("Command '%s' received\n", command); + orsout = fopen(ORS_OUTPUT_FILE, "w"); + if (!orsout) { + close(read_fd); + LOGINFO("Unable to fopen %s\n", ORS_OUTPUT_FILE); + unlink(ORS_INPUT_FILE); + unlink(ORS_OUTPUT_FILE); + return 0; + } + if (DataManager::GetIntValue("tw_busy") != 0) { + strcpy(result, "Failed, operation in progress\n"); + fprintf(orsout, "%s", result); + LOGINFO("Command cannot be performed, operation in progress.\n"); + } else { + if (gui_console_only() == 0) { + LOGINFO("Console started successfully\n"); + gui_set_FILE(orsout); + if (strlen(command) > 11 && strncmp(command, "runscript", 9) == 0) { + char* filename = command + 11; + if (OpenRecoveryScript::copy_script_file(filename) == 0) { + LOGERR("Unable to copy script file\n"); + } else { + OpenRecoveryScript::run_script_file(); + } + } else if (strlen(command) > 5 && strncmp(command, "get", 3) == 0) { + char* varname = command + 4; + string temp; + DataManager::GetValue(varname, temp); + gui_print("%s = %s\n", varname, temp.c_str()); + } else if (OpenRecoveryScript::Insert_ORS_Command(command)) { + OpenRecoveryScript::run_script_file(); + } + gui_set_FILE(NULL); + gGuiConsoleTerminate = 1; + } + } + fclose(orsout); + } + } + close(read_fd); + LOGINFO("Command thread exiting\n"); + return 0; +} + // This special function will return immediately the first time, but then // always returns 1/30th of a second (or immediately if called later) from // the last time it was called @@ -495,6 +577,10 @@ static int runPages(void) { loopTimer(); + if (gGuiConsoleRunning) { + continue; + } + if (!gForceRender) { int ret; @@ -769,7 +855,15 @@ extern "C" int gui_start(void) pthread_create(&t, NULL, input_thread, NULL); gGuiInputRunning = 1; } - +#ifndef TW_OEM_BUILD + if (!gCmdLineRunning) + { + // Start by spinning off an input handler. + pthread_t t; + pthread_create(&t, NULL, command_thread, NULL); + gCmdLineRunning = 1; + } +#endif return runPages(); } @@ -830,6 +924,9 @@ static void * console_thread(void *cookie) } } gGuiConsoleRunning = 0; + gForceRender = 1; // this will kickstart the GUI to render again + PageManager::EndConsole(); + LOGINFO("Console stopping\n"); return NULL; } -- cgit v1.2.3