blob: 3456324102075e21dd3aeb66769b309c65856176 [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_Troy51a0e822012-09-05 15:24:24 -040034
35#include <string>
36#include <sstream>
37#include "../partitions.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040038#include "../twrp-functions.hpp"
Dees_Troy812660f2012-09-20 09:55:17 -040039#include "../openrecoveryscript.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040040
Dees_Troy43d8b002012-09-17 16:00:01 -040041#include "../ui.h"
42#include "../adb_install.h"
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050043#include "blanktimer.hpp"
Dees_Troy43d8b002012-09-17 16:00:01 -040044
Dees_Troy51a0e822012-09-05 15:24:24 -040045extern "C" {
46#include "../common.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040047#include "../minuitwrp/minui.h"
48#include "../recovery_ui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040049#include "../variables.h"
Dees_Troy32c8eb82012-09-11 15:28:06 -040050#include "../twinstall.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040051
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
Dees_Troy43d8b002012-09-17 16:00:01 -040063extern RecoveryUI* ui;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050064extern blanktimer blankTimer;
Dees_Troy43d8b002012-09-17 16:00:01 -040065
Dees_Troy51a0e822012-09-05 15:24:24 -040066void curtainClose(void);
67
68GUIAction::GUIAction(xml_node<>* node)
69 : Conditional(node)
70{
71 xml_node<>* child;
72 xml_node<>* actions;
73 xml_attribute<>* attr;
74
75 mKey = 0;
76
77 if (!node) return;
78
79 // First, get the action
80 actions = node->first_node("actions");
81 if (actions) child = actions->first_node("action");
82 else child = node->first_node("action");
83
84 if (!child) return;
85
86 while (child)
87 {
88 Action action;
89
90 attr = child->first_attribute("function");
91 if (!attr) return;
92
93 action.mFunction = attr->value();
94 action.mArg = child->value();
95 mActions.push_back(action);
96
97 child = child->next_sibling("action");
98 }
99
100 // Now, let's get either the key or region
101 child = node->first_node("touch");
102 if (child)
103 {
104 attr = child->first_attribute("key");
105 if (attr)
106 {
107 std::string key = attr->value();
108
109 mKey = getKeyByName(key);
110 }
111 else
112 {
113 attr = child->first_attribute("x");
114 if (!attr) return;
115 mActionX = atol(attr->value());
116 attr = child->first_attribute("y");
117 if (!attr) return;
118 mActionY = atol(attr->value());
119 attr = child->first_attribute("w");
120 if (!attr) return;
121 mActionW = atol(attr->value());
122 attr = child->first_attribute("h");
123 if (!attr) return;
124 mActionH = atol(attr->value());
125 }
126 }
127}
128
129int GUIAction::NotifyTouch(TOUCH_STATE state, int x, int y)
130{
131 if (state == TOUCH_RELEASE)
132 doActions();
133
134 return 0;
135}
136
137int GUIAction::NotifyKey(int key)
138{
139 if (!mKey || key != mKey)
140 return 1;
141
142 doActions();
143 return 0;
144}
145
146int GUIAction::NotifyVarChange(std::string varName, std::string value)
147{
148 if (varName.empty() && !isConditionValid() && !mKey && !mActionW)
149 doActions();
150
151 // This handles notifying the condition system of page start
152 if (varName.empty() && isConditionValid())
153 NotifyPageSet();
154
155 if ((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
156 doActions();
157
158 return 0;
159}
160
161void GUIAction::simulate_progress_bar(void)
162{
163 ui_print("Simulating actions...\n");
164 for (int i = 0; i < 5; i++)
165 {
166 usleep(500000);
167 DataManager::SetValue("ui_progress", i * 20);
168 }
169}
170
Dees_Troy657c3092012-09-10 20:32:10 -0400171int GUIAction::flash_zip(std::string filename, std::string pageName, const int simulate, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400172{
173 int ret_val = 0;
174
175 DataManager::SetValue("ui_progress", 0);
176
177 if (filename.empty())
178 {
179 LOGE("No file specified.\n");
180 return -1;
181 }
182
183 // We're going to jump to this page first, like a loading page
184 gui_changePage(pageName);
185
186 int fd = -1;
187 ZipArchive zip;
188
Dees_Troy657c3092012-09-10 20:32:10 -0400189 if (!PartitionManager.Mount_By_Path(filename, true))
190 return -1;
191
192 if (mzOpenZipArchive(filename.c_str(), &zip))
Dees_Troy51a0e822012-09-05 15:24:24 -0400193 {
194 LOGE("Unable to open zip file.\n");
195 return -1;
196 }
197
198 // Check the zip to see if it has a custom installer theme
199 const ZipEntry* twrp = mzFindZipEntry(&zip, "META-INF/teamwin/twrp.zip");
200 if (twrp != NULL)
201 {
202 unlink("/tmp/twrp.zip");
203 fd = creat("/tmp/twrp.zip", 0666);
204 }
205 if (fd >= 0 && twrp != NULL &&
206 mzExtractZipEntryToFile(&zip, twrp, fd) &&
207 !PageManager::LoadPackage("install", "/tmp/twrp.zip", "main"))
208 {
209 mzCloseZipArchive(&zip);
Dees_Troy657c3092012-09-10 20:32:10 -0400210 PageManager::SelectPackage("install");
Dees_Troy51a0e822012-09-05 15:24:24 -0400211 gui_changePage("main");
212 }
213 else
214 {
215 // In this case, we just use the default page
216 mzCloseZipArchive(&zip);
Dees_Troy657c3092012-09-10 20:32:10 -0400217 gui_changePage(pageName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400218 }
219 if (fd >= 0)
220 close(fd);
221
222 if (simulate) {
223 simulate_progress_bar();
224 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400225 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400226
227 // Now, check if we need to ensure TWRP remains installed...
228 struct stat st;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500229 string result;
Dees_Troy51a0e822012-09-05 15:24:24 -0400230 if (stat("/sbin/installTwrp", &st) == 0)
231 {
232 DataManager::SetValue("tw_operation", "Configuring TWRP");
233 DataManager::SetValue("tw_partition", "");
234 ui_print("Configuring TWRP...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500235 if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall", result) < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400236 {
237 ui_print("Unable to configure TWRP with this kernel.\n");
238 }
239 }
240 }
241
242 // Done
243 DataManager::SetValue("ui_progress", 100);
244 DataManager::SetValue("ui_progress", 0);
245 return ret_val;
246}
247
248int GUIAction::doActions()
249{
250 if (mActions.size() < 1) return -1;
251 if (mActions.size() == 1)
252 return doAction(mActions.at(0), 0);
253
254 // 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)) {
259 LOGE("Unable to pthread_attr_init\n");
260 return -1;
261 }
262 if (pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_JOINABLE)) {
263 LOGE("Error setting pthread_attr_setdetachstate\n");
264 return -1;
265 }
266 if (pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM)) {
267 LOGE("Error setting pthread_attr_setscope\n");
268 return -1;
269 }
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500270 /*if (pthread_attr_setstacksize(&tattr, 524288)) {
Dees_Troyab4963c2013-01-16 20:35:51 +0000271 LOGE("Error setting pthread_attr_setstacksize\n");
272 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);
Dees_Troyab4963c2013-01-16 20:35:51 +0000276 if (ret) {
277 LOGE("Unable to create more threads for actions... continuing in same thread! %i\n", ret);
278 thread_start(this);
279 } else {
280 if (pthread_join(t, NULL)) {
281 LOGE("Error joining threads\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000282 }
283 }
284 if (pthread_attr_destroy(&tattr)) {
285 LOGE("Failed to pthread_attr_destroy\n");
286 return -1;
287 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400288
289 return 0;
290}
291
292void* GUIAction::thread_start(void *cookie)
293{
294 GUIAction* ourThis = (GUIAction*) cookie;
295
296 DataManager::SetValue(TW_ACTION_BUSY, 1);
297
298 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 }
308 int check = 0;
309 DataManager::GetValue("tw_background_thread_running", check);
310 if (check == 0)
311 DataManager::SetValue(TW_ACTION_BUSY, 0);
312 return NULL;
313}
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);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500344 blankTimer.resetTimerAndUnblank();
Dees_Troy51a0e822012-09-05 15:24:24 -0400345}
346
347int GUIAction::doAction(Action action, int isThreaded /* = 0 */)
348{
349 static string zip_queue[10];
350 static int zip_queue_index;
351 static pthread_t terminal_command;
352 int simulate;
353
354 std::string arg = gui_parse_text(action.mArg);
355
356 std::string function = gui_parse_text(action.mFunction);
357
358 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
359
Dees_Troy6ef66352013-02-21 08:26:57 -0600360 if (function == "reboot")
361 {
362 //curtainClose(); this sometimes causes a crash
Dees_Troy51a0e822012-09-05 15:24:24 -0400363
Dees_Troy6ef66352013-02-21 08:26:57 -0600364 sync();
365 DataManager::SetValue("tw_gui_done", 1);
366 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400367
Dees_Troy6ef66352013-02-21 08:26:57 -0600368 return 0;
369 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400370 if (function == "home")
371 {
372 PageManager::SelectPackage("TWRP");
373 gui_changePage("main");
374 return 0;
375 }
376
377 if (function == "key")
378 {
379 PageManager::NotifyKey(getKeyByName(arg));
380 return 0;
381 }
382
383 if (function == "page") {
384 std::string page_name = gui_parse_text(arg);
385 return gui_changePage(page_name);
386 }
387
388 if (function == "reload") {
389 int check = 0, ret_val = 0;
390 std::string theme_path;
391
392 operation_start("Reload Theme");
393 theme_path = DataManager::GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -0400394 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400395 LOGE("Unable to mount %s during reload function startup.\n", theme_path.c_str());
396 check = 1;
397 }
398
399 theme_path += "/TWRP/theme/ui.zip";
400 if (check != 0 || PageManager::ReloadPackage("TWRP", theme_path) != 0)
401 {
402 // Loading the custom theme failed - try loading the stock theme
403 LOGI("Attempting to reload stock theme...\n");
404 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
405 {
406 LOGE("Failed to load base packages.\n");
407 ret_val = 1;
408 }
409 }
410 operation_end(ret_val, simulate);
411 }
412
413 if (function == "readBackup")
414 {
415 string Restore_Name;
416 DataManager::GetValue("tw_restore", Restore_Name);
Dees_Troy5bf43922012-09-07 16:07:55 -0400417 PartitionManager.Set_Restore_Files(Restore_Name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400418 return 0;
419 }
420
421 if (function == "set")
422 {
423 if (arg.find('=') != string::npos)
424 {
425 string varName = arg.substr(0, arg.find('='));
426 string value = arg.substr(arg.find('=') + 1, string::npos);
427
428 DataManager::GetValue(value, value);
429 DataManager::SetValue(varName, value);
430 }
431 else
432 DataManager::SetValue(arg, "1");
433 return 0;
434 }
435 if (function == "clear")
436 {
437 DataManager::SetValue(arg, "0");
438 return 0;
439 }
440
441 if (function == "mount")
442 {
443 if (arg == "usb")
444 {
445 DataManager::SetValue(TW_ACTION_BUSY, 1);
446 if (!simulate)
Dees_Troy8170a922012-09-18 15:40:25 -0400447 PartitionManager.usb_storage_enable();
Dees_Troy51a0e822012-09-05 15:24:24 -0400448 else
449 ui_print("Simulating actions...\n");
450 }
451 else if (!simulate)
452 {
453 string cmd;
454 if (arg == "EXTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400455 PartitionManager.Mount_By_Path(DataManager::GetStrValue(TW_EXTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400456 else if (arg == "INTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400457 PartitionManager.Mount_By_Path(DataManager::GetStrValue(TW_INTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400458 else
Dees_Troy51127312012-09-08 13:08:49 -0400459 PartitionManager.Mount_By_Path(arg, true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400460 } else
461 ui_print("Simulating actions...\n");
462 return 0;
463 }
464
465 if (function == "umount" || function == "unmount")
466 {
467 if (arg == "usb")
468 {
469 if (!simulate)
Dees_Troy8170a922012-09-18 15:40:25 -0400470 PartitionManager.usb_storage_disable();
Dees_Troy51a0e822012-09-05 15:24:24 -0400471 else
472 ui_print("Simulating actions...\n");
473 DataManager::SetValue(TW_ACTION_BUSY, 0);
474 }
475 else if (!simulate)
476 {
477 string cmd;
478 if (arg == "EXTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400479 PartitionManager.UnMount_By_Path(DataManager::GetStrValue(TW_EXTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400480 else if (arg == "INTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400481 PartitionManager.UnMount_By_Path(DataManager::GetStrValue(TW_INTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400482 else
Dees_Troy51127312012-09-08 13:08:49 -0400483 PartitionManager.UnMount_By_Path(arg, true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400484 } else
485 ui_print("Simulating actions...\n");
486 return 0;
487 }
488
489 if (function == "restoredefaultsettings")
490 {
491 operation_start("Restore Defaults");
492 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
493 ui_print("Simulating actions...\n");
494 else {
495 DataManager::ResetDefaults();
Dees_Troy5bf43922012-09-07 16:07:55 -0400496 PartitionManager.Update_System_Details();
497 PartitionManager.Mount_Current_Storage(true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400498 }
499 operation_end(0, simulate);
500 }
501
502 if (function == "copylog")
503 {
504 operation_start("Copy Log");
505 if (!simulate)
506 {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500507 string dst;
Dees_Troy5bf43922012-09-07 16:07:55 -0400508 PartitionManager.Mount_Current_Storage(true);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500509 dst = DataManager::GetCurrentStoragePath() + "/recovery.log";
510 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
Dees_Troy51a0e822012-09-05 15:24:24 -0400511 sync();
512 ui_print("Copied recovery log to %s.\n", DataManager::GetCurrentStoragePath().c_str());
513 } else
514 simulate_progress_bar();
515 operation_end(0, simulate);
516 return 0;
517 }
518
519 if (function == "compute" || function == "addsubtract")
520 {
521 if (arg.find("+") != string::npos)
522 {
523 string varName = arg.substr(0, arg.find('+'));
524 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
525 int amount_to_add = atoi(string_to_add.c_str());
526 int value;
527
528 DataManager::GetValue(varName, value);
529 DataManager::SetValue(varName, value + amount_to_add);
530 return 0;
531 }
532 if (arg.find("-") != string::npos)
533 {
534 string varName = arg.substr(0, arg.find('-'));
535 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
536 int amount_to_subtract = atoi(string_to_subtract.c_str());
537 int value;
538
539 DataManager::GetValue(varName, value);
540 value -= amount_to_subtract;
541 if (value <= 0)
542 value = 0;
543 DataManager::SetValue(varName, value);
544 return 0;
545 }
546 }
547
548 if (function == "setguitimezone")
549 {
550 string SelectedZone;
551 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
552 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
553 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
554
555 int dst;
556 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
557
558 string offset;
559 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
560
561 string NewTimeZone = Zone;
562 if (offset != "0")
563 NewTimeZone += ":" + offset;
564
565 if (dst != 0)
566 NewTimeZone += DSTZone;
567
568 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
Dees_Troy8170a922012-09-18 15:40:25 -0400569 DataManager::update_tz_environment_variables();
Dees_Troy51a0e822012-09-05 15:24:24 -0400570 return 0;
571 }
572
573 if (function == "togglestorage") {
574 if (arg == "internal") {
575 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
576 } else if (arg == "external") {
577 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1);
578 }
Dees_Troy51127312012-09-08 13:08:49 -0400579 if (PartitionManager.Mount_Current_Storage(true)) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400580 if (arg == "internal") {
Dees_Troye2920fa2012-09-19 16:18:00 -0400581 string zip_path, zip_root;
582 DataManager::GetValue(TW_ZIP_INTERNAL_VAR, zip_path);
583 zip_root = TWFunc::Get_Root_Path(zip_path);
584#ifdef RECOVERY_SDCARD_ON_DATA
585 #ifndef TW_EXTERNAL_STORAGE_PATH
586 if (zip_root != "/sdcard")
587 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/sdcard");
588 #else
589 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
590 if (zip_root != "/emmc")
591 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/emmc");
592 } else {
593 if (zip_root != "/sdcard")
594 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/sdcard");
595 }
596 #endif
597#else
598 if (zip_root != DataManager::GetCurrentStoragePath())
599 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetCurrentStoragePath());
600#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400601 // Save the current zip location to the external variable
602 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, DataManager::GetStrValue(TW_ZIP_LOCATION_VAR));
603 // Change the current zip location to the internal variable
604 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetStrValue(TW_ZIP_INTERNAL_VAR));
605 } else if (arg == "external") {
Dees_Troye2920fa2012-09-19 16:18:00 -0400606 string zip_path, zip_root;
607 DataManager::GetValue(TW_ZIP_EXTERNAL_VAR, zip_path);
608 zip_root = TWFunc::Get_Root_Path(zip_path);
609 if (zip_root != DataManager::GetCurrentStoragePath()) {
610 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, DataManager::GetCurrentStoragePath());
611 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400612 // Save the current zip location to the internal variable
613 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, DataManager::GetStrValue(TW_ZIP_LOCATION_VAR));
614 // Change the current zip location to the external variable
615 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetStrValue(TW_ZIP_EXTERNAL_VAR));
616 }
617 } else {
618 // We weren't able to toggle for some reason, restore original setting
619 if (arg == "internal") {
620 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1);
621 } else if (arg == "external") {
622 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
623 }
624 }
625 return 0;
626 }
627
628 if (function == "overlay")
629 return gui_changeOverlay(arg);
630
631 if (function == "queuezip")
632 {
633 if (zip_queue_index >= 10) {
634 ui_print("Maximum zip queue reached!\n");
635 return 0;
636 }
637 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
638 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
639 zip_queue_index++;
640 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
641 }
642 return 0;
643 }
644
645 if (function == "cancelzip")
646 {
647 if (zip_queue_index <= 0) {
648 ui_print("Minimum zip queue reached!\n");
649 return 0;
650 } else {
651 zip_queue_index--;
652 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
653 }
654 return 0;
655 }
656
657 if (function == "queueclear")
658 {
659 zip_queue_index = 0;
660 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
661 return 0;
662 }
663
664 if (function == "sleep")
665 {
666 operation_start("Sleep");
667 usleep(atoi(arg.c_str()));
668 operation_end(0, simulate);
669 return 0;
670 }
671
672 if (isThreaded)
673 {
674 if (function == "fileexists")
675 {
676 struct stat st;
677 string newpath = arg + "/.";
678
679 operation_start("FileExists");
680 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
681 operation_end(0, simulate);
682 else
683 operation_end(1, simulate);
684 }
685
686 if (function == "flash")
687 {
Dees_Troy657c3092012-09-10 20:32:10 -0400688 int i, ret_val = 0, wipe_cache = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400689
690 for (i=0; i<zip_queue_index; i++) {
691 operation_start("Flashing");
692 DataManager::SetValue("tw_filename", zip_queue[i]);
693 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
694
Dees_Troy657c3092012-09-10 20:32:10 -0400695 ret_val = flash_zip(zip_queue[i], arg, simulate, &wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400696 if (ret_val != 0) {
697 ui_print("Error flashing zip '%s'\n", zip_queue[i].c_str());
698 i = 10; // Error flashing zip - exit queue
699 ret_val = 1;
700 }
701 }
702 zip_queue_index = 0;
703 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
704
Dees_Troy657c3092012-09-10 20:32:10 -0400705 if (wipe_cache)
706 PartitionManager.Wipe_By_Path("/cache");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500707 string result;
Dees_Troy51a0e822012-09-05 15:24:24 -0400708 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
709 operation_start("ReinjectTWRP");
710 ui_print("Injecting TWRP into boot image...\n");
711 if (simulate) {
712 simulate_progress_bar();
713 } else {
Dees_Troy06b4fe92012-10-16 11:43:20 -0400714 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
715 if (Boot == NULL || Boot->Current_File_System != "emmc")
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500716 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash", result);
Dees_Troy06b4fe92012-10-16 11:43:20 -0400717 else {
718 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500719 TWFunc::Exec_Cmd(injectcmd, result);
Dees_Troy06b4fe92012-10-16 11:43:20 -0400720 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400721 ui_print("TWRP injection complete.\n");
722 }
723 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400724 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400725 operation_end(ret_val, simulate);
726 return 0;
727 }
728 if (function == "wipe")
729 {
730 operation_start("Format");
731 DataManager::SetValue("tw_partition", arg);
732
Dees_Troy38bd7602012-09-14 13:33:53 -0400733 int ret_val = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400734
735 if (simulate) {
736 simulate_progress_bar();
737 } else {
738 if (arg == "data")
Dees_Troy38bd7602012-09-14 13:33:53 -0400739 ret_val = PartitionManager.Factory_Reset();
Dees_Troy51a0e822012-09-05 15:24:24 -0400740 else if (arg == "battery")
Dees_Troy38bd7602012-09-14 13:33:53 -0400741 ret_val = PartitionManager.Wipe_Battery_Stats();
Dees_Troy51a0e822012-09-05 15:24:24 -0400742 else if (arg == "rotate")
Dees_Troy38bd7602012-09-14 13:33:53 -0400743 ret_val = PartitionManager.Wipe_Rotate_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400744 else if (arg == "dalvik")
Dees_Troy38bd7602012-09-14 13:33:53 -0400745 ret_val = PartitionManager.Wipe_Dalvik_Cache();
Dees_Troy51a0e822012-09-05 15:24:24 -0400746 else if (arg == "DATAMEDIA") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400747 ret_val = PartitionManager.Format_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400748 } else if (arg == "INTERNAL") {
749 int has_datamedia, dual_storage;
750
751 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
752 if (has_datamedia) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400753 ret_val = PartitionManager.Wipe_Media_From_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400754 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -0400755 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
Dees_Troy51a0e822012-09-05 15:24:24 -0400756 }
757 } else if (arg == "EXTERNAL") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400758 string External_Path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400759
Dees_Troy38bd7602012-09-14 13:33:53 -0400760 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
761 ret_val = PartitionManager.Wipe_By_Path(External_Path);
Dees_Troy2ff5a8d2012-09-26 14:53:02 -0400762 } else if (arg == "ANDROIDSECURE") {
763 ret_val = PartitionManager.Wipe_Android_Secure();
Dees_Troya13d74f2013-03-24 08:54:55 -0500764 } else if (arg == "LIST") {
765 string Wipe_List, wipe_path;
766 bool skip = false;
767 ret_val = true;
768 TWPartition* wipe_part = NULL;
769
770 DataManager::GetValue("tw_wipe_list", Wipe_List);
771 LOGI("wipe list '%s'\n", Wipe_List.c_str());
772 if (!Wipe_List.empty()) {
773 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
774 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
775 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
776 LOGI("wipe_path '%s'\n", wipe_path.c_str());
777 if (wipe_path == "/and-sec") {
778 if (!PartitionManager.Wipe_Android_Secure()) {
779 LOGE("Unable to wipe android secure\n");
780 ret_val = false;
781 break;
782 } else {
783 skip = true;
784 }
785 } else if (wipe_path == "DALVIK") {
786 if (!PartitionManager.Wipe_Dalvik_Cache()) {
787 LOGE("Failed to wipe dalvik\n");
788 ret_val = false;
789 break;
790 } else {
791 skip = true;
792 }
793 }
794 if (!skip) {
795 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
796 LOGE("Unable to wipe '%s'\n", wipe_path.c_str());
797 ret_val = false;
798 break;
799 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
800 arg = wipe_path;
801 }
802 } else {
803 skip = false;
804 }
805 start_pos = end_pos + 1;
806 end_pos = Wipe_List.find(";", start_pos);
807 }
808 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400809 } else
810 ret_val = PartitionManager.Wipe_By_Path(arg);
811
812 if (arg == DataManager::GetSettingsStoragePath()) {
813 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
814 string Storage_Path = DataManager::GetSettingsStoragePath();
815
816 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
817 LOGI("Making TWRP folder and saving settings.\n");
818 Storage_Path += "/TWRP";
819 mkdir(Storage_Path.c_str(), 0777);
820 DataManager::Flush();
821 } else {
822 LOGE("Unable to recreate TWRP folder and save settings.\n");
823 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400824 }
825 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400826 PartitionManager.Update_System_Details();
Dees_Troy38bd7602012-09-14 13:33:53 -0400827 if (ret_val)
828 ret_val = 0; // 0 is success
829 else
830 ret_val = 1; // 1 is failure
Dees_Troy51a0e822012-09-05 15:24:24 -0400831 operation_end(ret_val, simulate);
832 return 0;
833 }
834 if (function == "refreshsizes")
835 {
836 operation_start("Refreshing Sizes");
837 if (simulate) {
838 simulate_progress_bar();
839 } else
Dees_Troy5bf43922012-09-07 16:07:55 -0400840 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400841 operation_end(0, simulate);
842 }
843 if (function == "nandroid")
844 {
845 operation_start("Nandroid");
Dees_Troy43d8b002012-09-17 16:00:01 -0400846 int ret = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400847
848 if (simulate) {
849 DataManager::SetValue("tw_partition", "Simulation");
850 simulate_progress_bar();
851 } else {
852 if (arg == "backup") {
853 string Backup_Name;
854 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500855 if (Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(true) == 0) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400856 ret = PartitionManager.Run_Backup();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500857 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400858 else {
859 operation_end(1, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400860 return -1;
Dees_Troy43d8b002012-09-17 16:00:01 -0400861 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400862 DataManager::SetValue(TW_BACKUP_NAME, "(Current Date)");
863 } else if (arg == "restore") {
864 string Restore_Name;
865 DataManager::GetValue("tw_restore", Restore_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400866 ret = PartitionManager.Run_Restore(Restore_Name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400867 } else {
868 operation_end(1, simulate);
869 return -1;
870 }
871 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400872 if (ret == false)
873 ret = 1; // 1 for failure
874 else
875 ret = 0; // 0 for success
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500876 operation_end(ret, simulate);
877 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400878 }
879 if (function == "fixpermissions")
880 {
881 operation_start("Fix Permissions");
882 LOGI("fix permissions started!\n");
883 if (simulate) {
884 simulate_progress_bar();
Dees_Troy4be841b2012-09-26 14:07:15 -0400885 } else {
Dees_Troy6480ce02012-10-10 10:26:54 -0400886 int op_status = PartitionManager.Fix_Permissions();
887 if (op_status != 0)
888 op_status = 1; // failure
889 operation_end(op_status, simulate);
Dees_Troy4be841b2012-09-26 14:07:15 -0400890 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400891 return 0;
892 }
893 if (function == "dd")
894 {
895 operation_start("imaging");
896
897 if (simulate) {
898 simulate_progress_bar();
899 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500900 string result;
901 string cmd = "dd " + arg;
902 TWFunc::Exec_Cmd(cmd, result);
Dees_Troy51a0e822012-09-05 15:24:24 -0400903 }
904 operation_end(0, simulate);
905 return 0;
906 }
907 if (function == "partitionsd")
908 {
909 operation_start("Partition SD Card");
Dees_Troy9350b8d2012-09-27 12:38:38 -0400910 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400911
912 if (simulate) {
913 simulate_progress_bar();
914 } else {
915 int allow_partition;
916 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
917 if (allow_partition == 0) {
918 ui_print("This device does not have a real SD Card!\nAborting!\n");
919 } else {
Dees_Troy9350b8d2012-09-27 12:38:38 -0400920 if (!PartitionManager.Partition_SDCard())
921 ret_val = 1; // failed
Dees_Troy51a0e822012-09-05 15:24:24 -0400922 }
923 }
Dees_Troy9350b8d2012-09-27 12:38:38 -0400924 operation_end(ret_val, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400925 return 0;
926 }
927 if (function == "installhtcdumlock")
928 {
929 operation_start("Install HTC Dumlock");
930 if (simulate) {
931 simulate_progress_bar();
932 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400933 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -0400934
935 operation_end(0, simulate);
936 return 0;
937 }
938 if (function == "htcdumlockrestoreboot")
939 {
940 operation_start("HTC Dumlock Restore Boot");
941 if (simulate) {
942 simulate_progress_bar();
943 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400944 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -0400945
946 operation_end(0, simulate);
947 return 0;
948 }
949 if (function == "htcdumlockreflashrecovery")
950 {
951 operation_start("HTC Dumlock Reflash Recovery");
952 if (simulate) {
953 simulate_progress_bar();
954 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400955 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -0400956
957 operation_end(0, simulate);
958 return 0;
959 }
960 if (function == "cmd")
961 {
962 int op_status = 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500963 string result;
Dees_Troy51a0e822012-09-05 15:24:24 -0400964
965 operation_start("Command");
966 LOGI("Running command: '%s'\n", arg.c_str());
967 if (simulate) {
968 simulate_progress_bar();
969 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500970 op_status = TWFunc::Exec_Cmd(arg, result);
Dees_Troy51a0e822012-09-05 15:24:24 -0400971 if (op_status != 0)
972 op_status = 1;
973 }
974
975 operation_end(op_status, simulate);
976 return 0;
977 }
978 if (function == "terminalcommand")
979 {
980 int op_status = 0;
981 string cmdpath, command;
982
983 DataManager::GetValue("tw_terminal_location", cmdpath);
984 operation_start("CommandOutput");
985 ui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
986 if (simulate) {
987 simulate_progress_bar();
988 operation_end(op_status, simulate);
989 } else {
Dees_Troy4be841b2012-09-26 14:07:15 -0400990 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
Dees_Troy51a0e822012-09-05 15:24:24 -0400991 LOGI("Actual command is: '%s'\n", command.c_str());
992 DataManager::SetValue("tw_terminal_command_thread", command);
993 DataManager::SetValue("tw_terminal_state", 1);
994 DataManager::SetValue("tw_background_thread_running", 1);
995 op_status = pthread_create(&terminal_command, NULL, command_thread, NULL);
996 if (op_status != 0) {
997 LOGE("Error starting terminal command thread, %i.\n", op_status);
998 DataManager::SetValue("tw_terminal_state", 0);
999 DataManager::SetValue("tw_background_thread_running", 0);
1000 operation_end(1, simulate);
1001 }
1002 }
1003 return 0;
1004 }
1005 if (function == "killterminal")
1006 {
1007 int op_status = 0;
1008
1009 LOGI("Sending kill command...\n");
1010 operation_start("KillCommand");
1011 DataManager::SetValue("tw_operation_status", 0);
1012 DataManager::SetValue("tw_operation_state", 1);
1013 DataManager::SetValue("tw_terminal_state", 0);
1014 DataManager::SetValue("tw_background_thread_running", 0);
1015 DataManager::SetValue(TW_ACTION_BUSY, 0);
1016 return 0;
1017 }
1018 if (function == "reinjecttwrp")
1019 {
1020 int op_status = 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001021 string result;
Dees_Troy51a0e822012-09-05 15:24:24 -04001022 operation_start("ReinjectTWRP");
1023 ui_print("Injecting TWRP into boot image...\n");
1024 if (simulate) {
1025 simulate_progress_bar();
1026 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001027 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash", result);
Dees_Troy51a0e822012-09-05 15:24:24 -04001028 ui_print("TWRP injection complete.\n");
1029 }
1030
1031 operation_end(op_status, simulate);
1032 return 0;
1033 }
1034 if (function == "checkbackupname")
1035 {
1036 int op_status = 0;
1037
1038 operation_start("CheckBackupName");
1039 if (simulate) {
1040 simulate_progress_bar();
1041 } else {
Dees_Troyc9ff7a32012-09-27 10:09:41 -04001042 op_status = PartitionManager.Check_Backup_Name(true);
Dees_Troy51a0e822012-09-05 15:24:24 -04001043 if (op_status != 0)
1044 op_status = 1;
1045 }
1046
1047 operation_end(op_status, simulate);
1048 return 0;
1049 }
1050 if (function == "decrypt")
1051 {
1052 int op_status = 0;
1053
1054 operation_start("Decrypt");
1055 if (simulate) {
1056 simulate_progress_bar();
1057 } else {
1058 string Password;
1059 DataManager::GetValue("tw_crypto_password", Password);
Dees_Troy5bf43922012-09-07 16:07:55 -04001060 op_status = PartitionManager.Decrypt_Device(Password);
Dees_Troy51a0e822012-09-05 15:24:24 -04001061 if (op_status != 0)
1062 op_status = 1;
1063 else {
1064 int load_theme = 1;
1065
1066 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001067
1068 if (load_theme) {
1069 int has_datamedia;
1070
1071 // Check for a custom theme and load it if exists
1072 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1073 if (has_datamedia != 0) {
1074 struct stat st;
1075 int check = 0;
1076 std::string theme_path;
1077
1078 theme_path = DataManager::GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -04001079 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -04001080 LOGE("Unable to mount %s during reload function startup.\n", theme_path.c_str());
1081 check = 1;
1082 }
1083
1084 theme_path += "/TWRP/theme/ui.zip";
1085 if (check == 0 && stat(theme_path.c_str(), &st) == 0) {
1086 if (PageManager::ReloadPackage("TWRP", theme_path) != 0)
1087 {
1088 // Loading the custom theme failed - try loading the stock theme
1089 LOGI("Attempting to reload stock theme...\n");
1090 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
1091 {
1092 LOGE("Failed to load base packages.\n");
1093 }
1094 }
1095 }
1096 }
1097 }
1098 }
1099 }
1100
1101 operation_end(op_status, simulate);
1102 return 0;
1103 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001104 if (function == "adbsideload")
1105 {
1106 int ret = 0;
1107
1108 operation_start("Sideload");
1109 if (simulate) {
1110 simulate_progress_bar();
1111 } else {
1112 int wipe_cache = 0;
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001113 int wipe_dalvik = 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001114 string result, Sideload_File;
Dees_Troycfb63ae2012-09-19 14:30:17 -04001115
1116 if (!PartitionManager.Mount_Current_Storage(true)) {
1117 operation_end(1, simulate);
1118 return 0;
1119 }
Dees_Troy9a4b5692012-09-19 15:09:45 -04001120 Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip";
1121 if (TWFunc::Path_Exists(Sideload_File)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001122 unlink(Sideload_File.c_str());
Dees_Troycfb63ae2012-09-19 14:30:17 -04001123 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001124 ui_print("Starting ADB sideload feature...\n");
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001125 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
Dees_Troy9a4b5692012-09-19 15:09:45 -04001126 ret = apply_from_adb(ui, &wipe_cache, Sideload_File.c_str());
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001127 if (ret != 0) {
Dees_Troycfb63ae2012-09-19 14:30:17 -04001128 ret = 1; // failure
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001129 } else {
1130 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1131 PartitionManager.Wipe_By_Path("/cache");
1132 if (wipe_dalvik)
1133 PartitionManager.Wipe_Dalvik_Cache();
1134 }
Dees_Troy06b4fe92012-10-16 11:43:20 -04001135 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
1136 operation_start("ReinjectTWRP");
1137 ui_print("Injecting TWRP into boot image...\n");
1138 if (simulate) {
1139 simulate_progress_bar();
1140 } else {
1141 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
1142 if (Boot == NULL || Boot->Current_File_System != "emmc")
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001143 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash", result);
Dees_Troy06b4fe92012-10-16 11:43:20 -04001144 else {
1145 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001146 TWFunc::Exec_Cmd(injectcmd, result);
Dees_Troy06b4fe92012-10-16 11:43:20 -04001147 }
1148 ui_print("TWRP injection complete.\n");
1149 }
1150 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001151 }
1152 operation_end(ret, simulate);
1153 return 0;
1154 }
Dees_Troycfb63ae2012-09-19 14:30:17 -04001155 if (function == "adbsideloadcancel")
1156 {
1157 int child_pid;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001158 string Sideload_File;
Dees_Troy9a4b5692012-09-19 15:09:45 -04001159 Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip";
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001160 unlink(Sideload_File.c_str());
Dees_Troycfb63ae2012-09-19 14:30:17 -04001161 DataManager::GetValue("tw_child_pid", child_pid);
1162 ui_print("Cancelling ADB sideload...\n");
1163 kill(child_pid, SIGTERM);
Dees_Troy4bc09ae2013-01-18 17:00:54 +00001164 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
Dees_Troycfb63ae2012-09-19 14:30:17 -04001165 return 0;
1166 }
Dees_Troy6ed34b72013-01-25 15:01:29 +00001167 if (function == "openrecoveryscript") {
1168 operation_start("OpenRecoveryScript");
1169 if (simulate) {
1170 simulate_progress_bar();
1171 } else {
1172 // Check for the SCRIPT_FILE_TMP first as these are AOSP recovery commands
1173 // that we converted to ORS commands during boot in recovery.cpp.
1174 // Run those first.
1175 int reboot = 0;
1176 if (TWFunc::Path_Exists(SCRIPT_FILE_TMP)) {
1177 ui_print("Processing AOSP recovery commands...\n");
1178 if (OpenRecoveryScript::run_script_file() == 0) {
1179 reboot = 1;
1180 }
1181 }
1182 // Check for the ORS file in /cache and attempt to run those commands.
1183 if (OpenRecoveryScript::check_for_script_file()) {
1184 ui_print("Processing OpenRecoveryScript file...\n");
1185 if (OpenRecoveryScript::run_script_file() == 0) {
1186 reboot = 1;
1187 }
1188 }
1189 if (reboot) {
1190 usleep(2000000); // Sleep for 2 seconds before rebooting
1191 TWFunc::tw_reboot(rb_system);
1192 } else {
1193 DataManager::SetValue("tw_page_done", 1);
1194 }
1195 }
1196 }
Dees_Troy6ef66352013-02-21 08:26:57 -06001197 if (function == "installsu")
1198 {
1199 int op_status = 0;
1200
1201 operation_start("Install SuperSU");
1202 if (simulate) {
1203 simulate_progress_bar();
1204 } else {
1205 if (!TWFunc::Install_SuperSU())
1206 op_status = 1;
1207 }
1208
1209 operation_end(op_status, simulate);
1210 return 0;
1211 }
1212 if (function == "fixsu")
1213 {
1214 int op_status = 0;
1215
1216 operation_start("Fixing Superuser Permissions");
1217 if (simulate) {
1218 simulate_progress_bar();
1219 } else {
1220 if (!TWFunc::Fix_su_Perms())
1221 op_status = 1;
1222 }
1223
1224 operation_end(op_status, simulate);
1225 return 0;
1226 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001227 }
1228 else
1229 {
1230 pthread_t t;
1231 pthread_create(&t, NULL, thread_start, this);
1232 return 0;
1233 }
1234 return -1;
1235}
1236
1237int GUIAction::getKeyByName(std::string key)
1238{
1239 if (key == "home") return KEY_HOME;
1240 else if (key == "menu") return KEY_MENU;
1241 else if (key == "back") return KEY_BACK;
1242 else if (key == "search") return KEY_SEARCH;
1243 else if (key == "voldown") return KEY_VOLUMEDOWN;
1244 else if (key == "volup") return KEY_VOLUMEUP;
1245 else if (key == "power") {
1246 int ret_val;
1247 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1248 if (!ret_val)
1249 return KEY_POWER;
1250 else
1251 return ret_val;
1252 }
1253
1254 return atol(key.c_str());
1255}
1256
1257void* GUIAction::command_thread(void *cookie)
1258{
1259 string command;
1260 FILE* fp;
1261 char line[512];
1262
1263 DataManager::GetValue("tw_terminal_command_thread", command);
Dees_Troy8170a922012-09-18 15:40:25 -04001264 fp = popen(command.c_str(), "r");
Dees_Troy51a0e822012-09-05 15:24:24 -04001265 if (fp == NULL) {
1266 LOGE("Error opening command to run.\n");
1267 } else {
1268 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1269 struct timeval timeout;
1270 fd_set fdset;
1271
1272 while(keep_going)
1273 {
1274 FD_ZERO(&fdset);
1275 FD_SET(fd, &fdset);
1276 timeout.tv_sec = 0;
1277 timeout.tv_usec = 400000;
1278 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1279 if (has_data == 0) {
1280 // Timeout reached
1281 DataManager::GetValue("tw_terminal_state", check);
1282 if (check == 0) {
1283 keep_going = 0;
1284 }
1285 } else if (has_data < 0) {
1286 // End of execution
1287 keep_going = 0;
1288 } else {
1289 // Try to read output
Dees_Troy4be841b2012-09-26 14:07:15 -04001290 memset(line, 0, sizeof(line));
Dees_Troy51a0e822012-09-05 15:24:24 -04001291 bytes_read = read(fd, line, sizeof(line));
1292 if (bytes_read > 0)
1293 ui_print("%s", line); // Display output
1294 else
1295 keep_going = 0; // Done executing
1296 }
1297 }
1298 fclose(fp);
1299 }
1300 DataManager::SetValue("tw_operation_status", 0);
1301 DataManager::SetValue("tw_operation_state", 1);
1302 DataManager::SetValue("tw_terminal_state", 0);
1303 DataManager::SetValue("tw_background_thread_running", 0);
1304 DataManager::SetValue(TW_ACTION_BUSY, 0);
1305 return NULL;
1306}