blob: d72b33194cb6790d4e2fa0ee316d54c45fb0eadf [file] [log] [blame]
jrior001aad03112014-07-05 10:31:21 -04001/*update
Dees_Troya13d74f2013-03-24 08:54:55 -05002 Copyright 2013 bigbiff/Dees_Troy TeamWin
3 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
Dees_Troy51a0e822012-09-05 15:24:24 -040018
19#include <stdarg.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <fcntl.h>
24#include <sys/stat.h>
25#include <sys/time.h>
26#include <sys/mman.h>
27#include <sys/types.h>
28#include <sys/ioctl.h>
29#include <linux/input.h>
30#include <time.h>
31#include <unistd.h>
32#include <stdlib.h>
Dees_Troy657c3092012-09-10 20:32:10 -040033#include <sys/wait.h>
Dees_Troy83bd4832013-05-04 12:39:56 +000034#include <dirent.h>
Vojtech Bocek03fd6c52014-03-13 18:46:34 +010035#include <pwd.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040036
37#include <string>
38#include <sstream>
39#include "../partitions.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040040#include "../twrp-functions.hpp"
Dees_Troy812660f2012-09-20 09:55:17 -040041#include "../openrecoveryscript.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040042
Dees_Troy43d8b002012-09-17 16:00:01 -040043#include "../adb_install.h"
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070044#ifndef TW_NO_SCREEN_TIMEOUT
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050045#include "blanktimer.hpp"
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070046#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040047extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000048#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040049#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040050#include "../variables.h"
Dees_Troy32c8eb82012-09-11 15:28:06 -040051#include "../twinstall.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000052#include "cutils/properties.h"
Dees_Troy43d8b002012-09-17 16:00:01 -040053#include "../minadbd/adb.h"
Ethan Yonker24813422014-11-07 17:19:07 -060054#include "../adb_install.h"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060055#include "../set_metadata.h"
Dees_Troy43d8b002012-09-17 16:00:01 -040056
Dees_Troy32c8eb82012-09-11 15:28:06 -040057int TWinstall_zip(const char* path, int* wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -040058void run_script(const char *str1, const char *str2, const char *str3, const char *str4, const char *str5, const char *str6, const char *str7, int request_confirm);
Dees_Troy51a0e822012-09-05 15:24:24 -040059int gui_console_only();
Dees_Troy51a0e822012-09-05 15:24:24 -040060int gui_start();
61};
62
63#include "rapidxml.hpp"
64#include "objects.hpp"
65
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070066#ifndef TW_NO_SCREEN_TIMEOUT
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050067extern blanktimer blankTimer;
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070068#endif
Dees_Troy43d8b002012-09-17 16:00:01 -040069
Dees_Troy51a0e822012-09-05 15:24:24 -040070void curtainClose(void);
71
72GUIAction::GUIAction(xml_node<>* node)
Vojtech Bocekede51c52014-02-07 23:58:09 +010073 : GUIObject(node)
Dees_Troy51a0e822012-09-05 15:24:24 -040074{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020075 xml_node<>* child;
76 xml_node<>* actions;
77 xml_attribute<>* attr;
Dees_Troy51a0e822012-09-05 15:24:24 -040078
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020079 if (!node) return;
Dees_Troy51a0e822012-09-05 15:24:24 -040080
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020081 // First, get the action
82 actions = node->first_node("actions");
83 if (actions) child = actions->first_node("action");
84 else child = node->first_node("action");
Dees_Troy51a0e822012-09-05 15:24:24 -040085
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020086 if (!child) return;
Dees_Troy51a0e822012-09-05 15:24:24 -040087
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020088 while (child)
89 {
90 Action action;
Dees_Troy51a0e822012-09-05 15:24:24 -040091
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020092 attr = child->first_attribute("function");
93 if (!attr) return;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -050094
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020095 action.mFunction = attr->value();
96 action.mArg = child->value();
97 mActions.push_back(action);
Dees_Troy51a0e822012-09-05 15:24:24 -040098
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020099 child = child->next_sibling("action");
100 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400101
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200102 // Now, let's get either the key or region
103 child = node->first_node("touch");
104 if (child)
105 {
106 attr = child->first_attribute("key");
107 if (attr)
108 {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100109 std::vector<std::string> keys = TWFunc::Split_String(attr->value(), "+");
110 for(size_t i = 0; i < keys.size(); ++i)
111 {
112 const int key = getKeyByName(keys[i]);
113 mKeys[key] = false;
114 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200115 }
116 else
117 {
118 attr = child->first_attribute("x");
119 if (!attr) return;
120 mActionX = atol(attr->value());
121 attr = child->first_attribute("y");
122 if (!attr) return;
123 mActionY = atol(attr->value());
124 attr = child->first_attribute("w");
125 if (!attr) return;
126 mActionW = atol(attr->value());
127 attr = child->first_attribute("h");
128 if (!attr) return;
129 mActionH = atol(attr->value());
130 }
131 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400132}
133
134int GUIAction::NotifyTouch(TOUCH_STATE state, int x, int y)
135{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200136 if (state == TOUCH_RELEASE)
137 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400138
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200139 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400140}
141
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100142int GUIAction::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -0400143{
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100144 if (mKeys.empty())
145 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400146
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100147 std::map<int, bool>::iterator itr = mKeys.find(key);
148 if(itr == mKeys.end())
149 return 0;
150
151 bool prevState = itr->second;
152 itr->second = down;
153
154 // If there is only one key for this action, wait for key up so it
155 // doesn't trigger with multi-key actions.
156 // Else, check if all buttons are pressed, then consume their release events
157 // so they don't trigger one-button actions and reset mKeys pressed status
158 if(mKeys.size() == 1) {
159 if(!down && prevState)
160 doActions();
161 } else if(down) {
162 for(itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
163 if(!itr->second)
164 return 0;
165 }
166
167 // Passed, all req buttons are pressed, reset them and consume release events
168 HardwareKeyboard *kb = PageManager::GetHardwareKeyboard();
169 for(itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
170 kb->ConsumeKeyRelease(itr->first);
171 itr->second = false;
172 }
173
174 doActions();
175 }
176
Dees_Troy51a0e822012-09-05 15:24:24 -0400177 return 0;
178}
179
Vojtech Bocek07220562014-02-08 02:05:33 +0100180int GUIAction::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400181{
Vojtech Bocek07220562014-02-08 02:05:33 +0100182 GUIObject::NotifyVarChange(varName, value);
183
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100184 if (varName.empty() && !isConditionValid() && mKeys.empty() && !mActionW)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200185 doActions();
Vojtech Bocek07220562014-02-08 02:05:33 +0100186 else if((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200187 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400188
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200189 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400190}
191
192void GUIAction::simulate_progress_bar(void)
193{
Dees_Troy2673cec2013-04-02 20:22:16 +0000194 gui_print("Simulating actions...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400195 for (int i = 0; i < 5; i++)
196 {
197 usleep(500000);
198 DataManager::SetValue("ui_progress", i * 20);
199 }
200}
201
Dees_Troy657c3092012-09-10 20:32:10 -0400202int GUIAction::flash_zip(std::string filename, std::string pageName, const int simulate, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400203{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200204 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400205
206 DataManager::SetValue("ui_progress", 0);
207
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200208 if (filename.empty())
209 {
210 LOGERR("No file specified.\n");
211 return -1;
212 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400213
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200214 // We're going to jump to this page first, like a loading page
215 gui_changePage(pageName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400216
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200217 int fd = -1;
218 ZipArchive zip;
Dees_Troy51a0e822012-09-05 15:24:24 -0400219
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200220 if (!PartitionManager.Mount_By_Path(filename, true))
Dees_Troy657c3092012-09-10 20:32:10 -0400221 return -1;
222
Ethan Yonker57e35872014-11-07 14:52:22 -0600223 gui_changePage(pageName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400224
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200225 if (fd >= 0)
226 close(fd);
Dees_Troy51a0e822012-09-05 15:24:24 -0400227
228 if (simulate) {
229 simulate_progress_bar();
230 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400231 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400232
233 // Now, check if we need to ensure TWRP remains installed...
234 struct stat st;
235 if (stat("/sbin/installTwrp", &st) == 0)
236 {
237 DataManager::SetValue("tw_operation", "Configuring TWRP");
238 DataManager::SetValue("tw_partition", "");
Dees_Troy2673cec2013-04-02 20:22:16 +0000239 gui_print("Configuring TWRP...\n");
Vojtech Bocek05534202013-09-11 08:11:56 +0200240 if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall") < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400241 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000242 gui_print("Unable to configure TWRP with this kernel.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400243 }
244 }
245 }
246
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200247 // Done
248 DataManager::SetValue("ui_progress", 100);
249 DataManager::SetValue("ui_progress", 0);
250 return ret_val;
Dees_Troy51a0e822012-09-05 15:24:24 -0400251}
252
253int GUIAction::doActions()
254{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200255 if (mActions.size() < 1) return -1;
256 if (mActions.size() == 1)
Dees_Troy51a0e822012-09-05 15:24:24 -0400257 return doAction(mActions.at(0), 0);
258
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200259 // For multi-action, we always use a thread
260 pthread_t t;
Dees_Troyab4963c2013-01-16 20:35:51 +0000261 pthread_attr_t tattr;
262
263 if (pthread_attr_init(&tattr)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000264 LOGERR("Unable to pthread_attr_init\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000265 return -1;
266 }
267 if (pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_JOINABLE)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000268 LOGERR("Error setting pthread_attr_setdetachstate\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000269 return -1;
270 }
271 if (pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000272 LOGERR("Error setting pthread_attr_setscope\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000273 return -1;
274 }
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500275 /*if (pthread_attr_setstacksize(&tattr, 524288)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000276 LOGERR("Error setting pthread_attr_setstacksize\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000277 return -1;
278 }
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500279 */
Dees_Troyab4963c2013-01-16 20:35:51 +0000280 int ret = pthread_create(&t, &tattr, thread_start, this);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200281 if (ret) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000282 LOGERR("Unable to create more threads for actions... continuing in same thread! %i\n", ret);
Dees_Troyab4963c2013-01-16 20:35:51 +0000283 thread_start(this);
284 } else {
285 if (pthread_join(t, NULL)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000286 LOGERR("Error joining threads\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000287 }
288 }
289 if (pthread_attr_destroy(&tattr)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000290 LOGERR("Failed to pthread_attr_destroy\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000291 return -1;
292 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400293
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200294 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400295}
296
297void* GUIAction::thread_start(void *cookie)
298{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200299 GUIAction* ourThis = (GUIAction*) cookie;
Dees_Troy51a0e822012-09-05 15:24:24 -0400300
301 DataManager::SetValue(TW_ACTION_BUSY, 1);
302
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200303 if (ourThis->mActions.size() > 1)
304 {
305 std::vector<Action>::iterator iter;
306 for (iter = ourThis->mActions.begin(); iter != ourThis->mActions.end(); iter++)
307 ourThis->doAction(*iter, 1);
308 }
309 else
310 {
311 ourThis->doAction(ourThis->mActions.at(0), 1);
312 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400313 int check = 0;
314 DataManager::GetValue("tw_background_thread_running", check);
315 if (check == 0)
316 DataManager::SetValue(TW_ACTION_BUSY, 0);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200317 return NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -0400318}
319
320void GUIAction::operation_start(const string operation_name)
321{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000322 time(&Start);
Dees_Troy51a0e822012-09-05 15:24:24 -0400323 DataManager::SetValue(TW_ACTION_BUSY, 1);
324 DataManager::SetValue("ui_progress", 0);
325 DataManager::SetValue("tw_operation", operation_name);
326 DataManager::SetValue("tw_operation_status", 0);
327 DataManager::SetValue("tw_operation_state", 0);
328}
329
330void GUIAction::operation_end(const int operation_status, const int simulate)
331{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000332 time_t Stop;
Dees_Troy51a0e822012-09-05 15:24:24 -0400333 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400334 DataManager::SetValue("ui_progress", 100);
335 if (simulate) {
336 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
337 if (simulate_fail != 0)
338 DataManager::SetValue("tw_operation_status", 1);
339 else
340 DataManager::SetValue("tw_operation_status", 0);
341 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500342 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400343 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500344 }
345 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400346 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500347 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400348 }
349 DataManager::SetValue("tw_operation_state", 1);
350 DataManager::SetValue(TW_ACTION_BUSY, 0);
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700351#ifndef TW_NO_SCREEN_TIMEOUT
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500352 blankTimer.resetTimerAndUnblank();
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700353#endif
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000354 time(&Stop);
355 if ((int) difftime(Stop, Start) > 10)
Ethan Yonker03db3262014-02-05 08:02:06 -0600356 DataManager::Vibrate("tw_action_vibrate");
Dees_Troy51a0e822012-09-05 15:24:24 -0400357}
358
359int GUIAction::doAction(Action action, int isThreaded /* = 0 */)
360{
361 static string zip_queue[10];
362 static int zip_queue_index;
363 static pthread_t terminal_command;
364 int simulate;
365
366 std::string arg = gui_parse_text(action.mArg);
367
368 std::string function = gui_parse_text(action.mFunction);
369
370 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
371
Dees_Troy6ef66352013-02-21 08:26:57 -0600372 if (function == "reboot")
373 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200374 //curtainClose(); this sometimes causes a crash
Dees_Troy51a0e822012-09-05 15:24:24 -0400375
Dees_Troy6ef66352013-02-21 08:26:57 -0600376 sync();
377 DataManager::SetValue("tw_gui_done", 1);
378 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400379
Dees_Troy6ef66352013-02-21 08:26:57 -0600380 return 0;
381 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200382 if (function == "home")
383 {
384 PageManager::SelectPackage("TWRP");
385 gui_changePage("main");
386 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400387 }
388
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200389 if (function == "key")
390 {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100391 const int key = getKeyByName(arg);
392 PageManager::NotifyKey(key, true);
393 PageManager::NotifyKey(key, false);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200394 return 0;
395 }
396
397 if (function == "page") {
398 std::string page_name = gui_parse_text(arg);
399 return gui_changePage(page_name);
400 }
401
402 if (function == "reload") {
Dees_Troy51a0e822012-09-05 15:24:24 -0400403 int check = 0, ret_val = 0;
404 std::string theme_path;
405
406 operation_start("Reload Theme");
407 theme_path = DataManager::GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -0400408 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000409 LOGERR("Unable to mount %s during reload function startup.\n", theme_path.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400410 check = 1;
411 }
412
413 theme_path += "/TWRP/theme/ui.zip";
414 if (check != 0 || PageManager::ReloadPackage("TWRP", theme_path) != 0)
415 {
416 // Loading the custom theme failed - try loading the stock theme
Dees_Troy2673cec2013-04-02 20:22:16 +0000417 LOGINFO("Attempting to reload stock theme...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400418 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
419 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000420 LOGERR("Failed to load base packages.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400421 ret_val = 1;
422 }
423 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200424 operation_end(ret_val, simulate);
Dees_Troy83bd4832013-05-04 12:39:56 +0000425 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400426 }
427
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200428 if (function == "readBackup")
429 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400430 string Restore_Name;
431 DataManager::GetValue("tw_restore", Restore_Name);
Dees_Troy5bf43922012-09-07 16:07:55 -0400432 PartitionManager.Set_Restore_Files(Restore_Name);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200433 return 0;
434 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400435
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200436 if (function == "set")
437 {
438 if (arg.find('=') != string::npos)
439 {
440 string varName = arg.substr(0, arg.find('='));
441 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400442
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200443 DataManager::GetValue(value, value);
444 DataManager::SetValue(varName, value);
445 }
446 else
447 DataManager::SetValue(arg, "1");
448 return 0;
449 }
450 if (function == "clear")
451 {
452 DataManager::SetValue(arg, "0");
453 return 0;
454 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400455
Ethan Yonker8214f0a2014-02-16 15:15:34 -0600456 if (function == "mount") {
457 if (arg == "usb") {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200458 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400459 if (!simulate)
Dees_Troy8170a922012-09-18 15:40:25 -0400460 PartitionManager.usb_storage_enable();
Dees_Troy51a0e822012-09-05 15:24:24 -0400461 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000462 gui_print("Simulating actions...\n");
Ethan Yonker8214f0a2014-02-16 15:15:34 -0600463 } else if (!simulate) {
464 PartitionManager.Mount_By_Path(arg, true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200465 } else
Dees_Troy2673cec2013-04-02 20:22:16 +0000466 gui_print("Simulating actions...\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200467 return 0;
468 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400469
Ethan Yonker8214f0a2014-02-16 15:15:34 -0600470 if (function == "umount" || function == "unmount") {
471 if (arg == "usb") {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200472 if (!simulate)
Dees_Troy8170a922012-09-18 15:40:25 -0400473 PartitionManager.usb_storage_disable();
Dees_Troy51a0e822012-09-05 15:24:24 -0400474 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000475 gui_print("Simulating actions...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400476 DataManager::SetValue(TW_ACTION_BUSY, 0);
Ethan Yonker8214f0a2014-02-16 15:15:34 -0600477 } else if (!simulate) {
478 PartitionManager.UnMount_By_Path(arg, true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200479 } else
Dees_Troy2673cec2013-04-02 20:22:16 +0000480 gui_print("Simulating actions...\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200481 return 0;
482 }
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500483
Dees_Troy51a0e822012-09-05 15:24:24 -0400484 if (function == "restoredefaultsettings")
485 {
486 operation_start("Restore Defaults");
487 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
Dees_Troy2673cec2013-04-02 20:22:16 +0000488 gui_print("Simulating actions...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400489 else {
490 DataManager::ResetDefaults();
Dees_Troy5bf43922012-09-07 16:07:55 -0400491 PartitionManager.Update_System_Details();
492 PartitionManager.Mount_Current_Storage(true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400493 }
494 operation_end(0, simulate);
Dees_Troy83bd4832013-05-04 12:39:56 +0000495 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400496 }
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500497
Dees_Troy51a0e822012-09-05 15:24:24 -0400498 if (function == "copylog")
499 {
500 operation_start("Copy Log");
501 if (!simulate)
502 {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500503 string dst;
Dees_Troy5bf43922012-09-07 16:07:55 -0400504 PartitionManager.Mount_Current_Storage(true);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500505 dst = DataManager::GetCurrentStoragePath() + "/recovery.log";
506 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -0600507 tw_set_default_metadata(dst.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400508 sync();
Dees_Troy2673cec2013-04-02 20:22:16 +0000509 gui_print("Copied recovery log to %s.\n", DataManager::GetCurrentStoragePath().c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400510 } else
511 simulate_progress_bar();
512 operation_end(0, simulate);
513 return 0;
514 }
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500515
Dees_Troy51a0e822012-09-05 15:24:24 -0400516 if (function == "compute" || function == "addsubtract")
517 {
518 if (arg.find("+") != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200519 {
520 string varName = arg.substr(0, arg.find('+'));
521 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400522 int amount_to_add = atoi(string_to_add.c_str());
523 int value;
524
525 DataManager::GetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200526 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400527 return 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200528 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400529 if (arg.find("-") != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200530 {
531 string varName = arg.substr(0, arg.find('-'));
532 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400533 int amount_to_subtract = atoi(string_to_subtract.c_str());
534 int value;
535
536 DataManager::GetValue(varName, value);
537 value -= amount_to_subtract;
538 if (value <= 0)
539 value = 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200540 DataManager::SetValue(varName, value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400541 return 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200542 }
Vojtech Bocek85932342013-04-01 22:11:33 +0200543 if (arg.find("*") != string::npos)
544 {
545 string varName = arg.substr(0, arg.find('*'));
546 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
547 int multiply_by = atoi(multiply_by_str.c_str());
548 int value;
549
550 DataManager::GetValue(varName, value);
551 DataManager::SetValue(varName, value*multiply_by);
552 return 0;
553 }
554 if (arg.find("/") != string::npos)
555 {
556 string varName = arg.substr(0, arg.find('/'));
557 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
558 int divide_by = atoi(divide_by_str.c_str());
559 int value;
560
561 if(divide_by != 0)
562 {
563 DataManager::GetValue(varName, value);
564 DataManager::SetValue(varName, value/divide_by);
565 }
566 return 0;
567 }
568 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
569 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400570 }
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500571
Dees_Troy51a0e822012-09-05 15:24:24 -0400572 if (function == "setguitimezone")
573 {
574 string SelectedZone;
575 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
576 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
577 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500578
Dees_Troy51a0e822012-09-05 15:24:24 -0400579 int dst;
580 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500581
Dees_Troy51a0e822012-09-05 15:24:24 -0400582 string offset;
583 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500584
Dees_Troy51a0e822012-09-05 15:24:24 -0400585 string NewTimeZone = Zone;
586 if (offset != "0")
587 NewTimeZone += ":" + offset;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500588
Dees_Troy51a0e822012-09-05 15:24:24 -0400589 if (dst != 0)
590 NewTimeZone += DSTZone;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500591
Dees_Troy51a0e822012-09-05 15:24:24 -0400592 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
Dees_Troy8170a922012-09-18 15:40:25 -0400593 DataManager::update_tz_environment_variables();
Dees_Troy51a0e822012-09-05 15:24:24 -0400594 return 0;
595 }
596
597 if (function == "togglestorage") {
Dees Troyf193f882013-09-11 14:56:20 +0000598 LOGERR("togglestorage action was deprecated from TWRP\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400599 return 0;
600 }
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500601
Dees_Troy51a0e822012-09-05 15:24:24 -0400602 if (function == "overlay")
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200603 return gui_changeOverlay(arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400604
605 if (function == "queuezip")
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200606 {
607 if (zip_queue_index >= 10) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000608 gui_print("Maximum zip queue reached!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400609 return 0;
610 }
611 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
612 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
613 zip_queue_index++;
614 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
615 }
616 return 0;
617 }
618
619 if (function == "cancelzip")
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200620 {
621 if (zip_queue_index <= 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000622 gui_print("Minimum zip queue reached!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400623 return 0;
624 } else {
625 zip_queue_index--;
626 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
627 }
628 return 0;
629 }
630
631 if (function == "queueclear")
632 {
633 zip_queue_index = 0;
634 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
635 return 0;
636 }
637
638 if (function == "sleep")
639 {
640 operation_start("Sleep");
641 usleep(atoi(arg.c_str()));
642 operation_end(0, simulate);
643 return 0;
644 }
645
Dees Troyb21cc642013-09-10 17:36:41 +0000646 if (function == "appenddatetobackupname")
647 {
648 operation_start("AppendDateToBackupName");
649 string Backup_Name;
650 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
651 Backup_Name += TWFunc::Get_Current_Date();
652 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
653 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
654 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
655 operation_end(0, simulate);
656 return 0;
657 }
658
659 if (function == "generatebackupname")
660 {
661 operation_start("GenerateBackupName");
662 TWFunc::Auto_Generate_Backup_Name();
663 operation_end(0, simulate);
664 return 0;
665 }
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500666 if (function == "checkpartitionlist") {
667 string Wipe_List, wipe_path;
668 int count = 0;
669
670 DataManager::GetValue("tw_wipe_list", Wipe_List);
671 LOGINFO("checkpartitionlist list '%s'\n", Wipe_List.c_str());
672 if (!Wipe_List.empty()) {
673 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
674 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
675 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
676 LOGINFO("checkpartitionlist wipe_path '%s'\n", wipe_path.c_str());
677 if (wipe_path == "/and-sec" || wipe_path == "DALVIK" || wipe_path == "INTERNAL") {
678 // Do nothing
679 } else {
680 count++;
681 }
682 start_pos = end_pos + 1;
683 end_pos = Wipe_List.find(";", start_pos);
684 }
685 DataManager::SetValue("tw_check_partition_list", count);
686 } else {
687 DataManager::SetValue("tw_check_partition_list", 0);
688 }
689 return 0;
690 }
691 if (function == "getpartitiondetails") {
692 string Wipe_List, wipe_path;
693 int count = 0;
694
695 DataManager::GetValue("tw_wipe_list", Wipe_List);
696 LOGINFO("getpartitiondetails list '%s'\n", Wipe_List.c_str());
697 if (!Wipe_List.empty()) {
698 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
699 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
700 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
701 LOGINFO("getpartitiondetails wipe_path '%s'\n", wipe_path.c_str());
702 if (wipe_path == "/and-sec" || wipe_path == "DALVIK" || wipe_path == "INTERNAL") {
703 // Do nothing
704 } else {
705 DataManager::SetValue("tw_partition_path", wipe_path);
706 break;
707 }
708 start_pos = end_pos + 1;
709 end_pos = Wipe_List.find(";", start_pos);
710 }
711 if (!wipe_path.empty()) {
712 TWPartition* Part = PartitionManager.Find_Partition_By_Path(wipe_path);
713 if (Part) {
714 unsigned long long mb = 1048576;
715
716 DataManager::SetValue("tw_partition_name", Part->Display_Name);
717 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
718 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
719 DataManager::SetValue("tw_partition_size", Part->Size / mb);
720 DataManager::SetValue("tw_partition_used", Part->Used / mb);
721 DataManager::SetValue("tw_partition_free", Part->Free / mb);
722 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
723 DataManager::SetValue("tw_partition_removable", Part->Removable);
724 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
725
726 if (Part->Can_Repair())
727 DataManager::SetValue("tw_partition_can_repair", 1);
728 else
729 DataManager::SetValue("tw_partition_can_repair", 0);
730 if (TWFunc::Path_Exists("/sbin/mkdosfs"))
731 DataManager::SetValue("tw_partition_vfat", 1);
732 else
733 DataManager::SetValue("tw_partition_vfat", 0);
734 if (TWFunc::Path_Exists("/sbin/mkfs.exfat"))
735 DataManager::SetValue("tw_partition_exfat", 1);
736 else
737 DataManager::SetValue("tw_partition_exfat", 0);
738 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs"))
739 DataManager::SetValue("tw_partition_f2fs", 1);
740 else
741 DataManager::SetValue("tw_partition_f2fs", 0);
742 if (TWFunc::Path_Exists("/sbin/mke2fs"))
743 DataManager::SetValue("tw_partition_ext", 1);
744 else
745 DataManager::SetValue("tw_partition_ext", 0);
746 return 0;
747 } else {
748 LOGERR("Unable to locate partition: '%s'\n", wipe_path.c_str());
749 }
750 }
751 }
752 DataManager::SetValue("tw_partition_name", "");
753 DataManager::SetValue("tw_partition_file_system", "");
754 return 0;
755 }
Dees Troyb21cc642013-09-10 17:36:41 +0000756
Vojtech Bocek03fd6c52014-03-13 18:46:34 +0100757 if (function == "screenshot")
758 {
759 time_t tm;
760 char path[256];
761 int path_len;
762 uid_t uid = -1;
763 gid_t gid = -1;
764
765 struct passwd *pwd = getpwnam("media_rw");
766 if(pwd) {
767 uid = pwd->pw_uid;
768 gid = pwd->pw_gid;
769 }
770
771 const std::string storage = DataManager::GetCurrentStoragePath();
772 if(PartitionManager.Is_Mounted_By_Path(storage)) {
773 snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
774 } else {
775 strcpy(path, "/tmp/");
776 }
777
778 if(!TWFunc::Create_Dir_Recursive(path, 0666, uid, gid))
779 return 0;
780
781 tm = time(NULL);
782 path_len = strlen(path);
783
784 // Screenshot_2014-01-01-18-21-38.png
785 strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
786
787 int res = gr_save_screenshot(path);
788 if(res == 0) {
789 chmod(path, 0666);
790 chown(path, uid, gid);
791
792 gui_print("Screenshot was saved to %s\n", path);
793
794 // blink to notify that the screenshow was taken
795 gr_color(255, 255, 255, 255);
796 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
797 gr_flip();
798 gui_forceRender();
799 } else {
800 LOGERR("Failed to take a screenshot!\n");
801 }
802 return 0;
803 }
804
xNUTxe85f02d2014-07-18 01:30:58 +0200805 if (function == "setbrightness")
806 {
807 return TWFunc::Set_Brightness(arg);
808 }
809
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200810 if (isThreaded)
811 {
812 if (function == "fileexists")
Dees_Troy51a0e822012-09-05 15:24:24 -0400813 {
814 struct stat st;
815 string newpath = arg + "/.";
816
817 operation_start("FileExists");
818 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
819 operation_end(0, simulate);
820 else
821 operation_end(1, simulate);
Dees_Troy83bd4832013-05-04 12:39:56 +0000822 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400823 }
824
825 if (function == "flash")
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200826 {
Dees_Troy657c3092012-09-10 20:32:10 -0400827 int i, ret_val = 0, wipe_cache = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400828
829 for (i=0; i<zip_queue_index; i++) {
830 operation_start("Flashing");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200831 DataManager::SetValue("tw_filename", zip_queue[i]);
832 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
Dees_Troy51a0e822012-09-05 15:24:24 -0400833
Tom Hite5a926722014-09-15 01:31:03 +0000834 TWFunc::SetPerformanceMode(true);
Dees_Troy657c3092012-09-10 20:32:10 -0400835 ret_val = flash_zip(zip_queue[i], arg, simulate, &wipe_cache);
Tom Hite5a926722014-09-15 01:31:03 +0000836 TWFunc::SetPerformanceMode(false);
Dees_Troy51a0e822012-09-05 15:24:24 -0400837 if (ret_val != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000838 gui_print("Error flashing zip '%s'\n", zip_queue[i].c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400839 i = 10; // Error flashing zip - exit queue
840 ret_val = 1;
841 }
842 }
843 zip_queue_index = 0;
844 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
845
Dees_Troy657c3092012-09-10 20:32:10 -0400846 if (wipe_cache)
847 PartitionManager.Wipe_By_Path("/cache");
Vojtech Bocek05534202013-09-11 08:11:56 +0200848
Dees_Troy51a0e822012-09-05 15:24:24 -0400849 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
850 operation_start("ReinjectTWRP");
Dees_Troy2673cec2013-04-02 20:22:16 +0000851 gui_print("Injecting TWRP into boot image...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400852 if (simulate) {
853 simulate_progress_bar();
854 } else {
Dees_Troy06b4fe92012-10-16 11:43:20 -0400855 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
856 if (Boot == NULL || Boot->Current_File_System != "emmc")
Vojtech Bocek05534202013-09-11 08:11:56 +0200857 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Dees_Troy06b4fe92012-10-16 11:43:20 -0400858 else {
859 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +0200860 TWFunc::Exec_Cmd(injectcmd);
Dees_Troy06b4fe92012-10-16 11:43:20 -0400861 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000862 gui_print("TWRP injection complete.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400863 }
864 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400865 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400866 operation_end(ret_val, simulate);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200867 return 0;
868 }
869 if (function == "wipe")
870 {
871 operation_start("Format");
872 DataManager::SetValue("tw_partition", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400873
Dees_Troy38bd7602012-09-14 13:33:53 -0400874 int ret_val = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400875
876 if (simulate) {
877 simulate_progress_bar();
878 } else {
879 if (arg == "data")
Dees_Troy38bd7602012-09-14 13:33:53 -0400880 ret_val = PartitionManager.Factory_Reset();
Dees_Troy51a0e822012-09-05 15:24:24 -0400881 else if (arg == "battery")
Dees_Troy38bd7602012-09-14 13:33:53 -0400882 ret_val = PartitionManager.Wipe_Battery_Stats();
Dees_Troy51a0e822012-09-05 15:24:24 -0400883 else if (arg == "rotate")
Dees_Troy38bd7602012-09-14 13:33:53 -0400884 ret_val = PartitionManager.Wipe_Rotate_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400885 else if (arg == "dalvik")
Dees_Troy38bd7602012-09-14 13:33:53 -0400886 ret_val = PartitionManager.Wipe_Dalvik_Cache();
Dees_Troy51a0e822012-09-05 15:24:24 -0400887 else if (arg == "DATAMEDIA") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400888 ret_val = PartitionManager.Format_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400889 } else if (arg == "INTERNAL") {
890 int has_datamedia, dual_storage;
891
892 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
893 if (has_datamedia) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400894 ret_val = PartitionManager.Wipe_Media_From_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400895 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -0400896 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
Dees_Troy51a0e822012-09-05 15:24:24 -0400897 }
898 } else if (arg == "EXTERNAL") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400899 string External_Path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400900
Dees_Troy38bd7602012-09-14 13:33:53 -0400901 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
902 ret_val = PartitionManager.Wipe_By_Path(External_Path);
Dees_Troy2ff5a8d2012-09-26 14:53:02 -0400903 } else if (arg == "ANDROIDSECURE") {
904 ret_val = PartitionManager.Wipe_Android_Secure();
Dees_Troya13d74f2013-03-24 08:54:55 -0500905 } else if (arg == "LIST") {
906 string Wipe_List, wipe_path;
907 bool skip = false;
908 ret_val = true;
909 TWPartition* wipe_part = NULL;
910
911 DataManager::GetValue("tw_wipe_list", Wipe_List);
Dees_Troy2673cec2013-04-02 20:22:16 +0000912 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500913 if (!Wipe_List.empty()) {
914 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
915 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
916 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
Dees_Troy2673cec2013-04-02 20:22:16 +0000917 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500918 if (wipe_path == "/and-sec") {
919 if (!PartitionManager.Wipe_Android_Secure()) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000920 LOGERR("Unable to wipe android secure\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500921 ret_val = false;
922 break;
923 } else {
924 skip = true;
925 }
926 } else if (wipe_path == "DALVIK") {
927 if (!PartitionManager.Wipe_Dalvik_Cache()) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000928 LOGERR("Failed to wipe dalvik\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500929 ret_val = false;
930 break;
931 } else {
932 skip = true;
933 }
Dees_Troy74fb2e92013-04-15 14:35:47 +0000934 } else if (wipe_path == "INTERNAL") {
935 if (!PartitionManager.Wipe_Media_From_Data()) {
936 ret_val = false;
937 break;
938 } else {
939 skip = true;
940 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500941 }
942 if (!skip) {
943 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000944 LOGERR("Unable to wipe '%s'\n", wipe_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500945 ret_val = false;
946 break;
947 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
948 arg = wipe_path;
949 }
950 } else {
951 skip = false;
952 }
953 start_pos = end_pos + 1;
954 end_pos = Wipe_List.find(";", start_pos);
955 }
956 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400957 } else
958 ret_val = PartitionManager.Wipe_By_Path(arg);
Ethan Yonker83e82572014-04-04 10:59:28 -0500959#ifdef TW_OEM_BUILD
Dees_Troy38bd7602012-09-14 13:33:53 -0400960 if (arg == DataManager::GetSettingsStoragePath()) {
961 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
962 string Storage_Path = DataManager::GetSettingsStoragePath();
963
964 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000965 LOGINFO("Making TWRP folder and saving settings.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400966 Storage_Path += "/TWRP";
967 mkdir(Storage_Path.c_str(), 0777);
968 DataManager::Flush();
969 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000970 LOGERR("Unable to recreate TWRP folder and save settings.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400971 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400972 }
Ethan Yonker83e82572014-04-04 10:59:28 -0500973#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400974 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400975 PartitionManager.Update_System_Details();
Dees_Troy38bd7602012-09-14 13:33:53 -0400976 if (ret_val)
977 ret_val = 0; // 0 is success
978 else
979 ret_val = 1; // 1 is failure
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200980 operation_end(ret_val, simulate);
981 return 0;
982 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400983 if (function == "refreshsizes")
984 {
985 operation_start("Refreshing Sizes");
986 if (simulate) {
987 simulate_progress_bar();
988 } else
Dees_Troy5bf43922012-09-07 16:07:55 -0400989 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400990 operation_end(0, simulate);
Dees_Troy83bd4832013-05-04 12:39:56 +0000991 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400992 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200993 if (function == "nandroid")
994 {
995 operation_start("Nandroid");
Dees_Troy43d8b002012-09-17 16:00:01 -0400996 int ret = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400997
998 if (simulate) {
999 DataManager::SetValue("tw_partition", "Simulation");
1000 simulate_progress_bar();
1001 } else {
1002 if (arg == "backup") {
1003 string Backup_Name;
1004 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees Troyb21cc642013-09-10 17:36:41 +00001005 if (Backup_Name == "(Auto Generate)" || Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(true) == 0) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001006 ret = PartitionManager.Run_Backup();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001007 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001008 else {
1009 operation_end(1, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -04001010 return -1;
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001011
Dees_Troy43d8b002012-09-17 16:00:01 -04001012 }
Dees Troyb21cc642013-09-10 17:36:41 +00001013 DataManager::SetValue(TW_BACKUP_NAME, "(Auto Generate)");
Dees_Troy51a0e822012-09-05 15:24:24 -04001014 } else if (arg == "restore") {
1015 string Restore_Name;
1016 DataManager::GetValue("tw_restore", Restore_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -04001017 ret = PartitionManager.Run_Restore(Restore_Name);
Dees_Troy51a0e822012-09-05 15:24:24 -04001018 } else {
1019 operation_end(1, simulate);
1020 return -1;
1021 }
1022 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001023 DataManager::SetValue("tw_encrypt_backup", 0);
Dees_Troy43d8b002012-09-17 16:00:01 -04001024 if (ret == false)
1025 ret = 1; // 1 for failure
1026 else
1027 ret = 0; // 0 for success
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001028 operation_end(ret, simulate);
Dees_Troy83bd4832013-05-04 12:39:56 +00001029 return 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001030 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001031 if (function == "fixpermissions")
1032 {
1033 operation_start("Fix Permissions");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001034 LOGINFO("fix permissions started!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001035 if (simulate) {
1036 simulate_progress_bar();
Dees_Troy4be841b2012-09-26 14:07:15 -04001037 } else {
Dees_Troy6480ce02012-10-10 10:26:54 -04001038 int op_status = PartitionManager.Fix_Permissions();
1039 if (op_status != 0)
1040 op_status = 1; // failure
1041 operation_end(op_status, simulate);
Dees_Troy4be841b2012-09-26 14:07:15 -04001042 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001043 return 0;
1044 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001045 if (function == "dd")
1046 {
1047 operation_start("imaging");
Dees_Troy51a0e822012-09-05 15:24:24 -04001048
1049 if (simulate) {
1050 simulate_progress_bar();
1051 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001052 string cmd = "dd " + arg;
Vojtech Bocek05534202013-09-11 08:11:56 +02001053 TWFunc::Exec_Cmd(cmd);
Dees_Troy51a0e822012-09-05 15:24:24 -04001054 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001055 operation_end(0, simulate);
1056 return 0;
1057 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001058 if (function == "partitionsd")
1059 {
1060 operation_start("Partition SD Card");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001061 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001062
1063 if (simulate) {
1064 simulate_progress_bar();
1065 } else {
1066 int allow_partition;
1067 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1068 if (allow_partition == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001069 gui_print("This device does not have a real SD Card!\nAborting!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001070 } else {
Dees_Troy9350b8d2012-09-27 12:38:38 -04001071 if (!PartitionManager.Partition_SDCard())
1072 ret_val = 1; // failed
Dees_Troy51a0e822012-09-05 15:24:24 -04001073 }
1074 }
Dees_Troy9350b8d2012-09-27 12:38:38 -04001075 operation_end(ret_val, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -04001076 return 0;
1077 }
1078 if (function == "installhtcdumlock")
1079 {
1080 operation_start("Install HTC Dumlock");
1081 if (simulate) {
1082 simulate_progress_bar();
1083 } else
Dees_Troy38bd7602012-09-14 13:33:53 -04001084 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -04001085
1086 operation_end(0, simulate);
1087 return 0;
1088 }
1089 if (function == "htcdumlockrestoreboot")
1090 {
1091 operation_start("HTC Dumlock Restore Boot");
1092 if (simulate) {
1093 simulate_progress_bar();
1094 } else
Dees_Troy38bd7602012-09-14 13:33:53 -04001095 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001096
1097 operation_end(0, simulate);
1098 return 0;
1099 }
1100 if (function == "htcdumlockreflashrecovery")
1101 {
1102 operation_start("HTC Dumlock Reflash Recovery");
1103 if (simulate) {
1104 simulate_progress_bar();
1105 } else
Dees_Troy38bd7602012-09-14 13:33:53 -04001106 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001107
1108 operation_end(0, simulate);
1109 return 0;
1110 }
1111 if (function == "cmd")
1112 {
1113 int op_status = 0;
1114
1115 operation_start("Command");
Dees_Troy2673cec2013-04-02 20:22:16 +00001116 LOGINFO("Running command: '%s'\n", arg.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001117 if (simulate) {
1118 simulate_progress_bar();
1119 } else {
Vojtech Bocek05534202013-09-11 08:11:56 +02001120 op_status = TWFunc::Exec_Cmd(arg);
Dees_Troy51a0e822012-09-05 15:24:24 -04001121 if (op_status != 0)
1122 op_status = 1;
1123 }
1124
1125 operation_end(op_status, simulate);
1126 return 0;
1127 }
1128 if (function == "terminalcommand")
1129 {
1130 int op_status = 0;
1131 string cmdpath, command;
1132
1133 DataManager::GetValue("tw_terminal_location", cmdpath);
1134 operation_start("CommandOutput");
Dees_Troy2673cec2013-04-02 20:22:16 +00001135 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001136 if (simulate) {
1137 simulate_progress_bar();
1138 operation_end(op_status, simulate);
1139 } else {
Dees_Troy4be841b2012-09-26 14:07:15 -04001140 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
Dees_Troy2673cec2013-04-02 20:22:16 +00001141 LOGINFO("Actual command is: '%s'\n", command.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001142 DataManager::SetValue("tw_terminal_command_thread", command);
1143 DataManager::SetValue("tw_terminal_state", 1);
1144 DataManager::SetValue("tw_background_thread_running", 1);
1145 op_status = pthread_create(&terminal_command, NULL, command_thread, NULL);
1146 if (op_status != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001147 LOGERR("Error starting terminal command thread, %i.\n", op_status);
Dees_Troy51a0e822012-09-05 15:24:24 -04001148 DataManager::SetValue("tw_terminal_state", 0);
1149 DataManager::SetValue("tw_background_thread_running", 0);
1150 operation_end(1, simulate);
1151 }
1152 }
1153 return 0;
1154 }
1155 if (function == "killterminal")
1156 {
1157 int op_status = 0;
1158
Dees_Troy2673cec2013-04-02 20:22:16 +00001159 LOGINFO("Sending kill command...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001160 operation_start("KillCommand");
1161 DataManager::SetValue("tw_operation_status", 0);
1162 DataManager::SetValue("tw_operation_state", 1);
1163 DataManager::SetValue("tw_terminal_state", 0);
1164 DataManager::SetValue("tw_background_thread_running", 0);
1165 DataManager::SetValue(TW_ACTION_BUSY, 0);
1166 return 0;
1167 }
1168 if (function == "reinjecttwrp")
1169 {
1170 int op_status = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001171 operation_start("ReinjectTWRP");
Dees_Troy2673cec2013-04-02 20:22:16 +00001172 gui_print("Injecting TWRP into boot image...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001173 if (simulate) {
1174 simulate_progress_bar();
1175 } else {
Vojtech Bocek05534202013-09-11 08:11:56 +02001176 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Dees_Troy2673cec2013-04-02 20:22:16 +00001177 gui_print("TWRP injection complete.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001178 }
1179
1180 operation_end(op_status, simulate);
1181 return 0;
1182 }
1183 if (function == "checkbackupname")
1184 {
1185 int op_status = 0;
1186
1187 operation_start("CheckBackupName");
1188 if (simulate) {
1189 simulate_progress_bar();
1190 } else {
Dees_Troyc9ff7a32012-09-27 10:09:41 -04001191 op_status = PartitionManager.Check_Backup_Name(true);
Dees_Troy51a0e822012-09-05 15:24:24 -04001192 if (op_status != 0)
1193 op_status = 1;
1194 }
1195
1196 operation_end(op_status, simulate);
1197 return 0;
1198 }
1199 if (function == "decrypt")
1200 {
1201 int op_status = 0;
1202
1203 operation_start("Decrypt");
1204 if (simulate) {
1205 simulate_progress_bar();
1206 } else {
1207 string Password;
1208 DataManager::GetValue("tw_crypto_password", Password);
Dees_Troy5bf43922012-09-07 16:07:55 -04001209 op_status = PartitionManager.Decrypt_Device(Password);
Dees_Troy51a0e822012-09-05 15:24:24 -04001210 if (op_status != 0)
1211 op_status = 1;
1212 else {
1213 int load_theme = 1;
1214
1215 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001216
1217 if (load_theme) {
1218 int has_datamedia;
1219
1220 // Check for a custom theme and load it if exists
1221 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1222 if (has_datamedia != 0) {
1223 struct stat st;
1224 int check = 0;
1225 std::string theme_path;
1226
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06001227 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
1228 LOGERR("Failed to get default contexts and file mode for storage files.\n");
1229 } else {
1230 LOGINFO("Got default contexts and file mode for storage files.\n");
1231 }
1232
Dees_Troy51a0e822012-09-05 15:24:24 -04001233 theme_path = DataManager::GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -04001234 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001235 LOGERR("Unable to mount %s during reload function startup.\n", theme_path.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001236 check = 1;
1237 }
1238
1239 theme_path += "/TWRP/theme/ui.zip";
1240 if (check == 0 && stat(theme_path.c_str(), &st) == 0) {
1241 if (PageManager::ReloadPackage("TWRP", theme_path) != 0)
1242 {
1243 // Loading the custom theme failed - try loading the stock theme
Dees_Troy2673cec2013-04-02 20:22:16 +00001244 LOGINFO("Attempting to reload stock theme...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001245 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
1246 {
Dees_Troy2673cec2013-04-02 20:22:16 +00001247 LOGERR("Failed to load base packages.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001248 }
1249 }
1250 }
1251 }
1252 }
1253 }
1254 }
1255
1256 operation_end(op_status, simulate);
1257 return 0;
1258 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001259 if (function == "adbsideload")
1260 {
1261 int ret = 0;
1262
1263 operation_start("Sideload");
1264 if (simulate) {
1265 simulate_progress_bar();
1266 } else {
1267 int wipe_cache = 0;
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001268 int wipe_dalvik = 0;
Dees_Troycfb63ae2012-09-19 14:30:17 -04001269
Dees_Troy2673cec2013-04-02 20:22:16 +00001270 gui_print("Starting ADB sideload feature...\n");
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001271 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
Ethan Yonker24813422014-11-07 17:19:07 -06001272 ret = apply_from_adb("/");
Dees_Troy2673cec2013-04-02 20:22:16 +00001273 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
Ethan Yonker24813422014-11-07 17:19:07 -06001274 char file_prop[PROPERTY_VALUE_MAX];
1275 property_get("tw_sideload_file", file_prop, "error");
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001276 if (ret != 0) {
Dees_Troycfb63ae2012-09-19 14:30:17 -04001277 ret = 1; // failure
Ethan Yonker24813422014-11-07 17:19:07 -06001278 if (ret == -2)
1279 gui_print("You need adb 1.0.32 or newer to sideload to this device.\n");
Dees_Troy2673cec2013-04-02 20:22:16 +00001280 } else {
Ethan Yonker24813422014-11-07 17:19:07 -06001281 if (TWinstall_zip(file_prop, &wipe_cache) == 0) {
1282 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1283 PartitionManager.Wipe_By_Path("/cache");
1284 if (wipe_dalvik)
1285 PartitionManager.Wipe_Dalvik_Cache();
1286 } else {
1287 ret = 1; // failure
1288 }
1289 set_usb_driver(false);
1290 maybe_restart_adbd();
1291 }
1292 if (strcmp(file_prop, "error") != 0) {
1293 struct stat st;
1294 stat("/sideload/exit", &st);
1295 int child_pid, status;
1296 char child_prop[PROPERTY_VALUE_MAX];
1297 property_get("tw_child_pid", child_prop, "error");
1298 if (strcmp(child_prop, "error") == 0) {
1299 LOGERR("Unable to get child ID from prop\n");
1300 } else {
1301 child_pid = atoi(child_prop);
1302 LOGINFO("Waiting for child sideload process to exit.\n");
1303 waitpid(child_pid, &status, 0);
1304 }
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001305 }
Dees_Troy06b4fe92012-10-16 11:43:20 -04001306 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
1307 operation_start("ReinjectTWRP");
Dees_Troy2673cec2013-04-02 20:22:16 +00001308 gui_print("Injecting TWRP into boot image...\n");
Dees_Troy06b4fe92012-10-16 11:43:20 -04001309 if (simulate) {
1310 simulate_progress_bar();
1311 } else {
1312 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
1313 if (Boot == NULL || Boot->Current_File_System != "emmc")
Vojtech Bocek05534202013-09-11 08:11:56 +02001314 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Dees_Troy06b4fe92012-10-16 11:43:20 -04001315 else {
1316 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001317 TWFunc::Exec_Cmd(injectcmd);
Dees_Troy06b4fe92012-10-16 11:43:20 -04001318 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001319 gui_print("TWRP injection complete.\n");
Dees_Troy06b4fe92012-10-16 11:43:20 -04001320 }
1321 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001322 }
1323 operation_end(ret, simulate);
1324 return 0;
1325 }
Dees_Troycfb63ae2012-09-19 14:30:17 -04001326 if (function == "adbsideloadcancel")
1327 {
Ethan Yonker24813422014-11-07 17:19:07 -06001328 int child_pid, status;
Dees_Troy2673cec2013-04-02 20:22:16 +00001329 char child_prop[PROPERTY_VALUE_MAX];
Ethan Yonker24813422014-11-07 17:19:07 -06001330 struct stat st;
1331 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
1332 gui_print("Cancelling ADB sideload...\n");
1333 stat("/sideload/exit", &st);
1334 sleep(1);
Dees_Troy2673cec2013-04-02 20:22:16 +00001335 property_get("tw_child_pid", child_prop, "error");
1336 if (strcmp(child_prop, "error") == 0) {
1337 LOGERR("Unable to get child ID from prop\n");
1338 return 0;
1339 }
1340 child_pid = atoi(child_prop);
Dees_Troycfb63ae2012-09-19 14:30:17 -04001341 kill(child_pid, SIGTERM);
Dees_Troy4bc09ae2013-01-18 17:00:54 +00001342 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
Dees_Troycfb63ae2012-09-19 14:30:17 -04001343 return 0;
1344 }
Dees_Troy6ed34b72013-01-25 15:01:29 +00001345 if (function == "openrecoveryscript") {
1346 operation_start("OpenRecoveryScript");
1347 if (simulate) {
1348 simulate_progress_bar();
1349 } else {
1350 // Check for the SCRIPT_FILE_TMP first as these are AOSP recovery commands
1351 // that we converted to ORS commands during boot in recovery.cpp.
1352 // Run those first.
1353 int reboot = 0;
1354 if (TWFunc::Path_Exists(SCRIPT_FILE_TMP)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001355 gui_print("Processing AOSP recovery commands...\n");
Dees_Troy6ed34b72013-01-25 15:01:29 +00001356 if (OpenRecoveryScript::run_script_file() == 0) {
1357 reboot = 1;
1358 }
1359 }
1360 // Check for the ORS file in /cache and attempt to run those commands.
1361 if (OpenRecoveryScript::check_for_script_file()) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001362 gui_print("Processing OpenRecoveryScript file...\n");
Dees_Troy6ed34b72013-01-25 15:01:29 +00001363 if (OpenRecoveryScript::run_script_file() == 0) {
1364 reboot = 1;
1365 }
1366 }
1367 if (reboot) {
1368 usleep(2000000); // Sleep for 2 seconds before rebooting
1369 TWFunc::tw_reboot(rb_system);
1370 } else {
1371 DataManager::SetValue("tw_page_done", 1);
1372 }
1373 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001374 return 0;
Dees_Troy6ed34b72013-01-25 15:01:29 +00001375 }
Dees_Troy6ef66352013-02-21 08:26:57 -06001376 if (function == "installsu")
1377 {
1378 int op_status = 0;
1379
1380 operation_start("Install SuperSU");
1381 if (simulate) {
1382 simulate_progress_bar();
1383 } else {
1384 if (!TWFunc::Install_SuperSU())
1385 op_status = 1;
1386 }
1387
1388 operation_end(op_status, simulate);
1389 return 0;
1390 }
1391 if (function == "fixsu")
1392 {
1393 int op_status = 0;
1394
1395 operation_start("Fixing Superuser Permissions");
1396 if (simulate) {
1397 simulate_progress_bar();
1398 } else {
Dees Troyf193f882013-09-11 14:56:20 +00001399 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1400 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
Dees_Troy6ef66352013-02-21 08:26:57 -06001401 }
1402
1403 operation_end(op_status, simulate);
1404 return 0;
1405 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001406 if (function == "decrypt_backup")
1407 {
1408 int op_status = 0;
1409
1410 operation_start("Try Restore Decrypt");
1411 if (simulate) {
1412 simulate_progress_bar();
1413 } else {
1414 string Restore_Path, Filename, Password;
1415 DataManager::GetValue("tw_restore", Restore_Path);
1416 Restore_Path += "/";
1417 DataManager::GetValue("tw_restore_password", Password);
Tom Hite5a926722014-09-15 01:31:03 +00001418 TWFunc::SetPerformanceMode(true);
Dees_Troy83bd4832013-05-04 12:39:56 +00001419 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1420 op_status = 0; // success
1421 else
1422 op_status = 1; // fail
Tom Hite5a926722014-09-15 01:31:03 +00001423 TWFunc::SetPerformanceMode(false);
Dees_Troy83bd4832013-05-04 12:39:56 +00001424 }
1425
1426 operation_end(op_status, simulate);
1427 return 0;
1428 }
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001429 if (function == "repair")
1430 {
1431 int op_status = 0;
1432
1433 operation_start("Repair Partition");
1434 if (simulate) {
1435 simulate_progress_bar();
1436 } else {
1437 string part_path;
1438 DataManager::GetValue("tw_partition_mount_point", part_path);
1439 if (PartitionManager.Repair_By_Path(part_path, true)) {
1440 op_status = 0; // success
1441 } else {
1442 LOGERR("Error repairing file system.\n");
1443 op_status = 1; // fail
1444 }
1445 }
1446
1447 operation_end(op_status, simulate);
1448 return 0;
1449 }
1450 if (function == "changefilesystem")
1451 {
1452 int op_status = 0;
1453
1454 operation_start("Change File System");
1455 if (simulate) {
1456 simulate_progress_bar();
1457 } else {
1458 string part_path, file_system;
1459 DataManager::GetValue("tw_partition_mount_point", part_path);
1460 DataManager::GetValue("tw_action_new_file_system", file_system);
1461 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1462 op_status = 0; // success
1463 } else {
1464 LOGERR("Error changing file system.\n");
1465 op_status = 1; // fail
1466 }
1467 }
jrior001aad03112014-07-05 10:31:21 -04001468 PartitionManager.Update_System_Details();
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001469 operation_end(op_status, simulate);
1470 return 0;
1471 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001472 if (function == "startmtp")
1473 {
1474 int op_status = 0;
1475
1476 operation_start("Start MTP");
1477 if (PartitionManager.Enable_MTP())
1478 op_status = 0; // success
1479 else
1480 op_status = 1; // fail
1481
1482 operation_end(op_status, simulate);
1483 return 0;
1484 }
1485 if (function == "stopmtp")
1486 {
1487 int op_status = 0;
1488
1489 operation_start("Stop MTP");
1490 if (PartitionManager.Disable_MTP())
1491 op_status = 0; // success
1492 else
1493 op_status = 1; // fail
1494
1495 operation_end(op_status, simulate);
1496 return 0;
1497 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001498 }
1499 else
1500 {
Dees_Troy83bd4832013-05-04 12:39:56 +00001501 pthread_t t;
1502 pthread_create(&t, NULL, thread_start, this);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001503 return 0;
1504 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001505 LOGERR("Unknown action '%s'\n", function.c_str());
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001506 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -04001507}
1508
1509int GUIAction::getKeyByName(std::string key)
1510{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001511 if (key == "home") return KEY_HOME;
1512 else if (key == "menu") return KEY_MENU;
1513 else if (key == "back") return KEY_BACK;
1514 else if (key == "search") return KEY_SEARCH;
1515 else if (key == "voldown") return KEY_VOLUMEDOWN;
1516 else if (key == "volup") return KEY_VOLUMEUP;
1517 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001518 int ret_val;
1519 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1520 if (!ret_val)
1521 return KEY_POWER;
1522 else
1523 return ret_val;
1524 }
1525
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001526 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001527}
1528
1529void* GUIAction::command_thread(void *cookie)
1530{
1531 string command;
1532 FILE* fp;
1533 char line[512];
1534
1535 DataManager::GetValue("tw_terminal_command_thread", command);
Dees_Troy8170a922012-09-18 15:40:25 -04001536 fp = popen(command.c_str(), "r");
Dees_Troy51a0e822012-09-05 15:24:24 -04001537 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001538 LOGERR("Error opening command to run.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001539 } else {
1540 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1541 struct timeval timeout;
1542 fd_set fdset;
1543
1544 while(keep_going)
1545 {
1546 FD_ZERO(&fdset);
1547 FD_SET(fd, &fdset);
1548 timeout.tv_sec = 0;
1549 timeout.tv_usec = 400000;
1550 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1551 if (has_data == 0) {
1552 // Timeout reached
1553 DataManager::GetValue("tw_terminal_state", check);
1554 if (check == 0) {
1555 keep_going = 0;
1556 }
1557 } else if (has_data < 0) {
1558 // End of execution
1559 keep_going = 0;
1560 } else {
1561 // Try to read output
Dees_Troy4be841b2012-09-26 14:07:15 -04001562 memset(line, 0, sizeof(line));
Dees_Troy51a0e822012-09-05 15:24:24 -04001563 bytes_read = read(fd, line, sizeof(line));
1564 if (bytes_read > 0)
Dees_Troy2673cec2013-04-02 20:22:16 +00001565 gui_print("%s", line); // Display output
Dees_Troy51a0e822012-09-05 15:24:24 -04001566 else
1567 keep_going = 0; // Done executing
1568 }
1569 }
1570 fclose(fp);
1571 }
1572 DataManager::SetValue("tw_operation_status", 0);
1573 DataManager::SetValue("tw_operation_state", 1);
1574 DataManager::SetValue("tw_terminal_state", 0);
1575 DataManager::SetValue("tw_background_thread_running", 0);
1576 DataManager::SetValue(TW_ACTION_BUSY, 0);
1577 return NULL;
1578}