blob: 6b8565aedd70e56a5dcf7ba292d31005f4dfba04 [file] [log] [blame]
jrior001aad03112014-07-05 10:31:21 -04001/*update
Dees_Troya13d74f2013-03-24 08:54:55 -05002 Copyright 2013 bigbiff/Dees_Troy TeamWin
3 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
Dees_Troy51a0e822012-09-05 15:24:24 -040018
19#include <stdarg.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <fcntl.h>
24#include <sys/stat.h>
25#include <sys/time.h>
26#include <sys/mman.h>
27#include <sys/types.h>
28#include <sys/ioctl.h>
29#include <linux/input.h>
30#include <time.h>
31#include <unistd.h>
32#include <stdlib.h>
Dees_Troy657c3092012-09-10 20:32:10 -040033#include <sys/wait.h>
Dees_Troy83bd4832013-05-04 12:39:56 +000034#include <dirent.h>
Vojtech Bocek03fd6c52014-03-13 18:46:34 +010035#include <pwd.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040036
37#include <string>
38#include <sstream>
39#include "../partitions.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040040#include "../twrp-functions.hpp"
Dees_Troy812660f2012-09-20 09:55:17 -040041#include "../openrecoveryscript.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040042
Dees_Troy43d8b002012-09-17 16:00:01 -040043#include "../adb_install.h"
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070044#ifndef TW_NO_SCREEN_TIMEOUT
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050045#include "blanktimer.hpp"
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070046#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040047extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000048#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040049#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040050#include "../variables.h"
Dees_Troy32c8eb82012-09-11 15:28:06 -040051#include "../twinstall.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000052#include "cutils/properties.h"
Dees_Troy43d8b002012-09-17 16:00:01 -040053#include "../minadbd/adb.h"
Ethan Yonker24813422014-11-07 17:19:07 -060054#include "../adb_install.h"
Dees_Troy43d8b002012-09-17 16:00:01 -040055
Dees_Troy32c8eb82012-09-11 15:28:06 -040056int TWinstall_zip(const char* path, int* wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -040057void 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 -040058int gui_console_only();
Dees_Troy51a0e822012-09-05 15:24:24 -040059int gui_start();
60};
61
62#include "rapidxml.hpp"
63#include "objects.hpp"
64
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070065#ifndef TW_NO_SCREEN_TIMEOUT
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050066extern blanktimer blankTimer;
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070067#endif
Dees_Troy43d8b002012-09-17 16:00:01 -040068
Dees_Troy51a0e822012-09-05 15:24:24 -040069void curtainClose(void);
70
71GUIAction::GUIAction(xml_node<>* node)
Vojtech Bocekede51c52014-02-07 23:58:09 +010072 : GUIObject(node)
Dees_Troy51a0e822012-09-05 15:24:24 -040073{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020074 xml_node<>* child;
75 xml_node<>* actions;
76 xml_attribute<>* attr;
Dees_Troy51a0e822012-09-05 15:24:24 -040077
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020078 if (!node) return;
Dees_Troy51a0e822012-09-05 15:24:24 -040079
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020080 // First, get the action
81 actions = node->first_node("actions");
82 if (actions) child = actions->first_node("action");
83 else child = node->first_node("action");
Dees_Troy51a0e822012-09-05 15:24:24 -040084
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020085 if (!child) return;
Dees_Troy51a0e822012-09-05 15:24:24 -040086
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020087 while (child)
88 {
89 Action action;
Dees_Troy51a0e822012-09-05 15:24:24 -040090
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020091 attr = child->first_attribute("function");
92 if (!attr) return;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -050093
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020094 action.mFunction = attr->value();
95 action.mArg = child->value();
96 mActions.push_back(action);
Dees_Troy51a0e822012-09-05 15:24:24 -040097
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020098 child = child->next_sibling("action");
99 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400100
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200101 // Now, let's get either the key or region
102 child = node->first_node("touch");
103 if (child)
104 {
105 attr = child->first_attribute("key");
106 if (attr)
107 {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100108 std::vector<std::string> keys = TWFunc::Split_String(attr->value(), "+");
109 for(size_t i = 0; i < keys.size(); ++i)
110 {
111 const int key = getKeyByName(keys[i]);
112 mKeys[key] = false;
113 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200114 }
115 else
116 {
117 attr = child->first_attribute("x");
118 if (!attr) return;
119 mActionX = atol(attr->value());
120 attr = child->first_attribute("y");
121 if (!attr) return;
122 mActionY = atol(attr->value());
123 attr = child->first_attribute("w");
124 if (!attr) return;
125 mActionW = atol(attr->value());
126 attr = child->first_attribute("h");
127 if (!attr) return;
128 mActionH = atol(attr->value());
129 }
130 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400131}
132
133int GUIAction::NotifyTouch(TOUCH_STATE state, int x, int y)
134{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200135 if (state == TOUCH_RELEASE)
136 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400137
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200138 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400139}
140
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100141int GUIAction::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -0400142{
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100143 if (mKeys.empty())
144 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400145
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100146 std::map<int, bool>::iterator itr = mKeys.find(key);
147 if(itr == mKeys.end())
148 return 0;
149
150 bool prevState = itr->second;
151 itr->second = down;
152
153 // If there is only one key for this action, wait for key up so it
154 // doesn't trigger with multi-key actions.
155 // Else, check if all buttons are pressed, then consume their release events
156 // so they don't trigger one-button actions and reset mKeys pressed status
157 if(mKeys.size() == 1) {
158 if(!down && prevState)
159 doActions();
160 } else if(down) {
161 for(itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
162 if(!itr->second)
163 return 0;
164 }
165
166 // Passed, all req buttons are pressed, reset them and consume release events
167 HardwareKeyboard *kb = PageManager::GetHardwareKeyboard();
168 for(itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
169 kb->ConsumeKeyRelease(itr->first);
170 itr->second = false;
171 }
172
173 doActions();
174 }
175
Dees_Troy51a0e822012-09-05 15:24:24 -0400176 return 0;
177}
178
Vojtech Bocek07220562014-02-08 02:05:33 +0100179int GUIAction::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400180{
Vojtech Bocek07220562014-02-08 02:05:33 +0100181 GUIObject::NotifyVarChange(varName, value);
182
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100183 if (varName.empty() && !isConditionValid() && mKeys.empty() && !mActionW)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200184 doActions();
Vojtech Bocek07220562014-02-08 02:05:33 +0100185 else if((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200186 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400187
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200188 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400189}
190
191void GUIAction::simulate_progress_bar(void)
192{
Dees_Troy2673cec2013-04-02 20:22:16 +0000193 gui_print("Simulating actions...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400194 for (int i = 0; i < 5; i++)
195 {
196 usleep(500000);
197 DataManager::SetValue("ui_progress", i * 20);
198 }
199}
200
Dees_Troy657c3092012-09-10 20:32:10 -0400201int GUIAction::flash_zip(std::string filename, std::string pageName, const int simulate, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400202{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200203 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400204
205 DataManager::SetValue("ui_progress", 0);
206
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200207 if (filename.empty())
208 {
209 LOGERR("No file specified.\n");
210 return -1;
211 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400212
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200213 // We're going to jump to this page first, like a loading page
214 gui_changePage(pageName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400215
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200216 int fd = -1;
217 ZipArchive zip;
Dees_Troy51a0e822012-09-05 15:24:24 -0400218
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200219 if (!PartitionManager.Mount_By_Path(filename, true))
Dees_Troy657c3092012-09-10 20:32:10 -0400220 return -1;
221
Ethan Yonker57e35872014-11-07 14:52:22 -0600222 gui_changePage(pageName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400223
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200224 if (fd >= 0)
225 close(fd);
Dees_Troy51a0e822012-09-05 15:24:24 -0400226
227 if (simulate) {
228 simulate_progress_bar();
229 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400230 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400231
232 // Now, check if we need to ensure TWRP remains installed...
233 struct stat st;
234 if (stat("/sbin/installTwrp", &st) == 0)
235 {
236 DataManager::SetValue("tw_operation", "Configuring TWRP");
237 DataManager::SetValue("tw_partition", "");
Dees_Troy2673cec2013-04-02 20:22:16 +0000238 gui_print("Configuring TWRP...\n");
Vojtech Bocek05534202013-09-11 08:11:56 +0200239 if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall") < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400240 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000241 gui_print("Unable to configure TWRP with this kernel.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400242 }
243 }
244 }
245
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200246 // Done
247 DataManager::SetValue("ui_progress", 100);
248 DataManager::SetValue("ui_progress", 0);
249 return ret_val;
Dees_Troy51a0e822012-09-05 15:24:24 -0400250}
251
252int GUIAction::doActions()
253{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200254 if (mActions.size() < 1) return -1;
255 if (mActions.size() == 1)
Dees_Troy51a0e822012-09-05 15:24:24 -0400256 return doAction(mActions.at(0), 0);
257
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200258 // For multi-action, we always use a thread
259 pthread_t t;
Dees_Troyab4963c2013-01-16 20:35:51 +0000260 pthread_attr_t tattr;
261
262 if (pthread_attr_init(&tattr)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000263 LOGERR("Unable to pthread_attr_init\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000264 return -1;
265 }
266 if (pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_JOINABLE)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000267 LOGERR("Error setting pthread_attr_setdetachstate\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000268 return -1;
269 }
270 if (pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000271 LOGERR("Error setting pthread_attr_setscope\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000272 return -1;
273 }
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500274 /*if (pthread_attr_setstacksize(&tattr, 524288)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000275 LOGERR("Error setting pthread_attr_setstacksize\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000276 return -1;
277 }
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -0500278 */
Dees_Troyab4963c2013-01-16 20:35:51 +0000279 int ret = pthread_create(&t, &tattr, thread_start, this);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200280 if (ret) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000281 LOGERR("Unable to create more threads for actions... continuing in same thread! %i\n", ret);
Dees_Troyab4963c2013-01-16 20:35:51 +0000282 thread_start(this);
283 } else {
284 if (pthread_join(t, NULL)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000285 LOGERR("Error joining threads\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000286 }
287 }
288 if (pthread_attr_destroy(&tattr)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000289 LOGERR("Failed to pthread_attr_destroy\n");
Dees_Troyab4963c2013-01-16 20:35:51 +0000290 return -1;
291 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400292
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200293 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400294}
295
296void* GUIAction::thread_start(void *cookie)
297{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200298 GUIAction* ourThis = (GUIAction*) cookie;
Dees_Troy51a0e822012-09-05 15:24:24 -0400299
300 DataManager::SetValue(TW_ACTION_BUSY, 1);
301
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200302 if (ourThis->mActions.size() > 1)
303 {
304 std::vector<Action>::iterator iter;
305 for (iter = ourThis->mActions.begin(); iter != ourThis->mActions.end(); iter++)
306 ourThis->doAction(*iter, 1);
307 }
308 else
309 {
310 ourThis->doAction(ourThis->mActions.at(0), 1);
311 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400312 int check = 0;
313 DataManager::GetValue("tw_background_thread_running", check);
314 if (check == 0)
315 DataManager::SetValue(TW_ACTION_BUSY, 0);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200316 return NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -0400317}
318
319void GUIAction::operation_start(const string operation_name)
320{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000321 time(&Start);
Dees_Troy51a0e822012-09-05 15:24:24 -0400322 DataManager::SetValue(TW_ACTION_BUSY, 1);
323 DataManager::SetValue("ui_progress", 0);
324 DataManager::SetValue("tw_operation", operation_name);
325 DataManager::SetValue("tw_operation_status", 0);
326 DataManager::SetValue("tw_operation_state", 0);
327}
328
329void GUIAction::operation_end(const int operation_status, const int simulate)
330{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000331 time_t Stop;
Dees_Troy51a0e822012-09-05 15:24:24 -0400332 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400333 DataManager::SetValue("ui_progress", 100);
334 if (simulate) {
335 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
336 if (simulate_fail != 0)
337 DataManager::SetValue("tw_operation_status", 1);
338 else
339 DataManager::SetValue("tw_operation_status", 0);
340 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500341 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400342 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500343 }
344 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400345 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500346 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400347 }
348 DataManager::SetValue("tw_operation_state", 1);
349 DataManager::SetValue(TW_ACTION_BUSY, 0);
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700350#ifndef TW_NO_SCREEN_TIMEOUT
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500351 blankTimer.resetTimerAndUnblank();
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700352#endif
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000353 time(&Stop);
354 if ((int) difftime(Stop, Start) > 10)
Ethan Yonker03db3262014-02-05 08:02:06 -0600355 DataManager::Vibrate("tw_action_vibrate");
Dees_Troy51a0e822012-09-05 15:24:24 -0400356}
357
358int GUIAction::doAction(Action action, int isThreaded /* = 0 */)
359{
360 static string zip_queue[10];
361 static int zip_queue_index;
362 static pthread_t terminal_command;
363 int simulate;
364
365 std::string arg = gui_parse_text(action.mArg);
366
367 std::string function = gui_parse_text(action.mFunction);
368
369 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
370
Dees_Troy6ef66352013-02-21 08:26:57 -0600371 if (function == "reboot")
372 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200373 //curtainClose(); this sometimes causes a crash
Dees_Troy51a0e822012-09-05 15:24:24 -0400374
Dees_Troy6ef66352013-02-21 08:26:57 -0600375 sync();
376 DataManager::SetValue("tw_gui_done", 1);
377 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400378
Dees_Troy6ef66352013-02-21 08:26:57 -0600379 return 0;
380 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200381 if (function == "home")
382 {
383 PageManager::SelectPackage("TWRP");
384 gui_changePage("main");
385 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400386 }
387
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200388 if (function == "key")
389 {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100390 const int key = getKeyByName(arg);
391 PageManager::NotifyKey(key, true);
392 PageManager::NotifyKey(key, false);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200393 return 0;
394 }
395
396 if (function == "page") {
397 std::string page_name = gui_parse_text(arg);
398 return gui_changePage(page_name);
399 }
400
401 if (function == "reload") {
Dees_Troy51a0e822012-09-05 15:24:24 -0400402 int check = 0, ret_val = 0;
403 std::string theme_path;
404
405 operation_start("Reload Theme");
406 theme_path = DataManager::GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -0400407 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000408 LOGERR("Unable to mount %s during reload function startup.\n", theme_path.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400409 check = 1;
410 }
411
412 theme_path += "/TWRP/theme/ui.zip";
413 if (check != 0 || PageManager::ReloadPackage("TWRP", theme_path) != 0)
414 {
415 // Loading the custom theme failed - try loading the stock theme
Dees_Troy2673cec2013-04-02 20:22:16 +0000416 LOGINFO("Attempting to reload stock theme...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400417 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
418 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000419 LOGERR("Failed to load base packages.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400420 ret_val = 1;
421 }
422 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200423 operation_end(ret_val, simulate);
Dees_Troy83bd4832013-05-04 12:39:56 +0000424 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400425 }
426
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200427 if (function == "readBackup")
428 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400429 string Restore_Name;
430 DataManager::GetValue("tw_restore", Restore_Name);
Dees_Troy5bf43922012-09-07 16:07:55 -0400431 PartitionManager.Set_Restore_Files(Restore_Name);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200432 return 0;
433 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400434
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200435 if (function == "set")
436 {
437 if (arg.find('=') != string::npos)
438 {
439 string varName = arg.substr(0, arg.find('='));
440 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400441
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200442 DataManager::GetValue(value, value);
443 DataManager::SetValue(varName, value);
444 }
445 else
446 DataManager::SetValue(arg, "1");
447 return 0;
448 }
449 if (function == "clear")
450 {
451 DataManager::SetValue(arg, "0");
452 return 0;
453 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400454
Ethan Yonker8214f0a2014-02-16 15:15:34 -0600455 if (function == "mount") {
456 if (arg == "usb") {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200457 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400458 if (!simulate)
Dees_Troy8170a922012-09-18 15:40:25 -0400459 PartitionManager.usb_storage_enable();
Dees_Troy51a0e822012-09-05 15:24:24 -0400460 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000461 gui_print("Simulating actions...\n");
Ethan Yonker8214f0a2014-02-16 15:15:34 -0600462 } else if (!simulate) {
463 PartitionManager.Mount_By_Path(arg, true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200464 } else
Dees_Troy2673cec2013-04-02 20:22:16 +0000465 gui_print("Simulating actions...\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200466 return 0;
467 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400468
Ethan Yonker8214f0a2014-02-16 15:15:34 -0600469 if (function == "umount" || function == "unmount") {
470 if (arg == "usb") {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200471 if (!simulate)
Dees_Troy8170a922012-09-18 15:40:25 -0400472 PartitionManager.usb_storage_disable();
Dees_Troy51a0e822012-09-05 15:24:24 -0400473 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000474 gui_print("Simulating actions...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400475 DataManager::SetValue(TW_ACTION_BUSY, 0);
Ethan Yonker8214f0a2014-02-16 15:15:34 -0600476 } else if (!simulate) {
477 PartitionManager.UnMount_By_Path(arg, true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200478 } else
Dees_Troy2673cec2013-04-02 20:22:16 +0000479 gui_print("Simulating actions...\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200480 return 0;
481 }
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500482
Dees_Troy51a0e822012-09-05 15:24:24 -0400483 if (function == "restoredefaultsettings")
484 {
485 operation_start("Restore Defaults");
486 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
Dees_Troy2673cec2013-04-02 20:22:16 +0000487 gui_print("Simulating actions...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400488 else {
489 DataManager::ResetDefaults();
Dees_Troy5bf43922012-09-07 16:07:55 -0400490 PartitionManager.Update_System_Details();
491 PartitionManager.Mount_Current_Storage(true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400492 }
493 operation_end(0, simulate);
Dees_Troy83bd4832013-05-04 12:39:56 +0000494 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400495 }
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500496
Dees_Troy51a0e822012-09-05 15:24:24 -0400497 if (function == "copylog")
498 {
499 operation_start("Copy Log");
500 if (!simulate)
501 {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500502 string dst;
Dees_Troy5bf43922012-09-07 16:07:55 -0400503 PartitionManager.Mount_Current_Storage(true);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500504 dst = DataManager::GetCurrentStoragePath() + "/recovery.log";
505 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
Dees_Troy51a0e822012-09-05 15:24:24 -0400506 sync();
Dees_Troy2673cec2013-04-02 20:22:16 +0000507 gui_print("Copied recovery log to %s.\n", DataManager::GetCurrentStoragePath().c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400508 } else
509 simulate_progress_bar();
510 operation_end(0, simulate);
511 return 0;
512 }
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500513
Dees_Troy51a0e822012-09-05 15:24:24 -0400514 if (function == "compute" || function == "addsubtract")
515 {
516 if (arg.find("+") != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200517 {
518 string varName = arg.substr(0, arg.find('+'));
519 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400520 int amount_to_add = atoi(string_to_add.c_str());
521 int value;
522
523 DataManager::GetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200524 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400525 return 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200526 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400527 if (arg.find("-") != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200528 {
529 string varName = arg.substr(0, arg.find('-'));
530 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400531 int amount_to_subtract = atoi(string_to_subtract.c_str());
532 int value;
533
534 DataManager::GetValue(varName, value);
535 value -= amount_to_subtract;
536 if (value <= 0)
537 value = 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200538 DataManager::SetValue(varName, value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400539 return 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200540 }
Vojtech Bocek85932342013-04-01 22:11:33 +0200541 if (arg.find("*") != string::npos)
542 {
543 string varName = arg.substr(0, arg.find('*'));
544 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
545 int multiply_by = atoi(multiply_by_str.c_str());
546 int value;
547
548 DataManager::GetValue(varName, value);
549 DataManager::SetValue(varName, value*multiply_by);
550 return 0;
551 }
552 if (arg.find("/") != string::npos)
553 {
554 string varName = arg.substr(0, arg.find('/'));
555 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
556 int divide_by = atoi(divide_by_str.c_str());
557 int value;
558
559 if(divide_by != 0)
560 {
561 DataManager::GetValue(varName, value);
562 DataManager::SetValue(varName, value/divide_by);
563 }
564 return 0;
565 }
566 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
567 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400568 }
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500569
Dees_Troy51a0e822012-09-05 15:24:24 -0400570 if (function == "setguitimezone")
571 {
572 string SelectedZone;
573 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
574 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
575 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500576
Dees_Troy51a0e822012-09-05 15:24:24 -0400577 int dst;
578 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500579
Dees_Troy51a0e822012-09-05 15:24:24 -0400580 string offset;
581 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500582
Dees_Troy51a0e822012-09-05 15:24:24 -0400583 string NewTimeZone = Zone;
584 if (offset != "0")
585 NewTimeZone += ":" + offset;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500586
Dees_Troy51a0e822012-09-05 15:24:24 -0400587 if (dst != 0)
588 NewTimeZone += DSTZone;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500589
Dees_Troy51a0e822012-09-05 15:24:24 -0400590 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
Dees_Troy8170a922012-09-18 15:40:25 -0400591 DataManager::update_tz_environment_variables();
Dees_Troy51a0e822012-09-05 15:24:24 -0400592 return 0;
593 }
594
595 if (function == "togglestorage") {
Dees Troyf193f882013-09-11 14:56:20 +0000596 LOGERR("togglestorage action was deprecated from TWRP\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400597 return 0;
598 }
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500599
Dees_Troy51a0e822012-09-05 15:24:24 -0400600 if (function == "overlay")
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200601 return gui_changeOverlay(arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400602
603 if (function == "queuezip")
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200604 {
605 if (zip_queue_index >= 10) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000606 gui_print("Maximum zip queue reached!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400607 return 0;
608 }
609 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
610 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
611 zip_queue_index++;
612 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
613 }
614 return 0;
615 }
616
617 if (function == "cancelzip")
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200618 {
619 if (zip_queue_index <= 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000620 gui_print("Minimum zip queue reached!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400621 return 0;
622 } else {
623 zip_queue_index--;
624 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
625 }
626 return 0;
627 }
628
629 if (function == "queueclear")
630 {
631 zip_queue_index = 0;
632 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
633 return 0;
634 }
635
636 if (function == "sleep")
637 {
638 operation_start("Sleep");
639 usleep(atoi(arg.c_str()));
640 operation_end(0, simulate);
641 return 0;
642 }
643
Dees Troyb21cc642013-09-10 17:36:41 +0000644 if (function == "appenddatetobackupname")
645 {
646 operation_start("AppendDateToBackupName");
647 string Backup_Name;
648 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
649 Backup_Name += TWFunc::Get_Current_Date();
650 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
651 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
652 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
653 operation_end(0, simulate);
654 return 0;
655 }
656
657 if (function == "generatebackupname")
658 {
659 operation_start("GenerateBackupName");
660 TWFunc::Auto_Generate_Backup_Name();
661 operation_end(0, simulate);
662 return 0;
663 }
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500664 if (function == "checkpartitionlist") {
665 string Wipe_List, wipe_path;
666 int count = 0;
667
668 DataManager::GetValue("tw_wipe_list", Wipe_List);
669 LOGINFO("checkpartitionlist list '%s'\n", Wipe_List.c_str());
670 if (!Wipe_List.empty()) {
671 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
672 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
673 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
674 LOGINFO("checkpartitionlist wipe_path '%s'\n", wipe_path.c_str());
675 if (wipe_path == "/and-sec" || wipe_path == "DALVIK" || wipe_path == "INTERNAL") {
676 // Do nothing
677 } else {
678 count++;
679 }
680 start_pos = end_pos + 1;
681 end_pos = Wipe_List.find(";", start_pos);
682 }
683 DataManager::SetValue("tw_check_partition_list", count);
684 } else {
685 DataManager::SetValue("tw_check_partition_list", 0);
686 }
687 return 0;
688 }
689 if (function == "getpartitiondetails") {
690 string Wipe_List, wipe_path;
691 int count = 0;
692
693 DataManager::GetValue("tw_wipe_list", Wipe_List);
694 LOGINFO("getpartitiondetails list '%s'\n", Wipe_List.c_str());
695 if (!Wipe_List.empty()) {
696 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
697 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
698 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
699 LOGINFO("getpartitiondetails wipe_path '%s'\n", wipe_path.c_str());
700 if (wipe_path == "/and-sec" || wipe_path == "DALVIK" || wipe_path == "INTERNAL") {
701 // Do nothing
702 } else {
703 DataManager::SetValue("tw_partition_path", wipe_path);
704 break;
705 }
706 start_pos = end_pos + 1;
707 end_pos = Wipe_List.find(";", start_pos);
708 }
709 if (!wipe_path.empty()) {
710 TWPartition* Part = PartitionManager.Find_Partition_By_Path(wipe_path);
711 if (Part) {
712 unsigned long long mb = 1048576;
713
714 DataManager::SetValue("tw_partition_name", Part->Display_Name);
715 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
716 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
717 DataManager::SetValue("tw_partition_size", Part->Size / mb);
718 DataManager::SetValue("tw_partition_used", Part->Used / mb);
719 DataManager::SetValue("tw_partition_free", Part->Free / mb);
720 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
721 DataManager::SetValue("tw_partition_removable", Part->Removable);
722 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
723
724 if (Part->Can_Repair())
725 DataManager::SetValue("tw_partition_can_repair", 1);
726 else
727 DataManager::SetValue("tw_partition_can_repair", 0);
728 if (TWFunc::Path_Exists("/sbin/mkdosfs"))
729 DataManager::SetValue("tw_partition_vfat", 1);
730 else
731 DataManager::SetValue("tw_partition_vfat", 0);
732 if (TWFunc::Path_Exists("/sbin/mkfs.exfat"))
733 DataManager::SetValue("tw_partition_exfat", 1);
734 else
735 DataManager::SetValue("tw_partition_exfat", 0);
736 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs"))
737 DataManager::SetValue("tw_partition_f2fs", 1);
738 else
739 DataManager::SetValue("tw_partition_f2fs", 0);
740 if (TWFunc::Path_Exists("/sbin/mke2fs"))
741 DataManager::SetValue("tw_partition_ext", 1);
742 else
743 DataManager::SetValue("tw_partition_ext", 0);
744 return 0;
745 } else {
746 LOGERR("Unable to locate partition: '%s'\n", wipe_path.c_str());
747 }
748 }
749 }
750 DataManager::SetValue("tw_partition_name", "");
751 DataManager::SetValue("tw_partition_file_system", "");
752 return 0;
753 }
Dees Troyb21cc642013-09-10 17:36:41 +0000754
Vojtech Bocek03fd6c52014-03-13 18:46:34 +0100755 if (function == "screenshot")
756 {
757 time_t tm;
758 char path[256];
759 int path_len;
760 uid_t uid = -1;
761 gid_t gid = -1;
762
763 struct passwd *pwd = getpwnam("media_rw");
764 if(pwd) {
765 uid = pwd->pw_uid;
766 gid = pwd->pw_gid;
767 }
768
769 const std::string storage = DataManager::GetCurrentStoragePath();
770 if(PartitionManager.Is_Mounted_By_Path(storage)) {
771 snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
772 } else {
773 strcpy(path, "/tmp/");
774 }
775
776 if(!TWFunc::Create_Dir_Recursive(path, 0666, uid, gid))
777 return 0;
778
779 tm = time(NULL);
780 path_len = strlen(path);
781
782 // Screenshot_2014-01-01-18-21-38.png
783 strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
784
785 int res = gr_save_screenshot(path);
786 if(res == 0) {
787 chmod(path, 0666);
788 chown(path, uid, gid);
789
790 gui_print("Screenshot was saved to %s\n", path);
791
792 // blink to notify that the screenshow was taken
793 gr_color(255, 255, 255, 255);
794 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
795 gr_flip();
796 gui_forceRender();
797 } else {
798 LOGERR("Failed to take a screenshot!\n");
799 }
800 return 0;
801 }
802
xNUTxe85f02d2014-07-18 01:30:58 +0200803 if (function == "setbrightness")
804 {
805 return TWFunc::Set_Brightness(arg);
806 }
807
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200808 if (isThreaded)
809 {
810 if (function == "fileexists")
Dees_Troy51a0e822012-09-05 15:24:24 -0400811 {
812 struct stat st;
813 string newpath = arg + "/.";
814
815 operation_start("FileExists");
816 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
817 operation_end(0, simulate);
818 else
819 operation_end(1, simulate);
Dees_Troy83bd4832013-05-04 12:39:56 +0000820 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400821 }
822
823 if (function == "flash")
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200824 {
Dees_Troy657c3092012-09-10 20:32:10 -0400825 int i, ret_val = 0, wipe_cache = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400826
827 for (i=0; i<zip_queue_index; i++) {
828 operation_start("Flashing");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200829 DataManager::SetValue("tw_filename", zip_queue[i]);
830 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
Dees_Troy51a0e822012-09-05 15:24:24 -0400831
Tom Hite5a926722014-09-15 01:31:03 +0000832 TWFunc::SetPerformanceMode(true);
Dees_Troy657c3092012-09-10 20:32:10 -0400833 ret_val = flash_zip(zip_queue[i], arg, simulate, &wipe_cache);
Tom Hite5a926722014-09-15 01:31:03 +0000834 TWFunc::SetPerformanceMode(false);
Dees_Troy51a0e822012-09-05 15:24:24 -0400835 if (ret_val != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000836 gui_print("Error flashing zip '%s'\n", zip_queue[i].c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400837 i = 10; // Error flashing zip - exit queue
838 ret_val = 1;
839 }
840 }
841 zip_queue_index = 0;
842 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
843
Dees_Troy657c3092012-09-10 20:32:10 -0400844 if (wipe_cache)
845 PartitionManager.Wipe_By_Path("/cache");
Vojtech Bocek05534202013-09-11 08:11:56 +0200846
Dees_Troy51a0e822012-09-05 15:24:24 -0400847 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
848 operation_start("ReinjectTWRP");
Dees_Troy2673cec2013-04-02 20:22:16 +0000849 gui_print("Injecting TWRP into boot image...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400850 if (simulate) {
851 simulate_progress_bar();
852 } else {
Dees_Troy06b4fe92012-10-16 11:43:20 -0400853 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
854 if (Boot == NULL || Boot->Current_File_System != "emmc")
Vojtech Bocek05534202013-09-11 08:11:56 +0200855 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Dees_Troy06b4fe92012-10-16 11:43:20 -0400856 else {
857 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +0200858 TWFunc::Exec_Cmd(injectcmd);
Dees_Troy06b4fe92012-10-16 11:43:20 -0400859 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000860 gui_print("TWRP injection complete.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400861 }
862 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400863 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400864 operation_end(ret_val, simulate);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200865 return 0;
866 }
867 if (function == "wipe")
868 {
869 operation_start("Format");
870 DataManager::SetValue("tw_partition", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400871
Dees_Troy38bd7602012-09-14 13:33:53 -0400872 int ret_val = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400873
874 if (simulate) {
875 simulate_progress_bar();
876 } else {
877 if (arg == "data")
Dees_Troy38bd7602012-09-14 13:33:53 -0400878 ret_val = PartitionManager.Factory_Reset();
Dees_Troy51a0e822012-09-05 15:24:24 -0400879 else if (arg == "battery")
Dees_Troy38bd7602012-09-14 13:33:53 -0400880 ret_val = PartitionManager.Wipe_Battery_Stats();
Dees_Troy51a0e822012-09-05 15:24:24 -0400881 else if (arg == "rotate")
Dees_Troy38bd7602012-09-14 13:33:53 -0400882 ret_val = PartitionManager.Wipe_Rotate_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400883 else if (arg == "dalvik")
Dees_Troy38bd7602012-09-14 13:33:53 -0400884 ret_val = PartitionManager.Wipe_Dalvik_Cache();
Dees_Troy51a0e822012-09-05 15:24:24 -0400885 else if (arg == "DATAMEDIA") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400886 ret_val = PartitionManager.Format_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400887 } else if (arg == "INTERNAL") {
888 int has_datamedia, dual_storage;
889
890 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
891 if (has_datamedia) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400892 ret_val = PartitionManager.Wipe_Media_From_Data();
Dees_Troy51a0e822012-09-05 15:24:24 -0400893 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -0400894 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
Dees_Troy51a0e822012-09-05 15:24:24 -0400895 }
896 } else if (arg == "EXTERNAL") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400897 string External_Path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400898
Dees_Troy38bd7602012-09-14 13:33:53 -0400899 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
900 ret_val = PartitionManager.Wipe_By_Path(External_Path);
Dees_Troy2ff5a8d2012-09-26 14:53:02 -0400901 } else if (arg == "ANDROIDSECURE") {
902 ret_val = PartitionManager.Wipe_Android_Secure();
Dees_Troya13d74f2013-03-24 08:54:55 -0500903 } else if (arg == "LIST") {
904 string Wipe_List, wipe_path;
905 bool skip = false;
906 ret_val = true;
907 TWPartition* wipe_part = NULL;
908
909 DataManager::GetValue("tw_wipe_list", Wipe_List);
Dees_Troy2673cec2013-04-02 20:22:16 +0000910 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500911 if (!Wipe_List.empty()) {
912 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
913 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
914 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
Dees_Troy2673cec2013-04-02 20:22:16 +0000915 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500916 if (wipe_path == "/and-sec") {
917 if (!PartitionManager.Wipe_Android_Secure()) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000918 LOGERR("Unable to wipe android secure\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500919 ret_val = false;
920 break;
921 } else {
922 skip = true;
923 }
924 } else if (wipe_path == "DALVIK") {
925 if (!PartitionManager.Wipe_Dalvik_Cache()) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000926 LOGERR("Failed to wipe dalvik\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500927 ret_val = false;
928 break;
929 } else {
930 skip = true;
931 }
Dees_Troy74fb2e92013-04-15 14:35:47 +0000932 } else if (wipe_path == "INTERNAL") {
933 if (!PartitionManager.Wipe_Media_From_Data()) {
934 ret_val = false;
935 break;
936 } else {
937 skip = true;
938 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500939 }
940 if (!skip) {
941 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000942 LOGERR("Unable to wipe '%s'\n", wipe_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500943 ret_val = false;
944 break;
945 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
946 arg = wipe_path;
947 }
948 } else {
949 skip = false;
950 }
951 start_pos = end_pos + 1;
952 end_pos = Wipe_List.find(";", start_pos);
953 }
954 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400955 } else
956 ret_val = PartitionManager.Wipe_By_Path(arg);
Ethan Yonker83e82572014-04-04 10:59:28 -0500957#ifdef TW_OEM_BUILD
Dees_Troy38bd7602012-09-14 13:33:53 -0400958 if (arg == DataManager::GetSettingsStoragePath()) {
959 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
960 string Storage_Path = DataManager::GetSettingsStoragePath();
961
962 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000963 LOGINFO("Making TWRP folder and saving settings.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400964 Storage_Path += "/TWRP";
965 mkdir(Storage_Path.c_str(), 0777);
966 DataManager::Flush();
967 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000968 LOGERR("Unable to recreate TWRP folder and save settings.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400969 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400970 }
Ethan Yonker83e82572014-04-04 10:59:28 -0500971#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400972 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400973 PartitionManager.Update_System_Details();
Dees_Troy38bd7602012-09-14 13:33:53 -0400974 if (ret_val)
975 ret_val = 0; // 0 is success
976 else
977 ret_val = 1; // 1 is failure
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200978 operation_end(ret_val, simulate);
979 return 0;
980 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400981 if (function == "refreshsizes")
982 {
983 operation_start("Refreshing Sizes");
984 if (simulate) {
985 simulate_progress_bar();
986 } else
Dees_Troy5bf43922012-09-07 16:07:55 -0400987 PartitionManager.Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -0400988 operation_end(0, simulate);
Dees_Troy83bd4832013-05-04 12:39:56 +0000989 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400990 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200991 if (function == "nandroid")
992 {
993 operation_start("Nandroid");
Dees_Troy43d8b002012-09-17 16:00:01 -0400994 int ret = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400995
996 if (simulate) {
997 DataManager::SetValue("tw_partition", "Simulation");
998 simulate_progress_bar();
999 } else {
1000 if (arg == "backup") {
1001 string Backup_Name;
1002 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees Troyb21cc642013-09-10 17:36:41 +00001003 if (Backup_Name == "(Auto Generate)" || Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(true) == 0) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001004 ret = PartitionManager.Run_Backup();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001005 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001006 else {
1007 operation_end(1, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -04001008 return -1;
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001009
Dees_Troy43d8b002012-09-17 16:00:01 -04001010 }
Dees Troyb21cc642013-09-10 17:36:41 +00001011 DataManager::SetValue(TW_BACKUP_NAME, "(Auto Generate)");
Dees_Troy51a0e822012-09-05 15:24:24 -04001012 } else if (arg == "restore") {
1013 string Restore_Name;
1014 DataManager::GetValue("tw_restore", Restore_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -04001015 ret = PartitionManager.Run_Restore(Restore_Name);
Dees_Troy51a0e822012-09-05 15:24:24 -04001016 } else {
1017 operation_end(1, simulate);
1018 return -1;
1019 }
1020 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001021 DataManager::SetValue("tw_encrypt_backup", 0);
Dees_Troy43d8b002012-09-17 16:00:01 -04001022 if (ret == false)
1023 ret = 1; // 1 for failure
1024 else
1025 ret = 0; // 0 for success
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001026 operation_end(ret, simulate);
Dees_Troy83bd4832013-05-04 12:39:56 +00001027 return 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001028 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001029 if (function == "fixpermissions")
1030 {
1031 operation_start("Fix Permissions");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001032 LOGINFO("fix permissions started!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001033 if (simulate) {
1034 simulate_progress_bar();
Dees_Troy4be841b2012-09-26 14:07:15 -04001035 } else {
Dees_Troy6480ce02012-10-10 10:26:54 -04001036 int op_status = PartitionManager.Fix_Permissions();
1037 if (op_status != 0)
1038 op_status = 1; // failure
1039 operation_end(op_status, simulate);
Dees_Troy4be841b2012-09-26 14:07:15 -04001040 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001041 return 0;
1042 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001043 if (function == "dd")
1044 {
1045 operation_start("imaging");
Dees_Troy51a0e822012-09-05 15:24:24 -04001046
1047 if (simulate) {
1048 simulate_progress_bar();
1049 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001050 string cmd = "dd " + arg;
Vojtech Bocek05534202013-09-11 08:11:56 +02001051 TWFunc::Exec_Cmd(cmd);
Dees_Troy51a0e822012-09-05 15:24:24 -04001052 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001053 operation_end(0, simulate);
1054 return 0;
1055 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001056 if (function == "partitionsd")
1057 {
1058 operation_start("Partition SD Card");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001059 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001060
1061 if (simulate) {
1062 simulate_progress_bar();
1063 } else {
1064 int allow_partition;
1065 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1066 if (allow_partition == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001067 gui_print("This device does not have a real SD Card!\nAborting!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001068 } else {
Dees_Troy9350b8d2012-09-27 12:38:38 -04001069 if (!PartitionManager.Partition_SDCard())
1070 ret_val = 1; // failed
Dees_Troy51a0e822012-09-05 15:24:24 -04001071 }
1072 }
Dees_Troy9350b8d2012-09-27 12:38:38 -04001073 operation_end(ret_val, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -04001074 return 0;
1075 }
1076 if (function == "installhtcdumlock")
1077 {
1078 operation_start("Install HTC Dumlock");
1079 if (simulate) {
1080 simulate_progress_bar();
1081 } else
Dees_Troy38bd7602012-09-14 13:33:53 -04001082 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -04001083
1084 operation_end(0, simulate);
1085 return 0;
1086 }
1087 if (function == "htcdumlockrestoreboot")
1088 {
1089 operation_start("HTC Dumlock Restore Boot");
1090 if (simulate) {
1091 simulate_progress_bar();
1092 } else
Dees_Troy38bd7602012-09-14 13:33:53 -04001093 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001094
1095 operation_end(0, simulate);
1096 return 0;
1097 }
1098 if (function == "htcdumlockreflashrecovery")
1099 {
1100 operation_start("HTC Dumlock Reflash Recovery");
1101 if (simulate) {
1102 simulate_progress_bar();
1103 } else
Dees_Troy38bd7602012-09-14 13:33:53 -04001104 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001105
1106 operation_end(0, simulate);
1107 return 0;
1108 }
1109 if (function == "cmd")
1110 {
1111 int op_status = 0;
1112
1113 operation_start("Command");
Dees_Troy2673cec2013-04-02 20:22:16 +00001114 LOGINFO("Running command: '%s'\n", arg.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001115 if (simulate) {
1116 simulate_progress_bar();
1117 } else {
Vojtech Bocek05534202013-09-11 08:11:56 +02001118 op_status = TWFunc::Exec_Cmd(arg);
Dees_Troy51a0e822012-09-05 15:24:24 -04001119 if (op_status != 0)
1120 op_status = 1;
1121 }
1122
1123 operation_end(op_status, simulate);
1124 return 0;
1125 }
1126 if (function == "terminalcommand")
1127 {
1128 int op_status = 0;
1129 string cmdpath, command;
1130
1131 DataManager::GetValue("tw_terminal_location", cmdpath);
1132 operation_start("CommandOutput");
Dees_Troy2673cec2013-04-02 20:22:16 +00001133 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001134 if (simulate) {
1135 simulate_progress_bar();
1136 operation_end(op_status, simulate);
1137 } else {
Dees_Troy4be841b2012-09-26 14:07:15 -04001138 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
Dees_Troy2673cec2013-04-02 20:22:16 +00001139 LOGINFO("Actual command is: '%s'\n", command.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001140 DataManager::SetValue("tw_terminal_command_thread", command);
1141 DataManager::SetValue("tw_terminal_state", 1);
1142 DataManager::SetValue("tw_background_thread_running", 1);
1143 op_status = pthread_create(&terminal_command, NULL, command_thread, NULL);
1144 if (op_status != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001145 LOGERR("Error starting terminal command thread, %i.\n", op_status);
Dees_Troy51a0e822012-09-05 15:24:24 -04001146 DataManager::SetValue("tw_terminal_state", 0);
1147 DataManager::SetValue("tw_background_thread_running", 0);
1148 operation_end(1, simulate);
1149 }
1150 }
1151 return 0;
1152 }
1153 if (function == "killterminal")
1154 {
1155 int op_status = 0;
1156
Dees_Troy2673cec2013-04-02 20:22:16 +00001157 LOGINFO("Sending kill command...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001158 operation_start("KillCommand");
1159 DataManager::SetValue("tw_operation_status", 0);
1160 DataManager::SetValue("tw_operation_state", 1);
1161 DataManager::SetValue("tw_terminal_state", 0);
1162 DataManager::SetValue("tw_background_thread_running", 0);
1163 DataManager::SetValue(TW_ACTION_BUSY, 0);
1164 return 0;
1165 }
1166 if (function == "reinjecttwrp")
1167 {
1168 int op_status = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001169 operation_start("ReinjectTWRP");
Dees_Troy2673cec2013-04-02 20:22:16 +00001170 gui_print("Injecting TWRP into boot image...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001171 if (simulate) {
1172 simulate_progress_bar();
1173 } else {
Vojtech Bocek05534202013-09-11 08:11:56 +02001174 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Dees_Troy2673cec2013-04-02 20:22:16 +00001175 gui_print("TWRP injection complete.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001176 }
1177
1178 operation_end(op_status, simulate);
1179 return 0;
1180 }
1181 if (function == "checkbackupname")
1182 {
1183 int op_status = 0;
1184
1185 operation_start("CheckBackupName");
1186 if (simulate) {
1187 simulate_progress_bar();
1188 } else {
Dees_Troyc9ff7a32012-09-27 10:09:41 -04001189 op_status = PartitionManager.Check_Backup_Name(true);
Dees_Troy51a0e822012-09-05 15:24:24 -04001190 if (op_status != 0)
1191 op_status = 1;
1192 }
1193
1194 operation_end(op_status, simulate);
1195 return 0;
1196 }
1197 if (function == "decrypt")
1198 {
1199 int op_status = 0;
1200
1201 operation_start("Decrypt");
1202 if (simulate) {
1203 simulate_progress_bar();
1204 } else {
1205 string Password;
1206 DataManager::GetValue("tw_crypto_password", Password);
Dees_Troy5bf43922012-09-07 16:07:55 -04001207 op_status = PartitionManager.Decrypt_Device(Password);
Dees_Troy51a0e822012-09-05 15:24:24 -04001208 if (op_status != 0)
1209 op_status = 1;
1210 else {
1211 int load_theme = 1;
1212
1213 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001214
1215 if (load_theme) {
1216 int has_datamedia;
1217
1218 // Check for a custom theme and load it if exists
1219 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1220 if (has_datamedia != 0) {
1221 struct stat st;
1222 int check = 0;
1223 std::string theme_path;
1224
1225 theme_path = DataManager::GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -04001226 if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001227 LOGERR("Unable to mount %s during reload function startup.\n", theme_path.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001228 check = 1;
1229 }
1230
1231 theme_path += "/TWRP/theme/ui.zip";
1232 if (check == 0 && stat(theme_path.c_str(), &st) == 0) {
1233 if (PageManager::ReloadPackage("TWRP", theme_path) != 0)
1234 {
1235 // Loading the custom theme failed - try loading the stock theme
Dees_Troy2673cec2013-04-02 20:22:16 +00001236 LOGINFO("Attempting to reload stock theme...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001237 if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
1238 {
Dees_Troy2673cec2013-04-02 20:22:16 +00001239 LOGERR("Failed to load base packages.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001240 }
1241 }
1242 }
1243 }
1244 }
1245 }
1246 }
1247
1248 operation_end(op_status, simulate);
1249 return 0;
1250 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001251 if (function == "adbsideload")
1252 {
1253 int ret = 0;
1254
1255 operation_start("Sideload");
1256 if (simulate) {
1257 simulate_progress_bar();
1258 } else {
1259 int wipe_cache = 0;
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001260 int wipe_dalvik = 0;
Dees_Troycfb63ae2012-09-19 14:30:17 -04001261
Dees_Troy2673cec2013-04-02 20:22:16 +00001262 gui_print("Starting ADB sideload feature...\n");
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001263 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
Ethan Yonker24813422014-11-07 17:19:07 -06001264 ret = apply_from_adb("/");
Dees_Troy2673cec2013-04-02 20:22:16 +00001265 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
Ethan Yonker24813422014-11-07 17:19:07 -06001266 char file_prop[PROPERTY_VALUE_MAX];
1267 property_get("tw_sideload_file", file_prop, "error");
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001268 if (ret != 0) {
Dees_Troycfb63ae2012-09-19 14:30:17 -04001269 ret = 1; // failure
Ethan Yonker24813422014-11-07 17:19:07 -06001270 if (ret == -2)
1271 gui_print("You need adb 1.0.32 or newer to sideload to this device.\n");
Dees_Troy2673cec2013-04-02 20:22:16 +00001272 } else {
Ethan Yonker24813422014-11-07 17:19:07 -06001273 if (TWinstall_zip(file_prop, &wipe_cache) == 0) {
1274 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1275 PartitionManager.Wipe_By_Path("/cache");
1276 if (wipe_dalvik)
1277 PartitionManager.Wipe_Dalvik_Cache();
1278 } else {
1279 ret = 1; // failure
1280 }
1281 set_usb_driver(false);
1282 maybe_restart_adbd();
1283 }
1284 if (strcmp(file_prop, "error") != 0) {
1285 struct stat st;
1286 stat("/sideload/exit", &st);
1287 int child_pid, status;
1288 char child_prop[PROPERTY_VALUE_MAX];
1289 property_get("tw_child_pid", child_prop, "error");
1290 if (strcmp(child_prop, "error") == 0) {
1291 LOGERR("Unable to get child ID from prop\n");
1292 } else {
1293 child_pid = atoi(child_prop);
1294 LOGINFO("Waiting for child sideload process to exit.\n");
1295 waitpid(child_pid, &status, 0);
1296 }
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -05001297 }
Dees_Troy06b4fe92012-10-16 11:43:20 -04001298 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
1299 operation_start("ReinjectTWRP");
Dees_Troy2673cec2013-04-02 20:22:16 +00001300 gui_print("Injecting TWRP into boot image...\n");
Dees_Troy06b4fe92012-10-16 11:43:20 -04001301 if (simulate) {
1302 simulate_progress_bar();
1303 } else {
1304 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
1305 if (Boot == NULL || Boot->Current_File_System != "emmc")
Vojtech Bocek05534202013-09-11 08:11:56 +02001306 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Dees_Troy06b4fe92012-10-16 11:43:20 -04001307 else {
1308 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001309 TWFunc::Exec_Cmd(injectcmd);
Dees_Troy06b4fe92012-10-16 11:43:20 -04001310 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001311 gui_print("TWRP injection complete.\n");
Dees_Troy06b4fe92012-10-16 11:43:20 -04001312 }
1313 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001314 }
1315 operation_end(ret, simulate);
1316 return 0;
1317 }
Dees_Troycfb63ae2012-09-19 14:30:17 -04001318 if (function == "adbsideloadcancel")
1319 {
Ethan Yonker24813422014-11-07 17:19:07 -06001320 int child_pid, status;
Dees_Troy2673cec2013-04-02 20:22:16 +00001321 char child_prop[PROPERTY_VALUE_MAX];
Ethan Yonker24813422014-11-07 17:19:07 -06001322 struct stat st;
1323 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
1324 gui_print("Cancelling ADB sideload...\n");
1325 stat("/sideload/exit", &st);
1326 sleep(1);
Dees_Troy2673cec2013-04-02 20:22:16 +00001327 property_get("tw_child_pid", child_prop, "error");
1328 if (strcmp(child_prop, "error") == 0) {
1329 LOGERR("Unable to get child ID from prop\n");
1330 return 0;
1331 }
1332 child_pid = atoi(child_prop);
Dees_Troycfb63ae2012-09-19 14:30:17 -04001333 kill(child_pid, SIGTERM);
Dees_Troy4bc09ae2013-01-18 17:00:54 +00001334 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
Dees_Troycfb63ae2012-09-19 14:30:17 -04001335 return 0;
1336 }
Dees_Troy6ed34b72013-01-25 15:01:29 +00001337 if (function == "openrecoveryscript") {
1338 operation_start("OpenRecoveryScript");
1339 if (simulate) {
1340 simulate_progress_bar();
1341 } else {
1342 // Check for the SCRIPT_FILE_TMP first as these are AOSP recovery commands
1343 // that we converted to ORS commands during boot in recovery.cpp.
1344 // Run those first.
1345 int reboot = 0;
1346 if (TWFunc::Path_Exists(SCRIPT_FILE_TMP)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001347 gui_print("Processing AOSP recovery commands...\n");
Dees_Troy6ed34b72013-01-25 15:01:29 +00001348 if (OpenRecoveryScript::run_script_file() == 0) {
1349 reboot = 1;
1350 }
1351 }
1352 // Check for the ORS file in /cache and attempt to run those commands.
1353 if (OpenRecoveryScript::check_for_script_file()) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001354 gui_print("Processing OpenRecoveryScript file...\n");
Dees_Troy6ed34b72013-01-25 15:01:29 +00001355 if (OpenRecoveryScript::run_script_file() == 0) {
1356 reboot = 1;
1357 }
1358 }
1359 if (reboot) {
1360 usleep(2000000); // Sleep for 2 seconds before rebooting
1361 TWFunc::tw_reboot(rb_system);
1362 } else {
1363 DataManager::SetValue("tw_page_done", 1);
1364 }
1365 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001366 return 0;
Dees_Troy6ed34b72013-01-25 15:01:29 +00001367 }
Dees_Troy6ef66352013-02-21 08:26:57 -06001368 if (function == "installsu")
1369 {
1370 int op_status = 0;
1371
1372 operation_start("Install SuperSU");
1373 if (simulate) {
1374 simulate_progress_bar();
1375 } else {
1376 if (!TWFunc::Install_SuperSU())
1377 op_status = 1;
1378 }
1379
1380 operation_end(op_status, simulate);
1381 return 0;
1382 }
1383 if (function == "fixsu")
1384 {
1385 int op_status = 0;
1386
1387 operation_start("Fixing Superuser Permissions");
1388 if (simulate) {
1389 simulate_progress_bar();
1390 } else {
Dees Troyf193f882013-09-11 14:56:20 +00001391 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1392 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
Dees_Troy6ef66352013-02-21 08:26:57 -06001393 }
1394
1395 operation_end(op_status, simulate);
1396 return 0;
1397 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001398 if (function == "decrypt_backup")
1399 {
1400 int op_status = 0;
1401
1402 operation_start("Try Restore Decrypt");
1403 if (simulate) {
1404 simulate_progress_bar();
1405 } else {
1406 string Restore_Path, Filename, Password;
1407 DataManager::GetValue("tw_restore", Restore_Path);
1408 Restore_Path += "/";
1409 DataManager::GetValue("tw_restore_password", Password);
Tom Hite5a926722014-09-15 01:31:03 +00001410 TWFunc::SetPerformanceMode(true);
Dees_Troy83bd4832013-05-04 12:39:56 +00001411 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1412 op_status = 0; // success
1413 else
1414 op_status = 1; // fail
Tom Hite5a926722014-09-15 01:31:03 +00001415 TWFunc::SetPerformanceMode(false);
Dees_Troy83bd4832013-05-04 12:39:56 +00001416 }
1417
1418 operation_end(op_status, simulate);
1419 return 0;
1420 }
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001421 if (function == "repair")
1422 {
1423 int op_status = 0;
1424
1425 operation_start("Repair Partition");
1426 if (simulate) {
1427 simulate_progress_bar();
1428 } else {
1429 string part_path;
1430 DataManager::GetValue("tw_partition_mount_point", part_path);
1431 if (PartitionManager.Repair_By_Path(part_path, true)) {
1432 op_status = 0; // success
1433 } else {
1434 LOGERR("Error repairing file system.\n");
1435 op_status = 1; // fail
1436 }
1437 }
1438
1439 operation_end(op_status, simulate);
1440 return 0;
1441 }
1442 if (function == "changefilesystem")
1443 {
1444 int op_status = 0;
1445
1446 operation_start("Change File System");
1447 if (simulate) {
1448 simulate_progress_bar();
1449 } else {
1450 string part_path, file_system;
1451 DataManager::GetValue("tw_partition_mount_point", part_path);
1452 DataManager::GetValue("tw_action_new_file_system", file_system);
1453 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1454 op_status = 0; // success
1455 } else {
1456 LOGERR("Error changing file system.\n");
1457 op_status = 1; // fail
1458 }
1459 }
jrior001aad03112014-07-05 10:31:21 -04001460 PartitionManager.Update_System_Details();
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001461 operation_end(op_status, simulate);
1462 return 0;
1463 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001464 if (function == "startmtp")
1465 {
1466 int op_status = 0;
1467
1468 operation_start("Start MTP");
1469 if (PartitionManager.Enable_MTP())
1470 op_status = 0; // success
1471 else
1472 op_status = 1; // fail
1473
1474 operation_end(op_status, simulate);
1475 return 0;
1476 }
1477 if (function == "stopmtp")
1478 {
1479 int op_status = 0;
1480
1481 operation_start("Stop MTP");
1482 if (PartitionManager.Disable_MTP())
1483 op_status = 0; // success
1484 else
1485 op_status = 1; // fail
1486
1487 operation_end(op_status, simulate);
1488 return 0;
1489 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001490 }
1491 else
1492 {
Dees_Troy83bd4832013-05-04 12:39:56 +00001493 pthread_t t;
1494 pthread_create(&t, NULL, thread_start, this);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001495 return 0;
1496 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001497 LOGERR("Unknown action '%s'\n", function.c_str());
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001498 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -04001499}
1500
1501int GUIAction::getKeyByName(std::string key)
1502{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001503 if (key == "home") return KEY_HOME;
1504 else if (key == "menu") return KEY_MENU;
1505 else if (key == "back") return KEY_BACK;
1506 else if (key == "search") return KEY_SEARCH;
1507 else if (key == "voldown") return KEY_VOLUMEDOWN;
1508 else if (key == "volup") return KEY_VOLUMEUP;
1509 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001510 int ret_val;
1511 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1512 if (!ret_val)
1513 return KEY_POWER;
1514 else
1515 return ret_val;
1516 }
1517
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001518 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001519}
1520
1521void* GUIAction::command_thread(void *cookie)
1522{
1523 string command;
1524 FILE* fp;
1525 char line[512];
1526
1527 DataManager::GetValue("tw_terminal_command_thread", command);
Dees_Troy8170a922012-09-18 15:40:25 -04001528 fp = popen(command.c_str(), "r");
Dees_Troy51a0e822012-09-05 15:24:24 -04001529 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001530 LOGERR("Error opening command to run.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001531 } else {
1532 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0;
1533 struct timeval timeout;
1534 fd_set fdset;
1535
1536 while(keep_going)
1537 {
1538 FD_ZERO(&fdset);
1539 FD_SET(fd, &fdset);
1540 timeout.tv_sec = 0;
1541 timeout.tv_usec = 400000;
1542 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1543 if (has_data == 0) {
1544 // Timeout reached
1545 DataManager::GetValue("tw_terminal_state", check);
1546 if (check == 0) {
1547 keep_going = 0;
1548 }
1549 } else if (has_data < 0) {
1550 // End of execution
1551 keep_going = 0;
1552 } else {
1553 // Try to read output
Dees_Troy4be841b2012-09-26 14:07:15 -04001554 memset(line, 0, sizeof(line));
Dees_Troy51a0e822012-09-05 15:24:24 -04001555 bytes_read = read(fd, line, sizeof(line));
1556 if (bytes_read > 0)
Dees_Troy2673cec2013-04-02 20:22:16 +00001557 gui_print("%s", line); // Display output
Dees_Troy51a0e822012-09-05 15:24:24 -04001558 else
1559 keep_going = 0; // Done executing
1560 }
1561 }
1562 fclose(fp);
1563 }
1564 DataManager::SetValue("tw_operation_status", 0);
1565 DataManager::SetValue("tw_operation_state", 1);
1566 DataManager::SetValue("tw_terminal_state", 0);
1567 DataManager::SetValue("tw_background_thread_running", 0);
1568 DataManager::SetValue(TW_ACTION_BUSY, 0);
1569 return NULL;
1570}