blob: ecb035ebb9bf74f9e312ac9d7268f18d664b4cd3 [file] [log] [blame]
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06001/*
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>
Matt Mower0c88b842017-01-15 16:00:49 -060035#include <private/android_filesystem_config.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"
thatcc8ddca2015-01-03 01:59:36 +010044#include "../fuse_sideload.h"
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050045#include "blanktimer.hpp"
Ethan Yonker8373cfe2017-09-08 06:50:54 -050046#include "../twinstall.h"
Ethan Yonkerfbb43532015-12-28 21:54:50 +010047
Dees_Troy51a0e822012-09-05 15:24:24 -040048extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000049#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040050#include "../variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000051#include "cutils/properties.h"
Ethan Yonker24813422014-11-07 17:19:07 -060052#include "../adb_install.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040053};
Ethan Yonkerf1179622016-08-25 15:32:21 -050054#include "../set_metadata.h"
Ethan Yonkerfbb43532015-12-28 21:54:50 +010055#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040056
57#include "rapidxml.hpp"
58#include "objects.hpp"
bigbiff7abc5fe2015-01-17 16:53:12 -050059#include "../tw_atomic.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040060
that3f7b1ac2014-12-30 11:30:13 +010061GUIAction::mapFunc GUIAction::mf;
thatc6085482015-01-09 22:12:43 +010062std::set<string> GUIAction::setActionsRunningInCallerThread;
that3f7b1ac2014-12-30 11:30:13 +010063static string zip_queue[10];
64static int zip_queue_index;
thatcc8ddca2015-01-03 01:59:36 +010065pid_t sideload_child_pid;
Noah Jacobson5a79f672019-04-28 00:10:07 -040066extern std::vector<users_struct> Users_List;
Mohd Faraz3c25ab32020-08-12 12:29:42 +000067extern GUITerminal* term;
that3f7b1ac2014-12-30 11:30:13 +010068
that73a52952015-01-28 23:07:34 +010069static void *ActionThread_work_wrapper(void *data);
70
71class ActionThread
72{
73public:
74 ActionThread();
75 ~ActionThread();
76
77 void threadActions(GUIAction *act);
78 void run(void *data);
79private:
80 friend void *ActionThread_work_wrapper(void*);
81 struct ThreadData
82 {
83 ActionThread *this_;
84 GUIAction *act;
85 ThreadData(ActionThread *this_, GUIAction *act) : this_(this_), act(act) {}
86 };
87
88 pthread_t m_thread;
89 bool m_thread_running;
90 pthread_mutex_t m_act_lock;
91};
92
93static ActionThread action_thread; // for all kinds of longer running actions
94static ActionThread cancel_thread; // for longer running "cancel" actions
thatc6085482015-01-09 22:12:43 +010095
96static void *ActionThread_work_wrapper(void *data)
97{
that73a52952015-01-28 23:07:34 +010098 static_cast<ActionThread::ThreadData*>(data)->this_->run(data);
thatc6085482015-01-09 22:12:43 +010099 return NULL;
100}
101
102ActionThread::ActionThread()
103{
104 m_thread_running = false;
105 pthread_mutex_init(&m_act_lock, NULL);
106}
107
108ActionThread::~ActionThread()
109{
110 pthread_mutex_lock(&m_act_lock);
Matt Mowera8a89d12016-12-30 18:10:37 -0600111 if (m_thread_running) {
thatc6085482015-01-09 22:12:43 +0100112 pthread_mutex_unlock(&m_act_lock);
113 pthread_join(m_thread, NULL);
114 } else {
115 pthread_mutex_unlock(&m_act_lock);
116 }
117 pthread_mutex_destroy(&m_act_lock);
118}
119
that7d3b54f2015-01-09 22:52:51 +0100120void ActionThread::threadActions(GUIAction *act)
thatc6085482015-01-09 22:12:43 +0100121{
122 pthread_mutex_lock(&m_act_lock);
123 if (m_thread_running) {
124 pthread_mutex_unlock(&m_act_lock);
that7d3b54f2015-01-09 22:52:51 +0100125 LOGERR("Another threaded action is already running -- not running %u actions starting with '%s'\n",
126 act->mActions.size(), act->mActions[0].mFunction.c_str());
thatc6085482015-01-09 22:12:43 +0100127 } else {
128 m_thread_running = true;
129 pthread_mutex_unlock(&m_act_lock);
that73a52952015-01-28 23:07:34 +0100130 ThreadData *d = new ThreadData(this, act);
thatc6085482015-01-09 22:12:43 +0100131 pthread_create(&m_thread, NULL, &ActionThread_work_wrapper, d);
132 }
133}
134
135void ActionThread::run(void *data)
136{
137 ThreadData *d = (ThreadData*)data;
138 GUIAction* act = d->act;
139
140 std::vector<GUIAction::Action>::iterator it;
that7d3b54f2015-01-09 22:52:51 +0100141 for (it = act->mActions.begin(); it != act->mActions.end(); ++it)
thatc6085482015-01-09 22:12:43 +0100142 act->doAction(*it);
143
144 pthread_mutex_lock(&m_act_lock);
145 m_thread_running = false;
146 pthread_mutex_unlock(&m_act_lock);
147 delete d;
148}
149
Dees_Troy51a0e822012-09-05 15:24:24 -0400150GUIAction::GUIAction(xml_node<>* node)
Vojtech Bocekede51c52014-02-07 23:58:09 +0100151 : GUIObject(node)
Dees_Troy51a0e822012-09-05 15:24:24 -0400152{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200153 xml_node<>* child;
154 xml_node<>* actions;
155 xml_attribute<>* attr;
Dees_Troy51a0e822012-09-05 15:24:24 -0400156
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200157 if (!node) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400158
that3f7b1ac2014-12-30 11:30:13 +0100159 if (mf.empty()) {
Vojtech Bocekecd89182015-01-15 16:28:54 +0100160#define ADD_ACTION(n) mf[#n] = &GUIAction::n
161#define ADD_ACTION_EX(name, func) mf[name] = &GUIAction::func
thatc6085482015-01-09 22:12:43 +0100162 // These actions will be run in the caller's thread
Vojtech Bocekecd89182015-01-15 16:28:54 +0100163 ADD_ACTION(reboot);
164 ADD_ACTION(home);
165 ADD_ACTION(key);
166 ADD_ACTION(page);
167 ADD_ACTION(reload);
168 ADD_ACTION(readBackup);
169 ADD_ACTION(set);
170 ADD_ACTION(clear);
171 ADD_ACTION(mount);
172 ADD_ACTION(unmount);
173 ADD_ACTION_EX("umount", unmount);
174 ADD_ACTION(restoredefaultsettings);
175 ADD_ACTION(copylog);
176 ADD_ACTION(compute);
177 ADD_ACTION_EX("addsubtract", compute);
178 ADD_ACTION(setguitimezone);
179 ADD_ACTION(overlay);
180 ADD_ACTION(queuezip);
181 ADD_ACTION(cancelzip);
182 ADD_ACTION(queueclear);
183 ADD_ACTION(sleep);
Matt Mower9a2a2052016-05-31 21:31:22 -0500184 ADD_ACTION(sleepcounter);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100185 ADD_ACTION(appenddatetobackupname);
186 ADD_ACTION(generatebackupname);
187 ADD_ACTION(checkpartitionlist);
188 ADD_ACTION(getpartitiondetails);
189 ADD_ACTION(screenshot);
190 ADD_ACTION(setbrightness);
191 ADD_ACTION(fileexists);
192 ADD_ACTION(killterminal);
193 ADD_ACTION(checkbackupname);
194 ADD_ACTION(adbsideloadcancel);
195 ADD_ACTION(fixsu);
196 ADD_ACTION(startmtp);
197 ADD_ACTION(stopmtp);
198 ADD_ACTION(cancelbackup);
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500199 ADD_ACTION(checkpartitionlifetimewrites);
200 ADD_ACTION(mountsystemtoggle);
Ethan Yonker74db1572015-10-28 12:44:49 -0500201 ADD_ACTION(setlanguage);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -0600202 ADD_ACTION(checkforapp);
Matt Mower9472ba12016-01-20 18:12:47 -0600203 ADD_ACTION(togglebacklight);
Mohd Faraz3c25ab32020-08-12 12:29:42 +0000204 ADD_ACTION(changeterminal);
thatc6085482015-01-09 22:12:43 +0100205
206 // remember actions that run in the caller thread
207 for (mapFunc::const_iterator it = mf.begin(); it != mf.end(); ++it)
208 setActionsRunningInCallerThread.insert(it->first);
209
210 // These actions will run in a separate thread
Vojtech Bocekecd89182015-01-15 16:28:54 +0100211 ADD_ACTION(flash);
212 ADD_ACTION(wipe);
213 ADD_ACTION(refreshsizes);
214 ADD_ACTION(nandroid);
Ethan Yonkerb5fab762016-01-28 14:03:33 -0600215 ADD_ACTION(fixcontexts);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100216 ADD_ACTION(fixpermissions);
217 ADD_ACTION(dd);
218 ADD_ACTION(partitionsd);
219 ADD_ACTION(installhtcdumlock);
220 ADD_ACTION(htcdumlockrestoreboot);
221 ADD_ACTION(htcdumlockreflashrecovery);
222 ADD_ACTION(cmd);
223 ADD_ACTION(terminalcommand);
224 ADD_ACTION(reinjecttwrp);
225 ADD_ACTION(decrypt);
226 ADD_ACTION(adbsideload);
227 ADD_ACTION(openrecoveryscript);
228 ADD_ACTION(installsu);
229 ADD_ACTION(decrypt_backup);
230 ADD_ACTION(repair);
Ethan Yonkera2719152015-05-28 09:44:41 -0500231 ADD_ACTION(resize);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100232 ADD_ACTION(changefilesystem);
233 ADD_ACTION(flashimage);
that10ae24f2015-12-26 20:53:51 +0100234 ADD_ACTION(twcmd);
Ethan Yonker1b190162016-12-05 15:25:19 -0600235 ADD_ACTION(setbootslot);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -0600236 ADD_ACTION(installapp);
Ethan Yonker76bbd3a2019-05-10 10:50:04 -0500237 ADD_ACTION(uninstalltwrpsystemapp);
Ethan Yonker53796e72019-01-11 22:49:52 -0600238 ADD_ACTION(repackimage);
239 ADD_ACTION(fixabrecoverybootloop);
Captain Throwback3b556102021-02-12 19:32:36 -0500240#ifndef TW_EXCLUDE_NANO
241 ADD_ACTION(editfile);
242#endif
that3f7b1ac2014-12-30 11:30:13 +0100243 }
244
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200245 // First, get the action
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600246 actions = FindNode(node, "actions");
247 if (actions) child = FindNode(actions, "action");
248 else child = FindNode(node, "action");
Dees_Troy51a0e822012-09-05 15:24:24 -0400249
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200250 if (!child) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400251
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200252 while (child)
253 {
254 Action action;
Dees_Troy51a0e822012-09-05 15:24:24 -0400255
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200256 attr = child->first_attribute("function");
257 if (!attr) return;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500258
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200259 action.mFunction = attr->value();
260 action.mArg = child->value();
261 mActions.push_back(action);
Dees_Troy51a0e822012-09-05 15:24:24 -0400262
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200263 child = child->next_sibling("action");
264 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400265
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200266 // Now, let's get either the key or region
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600267 child = FindNode(node, "touch");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200268 if (child)
269 {
270 attr = child->first_attribute("key");
271 if (attr)
272 {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100273 std::vector<std::string> keys = TWFunc::Split_String(attr->value(), "+");
Matt Mowera8a89d12016-12-30 18:10:37 -0600274 for (size_t i = 0; i < keys.size(); ++i)
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100275 {
276 const int key = getKeyByName(keys[i]);
277 mKeys[key] = false;
278 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200279 }
280 else
281 {
282 attr = child->first_attribute("x");
283 if (!attr) return;
284 mActionX = atol(attr->value());
285 attr = child->first_attribute("y");
286 if (!attr) return;
287 mActionY = atol(attr->value());
288 attr = child->first_attribute("w");
289 if (!attr) return;
290 mActionW = atol(attr->value());
291 attr = child->first_attribute("h");
292 if (!attr) return;
293 mActionH = atol(attr->value());
294 }
295 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400296}
297
Matt Mower25036b72016-06-24 12:30:18 -0500298int GUIAction::NotifyTouch(TOUCH_STATE state, int x __unused, int y __unused)
Dees_Troy51a0e822012-09-05 15:24:24 -0400299{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200300 if (state == TOUCH_RELEASE)
301 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400302
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200303 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400304}
305
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100306int GUIAction::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -0400307{
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100308 std::map<int, bool>::iterator itr = mKeys.find(key);
Matt Mowera8a89d12016-12-30 18:10:37 -0600309 if (itr == mKeys.end())
that8834a0f2016-01-05 23:29:30 +0100310 return 1;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100311
312 bool prevState = itr->second;
313 itr->second = down;
314
315 // If there is only one key for this action, wait for key up so it
316 // doesn't trigger with multi-key actions.
317 // Else, check if all buttons are pressed, then consume their release events
318 // so they don't trigger one-button actions and reset mKeys pressed status
Matt Mowera8a89d12016-12-30 18:10:37 -0600319 if (mKeys.size() == 1) {
320 if (!down && prevState) {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100321 doActions();
thatd4725ca2016-01-19 00:15:21 +0100322 return 0;
323 }
Matt Mowera8a89d12016-12-30 18:10:37 -0600324 } else if (down) {
325 for (itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
326 if (!itr->second)
that8834a0f2016-01-05 23:29:30 +0100327 return 1;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100328 }
329
330 // Passed, all req buttons are pressed, reset them and consume release events
331 HardwareKeyboard *kb = PageManager::GetHardwareKeyboard();
Matt Mowera8a89d12016-12-30 18:10:37 -0600332 for (itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100333 kb->ConsumeKeyRelease(itr->first);
334 itr->second = false;
335 }
336
337 doActions();
thatd4725ca2016-01-19 00:15:21 +0100338 return 0;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100339 }
340
thatd4725ca2016-01-19 00:15:21 +0100341 return 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400342}
343
Vojtech Bocek07220562014-02-08 02:05:33 +0100344int GUIAction::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400345{
Vojtech Bocek07220562014-02-08 02:05:33 +0100346 GUIObject::NotifyVarChange(varName, value);
347
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100348 if (varName.empty() && !isConditionValid() && mKeys.empty() && !mActionW)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200349 doActions();
Matt Mowera8a89d12016-12-30 18:10:37 -0600350 else if ((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200351 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400352
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200353 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400354}
355
356void GUIAction::simulate_progress_bar(void)
357{
Ethan Yonker74db1572015-10-28 12:44:49 -0500358 gui_msg("simulating=Simulating actions...");
Dees_Troy51a0e822012-09-05 15:24:24 -0400359 for (int i = 0; i < 5; i++)
360 {
bigbiff7abc5fe2015-01-17 16:53:12 -0500361 if (PartitionManager.stop_backup.get_value()) {
362 DataManager::SetValue("tw_cancel_backup", 1);
Matt Mower3c366972015-12-25 19:28:31 -0600363 gui_msg("backup_cancel=Backup Cancelled");
bigbiff7abc5fe2015-01-17 16:53:12 -0500364 DataManager::SetValue("ui_progress", 0);
365 PartitionManager.stop_backup.set_value(0);
366 return;
367 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400368 usleep(500000);
369 DataManager::SetValue("ui_progress", i * 20);
370 }
371}
372
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600373int GUIAction::flash_zip(std::string filename, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400374{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200375 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400376
377 DataManager::SetValue("ui_progress", 0);
Chaosmasterab164372020-02-03 15:38:02 +0100378 DataManager::SetValue("ui_portion_size", 0);
379 DataManager::SetValue("ui_portion_start", 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400380
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200381 if (filename.empty())
382 {
383 LOGERR("No file specified.\n");
384 return -1;
385 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400386
Ethan Yonker9598c072016-01-15 21:54:34 -0600387 if (!TWFunc::Path_Exists(filename)) {
388 if (!PartitionManager.Mount_By_Path(filename, true)) {
389 return -1;
390 }
391 if (!TWFunc::Path_Exists(filename)) {
392 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")(filename));
393 return -1;
394 }
395 }
Dees_Troy657c3092012-09-10 20:32:10 -0400396
Dees_Troy51a0e822012-09-05 15:24:24 -0400397 if (simulate) {
398 simulate_progress_bar();
399 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400400 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400401
402 // Now, check if we need to ensure TWRP remains installed...
403 struct stat st;
404 if (stat("/sbin/installTwrp", &st) == 0)
405 {
406 DataManager::SetValue("tw_operation", "Configuring TWRP");
407 DataManager::SetValue("tw_partition", "");
Ethan Yonker74db1572015-10-28 12:44:49 -0500408 gui_msg("config_twrp=Configuring TWRP...");
Vojtech Bocek05534202013-09-11 08:11:56 +0200409 if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall") < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400410 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500411 gui_msg("config_twrp_err=Unable to configure TWRP with this kernel.");
Dees_Troy51a0e822012-09-05 15:24:24 -0400412 }
413 }
414 }
415
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200416 // Done
417 DataManager::SetValue("ui_progress", 100);
418 DataManager::SetValue("ui_progress", 0);
Chaosmasterab164372020-02-03 15:38:02 +0100419 DataManager::SetValue("ui_portion_size", 0);
420 DataManager::SetValue("ui_portion_start", 0);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200421 return ret_val;
Dees_Troy51a0e822012-09-05 15:24:24 -0400422}
423
that73a52952015-01-28 23:07:34 +0100424GUIAction::ThreadType GUIAction::getThreadType(const GUIAction::Action& action)
thatc6085482015-01-09 22:12:43 +0100425{
that73a52952015-01-28 23:07:34 +0100426 string func = gui_parse_text(action.mFunction);
427 bool needsThread = setActionsRunningInCallerThread.find(func) == setActionsRunningInCallerThread.end();
428 if (needsThread) {
429 if (func == "cancelbackup")
430 return THREAD_CANCEL;
431 else
432 return THREAD_ACTION;
433 }
434 return THREAD_NONE;
thatc6085482015-01-09 22:12:43 +0100435}
436
Dees_Troy51a0e822012-09-05 15:24:24 -0400437int GUIAction::doActions()
438{
that3f7b1ac2014-12-30 11:30:13 +0100439 if (mActions.size() < 1)
440 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400441
that73a52952015-01-28 23:07:34 +0100442 // Determine in which thread to run the actions.
443 // Do it for all actions at once before starting, so that we can cancel the whole batch if the thread is already busy.
444 ThreadType threadType = THREAD_NONE;
that3f7b1ac2014-12-30 11:30:13 +0100445 std::vector<Action>::iterator it;
that73a52952015-01-28 23:07:34 +0100446 for (it = mActions.begin(); it != mActions.end(); ++it) {
447 ThreadType tt = getThreadType(*it);
448 if (tt == THREAD_NONE)
449 continue;
450 if (threadType == THREAD_NONE)
451 threadType = tt;
452 else if (threadType != tt) {
453 LOGERR("Can't mix normal and cancel actions in the same list.\n"
454 "Running the whole batch in the cancel thread.\n");
455 threadType = THREAD_CANCEL;
thatc6085482015-01-09 22:12:43 +0100456 break;
457 }
that7d3b54f2015-01-09 22:52:51 +0100458 }
that73a52952015-01-28 23:07:34 +0100459
460 // Now run the actions in the desired thread.
461 switch (threadType) {
462 case THREAD_ACTION:
463 action_thread.threadActions(this);
464 break;
465
466 case THREAD_CANCEL:
467 cancel_thread.threadActions(this);
468 break;
469
470 default: {
471 // no iterators here because theme reloading might kill our object
472 const size_t cnt = mActions.size();
473 for (size_t i = 0; i < cnt; ++i)
474 doAction(mActions[i]);
475 }
thatc6085482015-01-09 22:12:43 +0100476 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400477
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200478 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400479}
480
that3f7b1ac2014-12-30 11:30:13 +0100481int GUIAction::doAction(Action action)
Dees_Troy51a0e822012-09-05 15:24:24 -0400482{
that3f7b1ac2014-12-30 11:30:13 +0100483 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400484
that3f7b1ac2014-12-30 11:30:13 +0100485 std::string function = gui_parse_text(action.mFunction);
486 std::string arg = gui_parse_text(action.mArg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400487
that3f7b1ac2014-12-30 11:30:13 +0100488 // find function and execute it
489 mapFunc::const_iterator funcitr = mf.find(function);
490 if (funcitr != mf.end())
491 return (this->*funcitr->second)(arg);
492
493 LOGERR("Unknown action '%s'\n", function.c_str());
494 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400495}
496
497void GUIAction::operation_start(const string operation_name)
498{
that3f7b1ac2014-12-30 11:30:13 +0100499 LOGINFO("operation_start: '%s'\n", operation_name.c_str());
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000500 time(&Start);
Dees_Troy51a0e822012-09-05 15:24:24 -0400501 DataManager::SetValue(TW_ACTION_BUSY, 1);
502 DataManager::SetValue("ui_progress", 0);
Chaosmasterab164372020-02-03 15:38:02 +0100503 DataManager::SetValue("ui_portion_size", 0);
504 DataManager::SetValue("ui_portion_start", 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400505 DataManager::SetValue("tw_operation", operation_name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400506 DataManager::SetValue("tw_operation_state", 0);
Ethan Yonkerd83c9ea2015-01-02 15:22:31 -0600507 DataManager::SetValue("tw_operation_status", 0);
bigbiff349ea552020-06-19 16:07:38 -0400508 bool tw_ab_device = TWFunc::get_log_dir() != CACHE_LOGS_DIR;
bigbiff bigbiff19874f12019-01-08 20:06:57 -0500509 DataManager::SetValue("tw_ab_device", tw_ab_device);
Dees_Troy51a0e822012-09-05 15:24:24 -0400510}
511
that3f7b1ac2014-12-30 11:30:13 +0100512void GUIAction::operation_end(const int operation_status)
Dees_Troy51a0e822012-09-05 15:24:24 -0400513{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000514 time_t Stop;
Dees_Troy51a0e822012-09-05 15:24:24 -0400515 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400516 DataManager::SetValue("ui_progress", 100);
517 if (simulate) {
518 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
519 if (simulate_fail != 0)
520 DataManager::SetValue("tw_operation_status", 1);
521 else
522 DataManager::SetValue("tw_operation_status", 0);
523 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500524 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400525 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500526 }
527 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400528 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500529 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400530 }
531 DataManager::SetValue("tw_operation_state", 1);
532 DataManager::SetValue(TW_ACTION_BUSY, 0);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500533 blankTimer.resetTimerAndUnblank();
LuK133762326f42015-09-30 19:57:46 +0200534 property_set("twrp.action_complete", "1");
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000535 time(&Stop);
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400536
537#ifndef TW_NO_HAPTICS
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000538 if ((int) difftime(Stop, Start) > 10)
Ethan Yonker03db3262014-02-05 08:02:06 -0600539 DataManager::Vibrate("tw_action_vibrate");
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400540#endif
541
that3f7b1ac2014-12-30 11:30:13 +0100542 LOGINFO("operation_end - status=%d\n", operation_status);
Dees_Troy51a0e822012-09-05 15:24:24 -0400543}
544
that3f7b1ac2014-12-30 11:30:13 +0100545int GUIAction::reboot(std::string arg)
Dees_Troy51a0e822012-09-05 15:24:24 -0400546{
that3f7b1ac2014-12-30 11:30:13 +0100547 sync();
548 DataManager::SetValue("tw_gui_done", 1);
549 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400550
that3f7b1ac2014-12-30 11:30:13 +0100551 return 0;
552}
Dees_Troy51a0e822012-09-05 15:24:24 -0400553
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500554int GUIAction::home(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100555{
that3f7b1ac2014-12-30 11:30:13 +0100556 gui_changePage("main");
557 return 0;
558}
Dees_Troy51a0e822012-09-05 15:24:24 -0400559
that3f7b1ac2014-12-30 11:30:13 +0100560int GUIAction::key(std::string arg)
561{
562 const int key = getKeyByName(arg);
563 PageManager::NotifyKey(key, true);
564 PageManager::NotifyKey(key, false);
565 return 0;
566}
567
568int GUIAction::page(std::string arg)
569{
LuK133762326f42015-09-30 19:57:46 +0200570 property_set("twrp.action_complete", "0");
that3f7b1ac2014-12-30 11:30:13 +0100571 std::string page_name = gui_parse_text(arg);
572 return gui_changePage(page_name);
573}
574
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500575int GUIAction::reload(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100576{
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500577 PageManager::RequestReload();
578 // The actual reload is handled in pages.cpp in PageManager::RunReload()
579 // The reload will occur on the next Update or Render call and will
580 // be performed in the rendoer thread instead of the action thread
581 // to prevent crashing which could occur when we start deleting
582 // GUI resources in the action thread while we attempt to render
583 // with those same resources in another thread.
that3f7b1ac2014-12-30 11:30:13 +0100584 return 0;
585}
Dees_Troy51a0e822012-09-05 15:24:24 -0400586
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500587int GUIAction::readBackup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100588{
589 string Restore_Name;
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -0400590
that3f7b1ac2014-12-30 11:30:13 +0100591 DataManager::GetValue("tw_restore", Restore_Name);
592 PartitionManager.Set_Restore_Files(Restore_Name);
593 return 0;
594}
595
596int GUIAction::set(std::string arg)
597{
598 if (arg.find('=') != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200599 {
that3f7b1ac2014-12-30 11:30:13 +0100600 string varName = arg.substr(0, arg.find('='));
601 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400602
that3f7b1ac2014-12-30 11:30:13 +0100603 DataManager::GetValue(value, value);
604 DataManager::SetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200605 }
that3f7b1ac2014-12-30 11:30:13 +0100606 else
607 DataManager::SetValue(arg, "1");
608 return 0;
609}
Dees_Troy51a0e822012-09-05 15:24:24 -0400610
that3f7b1ac2014-12-30 11:30:13 +0100611int GUIAction::clear(std::string arg)
612{
613 DataManager::SetValue(arg, "0");
614 return 0;
615}
Dees_Troy51a0e822012-09-05 15:24:24 -0400616
that3f7b1ac2014-12-30 11:30:13 +0100617int GUIAction::mount(std::string arg)
618{
619 if (arg == "usb") {
620 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400621 if (!simulate)
that3f7b1ac2014-12-30 11:30:13 +0100622 PartitionManager.usb_storage_enable();
623 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500624 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100625 } else if (!simulate) {
626 PartitionManager.Mount_By_Path(arg, true);
627 PartitionManager.Add_MTP_Storage(arg);
628 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500629 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100630 return 0;
631}
632
633int GUIAction::unmount(std::string arg)
634{
635 if (arg == "usb") {
636 if (!simulate)
637 PartitionManager.usb_storage_disable();
638 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500639 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100640 DataManager::SetValue(TW_ACTION_BUSY, 0);
641 } else if (!simulate) {
642 PartitionManager.UnMount_By_Path(arg, true);
643 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500644 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100645 return 0;
646}
647
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500648int GUIAction::restoredefaultsettings(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100649{
650 operation_start("Restore Defaults");
651 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
Ethan Yonker74db1572015-10-28 12:44:49 -0500652 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100653 else {
654 DataManager::ResetDefaults();
655 PartitionManager.Update_System_Details();
656 PartitionManager.Mount_Current_Storage(true);
657 }
658 operation_end(0);
659 return 0;
660}
661
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500662int GUIAction::copylog(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100663{
664 operation_start("Copy Log");
665 if (!simulate)
666 {
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400667 string dst, curr_storage;
668 int copy_kernel_log = 0;
669
670 DataManager::GetValue("tw_include_kernel_log", copy_kernel_log);
that3f7b1ac2014-12-30 11:30:13 +0100671 PartitionManager.Mount_Current_Storage(true);
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400672 curr_storage = DataManager::GetCurrentStoragePath();
673 dst = curr_storage + "/recovery.log";
that3f7b1ac2014-12-30 11:30:13 +0100674 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
675 tw_set_default_metadata(dst.c_str());
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400676 if (copy_kernel_log)
677 TWFunc::copy_kernel_log(curr_storage);
that3f7b1ac2014-12-30 11:30:13 +0100678 sync();
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400679 gui_msg(Msg("copy_log=Copied recovery log to {1}")(dst));
that3f7b1ac2014-12-30 11:30:13 +0100680 } else
681 simulate_progress_bar();
682 operation_end(0);
683 return 0;
684}
685
686
687int GUIAction::compute(std::string arg)
688{
689 if (arg.find("+") != string::npos)
690 {
691 string varName = arg.substr(0, arg.find('+'));
692 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
693 int amount_to_add = atoi(string_to_add.c_str());
694 int value;
695
696 DataManager::GetValue(varName, value);
697 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400698 return 0;
699 }
that3f7b1ac2014-12-30 11:30:13 +0100700 if (arg.find("-") != string::npos)
Dees_Troy51a0e822012-09-05 15:24:24 -0400701 {
that3f7b1ac2014-12-30 11:30:13 +0100702 string varName = arg.substr(0, arg.find('-'));
703 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
704 int amount_to_subtract = atoi(string_to_subtract.c_str());
705 int value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400706
that3f7b1ac2014-12-30 11:30:13 +0100707 DataManager::GetValue(varName, value);
708 value -= amount_to_subtract;
709 if (value <= 0)
710 value = 0;
711 DataManager::SetValue(varName, value);
712 return 0;
713 }
714 if (arg.find("*") != string::npos)
715 {
716 string varName = arg.substr(0, arg.find('*'));
717 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
718 int multiply_by = atoi(multiply_by_str.c_str());
719 int value;
720
721 DataManager::GetValue(varName, value);
722 DataManager::SetValue(varName, value*multiply_by);
723 return 0;
724 }
725 if (arg.find("/") != string::npos)
726 {
727 string varName = arg.substr(0, arg.find('/'));
728 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
729 int divide_by = atoi(divide_by_str.c_str());
730 int value;
731
Matt Mowera8a89d12016-12-30 18:10:37 -0600732 if (divide_by != 0)
that3f7b1ac2014-12-30 11:30:13 +0100733 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400734 DataManager::GetValue(varName, value);
that3f7b1ac2014-12-30 11:30:13 +0100735 DataManager::SetValue(varName, value/divide_by);
Dees_Troy51a0e822012-09-05 15:24:24 -0400736 }
737 return 0;
738 }
that3f7b1ac2014-12-30 11:30:13 +0100739 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
740 return -1;
741}
Dees_Troy51a0e822012-09-05 15:24:24 -0400742
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500743int GUIAction::setguitimezone(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100744{
745 string SelectedZone;
746 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
747 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
748 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
749
750 int dst;
751 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
752
753 string offset;
754 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
755
756 string NewTimeZone = Zone;
757 if (offset != "0")
758 NewTimeZone += ":" + offset;
759
760 if (dst != 0)
761 NewTimeZone += DSTZone;
762
763 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
764 DataManager::update_tz_environment_variables();
765 return 0;
766}
767
768int GUIAction::overlay(std::string arg)
769{
770 return gui_changeOverlay(arg);
771}
772
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500773int GUIAction::queuezip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100774{
775 if (zip_queue_index >= 10) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500776 gui_msg("max_queue=Maximum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400777 return 0;
778 }
that3f7b1ac2014-12-30 11:30:13 +0100779 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
780 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
781 zip_queue_index++;
Dees_Troy51a0e822012-09-05 15:24:24 -0400782 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +0100783 }
784 return 0;
785}
786
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500787int GUIAction::cancelzip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100788{
789 if (zip_queue_index <= 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500790 gui_msg("min_queue=Minimum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400791 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100792 } else {
793 zip_queue_index--;
794 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
Dees_Troy51a0e822012-09-05 15:24:24 -0400795 }
that3f7b1ac2014-12-30 11:30:13 +0100796 return 0;
797}
Dees_Troy51a0e822012-09-05 15:24:24 -0400798
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500799int GUIAction::queueclear(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100800{
801 zip_queue_index = 0;
802 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
803 return 0;
804}
Dees_Troy51a0e822012-09-05 15:24:24 -0400805
that3f7b1ac2014-12-30 11:30:13 +0100806int GUIAction::sleep(std::string arg)
807{
808 operation_start("Sleep");
809 usleep(atoi(arg.c_str()));
810 operation_end(0);
811 return 0;
812}
Dees Troyb21cc642013-09-10 17:36:41 +0000813
Matt Mower9a2a2052016-05-31 21:31:22 -0500814int GUIAction::sleepcounter(std::string arg)
815{
816 operation_start("SleepCounter");
817 // Ensure user notices countdown in case it needs to be cancelled
818 blankTimer.resetTimerAndUnblank();
819 int total = atoi(arg.c_str());
820 for (int t = total; t > 0; t--) {
821 int progress = (int)(((float)(total-t)/(float)total)*100.0);
822 DataManager::SetValue("ui_progress", progress);
823 ::sleep(1);
824 DataManager::SetValue("tw_sleep", t-1);
825 }
826 DataManager::SetValue("ui_progress", 100);
827 operation_end(0);
828 return 0;
829}
830
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500831int GUIAction::appenddatetobackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100832{
833 operation_start("AppendDateToBackupName");
834 string Backup_Name;
835 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
836 Backup_Name += TWFunc::Get_Current_Date();
837 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
838 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
839 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
Matt Mower76b8afc2016-06-24 12:04:27 -0500840 PageManager::NotifyKey(KEY_END, true);
841 PageManager::NotifyKey(KEY_END, false);
that3f7b1ac2014-12-30 11:30:13 +0100842 operation_end(0);
843 return 0;
844}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500845
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500846int GUIAction::generatebackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100847{
848 operation_start("GenerateBackupName");
849 TWFunc::Auto_Generate_Backup_Name();
850 operation_end(0);
851 return 0;
852}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500853
Ethan Yonker483e9f42016-01-11 22:21:18 -0600854int GUIAction::checkpartitionlist(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100855{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600856 string List, part_path;
that3f7b1ac2014-12-30 11:30:13 +0100857 int count = 0;
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500858
Ethan Yonker483e9f42016-01-11 22:21:18 -0600859 if (arg.empty())
860 arg = "tw_wipe_list";
861 DataManager::GetValue(arg, List);
862 LOGINFO("checkpartitionlist list '%s'\n", List.c_str());
863 if (!List.empty()) {
864 size_t start_pos = 0, end_pos = List.find(";", start_pos);
865 while (end_pos != string::npos && start_pos < List.size()) {
866 part_path = List.substr(start_pos, end_pos - start_pos);
867 LOGINFO("checkpartitionlist part_path '%s'\n", part_path.c_str());
868 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100869 // Do nothing
Dees_Troy51a0e822012-09-05 15:24:24 -0400870 } else {
that3f7b1ac2014-12-30 11:30:13 +0100871 count++;
872 }
873 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600874 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100875 }
876 DataManager::SetValue("tw_check_partition_list", count);
877 } else {
878 DataManager::SetValue("tw_check_partition_list", 0);
879 }
Matt Mowera8a89d12016-12-30 18:10:37 -0600880 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100881}
Dees_Troy51a0e822012-09-05 15:24:24 -0400882
Ethan Yonker483e9f42016-01-11 22:21:18 -0600883int GUIAction::getpartitiondetails(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100884{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600885 string List, part_path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400886
Ethan Yonker483e9f42016-01-11 22:21:18 -0600887 if (arg.empty())
888 arg = "tw_wipe_list";
889 DataManager::GetValue(arg, List);
890 LOGINFO("getpartitiondetails list '%s'\n", List.c_str());
891 if (!List.empty()) {
892 size_t start_pos = 0, end_pos = List.find(";", start_pos);
893 part_path = List;
894 while (end_pos != string::npos && start_pos < List.size()) {
895 part_path = List.substr(start_pos, end_pos - start_pos);
896 LOGINFO("getpartitiondetails part_path '%s'\n", part_path.c_str());
897 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100898 // Do nothing
899 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600900 DataManager::SetValue("tw_partition_path", part_path);
that3f7b1ac2014-12-30 11:30:13 +0100901 break;
902 }
903 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600904 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100905 }
Ethan Yonker483e9f42016-01-11 22:21:18 -0600906 if (!part_path.empty()) {
907 TWPartition* Part = PartitionManager.Find_Partition_By_Path(part_path);
that3f7b1ac2014-12-30 11:30:13 +0100908 if (Part) {
909 unsigned long long mb = 1048576;
Dees_Troya13d74f2013-03-24 08:54:55 -0500910
that3f7b1ac2014-12-30 11:30:13 +0100911 DataManager::SetValue("tw_partition_name", Part->Display_Name);
912 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
913 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
914 DataManager::SetValue("tw_partition_size", Part->Size / mb);
915 DataManager::SetValue("tw_partition_used", Part->Used / mb);
916 DataManager::SetValue("tw_partition_free", Part->Free / mb);
917 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
918 DataManager::SetValue("tw_partition_removable", Part->Removable);
919 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
920
921 if (Part->Can_Repair())
922 DataManager::SetValue("tw_partition_can_repair", 1);
923 else
924 DataManager::SetValue("tw_partition_can_repair", 0);
Ethan Yonkera2719152015-05-28 09:44:41 -0500925 if (Part->Can_Resize())
926 DataManager::SetValue("tw_partition_can_resize", 1);
927 else
928 DataManager::SetValue("tw_partition_can_resize", 0);
Matt Mower18794c82015-11-11 16:22:45 -0600929 if (TWFunc::Path_Exists("/sbin/mkfs.fat"))
that3f7b1ac2014-12-30 11:30:13 +0100930 DataManager::SetValue("tw_partition_vfat", 1);
931 else
932 DataManager::SetValue("tw_partition_vfat", 0);
Matt Mower80f7b362015-12-12 15:38:01 -0600933 if (TWFunc::Path_Exists("/sbin/mkexfatfs"))
that3f7b1ac2014-12-30 11:30:13 +0100934 DataManager::SetValue("tw_partition_exfat", 1);
935 else
936 DataManager::SetValue("tw_partition_exfat", 0);
937 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs"))
938 DataManager::SetValue("tw_partition_f2fs", 1);
939 else
940 DataManager::SetValue("tw_partition_f2fs", 0);
941 if (TWFunc::Path_Exists("/sbin/mke2fs"))
942 DataManager::SetValue("tw_partition_ext", 1);
943 else
944 DataManager::SetValue("tw_partition_ext", 0);
945 return 0;
946 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600947 LOGERR("Unable to locate partition: '%s'\n", part_path.c_str());
that3f7b1ac2014-12-30 11:30:13 +0100948 }
949 }
950 }
951 DataManager::SetValue("tw_partition_name", "");
952 DataManager::SetValue("tw_partition_file_system", "");
Ethan Yonker483e9f42016-01-11 22:21:18 -0600953 // Set this to 0 to prevent trying to partition this device, just in case
954 DataManager::SetValue("tw_partition_removable", 0);
that3f7b1ac2014-12-30 11:30:13 +0100955 return 0;
956}
957
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500958int GUIAction::screenshot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100959{
960 time_t tm;
961 char path[256];
962 int path_len;
Matt Mower0c88b842017-01-15 16:00:49 -0600963 uid_t uid = AID_MEDIA_RW;
964 gid_t gid = AID_MEDIA_RW;
that3f7b1ac2014-12-30 11:30:13 +0100965
966 const std::string storage = DataManager::GetCurrentStoragePath();
Matt Mowera8a89d12016-12-30 18:10:37 -0600967 if (PartitionManager.Is_Mounted_By_Path(storage)) {
that3f7b1ac2014-12-30 11:30:13 +0100968 snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
969 } else {
970 strcpy(path, "/tmp/");
971 }
972
Matt Mowera8a89d12016-12-30 18:10:37 -0600973 if (!TWFunc::Create_Dir_Recursive(path, 0775, uid, gid))
that3f7b1ac2014-12-30 11:30:13 +0100974 return 0;
975
976 tm = time(NULL);
977 path_len = strlen(path);
978
979 // Screenshot_2014-01-01-18-21-38.png
980 strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
981
982 int res = gr_save_screenshot(path);
Matt Mowera8a89d12016-12-30 18:10:37 -0600983 if (res == 0) {
that3f7b1ac2014-12-30 11:30:13 +0100984 chmod(path, 0666);
985 chown(path, uid, gid);
986
that677b13f2015-12-26 23:57:31 +0100987 gui_msg(Msg("screenshot_saved=Screenshot was saved to {1}")(path));
that3f7b1ac2014-12-30 11:30:13 +0100988
VDavid0032034a412019-10-18 22:43:20 +0200989 // blink to notify that the screenshot was taken
that3f7b1ac2014-12-30 11:30:13 +0100990 gr_color(255, 255, 255, 255);
991 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
992 gr_flip();
993 gui_forceRender();
994 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500995 gui_err("screenshot_err=Failed to take a screenshot!");
that3f7b1ac2014-12-30 11:30:13 +0100996 }
997 return 0;
998}
999
1000int GUIAction::setbrightness(std::string arg)
1001{
1002 return TWFunc::Set_Brightness(arg);
1003}
1004
1005int GUIAction::fileexists(std::string arg)
1006{
1007 struct stat st;
1008 string newpath = arg + "/.";
1009
1010 operation_start("FileExists");
1011 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
1012 operation_end(0);
1013 else
1014 operation_end(1);
1015 return 0;
1016}
1017
thatcc8ddca2015-01-03 01:59:36 +01001018void GUIAction::reinject_after_flash()
1019{
1020 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001021 gui_msg("injecttwrp=Injecting TWRP into boot image...");
thatcc8ddca2015-01-03 01:59:36 +01001022 if (simulate) {
1023 simulate_progress_bar();
1024 } else {
1025 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
1026 if (Boot == NULL || Boot->Current_File_System != "emmc")
1027 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
1028 else {
1029 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
1030 TWFunc::Exec_Cmd(injectcmd);
1031 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001032 gui_msg("done=Done.");
thatcc8ddca2015-01-03 01:59:36 +01001033 }
1034 }
1035}
1036
mauronofrio0ff59842019-10-26 19:47:55 +02001037int GUIAction::ozip_decrypt(string zip_path)
1038{
1039 if (!TWFunc::Path_Exists("/sbin/ozip_decrypt")) {
1040 return 1;
1041 }
1042 gui_msg("ozip_decrypt_decryption=Starting Ozip Decryption...");
1043 TWFunc::Exec_Cmd("ozip_decrypt " + (string)TW_OZIP_DECRYPT_KEY + " '" + zip_path + "'");
1044 gui_msg("ozip_decrypt_finish=Ozip Decryption Finished!");
1045 return 0;
1046}
1047
that3f7b1ac2014-12-30 11:30:13 +01001048int GUIAction::flash(std::string arg)
1049{
1050 int i, ret_val = 0, wipe_cache = 0;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001051 // We're going to jump to this page first, like a loading page
1052 gui_changePage(arg);
that3f7b1ac2014-12-30 11:30:13 +01001053 for (i=0; i<zip_queue_index; i++) {
thatc7b631b2015-06-01 23:36:57 +02001054 string zip_path = zip_queue[i];
1055 size_t slashpos = zip_path.find_last_of('/');
1056 string zip_filename = (slashpos == string::npos) ? zip_path : zip_path.substr(slashpos + 1);
that3f7b1ac2014-12-30 11:30:13 +01001057 operation_start("Flashing");
mauronofrio0ff59842019-10-26 19:47:55 +02001058 if((zip_path.substr(zip_path.size() - 4, 4)) == "ozip")
1059 {
1060 if((ozip_decrypt(zip_path)) != 0)
1061 {
1062 LOGERR("Unable to find ozip_decrypt!");
1063 break;
1064 }
1065 zip_filename = (zip_filename.substr(0, zip_filename.size() - 4)).append("zip");
1066 zip_path = (zip_path.substr(0, zip_path.size() - 4)).append("zip");
1067 if (!TWFunc::Path_Exists(zip_path)) {
1068 LOGERR("Unable to find decrypted zip");
1069 break;
1070 }
1071 }
thatc7b631b2015-06-01 23:36:57 +02001072 DataManager::SetValue("tw_filename", zip_path);
1073 DataManager::SetValue("tw_file", zip_filename);
that3f7b1ac2014-12-30 11:30:13 +01001074 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
1075
1076 TWFunc::SetPerformanceMode(true);
thatc7b631b2015-06-01 23:36:57 +02001077 ret_val = flash_zip(zip_path, &wipe_cache);
that3f7b1ac2014-12-30 11:30:13 +01001078 TWFunc::SetPerformanceMode(false);
1079 if (ret_val != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001080 gui_msg(Msg(msg::kError, "zip_err=Error installing zip file '{1}'")(zip_path));
that3f7b1ac2014-12-30 11:30:13 +01001081 ret_val = 1;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001082 break;
that3f7b1ac2014-12-30 11:30:13 +01001083 }
1084 }
1085 zip_queue_index = 0;
that3f7b1ac2014-12-30 11:30:13 +01001086
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001087 if (wipe_cache) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001088 gui_msg("zip_wipe_cache=One or more zip requested a cache wipe -- Wiping cache now.");
that3f7b1ac2014-12-30 11:30:13 +01001089 PartitionManager.Wipe_By_Path("/cache");
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001090 }
that3f7b1ac2014-12-30 11:30:13 +01001091
thatcc8ddca2015-01-03 01:59:36 +01001092 reinject_after_flash();
that3f7b1ac2014-12-30 11:30:13 +01001093 PartitionManager.Update_System_Details();
1094 operation_end(ret_val);
bigbiffa869fc72016-03-01 19:40:36 -05001095 // This needs to be after the operation_end call so we change pages before we change variables that we display on the screen
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001096 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +01001097 return 0;
1098}
1099
1100int GUIAction::wipe(std::string arg)
1101{
1102 operation_start("Format");
1103 DataManager::SetValue("tw_partition", arg);
that3f7b1ac2014-12-30 11:30:13 +01001104 int ret_val = false;
1105
1106 if (simulate) {
1107 simulate_progress_bar();
1108 } else {
1109 if (arg == "data")
1110 ret_val = PartitionManager.Factory_Reset();
1111 else if (arg == "battery")
1112 ret_val = PartitionManager.Wipe_Battery_Stats();
1113 else if (arg == "rotate")
1114 ret_val = PartitionManager.Wipe_Rotate_Data();
1115 else if (arg == "dalvik")
1116 ret_val = PartitionManager.Wipe_Dalvik_Cache();
1117 else if (arg == "DATAMEDIA") {
1118 ret_val = PartitionManager.Format_Data();
1119 } else if (arg == "INTERNAL") {
Matt Mower23d8aae2017-01-06 14:30:33 -06001120 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001121
1122 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1123 if (has_datamedia) {
1124 ret_val = PartitionManager.Wipe_Media_From_Data();
1125 } else {
1126 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
1127 }
1128 } else if (arg == "EXTERNAL") {
1129 string External_Path;
1130
1131 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
1132 ret_val = PartitionManager.Wipe_By_Path(External_Path);
1133 } else if (arg == "ANDROIDSECURE") {
1134 ret_val = PartitionManager.Wipe_Android_Secure();
1135 } else if (arg == "LIST") {
1136 string Wipe_List, wipe_path;
1137 bool skip = false;
1138 ret_val = true;
that3f7b1ac2014-12-30 11:30:13 +01001139
1140 DataManager::GetValue("tw_wipe_list", Wipe_List);
1141 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
1142 if (!Wipe_List.empty()) {
1143 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
1144 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
1145 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
1146 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
1147 if (wipe_path == "/and-sec") {
1148 if (!PartitionManager.Wipe_Android_Secure()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001149 gui_msg("and_sec_wipe_err=Unable to wipe android secure");
that3f7b1ac2014-12-30 11:30:13 +01001150 ret_val = false;
1151 break;
1152 } else {
1153 skip = true;
1154 }
1155 } else if (wipe_path == "DALVIK") {
1156 if (!PartitionManager.Wipe_Dalvik_Cache()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001157 gui_err("dalvik_wipe_err=Failed to wipe dalvik");
that3f7b1ac2014-12-30 11:30:13 +01001158 ret_val = false;
1159 break;
1160 } else {
1161 skip = true;
1162 }
1163 } else if (wipe_path == "INTERNAL") {
1164 if (!PartitionManager.Wipe_Media_From_Data()) {
1165 ret_val = false;
1166 break;
1167 } else {
1168 skip = true;
Dees_Troya13d74f2013-03-24 08:54:55 -05001169 }
1170 }
that3f7b1ac2014-12-30 11:30:13 +01001171 if (!skip) {
1172 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001173 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(wipe_path));
that3f7b1ac2014-12-30 11:30:13 +01001174 ret_val = false;
1175 break;
1176 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
1177 arg = wipe_path;
1178 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001179 } else {
that3f7b1ac2014-12-30 11:30:13 +01001180 skip = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001181 }
that3f7b1ac2014-12-30 11:30:13 +01001182 start_pos = end_pos + 1;
1183 end_pos = Wipe_List.find(";", start_pos);
Dees_Troy51a0e822012-09-05 15:24:24 -04001184 }
1185 }
that3f7b1ac2014-12-30 11:30:13 +01001186 } else
1187 ret_val = PartitionManager.Wipe_By_Path(arg);
that73a52952015-01-28 23:07:34 +01001188#ifndef TW_OEM_BUILD
that3f7b1ac2014-12-30 11:30:13 +01001189 if (arg == DataManager::GetSettingsStoragePath()) {
1190 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
1191 string Storage_Path = DataManager::GetSettingsStoragePath();
Dees_Troy51a0e822012-09-05 15:24:24 -04001192
that3f7b1ac2014-12-30 11:30:13 +01001193 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
1194 LOGINFO("Making TWRP folder and saving settings.\n");
1195 Storage_Path += "/TWRP";
1196 mkdir(Storage_Path.c_str(), 0777);
1197 DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001198 } else {
that3f7b1ac2014-12-30 11:30:13 +01001199 LOGERR("Unable to recreate TWRP folder and save settings.\n");
1200 }
1201 }
1202#endif
1203 }
1204 PartitionManager.Update_System_Details();
1205 if (ret_val)
1206 ret_val = 0; // 0 is success
1207 else
1208 ret_val = 1; // 1 is failure
1209 operation_end(ret_val);
1210 return 0;
1211}
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001212
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001213int GUIAction::refreshsizes(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001214{
1215 operation_start("Refreshing Sizes");
1216 if (simulate) {
1217 simulate_progress_bar();
1218 } else
1219 PartitionManager.Update_System_Details();
1220 operation_end(0);
1221 return 0;
1222}
1223
1224int GUIAction::nandroid(std::string arg)
1225{
that3f7b1ac2014-12-30 11:30:13 +01001226 if (simulate) {
that73a52952015-01-28 23:07:34 +01001227 PartitionManager.stop_backup.set_value(0);
that3f7b1ac2014-12-30 11:30:13 +01001228 DataManager::SetValue("tw_partition", "Simulation");
1229 simulate_progress_bar();
that73a52952015-01-28 23:07:34 +01001230 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001231 } else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001232 operation_start("Nandroid");
1233 int ret = 0;
1234
that3f7b1ac2014-12-30 11:30:13 +01001235 if (arg == "backup") {
1236 string Backup_Name;
1237 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Ethan Yonker4adc33e2016-01-22 16:07:11 -06001238 string auto_gen = gui_lookup("auto_generate", "(Auto Generate)");
Ethan Yonker53796e72019-01-11 22:49:52 -06001239 if (Backup_Name == auto_gen || Backup_Name == gui_lookup("curr_date", "(Current Date)") || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(Backup_Name, true, true) == 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05001240 ret = PartitionManager.Run_Backup(false);
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001241 DataManager::SetValue("tw_encrypt_backup", 0); // reset value so we don't encrypt every subsequent backup
1242 if (!PartitionManager.stop_backup.get_value()) {
1243 if (ret == false)
1244 ret = 1; // 1 for failure
1245 else
1246 ret = 0; // 0 for success
1247 DataManager::SetValue("tw_cancel_backup", 0);
1248 } else {
1249 DataManager::SetValue("tw_cancel_backup", 1);
1250 gui_msg("backup_cancel=Backup Cancelled");
1251 ret = 0;
1252 }
bigbiffce8f83c2015-12-12 18:30:21 -05001253 } else {
that3f7b1ac2014-12-30 11:30:13 +01001254 operation_end(1);
1255 return -1;
that3f7b1ac2014-12-30 11:30:13 +01001256 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001257 DataManager::SetValue(TW_BACKUP_NAME, auto_gen);
that3f7b1ac2014-12-30 11:30:13 +01001258 } else if (arg == "restore") {
1259 string Restore_Name;
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001260 int gui_adb_backup;
1261
that3f7b1ac2014-12-30 11:30:13 +01001262 DataManager::GetValue("tw_restore", Restore_Name);
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001263 DataManager::GetValue("tw_enable_adb_backup", gui_adb_backup);
1264 if (gui_adb_backup) {
1265 DataManager::SetValue("tw_operation_state", 1);
1266 if (TWFunc::stream_adb_backup(Restore_Name) == 0)
1267 ret = 0; // success
1268 else
1269 ret = 1; // failure
1270 DataManager::SetValue("tw_enable_adb_backup", 0);
1271 ret = 0; // assume success???
1272 } else {
1273 if (PartitionManager.Run_Restore(Restore_Name))
1274 ret = 0; // success
1275 else
1276 ret = 1; // failure
1277 }
that3f7b1ac2014-12-30 11:30:13 +01001278 } else {
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001279 operation_end(1); // invalid arg specified, fail
bigbiff7abc5fe2015-01-17 16:53:12 -05001280 return -1;
1281 }
that73a52952015-01-28 23:07:34 +01001282 operation_end(ret);
bigbiff7abc5fe2015-01-17 16:53:12 -05001283 return ret;
1284 }
1285 return 0;
1286}
1287
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001288int GUIAction::cancelbackup(std::string arg __unused) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001289 if (simulate) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001290 PartitionManager.stop_backup.set_value(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001291 }
1292 else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001293 int op_status = PartitionManager.Cancel_Backup();
1294 if (op_status != 0)
1295 op_status = 1; // failure
bigbiff7abc5fe2015-01-17 16:53:12 -05001296 }
1297
1298 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001299}
Dees_Troy51a0e822012-09-05 15:24:24 -04001300
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001301int GUIAction::fixcontexts(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001302{
that73a52952015-01-28 23:07:34 +01001303 int op_status = 0;
1304
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001305 operation_start("Fix Contexts");
1306 LOGINFO("fix contexts started!\n");
that3f7b1ac2014-12-30 11:30:13 +01001307 if (simulate) {
1308 simulate_progress_bar();
1309 } else {
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001310 op_status = PartitionManager.Fix_Contexts();
that3f7b1ac2014-12-30 11:30:13 +01001311 if (op_status != 0)
1312 op_status = 1; // failure
that3f7b1ac2014-12-30 11:30:13 +01001313 }
that73a52952015-01-28 23:07:34 +01001314 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001315 return 0;
1316}
1317
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001318int GUIAction::fixpermissions(std::string arg)
1319{
1320 return fixcontexts(arg);
1321}
1322
that3f7b1ac2014-12-30 11:30:13 +01001323int GUIAction::dd(std::string arg)
1324{
1325 operation_start("imaging");
1326
1327 if (simulate) {
1328 simulate_progress_bar();
1329 } else {
1330 string cmd = "dd " + arg;
1331 TWFunc::Exec_Cmd(cmd);
1332 }
1333 operation_end(0);
1334 return 0;
1335}
1336
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001337int GUIAction::partitionsd(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001338{
1339 operation_start("Partition SD Card");
1340 int ret_val = 0;
1341
1342 if (simulate) {
1343 simulate_progress_bar();
1344 } else {
1345 int allow_partition;
1346 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1347 if (allow_partition == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001348 gui_err("no_real_sdcard=This device does not have a real SD Card! Aborting!");
that3f7b1ac2014-12-30 11:30:13 +01001349 } else {
1350 if (!PartitionManager.Partition_SDCard())
1351 ret_val = 1; // failed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001352 }
that3f7b1ac2014-12-30 11:30:13 +01001353 }
1354 operation_end(ret_val);
1355 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001356
that3f7b1ac2014-12-30 11:30:13 +01001357}
Dees_Troy51a0e822012-09-05 15:24:24 -04001358
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001359int GUIAction::installhtcdumlock(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001360{
1361 operation_start("Install HTC Dumlock");
1362 if (simulate) {
1363 simulate_progress_bar();
1364 } else
1365 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -04001366
that3f7b1ac2014-12-30 11:30:13 +01001367 operation_end(0);
1368 return 0;
1369}
Dees_Troy51a0e822012-09-05 15:24:24 -04001370
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001371int GUIAction::htcdumlockrestoreboot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001372{
1373 operation_start("HTC Dumlock Restore Boot");
1374 if (simulate) {
1375 simulate_progress_bar();
1376 } else
1377 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001378
that3f7b1ac2014-12-30 11:30:13 +01001379 operation_end(0);
1380 return 0;
1381}
Dees_Troy51a0e822012-09-05 15:24:24 -04001382
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001383int GUIAction::htcdumlockreflashrecovery(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001384{
1385 operation_start("HTC Dumlock Reflash Recovery");
1386 if (simulate) {
1387 simulate_progress_bar();
1388 } else
1389 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001390
that3f7b1ac2014-12-30 11:30:13 +01001391 operation_end(0);
1392 return 0;
1393}
Dees_Troy51a0e822012-09-05 15:24:24 -04001394
that3f7b1ac2014-12-30 11:30:13 +01001395int GUIAction::cmd(std::string arg)
1396{
1397 int op_status = 0;
1398
1399 operation_start("Command");
1400 LOGINFO("Running command: '%s'\n", arg.c_str());
1401 if (simulate) {
1402 simulate_progress_bar();
1403 } else {
1404 op_status = TWFunc::Exec_Cmd(arg);
1405 if (op_status != 0)
1406 op_status = 1;
1407 }
1408
1409 operation_end(op_status);
1410 return 0;
1411}
1412
1413int GUIAction::terminalcommand(std::string arg)
1414{
1415 int op_status = 0;
1416 string cmdpath, command;
1417
1418 DataManager::GetValue("tw_terminal_location", cmdpath);
1419 operation_start("CommandOutput");
1420 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
1421 if (simulate) {
1422 simulate_progress_bar();
1423 operation_end(op_status);
Matt Mower5aa29ab2015-02-25 23:50:55 -06001424 } else if (arg == "exit") {
1425 LOGINFO("Exiting terminal\n");
1426 operation_end(op_status);
1427 page("main");
that3f7b1ac2014-12-30 11:30:13 +01001428 } else {
1429 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
1430 LOGINFO("Actual command is: '%s'\n", command.c_str());
that3f7b1ac2014-12-30 11:30:13 +01001431 DataManager::SetValue("tw_terminal_state", 1);
1432 DataManager::SetValue("tw_background_thread_running", 1);
thatc6085482015-01-09 22:12:43 +01001433 FILE* fp;
1434 char line[512];
1435
1436 fp = popen(command.c_str(), "r");
1437 if (fp == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001438 LOGERR("Error opening command to run (%s).\n", strerror(errno));
thatc6085482015-01-09 22:12:43 +01001439 } else {
Matt Mower23d8aae2017-01-06 14:30:33 -06001440 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1;
thatc6085482015-01-09 22:12:43 +01001441 struct timeval timeout;
1442 fd_set fdset;
1443
Matt Mowera8a89d12016-12-30 18:10:37 -06001444 while (keep_going)
thatc6085482015-01-09 22:12:43 +01001445 {
1446 FD_ZERO(&fdset);
1447 FD_SET(fd, &fdset);
1448 timeout.tv_sec = 0;
1449 timeout.tv_usec = 400000;
1450 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1451 if (has_data == 0) {
1452 // Timeout reached
1453 DataManager::GetValue("tw_terminal_state", check);
1454 if (check == 0) {
1455 keep_going = 0;
1456 }
1457 } else if (has_data < 0) {
1458 // End of execution
1459 keep_going = 0;
1460 } else {
1461 // Try to read output
Matt Mowera8a89d12016-12-30 18:10:37 -06001462 if (fgets(line, sizeof(line), fp) != NULL)
thatc6085482015-01-09 22:12:43 +01001463 gui_print("%s", line); // Display output
1464 else
1465 keep_going = 0; // Done executing
1466 }
1467 }
1468 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -04001469 }
thatc6085482015-01-09 22:12:43 +01001470 DataManager::SetValue("tw_operation_status", 0);
1471 DataManager::SetValue("tw_operation_state", 1);
1472 DataManager::SetValue("tw_terminal_state", 0);
1473 DataManager::SetValue("tw_background_thread_running", 0);
1474 DataManager::SetValue(TW_ACTION_BUSY, 0);
that3f7b1ac2014-12-30 11:30:13 +01001475 }
1476 return 0;
1477}
1478
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001479int GUIAction::killterminal(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001480{
that3f7b1ac2014-12-30 11:30:13 +01001481 LOGINFO("Sending kill command...\n");
1482 operation_start("KillCommand");
1483 DataManager::SetValue("tw_operation_status", 0);
1484 DataManager::SetValue("tw_operation_state", 1);
1485 DataManager::SetValue("tw_terminal_state", 0);
1486 DataManager::SetValue("tw_background_thread_running", 0);
1487 DataManager::SetValue(TW_ACTION_BUSY, 0);
1488 return 0;
1489}
1490
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001491int GUIAction::reinjecttwrp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001492{
1493 int op_status = 0;
1494 operation_start("ReinjectTWRP");
Ethan Yonker74db1572015-10-28 12:44:49 -05001495 gui_msg("injecttwrp=Injecting TWRP into boot image...");
that3f7b1ac2014-12-30 11:30:13 +01001496 if (simulate) {
1497 simulate_progress_bar();
1498 } else {
1499 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Ethan Yonker74db1572015-10-28 12:44:49 -05001500 gui_msg("done=Done.");
that3f7b1ac2014-12-30 11:30:13 +01001501 }
1502
1503 operation_end(op_status);
1504 return 0;
1505}
1506
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001507int GUIAction::checkbackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001508{
1509 int op_status = 0;
1510
1511 operation_start("CheckBackupName");
1512 if (simulate) {
1513 simulate_progress_bar();
1514 } else {
Ethan Yonker53796e72019-01-11 22:49:52 -06001515 string Backup_Name;
1516 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
1517 op_status = PartitionManager.Check_Backup_Name(Backup_Name, true, true);
that3f7b1ac2014-12-30 11:30:13 +01001518 if (op_status != 0)
1519 op_status = 1;
1520 }
1521
1522 operation_end(op_status);
1523 return 0;
1524}
1525
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001526int GUIAction::decrypt(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001527{
1528 int op_status = 0;
1529
1530 operation_start("Decrypt");
1531 if (simulate) {
1532 simulate_progress_bar();
1533 } else {
1534 string Password;
Noah Jacobson5a79f672019-04-28 00:10:07 -04001535 string userID;
that3f7b1ac2014-12-30 11:30:13 +01001536 DataManager::GetValue("tw_crypto_password", Password);
Noah Jacobson5a79f672019-04-28 00:10:07 -04001537
1538 if (DataManager::GetIntValue(TW_IS_FBE)) { // for FBE
1539 DataManager::GetValue("tw_crypto_user_id", userID);
1540 if (userID != "") {
1541 op_status = PartitionManager.Decrypt_Device(Password, atoi(userID.c_str()));
1542 if (userID != "0") {
1543 if (op_status != 0)
1544 op_status = 1;
1545 operation_end(op_status);
1546 return 0;
1547 }
1548 } else {
1549 LOGINFO("User ID not found\n");
1550 op_status = 1;
1551 }
1552 ::sleep(1);
1553 } else { // for FDE
1554 op_status = PartitionManager.Decrypt_Device(Password);
1555 }
1556
that3f7b1ac2014-12-30 11:30:13 +01001557 if (op_status != 0)
1558 op_status = 1;
1559 else {
that3f7b1ac2014-12-30 11:30:13 +01001560 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1561
Ethan Yonkercf50da52015-01-12 21:59:07 -06001562 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001563
Ethan Yonkercf50da52015-01-12 21:59:07 -06001564 // Check for a custom theme and load it if exists
1565 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1566 if (has_datamedia != 0) {
1567 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06001568 LOGINFO("Failed to get default contexts and file mode for storage files.\n");
Ethan Yonkercf50da52015-01-12 21:59:07 -06001569 } else {
1570 LOGINFO("Got default contexts and file mode for storage files.\n");
that3f7b1ac2014-12-30 11:30:13 +01001571 }
1572 }
Ethan Yonker66a19492015-12-10 10:19:45 -06001573 PartitionManager.Decrypt_Adopted();
that3f7b1ac2014-12-30 11:30:13 +01001574 }
1575 }
1576
1577 operation_end(op_status);
1578 return 0;
1579}
1580
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001581int GUIAction::adbsideload(std::string arg __unused)
thatcc8ddca2015-01-03 01:59:36 +01001582{
1583 operation_start("Sideload");
1584 if (simulate) {
1585 simulate_progress_bar();
1586 operation_end(0);
1587 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001588 gui_msg("start_sideload=Starting ADB sideload feature...");
thatc6085482015-01-09 22:12:43 +01001589 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
1590
1591 // wait for the adb connection
1592 int ret = apply_from_adb("/", &sideload_child_pid);
1593 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
1594
1595 if (ret != 0) {
1596 if (ret == -2)
Ethan Yonker74db1572015-10-28 12:44:49 -05001597 gui_msg("need_new_adb=You need adb 1.0.32 or newer to sideload to this device.");
thatc6085482015-01-09 22:12:43 +01001598 ret = 1; // failure
1599 } else {
1600 int wipe_cache = 0;
1601 int wipe_dalvik = 0;
1602 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
1603
1604 if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) {
1605 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1606 PartitionManager.Wipe_By_Path("/cache");
1607 if (wipe_dalvik)
1608 PartitionManager.Wipe_Dalvik_Cache();
1609 } else {
1610 ret = 1; // failure
1611 }
thatcc8ddca2015-01-03 01:59:36 +01001612 }
thatc6085482015-01-09 22:12:43 +01001613 if (sideload_child_pid) {
1614 LOGINFO("Signaling child sideload process to exit.\n");
1615 struct stat st;
1616 // Calling stat() on this magic filename signals the minadbd
1617 // subprocess to shut down.
1618 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1619 int status;
1620 LOGINFO("Waiting for child sideload process to exit.\n");
1621 waitpid(sideload_child_pid, &status, 0);
1622 }
Dees Troy28a85d22015-04-28 02:03:16 +00001623 property_set("ctl.start", "adbd");
thatc6085482015-01-09 22:12:43 +01001624 TWFunc::Toggle_MTP(mtp_was_enabled);
1625 reinject_after_flash();
1626 operation_end(ret);
thatcc8ddca2015-01-03 01:59:36 +01001627 }
that3f7b1ac2014-12-30 11:30:13 +01001628 return 0;
1629}
1630
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001631int GUIAction::adbsideloadcancel(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001632{
that3f7b1ac2014-12-30 11:30:13 +01001633 struct stat st;
1634 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
Ethan Yonker74db1572015-10-28 12:44:49 -05001635 gui_msg("cancel_sideload=Cancelling ADB sideload...");
thatcc8ddca2015-01-03 01:59:36 +01001636 LOGINFO("Signaling child sideload process to exit.\n");
1637 // Calling stat() on this magic filename signals the minadbd
1638 // subprocess to shut down.
1639 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1640 if (!sideload_child_pid) {
1641 LOGERR("Unable to get child ID\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001642 return 0;
1643 }
thatcc8ddca2015-01-03 01:59:36 +01001644 ::sleep(1);
1645 LOGINFO("Killing child sideload process.\n");
1646 kill(sideload_child_pid, SIGTERM);
1647 int status;
1648 LOGINFO("Waiting for child sideload process to exit.\n");
1649 waitpid(sideload_child_pid, &status, 0);
1650 sideload_child_pid = 0;
that3f7b1ac2014-12-30 11:30:13 +01001651 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
1652 return 0;
1653}
1654
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001655int GUIAction::openrecoveryscript(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001656{
1657 operation_start("OpenRecoveryScript");
1658 if (simulate) {
1659 simulate_progress_bar();
Ethan Yonker24e7eb72015-01-03 16:13:06 -06001660 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001661 } else {
that10ae24f2015-12-26 20:53:51 +01001662 int op_status = OpenRecoveryScript::Run_OpenRecoveryScript_Action();
Ethan Yonkere1abe612015-06-09 11:09:32 -05001663 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001664 }
1665 return 0;
1666}
1667
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001668int GUIAction::installsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001669{
1670 int op_status = 0;
1671
1672 operation_start("Install SuperSU");
1673 if (simulate) {
1674 simulate_progress_bar();
1675 } else {
Ethan Yonkerfa67cbf2018-07-20 12:22:33 -05001676 LOGERR("Installing SuperSU was deprecated from TWRP.\n");
that3f7b1ac2014-12-30 11:30:13 +01001677 }
1678
1679 operation_end(op_status);
1680 return 0;
1681}
1682
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001683int GUIAction::fixsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001684{
1685 int op_status = 0;
1686
1687 operation_start("Fixing Superuser Permissions");
1688 if (simulate) {
1689 simulate_progress_bar();
1690 } else {
1691 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1692 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
1693 }
1694
1695 operation_end(op_status);
1696 return 0;
1697}
1698
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001699int GUIAction::decrypt_backup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001700{
1701 int op_status = 0;
1702
1703 operation_start("Try Restore Decrypt");
1704 if (simulate) {
1705 simulate_progress_bar();
1706 } else {
1707 string Restore_Path, Filename, Password;
1708 DataManager::GetValue("tw_restore", Restore_Path);
1709 Restore_Path += "/";
1710 DataManager::GetValue("tw_restore_password", Password);
1711 TWFunc::SetPerformanceMode(true);
1712 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1713 op_status = 0; // success
1714 else
1715 op_status = 1; // fail
1716 TWFunc::SetPerformanceMode(false);
1717 }
1718
1719 operation_end(op_status);
1720 return 0;
1721}
1722
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001723int GUIAction::repair(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001724{
1725 int op_status = 0;
1726
1727 operation_start("Repair Partition");
1728 if (simulate) {
1729 simulate_progress_bar();
1730 } else {
1731 string part_path;
1732 DataManager::GetValue("tw_partition_mount_point", part_path);
1733 if (PartitionManager.Repair_By_Path(part_path, true)) {
1734 op_status = 0; // success
1735 } else {
that3f7b1ac2014-12-30 11:30:13 +01001736 op_status = 1; // fail
1737 }
1738 }
1739
1740 operation_end(op_status);
1741 return 0;
1742}
1743
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001744int GUIAction::resize(std::string arg __unused)
Ethan Yonkera2719152015-05-28 09:44:41 -05001745{
1746 int op_status = 0;
1747
1748 operation_start("Resize Partition");
1749 if (simulate) {
1750 simulate_progress_bar();
1751 } else {
1752 string part_path;
1753 DataManager::GetValue("tw_partition_mount_point", part_path);
1754 if (PartitionManager.Resize_By_Path(part_path, true)) {
1755 op_status = 0; // success
1756 } else {
Ethan Yonkera2719152015-05-28 09:44:41 -05001757 op_status = 1; // fail
1758 }
1759 }
1760
1761 operation_end(op_status);
1762 return 0;
1763}
1764
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001765int GUIAction::changefilesystem(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001766{
1767 int op_status = 0;
1768
1769 operation_start("Change File System");
1770 if (simulate) {
1771 simulate_progress_bar();
1772 } else {
1773 string part_path, file_system;
1774 DataManager::GetValue("tw_partition_mount_point", part_path);
1775 DataManager::GetValue("tw_action_new_file_system", file_system);
1776 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1777 op_status = 0; // success
1778 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001779 gui_err("change_fs_err=Error changing file system.");
that3f7b1ac2014-12-30 11:30:13 +01001780 op_status = 1; // fail
1781 }
1782 }
1783 PartitionManager.Update_System_Details();
1784 operation_end(op_status);
1785 return 0;
1786}
1787
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001788int GUIAction::startmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001789{
1790 int op_status = 0;
1791
1792 operation_start("Start MTP");
1793 if (PartitionManager.Enable_MTP())
1794 op_status = 0; // success
1795 else
1796 op_status = 1; // fail
1797
1798 operation_end(op_status);
1799 return 0;
1800}
1801
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001802int GUIAction::stopmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001803{
1804 int op_status = 0;
1805
1806 operation_start("Stop MTP");
1807 if (PartitionManager.Disable_MTP())
1808 op_status = 0; // success
1809 else
1810 op_status = 1; // fail
1811
1812 operation_end(op_status);
1813 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001814}
1815
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001816int GUIAction::flashimage(std::string arg __unused)
Ethan Yonker96af84a2015-01-05 14:58:36 -06001817{
1818 int op_status = 0;
1819
1820 operation_start("Flash Image");
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001821 string path, filename;
1822 DataManager::GetValue("tw_zip_location", path);
1823 DataManager::GetValue("tw_file", filename);
Ethan Yonkere080c1f2016-09-19 13:50:25 -05001824 if (PartitionManager.Flash_Image(path, filename))
Ethan Yonker96af84a2015-01-05 14:58:36 -06001825 op_status = 0; // success
1826 else
1827 op_status = 1; // fail
1828
1829 operation_end(op_status);
1830 return 0;
1831}
1832
that10ae24f2015-12-26 20:53:51 +01001833int GUIAction::twcmd(std::string arg)
1834{
1835 operation_start("TWRP CLI Command");
1836 if (simulate)
1837 simulate_progress_bar();
1838 else
1839 OpenRecoveryScript::Run_CLI_Command(arg.c_str());
1840 operation_end(0);
1841 return 0;
1842}
1843
Dees_Troy51a0e822012-09-05 15:24:24 -04001844int GUIAction::getKeyByName(std::string key)
1845{
that8834a0f2016-01-05 23:29:30 +01001846 if (key == "home") return KEY_HOMEPAGE; // note: KEY_HOME is cursor movement (like KEY_END)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001847 else if (key == "menu") return KEY_MENU;
1848 else if (key == "back") return KEY_BACK;
1849 else if (key == "search") return KEY_SEARCH;
1850 else if (key == "voldown") return KEY_VOLUMEDOWN;
1851 else if (key == "volup") return KEY_VOLUMEUP;
1852 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001853 int ret_val;
1854 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1855 if (!ret_val)
1856 return KEY_POWER;
1857 else
1858 return ret_val;
1859 }
1860
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001861 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001862}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001863
1864int GUIAction::checkpartitionlifetimewrites(std::string arg)
1865{
1866 int op_status = 0;
1867 TWPartition* sys = PartitionManager.Find_Partition_By_Path(arg);
1868
1869 operation_start("Check Partition Lifetime Writes");
1870 if (sys) {
1871 if (sys->Check_Lifetime_Writes() != 0)
1872 DataManager::SetValue("tw_lifetime_writes", 1);
1873 else
1874 DataManager::SetValue("tw_lifetime_writes", 0);
1875 op_status = 0; // success
1876 } else {
1877 DataManager::SetValue("tw_lifetime_writes", 1);
1878 op_status = 1; // fail
1879 }
1880
1881 operation_end(op_status);
1882 return 0;
1883}
1884
1885int GUIAction::mountsystemtoggle(std::string arg)
1886{
1887 int op_status = 0;
Captain Throwback9d6feb52018-07-27 10:05:24 -04001888 bool remount_system = PartitionManager.Is_Mounted_By_Path(PartitionManager.Get_Android_Root_Path());
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001889 bool remount_vendor = PartitionManager.Is_Mounted_By_Path("/vendor");
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001890
1891 operation_start("Toggle System Mount");
Captain Throwback9d6feb52018-07-27 10:05:24 -04001892 if (!PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001893 op_status = 1; // fail
1894 } else {
Captain Throwback9d6feb52018-07-27 10:05:24 -04001895 TWPartition* Part = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001896 if (Part) {
Ethan Yonkerd6966f42015-05-30 14:52:16 -05001897 if (arg == "0") {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001898 DataManager::SetValue("tw_mount_system_ro", 0);
1899 Part->Change_Mount_Read_Only(false);
1900 } else {
1901 DataManager::SetValue("tw_mount_system_ro", 1);
1902 Part->Change_Mount_Read_Only(true);
1903 }
1904 if (remount_system) {
1905 Part->Mount(true);
1906 }
1907 op_status = 0; // success
1908 } else {
1909 op_status = 1; // fail
1910 }
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001911 Part = PartitionManager.Find_Partition_By_Path("/vendor");
1912 if (Part) {
1913 if (arg == "0") {
1914 Part->Change_Mount_Read_Only(false);
1915 } else {
1916 Part->Change_Mount_Read_Only(true);
1917 }
1918 if (remount_vendor) {
1919 Part->Mount(true);
1920 }
1921 op_status = 0; // success
1922 } else {
1923 op_status = 1; // fail
1924 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001925 }
1926
1927 operation_end(op_status);
1928 return 0;
1929}
Ethan Yonker74db1572015-10-28 12:44:49 -05001930
1931int GUIAction::setlanguage(std::string arg __unused)
1932{
1933 int op_status = 0;
1934
1935 operation_start("Set Language");
1936 PageManager::LoadLanguage(DataManager::GetStrValue("tw_language"));
1937 PageManager::RequestReload();
1938 op_status = 0; // success
1939
1940 operation_end(op_status);
1941 return 0;
1942}
Ethan Yonker1b190162016-12-05 15:25:19 -06001943
Matt Mower9472ba12016-01-20 18:12:47 -06001944int GUIAction::togglebacklight(std::string arg __unused)
1945{
1946 blankTimer.toggleBlank();
1947 return 0;
1948}
1949
Ethan Yonker1b190162016-12-05 15:25:19 -06001950int GUIAction::setbootslot(std::string arg)
1951{
1952 operation_start("Set Boot Slot");
1953 if (!simulate)
Ethan Yonker1b190162016-12-05 15:25:19 -06001954 PartitionManager.Set_Active_Slot(arg);
Matt Mower029a82d2017-01-08 13:12:38 -06001955 else
Ethan Yonker1b190162016-12-05 15:25:19 -06001956 simulate_progress_bar();
1957 operation_end(0);
1958 return 0;
1959}
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001960
1961int GUIAction::checkforapp(std::string arg __unused)
1962{
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001963 operation_start("Check for TWRP App");
1964 if (!simulate)
1965 {
epicX9d930a22020-12-29 00:39:36 +05301966 TWFunc::checkforapp();
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001967 } else
1968 simulate_progress_bar();
epicX9d930a22020-12-29 00:39:36 +05301969
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001970 operation_end(0);
1971 return 0;
1972}
1973
1974int GUIAction::installapp(std::string arg __unused)
1975{
1976 int op_status = 1;
1977 operation_start("Install TWRP App");
1978 if (!simulate)
1979 {
1980 if (DataManager::GetIntValue("tw_mount_system_ro") > 0 || DataManager::GetIntValue("tw_app_install_system") == 0) {
1981 if (PartitionManager.Mount_By_Path("/data", true)) {
1982 string install_path = "/data/app";
1983 string context = "u:object_r:apk_data_file:s0";
1984 if (!TWFunc::Path_Exists(install_path)) {
1985 if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
1986 LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno));
1987 goto exit;
1988 }
1989 if (chown(install_path.c_str(), 1000, 1000)) {
1990 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
1991 goto exit;
1992 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06001993 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001994 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
1995 goto exit;
1996 }
1997 }
1998 install_path += "/me.twrp.twrpapp-1";
1999 if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
2000 LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno));
2001 goto exit;
2002 }
2003 if (chown(install_path.c_str(), 1000, 1000)) {
2004 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
2005 goto exit;
2006 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002007 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002008 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2009 goto exit;
2010 }
2011 install_path += "/base.apk";
2012 if (TWFunc::copy_file("/sbin/me.twrp.twrpapp.apk", install_path, 0644)) {
2013 LOGERR("Error copying apk file\n");
2014 goto exit;
2015 }
2016 if (chown(install_path.c_str(), 1000, 1000)) {
2017 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
2018 goto exit;
2019 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002020 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002021 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2022 goto exit;
2023 }
2024 sync();
2025 sync();
2026 }
2027 } else {
Captain Throwback9d6feb52018-07-27 10:05:24 -04002028 if (PartitionManager.Mount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) {
2029 string base_path = PartitionManager.Get_Android_Root_Path();
2030 if (TWFunc::Path_Exists(PartitionManager.Get_Android_Root_Path() + "/system"))
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002031 base_path += "/system"; // For devices with system as a root file system (e.g. Pixel)
2032 string install_path = base_path + "/priv-app";
2033 string context = "u:object_r:system_file:s0";
2034 if (!TWFunc::Path_Exists(install_path))
2035 install_path = base_path + "/app";
2036 if (TWFunc::Path_Exists(install_path)) {
2037 install_path += "/twrpapp";
2038 LOGINFO("Installing app to '%s'\n", install_path.c_str());
2039 if (mkdir(install_path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0) {
Ethan Yonker4767caf2017-01-11 10:45:04 -06002040 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002041 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2042 goto exit;
2043 }
2044 install_path += "/me.twrp.twrpapp.apk";
2045 if (TWFunc::copy_file("/sbin/me.twrp.twrpapp.apk", install_path, 0644)) {
2046 LOGERR("Error copying apk file\n");
2047 goto exit;
2048 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002049 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002050 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2051 goto exit;
2052 }
Stefan Tauner86a43702020-01-11 22:28:53 +01002053
2054 // System apps require their permissions to be pre-set via an XML file in /etc/permissions
2055 string permission_path = base_path + "/etc/permissions/privapp-permissions-twrpapp.xml";
2056 if (TWFunc::copy_file("/sbin/privapp-permissions-twrpapp.xml", permission_path, 0644)) {
2057 LOGERR("Error copying permission file\n");
2058 goto exit;
2059 }
2060 if (chown(permission_path.c_str(), 1000, 1000)) {
2061 LOGERR("chown %s error: %s\n", permission_path.c_str(), strerror(errno));
2062 goto exit;
2063 }
2064 if (setfilecon(permission_path.c_str(), (security_context_t)context.c_str()) < 0) {
2065 LOGERR("setfilecon %s error: %s\n", permission_path.c_str(), strerror(errno));
2066 goto exit;
2067 }
2068
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002069 sync();
2070 sync();
Captain Throwback9d6feb52018-07-27 10:05:24 -04002071 PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002072 op_status = 0;
2073 } else {
2074 LOGERR("Error making app directory '%s': %s\n", strerror(errno));
2075 }
2076 }
2077 }
2078 }
2079 } else
2080 simulate_progress_bar();
2081exit:
epicX9d930a22020-12-29 00:39:36 +05302082 TWFunc::checkforapp();
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002083 operation_end(0);
2084 return 0;
2085}
Ethan Yonker53796e72019-01-11 22:49:52 -06002086
Ethan Yonker76bbd3a2019-05-10 10:50:04 -05002087int GUIAction::uninstalltwrpsystemapp(std::string arg __unused)
2088{
2089 int op_status = 1;
2090 operation_start("Uninstall TWRP System App");
2091 if (!simulate)
2092 {
2093 int Mount_System_RO = DataManager::GetIntValue("tw_mount_system_ro");
2094 TWPartition* Part = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path());
2095 if (!Part) {
2096 LOGERR("Unabled to find system partition.\n");
2097 goto exit;
2098 }
2099 if (!Part->UnMount(true)) {
2100 goto exit;
2101 }
2102 if (Mount_System_RO > 0) {
2103 DataManager::SetValue("tw_mount_system_ro", 0);
2104 Part->Change_Mount_Read_Only(false);
2105 }
2106 if (Part->Mount(true)) {
2107 string base_path = PartitionManager.Get_Android_Root_Path();
2108 if (TWFunc::Path_Exists(PartitionManager.Get_Android_Root_Path() + "/system"))
2109 base_path += "/system"; // For devices with system as a root file system (e.g. Pixel)
2110 string uninstall_path = base_path + "/priv-app";
2111 if (!TWFunc::Path_Exists(uninstall_path))
2112 uninstall_path = base_path + "/app";
2113 uninstall_path += "/twrpapp";
2114 if (TWFunc::Path_Exists(uninstall_path)) {
2115 LOGINFO("Uninstalling TWRP App from '%s'\n", uninstall_path.c_str());
2116 if (TWFunc::removeDir(uninstall_path, false) == 0) {
2117 sync();
2118 op_status = 0;
2119 DataManager::SetValue("tw_app_installed_in_system", 0);
2120 DataManager::SetValue("tw_app_install_status", 0);
2121 } else {
2122 LOGERR("Unable to remove TWRP app from system.\n");
2123 }
2124 } else {
2125 LOGINFO("didn't find TWRP app in '%s'\n", uninstall_path.c_str());
2126 }
2127 }
2128 Part->UnMount(true);
2129 if (Mount_System_RO > 0) {
2130 DataManager::SetValue("tw_mount_system_ro", Mount_System_RO);
2131 Part->Change_Mount_Read_Only(true);
2132 }
2133 } else
2134 simulate_progress_bar();
2135exit:
epicX9d930a22020-12-29 00:39:36 +05302136 TWFunc::checkforapp();
Ethan Yonker76bbd3a2019-05-10 10:50:04 -05002137 operation_end(0);
2138 return 0;
2139}
2140
Ethan Yonker53796e72019-01-11 22:49:52 -06002141int GUIAction::repackimage(std::string arg __unused)
2142{
2143 int op_status = 1;
2144 operation_start("Repack Image");
2145 if (!simulate)
2146 {
2147 std::string path = DataManager::GetStrValue("tw_filename");
2148 Repack_Options_struct Repack_Options;
2149 Repack_Options.Disable_Verity = false;
2150 Repack_Options.Disable_Force_Encrypt = false;
2151 Repack_Options.Backup_First = DataManager::GetIntValue("tw_repack_backup_first") != 0;
2152 if (DataManager::GetIntValue("tw_repack_kernel") == 1)
2153 Repack_Options.Type = REPLACE_KERNEL;
2154 else
2155 Repack_Options.Type = REPLACE_RAMDISK;
2156 if (!PartitionManager.Repack_Images(path, Repack_Options))
2157 goto exit;
2158 } else
2159 simulate_progress_bar();
2160 op_status = 0;
2161exit:
2162 operation_end(op_status);
2163 return 0;
2164}
2165
2166int GUIAction::fixabrecoverybootloop(std::string arg __unused)
2167{
2168 int op_status = 1;
2169 operation_start("Repack Image");
2170 if (!simulate)
2171 {
2172 if (!TWFunc::Path_Exists("/sbin/magiskboot")) {
2173 LOGERR("Image repacking tool not present in this TWRP build!");
2174 goto exit;
2175 }
2176 DataManager::SetProgress(0);
2177 TWPartition* part = PartitionManager.Find_Partition_By_Path("/boot");
2178 if (part)
2179 gui_msg(Msg("unpacking_image=Unpacking {1}...")(part->Display_Name));
2180 else {
2181 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")("/boot"));
2182 goto exit;
2183 }
2184 if (!PartitionManager.Prepare_Repack(part, REPACK_ORIG_DIR, DataManager::GetIntValue("tw_repack_backup_first") != 0, gui_lookup("repack", "Repack")))
2185 goto exit;
2186 DataManager::SetProgress(.25);
2187 gui_msg("fixing_recovery_loop_patch=Patching kernel...");
Mohd Faraz5738e762020-05-10 04:18:30 +05302188 std::string command = "cd " REPACK_ORIG_DIR " && /sbin/magiskboot hexpatch kernel 77616E745F696E697472616D667300 736B69705F696E697472616D667300";
Ethan Yonker53796e72019-01-11 22:49:52 -06002189 if (TWFunc::Exec_Cmd(command) != 0) {
2190 gui_msg(Msg(msg::kError, "fix_recovery_loop_patch_error=Error patching kernel."));
2191 goto exit;
2192 }
2193 std::string header_path = REPACK_ORIG_DIR;
2194 header_path += "header";
2195 if (TWFunc::Path_Exists(header_path)) {
2196 command = "cd " REPACK_ORIG_DIR " && sed -i \"s|$(grep '^cmdline=' header | cut -d= -f2-)|$(grep '^cmdline=' header | cut -d= -f2- | sed -e 's/skip_override//' -e 's/ */ /g' -e 's/[ \t]*$//')|\" header";
2197 if (TWFunc::Exec_Cmd(command) != 0) {
2198 gui_msg(Msg(msg::kError, "fix_recovery_loop_patch_error=Error patching kernel."));
2199 goto exit;
2200 }
2201 }
2202 DataManager::SetProgress(.5);
2203 gui_msg(Msg("repacking_image=Repacking {1}...")(part->Display_Name));
Mohd Faraz5738e762020-05-10 04:18:30 +05302204 command = "cd " REPACK_ORIG_DIR " && /sbin/magiskboot repack " REPACK_ORIG_DIR "boot.img";
Ethan Yonker53796e72019-01-11 22:49:52 -06002205 if (TWFunc::Exec_Cmd(command) != 0) {
2206 gui_msg(Msg(msg::kError, "repack_error=Error repacking image."));
2207 goto exit;
2208 }
2209 DataManager::SetProgress(.75);
2210 std::string path = REPACK_ORIG_DIR;
2211 std::string file = "new-boot.img";
2212 DataManager::SetValue("tw_flash_partition", "/boot;");
2213 if (!PartitionManager.Flash_Image(path, file)) {
2214 LOGINFO("Error flashing new image\n");
2215 goto exit;
2216 }
2217 DataManager::SetProgress(1);
2218 TWFunc::removeDir(REPACK_ORIG_DIR, false);
2219 } else
2220 simulate_progress_bar();
2221 op_status = 0;
2222exit:
2223 operation_end(op_status);
2224 return 0;
2225}
Mohd Faraz3c25ab32020-08-12 12:29:42 +00002226
2227int GUIAction::changeterminal(std::string arg) {
2228 bool res = true;
2229 std::string resp, cmd = "cd " + arg;
2230 DataManager::GetValue("tw_terminal_location", resp);
2231 if (arg.empty() && !resp.empty()) {
2232 cmd = "cd /";
2233 for (uint8_t iter = 0; iter < cmd.size(); iter++)
2234 term->NotifyCharInput(cmd.at(iter));
2235 term->NotifyCharInput(13);
2236 DataManager::SetValue("tw_terminal_location", "");
2237 return 0;
2238 }
2239 if (term != NULL && !arg.empty()) {
2240 DataManager::SetValue("tw_terminal_location", arg);
2241 if (term->status()) {
2242 for (uint8_t iter = 0; iter < cmd.size(); iter++)
2243 term->NotifyCharInput(cmd.at(iter));
2244 term->NotifyCharInput(13);
2245 }
2246 else if (chdir(arg.c_str()) != 0) {
2247 LOGINFO("Unable to change dir to %s\n", arg.c_str());
2248 res = false;
2249 }
2250 }
2251 else {
2252 res = false;
2253 LOGINFO("Unable to switch to Terminal\n");
2254 }
2255 if (res)
2256 gui_changePage("terminalcommand");
2257 return 0;
2258}
Captain Throwback3b556102021-02-12 19:32:36 -05002259#ifndef TW_EXCLUDE_NANO
2260int GUIAction::editfile(std::string arg) {
2261 if (term != NULL) {
2262 for (uint8_t iter = 0; iter < arg.size(); iter++)
2263 term->NotifyCharInput(arg.at(iter));
2264 term->NotifyCharInput(13);
2265 }
2266 else
2267 LOGINFO("Unable to switch to Terminal\n");
2268 return 0;
2269}
2270#endif