blob: 15180ca064205e97de9fdfeb0daab879aebda57c [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001// image.cpp - GUIImage object
2
3#include <stdarg.h>
4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
7#include <fcntl.h>
8#include <sys/stat.h>
9#include <sys/time.h>
10#include <sys/mman.h>
11#include <sys/types.h>
12#include <sys/ioctl.h>
13#include <linux/input.h>
14#include <time.h>
15#include <unistd.h>
16#include <stdlib.h>
Dees_Troy657c3092012-09-10 20:32:10 -040017#include <sys/wait.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040018
19#include <string>
20#include <sstream>
21#include "../partitions.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040022#include "../twrp-functions.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040023
Dees_Troy43d8b002012-09-17 16:00:01 -040024#include "../ui.h"
25#include "../adb_install.h"
26
Dees_Troy51a0e822012-09-05 15:24:24 -040027extern "C" {
28#include "../common.h"
29#include "../roots.h"
30#include "../tw_reboot.h"
31#include "../minuitwrp/minui.h"
32#include "../recovery_ui.h"
Dees_Troy5bf43922012-09-07 16:07:55 -040033#include "../extra-functions.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040034#include "../variables.h"
Dees_Troy32c8eb82012-09-11 15:28:06 -040035#include "../twinstall.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040036
Dees_Troy43d8b002012-09-17 16:00:01 -040037#include "../minadbd/adb.h"
38
Dees_Troy32c8eb82012-09-11 15:28:06 -040039int TWinstall_zip(const char* path, int* wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -040040int check_backup_name(int show_error);
Dees_Troy51a0e822012-09-05 15:24:24 -040041void run_script(const char *str1, const char *str2, const char *str3, const char *str4, const char *str5, const char *str6, const char *str7, int request_confirm);
Dees_Troy51a0e822012-09-05 15:24:24 -040042int gui_console_only();
Dees_Troy51a0e822012-09-05 15:24:24 -040043int gui_start();
44};
45
46#include "rapidxml.hpp"
47#include "objects.hpp"
48
Dees_Troy43d8b002012-09-17 16:00:01 -040049extern RecoveryUI* ui;
50
Dees_Troy51a0e822012-09-05 15:24:24 -040051void curtainClose(void);
52
53GUIAction::GUIAction(xml_node<>* node)
54 : Conditional(node)
55{
56 xml_node<>* child;
57 xml_node<>* actions;
58 xml_attribute<>* attr;
59
60 mKey = 0;
61
62 if (!node) return;
63
64 // First, get the action
65 actions = node->first_node("actions");
66 if (actions) child = actions->first_node("action");
67 else child = node->first_node("action");
68
69 if (!child) return;
70
71 while (child)
72 {
73 Action action;
74
75 attr = child->first_attribute("function");
76 if (!attr) return;
77
78 action.mFunction = attr->value();
79 action.mArg = child->value();
80 mActions.push_back(action);
81
82 child = child->next_sibling("action");
83 }
84
85 // Now, let's get either the key or region
86 child = node->first_node("touch");
87 if (child)
88 {
89 attr = child->first_attribute("key");
90 if (attr)
91 {
92 std::string key = attr->value();
93
94 mKey = getKeyByName(key);
95 }
96 else
97 {
98 attr = child->first_attribute("x");
99 if (!attr) return;
100 mActionX = atol(attr->value());
101 attr = child->first_attribute("y");
102 if (!attr) return;
103 mActionY = atol(attr->value());
104 attr = child->first_attribute("w");
105 if (!attr) return;
106 mActionW = atol(attr->value());
107 attr = child->first_attribute("h");
108 if (!attr) return;
109 mActionH = atol(attr->value());
110 }
111 }
112}
113
114int GUIAction::NotifyTouch(TOUCH_STATE state, int x, int y)
115{
116 if (state == TOUCH_RELEASE)
117 doActions();
118
119 return 0;
120}
121
122int GUIAction::NotifyKey(int key)
123{
124 if (!mKey || key != mKey)
125 return 1;
126
127 doActions();
128 return 0;
129}
130
131int GUIAction::NotifyVarChange(std::string varName, std::string value)
132{
133 if (varName.empty() && !isConditionValid() && !mKey && !mActionW)
134 doActions();
135
136 // This handles notifying the condition system of page start
137 if (varName.empty() && isConditionValid())
138 NotifyPageSet();
139
140 if ((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
141 doActions();
142
143 return 0;
144}
145
146void GUIAction::simulate_progress_bar(void)
147{
148 ui_print("Simulating actions...\n");
149 for (int i = 0; i < 5; i++)
150 {
151 usleep(500000);
152 DataManager::SetValue("ui_progress", i * 20);
153 }
154}
155
Dees_Troy657c3092012-09-10 20:32:10 -0400156int GUIAction::flash_zip(std::string filename, std::string pageName, const int simulate, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400157{
158 int ret_val = 0;
159
160 DataManager::SetValue("ui_progress", 0);
161
162 if (filename.empty())
163 {
164 LOGE("No file specified.\n");
165 return -1;
166 }
167
168 // We're going to jump to this page first, like a loading page
169 gui_changePage(pageName);
170
171 int fd = -1;
172 ZipArchive zip;
173
Dees_Troy657c3092012-09-10 20:32:10 -0400174 if (!PartitionManager.Mount_By_Path(filename, true))
175 return -1;
176
177 if (mzOpenZipArchive(filename.c_str(), &zip))
Dees_Troy51a0e822012-09-05 15:24:24 -0400178 {
179 LOGE("Unable to open zip file.\n");
180 return -1;
181 }
182
183 // Check the zip to see if it has a custom installer theme
184 const ZipEntry* twrp = mzFindZipEntry(&zip, "META-INF/teamwin/twrp.zip");
185 if (twrp != NULL)
186 {
187 unlink("/tmp/twrp.zip");
188 fd = creat("/tmp/twrp.zip", 0666);
189 }
190 if (fd >= 0 && twrp != NULL &&
191 mzExtractZipEntryToFile(&zip, twrp, fd) &&
192 !PageManager::LoadPackage("install", "/tmp/twrp.zip", "main"))
193 {
194 mzCloseZipArchive(&zip);
Dees_Troy657c3092012-09-10 20:32:10 -0400195 PageManager::SelectPackage("install");
Dees_Troy51a0e822012-09-05 15:24:24 -0400196 gui_changePage("main");
197 }
198 else
199 {
200 // In this case, we just use the default page
201 mzCloseZipArchive(&zip);
Dees_Troy657c3092012-09-10 20:32:10 -0400202 gui_changePage(pageName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400203 }
204 if (fd >= 0)
205 close(fd);
206
207 if (simulate) {
208 simulate_progress_bar();
209 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400210 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400211
212 // Now, check if we need to ensure TWRP remains installed...
213 struct stat st;
214 if (stat("/sbin/installTwrp", &st) == 0)
215 {
216 DataManager::SetValue("tw_operation", "Configuring TWRP");
217 DataManager::SetValue("tw_partition", "");
218 ui_print("Configuring TWRP...\n");
Dees_Troy8170a922012-09-18 15:40:25 -0400219 if (system("/sbin/installTwrp reinstall") < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400220 {
221 ui_print("Unable to configure TWRP with this kernel.\n");
222 }
223 }
224 }
225
226 // Done
227 DataManager::SetValue("ui_progress", 100);
228 DataManager::SetValue("ui_progress", 0);
229 return ret_val;
230}
231
232int GUIAction::doActions()
233{
234 if (mActions.size() < 1) return -1;
235 if (mActions.size() == 1)
236 return doAction(mActions.at(0), 0);
237
238 // For multi-action, we always use a thread
239 pthread_t t;
240 pthread_create(&t, NULL, thread_start, this);
241
242 return 0;
243}
244
245void* GUIAction::thread_start(void *cookie)
246{
247 GUIAction* ourThis = (GUIAction*) cookie;
248
249 DataManager::SetValue(TW_ACTION_BUSY, 1);
250
251 if (ourThis->mActions.size() > 1)
252 {
253 std::vector<Action>::iterator iter;
254 for (iter = ourThis->mActions.begin(); iter != ourThis->mActions.end(); iter++)
255 ourThis->doAction(*iter, 1);
256 }
257 else
258 {
259 ourThis->doAction(ourThis->mActions.at(0), 1);
260 }
261 int check = 0;
262 DataManager::GetValue("tw_background_thread_running", check);
263 if (check == 0)
264 DataManager::SetValue(TW_ACTION_BUSY, 0);
265 return NULL;
266}
267
268void GUIAction::operation_start(const string operation_name)
269{
270 DataManager::SetValue(TW_ACTION_BUSY, 1);
271 DataManager::SetValue("ui_progress", 0);
272 DataManager::SetValue("tw_operation", operation_name);
273 DataManager::SetValue("tw_operation_status", 0);
274 DataManager::SetValue("tw_operation_state", 0);
275}
276
277void GUIAction::operation_end(const int operation_status, const int simulate)
278{
279 int simulate_fail;
280
281 DataManager::SetValue("ui_progress", 100);
282 if (simulate) {
283 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
284 if (simulate_fail != 0)
285 DataManager::SetValue("tw_operation_status", 1);
286 else
287 DataManager::SetValue("tw_operation_status", 0);
288 } else {
289 if (operation_status != 0)
290 DataManager::SetValue("tw_operation_status", 1);
291 else
292 DataManager::SetValue("tw_operation_status", 0);
293 }
294 DataManager::SetValue("tw_operation_state", 1);
295 DataManager::SetValue(TW_ACTION_BUSY, 0);
296}
297
298int GUIAction::doAction(Action action, int isThreaded /* = 0 */)
299{
300 static string zip_queue[10];
301 static int zip_queue_index;
302 static pthread_t terminal_command;
303 int simulate;
304
305 std::string arg = gui_parse_text(action.mArg);
306
307 std::string function = gui_parse_text(action.mFunction);
308
309 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
310
311 if (function == "reboot")
312 {
313 //curtainClose(); this sometimes causes a crash
314
315 sync();
316
317 if (arg == "recovery")
318 tw_reboot(rb_recovery);
319 else if (arg == "poweroff")
320 tw_reboot(rb_poweroff);
321 else if (arg == "bootloader")
322 tw_reboot(rb_bootloader);
323 else if (arg == "download")
324 tw_reboot(rb_download);
325 else
326 tw_reboot(rb_system);
327
328 // This should never occur
329 return -1;
330 }
331 if (function == "home")
332 {
333 PageManager::SelectPackage("TWRP");
334 gui_changePage("main");
335 return 0;
336 }
337
338 if (function == "key")
339 {
340 PageManager::NotifyKey(getKeyByName(arg));
341 return 0;
342 }
343
344 if (function == "page") {
345 std::string page_name = gui_parse_text(arg);
346 return gui_changePage(page_name);
347 }
348
349 if (function == "reload") {
350 int check = 0, ret_val = 0;
351 std::string theme_path;
352
353 operation_start("Reload Theme");
354 theme_path = DataManager::GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -0400355 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400356 LOGE("Unable to mount %s during reload function startup.\n", theme_path.c_str());
357 check = 1;
358 }
359
360 theme_path += "/TWRP/theme/ui.zip";
361 if (check != 0 || PageManager::ReloadPackage("TWRP", theme_path) != 0)
362 {
363 // Loading the custom theme failed - try loading the stock theme
364 LOGI("Attempting to reload stock theme...\n");
365 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
366 {
367 LOGE("Failed to load base packages.\n");
368 ret_val = 1;
369 }
370 }
371 operation_end(ret_val, simulate);
372 }
373
374 if (function == "readBackup")
375 {
376 string Restore_Name;
377 DataManager::GetValue("tw_restore", Restore_Name);
Dees_Troy5bf43922012-09-07 16:07:55 -0400378 PartitionManager.Set_Restore_Files(Restore_Name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400379 return 0;
380 }
381
382 if (function == "set")
383 {
384 if (arg.find('=') != string::npos)
385 {
386 string varName = arg.substr(0, arg.find('='));
387 string value = arg.substr(arg.find('=') + 1, string::npos);
388
389 DataManager::GetValue(value, value);
390 DataManager::SetValue(varName, value);
391 }
392 else
393 DataManager::SetValue(arg, "1");
394 return 0;
395 }
396 if (function == "clear")
397 {
398 DataManager::SetValue(arg, "0");
399 return 0;
400 }
401
402 if (function == "mount")
403 {
404 if (arg == "usb")
405 {
406 DataManager::SetValue(TW_ACTION_BUSY, 1);
407 if (!simulate)
Dees_Troy8170a922012-09-18 15:40:25 -0400408 PartitionManager.usb_storage_enable();
Dees_Troy51a0e822012-09-05 15:24:24 -0400409 else
410 ui_print("Simulating actions...\n");
411 }
412 else if (!simulate)
413 {
414 string cmd;
415 if (arg == "EXTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400416 PartitionManager.Mount_By_Path(DataManager::GetStrValue(TW_EXTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400417 else if (arg == "INTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400418 PartitionManager.Mount_By_Path(DataManager::GetStrValue(TW_INTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400419 else
Dees_Troy51127312012-09-08 13:08:49 -0400420 PartitionManager.Mount_By_Path(arg, true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400421 } else
422 ui_print("Simulating actions...\n");
423 return 0;
424 }
425
426 if (function == "umount" || function == "unmount")
427 {
428 if (arg == "usb")
429 {
430 if (!simulate)
Dees_Troy8170a922012-09-18 15:40:25 -0400431 PartitionManager.usb_storage_disable();
Dees_Troy51a0e822012-09-05 15:24:24 -0400432 else
433 ui_print("Simulating actions...\n");
434 DataManager::SetValue(TW_ACTION_BUSY, 0);
435 }
436 else if (!simulate)
437 {
438 string cmd;
439 if (arg == "EXTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400440 PartitionManager.UnMount_By_Path(DataManager::GetStrValue(TW_EXTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400441 else if (arg == "INTERNAL")
Dees_Troy51127312012-09-08 13:08:49 -0400442 PartitionManager.UnMount_By_Path(DataManager::GetStrValue(TW_INTERNAL_MOUNT), true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400443 else
Dees_Troy51127312012-09-08 13:08:49 -0400444 PartitionManager.UnMount_By_Path(arg, true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400445 } else
446 ui_print("Simulating actions...\n");
447 return 0;
448 }
449
450 if (function == "restoredefaultsettings")
451 {
452 operation_start("Restore Defaults");
453 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
454 ui_print("Simulating actions...\n");
455 else {
456 DataManager::ResetDefaults();
Dees_Troy5bf43922012-09-07 16:07:55 -0400457 PartitionManager.Update_System_Details();
458 PartitionManager.Mount_Current_Storage(true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400459 }
460 operation_end(0, simulate);
461 }
462
463 if (function == "copylog")
464 {
465 operation_start("Copy Log");
466 if (!simulate)
467 {
468 char command[255];
469
Dees_Troy5bf43922012-09-07 16:07:55 -0400470 PartitionManager.Mount_Current_Storage(true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400471 sprintf(command, "cp /tmp/recovery.log %s", DataManager::GetCurrentStoragePath().c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400472 system(command);
Dees_Troy51a0e822012-09-05 15:24:24 -0400473 sync();
474 ui_print("Copied recovery log to %s.\n", DataManager::GetCurrentStoragePath().c_str());
475 } else
476 simulate_progress_bar();
477 operation_end(0, simulate);
478 return 0;
479 }
480
481 if (function == "compute" || function == "addsubtract")
482 {
483 if (arg.find("+") != string::npos)
484 {
485 string varName = arg.substr(0, arg.find('+'));
486 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
487 int amount_to_add = atoi(string_to_add.c_str());
488 int value;
489
490 DataManager::GetValue(varName, value);
491 DataManager::SetValue(varName, value + amount_to_add);
492 return 0;
493 }
494 if (arg.find("-") != string::npos)
495 {
496 string varName = arg.substr(0, arg.find('-'));
497 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
498 int amount_to_subtract = atoi(string_to_subtract.c_str());
499 int value;
500
501 DataManager::GetValue(varName, value);
502 value -= amount_to_subtract;
503 if (value <= 0)
504 value = 0;
505 DataManager::SetValue(varName, value);
506 return 0;
507 }
508 }
509
510 if (function == "setguitimezone")
511 {
512 string SelectedZone;
513 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
514 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
515 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
516
517 int dst;
518 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
519
520 string offset;
521 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
522
523 string NewTimeZone = Zone;
524 if (offset != "0")
525 NewTimeZone += ":" + offset;
526
527 if (dst != 0)
528 NewTimeZone += DSTZone;
529
530 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
Dees_Troy8170a922012-09-18 15:40:25 -0400531 DataManager::update_tz_environment_variables();
Dees_Troy51a0e822012-09-05 15:24:24 -0400532 return 0;
533 }
534
535 if (function == "togglestorage") {
536 if (arg == "internal") {
537 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
538 } else if (arg == "external") {
539 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1);
540 }
Dees_Troy51127312012-09-08 13:08:49 -0400541 if (PartitionManager.Mount_Current_Storage(true)) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400542 if (arg == "internal") {
543 // Save the current zip location to the external variable
544 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, DataManager::GetStrValue(TW_ZIP_LOCATION_VAR));
545 // Change the current zip location to the internal variable
546 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetStrValue(TW_ZIP_INTERNAL_VAR));
547 } else if (arg == "external") {
548 // Save the current zip location to the internal variable
549 DataManager::SetValue(TW_ZIP_INTERNAL_VAR, DataManager::GetStrValue(TW_ZIP_LOCATION_VAR));
550 // Change the current zip location to the external variable
551 DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetStrValue(TW_ZIP_EXTERNAL_VAR));
552 }
553 } else {
554 // We weren't able to toggle for some reason, restore original setting
555 if (arg == "internal") {
556 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1);
557 } else if (arg == "external") {
558 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
559 }
560 }
561 return 0;
562 }
563
564 if (function == "overlay")
565 return gui_changeOverlay(arg);
566
567 if (function == "queuezip")
568 {
569 if (zip_queue_index >= 10) {
570 ui_print("Maximum zip queue reached!\n");
571 return 0;
572 }
573 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
574 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
575 zip_queue_index++;
576 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
577 }
578 return 0;
579 }
580
581 if (function == "cancelzip")
582 {
583 if (zip_queue_index <= 0) {
584 ui_print("Minimum zip queue reached!\n");
585 return 0;
586 } else {
587 zip_queue_index--;
588 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
589 }
590 return 0;
591 }
592
593 if (function == "queueclear")
594 {
595 zip_queue_index = 0;
596 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
597 return 0;
598 }
599
600 if (function == "sleep")
601 {
602 operation_start("Sleep");
603 usleep(atoi(arg.c_str()));
604 operation_end(0, simulate);
605 return 0;
606 }
607
608 if (isThreaded)
609 {
610 if (function == "fileexists")
611 {
612 struct stat st;
613 string newpath = arg + "/.";
614
615 operation_start("FileExists");
616 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
617 operation_end(0, simulate);
618 else
619 operation_end(1, simulate);
620 }
621
622 if (function == "flash")
623 {
Dees_Troy657c3092012-09-10 20:32:10 -0400624 int i, ret_val = 0, wipe_cache = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400625
626 for (i=0; i<zip_queue_index; i++) {
627 operation_start("Flashing");
628 DataManager::SetValue("tw_filename", zip_queue[i]);
629 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
630
Dees_Troy657c3092012-09-10 20:32:10 -0400631 ret_val = flash_zip(zip_queue[i], arg, simulate, &wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400632 if (ret_val != 0) {
633 ui_print("Error flashing zip '%s'\n", zip_queue[i].c_str());
634 i = 10; // Error flashing zip - exit queue
635 ret_val = 1;
636 }
637 }
638 zip_queue_index = 0;
639 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
640
Dees_Troy657c3092012-09-10 20:32:10 -0400641 if (wipe_cache)
642 PartitionManager.Wipe_By_Path("/cache");
643
Dees_Troy51a0e822012-09-05 15:24:24 -0400644 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 {
Dees_Troy8170a922012-09-18 15:40:25 -0400650 system("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Dees_Troy51a0e822012-09-05 15:24:24 -0400651 ui_print("TWRP injection complete.\n");
652 }
653 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400654 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400655 operation_end(ret_val, simulate);
656 return 0;
657 }
658 if (function == "wipe")
659 {
660 operation_start("Format");
661 DataManager::SetValue("tw_partition", arg);
662
Dees_Troy38bd7602012-09-14 13:33:53 -0400663 int ret_val = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400664
665 if (simulate) {
666 simulate_progress_bar();
667 } else {
668 if (arg == "data")
Dees_Troy38bd7602012-09-14 13:33:53 -0400669 ret_val = PartitionManager.Factory_Reset();
Dees_Troy51a0e822012-09-05 15:24:24 -0400670 else if (arg == "battery")
Dees_Troy38bd7602012-09-14 13:33:53 -0400671 ret_val = PartitionManager.Wipe_Battery_Stats();
Dees_Troy51a0e822012-09-05 15:24:24 -0400672 else if (arg == "rotate")
Dees_Troy38bd7602012-09-14 13:33:53 -0400673 ret_val = PartitionManager.Wipe_Rotate_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400674 else if (arg == "dalvik")
Dees_Troy38bd7602012-09-14 13:33:53 -0400675 ret_val = PartitionManager.Wipe_Dalvik_Cache();
Dees_Troy51a0e822012-09-05 15:24:24 -0400676 else if (arg == "DATAMEDIA") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400677 ret_val = PartitionManager.Format_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400678 } else if (arg == "INTERNAL") {
679 int has_datamedia, dual_storage;
680
681 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
682 if (has_datamedia) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400683 ret_val = PartitionManager.Wipe_Media_From_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400684 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -0400685 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
Dees_Troy51a0e822012-09-05 15:24:24 -0400686 }
687 } else if (arg == "EXTERNAL") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400688 string External_Path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400689
Dees_Troy38bd7602012-09-14 13:33:53 -0400690 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
691 ret_val = PartitionManager.Wipe_By_Path(External_Path);
692 } else
693 ret_val = PartitionManager.Wipe_By_Path(arg);
694
695 if (arg == DataManager::GetSettingsStoragePath()) {
696 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
697 string Storage_Path = DataManager::GetSettingsStoragePath();
698
699 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
700 LOGI("Making TWRP folder and saving settings.\n");
701 Storage_Path += "/TWRP";
702 mkdir(Storage_Path.c_str(), 0777);
703 DataManager::Flush();
704 } else {
705 LOGE("Unable to recreate TWRP folder and save settings.\n");
706 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400707 }
708 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400709 PartitionManager.Update_System_Details();
Dees_Troy38bd7602012-09-14 13:33:53 -0400710 if (ret_val)
711 ret_val = 0; // 0 is success
712 else
713 ret_val = 1; // 1 is failure
Dees_Troy51a0e822012-09-05 15:24:24 -0400714 operation_end(ret_val, simulate);
715 return 0;
716 }
717 if (function == "refreshsizes")
718 {
719 operation_start("Refreshing Sizes");
720 if (simulate) {
721 simulate_progress_bar();
722 } else
Dees_Troy5bf43922012-09-07 16:07:55 -0400723 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400724 operation_end(0, simulate);
725 }
726 if (function == "nandroid")
727 {
728 operation_start("Nandroid");
Dees_Troy43d8b002012-09-17 16:00:01 -0400729 int ret = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400730
731 if (simulate) {
732 DataManager::SetValue("tw_partition", "Simulation");
733 simulate_progress_bar();
734 } else {
735 if (arg == "backup") {
736 string Backup_Name;
737 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400738 if (Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name == "(" || check_backup_name(1) == 0)
739 ret = PartitionManager.Run_Backup();
740 else {
741 operation_end(1, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400742 return -1;
Dees_Troy43d8b002012-09-17 16:00:01 -0400743 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400744 DataManager::SetValue(TW_BACKUP_NAME, "(Current Date)");
745 } else if (arg == "restore") {
746 string Restore_Name;
747 DataManager::GetValue("tw_restore", Restore_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400748 ret = PartitionManager.Run_Restore(Restore_Name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400749 } else {
750 operation_end(1, simulate);
751 return -1;
752 }
753 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400754 if (ret == false)
755 ret = 1; // 1 for failure
756 else
757 ret = 0; // 0 for success
758 operation_end(ret, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400759 return 0;
760 }
761 if (function == "fixpermissions")
762 {
763 operation_start("Fix Permissions");
764 LOGI("fix permissions started!\n");
765 if (simulate) {
766 simulate_progress_bar();
767 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400768 PartitionManager.Fix_Permissions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400769
770 LOGI("fix permissions DONE!\n");
771 operation_end(0, simulate);
772 return 0;
773 }
774 if (function == "dd")
775 {
776 operation_start("imaging");
777
778 if (simulate) {
779 simulate_progress_bar();
780 } else {
781 char cmd[512];
782 sprintf(cmd, "dd %s", arg.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400783 system(cmd);
Dees_Troy51a0e822012-09-05 15:24:24 -0400784 }
785 operation_end(0, simulate);
786 return 0;
787 }
788 if (function == "partitionsd")
789 {
790 operation_start("Partition SD Card");
791
792 if (simulate) {
793 simulate_progress_bar();
794 } else {
795 int allow_partition;
796 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
797 if (allow_partition == 0) {
798 ui_print("This device does not have a real SD Card!\nAborting!\n");
799 } else {
800 // Below seen in Koush's recovery
801 char sddevice[256];
802 char mkdir_path[255];
803 Volume *vol = volume_for_path("/sdcard");
804 strcpy(sddevice, vol->device);
805 // Just need block not whole partition
Dees_Troy8170a922012-09-18 15:40:25 -0400806 sddevice[strlen("/dev/block/mmcblkX")] = '\0';
Dees_Troy51a0e822012-09-05 15:24:24 -0400807
808 char es[64];
809 std::string ext_format, sd_path;
810 int ext, swap;
811 DataManager::GetValue("tw_sdext_size", ext);
812 DataManager::GetValue("tw_swap_size", swap);
813 DataManager::GetValue("tw_sdpart_file_system", ext_format);
814 sprintf(es, "/sbin/sdparted -es %dM -ss %dM -efs ext3 -s > /cache/part.log",ext,swap);
815 LOGI("\nrunning script: %s\n", es);
816 run_script("\nContinue partitioning?",
817 "\nPartitioning sdcard : ",
818 es,
819 "\nunable to execute parted!\n(%s)\n",
820 "\nOops... something went wrong!\nPlease check the recovery log!\n",
821 "\nPartitioning complete!\n\n",
822 "\nPartitioning aborted!\n\n", 0);
823
824 // recreate TWRP folder and rewrite settings - these will be gone after sdcard is partitioned
825#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy5bf43922012-09-07 16:07:55 -0400826 PartitionManager.Mount_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400827 DataManager::GetValue(TW_EXTERNAL_PATH, sd_path);
828 memset(mkdir_path, 0, sizeof(mkdir_path));
829 sprintf(mkdir_path, "%s/TWRP", sd_path.c_str());
830#else
Dees_Troy5bf43922012-09-07 16:07:55 -0400831 PartitionManager.Mount_By_Path("/sdcard", 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400832 strcpy(mkdir_path, "/sdcard/TWRP");
833#endif
834 mkdir(mkdir_path, 0777);
835 DataManager::Flush();
836#ifdef TW_EXTERNAL_STORAGE_PATH
837 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
838 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
839 DataManager::SetValue(TW_ZIP_LOCATION_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
840#else
841 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, "/sdcard");
842 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
843 DataManager::SetValue(TW_ZIP_LOCATION_VAR, "/sdcard");
844#endif
845 // This is sometimes needed to make a healthy ext4 partition
846 if (ext > 0 && strcmp(ext_format.c_str(), "ext4") == 0) {
847 char command[256];
848 LOGE("Fix this format command!\n");
849 //sprintf(command, "mke2fs -t ext4 -m 0 %s", sde.blk);
850 ui_print("Formatting sd-ext as ext4...\n");
851 LOGI("Formatting sd-ext after partitioning, command: '%s'\n", command);
Dees_Troy8170a922012-09-18 15:40:25 -0400852 system(command);
Dees_Troy51a0e822012-09-05 15:24:24 -0400853 ui_print("DONE\n");
854 }
855
Dees_Troy5bf43922012-09-07 16:07:55 -0400856 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400857 }
858 }
859 operation_end(0, simulate);
860 return 0;
861 }
862 if (function == "installhtcdumlock")
863 {
864 operation_start("Install HTC Dumlock");
865 if (simulate) {
866 simulate_progress_bar();
867 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400868 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -0400869
870 operation_end(0, simulate);
871 return 0;
872 }
873 if (function == "htcdumlockrestoreboot")
874 {
875 operation_start("HTC Dumlock Restore Boot");
876 if (simulate) {
877 simulate_progress_bar();
878 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400879 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -0400880
881 operation_end(0, simulate);
882 return 0;
883 }
884 if (function == "htcdumlockreflashrecovery")
885 {
886 operation_start("HTC Dumlock Reflash Recovery");
887 if (simulate) {
888 simulate_progress_bar();
889 } else
Dees_Troy38bd7602012-09-14 13:33:53 -0400890 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -0400891
892 operation_end(0, simulate);
893 return 0;
894 }
895 if (function == "cmd")
896 {
897 int op_status = 0;
898
899 operation_start("Command");
900 LOGI("Running command: '%s'\n", arg.c_str());
901 if (simulate) {
902 simulate_progress_bar();
903 } else {
Dees_Troy8170a922012-09-18 15:40:25 -0400904 op_status = system(arg.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400905 if (op_status != 0)
906 op_status = 1;
907 }
908
909 operation_end(op_status, simulate);
910 return 0;
911 }
912 if (function == "terminalcommand")
913 {
914 int op_status = 0;
915 string cmdpath, command;
916
917 DataManager::GetValue("tw_terminal_location", cmdpath);
918 operation_start("CommandOutput");
919 ui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
920 if (simulate) {
921 simulate_progress_bar();
922 operation_end(op_status, simulate);
923 } else {
924 command = "cd \"";
925 command += cmdpath;
926 command += "\" && ";
927 command += arg;
928 LOGI("Actual command is: '%s'\n", command.c_str());
929 DataManager::SetValue("tw_terminal_command_thread", command);
930 DataManager::SetValue("tw_terminal_state", 1);
931 DataManager::SetValue("tw_background_thread_running", 1);
932 op_status = pthread_create(&terminal_command, NULL, command_thread, NULL);
933 if (op_status != 0) {
934 LOGE("Error starting terminal command thread, %i.\n", op_status);
935 DataManager::SetValue("tw_terminal_state", 0);
936 DataManager::SetValue("tw_background_thread_running", 0);
937 operation_end(1, simulate);
938 }
939 }
940 return 0;
941 }
942 if (function == "killterminal")
943 {
944 int op_status = 0;
945
946 LOGI("Sending kill command...\n");
947 operation_start("KillCommand");
948 DataManager::SetValue("tw_operation_status", 0);
949 DataManager::SetValue("tw_operation_state", 1);
950 DataManager::SetValue("tw_terminal_state", 0);
951 DataManager::SetValue("tw_background_thread_running", 0);
952 DataManager::SetValue(TW_ACTION_BUSY, 0);
953 return 0;
954 }
955 if (function == "reinjecttwrp")
956 {
957 int op_status = 0;
958
959 operation_start("ReinjectTWRP");
960 ui_print("Injecting TWRP into boot image...\n");
961 if (simulate) {
962 simulate_progress_bar();
963 } else {
Dees_Troy8170a922012-09-18 15:40:25 -0400964 system("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Dees_Troy51a0e822012-09-05 15:24:24 -0400965 ui_print("TWRP injection complete.\n");
966 }
967
968 operation_end(op_status, simulate);
969 return 0;
970 }
971 if (function == "checkbackupname")
972 {
973 int op_status = 0;
974
975 operation_start("CheckBackupName");
976 if (simulate) {
977 simulate_progress_bar();
978 } else {
979 op_status = check_backup_name(1);
980 if (op_status != 0)
981 op_status = 1;
982 }
983
984 operation_end(op_status, simulate);
985 return 0;
986 }
987 if (function == "decrypt")
988 {
989 int op_status = 0;
990
991 operation_start("Decrypt");
992 if (simulate) {
993 simulate_progress_bar();
994 } else {
995 string Password;
996 DataManager::GetValue("tw_crypto_password", Password);
Dees_Troy5bf43922012-09-07 16:07:55 -0400997 op_status = PartitionManager.Decrypt_Device(Password);
Dees_Troy51a0e822012-09-05 15:24:24 -0400998 if (op_status != 0)
999 op_status = 1;
1000 else {
1001 int load_theme = 1;
1002
1003 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1004 DataManager::ReadSettingsFile();
Dees_Troy7d15c252012-09-05 20:47:21 -04001005LOGE("TODO: Implement ORS support\n");
1006 if (0/*check_for_script_file()*/) {
Dees_Troy51a0e822012-09-05 15:24:24 -04001007 ui_print("Processing OpenRecoveryScript file...\n");
Dees_Troy7d15c252012-09-05 20:47:21 -04001008 if (/*run_script_file() ==*/ 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -04001009 usleep(2000000); // Sleep for 2 seconds before rebooting
1010 tw_reboot(rb_system);
1011 load_theme = 0;
1012 }
1013 }
1014
1015 if (load_theme) {
1016 int has_datamedia;
1017
1018 // Check for a custom theme and load it if exists
1019 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1020 if (has_datamedia != 0) {
1021 struct stat st;
1022 int check = 0;
1023 std::string theme_path;
1024
1025 theme_path = DataManager::GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -04001026 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -04001027 LOGE("Unable to mount %s during reload function startup.\n", theme_path.c_str());
1028 check = 1;
1029 }
1030
1031 theme_path += "/TWRP/theme/ui.zip";
1032 if (check == 0 && stat(theme_path.c_str(), &st) == 0) {
1033 if (PageManager::ReloadPackage("TWRP", theme_path) != 0)
1034 {
1035 // Loading the custom theme failed - try loading the stock theme
1036 LOGI("Attempting to reload stock theme...\n");
1037 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
1038 {
1039 LOGE("Failed to load base packages.\n");
1040 }
1041 }
1042 }
1043 }
1044 }
1045 }
1046 }
1047
1048 operation_end(op_status, simulate);
1049 return 0;
1050 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001051 if (function == "adbsideload")
1052 {
1053 int ret = 0;
1054
1055 operation_start("Sideload");
1056 if (simulate) {
1057 simulate_progress_bar();
1058 } else {
1059 int wipe_cache = 0;
Dees_Troy9a4b5692012-09-19 15:09:45 -04001060 string Command, Sideload_File;
Dees_Troycfb63ae2012-09-19 14:30:17 -04001061
1062 if (!PartitionManager.Mount_Current_Storage(true)) {
1063 operation_end(1, simulate);
1064 return 0;
1065 }
Dees_Troy9a4b5692012-09-19 15:09:45 -04001066 Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip";
1067 if (TWFunc::Path_Exists(Sideload_File)) {
1068 Command = "rm " + Sideload_File;
Dees_Troycfb63ae2012-09-19 14:30:17 -04001069 system(Command.c_str());
1070 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001071 ui_print("Starting ADB sideload feature...\n");
Dees_Troy9a4b5692012-09-19 15:09:45 -04001072 ret = apply_from_adb(ui, &wipe_cache, Sideload_File.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001073 if (ret != 0)
Dees_Troycfb63ae2012-09-19 14:30:17 -04001074 ret = 1; // failure
1075 else if (wipe_cache)
1076 PartitionManager.Wipe_By_Path("/cache");
Dees_Troy43d8b002012-09-17 16:00:01 -04001077 }
1078 operation_end(ret, simulate);
1079 return 0;
1080 }
Dees_Troycfb63ae2012-09-19 14:30:17 -04001081 if (function == "adbsideloadcancel")
1082 {
1083 int child_pid;
Dees_Troy9a4b5692012-09-19 15:09:45 -04001084 string Command, Sideload_File;
1085 Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip";
1086 Command = "rm " + Sideload_File;
Dees_Troycfb63ae2012-09-19 14:30:17 -04001087 system(Command.c_str());
1088 DataManager::GetValue("tw_child_pid", child_pid);
1089 ui_print("Cancelling ADB sideload...\n");
1090 kill(child_pid, SIGTERM);
1091 return 0;
1092 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001093 }
1094 else
1095 {
1096 pthread_t t;
1097 pthread_create(&t, NULL, thread_start, this);
1098 return 0;
1099 }
1100 return -1;
1101}
1102
1103int GUIAction::getKeyByName(std::string key)
1104{
1105 if (key == "home") return KEY_HOME;
1106 else if (key == "menu") return KEY_MENU;
1107 else if (key == "back") return KEY_BACK;
1108 else if (key == "search") return KEY_SEARCH;
1109 else if (key == "voldown") return KEY_VOLUMEDOWN;
1110 else if (key == "volup") return KEY_VOLUMEUP;
1111 else if (key == "power") {
1112 int ret_val;
1113 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1114 if (!ret_val)
1115 return KEY_POWER;
1116 else
1117 return ret_val;
1118 }
1119
1120 return atol(key.c_str());
1121}
1122
1123void* GUIAction::command_thread(void *cookie)
1124{
1125 string command;
1126 FILE* fp;
1127 char line[512];
1128
1129 DataManager::GetValue("tw_terminal_command_thread", command);
Dees_Troy8170a922012-09-18 15:40:25 -04001130 fp = popen(command.c_str(), "r");
Dees_Troy51a0e822012-09-05 15:24:24 -04001131 if (fp == NULL) {
1132 LOGE("Error opening command to run.\n");
1133 } else {
1134 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1135 struct timeval timeout;
1136 fd_set fdset;
1137
1138 while(keep_going)
1139 {
1140 FD_ZERO(&fdset);
1141 FD_SET(fd, &fdset);
1142 timeout.tv_sec = 0;
1143 timeout.tv_usec = 400000;
1144 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1145 if (has_data == 0) {
1146 // Timeout reached
1147 DataManager::GetValue("tw_terminal_state", check);
1148 if (check == 0) {
1149 keep_going = 0;
1150 }
1151 } else if (has_data < 0) {
1152 // End of execution
1153 keep_going = 0;
1154 } else {
1155 // Try to read output
1156 bytes_read = read(fd, line, sizeof(line));
1157 if (bytes_read > 0)
1158 ui_print("%s", line); // Display output
1159 else
1160 keep_going = 0; // Done executing
1161 }
1162 }
1163 fclose(fp);
1164 }
1165 DataManager::SetValue("tw_operation_status", 0);
1166 DataManager::SetValue("tw_operation_state", 1);
1167 DataManager::SetValue("tw_terminal_state", 0);
1168 DataManager::SetValue("tw_background_thread_running", 0);
1169 DataManager::SetValue(TW_ACTION_BUSY, 0);
1170 return NULL;
1171}