blob: 5536c82b10bddf68fbb56c23087c5b3581f95654 [file] [log] [blame]
Dees_Troya13d74f2013-03-24 08:54:55 -05001/*
2 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>
Dees_Troy51a0e822012-09-05 15:24:24 -040035
36#include <string>
37#include <sstream>
38#include "../partitions.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040039#include "../twrp-functions.hpp"
Dees_Troy812660f2012-09-20 09:55:17 -040040#include "../openrecoveryscript.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040041
Dees_Troy43d8b002012-09-17 16:00:01 -040042#include "../adb_install.h"
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070043#ifndef TW_NO_SCREEN_TIMEOUT
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050044#include "blanktimer.hpp"
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070045#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040046extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000047#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040048#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040049#include "../variables.h"
Dees_Troy32c8eb82012-09-11 15:28:06 -040050#include "../twinstall.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000051#include "cutils/properties.h"
Dees_Troy43d8b002012-09-17 16:00:01 -040052#include "../minadbd/adb.h"
53
Dees_Troy32c8eb82012-09-11 15:28:06 -040054int TWinstall_zip(const char* path, int* wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -040055void 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 -040056int gui_console_only();
Dees_Troy51a0e822012-09-05 15:24:24 -040057int gui_start();
58};
59
60#include "rapidxml.hpp"
61#include "objects.hpp"
62
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070063#ifndef TW_NO_SCREEN_TIMEOUT
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050064extern blanktimer blankTimer;
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070065#endif
Dees_Troy43d8b002012-09-17 16:00:01 -040066
Dees_Troy51a0e822012-09-05 15:24:24 -040067void curtainClose(void);
68
69GUIAction::GUIAction(xml_node<>* node)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020070 : Conditional(node)
Dees_Troy51a0e822012-09-05 15:24:24 -040071{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020072 xml_node<>* child;
73 xml_node<>* actions;
74 xml_attribute<>* attr;
Dees_Troy51a0e822012-09-05 15:24:24 -040075
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020076 mKey = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -040077
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020078 if (!node) return;
Dees_Troy51a0e822012-09-05 15:24:24 -040079
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020080 // First, get the action
81 actions = node->first_node("actions");
82 if (actions) child = actions->first_node("action");
83 else child = node->first_node("action");
Dees_Troy51a0e822012-09-05 15:24:24 -040084
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020085 if (!child) return;
Dees_Troy51a0e822012-09-05 15:24:24 -040086
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020087 while (child)
88 {
89 Action action;
Dees_Troy51a0e822012-09-05 15:24:24 -040090
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020091 attr = child->first_attribute("function");
92 if (!attr) return;
93
94 action.mFunction = attr->value();
95 action.mArg = child->value();
96 mActions.push_back(action);
Dees_Troy51a0e822012-09-05 15:24:24 -040097
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020098 child = child->next_sibling("action");
99 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400100
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200101 // Now, let's get either the key or region
102 child = node->first_node("touch");
103 if (child)
104 {
105 attr = child->first_attribute("key");
106 if (attr)
107 {
108 std::string key = attr->value();
109
110 mKey = getKeyByName(key);
111 }
112 else
113 {
114 attr = child->first_attribute("x");
115 if (!attr) return;
116 mActionX = atol(attr->value());
117 attr = child->first_attribute("y");
118 if (!attr) return;
119 mActionY = atol(attr->value());
120 attr = child->first_attribute("w");
121 if (!attr) return;
122 mActionW = atol(attr->value());
123 attr = child->first_attribute("h");
124 if (!attr) return;
125 mActionH = atol(attr->value());
126 }
127 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400128}
129
130int GUIAction::NotifyTouch(TOUCH_STATE state, int x, int y)
131{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200132 if (state == TOUCH_RELEASE)
133 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400134
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200135 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400136}
137
138int GUIAction::NotifyKey(int key)
139{
140 if (!mKey || key != mKey)
141 return 1;
142
143 doActions();
144 return 0;
145}
146
147int GUIAction::NotifyVarChange(std::string varName, std::string value)
148{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200149 if (varName.empty() && !isConditionValid() && !mKey && !mActionW)
150 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400151
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200152 // This handles notifying the condition system of page start
153 if (varName.empty() && isConditionValid())
154 NotifyPageSet();
Dees_Troy51a0e822012-09-05 15:24:24 -0400155
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200156 if ((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
157 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400158
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200159 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400160}
161
162void GUIAction::simulate_progress_bar(void)
163{
Dees_Troy2673cec2013-04-02 20:22:16 +0000164 gui_print("Simulating actions...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400165 for (int i = 0; i < 5; i++)
166 {
167 usleep(500000);
168 DataManager::SetValue("ui_progress", i * 20);
169 }
170}
171
Dees_Troy657c3092012-09-10 20:32:10 -0400172int GUIAction::flash_zip(std::string filename, std::string pageName, const int simulate, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400173{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200174 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400175
176 DataManager::SetValue("ui_progress", 0);
177
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200178 if (filename.empty())
179 {
180 LOGERR("No file specified.\n");
181 return -1;
182 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400183
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200184 // We're going to jump to this page first, like a loading page
185 gui_changePage(pageName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400186
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200187 int fd = -1;
188 ZipArchive zip;
Dees_Troy51a0e822012-09-05 15:24:24 -0400189
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200190 if (!PartitionManager.Mount_By_Path(filename, true))
Dees_Troy657c3092012-09-10 20:32:10 -0400191 return -1;
192
193 if (mzOpenZipArchive(filename.c_str(), &zip))
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200194 {
195 LOGERR("Unable to open zip file.\n");
196 return -1;
197 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400198
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200199 // Check the zip to see if it has a custom installer theme
Dees_Troy51a0e822012-09-05 15:24:24 -0400200 const ZipEntry* twrp = mzFindZipEntry(&zip, "META-INF/teamwin/twrp.zip");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200201 if (twrp != NULL)
202 {
203 unlink("/tmp/twrp.zip");
204 fd = creat("/tmp/twrp.zip", 0666);
205 }
206 if (fd >= 0 && twrp != NULL &&
207 mzExtractZipEntryToFile(&zip, twrp, fd) &&
208 !PageManager::LoadPackage("install", "/tmp/twrp.zip", "main"))
209 {
210 mzCloseZipArchive(&zip);
Dees_Troy657c3092012-09-10 20:32:10 -0400211 PageManager::SelectPackage("install");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200212 gui_changePage("main");
213 }
214 else
215 {
216 // In this case, we just use the default page
217 mzCloseZipArchive(&zip);
Dees_Troy657c3092012-09-10 20:32:10 -0400218 gui_changePage(pageName);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200219 }
220 if (fd >= 0)
221 close(fd);
Dees_Troy51a0e822012-09-05 15:24:24 -0400222
223 if (simulate) {
224 simulate_progress_bar();
225 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400226 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400227
228 // Now, check if we need to ensure TWRP remains installed...
229 struct stat st;
230 if (stat("/sbin/installTwrp", &st) == 0)
231 {
232 DataManager::SetValue("tw_operation", "Configuring TWRP");
233 DataManager::SetValue("tw_partition", "");
Dees_Troy2673cec2013-04-02 20:22:16 +0000234 gui_print("Configuring TWRP...\n");
Vojtech Bocek05534202013-09-11 08:11:56 +0200235 if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall") < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400236 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000237 gui_print("Unable to configure TWRP with this kernel.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400238 }
239 }
240 }
241
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200242 // Done
243 DataManager::SetValue("ui_progress", 100);
244 DataManager::SetValue("ui_progress", 0);
245 return ret_val;
Dees_Troy51a0e822012-09-05 15:24:24 -0400246}
247
248int GUIAction::doActions()
249{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200250 if (mActions.size() < 1) return -1;
251 if (mActions.size() == 1)
Dees_Troy51a0e822012-09-05 15:24:24 -0400252 return doAction(mActions.at(0), 0);
253
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200254 // For multi-action, we always use a thread
255 pthread_t t;
Dees_Troyab4963c2013-01-16 20:35:51 +0000256 pthread_attr_t tattr;
257
258 if (pthread_attr_init(&tattr)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000259 LOGERR("Unable to pthread_attr_init\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000260 return -1;
261 }
262 if (pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_JOINABLE)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000263 LOGERR("Error setting pthread_attr_setdetachstate\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000264 return -1;
265 }
266 if (pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000267 LOGERR("Error setting pthread_attr_setscope\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000268 return -1;
269 }
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500270 /*if (pthread_attr_setstacksize(&tattr, 524288)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000271 LOGERR("Error setting pthread_attr_setstacksize\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000272 return -1;
273 }
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500274 */
Dees_Troyab4963c2013-01-16 20:35:51 +0000275 int ret = pthread_create(&t, &tattr, thread_start, this);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200276 if (ret) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000277 LOGERR("Unable to create more threads for actions... continuing in same thread! %i\n", ret);
Dees_Troyab4963c2013-01-16 20:35:51 +0000278 thread_start(this);
279 } else {
280 if (pthread_join(t, NULL)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000281 LOGERR("Error joining threads\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000282 }
283 }
284 if (pthread_attr_destroy(&tattr)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000285 LOGERR("Failed to pthread_attr_destroy\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000286 return -1;
287 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400288
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200289 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400290}
291
292void* GUIAction::thread_start(void *cookie)
293{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200294 GUIAction* ourThis = (GUIAction*) cookie;
Dees_Troy51a0e822012-09-05 15:24:24 -0400295
296 DataManager::SetValue(TW_ACTION_BUSY, 1);
297
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200298 if (ourThis->mActions.size() > 1)
299 {
300 std::vector<Action>::iterator iter;
301 for (iter = ourThis->mActions.begin(); iter != ourThis->mActions.end(); iter++)
302 ourThis->doAction(*iter, 1);
303 }
304 else
305 {
306 ourThis->doAction(ourThis->mActions.at(0), 1);
307 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400308 int check = 0;
309 DataManager::GetValue("tw_background_thread_running", check);
310 if (check == 0)
311 DataManager::SetValue(TW_ACTION_BUSY, 0);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200312 return NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -0400313}
314
315void GUIAction::operation_start(const string operation_name)
316{
317 DataManager::SetValue(TW_ACTION_BUSY, 1);
318 DataManager::SetValue("ui_progress", 0);
319 DataManager::SetValue("tw_operation", operation_name);
320 DataManager::SetValue("tw_operation_status", 0);
321 DataManager::SetValue("tw_operation_state", 0);
322}
323
324void GUIAction::operation_end(const int operation_status, const int simulate)
325{
326 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400327 DataManager::SetValue("ui_progress", 100);
328 if (simulate) {
329 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
330 if (simulate_fail != 0)
331 DataManager::SetValue("tw_operation_status", 1);
332 else
333 DataManager::SetValue("tw_operation_status", 0);
334 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500335 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400336 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500337 }
338 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400339 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500340 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400341 }
342 DataManager::SetValue("tw_operation_state", 1);
343 DataManager::SetValue(TW_ACTION_BUSY, 0);
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700344#ifndef TW_NO_SCREEN_TIMEOUT
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500345 blankTimer.resetTimerAndUnblank();
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700346#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400347}
348
349int GUIAction::doAction(Action action, int isThreaded /* = 0 */)
350{
351 static string zip_queue[10];
352 static int zip_queue_index;
353 static pthread_t terminal_command;
354 int simulate;
355
356 std::string arg = gui_parse_text(action.mArg);
357
358 std::string function = gui_parse_text(action.mFunction);
359
360 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
361
Dees_Troy6ef66352013-02-21 08:26:57 -0600362 if (function == "reboot")
363 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200364 //curtainClose(); this sometimes causes a crash
Dees_Troy51a0e822012-09-05 15:24:24 -0400365
Dees_Troy6ef66352013-02-21 08:26:57 -0600366 sync();
367 DataManager::SetValue("tw_gui_done", 1);
368 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400369
Dees_Troy6ef66352013-02-21 08:26:57 -0600370 return 0;
371 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200372 if (function == "home")
373 {
374 PageManager::SelectPackage("TWRP");
375 gui_changePage("main");
376 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400377 }
378
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200379 if (function == "key")
380 {
381 PageManager::NotifyKey(getKeyByName(arg));
382 return 0;
383 }
384
385 if (function == "page") {
386 std::string page_name = gui_parse_text(arg);
387 return gui_changePage(page_name);
388 }
389
390 if (function == "reload") {
Dees_Troy51a0e822012-09-05 15:24:24 -0400391 int check = 0, ret_val = 0;
392 std::string theme_path;
393
394 operation_start("Reload Theme");
395 theme_path = DataManager::GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -0400396 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000397 LOGERR("Unable to mount %s during reload function startup.\n", theme_path.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400398 check = 1;
399 }
400
401 theme_path += "/TWRP/theme/ui.zip";
402 if (check != 0 || PageManager::ReloadPackage("TWRP", theme_path) != 0)
403 {
404 // Loading the custom theme failed - try loading the stock theme
Dees_Troy2673cec2013-04-02 20:22:16 +0000405 LOGINFO("Attempting to reload stock theme...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400406 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
407 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000408 LOGERR("Failed to load base packages.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400409 ret_val = 1;
410 }
411 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200412 operation_end(ret_val, simulate);
Dees_Troy83bd4832013-05-04 12:39:56 +0000413 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400414 }
415
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200416 if (function == "readBackup")
417 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400418 string Restore_Name;
419 DataManager::GetValue("tw_restore", Restore_Name);
Dees_Troy5bf43922012-09-07 16:07:55 -0400420 PartitionManager.Set_Restore_Files(Restore_Name);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200421 return 0;
422 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400423
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200424 if (function == "set")
425 {
426 if (arg.find('=') != string::npos)
427 {
428 string varName = arg.substr(0, arg.find('='));
429 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400430
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200431 DataManager::GetValue(value, value);
432 DataManager::SetValue(varName, value);
433 }
434 else
435 DataManager::SetValue(arg, "1");
436 return 0;
437 }
438 if (function == "clear")
439 {
440 DataManager::SetValue(arg, "0");
441 return 0;
442 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400443
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200444 if (function == "mount")
445 {
446 if (arg == "usb")
447 {
448 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400449 if (!simulate)
Dees_Troy8170a922012-09-18 15:40:25 -0400450 PartitionManager.usb_storage_enable();
Dees_Troy51a0e822012-09-05 15:24:24 -0400451 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000452 gui_print("Simulating actions...\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200453 }
454 else if (!simulate)
455 {
456 string cmd;
Dees_Troy51a0e822012-09-05 15:24:24 -0400457 if (arg == "EXTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400458 PartitionManager.Mount_By_Path(DataManager::GetStrValue(TW_EXTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400459 else if (arg == "INTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400460 PartitionManager.Mount_By_Path(DataManager::GetStrValue(TW_INTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400461 else
Dees_Troy51127312012-09-08 13:08:49 -0400462 PartitionManager.Mount_By_Path(arg, true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200463 } else
Dees_Troy2673cec2013-04-02 20:22:16 +0000464 gui_print("Simulating actions...\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200465 return 0;
466 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400467
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200468 if (function == "umount" || function == "unmount")
469 {
470 if (arg == "usb")
471 {
472 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);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200477 }
478 else if (!simulate)
479 {
480 string cmd;
Dees_Troy51a0e822012-09-05 15:24:24 -0400481 if (arg == "EXTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400482 PartitionManager.UnMount_By_Path(DataManager::GetStrValue(TW_EXTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400483 else if (arg == "INTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400484 PartitionManager.UnMount_By_Path(DataManager::GetStrValue(TW_INTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400485 else
Dees_Troy51127312012-09-08 13:08:49 -0400486 PartitionManager.UnMount_By_Path(arg, true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200487 } else
Dees_Troy2673cec2013-04-02 20:22:16 +0000488 gui_print("Simulating actions...\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200489 return 0;
490 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400491
492 if (function == "restoredefaultsettings")
493 {
494 operation_start("Restore Defaults");
495 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
Dees_Troy2673cec2013-04-02 20:22:16 +0000496 gui_print("Simulating actions...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400497 else {
498 DataManager::ResetDefaults();
Dees_Troy5bf43922012-09-07 16:07:55 -0400499 PartitionManager.Update_System_Details();
500 PartitionManager.Mount_Current_Storage(true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400501 }
502 operation_end(0, simulate);
Dees_Troy83bd4832013-05-04 12:39:56 +0000503 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400504 }
505
506 if (function == "copylog")
507 {
508 operation_start("Copy Log");
509 if (!simulate)
510 {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500511 string dst;
Dees_Troy5bf43922012-09-07 16:07:55 -0400512 PartitionManager.Mount_Current_Storage(true);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500513 dst = DataManager::GetCurrentStoragePath() + "/recovery.log";
514 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
Dees_Troy51a0e822012-09-05 15:24:24 -0400515 sync();
Dees_Troy2673cec2013-04-02 20:22:16 +0000516 gui_print("Copied recovery log to %s.\n", DataManager::GetCurrentStoragePath().c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400517 } else
518 simulate_progress_bar();
519 operation_end(0, simulate);
520 return 0;
521 }
522
523 if (function == "compute" || function == "addsubtract")
524 {
525 if (arg.find("+") != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200526 {
527 string varName = arg.substr(0, arg.find('+'));
528 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400529 int amount_to_add = atoi(string_to_add.c_str());
530 int value;
531
532 DataManager::GetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200533 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400534 return 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200535 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400536 if (arg.find("-") != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200537 {
538 string varName = arg.substr(0, arg.find('-'));
539 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400540 int amount_to_subtract = atoi(string_to_subtract.c_str());
541 int value;
542
543 DataManager::GetValue(varName, value);
544 value -= amount_to_subtract;
545 if (value <= 0)
546 value = 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200547 DataManager::SetValue(varName, value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400548 return 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200549 }
Vojtech Bocek85932342013-04-01 22:11:33 +0200550 if (arg.find("*") != string::npos)
551 {
552 string varName = arg.substr(0, arg.find('*'));
553 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
554 int multiply_by = atoi(multiply_by_str.c_str());
555 int value;
556
557 DataManager::GetValue(varName, value);
558 DataManager::SetValue(varName, value*multiply_by);
559 return 0;
560 }
561 if (arg.find("/") != string::npos)
562 {
563 string varName = arg.substr(0, arg.find('/'));
564 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
565 int divide_by = atoi(divide_by_str.c_str());
566 int value;
567
568 if(divide_by != 0)
569 {
570 DataManager::GetValue(varName, value);
571 DataManager::SetValue(varName, value/divide_by);
572 }
573 return 0;
574 }
575 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
576 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400577 }
578
579 if (function == "setguitimezone")
580 {
581 string SelectedZone;
582 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
583 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
584 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
585
586 int dst;
587 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
588
589 string offset;
590 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
591
592 string NewTimeZone = Zone;
593 if (offset != "0")
594 NewTimeZone += ":" + offset;
595
596 if (dst != 0)
597 NewTimeZone += DSTZone;
598
599 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
Dees_Troy8170a922012-09-18 15:40:25 -0400600 DataManager::update_tz_environment_variables();
Dees_Troy51a0e822012-09-05 15:24:24 -0400601 return 0;
602 }
603
604 if (function == "togglestorage") {
Dees Troyf193f882013-09-11 14:56:20 +0000605 LOGERR("togglestorage action was deprecated from TWRP\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400606 if (arg == "internal") {
607 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
608 } else if (arg == "external") {
609 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1);
610 }
Dees_Troy51127312012-09-08 13:08:49 -0400611 if (PartitionManager.Mount_Current_Storage(true)) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400612 if (arg == "internal") {
Dees_Troye2920fa2012-09-19 16:18:00 -0400613 string zip_path, zip_root;
614 DataManager::GetValue(TW_ZIP_INTERNAL_VAR, zip_path);
615 zip_root = TWFunc::Get_Root_Path(zip_path);
616#ifdef RECOVERY_SDCARD_ON_DATA
617 #ifndef TW_EXTERNAL_STORAGE_PATH
618 if (zip_root != "/sdcard")
619 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/sdcard");
620 #else
621 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
622 if (zip_root != "/emmc")
623 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/emmc");
624 } else {
625 if (zip_root != "/sdcard")
626 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/sdcard");
627 }
628 #endif
629#else
630 if (zip_root != DataManager::GetCurrentStoragePath())
631 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetCurrentStoragePath());
632#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400633 // Save the current zip location to the external variable
634 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, DataManager::GetStrValue(TW_ZIP_LOCATION_VAR));
635 // Change the current zip location to the internal variable
636 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetStrValue(TW_ZIP_INTERNAL_VAR));
637 } else if (arg == "external") {
Dees_Troye2920fa2012-09-19 16:18:00 -0400638 string zip_path, zip_root;
639 DataManager::GetValue(TW_ZIP_EXTERNAL_VAR, zip_path);
640 zip_root = TWFunc::Get_Root_Path(zip_path);
641 if (zip_root != DataManager::GetCurrentStoragePath()) {
642 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, DataManager::GetCurrentStoragePath());
643 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400644 // Save the current zip location to the internal variable
645 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, DataManager::GetStrValue(TW_ZIP_LOCATION_VAR));
646 // Change the current zip location to the external variable
647 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetStrValue(TW_ZIP_EXTERNAL_VAR));
648 }
649 } else {
650 // We weren't able to toggle for some reason, restore original setting
651 if (arg == "internal") {
652 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1);
653 } else if (arg == "external") {
654 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
655 }
656 }
657 return 0;
658 }
659
660 if (function == "overlay")
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200661 return gui_changeOverlay(arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400662
663 if (function == "queuezip")
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200664 {
665 if (zip_queue_index >= 10) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000666 gui_print("Maximum zip queue reached!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400667 return 0;
668 }
669 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
670 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
671 zip_queue_index++;
672 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
673 }
674 return 0;
675 }
676
677 if (function == "cancelzip")
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200678 {
679 if (zip_queue_index <= 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000680 gui_print("Minimum zip queue reached!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400681 return 0;
682 } else {
683 zip_queue_index--;
684 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
685 }
686 return 0;
687 }
688
689 if (function == "queueclear")
690 {
691 zip_queue_index = 0;
692 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
693 return 0;
694 }
695
696 if (function == "sleep")
697 {
698 operation_start("Sleep");
699 usleep(atoi(arg.c_str()));
700 operation_end(0, simulate);
701 return 0;
702 }
703
Dees Troyb21cc642013-09-10 17:36:41 +0000704 if (function == "appenddatetobackupname")
705 {
706 operation_start("AppendDateToBackupName");
707 string Backup_Name;
708 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
709 Backup_Name += TWFunc::Get_Current_Date();
710 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
711 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
712 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
713 operation_end(0, simulate);
714 return 0;
715 }
716
717 if (function == "generatebackupname")
718 {
719 operation_start("GenerateBackupName");
720 TWFunc::Auto_Generate_Backup_Name();
721 operation_end(0, simulate);
722 return 0;
723 }
724
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200725 if (isThreaded)
726 {
727 if (function == "fileexists")
Dees_Troy51a0e822012-09-05 15:24:24 -0400728 {
729 struct stat st;
730 string newpath = arg + "/.";
731
732 operation_start("FileExists");
733 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
734 operation_end(0, simulate);
735 else
736 operation_end(1, simulate);
Dees_Troy83bd4832013-05-04 12:39:56 +0000737 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400738 }
739
740 if (function == "flash")
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200741 {
Dees_Troy657c3092012-09-10 20:32:10 -0400742 int i, ret_val = 0, wipe_cache = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400743
744 for (i=0; i<zip_queue_index; i++) {
745 operation_start("Flashing");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200746 DataManager::SetValue("tw_filename", zip_queue[i]);
747 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
Dees_Troy51a0e822012-09-05 15:24:24 -0400748
Dees_Troy657c3092012-09-10 20:32:10 -0400749 ret_val = flash_zip(zip_queue[i], arg, simulate, &wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400750 if (ret_val != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000751 gui_print("Error flashing zip '%s'\n", zip_queue[i].c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400752 i = 10; // Error flashing zip - exit queue
753 ret_val = 1;
754 }
755 }
756 zip_queue_index = 0;
757 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
758
Dees_Troy657c3092012-09-10 20:32:10 -0400759 if (wipe_cache)
760 PartitionManager.Wipe_By_Path("/cache");
Vojtech Bocek05534202013-09-11 08:11:56 +0200761
Dees_Troy51a0e822012-09-05 15:24:24 -0400762 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
763 operation_start("ReinjectTWRP");
Dees_Troy2673cec2013-04-02 20:22:16 +0000764 gui_print("Injecting TWRP into boot image...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400765 if (simulate) {
766 simulate_progress_bar();
767 } else {
Dees_Troy06b4fe92012-10-16 11:43:20 -0400768 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
769 if (Boot == NULL || Boot->Current_File_System != "emmc")
Vojtech Bocek05534202013-09-11 08:11:56 +0200770 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Dees_Troy06b4fe92012-10-16 11:43:20 -0400771 else {
772 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 +0200773 TWFunc::Exec_Cmd(injectcmd);
Dees_Troy06b4fe92012-10-16 11:43:20 -0400774 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000775 gui_print("TWRP injection complete.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400776 }
777 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400778 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400779 operation_end(ret_val, simulate);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200780 return 0;
781 }
782 if (function == "wipe")
783 {
784 operation_start("Format");
785 DataManager::SetValue("tw_partition", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400786
Dees_Troy38bd7602012-09-14 13:33:53 -0400787 int ret_val = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400788
789 if (simulate) {
790 simulate_progress_bar();
791 } else {
792 if (arg == "data")
Dees_Troy38bd7602012-09-14 13:33:53 -0400793 ret_val = PartitionManager.Factory_Reset();
Dees_Troy51a0e822012-09-05 15:24:24 -0400794 else if (arg == "battery")
Dees_Troy38bd7602012-09-14 13:33:53 -0400795 ret_val = PartitionManager.Wipe_Battery_Stats();
Dees_Troy51a0e822012-09-05 15:24:24 -0400796 else if (arg == "rotate")
Dees_Troy38bd7602012-09-14 13:33:53 -0400797 ret_val = PartitionManager.Wipe_Rotate_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400798 else if (arg == "dalvik")
Dees_Troy38bd7602012-09-14 13:33:53 -0400799 ret_val = PartitionManager.Wipe_Dalvik_Cache();
Dees_Troy51a0e822012-09-05 15:24:24 -0400800 else if (arg == "DATAMEDIA") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400801 ret_val = PartitionManager.Format_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400802 } else if (arg == "INTERNAL") {
803 int has_datamedia, dual_storage;
804
805 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
806 if (has_datamedia) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400807 ret_val = PartitionManager.Wipe_Media_From_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400808 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -0400809 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
Dees_Troy51a0e822012-09-05 15:24:24 -0400810 }
811 } else if (arg == "EXTERNAL") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400812 string External_Path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400813
Dees_Troy38bd7602012-09-14 13:33:53 -0400814 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
815 ret_val = PartitionManager.Wipe_By_Path(External_Path);
Dees_Troy2ff5a8d2012-09-26 14:53:02 -0400816 } else if (arg == "ANDROIDSECURE") {
817 ret_val = PartitionManager.Wipe_Android_Secure();
Dees_Troya13d74f2013-03-24 08:54:55 -0500818 } else if (arg == "LIST") {
819 string Wipe_List, wipe_path;
820 bool skip = false;
821 ret_val = true;
822 TWPartition* wipe_part = NULL;
823
824 DataManager::GetValue("tw_wipe_list", Wipe_List);
Dees_Troy2673cec2013-04-02 20:22:16 +0000825 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500826 if (!Wipe_List.empty()) {
827 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
828 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
829 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
Dees_Troy2673cec2013-04-02 20:22:16 +0000830 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500831 if (wipe_path == "/and-sec") {
832 if (!PartitionManager.Wipe_Android_Secure()) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000833 LOGERR("Unable to wipe android secure\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500834 ret_val = false;
835 break;
836 } else {
837 skip = true;
838 }
839 } else if (wipe_path == "DALVIK") {
840 if (!PartitionManager.Wipe_Dalvik_Cache()) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000841 LOGERR("Failed to wipe dalvik\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500842 ret_val = false;
843 break;
844 } else {
845 skip = true;
846 }
Dees_Troy74fb2e92013-04-15 14:35:47 +0000847 } else if (wipe_path == "INTERNAL") {
848 if (!PartitionManager.Wipe_Media_From_Data()) {
849 ret_val = false;
850 break;
851 } else {
852 skip = true;
853 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500854 }
855 if (!skip) {
856 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000857 LOGERR("Unable to wipe '%s'\n", wipe_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500858 ret_val = false;
859 break;
860 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
861 arg = wipe_path;
862 }
863 } else {
864 skip = false;
865 }
866 start_pos = end_pos + 1;
867 end_pos = Wipe_List.find(";", start_pos);
868 }
869 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400870 } else
871 ret_val = PartitionManager.Wipe_By_Path(arg);
872
873 if (arg == DataManager::GetSettingsStoragePath()) {
874 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
875 string Storage_Path = DataManager::GetSettingsStoragePath();
876
877 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000878 LOGINFO("Making TWRP folder and saving settings.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400879 Storage_Path += "/TWRP";
880 mkdir(Storage_Path.c_str(), 0777);
881 DataManager::Flush();
882 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000883 LOGERR("Unable to recreate TWRP folder and save settings.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400884 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400885 }
886 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400887 PartitionManager.Update_System_Details();
Dees_Troy38bd7602012-09-14 13:33:53 -0400888 if (ret_val)
889 ret_val = 0; // 0 is success
890 else
891 ret_val = 1; // 1 is failure
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200892 operation_end(ret_val, simulate);
893 return 0;
894 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400895 if (function == "refreshsizes")
896 {
897 operation_start("Refreshing Sizes");
898 if (simulate) {
899 simulate_progress_bar();
900 } else
Dees_Troy5bf43922012-09-07 16:07:55 -0400901 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400902 operation_end(0, simulate);
Dees_Troy83bd4832013-05-04 12:39:56 +0000903 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400904 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200905 if (function == "nandroid")
906 {
907 operation_start("Nandroid");
Dees_Troy43d8b002012-09-17 16:00:01 -0400908 int ret = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400909
910 if (simulate) {
911 DataManager::SetValue("tw_partition", "Simulation");
912 simulate_progress_bar();
913 } else {
914 if (arg == "backup") {
915 string Backup_Name;
916 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees Troyb21cc642013-09-10 17:36:41 +0000917 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 -0400918 ret = PartitionManager.Run_Backup();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500919 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400920 else {
921 operation_end(1, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400922 return -1;
Dees_Troy43d8b002012-09-17 16:00:01 -0400923 }
Dees Troyb21cc642013-09-10 17:36:41 +0000924 DataManager::SetValue(TW_BACKUP_NAME, "(Auto Generate)");
Dees_Troy51a0e822012-09-05 15:24:24 -0400925 } else if (arg == "restore") {
926 string Restore_Name;
927 DataManager::GetValue("tw_restore", Restore_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400928 ret = PartitionManager.Run_Restore(Restore_Name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400929 } else {
930 operation_end(1, simulate);
931 return -1;
932 }
933 }
Dees_Troy83bd4832013-05-04 12:39:56 +0000934 DataManager::SetValue("tw_encrypt_backup", 0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400935 if (ret == false)
936 ret = 1; // 1 for failure
937 else
938 ret = 0; // 0 for success
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200939 operation_end(ret, simulate);
Dees_Troy83bd4832013-05-04 12:39:56 +0000940 return 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200941 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400942 if (function == "fixpermissions")
943 {
944 operation_start("Fix Permissions");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200945 LOGINFO("fix permissions started!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400946 if (simulate) {
947 simulate_progress_bar();
Dees_Troy4be841b2012-09-26 14:07:15 -0400948 } else {
Dees_Troy6480ce02012-10-10 10:26:54 -0400949 int op_status = PartitionManager.Fix_Permissions();
950 if (op_status != 0)
951 op_status = 1; // failure
952 operation_end(op_status, simulate);
Dees_Troy4be841b2012-09-26 14:07:15 -0400953 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400954 return 0;
955 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200956 if (function == "dd")
957 {
958 operation_start("imaging");
Dees_Troy51a0e822012-09-05 15:24:24 -0400959
960 if (simulate) {
961 simulate_progress_bar();
962 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500963 string cmd = "dd " + arg;
Vojtech Bocek05534202013-09-11 08:11:56 +0200964 TWFunc::Exec_Cmd(cmd);
Dees_Troy51a0e822012-09-05 15:24:24 -0400965 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200966 operation_end(0, simulate);
967 return 0;
968 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400969 if (function == "partitionsd")
970 {
971 operation_start("Partition SD Card");
Dees_Troy9350b8d2012-09-27 12:38:38 -0400972 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400973
974 if (simulate) {
975 simulate_progress_bar();
976 } else {
977 int allow_partition;
978 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
979 if (allow_partition == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000980 gui_print("This device does not have a real SD Card!\nAborting!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400981 } else {
Dees_Troy9350b8d2012-09-27 12:38:38 -0400982 if (!PartitionManager.Partition_SDCard())
983 ret_val = 1; // failed
Dees_Troy51a0e822012-09-05 15:24:24 -0400984 }
985 }
Dees_Troy9350b8d2012-09-27 12:38:38 -0400986 operation_end(ret_val, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400987 return 0;
988 }
989 if (function == "installhtcdumlock")
990 {
991 operation_start("Install HTC Dumlock");
992 if (simulate) {
993 simulate_progress_bar();
994 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400995 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -0400996
997 operation_end(0, simulate);
998 return 0;
999 }
1000 if (function == "htcdumlockrestoreboot")
1001 {
1002 operation_start("HTC Dumlock Restore Boot");
1003 if (simulate) {
1004 simulate_progress_bar();
1005 } else
Dees_Troy38bd7602012-09-14 13:33:53 -04001006 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001007
1008 operation_end(0, simulate);
1009 return 0;
1010 }
1011 if (function == "htcdumlockreflashrecovery")
1012 {
1013 operation_start("HTC Dumlock Reflash Recovery");
1014 if (simulate) {
1015 simulate_progress_bar();
1016 } else
Dees_Troy38bd7602012-09-14 13:33:53 -04001017 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001018
1019 operation_end(0, simulate);
1020 return 0;
1021 }
1022 if (function == "cmd")
1023 {
1024 int op_status = 0;
1025
1026 operation_start("Command");
Dees_Troy2673cec2013-04-02 20:22:16 +00001027 LOGINFO("Running command: '%s'\n", arg.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001028 if (simulate) {
1029 simulate_progress_bar();
1030 } else {
Vojtech Bocek05534202013-09-11 08:11:56 +02001031 op_status = TWFunc::Exec_Cmd(arg);
Dees_Troy51a0e822012-09-05 15:24:24 -04001032 if (op_status != 0)
1033 op_status = 1;
1034 }
1035
1036 operation_end(op_status, simulate);
1037 return 0;
1038 }
1039 if (function == "terminalcommand")
1040 {
1041 int op_status = 0;
1042 string cmdpath, command;
1043
1044 DataManager::GetValue("tw_terminal_location", cmdpath);
1045 operation_start("CommandOutput");
Dees_Troy2673cec2013-04-02 20:22:16 +00001046 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001047 if (simulate) {
1048 simulate_progress_bar();
1049 operation_end(op_status, simulate);
1050 } else {
Dees_Troy4be841b2012-09-26 14:07:15 -04001051 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
Dees_Troy2673cec2013-04-02 20:22:16 +00001052 LOGINFO("Actual command is: '%s'\n", command.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001053 DataManager::SetValue("tw_terminal_command_thread", command);
1054 DataManager::SetValue("tw_terminal_state", 1);
1055 DataManager::SetValue("tw_background_thread_running", 1);
1056 op_status = pthread_create(&terminal_command, NULL, command_thread, NULL);
1057 if (op_status != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001058 LOGERR("Error starting terminal command thread, %i.\n", op_status);
Dees_Troy51a0e822012-09-05 15:24:24 -04001059 DataManager::SetValue("tw_terminal_state", 0);
1060 DataManager::SetValue("tw_background_thread_running", 0);
1061 operation_end(1, simulate);
1062 }
1063 }
1064 return 0;
1065 }
1066 if (function == "killterminal")
1067 {
1068 int op_status = 0;
1069
Dees_Troy2673cec2013-04-02 20:22:16 +00001070 LOGINFO("Sending kill command...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001071 operation_start("KillCommand");
1072 DataManager::SetValue("tw_operation_status", 0);
1073 DataManager::SetValue("tw_operation_state", 1);
1074 DataManager::SetValue("tw_terminal_state", 0);
1075 DataManager::SetValue("tw_background_thread_running", 0);
1076 DataManager::SetValue(TW_ACTION_BUSY, 0);
1077 return 0;
1078 }
1079 if (function == "reinjecttwrp")
1080 {
1081 int op_status = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001082 operation_start("ReinjectTWRP");
Dees_Troy2673cec2013-04-02 20:22:16 +00001083 gui_print("Injecting TWRP into boot image...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001084 if (simulate) {
1085 simulate_progress_bar();
1086 } else {
Vojtech Bocek05534202013-09-11 08:11:56 +02001087 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Dees_Troy2673cec2013-04-02 20:22:16 +00001088 gui_print("TWRP injection complete.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001089 }
1090
1091 operation_end(op_status, simulate);
1092 return 0;
1093 }
1094 if (function == "checkbackupname")
1095 {
1096 int op_status = 0;
1097
1098 operation_start("CheckBackupName");
1099 if (simulate) {
1100 simulate_progress_bar();
1101 } else {
Dees_Troyc9ff7a32012-09-27 10:09:41 -04001102 op_status = PartitionManager.Check_Backup_Name(true);
Dees_Troy51a0e822012-09-05 15:24:24 -04001103 if (op_status != 0)
1104 op_status = 1;
1105 }
1106
1107 operation_end(op_status, simulate);
1108 return 0;
1109 }
1110 if (function == "decrypt")
1111 {
1112 int op_status = 0;
1113
1114 operation_start("Decrypt");
1115 if (simulate) {
1116 simulate_progress_bar();
1117 } else {
1118 string Password;
1119 DataManager::GetValue("tw_crypto_password", Password);
Dees_Troy5bf43922012-09-07 16:07:55 -04001120 op_status = PartitionManager.Decrypt_Device(Password);
Dees_Troy51a0e822012-09-05 15:24:24 -04001121 if (op_status != 0)
1122 op_status = 1;
1123 else {
1124 int load_theme = 1;
1125
1126 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001127
1128 if (load_theme) {
1129 int has_datamedia;
1130
1131 // Check for a custom theme and load it if exists
1132 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1133 if (has_datamedia != 0) {
1134 struct stat st;
1135 int check = 0;
1136 std::string theme_path;
1137
1138 theme_path = DataManager::GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -04001139 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001140 LOGERR("Unable to mount %s during reload function startup.\n", theme_path.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001141 check = 1;
1142 }
1143
1144 theme_path += "/TWRP/theme/ui.zip";
1145 if (check == 0 && stat(theme_path.c_str(), &st) == 0) {
1146 if (PageManager::ReloadPackage("TWRP", theme_path) != 0)
1147 {
1148 // Loading the custom theme failed - try loading the stock theme
Dees_Troy2673cec2013-04-02 20:22:16 +00001149 LOGINFO("Attempting to reload stock theme...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001150 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
1151 {
Dees_Troy2673cec2013-04-02 20:22:16 +00001152 LOGERR("Failed to load base packages.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001153 }
1154 }
1155 }
1156 }
1157 }
1158 }
1159 }
1160
1161 operation_end(op_status, simulate);
1162 return 0;
1163 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001164 if (function == "adbsideload")
1165 {
1166 int ret = 0;
1167
1168 operation_start("Sideload");
1169 if (simulate) {
1170 simulate_progress_bar();
1171 } else {
1172 int wipe_cache = 0;
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001173 int wipe_dalvik = 0;
Vojtech Bocek05534202013-09-11 08:11:56 +02001174 string Sideload_File;
Dees_Troycfb63ae2012-09-19 14:30:17 -04001175
1176 if (!PartitionManager.Mount_Current_Storage(true)) {
1177 operation_end(1, simulate);
1178 return 0;
1179 }
Dees_Troy9a4b5692012-09-19 15:09:45 -04001180 Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip";
1181 if (TWFunc::Path_Exists(Sideload_File)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001182 unlink(Sideload_File.c_str());
Dees_Troycfb63ae2012-09-19 14:30:17 -04001183 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001184 gui_print("Starting ADB sideload feature...\n");
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001185 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
Dees_Troy2673cec2013-04-02 20:22:16 +00001186 ret = apply_from_adb(Sideload_File.c_str());
1187 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001188 if (ret != 0) {
Dees_Troycfb63ae2012-09-19 14:30:17 -04001189 ret = 1; // failure
Dees_Troy2673cec2013-04-02 20:22:16 +00001190 } else if (TWinstall_zip(Sideload_File.c_str(), &wipe_cache) == 0) {
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001191 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1192 PartitionManager.Wipe_By_Path("/cache");
1193 if (wipe_dalvik)
1194 PartitionManager.Wipe_Dalvik_Cache();
Dees_Troy2673cec2013-04-02 20:22:16 +00001195 } else {
1196 ret = 1; // failure
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001197 }
Dees Troy8c0f06b2013-11-26 21:21:29 +00001198 PartitionManager.Update_System_Details();
Dees_Troy06b4fe92012-10-16 11:43:20 -04001199 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
1200 operation_start("ReinjectTWRP");
Dees_Troy2673cec2013-04-02 20:22:16 +00001201 gui_print("Injecting TWRP into boot image...\n");
Dees_Troy06b4fe92012-10-16 11:43:20 -04001202 if (simulate) {
1203 simulate_progress_bar();
1204 } else {
1205 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
1206 if (Boot == NULL || Boot->Current_File_System != "emmc")
Vojtech Bocek05534202013-09-11 08:11:56 +02001207 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Dees_Troy06b4fe92012-10-16 11:43:20 -04001208 else {
1209 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 +02001210 TWFunc::Exec_Cmd(injectcmd);
Dees_Troy06b4fe92012-10-16 11:43:20 -04001211 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001212 gui_print("TWRP injection complete.\n");
Dees_Troy06b4fe92012-10-16 11:43:20 -04001213 }
1214 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001215 }
1216 operation_end(ret, simulate);
1217 return 0;
1218 }
Dees_Troycfb63ae2012-09-19 14:30:17 -04001219 if (function == "adbsideloadcancel")
1220 {
1221 int child_pid;
Dees_Troy2673cec2013-04-02 20:22:16 +00001222 char child_prop[PROPERTY_VALUE_MAX];
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001223 string Sideload_File;
Dees_Troy9a4b5692012-09-19 15:09:45 -04001224 Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip";
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001225 unlink(Sideload_File.c_str());
Dees_Troy2673cec2013-04-02 20:22:16 +00001226 property_get("tw_child_pid", child_prop, "error");
1227 if (strcmp(child_prop, "error") == 0) {
1228 LOGERR("Unable to get child ID from prop\n");
1229 return 0;
1230 }
1231 child_pid = atoi(child_prop);
1232 gui_print("Cancelling ADB sideload...\n");
Dees_Troycfb63ae2012-09-19 14:30:17 -04001233 kill(child_pid, SIGTERM);
Dees_Troy4bc09ae2013-01-18 17:00:54 +00001234 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
Dees_Troycfb63ae2012-09-19 14:30:17 -04001235 return 0;
1236 }
Dees_Troy6ed34b72013-01-25 15:01:29 +00001237 if (function == "openrecoveryscript") {
1238 operation_start("OpenRecoveryScript");
1239 if (simulate) {
1240 simulate_progress_bar();
1241 } else {
1242 // Check for the SCRIPT_FILE_TMP first as these are AOSP recovery commands
1243 // that we converted to ORS commands during boot in recovery.cpp.
1244 // Run those first.
1245 int reboot = 0;
1246 if (TWFunc::Path_Exists(SCRIPT_FILE_TMP)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001247 gui_print("Processing AOSP recovery commands...\n");
Dees_Troy6ed34b72013-01-25 15:01:29 +00001248 if (OpenRecoveryScript::run_script_file() == 0) {
1249 reboot = 1;
1250 }
1251 }
1252 // Check for the ORS file in /cache and attempt to run those commands.
1253 if (OpenRecoveryScript::check_for_script_file()) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001254 gui_print("Processing OpenRecoveryScript file...\n");
Dees_Troy6ed34b72013-01-25 15:01:29 +00001255 if (OpenRecoveryScript::run_script_file() == 0) {
1256 reboot = 1;
1257 }
1258 }
1259 if (reboot) {
1260 usleep(2000000); // Sleep for 2 seconds before rebooting
1261 TWFunc::tw_reboot(rb_system);
1262 } else {
1263 DataManager::SetValue("tw_page_done", 1);
1264 }
1265 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001266 return 0;
Dees_Troy6ed34b72013-01-25 15:01:29 +00001267 }
Dees_Troy6ef66352013-02-21 08:26:57 -06001268 if (function == "installsu")
1269 {
1270 int op_status = 0;
1271
1272 operation_start("Install SuperSU");
1273 if (simulate) {
1274 simulate_progress_bar();
1275 } else {
1276 if (!TWFunc::Install_SuperSU())
1277 op_status = 1;
1278 }
1279
1280 operation_end(op_status, simulate);
1281 return 0;
1282 }
1283 if (function == "fixsu")
1284 {
1285 int op_status = 0;
1286
1287 operation_start("Fixing Superuser Permissions");
1288 if (simulate) {
1289 simulate_progress_bar();
1290 } else {
Dees Troyf193f882013-09-11 14:56:20 +00001291 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1292 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
Dees_Troy6ef66352013-02-21 08:26:57 -06001293 }
1294
1295 operation_end(op_status, simulate);
1296 return 0;
1297 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001298 if (function == "decrypt_backup")
1299 {
1300 int op_status = 0;
1301
1302 operation_start("Try Restore Decrypt");
1303 if (simulate) {
1304 simulate_progress_bar();
1305 } else {
1306 string Restore_Path, Filename, Password;
1307 DataManager::GetValue("tw_restore", Restore_Path);
1308 Restore_Path += "/";
1309 DataManager::GetValue("tw_restore_password", Password);
1310 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1311 op_status = 0; // success
1312 else
1313 op_status = 1; // fail
1314 }
1315
1316 operation_end(op_status, simulate);
1317 return 0;
1318 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001319 }
1320 else
1321 {
Dees_Troy83bd4832013-05-04 12:39:56 +00001322 pthread_t t;
1323 pthread_create(&t, NULL, thread_start, this);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001324 return 0;
1325 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001326 LOGERR("Unknown action '%s'\n", function.c_str());
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001327 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -04001328}
1329
1330int GUIAction::getKeyByName(std::string key)
1331{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001332 if (key == "home") return KEY_HOME;
1333 else if (key == "menu") return KEY_MENU;
1334 else if (key == "back") return KEY_BACK;
1335 else if (key == "search") return KEY_SEARCH;
1336 else if (key == "voldown") return KEY_VOLUMEDOWN;
1337 else if (key == "volup") return KEY_VOLUMEUP;
1338 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001339 int ret_val;
1340 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1341 if (!ret_val)
1342 return KEY_POWER;
1343 else
1344 return ret_val;
1345 }
1346
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001347 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001348}
1349
1350void* GUIAction::command_thread(void *cookie)
1351{
1352 string command;
1353 FILE* fp;
1354 char line[512];
1355
1356 DataManager::GetValue("tw_terminal_command_thread", command);
Dees_Troy8170a922012-09-18 15:40:25 -04001357 fp = popen(command.c_str(), "r");
Dees_Troy51a0e822012-09-05 15:24:24 -04001358 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001359 LOGERR("Error opening command to run.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001360 } else {
1361 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1362 struct timeval timeout;
1363 fd_set fdset;
1364
1365 while(keep_going)
1366 {
1367 FD_ZERO(&fdset);
1368 FD_SET(fd, &fdset);
1369 timeout.tv_sec = 0;
1370 timeout.tv_usec = 400000;
1371 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1372 if (has_data == 0) {
1373 // Timeout reached
1374 DataManager::GetValue("tw_terminal_state", check);
1375 if (check == 0) {
1376 keep_going = 0;
1377 }
1378 } else if (has_data < 0) {
1379 // End of execution
1380 keep_going = 0;
1381 } else {
1382 // Try to read output
Dees_Troy4be841b2012-09-26 14:07:15 -04001383 memset(line, 0, sizeof(line));
Dees_Troy51a0e822012-09-05 15:24:24 -04001384 bytes_read = read(fd, line, sizeof(line));
1385 if (bytes_read > 0)
Dees_Troy2673cec2013-04-02 20:22:16 +00001386 gui_print("%s", line); // Display output
Dees_Troy51a0e822012-09-05 15:24:24 -04001387 else
1388 keep_going = 0; // Done executing
1389 }
1390 }
1391 fclose(fp);
1392 }
1393 DataManager::SetValue("tw_operation_status", 0);
1394 DataManager::SetValue("tw_operation_state", 1);
1395 DataManager::SetValue("tw_terminal_state", 0);
1396 DataManager::SetValue("tw_background_thread_running", 0);
1397 DataManager::SetValue(TW_ACTION_BUSY, 0);
1398 return NULL;
1399}