blob: 6ca607b34fd6e10c34315edb32238176543f11e7 [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>
17
18#include <string>
19#include <sstream>
20#include "../partitions.hpp"
21
22extern "C" {
23#include "../common.h"
24#include "../roots.h"
25#include "../tw_reboot.h"
26#include "../minuitwrp/minui.h"
27#include "../recovery_ui.h"
28#include "../extra-functions.h"
29#include "../variables.h"
30
31int install_zip_package(const char* zip_path_filename);
32void fix_perms();
33void wipe_dalvik_cache(void);
34int check_backup_name(int show_error);
35void wipe_battery_stats(void);
36void wipe_rotate_data(void);
37int usb_storage_enable(void);
38int usb_storage_disable(void);
39int __system(const char *command);
40FILE * __popen(const char *program, const char *type);
41int __pclose(FILE *iop);
42void 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);
43void update_tz_environment_variables();
44void install_htc_dumlock(void);
45void htc_dumlock_restore_original_boot(void);
46void htc_dumlock_reflash_recovery_to_boot(void);
47int check_for_script_file(void);
48int gui_console_only();
49int run_script_file(void);
50int gui_start();
51};
52
53#include "rapidxml.hpp"
54#include "objects.hpp"
55
56
57void curtainClose(void);
58
59GUIAction::GUIAction(xml_node<>* node)
60 : Conditional(node)
61{
62 xml_node<>* child;
63 xml_node<>* actions;
64 xml_attribute<>* attr;
65
66 mKey = 0;
67
68 if (!node) return;
69
70 // First, get the action
71 actions = node->first_node("actions");
72 if (actions) child = actions->first_node("action");
73 else child = node->first_node("action");
74
75 if (!child) return;
76
77 while (child)
78 {
79 Action action;
80
81 attr = child->first_attribute("function");
82 if (!attr) return;
83
84 action.mFunction = attr->value();
85 action.mArg = child->value();
86 mActions.push_back(action);
87
88 child = child->next_sibling("action");
89 }
90
91 // Now, let's get either the key or region
92 child = node->first_node("touch");
93 if (child)
94 {
95 attr = child->first_attribute("key");
96 if (attr)
97 {
98 std::string key = attr->value();
99
100 mKey = getKeyByName(key);
101 }
102 else
103 {
104 attr = child->first_attribute("x");
105 if (!attr) return;
106 mActionX = atol(attr->value());
107 attr = child->first_attribute("y");
108 if (!attr) return;
109 mActionY = atol(attr->value());
110 attr = child->first_attribute("w");
111 if (!attr) return;
112 mActionW = atol(attr->value());
113 attr = child->first_attribute("h");
114 if (!attr) return;
115 mActionH = atol(attr->value());
116 }
117 }
118}
119
120int GUIAction::NotifyTouch(TOUCH_STATE state, int x, int y)
121{
122 if (state == TOUCH_RELEASE)
123 doActions();
124
125 return 0;
126}
127
128int GUIAction::NotifyKey(int key)
129{
130 if (!mKey || key != mKey)
131 return 1;
132
133 doActions();
134 return 0;
135}
136
137int GUIAction::NotifyVarChange(std::string varName, std::string value)
138{
139 if (varName.empty() && !isConditionValid() && !mKey && !mActionW)
140 doActions();
141
142 // This handles notifying the condition system of page start
143 if (varName.empty() && isConditionValid())
144 NotifyPageSet();
145
146 if ((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
147 doActions();
148
149 return 0;
150}
151
152void GUIAction::simulate_progress_bar(void)
153{
154 ui_print("Simulating actions...\n");
155 for (int i = 0; i < 5; i++)
156 {
157 usleep(500000);
158 DataManager::SetValue("ui_progress", i * 20);
159 }
160}
161
162int GUIAction::flash_zip(std::string filename, std::string pageName, const int simulate)
163{
164 int ret_val = 0;
165
166 DataManager::SetValue("ui_progress", 0);
167
168 if (filename.empty())
169 {
170 LOGE("No file specified.\n");
171 return -1;
172 }
173
174 // We're going to jump to this page first, like a loading page
175 gui_changePage(pageName);
176
177 int fd = -1;
178 ZipArchive zip;
179
180 if (mzOpenZipArchive(filename.c_str(), &zip))
181 {
182 LOGE("Unable to open zip file.\n");
183 return -1;
184 }
185
186 // Check the zip to see if it has a custom installer theme
187 const ZipEntry* twrp = mzFindZipEntry(&zip, "META-INF/teamwin/twrp.zip");
188 if (twrp != NULL)
189 {
190 unlink("/tmp/twrp.zip");
191 fd = creat("/tmp/twrp.zip", 0666);
192 }
193 if (fd >= 0 && twrp != NULL &&
194 mzExtractZipEntryToFile(&zip, twrp, fd) &&
195 !PageManager::LoadPackage("install", "/tmp/twrp.zip", "main"))
196 {
197 mzCloseZipArchive(&zip);
198 PageManager::SelectPackage("install");
199 gui_changePage("main");
200 }
201 else
202 {
203 // In this case, we just use the default page
204 mzCloseZipArchive(&zip);
205 gui_changePage(pageName);
206 }
207 if (fd >= 0)
208 close(fd);
209
210 if (simulate) {
211 simulate_progress_bar();
212 } else {
213 ret_val = install_zip_package(filename.c_str());
214
215 // Now, check if we need to ensure TWRP remains installed...
216 struct stat st;
217 if (stat("/sbin/installTwrp", &st) == 0)
218 {
219 DataManager::SetValue("tw_operation", "Configuring TWRP");
220 DataManager::SetValue("tw_partition", "");
221 ui_print("Configuring TWRP...\n");
222 if (__system("/sbin/installTwrp reinstall") < 0)
223 {
224 ui_print("Unable to configure TWRP with this kernel.\n");
225 }
226 }
227 }
228
229 // Done
230 DataManager::SetValue("ui_progress", 100);
231 DataManager::SetValue("ui_progress", 0);
232 return ret_val;
233}
234
235int GUIAction::doActions()
236{
237 if (mActions.size() < 1) return -1;
238 if (mActions.size() == 1)
239 return doAction(mActions.at(0), 0);
240
241 // For multi-action, we always use a thread
242 pthread_t t;
243 pthread_create(&t, NULL, thread_start, this);
244
245 return 0;
246}
247
248void* GUIAction::thread_start(void *cookie)
249{
250 GUIAction* ourThis = (GUIAction*) cookie;
251
252 DataManager::SetValue(TW_ACTION_BUSY, 1);
253
254 if (ourThis->mActions.size() > 1)
255 {
256 std::vector<Action>::iterator iter;
257 for (iter = ourThis->mActions.begin(); iter != ourThis->mActions.end(); iter++)
258 ourThis->doAction(*iter, 1);
259 }
260 else
261 {
262 ourThis->doAction(ourThis->mActions.at(0), 1);
263 }
264 int check = 0;
265 DataManager::GetValue("tw_background_thread_running", check);
266 if (check == 0)
267 DataManager::SetValue(TW_ACTION_BUSY, 0);
268 return NULL;
269}
270
271void GUIAction::operation_start(const string operation_name)
272{
273 DataManager::SetValue(TW_ACTION_BUSY, 1);
274 DataManager::SetValue("ui_progress", 0);
275 DataManager::SetValue("tw_operation", operation_name);
276 DataManager::SetValue("tw_operation_status", 0);
277 DataManager::SetValue("tw_operation_state", 0);
278}
279
280void GUIAction::operation_end(const int operation_status, const int simulate)
281{
282 int simulate_fail;
283
284 DataManager::SetValue("ui_progress", 100);
285 if (simulate) {
286 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
287 if (simulate_fail != 0)
288 DataManager::SetValue("tw_operation_status", 1);
289 else
290 DataManager::SetValue("tw_operation_status", 0);
291 } else {
292 if (operation_status != 0)
293 DataManager::SetValue("tw_operation_status", 1);
294 else
295 DataManager::SetValue("tw_operation_status", 0);
296 }
297 DataManager::SetValue("tw_operation_state", 1);
298 DataManager::SetValue(TW_ACTION_BUSY, 0);
299}
300
301int GUIAction::doAction(Action action, int isThreaded /* = 0 */)
302{
303 static string zip_queue[10];
304 static int zip_queue_index;
305 static pthread_t terminal_command;
306 int simulate;
307
308 std::string arg = gui_parse_text(action.mArg);
309
310 std::string function = gui_parse_text(action.mFunction);
311
312 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
313
314 if (function == "reboot")
315 {
316 //curtainClose(); this sometimes causes a crash
317
318 sync();
319
320 if (arg == "recovery")
321 tw_reboot(rb_recovery);
322 else if (arg == "poweroff")
323 tw_reboot(rb_poweroff);
324 else if (arg == "bootloader")
325 tw_reboot(rb_bootloader);
326 else if (arg == "download")
327 tw_reboot(rb_download);
328 else
329 tw_reboot(rb_system);
330
331 // This should never occur
332 return -1;
333 }
334 if (function == "home")
335 {
336 PageManager::SelectPackage("TWRP");
337 gui_changePage("main");
338 return 0;
339 }
340
341 if (function == "key")
342 {
343 PageManager::NotifyKey(getKeyByName(arg));
344 return 0;
345 }
346
347 if (function == "page") {
348 std::string page_name = gui_parse_text(arg);
349 return gui_changePage(page_name);
350 }
351
352 if (function == "reload") {
353 int check = 0, ret_val = 0;
354 std::string theme_path;
355
356 operation_start("Reload Theme");
357 theme_path = DataManager::GetSettingsStoragePath();
358 if (TWPartitionManager::Mount_By_Path(theme_path.c_str(), 1) < 0) {
359 LOGE("Unable to mount %s during reload function startup.\n", theme_path.c_str());
360 check = 1;
361 }
362
363 theme_path += "/TWRP/theme/ui.zip";
364 if (check != 0 || PageManager::ReloadPackage("TWRP", theme_path) != 0)
365 {
366 // Loading the custom theme failed - try loading the stock theme
367 LOGI("Attempting to reload stock theme...\n");
368 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
369 {
370 LOGE("Failed to load base packages.\n");
371 ret_val = 1;
372 }
373 }
374 operation_end(ret_val, simulate);
375 }
376
377 if (function == "readBackup")
378 {
379 string Restore_Name;
380 DataManager::GetValue("tw_restore", Restore_Name);
381 TWPartitionManager::Set_Restore_Files(Restore_Name);
382 return 0;
383 }
384
385 if (function == "set")
386 {
387 if (arg.find('=') != string::npos)
388 {
389 string varName = arg.substr(0, arg.find('='));
390 string value = arg.substr(arg.find('=') + 1, string::npos);
391
392 DataManager::GetValue(value, value);
393 DataManager::SetValue(varName, value);
394 }
395 else
396 DataManager::SetValue(arg, "1");
397 return 0;
398 }
399 if (function == "clear")
400 {
401 DataManager::SetValue(arg, "0");
402 return 0;
403 }
404
405 if (function == "mount")
406 {
407 if (arg == "usb")
408 {
409 DataManager::SetValue(TW_ACTION_BUSY, 1);
410 if (!simulate)
411 usb_storage_enable();
412 else
413 ui_print("Simulating actions...\n");
414 }
415 else if (!simulate)
416 {
417 string cmd;
418 if (arg == "EXTERNAL")
419 cmd = "mount " + DataManager::GetStrValue(TW_EXTERNAL_MOUNT);
420 else if (arg == "INTERNAL")
421 cmd = "mount " + DataManager::GetStrValue(TW_INTERNAL_MOUNT);
422 else
423 cmd = "mount " + arg;
424 __system(cmd.c_str());
425 if (arg == "/data" && DataManager::GetIntValue(TW_HAS_DATADATA) == 1)
426 __system("mount /datadata");
427 } else
428 ui_print("Simulating actions...\n");
429 return 0;
430 }
431
432 if (function == "umount" || function == "unmount")
433 {
434 if (arg == "usb")
435 {
436 if (!simulate)
437 usb_storage_disable();
438 else
439 ui_print("Simulating actions...\n");
440 DataManager::SetValue(TW_ACTION_BUSY, 0);
441 }
442 else if (!simulate)
443 {
444 string cmd;
445 if (arg == "EXTERNAL")
446 cmd = "umount " + DataManager::GetStrValue(TW_EXTERNAL_MOUNT);
447 else if (arg == "INTERNAL")
448 cmd = "umount " + DataManager::GetStrValue(TW_INTERNAL_MOUNT);
449 else if (DataManager::GetIntValue(TW_DONT_UNMOUNT_SYSTEM) == 1 && (arg == "system" || arg == "/system"))
450 return 0;
451 else
452 cmd = "umount " + arg;
453 __system(cmd.c_str());
454 if (arg == "/data" && DataManager::GetIntValue(TW_HAS_DATADATA) == 1)
455 __system("umount /datadata");
456 } else
457 ui_print("Simulating actions...\n");
458 return 0;
459 }
460
461 if (function == "restoredefaultsettings")
462 {
463 operation_start("Restore Defaults");
464 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
465 ui_print("Simulating actions...\n");
466 else {
467 DataManager::ResetDefaults();
468 TWPartitionManager::Update_System_Details();
469 TWPartitionManager::Mount_Current_Storage();
470 }
471 operation_end(0, simulate);
472 }
473
474 if (function == "copylog")
475 {
476 operation_start("Copy Log");
477 if (!simulate)
478 {
479 char command[255];
480
481 TWPartitionManager::Mount_Current_Storage();
482 sprintf(command, "cp /tmp/recovery.log %s", DataManager::GetCurrentStoragePath().c_str());
483 __system(command);
484 sync();
485 ui_print("Copied recovery log to %s.\n", DataManager::GetCurrentStoragePath().c_str());
486 } else
487 simulate_progress_bar();
488 operation_end(0, simulate);
489 return 0;
490 }
491
492 if (function == "compute" || function == "addsubtract")
493 {
494 if (arg.find("+") != string::npos)
495 {
496 string varName = arg.substr(0, arg.find('+'));
497 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
498 int amount_to_add = atoi(string_to_add.c_str());
499 int value;
500
501 DataManager::GetValue(varName, value);
502 DataManager::SetValue(varName, value + amount_to_add);
503 return 0;
504 }
505 if (arg.find("-") != string::npos)
506 {
507 string varName = arg.substr(0, arg.find('-'));
508 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
509 int amount_to_subtract = atoi(string_to_subtract.c_str());
510 int value;
511
512 DataManager::GetValue(varName, value);
513 value -= amount_to_subtract;
514 if (value <= 0)
515 value = 0;
516 DataManager::SetValue(varName, value);
517 return 0;
518 }
519 }
520
521 if (function == "setguitimezone")
522 {
523 string SelectedZone;
524 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
525 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
526 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
527
528 int dst;
529 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
530
531 string offset;
532 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
533
534 string NewTimeZone = Zone;
535 if (offset != "0")
536 NewTimeZone += ":" + offset;
537
538 if (dst != 0)
539 NewTimeZone += DSTZone;
540
541 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
542 update_tz_environment_variables();
543 return 0;
544 }
545
546 if (function == "togglestorage") {
547 if (arg == "internal") {
548 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
549 } else if (arg == "external") {
550 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1);
551 }
552 if (TWPartitionManager::Mount_Current_Storage() == 0) {
553 if (arg == "internal") {
554 // Save the current zip location to the external variable
555 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, DataManager::GetStrValue(TW_ZIP_LOCATION_VAR));
556 // Change the current zip location to the internal variable
557 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetStrValue(TW_ZIP_INTERNAL_VAR));
558 } else if (arg == "external") {
559 // Save the current zip location to the internal variable
560 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, DataManager::GetStrValue(TW_ZIP_LOCATION_VAR));
561 // Change the current zip location to the external variable
562 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetStrValue(TW_ZIP_EXTERNAL_VAR));
563 }
564 } else {
565 // We weren't able to toggle for some reason, restore original setting
566 if (arg == "internal") {
567 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1);
568 } else if (arg == "external") {
569 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
570 }
571 }
572 return 0;
573 }
574
575 if (function == "overlay")
576 return gui_changeOverlay(arg);
577
578 if (function == "queuezip")
579 {
580 if (zip_queue_index >= 10) {
581 ui_print("Maximum zip queue reached!\n");
582 return 0;
583 }
584 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
585 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
586 zip_queue_index++;
587 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
588 }
589 return 0;
590 }
591
592 if (function == "cancelzip")
593 {
594 if (zip_queue_index <= 0) {
595 ui_print("Minimum zip queue reached!\n");
596 return 0;
597 } else {
598 zip_queue_index--;
599 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
600 }
601 return 0;
602 }
603
604 if (function == "queueclear")
605 {
606 zip_queue_index = 0;
607 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
608 return 0;
609 }
610
611 if (function == "sleep")
612 {
613 operation_start("Sleep");
614 usleep(atoi(arg.c_str()));
615 operation_end(0, simulate);
616 return 0;
617 }
618
619 if (isThreaded)
620 {
621 if (function == "fileexists")
622 {
623 struct stat st;
624 string newpath = arg + "/.";
625
626 operation_start("FileExists");
627 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
628 operation_end(0, simulate);
629 else
630 operation_end(1, simulate);
631 }
632
633 if (function == "flash")
634 {
635 int i, ret_val = 0;
636
637 for (i=0; i<zip_queue_index; i++) {
638 operation_start("Flashing");
639 DataManager::SetValue("tw_filename", zip_queue[i]);
640 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
641
642 ret_val = flash_zip(zip_queue[i], arg, simulate);
643 if (ret_val != 0) {
644 ui_print("Error flashing zip '%s'\n", zip_queue[i].c_str());
645 i = 10; // Error flashing zip - exit queue
646 ret_val = 1;
647 }
648 }
649 zip_queue_index = 0;
650 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
651
652 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
653 operation_start("ReinjectTWRP");
654 ui_print("Injecting TWRP into boot image...\n");
655 if (simulate) {
656 simulate_progress_bar();
657 } else {
658 __system("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
659 ui_print("TWRP injection complete.\n");
660 }
661 }
662 operation_end(ret_val, simulate);
663 return 0;
664 }
665 if (function == "wipe")
666 {
667 operation_start("Format");
668 DataManager::SetValue("tw_partition", arg);
669
670 int ret_val = 0;
671
672 if (simulate) {
673 simulate_progress_bar();
674 } else {
675 if (arg == "data")
676 TWPartitionManager::Factory_Reset();
677 else if (arg == "battery")
678 wipe_battery_stats();
679 else if (arg == "rotate")
680 wipe_rotate_data();
681 else if (arg == "dalvik")
682 wipe_dalvik_cache();
683 else if (arg == "DATAMEDIA") {
684 LOGE("TODO: Implement formatting of datamedia device!\n");
685 ret_val = 1; //format_data_media();
686 int has_datamedia, dual_storage;
687
688 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
689 DataManager::GetValue(TW_HAS_DUAL_STORAGE, dual_storage);
690 if (has_datamedia && !dual_storage) {
691 system("umount /sdcard");
692 system("mount /data/media /sdcard");
693 }
694 } else if (arg == "INTERNAL") {
695 int has_datamedia, dual_storage;
696
697 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
698 if (has_datamedia) {
699 TWPartitionManager::Mount_By_Path("/data", 1);
700 __system("rm -rf /data/media");
701 __system("cd /data && mkdir media && chmod 775 media");
702 DataManager::GetValue(TW_HAS_DUAL_STORAGE, dual_storage);
703 if (!dual_storage) {
704 system("umount /sdcard");
705 system("mount /data/media /sdcard");
706 }
707 } else {
708 ret_val = 0;
709 LOGE("Wipe not implemented yet!\n");
710 }
711 } else if (arg == "EXTERNAL") {
712 ret_val = 0;
713 LOGE("Wipe not implemented yet!\n");
714 } else
715 TWPartitionManager::Wipe_By_Path(arg);
716
717 if (arg == "/sdcard") {
718 TWPartitionManager::Mount_By_Path("/sdcard", 1);
719 mkdir("/sdcard/TWRP", 0777);
720 DataManager::Flush();
721 }
722 }
723 TWPartitionManager::Update_System_Details();
724 if (ret_val != 0)
725 ret_val = 1;
726 operation_end(ret_val, simulate);
727 return 0;
728 }
729 if (function == "refreshsizes")
730 {
731 operation_start("Refreshing Sizes");
732 if (simulate) {
733 simulate_progress_bar();
734 } else
735 TWPartitionManager::Update_System_Details();
736 operation_end(0, simulate);
737 }
738 if (function == "nandroid")
739 {
740 operation_start("Nandroid");
741
742 if (simulate) {
743 DataManager::SetValue("tw_partition", "Simulation");
744 simulate_progress_bar();
745 } else {
746 if (arg == "backup") {
747 string Backup_Name;
748 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
749 if (Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name == "(" || check_backup_name(1))
750 TWPartitionManager::Run_Backup(Backup_Name);
751 else
752 return -1;
753 DataManager::SetValue(TW_BACKUP_NAME, "(Current Date)");
754 } else if (arg == "restore") {
755 string Restore_Name;
756 DataManager::GetValue("tw_restore", Restore_Name);
757 TWPartitionManager::Run_Restore(Restore_Name);
758 } else {
759 operation_end(1, simulate);
760 return -1;
761 }
762 }
763 operation_end(0, simulate);
764 return 0;
765 }
766 if (function == "fixpermissions")
767 {
768 operation_start("Fix Permissions");
769 LOGI("fix permissions started!\n");
770 if (simulate) {
771 simulate_progress_bar();
772 } else
773 fix_perms();
774
775 LOGI("fix permissions DONE!\n");
776 operation_end(0, simulate);
777 return 0;
778 }
779 if (function == "dd")
780 {
781 operation_start("imaging");
782
783 if (simulate) {
784 simulate_progress_bar();
785 } else {
786 char cmd[512];
787 sprintf(cmd, "dd %s", arg.c_str());
788 __system(cmd);
789 }
790 operation_end(0, simulate);
791 return 0;
792 }
793 if (function == "partitionsd")
794 {
795 operation_start("Partition SD Card");
796
797 if (simulate) {
798 simulate_progress_bar();
799 } else {
800 int allow_partition;
801 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
802 if (allow_partition == 0) {
803 ui_print("This device does not have a real SD Card!\nAborting!\n");
804 } else {
805 // Below seen in Koush's recovery
806 char sddevice[256];
807 char mkdir_path[255];
808 Volume *vol = volume_for_path("/sdcard");
809 strcpy(sddevice, vol->device);
810 // Just need block not whole partition
811 sddevice[strlen("/dev/block/mmcblkX")] = NULL;
812
813 char es[64];
814 std::string ext_format, sd_path;
815 int ext, swap;
816 DataManager::GetValue("tw_sdext_size", ext);
817 DataManager::GetValue("tw_swap_size", swap);
818 DataManager::GetValue("tw_sdpart_file_system", ext_format);
819 sprintf(es, "/sbin/sdparted -es %dM -ss %dM -efs ext3 -s > /cache/part.log",ext,swap);
820 LOGI("\nrunning script: %s\n", es);
821 run_script("\nContinue partitioning?",
822 "\nPartitioning sdcard : ",
823 es,
824 "\nunable to execute parted!\n(%s)\n",
825 "\nOops... something went wrong!\nPlease check the recovery log!\n",
826 "\nPartitioning complete!\n\n",
827 "\nPartitioning aborted!\n\n", 0);
828
829 // recreate TWRP folder and rewrite settings - these will be gone after sdcard is partitioned
830#ifdef TW_EXTERNAL_STORAGE_PATH
831 TWPartitionManager::Mount_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
832 DataManager::GetValue(TW_EXTERNAL_PATH, sd_path);
833 memset(mkdir_path, 0, sizeof(mkdir_path));
834 sprintf(mkdir_path, "%s/TWRP", sd_path.c_str());
835#else
836 TWPartitionManager::Mount_By_Path("/sdcard", 1);
837 strcpy(mkdir_path, "/sdcard/TWRP");
838#endif
839 mkdir(mkdir_path, 0777);
840 DataManager::Flush();
841#ifdef TW_EXTERNAL_STORAGE_PATH
842 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
843 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
844 DataManager::SetValue(TW_ZIP_LOCATION_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
845#else
846 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, "/sdcard");
847 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
848 DataManager::SetValue(TW_ZIP_LOCATION_VAR, "/sdcard");
849#endif
850 // This is sometimes needed to make a healthy ext4 partition
851 if (ext > 0 && strcmp(ext_format.c_str(), "ext4") == 0) {
852 char command[256];
853 LOGE("Fix this format command!\n");
854 //sprintf(command, "mke2fs -t ext4 -m 0 %s", sde.blk);
855 ui_print("Formatting sd-ext as ext4...\n");
856 LOGI("Formatting sd-ext after partitioning, command: '%s'\n", command);
857 __system(command);
858 ui_print("DONE\n");
859 }
860
861 TWPartitionManager::Update_System_Details();
862 }
863 }
864 operation_end(0, simulate);
865 return 0;
866 }
867 if (function == "installhtcdumlock")
868 {
869 operation_start("Install HTC Dumlock");
870 if (simulate) {
871 simulate_progress_bar();
872 } else
873 install_htc_dumlock();
874
875 operation_end(0, simulate);
876 return 0;
877 }
878 if (function == "htcdumlockrestoreboot")
879 {
880 operation_start("HTC Dumlock Restore Boot");
881 if (simulate) {
882 simulate_progress_bar();
883 } else
884 htc_dumlock_restore_original_boot();
885
886 operation_end(0, simulate);
887 return 0;
888 }
889 if (function == "htcdumlockreflashrecovery")
890 {
891 operation_start("HTC Dumlock Reflash Recovery");
892 if (simulate) {
893 simulate_progress_bar();
894 } else
895 htc_dumlock_reflash_recovery_to_boot();
896
897 operation_end(0, simulate);
898 return 0;
899 }
900 if (function == "cmd")
901 {
902 int op_status = 0;
903
904 operation_start("Command");
905 LOGI("Running command: '%s'\n", arg.c_str());
906 if (simulate) {
907 simulate_progress_bar();
908 } else {
909 op_status = __system(arg.c_str());
910 if (op_status != 0)
911 op_status = 1;
912 }
913
914 operation_end(op_status, simulate);
915 return 0;
916 }
917 if (function == "terminalcommand")
918 {
919 int op_status = 0;
920 string cmdpath, command;
921
922 DataManager::GetValue("tw_terminal_location", cmdpath);
923 operation_start("CommandOutput");
924 ui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
925 if (simulate) {
926 simulate_progress_bar();
927 operation_end(op_status, simulate);
928 } else {
929 command = "cd \"";
930 command += cmdpath;
931 command += "\" && ";
932 command += arg;
933 LOGI("Actual command is: '%s'\n", command.c_str());
934 DataManager::SetValue("tw_terminal_command_thread", command);
935 DataManager::SetValue("tw_terminal_state", 1);
936 DataManager::SetValue("tw_background_thread_running", 1);
937 op_status = pthread_create(&terminal_command, NULL, command_thread, NULL);
938 if (op_status != 0) {
939 LOGE("Error starting terminal command thread, %i.\n", op_status);
940 DataManager::SetValue("tw_terminal_state", 0);
941 DataManager::SetValue("tw_background_thread_running", 0);
942 operation_end(1, simulate);
943 }
944 }
945 return 0;
946 }
947 if (function == "killterminal")
948 {
949 int op_status = 0;
950
951 LOGI("Sending kill command...\n");
952 operation_start("KillCommand");
953 DataManager::SetValue("tw_operation_status", 0);
954 DataManager::SetValue("tw_operation_state", 1);
955 DataManager::SetValue("tw_terminal_state", 0);
956 DataManager::SetValue("tw_background_thread_running", 0);
957 DataManager::SetValue(TW_ACTION_BUSY, 0);
958 return 0;
959 }
960 if (function == "reinjecttwrp")
961 {
962 int op_status = 0;
963
964 operation_start("ReinjectTWRP");
965 ui_print("Injecting TWRP into boot image...\n");
966 if (simulate) {
967 simulate_progress_bar();
968 } else {
969 __system("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
970 ui_print("TWRP injection complete.\n");
971 }
972
973 operation_end(op_status, simulate);
974 return 0;
975 }
976 if (function == "checkbackupname")
977 {
978 int op_status = 0;
979
980 operation_start("CheckBackupName");
981 if (simulate) {
982 simulate_progress_bar();
983 } else {
984 op_status = check_backup_name(1);
985 if (op_status != 0)
986 op_status = 1;
987 }
988
989 operation_end(op_status, simulate);
990 return 0;
991 }
992 if (function == "decrypt")
993 {
994 int op_status = 0;
995
996 operation_start("Decrypt");
997 if (simulate) {
998 simulate_progress_bar();
999 } else {
1000 string Password;
1001 DataManager::GetValue("tw_crypto_password", Password);
1002 op_status = TWPartitionManager::Decrypt_Device(Password);
1003 if (op_status != 0)
1004 op_status = 1;
1005 else {
1006 int load_theme = 1;
1007
1008 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1009 DataManager::ReadSettingsFile();
1010
1011 if (check_for_script_file()) {
1012 ui_print("Processing OpenRecoveryScript file...\n");
1013 if (run_script_file() == 0) {
1014 usleep(2000000); // Sleep for 2 seconds before rebooting
1015 tw_reboot(rb_system);
1016 load_theme = 0;
1017 }
1018 }
1019
1020 if (load_theme) {
1021 int has_datamedia;
1022
1023 // Check for a custom theme and load it if exists
1024 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1025 if (has_datamedia != 0) {
1026 struct stat st;
1027 int check = 0;
1028 std::string theme_path;
1029
1030 theme_path = DataManager::GetSettingsStoragePath();
1031 if (TWPartitionManager::Mount_By_Path(theme_path.c_str(), 1) < 0) {
1032 LOGE("Unable to mount %s during reload function startup.\n", theme_path.c_str());
1033 check = 1;
1034 }
1035
1036 theme_path += "/TWRP/theme/ui.zip";
1037 if (check == 0 && stat(theme_path.c_str(), &st) == 0) {
1038 if (PageManager::ReloadPackage("TWRP", theme_path) != 0)
1039 {
1040 // Loading the custom theme failed - try loading the stock theme
1041 LOGI("Attempting to reload stock theme...\n");
1042 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
1043 {
1044 LOGE("Failed to load base packages.\n");
1045 }
1046 }
1047 }
1048 }
1049 }
1050 }
1051 }
1052
1053 operation_end(op_status, simulate);
1054 return 0;
1055 }
1056 }
1057 else
1058 {
1059 pthread_t t;
1060 pthread_create(&t, NULL, thread_start, this);
1061 return 0;
1062 }
1063 return -1;
1064}
1065
1066int GUIAction::getKeyByName(std::string key)
1067{
1068 if (key == "home") return KEY_HOME;
1069 else if (key == "menu") return KEY_MENU;
1070 else if (key == "back") return KEY_BACK;
1071 else if (key == "search") return KEY_SEARCH;
1072 else if (key == "voldown") return KEY_VOLUMEDOWN;
1073 else if (key == "volup") return KEY_VOLUMEUP;
1074 else if (key == "power") {
1075 int ret_val;
1076 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1077 if (!ret_val)
1078 return KEY_POWER;
1079 else
1080 return ret_val;
1081 }
1082
1083 return atol(key.c_str());
1084}
1085
1086void* GUIAction::command_thread(void *cookie)
1087{
1088 string command;
1089 FILE* fp;
1090 char line[512];
1091
1092 DataManager::GetValue("tw_terminal_command_thread", command);
1093 fp = __popen(command.c_str(), "r");
1094 if (fp == NULL) {
1095 LOGE("Error opening command to run.\n");
1096 } else {
1097 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1098 struct timeval timeout;
1099 fd_set fdset;
1100
1101 while(keep_going)
1102 {
1103 FD_ZERO(&fdset);
1104 FD_SET(fd, &fdset);
1105 timeout.tv_sec = 0;
1106 timeout.tv_usec = 400000;
1107 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1108 if (has_data == 0) {
1109 // Timeout reached
1110 DataManager::GetValue("tw_terminal_state", check);
1111 if (check == 0) {
1112 keep_going = 0;
1113 }
1114 } else if (has_data < 0) {
1115 // End of execution
1116 keep_going = 0;
1117 } else {
1118 // Try to read output
1119 bytes_read = read(fd, line, sizeof(line));
1120 if (bytes_read > 0)
1121 ui_print("%s", line); // Display output
1122 else
1123 keep_going = 0; // Done executing
1124 }
1125 }
1126 fclose(fp);
1127 }
1128 DataManager::SetValue("tw_operation_status", 0);
1129 DataManager::SetValue("tw_operation_state", 1);
1130 DataManager::SetValue("tw_terminal_state", 0);
1131 DataManager::SetValue("tw_background_thread_running", 0);
1132 DataManager::SetValue(TW_ACTION_BUSY, 0);
1133 return NULL;
1134}