blob: 9c3ee59166cf9eaa634be0c774a04d2e70aa7778 [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001// image.cpp - GUIImage object
2
3#include <stdarg.h>
4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
7#include <fcntl.h>
8#include <sys/stat.h>
9#include <sys/time.h>
10#include <sys/mman.h>
11#include <sys/types.h>
12#include <sys/ioctl.h>
13#include <linux/input.h>
14#include <time.h>
15#include <unistd.h>
16#include <stdlib.h>
Dees_Troy657c3092012-09-10 20:32:10 -040017#include <sys/wait.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040018
19#include <string>
20#include <sstream>
21#include "../partitions.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040022#include "../twrp-functions.hpp"
Dees_Troy812660f2012-09-20 09:55:17 -040023#include "../openrecoveryscript.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040024
Dees_Troy43d8b002012-09-17 16:00:01 -040025#include "../ui.h"
26#include "../adb_install.h"
27
Dees_Troy51a0e822012-09-05 15:24:24 -040028extern "C" {
29#include "../common.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040030#include "../minuitwrp/minui.h"
31#include "../recovery_ui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040032#include "../variables.h"
Dees_Troy32c8eb82012-09-11 15:28:06 -040033#include "../twinstall.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040034
Dees_Troy43d8b002012-09-17 16:00:01 -040035#include "../minadbd/adb.h"
36
Dees_Troy32c8eb82012-09-11 15:28:06 -040037int TWinstall_zip(const char* path, int* wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -040038void 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 -040039int gui_console_only();
Dees_Troy51a0e822012-09-05 15:24:24 -040040int gui_start();
41};
42
43#include "rapidxml.hpp"
44#include "objects.hpp"
45
Dees_Troy43d8b002012-09-17 16:00:01 -040046extern RecoveryUI* ui;
47
Dees_Troy51a0e822012-09-05 15:24:24 -040048void curtainClose(void);
49
50GUIAction::GUIAction(xml_node<>* node)
51 : Conditional(node)
52{
53 xml_node<>* child;
54 xml_node<>* actions;
55 xml_attribute<>* attr;
56
57 mKey = 0;
58
59 if (!node) return;
60
61 // First, get the action
62 actions = node->first_node("actions");
63 if (actions) child = actions->first_node("action");
64 else child = node->first_node("action");
65
66 if (!child) return;
67
68 while (child)
69 {
70 Action action;
71
72 attr = child->first_attribute("function");
73 if (!attr) return;
74
75 action.mFunction = attr->value();
76 action.mArg = child->value();
77 mActions.push_back(action);
78
79 child = child->next_sibling("action");
80 }
81
82 // Now, let's get either the key or region
83 child = node->first_node("touch");
84 if (child)
85 {
86 attr = child->first_attribute("key");
87 if (attr)
88 {
89 std::string key = attr->value();
90
91 mKey = getKeyByName(key);
92 }
93 else
94 {
95 attr = child->first_attribute("x");
96 if (!attr) return;
97 mActionX = atol(attr->value());
98 attr = child->first_attribute("y");
99 if (!attr) return;
100 mActionY = atol(attr->value());
101 attr = child->first_attribute("w");
102 if (!attr) return;
103 mActionW = atol(attr->value());
104 attr = child->first_attribute("h");
105 if (!attr) return;
106 mActionH = atol(attr->value());
107 }
108 }
109}
110
111int GUIAction::NotifyTouch(TOUCH_STATE state, int x, int y)
112{
113 if (state == TOUCH_RELEASE)
114 doActions();
115
116 return 0;
117}
118
119int GUIAction::NotifyKey(int key)
120{
121 if (!mKey || key != mKey)
122 return 1;
123
124 doActions();
125 return 0;
126}
127
128int GUIAction::NotifyVarChange(std::string varName, std::string value)
129{
130 if (varName.empty() && !isConditionValid() && !mKey && !mActionW)
131 doActions();
132
133 // This handles notifying the condition system of page start
134 if (varName.empty() && isConditionValid())
135 NotifyPageSet();
136
137 if ((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
138 doActions();
139
140 return 0;
141}
142
143void GUIAction::simulate_progress_bar(void)
144{
145 ui_print("Simulating actions...\n");
146 for (int i = 0; i < 5; i++)
147 {
148 usleep(500000);
149 DataManager::SetValue("ui_progress", i * 20);
150 }
151}
152
Dees_Troy657c3092012-09-10 20:32:10 -0400153int GUIAction::flash_zip(std::string filename, std::string pageName, const int simulate, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400154{
155 int ret_val = 0;
156
157 DataManager::SetValue("ui_progress", 0);
158
159 if (filename.empty())
160 {
161 LOGE("No file specified.\n");
162 return -1;
163 }
164
165 // We're going to jump to this page first, like a loading page
166 gui_changePage(pageName);
167
168 int fd = -1;
169 ZipArchive zip;
170
Dees_Troy657c3092012-09-10 20:32:10 -0400171 if (!PartitionManager.Mount_By_Path(filename, true))
172 return -1;
173
174 if (mzOpenZipArchive(filename.c_str(), &zip))
Dees_Troy51a0e822012-09-05 15:24:24 -0400175 {
176 LOGE("Unable to open zip file.\n");
177 return -1;
178 }
179
180 // Check the zip to see if it has a custom installer theme
181 const ZipEntry* twrp = mzFindZipEntry(&zip, "META-INF/teamwin/twrp.zip");
182 if (twrp != NULL)
183 {
184 unlink("/tmp/twrp.zip");
185 fd = creat("/tmp/twrp.zip", 0666);
186 }
187 if (fd >= 0 && twrp != NULL &&
188 mzExtractZipEntryToFile(&zip, twrp, fd) &&
189 !PageManager::LoadPackage("install", "/tmp/twrp.zip", "main"))
190 {
191 mzCloseZipArchive(&zip);
Dees_Troy657c3092012-09-10 20:32:10 -0400192 PageManager::SelectPackage("install");
Dees_Troy51a0e822012-09-05 15:24:24 -0400193 gui_changePage("main");
194 }
195 else
196 {
197 // In this case, we just use the default page
198 mzCloseZipArchive(&zip);
Dees_Troy657c3092012-09-10 20:32:10 -0400199 gui_changePage(pageName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400200 }
201 if (fd >= 0)
202 close(fd);
203
204 if (simulate) {
205 simulate_progress_bar();
206 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400207 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400208
209 // Now, check if we need to ensure TWRP remains installed...
210 struct stat st;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500211 string result;
Dees_Troy51a0e822012-09-05 15:24:24 -0400212 if (stat("/sbin/installTwrp", &st) == 0)
213 {
214 DataManager::SetValue("tw_operation", "Configuring TWRP");
215 DataManager::SetValue("tw_partition", "");
216 ui_print("Configuring TWRP...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500217 if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall", result) < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400218 {
219 ui_print("Unable to configure TWRP with this kernel.\n");
220 }
221 }
222 }
223
224 // Done
225 DataManager::SetValue("ui_progress", 100);
226 DataManager::SetValue("ui_progress", 0);
227 return ret_val;
228}
229
230int GUIAction::doActions()
231{
232 if (mActions.size() < 1) return -1;
233 if (mActions.size() == 1)
234 return doAction(mActions.at(0), 0);
235
236 // For multi-action, we always use a thread
237 pthread_t t;
Dees_Troyab4963c2013-01-16 20:35:51 +0000238 pthread_attr_t tattr;
239
240 if (pthread_attr_init(&tattr)) {
241 LOGE("Unable to pthread_attr_init\n");
242 return -1;
243 }
244 if (pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_JOINABLE)) {
245 LOGE("Error setting pthread_attr_setdetachstate\n");
246 return -1;
247 }
248 if (pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM)) {
249 LOGE("Error setting pthread_attr_setscope\n");
250 return -1;
251 }
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500252 /*if (pthread_attr_setstacksize(&tattr, 524288)) {
Dees_Troyab4963c2013-01-16 20:35:51 +0000253 LOGE("Error setting pthread_attr_setstacksize\n");
254 return -1;
255 }
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500256 */
Dees_Troy4bc09ae2013-01-18 17:00:54 +0000257 LOGI("Creating thread\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000258 int ret = pthread_create(&t, &tattr, thread_start, this);
Dees_Troyab4963c2013-01-16 20:35:51 +0000259 if (ret) {
260 LOGE("Unable to create more threads for actions... continuing in same thread! %i\n", ret);
261 thread_start(this);
262 } else {
263 if (pthread_join(t, NULL)) {
264 LOGE("Error joining threads\n");
265 } else {
266 LOGI("Thread joined\n");
267 }
268 }
269 if (pthread_attr_destroy(&tattr)) {
270 LOGE("Failed to pthread_attr_destroy\n");
271 return -1;
272 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400273
274 return 0;
275}
276
277void* GUIAction::thread_start(void *cookie)
278{
279 GUIAction* ourThis = (GUIAction*) cookie;
280
281 DataManager::SetValue(TW_ACTION_BUSY, 1);
282
283 if (ourThis->mActions.size() > 1)
284 {
285 std::vector<Action>::iterator iter;
286 for (iter = ourThis->mActions.begin(); iter != ourThis->mActions.end(); iter++)
287 ourThis->doAction(*iter, 1);
288 }
289 else
290 {
291 ourThis->doAction(ourThis->mActions.at(0), 1);
292 }
293 int check = 0;
294 DataManager::GetValue("tw_background_thread_running", check);
295 if (check == 0)
296 DataManager::SetValue(TW_ACTION_BUSY, 0);
297 return NULL;
298}
299
300void GUIAction::operation_start(const string operation_name)
301{
302 DataManager::SetValue(TW_ACTION_BUSY, 1);
303 DataManager::SetValue("ui_progress", 0);
304 DataManager::SetValue("tw_operation", operation_name);
305 DataManager::SetValue("tw_operation_status", 0);
306 DataManager::SetValue("tw_operation_state", 0);
307}
308
309void GUIAction::operation_end(const int operation_status, const int simulate)
310{
311 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400312 DataManager::SetValue("ui_progress", 100);
313 if (simulate) {
314 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
315 if (simulate_fail != 0)
316 DataManager::SetValue("tw_operation_status", 1);
317 else
318 DataManager::SetValue("tw_operation_status", 0);
319 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500320 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400321 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500322 }
323 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400324 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500325 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400326 }
327 DataManager::SetValue("tw_operation_state", 1);
328 DataManager::SetValue(TW_ACTION_BUSY, 0);
329}
330
331int GUIAction::doAction(Action action, int isThreaded /* = 0 */)
332{
333 static string zip_queue[10];
334 static int zip_queue_index;
335 static pthread_t terminal_command;
336 int simulate;
337
338 std::string arg = gui_parse_text(action.mArg);
339
340 std::string function = gui_parse_text(action.mFunction);
341
342 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
343
344 if (function == "reboot")
345 {
346 //curtainClose(); this sometimes causes a crash
347
348 sync();
349
Dees_Troya58bead2012-09-27 09:49:29 -0400350 if (arg == "recovery")
351 TWFunc::tw_reboot(rb_recovery);
352 else if (arg == "poweroff")
353 TWFunc::tw_reboot(rb_poweroff);
354 else if (arg == "bootloader")
355 TWFunc::tw_reboot(rb_bootloader);
356 else if (arg == "download")
357 TWFunc::tw_reboot(rb_download);
358 else
359 TWFunc::tw_reboot(rb_system);
Dees_Troy51a0e822012-09-05 15:24:24 -0400360
361 // This should never occur
362 return -1;
363 }
364 if (function == "home")
365 {
366 PageManager::SelectPackage("TWRP");
367 gui_changePage("main");
368 return 0;
369 }
370
371 if (function == "key")
372 {
373 PageManager::NotifyKey(getKeyByName(arg));
374 return 0;
375 }
376
377 if (function == "page") {
378 std::string page_name = gui_parse_text(arg);
379 return gui_changePage(page_name);
380 }
381
382 if (function == "reload") {
383 int check = 0, ret_val = 0;
384 std::string theme_path;
385
386 operation_start("Reload Theme");
387 theme_path = DataManager::GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -0400388 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400389 LOGE("Unable to mount %s during reload function startup.\n", theme_path.c_str());
390 check = 1;
391 }
392
393 theme_path += "/TWRP/theme/ui.zip";
394 if (check != 0 || PageManager::ReloadPackage("TWRP", theme_path) != 0)
395 {
396 // Loading the custom theme failed - try loading the stock theme
397 LOGI("Attempting to reload stock theme...\n");
398 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
399 {
400 LOGE("Failed to load base packages.\n");
401 ret_val = 1;
402 }
403 }
404 operation_end(ret_val, simulate);
405 }
406
407 if (function == "readBackup")
408 {
409 string Restore_Name;
410 DataManager::GetValue("tw_restore", Restore_Name);
Dees_Troy5bf43922012-09-07 16:07:55 -0400411 PartitionManager.Set_Restore_Files(Restore_Name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400412 return 0;
413 }
414
415 if (function == "set")
416 {
417 if (arg.find('=') != string::npos)
418 {
419 string varName = arg.substr(0, arg.find('='));
420 string value = arg.substr(arg.find('=') + 1, string::npos);
421
422 DataManager::GetValue(value, value);
423 DataManager::SetValue(varName, value);
424 }
425 else
426 DataManager::SetValue(arg, "1");
427 return 0;
428 }
429 if (function == "clear")
430 {
431 DataManager::SetValue(arg, "0");
432 return 0;
433 }
434
435 if (function == "mount")
436 {
437 if (arg == "usb")
438 {
439 DataManager::SetValue(TW_ACTION_BUSY, 1);
440 if (!simulate)
Dees_Troy8170a922012-09-18 15:40:25 -0400441 PartitionManager.usb_storage_enable();
Dees_Troy51a0e822012-09-05 15:24:24 -0400442 else
443 ui_print("Simulating actions...\n");
444 }
445 else if (!simulate)
446 {
447 string cmd;
448 if (arg == "EXTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400449 PartitionManager.Mount_By_Path(DataManager::GetStrValue(TW_EXTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400450 else if (arg == "INTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400451 PartitionManager.Mount_By_Path(DataManager::GetStrValue(TW_INTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400452 else
Dees_Troy51127312012-09-08 13:08:49 -0400453 PartitionManager.Mount_By_Path(arg, true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400454 } else
455 ui_print("Simulating actions...\n");
456 return 0;
457 }
458
459 if (function == "umount" || function == "unmount")
460 {
461 if (arg == "usb")
462 {
463 if (!simulate)
Dees_Troy8170a922012-09-18 15:40:25 -0400464 PartitionManager.usb_storage_disable();
Dees_Troy51a0e822012-09-05 15:24:24 -0400465 else
466 ui_print("Simulating actions...\n");
467 DataManager::SetValue(TW_ACTION_BUSY, 0);
468 }
469 else if (!simulate)
470 {
471 string cmd;
472 if (arg == "EXTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400473 PartitionManager.UnMount_By_Path(DataManager::GetStrValue(TW_EXTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400474 else if (arg == "INTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400475 PartitionManager.UnMount_By_Path(DataManager::GetStrValue(TW_INTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400476 else
Dees_Troy51127312012-09-08 13:08:49 -0400477 PartitionManager.UnMount_By_Path(arg, true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400478 } else
479 ui_print("Simulating actions...\n");
480 return 0;
481 }
482
483 if (function == "restoredefaultsettings")
484 {
485 operation_start("Restore Defaults");
486 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
487 ui_print("Simulating actions...\n");
488 else {
489 DataManager::ResetDefaults();
Dees_Troy5bf43922012-09-07 16:07:55 -0400490 PartitionManager.Update_System_Details();
491 PartitionManager.Mount_Current_Storage(true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400492 }
493 operation_end(0, simulate);
494 }
495
496 if (function == "copylog")
497 {
498 operation_start("Copy Log");
499 if (!simulate)
500 {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500501 string dst;
Dees_Troy5bf43922012-09-07 16:07:55 -0400502 PartitionManager.Mount_Current_Storage(true);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500503 dst = DataManager::GetCurrentStoragePath() + "/recovery.log";
504 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
Dees_Troy51a0e822012-09-05 15:24:24 -0400505 sync();
506 ui_print("Copied recovery log to %s.\n", DataManager::GetCurrentStoragePath().c_str());
507 } else
508 simulate_progress_bar();
509 operation_end(0, simulate);
510 return 0;
511 }
512
513 if (function == "compute" || function == "addsubtract")
514 {
515 if (arg.find("+") != string::npos)
516 {
517 string varName = arg.substr(0, arg.find('+'));
518 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
519 int amount_to_add = atoi(string_to_add.c_str());
520 int value;
521
522 DataManager::GetValue(varName, value);
523 DataManager::SetValue(varName, value + amount_to_add);
524 return 0;
525 }
526 if (arg.find("-") != string::npos)
527 {
528 string varName = arg.substr(0, arg.find('-'));
529 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
530 int amount_to_subtract = atoi(string_to_subtract.c_str());
531 int value;
532
533 DataManager::GetValue(varName, value);
534 value -= amount_to_subtract;
535 if (value <= 0)
536 value = 0;
537 DataManager::SetValue(varName, value);
538 return 0;
539 }
540 }
541
542 if (function == "setguitimezone")
543 {
544 string SelectedZone;
545 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
546 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
547 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
548
549 int dst;
550 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
551
552 string offset;
553 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
554
555 string NewTimeZone = Zone;
556 if (offset != "0")
557 NewTimeZone += ":" + offset;
558
559 if (dst != 0)
560 NewTimeZone += DSTZone;
561
562 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
Dees_Troy8170a922012-09-18 15:40:25 -0400563 DataManager::update_tz_environment_variables();
Dees_Troy51a0e822012-09-05 15:24:24 -0400564 return 0;
565 }
566
567 if (function == "togglestorage") {
568 if (arg == "internal") {
569 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
570 } else if (arg == "external") {
571 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1);
572 }
Dees_Troy51127312012-09-08 13:08:49 -0400573 if (PartitionManager.Mount_Current_Storage(true)) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400574 if (arg == "internal") {
Dees_Troye2920fa2012-09-19 16:18:00 -0400575 string zip_path, zip_root;
576 DataManager::GetValue(TW_ZIP_INTERNAL_VAR, zip_path);
577 zip_root = TWFunc::Get_Root_Path(zip_path);
578#ifdef RECOVERY_SDCARD_ON_DATA
579 #ifndef TW_EXTERNAL_STORAGE_PATH
580 if (zip_root != "/sdcard")
581 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/sdcard");
582 #else
583 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
584 if (zip_root != "/emmc")
585 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/emmc");
586 } else {
587 if (zip_root != "/sdcard")
588 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/sdcard");
589 }
590 #endif
591#else
592 if (zip_root != DataManager::GetCurrentStoragePath())
593 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetCurrentStoragePath());
594#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400595 // Save the current zip location to the external variable
596 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, DataManager::GetStrValue(TW_ZIP_LOCATION_VAR));
597 // Change the current zip location to the internal variable
598 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetStrValue(TW_ZIP_INTERNAL_VAR));
599 } else if (arg == "external") {
Dees_Troye2920fa2012-09-19 16:18:00 -0400600 string zip_path, zip_root;
601 DataManager::GetValue(TW_ZIP_EXTERNAL_VAR, zip_path);
602 zip_root = TWFunc::Get_Root_Path(zip_path);
603 if (zip_root != DataManager::GetCurrentStoragePath()) {
604 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, DataManager::GetCurrentStoragePath());
605 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400606 // Save the current zip location to the internal variable
607 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, DataManager::GetStrValue(TW_ZIP_LOCATION_VAR));
608 // Change the current zip location to the external variable
609 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetStrValue(TW_ZIP_EXTERNAL_VAR));
610 }
611 } else {
612 // We weren't able to toggle for some reason, restore original setting
613 if (arg == "internal") {
614 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1);
615 } else if (arg == "external") {
616 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
617 }
618 }
619 return 0;
620 }
621
622 if (function == "overlay")
623 return gui_changeOverlay(arg);
624
625 if (function == "queuezip")
626 {
627 if (zip_queue_index >= 10) {
628 ui_print("Maximum zip queue reached!\n");
629 return 0;
630 }
631 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
632 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
633 zip_queue_index++;
634 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
635 }
636 return 0;
637 }
638
639 if (function == "cancelzip")
640 {
641 if (zip_queue_index <= 0) {
642 ui_print("Minimum zip queue reached!\n");
643 return 0;
644 } else {
645 zip_queue_index--;
646 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
647 }
648 return 0;
649 }
650
651 if (function == "queueclear")
652 {
653 zip_queue_index = 0;
654 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
655 return 0;
656 }
657
658 if (function == "sleep")
659 {
660 operation_start("Sleep");
661 usleep(atoi(arg.c_str()));
662 operation_end(0, simulate);
663 return 0;
664 }
665
666 if (isThreaded)
667 {
668 if (function == "fileexists")
669 {
670 struct stat st;
671 string newpath = arg + "/.";
672
673 operation_start("FileExists");
674 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
675 operation_end(0, simulate);
676 else
677 operation_end(1, simulate);
678 }
679
680 if (function == "flash")
681 {
Dees_Troy657c3092012-09-10 20:32:10 -0400682 int i, ret_val = 0, wipe_cache = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400683
684 for (i=0; i<zip_queue_index; i++) {
685 operation_start("Flashing");
686 DataManager::SetValue("tw_filename", zip_queue[i]);
687 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
688
Dees_Troy657c3092012-09-10 20:32:10 -0400689 ret_val = flash_zip(zip_queue[i], arg, simulate, &wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400690 if (ret_val != 0) {
691 ui_print("Error flashing zip '%s'\n", zip_queue[i].c_str());
692 i = 10; // Error flashing zip - exit queue
693 ret_val = 1;
694 }
695 }
696 zip_queue_index = 0;
697 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
698
Dees_Troy657c3092012-09-10 20:32:10 -0400699 if (wipe_cache)
700 PartitionManager.Wipe_By_Path("/cache");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500701 string result;
Dees_Troy51a0e822012-09-05 15:24:24 -0400702 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
703 operation_start("ReinjectTWRP");
704 ui_print("Injecting TWRP into boot image...\n");
705 if (simulate) {
706 simulate_progress_bar();
707 } else {
Dees_Troy06b4fe92012-10-16 11:43:20 -0400708 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
709 if (Boot == NULL || Boot->Current_File_System != "emmc")
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500710 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash", result);
Dees_Troy06b4fe92012-10-16 11:43:20 -0400711 else {
712 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 -0500713 TWFunc::Exec_Cmd(injectcmd, result);
Dees_Troy06b4fe92012-10-16 11:43:20 -0400714 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400715 ui_print("TWRP injection complete.\n");
716 }
717 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400718 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400719 operation_end(ret_val, simulate);
720 return 0;
721 }
722 if (function == "wipe")
723 {
724 operation_start("Format");
725 DataManager::SetValue("tw_partition", arg);
726
Dees_Troy38bd7602012-09-14 13:33:53 -0400727 int ret_val = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400728
729 if (simulate) {
730 simulate_progress_bar();
731 } else {
732 if (arg == "data")
Dees_Troy38bd7602012-09-14 13:33:53 -0400733 ret_val = PartitionManager.Factory_Reset();
Dees_Troy51a0e822012-09-05 15:24:24 -0400734 else if (arg == "battery")
Dees_Troy38bd7602012-09-14 13:33:53 -0400735 ret_val = PartitionManager.Wipe_Battery_Stats();
Dees_Troy51a0e822012-09-05 15:24:24 -0400736 else if (arg == "rotate")
Dees_Troy38bd7602012-09-14 13:33:53 -0400737 ret_val = PartitionManager.Wipe_Rotate_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400738 else if (arg == "dalvik")
Dees_Troy38bd7602012-09-14 13:33:53 -0400739 ret_val = PartitionManager.Wipe_Dalvik_Cache();
Dees_Troy51a0e822012-09-05 15:24:24 -0400740 else if (arg == "DATAMEDIA") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400741 ret_val = PartitionManager.Format_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400742 } else if (arg == "INTERNAL") {
743 int has_datamedia, dual_storage;
744
745 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
746 if (has_datamedia) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400747 ret_val = PartitionManager.Wipe_Media_From_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400748 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -0400749 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
Dees_Troy51a0e822012-09-05 15:24:24 -0400750 }
751 } else if (arg == "EXTERNAL") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400752 string External_Path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400753
Dees_Troy38bd7602012-09-14 13:33:53 -0400754 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
755 ret_val = PartitionManager.Wipe_By_Path(External_Path);
Dees_Troy2ff5a8d2012-09-26 14:53:02 -0400756 } else if (arg == "ANDROIDSECURE") {
757 ret_val = PartitionManager.Wipe_Android_Secure();
Dees_Troy38bd7602012-09-14 13:33:53 -0400758 } else
759 ret_val = PartitionManager.Wipe_By_Path(arg);
760
761 if (arg == DataManager::GetSettingsStoragePath()) {
762 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
763 string Storage_Path = DataManager::GetSettingsStoragePath();
764
765 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
766 LOGI("Making TWRP folder and saving settings.\n");
767 Storage_Path += "/TWRP";
768 mkdir(Storage_Path.c_str(), 0777);
769 DataManager::Flush();
770 } else {
771 LOGE("Unable to recreate TWRP folder and save settings.\n");
772 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400773 }
774 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400775 PartitionManager.Update_System_Details();
Dees_Troy38bd7602012-09-14 13:33:53 -0400776 if (ret_val)
777 ret_val = 0; // 0 is success
778 else
779 ret_val = 1; // 1 is failure
Dees_Troy51a0e822012-09-05 15:24:24 -0400780 operation_end(ret_val, simulate);
781 return 0;
782 }
783 if (function == "refreshsizes")
784 {
785 operation_start("Refreshing Sizes");
786 if (simulate) {
787 simulate_progress_bar();
788 } else
Dees_Troy5bf43922012-09-07 16:07:55 -0400789 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400790 operation_end(0, simulate);
791 }
792 if (function == "nandroid")
793 {
794 operation_start("Nandroid");
Dees_Troy43d8b002012-09-17 16:00:01 -0400795 int ret = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400796
797 if (simulate) {
798 DataManager::SetValue("tw_partition", "Simulation");
799 simulate_progress_bar();
800 } else {
801 if (arg == "backup") {
802 string Backup_Name;
803 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500804 if (Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(true) == 0) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400805 ret = PartitionManager.Run_Backup();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500806 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400807 else {
808 operation_end(1, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400809 return -1;
Dees_Troy43d8b002012-09-17 16:00:01 -0400810 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400811 DataManager::SetValue(TW_BACKUP_NAME, "(Current Date)");
812 } else if (arg == "restore") {
813 string Restore_Name;
814 DataManager::GetValue("tw_restore", Restore_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400815 ret = PartitionManager.Run_Restore(Restore_Name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400816 } else {
817 operation_end(1, simulate);
818 return -1;
819 }
820 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400821 if (ret == false)
822 ret = 1; // 1 for failure
823 else
824 ret = 0; // 0 for success
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500825 operation_end(ret, simulate);
826 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400827 }
828 if (function == "fixpermissions")
829 {
830 operation_start("Fix Permissions");
831 LOGI("fix permissions started!\n");
832 if (simulate) {
833 simulate_progress_bar();
Dees_Troy4be841b2012-09-26 14:07:15 -0400834 } else {
Dees_Troy6480ce02012-10-10 10:26:54 -0400835 int op_status = PartitionManager.Fix_Permissions();
836 if (op_status != 0)
837 op_status = 1; // failure
838 operation_end(op_status, simulate);
Dees_Troy4be841b2012-09-26 14:07:15 -0400839 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400840 return 0;
841 }
842 if (function == "dd")
843 {
844 operation_start("imaging");
845
846 if (simulate) {
847 simulate_progress_bar();
848 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500849 string result;
850 string cmd = "dd " + arg;
851 TWFunc::Exec_Cmd(cmd, result);
Dees_Troy51a0e822012-09-05 15:24:24 -0400852 }
853 operation_end(0, simulate);
854 return 0;
855 }
856 if (function == "partitionsd")
857 {
858 operation_start("Partition SD Card");
Dees_Troy9350b8d2012-09-27 12:38:38 -0400859 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400860
861 if (simulate) {
862 simulate_progress_bar();
863 } else {
864 int allow_partition;
865 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
866 if (allow_partition == 0) {
867 ui_print("This device does not have a real SD Card!\nAborting!\n");
868 } else {
Dees_Troy9350b8d2012-09-27 12:38:38 -0400869 if (!PartitionManager.Partition_SDCard())
870 ret_val = 1; // failed
Dees_Troy51a0e822012-09-05 15:24:24 -0400871 }
872 }
Dees_Troy9350b8d2012-09-27 12:38:38 -0400873 operation_end(ret_val, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400874 return 0;
875 }
876 if (function == "installhtcdumlock")
877 {
878 operation_start("Install HTC Dumlock");
879 if (simulate) {
880 simulate_progress_bar();
881 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400882 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -0400883
884 operation_end(0, simulate);
885 return 0;
886 }
887 if (function == "htcdumlockrestoreboot")
888 {
889 operation_start("HTC Dumlock Restore Boot");
890 if (simulate) {
891 simulate_progress_bar();
892 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400893 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -0400894
895 operation_end(0, simulate);
896 return 0;
897 }
898 if (function == "htcdumlockreflashrecovery")
899 {
900 operation_start("HTC Dumlock Reflash Recovery");
901 if (simulate) {
902 simulate_progress_bar();
903 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400904 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -0400905
906 operation_end(0, simulate);
907 return 0;
908 }
909 if (function == "cmd")
910 {
911 int op_status = 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500912 string result;
Dees_Troy51a0e822012-09-05 15:24:24 -0400913
914 operation_start("Command");
915 LOGI("Running command: '%s'\n", arg.c_str());
916 if (simulate) {
917 simulate_progress_bar();
918 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500919 op_status = TWFunc::Exec_Cmd(arg, result);
Dees_Troy51a0e822012-09-05 15:24:24 -0400920 if (op_status != 0)
921 op_status = 1;
922 }
923
924 operation_end(op_status, simulate);
925 return 0;
926 }
927 if (function == "terminalcommand")
928 {
929 int op_status = 0;
930 string cmdpath, command;
931
932 DataManager::GetValue("tw_terminal_location", cmdpath);
933 operation_start("CommandOutput");
934 ui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
935 if (simulate) {
936 simulate_progress_bar();
937 operation_end(op_status, simulate);
938 } else {
Dees_Troy4be841b2012-09-26 14:07:15 -0400939 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
Dees_Troy51a0e822012-09-05 15:24:24 -0400940 LOGI("Actual command is: '%s'\n", command.c_str());
941 DataManager::SetValue("tw_terminal_command_thread", command);
942 DataManager::SetValue("tw_terminal_state", 1);
943 DataManager::SetValue("tw_background_thread_running", 1);
944 op_status = pthread_create(&terminal_command, NULL, command_thread, NULL);
945 if (op_status != 0) {
946 LOGE("Error starting terminal command thread, %i.\n", op_status);
947 DataManager::SetValue("tw_terminal_state", 0);
948 DataManager::SetValue("tw_background_thread_running", 0);
949 operation_end(1, simulate);
950 }
951 }
952 return 0;
953 }
954 if (function == "killterminal")
955 {
956 int op_status = 0;
957
958 LOGI("Sending kill command...\n");
959 operation_start("KillCommand");
960 DataManager::SetValue("tw_operation_status", 0);
961 DataManager::SetValue("tw_operation_state", 1);
962 DataManager::SetValue("tw_terminal_state", 0);
963 DataManager::SetValue("tw_background_thread_running", 0);
964 DataManager::SetValue(TW_ACTION_BUSY, 0);
965 return 0;
966 }
967 if (function == "reinjecttwrp")
968 {
969 int op_status = 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500970 string result;
Dees_Troy51a0e822012-09-05 15:24:24 -0400971 operation_start("ReinjectTWRP");
972 ui_print("Injecting TWRP into boot image...\n");
973 if (simulate) {
974 simulate_progress_bar();
975 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500976 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash", result);
Dees_Troy51a0e822012-09-05 15:24:24 -0400977 ui_print("TWRP injection complete.\n");
978 }
979
980 operation_end(op_status, simulate);
981 return 0;
982 }
983 if (function == "checkbackupname")
984 {
985 int op_status = 0;
986
987 operation_start("CheckBackupName");
988 if (simulate) {
989 simulate_progress_bar();
990 } else {
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400991 op_status = PartitionManager.Check_Backup_Name(true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400992 if (op_status != 0)
993 op_status = 1;
994 }
995
996 operation_end(op_status, simulate);
997 return 0;
998 }
999 if (function == "decrypt")
1000 {
1001 int op_status = 0;
1002
1003 operation_start("Decrypt");
1004 if (simulate) {
1005 simulate_progress_bar();
1006 } else {
1007 string Password;
1008 DataManager::GetValue("tw_crypto_password", Password);
Dees_Troy5bf43922012-09-07 16:07:55 -04001009 op_status = PartitionManager.Decrypt_Device(Password);
Dees_Troy51a0e822012-09-05 15:24:24 -04001010 if (op_status != 0)
1011 op_status = 1;
1012 else {
1013 int load_theme = 1;
1014
1015 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1016 DataManager::ReadSettingsFile();
Dees_Troy01b6d0c2013-01-22 01:41:09 +00001017 // Check for the SCRIPT_FILE_TMP first as these are AOSP recovery commands
1018 // that we converted to ORS commands during boot in recovery.cpp.
1019 // Run those first.
1020 if (TWFunc::Path_Exists(SCRIPT_FILE_TMP)) {
1021 ui_print("Processing AOSP recovery commands...\n");
1022 if (OpenRecoveryScript::run_script_file() == 0) {
1023 usleep(2000000); // Sleep for 2 seconds before rebooting
1024 TWFunc::tw_reboot(rb_system);
1025 load_theme = 0;
1026 }
1027 }
1028 // Check for the ORS file in /cache and attempt to run those commands.
Dees_Troy812660f2012-09-20 09:55:17 -04001029 if (OpenRecoveryScript::check_for_script_file()) {
Dees_Troy51a0e822012-09-05 15:24:24 -04001030 ui_print("Processing OpenRecoveryScript file...\n");
Dees_Troy812660f2012-09-20 09:55:17 -04001031 if (OpenRecoveryScript::run_script_file() == 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -04001032 usleep(2000000); // Sleep for 2 seconds before rebooting
Dees_Troya58bead2012-09-27 09:49:29 -04001033 TWFunc::tw_reboot(rb_system);
Dees_Troy51a0e822012-09-05 15:24:24 -04001034 load_theme = 0;
1035 }
1036 }
1037
1038 if (load_theme) {
1039 int has_datamedia;
1040
1041 // Check for a custom theme and load it if exists
1042 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1043 if (has_datamedia != 0) {
1044 struct stat st;
1045 int check = 0;
1046 std::string theme_path;
1047
1048 theme_path = DataManager::GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -04001049 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -04001050 LOGE("Unable to mount %s during reload function startup.\n", theme_path.c_str());
1051 check = 1;
1052 }
1053
1054 theme_path += "/TWRP/theme/ui.zip";
1055 if (check == 0 && stat(theme_path.c_str(), &st) == 0) {
1056 if (PageManager::ReloadPackage("TWRP", theme_path) != 0)
1057 {
1058 // Loading the custom theme failed - try loading the stock theme
1059 LOGI("Attempting to reload stock theme...\n");
1060 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
1061 {
1062 LOGE("Failed to load base packages.\n");
1063 }
1064 }
1065 }
1066 }
1067 }
1068 }
1069 }
1070
1071 operation_end(op_status, simulate);
1072 return 0;
1073 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001074 if (function == "adbsideload")
1075 {
1076 int ret = 0;
1077
1078 operation_start("Sideload");
1079 if (simulate) {
1080 simulate_progress_bar();
1081 } else {
1082 int wipe_cache = 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001083 string result, Sideload_File;
Dees_Troycfb63ae2012-09-19 14:30:17 -04001084
1085 if (!PartitionManager.Mount_Current_Storage(true)) {
Dees_Troy4bc09ae2013-01-18 17:00:54 +00001086 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
Dees_Troycfb63ae2012-09-19 14:30:17 -04001087 operation_end(1, simulate);
1088 return 0;
1089 }
Dees_Troy9a4b5692012-09-19 15:09:45 -04001090 Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip";
1091 if (TWFunc::Path_Exists(Sideload_File)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001092 unlink(Sideload_File.c_str());
Dees_Troycfb63ae2012-09-19 14:30:17 -04001093 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001094 ui_print("Starting ADB sideload feature...\n");
Dees_Troy9a4b5692012-09-19 15:09:45 -04001095 ret = apply_from_adb(ui, &wipe_cache, Sideload_File.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001096 if (ret != 0)
Dees_Troycfb63ae2012-09-19 14:30:17 -04001097 ret = 1; // failure
1098 else if (wipe_cache)
1099 PartitionManager.Wipe_By_Path("/cache");
Dees_Troy06b4fe92012-10-16 11:43:20 -04001100 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
1101 operation_start("ReinjectTWRP");
1102 ui_print("Injecting TWRP into boot image...\n");
1103 if (simulate) {
1104 simulate_progress_bar();
1105 } else {
1106 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
1107 if (Boot == NULL || Boot->Current_File_System != "emmc")
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001108 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash", result);
Dees_Troy06b4fe92012-10-16 11:43:20 -04001109 else {
1110 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 -05001111 TWFunc::Exec_Cmd(injectcmd, result);
Dees_Troy06b4fe92012-10-16 11:43:20 -04001112 }
1113 ui_print("TWRP injection complete.\n");
1114 }
1115 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001116 }
Dees_Troy4bc09ae2013-01-18 17:00:54 +00001117 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
Dees_Troy43d8b002012-09-17 16:00:01 -04001118 operation_end(ret, simulate);
1119 return 0;
1120 }
Dees_Troycfb63ae2012-09-19 14:30:17 -04001121 if (function == "adbsideloadcancel")
1122 {
1123 int child_pid;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001124 string Sideload_File;
Dees_Troy9a4b5692012-09-19 15:09:45 -04001125 Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip";
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001126 unlink(Sideload_File.c_str());
Dees_Troycfb63ae2012-09-19 14:30:17 -04001127 DataManager::GetValue("tw_child_pid", child_pid);
1128 ui_print("Cancelling ADB sideload...\n");
1129 kill(child_pid, SIGTERM);
Dees_Troy4bc09ae2013-01-18 17:00:54 +00001130 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
Dees_Troycfb63ae2012-09-19 14:30:17 -04001131 return 0;
1132 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001133 }
1134 else
1135 {
1136 pthread_t t;
1137 pthread_create(&t, NULL, thread_start, this);
1138 return 0;
1139 }
1140 return -1;
1141}
1142
1143int GUIAction::getKeyByName(std::string key)
1144{
1145 if (key == "home") return KEY_HOME;
1146 else if (key == "menu") return KEY_MENU;
1147 else if (key == "back") return KEY_BACK;
1148 else if (key == "search") return KEY_SEARCH;
1149 else if (key == "voldown") return KEY_VOLUMEDOWN;
1150 else if (key == "volup") return KEY_VOLUMEUP;
1151 else if (key == "power") {
1152 int ret_val;
1153 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1154 if (!ret_val)
1155 return KEY_POWER;
1156 else
1157 return ret_val;
1158 }
1159
1160 return atol(key.c_str());
1161}
1162
1163void* GUIAction::command_thread(void *cookie)
1164{
1165 string command;
1166 FILE* fp;
1167 char line[512];
1168
1169 DataManager::GetValue("tw_terminal_command_thread", command);
Dees_Troy8170a922012-09-18 15:40:25 -04001170 fp = popen(command.c_str(), "r");
Dees_Troy51a0e822012-09-05 15:24:24 -04001171 if (fp == NULL) {
1172 LOGE("Error opening command to run.\n");
1173 } else {
1174 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1175 struct timeval timeout;
1176 fd_set fdset;
1177
1178 while(keep_going)
1179 {
1180 FD_ZERO(&fdset);
1181 FD_SET(fd, &fdset);
1182 timeout.tv_sec = 0;
1183 timeout.tv_usec = 400000;
1184 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1185 if (has_data == 0) {
1186 // Timeout reached
1187 DataManager::GetValue("tw_terminal_state", check);
1188 if (check == 0) {
1189 keep_going = 0;
1190 }
1191 } else if (has_data < 0) {
1192 // End of execution
1193 keep_going = 0;
1194 } else {
1195 // Try to read output
Dees_Troy4be841b2012-09-26 14:07:15 -04001196 memset(line, 0, sizeof(line));
Dees_Troy51a0e822012-09-05 15:24:24 -04001197 bytes_read = read(fd, line, sizeof(line));
1198 if (bytes_read > 0)
1199 ui_print("%s", line); // Display output
1200 else
1201 keep_going = 0; // Done executing
1202 }
1203 }
1204 fclose(fp);
1205 }
1206 DataManager::SetValue("tw_operation_status", 0);
1207 DataManager::SetValue("tw_operation_state", 1);
1208 DataManager::SetValue("tw_terminal_state", 0);
1209 DataManager::SetValue("tw_background_thread_running", 0);
1210 DataManager::SetValue(TW_ACTION_BUSY, 0);
1211 return NULL;
1212}