blob: aee56849fee717329a7884c102c388063755b5a7 [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"
Dees_Troy5bf43922012-09-07 16:07:55 -040028#include "../extra-functions.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040029#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();
Dees_Troy5bf43922012-09-07 16:07:55 -0400358 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400359 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);
Dees_Troy5bf43922012-09-07 16:07:55 -0400381 PartitionManager.Set_Restore_Files(Restore_Name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400382 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")
Dees_Troy51127312012-09-08 13:08:49 -0400419 PartitionManager.Mount_By_Path(DataManager::GetStrValue(TW_EXTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400420 else if (arg == "INTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400421 PartitionManager.Mount_By_Path(DataManager::GetStrValue(TW_INTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400422 else
Dees_Troy51127312012-09-08 13:08:49 -0400423 PartitionManager.Mount_By_Path(arg, true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400424 } else
425 ui_print("Simulating actions...\n");
426 return 0;
427 }
428
429 if (function == "umount" || function == "unmount")
430 {
431 if (arg == "usb")
432 {
433 if (!simulate)
434 usb_storage_disable();
435 else
436 ui_print("Simulating actions...\n");
437 DataManager::SetValue(TW_ACTION_BUSY, 0);
438 }
439 else if (!simulate)
440 {
441 string cmd;
442 if (arg == "EXTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400443 PartitionManager.UnMount_By_Path(DataManager::GetStrValue(TW_EXTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400444 else if (arg == "INTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400445 PartitionManager.UnMount_By_Path(DataManager::GetStrValue(TW_INTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400446 else
Dees_Troy51127312012-09-08 13:08:49 -0400447 PartitionManager.UnMount_By_Path(arg, true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400448 } else
449 ui_print("Simulating actions...\n");
450 return 0;
451 }
452
453 if (function == "restoredefaultsettings")
454 {
455 operation_start("Restore Defaults");
456 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
457 ui_print("Simulating actions...\n");
458 else {
459 DataManager::ResetDefaults();
Dees_Troy5bf43922012-09-07 16:07:55 -0400460 PartitionManager.Update_System_Details();
461 PartitionManager.Mount_Current_Storage(true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400462 }
463 operation_end(0, simulate);
464 }
465
466 if (function == "copylog")
467 {
468 operation_start("Copy Log");
469 if (!simulate)
470 {
471 char command[255];
472
Dees_Troy5bf43922012-09-07 16:07:55 -0400473 PartitionManager.Mount_Current_Storage(true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400474 sprintf(command, "cp /tmp/recovery.log %s", DataManager::GetCurrentStoragePath().c_str());
475 __system(command);
476 sync();
477 ui_print("Copied recovery log to %s.\n", DataManager::GetCurrentStoragePath().c_str());
478 } else
479 simulate_progress_bar();
480 operation_end(0, simulate);
481 return 0;
482 }
483
484 if (function == "compute" || function == "addsubtract")
485 {
486 if (arg.find("+") != string::npos)
487 {
488 string varName = arg.substr(0, arg.find('+'));
489 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
490 int amount_to_add = atoi(string_to_add.c_str());
491 int value;
492
493 DataManager::GetValue(varName, value);
494 DataManager::SetValue(varName, value + amount_to_add);
495 return 0;
496 }
497 if (arg.find("-") != string::npos)
498 {
499 string varName = arg.substr(0, arg.find('-'));
500 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
501 int amount_to_subtract = atoi(string_to_subtract.c_str());
502 int value;
503
504 DataManager::GetValue(varName, value);
505 value -= amount_to_subtract;
506 if (value <= 0)
507 value = 0;
508 DataManager::SetValue(varName, value);
509 return 0;
510 }
511 }
512
513 if (function == "setguitimezone")
514 {
515 string SelectedZone;
516 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
517 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
518 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
519
520 int dst;
521 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
522
523 string offset;
524 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
525
526 string NewTimeZone = Zone;
527 if (offset != "0")
528 NewTimeZone += ":" + offset;
529
530 if (dst != 0)
531 NewTimeZone += DSTZone;
532
533 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
534 update_tz_environment_variables();
535 return 0;
536 }
537
538 if (function == "togglestorage") {
539 if (arg == "internal") {
540 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
541 } else if (arg == "external") {
542 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1);
543 }
Dees_Troy51127312012-09-08 13:08:49 -0400544 if (PartitionManager.Mount_Current_Storage(true)) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400545 if (arg == "internal") {
546 // Save the current zip location to the external variable
547 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, DataManager::GetStrValue(TW_ZIP_LOCATION_VAR));
548 // Change the current zip location to the internal variable
549 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetStrValue(TW_ZIP_INTERNAL_VAR));
550 } else if (arg == "external") {
551 // Save the current zip location to the internal variable
552 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, DataManager::GetStrValue(TW_ZIP_LOCATION_VAR));
553 // Change the current zip location to the external variable
554 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetStrValue(TW_ZIP_EXTERNAL_VAR));
555 }
556 } else {
557 // We weren't able to toggle for some reason, restore original setting
558 if (arg == "internal") {
559 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1);
560 } else if (arg == "external") {
561 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
562 }
563 }
564 return 0;
565 }
566
567 if (function == "overlay")
568 return gui_changeOverlay(arg);
569
570 if (function == "queuezip")
571 {
572 if (zip_queue_index >= 10) {
573 ui_print("Maximum zip queue reached!\n");
574 return 0;
575 }
576 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
577 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
578 zip_queue_index++;
579 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
580 }
581 return 0;
582 }
583
584 if (function == "cancelzip")
585 {
586 if (zip_queue_index <= 0) {
587 ui_print("Minimum zip queue reached!\n");
588 return 0;
589 } else {
590 zip_queue_index--;
591 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
592 }
593 return 0;
594 }
595
596 if (function == "queueclear")
597 {
598 zip_queue_index = 0;
599 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
600 return 0;
601 }
602
603 if (function == "sleep")
604 {
605 operation_start("Sleep");
606 usleep(atoi(arg.c_str()));
607 operation_end(0, simulate);
608 return 0;
609 }
610
611 if (isThreaded)
612 {
613 if (function == "fileexists")
614 {
615 struct stat st;
616 string newpath = arg + "/.";
617
618 operation_start("FileExists");
619 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
620 operation_end(0, simulate);
621 else
622 operation_end(1, simulate);
623 }
624
625 if (function == "flash")
626 {
627 int i, ret_val = 0;
628
629 for (i=0; i<zip_queue_index; i++) {
630 operation_start("Flashing");
631 DataManager::SetValue("tw_filename", zip_queue[i]);
632 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
633
634 ret_val = flash_zip(zip_queue[i], arg, simulate);
635 if (ret_val != 0) {
636 ui_print("Error flashing zip '%s'\n", zip_queue[i].c_str());
637 i = 10; // Error flashing zip - exit queue
638 ret_val = 1;
639 }
640 }
641 zip_queue_index = 0;
642 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
643
644 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
645 operation_start("ReinjectTWRP");
646 ui_print("Injecting TWRP into boot image...\n");
647 if (simulate) {
648 simulate_progress_bar();
649 } else {
650 __system("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
651 ui_print("TWRP injection complete.\n");
652 }
653 }
654 operation_end(ret_val, simulate);
655 return 0;
656 }
657 if (function == "wipe")
658 {
659 operation_start("Format");
660 DataManager::SetValue("tw_partition", arg);
661
662 int ret_val = 0;
663
664 if (simulate) {
665 simulate_progress_bar();
666 } else {
667 if (arg == "data")
Dees_Troy5bf43922012-09-07 16:07:55 -0400668 PartitionManager.Factory_Reset();
Dees_Troy51a0e822012-09-05 15:24:24 -0400669 else if (arg == "battery")
670 wipe_battery_stats();
671 else if (arg == "rotate")
672 wipe_rotate_data();
673 else if (arg == "dalvik")
674 wipe_dalvik_cache();
675 else if (arg == "DATAMEDIA") {
676 LOGE("TODO: Implement formatting of datamedia device!\n");
677 ret_val = 1; //format_data_media();
678 int has_datamedia, dual_storage;
679
680 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
681 DataManager::GetValue(TW_HAS_DUAL_STORAGE, dual_storage);
682 if (has_datamedia && !dual_storage) {
683 system("umount /sdcard");
684 system("mount /data/media /sdcard");
685 }
686 } else if (arg == "INTERNAL") {
687 int has_datamedia, dual_storage;
688
689 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
690 if (has_datamedia) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400691 PartitionManager.Mount_By_Path("/data", 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400692 __system("rm -rf /data/media");
693 __system("cd /data && mkdir media && chmod 775 media");
694 DataManager::GetValue(TW_HAS_DUAL_STORAGE, dual_storage);
695 if (!dual_storage) {
696 system("umount /sdcard");
697 system("mount /data/media /sdcard");
698 }
699 } else {
700 ret_val = 0;
701 LOGE("Wipe not implemented yet!\n");
702 }
703 } else if (arg == "EXTERNAL") {
704 ret_val = 0;
705 LOGE("Wipe not implemented yet!\n");
706 } else
Dees_Troy5bf43922012-09-07 16:07:55 -0400707 PartitionManager.Wipe_By_Path(arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400708
709 if (arg == "/sdcard") {
Dees_Troy5bf43922012-09-07 16:07:55 -0400710 PartitionManager.Mount_By_Path("/sdcard", 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400711 mkdir("/sdcard/TWRP", 0777);
712 DataManager::Flush();
713 }
714 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400715 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400716 if (ret_val != 0)
717 ret_val = 1;
718 operation_end(ret_val, simulate);
719 return 0;
720 }
721 if (function == "refreshsizes")
722 {
723 operation_start("Refreshing Sizes");
724 if (simulate) {
725 simulate_progress_bar();
726 } else
Dees_Troy5bf43922012-09-07 16:07:55 -0400727 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400728 operation_end(0, simulate);
729 }
730 if (function == "nandroid")
731 {
732 operation_start("Nandroid");
733
734 if (simulate) {
735 DataManager::SetValue("tw_partition", "Simulation");
736 simulate_progress_bar();
737 } else {
738 if (arg == "backup") {
739 string Backup_Name;
740 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
741 if (Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name == "(" || check_backup_name(1))
Dees_Troy5bf43922012-09-07 16:07:55 -0400742 PartitionManager.Run_Backup(Backup_Name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400743 else
744 return -1;
745 DataManager::SetValue(TW_BACKUP_NAME, "(Current Date)");
746 } else if (arg == "restore") {
747 string Restore_Name;
748 DataManager::GetValue("tw_restore", Restore_Name);
Dees_Troy5bf43922012-09-07 16:07:55 -0400749 PartitionManager.Run_Restore(Restore_Name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400750 } else {
751 operation_end(1, simulate);
752 return -1;
753 }
754 }
755 operation_end(0, simulate);
756 return 0;
757 }
758 if (function == "fixpermissions")
759 {
760 operation_start("Fix Permissions");
761 LOGI("fix permissions started!\n");
762 if (simulate) {
763 simulate_progress_bar();
764 } else
765 fix_perms();
766
767 LOGI("fix permissions DONE!\n");
768 operation_end(0, simulate);
769 return 0;
770 }
771 if (function == "dd")
772 {
773 operation_start("imaging");
774
775 if (simulate) {
776 simulate_progress_bar();
777 } else {
778 char cmd[512];
779 sprintf(cmd, "dd %s", arg.c_str());
780 __system(cmd);
781 }
782 operation_end(0, simulate);
783 return 0;
784 }
785 if (function == "partitionsd")
786 {
787 operation_start("Partition SD Card");
788
789 if (simulate) {
790 simulate_progress_bar();
791 } else {
792 int allow_partition;
793 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
794 if (allow_partition == 0) {
795 ui_print("This device does not have a real SD Card!\nAborting!\n");
796 } else {
797 // Below seen in Koush's recovery
798 char sddevice[256];
799 char mkdir_path[255];
800 Volume *vol = volume_for_path("/sdcard");
801 strcpy(sddevice, vol->device);
802 // Just need block not whole partition
803 sddevice[strlen("/dev/block/mmcblkX")] = NULL;
804
805 char es[64];
806 std::string ext_format, sd_path;
807 int ext, swap;
808 DataManager::GetValue("tw_sdext_size", ext);
809 DataManager::GetValue("tw_swap_size", swap);
810 DataManager::GetValue("tw_sdpart_file_system", ext_format);
811 sprintf(es, "/sbin/sdparted -es %dM -ss %dM -efs ext3 -s > /cache/part.log",ext,swap);
812 LOGI("\nrunning script: %s\n", es);
813 run_script("\nContinue partitioning?",
814 "\nPartitioning sdcard : ",
815 es,
816 "\nunable to execute parted!\n(%s)\n",
817 "\nOops... something went wrong!\nPlease check the recovery log!\n",
818 "\nPartitioning complete!\n\n",
819 "\nPartitioning aborted!\n\n", 0);
820
821 // recreate TWRP folder and rewrite settings - these will be gone after sdcard is partitioned
822#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy5bf43922012-09-07 16:07:55 -0400823 PartitionManager.Mount_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400824 DataManager::GetValue(TW_EXTERNAL_PATH, sd_path);
825 memset(mkdir_path, 0, sizeof(mkdir_path));
826 sprintf(mkdir_path, "%s/TWRP", sd_path.c_str());
827#else
Dees_Troy5bf43922012-09-07 16:07:55 -0400828 PartitionManager.Mount_By_Path("/sdcard", 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400829 strcpy(mkdir_path, "/sdcard/TWRP");
830#endif
831 mkdir(mkdir_path, 0777);
832 DataManager::Flush();
833#ifdef TW_EXTERNAL_STORAGE_PATH
834 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
835 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
836 DataManager::SetValue(TW_ZIP_LOCATION_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
837#else
838 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, "/sdcard");
839 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
840 DataManager::SetValue(TW_ZIP_LOCATION_VAR, "/sdcard");
841#endif
842 // This is sometimes needed to make a healthy ext4 partition
843 if (ext > 0 && strcmp(ext_format.c_str(), "ext4") == 0) {
844 char command[256];
845 LOGE("Fix this format command!\n");
846 //sprintf(command, "mke2fs -t ext4 -m 0 %s", sde.blk);
847 ui_print("Formatting sd-ext as ext4...\n");
848 LOGI("Formatting sd-ext after partitioning, command: '%s'\n", command);
849 __system(command);
850 ui_print("DONE\n");
851 }
852
Dees_Troy5bf43922012-09-07 16:07:55 -0400853 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400854 }
855 }
856 operation_end(0, simulate);
857 return 0;
858 }
859 if (function == "installhtcdumlock")
860 {
861 operation_start("Install HTC Dumlock");
862 if (simulate) {
863 simulate_progress_bar();
864 } else
865 install_htc_dumlock();
866
867 operation_end(0, simulate);
868 return 0;
869 }
870 if (function == "htcdumlockrestoreboot")
871 {
872 operation_start("HTC Dumlock Restore Boot");
873 if (simulate) {
874 simulate_progress_bar();
875 } else
876 htc_dumlock_restore_original_boot();
877
878 operation_end(0, simulate);
879 return 0;
880 }
881 if (function == "htcdumlockreflashrecovery")
882 {
883 operation_start("HTC Dumlock Reflash Recovery");
884 if (simulate) {
885 simulate_progress_bar();
886 } else
887 htc_dumlock_reflash_recovery_to_boot();
888
889 operation_end(0, simulate);
890 return 0;
891 }
892 if (function == "cmd")
893 {
894 int op_status = 0;
895
896 operation_start("Command");
897 LOGI("Running command: '%s'\n", arg.c_str());
898 if (simulate) {
899 simulate_progress_bar();
900 } else {
901 op_status = __system(arg.c_str());
902 if (op_status != 0)
903 op_status = 1;
904 }
905
906 operation_end(op_status, simulate);
907 return 0;
908 }
909 if (function == "terminalcommand")
910 {
911 int op_status = 0;
912 string cmdpath, command;
913
914 DataManager::GetValue("tw_terminal_location", cmdpath);
915 operation_start("CommandOutput");
916 ui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
917 if (simulate) {
918 simulate_progress_bar();
919 operation_end(op_status, simulate);
920 } else {
921 command = "cd \"";
922 command += cmdpath;
923 command += "\" && ";
924 command += arg;
925 LOGI("Actual command is: '%s'\n", command.c_str());
926 DataManager::SetValue("tw_terminal_command_thread", command);
927 DataManager::SetValue("tw_terminal_state", 1);
928 DataManager::SetValue("tw_background_thread_running", 1);
929 op_status = pthread_create(&terminal_command, NULL, command_thread, NULL);
930 if (op_status != 0) {
931 LOGE("Error starting terminal command thread, %i.\n", op_status);
932 DataManager::SetValue("tw_terminal_state", 0);
933 DataManager::SetValue("tw_background_thread_running", 0);
934 operation_end(1, simulate);
935 }
936 }
937 return 0;
938 }
939 if (function == "killterminal")
940 {
941 int op_status = 0;
942
943 LOGI("Sending kill command...\n");
944 operation_start("KillCommand");
945 DataManager::SetValue("tw_operation_status", 0);
946 DataManager::SetValue("tw_operation_state", 1);
947 DataManager::SetValue("tw_terminal_state", 0);
948 DataManager::SetValue("tw_background_thread_running", 0);
949 DataManager::SetValue(TW_ACTION_BUSY, 0);
950 return 0;
951 }
952 if (function == "reinjecttwrp")
953 {
954 int op_status = 0;
955
956 operation_start("ReinjectTWRP");
957 ui_print("Injecting TWRP into boot image...\n");
958 if (simulate) {
959 simulate_progress_bar();
960 } else {
961 __system("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
962 ui_print("TWRP injection complete.\n");
963 }
964
965 operation_end(op_status, simulate);
966 return 0;
967 }
968 if (function == "checkbackupname")
969 {
970 int op_status = 0;
971
972 operation_start("CheckBackupName");
973 if (simulate) {
974 simulate_progress_bar();
975 } else {
976 op_status = check_backup_name(1);
977 if (op_status != 0)
978 op_status = 1;
979 }
980
981 operation_end(op_status, simulate);
982 return 0;
983 }
984 if (function == "decrypt")
985 {
986 int op_status = 0;
987
988 operation_start("Decrypt");
989 if (simulate) {
990 simulate_progress_bar();
991 } else {
992 string Password;
993 DataManager::GetValue("tw_crypto_password", Password);
Dees_Troy5bf43922012-09-07 16:07:55 -0400994 op_status = PartitionManager.Decrypt_Device(Password);
Dees_Troy51a0e822012-09-05 15:24:24 -0400995 if (op_status != 0)
996 op_status = 1;
997 else {
998 int load_theme = 1;
999
1000 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1001 DataManager::ReadSettingsFile();
Dees_Troy7d15c252012-09-05 20:47:21 -04001002LOGE("TODO: Implement ORS support\n");
1003 if (0/*check_for_script_file()*/) {
Dees_Troy51a0e822012-09-05 15:24:24 -04001004 ui_print("Processing OpenRecoveryScript file...\n");
Dees_Troy7d15c252012-09-05 20:47:21 -04001005 if (/*run_script_file() ==*/ 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -04001006 usleep(2000000); // Sleep for 2 seconds before rebooting
1007 tw_reboot(rb_system);
1008 load_theme = 0;
1009 }
1010 }
1011
1012 if (load_theme) {
1013 int has_datamedia;
1014
1015 // Check for a custom theme and load it if exists
1016 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1017 if (has_datamedia != 0) {
1018 struct stat st;
1019 int check = 0;
1020 std::string theme_path;
1021
1022 theme_path = DataManager::GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -04001023 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -04001024 LOGE("Unable to mount %s during reload function startup.\n", theme_path.c_str());
1025 check = 1;
1026 }
1027
1028 theme_path += "/TWRP/theme/ui.zip";
1029 if (check == 0 && stat(theme_path.c_str(), &st) == 0) {
1030 if (PageManager::ReloadPackage("TWRP", theme_path) != 0)
1031 {
1032 // Loading the custom theme failed - try loading the stock theme
1033 LOGI("Attempting to reload stock theme...\n");
1034 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
1035 {
1036 LOGE("Failed to load base packages.\n");
1037 }
1038 }
1039 }
1040 }
1041 }
1042 }
1043 }
1044
1045 operation_end(op_status, simulate);
1046 return 0;
1047 }
1048 }
1049 else
1050 {
1051 pthread_t t;
1052 pthread_create(&t, NULL, thread_start, this);
1053 return 0;
1054 }
1055 return -1;
1056}
1057
1058int GUIAction::getKeyByName(std::string key)
1059{
1060 if (key == "home") return KEY_HOME;
1061 else if (key == "menu") return KEY_MENU;
1062 else if (key == "back") return KEY_BACK;
1063 else if (key == "search") return KEY_SEARCH;
1064 else if (key == "voldown") return KEY_VOLUMEDOWN;
1065 else if (key == "volup") return KEY_VOLUMEUP;
1066 else if (key == "power") {
1067 int ret_val;
1068 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1069 if (!ret_val)
1070 return KEY_POWER;
1071 else
1072 return ret_val;
1073 }
1074
1075 return atol(key.c_str());
1076}
1077
1078void* GUIAction::command_thread(void *cookie)
1079{
1080 string command;
1081 FILE* fp;
1082 char line[512];
1083
1084 DataManager::GetValue("tw_terminal_command_thread", command);
1085 fp = __popen(command.c_str(), "r");
1086 if (fp == NULL) {
1087 LOGE("Error opening command to run.\n");
1088 } else {
1089 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1090 struct timeval timeout;
1091 fd_set fdset;
1092
1093 while(keep_going)
1094 {
1095 FD_ZERO(&fdset);
1096 FD_SET(fd, &fdset);
1097 timeout.tv_sec = 0;
1098 timeout.tv_usec = 400000;
1099 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1100 if (has_data == 0) {
1101 // Timeout reached
1102 DataManager::GetValue("tw_terminal_state", check);
1103 if (check == 0) {
1104 keep_going = 0;
1105 }
1106 } else if (has_data < 0) {
1107 // End of execution
1108 keep_going = 0;
1109 } else {
1110 // Try to read output
1111 bytes_read = read(fd, line, sizeof(line));
1112 if (bytes_read > 0)
1113 ui_print("%s", line); // Display output
1114 else
1115 keep_going = 0; // Done executing
1116 }
1117 }
1118 fclose(fp);
1119 }
1120 DataManager::SetValue("tw_operation_status", 0);
1121 DataManager::SetValue("tw_operation_state", 1);
1122 DataManager::SetValue("tw_terminal_state", 0);
1123 DataManager::SetValue("tw_background_thread_running", 0);
1124 DataManager::SetValue(TW_ACTION_BUSY, 0);
1125 return NULL;
1126}