blob: a1f7dd3dcbe8e06899790a9bf5ad99f76a2b2766 [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"
22
23extern "C" {
24#include "../common.h"
25#include "../roots.h"
26#include "../tw_reboot.h"
27#include "../minuitwrp/minui.h"
28#include "../recovery_ui.h"
Dees_Troy5bf43922012-09-07 16:07:55 -040029#include "../extra-functions.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040030#include "../variables.h"
Dees_Troy32c8eb82012-09-11 15:28:06 -040031#include "../twinstall.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040032
Dees_Troy32c8eb82012-09-11 15:28:06 -040033int TWinstall_zip(const char* path, int* wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -040034void fix_perms();
35void wipe_dalvik_cache(void);
36int check_backup_name(int show_error);
37void wipe_battery_stats(void);
38void wipe_rotate_data(void);
39int usb_storage_enable(void);
40int usb_storage_disable(void);
41int __system(const char *command);
42FILE * __popen(const char *program, const char *type);
43int __pclose(FILE *iop);
44void 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);
45void update_tz_environment_variables();
46void install_htc_dumlock(void);
47void htc_dumlock_restore_original_boot(void);
48void htc_dumlock_reflash_recovery_to_boot(void);
49int check_for_script_file(void);
50int gui_console_only();
51int run_script_file(void);
52int gui_start();
53};
54
55#include "rapidxml.hpp"
56#include "objects.hpp"
57
Dees_Troy51a0e822012-09-05 15:24:24 -040058void curtainClose(void);
59
60GUIAction::GUIAction(xml_node<>* node)
61 : Conditional(node)
62{
63 xml_node<>* child;
64 xml_node<>* actions;
65 xml_attribute<>* attr;
66
67 mKey = 0;
68
69 if (!node) return;
70
71 // First, get the action
72 actions = node->first_node("actions");
73 if (actions) child = actions->first_node("action");
74 else child = node->first_node("action");
75
76 if (!child) return;
77
78 while (child)
79 {
80 Action action;
81
82 attr = child->first_attribute("function");
83 if (!attr) return;
84
85 action.mFunction = attr->value();
86 action.mArg = child->value();
87 mActions.push_back(action);
88
89 child = child->next_sibling("action");
90 }
91
92 // Now, let's get either the key or region
93 child = node->first_node("touch");
94 if (child)
95 {
96 attr = child->first_attribute("key");
97 if (attr)
98 {
99 std::string key = attr->value();
100
101 mKey = getKeyByName(key);
102 }
103 else
104 {
105 attr = child->first_attribute("x");
106 if (!attr) return;
107 mActionX = atol(attr->value());
108 attr = child->first_attribute("y");
109 if (!attr) return;
110 mActionY = atol(attr->value());
111 attr = child->first_attribute("w");
112 if (!attr) return;
113 mActionW = atol(attr->value());
114 attr = child->first_attribute("h");
115 if (!attr) return;
116 mActionH = atol(attr->value());
117 }
118 }
119}
120
121int GUIAction::NotifyTouch(TOUCH_STATE state, int x, int y)
122{
123 if (state == TOUCH_RELEASE)
124 doActions();
125
126 return 0;
127}
128
129int GUIAction::NotifyKey(int key)
130{
131 if (!mKey || key != mKey)
132 return 1;
133
134 doActions();
135 return 0;
136}
137
138int GUIAction::NotifyVarChange(std::string varName, std::string value)
139{
140 if (varName.empty() && !isConditionValid() && !mKey && !mActionW)
141 doActions();
142
143 // This handles notifying the condition system of page start
144 if (varName.empty() && isConditionValid())
145 NotifyPageSet();
146
147 if ((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
148 doActions();
149
150 return 0;
151}
152
153void GUIAction::simulate_progress_bar(void)
154{
155 ui_print("Simulating actions...\n");
156 for (int i = 0; i < 5; i++)
157 {
158 usleep(500000);
159 DataManager::SetValue("ui_progress", i * 20);
160 }
161}
162
Dees_Troy657c3092012-09-10 20:32:10 -0400163int GUIAction::flash_zip(std::string filename, std::string pageName, const int simulate, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400164{
165 int ret_val = 0;
166
167 DataManager::SetValue("ui_progress", 0);
168
169 if (filename.empty())
170 {
171 LOGE("No file specified.\n");
172 return -1;
173 }
174
175 // We're going to jump to this page first, like a loading page
176 gui_changePage(pageName);
177
178 int fd = -1;
179 ZipArchive zip;
180
Dees_Troy657c3092012-09-10 20:32:10 -0400181 if (!PartitionManager.Mount_By_Path(filename, true))
182 return -1;
183
184 if (mzOpenZipArchive(filename.c_str(), &zip))
Dees_Troy51a0e822012-09-05 15:24:24 -0400185 {
186 LOGE("Unable to open zip file.\n");
187 return -1;
188 }
189
190 // Check the zip to see if it has a custom installer theme
191 const ZipEntry* twrp = mzFindZipEntry(&zip, "META-INF/teamwin/twrp.zip");
192 if (twrp != NULL)
193 {
194 unlink("/tmp/twrp.zip");
195 fd = creat("/tmp/twrp.zip", 0666);
196 }
197 if (fd >= 0 && twrp != NULL &&
198 mzExtractZipEntryToFile(&zip, twrp, fd) &&
199 !PageManager::LoadPackage("install", "/tmp/twrp.zip", "main"))
200 {
201 mzCloseZipArchive(&zip);
Dees_Troy657c3092012-09-10 20:32:10 -0400202 PageManager::SelectPackage("install");
Dees_Troy51a0e822012-09-05 15:24:24 -0400203 gui_changePage("main");
204 }
205 else
206 {
207 // In this case, we just use the default page
208 mzCloseZipArchive(&zip);
Dees_Troy657c3092012-09-10 20:32:10 -0400209 gui_changePage(pageName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400210 }
211 if (fd >= 0)
212 close(fd);
213
214 if (simulate) {
215 simulate_progress_bar();
216 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400217 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400218
219 // Now, check if we need to ensure TWRP remains installed...
220 struct stat st;
221 if (stat("/sbin/installTwrp", &st) == 0)
222 {
223 DataManager::SetValue("tw_operation", "Configuring TWRP");
224 DataManager::SetValue("tw_partition", "");
225 ui_print("Configuring TWRP...\n");
226 if (__system("/sbin/installTwrp reinstall") < 0)
227 {
228 ui_print("Unable to configure TWRP with this kernel.\n");
229 }
230 }
231 }
232
233 // Done
234 DataManager::SetValue("ui_progress", 100);
235 DataManager::SetValue("ui_progress", 0);
236 return ret_val;
237}
238
239int GUIAction::doActions()
240{
241 if (mActions.size() < 1) return -1;
242 if (mActions.size() == 1)
243 return doAction(mActions.at(0), 0);
244
245 // For multi-action, we always use a thread
246 pthread_t t;
247 pthread_create(&t, NULL, thread_start, this);
248
249 return 0;
250}
251
252void* GUIAction::thread_start(void *cookie)
253{
254 GUIAction* ourThis = (GUIAction*) cookie;
255
256 DataManager::SetValue(TW_ACTION_BUSY, 1);
257
258 if (ourThis->mActions.size() > 1)
259 {
260 std::vector<Action>::iterator iter;
261 for (iter = ourThis->mActions.begin(); iter != ourThis->mActions.end(); iter++)
262 ourThis->doAction(*iter, 1);
263 }
264 else
265 {
266 ourThis->doAction(ourThis->mActions.at(0), 1);
267 }
268 int check = 0;
269 DataManager::GetValue("tw_background_thread_running", check);
270 if (check == 0)
271 DataManager::SetValue(TW_ACTION_BUSY, 0);
272 return NULL;
273}
274
275void GUIAction::operation_start(const string operation_name)
276{
277 DataManager::SetValue(TW_ACTION_BUSY, 1);
278 DataManager::SetValue("ui_progress", 0);
279 DataManager::SetValue("tw_operation", operation_name);
280 DataManager::SetValue("tw_operation_status", 0);
281 DataManager::SetValue("tw_operation_state", 0);
282}
283
284void GUIAction::operation_end(const int operation_status, const int simulate)
285{
286 int simulate_fail;
287
288 DataManager::SetValue("ui_progress", 100);
289 if (simulate) {
290 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
291 if (simulate_fail != 0)
292 DataManager::SetValue("tw_operation_status", 1);
293 else
294 DataManager::SetValue("tw_operation_status", 0);
295 } else {
296 if (operation_status != 0)
297 DataManager::SetValue("tw_operation_status", 1);
298 else
299 DataManager::SetValue("tw_operation_status", 0);
300 }
301 DataManager::SetValue("tw_operation_state", 1);
302 DataManager::SetValue(TW_ACTION_BUSY, 0);
303}
304
305int GUIAction::doAction(Action action, int isThreaded /* = 0 */)
306{
307 static string zip_queue[10];
308 static int zip_queue_index;
309 static pthread_t terminal_command;
310 int simulate;
311
312 std::string arg = gui_parse_text(action.mArg);
313
314 std::string function = gui_parse_text(action.mFunction);
315
316 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
317
318 if (function == "reboot")
319 {
320 //curtainClose(); this sometimes causes a crash
321
322 sync();
323
324 if (arg == "recovery")
325 tw_reboot(rb_recovery);
326 else if (arg == "poweroff")
327 tw_reboot(rb_poweroff);
328 else if (arg == "bootloader")
329 tw_reboot(rb_bootloader);
330 else if (arg == "download")
331 tw_reboot(rb_download);
332 else
333 tw_reboot(rb_system);
334
335 // This should never occur
336 return -1;
337 }
338 if (function == "home")
339 {
340 PageManager::SelectPackage("TWRP");
341 gui_changePage("main");
342 return 0;
343 }
344
345 if (function == "key")
346 {
347 PageManager::NotifyKey(getKeyByName(arg));
348 return 0;
349 }
350
351 if (function == "page") {
352 std::string page_name = gui_parse_text(arg);
353 return gui_changePage(page_name);
354 }
355
356 if (function == "reload") {
357 int check = 0, ret_val = 0;
358 std::string theme_path;
359
360 operation_start("Reload Theme");
361 theme_path = DataManager::GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -0400362 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400363 LOGE("Unable to mount %s during reload function startup.\n", theme_path.c_str());
364 check = 1;
365 }
366
367 theme_path += "/TWRP/theme/ui.zip";
368 if (check != 0 || PageManager::ReloadPackage("TWRP", theme_path) != 0)
369 {
370 // Loading the custom theme failed - try loading the stock theme
371 LOGI("Attempting to reload stock theme...\n");
372 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
373 {
374 LOGE("Failed to load base packages.\n");
375 ret_val = 1;
376 }
377 }
378 operation_end(ret_val, simulate);
379 }
380
381 if (function == "readBackup")
382 {
383 string Restore_Name;
384 DataManager::GetValue("tw_restore", Restore_Name);
Dees_Troy5bf43922012-09-07 16:07:55 -0400385 PartitionManager.Set_Restore_Files(Restore_Name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400386 return 0;
387 }
388
389 if (function == "set")
390 {
391 if (arg.find('=') != string::npos)
392 {
393 string varName = arg.substr(0, arg.find('='));
394 string value = arg.substr(arg.find('=') + 1, string::npos);
395
396 DataManager::GetValue(value, value);
397 DataManager::SetValue(varName, value);
398 }
399 else
400 DataManager::SetValue(arg, "1");
401 return 0;
402 }
403 if (function == "clear")
404 {
405 DataManager::SetValue(arg, "0");
406 return 0;
407 }
408
409 if (function == "mount")
410 {
411 if (arg == "usb")
412 {
413 DataManager::SetValue(TW_ACTION_BUSY, 1);
414 if (!simulate)
415 usb_storage_enable();
416 else
417 ui_print("Simulating actions...\n");
418 }
419 else if (!simulate)
420 {
421 string cmd;
422 if (arg == "EXTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400423 PartitionManager.Mount_By_Path(DataManager::GetStrValue(TW_EXTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400424 else if (arg == "INTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400425 PartitionManager.Mount_By_Path(DataManager::GetStrValue(TW_INTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400426 else
Dees_Troy51127312012-09-08 13:08:49 -0400427 PartitionManager.Mount_By_Path(arg, true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400428 } else
429 ui_print("Simulating actions...\n");
430 return 0;
431 }
432
433 if (function == "umount" || function == "unmount")
434 {
435 if (arg == "usb")
436 {
437 if (!simulate)
438 usb_storage_disable();
439 else
440 ui_print("Simulating actions...\n");
441 DataManager::SetValue(TW_ACTION_BUSY, 0);
442 }
443 else if (!simulate)
444 {
445 string cmd;
446 if (arg == "EXTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400447 PartitionManager.UnMount_By_Path(DataManager::GetStrValue(TW_EXTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400448 else if (arg == "INTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400449 PartitionManager.UnMount_By_Path(DataManager::GetStrValue(TW_INTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400450 else
Dees_Troy51127312012-09-08 13:08:49 -0400451 PartitionManager.UnMount_By_Path(arg, true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400452 } else
453 ui_print("Simulating actions...\n");
454 return 0;
455 }
456
457 if (function == "restoredefaultsettings")
458 {
459 operation_start("Restore Defaults");
460 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
461 ui_print("Simulating actions...\n");
462 else {
463 DataManager::ResetDefaults();
Dees_Troy5bf43922012-09-07 16:07:55 -0400464 PartitionManager.Update_System_Details();
465 PartitionManager.Mount_Current_Storage(true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400466 }
467 operation_end(0, simulate);
468 }
469
470 if (function == "copylog")
471 {
472 operation_start("Copy Log");
473 if (!simulate)
474 {
475 char command[255];
476
Dees_Troy5bf43922012-09-07 16:07:55 -0400477 PartitionManager.Mount_Current_Storage(true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400478 sprintf(command, "cp /tmp/recovery.log %s", DataManager::GetCurrentStoragePath().c_str());
479 __system(command);
480 sync();
481 ui_print("Copied recovery log to %s.\n", DataManager::GetCurrentStoragePath().c_str());
482 } else
483 simulate_progress_bar();
484 operation_end(0, simulate);
485 return 0;
486 }
487
488 if (function == "compute" || function == "addsubtract")
489 {
490 if (arg.find("+") != string::npos)
491 {
492 string varName = arg.substr(0, arg.find('+'));
493 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
494 int amount_to_add = atoi(string_to_add.c_str());
495 int value;
496
497 DataManager::GetValue(varName, value);
498 DataManager::SetValue(varName, value + amount_to_add);
499 return 0;
500 }
501 if (arg.find("-") != string::npos)
502 {
503 string varName = arg.substr(0, arg.find('-'));
504 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
505 int amount_to_subtract = atoi(string_to_subtract.c_str());
506 int value;
507
508 DataManager::GetValue(varName, value);
509 value -= amount_to_subtract;
510 if (value <= 0)
511 value = 0;
512 DataManager::SetValue(varName, value);
513 return 0;
514 }
515 }
516
517 if (function == "setguitimezone")
518 {
519 string SelectedZone;
520 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
521 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
522 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
523
524 int dst;
525 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
526
527 string offset;
528 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
529
530 string NewTimeZone = Zone;
531 if (offset != "0")
532 NewTimeZone += ":" + offset;
533
534 if (dst != 0)
535 NewTimeZone += DSTZone;
536
537 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
538 update_tz_environment_variables();
539 return 0;
540 }
541
542 if (function == "togglestorage") {
543 if (arg == "internal") {
544 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
545 } else if (arg == "external") {
546 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1);
547 }
Dees_Troy51127312012-09-08 13:08:49 -0400548 if (PartitionManager.Mount_Current_Storage(true)) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400549 if (arg == "internal") {
550 // Save the current zip location to the external variable
551 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, DataManager::GetStrValue(TW_ZIP_LOCATION_VAR));
552 // Change the current zip location to the internal variable
553 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetStrValue(TW_ZIP_INTERNAL_VAR));
554 } else if (arg == "external") {
555 // Save the current zip location to the internal variable
556 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, DataManager::GetStrValue(TW_ZIP_LOCATION_VAR));
557 // Change the current zip location to the external variable
558 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetStrValue(TW_ZIP_EXTERNAL_VAR));
559 }
560 } else {
561 // We weren't able to toggle for some reason, restore original setting
562 if (arg == "internal") {
563 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1);
564 } else if (arg == "external") {
565 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
566 }
567 }
568 return 0;
569 }
570
571 if (function == "overlay")
572 return gui_changeOverlay(arg);
573
574 if (function == "queuezip")
575 {
576 if (zip_queue_index >= 10) {
577 ui_print("Maximum zip queue reached!\n");
578 return 0;
579 }
580 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
581 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
582 zip_queue_index++;
583 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
584 }
585 return 0;
586 }
587
588 if (function == "cancelzip")
589 {
590 if (zip_queue_index <= 0) {
591 ui_print("Minimum zip queue reached!\n");
592 return 0;
593 } else {
594 zip_queue_index--;
595 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
596 }
597 return 0;
598 }
599
600 if (function == "queueclear")
601 {
602 zip_queue_index = 0;
603 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
604 return 0;
605 }
606
607 if (function == "sleep")
608 {
609 operation_start("Sleep");
610 usleep(atoi(arg.c_str()));
611 operation_end(0, simulate);
612 return 0;
613 }
614
615 if (isThreaded)
616 {
617 if (function == "fileexists")
618 {
619 struct stat st;
620 string newpath = arg + "/.";
621
622 operation_start("FileExists");
623 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
624 operation_end(0, simulate);
625 else
626 operation_end(1, simulate);
627 }
628
629 if (function == "flash")
630 {
Dees_Troy657c3092012-09-10 20:32:10 -0400631 int i, ret_val = 0, wipe_cache = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400632
633 for (i=0; i<zip_queue_index; i++) {
634 operation_start("Flashing");
635 DataManager::SetValue("tw_filename", zip_queue[i]);
636 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
637
Dees_Troy657c3092012-09-10 20:32:10 -0400638 ret_val = flash_zip(zip_queue[i], arg, simulate, &wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400639 if (ret_val != 0) {
640 ui_print("Error flashing zip '%s'\n", zip_queue[i].c_str());
641 i = 10; // Error flashing zip - exit queue
642 ret_val = 1;
643 }
644 }
645 zip_queue_index = 0;
646 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
647
Dees_Troy657c3092012-09-10 20:32:10 -0400648 if (wipe_cache)
649 PartitionManager.Wipe_By_Path("/cache");
650
Dees_Troy51a0e822012-09-05 15:24:24 -0400651 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
652 operation_start("ReinjectTWRP");
653 ui_print("Injecting TWRP into boot image...\n");
654 if (simulate) {
655 simulate_progress_bar();
656 } else {
657 __system("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
658 ui_print("TWRP injection complete.\n");
659 }
660 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400661 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400662 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")
Dees_Troy5bf43922012-09-07 16:07:55 -0400676 PartitionManager.Factory_Reset();
Dees_Troy51a0e822012-09-05 15:24:24 -0400677 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) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400699 PartitionManager.Mount_By_Path("/data", 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400700 __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
Dees_Troy5bf43922012-09-07 16:07:55 -0400715 PartitionManager.Wipe_By_Path(arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400716
717 if (arg == "/sdcard") {
Dees_Troy5bf43922012-09-07 16:07:55 -0400718 PartitionManager.Mount_By_Path("/sdcard", 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400719 mkdir("/sdcard/TWRP", 0777);
720 DataManager::Flush();
721 }
722 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400723 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400724 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
Dees_Troy5bf43922012-09-07 16:07:55 -0400735 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400736 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))
Dees_Troy5bf43922012-09-07 16:07:55 -0400750 PartitionManager.Run_Backup(Backup_Name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400751 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);
Dees_Troy5bf43922012-09-07 16:07:55 -0400757 PartitionManager.Run_Restore(Restore_Name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400758 } 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
Dees_Troy5bf43922012-09-07 16:07:55 -0400831 PartitionManager.Mount_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400832 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
Dees_Troy5bf43922012-09-07 16:07:55 -0400836 PartitionManager.Mount_By_Path("/sdcard", 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400837 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
Dees_Troy5bf43922012-09-07 16:07:55 -0400861 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400862 }
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);
Dees_Troy5bf43922012-09-07 16:07:55 -04001002 op_status = PartitionManager.Decrypt_Device(Password);
Dees_Troy51a0e822012-09-05 15:24:24 -04001003 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();
Dees_Troy7d15c252012-09-05 20:47:21 -04001010LOGE("TODO: Implement ORS support\n");
1011 if (0/*check_for_script_file()*/) {
Dees_Troy51a0e822012-09-05 15:24:24 -04001012 ui_print("Processing OpenRecoveryScript file...\n");
Dees_Troy7d15c252012-09-05 20:47:21 -04001013 if (/*run_script_file() ==*/ 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -04001014 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();
Dees_Troy5bf43922012-09-07 16:07:55 -04001031 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -04001032 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}