blob: 12ea91dd14b451f5a47c09729cb1a97edfae3ac0 [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
bigbiffd58ba182020-03-23 10:02:29 -040043#include "install/adb_install.h"
44
45#include "fuse_sideload.h"
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050046#include "blanktimer.hpp"
Ethan Yonker8373cfe2017-09-08 06:50:54 -050047#include "../twinstall.h"
Ethan Yonkerfbb43532015-12-28 21:54:50 +010048
Dees_Troy51a0e822012-09-05 15:24:24 -040049extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000050#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040051#include "../variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000052#include "cutils/properties.h"
bigbiffd58ba182020-03-23 10:02:29 -040053#include "install/adb_install.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040054};
bigbiffd58ba182020-03-23 10:02:29 -040055#include "set_metadata.h"
Ethan Yonkerfbb43532015-12-28 21:54:50 +010056#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040057
58#include "rapidxml.hpp"
59#include "objects.hpp"
bigbiffd58ba182020-03-23 10:02:29 -040060#include "tw_atomic.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040061
that3f7b1ac2014-12-30 11:30:13 +010062GUIAction::mapFunc GUIAction::mf;
thatc6085482015-01-09 22:12:43 +010063std::set<string> GUIAction::setActionsRunningInCallerThread;
that3f7b1ac2014-12-30 11:30:13 +010064static string zip_queue[10];
65static int zip_queue_index;
thatcc8ddca2015-01-03 01:59:36 +010066pid_t sideload_child_pid;
that3f7b1ac2014-12-30 11:30:13 +010067
that73a52952015-01-28 23:07:34 +010068static void *ActionThread_work_wrapper(void *data);
69
70class ActionThread
71{
72public:
73 ActionThread();
74 ~ActionThread();
75
76 void threadActions(GUIAction *act);
77 void run(void *data);
78private:
79 friend void *ActionThread_work_wrapper(void*);
80 struct ThreadData
81 {
82 ActionThread *this_;
83 GUIAction *act;
84 ThreadData(ActionThread *this_, GUIAction *act) : this_(this_), act(act) {}
85 };
86
87 pthread_t m_thread;
88 bool m_thread_running;
89 pthread_mutex_t m_act_lock;
90};
91
92static ActionThread action_thread; // for all kinds of longer running actions
93static ActionThread cancel_thread; // for longer running "cancel" actions
thatc6085482015-01-09 22:12:43 +010094
95static void *ActionThread_work_wrapper(void *data)
96{
that73a52952015-01-28 23:07:34 +010097 static_cast<ActionThread::ThreadData*>(data)->this_->run(data);
thatc6085482015-01-09 22:12:43 +010098 return NULL;
99}
100
101ActionThread::ActionThread()
102{
103 m_thread_running = false;
104 pthread_mutex_init(&m_act_lock, NULL);
105}
106
107ActionThread::~ActionThread()
108{
109 pthread_mutex_lock(&m_act_lock);
Matt Mowera8a89d12016-12-30 18:10:37 -0600110 if (m_thread_running) {
thatc6085482015-01-09 22:12:43 +0100111 pthread_mutex_unlock(&m_act_lock);
112 pthread_join(m_thread, NULL);
113 } else {
114 pthread_mutex_unlock(&m_act_lock);
115 }
116 pthread_mutex_destroy(&m_act_lock);
117}
118
that7d3b54f2015-01-09 22:52:51 +0100119void ActionThread::threadActions(GUIAction *act)
thatc6085482015-01-09 22:12:43 +0100120{
121 pthread_mutex_lock(&m_act_lock);
122 if (m_thread_running) {
123 pthread_mutex_unlock(&m_act_lock);
that7d3b54f2015-01-09 22:52:51 +0100124 LOGERR("Another threaded action is already running -- not running %u actions starting with '%s'\n",
125 act->mActions.size(), act->mActions[0].mFunction.c_str());
thatc6085482015-01-09 22:12:43 +0100126 } else {
127 m_thread_running = true;
128 pthread_mutex_unlock(&m_act_lock);
that73a52952015-01-28 23:07:34 +0100129 ThreadData *d = new ThreadData(this, act);
thatc6085482015-01-09 22:12:43 +0100130 pthread_create(&m_thread, NULL, &ActionThread_work_wrapper, d);
131 }
132}
133
134void ActionThread::run(void *data)
135{
136 ThreadData *d = (ThreadData*)data;
137 GUIAction* act = d->act;
138
139 std::vector<GUIAction::Action>::iterator it;
that7d3b54f2015-01-09 22:52:51 +0100140 for (it = act->mActions.begin(); it != act->mActions.end(); ++it)
thatc6085482015-01-09 22:12:43 +0100141 act->doAction(*it);
142
143 pthread_mutex_lock(&m_act_lock);
144 m_thread_running = false;
145 pthread_mutex_unlock(&m_act_lock);
146 delete d;
147}
148
Dees_Troy51a0e822012-09-05 15:24:24 -0400149GUIAction::GUIAction(xml_node<>* node)
Vojtech Bocekede51c52014-02-07 23:58:09 +0100150 : GUIObject(node)
Dees_Troy51a0e822012-09-05 15:24:24 -0400151{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200152 xml_node<>* child;
153 xml_node<>* actions;
154 xml_attribute<>* attr;
Dees_Troy51a0e822012-09-05 15:24:24 -0400155
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200156 if (!node) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400157
that3f7b1ac2014-12-30 11:30:13 +0100158 if (mf.empty()) {
Vojtech Bocekecd89182015-01-15 16:28:54 +0100159#define ADD_ACTION(n) mf[#n] = &GUIAction::n
160#define ADD_ACTION_EX(name, func) mf[name] = &GUIAction::func
thatc6085482015-01-09 22:12:43 +0100161 // These actions will be run in the caller's thread
Vojtech Bocekecd89182015-01-15 16:28:54 +0100162 ADD_ACTION(reboot);
163 ADD_ACTION(home);
164 ADD_ACTION(key);
165 ADD_ACTION(page);
166 ADD_ACTION(reload);
167 ADD_ACTION(readBackup);
168 ADD_ACTION(set);
169 ADD_ACTION(clear);
170 ADD_ACTION(mount);
171 ADD_ACTION(unmount);
172 ADD_ACTION_EX("umount", unmount);
173 ADD_ACTION(restoredefaultsettings);
174 ADD_ACTION(copylog);
175 ADD_ACTION(compute);
176 ADD_ACTION_EX("addsubtract", compute);
177 ADD_ACTION(setguitimezone);
178 ADD_ACTION(overlay);
179 ADD_ACTION(queuezip);
180 ADD_ACTION(cancelzip);
181 ADD_ACTION(queueclear);
182 ADD_ACTION(sleep);
Matt Mower9a2a2052016-05-31 21:31:22 -0500183 ADD_ACTION(sleepcounter);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100184 ADD_ACTION(appenddatetobackupname);
185 ADD_ACTION(generatebackupname);
186 ADD_ACTION(checkpartitionlist);
187 ADD_ACTION(getpartitiondetails);
188 ADD_ACTION(screenshot);
189 ADD_ACTION(setbrightness);
190 ADD_ACTION(fileexists);
191 ADD_ACTION(killterminal);
192 ADD_ACTION(checkbackupname);
193 ADD_ACTION(adbsideloadcancel);
194 ADD_ACTION(fixsu);
195 ADD_ACTION(startmtp);
196 ADD_ACTION(stopmtp);
197 ADD_ACTION(cancelbackup);
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500198 ADD_ACTION(checkpartitionlifetimewrites);
199 ADD_ACTION(mountsystemtoggle);
Ethan Yonker74db1572015-10-28 12:44:49 -0500200 ADD_ACTION(setlanguage);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -0600201 ADD_ACTION(checkforapp);
Matt Mower9472ba12016-01-20 18:12:47 -0600202 ADD_ACTION(togglebacklight);
thatc6085482015-01-09 22:12:43 +0100203
204 // remember actions that run in the caller thread
205 for (mapFunc::const_iterator it = mf.begin(); it != mf.end(); ++it)
206 setActionsRunningInCallerThread.insert(it->first);
207
208 // These actions will run in a separate thread
Vojtech Bocekecd89182015-01-15 16:28:54 +0100209 ADD_ACTION(flash);
210 ADD_ACTION(wipe);
211 ADD_ACTION(refreshsizes);
212 ADD_ACTION(nandroid);
Ethan Yonkerb5fab762016-01-28 14:03:33 -0600213 ADD_ACTION(fixcontexts);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100214 ADD_ACTION(fixpermissions);
215 ADD_ACTION(dd);
216 ADD_ACTION(partitionsd);
217 ADD_ACTION(installhtcdumlock);
218 ADD_ACTION(htcdumlockrestoreboot);
219 ADD_ACTION(htcdumlockreflashrecovery);
220 ADD_ACTION(cmd);
221 ADD_ACTION(terminalcommand);
222 ADD_ACTION(reinjecttwrp);
223 ADD_ACTION(decrypt);
224 ADD_ACTION(adbsideload);
225 ADD_ACTION(openrecoveryscript);
226 ADD_ACTION(installsu);
227 ADD_ACTION(decrypt_backup);
228 ADD_ACTION(repair);
Ethan Yonkera2719152015-05-28 09:44:41 -0500229 ADD_ACTION(resize);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100230 ADD_ACTION(changefilesystem);
231 ADD_ACTION(flashimage);
that10ae24f2015-12-26 20:53:51 +0100232 ADD_ACTION(twcmd);
Ethan Yonker1b190162016-12-05 15:25:19 -0600233 ADD_ACTION(setbootslot);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -0600234 ADD_ACTION(installapp);
Ethan Yonker76bbd3a2019-05-10 10:50:04 -0500235 ADD_ACTION(uninstalltwrpsystemapp);
Ethan Yonker53796e72019-01-11 22:49:52 -0600236 ADD_ACTION(repackimage);
237 ADD_ACTION(fixabrecoverybootloop);
that3f7b1ac2014-12-30 11:30:13 +0100238 }
239
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200240 // First, get the action
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600241 actions = FindNode(node, "actions");
242 if (actions) child = FindNode(actions, "action");
243 else child = FindNode(node, "action");
Dees_Troy51a0e822012-09-05 15:24:24 -0400244
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200245 if (!child) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400246
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200247 while (child)
248 {
249 Action action;
Dees_Troy51a0e822012-09-05 15:24:24 -0400250
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200251 attr = child->first_attribute("function");
252 if (!attr) return;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500253
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200254 action.mFunction = attr->value();
255 action.mArg = child->value();
256 mActions.push_back(action);
Dees_Troy51a0e822012-09-05 15:24:24 -0400257
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200258 child = child->next_sibling("action");
259 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400260
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200261 // Now, let's get either the key or region
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600262 child = FindNode(node, "touch");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200263 if (child)
264 {
265 attr = child->first_attribute("key");
266 if (attr)
267 {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100268 std::vector<std::string> keys = TWFunc::Split_String(attr->value(), "+");
Matt Mowera8a89d12016-12-30 18:10:37 -0600269 for (size_t i = 0; i < keys.size(); ++i)
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100270 {
271 const int key = getKeyByName(keys[i]);
272 mKeys[key] = false;
273 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200274 }
275 else
276 {
277 attr = child->first_attribute("x");
278 if (!attr) return;
279 mActionX = atol(attr->value());
280 attr = child->first_attribute("y");
281 if (!attr) return;
282 mActionY = atol(attr->value());
283 attr = child->first_attribute("w");
284 if (!attr) return;
285 mActionW = atol(attr->value());
286 attr = child->first_attribute("h");
287 if (!attr) return;
288 mActionH = atol(attr->value());
289 }
290 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400291}
292
Matt Mower25036b72016-06-24 12:30:18 -0500293int GUIAction::NotifyTouch(TOUCH_STATE state, int x __unused, int y __unused)
Dees_Troy51a0e822012-09-05 15:24:24 -0400294{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200295 if (state == TOUCH_RELEASE)
296 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400297
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200298 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400299}
300
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100301int GUIAction::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -0400302{
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100303 std::map<int, bool>::iterator itr = mKeys.find(key);
Matt Mowera8a89d12016-12-30 18:10:37 -0600304 if (itr == mKeys.end())
that8834a0f2016-01-05 23:29:30 +0100305 return 1;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100306
307 bool prevState = itr->second;
308 itr->second = down;
309
310 // If there is only one key for this action, wait for key up so it
311 // doesn't trigger with multi-key actions.
312 // Else, check if all buttons are pressed, then consume their release events
313 // so they don't trigger one-button actions and reset mKeys pressed status
Matt Mowera8a89d12016-12-30 18:10:37 -0600314 if (mKeys.size() == 1) {
315 if (!down && prevState) {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100316 doActions();
thatd4725ca2016-01-19 00:15:21 +0100317 return 0;
318 }
Matt Mowera8a89d12016-12-30 18:10:37 -0600319 } else if (down) {
320 for (itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
321 if (!itr->second)
that8834a0f2016-01-05 23:29:30 +0100322 return 1;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100323 }
324
325 // Passed, all req buttons are pressed, reset them and consume release events
326 HardwareKeyboard *kb = PageManager::GetHardwareKeyboard();
Matt Mowera8a89d12016-12-30 18:10:37 -0600327 for (itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100328 kb->ConsumeKeyRelease(itr->first);
329 itr->second = false;
330 }
331
332 doActions();
thatd4725ca2016-01-19 00:15:21 +0100333 return 0;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100334 }
335
thatd4725ca2016-01-19 00:15:21 +0100336 return 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400337}
338
Vojtech Bocek07220562014-02-08 02:05:33 +0100339int GUIAction::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400340{
Vojtech Bocek07220562014-02-08 02:05:33 +0100341 GUIObject::NotifyVarChange(varName, value);
342
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100343 if (varName.empty() && !isConditionValid() && mKeys.empty() && !mActionW)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200344 doActions();
Matt Mowera8a89d12016-12-30 18:10:37 -0600345 else if ((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200346 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400347
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200348 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400349}
350
351void GUIAction::simulate_progress_bar(void)
352{
Ethan Yonker74db1572015-10-28 12:44:49 -0500353 gui_msg("simulating=Simulating actions...");
Dees_Troy51a0e822012-09-05 15:24:24 -0400354 for (int i = 0; i < 5; i++)
355 {
bigbiff7abc5fe2015-01-17 16:53:12 -0500356 if (PartitionManager.stop_backup.get_value()) {
357 DataManager::SetValue("tw_cancel_backup", 1);
Matt Mower3c366972015-12-25 19:28:31 -0600358 gui_msg("backup_cancel=Backup Cancelled");
bigbiff7abc5fe2015-01-17 16:53:12 -0500359 DataManager::SetValue("ui_progress", 0);
360 PartitionManager.stop_backup.set_value(0);
361 return;
362 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400363 usleep(500000);
364 DataManager::SetValue("ui_progress", i * 20);
365 }
366}
367
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600368int GUIAction::flash_zip(std::string filename, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400369{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200370 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400371
372 DataManager::SetValue("ui_progress", 0);
Chaosmasterd5364a02020-02-03 15:38:02 +0100373 DataManager::SetValue("ui_portion_size", 0);
374 DataManager::SetValue("ui_portion_start", 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400375
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200376 if (filename.empty())
377 {
378 LOGERR("No file specified.\n");
379 return -1;
380 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400381
Ethan Yonker9598c072016-01-15 21:54:34 -0600382 if (!TWFunc::Path_Exists(filename)) {
383 if (!PartitionManager.Mount_By_Path(filename, true)) {
384 return -1;
385 }
386 if (!TWFunc::Path_Exists(filename)) {
387 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")(filename));
388 return -1;
389 }
390 }
Dees_Troy657c3092012-09-10 20:32:10 -0400391
Dees_Troy51a0e822012-09-05 15:24:24 -0400392 if (simulate) {
393 simulate_progress_bar();
394 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400395 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400396
397 // Now, check if we need to ensure TWRP remains installed...
398 struct stat st;
399 if (stat("/sbin/installTwrp", &st) == 0)
400 {
401 DataManager::SetValue("tw_operation", "Configuring TWRP");
402 DataManager::SetValue("tw_partition", "");
Ethan Yonker74db1572015-10-28 12:44:49 -0500403 gui_msg("config_twrp=Configuring TWRP...");
Vojtech Bocek05534202013-09-11 08:11:56 +0200404 if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall") < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400405 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500406 gui_msg("config_twrp_err=Unable to configure TWRP with this kernel.");
Dees_Troy51a0e822012-09-05 15:24:24 -0400407 }
408 }
409 }
410
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200411 // Done
412 DataManager::SetValue("ui_progress", 100);
413 DataManager::SetValue("ui_progress", 0);
Chaosmasterd5364a02020-02-03 15:38:02 +0100414 DataManager::SetValue("ui_portion_size", 0);
415 DataManager::SetValue("ui_portion_start", 0);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200416 return ret_val;
Dees_Troy51a0e822012-09-05 15:24:24 -0400417}
418
that73a52952015-01-28 23:07:34 +0100419GUIAction::ThreadType GUIAction::getThreadType(const GUIAction::Action& action)
thatc6085482015-01-09 22:12:43 +0100420{
that73a52952015-01-28 23:07:34 +0100421 string func = gui_parse_text(action.mFunction);
422 bool needsThread = setActionsRunningInCallerThread.find(func) == setActionsRunningInCallerThread.end();
423 if (needsThread) {
424 if (func == "cancelbackup")
425 return THREAD_CANCEL;
426 else
427 return THREAD_ACTION;
428 }
429 return THREAD_NONE;
thatc6085482015-01-09 22:12:43 +0100430}
431
Dees_Troy51a0e822012-09-05 15:24:24 -0400432int GUIAction::doActions()
433{
that3f7b1ac2014-12-30 11:30:13 +0100434 if (mActions.size() < 1)
435 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400436
that73a52952015-01-28 23:07:34 +0100437 // Determine in which thread to run the actions.
438 // Do it for all actions at once before starting, so that we can cancel the whole batch if the thread is already busy.
439 ThreadType threadType = THREAD_NONE;
that3f7b1ac2014-12-30 11:30:13 +0100440 std::vector<Action>::iterator it;
that73a52952015-01-28 23:07:34 +0100441 for (it = mActions.begin(); it != mActions.end(); ++it) {
442 ThreadType tt = getThreadType(*it);
443 if (tt == THREAD_NONE)
444 continue;
445 if (threadType == THREAD_NONE)
446 threadType = tt;
447 else if (threadType != tt) {
448 LOGERR("Can't mix normal and cancel actions in the same list.\n"
449 "Running the whole batch in the cancel thread.\n");
450 threadType = THREAD_CANCEL;
thatc6085482015-01-09 22:12:43 +0100451 break;
452 }
that7d3b54f2015-01-09 22:52:51 +0100453 }
that73a52952015-01-28 23:07:34 +0100454
455 // Now run the actions in the desired thread.
456 switch (threadType) {
457 case THREAD_ACTION:
458 action_thread.threadActions(this);
459 break;
460
461 case THREAD_CANCEL:
462 cancel_thread.threadActions(this);
463 break;
464
465 default: {
466 // no iterators here because theme reloading might kill our object
467 const size_t cnt = mActions.size();
468 for (size_t i = 0; i < cnt; ++i)
469 doAction(mActions[i]);
470 }
thatc6085482015-01-09 22:12:43 +0100471 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400472
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200473 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400474}
475
that3f7b1ac2014-12-30 11:30:13 +0100476int GUIAction::doAction(Action action)
Dees_Troy51a0e822012-09-05 15:24:24 -0400477{
that3f7b1ac2014-12-30 11:30:13 +0100478 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400479
that3f7b1ac2014-12-30 11:30:13 +0100480 std::string function = gui_parse_text(action.mFunction);
481 std::string arg = gui_parse_text(action.mArg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400482
that3f7b1ac2014-12-30 11:30:13 +0100483 // find function and execute it
484 mapFunc::const_iterator funcitr = mf.find(function);
485 if (funcitr != mf.end())
486 return (this->*funcitr->second)(arg);
487
488 LOGERR("Unknown action '%s'\n", function.c_str());
489 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400490}
491
492void GUIAction::operation_start(const string operation_name)
493{
that3f7b1ac2014-12-30 11:30:13 +0100494 LOGINFO("operation_start: '%s'\n", operation_name.c_str());
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000495 time(&Start);
Dees_Troy51a0e822012-09-05 15:24:24 -0400496 DataManager::SetValue(TW_ACTION_BUSY, 1);
497 DataManager::SetValue("ui_progress", 0);
Chaosmasterd5364a02020-02-03 15:38:02 +0100498 DataManager::SetValue("ui_portion_size", 0);
499 DataManager::SetValue("ui_portion_start", 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400500 DataManager::SetValue("tw_operation", operation_name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400501 DataManager::SetValue("tw_operation_state", 0);
Ethan Yonkerd83c9ea2015-01-02 15:22:31 -0600502 DataManager::SetValue("tw_operation_status", 0);
bigbiff25d25b92020-06-19 16:07:38 -0400503 bool tw_ab_device = TWFunc::get_log_dir() != CACHE_LOGS_DIR;
bigbiff bigbiff19874f12019-01-08 20:06:57 -0500504 DataManager::SetValue("tw_ab_device", tw_ab_device);
Dees_Troy51a0e822012-09-05 15:24:24 -0400505}
506
that3f7b1ac2014-12-30 11:30:13 +0100507void GUIAction::operation_end(const int operation_status)
Dees_Troy51a0e822012-09-05 15:24:24 -0400508{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000509 time_t Stop;
Dees_Troy51a0e822012-09-05 15:24:24 -0400510 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400511 DataManager::SetValue("ui_progress", 100);
512 if (simulate) {
513 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
514 if (simulate_fail != 0)
515 DataManager::SetValue("tw_operation_status", 1);
516 else
517 DataManager::SetValue("tw_operation_status", 0);
518 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500519 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400520 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500521 }
522 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400523 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500524 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400525 }
526 DataManager::SetValue("tw_operation_state", 1);
527 DataManager::SetValue(TW_ACTION_BUSY, 0);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500528 blankTimer.resetTimerAndUnblank();
LuK133762326f42015-09-30 19:57:46 +0200529 property_set("twrp.action_complete", "1");
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000530 time(&Stop);
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400531
532#ifndef TW_NO_HAPTICS
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000533 if ((int) difftime(Stop, Start) > 10)
Ethan Yonker03db3262014-02-05 08:02:06 -0600534 DataManager::Vibrate("tw_action_vibrate");
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400535#endif
536
that3f7b1ac2014-12-30 11:30:13 +0100537 LOGINFO("operation_end - status=%d\n", operation_status);
Dees_Troy51a0e822012-09-05 15:24:24 -0400538}
539
that3f7b1ac2014-12-30 11:30:13 +0100540int GUIAction::reboot(std::string arg)
Dees_Troy51a0e822012-09-05 15:24:24 -0400541{
that3f7b1ac2014-12-30 11:30:13 +0100542 sync();
543 DataManager::SetValue("tw_gui_done", 1);
544 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400545
that3f7b1ac2014-12-30 11:30:13 +0100546 return 0;
547}
Dees_Troy51a0e822012-09-05 15:24:24 -0400548
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500549int GUIAction::home(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100550{
that3f7b1ac2014-12-30 11:30:13 +0100551 gui_changePage("main");
552 return 0;
553}
Dees_Troy51a0e822012-09-05 15:24:24 -0400554
that3f7b1ac2014-12-30 11:30:13 +0100555int GUIAction::key(std::string arg)
556{
557 const int key = getKeyByName(arg);
558 PageManager::NotifyKey(key, true);
559 PageManager::NotifyKey(key, false);
560 return 0;
561}
562
563int GUIAction::page(std::string arg)
564{
LuK133762326f42015-09-30 19:57:46 +0200565 property_set("twrp.action_complete", "0");
that3f7b1ac2014-12-30 11:30:13 +0100566 std::string page_name = gui_parse_text(arg);
567 return gui_changePage(page_name);
568}
569
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500570int GUIAction::reload(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100571{
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500572 PageManager::RequestReload();
573 // The actual reload is handled in pages.cpp in PageManager::RunReload()
574 // The reload will occur on the next Update or Render call and will
575 // be performed in the rendoer thread instead of the action thread
576 // to prevent crashing which could occur when we start deleting
577 // GUI resources in the action thread while we attempt to render
578 // with those same resources in another thread.
that3f7b1ac2014-12-30 11:30:13 +0100579 return 0;
580}
Dees_Troy51a0e822012-09-05 15:24:24 -0400581
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500582int GUIAction::readBackup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100583{
584 string Restore_Name;
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -0400585
that3f7b1ac2014-12-30 11:30:13 +0100586 DataManager::GetValue("tw_restore", Restore_Name);
587 PartitionManager.Set_Restore_Files(Restore_Name);
588 return 0;
589}
590
591int GUIAction::set(std::string arg)
592{
593 if (arg.find('=') != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200594 {
that3f7b1ac2014-12-30 11:30:13 +0100595 string varName = arg.substr(0, arg.find('='));
596 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400597
that3f7b1ac2014-12-30 11:30:13 +0100598 DataManager::GetValue(value, value);
599 DataManager::SetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200600 }
that3f7b1ac2014-12-30 11:30:13 +0100601 else
602 DataManager::SetValue(arg, "1");
603 return 0;
604}
Dees_Troy51a0e822012-09-05 15:24:24 -0400605
that3f7b1ac2014-12-30 11:30:13 +0100606int GUIAction::clear(std::string arg)
607{
608 DataManager::SetValue(arg, "0");
609 return 0;
610}
Dees_Troy51a0e822012-09-05 15:24:24 -0400611
that3f7b1ac2014-12-30 11:30:13 +0100612int GUIAction::mount(std::string arg)
613{
614 if (arg == "usb") {
615 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400616 if (!simulate)
that3f7b1ac2014-12-30 11:30:13 +0100617 PartitionManager.usb_storage_enable();
618 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500619 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100620 } else if (!simulate) {
621 PartitionManager.Mount_By_Path(arg, true);
622 PartitionManager.Add_MTP_Storage(arg);
623 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500624 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100625 return 0;
626}
627
628int GUIAction::unmount(std::string arg)
629{
630 if (arg == "usb") {
631 if (!simulate)
632 PartitionManager.usb_storage_disable();
633 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500634 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100635 DataManager::SetValue(TW_ACTION_BUSY, 0);
636 } else if (!simulate) {
637 PartitionManager.UnMount_By_Path(arg, true);
638 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500639 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100640 return 0;
641}
642
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500643int GUIAction::restoredefaultsettings(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100644{
645 operation_start("Restore Defaults");
646 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
Ethan Yonker74db1572015-10-28 12:44:49 -0500647 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100648 else {
649 DataManager::ResetDefaults();
650 PartitionManager.Update_System_Details();
651 PartitionManager.Mount_Current_Storage(true);
652 }
653 operation_end(0);
654 return 0;
655}
656
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500657int GUIAction::copylog(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100658{
659 operation_start("Copy Log");
660 if (!simulate)
661 {
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400662 string dst, curr_storage;
663 int copy_kernel_log = 0;
664
665 DataManager::GetValue("tw_include_kernel_log", copy_kernel_log);
that3f7b1ac2014-12-30 11:30:13 +0100666 PartitionManager.Mount_Current_Storage(true);
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400667 curr_storage = DataManager::GetCurrentStoragePath();
668 dst = curr_storage + "/recovery.log";
that3f7b1ac2014-12-30 11:30:13 +0100669 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
670 tw_set_default_metadata(dst.c_str());
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400671 if (copy_kernel_log)
672 TWFunc::copy_kernel_log(curr_storage);
that3f7b1ac2014-12-30 11:30:13 +0100673 sync();
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400674 gui_msg(Msg("copy_log=Copied recovery log to {1}")(dst));
that3f7b1ac2014-12-30 11:30:13 +0100675 } else
676 simulate_progress_bar();
677 operation_end(0);
678 return 0;
679}
680
681
682int GUIAction::compute(std::string arg)
683{
684 if (arg.find("+") != string::npos)
685 {
686 string varName = arg.substr(0, arg.find('+'));
687 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
688 int amount_to_add = atoi(string_to_add.c_str());
689 int value;
690
691 DataManager::GetValue(varName, value);
692 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400693 return 0;
694 }
that3f7b1ac2014-12-30 11:30:13 +0100695 if (arg.find("-") != string::npos)
Dees_Troy51a0e822012-09-05 15:24:24 -0400696 {
that3f7b1ac2014-12-30 11:30:13 +0100697 string varName = arg.substr(0, arg.find('-'));
698 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
699 int amount_to_subtract = atoi(string_to_subtract.c_str());
700 int value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400701
that3f7b1ac2014-12-30 11:30:13 +0100702 DataManager::GetValue(varName, value);
703 value -= amount_to_subtract;
704 if (value <= 0)
705 value = 0;
706 DataManager::SetValue(varName, value);
707 return 0;
708 }
709 if (arg.find("*") != string::npos)
710 {
711 string varName = arg.substr(0, arg.find('*'));
712 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
713 int multiply_by = atoi(multiply_by_str.c_str());
714 int value;
715
716 DataManager::GetValue(varName, value);
717 DataManager::SetValue(varName, value*multiply_by);
718 return 0;
719 }
720 if (arg.find("/") != string::npos)
721 {
722 string varName = arg.substr(0, arg.find('/'));
723 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
724 int divide_by = atoi(divide_by_str.c_str());
725 int value;
726
Matt Mowera8a89d12016-12-30 18:10:37 -0600727 if (divide_by != 0)
that3f7b1ac2014-12-30 11:30:13 +0100728 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400729 DataManager::GetValue(varName, value);
that3f7b1ac2014-12-30 11:30:13 +0100730 DataManager::SetValue(varName, value/divide_by);
Dees_Troy51a0e822012-09-05 15:24:24 -0400731 }
732 return 0;
733 }
that3f7b1ac2014-12-30 11:30:13 +0100734 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
735 return -1;
736}
Dees_Troy51a0e822012-09-05 15:24:24 -0400737
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500738int GUIAction::setguitimezone(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100739{
740 string SelectedZone;
741 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
742 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
743 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
744
745 int dst;
746 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
747
748 string offset;
749 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
750
751 string NewTimeZone = Zone;
752 if (offset != "0")
753 NewTimeZone += ":" + offset;
754
755 if (dst != 0)
756 NewTimeZone += DSTZone;
757
758 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
759 DataManager::update_tz_environment_variables();
760 return 0;
761}
762
763int GUIAction::overlay(std::string arg)
764{
765 return gui_changeOverlay(arg);
766}
767
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500768int GUIAction::queuezip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100769{
770 if (zip_queue_index >= 10) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500771 gui_msg("max_queue=Maximum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400772 return 0;
773 }
that3f7b1ac2014-12-30 11:30:13 +0100774 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
775 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
776 zip_queue_index++;
Dees_Troy51a0e822012-09-05 15:24:24 -0400777 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +0100778 }
779 return 0;
780}
781
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500782int GUIAction::cancelzip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100783{
784 if (zip_queue_index <= 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500785 gui_msg("min_queue=Minimum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400786 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100787 } else {
788 zip_queue_index--;
789 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
Dees_Troy51a0e822012-09-05 15:24:24 -0400790 }
that3f7b1ac2014-12-30 11:30:13 +0100791 return 0;
792}
Dees_Troy51a0e822012-09-05 15:24:24 -0400793
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500794int GUIAction::queueclear(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100795{
796 zip_queue_index = 0;
797 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
798 return 0;
799}
Dees_Troy51a0e822012-09-05 15:24:24 -0400800
that3f7b1ac2014-12-30 11:30:13 +0100801int GUIAction::sleep(std::string arg)
802{
803 operation_start("Sleep");
804 usleep(atoi(arg.c_str()));
805 operation_end(0);
806 return 0;
807}
Dees Troyb21cc642013-09-10 17:36:41 +0000808
Matt Mower9a2a2052016-05-31 21:31:22 -0500809int GUIAction::sleepcounter(std::string arg)
810{
811 operation_start("SleepCounter");
812 // Ensure user notices countdown in case it needs to be cancelled
813 blankTimer.resetTimerAndUnblank();
814 int total = atoi(arg.c_str());
815 for (int t = total; t > 0; t--) {
816 int progress = (int)(((float)(total-t)/(float)total)*100.0);
817 DataManager::SetValue("ui_progress", progress);
818 ::sleep(1);
819 DataManager::SetValue("tw_sleep", t-1);
820 }
821 DataManager::SetValue("ui_progress", 100);
822 operation_end(0);
823 return 0;
824}
825
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500826int GUIAction::appenddatetobackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100827{
828 operation_start("AppendDateToBackupName");
829 string Backup_Name;
830 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
831 Backup_Name += TWFunc::Get_Current_Date();
832 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
833 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
834 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
Matt Mower76b8afc2016-06-24 12:04:27 -0500835 PageManager::NotifyKey(KEY_END, true);
836 PageManager::NotifyKey(KEY_END, false);
that3f7b1ac2014-12-30 11:30:13 +0100837 operation_end(0);
838 return 0;
839}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500840
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500841int GUIAction::generatebackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100842{
843 operation_start("GenerateBackupName");
844 TWFunc::Auto_Generate_Backup_Name();
845 operation_end(0);
846 return 0;
847}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500848
Ethan Yonker483e9f42016-01-11 22:21:18 -0600849int GUIAction::checkpartitionlist(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100850{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600851 string List, part_path;
that3f7b1ac2014-12-30 11:30:13 +0100852 int count = 0;
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500853
Ethan Yonker483e9f42016-01-11 22:21:18 -0600854 if (arg.empty())
855 arg = "tw_wipe_list";
856 DataManager::GetValue(arg, List);
857 LOGINFO("checkpartitionlist list '%s'\n", List.c_str());
858 if (!List.empty()) {
859 size_t start_pos = 0, end_pos = List.find(";", start_pos);
860 while (end_pos != string::npos && start_pos < List.size()) {
861 part_path = List.substr(start_pos, end_pos - start_pos);
862 LOGINFO("checkpartitionlist part_path '%s'\n", part_path.c_str());
863 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100864 // Do nothing
Dees_Troy51a0e822012-09-05 15:24:24 -0400865 } else {
that3f7b1ac2014-12-30 11:30:13 +0100866 count++;
867 }
868 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600869 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100870 }
871 DataManager::SetValue("tw_check_partition_list", count);
872 } else {
873 DataManager::SetValue("tw_check_partition_list", 0);
874 }
Matt Mowera8a89d12016-12-30 18:10:37 -0600875 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100876}
Dees_Troy51a0e822012-09-05 15:24:24 -0400877
Ethan Yonker483e9f42016-01-11 22:21:18 -0600878int GUIAction::getpartitiondetails(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100879{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600880 string List, part_path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400881
Ethan Yonker483e9f42016-01-11 22:21:18 -0600882 if (arg.empty())
883 arg = "tw_wipe_list";
884 DataManager::GetValue(arg, List);
885 LOGINFO("getpartitiondetails list '%s'\n", List.c_str());
886 if (!List.empty()) {
887 size_t start_pos = 0, end_pos = List.find(";", start_pos);
888 part_path = List;
889 while (end_pos != string::npos && start_pos < List.size()) {
890 part_path = List.substr(start_pos, end_pos - start_pos);
891 LOGINFO("getpartitiondetails part_path '%s'\n", part_path.c_str());
892 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100893 // Do nothing
894 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600895 DataManager::SetValue("tw_partition_path", part_path);
that3f7b1ac2014-12-30 11:30:13 +0100896 break;
897 }
898 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600899 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100900 }
Ethan Yonker483e9f42016-01-11 22:21:18 -0600901 if (!part_path.empty()) {
902 TWPartition* Part = PartitionManager.Find_Partition_By_Path(part_path);
that3f7b1ac2014-12-30 11:30:13 +0100903 if (Part) {
904 unsigned long long mb = 1048576;
Dees_Troya13d74f2013-03-24 08:54:55 -0500905
that3f7b1ac2014-12-30 11:30:13 +0100906 DataManager::SetValue("tw_partition_name", Part->Display_Name);
907 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
908 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
909 DataManager::SetValue("tw_partition_size", Part->Size / mb);
910 DataManager::SetValue("tw_partition_used", Part->Used / mb);
911 DataManager::SetValue("tw_partition_free", Part->Free / mb);
912 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
913 DataManager::SetValue("tw_partition_removable", Part->Removable);
914 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
915
916 if (Part->Can_Repair())
917 DataManager::SetValue("tw_partition_can_repair", 1);
918 else
919 DataManager::SetValue("tw_partition_can_repair", 0);
Ethan Yonkera2719152015-05-28 09:44:41 -0500920 if (Part->Can_Resize())
921 DataManager::SetValue("tw_partition_can_resize", 1);
922 else
923 DataManager::SetValue("tw_partition_can_resize", 0);
Matt Mower18794c82015-11-11 16:22:45 -0600924 if (TWFunc::Path_Exists("/sbin/mkfs.fat"))
that3f7b1ac2014-12-30 11:30:13 +0100925 DataManager::SetValue("tw_partition_vfat", 1);
926 else
927 DataManager::SetValue("tw_partition_vfat", 0);
Matt Mower80f7b362015-12-12 15:38:01 -0600928 if (TWFunc::Path_Exists("/sbin/mkexfatfs"))
that3f7b1ac2014-12-30 11:30:13 +0100929 DataManager::SetValue("tw_partition_exfat", 1);
930 else
931 DataManager::SetValue("tw_partition_exfat", 0);
932 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs"))
933 DataManager::SetValue("tw_partition_f2fs", 1);
934 else
935 DataManager::SetValue("tw_partition_f2fs", 0);
936 if (TWFunc::Path_Exists("/sbin/mke2fs"))
937 DataManager::SetValue("tw_partition_ext", 1);
938 else
939 DataManager::SetValue("tw_partition_ext", 0);
940 return 0;
941 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600942 LOGERR("Unable to locate partition: '%s'\n", part_path.c_str());
that3f7b1ac2014-12-30 11:30:13 +0100943 }
944 }
945 }
946 DataManager::SetValue("tw_partition_name", "");
947 DataManager::SetValue("tw_partition_file_system", "");
Ethan Yonker483e9f42016-01-11 22:21:18 -0600948 // Set this to 0 to prevent trying to partition this device, just in case
949 DataManager::SetValue("tw_partition_removable", 0);
that3f7b1ac2014-12-30 11:30:13 +0100950 return 0;
951}
952
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500953int GUIAction::screenshot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100954{
955 time_t tm;
956 char path[256];
957 int path_len;
Matt Mower0c88b842017-01-15 16:00:49 -0600958 uid_t uid = AID_MEDIA_RW;
959 gid_t gid = AID_MEDIA_RW;
that3f7b1ac2014-12-30 11:30:13 +0100960
961 const std::string storage = DataManager::GetCurrentStoragePath();
Matt Mowera8a89d12016-12-30 18:10:37 -0600962 if (PartitionManager.Is_Mounted_By_Path(storage)) {
that3f7b1ac2014-12-30 11:30:13 +0100963 snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
964 } else {
965 strcpy(path, "/tmp/");
966 }
967
Matt Mowera8a89d12016-12-30 18:10:37 -0600968 if (!TWFunc::Create_Dir_Recursive(path, 0775, uid, gid))
that3f7b1ac2014-12-30 11:30:13 +0100969 return 0;
970
971 tm = time(NULL);
972 path_len = strlen(path);
973
974 // Screenshot_2014-01-01-18-21-38.png
975 strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
976
977 int res = gr_save_screenshot(path);
Matt Mowera8a89d12016-12-30 18:10:37 -0600978 if (res == 0) {
that3f7b1ac2014-12-30 11:30:13 +0100979 chmod(path, 0666);
980 chown(path, uid, gid);
981
that677b13f2015-12-26 23:57:31 +0100982 gui_msg(Msg("screenshot_saved=Screenshot was saved to {1}")(path));
that3f7b1ac2014-12-30 11:30:13 +0100983
VDavid0032034a412019-10-18 22:43:20 +0200984 // blink to notify that the screenshot was taken
that3f7b1ac2014-12-30 11:30:13 +0100985 gr_color(255, 255, 255, 255);
986 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
987 gr_flip();
988 gui_forceRender();
989 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500990 gui_err("screenshot_err=Failed to take a screenshot!");
that3f7b1ac2014-12-30 11:30:13 +0100991 }
992 return 0;
993}
994
995int GUIAction::setbrightness(std::string arg)
996{
997 return TWFunc::Set_Brightness(arg);
998}
999
1000int GUIAction::fileexists(std::string arg)
1001{
1002 struct stat st;
1003 string newpath = arg + "/.";
1004
1005 operation_start("FileExists");
1006 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
1007 operation_end(0);
1008 else
1009 operation_end(1);
1010 return 0;
1011}
1012
thatcc8ddca2015-01-03 01:59:36 +01001013void GUIAction::reinject_after_flash()
1014{
1015 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001016 gui_msg("injecttwrp=Injecting TWRP into boot image...");
thatcc8ddca2015-01-03 01:59:36 +01001017 if (simulate) {
1018 simulate_progress_bar();
1019 } else {
1020 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
1021 if (Boot == NULL || Boot->Current_File_System != "emmc")
1022 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
1023 else {
1024 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
1025 TWFunc::Exec_Cmd(injectcmd);
1026 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001027 gui_msg("done=Done.");
thatcc8ddca2015-01-03 01:59:36 +01001028 }
1029 }
1030}
1031
mauronofrio6d3bf892019-10-26 19:47:55 +02001032int GUIAction::ozip_decrypt(string zip_path)
1033{
1034 if (!TWFunc::Path_Exists("/sbin/ozip_decrypt")) {
1035 return 1;
1036 }
1037 gui_msg("ozip_decrypt_decryption=Starting Ozip Decryption...");
1038 TWFunc::Exec_Cmd("ozip_decrypt " + (string)TW_OZIP_DECRYPT_KEY + " '" + zip_path + "'");
1039 gui_msg("ozip_decrypt_finish=Ozip Decryption Finished!");
1040 return 0;
1041}
1042
that3f7b1ac2014-12-30 11:30:13 +01001043int GUIAction::flash(std::string arg)
1044{
1045 int i, ret_val = 0, wipe_cache = 0;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001046 // We're going to jump to this page first, like a loading page
1047 gui_changePage(arg);
that3f7b1ac2014-12-30 11:30:13 +01001048 for (i=0; i<zip_queue_index; i++) {
thatc7b631b2015-06-01 23:36:57 +02001049 string zip_path = zip_queue[i];
1050 size_t slashpos = zip_path.find_last_of('/');
1051 string zip_filename = (slashpos == string::npos) ? zip_path : zip_path.substr(slashpos + 1);
that3f7b1ac2014-12-30 11:30:13 +01001052 operation_start("Flashing");
mauronofrio6d3bf892019-10-26 19:47:55 +02001053 if((zip_path.substr(zip_path.size() - 4, 4)) == "ozip")
1054 {
1055 if((ozip_decrypt(zip_path)) != 0)
1056 {
1057 LOGERR("Unable to find ozip_decrypt!");
1058 break;
1059 }
1060 zip_filename = (zip_filename.substr(0, zip_filename.size() - 4)).append("zip");
1061 zip_path = (zip_path.substr(0, zip_path.size() - 4)).append("zip");
1062 if (!TWFunc::Path_Exists(zip_path)) {
1063 LOGERR("Unable to find decrypted zip");
1064 break;
1065 }
1066 }
thatc7b631b2015-06-01 23:36:57 +02001067 DataManager::SetValue("tw_filename", zip_path);
1068 DataManager::SetValue("tw_file", zip_filename);
that3f7b1ac2014-12-30 11:30:13 +01001069 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
1070
1071 TWFunc::SetPerformanceMode(true);
thatc7b631b2015-06-01 23:36:57 +02001072 ret_val = flash_zip(zip_path, &wipe_cache);
that3f7b1ac2014-12-30 11:30:13 +01001073 TWFunc::SetPerformanceMode(false);
1074 if (ret_val != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001075 gui_msg(Msg(msg::kError, "zip_err=Error installing zip file '{1}'")(zip_path));
that3f7b1ac2014-12-30 11:30:13 +01001076 ret_val = 1;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001077 break;
that3f7b1ac2014-12-30 11:30:13 +01001078 }
1079 }
1080 zip_queue_index = 0;
that3f7b1ac2014-12-30 11:30:13 +01001081
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001082 if (wipe_cache) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001083 gui_msg("zip_wipe_cache=One or more zip requested a cache wipe -- Wiping cache now.");
that3f7b1ac2014-12-30 11:30:13 +01001084 PartitionManager.Wipe_By_Path("/cache");
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001085 }
that3f7b1ac2014-12-30 11:30:13 +01001086
thatcc8ddca2015-01-03 01:59:36 +01001087 reinject_after_flash();
that3f7b1ac2014-12-30 11:30:13 +01001088 PartitionManager.Update_System_Details();
1089 operation_end(ret_val);
bigbiffa869fc72016-03-01 19:40:36 -05001090 // 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 -06001091 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +01001092 return 0;
1093}
1094
1095int GUIAction::wipe(std::string arg)
1096{
1097 operation_start("Format");
1098 DataManager::SetValue("tw_partition", arg);
that3f7b1ac2014-12-30 11:30:13 +01001099 int ret_val = false;
1100
1101 if (simulate) {
1102 simulate_progress_bar();
1103 } else {
1104 if (arg == "data")
1105 ret_val = PartitionManager.Factory_Reset();
1106 else if (arg == "battery")
1107 ret_val = PartitionManager.Wipe_Battery_Stats();
1108 else if (arg == "rotate")
1109 ret_val = PartitionManager.Wipe_Rotate_Data();
1110 else if (arg == "dalvik")
1111 ret_val = PartitionManager.Wipe_Dalvik_Cache();
1112 else if (arg == "DATAMEDIA") {
1113 ret_val = PartitionManager.Format_Data();
1114 } else if (arg == "INTERNAL") {
Matt Mower23d8aae2017-01-06 14:30:33 -06001115 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001116
1117 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1118 if (has_datamedia) {
1119 ret_val = PartitionManager.Wipe_Media_From_Data();
1120 } else {
1121 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
1122 }
1123 } else if (arg == "EXTERNAL") {
1124 string External_Path;
1125
1126 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
1127 ret_val = PartitionManager.Wipe_By_Path(External_Path);
1128 } else if (arg == "ANDROIDSECURE") {
1129 ret_val = PartitionManager.Wipe_Android_Secure();
1130 } else if (arg == "LIST") {
1131 string Wipe_List, wipe_path;
1132 bool skip = false;
1133 ret_val = true;
that3f7b1ac2014-12-30 11:30:13 +01001134
1135 DataManager::GetValue("tw_wipe_list", Wipe_List);
1136 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
1137 if (!Wipe_List.empty()) {
1138 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
1139 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
1140 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
1141 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
1142 if (wipe_path == "/and-sec") {
1143 if (!PartitionManager.Wipe_Android_Secure()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001144 gui_msg("and_sec_wipe_err=Unable to wipe android secure");
that3f7b1ac2014-12-30 11:30:13 +01001145 ret_val = false;
1146 break;
1147 } else {
1148 skip = true;
1149 }
1150 } else if (wipe_path == "DALVIK") {
1151 if (!PartitionManager.Wipe_Dalvik_Cache()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001152 gui_err("dalvik_wipe_err=Failed to wipe dalvik");
that3f7b1ac2014-12-30 11:30:13 +01001153 ret_val = false;
1154 break;
1155 } else {
1156 skip = true;
1157 }
1158 } else if (wipe_path == "INTERNAL") {
1159 if (!PartitionManager.Wipe_Media_From_Data()) {
1160 ret_val = false;
1161 break;
1162 } else {
1163 skip = true;
Dees_Troya13d74f2013-03-24 08:54:55 -05001164 }
1165 }
that3f7b1ac2014-12-30 11:30:13 +01001166 if (!skip) {
1167 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001168 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(wipe_path));
that3f7b1ac2014-12-30 11:30:13 +01001169 ret_val = false;
1170 break;
1171 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
1172 arg = wipe_path;
1173 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001174 } else {
that3f7b1ac2014-12-30 11:30:13 +01001175 skip = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001176 }
that3f7b1ac2014-12-30 11:30:13 +01001177 start_pos = end_pos + 1;
1178 end_pos = Wipe_List.find(";", start_pos);
Dees_Troy51a0e822012-09-05 15:24:24 -04001179 }
1180 }
that3f7b1ac2014-12-30 11:30:13 +01001181 } else
1182 ret_val = PartitionManager.Wipe_By_Path(arg);
that73a52952015-01-28 23:07:34 +01001183#ifndef TW_OEM_BUILD
that3f7b1ac2014-12-30 11:30:13 +01001184 if (arg == DataManager::GetSettingsStoragePath()) {
1185 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
1186 string Storage_Path = DataManager::GetSettingsStoragePath();
Dees_Troy51a0e822012-09-05 15:24:24 -04001187
that3f7b1ac2014-12-30 11:30:13 +01001188 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
1189 LOGINFO("Making TWRP folder and saving settings.\n");
1190 Storage_Path += "/TWRP";
1191 mkdir(Storage_Path.c_str(), 0777);
1192 DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001193 } else {
that3f7b1ac2014-12-30 11:30:13 +01001194 LOGERR("Unable to recreate TWRP folder and save settings.\n");
1195 }
1196 }
1197#endif
1198 }
1199 PartitionManager.Update_System_Details();
1200 if (ret_val)
1201 ret_val = 0; // 0 is success
1202 else
1203 ret_val = 1; // 1 is failure
1204 operation_end(ret_val);
1205 return 0;
1206}
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001207
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001208int GUIAction::refreshsizes(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001209{
1210 operation_start("Refreshing Sizes");
1211 if (simulate) {
1212 simulate_progress_bar();
1213 } else
1214 PartitionManager.Update_System_Details();
1215 operation_end(0);
1216 return 0;
1217}
1218
1219int GUIAction::nandroid(std::string arg)
1220{
that3f7b1ac2014-12-30 11:30:13 +01001221 if (simulate) {
that73a52952015-01-28 23:07:34 +01001222 PartitionManager.stop_backup.set_value(0);
that3f7b1ac2014-12-30 11:30:13 +01001223 DataManager::SetValue("tw_partition", "Simulation");
1224 simulate_progress_bar();
that73a52952015-01-28 23:07:34 +01001225 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001226 } else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001227 operation_start("Nandroid");
1228 int ret = 0;
1229
that3f7b1ac2014-12-30 11:30:13 +01001230 if (arg == "backup") {
1231 string Backup_Name;
1232 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Ethan Yonker4adc33e2016-01-22 16:07:11 -06001233 string auto_gen = gui_lookup("auto_generate", "(Auto Generate)");
Ethan Yonker53796e72019-01-11 22:49:52 -06001234 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 -05001235 ret = PartitionManager.Run_Backup(false);
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001236 DataManager::SetValue("tw_encrypt_backup", 0); // reset value so we don't encrypt every subsequent backup
1237 if (!PartitionManager.stop_backup.get_value()) {
1238 if (ret == false)
1239 ret = 1; // 1 for failure
1240 else
1241 ret = 0; // 0 for success
1242 DataManager::SetValue("tw_cancel_backup", 0);
1243 } else {
1244 DataManager::SetValue("tw_cancel_backup", 1);
1245 gui_msg("backup_cancel=Backup Cancelled");
1246 ret = 0;
1247 }
bigbiffce8f83c2015-12-12 18:30:21 -05001248 } else {
that3f7b1ac2014-12-30 11:30:13 +01001249 operation_end(1);
1250 return -1;
that3f7b1ac2014-12-30 11:30:13 +01001251 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001252 DataManager::SetValue(TW_BACKUP_NAME, auto_gen);
that3f7b1ac2014-12-30 11:30:13 +01001253 } else if (arg == "restore") {
1254 string Restore_Name;
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001255 int gui_adb_backup;
1256
that3f7b1ac2014-12-30 11:30:13 +01001257 DataManager::GetValue("tw_restore", Restore_Name);
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001258 DataManager::GetValue("tw_enable_adb_backup", gui_adb_backup);
1259 if (gui_adb_backup) {
1260 DataManager::SetValue("tw_operation_state", 1);
1261 if (TWFunc::stream_adb_backup(Restore_Name) == 0)
1262 ret = 0; // success
1263 else
1264 ret = 1; // failure
1265 DataManager::SetValue("tw_enable_adb_backup", 0);
1266 ret = 0; // assume success???
1267 } else {
1268 if (PartitionManager.Run_Restore(Restore_Name))
1269 ret = 0; // success
1270 else
1271 ret = 1; // failure
1272 }
that3f7b1ac2014-12-30 11:30:13 +01001273 } else {
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001274 operation_end(1); // invalid arg specified, fail
bigbiff7abc5fe2015-01-17 16:53:12 -05001275 return -1;
1276 }
that73a52952015-01-28 23:07:34 +01001277 operation_end(ret);
bigbiff7abc5fe2015-01-17 16:53:12 -05001278 return ret;
1279 }
1280 return 0;
1281}
1282
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001283int GUIAction::cancelbackup(std::string arg __unused) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001284 if (simulate) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001285 PartitionManager.stop_backup.set_value(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001286 }
1287 else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001288 int op_status = PartitionManager.Cancel_Backup();
1289 if (op_status != 0)
1290 op_status = 1; // failure
bigbiff7abc5fe2015-01-17 16:53:12 -05001291 }
1292
1293 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001294}
Dees_Troy51a0e822012-09-05 15:24:24 -04001295
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001296int GUIAction::fixcontexts(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001297{
that73a52952015-01-28 23:07:34 +01001298 int op_status = 0;
1299
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001300 operation_start("Fix Contexts");
1301 LOGINFO("fix contexts started!\n");
that3f7b1ac2014-12-30 11:30:13 +01001302 if (simulate) {
1303 simulate_progress_bar();
1304 } else {
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001305 op_status = PartitionManager.Fix_Contexts();
that3f7b1ac2014-12-30 11:30:13 +01001306 if (op_status != 0)
1307 op_status = 1; // failure
that3f7b1ac2014-12-30 11:30:13 +01001308 }
that73a52952015-01-28 23:07:34 +01001309 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001310 return 0;
1311}
1312
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001313int GUIAction::fixpermissions(std::string arg)
1314{
1315 return fixcontexts(arg);
1316}
1317
that3f7b1ac2014-12-30 11:30:13 +01001318int GUIAction::dd(std::string arg)
1319{
1320 operation_start("imaging");
1321
1322 if (simulate) {
1323 simulate_progress_bar();
1324 } else {
1325 string cmd = "dd " + arg;
1326 TWFunc::Exec_Cmd(cmd);
1327 }
1328 operation_end(0);
1329 return 0;
1330}
1331
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001332int GUIAction::partitionsd(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001333{
1334 operation_start("Partition SD Card");
1335 int ret_val = 0;
1336
1337 if (simulate) {
1338 simulate_progress_bar();
1339 } else {
1340 int allow_partition;
1341 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1342 if (allow_partition == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001343 gui_err("no_real_sdcard=This device does not have a real SD Card! Aborting!");
that3f7b1ac2014-12-30 11:30:13 +01001344 } else {
1345 if (!PartitionManager.Partition_SDCard())
1346 ret_val = 1; // failed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001347 }
that3f7b1ac2014-12-30 11:30:13 +01001348 }
1349 operation_end(ret_val);
1350 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001351
that3f7b1ac2014-12-30 11:30:13 +01001352}
Dees_Troy51a0e822012-09-05 15:24:24 -04001353
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001354int GUIAction::installhtcdumlock(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001355{
1356 operation_start("Install HTC Dumlock");
1357 if (simulate) {
1358 simulate_progress_bar();
1359 } else
1360 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -04001361
that3f7b1ac2014-12-30 11:30:13 +01001362 operation_end(0);
1363 return 0;
1364}
Dees_Troy51a0e822012-09-05 15:24:24 -04001365
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001366int GUIAction::htcdumlockrestoreboot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001367{
1368 operation_start("HTC Dumlock Restore Boot");
1369 if (simulate) {
1370 simulate_progress_bar();
1371 } else
1372 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001373
that3f7b1ac2014-12-30 11:30:13 +01001374 operation_end(0);
1375 return 0;
1376}
Dees_Troy51a0e822012-09-05 15:24:24 -04001377
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001378int GUIAction::htcdumlockreflashrecovery(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001379{
1380 operation_start("HTC Dumlock Reflash Recovery");
1381 if (simulate) {
1382 simulate_progress_bar();
1383 } else
1384 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001385
that3f7b1ac2014-12-30 11:30:13 +01001386 operation_end(0);
1387 return 0;
1388}
Dees_Troy51a0e822012-09-05 15:24:24 -04001389
that3f7b1ac2014-12-30 11:30:13 +01001390int GUIAction::cmd(std::string arg)
1391{
1392 int op_status = 0;
1393
1394 operation_start("Command");
1395 LOGINFO("Running command: '%s'\n", arg.c_str());
1396 if (simulate) {
1397 simulate_progress_bar();
1398 } else {
1399 op_status = TWFunc::Exec_Cmd(arg);
1400 if (op_status != 0)
1401 op_status = 1;
1402 }
1403
1404 operation_end(op_status);
1405 return 0;
1406}
1407
1408int GUIAction::terminalcommand(std::string arg)
1409{
1410 int op_status = 0;
1411 string cmdpath, command;
1412
1413 DataManager::GetValue("tw_terminal_location", cmdpath);
1414 operation_start("CommandOutput");
1415 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
1416 if (simulate) {
1417 simulate_progress_bar();
1418 operation_end(op_status);
Matt Mower5aa29ab2015-02-25 23:50:55 -06001419 } else if (arg == "exit") {
1420 LOGINFO("Exiting terminal\n");
1421 operation_end(op_status);
1422 page("main");
that3f7b1ac2014-12-30 11:30:13 +01001423 } else {
1424 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
1425 LOGINFO("Actual command is: '%s'\n", command.c_str());
that3f7b1ac2014-12-30 11:30:13 +01001426 DataManager::SetValue("tw_terminal_state", 1);
1427 DataManager::SetValue("tw_background_thread_running", 1);
thatc6085482015-01-09 22:12:43 +01001428 FILE* fp;
1429 char line[512];
1430
1431 fp = popen(command.c_str(), "r");
1432 if (fp == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001433 LOGERR("Error opening command to run (%s).\n", strerror(errno));
thatc6085482015-01-09 22:12:43 +01001434 } else {
Matt Mower23d8aae2017-01-06 14:30:33 -06001435 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1;
thatc6085482015-01-09 22:12:43 +01001436 struct timeval timeout;
1437 fd_set fdset;
1438
Matt Mowera8a89d12016-12-30 18:10:37 -06001439 while (keep_going)
thatc6085482015-01-09 22:12:43 +01001440 {
1441 FD_ZERO(&fdset);
1442 FD_SET(fd, &fdset);
1443 timeout.tv_sec = 0;
1444 timeout.tv_usec = 400000;
1445 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1446 if (has_data == 0) {
1447 // Timeout reached
1448 DataManager::GetValue("tw_terminal_state", check);
1449 if (check == 0) {
1450 keep_going = 0;
1451 }
1452 } else if (has_data < 0) {
1453 // End of execution
1454 keep_going = 0;
1455 } else {
1456 // Try to read output
Matt Mowera8a89d12016-12-30 18:10:37 -06001457 if (fgets(line, sizeof(line), fp) != NULL)
thatc6085482015-01-09 22:12:43 +01001458 gui_print("%s", line); // Display output
1459 else
1460 keep_going = 0; // Done executing
1461 }
1462 }
1463 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -04001464 }
thatc6085482015-01-09 22:12:43 +01001465 DataManager::SetValue("tw_operation_status", 0);
1466 DataManager::SetValue("tw_operation_state", 1);
1467 DataManager::SetValue("tw_terminal_state", 0);
1468 DataManager::SetValue("tw_background_thread_running", 0);
1469 DataManager::SetValue(TW_ACTION_BUSY, 0);
that3f7b1ac2014-12-30 11:30:13 +01001470 }
1471 return 0;
1472}
1473
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001474int GUIAction::killterminal(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001475{
that3f7b1ac2014-12-30 11:30:13 +01001476 LOGINFO("Sending kill command...\n");
1477 operation_start("KillCommand");
1478 DataManager::SetValue("tw_operation_status", 0);
1479 DataManager::SetValue("tw_operation_state", 1);
1480 DataManager::SetValue("tw_terminal_state", 0);
1481 DataManager::SetValue("tw_background_thread_running", 0);
1482 DataManager::SetValue(TW_ACTION_BUSY, 0);
1483 return 0;
1484}
1485
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001486int GUIAction::reinjecttwrp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001487{
1488 int op_status = 0;
1489 operation_start("ReinjectTWRP");
Ethan Yonker74db1572015-10-28 12:44:49 -05001490 gui_msg("injecttwrp=Injecting TWRP into boot image...");
that3f7b1ac2014-12-30 11:30:13 +01001491 if (simulate) {
1492 simulate_progress_bar();
1493 } else {
1494 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Ethan Yonker74db1572015-10-28 12:44:49 -05001495 gui_msg("done=Done.");
that3f7b1ac2014-12-30 11:30:13 +01001496 }
1497
1498 operation_end(op_status);
1499 return 0;
1500}
1501
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001502int GUIAction::checkbackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001503{
1504 int op_status = 0;
1505
1506 operation_start("CheckBackupName");
1507 if (simulate) {
1508 simulate_progress_bar();
1509 } else {
Ethan Yonker53796e72019-01-11 22:49:52 -06001510 string Backup_Name;
1511 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
1512 op_status = PartitionManager.Check_Backup_Name(Backup_Name, true, true);
that3f7b1ac2014-12-30 11:30:13 +01001513 if (op_status != 0)
1514 op_status = 1;
1515 }
1516
1517 operation_end(op_status);
1518 return 0;
1519}
1520
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001521int GUIAction::decrypt(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001522{
1523 int op_status = 0;
1524
1525 operation_start("Decrypt");
1526 if (simulate) {
1527 simulate_progress_bar();
1528 } else {
1529 string Password;
1530 DataManager::GetValue("tw_crypto_password", Password);
bigbiffadc599e2020-05-28 19:36:30 +00001531 op_status = PartitionManager.Decrypt_Device(Password);
that3f7b1ac2014-12-30 11:30:13 +01001532 if (op_status != 0)
1533 op_status = 1;
1534 else {
bigbiffadc599e2020-05-28 19:36:30 +00001535
that3f7b1ac2014-12-30 11:30:13 +01001536 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1537
Ethan Yonkercf50da52015-01-12 21:59:07 -06001538 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001539
Ethan Yonkercf50da52015-01-12 21:59:07 -06001540 // Check for a custom theme and load it if exists
1541 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1542 if (has_datamedia != 0) {
1543 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06001544 LOGINFO("Failed to get default contexts and file mode for storage files.\n");
Ethan Yonkercf50da52015-01-12 21:59:07 -06001545 } else {
1546 LOGINFO("Got default contexts and file mode for storage files.\n");
that3f7b1ac2014-12-30 11:30:13 +01001547 }
1548 }
Ethan Yonker66a19492015-12-10 10:19:45 -06001549 PartitionManager.Decrypt_Adopted();
that3f7b1ac2014-12-30 11:30:13 +01001550 }
1551 }
1552
1553 operation_end(op_status);
1554 return 0;
1555}
1556
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001557int GUIAction::adbsideload(std::string arg __unused)
thatcc8ddca2015-01-03 01:59:36 +01001558{
1559 operation_start("Sideload");
1560 if (simulate) {
1561 simulate_progress_bar();
1562 operation_end(0);
1563 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001564 gui_msg("start_sideload=Starting ADB sideload feature...");
thatc6085482015-01-09 22:12:43 +01001565 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
1566
1567 // wait for the adb connection
bigbiffd58ba182020-03-23 10:02:29 -04001568 // int ret = apply_from_adb("/", &sideload_child_pid);
1569 Device::BuiltinAction reboot_action = Device::REBOOT_BOOTLOADER;
1570 int ret = ApplyFromAdb("/", &reboot_action);
thatc6085482015-01-09 22:12:43 +01001571 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
1572
1573 if (ret != 0) {
1574 if (ret == -2)
Ethan Yonker74db1572015-10-28 12:44:49 -05001575 gui_msg("need_new_adb=You need adb 1.0.32 or newer to sideload to this device.");
thatc6085482015-01-09 22:12:43 +01001576 ret = 1; // failure
1577 } else {
1578 int wipe_cache = 0;
1579 int wipe_dalvik = 0;
1580 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
1581
1582 if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) {
1583 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1584 PartitionManager.Wipe_By_Path("/cache");
1585 if (wipe_dalvik)
1586 PartitionManager.Wipe_Dalvik_Cache();
1587 } else {
1588 ret = 1; // failure
1589 }
thatcc8ddca2015-01-03 01:59:36 +01001590 }
thatc6085482015-01-09 22:12:43 +01001591 if (sideload_child_pid) {
1592 LOGINFO("Signaling child sideload process to exit.\n");
1593 struct stat st;
1594 // Calling stat() on this magic filename signals the minadbd
1595 // subprocess to shut down.
1596 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1597 int status;
1598 LOGINFO("Waiting for child sideload process to exit.\n");
1599 waitpid(sideload_child_pid, &status, 0);
1600 }
Dees Troy28a85d22015-04-28 02:03:16 +00001601 property_set("ctl.start", "adbd");
thatc6085482015-01-09 22:12:43 +01001602 TWFunc::Toggle_MTP(mtp_was_enabled);
1603 reinject_after_flash();
1604 operation_end(ret);
thatcc8ddca2015-01-03 01:59:36 +01001605 }
that3f7b1ac2014-12-30 11:30:13 +01001606 return 0;
1607}
1608
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001609int GUIAction::adbsideloadcancel(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001610{
that3f7b1ac2014-12-30 11:30:13 +01001611 struct stat st;
1612 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
Ethan Yonker74db1572015-10-28 12:44:49 -05001613 gui_msg("cancel_sideload=Cancelling ADB sideload...");
thatcc8ddca2015-01-03 01:59:36 +01001614 LOGINFO("Signaling child sideload process to exit.\n");
1615 // Calling stat() on this magic filename signals the minadbd
1616 // subprocess to shut down.
1617 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1618 if (!sideload_child_pid) {
1619 LOGERR("Unable to get child ID\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001620 return 0;
1621 }
thatcc8ddca2015-01-03 01:59:36 +01001622 ::sleep(1);
1623 LOGINFO("Killing child sideload process.\n");
1624 kill(sideload_child_pid, SIGTERM);
1625 int status;
1626 LOGINFO("Waiting for child sideload process to exit.\n");
1627 waitpid(sideload_child_pid, &status, 0);
1628 sideload_child_pid = 0;
that3f7b1ac2014-12-30 11:30:13 +01001629 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
1630 return 0;
1631}
1632
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001633int GUIAction::openrecoveryscript(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001634{
1635 operation_start("OpenRecoveryScript");
1636 if (simulate) {
1637 simulate_progress_bar();
Ethan Yonker24e7eb72015-01-03 16:13:06 -06001638 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001639 } else {
that10ae24f2015-12-26 20:53:51 +01001640 int op_status = OpenRecoveryScript::Run_OpenRecoveryScript_Action();
Ethan Yonkere1abe612015-06-09 11:09:32 -05001641 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001642 }
1643 return 0;
1644}
1645
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001646int GUIAction::installsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001647{
1648 int op_status = 0;
1649
1650 operation_start("Install SuperSU");
1651 if (simulate) {
1652 simulate_progress_bar();
1653 } else {
Ethan Yonkerfa67cbf2018-07-20 12:22:33 -05001654 LOGERR("Installing SuperSU was deprecated from TWRP.\n");
that3f7b1ac2014-12-30 11:30:13 +01001655 }
1656
1657 operation_end(op_status);
1658 return 0;
1659}
1660
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001661int GUIAction::fixsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001662{
1663 int op_status = 0;
1664
1665 operation_start("Fixing Superuser Permissions");
1666 if (simulate) {
1667 simulate_progress_bar();
1668 } else {
1669 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1670 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
1671 }
1672
1673 operation_end(op_status);
1674 return 0;
1675}
1676
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001677int GUIAction::decrypt_backup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001678{
1679 int op_status = 0;
1680
1681 operation_start("Try Restore Decrypt");
1682 if (simulate) {
1683 simulate_progress_bar();
1684 } else {
1685 string Restore_Path, Filename, Password;
1686 DataManager::GetValue("tw_restore", Restore_Path);
1687 Restore_Path += "/";
1688 DataManager::GetValue("tw_restore_password", Password);
1689 TWFunc::SetPerformanceMode(true);
1690 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1691 op_status = 0; // success
1692 else
1693 op_status = 1; // fail
1694 TWFunc::SetPerformanceMode(false);
1695 }
1696
1697 operation_end(op_status);
1698 return 0;
1699}
1700
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001701int GUIAction::repair(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001702{
1703 int op_status = 0;
1704
1705 operation_start("Repair Partition");
1706 if (simulate) {
1707 simulate_progress_bar();
1708 } else {
1709 string part_path;
1710 DataManager::GetValue("tw_partition_mount_point", part_path);
1711 if (PartitionManager.Repair_By_Path(part_path, true)) {
1712 op_status = 0; // success
1713 } else {
that3f7b1ac2014-12-30 11:30:13 +01001714 op_status = 1; // fail
1715 }
1716 }
1717
1718 operation_end(op_status);
1719 return 0;
1720}
1721
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001722int GUIAction::resize(std::string arg __unused)
Ethan Yonkera2719152015-05-28 09:44:41 -05001723{
1724 int op_status = 0;
1725
1726 operation_start("Resize Partition");
1727 if (simulate) {
1728 simulate_progress_bar();
1729 } else {
1730 string part_path;
1731 DataManager::GetValue("tw_partition_mount_point", part_path);
1732 if (PartitionManager.Resize_By_Path(part_path, true)) {
1733 op_status = 0; // success
1734 } else {
Ethan Yonkera2719152015-05-28 09:44:41 -05001735 op_status = 1; // fail
1736 }
1737 }
1738
1739 operation_end(op_status);
1740 return 0;
1741}
1742
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001743int GUIAction::changefilesystem(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001744{
1745 int op_status = 0;
1746
1747 operation_start("Change File System");
1748 if (simulate) {
1749 simulate_progress_bar();
1750 } else {
1751 string part_path, file_system;
1752 DataManager::GetValue("tw_partition_mount_point", part_path);
1753 DataManager::GetValue("tw_action_new_file_system", file_system);
1754 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1755 op_status = 0; // success
1756 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001757 gui_err("change_fs_err=Error changing file system.");
that3f7b1ac2014-12-30 11:30:13 +01001758 op_status = 1; // fail
1759 }
1760 }
1761 PartitionManager.Update_System_Details();
1762 operation_end(op_status);
1763 return 0;
1764}
1765
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001766int GUIAction::startmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001767{
1768 int op_status = 0;
1769
1770 operation_start("Start MTP");
1771 if (PartitionManager.Enable_MTP())
1772 op_status = 0; // success
1773 else
1774 op_status = 1; // fail
1775
1776 operation_end(op_status);
1777 return 0;
1778}
1779
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001780int GUIAction::stopmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001781{
1782 int op_status = 0;
1783
1784 operation_start("Stop MTP");
1785 if (PartitionManager.Disable_MTP())
1786 op_status = 0; // success
1787 else
1788 op_status = 1; // fail
1789
1790 operation_end(op_status);
1791 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001792}
1793
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001794int GUIAction::flashimage(std::string arg __unused)
Ethan Yonker96af84a2015-01-05 14:58:36 -06001795{
1796 int op_status = 0;
1797
1798 operation_start("Flash Image");
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001799 string path, filename;
1800 DataManager::GetValue("tw_zip_location", path);
1801 DataManager::GetValue("tw_file", filename);
Ethan Yonkere080c1f2016-09-19 13:50:25 -05001802 if (PartitionManager.Flash_Image(path, filename))
Ethan Yonker96af84a2015-01-05 14:58:36 -06001803 op_status = 0; // success
1804 else
1805 op_status = 1; // fail
1806
1807 operation_end(op_status);
1808 return 0;
1809}
1810
that10ae24f2015-12-26 20:53:51 +01001811int GUIAction::twcmd(std::string arg)
1812{
1813 operation_start("TWRP CLI Command");
1814 if (simulate)
1815 simulate_progress_bar();
1816 else
1817 OpenRecoveryScript::Run_CLI_Command(arg.c_str());
1818 operation_end(0);
1819 return 0;
1820}
1821
Dees_Troy51a0e822012-09-05 15:24:24 -04001822int GUIAction::getKeyByName(std::string key)
1823{
that8834a0f2016-01-05 23:29:30 +01001824 if (key == "home") return KEY_HOMEPAGE; // note: KEY_HOME is cursor movement (like KEY_END)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001825 else if (key == "menu") return KEY_MENU;
1826 else if (key == "back") return KEY_BACK;
1827 else if (key == "search") return KEY_SEARCH;
1828 else if (key == "voldown") return KEY_VOLUMEDOWN;
1829 else if (key == "volup") return KEY_VOLUMEUP;
1830 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001831 int ret_val;
1832 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1833 if (!ret_val)
1834 return KEY_POWER;
1835 else
1836 return ret_val;
1837 }
1838
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001839 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001840}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001841
1842int GUIAction::checkpartitionlifetimewrites(std::string arg)
1843{
1844 int op_status = 0;
1845 TWPartition* sys = PartitionManager.Find_Partition_By_Path(arg);
1846
1847 operation_start("Check Partition Lifetime Writes");
1848 if (sys) {
1849 if (sys->Check_Lifetime_Writes() != 0)
1850 DataManager::SetValue("tw_lifetime_writes", 1);
1851 else
1852 DataManager::SetValue("tw_lifetime_writes", 0);
1853 op_status = 0; // success
1854 } else {
1855 DataManager::SetValue("tw_lifetime_writes", 1);
1856 op_status = 1; // fail
1857 }
1858
1859 operation_end(op_status);
1860 return 0;
1861}
1862
1863int GUIAction::mountsystemtoggle(std::string arg)
1864{
1865 int op_status = 0;
Captain Throwback9d6feb52018-07-27 10:05:24 -04001866 bool remount_system = PartitionManager.Is_Mounted_By_Path(PartitionManager.Get_Android_Root_Path());
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001867 bool remount_vendor = PartitionManager.Is_Mounted_By_Path("/vendor");
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001868
1869 operation_start("Toggle System Mount");
Captain Throwback9d6feb52018-07-27 10:05:24 -04001870 if (!PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001871 op_status = 1; // fail
1872 } else {
Captain Throwback9d6feb52018-07-27 10:05:24 -04001873 TWPartition* Part = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001874 if (Part) {
Ethan Yonkerd6966f42015-05-30 14:52:16 -05001875 if (arg == "0") {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001876 DataManager::SetValue("tw_mount_system_ro", 0);
1877 Part->Change_Mount_Read_Only(false);
1878 } else {
1879 DataManager::SetValue("tw_mount_system_ro", 1);
1880 Part->Change_Mount_Read_Only(true);
1881 }
1882 if (remount_system) {
1883 Part->Mount(true);
1884 }
1885 op_status = 0; // success
1886 } else {
1887 op_status = 1; // fail
1888 }
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001889 Part = PartitionManager.Find_Partition_By_Path("/vendor");
1890 if (Part) {
1891 if (arg == "0") {
1892 Part->Change_Mount_Read_Only(false);
1893 } else {
1894 Part->Change_Mount_Read_Only(true);
1895 }
1896 if (remount_vendor) {
1897 Part->Mount(true);
1898 }
1899 op_status = 0; // success
1900 } else {
1901 op_status = 1; // fail
1902 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001903 }
1904
1905 operation_end(op_status);
1906 return 0;
1907}
Ethan Yonker74db1572015-10-28 12:44:49 -05001908
1909int GUIAction::setlanguage(std::string arg __unused)
1910{
1911 int op_status = 0;
1912
1913 operation_start("Set Language");
1914 PageManager::LoadLanguage(DataManager::GetStrValue("tw_language"));
1915 PageManager::RequestReload();
1916 op_status = 0; // success
1917
1918 operation_end(op_status);
1919 return 0;
1920}
Ethan Yonker1b190162016-12-05 15:25:19 -06001921
Matt Mower9472ba12016-01-20 18:12:47 -06001922int GUIAction::togglebacklight(std::string arg __unused)
1923{
1924 blankTimer.toggleBlank();
1925 return 0;
1926}
1927
Ethan Yonker1b190162016-12-05 15:25:19 -06001928int GUIAction::setbootslot(std::string arg)
1929{
1930 operation_start("Set Boot Slot");
1931 if (!simulate)
Ethan Yonker1b190162016-12-05 15:25:19 -06001932 PartitionManager.Set_Active_Slot(arg);
Matt Mower029a82d2017-01-08 13:12:38 -06001933 else
Ethan Yonker1b190162016-12-05 15:25:19 -06001934 simulate_progress_bar();
1935 operation_end(0);
1936 return 0;
1937}
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001938
1939int GUIAction::checkforapp(std::string arg __unused)
1940{
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001941 operation_start("Check for TWRP App");
1942 if (!simulate)
1943 {
1944 string sdkverstr = TWFunc::System_Property_Get("ro.build.version.sdk");
1945 int sdkver = 0;
1946 if (!sdkverstr.empty()) {
1947 sdkver = atoi(sdkverstr.c_str());
1948 }
1949 if (sdkver <= 13) {
1950 if (sdkver == 0)
1951 LOGINFO("Unable to read sdk version from build prop\n");
1952 else
1953 LOGINFO("SDK version too low for TWRP app (%i < 14)\n", sdkver);
Ethan Yonker64c5c0a2017-03-06 12:42:54 -06001954 DataManager::SetValue("tw_app_install_status", 1); // 0 = no status, 1 = not installed, 2 = already installed or do not install
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001955 goto exit;
1956 }
Ethan Yonker76bbd3a2019-05-10 10:50:04 -05001957 if (TWFunc::Is_TWRP_App_In_System()) {
1958 DataManager::SetValue("tw_app_install_status", 2); // 0 = no status, 1 = not installed, 2 = already installed or do not install
1959 goto exit;
Ethan Yonker5e1a7f92017-01-18 16:44:54 -06001960 }
1961 if (PartitionManager.Mount_By_Path("/data", false)) {
Ethan Yonker5128d292017-02-04 19:52:56 -06001962 const char parent_path[] = "/data/app";
1963 const char app_prefix[] = "me.twrp.twrpapp-";
1964 DIR *d = opendir(parent_path);
1965 if (d) {
1966 struct dirent *p;
1967 while ((p = readdir(d))) {
1968 if (p->d_type != DT_DIR || strlen(p->d_name) < strlen(app_prefix) || strncmp(p->d_name, app_prefix, strlen(app_prefix)))
1969 continue;
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001970 closedir(d);
Ethan Yonker5128d292017-02-04 19:52:56 -06001971 LOGINFO("App found at '%s/%s'\n", parent_path, p->d_name);
Ethan Yonker64c5c0a2017-03-06 12:42:54 -06001972 DataManager::SetValue("tw_app_install_status", 2); // 0 = no status, 1 = not installed, 2 = already installed or do not install
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001973 goto exit;
1974 }
Ethan Yonker5128d292017-02-04 19:52:56 -06001975 closedir(d);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001976 }
Ethan Yonker64c5c0a2017-03-06 12:42:54 -06001977 } else {
1978 LOGINFO("Data partition cannot be mounted during app check\n");
1979 DataManager::SetValue("tw_app_install_status", 2); // 0 = no status, 1 = not installed, 2 = already installed or do not install
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001980 }
1981 } else
1982 simulate_progress_bar();
1983 LOGINFO("App not installed\n");
1984 DataManager::SetValue("tw_app_install_status", 1); // 0 = no status, 1 = not installed, 2 = already installed
1985exit:
1986 operation_end(0);
1987 return 0;
1988}
1989
1990int GUIAction::installapp(std::string arg __unused)
1991{
1992 int op_status = 1;
1993 operation_start("Install TWRP App");
1994 if (!simulate)
1995 {
1996 if (DataManager::GetIntValue("tw_mount_system_ro") > 0 || DataManager::GetIntValue("tw_app_install_system") == 0) {
1997 if (PartitionManager.Mount_By_Path("/data", true)) {
1998 string install_path = "/data/app";
1999 string context = "u:object_r:apk_data_file:s0";
2000 if (!TWFunc::Path_Exists(install_path)) {
2001 if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
2002 LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno));
2003 goto exit;
2004 }
2005 if (chown(install_path.c_str(), 1000, 1000)) {
2006 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
2007 goto exit;
2008 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002009 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002010 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2011 goto exit;
2012 }
2013 }
2014 install_path += "/me.twrp.twrpapp-1";
2015 if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
2016 LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno));
2017 goto exit;
2018 }
2019 if (chown(install_path.c_str(), 1000, 1000)) {
2020 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
2021 goto exit;
2022 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002023 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002024 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2025 goto exit;
2026 }
2027 install_path += "/base.apk";
2028 if (TWFunc::copy_file("/sbin/me.twrp.twrpapp.apk", install_path, 0644)) {
2029 LOGERR("Error copying apk file\n");
2030 goto exit;
2031 }
2032 if (chown(install_path.c_str(), 1000, 1000)) {
2033 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
2034 goto exit;
2035 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002036 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002037 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2038 goto exit;
2039 }
2040 sync();
2041 sync();
2042 }
2043 } else {
Captain Throwback9d6feb52018-07-27 10:05:24 -04002044 if (PartitionManager.Mount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) {
2045 string base_path = PartitionManager.Get_Android_Root_Path();
2046 if (TWFunc::Path_Exists(PartitionManager.Get_Android_Root_Path() + "/system"))
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002047 base_path += "/system"; // For devices with system as a root file system (e.g. Pixel)
2048 string install_path = base_path + "/priv-app";
2049 string context = "u:object_r:system_file:s0";
2050 if (!TWFunc::Path_Exists(install_path))
2051 install_path = base_path + "/app";
2052 if (TWFunc::Path_Exists(install_path)) {
2053 install_path += "/twrpapp";
2054 LOGINFO("Installing app to '%s'\n", install_path.c_str());
2055 if (mkdir(install_path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0) {
Ethan Yonker4767caf2017-01-11 10:45:04 -06002056 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002057 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2058 goto exit;
2059 }
2060 install_path += "/me.twrp.twrpapp.apk";
2061 if (TWFunc::copy_file("/sbin/me.twrp.twrpapp.apk", install_path, 0644)) {
2062 LOGERR("Error copying apk file\n");
2063 goto exit;
2064 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002065 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002066 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2067 goto exit;
2068 }
Stefan Tauner6ad14572020-01-11 22:28:53 +01002069
2070 // System apps require their permissions to be pre-set via an XML file in /etc/permissions
2071 string permission_path = base_path + "/etc/permissions/privapp-permissions-twrpapp.xml";
2072 if (TWFunc::copy_file("/sbin/privapp-permissions-twrpapp.xml", permission_path, 0644)) {
2073 LOGERR("Error copying permission file\n");
2074 goto exit;
2075 }
2076 if (chown(permission_path.c_str(), 1000, 1000)) {
2077 LOGERR("chown %s error: %s\n", permission_path.c_str(), strerror(errno));
2078 goto exit;
2079 }
2080 if (setfilecon(permission_path.c_str(), (security_context_t)context.c_str()) < 0) {
2081 LOGERR("setfilecon %s error: %s\n", permission_path.c_str(), strerror(errno));
2082 goto exit;
2083 }
2084
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002085 sync();
2086 sync();
Captain Throwback9d6feb52018-07-27 10:05:24 -04002087 PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002088 op_status = 0;
2089 } else {
2090 LOGERR("Error making app directory '%s': %s\n", strerror(errno));
2091 }
2092 }
2093 }
2094 }
2095 } else
2096 simulate_progress_bar();
2097exit:
2098 operation_end(0);
2099 return 0;
2100}
Ethan Yonker53796e72019-01-11 22:49:52 -06002101
Ethan Yonker76bbd3a2019-05-10 10:50:04 -05002102int GUIAction::uninstalltwrpsystemapp(std::string arg __unused)
2103{
2104 int op_status = 1;
2105 operation_start("Uninstall TWRP System App");
2106 if (!simulate)
2107 {
2108 int Mount_System_RO = DataManager::GetIntValue("tw_mount_system_ro");
2109 TWPartition* Part = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path());
2110 if (!Part) {
2111 LOGERR("Unabled to find system partition.\n");
2112 goto exit;
2113 }
2114 if (!Part->UnMount(true)) {
2115 goto exit;
2116 }
2117 if (Mount_System_RO > 0) {
2118 DataManager::SetValue("tw_mount_system_ro", 0);
2119 Part->Change_Mount_Read_Only(false);
2120 }
2121 if (Part->Mount(true)) {
2122 string base_path = PartitionManager.Get_Android_Root_Path();
2123 if (TWFunc::Path_Exists(PartitionManager.Get_Android_Root_Path() + "/system"))
2124 base_path += "/system"; // For devices with system as a root file system (e.g. Pixel)
2125 string uninstall_path = base_path + "/priv-app";
2126 if (!TWFunc::Path_Exists(uninstall_path))
2127 uninstall_path = base_path + "/app";
2128 uninstall_path += "/twrpapp";
2129 if (TWFunc::Path_Exists(uninstall_path)) {
2130 LOGINFO("Uninstalling TWRP App from '%s'\n", uninstall_path.c_str());
2131 if (TWFunc::removeDir(uninstall_path, false) == 0) {
2132 sync();
2133 op_status = 0;
2134 DataManager::SetValue("tw_app_installed_in_system", 0);
2135 DataManager::SetValue("tw_app_install_status", 0);
2136 } else {
2137 LOGERR("Unable to remove TWRP app from system.\n");
2138 }
2139 } else {
2140 LOGINFO("didn't find TWRP app in '%s'\n", uninstall_path.c_str());
2141 }
2142 }
2143 Part->UnMount(true);
2144 if (Mount_System_RO > 0) {
2145 DataManager::SetValue("tw_mount_system_ro", Mount_System_RO);
2146 Part->Change_Mount_Read_Only(true);
2147 }
2148 } else
2149 simulate_progress_bar();
2150exit:
2151 operation_end(0);
2152 return 0;
2153}
2154
Ethan Yonker53796e72019-01-11 22:49:52 -06002155int GUIAction::repackimage(std::string arg __unused)
2156{
2157 int op_status = 1;
2158 operation_start("Repack Image");
2159 if (!simulate)
2160 {
2161 std::string path = DataManager::GetStrValue("tw_filename");
2162 Repack_Options_struct Repack_Options;
2163 Repack_Options.Disable_Verity = false;
2164 Repack_Options.Disable_Force_Encrypt = false;
2165 Repack_Options.Backup_First = DataManager::GetIntValue("tw_repack_backup_first") != 0;
2166 if (DataManager::GetIntValue("tw_repack_kernel") == 1)
2167 Repack_Options.Type = REPLACE_KERNEL;
2168 else
2169 Repack_Options.Type = REPLACE_RAMDISK;
2170 if (!PartitionManager.Repack_Images(path, Repack_Options))
2171 goto exit;
2172 } else
2173 simulate_progress_bar();
2174 op_status = 0;
2175exit:
2176 operation_end(op_status);
2177 return 0;
2178}
2179
2180int GUIAction::fixabrecoverybootloop(std::string arg __unused)
2181{
2182 int op_status = 1;
2183 operation_start("Repack Image");
2184 if (!simulate)
2185 {
2186 if (!TWFunc::Path_Exists("/sbin/magiskboot")) {
2187 LOGERR("Image repacking tool not present in this TWRP build!");
2188 goto exit;
2189 }
2190 DataManager::SetProgress(0);
2191 TWPartition* part = PartitionManager.Find_Partition_By_Path("/boot");
2192 if (part)
2193 gui_msg(Msg("unpacking_image=Unpacking {1}...")(part->Display_Name));
2194 else {
2195 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")("/boot"));
2196 goto exit;
2197 }
2198 if (!PartitionManager.Prepare_Repack(part, REPACK_ORIG_DIR, DataManager::GetIntValue("tw_repack_backup_first") != 0, gui_lookup("repack", "Repack")))
2199 goto exit;
2200 DataManager::SetProgress(.25);
2201 gui_msg("fixing_recovery_loop_patch=Patching kernel...");
Mohd Farazb98b4f72020-05-10 04:18:30 +05302202 std::string command = "cd " REPACK_ORIG_DIR " && /sbin/magiskboot hexpatch kernel 77616E745F696E697472616D667300 736B69705F696E697472616D667300";
Ethan Yonker53796e72019-01-11 22:49:52 -06002203 if (TWFunc::Exec_Cmd(command) != 0) {
2204 gui_msg(Msg(msg::kError, "fix_recovery_loop_patch_error=Error patching kernel."));
2205 goto exit;
2206 }
2207 std::string header_path = REPACK_ORIG_DIR;
2208 header_path += "header";
2209 if (TWFunc::Path_Exists(header_path)) {
2210 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";
2211 if (TWFunc::Exec_Cmd(command) != 0) {
2212 gui_msg(Msg(msg::kError, "fix_recovery_loop_patch_error=Error patching kernel."));
2213 goto exit;
2214 }
2215 }
2216 DataManager::SetProgress(.5);
2217 gui_msg(Msg("repacking_image=Repacking {1}...")(part->Display_Name));
Mohd Farazb98b4f72020-05-10 04:18:30 +05302218 command = "cd " REPACK_ORIG_DIR " && /sbin/magiskboot repack " REPACK_ORIG_DIR "boot.img";
Ethan Yonker53796e72019-01-11 22:49:52 -06002219 if (TWFunc::Exec_Cmd(command) != 0) {
2220 gui_msg(Msg(msg::kError, "repack_error=Error repacking image."));
2221 goto exit;
2222 }
2223 DataManager::SetProgress(.75);
2224 std::string path = REPACK_ORIG_DIR;
2225 std::string file = "new-boot.img";
2226 DataManager::SetValue("tw_flash_partition", "/boot;");
2227 if (!PartitionManager.Flash_Image(path, file)) {
2228 LOGINFO("Error flashing new image\n");
2229 goto exit;
2230 }
2231 DataManager::SetProgress(1);
2232 TWFunc::removeDir(REPACK_ORIG_DIR, false);
2233 } else
2234 simulate_progress_bar();
2235 op_status = 0;
2236exit:
2237 operation_end(op_status);
2238 return 0;
2239}