blob: 464ed2788233016302af2f582189e99c9f779871 [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>
bigbiffdf8436b2020-08-30 16:22:34 -040036#include <android-base/properties.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040037
38#include <string>
39#include <sstream>
40#include "../partitions.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040041#include "../twrp-functions.hpp"
bigbiffad58e1b2020-07-06 20:24:34 -040042#include "../twrpRepacker.hpp"
Dees_Troy812660f2012-09-20 09:55:17 -040043#include "../openrecoveryscript.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040044
bigbiff1f9e4842020-10-31 11:33:15 -040045#include "twinstall/adb_install.h"
bigbiffd58ba182020-03-23 10:02:29 -040046
47#include "fuse_sideload.h"
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050048#include "blanktimer.hpp"
bigbiff1f9e4842020-10-31 11:33:15 -040049#include "twinstall.h"
Ethan Yonkerfbb43532015-12-28 21:54:50 +010050
Dees_Troy51a0e822012-09-05 15:24:24 -040051extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000052#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040053#include "../variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000054#include "cutils/properties.h"
bigbiff673c7ae2020-12-02 19:44:56 -050055#include "twinstall/adb_install.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040056};
bigbiffd58ba182020-03-23 10:02:29 -040057#include "set_metadata.h"
bigbiffd81833a2021-01-17 11:06:57 -050058#include "minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040059
60#include "rapidxml.hpp"
61#include "objects.hpp"
bigbiffd58ba182020-03-23 10:02:29 -040062#include "tw_atomic.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040063
that3f7b1ac2014-12-30 11:30:13 +010064GUIAction::mapFunc GUIAction::mf;
thatc6085482015-01-09 22:12:43 +010065std::set<string> GUIAction::setActionsRunningInCallerThread;
that3f7b1ac2014-12-30 11:30:13 +010066static string zip_queue[10];
67static int zip_queue_index;
thatcc8ddca2015-01-03 01:59:36 +010068pid_t sideload_child_pid;
Noah Jacobson81d638d2019-04-28 00:10:07 -040069extern std::vector<users_struct> Users_List;
Mohd Faraz77bbeb02020-08-12 12:29:42 +000070extern GUITerminal* term;
that3f7b1ac2014-12-30 11:30:13 +010071
that73a52952015-01-28 23:07:34 +010072static void *ActionThread_work_wrapper(void *data);
73
74class ActionThread
75{
76public:
77 ActionThread();
78 ~ActionThread();
79
80 void threadActions(GUIAction *act);
81 void run(void *data);
82private:
83 friend void *ActionThread_work_wrapper(void*);
84 struct ThreadData
85 {
86 ActionThread *this_;
87 GUIAction *act;
88 ThreadData(ActionThread *this_, GUIAction *act) : this_(this_), act(act) {}
89 };
90
91 pthread_t m_thread;
92 bool m_thread_running;
93 pthread_mutex_t m_act_lock;
94};
95
96static ActionThread action_thread; // for all kinds of longer running actions
97static ActionThread cancel_thread; // for longer running "cancel" actions
thatc6085482015-01-09 22:12:43 +010098
99static void *ActionThread_work_wrapper(void *data)
100{
that73a52952015-01-28 23:07:34 +0100101 static_cast<ActionThread::ThreadData*>(data)->this_->run(data);
thatc6085482015-01-09 22:12:43 +0100102 return NULL;
103}
104
105ActionThread::ActionThread()
106{
107 m_thread_running = false;
108 pthread_mutex_init(&m_act_lock, NULL);
109}
110
111ActionThread::~ActionThread()
112{
113 pthread_mutex_lock(&m_act_lock);
Matt Mowera8a89d12016-12-30 18:10:37 -0600114 if (m_thread_running) {
thatc6085482015-01-09 22:12:43 +0100115 pthread_mutex_unlock(&m_act_lock);
116 pthread_join(m_thread, NULL);
117 } else {
118 pthread_mutex_unlock(&m_act_lock);
119 }
120 pthread_mutex_destroy(&m_act_lock);
121}
122
that7d3b54f2015-01-09 22:52:51 +0100123void ActionThread::threadActions(GUIAction *act)
thatc6085482015-01-09 22:12:43 +0100124{
125 pthread_mutex_lock(&m_act_lock);
126 if (m_thread_running) {
127 pthread_mutex_unlock(&m_act_lock);
that7d3b54f2015-01-09 22:52:51 +0100128 LOGERR("Another threaded action is already running -- not running %u actions starting with '%s'\n",
129 act->mActions.size(), act->mActions[0].mFunction.c_str());
thatc6085482015-01-09 22:12:43 +0100130 } else {
131 m_thread_running = true;
132 pthread_mutex_unlock(&m_act_lock);
that73a52952015-01-28 23:07:34 +0100133 ThreadData *d = new ThreadData(this, act);
thatc6085482015-01-09 22:12:43 +0100134 pthread_create(&m_thread, NULL, &ActionThread_work_wrapper, d);
135 }
136}
137
138void ActionThread::run(void *data)
139{
140 ThreadData *d = (ThreadData*)data;
141 GUIAction* act = d->act;
142
143 std::vector<GUIAction::Action>::iterator it;
that7d3b54f2015-01-09 22:52:51 +0100144 for (it = act->mActions.begin(); it != act->mActions.end(); ++it)
thatc6085482015-01-09 22:12:43 +0100145 act->doAction(*it);
146
147 pthread_mutex_lock(&m_act_lock);
148 m_thread_running = false;
149 pthread_mutex_unlock(&m_act_lock);
150 delete d;
151}
152
Dees_Troy51a0e822012-09-05 15:24:24 -0400153GUIAction::GUIAction(xml_node<>* node)
Vojtech Bocekede51c52014-02-07 23:58:09 +0100154 : GUIObject(node)
Dees_Troy51a0e822012-09-05 15:24:24 -0400155{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200156 xml_node<>* child;
157 xml_node<>* actions;
158 xml_attribute<>* attr;
Dees_Troy51a0e822012-09-05 15:24:24 -0400159
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200160 if (!node) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400161
that3f7b1ac2014-12-30 11:30:13 +0100162 if (mf.empty()) {
Vojtech Bocekecd89182015-01-15 16:28:54 +0100163#define ADD_ACTION(n) mf[#n] = &GUIAction::n
164#define ADD_ACTION_EX(name, func) mf[name] = &GUIAction::func
thatc6085482015-01-09 22:12:43 +0100165 // These actions will be run in the caller's thread
Vojtech Bocekecd89182015-01-15 16:28:54 +0100166 ADD_ACTION(reboot);
167 ADD_ACTION(home);
168 ADD_ACTION(key);
169 ADD_ACTION(page);
170 ADD_ACTION(reload);
171 ADD_ACTION(readBackup);
172 ADD_ACTION(set);
173 ADD_ACTION(clear);
174 ADD_ACTION(mount);
175 ADD_ACTION(unmount);
176 ADD_ACTION_EX("umount", unmount);
177 ADD_ACTION(restoredefaultsettings);
178 ADD_ACTION(copylog);
179 ADD_ACTION(compute);
180 ADD_ACTION_EX("addsubtract", compute);
181 ADD_ACTION(setguitimezone);
182 ADD_ACTION(overlay);
183 ADD_ACTION(queuezip);
184 ADD_ACTION(cancelzip);
185 ADD_ACTION(queueclear);
186 ADD_ACTION(sleep);
Matt Mower9a2a2052016-05-31 21:31:22 -0500187 ADD_ACTION(sleepcounter);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100188 ADD_ACTION(appenddatetobackupname);
189 ADD_ACTION(generatebackupname);
190 ADD_ACTION(checkpartitionlist);
191 ADD_ACTION(getpartitiondetails);
192 ADD_ACTION(screenshot);
193 ADD_ACTION(setbrightness);
194 ADD_ACTION(fileexists);
195 ADD_ACTION(killterminal);
196 ADD_ACTION(checkbackupname);
197 ADD_ACTION(adbsideloadcancel);
198 ADD_ACTION(fixsu);
199 ADD_ACTION(startmtp);
200 ADD_ACTION(stopmtp);
201 ADD_ACTION(cancelbackup);
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500202 ADD_ACTION(checkpartitionlifetimewrites);
203 ADD_ACTION(mountsystemtoggle);
Ethan Yonker74db1572015-10-28 12:44:49 -0500204 ADD_ACTION(setlanguage);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -0600205 ADD_ACTION(checkforapp);
Matt Mower9472ba12016-01-20 18:12:47 -0600206 ADD_ACTION(togglebacklight);
bigbiffdf8436b2020-08-30 16:22:34 -0400207 ADD_ACTION(enableadb);
208 ADD_ACTION(enablefastboot);
Mohd Faraz77bbeb02020-08-12 12:29:42 +0000209 ADD_ACTION(changeterminal);
bigbiffcfa875c2021-06-20 16:20:27 -0400210 ADD_ACTION(unmapsuperdevices);
thatc6085482015-01-09 22:12:43 +0100211
212 // remember actions that run in the caller thread
213 for (mapFunc::const_iterator it = mf.begin(); it != mf.end(); ++it)
214 setActionsRunningInCallerThread.insert(it->first);
215
216 // These actions will run in a separate thread
Vojtech Bocekecd89182015-01-15 16:28:54 +0100217 ADD_ACTION(flash);
218 ADD_ACTION(wipe);
219 ADD_ACTION(refreshsizes);
220 ADD_ACTION(nandroid);
Ethan Yonkerb5fab762016-01-28 14:03:33 -0600221 ADD_ACTION(fixcontexts);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100222 ADD_ACTION(fixpermissions);
223 ADD_ACTION(dd);
224 ADD_ACTION(partitionsd);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100225 ADD_ACTION(cmd);
226 ADD_ACTION(terminalcommand);
227 ADD_ACTION(reinjecttwrp);
228 ADD_ACTION(decrypt);
229 ADD_ACTION(adbsideload);
230 ADD_ACTION(openrecoveryscript);
231 ADD_ACTION(installsu);
232 ADD_ACTION(decrypt_backup);
233 ADD_ACTION(repair);
Ethan Yonkera2719152015-05-28 09:44:41 -0500234 ADD_ACTION(resize);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100235 ADD_ACTION(changefilesystem);
236 ADD_ACTION(flashimage);
that10ae24f2015-12-26 20:53:51 +0100237 ADD_ACTION(twcmd);
Ethan Yonker1b190162016-12-05 15:25:19 -0600238 ADD_ACTION(setbootslot);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -0600239 ADD_ACTION(installapp);
Ethan Yonker76bbd3a2019-05-10 10:50:04 -0500240 ADD_ACTION(uninstalltwrpsystemapp);
Ethan Yonker53796e72019-01-11 22:49:52 -0600241 ADD_ACTION(repackimage);
nebrassyac29e692021-05-20 13:03:30 +0200242 ADD_ACTION(reflashtwrp);
Ethan Yonker53796e72019-01-11 22:49:52 -0600243 ADD_ACTION(fixabrecoverybootloop);
epicXa721f952021-01-04 13:01:31 +0530244 ADD_ACTION(applycustomtwrpfolder);
Captain Throwback16dd81b2021-02-12 19:32:36 -0500245#ifndef TW_EXCLUDE_NANO
246 ADD_ACTION(editfile);
247#endif
that3f7b1ac2014-12-30 11:30:13 +0100248 }
249
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200250 // First, get the action
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600251 actions = FindNode(node, "actions");
252 if (actions) child = FindNode(actions, "action");
253 else child = FindNode(node, "action");
Dees_Troy51a0e822012-09-05 15:24:24 -0400254
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200255 if (!child) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400256
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200257 while (child)
258 {
259 Action action;
Dees_Troy51a0e822012-09-05 15:24:24 -0400260
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200261 attr = child->first_attribute("function");
262 if (!attr) return;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500263
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200264 action.mFunction = attr->value();
265 action.mArg = child->value();
266 mActions.push_back(action);
Dees_Troy51a0e822012-09-05 15:24:24 -0400267
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200268 child = child->next_sibling("action");
269 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400270
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200271 // Now, let's get either the key or region
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600272 child = FindNode(node, "touch");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200273 if (child)
274 {
275 attr = child->first_attribute("key");
276 if (attr)
277 {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100278 std::vector<std::string> keys = TWFunc::Split_String(attr->value(), "+");
Matt Mowera8a89d12016-12-30 18:10:37 -0600279 for (size_t i = 0; i < keys.size(); ++i)
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100280 {
281 const int key = getKeyByName(keys[i]);
282 mKeys[key] = false;
283 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200284 }
285 else
286 {
287 attr = child->first_attribute("x");
288 if (!attr) return;
289 mActionX = atol(attr->value());
290 attr = child->first_attribute("y");
291 if (!attr) return;
292 mActionY = atol(attr->value());
293 attr = child->first_attribute("w");
294 if (!attr) return;
295 mActionW = atol(attr->value());
296 attr = child->first_attribute("h");
297 if (!attr) return;
298 mActionH = atol(attr->value());
299 }
300 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400301}
302
Matt Mower25036b72016-06-24 12:30:18 -0500303int GUIAction::NotifyTouch(TOUCH_STATE state, int x __unused, int y __unused)
Dees_Troy51a0e822012-09-05 15:24:24 -0400304{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200305 if (state == TOUCH_RELEASE)
306 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400307
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200308 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400309}
310
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100311int GUIAction::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -0400312{
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100313 std::map<int, bool>::iterator itr = mKeys.find(key);
Matt Mowera8a89d12016-12-30 18:10:37 -0600314 if (itr == mKeys.end())
that8834a0f2016-01-05 23:29:30 +0100315 return 1;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100316
317 bool prevState = itr->second;
318 itr->second = down;
319
320 // If there is only one key for this action, wait for key up so it
321 // doesn't trigger with multi-key actions.
322 // Else, check if all buttons are pressed, then consume their release events
323 // so they don't trigger one-button actions and reset mKeys pressed status
Matt Mowera8a89d12016-12-30 18:10:37 -0600324 if (mKeys.size() == 1) {
325 if (!down && prevState) {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100326 doActions();
thatd4725ca2016-01-19 00:15:21 +0100327 return 0;
328 }
Matt Mowera8a89d12016-12-30 18:10:37 -0600329 } else if (down) {
330 for (itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
331 if (!itr->second)
that8834a0f2016-01-05 23:29:30 +0100332 return 1;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100333 }
334
335 // Passed, all req buttons are pressed, reset them and consume release events
336 HardwareKeyboard *kb = PageManager::GetHardwareKeyboard();
Matt Mowera8a89d12016-12-30 18:10:37 -0600337 for (itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100338 kb->ConsumeKeyRelease(itr->first);
339 itr->second = false;
340 }
341
342 doActions();
thatd4725ca2016-01-19 00:15:21 +0100343 return 0;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100344 }
345
thatd4725ca2016-01-19 00:15:21 +0100346 return 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400347}
348
Vojtech Bocek07220562014-02-08 02:05:33 +0100349int GUIAction::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400350{
Vojtech Bocek07220562014-02-08 02:05:33 +0100351 GUIObject::NotifyVarChange(varName, value);
352
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100353 if (varName.empty() && !isConditionValid() && mKeys.empty() && !mActionW)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200354 doActions();
Matt Mowera8a89d12016-12-30 18:10:37 -0600355 else if ((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200356 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400357
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200358 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400359}
360
361void GUIAction::simulate_progress_bar(void)
362{
Ethan Yonker74db1572015-10-28 12:44:49 -0500363 gui_msg("simulating=Simulating actions...");
Dees_Troy51a0e822012-09-05 15:24:24 -0400364 for (int i = 0; i < 5; i++)
365 {
bigbiff7abc5fe2015-01-17 16:53:12 -0500366 if (PartitionManager.stop_backup.get_value()) {
367 DataManager::SetValue("tw_cancel_backup", 1);
Matt Mower3c366972015-12-25 19:28:31 -0600368 gui_msg("backup_cancel=Backup Cancelled");
bigbiff7abc5fe2015-01-17 16:53:12 -0500369 DataManager::SetValue("ui_progress", 0);
370 PartitionManager.stop_backup.set_value(0);
371 return;
372 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400373 usleep(500000);
374 DataManager::SetValue("ui_progress", i * 20);
375 }
376}
377
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600378int GUIAction::flash_zip(std::string filename, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400379{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200380 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400381
382 DataManager::SetValue("ui_progress", 0);
Chaosmasterd5364a02020-02-03 15:38:02 +0100383 DataManager::SetValue("ui_portion_size", 0);
384 DataManager::SetValue("ui_portion_start", 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400385
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200386 if (filename.empty())
387 {
388 LOGERR("No file specified.\n");
389 return -1;
390 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400391
Ethan Yonker9598c072016-01-15 21:54:34 -0600392 if (!TWFunc::Path_Exists(filename)) {
393 if (!PartitionManager.Mount_By_Path(filename, true)) {
394 return -1;
395 }
396 if (!TWFunc::Path_Exists(filename)) {
397 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")(filename));
398 return -1;
399 }
400 }
Dees_Troy657c3092012-09-10 20:32:10 -0400401
Dees_Troy51a0e822012-09-05 15:24:24 -0400402 if (simulate) {
403 simulate_progress_bar();
404 } else {
bigbiffab036612021-06-24 21:31:40 -0400405 char apex_enabled[PROPERTY_VALUE_MAX];
406 property_get("twrp.apex.flattened", apex_enabled, "");
407 if (strcmp(apex_enabled, "true") == 0) {
408 umount("/apex");
409 }
epicX9597b842021-03-20 21:58:17 +0530410 ret_val = TWinstall_zip(filename.c_str(), wipe_cache, (bool) !DataManager::GetIntValue(TW_SKIP_DIGEST_CHECK_ZIP_VAR));
bigbiff1f9e4842020-10-31 11:33:15 -0400411 PartitionManager.Unlock_Block_Partitions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400412 // Now, check if we need to ensure TWRP remains installed...
413 struct stat st;
bigbiffad58e1b2020-07-06 20:24:34 -0400414 if (stat("/system/bin/installTwrp", &st) == 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400415 {
416 DataManager::SetValue("tw_operation", "Configuring TWRP");
417 DataManager::SetValue("tw_partition", "");
Ethan Yonker74db1572015-10-28 12:44:49 -0500418 gui_msg("config_twrp=Configuring TWRP...");
bigbiffad58e1b2020-07-06 20:24:34 -0400419 if (TWFunc::Exec_Cmd("/system/bin/installTwrp reinstall") < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400420 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500421 gui_msg("config_twrp_err=Unable to configure TWRP with this kernel.");
Dees_Troy51a0e822012-09-05 15:24:24 -0400422 }
423 }
424 }
425
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200426 // Done
427 DataManager::SetValue("ui_progress", 100);
428 DataManager::SetValue("ui_progress", 0);
Chaosmasterd5364a02020-02-03 15:38:02 +0100429 DataManager::SetValue("ui_portion_size", 0);
430 DataManager::SetValue("ui_portion_start", 0);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200431 return ret_val;
Dees_Troy51a0e822012-09-05 15:24:24 -0400432}
433
that73a52952015-01-28 23:07:34 +0100434GUIAction::ThreadType GUIAction::getThreadType(const GUIAction::Action& action)
thatc6085482015-01-09 22:12:43 +0100435{
that73a52952015-01-28 23:07:34 +0100436 string func = gui_parse_text(action.mFunction);
437 bool needsThread = setActionsRunningInCallerThread.find(func) == setActionsRunningInCallerThread.end();
438 if (needsThread) {
439 if (func == "cancelbackup")
440 return THREAD_CANCEL;
441 else
442 return THREAD_ACTION;
443 }
444 return THREAD_NONE;
thatc6085482015-01-09 22:12:43 +0100445}
446
Dees_Troy51a0e822012-09-05 15:24:24 -0400447int GUIAction::doActions()
448{
that3f7b1ac2014-12-30 11:30:13 +0100449 if (mActions.size() < 1)
450 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400451
that73a52952015-01-28 23:07:34 +0100452 // Determine in which thread to run the actions.
453 // Do it for all actions at once before starting, so that we can cancel the whole batch if the thread is already busy.
454 ThreadType threadType = THREAD_NONE;
that3f7b1ac2014-12-30 11:30:13 +0100455 std::vector<Action>::iterator it;
that73a52952015-01-28 23:07:34 +0100456 for (it = mActions.begin(); it != mActions.end(); ++it) {
457 ThreadType tt = getThreadType(*it);
458 if (tt == THREAD_NONE)
459 continue;
460 if (threadType == THREAD_NONE)
461 threadType = tt;
462 else if (threadType != tt) {
463 LOGERR("Can't mix normal and cancel actions in the same list.\n"
464 "Running the whole batch in the cancel thread.\n");
465 threadType = THREAD_CANCEL;
thatc6085482015-01-09 22:12:43 +0100466 break;
467 }
that7d3b54f2015-01-09 22:52:51 +0100468 }
that73a52952015-01-28 23:07:34 +0100469
470 // Now run the actions in the desired thread.
471 switch (threadType) {
472 case THREAD_ACTION:
473 action_thread.threadActions(this);
474 break;
475
476 case THREAD_CANCEL:
477 cancel_thread.threadActions(this);
478 break;
479
480 default: {
481 // no iterators here because theme reloading might kill our object
482 const size_t cnt = mActions.size();
483 for (size_t i = 0; i < cnt; ++i)
484 doAction(mActions[i]);
485 }
thatc6085482015-01-09 22:12:43 +0100486 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400487
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200488 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400489}
490
that3f7b1ac2014-12-30 11:30:13 +0100491int GUIAction::doAction(Action action)
Dees_Troy51a0e822012-09-05 15:24:24 -0400492{
that3f7b1ac2014-12-30 11:30:13 +0100493 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400494
that3f7b1ac2014-12-30 11:30:13 +0100495 std::string function = gui_parse_text(action.mFunction);
496 std::string arg = gui_parse_text(action.mArg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400497
that3f7b1ac2014-12-30 11:30:13 +0100498 // find function and execute it
499 mapFunc::const_iterator funcitr = mf.find(function);
500 if (funcitr != mf.end())
501 return (this->*funcitr->second)(arg);
502
503 LOGERR("Unknown action '%s'\n", function.c_str());
504 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400505}
506
507void GUIAction::operation_start(const string operation_name)
508{
that3f7b1ac2014-12-30 11:30:13 +0100509 LOGINFO("operation_start: '%s'\n", operation_name.c_str());
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000510 time(&Start);
Dees_Troy51a0e822012-09-05 15:24:24 -0400511 DataManager::SetValue(TW_ACTION_BUSY, 1);
512 DataManager::SetValue("ui_progress", 0);
Chaosmasterd5364a02020-02-03 15:38:02 +0100513 DataManager::SetValue("ui_portion_size", 0);
514 DataManager::SetValue("ui_portion_start", 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400515 DataManager::SetValue("tw_operation", operation_name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400516 DataManager::SetValue("tw_operation_state", 0);
Ethan Yonkerd83c9ea2015-01-02 15:22:31 -0600517 DataManager::SetValue("tw_operation_status", 0);
bigbiff25d25b92020-06-19 16:07:38 -0400518 bool tw_ab_device = TWFunc::get_log_dir() != CACHE_LOGS_DIR;
bigbiff bigbiff19874f12019-01-08 20:06:57 -0500519 DataManager::SetValue("tw_ab_device", tw_ab_device);
Dees_Troy51a0e822012-09-05 15:24:24 -0400520}
521
that3f7b1ac2014-12-30 11:30:13 +0100522void GUIAction::operation_end(const int operation_status)
Dees_Troy51a0e822012-09-05 15:24:24 -0400523{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000524 time_t Stop;
Dees_Troy51a0e822012-09-05 15:24:24 -0400525 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400526 DataManager::SetValue("ui_progress", 100);
527 if (simulate) {
528 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
529 if (simulate_fail != 0)
530 DataManager::SetValue("tw_operation_status", 1);
531 else
532 DataManager::SetValue("tw_operation_status", 0);
533 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500534 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400535 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500536 }
537 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400538 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500539 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400540 }
541 DataManager::SetValue("tw_operation_state", 1);
542 DataManager::SetValue(TW_ACTION_BUSY, 0);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500543 blankTimer.resetTimerAndUnblank();
LuK133762326f42015-09-30 19:57:46 +0200544 property_set("twrp.action_complete", "1");
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000545 time(&Stop);
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400546
547#ifndef TW_NO_HAPTICS
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000548 if ((int) difftime(Stop, Start) > 10)
Ethan Yonker03db3262014-02-05 08:02:06 -0600549 DataManager::Vibrate("tw_action_vibrate");
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400550#endif
551
that3f7b1ac2014-12-30 11:30:13 +0100552 LOGINFO("operation_end - status=%d\n", operation_status);
Dees_Troy51a0e822012-09-05 15:24:24 -0400553}
554
that3f7b1ac2014-12-30 11:30:13 +0100555int GUIAction::reboot(std::string arg)
Dees_Troy51a0e822012-09-05 15:24:24 -0400556{
that3f7b1ac2014-12-30 11:30:13 +0100557 sync();
558 DataManager::SetValue("tw_gui_done", 1);
559 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400560
that3f7b1ac2014-12-30 11:30:13 +0100561 return 0;
562}
Dees_Troy51a0e822012-09-05 15:24:24 -0400563
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500564int GUIAction::home(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100565{
that3f7b1ac2014-12-30 11:30:13 +0100566 gui_changePage("main");
567 return 0;
568}
Dees_Troy51a0e822012-09-05 15:24:24 -0400569
that3f7b1ac2014-12-30 11:30:13 +0100570int GUIAction::key(std::string arg)
571{
572 const int key = getKeyByName(arg);
573 PageManager::NotifyKey(key, true);
574 PageManager::NotifyKey(key, false);
575 return 0;
576}
577
578int GUIAction::page(std::string arg)
579{
LuK133762326f42015-09-30 19:57:46 +0200580 property_set("twrp.action_complete", "0");
that3f7b1ac2014-12-30 11:30:13 +0100581 std::string page_name = gui_parse_text(arg);
582 return gui_changePage(page_name);
583}
584
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500585int GUIAction::reload(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100586{
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500587 PageManager::RequestReload();
588 // The actual reload is handled in pages.cpp in PageManager::RunReload()
589 // The reload will occur on the next Update or Render call and will
590 // be performed in the rendoer thread instead of the action thread
591 // to prevent crashing which could occur when we start deleting
592 // GUI resources in the action thread while we attempt to render
593 // with those same resources in another thread.
that3f7b1ac2014-12-30 11:30:13 +0100594 return 0;
595}
Dees_Troy51a0e822012-09-05 15:24:24 -0400596
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500597int GUIAction::readBackup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100598{
599 string Restore_Name;
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -0400600
that3f7b1ac2014-12-30 11:30:13 +0100601 DataManager::GetValue("tw_restore", Restore_Name);
602 PartitionManager.Set_Restore_Files(Restore_Name);
603 return 0;
604}
605
606int GUIAction::set(std::string arg)
607{
608 if (arg.find('=') != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200609 {
that3f7b1ac2014-12-30 11:30:13 +0100610 string varName = arg.substr(0, arg.find('='));
611 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400612
that3f7b1ac2014-12-30 11:30:13 +0100613 DataManager::GetValue(value, value);
614 DataManager::SetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200615 }
that3f7b1ac2014-12-30 11:30:13 +0100616 else
617 DataManager::SetValue(arg, "1");
618 return 0;
619}
Dees_Troy51a0e822012-09-05 15:24:24 -0400620
that3f7b1ac2014-12-30 11:30:13 +0100621int GUIAction::clear(std::string arg)
622{
623 DataManager::SetValue(arg, "0");
624 return 0;
625}
Dees_Troy51a0e822012-09-05 15:24:24 -0400626
that3f7b1ac2014-12-30 11:30:13 +0100627int GUIAction::mount(std::string arg)
628{
629 if (arg == "usb") {
630 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400631 if (!simulate)
that3f7b1ac2014-12-30 11:30:13 +0100632 PartitionManager.usb_storage_enable();
633 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500634 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100635 } else if (!simulate) {
636 PartitionManager.Mount_By_Path(arg, true);
637 PartitionManager.Add_MTP_Storage(arg);
638 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500639 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100640 return 0;
641}
642
643int GUIAction::unmount(std::string arg)
644{
645 if (arg == "usb") {
646 if (!simulate)
647 PartitionManager.usb_storage_disable();
648 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500649 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100650 DataManager::SetValue(TW_ACTION_BUSY, 0);
651 } else if (!simulate) {
652 PartitionManager.UnMount_By_Path(arg, true);
653 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500654 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100655 return 0;
656}
657
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500658int GUIAction::restoredefaultsettings(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100659{
660 operation_start("Restore Defaults");
661 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
Ethan Yonker74db1572015-10-28 12:44:49 -0500662 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100663 else {
664 DataManager::ResetDefaults();
665 PartitionManager.Update_System_Details();
666 PartitionManager.Mount_Current_Storage(true);
667 }
668 operation_end(0);
669 return 0;
670}
671
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500672int GUIAction::copylog(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100673{
674 operation_start("Copy Log");
675 if (!simulate)
676 {
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400677 string dst, curr_storage;
678 int copy_kernel_log = 0;
Captain Throwback52978932021-09-05 15:11:07 -0400679 int copy_logcat = 1;
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400680
681 DataManager::GetValue("tw_include_kernel_log", copy_kernel_log);
Captain Throwback52978932021-09-05 15:11:07 -0400682 DataManager::GetValue("tw_include_logcat", copy_logcat);
that3f7b1ac2014-12-30 11:30:13 +0100683 PartitionManager.Mount_Current_Storage(true);
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400684 curr_storage = DataManager::GetCurrentStoragePath();
685 dst = curr_storage + "/recovery.log";
that3f7b1ac2014-12-30 11:30:13 +0100686 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
687 tw_set_default_metadata(dst.c_str());
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400688 if (copy_kernel_log)
689 TWFunc::copy_kernel_log(curr_storage);
Captain Throwback52978932021-09-05 15:11:07 -0400690 if (copy_logcat)
691 TWFunc::copy_logcat(curr_storage);
that3f7b1ac2014-12-30 11:30:13 +0100692 sync();
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400693 gui_msg(Msg("copy_log=Copied recovery log to {1}")(dst));
that3f7b1ac2014-12-30 11:30:13 +0100694 } else
695 simulate_progress_bar();
696 operation_end(0);
697 return 0;
698}
699
700
701int GUIAction::compute(std::string arg)
702{
703 if (arg.find("+") != string::npos)
704 {
705 string varName = arg.substr(0, arg.find('+'));
706 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
707 int amount_to_add = atoi(string_to_add.c_str());
708 int value;
709
710 DataManager::GetValue(varName, value);
711 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400712 return 0;
713 }
that3f7b1ac2014-12-30 11:30:13 +0100714 if (arg.find("-") != string::npos)
Dees_Troy51a0e822012-09-05 15:24:24 -0400715 {
that3f7b1ac2014-12-30 11:30:13 +0100716 string varName = arg.substr(0, arg.find('-'));
717 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
718 int amount_to_subtract = atoi(string_to_subtract.c_str());
719 int value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400720
that3f7b1ac2014-12-30 11:30:13 +0100721 DataManager::GetValue(varName, value);
722 value -= amount_to_subtract;
723 if (value <= 0)
724 value = 0;
725 DataManager::SetValue(varName, value);
726 return 0;
727 }
728 if (arg.find("*") != string::npos)
729 {
730 string varName = arg.substr(0, arg.find('*'));
731 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
732 int multiply_by = atoi(multiply_by_str.c_str());
733 int value;
734
735 DataManager::GetValue(varName, value);
736 DataManager::SetValue(varName, value*multiply_by);
737 return 0;
738 }
739 if (arg.find("/") != string::npos)
740 {
741 string varName = arg.substr(0, arg.find('/'));
742 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
743 int divide_by = atoi(divide_by_str.c_str());
744 int value;
745
Matt Mowera8a89d12016-12-30 18:10:37 -0600746 if (divide_by != 0)
that3f7b1ac2014-12-30 11:30:13 +0100747 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400748 DataManager::GetValue(varName, value);
that3f7b1ac2014-12-30 11:30:13 +0100749 DataManager::SetValue(varName, value/divide_by);
Dees_Troy51a0e822012-09-05 15:24:24 -0400750 }
751 return 0;
752 }
that3f7b1ac2014-12-30 11:30:13 +0100753 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
754 return -1;
755}
Dees_Troy51a0e822012-09-05 15:24:24 -0400756
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500757int GUIAction::setguitimezone(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100758{
759 string SelectedZone;
760 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
761 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
762 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
763
764 int dst;
765 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
766
767 string offset;
768 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
769
770 string NewTimeZone = Zone;
771 if (offset != "0")
772 NewTimeZone += ":" + offset;
773
774 if (dst != 0)
775 NewTimeZone += DSTZone;
776
777 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
778 DataManager::update_tz_environment_variables();
779 return 0;
780}
781
782int GUIAction::overlay(std::string arg)
783{
784 return gui_changeOverlay(arg);
785}
786
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500787int GUIAction::queuezip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100788{
789 if (zip_queue_index >= 10) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500790 gui_msg("max_queue=Maximum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400791 return 0;
792 }
that3f7b1ac2014-12-30 11:30:13 +0100793 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
794 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
795 zip_queue_index++;
Dees_Troy51a0e822012-09-05 15:24:24 -0400796 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +0100797 }
798 return 0;
799}
800
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500801int GUIAction::cancelzip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100802{
803 if (zip_queue_index <= 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500804 gui_msg("min_queue=Minimum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400805 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100806 } else {
807 zip_queue_index--;
808 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
Dees_Troy51a0e822012-09-05 15:24:24 -0400809 }
that3f7b1ac2014-12-30 11:30:13 +0100810 return 0;
811}
Dees_Troy51a0e822012-09-05 15:24:24 -0400812
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500813int GUIAction::queueclear(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100814{
815 zip_queue_index = 0;
816 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
817 return 0;
818}
Dees_Troy51a0e822012-09-05 15:24:24 -0400819
that3f7b1ac2014-12-30 11:30:13 +0100820int GUIAction::sleep(std::string arg)
821{
822 operation_start("Sleep");
823 usleep(atoi(arg.c_str()));
824 operation_end(0);
825 return 0;
826}
Dees Troyb21cc642013-09-10 17:36:41 +0000827
Matt Mower9a2a2052016-05-31 21:31:22 -0500828int GUIAction::sleepcounter(std::string arg)
829{
830 operation_start("SleepCounter");
831 // Ensure user notices countdown in case it needs to be cancelled
832 blankTimer.resetTimerAndUnblank();
833 int total = atoi(arg.c_str());
834 for (int t = total; t > 0; t--) {
835 int progress = (int)(((float)(total-t)/(float)total)*100.0);
836 DataManager::SetValue("ui_progress", progress);
837 ::sleep(1);
838 DataManager::SetValue("tw_sleep", t-1);
839 }
840 DataManager::SetValue("ui_progress", 100);
841 operation_end(0);
842 return 0;
843}
844
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500845int GUIAction::appenddatetobackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100846{
847 operation_start("AppendDateToBackupName");
848 string Backup_Name;
849 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
850 Backup_Name += TWFunc::Get_Current_Date();
851 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
852 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
853 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
Matt Mower76b8afc2016-06-24 12:04:27 -0500854 PageManager::NotifyKey(KEY_END, true);
855 PageManager::NotifyKey(KEY_END, false);
that3f7b1ac2014-12-30 11:30:13 +0100856 operation_end(0);
857 return 0;
858}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500859
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500860int GUIAction::generatebackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100861{
862 operation_start("GenerateBackupName");
863 TWFunc::Auto_Generate_Backup_Name();
864 operation_end(0);
865 return 0;
866}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500867
Ethan Yonker483e9f42016-01-11 22:21:18 -0600868int GUIAction::checkpartitionlist(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100869{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600870 string List, part_path;
that3f7b1ac2014-12-30 11:30:13 +0100871 int count = 0;
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500872
Ethan Yonker483e9f42016-01-11 22:21:18 -0600873 if (arg.empty())
874 arg = "tw_wipe_list";
875 DataManager::GetValue(arg, List);
876 LOGINFO("checkpartitionlist list '%s'\n", List.c_str());
877 if (!List.empty()) {
878 size_t start_pos = 0, end_pos = List.find(";", start_pos);
879 while (end_pos != string::npos && start_pos < List.size()) {
880 part_path = List.substr(start_pos, end_pos - start_pos);
881 LOGINFO("checkpartitionlist part_path '%s'\n", part_path.c_str());
882 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100883 // Do nothing
Dees_Troy51a0e822012-09-05 15:24:24 -0400884 } else {
that3f7b1ac2014-12-30 11:30:13 +0100885 count++;
886 }
887 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600888 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100889 }
890 DataManager::SetValue("tw_check_partition_list", count);
891 } else {
892 DataManager::SetValue("tw_check_partition_list", 0);
893 }
Matt Mowera8a89d12016-12-30 18:10:37 -0600894 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100895}
Dees_Troy51a0e822012-09-05 15:24:24 -0400896
Ethan Yonker483e9f42016-01-11 22:21:18 -0600897int GUIAction::getpartitiondetails(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100898{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600899 string List, part_path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400900
Ethan Yonker483e9f42016-01-11 22:21:18 -0600901 if (arg.empty())
902 arg = "tw_wipe_list";
903 DataManager::GetValue(arg, List);
904 LOGINFO("getpartitiondetails list '%s'\n", List.c_str());
905 if (!List.empty()) {
906 size_t start_pos = 0, end_pos = List.find(";", start_pos);
907 part_path = List;
908 while (end_pos != string::npos && start_pos < List.size()) {
909 part_path = List.substr(start_pos, end_pos - start_pos);
910 LOGINFO("getpartitiondetails part_path '%s'\n", part_path.c_str());
911 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100912 // Do nothing
913 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600914 DataManager::SetValue("tw_partition_path", part_path);
that3f7b1ac2014-12-30 11:30:13 +0100915 break;
916 }
917 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600918 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100919 }
Ethan Yonker483e9f42016-01-11 22:21:18 -0600920 if (!part_path.empty()) {
921 TWPartition* Part = PartitionManager.Find_Partition_By_Path(part_path);
that3f7b1ac2014-12-30 11:30:13 +0100922 if (Part) {
923 unsigned long long mb = 1048576;
Dees_Troya13d74f2013-03-24 08:54:55 -0500924
that3f7b1ac2014-12-30 11:30:13 +0100925 DataManager::SetValue("tw_partition_name", Part->Display_Name);
926 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
927 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
928 DataManager::SetValue("tw_partition_size", Part->Size / mb);
929 DataManager::SetValue("tw_partition_used", Part->Used / mb);
930 DataManager::SetValue("tw_partition_free", Part->Free / mb);
931 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
932 DataManager::SetValue("tw_partition_removable", Part->Removable);
933 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
934
935 if (Part->Can_Repair())
936 DataManager::SetValue("tw_partition_can_repair", 1);
937 else
938 DataManager::SetValue("tw_partition_can_repair", 0);
Ethan Yonkera2719152015-05-28 09:44:41 -0500939 if (Part->Can_Resize())
940 DataManager::SetValue("tw_partition_can_resize", 1);
941 else
942 DataManager::SetValue("tw_partition_can_resize", 0);
bigbiffad58e1b2020-07-06 20:24:34 -0400943 if (TWFunc::Path_Exists("/system/bin/mkfs.fat"))
that3f7b1ac2014-12-30 11:30:13 +0100944 DataManager::SetValue("tw_partition_vfat", 1);
945 else
946 DataManager::SetValue("tw_partition_vfat", 0);
bigbiffad58e1b2020-07-06 20:24:34 -0400947 if (TWFunc::Path_Exists("/system/bin/mkexfatfs"))
that3f7b1ac2014-12-30 11:30:13 +0100948 DataManager::SetValue("tw_partition_exfat", 1);
949 else
950 DataManager::SetValue("tw_partition_exfat", 0);
whylef06b1652020-11-22 00:25:18 +0100951 if (TWFunc::Path_Exists("/system/bin/mkfs.f2fs") || TWFunc::Path_Exists("/system/bin/make_f2fs"))
that3f7b1ac2014-12-30 11:30:13 +0100952 DataManager::SetValue("tw_partition_f2fs", 1);
953 else
954 DataManager::SetValue("tw_partition_f2fs", 0);
bigbiffad58e1b2020-07-06 20:24:34 -0400955 if (TWFunc::Path_Exists("/system/bin/mke2fs"))
that3f7b1ac2014-12-30 11:30:13 +0100956 DataManager::SetValue("tw_partition_ext", 1);
957 else
958 DataManager::SetValue("tw_partition_ext", 0);
959 return 0;
960 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600961 LOGERR("Unable to locate partition: '%s'\n", part_path.c_str());
that3f7b1ac2014-12-30 11:30:13 +0100962 }
963 }
964 }
965 DataManager::SetValue("tw_partition_name", "");
966 DataManager::SetValue("tw_partition_file_system", "");
Ethan Yonker483e9f42016-01-11 22:21:18 -0600967 // Set this to 0 to prevent trying to partition this device, just in case
968 DataManager::SetValue("tw_partition_removable", 0);
that3f7b1ac2014-12-30 11:30:13 +0100969 return 0;
970}
971
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500972int GUIAction::screenshot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100973{
974 time_t tm;
975 char path[256];
976 int path_len;
Matt Mower0c88b842017-01-15 16:00:49 -0600977 uid_t uid = AID_MEDIA_RW;
978 gid_t gid = AID_MEDIA_RW;
that3f7b1ac2014-12-30 11:30:13 +0100979
980 const std::string storage = DataManager::GetCurrentStoragePath();
Matt Mowera8a89d12016-12-30 18:10:37 -0600981 if (PartitionManager.Is_Mounted_By_Path(storage)) {
that3f7b1ac2014-12-30 11:30:13 +0100982 snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
983 } else {
984 strcpy(path, "/tmp/");
985 }
986
Matt Mowera8a89d12016-12-30 18:10:37 -0600987 if (!TWFunc::Create_Dir_Recursive(path, 0775, uid, gid))
that3f7b1ac2014-12-30 11:30:13 +0100988 return 0;
989
990 tm = time(NULL);
991 path_len = strlen(path);
992
993 // Screenshot_2014-01-01-18-21-38.png
994 strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
995
996 int res = gr_save_screenshot(path);
Matt Mowera8a89d12016-12-30 18:10:37 -0600997 if (res == 0) {
that3f7b1ac2014-12-30 11:30:13 +0100998 chmod(path, 0666);
999 chown(path, uid, gid);
1000
that677b13f2015-12-26 23:57:31 +01001001 gui_msg(Msg("screenshot_saved=Screenshot was saved to {1}")(path));
that3f7b1ac2014-12-30 11:30:13 +01001002
VDavid0032034a412019-10-18 22:43:20 +02001003 // blink to notify that the screenshot was taken
that3f7b1ac2014-12-30 11:30:13 +01001004 gr_color(255, 255, 255, 255);
1005 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
1006 gr_flip();
1007 gui_forceRender();
1008 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001009 gui_err("screenshot_err=Failed to take a screenshot!");
that3f7b1ac2014-12-30 11:30:13 +01001010 }
1011 return 0;
1012}
1013
1014int GUIAction::setbrightness(std::string arg)
1015{
1016 return TWFunc::Set_Brightness(arg);
1017}
1018
1019int GUIAction::fileexists(std::string arg)
1020{
1021 struct stat st;
1022 string newpath = arg + "/.";
1023
1024 operation_start("FileExists");
1025 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
1026 operation_end(0);
1027 else
1028 operation_end(1);
1029 return 0;
1030}
1031
thatcc8ddca2015-01-03 01:59:36 +01001032void GUIAction::reinject_after_flash()
1033{
1034 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001035 gui_msg("injecttwrp=Injecting TWRP into boot image...");
thatcc8ddca2015-01-03 01:59:36 +01001036 if (simulate) {
1037 simulate_progress_bar();
1038 } else {
1039 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
1040 if (Boot == NULL || Boot->Current_File_System != "emmc")
1041 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
1042 else {
1043 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
1044 TWFunc::Exec_Cmd(injectcmd);
1045 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001046 gui_msg("done=Done.");
thatcc8ddca2015-01-03 01:59:36 +01001047 }
1048 }
1049}
1050
mauronofrio6d3bf892019-10-26 19:47:55 +02001051int GUIAction::ozip_decrypt(string zip_path)
1052{
bigbiffad58e1b2020-07-06 20:24:34 -04001053 if (!TWFunc::Path_Exists("/system/bin/ozip_decrypt")) {
mauronofrio6d3bf892019-10-26 19:47:55 +02001054 return 1;
1055 }
1056 gui_msg("ozip_decrypt_decryption=Starting Ozip Decryption...");
1057 TWFunc::Exec_Cmd("ozip_decrypt " + (string)TW_OZIP_DECRYPT_KEY + " '" + zip_path + "'");
1058 gui_msg("ozip_decrypt_finish=Ozip Decryption Finished!");
1059 return 0;
1060}
1061
that3f7b1ac2014-12-30 11:30:13 +01001062int GUIAction::flash(std::string arg)
1063{
1064 int i, ret_val = 0, wipe_cache = 0;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001065 // We're going to jump to this page first, like a loading page
1066 gui_changePage(arg);
that3f7b1ac2014-12-30 11:30:13 +01001067 for (i=0; i<zip_queue_index; i++) {
thatc7b631b2015-06-01 23:36:57 +02001068 string zip_path = zip_queue[i];
1069 size_t slashpos = zip_path.find_last_of('/');
1070 string zip_filename = (slashpos == string::npos) ? zip_path : zip_path.substr(slashpos + 1);
that3f7b1ac2014-12-30 11:30:13 +01001071 operation_start("Flashing");
mauronofrio6d3bf892019-10-26 19:47:55 +02001072 if((zip_path.substr(zip_path.size() - 4, 4)) == "ozip")
1073 {
1074 if((ozip_decrypt(zip_path)) != 0)
1075 {
1076 LOGERR("Unable to find ozip_decrypt!");
1077 break;
1078 }
1079 zip_filename = (zip_filename.substr(0, zip_filename.size() - 4)).append("zip");
1080 zip_path = (zip_path.substr(0, zip_path.size() - 4)).append("zip");
1081 if (!TWFunc::Path_Exists(zip_path)) {
1082 LOGERR("Unable to find decrypted zip");
1083 break;
1084 }
1085 }
thatc7b631b2015-06-01 23:36:57 +02001086 DataManager::SetValue("tw_filename", zip_path);
1087 DataManager::SetValue("tw_file", zip_filename);
that3f7b1ac2014-12-30 11:30:13 +01001088 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
1089
1090 TWFunc::SetPerformanceMode(true);
thatc7b631b2015-06-01 23:36:57 +02001091 ret_val = flash_zip(zip_path, &wipe_cache);
that3f7b1ac2014-12-30 11:30:13 +01001092 TWFunc::SetPerformanceMode(false);
1093 if (ret_val != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001094 gui_msg(Msg(msg::kError, "zip_err=Error installing zip file '{1}'")(zip_path));
that3f7b1ac2014-12-30 11:30:13 +01001095 ret_val = 1;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001096 break;
that3f7b1ac2014-12-30 11:30:13 +01001097 }
1098 }
1099 zip_queue_index = 0;
that3f7b1ac2014-12-30 11:30:13 +01001100
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001101 if (wipe_cache) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001102 gui_msg("zip_wipe_cache=One or more zip requested a cache wipe -- Wiping cache now.");
that3f7b1ac2014-12-30 11:30:13 +01001103 PartitionManager.Wipe_By_Path("/cache");
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001104 }
that3f7b1ac2014-12-30 11:30:13 +01001105
thatcc8ddca2015-01-03 01:59:36 +01001106 reinject_after_flash();
that3f7b1ac2014-12-30 11:30:13 +01001107 PartitionManager.Update_System_Details();
1108 operation_end(ret_val);
bigbiffa869fc72016-03-01 19:40:36 -05001109 // 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 -06001110 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +01001111 return 0;
1112}
1113
1114int GUIAction::wipe(std::string arg)
1115{
1116 operation_start("Format");
1117 DataManager::SetValue("tw_partition", arg);
that3f7b1ac2014-12-30 11:30:13 +01001118 int ret_val = false;
1119
1120 if (simulate) {
1121 simulate_progress_bar();
1122 } else {
1123 if (arg == "data")
1124 ret_val = PartitionManager.Factory_Reset();
1125 else if (arg == "battery")
1126 ret_val = PartitionManager.Wipe_Battery_Stats();
1127 else if (arg == "rotate")
1128 ret_val = PartitionManager.Wipe_Rotate_Data();
1129 else if (arg == "dalvik")
1130 ret_val = PartitionManager.Wipe_Dalvik_Cache();
1131 else if (arg == "DATAMEDIA") {
1132 ret_val = PartitionManager.Format_Data();
1133 } else if (arg == "INTERNAL") {
Matt Mower23d8aae2017-01-06 14:30:33 -06001134 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001135
1136 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1137 if (has_datamedia) {
1138 ret_val = PartitionManager.Wipe_Media_From_Data();
1139 } else {
1140 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
1141 }
1142 } else if (arg == "EXTERNAL") {
1143 string External_Path;
1144
1145 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
1146 ret_val = PartitionManager.Wipe_By_Path(External_Path);
1147 } else if (arg == "ANDROIDSECURE") {
1148 ret_val = PartitionManager.Wipe_Android_Secure();
1149 } else if (arg == "LIST") {
1150 string Wipe_List, wipe_path;
1151 bool skip = false;
1152 ret_val = true;
that3f7b1ac2014-12-30 11:30:13 +01001153
1154 DataManager::GetValue("tw_wipe_list", Wipe_List);
1155 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
1156 if (!Wipe_List.empty()) {
1157 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
1158 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
1159 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
1160 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
1161 if (wipe_path == "/and-sec") {
1162 if (!PartitionManager.Wipe_Android_Secure()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001163 gui_msg("and_sec_wipe_err=Unable to wipe android secure");
that3f7b1ac2014-12-30 11:30:13 +01001164 ret_val = false;
1165 break;
1166 } else {
1167 skip = true;
1168 }
1169 } else if (wipe_path == "DALVIK") {
1170 if (!PartitionManager.Wipe_Dalvik_Cache()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001171 gui_err("dalvik_wipe_err=Failed to wipe dalvik");
that3f7b1ac2014-12-30 11:30:13 +01001172 ret_val = false;
1173 break;
1174 } else {
1175 skip = true;
1176 }
1177 } else if (wipe_path == "INTERNAL") {
1178 if (!PartitionManager.Wipe_Media_From_Data()) {
1179 ret_val = false;
1180 break;
1181 } else {
1182 skip = true;
Dees_Troya13d74f2013-03-24 08:54:55 -05001183 }
1184 }
that3f7b1ac2014-12-30 11:30:13 +01001185 if (!skip) {
1186 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001187 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(wipe_path));
that3f7b1ac2014-12-30 11:30:13 +01001188 ret_val = false;
1189 break;
1190 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
1191 arg = wipe_path;
1192 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001193 } else {
that3f7b1ac2014-12-30 11:30:13 +01001194 skip = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001195 }
that3f7b1ac2014-12-30 11:30:13 +01001196 start_pos = end_pos + 1;
1197 end_pos = Wipe_List.find(";", start_pos);
Dees_Troy51a0e822012-09-05 15:24:24 -04001198 }
1199 }
that3f7b1ac2014-12-30 11:30:13 +01001200 } else
1201 ret_val = PartitionManager.Wipe_By_Path(arg);
that73a52952015-01-28 23:07:34 +01001202#ifndef TW_OEM_BUILD
that3f7b1ac2014-12-30 11:30:13 +01001203 if (arg == DataManager::GetSettingsStoragePath()) {
1204 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
1205 string Storage_Path = DataManager::GetSettingsStoragePath();
Dees_Troy51a0e822012-09-05 15:24:24 -04001206
that3f7b1ac2014-12-30 11:30:13 +01001207 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
1208 LOGINFO("Making TWRP folder and saving settings.\n");
1209 Storage_Path += "/TWRP";
1210 mkdir(Storage_Path.c_str(), 0777);
1211 DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001212 } else {
that3f7b1ac2014-12-30 11:30:13 +01001213 LOGERR("Unable to recreate TWRP folder and save settings.\n");
1214 }
1215 }
1216#endif
1217 }
1218 PartitionManager.Update_System_Details();
1219 if (ret_val)
1220 ret_val = 0; // 0 is success
1221 else
1222 ret_val = 1; // 1 is failure
1223 operation_end(ret_val);
1224 return 0;
1225}
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001226
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001227int GUIAction::refreshsizes(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001228{
1229 operation_start("Refreshing Sizes");
1230 if (simulate) {
1231 simulate_progress_bar();
1232 } else
1233 PartitionManager.Update_System_Details();
1234 operation_end(0);
1235 return 0;
1236}
1237
1238int GUIAction::nandroid(std::string arg)
1239{
that3f7b1ac2014-12-30 11:30:13 +01001240 if (simulate) {
that73a52952015-01-28 23:07:34 +01001241 PartitionManager.stop_backup.set_value(0);
that3f7b1ac2014-12-30 11:30:13 +01001242 DataManager::SetValue("tw_partition", "Simulation");
1243 simulate_progress_bar();
that73a52952015-01-28 23:07:34 +01001244 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001245 } else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001246 operation_start("Nandroid");
1247 int ret = 0;
1248
that3f7b1ac2014-12-30 11:30:13 +01001249 if (arg == "backup") {
1250 string Backup_Name;
1251 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Ethan Yonker4adc33e2016-01-22 16:07:11 -06001252 string auto_gen = gui_lookup("auto_generate", "(Auto Generate)");
Ethan Yonker53796e72019-01-11 22:49:52 -06001253 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 -05001254 ret = PartitionManager.Run_Backup(false);
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001255 DataManager::SetValue("tw_encrypt_backup", 0); // reset value so we don't encrypt every subsequent backup
1256 if (!PartitionManager.stop_backup.get_value()) {
1257 if (ret == false)
1258 ret = 1; // 1 for failure
1259 else
1260 ret = 0; // 0 for success
1261 DataManager::SetValue("tw_cancel_backup", 0);
1262 } else {
1263 DataManager::SetValue("tw_cancel_backup", 1);
1264 gui_msg("backup_cancel=Backup Cancelled");
1265 ret = 0;
1266 }
bigbiffce8f83c2015-12-12 18:30:21 -05001267 } else {
that3f7b1ac2014-12-30 11:30:13 +01001268 operation_end(1);
1269 return -1;
that3f7b1ac2014-12-30 11:30:13 +01001270 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001271 DataManager::SetValue(TW_BACKUP_NAME, auto_gen);
that3f7b1ac2014-12-30 11:30:13 +01001272 } else if (arg == "restore") {
1273 string Restore_Name;
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001274 int gui_adb_backup;
1275
that3f7b1ac2014-12-30 11:30:13 +01001276 DataManager::GetValue("tw_restore", Restore_Name);
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001277 DataManager::GetValue("tw_enable_adb_backup", gui_adb_backup);
1278 if (gui_adb_backup) {
1279 DataManager::SetValue("tw_operation_state", 1);
1280 if (TWFunc::stream_adb_backup(Restore_Name) == 0)
1281 ret = 0; // success
1282 else
1283 ret = 1; // failure
1284 DataManager::SetValue("tw_enable_adb_backup", 0);
1285 ret = 0; // assume success???
1286 } else {
1287 if (PartitionManager.Run_Restore(Restore_Name))
1288 ret = 0; // success
1289 else
1290 ret = 1; // failure
1291 }
that3f7b1ac2014-12-30 11:30:13 +01001292 } else {
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001293 operation_end(1); // invalid arg specified, fail
bigbiff7abc5fe2015-01-17 16:53:12 -05001294 return -1;
1295 }
that73a52952015-01-28 23:07:34 +01001296 operation_end(ret);
bigbiff7abc5fe2015-01-17 16:53:12 -05001297 return ret;
1298 }
1299 return 0;
1300}
1301
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001302int GUIAction::cancelbackup(std::string arg __unused) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001303 if (simulate) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001304 PartitionManager.stop_backup.set_value(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001305 }
1306 else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001307 int op_status = PartitionManager.Cancel_Backup();
1308 if (op_status != 0)
1309 op_status = 1; // failure
bigbiff7abc5fe2015-01-17 16:53:12 -05001310 }
1311
1312 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001313}
Dees_Troy51a0e822012-09-05 15:24:24 -04001314
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001315int GUIAction::fixcontexts(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001316{
that73a52952015-01-28 23:07:34 +01001317 int op_status = 0;
1318
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001319 operation_start("Fix Contexts");
1320 LOGINFO("fix contexts started!\n");
that3f7b1ac2014-12-30 11:30:13 +01001321 if (simulate) {
1322 simulate_progress_bar();
1323 } else {
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001324 op_status = PartitionManager.Fix_Contexts();
that3f7b1ac2014-12-30 11:30:13 +01001325 if (op_status != 0)
1326 op_status = 1; // failure
that3f7b1ac2014-12-30 11:30:13 +01001327 }
that73a52952015-01-28 23:07:34 +01001328 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001329 return 0;
1330}
1331
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001332int GUIAction::fixpermissions(std::string arg)
1333{
1334 return fixcontexts(arg);
1335}
1336
that3f7b1ac2014-12-30 11:30:13 +01001337int GUIAction::dd(std::string arg)
1338{
1339 operation_start("imaging");
1340
1341 if (simulate) {
1342 simulate_progress_bar();
1343 } else {
1344 string cmd = "dd " + arg;
1345 TWFunc::Exec_Cmd(cmd);
1346 }
1347 operation_end(0);
1348 return 0;
1349}
1350
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001351int GUIAction::partitionsd(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001352{
1353 operation_start("Partition SD Card");
1354 int ret_val = 0;
1355
1356 if (simulate) {
1357 simulate_progress_bar();
1358 } else {
1359 int allow_partition;
1360 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1361 if (allow_partition == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001362 gui_err("no_real_sdcard=This device does not have a real SD Card! Aborting!");
that3f7b1ac2014-12-30 11:30:13 +01001363 } else {
1364 if (!PartitionManager.Partition_SDCard())
1365 ret_val = 1; // failed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001366 }
that3f7b1ac2014-12-30 11:30:13 +01001367 }
1368 operation_end(ret_val);
1369 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001370
that3f7b1ac2014-12-30 11:30:13 +01001371}
Dees_Troy51a0e822012-09-05 15:24:24 -04001372
that3f7b1ac2014-12-30 11:30:13 +01001373int GUIAction::cmd(std::string arg)
1374{
1375 int op_status = 0;
1376
1377 operation_start("Command");
1378 LOGINFO("Running command: '%s'\n", arg.c_str());
1379 if (simulate) {
1380 simulate_progress_bar();
1381 } else {
1382 op_status = TWFunc::Exec_Cmd(arg);
1383 if (op_status != 0)
1384 op_status = 1;
1385 }
1386
1387 operation_end(op_status);
1388 return 0;
1389}
1390
1391int GUIAction::terminalcommand(std::string arg)
1392{
1393 int op_status = 0;
1394 string cmdpath, command;
1395
1396 DataManager::GetValue("tw_terminal_location", cmdpath);
1397 operation_start("CommandOutput");
1398 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
1399 if (simulate) {
1400 simulate_progress_bar();
1401 operation_end(op_status);
Matt Mower5aa29ab2015-02-25 23:50:55 -06001402 } else if (arg == "exit") {
1403 LOGINFO("Exiting terminal\n");
1404 operation_end(op_status);
1405 page("main");
that3f7b1ac2014-12-30 11:30:13 +01001406 } else {
1407 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
1408 LOGINFO("Actual command is: '%s'\n", command.c_str());
that3f7b1ac2014-12-30 11:30:13 +01001409 DataManager::SetValue("tw_terminal_state", 1);
1410 DataManager::SetValue("tw_background_thread_running", 1);
thatc6085482015-01-09 22:12:43 +01001411 FILE* fp;
1412 char line[512];
1413
1414 fp = popen(command.c_str(), "r");
1415 if (fp == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001416 LOGERR("Error opening command to run (%s).\n", strerror(errno));
thatc6085482015-01-09 22:12:43 +01001417 } else {
Matt Mower23d8aae2017-01-06 14:30:33 -06001418 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1;
thatc6085482015-01-09 22:12:43 +01001419 struct timeval timeout;
1420 fd_set fdset;
1421
Matt Mowera8a89d12016-12-30 18:10:37 -06001422 while (keep_going)
thatc6085482015-01-09 22:12:43 +01001423 {
1424 FD_ZERO(&fdset);
1425 FD_SET(fd, &fdset);
1426 timeout.tv_sec = 0;
1427 timeout.tv_usec = 400000;
1428 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1429 if (has_data == 0) {
1430 // Timeout reached
1431 DataManager::GetValue("tw_terminal_state", check);
1432 if (check == 0) {
1433 keep_going = 0;
1434 }
1435 } else if (has_data < 0) {
1436 // End of execution
1437 keep_going = 0;
1438 } else {
1439 // Try to read output
Matt Mowera8a89d12016-12-30 18:10:37 -06001440 if (fgets(line, sizeof(line), fp) != NULL)
thatc6085482015-01-09 22:12:43 +01001441 gui_print("%s", line); // Display output
1442 else
1443 keep_going = 0; // Done executing
1444 }
1445 }
1446 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -04001447 }
thatc6085482015-01-09 22:12:43 +01001448 DataManager::SetValue("tw_operation_status", 0);
1449 DataManager::SetValue("tw_operation_state", 1);
1450 DataManager::SetValue("tw_terminal_state", 0);
1451 DataManager::SetValue("tw_background_thread_running", 0);
1452 DataManager::SetValue(TW_ACTION_BUSY, 0);
that3f7b1ac2014-12-30 11:30:13 +01001453 }
1454 return 0;
1455}
1456
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001457int GUIAction::killterminal(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001458{
that3f7b1ac2014-12-30 11:30:13 +01001459 LOGINFO("Sending kill command...\n");
1460 operation_start("KillCommand");
1461 DataManager::SetValue("tw_operation_status", 0);
1462 DataManager::SetValue("tw_operation_state", 1);
1463 DataManager::SetValue("tw_terminal_state", 0);
1464 DataManager::SetValue("tw_background_thread_running", 0);
1465 DataManager::SetValue(TW_ACTION_BUSY, 0);
1466 return 0;
1467}
1468
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001469int GUIAction::reinjecttwrp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001470{
1471 int op_status = 0;
1472 operation_start("ReinjectTWRP");
Ethan Yonker74db1572015-10-28 12:44:49 -05001473 gui_msg("injecttwrp=Injecting TWRP into boot image...");
that3f7b1ac2014-12-30 11:30:13 +01001474 if (simulate) {
1475 simulate_progress_bar();
1476 } else {
1477 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Ethan Yonker74db1572015-10-28 12:44:49 -05001478 gui_msg("done=Done.");
that3f7b1ac2014-12-30 11:30:13 +01001479 }
1480
1481 operation_end(op_status);
1482 return 0;
1483}
1484
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001485int GUIAction::checkbackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001486{
1487 int op_status = 0;
1488
1489 operation_start("CheckBackupName");
1490 if (simulate) {
1491 simulate_progress_bar();
1492 } else {
Ethan Yonker53796e72019-01-11 22:49:52 -06001493 string Backup_Name;
1494 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
1495 op_status = PartitionManager.Check_Backup_Name(Backup_Name, true, true);
that3f7b1ac2014-12-30 11:30:13 +01001496 if (op_status != 0)
1497 op_status = 1;
1498 }
1499
1500 operation_end(op_status);
1501 return 0;
1502}
1503
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001504int GUIAction::decrypt(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001505{
1506 int op_status = 0;
1507
1508 operation_start("Decrypt");
1509 if (simulate) {
1510 simulate_progress_bar();
1511 } else {
1512 string Password;
Noah Jacobson81d638d2019-04-28 00:10:07 -04001513 string userID;
that3f7b1ac2014-12-30 11:30:13 +01001514 DataManager::GetValue("tw_crypto_password", Password);
Noah Jacobson81d638d2019-04-28 00:10:07 -04001515
1516 if (DataManager::GetIntValue(TW_IS_FBE)) { // for FBE
1517 DataManager::GetValue("tw_crypto_user_id", userID);
1518 if (userID != "") {
1519 op_status = PartitionManager.Decrypt_Device(Password, atoi(userID.c_str()));
1520 if (userID != "0") {
1521 if (op_status != 0)
1522 op_status = 1;
1523 operation_end(op_status);
1524 return 0;
1525 }
1526 } else {
1527 LOGINFO("User ID not found\n");
1528 op_status = 1;
1529 }
1530 ::sleep(1);
1531 } else { // for FDE
1532 op_status = PartitionManager.Decrypt_Device(Password);
1533 }
1534
that3f7b1ac2014-12-30 11:30:13 +01001535 if (op_status != 0)
1536 op_status = 1;
1537 else {
that3f7b1ac2014-12-30 11:30:13 +01001538 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1539
Ethan Yonkercf50da52015-01-12 21:59:07 -06001540 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001541
Ethan Yonkercf50da52015-01-12 21:59:07 -06001542 // Check for a custom theme and load it if exists
1543 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1544 if (has_datamedia != 0) {
1545 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06001546 LOGINFO("Failed to get default contexts and file mode for storage files.\n");
Ethan Yonkercf50da52015-01-12 21:59:07 -06001547 } else {
1548 LOGINFO("Got default contexts and file mode for storage files.\n");
that3f7b1ac2014-12-30 11:30:13 +01001549 }
1550 }
Ethan Yonker66a19492015-12-10 10:19:45 -06001551 PartitionManager.Decrypt_Adopted();
that3f7b1ac2014-12-30 11:30:13 +01001552 }
1553 }
1554
1555 operation_end(op_status);
1556 return 0;
1557}
1558
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001559int GUIAction::adbsideload(std::string arg __unused)
thatcc8ddca2015-01-03 01:59:36 +01001560{
1561 operation_start("Sideload");
1562 if (simulate) {
1563 simulate_progress_bar();
1564 operation_end(0);
1565 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001566 gui_msg("start_sideload=Starting ADB sideload feature...");
thatc6085482015-01-09 22:12:43 +01001567 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
1568
1569 // wait for the adb connection
bigbiffd58ba182020-03-23 10:02:29 -04001570 Device::BuiltinAction reboot_action = Device::REBOOT_BOOTLOADER;
bigbiff1f9e4842020-10-31 11:33:15 -04001571 int ret = twrp_sideload("/", &reboot_action);
1572 sideload_child_pid = GetMiniAdbdPid();
thatc6085482015-01-09 22:12:43 +01001573 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
1574
1575 if (ret != 0) {
1576 if (ret == -2)
Ethan Yonker74db1572015-10-28 12:44:49 -05001577 gui_msg("need_new_adb=You need adb 1.0.32 or newer to sideload to this device.");
thatc6085482015-01-09 22:12:43 +01001578 ret = 1; // failure
1579 } else {
1580 int wipe_cache = 0;
1581 int wipe_dalvik = 0;
1582 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
bigbiff1f9e4842020-10-31 11:33:15 -04001583 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1584 PartitionManager.Wipe_By_Path("/cache");
1585 if (wipe_dalvik)
1586 PartitionManager.Wipe_Dalvik_Cache();
thatcc8ddca2015-01-03 01:59:36 +01001587 }
thatc6085482015-01-09 22:12:43 +01001588 TWFunc::Toggle_MTP(mtp_was_enabled);
1589 reinject_after_flash();
1590 operation_end(ret);
thatcc8ddca2015-01-03 01:59:36 +01001591 }
that3f7b1ac2014-12-30 11:30:13 +01001592 return 0;
1593}
1594
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001595int GUIAction::adbsideloadcancel(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001596{
that3f7b1ac2014-12-30 11:30:13 +01001597 struct stat st;
1598 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
Ethan Yonker74db1572015-10-28 12:44:49 -05001599 gui_msg("cancel_sideload=Cancelling ADB sideload...");
thatcc8ddca2015-01-03 01:59:36 +01001600 LOGINFO("Signaling child sideload process to exit.\n");
1601 // Calling stat() on this magic filename signals the minadbd
1602 // subprocess to shut down.
1603 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
bigbiff1f9e4842020-10-31 11:33:15 -04001604 sideload_child_pid = GetMiniAdbdPid();
thatcc8ddca2015-01-03 01:59:36 +01001605 if (!sideload_child_pid) {
1606 LOGERR("Unable to get child ID\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001607 return 0;
1608 }
thatcc8ddca2015-01-03 01:59:36 +01001609 ::sleep(1);
1610 LOGINFO("Killing child sideload process.\n");
1611 kill(sideload_child_pid, SIGTERM);
1612 int status;
1613 LOGINFO("Waiting for child sideload process to exit.\n");
1614 waitpid(sideload_child_pid, &status, 0);
1615 sideload_child_pid = 0;
that3f7b1ac2014-12-30 11:30:13 +01001616 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
1617 return 0;
1618}
1619
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001620int GUIAction::openrecoveryscript(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001621{
1622 operation_start("OpenRecoveryScript");
1623 if (simulate) {
1624 simulate_progress_bar();
Ethan Yonker24e7eb72015-01-03 16:13:06 -06001625 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001626 } else {
that10ae24f2015-12-26 20:53:51 +01001627 int op_status = OpenRecoveryScript::Run_OpenRecoveryScript_Action();
Ethan Yonkere1abe612015-06-09 11:09:32 -05001628 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001629 }
1630 return 0;
1631}
1632
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001633int GUIAction::installsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001634{
1635 int op_status = 0;
1636
1637 operation_start("Install SuperSU");
1638 if (simulate) {
1639 simulate_progress_bar();
1640 } else {
Ethan Yonkerfa67cbf2018-07-20 12:22:33 -05001641 LOGERR("Installing SuperSU was deprecated from TWRP.\n");
that3f7b1ac2014-12-30 11:30:13 +01001642 }
1643
1644 operation_end(op_status);
1645 return 0;
1646}
1647
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001648int GUIAction::fixsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001649{
1650 int op_status = 0;
1651
1652 operation_start("Fixing Superuser Permissions");
1653 if (simulate) {
1654 simulate_progress_bar();
1655 } else {
1656 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1657 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
1658 }
1659
1660 operation_end(op_status);
1661 return 0;
1662}
1663
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001664int GUIAction::decrypt_backup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001665{
1666 int op_status = 0;
1667
1668 operation_start("Try Restore Decrypt");
1669 if (simulate) {
1670 simulate_progress_bar();
1671 } else {
1672 string Restore_Path, Filename, Password;
1673 DataManager::GetValue("tw_restore", Restore_Path);
1674 Restore_Path += "/";
1675 DataManager::GetValue("tw_restore_password", Password);
1676 TWFunc::SetPerformanceMode(true);
1677 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1678 op_status = 0; // success
1679 else
1680 op_status = 1; // fail
1681 TWFunc::SetPerformanceMode(false);
1682 }
1683
1684 operation_end(op_status);
1685 return 0;
1686}
1687
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001688int GUIAction::repair(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001689{
1690 int op_status = 0;
1691
1692 operation_start("Repair Partition");
1693 if (simulate) {
1694 simulate_progress_bar();
1695 } else {
1696 string part_path;
1697 DataManager::GetValue("tw_partition_mount_point", part_path);
1698 if (PartitionManager.Repair_By_Path(part_path, true)) {
1699 op_status = 0; // success
1700 } else {
that3f7b1ac2014-12-30 11:30:13 +01001701 op_status = 1; // fail
1702 }
1703 }
1704
1705 operation_end(op_status);
1706 return 0;
1707}
1708
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001709int GUIAction::resize(std::string arg __unused)
Ethan Yonkera2719152015-05-28 09:44:41 -05001710{
1711 int op_status = 0;
1712
1713 operation_start("Resize Partition");
1714 if (simulate) {
1715 simulate_progress_bar();
1716 } else {
1717 string part_path;
1718 DataManager::GetValue("tw_partition_mount_point", part_path);
1719 if (PartitionManager.Resize_By_Path(part_path, true)) {
1720 op_status = 0; // success
1721 } else {
Ethan Yonkera2719152015-05-28 09:44:41 -05001722 op_status = 1; // fail
1723 }
1724 }
1725
1726 operation_end(op_status);
1727 return 0;
1728}
1729
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001730int GUIAction::changefilesystem(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001731{
1732 int op_status = 0;
1733
1734 operation_start("Change File System");
1735 if (simulate) {
1736 simulate_progress_bar();
1737 } else {
1738 string part_path, file_system;
1739 DataManager::GetValue("tw_partition_mount_point", part_path);
1740 DataManager::GetValue("tw_action_new_file_system", file_system);
1741 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1742 op_status = 0; // success
1743 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001744 gui_err("change_fs_err=Error changing file system.");
that3f7b1ac2014-12-30 11:30:13 +01001745 op_status = 1; // fail
1746 }
1747 }
1748 PartitionManager.Update_System_Details();
1749 operation_end(op_status);
1750 return 0;
1751}
1752
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001753int GUIAction::startmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001754{
1755 int op_status = 0;
1756
1757 operation_start("Start MTP");
1758 if (PartitionManager.Enable_MTP())
1759 op_status = 0; // success
1760 else
1761 op_status = 1; // fail
1762
1763 operation_end(op_status);
1764 return 0;
1765}
1766
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001767int GUIAction::stopmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001768{
1769 int op_status = 0;
1770
1771 operation_start("Stop MTP");
1772 if (PartitionManager.Disable_MTP())
1773 op_status = 0; // success
1774 else
1775 op_status = 1; // fail
1776
1777 operation_end(op_status);
1778 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001779}
1780
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001781int GUIAction::flashimage(std::string arg __unused)
Ethan Yonker96af84a2015-01-05 14:58:36 -06001782{
1783 int op_status = 0;
epicX8f52c0a2021-02-24 23:12:08 +05301784 bool flag = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001785
1786 operation_start("Flash Image");
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001787 string path, filename;
1788 DataManager::GetValue("tw_zip_location", path);
1789 DataManager::GetValue("tw_file", filename);
Ethan Yonker96af84a2015-01-05 14:58:36 -06001790
epicX8f52c0a2021-02-24 23:12:08 +05301791#ifdef AB_OTA_UPDATER
1792 string target = DataManager::GetStrValue("tw_flash_partition");
1793 unsigned int pos = target.find_last_of(';');
1794 string mount_point = pos != string::npos ? target.substr(0, pos) : "";
1795 TWPartition* t_part = PartitionManager.Find_Partition_By_Path(mount_point);
1796 bool flash_in_both_slots = DataManager::GetIntValue("tw_flash_both_slots") ? true : false;
1797
1798 if (t_part != NULL && (flash_in_both_slots && t_part->SlotSelect))
1799 {
1800 string current_slot = PartitionManager.Get_Active_Slot_Display();
1801 bool pre_op_status = PartitionManager.Flash_Image(path, filename);
1802
1803 PartitionManager.Set_Active_Slot(current_slot == "A" ? "B" : "A");
1804 op_status = (int) !(pre_op_status && PartitionManager.Flash_Image(path, filename));
1805 PartitionManager.Set_Active_Slot(current_slot);
1806
1807 DataManager::SetValue("tw_flash_both_slots", 0);
1808 flag = false;
1809 }
1810#endif
1811 if (flag)
1812 {
1813 if (PartitionManager.Flash_Image(path, filename))
1814 op_status = 0; // success
1815 else
1816 op_status = 1; // fail
1817 }
1818
Ethan Yonker96af84a2015-01-05 14:58:36 -06001819 operation_end(op_status);
1820 return 0;
1821}
1822
that10ae24f2015-12-26 20:53:51 +01001823int GUIAction::twcmd(std::string arg)
1824{
1825 operation_start("TWRP CLI Command");
1826 if (simulate)
1827 simulate_progress_bar();
1828 else
1829 OpenRecoveryScript::Run_CLI_Command(arg.c_str());
1830 operation_end(0);
1831 return 0;
1832}
1833
Dees_Troy51a0e822012-09-05 15:24:24 -04001834int GUIAction::getKeyByName(std::string key)
1835{
that8834a0f2016-01-05 23:29:30 +01001836 if (key == "home") return KEY_HOMEPAGE; // note: KEY_HOME is cursor movement (like KEY_END)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001837 else if (key == "menu") return KEY_MENU;
1838 else if (key == "back") return KEY_BACK;
1839 else if (key == "search") return KEY_SEARCH;
1840 else if (key == "voldown") return KEY_VOLUMEDOWN;
1841 else if (key == "volup") return KEY_VOLUMEUP;
1842 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001843 int ret_val;
1844 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1845 if (!ret_val)
1846 return KEY_POWER;
1847 else
1848 return ret_val;
1849 }
1850
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001851 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001852}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001853
1854int GUIAction::checkpartitionlifetimewrites(std::string arg)
1855{
1856 int op_status = 0;
1857 TWPartition* sys = PartitionManager.Find_Partition_By_Path(arg);
1858
1859 operation_start("Check Partition Lifetime Writes");
1860 if (sys) {
1861 if (sys->Check_Lifetime_Writes() != 0)
1862 DataManager::SetValue("tw_lifetime_writes", 1);
1863 else
1864 DataManager::SetValue("tw_lifetime_writes", 0);
1865 op_status = 0; // success
1866 } else {
1867 DataManager::SetValue("tw_lifetime_writes", 1);
1868 op_status = 1; // fail
1869 }
1870
1871 operation_end(op_status);
1872 return 0;
1873}
1874
1875int GUIAction::mountsystemtoggle(std::string arg)
1876{
1877 int op_status = 0;
Captain Throwback9d6feb52018-07-27 10:05:24 -04001878 bool remount_system = PartitionManager.Is_Mounted_By_Path(PartitionManager.Get_Android_Root_Path());
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001879 bool remount_vendor = PartitionManager.Is_Mounted_By_Path("/vendor");
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001880
1881 operation_start("Toggle System Mount");
Captain Throwback9d6feb52018-07-27 10:05:24 -04001882 if (!PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001883 op_status = 1; // fail
1884 } else {
Captain Throwback9d6feb52018-07-27 10:05:24 -04001885 TWPartition* Part = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001886 if (Part) {
Ethan Yonkerd6966f42015-05-30 14:52:16 -05001887 if (arg == "0") {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001888 DataManager::SetValue("tw_mount_system_ro", 0);
1889 Part->Change_Mount_Read_Only(false);
1890 } else {
1891 DataManager::SetValue("tw_mount_system_ro", 1);
1892 Part->Change_Mount_Read_Only(true);
1893 }
1894 if (remount_system) {
1895 Part->Mount(true);
1896 }
1897 op_status = 0; // success
1898 } else {
1899 op_status = 1; // fail
1900 }
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001901 Part = PartitionManager.Find_Partition_By_Path("/vendor");
1902 if (Part) {
1903 if (arg == "0") {
1904 Part->Change_Mount_Read_Only(false);
1905 } else {
1906 Part->Change_Mount_Read_Only(true);
1907 }
1908 if (remount_vendor) {
1909 Part->Mount(true);
1910 }
1911 op_status = 0; // success
1912 } else {
1913 op_status = 1; // fail
1914 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001915 }
1916
1917 operation_end(op_status);
1918 return 0;
1919}
Ethan Yonker74db1572015-10-28 12:44:49 -05001920
1921int GUIAction::setlanguage(std::string arg __unused)
1922{
1923 int op_status = 0;
1924
1925 operation_start("Set Language");
1926 PageManager::LoadLanguage(DataManager::GetStrValue("tw_language"));
1927 PageManager::RequestReload();
1928 op_status = 0; // success
1929
1930 operation_end(op_status);
1931 return 0;
1932}
Ethan Yonker1b190162016-12-05 15:25:19 -06001933
Matt Mower9472ba12016-01-20 18:12:47 -06001934int GUIAction::togglebacklight(std::string arg __unused)
1935{
1936 blankTimer.toggleBlank();
1937 return 0;
1938}
1939
Ethan Yonker1b190162016-12-05 15:25:19 -06001940int GUIAction::setbootslot(std::string arg)
1941{
1942 operation_start("Set Boot Slot");
Captain Throwbackb60a2732020-10-13 17:55:43 -04001943 if (!simulate) {
1944 if (!PartitionManager.UnMount_By_Path("/vendor", false)) {
1945 // PartitionManager failed to unmount /vendor, this should not happen,
1946 // but in case it does, do a lazy unmount
1947 LOGINFO("WARNING: vendor partition could not be unmounted normally!\n");
1948 umount2("/vendor", MNT_DETACH);
1949 PartitionManager.Set_Active_Slot(arg);
1950 } else {
1951 PartitionManager.Set_Active_Slot(arg);
1952 }
1953 } else {
Ethan Yonker1b190162016-12-05 15:25:19 -06001954 simulate_progress_bar();
Captain Throwbackb60a2732020-10-13 17:55:43 -04001955 }
Ethan Yonker1b190162016-12-05 15:25:19 -06001956 operation_end(0);
1957 return 0;
1958}
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001959
1960int GUIAction::checkforapp(std::string arg __unused)
1961{
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001962 operation_start("Check for TWRP App");
1963 if (!simulate)
1964 {
epicX271bb3a2020-12-30 01:03:18 +05301965 TWFunc::checkforapp();
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001966 } else
1967 simulate_progress_bar();
epicX271bb3a2020-12-30 01:03:18 +05301968
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001969 operation_end(0);
1970 return 0;
1971}
1972
1973int GUIAction::installapp(std::string arg __unused)
1974{
1975 int op_status = 1;
1976 operation_start("Install TWRP App");
1977 if (!simulate)
1978 {
1979 if (DataManager::GetIntValue("tw_mount_system_ro") > 0 || DataManager::GetIntValue("tw_app_install_system") == 0) {
1980 if (PartitionManager.Mount_By_Path("/data", true)) {
1981 string install_path = "/data/app";
1982 string context = "u:object_r:apk_data_file:s0";
1983 if (!TWFunc::Path_Exists(install_path)) {
1984 if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
1985 LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno));
1986 goto exit;
1987 }
1988 if (chown(install_path.c_str(), 1000, 1000)) {
1989 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
1990 goto exit;
1991 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06001992 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001993 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
1994 goto exit;
1995 }
1996 }
1997 install_path += "/me.twrp.twrpapp-1";
1998 if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
1999 LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno));
2000 goto exit;
2001 }
2002 if (chown(install_path.c_str(), 1000, 1000)) {
2003 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
2004 goto exit;
2005 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002006 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002007 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2008 goto exit;
2009 }
2010 install_path += "/base.apk";
bigbiffad58e1b2020-07-06 20:24:34 -04002011 if (TWFunc::copy_file("/system/bin/me.twrp.twrpapp.apk", install_path, 0644)) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002012 LOGERR("Error copying apk file\n");
2013 goto exit;
2014 }
2015 if (chown(install_path.c_str(), 1000, 1000)) {
2016 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
2017 goto exit;
2018 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002019 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002020 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2021 goto exit;
2022 }
2023 sync();
2024 sync();
2025 }
2026 } else {
Captain Throwback9d6feb52018-07-27 10:05:24 -04002027 if (PartitionManager.Mount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) {
2028 string base_path = PartitionManager.Get_Android_Root_Path();
2029 if (TWFunc::Path_Exists(PartitionManager.Get_Android_Root_Path() + "/system"))
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002030 base_path += "/system"; // For devices with system as a root file system (e.g. Pixel)
2031 string install_path = base_path + "/priv-app";
2032 string context = "u:object_r:system_file:s0";
2033 if (!TWFunc::Path_Exists(install_path))
2034 install_path = base_path + "/app";
2035 if (TWFunc::Path_Exists(install_path)) {
2036 install_path += "/twrpapp";
2037 LOGINFO("Installing app to '%s'\n", install_path.c_str());
2038 if (mkdir(install_path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0) {
Ethan Yonker4767caf2017-01-11 10:45:04 -06002039 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002040 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2041 goto exit;
2042 }
2043 install_path += "/me.twrp.twrpapp.apk";
bigbiffad58e1b2020-07-06 20:24:34 -04002044 if (TWFunc::copy_file("/system/bin/me.twrp.twrpapp.apk", install_path, 0644)) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002045 LOGERR("Error copying apk file\n");
2046 goto exit;
2047 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002048 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002049 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2050 goto exit;
2051 }
Stefan Tauner6ad14572020-01-11 22:28:53 +01002052
2053 // System apps require their permissions to be pre-set via an XML file in /etc/permissions
2054 string permission_path = base_path + "/etc/permissions/privapp-permissions-twrpapp.xml";
bigbiffad58e1b2020-07-06 20:24:34 -04002055 if (TWFunc::copy_file("/system/bin/privapp-permissions-twrpapp.xml", permission_path, 0644)) {
Stefan Tauner6ad14572020-01-11 22:28:53 +01002056 LOGERR("Error copying permission file\n");
2057 goto exit;
2058 }
2059 if (chown(permission_path.c_str(), 1000, 1000)) {
2060 LOGERR("chown %s error: %s\n", permission_path.c_str(), strerror(errno));
2061 goto exit;
2062 }
2063 if (setfilecon(permission_path.c_str(), (security_context_t)context.c_str()) < 0) {
2064 LOGERR("setfilecon %s error: %s\n", permission_path.c_str(), strerror(errno));
2065 goto exit;
2066 }
2067
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002068 sync();
2069 sync();
Captain Throwback9d6feb52018-07-27 10:05:24 -04002070 PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002071 op_status = 0;
2072 } else {
2073 LOGERR("Error making app directory '%s': %s\n", strerror(errno));
2074 }
2075 }
2076 }
2077 }
2078 } else
2079 simulate_progress_bar();
2080exit:
epicX271bb3a2020-12-30 01:03:18 +05302081 TWFunc::checkforapp();
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002082 operation_end(0);
2083 return 0;
2084}
Ethan Yonker53796e72019-01-11 22:49:52 -06002085
Ethan Yonker76bbd3a2019-05-10 10:50:04 -05002086int GUIAction::uninstalltwrpsystemapp(std::string arg __unused)
2087{
2088 int op_status = 1;
2089 operation_start("Uninstall TWRP System App");
2090 if (!simulate)
2091 {
2092 int Mount_System_RO = DataManager::GetIntValue("tw_mount_system_ro");
2093 TWPartition* Part = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path());
2094 if (!Part) {
2095 LOGERR("Unabled to find system partition.\n");
2096 goto exit;
2097 }
2098 if (!Part->UnMount(true)) {
2099 goto exit;
2100 }
2101 if (Mount_System_RO > 0) {
2102 DataManager::SetValue("tw_mount_system_ro", 0);
2103 Part->Change_Mount_Read_Only(false);
2104 }
2105 if (Part->Mount(true)) {
2106 string base_path = PartitionManager.Get_Android_Root_Path();
2107 if (TWFunc::Path_Exists(PartitionManager.Get_Android_Root_Path() + "/system"))
2108 base_path += "/system"; // For devices with system as a root file system (e.g. Pixel)
2109 string uninstall_path = base_path + "/priv-app";
2110 if (!TWFunc::Path_Exists(uninstall_path))
2111 uninstall_path = base_path + "/app";
2112 uninstall_path += "/twrpapp";
2113 if (TWFunc::Path_Exists(uninstall_path)) {
2114 LOGINFO("Uninstalling TWRP App from '%s'\n", uninstall_path.c_str());
2115 if (TWFunc::removeDir(uninstall_path, false) == 0) {
2116 sync();
2117 op_status = 0;
2118 DataManager::SetValue("tw_app_installed_in_system", 0);
2119 DataManager::SetValue("tw_app_install_status", 0);
2120 } else {
2121 LOGERR("Unable to remove TWRP app from system.\n");
2122 }
2123 } else {
2124 LOGINFO("didn't find TWRP app in '%s'\n", uninstall_path.c_str());
2125 }
2126 }
2127 Part->UnMount(true);
2128 if (Mount_System_RO > 0) {
2129 DataManager::SetValue("tw_mount_system_ro", Mount_System_RO);
2130 Part->Change_Mount_Read_Only(true);
2131 }
2132 } else
2133 simulate_progress_bar();
2134exit:
epicX271bb3a2020-12-30 01:03:18 +05302135 TWFunc::checkforapp();
Ethan Yonker76bbd3a2019-05-10 10:50:04 -05002136 operation_end(0);
2137 return 0;
2138}
2139
Ethan Yonker53796e72019-01-11 22:49:52 -06002140int GUIAction::repackimage(std::string arg __unused)
2141{
2142 int op_status = 1;
bigbiffad58e1b2020-07-06 20:24:34 -04002143 twrpRepacker repacker;
2144
Ethan Yonker53796e72019-01-11 22:49:52 -06002145 operation_start("Repack Image");
2146 if (!simulate)
2147 {
2148 std::string path = DataManager::GetStrValue("tw_filename");
2149 Repack_Options_struct Repack_Options;
2150 Repack_Options.Disable_Verity = false;
2151 Repack_Options.Disable_Force_Encrypt = false;
2152 Repack_Options.Backup_First = DataManager::GetIntValue("tw_repack_backup_first") != 0;
2153 if (DataManager::GetIntValue("tw_repack_kernel") == 1)
2154 Repack_Options.Type = REPLACE_KERNEL;
2155 else
2156 Repack_Options.Type = REPLACE_RAMDISK;
bigbiffad58e1b2020-07-06 20:24:34 -04002157 if (!repacker.Repack_Image_And_Flash(path, Repack_Options))
Ethan Yonker53796e72019-01-11 22:49:52 -06002158 goto exit;
2159 } else
2160 simulate_progress_bar();
2161 op_status = 0;
2162exit:
2163 operation_end(op_status);
2164 return 0;
2165}
2166
nebrassyac29e692021-05-20 13:03:30 +02002167int GUIAction::reflashtwrp(std::string arg __unused)
2168{
2169 int op_status = 1;
2170 twrpRepacker repacker;
2171
2172 operation_start("Repack Image");
2173 if (!simulate)
2174 {
2175 if (!repacker.Flash_Current_Twrp())
2176 goto exit;
2177 } else
2178 simulate_progress_bar();
2179 op_status = 0;
2180exit:
2181 operation_end(op_status);
2182 return 0;
2183}
Ethan Yonker53796e72019-01-11 22:49:52 -06002184int GUIAction::fixabrecoverybootloop(std::string arg __unused)
2185{
2186 int op_status = 1;
bigbiffad58e1b2020-07-06 20:24:34 -04002187 twrpRepacker repacker;
2188
Ethan Yonker53796e72019-01-11 22:49:52 -06002189 operation_start("Repack Image");
2190 if (!simulate)
2191 {
bigbiffad58e1b2020-07-06 20:24:34 -04002192 if (!TWFunc::Path_Exists("/system/bin/magiskboot")) {
Ethan Yonker53796e72019-01-11 22:49:52 -06002193 LOGERR("Image repacking tool not present in this TWRP build!");
2194 goto exit;
2195 }
2196 DataManager::SetProgress(0);
2197 TWPartition* part = PartitionManager.Find_Partition_By_Path("/boot");
2198 if (part)
2199 gui_msg(Msg("unpacking_image=Unpacking {1}...")(part->Display_Name));
2200 else {
2201 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")("/boot"));
2202 goto exit;
2203 }
bigbiffad58e1b2020-07-06 20:24:34 -04002204 if (!repacker.Backup_Image_For_Repack(part, REPACK_ORIG_DIR, DataManager::GetIntValue("tw_repack_backup_first") != 0, gui_lookup("repack", "Repack")))
Ethan Yonker53796e72019-01-11 22:49:52 -06002205 goto exit;
2206 DataManager::SetProgress(.25);
2207 gui_msg("fixing_recovery_loop_patch=Patching kernel...");
bigbiffad58e1b2020-07-06 20:24:34 -04002208 std::string command = "cd " REPACK_ORIG_DIR " && /system/bin/magiskboot hexpatch kernel 77616E745F696E697472616D667300 736B69705F696E697472616D667300";
Ethan Yonker53796e72019-01-11 22:49:52 -06002209 if (TWFunc::Exec_Cmd(command) != 0) {
2210 gui_msg(Msg(msg::kError, "fix_recovery_loop_patch_error=Error patching kernel."));
2211 goto exit;
2212 }
2213 std::string header_path = REPACK_ORIG_DIR;
2214 header_path += "header";
2215 if (TWFunc::Path_Exists(header_path)) {
2216 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";
2217 if (TWFunc::Exec_Cmd(command) != 0) {
2218 gui_msg(Msg(msg::kError, "fix_recovery_loop_patch_error=Error patching kernel."));
2219 goto exit;
2220 }
2221 }
2222 DataManager::SetProgress(.5);
2223 gui_msg(Msg("repacking_image=Repacking {1}...")(part->Display_Name));
bigbiffad58e1b2020-07-06 20:24:34 -04002224 command = "cd " REPACK_ORIG_DIR " && /system/bin/magiskboot repack " REPACK_ORIG_DIR "boot.img";
Ethan Yonker53796e72019-01-11 22:49:52 -06002225 if (TWFunc::Exec_Cmd(command) != 0) {
2226 gui_msg(Msg(msg::kError, "repack_error=Error repacking image."));
2227 goto exit;
2228 }
2229 DataManager::SetProgress(.75);
2230 std::string path = REPACK_ORIG_DIR;
2231 std::string file = "new-boot.img";
2232 DataManager::SetValue("tw_flash_partition", "/boot;");
2233 if (!PartitionManager.Flash_Image(path, file)) {
2234 LOGINFO("Error flashing new image\n");
2235 goto exit;
2236 }
2237 DataManager::SetProgress(1);
2238 TWFunc::removeDir(REPACK_ORIG_DIR, false);
2239 } else
2240 simulate_progress_bar();
2241 op_status = 0;
2242exit:
2243 operation_end(op_status);
2244 return 0;
2245}
bigbiffdf8436b2020-08-30 16:22:34 -04002246
2247
2248int GUIAction::enableadb(std::string arg __unused) {
2249 android::base::SetProperty("sys.usb.config", "none");
2250 android::base::SetProperty("sys.usb.config", "adb");
2251 return 0;
2252}
2253
2254int GUIAction::enablefastboot(std::string arg __unused) {
2255 android::base::SetProperty("sys.usb.config", "none");
2256 android::base::SetProperty("sys.usb.config", "fastboot");
2257 return 0;
2258}
Mohd Faraz77bbeb02020-08-12 12:29:42 +00002259
2260int GUIAction::changeterminal(std::string arg) {
2261 bool res = true;
2262 std::string resp, cmd = "cd " + arg;
2263 DataManager::GetValue("tw_terminal_location", resp);
2264 if (arg.empty() && !resp.empty()) {
2265 cmd = "cd /";
2266 for (uint8_t iter = 0; iter < cmd.size(); iter++)
2267 term->NotifyCharInput(cmd.at(iter));
2268 term->NotifyCharInput(13);
2269 DataManager::SetValue("tw_terminal_location", "");
2270 return 0;
2271 }
2272 if (term != NULL && !arg.empty()) {
2273 DataManager::SetValue("tw_terminal_location", arg);
2274 if (term->status()) {
2275 for (uint8_t iter = 0; iter < cmd.size(); iter++)
2276 term->NotifyCharInput(cmd.at(iter));
2277 term->NotifyCharInput(13);
2278 }
2279 else if (chdir(arg.c_str()) != 0) {
2280 LOGINFO("Unable to change dir to %s\n", arg.c_str());
2281 res = false;
2282 }
2283 }
2284 else {
2285 res = false;
2286 LOGINFO("Unable to switch to Terminal\n");
2287 }
2288 if (res)
2289 gui_changePage("terminalcommand");
2290 return 0;
2291}
bigbiffcfa875c2021-06-20 16:20:27 -04002292
2293int GUIAction::unmapsuperdevices(std::string arg __unused) {
2294 int op_status = 1;
2295
2296 operation_start("Remove Super Devices");
2297 if (simulate) {
2298 simulate_progress_bar();
2299 } else {
2300 if (PartitionManager.Unmap_Super_Devices()) {
2301 op_status = 0;
2302 }
2303 }
2304
2305 operation_end(op_status);
2306 return 0;
2307}
2308
Captain Throwback16dd81b2021-02-12 19:32:36 -05002309#ifndef TW_EXCLUDE_NANO
2310int GUIAction::editfile(std::string arg) {
2311 if (term != NULL) {
2312 for (uint8_t iter = 0; iter < arg.size(); iter++)
2313 term->NotifyCharInput(arg.at(iter));
2314 term->NotifyCharInput(13);
2315 }
2316 else
2317 LOGINFO("Unable to switch to Terminal\n");
2318 return 0;
2319}
2320#endif
epicXa721f952021-01-04 13:01:31 +05302321
2322int GUIAction::applycustomtwrpfolder(string arg __unused)
2323{
2324 operation_start("ChangingTWRPFolder");
2325 string storageFolder = DataManager::GetSettingsStoragePath();
2326 string newFolder = storageFolder + '/' + arg;
2327 string newBackupFolder = newFolder + "/BACKUPS/" + DataManager::GetStrValue("device_id");
2328 string prevFolder = storageFolder + DataManager::GetStrValue(TW_RECOVERY_FOLDER_VAR);
2329 bool ret = false;
2330
2331 if (TWFunc::Path_Exists(newFolder)) {
2332 gui_msg(Msg(msg::kError, "tw_folder_exists=A folder with that name already exists!"));
2333 } else {
2334 ret = true;
2335 }
2336
2337 if (newFolder != prevFolder && ret) {
2338 ret = TWFunc::Exec_Cmd("mv -f \"" + prevFolder + "\" \"" + newFolder + '\"') != 0 ? false : true;
2339 } else {
2340 gui_msg(Msg(msg::kError, "tw_folder_exists=A folder with that name already exists!"));
2341 }
2342
epicX11e90832021-03-01 00:02:57 +05302343 if (ret) ret = TWFunc::Recursive_Mkdir(newBackupFolder) ? true : false;
epicXa721f952021-01-04 13:01:31 +05302344
2345
2346 if (ret) {
2347 DataManager::SetValue(TW_RECOVERY_FOLDER_VAR, '/' + arg);
2348 DataManager::SetValue(TW_BACKUPS_FOLDER_VAR, newBackupFolder);
2349 DataManager::mBackingFile = newFolder + '/' + TW_SETTINGS_FILE;
2350 }
2351 operation_end((int)!ret);
2352 return 0;
2353}