blob: 65526b99375be6c795fc6f6cfc02b595fa1a99fb [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;
Noah Jacobson0b25b1d2019-04-28 00:10:07 -040067extern std::vector<users_struct> Users_List;
that3f7b1ac2014-12-30 11:30:13 +010068
that73a52952015-01-28 23:07:34 +010069static void *ActionThread_work_wrapper(void *data);
70
71class ActionThread
72{
73public:
74 ActionThread();
75 ~ActionThread();
76
77 void threadActions(GUIAction *act);
78 void run(void *data);
79private:
80 friend void *ActionThread_work_wrapper(void*);
81 struct ThreadData
82 {
83 ActionThread *this_;
84 GUIAction *act;
85 ThreadData(ActionThread *this_, GUIAction *act) : this_(this_), act(act) {}
86 };
87
88 pthread_t m_thread;
89 bool m_thread_running;
90 pthread_mutex_t m_act_lock;
91};
92
93static ActionThread action_thread; // for all kinds of longer running actions
94static ActionThread cancel_thread; // for longer running "cancel" actions
thatc6085482015-01-09 22:12:43 +010095
96static void *ActionThread_work_wrapper(void *data)
97{
that73a52952015-01-28 23:07:34 +010098 static_cast<ActionThread::ThreadData*>(data)->this_->run(data);
thatc6085482015-01-09 22:12:43 +010099 return NULL;
100}
101
102ActionThread::ActionThread()
103{
104 m_thread_running = false;
105 pthread_mutex_init(&m_act_lock, NULL);
106}
107
108ActionThread::~ActionThread()
109{
110 pthread_mutex_lock(&m_act_lock);
Matt Mowera8a89d12016-12-30 18:10:37 -0600111 if (m_thread_running) {
thatc6085482015-01-09 22:12:43 +0100112 pthread_mutex_unlock(&m_act_lock);
113 pthread_join(m_thread, NULL);
114 } else {
115 pthread_mutex_unlock(&m_act_lock);
116 }
117 pthread_mutex_destroy(&m_act_lock);
118}
119
that7d3b54f2015-01-09 22:52:51 +0100120void ActionThread::threadActions(GUIAction *act)
thatc6085482015-01-09 22:12:43 +0100121{
122 pthread_mutex_lock(&m_act_lock);
123 if (m_thread_running) {
124 pthread_mutex_unlock(&m_act_lock);
that7d3b54f2015-01-09 22:52:51 +0100125 LOGERR("Another threaded action is already running -- not running %u actions starting with '%s'\n",
126 act->mActions.size(), act->mActions[0].mFunction.c_str());
thatc6085482015-01-09 22:12:43 +0100127 } else {
128 m_thread_running = true;
129 pthread_mutex_unlock(&m_act_lock);
that73a52952015-01-28 23:07:34 +0100130 ThreadData *d = new ThreadData(this, act);
thatc6085482015-01-09 22:12:43 +0100131 pthread_create(&m_thread, NULL, &ActionThread_work_wrapper, d);
132 }
133}
134
135void ActionThread::run(void *data)
136{
137 ThreadData *d = (ThreadData*)data;
138 GUIAction* act = d->act;
139
140 std::vector<GUIAction::Action>::iterator it;
that7d3b54f2015-01-09 22:52:51 +0100141 for (it = act->mActions.begin(); it != act->mActions.end(); ++it)
thatc6085482015-01-09 22:12:43 +0100142 act->doAction(*it);
143
144 pthread_mutex_lock(&m_act_lock);
145 m_thread_running = false;
146 pthread_mutex_unlock(&m_act_lock);
147 delete d;
148}
149
Dees_Troy51a0e822012-09-05 15:24:24 -0400150GUIAction::GUIAction(xml_node<>* node)
Vojtech Bocekede51c52014-02-07 23:58:09 +0100151 : GUIObject(node)
Dees_Troy51a0e822012-09-05 15:24:24 -0400152{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200153 xml_node<>* child;
154 xml_node<>* actions;
155 xml_attribute<>* attr;
Dees_Troy51a0e822012-09-05 15:24:24 -0400156
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200157 if (!node) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400158
that3f7b1ac2014-12-30 11:30:13 +0100159 if (mf.empty()) {
Vojtech Bocekecd89182015-01-15 16:28:54 +0100160#define ADD_ACTION(n) mf[#n] = &GUIAction::n
161#define ADD_ACTION_EX(name, func) mf[name] = &GUIAction::func
thatc6085482015-01-09 22:12:43 +0100162 // These actions will be run in the caller's thread
Vojtech Bocekecd89182015-01-15 16:28:54 +0100163 ADD_ACTION(reboot);
164 ADD_ACTION(home);
165 ADD_ACTION(key);
166 ADD_ACTION(page);
167 ADD_ACTION(reload);
168 ADD_ACTION(readBackup);
169 ADD_ACTION(set);
170 ADD_ACTION(clear);
171 ADD_ACTION(mount);
172 ADD_ACTION(unmount);
173 ADD_ACTION_EX("umount", unmount);
174 ADD_ACTION(restoredefaultsettings);
175 ADD_ACTION(copylog);
176 ADD_ACTION(compute);
177 ADD_ACTION_EX("addsubtract", compute);
178 ADD_ACTION(setguitimezone);
179 ADD_ACTION(overlay);
180 ADD_ACTION(queuezip);
181 ADD_ACTION(cancelzip);
182 ADD_ACTION(queueclear);
183 ADD_ACTION(sleep);
Matt Mower9a2a2052016-05-31 21:31:22 -0500184 ADD_ACTION(sleepcounter);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100185 ADD_ACTION(appenddatetobackupname);
186 ADD_ACTION(generatebackupname);
187 ADD_ACTION(checkpartitionlist);
188 ADD_ACTION(getpartitiondetails);
189 ADD_ACTION(screenshot);
190 ADD_ACTION(setbrightness);
191 ADD_ACTION(fileexists);
192 ADD_ACTION(killterminal);
193 ADD_ACTION(checkbackupname);
194 ADD_ACTION(adbsideloadcancel);
195 ADD_ACTION(fixsu);
196 ADD_ACTION(startmtp);
197 ADD_ACTION(stopmtp);
198 ADD_ACTION(cancelbackup);
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500199 ADD_ACTION(checkpartitionlifetimewrites);
200 ADD_ACTION(mountsystemtoggle);
Ethan Yonker74db1572015-10-28 12:44:49 -0500201 ADD_ACTION(setlanguage);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -0600202 ADD_ACTION(checkforapp);
Matt Mower9472ba12016-01-20 18:12:47 -0600203 ADD_ACTION(togglebacklight);
thatc6085482015-01-09 22:12:43 +0100204
205 // remember actions that run in the caller thread
206 for (mapFunc::const_iterator it = mf.begin(); it != mf.end(); ++it)
207 setActionsRunningInCallerThread.insert(it->first);
208
209 // These actions will run in a separate thread
Vojtech Bocekecd89182015-01-15 16:28:54 +0100210 ADD_ACTION(flash);
211 ADD_ACTION(wipe);
212 ADD_ACTION(refreshsizes);
213 ADD_ACTION(nandroid);
Ethan Yonkerb5fab762016-01-28 14:03:33 -0600214 ADD_ACTION(fixcontexts);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100215 ADD_ACTION(fixpermissions);
216 ADD_ACTION(dd);
217 ADD_ACTION(partitionsd);
218 ADD_ACTION(installhtcdumlock);
219 ADD_ACTION(htcdumlockrestoreboot);
220 ADD_ACTION(htcdumlockreflashrecovery);
221 ADD_ACTION(cmd);
222 ADD_ACTION(terminalcommand);
223 ADD_ACTION(reinjecttwrp);
224 ADD_ACTION(decrypt);
225 ADD_ACTION(adbsideload);
226 ADD_ACTION(openrecoveryscript);
227 ADD_ACTION(installsu);
228 ADD_ACTION(decrypt_backup);
229 ADD_ACTION(repair);
Ethan Yonkera2719152015-05-28 09:44:41 -0500230 ADD_ACTION(resize);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100231 ADD_ACTION(changefilesystem);
232 ADD_ACTION(flashimage);
that10ae24f2015-12-26 20:53:51 +0100233 ADD_ACTION(twcmd);
Ethan Yonker1b190162016-12-05 15:25:19 -0600234 ADD_ACTION(setbootslot);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -0600235 ADD_ACTION(installapp);
Ethan Yonker76bbd3a2019-05-10 10:50:04 -0500236 ADD_ACTION(uninstalltwrpsystemapp);
Ethan Yonker53796e72019-01-11 22:49:52 -0600237 ADD_ACTION(repackimage);
238 ADD_ACTION(fixabrecoverybootloop);
that3f7b1ac2014-12-30 11:30:13 +0100239 }
240
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200241 // First, get the action
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600242 actions = FindNode(node, "actions");
243 if (actions) child = FindNode(actions, "action");
244 else child = FindNode(node, "action");
Dees_Troy51a0e822012-09-05 15:24:24 -0400245
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200246 if (!child) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400247
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200248 while (child)
249 {
250 Action action;
Dees_Troy51a0e822012-09-05 15:24:24 -0400251
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200252 attr = child->first_attribute("function");
253 if (!attr) return;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500254
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200255 action.mFunction = attr->value();
256 action.mArg = child->value();
257 mActions.push_back(action);
Dees_Troy51a0e822012-09-05 15:24:24 -0400258
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200259 child = child->next_sibling("action");
260 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400261
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200262 // Now, let's get either the key or region
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600263 child = FindNode(node, "touch");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200264 if (child)
265 {
266 attr = child->first_attribute("key");
267 if (attr)
268 {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100269 std::vector<std::string> keys = TWFunc::Split_String(attr->value(), "+");
Matt Mowera8a89d12016-12-30 18:10:37 -0600270 for (size_t i = 0; i < keys.size(); ++i)
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100271 {
272 const int key = getKeyByName(keys[i]);
273 mKeys[key] = false;
274 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200275 }
276 else
277 {
278 attr = child->first_attribute("x");
279 if (!attr) return;
280 mActionX = atol(attr->value());
281 attr = child->first_attribute("y");
282 if (!attr) return;
283 mActionY = atol(attr->value());
284 attr = child->first_attribute("w");
285 if (!attr) return;
286 mActionW = atol(attr->value());
287 attr = child->first_attribute("h");
288 if (!attr) return;
289 mActionH = atol(attr->value());
290 }
291 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400292}
293
Matt Mower25036b72016-06-24 12:30:18 -0500294int GUIAction::NotifyTouch(TOUCH_STATE state, int x __unused, int y __unused)
Dees_Troy51a0e822012-09-05 15:24:24 -0400295{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200296 if (state == TOUCH_RELEASE)
297 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400298
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200299 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400300}
301
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100302int GUIAction::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -0400303{
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100304 std::map<int, bool>::iterator itr = mKeys.find(key);
Matt Mowera8a89d12016-12-30 18:10:37 -0600305 if (itr == mKeys.end())
that8834a0f2016-01-05 23:29:30 +0100306 return 1;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100307
308 bool prevState = itr->second;
309 itr->second = down;
310
311 // If there is only one key for this action, wait for key up so it
312 // doesn't trigger with multi-key actions.
313 // Else, check if all buttons are pressed, then consume their release events
314 // so they don't trigger one-button actions and reset mKeys pressed status
Matt Mowera8a89d12016-12-30 18:10:37 -0600315 if (mKeys.size() == 1) {
316 if (!down && prevState) {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100317 doActions();
thatd4725ca2016-01-19 00:15:21 +0100318 return 0;
319 }
Matt Mowera8a89d12016-12-30 18:10:37 -0600320 } else if (down) {
321 for (itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
322 if (!itr->second)
that8834a0f2016-01-05 23:29:30 +0100323 return 1;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100324 }
325
326 // Passed, all req buttons are pressed, reset them and consume release events
327 HardwareKeyboard *kb = PageManager::GetHardwareKeyboard();
Matt Mowera8a89d12016-12-30 18:10:37 -0600328 for (itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100329 kb->ConsumeKeyRelease(itr->first);
330 itr->second = false;
331 }
332
333 doActions();
thatd4725ca2016-01-19 00:15:21 +0100334 return 0;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100335 }
336
thatd4725ca2016-01-19 00:15:21 +0100337 return 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400338}
339
Vojtech Bocek07220562014-02-08 02:05:33 +0100340int GUIAction::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400341{
Vojtech Bocek07220562014-02-08 02:05:33 +0100342 GUIObject::NotifyVarChange(varName, value);
343
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100344 if (varName.empty() && !isConditionValid() && mKeys.empty() && !mActionW)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200345 doActions();
Matt Mowera8a89d12016-12-30 18:10:37 -0600346 else if ((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200347 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400348
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200349 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400350}
351
352void GUIAction::simulate_progress_bar(void)
353{
Ethan Yonker74db1572015-10-28 12:44:49 -0500354 gui_msg("simulating=Simulating actions...");
Dees_Troy51a0e822012-09-05 15:24:24 -0400355 for (int i = 0; i < 5; i++)
356 {
bigbiff7abc5fe2015-01-17 16:53:12 -0500357 if (PartitionManager.stop_backup.get_value()) {
358 DataManager::SetValue("tw_cancel_backup", 1);
Matt Mower3c366972015-12-25 19:28:31 -0600359 gui_msg("backup_cancel=Backup Cancelled");
bigbiff7abc5fe2015-01-17 16:53:12 -0500360 DataManager::SetValue("ui_progress", 0);
361 PartitionManager.stop_backup.set_value(0);
362 return;
363 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400364 usleep(500000);
365 DataManager::SetValue("ui_progress", i * 20);
366 }
367}
368
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600369int GUIAction::flash_zip(std::string filename, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400370{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200371 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400372
373 DataManager::SetValue("ui_progress", 0);
Chaosmasterd5364a02020-02-03 15:38:02 +0100374 DataManager::SetValue("ui_portion_size", 0);
375 DataManager::SetValue("ui_portion_start", 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400376
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200377 if (filename.empty())
378 {
379 LOGERR("No file specified.\n");
380 return -1;
381 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400382
Ethan Yonker9598c072016-01-15 21:54:34 -0600383 if (!TWFunc::Path_Exists(filename)) {
384 if (!PartitionManager.Mount_By_Path(filename, true)) {
385 return -1;
386 }
387 if (!TWFunc::Path_Exists(filename)) {
388 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")(filename));
389 return -1;
390 }
391 }
Dees_Troy657c3092012-09-10 20:32:10 -0400392
Dees_Troy51a0e822012-09-05 15:24:24 -0400393 if (simulate) {
394 simulate_progress_bar();
395 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400396 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400397
398 // Now, check if we need to ensure TWRP remains installed...
399 struct stat st;
400 if (stat("/sbin/installTwrp", &st) == 0)
401 {
402 DataManager::SetValue("tw_operation", "Configuring TWRP");
403 DataManager::SetValue("tw_partition", "");
Ethan Yonker74db1572015-10-28 12:44:49 -0500404 gui_msg("config_twrp=Configuring TWRP...");
Vojtech Bocek05534202013-09-11 08:11:56 +0200405 if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall") < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400406 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500407 gui_msg("config_twrp_err=Unable to configure TWRP with this kernel.");
Dees_Troy51a0e822012-09-05 15:24:24 -0400408 }
409 }
410 }
411
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200412 // Done
413 DataManager::SetValue("ui_progress", 100);
414 DataManager::SetValue("ui_progress", 0);
Chaosmasterd5364a02020-02-03 15:38:02 +0100415 DataManager::SetValue("ui_portion_size", 0);
416 DataManager::SetValue("ui_portion_start", 0);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200417 return ret_val;
Dees_Troy51a0e822012-09-05 15:24:24 -0400418}
419
that73a52952015-01-28 23:07:34 +0100420GUIAction::ThreadType GUIAction::getThreadType(const GUIAction::Action& action)
thatc6085482015-01-09 22:12:43 +0100421{
that73a52952015-01-28 23:07:34 +0100422 string func = gui_parse_text(action.mFunction);
423 bool needsThread = setActionsRunningInCallerThread.find(func) == setActionsRunningInCallerThread.end();
424 if (needsThread) {
425 if (func == "cancelbackup")
426 return THREAD_CANCEL;
427 else
428 return THREAD_ACTION;
429 }
430 return THREAD_NONE;
thatc6085482015-01-09 22:12:43 +0100431}
432
Dees_Troy51a0e822012-09-05 15:24:24 -0400433int GUIAction::doActions()
434{
that3f7b1ac2014-12-30 11:30:13 +0100435 if (mActions.size() < 1)
436 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400437
that73a52952015-01-28 23:07:34 +0100438 // Determine in which thread to run the actions.
439 // Do it for all actions at once before starting, so that we can cancel the whole batch if the thread is already busy.
440 ThreadType threadType = THREAD_NONE;
that3f7b1ac2014-12-30 11:30:13 +0100441 std::vector<Action>::iterator it;
that73a52952015-01-28 23:07:34 +0100442 for (it = mActions.begin(); it != mActions.end(); ++it) {
443 ThreadType tt = getThreadType(*it);
444 if (tt == THREAD_NONE)
445 continue;
446 if (threadType == THREAD_NONE)
447 threadType = tt;
448 else if (threadType != tt) {
449 LOGERR("Can't mix normal and cancel actions in the same list.\n"
450 "Running the whole batch in the cancel thread.\n");
451 threadType = THREAD_CANCEL;
thatc6085482015-01-09 22:12:43 +0100452 break;
453 }
that7d3b54f2015-01-09 22:52:51 +0100454 }
that73a52952015-01-28 23:07:34 +0100455
456 // Now run the actions in the desired thread.
457 switch (threadType) {
458 case THREAD_ACTION:
459 action_thread.threadActions(this);
460 break;
461
462 case THREAD_CANCEL:
463 cancel_thread.threadActions(this);
464 break;
465
466 default: {
467 // no iterators here because theme reloading might kill our object
468 const size_t cnt = mActions.size();
469 for (size_t i = 0; i < cnt; ++i)
470 doAction(mActions[i]);
471 }
thatc6085482015-01-09 22:12:43 +0100472 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400473
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200474 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400475}
476
that3f7b1ac2014-12-30 11:30:13 +0100477int GUIAction::doAction(Action action)
Dees_Troy51a0e822012-09-05 15:24:24 -0400478{
that3f7b1ac2014-12-30 11:30:13 +0100479 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400480
that3f7b1ac2014-12-30 11:30:13 +0100481 std::string function = gui_parse_text(action.mFunction);
482 std::string arg = gui_parse_text(action.mArg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400483
that3f7b1ac2014-12-30 11:30:13 +0100484 // find function and execute it
485 mapFunc::const_iterator funcitr = mf.find(function);
486 if (funcitr != mf.end())
487 return (this->*funcitr->second)(arg);
488
489 LOGERR("Unknown action '%s'\n", function.c_str());
490 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400491}
492
493void GUIAction::operation_start(const string operation_name)
494{
that3f7b1ac2014-12-30 11:30:13 +0100495 LOGINFO("operation_start: '%s'\n", operation_name.c_str());
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000496 time(&Start);
Dees_Troy51a0e822012-09-05 15:24:24 -0400497 DataManager::SetValue(TW_ACTION_BUSY, 1);
498 DataManager::SetValue("ui_progress", 0);
Chaosmasterd5364a02020-02-03 15:38:02 +0100499 DataManager::SetValue("ui_portion_size", 0);
500 DataManager::SetValue("ui_portion_start", 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400501 DataManager::SetValue("tw_operation", operation_name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400502 DataManager::SetValue("tw_operation_state", 0);
Ethan Yonkerd83c9ea2015-01-02 15:22:31 -0600503 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff19874f12019-01-08 20:06:57 -0500504 bool tw_ab_device = TWFunc::get_cache_dir() != NON_AB_CACHE_DIR;
505 DataManager::SetValue("tw_ab_device", tw_ab_device);
Dees_Troy51a0e822012-09-05 15:24:24 -0400506}
507
that3f7b1ac2014-12-30 11:30:13 +0100508void GUIAction::operation_end(const int operation_status)
Dees_Troy51a0e822012-09-05 15:24:24 -0400509{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000510 time_t Stop;
Dees_Troy51a0e822012-09-05 15:24:24 -0400511 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400512 DataManager::SetValue("ui_progress", 100);
513 if (simulate) {
514 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
515 if (simulate_fail != 0)
516 DataManager::SetValue("tw_operation_status", 1);
517 else
518 DataManager::SetValue("tw_operation_status", 0);
519 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500520 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400521 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500522 }
523 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400524 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500525 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400526 }
527 DataManager::SetValue("tw_operation_state", 1);
528 DataManager::SetValue(TW_ACTION_BUSY, 0);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500529 blankTimer.resetTimerAndUnblank();
LuK133762326f42015-09-30 19:57:46 +0200530 property_set("twrp.action_complete", "1");
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000531 time(&Stop);
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400532
533#ifndef TW_NO_HAPTICS
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000534 if ((int) difftime(Stop, Start) > 10)
Ethan Yonker03db3262014-02-05 08:02:06 -0600535 DataManager::Vibrate("tw_action_vibrate");
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400536#endif
537
that3f7b1ac2014-12-30 11:30:13 +0100538 LOGINFO("operation_end - status=%d\n", operation_status);
Dees_Troy51a0e822012-09-05 15:24:24 -0400539}
540
that3f7b1ac2014-12-30 11:30:13 +0100541int GUIAction::reboot(std::string arg)
Dees_Troy51a0e822012-09-05 15:24:24 -0400542{
that3f7b1ac2014-12-30 11:30:13 +0100543 sync();
544 DataManager::SetValue("tw_gui_done", 1);
545 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400546
that3f7b1ac2014-12-30 11:30:13 +0100547 return 0;
548}
Dees_Troy51a0e822012-09-05 15:24:24 -0400549
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500550int GUIAction::home(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100551{
that3f7b1ac2014-12-30 11:30:13 +0100552 gui_changePage("main");
553 return 0;
554}
Dees_Troy51a0e822012-09-05 15:24:24 -0400555
that3f7b1ac2014-12-30 11:30:13 +0100556int GUIAction::key(std::string arg)
557{
558 const int key = getKeyByName(arg);
559 PageManager::NotifyKey(key, true);
560 PageManager::NotifyKey(key, false);
561 return 0;
562}
563
564int GUIAction::page(std::string arg)
565{
LuK133762326f42015-09-30 19:57:46 +0200566 property_set("twrp.action_complete", "0");
that3f7b1ac2014-12-30 11:30:13 +0100567 std::string page_name = gui_parse_text(arg);
568 return gui_changePage(page_name);
569}
570
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500571int GUIAction::reload(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100572{
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500573 PageManager::RequestReload();
574 // The actual reload is handled in pages.cpp in PageManager::RunReload()
575 // The reload will occur on the next Update or Render call and will
576 // be performed in the rendoer thread instead of the action thread
577 // to prevent crashing which could occur when we start deleting
578 // GUI resources in the action thread while we attempt to render
579 // with those same resources in another thread.
that3f7b1ac2014-12-30 11:30:13 +0100580 return 0;
581}
Dees_Troy51a0e822012-09-05 15:24:24 -0400582
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500583int GUIAction::readBackup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100584{
585 string Restore_Name;
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -0400586
that3f7b1ac2014-12-30 11:30:13 +0100587 DataManager::GetValue("tw_restore", Restore_Name);
588 PartitionManager.Set_Restore_Files(Restore_Name);
589 return 0;
590}
591
592int GUIAction::set(std::string arg)
593{
594 if (arg.find('=') != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200595 {
that3f7b1ac2014-12-30 11:30:13 +0100596 string varName = arg.substr(0, arg.find('='));
597 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400598
that3f7b1ac2014-12-30 11:30:13 +0100599 DataManager::GetValue(value, value);
600 DataManager::SetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200601 }
that3f7b1ac2014-12-30 11:30:13 +0100602 else
603 DataManager::SetValue(arg, "1");
604 return 0;
605}
Dees_Troy51a0e822012-09-05 15:24:24 -0400606
that3f7b1ac2014-12-30 11:30:13 +0100607int GUIAction::clear(std::string arg)
608{
609 DataManager::SetValue(arg, "0");
610 return 0;
611}
Dees_Troy51a0e822012-09-05 15:24:24 -0400612
that3f7b1ac2014-12-30 11:30:13 +0100613int GUIAction::mount(std::string arg)
614{
615 if (arg == "usb") {
616 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400617 if (!simulate)
that3f7b1ac2014-12-30 11:30:13 +0100618 PartitionManager.usb_storage_enable();
619 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500620 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100621 } else if (!simulate) {
622 PartitionManager.Mount_By_Path(arg, true);
623 PartitionManager.Add_MTP_Storage(arg);
624 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500625 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100626 return 0;
627}
628
629int GUIAction::unmount(std::string arg)
630{
631 if (arg == "usb") {
632 if (!simulate)
633 PartitionManager.usb_storage_disable();
634 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500635 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100636 DataManager::SetValue(TW_ACTION_BUSY, 0);
637 } else if (!simulate) {
638 PartitionManager.UnMount_By_Path(arg, true);
639 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500640 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100641 return 0;
642}
643
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500644int GUIAction::restoredefaultsettings(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100645{
646 operation_start("Restore Defaults");
647 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
Ethan Yonker74db1572015-10-28 12:44:49 -0500648 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100649 else {
650 DataManager::ResetDefaults();
651 PartitionManager.Update_System_Details();
652 PartitionManager.Mount_Current_Storage(true);
653 }
654 operation_end(0);
655 return 0;
656}
657
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500658int GUIAction::copylog(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100659{
660 operation_start("Copy Log");
661 if (!simulate)
662 {
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400663 string dst, curr_storage;
664 int copy_kernel_log = 0;
665
666 DataManager::GetValue("tw_include_kernel_log", copy_kernel_log);
that3f7b1ac2014-12-30 11:30:13 +0100667 PartitionManager.Mount_Current_Storage(true);
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400668 curr_storage = DataManager::GetCurrentStoragePath();
669 dst = curr_storage + "/recovery.log";
that3f7b1ac2014-12-30 11:30:13 +0100670 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
671 tw_set_default_metadata(dst.c_str());
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400672 if (copy_kernel_log)
673 TWFunc::copy_kernel_log(curr_storage);
that3f7b1ac2014-12-30 11:30:13 +0100674 sync();
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400675 gui_msg(Msg("copy_log=Copied recovery log to {1}")(dst));
that3f7b1ac2014-12-30 11:30:13 +0100676 } else
677 simulate_progress_bar();
678 operation_end(0);
679 return 0;
680}
681
682
683int GUIAction::compute(std::string arg)
684{
685 if (arg.find("+") != string::npos)
686 {
687 string varName = arg.substr(0, arg.find('+'));
688 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
689 int amount_to_add = atoi(string_to_add.c_str());
690 int value;
691
692 DataManager::GetValue(varName, value);
693 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400694 return 0;
695 }
that3f7b1ac2014-12-30 11:30:13 +0100696 if (arg.find("-") != string::npos)
Dees_Troy51a0e822012-09-05 15:24:24 -0400697 {
that3f7b1ac2014-12-30 11:30:13 +0100698 string varName = arg.substr(0, arg.find('-'));
699 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
700 int amount_to_subtract = atoi(string_to_subtract.c_str());
701 int value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400702
that3f7b1ac2014-12-30 11:30:13 +0100703 DataManager::GetValue(varName, value);
704 value -= amount_to_subtract;
705 if (value <= 0)
706 value = 0;
707 DataManager::SetValue(varName, value);
708 return 0;
709 }
710 if (arg.find("*") != string::npos)
711 {
712 string varName = arg.substr(0, arg.find('*'));
713 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
714 int multiply_by = atoi(multiply_by_str.c_str());
715 int value;
716
717 DataManager::GetValue(varName, value);
718 DataManager::SetValue(varName, value*multiply_by);
719 return 0;
720 }
721 if (arg.find("/") != string::npos)
722 {
723 string varName = arg.substr(0, arg.find('/'));
724 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
725 int divide_by = atoi(divide_by_str.c_str());
726 int value;
727
Matt Mowera8a89d12016-12-30 18:10:37 -0600728 if (divide_by != 0)
that3f7b1ac2014-12-30 11:30:13 +0100729 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400730 DataManager::GetValue(varName, value);
that3f7b1ac2014-12-30 11:30:13 +0100731 DataManager::SetValue(varName, value/divide_by);
Dees_Troy51a0e822012-09-05 15:24:24 -0400732 }
733 return 0;
734 }
that3f7b1ac2014-12-30 11:30:13 +0100735 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
736 return -1;
737}
Dees_Troy51a0e822012-09-05 15:24:24 -0400738
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500739int GUIAction::setguitimezone(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100740{
741 string SelectedZone;
742 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
743 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
744 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
745
746 int dst;
747 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
748
749 string offset;
750 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
751
752 string NewTimeZone = Zone;
753 if (offset != "0")
754 NewTimeZone += ":" + offset;
755
756 if (dst != 0)
757 NewTimeZone += DSTZone;
758
759 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
760 DataManager::update_tz_environment_variables();
761 return 0;
762}
763
764int GUIAction::overlay(std::string arg)
765{
766 return gui_changeOverlay(arg);
767}
768
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500769int GUIAction::queuezip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100770{
771 if (zip_queue_index >= 10) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500772 gui_msg("max_queue=Maximum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400773 return 0;
774 }
that3f7b1ac2014-12-30 11:30:13 +0100775 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
776 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
777 zip_queue_index++;
Dees_Troy51a0e822012-09-05 15:24:24 -0400778 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +0100779 }
780 return 0;
781}
782
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500783int GUIAction::cancelzip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100784{
785 if (zip_queue_index <= 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500786 gui_msg("min_queue=Minimum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400787 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100788 } else {
789 zip_queue_index--;
790 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
Dees_Troy51a0e822012-09-05 15:24:24 -0400791 }
that3f7b1ac2014-12-30 11:30:13 +0100792 return 0;
793}
Dees_Troy51a0e822012-09-05 15:24:24 -0400794
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500795int GUIAction::queueclear(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100796{
797 zip_queue_index = 0;
798 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
799 return 0;
800}
Dees_Troy51a0e822012-09-05 15:24:24 -0400801
that3f7b1ac2014-12-30 11:30:13 +0100802int GUIAction::sleep(std::string arg)
803{
804 operation_start("Sleep");
805 usleep(atoi(arg.c_str()));
806 operation_end(0);
807 return 0;
808}
Dees Troyb21cc642013-09-10 17:36:41 +0000809
Matt Mower9a2a2052016-05-31 21:31:22 -0500810int GUIAction::sleepcounter(std::string arg)
811{
812 operation_start("SleepCounter");
813 // Ensure user notices countdown in case it needs to be cancelled
814 blankTimer.resetTimerAndUnblank();
815 int total = atoi(arg.c_str());
816 for (int t = total; t > 0; t--) {
817 int progress = (int)(((float)(total-t)/(float)total)*100.0);
818 DataManager::SetValue("ui_progress", progress);
819 ::sleep(1);
820 DataManager::SetValue("tw_sleep", t-1);
821 }
822 DataManager::SetValue("ui_progress", 100);
823 operation_end(0);
824 return 0;
825}
826
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500827int GUIAction::appenddatetobackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100828{
829 operation_start("AppendDateToBackupName");
830 string Backup_Name;
831 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
832 Backup_Name += TWFunc::Get_Current_Date();
833 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
834 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
835 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
Matt Mower76b8afc2016-06-24 12:04:27 -0500836 PageManager::NotifyKey(KEY_END, true);
837 PageManager::NotifyKey(KEY_END, false);
that3f7b1ac2014-12-30 11:30:13 +0100838 operation_end(0);
839 return 0;
840}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500841
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500842int GUIAction::generatebackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100843{
844 operation_start("GenerateBackupName");
845 TWFunc::Auto_Generate_Backup_Name();
846 operation_end(0);
847 return 0;
848}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500849
Ethan Yonker483e9f42016-01-11 22:21:18 -0600850int GUIAction::checkpartitionlist(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100851{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600852 string List, part_path;
that3f7b1ac2014-12-30 11:30:13 +0100853 int count = 0;
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500854
Ethan Yonker483e9f42016-01-11 22:21:18 -0600855 if (arg.empty())
856 arg = "tw_wipe_list";
857 DataManager::GetValue(arg, List);
858 LOGINFO("checkpartitionlist list '%s'\n", List.c_str());
859 if (!List.empty()) {
860 size_t start_pos = 0, end_pos = List.find(";", start_pos);
861 while (end_pos != string::npos && start_pos < List.size()) {
862 part_path = List.substr(start_pos, end_pos - start_pos);
863 LOGINFO("checkpartitionlist part_path '%s'\n", part_path.c_str());
864 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100865 // Do nothing
Dees_Troy51a0e822012-09-05 15:24:24 -0400866 } else {
that3f7b1ac2014-12-30 11:30:13 +0100867 count++;
868 }
869 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600870 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100871 }
872 DataManager::SetValue("tw_check_partition_list", count);
873 } else {
874 DataManager::SetValue("tw_check_partition_list", 0);
875 }
Matt Mowera8a89d12016-12-30 18:10:37 -0600876 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100877}
Dees_Troy51a0e822012-09-05 15:24:24 -0400878
Ethan Yonker483e9f42016-01-11 22:21:18 -0600879int GUIAction::getpartitiondetails(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100880{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600881 string List, part_path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400882
Ethan Yonker483e9f42016-01-11 22:21:18 -0600883 if (arg.empty())
884 arg = "tw_wipe_list";
885 DataManager::GetValue(arg, List);
886 LOGINFO("getpartitiondetails list '%s'\n", List.c_str());
887 if (!List.empty()) {
888 size_t start_pos = 0, end_pos = List.find(";", start_pos);
889 part_path = List;
890 while (end_pos != string::npos && start_pos < List.size()) {
891 part_path = List.substr(start_pos, end_pos - start_pos);
892 LOGINFO("getpartitiondetails part_path '%s'\n", part_path.c_str());
893 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100894 // Do nothing
895 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600896 DataManager::SetValue("tw_partition_path", part_path);
that3f7b1ac2014-12-30 11:30:13 +0100897 break;
898 }
899 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600900 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100901 }
Ethan Yonker483e9f42016-01-11 22:21:18 -0600902 if (!part_path.empty()) {
903 TWPartition* Part = PartitionManager.Find_Partition_By_Path(part_path);
that3f7b1ac2014-12-30 11:30:13 +0100904 if (Part) {
905 unsigned long long mb = 1048576;
Dees_Troya13d74f2013-03-24 08:54:55 -0500906
that3f7b1ac2014-12-30 11:30:13 +0100907 DataManager::SetValue("tw_partition_name", Part->Display_Name);
908 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
909 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
910 DataManager::SetValue("tw_partition_size", Part->Size / mb);
911 DataManager::SetValue("tw_partition_used", Part->Used / mb);
912 DataManager::SetValue("tw_partition_free", Part->Free / mb);
913 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
914 DataManager::SetValue("tw_partition_removable", Part->Removable);
915 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
916
917 if (Part->Can_Repair())
918 DataManager::SetValue("tw_partition_can_repair", 1);
919 else
920 DataManager::SetValue("tw_partition_can_repair", 0);
Ethan Yonkera2719152015-05-28 09:44:41 -0500921 if (Part->Can_Resize())
922 DataManager::SetValue("tw_partition_can_resize", 1);
923 else
924 DataManager::SetValue("tw_partition_can_resize", 0);
Matt Mower18794c82015-11-11 16:22:45 -0600925 if (TWFunc::Path_Exists("/sbin/mkfs.fat"))
that3f7b1ac2014-12-30 11:30:13 +0100926 DataManager::SetValue("tw_partition_vfat", 1);
927 else
928 DataManager::SetValue("tw_partition_vfat", 0);
Matt Mower80f7b362015-12-12 15:38:01 -0600929 if (TWFunc::Path_Exists("/sbin/mkexfatfs"))
that3f7b1ac2014-12-30 11:30:13 +0100930 DataManager::SetValue("tw_partition_exfat", 1);
931 else
932 DataManager::SetValue("tw_partition_exfat", 0);
933 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs"))
934 DataManager::SetValue("tw_partition_f2fs", 1);
935 else
936 DataManager::SetValue("tw_partition_f2fs", 0);
937 if (TWFunc::Path_Exists("/sbin/mke2fs"))
938 DataManager::SetValue("tw_partition_ext", 1);
939 else
940 DataManager::SetValue("tw_partition_ext", 0);
941 return 0;
942 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600943 LOGERR("Unable to locate partition: '%s'\n", part_path.c_str());
that3f7b1ac2014-12-30 11:30:13 +0100944 }
945 }
946 }
947 DataManager::SetValue("tw_partition_name", "");
948 DataManager::SetValue("tw_partition_file_system", "");
Ethan Yonker483e9f42016-01-11 22:21:18 -0600949 // Set this to 0 to prevent trying to partition this device, just in case
950 DataManager::SetValue("tw_partition_removable", 0);
that3f7b1ac2014-12-30 11:30:13 +0100951 return 0;
952}
953
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500954int GUIAction::screenshot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100955{
956 time_t tm;
957 char path[256];
958 int path_len;
Matt Mower0c88b842017-01-15 16:00:49 -0600959 uid_t uid = AID_MEDIA_RW;
960 gid_t gid = AID_MEDIA_RW;
that3f7b1ac2014-12-30 11:30:13 +0100961
962 const std::string storage = DataManager::GetCurrentStoragePath();
Matt Mowera8a89d12016-12-30 18:10:37 -0600963 if (PartitionManager.Is_Mounted_By_Path(storage)) {
that3f7b1ac2014-12-30 11:30:13 +0100964 snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
965 } else {
966 strcpy(path, "/tmp/");
967 }
968
Matt Mowera8a89d12016-12-30 18:10:37 -0600969 if (!TWFunc::Create_Dir_Recursive(path, 0775, uid, gid))
that3f7b1ac2014-12-30 11:30:13 +0100970 return 0;
971
972 tm = time(NULL);
973 path_len = strlen(path);
974
975 // Screenshot_2014-01-01-18-21-38.png
976 strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
977
978 int res = gr_save_screenshot(path);
Matt Mowera8a89d12016-12-30 18:10:37 -0600979 if (res == 0) {
that3f7b1ac2014-12-30 11:30:13 +0100980 chmod(path, 0666);
981 chown(path, uid, gid);
982
that677b13f2015-12-26 23:57:31 +0100983 gui_msg(Msg("screenshot_saved=Screenshot was saved to {1}")(path));
that3f7b1ac2014-12-30 11:30:13 +0100984
VDavid0032034a412019-10-18 22:43:20 +0200985 // blink to notify that the screenshot was taken
that3f7b1ac2014-12-30 11:30:13 +0100986 gr_color(255, 255, 255, 255);
987 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
988 gr_flip();
989 gui_forceRender();
990 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500991 gui_err("screenshot_err=Failed to take a screenshot!");
that3f7b1ac2014-12-30 11:30:13 +0100992 }
993 return 0;
994}
995
996int GUIAction::setbrightness(std::string arg)
997{
998 return TWFunc::Set_Brightness(arg);
999}
1000
1001int GUIAction::fileexists(std::string arg)
1002{
1003 struct stat st;
1004 string newpath = arg + "/.";
1005
1006 operation_start("FileExists");
1007 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
1008 operation_end(0);
1009 else
1010 operation_end(1);
1011 return 0;
1012}
1013
thatcc8ddca2015-01-03 01:59:36 +01001014void GUIAction::reinject_after_flash()
1015{
1016 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001017 gui_msg("injecttwrp=Injecting TWRP into boot image...");
thatcc8ddca2015-01-03 01:59:36 +01001018 if (simulate) {
1019 simulate_progress_bar();
1020 } else {
1021 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
1022 if (Boot == NULL || Boot->Current_File_System != "emmc")
1023 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
1024 else {
1025 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
1026 TWFunc::Exec_Cmd(injectcmd);
1027 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001028 gui_msg("done=Done.");
thatcc8ddca2015-01-03 01:59:36 +01001029 }
1030 }
1031}
1032
that3f7b1ac2014-12-30 11:30:13 +01001033int GUIAction::flash(std::string arg)
1034{
1035 int i, ret_val = 0, wipe_cache = 0;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001036 // We're going to jump to this page first, like a loading page
1037 gui_changePage(arg);
that3f7b1ac2014-12-30 11:30:13 +01001038 for (i=0; i<zip_queue_index; i++) {
thatc7b631b2015-06-01 23:36:57 +02001039 string zip_path = zip_queue[i];
1040 size_t slashpos = zip_path.find_last_of('/');
1041 string zip_filename = (slashpos == string::npos) ? zip_path : zip_path.substr(slashpos + 1);
that3f7b1ac2014-12-30 11:30:13 +01001042 operation_start("Flashing");
thatc7b631b2015-06-01 23:36:57 +02001043 DataManager::SetValue("tw_filename", zip_path);
1044 DataManager::SetValue("tw_file", zip_filename);
that3f7b1ac2014-12-30 11:30:13 +01001045 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
1046
1047 TWFunc::SetPerformanceMode(true);
thatc7b631b2015-06-01 23:36:57 +02001048 ret_val = flash_zip(zip_path, &wipe_cache);
that3f7b1ac2014-12-30 11:30:13 +01001049 TWFunc::SetPerformanceMode(false);
1050 if (ret_val != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001051 gui_msg(Msg(msg::kError, "zip_err=Error installing zip file '{1}'")(zip_path));
that3f7b1ac2014-12-30 11:30:13 +01001052 ret_val = 1;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001053 break;
that3f7b1ac2014-12-30 11:30:13 +01001054 }
1055 }
1056 zip_queue_index = 0;
that3f7b1ac2014-12-30 11:30:13 +01001057
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001058 if (wipe_cache) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001059 gui_msg("zip_wipe_cache=One or more zip requested a cache wipe -- Wiping cache now.");
that3f7b1ac2014-12-30 11:30:13 +01001060 PartitionManager.Wipe_By_Path("/cache");
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001061 }
that3f7b1ac2014-12-30 11:30:13 +01001062
thatcc8ddca2015-01-03 01:59:36 +01001063 reinject_after_flash();
that3f7b1ac2014-12-30 11:30:13 +01001064 PartitionManager.Update_System_Details();
1065 operation_end(ret_val);
bigbiffa869fc72016-03-01 19:40:36 -05001066 // 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 -06001067 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +01001068 return 0;
1069}
1070
1071int GUIAction::wipe(std::string arg)
1072{
1073 operation_start("Format");
1074 DataManager::SetValue("tw_partition", arg);
that3f7b1ac2014-12-30 11:30:13 +01001075 int ret_val = false;
1076
1077 if (simulate) {
1078 simulate_progress_bar();
1079 } else {
1080 if (arg == "data")
1081 ret_val = PartitionManager.Factory_Reset();
1082 else if (arg == "battery")
1083 ret_val = PartitionManager.Wipe_Battery_Stats();
1084 else if (arg == "rotate")
1085 ret_val = PartitionManager.Wipe_Rotate_Data();
1086 else if (arg == "dalvik")
1087 ret_val = PartitionManager.Wipe_Dalvik_Cache();
1088 else if (arg == "DATAMEDIA") {
1089 ret_val = PartitionManager.Format_Data();
1090 } else if (arg == "INTERNAL") {
Matt Mower23d8aae2017-01-06 14:30:33 -06001091 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001092
1093 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1094 if (has_datamedia) {
1095 ret_val = PartitionManager.Wipe_Media_From_Data();
1096 } else {
1097 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
1098 }
1099 } else if (arg == "EXTERNAL") {
1100 string External_Path;
1101
1102 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
1103 ret_val = PartitionManager.Wipe_By_Path(External_Path);
1104 } else if (arg == "ANDROIDSECURE") {
1105 ret_val = PartitionManager.Wipe_Android_Secure();
1106 } else if (arg == "LIST") {
1107 string Wipe_List, wipe_path;
1108 bool skip = false;
1109 ret_val = true;
that3f7b1ac2014-12-30 11:30:13 +01001110
1111 DataManager::GetValue("tw_wipe_list", Wipe_List);
1112 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
1113 if (!Wipe_List.empty()) {
1114 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
1115 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
1116 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
1117 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
1118 if (wipe_path == "/and-sec") {
1119 if (!PartitionManager.Wipe_Android_Secure()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001120 gui_msg("and_sec_wipe_err=Unable to wipe android secure");
that3f7b1ac2014-12-30 11:30:13 +01001121 ret_val = false;
1122 break;
1123 } else {
1124 skip = true;
1125 }
1126 } else if (wipe_path == "DALVIK") {
1127 if (!PartitionManager.Wipe_Dalvik_Cache()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001128 gui_err("dalvik_wipe_err=Failed to wipe dalvik");
that3f7b1ac2014-12-30 11:30:13 +01001129 ret_val = false;
1130 break;
1131 } else {
1132 skip = true;
1133 }
1134 } else if (wipe_path == "INTERNAL") {
1135 if (!PartitionManager.Wipe_Media_From_Data()) {
1136 ret_val = false;
1137 break;
1138 } else {
1139 skip = true;
Dees_Troya13d74f2013-03-24 08:54:55 -05001140 }
1141 }
that3f7b1ac2014-12-30 11:30:13 +01001142 if (!skip) {
1143 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001144 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(wipe_path));
that3f7b1ac2014-12-30 11:30:13 +01001145 ret_val = false;
1146 break;
1147 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
1148 arg = wipe_path;
1149 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001150 } else {
that3f7b1ac2014-12-30 11:30:13 +01001151 skip = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001152 }
that3f7b1ac2014-12-30 11:30:13 +01001153 start_pos = end_pos + 1;
1154 end_pos = Wipe_List.find(";", start_pos);
Dees_Troy51a0e822012-09-05 15:24:24 -04001155 }
1156 }
that3f7b1ac2014-12-30 11:30:13 +01001157 } else
1158 ret_val = PartitionManager.Wipe_By_Path(arg);
that73a52952015-01-28 23:07:34 +01001159#ifndef TW_OEM_BUILD
that3f7b1ac2014-12-30 11:30:13 +01001160 if (arg == DataManager::GetSettingsStoragePath()) {
1161 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
1162 string Storage_Path = DataManager::GetSettingsStoragePath();
Dees_Troy51a0e822012-09-05 15:24:24 -04001163
that3f7b1ac2014-12-30 11:30:13 +01001164 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
1165 LOGINFO("Making TWRP folder and saving settings.\n");
1166 Storage_Path += "/TWRP";
1167 mkdir(Storage_Path.c_str(), 0777);
1168 DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001169 } else {
that3f7b1ac2014-12-30 11:30:13 +01001170 LOGERR("Unable to recreate TWRP folder and save settings.\n");
1171 }
1172 }
1173#endif
1174 }
1175 PartitionManager.Update_System_Details();
1176 if (ret_val)
1177 ret_val = 0; // 0 is success
1178 else
1179 ret_val = 1; // 1 is failure
1180 operation_end(ret_val);
1181 return 0;
1182}
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001183
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001184int GUIAction::refreshsizes(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001185{
1186 operation_start("Refreshing Sizes");
1187 if (simulate) {
1188 simulate_progress_bar();
1189 } else
1190 PartitionManager.Update_System_Details();
1191 operation_end(0);
1192 return 0;
1193}
1194
1195int GUIAction::nandroid(std::string arg)
1196{
that3f7b1ac2014-12-30 11:30:13 +01001197 if (simulate) {
that73a52952015-01-28 23:07:34 +01001198 PartitionManager.stop_backup.set_value(0);
that3f7b1ac2014-12-30 11:30:13 +01001199 DataManager::SetValue("tw_partition", "Simulation");
1200 simulate_progress_bar();
that73a52952015-01-28 23:07:34 +01001201 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001202 } else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001203 operation_start("Nandroid");
1204 int ret = 0;
1205
that3f7b1ac2014-12-30 11:30:13 +01001206 if (arg == "backup") {
1207 string Backup_Name;
1208 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Ethan Yonker4adc33e2016-01-22 16:07:11 -06001209 string auto_gen = gui_lookup("auto_generate", "(Auto Generate)");
Ethan Yonker53796e72019-01-11 22:49:52 -06001210 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 -05001211 ret = PartitionManager.Run_Backup(false);
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001212 DataManager::SetValue("tw_encrypt_backup", 0); // reset value so we don't encrypt every subsequent backup
1213 if (!PartitionManager.stop_backup.get_value()) {
1214 if (ret == false)
1215 ret = 1; // 1 for failure
1216 else
1217 ret = 0; // 0 for success
1218 DataManager::SetValue("tw_cancel_backup", 0);
1219 } else {
1220 DataManager::SetValue("tw_cancel_backup", 1);
1221 gui_msg("backup_cancel=Backup Cancelled");
1222 ret = 0;
1223 }
bigbiffce8f83c2015-12-12 18:30:21 -05001224 } else {
that3f7b1ac2014-12-30 11:30:13 +01001225 operation_end(1);
1226 return -1;
that3f7b1ac2014-12-30 11:30:13 +01001227 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001228 DataManager::SetValue(TW_BACKUP_NAME, auto_gen);
that3f7b1ac2014-12-30 11:30:13 +01001229 } else if (arg == "restore") {
1230 string Restore_Name;
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001231 int gui_adb_backup;
1232
that3f7b1ac2014-12-30 11:30:13 +01001233 DataManager::GetValue("tw_restore", Restore_Name);
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001234 DataManager::GetValue("tw_enable_adb_backup", gui_adb_backup);
1235 if (gui_adb_backup) {
1236 DataManager::SetValue("tw_operation_state", 1);
1237 if (TWFunc::stream_adb_backup(Restore_Name) == 0)
1238 ret = 0; // success
1239 else
1240 ret = 1; // failure
1241 DataManager::SetValue("tw_enable_adb_backup", 0);
1242 ret = 0; // assume success???
1243 } else {
1244 if (PartitionManager.Run_Restore(Restore_Name))
1245 ret = 0; // success
1246 else
1247 ret = 1; // failure
1248 }
that3f7b1ac2014-12-30 11:30:13 +01001249 } else {
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001250 operation_end(1); // invalid arg specified, fail
bigbiff7abc5fe2015-01-17 16:53:12 -05001251 return -1;
1252 }
that73a52952015-01-28 23:07:34 +01001253 operation_end(ret);
bigbiff7abc5fe2015-01-17 16:53:12 -05001254 return ret;
1255 }
1256 return 0;
1257}
1258
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001259int GUIAction::cancelbackup(std::string arg __unused) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001260 if (simulate) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001261 PartitionManager.stop_backup.set_value(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001262 }
1263 else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001264 int op_status = PartitionManager.Cancel_Backup();
1265 if (op_status != 0)
1266 op_status = 1; // failure
bigbiff7abc5fe2015-01-17 16:53:12 -05001267 }
1268
1269 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001270}
Dees_Troy51a0e822012-09-05 15:24:24 -04001271
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001272int GUIAction::fixcontexts(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001273{
that73a52952015-01-28 23:07:34 +01001274 int op_status = 0;
1275
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001276 operation_start("Fix Contexts");
1277 LOGINFO("fix contexts started!\n");
that3f7b1ac2014-12-30 11:30:13 +01001278 if (simulate) {
1279 simulate_progress_bar();
1280 } else {
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001281 op_status = PartitionManager.Fix_Contexts();
that3f7b1ac2014-12-30 11:30:13 +01001282 if (op_status != 0)
1283 op_status = 1; // failure
that3f7b1ac2014-12-30 11:30:13 +01001284 }
that73a52952015-01-28 23:07:34 +01001285 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001286 return 0;
1287}
1288
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001289int GUIAction::fixpermissions(std::string arg)
1290{
1291 return fixcontexts(arg);
1292}
1293
that3f7b1ac2014-12-30 11:30:13 +01001294int GUIAction::dd(std::string arg)
1295{
1296 operation_start("imaging");
1297
1298 if (simulate) {
1299 simulate_progress_bar();
1300 } else {
1301 string cmd = "dd " + arg;
1302 TWFunc::Exec_Cmd(cmd);
1303 }
1304 operation_end(0);
1305 return 0;
1306}
1307
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001308int GUIAction::partitionsd(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001309{
1310 operation_start("Partition SD Card");
1311 int ret_val = 0;
1312
1313 if (simulate) {
1314 simulate_progress_bar();
1315 } else {
1316 int allow_partition;
1317 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1318 if (allow_partition == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001319 gui_err("no_real_sdcard=This device does not have a real SD Card! Aborting!");
that3f7b1ac2014-12-30 11:30:13 +01001320 } else {
1321 if (!PartitionManager.Partition_SDCard())
1322 ret_val = 1; // failed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001323 }
that3f7b1ac2014-12-30 11:30:13 +01001324 }
1325 operation_end(ret_val);
1326 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001327
that3f7b1ac2014-12-30 11:30:13 +01001328}
Dees_Troy51a0e822012-09-05 15:24:24 -04001329
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001330int GUIAction::installhtcdumlock(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001331{
1332 operation_start("Install HTC Dumlock");
1333 if (simulate) {
1334 simulate_progress_bar();
1335 } else
1336 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -04001337
that3f7b1ac2014-12-30 11:30:13 +01001338 operation_end(0);
1339 return 0;
1340}
Dees_Troy51a0e822012-09-05 15:24:24 -04001341
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001342int GUIAction::htcdumlockrestoreboot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001343{
1344 operation_start("HTC Dumlock Restore Boot");
1345 if (simulate) {
1346 simulate_progress_bar();
1347 } else
1348 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001349
that3f7b1ac2014-12-30 11:30:13 +01001350 operation_end(0);
1351 return 0;
1352}
Dees_Troy51a0e822012-09-05 15:24:24 -04001353
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001354int GUIAction::htcdumlockreflashrecovery(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001355{
1356 operation_start("HTC Dumlock Reflash Recovery");
1357 if (simulate) {
1358 simulate_progress_bar();
1359 } else
1360 TWFunc::htc_dumlock_reflash_recovery_to_boot();
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
that3f7b1ac2014-12-30 11:30:13 +01001366int GUIAction::cmd(std::string arg)
1367{
1368 int op_status = 0;
1369
1370 operation_start("Command");
1371 LOGINFO("Running command: '%s'\n", arg.c_str());
1372 if (simulate) {
1373 simulate_progress_bar();
1374 } else {
1375 op_status = TWFunc::Exec_Cmd(arg);
1376 if (op_status != 0)
1377 op_status = 1;
1378 }
1379
1380 operation_end(op_status);
1381 return 0;
1382}
1383
1384int GUIAction::terminalcommand(std::string arg)
1385{
1386 int op_status = 0;
1387 string cmdpath, command;
1388
1389 DataManager::GetValue("tw_terminal_location", cmdpath);
1390 operation_start("CommandOutput");
1391 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
1392 if (simulate) {
1393 simulate_progress_bar();
1394 operation_end(op_status);
Matt Mower5aa29ab2015-02-25 23:50:55 -06001395 } else if (arg == "exit") {
1396 LOGINFO("Exiting terminal\n");
1397 operation_end(op_status);
1398 page("main");
that3f7b1ac2014-12-30 11:30:13 +01001399 } else {
1400 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
1401 LOGINFO("Actual command is: '%s'\n", command.c_str());
that3f7b1ac2014-12-30 11:30:13 +01001402 DataManager::SetValue("tw_terminal_state", 1);
1403 DataManager::SetValue("tw_background_thread_running", 1);
thatc6085482015-01-09 22:12:43 +01001404 FILE* fp;
1405 char line[512];
1406
1407 fp = popen(command.c_str(), "r");
1408 if (fp == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001409 LOGERR("Error opening command to run (%s).\n", strerror(errno));
thatc6085482015-01-09 22:12:43 +01001410 } else {
Matt Mower23d8aae2017-01-06 14:30:33 -06001411 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1;
thatc6085482015-01-09 22:12:43 +01001412 struct timeval timeout;
1413 fd_set fdset;
1414
Matt Mowera8a89d12016-12-30 18:10:37 -06001415 while (keep_going)
thatc6085482015-01-09 22:12:43 +01001416 {
1417 FD_ZERO(&fdset);
1418 FD_SET(fd, &fdset);
1419 timeout.tv_sec = 0;
1420 timeout.tv_usec = 400000;
1421 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1422 if (has_data == 0) {
1423 // Timeout reached
1424 DataManager::GetValue("tw_terminal_state", check);
1425 if (check == 0) {
1426 keep_going = 0;
1427 }
1428 } else if (has_data < 0) {
1429 // End of execution
1430 keep_going = 0;
1431 } else {
1432 // Try to read output
Matt Mowera8a89d12016-12-30 18:10:37 -06001433 if (fgets(line, sizeof(line), fp) != NULL)
thatc6085482015-01-09 22:12:43 +01001434 gui_print("%s", line); // Display output
1435 else
1436 keep_going = 0; // Done executing
1437 }
1438 }
1439 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -04001440 }
thatc6085482015-01-09 22:12:43 +01001441 DataManager::SetValue("tw_operation_status", 0);
1442 DataManager::SetValue("tw_operation_state", 1);
1443 DataManager::SetValue("tw_terminal_state", 0);
1444 DataManager::SetValue("tw_background_thread_running", 0);
1445 DataManager::SetValue(TW_ACTION_BUSY, 0);
that3f7b1ac2014-12-30 11:30:13 +01001446 }
1447 return 0;
1448}
1449
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001450int GUIAction::killterminal(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001451{
that3f7b1ac2014-12-30 11:30:13 +01001452 LOGINFO("Sending kill command...\n");
1453 operation_start("KillCommand");
1454 DataManager::SetValue("tw_operation_status", 0);
1455 DataManager::SetValue("tw_operation_state", 1);
1456 DataManager::SetValue("tw_terminal_state", 0);
1457 DataManager::SetValue("tw_background_thread_running", 0);
1458 DataManager::SetValue(TW_ACTION_BUSY, 0);
1459 return 0;
1460}
1461
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001462int GUIAction::reinjecttwrp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001463{
1464 int op_status = 0;
1465 operation_start("ReinjectTWRP");
Ethan Yonker74db1572015-10-28 12:44:49 -05001466 gui_msg("injecttwrp=Injecting TWRP into boot image...");
that3f7b1ac2014-12-30 11:30:13 +01001467 if (simulate) {
1468 simulate_progress_bar();
1469 } else {
1470 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Ethan Yonker74db1572015-10-28 12:44:49 -05001471 gui_msg("done=Done.");
that3f7b1ac2014-12-30 11:30:13 +01001472 }
1473
1474 operation_end(op_status);
1475 return 0;
1476}
1477
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001478int GUIAction::checkbackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001479{
1480 int op_status = 0;
1481
1482 operation_start("CheckBackupName");
1483 if (simulate) {
1484 simulate_progress_bar();
1485 } else {
Ethan Yonker53796e72019-01-11 22:49:52 -06001486 string Backup_Name;
1487 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
1488 op_status = PartitionManager.Check_Backup_Name(Backup_Name, true, true);
that3f7b1ac2014-12-30 11:30:13 +01001489 if (op_status != 0)
1490 op_status = 1;
1491 }
1492
1493 operation_end(op_status);
1494 return 0;
1495}
1496
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001497int GUIAction::decrypt(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001498{
1499 int op_status = 0;
1500
1501 operation_start("Decrypt");
1502 if (simulate) {
1503 simulate_progress_bar();
1504 } else {
1505 string Password;
Noah Jacobson0b25b1d2019-04-28 00:10:07 -04001506 string userID;
that3f7b1ac2014-12-30 11:30:13 +01001507 DataManager::GetValue("tw_crypto_password", Password);
Noah Jacobson0b25b1d2019-04-28 00:10:07 -04001508
1509 if (DataManager::GetIntValue(TW_IS_FBE)) { // for FBE
1510 DataManager::GetValue("tw_crypto_user_id", userID);
1511 if (userID != "") {
1512 op_status = PartitionManager.Decrypt_Device(Password, atoi(userID.c_str()));
1513 if (userID != "0") {
1514 if (op_status != 0)
1515 op_status = 1;
1516 operation_end(op_status);
1517 return 0;
1518 }
1519 } else {
1520 LOGINFO("User ID not found\n");
1521 op_status = 1;
1522 }
1523 ::sleep(1);
1524 } else { // for FDE
1525 op_status = PartitionManager.Decrypt_Device(Password);
1526 }
1527
that3f7b1ac2014-12-30 11:30:13 +01001528 if (op_status != 0)
1529 op_status = 1;
1530 else {
that3f7b1ac2014-12-30 11:30:13 +01001531 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1532
Ethan Yonkercf50da52015-01-12 21:59:07 -06001533 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001534
Ethan Yonkercf50da52015-01-12 21:59:07 -06001535 // Check for a custom theme and load it if exists
1536 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1537 if (has_datamedia != 0) {
1538 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06001539 LOGINFO("Failed to get default contexts and file mode for storage files.\n");
Ethan Yonkercf50da52015-01-12 21:59:07 -06001540 } else {
1541 LOGINFO("Got default contexts and file mode for storage files.\n");
that3f7b1ac2014-12-30 11:30:13 +01001542 }
1543 }
Ethan Yonker66a19492015-12-10 10:19:45 -06001544 PartitionManager.Decrypt_Adopted();
that3f7b1ac2014-12-30 11:30:13 +01001545 }
1546 }
1547
1548 operation_end(op_status);
1549 return 0;
1550}
1551
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001552int GUIAction::adbsideload(std::string arg __unused)
thatcc8ddca2015-01-03 01:59:36 +01001553{
1554 operation_start("Sideload");
1555 if (simulate) {
1556 simulate_progress_bar();
1557 operation_end(0);
1558 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001559 gui_msg("start_sideload=Starting ADB sideload feature...");
thatc6085482015-01-09 22:12:43 +01001560 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
1561
1562 // wait for the adb connection
bigbiffd58ba182020-03-23 10:02:29 -04001563 // int ret = apply_from_adb("/", &sideload_child_pid);
1564 Device::BuiltinAction reboot_action = Device::REBOOT_BOOTLOADER;
1565 int ret = ApplyFromAdb("/", &reboot_action);
thatc6085482015-01-09 22:12:43 +01001566 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
1567
1568 if (ret != 0) {
1569 if (ret == -2)
Ethan Yonker74db1572015-10-28 12:44:49 -05001570 gui_msg("need_new_adb=You need adb 1.0.32 or newer to sideload to this device.");
thatc6085482015-01-09 22:12:43 +01001571 ret = 1; // failure
1572 } else {
1573 int wipe_cache = 0;
1574 int wipe_dalvik = 0;
1575 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
1576
1577 if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) {
1578 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1579 PartitionManager.Wipe_By_Path("/cache");
1580 if (wipe_dalvik)
1581 PartitionManager.Wipe_Dalvik_Cache();
1582 } else {
1583 ret = 1; // failure
1584 }
thatcc8ddca2015-01-03 01:59:36 +01001585 }
thatc6085482015-01-09 22:12:43 +01001586 if (sideload_child_pid) {
1587 LOGINFO("Signaling child sideload process to exit.\n");
1588 struct stat st;
1589 // Calling stat() on this magic filename signals the minadbd
1590 // subprocess to shut down.
1591 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1592 int status;
1593 LOGINFO("Waiting for child sideload process to exit.\n");
1594 waitpid(sideload_child_pid, &status, 0);
1595 }
Dees Troy28a85d22015-04-28 02:03:16 +00001596 property_set("ctl.start", "adbd");
thatc6085482015-01-09 22:12:43 +01001597 TWFunc::Toggle_MTP(mtp_was_enabled);
1598 reinject_after_flash();
1599 operation_end(ret);
thatcc8ddca2015-01-03 01:59:36 +01001600 }
that3f7b1ac2014-12-30 11:30:13 +01001601 return 0;
1602}
1603
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001604int GUIAction::adbsideloadcancel(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001605{
that3f7b1ac2014-12-30 11:30:13 +01001606 struct stat st;
1607 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
Ethan Yonker74db1572015-10-28 12:44:49 -05001608 gui_msg("cancel_sideload=Cancelling ADB sideload...");
thatcc8ddca2015-01-03 01:59:36 +01001609 LOGINFO("Signaling child sideload process to exit.\n");
1610 // Calling stat() on this magic filename signals the minadbd
1611 // subprocess to shut down.
1612 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1613 if (!sideload_child_pid) {
1614 LOGERR("Unable to get child ID\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001615 return 0;
1616 }
thatcc8ddca2015-01-03 01:59:36 +01001617 ::sleep(1);
1618 LOGINFO("Killing child sideload process.\n");
1619 kill(sideload_child_pid, SIGTERM);
1620 int status;
1621 LOGINFO("Waiting for child sideload process to exit.\n");
1622 waitpid(sideload_child_pid, &status, 0);
1623 sideload_child_pid = 0;
that3f7b1ac2014-12-30 11:30:13 +01001624 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
1625 return 0;
1626}
1627
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001628int GUIAction::openrecoveryscript(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001629{
1630 operation_start("OpenRecoveryScript");
1631 if (simulate) {
1632 simulate_progress_bar();
Ethan Yonker24e7eb72015-01-03 16:13:06 -06001633 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001634 } else {
that10ae24f2015-12-26 20:53:51 +01001635 int op_status = OpenRecoveryScript::Run_OpenRecoveryScript_Action();
Ethan Yonkere1abe612015-06-09 11:09:32 -05001636 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001637 }
1638 return 0;
1639}
1640
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001641int GUIAction::installsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001642{
1643 int op_status = 0;
1644
1645 operation_start("Install SuperSU");
1646 if (simulate) {
1647 simulate_progress_bar();
1648 } else {
Ethan Yonkerfa67cbf2018-07-20 12:22:33 -05001649 LOGERR("Installing SuperSU was deprecated from TWRP.\n");
that3f7b1ac2014-12-30 11:30:13 +01001650 }
1651
1652 operation_end(op_status);
1653 return 0;
1654}
1655
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001656int GUIAction::fixsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001657{
1658 int op_status = 0;
1659
1660 operation_start("Fixing Superuser Permissions");
1661 if (simulate) {
1662 simulate_progress_bar();
1663 } else {
1664 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1665 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
1666 }
1667
1668 operation_end(op_status);
1669 return 0;
1670}
1671
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001672int GUIAction::decrypt_backup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001673{
1674 int op_status = 0;
1675
1676 operation_start("Try Restore Decrypt");
1677 if (simulate) {
1678 simulate_progress_bar();
1679 } else {
1680 string Restore_Path, Filename, Password;
1681 DataManager::GetValue("tw_restore", Restore_Path);
1682 Restore_Path += "/";
1683 DataManager::GetValue("tw_restore_password", Password);
1684 TWFunc::SetPerformanceMode(true);
1685 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1686 op_status = 0; // success
1687 else
1688 op_status = 1; // fail
1689 TWFunc::SetPerformanceMode(false);
1690 }
1691
1692 operation_end(op_status);
1693 return 0;
1694}
1695
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001696int GUIAction::repair(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001697{
1698 int op_status = 0;
1699
1700 operation_start("Repair Partition");
1701 if (simulate) {
1702 simulate_progress_bar();
1703 } else {
1704 string part_path;
1705 DataManager::GetValue("tw_partition_mount_point", part_path);
1706 if (PartitionManager.Repair_By_Path(part_path, true)) {
1707 op_status = 0; // success
1708 } else {
that3f7b1ac2014-12-30 11:30:13 +01001709 op_status = 1; // fail
1710 }
1711 }
1712
1713 operation_end(op_status);
1714 return 0;
1715}
1716
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001717int GUIAction::resize(std::string arg __unused)
Ethan Yonkera2719152015-05-28 09:44:41 -05001718{
1719 int op_status = 0;
1720
1721 operation_start("Resize Partition");
1722 if (simulate) {
1723 simulate_progress_bar();
1724 } else {
1725 string part_path;
1726 DataManager::GetValue("tw_partition_mount_point", part_path);
1727 if (PartitionManager.Resize_By_Path(part_path, true)) {
1728 op_status = 0; // success
1729 } else {
Ethan Yonkera2719152015-05-28 09:44:41 -05001730 op_status = 1; // fail
1731 }
1732 }
1733
1734 operation_end(op_status);
1735 return 0;
1736}
1737
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001738int GUIAction::changefilesystem(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001739{
1740 int op_status = 0;
1741
1742 operation_start("Change File System");
1743 if (simulate) {
1744 simulate_progress_bar();
1745 } else {
1746 string part_path, file_system;
1747 DataManager::GetValue("tw_partition_mount_point", part_path);
1748 DataManager::GetValue("tw_action_new_file_system", file_system);
1749 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1750 op_status = 0; // success
1751 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001752 gui_err("change_fs_err=Error changing file system.");
that3f7b1ac2014-12-30 11:30:13 +01001753 op_status = 1; // fail
1754 }
1755 }
1756 PartitionManager.Update_System_Details();
1757 operation_end(op_status);
1758 return 0;
1759}
1760
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001761int GUIAction::startmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001762{
1763 int op_status = 0;
1764
1765 operation_start("Start MTP");
1766 if (PartitionManager.Enable_MTP())
1767 op_status = 0; // success
1768 else
1769 op_status = 1; // fail
1770
1771 operation_end(op_status);
1772 return 0;
1773}
1774
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001775int GUIAction::stopmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001776{
1777 int op_status = 0;
1778
1779 operation_start("Stop MTP");
1780 if (PartitionManager.Disable_MTP())
1781 op_status = 0; // success
1782 else
1783 op_status = 1; // fail
1784
1785 operation_end(op_status);
1786 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001787}
1788
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001789int GUIAction::flashimage(std::string arg __unused)
Ethan Yonker96af84a2015-01-05 14:58:36 -06001790{
1791 int op_status = 0;
1792
1793 operation_start("Flash Image");
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001794 string path, filename;
1795 DataManager::GetValue("tw_zip_location", path);
1796 DataManager::GetValue("tw_file", filename);
Ethan Yonkere080c1f2016-09-19 13:50:25 -05001797 if (PartitionManager.Flash_Image(path, filename))
Ethan Yonker96af84a2015-01-05 14:58:36 -06001798 op_status = 0; // success
1799 else
1800 op_status = 1; // fail
1801
1802 operation_end(op_status);
1803 return 0;
1804}
1805
that10ae24f2015-12-26 20:53:51 +01001806int GUIAction::twcmd(std::string arg)
1807{
1808 operation_start("TWRP CLI Command");
1809 if (simulate)
1810 simulate_progress_bar();
1811 else
1812 OpenRecoveryScript::Run_CLI_Command(arg.c_str());
1813 operation_end(0);
1814 return 0;
1815}
1816
Dees_Troy51a0e822012-09-05 15:24:24 -04001817int GUIAction::getKeyByName(std::string key)
1818{
that8834a0f2016-01-05 23:29:30 +01001819 if (key == "home") return KEY_HOMEPAGE; // note: KEY_HOME is cursor movement (like KEY_END)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001820 else if (key == "menu") return KEY_MENU;
1821 else if (key == "back") return KEY_BACK;
1822 else if (key == "search") return KEY_SEARCH;
1823 else if (key == "voldown") return KEY_VOLUMEDOWN;
1824 else if (key == "volup") return KEY_VOLUMEUP;
1825 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001826 int ret_val;
1827 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1828 if (!ret_val)
1829 return KEY_POWER;
1830 else
1831 return ret_val;
1832 }
1833
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001834 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001835}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001836
1837int GUIAction::checkpartitionlifetimewrites(std::string arg)
1838{
1839 int op_status = 0;
1840 TWPartition* sys = PartitionManager.Find_Partition_By_Path(arg);
1841
1842 operation_start("Check Partition Lifetime Writes");
1843 if (sys) {
1844 if (sys->Check_Lifetime_Writes() != 0)
1845 DataManager::SetValue("tw_lifetime_writes", 1);
1846 else
1847 DataManager::SetValue("tw_lifetime_writes", 0);
1848 op_status = 0; // success
1849 } else {
1850 DataManager::SetValue("tw_lifetime_writes", 1);
1851 op_status = 1; // fail
1852 }
1853
1854 operation_end(op_status);
1855 return 0;
1856}
1857
1858int GUIAction::mountsystemtoggle(std::string arg)
1859{
1860 int op_status = 0;
Captain Throwback9d6feb52018-07-27 10:05:24 -04001861 bool remount_system = PartitionManager.Is_Mounted_By_Path(PartitionManager.Get_Android_Root_Path());
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001862 bool remount_vendor = PartitionManager.Is_Mounted_By_Path("/vendor");
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001863
1864 operation_start("Toggle System Mount");
Captain Throwback9d6feb52018-07-27 10:05:24 -04001865 if (!PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001866 op_status = 1; // fail
1867 } else {
Captain Throwback9d6feb52018-07-27 10:05:24 -04001868 TWPartition* Part = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001869 if (Part) {
Ethan Yonkerd6966f42015-05-30 14:52:16 -05001870 if (arg == "0") {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001871 DataManager::SetValue("tw_mount_system_ro", 0);
1872 Part->Change_Mount_Read_Only(false);
1873 } else {
1874 DataManager::SetValue("tw_mount_system_ro", 1);
1875 Part->Change_Mount_Read_Only(true);
1876 }
1877 if (remount_system) {
1878 Part->Mount(true);
1879 }
1880 op_status = 0; // success
1881 } else {
1882 op_status = 1; // fail
1883 }
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001884 Part = PartitionManager.Find_Partition_By_Path("/vendor");
1885 if (Part) {
1886 if (arg == "0") {
1887 Part->Change_Mount_Read_Only(false);
1888 } else {
1889 Part->Change_Mount_Read_Only(true);
1890 }
1891 if (remount_vendor) {
1892 Part->Mount(true);
1893 }
1894 op_status = 0; // success
1895 } else {
1896 op_status = 1; // fail
1897 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001898 }
1899
1900 operation_end(op_status);
1901 return 0;
1902}
Ethan Yonker74db1572015-10-28 12:44:49 -05001903
1904int GUIAction::setlanguage(std::string arg __unused)
1905{
1906 int op_status = 0;
1907
1908 operation_start("Set Language");
1909 PageManager::LoadLanguage(DataManager::GetStrValue("tw_language"));
1910 PageManager::RequestReload();
1911 op_status = 0; // success
1912
1913 operation_end(op_status);
1914 return 0;
1915}
Ethan Yonker1b190162016-12-05 15:25:19 -06001916
Matt Mower9472ba12016-01-20 18:12:47 -06001917int GUIAction::togglebacklight(std::string arg __unused)
1918{
1919 blankTimer.toggleBlank();
1920 return 0;
1921}
1922
Ethan Yonker1b190162016-12-05 15:25:19 -06001923int GUIAction::setbootslot(std::string arg)
1924{
1925 operation_start("Set Boot Slot");
1926 if (!simulate)
Ethan Yonker1b190162016-12-05 15:25:19 -06001927 PartitionManager.Set_Active_Slot(arg);
Matt Mower029a82d2017-01-08 13:12:38 -06001928 else
Ethan Yonker1b190162016-12-05 15:25:19 -06001929 simulate_progress_bar();
1930 operation_end(0);
1931 return 0;
1932}
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001933
1934int GUIAction::checkforapp(std::string arg __unused)
1935{
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001936 operation_start("Check for TWRP App");
1937 if (!simulate)
1938 {
1939 string sdkverstr = TWFunc::System_Property_Get("ro.build.version.sdk");
1940 int sdkver = 0;
1941 if (!sdkverstr.empty()) {
1942 sdkver = atoi(sdkverstr.c_str());
1943 }
1944 if (sdkver <= 13) {
1945 if (sdkver == 0)
1946 LOGINFO("Unable to read sdk version from build prop\n");
1947 else
1948 LOGINFO("SDK version too low for TWRP app (%i < 14)\n", sdkver);
Ethan Yonker64c5c0a2017-03-06 12:42:54 -06001949 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 -06001950 goto exit;
1951 }
Ethan Yonker76bbd3a2019-05-10 10:50:04 -05001952 if (TWFunc::Is_TWRP_App_In_System()) {
1953 DataManager::SetValue("tw_app_install_status", 2); // 0 = no status, 1 = not installed, 2 = already installed or do not install
1954 goto exit;
Ethan Yonker5e1a7f92017-01-18 16:44:54 -06001955 }
1956 if (PartitionManager.Mount_By_Path("/data", false)) {
Ethan Yonker5128d292017-02-04 19:52:56 -06001957 const char parent_path[] = "/data/app";
1958 const char app_prefix[] = "me.twrp.twrpapp-";
1959 DIR *d = opendir(parent_path);
1960 if (d) {
1961 struct dirent *p;
1962 while ((p = readdir(d))) {
1963 if (p->d_type != DT_DIR || strlen(p->d_name) < strlen(app_prefix) || strncmp(p->d_name, app_prefix, strlen(app_prefix)))
1964 continue;
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001965 closedir(d);
Ethan Yonker5128d292017-02-04 19:52:56 -06001966 LOGINFO("App found at '%s/%s'\n", parent_path, p->d_name);
Ethan Yonker64c5c0a2017-03-06 12:42:54 -06001967 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 -06001968 goto exit;
1969 }
Ethan Yonker5128d292017-02-04 19:52:56 -06001970 closedir(d);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001971 }
Ethan Yonker64c5c0a2017-03-06 12:42:54 -06001972 } else {
1973 LOGINFO("Data partition cannot be mounted during app check\n");
1974 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 -06001975 }
1976 } else
1977 simulate_progress_bar();
1978 LOGINFO("App not installed\n");
1979 DataManager::SetValue("tw_app_install_status", 1); // 0 = no status, 1 = not installed, 2 = already installed
1980exit:
1981 operation_end(0);
1982 return 0;
1983}
1984
1985int GUIAction::installapp(std::string arg __unused)
1986{
1987 int op_status = 1;
1988 operation_start("Install TWRP App");
1989 if (!simulate)
1990 {
1991 if (DataManager::GetIntValue("tw_mount_system_ro") > 0 || DataManager::GetIntValue("tw_app_install_system") == 0) {
1992 if (PartitionManager.Mount_By_Path("/data", true)) {
1993 string install_path = "/data/app";
1994 string context = "u:object_r:apk_data_file:s0";
1995 if (!TWFunc::Path_Exists(install_path)) {
1996 if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
1997 LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno));
1998 goto exit;
1999 }
2000 if (chown(install_path.c_str(), 1000, 1000)) {
2001 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
2002 goto exit;
2003 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002004 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002005 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2006 goto exit;
2007 }
2008 }
2009 install_path += "/me.twrp.twrpapp-1";
2010 if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
2011 LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno));
2012 goto exit;
2013 }
2014 if (chown(install_path.c_str(), 1000, 1000)) {
2015 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
2016 goto exit;
2017 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002018 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002019 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2020 goto exit;
2021 }
2022 install_path += "/base.apk";
2023 if (TWFunc::copy_file("/sbin/me.twrp.twrpapp.apk", install_path, 0644)) {
2024 LOGERR("Error copying apk file\n");
2025 goto exit;
2026 }
2027 if (chown(install_path.c_str(), 1000, 1000)) {
2028 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
2029 goto exit;
2030 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002031 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002032 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2033 goto exit;
2034 }
2035 sync();
2036 sync();
2037 }
2038 } else {
Captain Throwback9d6feb52018-07-27 10:05:24 -04002039 if (PartitionManager.Mount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) {
2040 string base_path = PartitionManager.Get_Android_Root_Path();
2041 if (TWFunc::Path_Exists(PartitionManager.Get_Android_Root_Path() + "/system"))
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002042 base_path += "/system"; // For devices with system as a root file system (e.g. Pixel)
2043 string install_path = base_path + "/priv-app";
2044 string context = "u:object_r:system_file:s0";
2045 if (!TWFunc::Path_Exists(install_path))
2046 install_path = base_path + "/app";
2047 if (TWFunc::Path_Exists(install_path)) {
2048 install_path += "/twrpapp";
2049 LOGINFO("Installing app to '%s'\n", install_path.c_str());
2050 if (mkdir(install_path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0) {
Ethan Yonker4767caf2017-01-11 10:45:04 -06002051 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002052 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2053 goto exit;
2054 }
2055 install_path += "/me.twrp.twrpapp.apk";
2056 if (TWFunc::copy_file("/sbin/me.twrp.twrpapp.apk", install_path, 0644)) {
2057 LOGERR("Error copying apk file\n");
2058 goto exit;
2059 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002060 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002061 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2062 goto exit;
2063 }
2064 sync();
2065 sync();
Captain Throwback9d6feb52018-07-27 10:05:24 -04002066 PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002067 op_status = 0;
2068 } else {
2069 LOGERR("Error making app directory '%s': %s\n", strerror(errno));
2070 }
2071 }
2072 }
2073 }
2074 } else
2075 simulate_progress_bar();
2076exit:
2077 operation_end(0);
2078 return 0;
2079}
Ethan Yonker53796e72019-01-11 22:49:52 -06002080
Ethan Yonker76bbd3a2019-05-10 10:50:04 -05002081int GUIAction::uninstalltwrpsystemapp(std::string arg __unused)
2082{
2083 int op_status = 1;
2084 operation_start("Uninstall TWRP System App");
2085 if (!simulate)
2086 {
2087 int Mount_System_RO = DataManager::GetIntValue("tw_mount_system_ro");
2088 TWPartition* Part = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path());
2089 if (!Part) {
2090 LOGERR("Unabled to find system partition.\n");
2091 goto exit;
2092 }
2093 if (!Part->UnMount(true)) {
2094 goto exit;
2095 }
2096 if (Mount_System_RO > 0) {
2097 DataManager::SetValue("tw_mount_system_ro", 0);
2098 Part->Change_Mount_Read_Only(false);
2099 }
2100 if (Part->Mount(true)) {
2101 string base_path = PartitionManager.Get_Android_Root_Path();
2102 if (TWFunc::Path_Exists(PartitionManager.Get_Android_Root_Path() + "/system"))
2103 base_path += "/system"; // For devices with system as a root file system (e.g. Pixel)
2104 string uninstall_path = base_path + "/priv-app";
2105 if (!TWFunc::Path_Exists(uninstall_path))
2106 uninstall_path = base_path + "/app";
2107 uninstall_path += "/twrpapp";
2108 if (TWFunc::Path_Exists(uninstall_path)) {
2109 LOGINFO("Uninstalling TWRP App from '%s'\n", uninstall_path.c_str());
2110 if (TWFunc::removeDir(uninstall_path, false) == 0) {
2111 sync();
2112 op_status = 0;
2113 DataManager::SetValue("tw_app_installed_in_system", 0);
2114 DataManager::SetValue("tw_app_install_status", 0);
2115 } else {
2116 LOGERR("Unable to remove TWRP app from system.\n");
2117 }
2118 } else {
2119 LOGINFO("didn't find TWRP app in '%s'\n", uninstall_path.c_str());
2120 }
2121 }
2122 Part->UnMount(true);
2123 if (Mount_System_RO > 0) {
2124 DataManager::SetValue("tw_mount_system_ro", Mount_System_RO);
2125 Part->Change_Mount_Read_Only(true);
2126 }
2127 } else
2128 simulate_progress_bar();
2129exit:
2130 operation_end(0);
2131 return 0;
2132}
2133
Ethan Yonker53796e72019-01-11 22:49:52 -06002134int GUIAction::repackimage(std::string arg __unused)
2135{
2136 int op_status = 1;
2137 operation_start("Repack Image");
2138 if (!simulate)
2139 {
2140 std::string path = DataManager::GetStrValue("tw_filename");
2141 Repack_Options_struct Repack_Options;
2142 Repack_Options.Disable_Verity = false;
2143 Repack_Options.Disable_Force_Encrypt = false;
2144 Repack_Options.Backup_First = DataManager::GetIntValue("tw_repack_backup_first") != 0;
2145 if (DataManager::GetIntValue("tw_repack_kernel") == 1)
2146 Repack_Options.Type = REPLACE_KERNEL;
2147 else
2148 Repack_Options.Type = REPLACE_RAMDISK;
2149 if (!PartitionManager.Repack_Images(path, Repack_Options))
2150 goto exit;
2151 } else
2152 simulate_progress_bar();
2153 op_status = 0;
2154exit:
2155 operation_end(op_status);
2156 return 0;
2157}
2158
2159int GUIAction::fixabrecoverybootloop(std::string arg __unused)
2160{
2161 int op_status = 1;
2162 operation_start("Repack Image");
2163 if (!simulate)
2164 {
2165 if (!TWFunc::Path_Exists("/sbin/magiskboot")) {
2166 LOGERR("Image repacking tool not present in this TWRP build!");
2167 goto exit;
2168 }
2169 DataManager::SetProgress(0);
2170 TWPartition* part = PartitionManager.Find_Partition_By_Path("/boot");
2171 if (part)
2172 gui_msg(Msg("unpacking_image=Unpacking {1}...")(part->Display_Name));
2173 else {
2174 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")("/boot"));
2175 goto exit;
2176 }
2177 if (!PartitionManager.Prepare_Repack(part, REPACK_ORIG_DIR, DataManager::GetIntValue("tw_repack_backup_first") != 0, gui_lookup("repack", "Repack")))
2178 goto exit;
2179 DataManager::SetProgress(.25);
2180 gui_msg("fixing_recovery_loop_patch=Patching kernel...");
2181 std::string command = "cd " REPACK_ORIG_DIR " && /sbin/magiskboot --hexpatch kernel 77616E745F696E697472616D667300 736B69705F696E697472616D667300";
2182 if (TWFunc::Exec_Cmd(command) != 0) {
2183 gui_msg(Msg(msg::kError, "fix_recovery_loop_patch_error=Error patching kernel."));
2184 goto exit;
2185 }
2186 std::string header_path = REPACK_ORIG_DIR;
2187 header_path += "header";
2188 if (TWFunc::Path_Exists(header_path)) {
2189 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";
2190 if (TWFunc::Exec_Cmd(command) != 0) {
2191 gui_msg(Msg(msg::kError, "fix_recovery_loop_patch_error=Error patching kernel."));
2192 goto exit;
2193 }
2194 }
2195 DataManager::SetProgress(.5);
2196 gui_msg(Msg("repacking_image=Repacking {1}...")(part->Display_Name));
2197 command = "cd " REPACK_ORIG_DIR " && /sbin/magiskboot --repack " REPACK_ORIG_DIR "boot.img";
2198 if (TWFunc::Exec_Cmd(command) != 0) {
2199 gui_msg(Msg(msg::kError, "repack_error=Error repacking image."));
2200 goto exit;
2201 }
2202 DataManager::SetProgress(.75);
2203 std::string path = REPACK_ORIG_DIR;
2204 std::string file = "new-boot.img";
2205 DataManager::SetValue("tw_flash_partition", "/boot;");
2206 if (!PartitionManager.Flash_Image(path, file)) {
2207 LOGINFO("Error flashing new image\n");
2208 goto exit;
2209 }
2210 DataManager::SetProgress(1);
2211 TWFunc::removeDir(REPACK_ORIG_DIR, false);
2212 } else
2213 simulate_progress_bar();
2214 op_status = 0;
2215exit:
2216 operation_end(op_status);
2217 return 0;
2218}