blob: d5b8c8f4fe7ae315698391f231bd1a92f86f15cf [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
bigbiffd21252f2021-09-18 15:56:32 -0400248 ADD_ACTION(mergesnapshots);
that3f7b1ac2014-12-30 11:30:13 +0100249 }
250
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200251 // First, get the action
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600252 actions = FindNode(node, "actions");
253 if (actions) child = FindNode(actions, "action");
254 else child = FindNode(node, "action");
Dees_Troy51a0e822012-09-05 15:24:24 -0400255
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200256 if (!child) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400257
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200258 while (child)
259 {
260 Action action;
Dees_Troy51a0e822012-09-05 15:24:24 -0400261
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200262 attr = child->first_attribute("function");
263 if (!attr) return;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500264
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200265 action.mFunction = attr->value();
266 action.mArg = child->value();
267 mActions.push_back(action);
Dees_Troy51a0e822012-09-05 15:24:24 -0400268
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200269 child = child->next_sibling("action");
270 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400271
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200272 // Now, let's get either the key or region
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600273 child = FindNode(node, "touch");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200274 if (child)
275 {
276 attr = child->first_attribute("key");
277 if (attr)
278 {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100279 std::vector<std::string> keys = TWFunc::Split_String(attr->value(), "+");
Matt Mowera8a89d12016-12-30 18:10:37 -0600280 for (size_t i = 0; i < keys.size(); ++i)
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100281 {
282 const int key = getKeyByName(keys[i]);
283 mKeys[key] = false;
284 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200285 }
286 else
287 {
288 attr = child->first_attribute("x");
289 if (!attr) return;
290 mActionX = atol(attr->value());
291 attr = child->first_attribute("y");
292 if (!attr) return;
293 mActionY = atol(attr->value());
294 attr = child->first_attribute("w");
295 if (!attr) return;
296 mActionW = atol(attr->value());
297 attr = child->first_attribute("h");
298 if (!attr) return;
299 mActionH = atol(attr->value());
300 }
301 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400302}
303
Matt Mower25036b72016-06-24 12:30:18 -0500304int GUIAction::NotifyTouch(TOUCH_STATE state, int x __unused, int y __unused)
Dees_Troy51a0e822012-09-05 15:24:24 -0400305{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200306 if (state == TOUCH_RELEASE)
307 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400308
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200309 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400310}
311
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100312int GUIAction::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -0400313{
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100314 std::map<int, bool>::iterator itr = mKeys.find(key);
Matt Mowera8a89d12016-12-30 18:10:37 -0600315 if (itr == mKeys.end())
that8834a0f2016-01-05 23:29:30 +0100316 return 1;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100317
318 bool prevState = itr->second;
319 itr->second = down;
320
321 // If there is only one key for this action, wait for key up so it
322 // doesn't trigger with multi-key actions.
323 // Else, check if all buttons are pressed, then consume their release events
324 // so they don't trigger one-button actions and reset mKeys pressed status
Matt Mowera8a89d12016-12-30 18:10:37 -0600325 if (mKeys.size() == 1) {
326 if (!down && prevState) {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100327 doActions();
thatd4725ca2016-01-19 00:15:21 +0100328 return 0;
329 }
Matt Mowera8a89d12016-12-30 18:10:37 -0600330 } else if (down) {
331 for (itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
332 if (!itr->second)
that8834a0f2016-01-05 23:29:30 +0100333 return 1;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100334 }
335
336 // Passed, all req buttons are pressed, reset them and consume release events
337 HardwareKeyboard *kb = PageManager::GetHardwareKeyboard();
Matt Mowera8a89d12016-12-30 18:10:37 -0600338 for (itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100339 kb->ConsumeKeyRelease(itr->first);
340 itr->second = false;
341 }
342
343 doActions();
thatd4725ca2016-01-19 00:15:21 +0100344 return 0;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100345 }
346
thatd4725ca2016-01-19 00:15:21 +0100347 return 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400348}
349
Vojtech Bocek07220562014-02-08 02:05:33 +0100350int GUIAction::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400351{
Vojtech Bocek07220562014-02-08 02:05:33 +0100352 GUIObject::NotifyVarChange(varName, value);
353
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100354 if (varName.empty() && !isConditionValid() && mKeys.empty() && !mActionW)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200355 doActions();
Matt Mowera8a89d12016-12-30 18:10:37 -0600356 else if ((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200357 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400358
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200359 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400360}
361
362void GUIAction::simulate_progress_bar(void)
363{
Ethan Yonker74db1572015-10-28 12:44:49 -0500364 gui_msg("simulating=Simulating actions...");
Dees_Troy51a0e822012-09-05 15:24:24 -0400365 for (int i = 0; i < 5; i++)
366 {
bigbiff7abc5fe2015-01-17 16:53:12 -0500367 if (PartitionManager.stop_backup.get_value()) {
368 DataManager::SetValue("tw_cancel_backup", 1);
Matt Mower3c366972015-12-25 19:28:31 -0600369 gui_msg("backup_cancel=Backup Cancelled");
bigbiff7abc5fe2015-01-17 16:53:12 -0500370 DataManager::SetValue("ui_progress", 0);
371 PartitionManager.stop_backup.set_value(0);
372 return;
373 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400374 usleep(500000);
375 DataManager::SetValue("ui_progress", i * 20);
376 }
377}
378
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600379int GUIAction::flash_zip(std::string filename, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400380{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200381 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400382
383 DataManager::SetValue("ui_progress", 0);
Chaosmasterd5364a02020-02-03 15:38:02 +0100384 DataManager::SetValue("ui_portion_size", 0);
385 DataManager::SetValue("ui_portion_start", 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400386
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200387 if (filename.empty())
388 {
389 LOGERR("No file specified.\n");
390 return -1;
391 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400392
Ethan Yonker9598c072016-01-15 21:54:34 -0600393 if (!TWFunc::Path_Exists(filename)) {
394 if (!PartitionManager.Mount_By_Path(filename, true)) {
395 return -1;
396 }
397 if (!TWFunc::Path_Exists(filename)) {
398 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")(filename));
399 return -1;
400 }
401 }
Dees_Troy657c3092012-09-10 20:32:10 -0400402
Dees_Troy51a0e822012-09-05 15:24:24 -0400403 if (simulate) {
404 simulate_progress_bar();
405 } else {
bigbiffab036612021-06-24 21:31:40 -0400406 char apex_enabled[PROPERTY_VALUE_MAX];
407 property_get("twrp.apex.flattened", apex_enabled, "");
408 if (strcmp(apex_enabled, "true") == 0) {
409 umount("/apex");
410 }
epicX9597b842021-03-20 21:58:17 +0530411 ret_val = TWinstall_zip(filename.c_str(), wipe_cache, (bool) !DataManager::GetIntValue(TW_SKIP_DIGEST_CHECK_ZIP_VAR));
bigbiff1f9e4842020-10-31 11:33:15 -0400412 PartitionManager.Unlock_Block_Partitions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400413 // Now, check if we need to ensure TWRP remains installed...
414 struct stat st;
bigbiffad58e1b2020-07-06 20:24:34 -0400415 if (stat("/system/bin/installTwrp", &st) == 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400416 {
417 DataManager::SetValue("tw_operation", "Configuring TWRP");
418 DataManager::SetValue("tw_partition", "");
Ethan Yonker74db1572015-10-28 12:44:49 -0500419 gui_msg("config_twrp=Configuring TWRP...");
bigbiffad58e1b2020-07-06 20:24:34 -0400420 if (TWFunc::Exec_Cmd("/system/bin/installTwrp reinstall") < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400421 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500422 gui_msg("config_twrp_err=Unable to configure TWRP with this kernel.");
Dees_Troy51a0e822012-09-05 15:24:24 -0400423 }
424 }
425 }
426
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200427 // Done
428 DataManager::SetValue("ui_progress", 100);
429 DataManager::SetValue("ui_progress", 0);
Chaosmasterd5364a02020-02-03 15:38:02 +0100430 DataManager::SetValue("ui_portion_size", 0);
431 DataManager::SetValue("ui_portion_start", 0);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200432 return ret_val;
Dees_Troy51a0e822012-09-05 15:24:24 -0400433}
434
that73a52952015-01-28 23:07:34 +0100435GUIAction::ThreadType GUIAction::getThreadType(const GUIAction::Action& action)
thatc6085482015-01-09 22:12:43 +0100436{
that73a52952015-01-28 23:07:34 +0100437 string func = gui_parse_text(action.mFunction);
438 bool needsThread = setActionsRunningInCallerThread.find(func) == setActionsRunningInCallerThread.end();
439 if (needsThread) {
440 if (func == "cancelbackup")
441 return THREAD_CANCEL;
442 else
443 return THREAD_ACTION;
444 }
445 return THREAD_NONE;
thatc6085482015-01-09 22:12:43 +0100446}
447
Dees_Troy51a0e822012-09-05 15:24:24 -0400448int GUIAction::doActions()
449{
that3f7b1ac2014-12-30 11:30:13 +0100450 if (mActions.size() < 1)
451 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400452
that73a52952015-01-28 23:07:34 +0100453 // Determine in which thread to run the actions.
454 // Do it for all actions at once before starting, so that we can cancel the whole batch if the thread is already busy.
455 ThreadType threadType = THREAD_NONE;
that3f7b1ac2014-12-30 11:30:13 +0100456 std::vector<Action>::iterator it;
that73a52952015-01-28 23:07:34 +0100457 for (it = mActions.begin(); it != mActions.end(); ++it) {
458 ThreadType tt = getThreadType(*it);
459 if (tt == THREAD_NONE)
460 continue;
461 if (threadType == THREAD_NONE)
462 threadType = tt;
463 else if (threadType != tt) {
464 LOGERR("Can't mix normal and cancel actions in the same list.\n"
465 "Running the whole batch in the cancel thread.\n");
466 threadType = THREAD_CANCEL;
thatc6085482015-01-09 22:12:43 +0100467 break;
468 }
that7d3b54f2015-01-09 22:52:51 +0100469 }
that73a52952015-01-28 23:07:34 +0100470
471 // Now run the actions in the desired thread.
472 switch (threadType) {
473 case THREAD_ACTION:
474 action_thread.threadActions(this);
475 break;
476
477 case THREAD_CANCEL:
478 cancel_thread.threadActions(this);
479 break;
480
481 default: {
482 // no iterators here because theme reloading might kill our object
483 const size_t cnt = mActions.size();
484 for (size_t i = 0; i < cnt; ++i)
485 doAction(mActions[i]);
486 }
thatc6085482015-01-09 22:12:43 +0100487 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400488
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200489 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400490}
491
that3f7b1ac2014-12-30 11:30:13 +0100492int GUIAction::doAction(Action action)
Dees_Troy51a0e822012-09-05 15:24:24 -0400493{
that3f7b1ac2014-12-30 11:30:13 +0100494 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400495
that3f7b1ac2014-12-30 11:30:13 +0100496 std::string function = gui_parse_text(action.mFunction);
497 std::string arg = gui_parse_text(action.mArg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400498
that3f7b1ac2014-12-30 11:30:13 +0100499 // find function and execute it
500 mapFunc::const_iterator funcitr = mf.find(function);
501 if (funcitr != mf.end())
502 return (this->*funcitr->second)(arg);
503
504 LOGERR("Unknown action '%s'\n", function.c_str());
505 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400506}
507
508void GUIAction::operation_start(const string operation_name)
509{
that3f7b1ac2014-12-30 11:30:13 +0100510 LOGINFO("operation_start: '%s'\n", operation_name.c_str());
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000511 time(&Start);
Dees_Troy51a0e822012-09-05 15:24:24 -0400512 DataManager::SetValue(TW_ACTION_BUSY, 1);
513 DataManager::SetValue("ui_progress", 0);
Chaosmasterd5364a02020-02-03 15:38:02 +0100514 DataManager::SetValue("ui_portion_size", 0);
515 DataManager::SetValue("ui_portion_start", 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400516 DataManager::SetValue("tw_operation", operation_name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400517 DataManager::SetValue("tw_operation_state", 0);
Ethan Yonkerd83c9ea2015-01-02 15:22:31 -0600518 DataManager::SetValue("tw_operation_status", 0);
bigbiff25d25b92020-06-19 16:07:38 -0400519 bool tw_ab_device = TWFunc::get_log_dir() != CACHE_LOGS_DIR;
bigbiff bigbiff19874f12019-01-08 20:06:57 -0500520 DataManager::SetValue("tw_ab_device", tw_ab_device);
Dees_Troy51a0e822012-09-05 15:24:24 -0400521}
522
that3f7b1ac2014-12-30 11:30:13 +0100523void GUIAction::operation_end(const int operation_status)
Dees_Troy51a0e822012-09-05 15:24:24 -0400524{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000525 time_t Stop;
Dees_Troy51a0e822012-09-05 15:24:24 -0400526 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400527 DataManager::SetValue("ui_progress", 100);
528 if (simulate) {
529 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
530 if (simulate_fail != 0)
531 DataManager::SetValue("tw_operation_status", 1);
532 else
533 DataManager::SetValue("tw_operation_status", 0);
534 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500535 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400536 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500537 }
538 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400539 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500540 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400541 }
542 DataManager::SetValue("tw_operation_state", 1);
543 DataManager::SetValue(TW_ACTION_BUSY, 0);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500544 blankTimer.resetTimerAndUnblank();
LuK133762326f42015-09-30 19:57:46 +0200545 property_set("twrp.action_complete", "1");
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000546 time(&Stop);
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400547
548#ifndef TW_NO_HAPTICS
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000549 if ((int) difftime(Stop, Start) > 10)
Ethan Yonker03db3262014-02-05 08:02:06 -0600550 DataManager::Vibrate("tw_action_vibrate");
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400551#endif
552
that3f7b1ac2014-12-30 11:30:13 +0100553 LOGINFO("operation_end - status=%d\n", operation_status);
Dees_Troy51a0e822012-09-05 15:24:24 -0400554}
555
that3f7b1ac2014-12-30 11:30:13 +0100556int GUIAction::reboot(std::string arg)
Dees_Troy51a0e822012-09-05 15:24:24 -0400557{
that3f7b1ac2014-12-30 11:30:13 +0100558 sync();
559 DataManager::SetValue("tw_gui_done", 1);
560 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400561
that3f7b1ac2014-12-30 11:30:13 +0100562 return 0;
563}
Dees_Troy51a0e822012-09-05 15:24:24 -0400564
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500565int GUIAction::home(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100566{
that3f7b1ac2014-12-30 11:30:13 +0100567 gui_changePage("main");
568 return 0;
569}
Dees_Troy51a0e822012-09-05 15:24:24 -0400570
that3f7b1ac2014-12-30 11:30:13 +0100571int GUIAction::key(std::string arg)
572{
573 const int key = getKeyByName(arg);
574 PageManager::NotifyKey(key, true);
575 PageManager::NotifyKey(key, false);
576 return 0;
577}
578
579int GUIAction::page(std::string arg)
580{
LuK133762326f42015-09-30 19:57:46 +0200581 property_set("twrp.action_complete", "0");
that3f7b1ac2014-12-30 11:30:13 +0100582 std::string page_name = gui_parse_text(arg);
583 return gui_changePage(page_name);
584}
585
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500586int GUIAction::reload(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100587{
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500588 PageManager::RequestReload();
589 // The actual reload is handled in pages.cpp in PageManager::RunReload()
590 // The reload will occur on the next Update or Render call and will
591 // be performed in the rendoer thread instead of the action thread
592 // to prevent crashing which could occur when we start deleting
593 // GUI resources in the action thread while we attempt to render
594 // with those same resources in another thread.
that3f7b1ac2014-12-30 11:30:13 +0100595 return 0;
596}
Dees_Troy51a0e822012-09-05 15:24:24 -0400597
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500598int GUIAction::readBackup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100599{
600 string Restore_Name;
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -0400601
that3f7b1ac2014-12-30 11:30:13 +0100602 DataManager::GetValue("tw_restore", Restore_Name);
603 PartitionManager.Set_Restore_Files(Restore_Name);
604 return 0;
605}
606
607int GUIAction::set(std::string arg)
608{
609 if (arg.find('=') != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200610 {
that3f7b1ac2014-12-30 11:30:13 +0100611 string varName = arg.substr(0, arg.find('='));
612 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400613
that3f7b1ac2014-12-30 11:30:13 +0100614 DataManager::GetValue(value, value);
615 DataManager::SetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200616 }
that3f7b1ac2014-12-30 11:30:13 +0100617 else
618 DataManager::SetValue(arg, "1");
619 return 0;
620}
Dees_Troy51a0e822012-09-05 15:24:24 -0400621
that3f7b1ac2014-12-30 11:30:13 +0100622int GUIAction::clear(std::string arg)
623{
624 DataManager::SetValue(arg, "0");
625 return 0;
626}
Dees_Troy51a0e822012-09-05 15:24:24 -0400627
that3f7b1ac2014-12-30 11:30:13 +0100628int GUIAction::mount(std::string arg)
629{
630 if (arg == "usb") {
631 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400632 if (!simulate)
that3f7b1ac2014-12-30 11:30:13 +0100633 PartitionManager.usb_storage_enable();
634 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500635 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100636 } else if (!simulate) {
637 PartitionManager.Mount_By_Path(arg, true);
638 PartitionManager.Add_MTP_Storage(arg);
639 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500640 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100641 return 0;
642}
643
644int GUIAction::unmount(std::string arg)
645{
646 if (arg == "usb") {
647 if (!simulate)
648 PartitionManager.usb_storage_disable();
649 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500650 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100651 DataManager::SetValue(TW_ACTION_BUSY, 0);
652 } else if (!simulate) {
653 PartitionManager.UnMount_By_Path(arg, true);
654 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500655 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100656 return 0;
657}
658
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500659int GUIAction::restoredefaultsettings(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100660{
661 operation_start("Restore Defaults");
662 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
Ethan Yonker74db1572015-10-28 12:44:49 -0500663 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100664 else {
665 DataManager::ResetDefaults();
666 PartitionManager.Update_System_Details();
667 PartitionManager.Mount_Current_Storage(true);
668 }
669 operation_end(0);
670 return 0;
671}
672
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500673int GUIAction::copylog(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100674{
675 operation_start("Copy Log");
676 if (!simulate)
677 {
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400678 string dst, curr_storage;
679 int copy_kernel_log = 0;
Captain Throwback52978932021-09-05 15:11:07 -0400680 int copy_logcat = 1;
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400681
682 DataManager::GetValue("tw_include_kernel_log", copy_kernel_log);
Captain Throwback52978932021-09-05 15:11:07 -0400683 DataManager::GetValue("tw_include_logcat", copy_logcat);
that3f7b1ac2014-12-30 11:30:13 +0100684 PartitionManager.Mount_Current_Storage(true);
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400685 curr_storage = DataManager::GetCurrentStoragePath();
686 dst = curr_storage + "/recovery.log";
that3f7b1ac2014-12-30 11:30:13 +0100687 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
688 tw_set_default_metadata(dst.c_str());
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400689 if (copy_kernel_log)
690 TWFunc::copy_kernel_log(curr_storage);
Captain Throwback52978932021-09-05 15:11:07 -0400691 if (copy_logcat)
692 TWFunc::copy_logcat(curr_storage);
that3f7b1ac2014-12-30 11:30:13 +0100693 sync();
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400694 gui_msg(Msg("copy_log=Copied recovery log to {1}")(dst));
that3f7b1ac2014-12-30 11:30:13 +0100695 } else
696 simulate_progress_bar();
697 operation_end(0);
698 return 0;
699}
700
701
702int GUIAction::compute(std::string arg)
703{
704 if (arg.find("+") != string::npos)
705 {
706 string varName = arg.substr(0, arg.find('+'));
707 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
708 int amount_to_add = atoi(string_to_add.c_str());
709 int value;
710
711 DataManager::GetValue(varName, value);
712 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400713 return 0;
714 }
that3f7b1ac2014-12-30 11:30:13 +0100715 if (arg.find("-") != string::npos)
Dees_Troy51a0e822012-09-05 15:24:24 -0400716 {
that3f7b1ac2014-12-30 11:30:13 +0100717 string varName = arg.substr(0, arg.find('-'));
718 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
719 int amount_to_subtract = atoi(string_to_subtract.c_str());
720 int value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400721
that3f7b1ac2014-12-30 11:30:13 +0100722 DataManager::GetValue(varName, value);
723 value -= amount_to_subtract;
724 if (value <= 0)
725 value = 0;
726 DataManager::SetValue(varName, value);
727 return 0;
728 }
729 if (arg.find("*") != string::npos)
730 {
731 string varName = arg.substr(0, arg.find('*'));
732 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
733 int multiply_by = atoi(multiply_by_str.c_str());
734 int value;
735
736 DataManager::GetValue(varName, value);
737 DataManager::SetValue(varName, value*multiply_by);
738 return 0;
739 }
740 if (arg.find("/") != string::npos)
741 {
742 string varName = arg.substr(0, arg.find('/'));
743 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
744 int divide_by = atoi(divide_by_str.c_str());
745 int value;
746
Matt Mowera8a89d12016-12-30 18:10:37 -0600747 if (divide_by != 0)
that3f7b1ac2014-12-30 11:30:13 +0100748 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400749 DataManager::GetValue(varName, value);
that3f7b1ac2014-12-30 11:30:13 +0100750 DataManager::SetValue(varName, value/divide_by);
Dees_Troy51a0e822012-09-05 15:24:24 -0400751 }
752 return 0;
753 }
that3f7b1ac2014-12-30 11:30:13 +0100754 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
755 return -1;
756}
Dees_Troy51a0e822012-09-05 15:24:24 -0400757
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500758int GUIAction::setguitimezone(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100759{
760 string SelectedZone;
761 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
762 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
763 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
764
765 int dst;
766 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
767
768 string offset;
769 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
770
771 string NewTimeZone = Zone;
772 if (offset != "0")
773 NewTimeZone += ":" + offset;
774
775 if (dst != 0)
776 NewTimeZone += DSTZone;
777
778 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
779 DataManager::update_tz_environment_variables();
780 return 0;
781}
782
783int GUIAction::overlay(std::string arg)
784{
785 return gui_changeOverlay(arg);
786}
787
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500788int GUIAction::queuezip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100789{
790 if (zip_queue_index >= 10) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500791 gui_msg("max_queue=Maximum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400792 return 0;
793 }
that3f7b1ac2014-12-30 11:30:13 +0100794 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
795 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
796 zip_queue_index++;
Dees_Troy51a0e822012-09-05 15:24:24 -0400797 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +0100798 }
799 return 0;
800}
801
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500802int GUIAction::cancelzip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100803{
804 if (zip_queue_index <= 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500805 gui_msg("min_queue=Minimum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400806 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100807 } else {
808 zip_queue_index--;
809 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
Dees_Troy51a0e822012-09-05 15:24:24 -0400810 }
that3f7b1ac2014-12-30 11:30:13 +0100811 return 0;
812}
Dees_Troy51a0e822012-09-05 15:24:24 -0400813
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500814int GUIAction::queueclear(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100815{
816 zip_queue_index = 0;
817 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
818 return 0;
819}
Dees_Troy51a0e822012-09-05 15:24:24 -0400820
that3f7b1ac2014-12-30 11:30:13 +0100821int GUIAction::sleep(std::string arg)
822{
823 operation_start("Sleep");
824 usleep(atoi(arg.c_str()));
825 operation_end(0);
826 return 0;
827}
Dees Troyb21cc642013-09-10 17:36:41 +0000828
Matt Mower9a2a2052016-05-31 21:31:22 -0500829int GUIAction::sleepcounter(std::string arg)
830{
831 operation_start("SleepCounter");
832 // Ensure user notices countdown in case it needs to be cancelled
833 blankTimer.resetTimerAndUnblank();
834 int total = atoi(arg.c_str());
835 for (int t = total; t > 0; t--) {
836 int progress = (int)(((float)(total-t)/(float)total)*100.0);
837 DataManager::SetValue("ui_progress", progress);
838 ::sleep(1);
839 DataManager::SetValue("tw_sleep", t-1);
840 }
841 DataManager::SetValue("ui_progress", 100);
842 operation_end(0);
843 return 0;
844}
845
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500846int GUIAction::appenddatetobackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100847{
848 operation_start("AppendDateToBackupName");
849 string Backup_Name;
850 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
851 Backup_Name += TWFunc::Get_Current_Date();
852 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
853 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
854 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
Matt Mower76b8afc2016-06-24 12:04:27 -0500855 PageManager::NotifyKey(KEY_END, true);
856 PageManager::NotifyKey(KEY_END, false);
that3f7b1ac2014-12-30 11:30:13 +0100857 operation_end(0);
858 return 0;
859}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500860
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500861int GUIAction::generatebackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100862{
863 operation_start("GenerateBackupName");
864 TWFunc::Auto_Generate_Backup_Name();
865 operation_end(0);
866 return 0;
867}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500868
Ethan Yonker483e9f42016-01-11 22:21:18 -0600869int GUIAction::checkpartitionlist(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100870{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600871 string List, part_path;
that3f7b1ac2014-12-30 11:30:13 +0100872 int count = 0;
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500873
Ethan Yonker483e9f42016-01-11 22:21:18 -0600874 if (arg.empty())
875 arg = "tw_wipe_list";
876 DataManager::GetValue(arg, List);
877 LOGINFO("checkpartitionlist list '%s'\n", List.c_str());
878 if (!List.empty()) {
879 size_t start_pos = 0, end_pos = List.find(";", start_pos);
880 while (end_pos != string::npos && start_pos < List.size()) {
881 part_path = List.substr(start_pos, end_pos - start_pos);
882 LOGINFO("checkpartitionlist part_path '%s'\n", part_path.c_str());
883 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100884 // Do nothing
Dees_Troy51a0e822012-09-05 15:24:24 -0400885 } else {
that3f7b1ac2014-12-30 11:30:13 +0100886 count++;
887 }
888 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600889 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100890 }
891 DataManager::SetValue("tw_check_partition_list", count);
892 } else {
893 DataManager::SetValue("tw_check_partition_list", 0);
894 }
Matt Mowera8a89d12016-12-30 18:10:37 -0600895 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100896}
Dees_Troy51a0e822012-09-05 15:24:24 -0400897
Ethan Yonker483e9f42016-01-11 22:21:18 -0600898int GUIAction::getpartitiondetails(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100899{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600900 string List, part_path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400901
Ethan Yonker483e9f42016-01-11 22:21:18 -0600902 if (arg.empty())
903 arg = "tw_wipe_list";
904 DataManager::GetValue(arg, List);
905 LOGINFO("getpartitiondetails list '%s'\n", List.c_str());
906 if (!List.empty()) {
907 size_t start_pos = 0, end_pos = List.find(";", start_pos);
908 part_path = List;
909 while (end_pos != string::npos && start_pos < List.size()) {
910 part_path = List.substr(start_pos, end_pos - start_pos);
911 LOGINFO("getpartitiondetails part_path '%s'\n", part_path.c_str());
912 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100913 // Do nothing
914 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600915 DataManager::SetValue("tw_partition_path", part_path);
that3f7b1ac2014-12-30 11:30:13 +0100916 break;
917 }
918 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600919 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100920 }
Ethan Yonker483e9f42016-01-11 22:21:18 -0600921 if (!part_path.empty()) {
922 TWPartition* Part = PartitionManager.Find_Partition_By_Path(part_path);
that3f7b1ac2014-12-30 11:30:13 +0100923 if (Part) {
924 unsigned long long mb = 1048576;
Dees_Troya13d74f2013-03-24 08:54:55 -0500925
that3f7b1ac2014-12-30 11:30:13 +0100926 DataManager::SetValue("tw_partition_name", Part->Display_Name);
927 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
928 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
929 DataManager::SetValue("tw_partition_size", Part->Size / mb);
930 DataManager::SetValue("tw_partition_used", Part->Used / mb);
931 DataManager::SetValue("tw_partition_free", Part->Free / mb);
932 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
933 DataManager::SetValue("tw_partition_removable", Part->Removable);
934 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
935
936 if (Part->Can_Repair())
937 DataManager::SetValue("tw_partition_can_repair", 1);
938 else
939 DataManager::SetValue("tw_partition_can_repair", 0);
Ethan Yonkera2719152015-05-28 09:44:41 -0500940 if (Part->Can_Resize())
941 DataManager::SetValue("tw_partition_can_resize", 1);
942 else
943 DataManager::SetValue("tw_partition_can_resize", 0);
bigbiffad58e1b2020-07-06 20:24:34 -0400944 if (TWFunc::Path_Exists("/system/bin/mkfs.fat"))
that3f7b1ac2014-12-30 11:30:13 +0100945 DataManager::SetValue("tw_partition_vfat", 1);
946 else
947 DataManager::SetValue("tw_partition_vfat", 0);
bigbiffad58e1b2020-07-06 20:24:34 -0400948 if (TWFunc::Path_Exists("/system/bin/mkexfatfs"))
that3f7b1ac2014-12-30 11:30:13 +0100949 DataManager::SetValue("tw_partition_exfat", 1);
950 else
951 DataManager::SetValue("tw_partition_exfat", 0);
whylef06b1652020-11-22 00:25:18 +0100952 if (TWFunc::Path_Exists("/system/bin/mkfs.f2fs") || TWFunc::Path_Exists("/system/bin/make_f2fs"))
that3f7b1ac2014-12-30 11:30:13 +0100953 DataManager::SetValue("tw_partition_f2fs", 1);
954 else
955 DataManager::SetValue("tw_partition_f2fs", 0);
bigbiffad58e1b2020-07-06 20:24:34 -0400956 if (TWFunc::Path_Exists("/system/bin/mke2fs"))
that3f7b1ac2014-12-30 11:30:13 +0100957 DataManager::SetValue("tw_partition_ext", 1);
958 else
959 DataManager::SetValue("tw_partition_ext", 0);
960 return 0;
961 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600962 LOGERR("Unable to locate partition: '%s'\n", part_path.c_str());
that3f7b1ac2014-12-30 11:30:13 +0100963 }
964 }
965 }
966 DataManager::SetValue("tw_partition_name", "");
967 DataManager::SetValue("tw_partition_file_system", "");
Ethan Yonker483e9f42016-01-11 22:21:18 -0600968 // Set this to 0 to prevent trying to partition this device, just in case
969 DataManager::SetValue("tw_partition_removable", 0);
that3f7b1ac2014-12-30 11:30:13 +0100970 return 0;
971}
972
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500973int GUIAction::screenshot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100974{
975 time_t tm;
976 char path[256];
977 int path_len;
Matt Mower0c88b842017-01-15 16:00:49 -0600978 uid_t uid = AID_MEDIA_RW;
979 gid_t gid = AID_MEDIA_RW;
that3f7b1ac2014-12-30 11:30:13 +0100980
981 const std::string storage = DataManager::GetCurrentStoragePath();
Matt Mowera8a89d12016-12-30 18:10:37 -0600982 if (PartitionManager.Is_Mounted_By_Path(storage)) {
that3f7b1ac2014-12-30 11:30:13 +0100983 snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
984 } else {
985 strcpy(path, "/tmp/");
986 }
987
Matt Mowera8a89d12016-12-30 18:10:37 -0600988 if (!TWFunc::Create_Dir_Recursive(path, 0775, uid, gid))
that3f7b1ac2014-12-30 11:30:13 +0100989 return 0;
990
991 tm = time(NULL);
992 path_len = strlen(path);
993
994 // Screenshot_2014-01-01-18-21-38.png
995 strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
996
997 int res = gr_save_screenshot(path);
Matt Mowera8a89d12016-12-30 18:10:37 -0600998 if (res == 0) {
that3f7b1ac2014-12-30 11:30:13 +0100999 chmod(path, 0666);
1000 chown(path, uid, gid);
1001
that677b13f2015-12-26 23:57:31 +01001002 gui_msg(Msg("screenshot_saved=Screenshot was saved to {1}")(path));
that3f7b1ac2014-12-30 11:30:13 +01001003
VDavid0032034a412019-10-18 22:43:20 +02001004 // blink to notify that the screenshot was taken
that3f7b1ac2014-12-30 11:30:13 +01001005 gr_color(255, 255, 255, 255);
1006 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
1007 gr_flip();
1008 gui_forceRender();
1009 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001010 gui_err("screenshot_err=Failed to take a screenshot!");
that3f7b1ac2014-12-30 11:30:13 +01001011 }
1012 return 0;
1013}
1014
1015int GUIAction::setbrightness(std::string arg)
1016{
1017 return TWFunc::Set_Brightness(arg);
1018}
1019
1020int GUIAction::fileexists(std::string arg)
1021{
1022 struct stat st;
1023 string newpath = arg + "/.";
1024
1025 operation_start("FileExists");
1026 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
1027 operation_end(0);
1028 else
1029 operation_end(1);
1030 return 0;
1031}
1032
thatcc8ddca2015-01-03 01:59:36 +01001033void GUIAction::reinject_after_flash()
1034{
1035 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001036 gui_msg("injecttwrp=Injecting TWRP into boot image...");
thatcc8ddca2015-01-03 01:59:36 +01001037 if (simulate) {
1038 simulate_progress_bar();
1039 } else {
1040 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
1041 if (Boot == NULL || Boot->Current_File_System != "emmc")
1042 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
1043 else {
1044 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
1045 TWFunc::Exec_Cmd(injectcmd);
1046 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001047 gui_msg("done=Done.");
thatcc8ddca2015-01-03 01:59:36 +01001048 }
1049 }
1050}
1051
mauronofrio6d3bf892019-10-26 19:47:55 +02001052int GUIAction::ozip_decrypt(string zip_path)
1053{
bigbiffad58e1b2020-07-06 20:24:34 -04001054 if (!TWFunc::Path_Exists("/system/bin/ozip_decrypt")) {
mauronofrio6d3bf892019-10-26 19:47:55 +02001055 return 1;
1056 }
1057 gui_msg("ozip_decrypt_decryption=Starting Ozip Decryption...");
1058 TWFunc::Exec_Cmd("ozip_decrypt " + (string)TW_OZIP_DECRYPT_KEY + " '" + zip_path + "'");
1059 gui_msg("ozip_decrypt_finish=Ozip Decryption Finished!");
1060 return 0;
1061}
1062
that3f7b1ac2014-12-30 11:30:13 +01001063int GUIAction::flash(std::string arg)
1064{
1065 int i, ret_val = 0, wipe_cache = 0;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001066 // We're going to jump to this page first, like a loading page
1067 gui_changePage(arg);
that3f7b1ac2014-12-30 11:30:13 +01001068 for (i=0; i<zip_queue_index; i++) {
thatc7b631b2015-06-01 23:36:57 +02001069 string zip_path = zip_queue[i];
1070 size_t slashpos = zip_path.find_last_of('/');
1071 string zip_filename = (slashpos == string::npos) ? zip_path : zip_path.substr(slashpos + 1);
that3f7b1ac2014-12-30 11:30:13 +01001072 operation_start("Flashing");
mauronofrio6d3bf892019-10-26 19:47:55 +02001073 if((zip_path.substr(zip_path.size() - 4, 4)) == "ozip")
1074 {
1075 if((ozip_decrypt(zip_path)) != 0)
1076 {
1077 LOGERR("Unable to find ozip_decrypt!");
1078 break;
1079 }
1080 zip_filename = (zip_filename.substr(0, zip_filename.size() - 4)).append("zip");
1081 zip_path = (zip_path.substr(0, zip_path.size() - 4)).append("zip");
1082 if (!TWFunc::Path_Exists(zip_path)) {
1083 LOGERR("Unable to find decrypted zip");
1084 break;
1085 }
1086 }
thatc7b631b2015-06-01 23:36:57 +02001087 DataManager::SetValue("tw_filename", zip_path);
1088 DataManager::SetValue("tw_file", zip_filename);
that3f7b1ac2014-12-30 11:30:13 +01001089 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
1090
1091 TWFunc::SetPerformanceMode(true);
thatc7b631b2015-06-01 23:36:57 +02001092 ret_val = flash_zip(zip_path, &wipe_cache);
that3f7b1ac2014-12-30 11:30:13 +01001093 TWFunc::SetPerformanceMode(false);
1094 if (ret_val != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001095 gui_msg(Msg(msg::kError, "zip_err=Error installing zip file '{1}'")(zip_path));
that3f7b1ac2014-12-30 11:30:13 +01001096 ret_val = 1;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001097 break;
that3f7b1ac2014-12-30 11:30:13 +01001098 }
1099 }
1100 zip_queue_index = 0;
that3f7b1ac2014-12-30 11:30:13 +01001101
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001102 if (wipe_cache) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001103 gui_msg("zip_wipe_cache=One or more zip requested a cache wipe -- Wiping cache now.");
that3f7b1ac2014-12-30 11:30:13 +01001104 PartitionManager.Wipe_By_Path("/cache");
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001105 }
that3f7b1ac2014-12-30 11:30:13 +01001106
thatcc8ddca2015-01-03 01:59:36 +01001107 reinject_after_flash();
that3f7b1ac2014-12-30 11:30:13 +01001108 PartitionManager.Update_System_Details();
1109 operation_end(ret_val);
bigbiffa869fc72016-03-01 19:40:36 -05001110 // 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 -06001111 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +01001112 return 0;
1113}
1114
1115int GUIAction::wipe(std::string arg)
1116{
1117 operation_start("Format");
1118 DataManager::SetValue("tw_partition", arg);
that3f7b1ac2014-12-30 11:30:13 +01001119 int ret_val = false;
1120
1121 if (simulate) {
1122 simulate_progress_bar();
1123 } else {
1124 if (arg == "data")
1125 ret_val = PartitionManager.Factory_Reset();
1126 else if (arg == "battery")
1127 ret_val = PartitionManager.Wipe_Battery_Stats();
1128 else if (arg == "rotate")
1129 ret_val = PartitionManager.Wipe_Rotate_Data();
1130 else if (arg == "dalvik")
1131 ret_val = PartitionManager.Wipe_Dalvik_Cache();
1132 else if (arg == "DATAMEDIA") {
1133 ret_val = PartitionManager.Format_Data();
1134 } else if (arg == "INTERNAL") {
Matt Mower23d8aae2017-01-06 14:30:33 -06001135 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001136
1137 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1138 if (has_datamedia) {
1139 ret_val = PartitionManager.Wipe_Media_From_Data();
1140 } else {
1141 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
1142 }
1143 } else if (arg == "EXTERNAL") {
1144 string External_Path;
1145
1146 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
1147 ret_val = PartitionManager.Wipe_By_Path(External_Path);
1148 } else if (arg == "ANDROIDSECURE") {
1149 ret_val = PartitionManager.Wipe_Android_Secure();
1150 } else if (arg == "LIST") {
1151 string Wipe_List, wipe_path;
1152 bool skip = false;
1153 ret_val = true;
that3f7b1ac2014-12-30 11:30:13 +01001154
1155 DataManager::GetValue("tw_wipe_list", Wipe_List);
1156 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
1157 if (!Wipe_List.empty()) {
1158 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
1159 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
1160 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
1161 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
1162 if (wipe_path == "/and-sec") {
1163 if (!PartitionManager.Wipe_Android_Secure()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001164 gui_msg("and_sec_wipe_err=Unable to wipe android secure");
that3f7b1ac2014-12-30 11:30:13 +01001165 ret_val = false;
1166 break;
1167 } else {
1168 skip = true;
1169 }
1170 } else if (wipe_path == "DALVIK") {
1171 if (!PartitionManager.Wipe_Dalvik_Cache()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001172 gui_err("dalvik_wipe_err=Failed to wipe dalvik");
that3f7b1ac2014-12-30 11:30:13 +01001173 ret_val = false;
1174 break;
1175 } else {
1176 skip = true;
1177 }
1178 } else if (wipe_path == "INTERNAL") {
1179 if (!PartitionManager.Wipe_Media_From_Data()) {
1180 ret_val = false;
1181 break;
1182 } else {
1183 skip = true;
Dees_Troya13d74f2013-03-24 08:54:55 -05001184 }
1185 }
that3f7b1ac2014-12-30 11:30:13 +01001186 if (!skip) {
1187 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001188 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(wipe_path));
that3f7b1ac2014-12-30 11:30:13 +01001189 ret_val = false;
1190 break;
1191 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
1192 arg = wipe_path;
1193 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001194 } else {
that3f7b1ac2014-12-30 11:30:13 +01001195 skip = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001196 }
that3f7b1ac2014-12-30 11:30:13 +01001197 start_pos = end_pos + 1;
1198 end_pos = Wipe_List.find(";", start_pos);
Dees_Troy51a0e822012-09-05 15:24:24 -04001199 }
1200 }
that3f7b1ac2014-12-30 11:30:13 +01001201 } else
1202 ret_val = PartitionManager.Wipe_By_Path(arg);
that73a52952015-01-28 23:07:34 +01001203#ifndef TW_OEM_BUILD
that3f7b1ac2014-12-30 11:30:13 +01001204 if (arg == DataManager::GetSettingsStoragePath()) {
1205 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
1206 string Storage_Path = DataManager::GetSettingsStoragePath();
Dees_Troy51a0e822012-09-05 15:24:24 -04001207
that3f7b1ac2014-12-30 11:30:13 +01001208 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
1209 LOGINFO("Making TWRP folder and saving settings.\n");
1210 Storage_Path += "/TWRP";
1211 mkdir(Storage_Path.c_str(), 0777);
1212 DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001213 } else {
that3f7b1ac2014-12-30 11:30:13 +01001214 LOGERR("Unable to recreate TWRP folder and save settings.\n");
1215 }
1216 }
1217#endif
1218 }
1219 PartitionManager.Update_System_Details();
1220 if (ret_val)
1221 ret_val = 0; // 0 is success
1222 else
1223 ret_val = 1; // 1 is failure
1224 operation_end(ret_val);
1225 return 0;
1226}
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001227
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001228int GUIAction::refreshsizes(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001229{
1230 operation_start("Refreshing Sizes");
1231 if (simulate) {
1232 simulate_progress_bar();
1233 } else
1234 PartitionManager.Update_System_Details();
1235 operation_end(0);
1236 return 0;
1237}
1238
1239int GUIAction::nandroid(std::string arg)
1240{
that3f7b1ac2014-12-30 11:30:13 +01001241 if (simulate) {
that73a52952015-01-28 23:07:34 +01001242 PartitionManager.stop_backup.set_value(0);
that3f7b1ac2014-12-30 11:30:13 +01001243 DataManager::SetValue("tw_partition", "Simulation");
1244 simulate_progress_bar();
that73a52952015-01-28 23:07:34 +01001245 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001246 } else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001247 operation_start("Nandroid");
1248 int ret = 0;
1249
that3f7b1ac2014-12-30 11:30:13 +01001250 if (arg == "backup") {
1251 string Backup_Name;
1252 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Ethan Yonker4adc33e2016-01-22 16:07:11 -06001253 string auto_gen = gui_lookup("auto_generate", "(Auto Generate)");
Ethan Yonker53796e72019-01-11 22:49:52 -06001254 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 -05001255 ret = PartitionManager.Run_Backup(false);
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001256 DataManager::SetValue("tw_encrypt_backup", 0); // reset value so we don't encrypt every subsequent backup
1257 if (!PartitionManager.stop_backup.get_value()) {
1258 if (ret == false)
1259 ret = 1; // 1 for failure
1260 else
1261 ret = 0; // 0 for success
1262 DataManager::SetValue("tw_cancel_backup", 0);
1263 } else {
1264 DataManager::SetValue("tw_cancel_backup", 1);
1265 gui_msg("backup_cancel=Backup Cancelled");
1266 ret = 0;
1267 }
bigbiffce8f83c2015-12-12 18:30:21 -05001268 } else {
that3f7b1ac2014-12-30 11:30:13 +01001269 operation_end(1);
1270 return -1;
that3f7b1ac2014-12-30 11:30:13 +01001271 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001272 DataManager::SetValue(TW_BACKUP_NAME, auto_gen);
that3f7b1ac2014-12-30 11:30:13 +01001273 } else if (arg == "restore") {
1274 string Restore_Name;
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001275 int gui_adb_backup;
1276
that3f7b1ac2014-12-30 11:30:13 +01001277 DataManager::GetValue("tw_restore", Restore_Name);
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001278 DataManager::GetValue("tw_enable_adb_backup", gui_adb_backup);
1279 if (gui_adb_backup) {
1280 DataManager::SetValue("tw_operation_state", 1);
1281 if (TWFunc::stream_adb_backup(Restore_Name) == 0)
1282 ret = 0; // success
1283 else
1284 ret = 1; // failure
1285 DataManager::SetValue("tw_enable_adb_backup", 0);
1286 ret = 0; // assume success???
1287 } else {
1288 if (PartitionManager.Run_Restore(Restore_Name))
1289 ret = 0; // success
1290 else
1291 ret = 1; // failure
1292 }
that3f7b1ac2014-12-30 11:30:13 +01001293 } else {
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001294 operation_end(1); // invalid arg specified, fail
bigbiff7abc5fe2015-01-17 16:53:12 -05001295 return -1;
1296 }
that73a52952015-01-28 23:07:34 +01001297 operation_end(ret);
bigbiff7abc5fe2015-01-17 16:53:12 -05001298 return ret;
1299 }
1300 return 0;
1301}
1302
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001303int GUIAction::cancelbackup(std::string arg __unused) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001304 if (simulate) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001305 PartitionManager.stop_backup.set_value(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001306 }
1307 else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001308 int op_status = PartitionManager.Cancel_Backup();
1309 if (op_status != 0)
1310 op_status = 1; // failure
bigbiff7abc5fe2015-01-17 16:53:12 -05001311 }
1312
1313 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001314}
Dees_Troy51a0e822012-09-05 15:24:24 -04001315
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001316int GUIAction::fixcontexts(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001317{
that73a52952015-01-28 23:07:34 +01001318 int op_status = 0;
1319
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001320 operation_start("Fix Contexts");
1321 LOGINFO("fix contexts started!\n");
that3f7b1ac2014-12-30 11:30:13 +01001322 if (simulate) {
1323 simulate_progress_bar();
1324 } else {
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001325 op_status = PartitionManager.Fix_Contexts();
that3f7b1ac2014-12-30 11:30:13 +01001326 if (op_status != 0)
1327 op_status = 1; // failure
that3f7b1ac2014-12-30 11:30:13 +01001328 }
that73a52952015-01-28 23:07:34 +01001329 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001330 return 0;
1331}
1332
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001333int GUIAction::fixpermissions(std::string arg)
1334{
1335 return fixcontexts(arg);
1336}
1337
that3f7b1ac2014-12-30 11:30:13 +01001338int GUIAction::dd(std::string arg)
1339{
1340 operation_start("imaging");
1341
1342 if (simulate) {
1343 simulate_progress_bar();
1344 } else {
1345 string cmd = "dd " + arg;
1346 TWFunc::Exec_Cmd(cmd);
1347 }
1348 operation_end(0);
1349 return 0;
1350}
1351
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001352int GUIAction::partitionsd(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001353{
1354 operation_start("Partition SD Card");
1355 int ret_val = 0;
1356
1357 if (simulate) {
1358 simulate_progress_bar();
1359 } else {
1360 int allow_partition;
1361 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1362 if (allow_partition == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001363 gui_err("no_real_sdcard=This device does not have a real SD Card! Aborting!");
that3f7b1ac2014-12-30 11:30:13 +01001364 } else {
1365 if (!PartitionManager.Partition_SDCard())
1366 ret_val = 1; // failed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001367 }
that3f7b1ac2014-12-30 11:30:13 +01001368 }
1369 operation_end(ret_val);
1370 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001371
that3f7b1ac2014-12-30 11:30:13 +01001372}
Dees_Troy51a0e822012-09-05 15:24:24 -04001373
that3f7b1ac2014-12-30 11:30:13 +01001374int GUIAction::cmd(std::string arg)
1375{
1376 int op_status = 0;
1377
1378 operation_start("Command");
1379 LOGINFO("Running command: '%s'\n", arg.c_str());
1380 if (simulate) {
1381 simulate_progress_bar();
1382 } else {
1383 op_status = TWFunc::Exec_Cmd(arg);
1384 if (op_status != 0)
1385 op_status = 1;
1386 }
1387
1388 operation_end(op_status);
1389 return 0;
1390}
1391
1392int GUIAction::terminalcommand(std::string arg)
1393{
1394 int op_status = 0;
1395 string cmdpath, command;
1396
1397 DataManager::GetValue("tw_terminal_location", cmdpath);
1398 operation_start("CommandOutput");
1399 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
1400 if (simulate) {
1401 simulate_progress_bar();
1402 operation_end(op_status);
Matt Mower5aa29ab2015-02-25 23:50:55 -06001403 } else if (arg == "exit") {
1404 LOGINFO("Exiting terminal\n");
1405 operation_end(op_status);
1406 page("main");
that3f7b1ac2014-12-30 11:30:13 +01001407 } else {
1408 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
1409 LOGINFO("Actual command is: '%s'\n", command.c_str());
that3f7b1ac2014-12-30 11:30:13 +01001410 DataManager::SetValue("tw_terminal_state", 1);
1411 DataManager::SetValue("tw_background_thread_running", 1);
thatc6085482015-01-09 22:12:43 +01001412 FILE* fp;
1413 char line[512];
1414
1415 fp = popen(command.c_str(), "r");
1416 if (fp == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001417 LOGERR("Error opening command to run (%s).\n", strerror(errno));
thatc6085482015-01-09 22:12:43 +01001418 } else {
Matt Mower23d8aae2017-01-06 14:30:33 -06001419 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1;
thatc6085482015-01-09 22:12:43 +01001420 struct timeval timeout;
1421 fd_set fdset;
1422
Matt Mowera8a89d12016-12-30 18:10:37 -06001423 while (keep_going)
thatc6085482015-01-09 22:12:43 +01001424 {
1425 FD_ZERO(&fdset);
1426 FD_SET(fd, &fdset);
1427 timeout.tv_sec = 0;
1428 timeout.tv_usec = 400000;
1429 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1430 if (has_data == 0) {
1431 // Timeout reached
1432 DataManager::GetValue("tw_terminal_state", check);
1433 if (check == 0) {
1434 keep_going = 0;
1435 }
1436 } else if (has_data < 0) {
1437 // End of execution
1438 keep_going = 0;
1439 } else {
1440 // Try to read output
Matt Mowera8a89d12016-12-30 18:10:37 -06001441 if (fgets(line, sizeof(line), fp) != NULL)
thatc6085482015-01-09 22:12:43 +01001442 gui_print("%s", line); // Display output
1443 else
1444 keep_going = 0; // Done executing
1445 }
1446 }
1447 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -04001448 }
thatc6085482015-01-09 22:12:43 +01001449 DataManager::SetValue("tw_operation_status", 0);
1450 DataManager::SetValue("tw_operation_state", 1);
1451 DataManager::SetValue("tw_terminal_state", 0);
1452 DataManager::SetValue("tw_background_thread_running", 0);
1453 DataManager::SetValue(TW_ACTION_BUSY, 0);
that3f7b1ac2014-12-30 11:30:13 +01001454 }
1455 return 0;
1456}
1457
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001458int GUIAction::killterminal(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001459{
that3f7b1ac2014-12-30 11:30:13 +01001460 LOGINFO("Sending kill command...\n");
1461 operation_start("KillCommand");
1462 DataManager::SetValue("tw_operation_status", 0);
1463 DataManager::SetValue("tw_operation_state", 1);
1464 DataManager::SetValue("tw_terminal_state", 0);
1465 DataManager::SetValue("tw_background_thread_running", 0);
1466 DataManager::SetValue(TW_ACTION_BUSY, 0);
1467 return 0;
1468}
1469
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001470int GUIAction::reinjecttwrp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001471{
1472 int op_status = 0;
1473 operation_start("ReinjectTWRP");
Ethan Yonker74db1572015-10-28 12:44:49 -05001474 gui_msg("injecttwrp=Injecting TWRP into boot image...");
that3f7b1ac2014-12-30 11:30:13 +01001475 if (simulate) {
1476 simulate_progress_bar();
1477 } else {
1478 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Ethan Yonker74db1572015-10-28 12:44:49 -05001479 gui_msg("done=Done.");
that3f7b1ac2014-12-30 11:30:13 +01001480 }
1481
1482 operation_end(op_status);
1483 return 0;
1484}
1485
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001486int GUIAction::checkbackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001487{
1488 int op_status = 0;
1489
1490 operation_start("CheckBackupName");
1491 if (simulate) {
1492 simulate_progress_bar();
1493 } else {
Ethan Yonker53796e72019-01-11 22:49:52 -06001494 string Backup_Name;
1495 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
1496 op_status = PartitionManager.Check_Backup_Name(Backup_Name, true, true);
that3f7b1ac2014-12-30 11:30:13 +01001497 if (op_status != 0)
1498 op_status = 1;
1499 }
1500
1501 operation_end(op_status);
1502 return 0;
1503}
1504
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001505int GUIAction::decrypt(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001506{
1507 int op_status = 0;
1508
1509 operation_start("Decrypt");
1510 if (simulate) {
1511 simulate_progress_bar();
1512 } else {
1513 string Password;
Noah Jacobson81d638d2019-04-28 00:10:07 -04001514 string userID;
that3f7b1ac2014-12-30 11:30:13 +01001515 DataManager::GetValue("tw_crypto_password", Password);
Noah Jacobson81d638d2019-04-28 00:10:07 -04001516
1517 if (DataManager::GetIntValue(TW_IS_FBE)) { // for FBE
1518 DataManager::GetValue("tw_crypto_user_id", userID);
1519 if (userID != "") {
1520 op_status = PartitionManager.Decrypt_Device(Password, atoi(userID.c_str()));
1521 if (userID != "0") {
1522 if (op_status != 0)
1523 op_status = 1;
1524 operation_end(op_status);
1525 return 0;
1526 }
1527 } else {
1528 LOGINFO("User ID not found\n");
1529 op_status = 1;
1530 }
1531 ::sleep(1);
1532 } else { // for FDE
1533 op_status = PartitionManager.Decrypt_Device(Password);
1534 }
1535
that3f7b1ac2014-12-30 11:30:13 +01001536 if (op_status != 0)
1537 op_status = 1;
1538 else {
that3f7b1ac2014-12-30 11:30:13 +01001539 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1540
Ethan Yonkercf50da52015-01-12 21:59:07 -06001541 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001542
Ethan Yonkercf50da52015-01-12 21:59:07 -06001543 // Check for a custom theme and load it if exists
1544 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1545 if (has_datamedia != 0) {
1546 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06001547 LOGINFO("Failed to get default contexts and file mode for storage files.\n");
Ethan Yonkercf50da52015-01-12 21:59:07 -06001548 } else {
1549 LOGINFO("Got default contexts and file mode for storage files.\n");
that3f7b1ac2014-12-30 11:30:13 +01001550 }
1551 }
Ethan Yonker66a19492015-12-10 10:19:45 -06001552 PartitionManager.Decrypt_Adopted();
that3f7b1ac2014-12-30 11:30:13 +01001553 }
1554 }
1555
1556 operation_end(op_status);
1557 return 0;
1558}
1559
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001560int GUIAction::adbsideload(std::string arg __unused)
thatcc8ddca2015-01-03 01:59:36 +01001561{
1562 operation_start("Sideload");
1563 if (simulate) {
1564 simulate_progress_bar();
1565 operation_end(0);
1566 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001567 gui_msg("start_sideload=Starting ADB sideload feature...");
thatc6085482015-01-09 22:12:43 +01001568 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
1569
1570 // wait for the adb connection
bigbiffd58ba182020-03-23 10:02:29 -04001571 Device::BuiltinAction reboot_action = Device::REBOOT_BOOTLOADER;
bigbiff1f9e4842020-10-31 11:33:15 -04001572 int ret = twrp_sideload("/", &reboot_action);
1573 sideload_child_pid = GetMiniAdbdPid();
thatc6085482015-01-09 22:12:43 +01001574 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
1575
1576 if (ret != 0) {
1577 if (ret == -2)
Ethan Yonker74db1572015-10-28 12:44:49 -05001578 gui_msg("need_new_adb=You need adb 1.0.32 or newer to sideload to this device.");
thatc6085482015-01-09 22:12:43 +01001579 ret = 1; // failure
1580 } else {
1581 int wipe_cache = 0;
1582 int wipe_dalvik = 0;
1583 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
bigbiff1f9e4842020-10-31 11:33:15 -04001584 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1585 PartitionManager.Wipe_By_Path("/cache");
1586 if (wipe_dalvik)
1587 PartitionManager.Wipe_Dalvik_Cache();
thatcc8ddca2015-01-03 01:59:36 +01001588 }
thatc6085482015-01-09 22:12:43 +01001589 TWFunc::Toggle_MTP(mtp_was_enabled);
1590 reinject_after_flash();
1591 operation_end(ret);
thatcc8ddca2015-01-03 01:59:36 +01001592 }
that3f7b1ac2014-12-30 11:30:13 +01001593 return 0;
1594}
1595
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001596int GUIAction::adbsideloadcancel(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001597{
that3f7b1ac2014-12-30 11:30:13 +01001598 struct stat st;
1599 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
Ethan Yonker74db1572015-10-28 12:44:49 -05001600 gui_msg("cancel_sideload=Cancelling ADB sideload...");
thatcc8ddca2015-01-03 01:59:36 +01001601 LOGINFO("Signaling child sideload process to exit.\n");
1602 // Calling stat() on this magic filename signals the minadbd
1603 // subprocess to shut down.
1604 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
bigbiff1f9e4842020-10-31 11:33:15 -04001605 sideload_child_pid = GetMiniAdbdPid();
thatcc8ddca2015-01-03 01:59:36 +01001606 if (!sideload_child_pid) {
1607 LOGERR("Unable to get child ID\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001608 return 0;
1609 }
thatcc8ddca2015-01-03 01:59:36 +01001610 ::sleep(1);
1611 LOGINFO("Killing child sideload process.\n");
1612 kill(sideload_child_pid, SIGTERM);
1613 int status;
1614 LOGINFO("Waiting for child sideload process to exit.\n");
1615 waitpid(sideload_child_pid, &status, 0);
1616 sideload_child_pid = 0;
that3f7b1ac2014-12-30 11:30:13 +01001617 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
1618 return 0;
1619}
1620
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001621int GUIAction::openrecoveryscript(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001622{
1623 operation_start("OpenRecoveryScript");
1624 if (simulate) {
1625 simulate_progress_bar();
Ethan Yonker24e7eb72015-01-03 16:13:06 -06001626 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001627 } else {
that10ae24f2015-12-26 20:53:51 +01001628 int op_status = OpenRecoveryScript::Run_OpenRecoveryScript_Action();
Ethan Yonkere1abe612015-06-09 11:09:32 -05001629 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001630 }
1631 return 0;
1632}
1633
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001634int GUIAction::installsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001635{
1636 int op_status = 0;
1637
1638 operation_start("Install SuperSU");
1639 if (simulate) {
1640 simulate_progress_bar();
1641 } else {
Ethan Yonkerfa67cbf2018-07-20 12:22:33 -05001642 LOGERR("Installing SuperSU was deprecated from TWRP.\n");
that3f7b1ac2014-12-30 11:30:13 +01001643 }
1644
1645 operation_end(op_status);
1646 return 0;
1647}
1648
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001649int GUIAction::fixsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001650{
1651 int op_status = 0;
1652
1653 operation_start("Fixing Superuser Permissions");
1654 if (simulate) {
1655 simulate_progress_bar();
1656 } else {
1657 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1658 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
1659 }
1660
1661 operation_end(op_status);
1662 return 0;
1663}
1664
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001665int GUIAction::decrypt_backup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001666{
1667 int op_status = 0;
1668
1669 operation_start("Try Restore Decrypt");
1670 if (simulate) {
1671 simulate_progress_bar();
1672 } else {
1673 string Restore_Path, Filename, Password;
1674 DataManager::GetValue("tw_restore", Restore_Path);
1675 Restore_Path += "/";
1676 DataManager::GetValue("tw_restore_password", Password);
1677 TWFunc::SetPerformanceMode(true);
1678 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1679 op_status = 0; // success
1680 else
1681 op_status = 1; // fail
1682 TWFunc::SetPerformanceMode(false);
1683 }
1684
1685 operation_end(op_status);
1686 return 0;
1687}
1688
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001689int GUIAction::repair(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001690{
1691 int op_status = 0;
1692
1693 operation_start("Repair Partition");
1694 if (simulate) {
1695 simulate_progress_bar();
1696 } else {
1697 string part_path;
1698 DataManager::GetValue("tw_partition_mount_point", part_path);
1699 if (PartitionManager.Repair_By_Path(part_path, true)) {
1700 op_status = 0; // success
1701 } else {
that3f7b1ac2014-12-30 11:30:13 +01001702 op_status = 1; // fail
1703 }
1704 }
1705
1706 operation_end(op_status);
1707 return 0;
1708}
1709
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001710int GUIAction::resize(std::string arg __unused)
Ethan Yonkera2719152015-05-28 09:44:41 -05001711{
1712 int op_status = 0;
1713
1714 operation_start("Resize Partition");
1715 if (simulate) {
1716 simulate_progress_bar();
1717 } else {
1718 string part_path;
1719 DataManager::GetValue("tw_partition_mount_point", part_path);
1720 if (PartitionManager.Resize_By_Path(part_path, true)) {
1721 op_status = 0; // success
1722 } else {
Ethan Yonkera2719152015-05-28 09:44:41 -05001723 op_status = 1; // fail
1724 }
1725 }
1726
1727 operation_end(op_status);
1728 return 0;
1729}
1730
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001731int GUIAction::changefilesystem(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001732{
1733 int op_status = 0;
1734
1735 operation_start("Change File System");
1736 if (simulate) {
1737 simulate_progress_bar();
1738 } else {
1739 string part_path, file_system;
1740 DataManager::GetValue("tw_partition_mount_point", part_path);
1741 DataManager::GetValue("tw_action_new_file_system", file_system);
1742 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1743 op_status = 0; // success
1744 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001745 gui_err("change_fs_err=Error changing file system.");
that3f7b1ac2014-12-30 11:30:13 +01001746 op_status = 1; // fail
1747 }
1748 }
1749 PartitionManager.Update_System_Details();
1750 operation_end(op_status);
1751 return 0;
1752}
1753
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001754int GUIAction::startmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001755{
1756 int op_status = 0;
1757
1758 operation_start("Start MTP");
1759 if (PartitionManager.Enable_MTP())
1760 op_status = 0; // success
1761 else
1762 op_status = 1; // fail
1763
1764 operation_end(op_status);
1765 return 0;
1766}
1767
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001768int GUIAction::stopmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001769{
1770 int op_status = 0;
1771
1772 operation_start("Stop MTP");
1773 if (PartitionManager.Disable_MTP())
1774 op_status = 0; // success
1775 else
1776 op_status = 1; // fail
1777
1778 operation_end(op_status);
1779 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001780}
1781
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001782int GUIAction::flashimage(std::string arg __unused)
Ethan Yonker96af84a2015-01-05 14:58:36 -06001783{
1784 int op_status = 0;
epicX8f52c0a2021-02-24 23:12:08 +05301785 bool flag = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001786
1787 operation_start("Flash Image");
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001788 string path, filename;
1789 DataManager::GetValue("tw_zip_location", path);
1790 DataManager::GetValue("tw_file", filename);
Ethan Yonker96af84a2015-01-05 14:58:36 -06001791
epicX8f52c0a2021-02-24 23:12:08 +05301792#ifdef AB_OTA_UPDATER
1793 string target = DataManager::GetStrValue("tw_flash_partition");
1794 unsigned int pos = target.find_last_of(';');
1795 string mount_point = pos != string::npos ? target.substr(0, pos) : "";
1796 TWPartition* t_part = PartitionManager.Find_Partition_By_Path(mount_point);
1797 bool flash_in_both_slots = DataManager::GetIntValue("tw_flash_both_slots") ? true : false;
1798
1799 if (t_part != NULL && (flash_in_both_slots && t_part->SlotSelect))
1800 {
1801 string current_slot = PartitionManager.Get_Active_Slot_Display();
1802 bool pre_op_status = PartitionManager.Flash_Image(path, filename);
1803
Captain Throwbackd0fcdaf2022-05-26 14:14:12 -04001804 PartitionManager.Override_Active_Slot(current_slot == "A" ? "B" : "A");
epicX8f52c0a2021-02-24 23:12:08 +05301805 op_status = (int) !(pre_op_status && PartitionManager.Flash_Image(path, filename));
Captain Throwbackd0fcdaf2022-05-26 14:14:12 -04001806 PartitionManager.Override_Active_Slot(current_slot);
epicX8f52c0a2021-02-24 23:12:08 +05301807
1808 DataManager::SetValue("tw_flash_both_slots", 0);
1809 flag = false;
1810 }
1811#endif
1812 if (flag)
1813 {
1814 if (PartitionManager.Flash_Image(path, filename))
1815 op_status = 0; // success
1816 else
1817 op_status = 1; // fail
1818 }
1819
Ethan Yonker96af84a2015-01-05 14:58:36 -06001820 operation_end(op_status);
1821 return 0;
1822}
1823
that10ae24f2015-12-26 20:53:51 +01001824int GUIAction::twcmd(std::string arg)
1825{
1826 operation_start("TWRP CLI Command");
1827 if (simulate)
1828 simulate_progress_bar();
1829 else
1830 OpenRecoveryScript::Run_CLI_Command(arg.c_str());
1831 operation_end(0);
1832 return 0;
1833}
1834
Dees_Troy51a0e822012-09-05 15:24:24 -04001835int GUIAction::getKeyByName(std::string key)
1836{
that8834a0f2016-01-05 23:29:30 +01001837 if (key == "home") return KEY_HOMEPAGE; // note: KEY_HOME is cursor movement (like KEY_END)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001838 else if (key == "menu") return KEY_MENU;
1839 else if (key == "back") return KEY_BACK;
1840 else if (key == "search") return KEY_SEARCH;
1841 else if (key == "voldown") return KEY_VOLUMEDOWN;
1842 else if (key == "volup") return KEY_VOLUMEUP;
1843 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001844 int ret_val;
1845 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1846 if (!ret_val)
1847 return KEY_POWER;
1848 else
1849 return ret_val;
1850 }
1851
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001852 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001853}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001854
1855int GUIAction::checkpartitionlifetimewrites(std::string arg)
1856{
1857 int op_status = 0;
1858 TWPartition* sys = PartitionManager.Find_Partition_By_Path(arg);
1859
1860 operation_start("Check Partition Lifetime Writes");
1861 if (sys) {
1862 if (sys->Check_Lifetime_Writes() != 0)
1863 DataManager::SetValue("tw_lifetime_writes", 1);
1864 else
1865 DataManager::SetValue("tw_lifetime_writes", 0);
1866 op_status = 0; // success
1867 } else {
1868 DataManager::SetValue("tw_lifetime_writes", 1);
1869 op_status = 1; // fail
1870 }
1871
1872 operation_end(op_status);
1873 return 0;
1874}
1875
1876int GUIAction::mountsystemtoggle(std::string arg)
1877{
1878 int op_status = 0;
Captain Throwback9d6feb52018-07-27 10:05:24 -04001879 bool remount_system = PartitionManager.Is_Mounted_By_Path(PartitionManager.Get_Android_Root_Path());
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001880 bool remount_vendor = PartitionManager.Is_Mounted_By_Path("/vendor");
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001881
1882 operation_start("Toggle System Mount");
Captain Throwback9d6feb52018-07-27 10:05:24 -04001883 if (!PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001884 op_status = 1; // fail
1885 } else {
Captain Throwback9d6feb52018-07-27 10:05:24 -04001886 TWPartition* Part = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001887 if (Part) {
Ethan Yonkerd6966f42015-05-30 14:52:16 -05001888 if (arg == "0") {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001889 DataManager::SetValue("tw_mount_system_ro", 0);
1890 Part->Change_Mount_Read_Only(false);
1891 } else {
1892 DataManager::SetValue("tw_mount_system_ro", 1);
1893 Part->Change_Mount_Read_Only(true);
1894 }
1895 if (remount_system) {
1896 Part->Mount(true);
1897 }
1898 op_status = 0; // success
1899 } else {
1900 op_status = 1; // fail
1901 }
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001902 Part = PartitionManager.Find_Partition_By_Path("/vendor");
1903 if (Part) {
1904 if (arg == "0") {
1905 Part->Change_Mount_Read_Only(false);
1906 } else {
1907 Part->Change_Mount_Read_Only(true);
1908 }
1909 if (remount_vendor) {
1910 Part->Mount(true);
1911 }
1912 op_status = 0; // success
1913 } else {
1914 op_status = 1; // fail
1915 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001916 }
1917
1918 operation_end(op_status);
1919 return 0;
1920}
Ethan Yonker74db1572015-10-28 12:44:49 -05001921
1922int GUIAction::setlanguage(std::string arg __unused)
1923{
1924 int op_status = 0;
1925
1926 operation_start("Set Language");
1927 PageManager::LoadLanguage(DataManager::GetStrValue("tw_language"));
1928 PageManager::RequestReload();
1929 op_status = 0; // success
1930
1931 operation_end(op_status);
1932 return 0;
1933}
Ethan Yonker1b190162016-12-05 15:25:19 -06001934
Matt Mower9472ba12016-01-20 18:12:47 -06001935int GUIAction::togglebacklight(std::string arg __unused)
1936{
1937 blankTimer.toggleBlank();
1938 return 0;
1939}
1940
Ethan Yonker1b190162016-12-05 15:25:19 -06001941int GUIAction::setbootslot(std::string arg)
1942{
1943 operation_start("Set Boot Slot");
Captain Throwbackb60a2732020-10-13 17:55:43 -04001944 if (!simulate) {
LinkBoi00f5547382021-11-16 08:56:12 +02001945 if (PartitionManager.Find_Partition_By_Path("/vendor")) {
1946 if (!PartitionManager.UnMount_By_Path("/vendor", false)) {
1947 // PartitionManager failed to unmount /vendor, this should not happen,
1948 // but in case it does, do a lazy unmount
1949 LOGINFO("WARNING: vendor partition could not be unmounted normally!\n");
1950 umount2("/vendor", MNT_DETACH);
1951 }
Captain Throwbackb60a2732020-10-13 17:55:43 -04001952 }
LinkBoi00f5547382021-11-16 08:56:12 +02001953 PartitionManager.Set_Active_Slot(arg);
Captain Throwbackb60a2732020-10-13 17:55:43 -04001954 } else {
Ethan Yonker1b190162016-12-05 15:25:19 -06001955 simulate_progress_bar();
Captain Throwbackb60a2732020-10-13 17:55:43 -04001956 }
Ethan Yonker1b190162016-12-05 15:25:19 -06001957 operation_end(0);
1958 return 0;
1959}
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001960
1961int GUIAction::checkforapp(std::string arg __unused)
1962{
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001963 operation_start("Check for TWRP App");
1964 if (!simulate)
1965 {
epicX271bb3a2020-12-30 01:03:18 +05301966 TWFunc::checkforapp();
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001967 } else
1968 simulate_progress_bar();
epicX271bb3a2020-12-30 01:03:18 +05301969
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001970 operation_end(0);
1971 return 0;
1972}
1973
1974int GUIAction::installapp(std::string arg __unused)
1975{
1976 int op_status = 1;
1977 operation_start("Install TWRP App");
1978 if (!simulate)
1979 {
1980 if (DataManager::GetIntValue("tw_mount_system_ro") > 0 || DataManager::GetIntValue("tw_app_install_system") == 0) {
1981 if (PartitionManager.Mount_By_Path("/data", true)) {
1982 string install_path = "/data/app";
1983 string context = "u:object_r:apk_data_file:s0";
1984 if (!TWFunc::Path_Exists(install_path)) {
1985 if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
1986 LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno));
1987 goto exit;
1988 }
1989 if (chown(install_path.c_str(), 1000, 1000)) {
1990 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
1991 goto exit;
1992 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06001993 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001994 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
1995 goto exit;
1996 }
1997 }
1998 install_path += "/me.twrp.twrpapp-1";
1999 if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
2000 LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno));
2001 goto exit;
2002 }
2003 if (chown(install_path.c_str(), 1000, 1000)) {
2004 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
2005 goto exit;
2006 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002007 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002008 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2009 goto exit;
2010 }
2011 install_path += "/base.apk";
bigbiffad58e1b2020-07-06 20:24:34 -04002012 if (TWFunc::copy_file("/system/bin/me.twrp.twrpapp.apk", install_path, 0644)) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002013 LOGERR("Error copying apk file\n");
2014 goto exit;
2015 }
2016 if (chown(install_path.c_str(), 1000, 1000)) {
2017 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
2018 goto exit;
2019 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002020 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002021 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2022 goto exit;
2023 }
2024 sync();
2025 sync();
2026 }
2027 } else {
Captain Throwback9d6feb52018-07-27 10:05:24 -04002028 if (PartitionManager.Mount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) {
2029 string base_path = PartitionManager.Get_Android_Root_Path();
2030 if (TWFunc::Path_Exists(PartitionManager.Get_Android_Root_Path() + "/system"))
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002031 base_path += "/system"; // For devices with system as a root file system (e.g. Pixel)
2032 string install_path = base_path + "/priv-app";
2033 string context = "u:object_r:system_file:s0";
2034 if (!TWFunc::Path_Exists(install_path))
2035 install_path = base_path + "/app";
2036 if (TWFunc::Path_Exists(install_path)) {
2037 install_path += "/twrpapp";
2038 LOGINFO("Installing app to '%s'\n", install_path.c_str());
2039 if (mkdir(install_path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0) {
Ethan Yonker4767caf2017-01-11 10:45:04 -06002040 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002041 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2042 goto exit;
2043 }
2044 install_path += "/me.twrp.twrpapp.apk";
bigbiffad58e1b2020-07-06 20:24:34 -04002045 if (TWFunc::copy_file("/system/bin/me.twrp.twrpapp.apk", install_path, 0644)) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002046 LOGERR("Error copying apk file\n");
2047 goto exit;
2048 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002049 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002050 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2051 goto exit;
2052 }
Stefan Tauner6ad14572020-01-11 22:28:53 +01002053
2054 // System apps require their permissions to be pre-set via an XML file in /etc/permissions
2055 string permission_path = base_path + "/etc/permissions/privapp-permissions-twrpapp.xml";
bigbiffad58e1b2020-07-06 20:24:34 -04002056 if (TWFunc::copy_file("/system/bin/privapp-permissions-twrpapp.xml", permission_path, 0644)) {
Stefan Tauner6ad14572020-01-11 22:28:53 +01002057 LOGERR("Error copying permission file\n");
2058 goto exit;
2059 }
2060 if (chown(permission_path.c_str(), 1000, 1000)) {
2061 LOGERR("chown %s error: %s\n", permission_path.c_str(), strerror(errno));
2062 goto exit;
2063 }
2064 if (setfilecon(permission_path.c_str(), (security_context_t)context.c_str()) < 0) {
2065 LOGERR("setfilecon %s error: %s\n", permission_path.c_str(), strerror(errno));
2066 goto exit;
2067 }
2068
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002069 sync();
2070 sync();
Captain Throwback9d6feb52018-07-27 10:05:24 -04002071 PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002072 op_status = 0;
2073 } else {
2074 LOGERR("Error making app directory '%s': %s\n", strerror(errno));
2075 }
2076 }
2077 }
2078 }
2079 } else
2080 simulate_progress_bar();
2081exit:
epicX271bb3a2020-12-30 01:03:18 +05302082 TWFunc::checkforapp();
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002083 operation_end(0);
2084 return 0;
2085}
Ethan Yonker53796e72019-01-11 22:49:52 -06002086
Ethan Yonker76bbd3a2019-05-10 10:50:04 -05002087int GUIAction::uninstalltwrpsystemapp(std::string arg __unused)
2088{
2089 int op_status = 1;
2090 operation_start("Uninstall TWRP System App");
2091 if (!simulate)
2092 {
2093 int Mount_System_RO = DataManager::GetIntValue("tw_mount_system_ro");
2094 TWPartition* Part = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path());
2095 if (!Part) {
2096 LOGERR("Unabled to find system partition.\n");
2097 goto exit;
2098 }
2099 if (!Part->UnMount(true)) {
2100 goto exit;
2101 }
2102 if (Mount_System_RO > 0) {
2103 DataManager::SetValue("tw_mount_system_ro", 0);
2104 Part->Change_Mount_Read_Only(false);
2105 }
2106 if (Part->Mount(true)) {
2107 string base_path = PartitionManager.Get_Android_Root_Path();
2108 if (TWFunc::Path_Exists(PartitionManager.Get_Android_Root_Path() + "/system"))
2109 base_path += "/system"; // For devices with system as a root file system (e.g. Pixel)
2110 string uninstall_path = base_path + "/priv-app";
2111 if (!TWFunc::Path_Exists(uninstall_path))
2112 uninstall_path = base_path + "/app";
2113 uninstall_path += "/twrpapp";
2114 if (TWFunc::Path_Exists(uninstall_path)) {
2115 LOGINFO("Uninstalling TWRP App from '%s'\n", uninstall_path.c_str());
2116 if (TWFunc::removeDir(uninstall_path, false) == 0) {
2117 sync();
2118 op_status = 0;
2119 DataManager::SetValue("tw_app_installed_in_system", 0);
2120 DataManager::SetValue("tw_app_install_status", 0);
2121 } else {
2122 LOGERR("Unable to remove TWRP app from system.\n");
2123 }
2124 } else {
2125 LOGINFO("didn't find TWRP app in '%s'\n", uninstall_path.c_str());
2126 }
2127 }
2128 Part->UnMount(true);
2129 if (Mount_System_RO > 0) {
2130 DataManager::SetValue("tw_mount_system_ro", Mount_System_RO);
2131 Part->Change_Mount_Read_Only(true);
2132 }
2133 } else
2134 simulate_progress_bar();
2135exit:
epicX271bb3a2020-12-30 01:03:18 +05302136 TWFunc::checkforapp();
Ethan Yonker76bbd3a2019-05-10 10:50:04 -05002137 operation_end(0);
2138 return 0;
2139}
2140
Ethan Yonker53796e72019-01-11 22:49:52 -06002141int GUIAction::repackimage(std::string arg __unused)
2142{
2143 int op_status = 1;
bigbiffad58e1b2020-07-06 20:24:34 -04002144 twrpRepacker repacker;
2145
Ethan Yonker53796e72019-01-11 22:49:52 -06002146 operation_start("Repack Image");
2147 if (!simulate)
2148 {
2149 std::string path = DataManager::GetStrValue("tw_filename");
2150 Repack_Options_struct Repack_Options;
2151 Repack_Options.Disable_Verity = false;
2152 Repack_Options.Disable_Force_Encrypt = false;
2153 Repack_Options.Backup_First = DataManager::GetIntValue("tw_repack_backup_first") != 0;
2154 if (DataManager::GetIntValue("tw_repack_kernel") == 1)
2155 Repack_Options.Type = REPLACE_KERNEL;
2156 else
2157 Repack_Options.Type = REPLACE_RAMDISK;
bigbiffad58e1b2020-07-06 20:24:34 -04002158 if (!repacker.Repack_Image_And_Flash(path, Repack_Options))
Ethan Yonker53796e72019-01-11 22:49:52 -06002159 goto exit;
2160 } else
2161 simulate_progress_bar();
2162 op_status = 0;
2163exit:
2164 operation_end(op_status);
2165 return 0;
2166}
2167
nebrassyac29e692021-05-20 13:03:30 +02002168int GUIAction::reflashtwrp(std::string arg __unused)
2169{
2170 int op_status = 1;
2171 twrpRepacker repacker;
2172
2173 operation_start("Repack Image");
2174 if (!simulate)
2175 {
2176 if (!repacker.Flash_Current_Twrp())
2177 goto exit;
2178 } else
2179 simulate_progress_bar();
2180 op_status = 0;
2181exit:
2182 operation_end(op_status);
2183 return 0;
2184}
Ethan Yonker53796e72019-01-11 22:49:52 -06002185int GUIAction::fixabrecoverybootloop(std::string arg __unused)
2186{
2187 int op_status = 1;
bigbiffad58e1b2020-07-06 20:24:34 -04002188 twrpRepacker repacker;
2189
Ethan Yonker53796e72019-01-11 22:49:52 -06002190 operation_start("Repack Image");
2191 if (!simulate)
2192 {
bigbiffad58e1b2020-07-06 20:24:34 -04002193 if (!TWFunc::Path_Exists("/system/bin/magiskboot")) {
Ethan Yonker53796e72019-01-11 22:49:52 -06002194 LOGERR("Image repacking tool not present in this TWRP build!");
2195 goto exit;
2196 }
2197 DataManager::SetProgress(0);
2198 TWPartition* part = PartitionManager.Find_Partition_By_Path("/boot");
2199 if (part)
2200 gui_msg(Msg("unpacking_image=Unpacking {1}...")(part->Display_Name));
2201 else {
2202 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")("/boot"));
2203 goto exit;
2204 }
bigbiffad58e1b2020-07-06 20:24:34 -04002205 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 -06002206 goto exit;
2207 DataManager::SetProgress(.25);
2208 gui_msg("fixing_recovery_loop_patch=Patching kernel...");
bigbiffad58e1b2020-07-06 20:24:34 -04002209 std::string command = "cd " REPACK_ORIG_DIR " && /system/bin/magiskboot hexpatch kernel 77616E745F696E697472616D667300 736B69705F696E697472616D667300";
Ethan Yonker53796e72019-01-11 22:49:52 -06002210 if (TWFunc::Exec_Cmd(command) != 0) {
2211 gui_msg(Msg(msg::kError, "fix_recovery_loop_patch_error=Error patching kernel."));
2212 goto exit;
2213 }
2214 std::string header_path = REPACK_ORIG_DIR;
2215 header_path += "header";
2216 if (TWFunc::Path_Exists(header_path)) {
2217 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";
2218 if (TWFunc::Exec_Cmd(command) != 0) {
2219 gui_msg(Msg(msg::kError, "fix_recovery_loop_patch_error=Error patching kernel."));
2220 goto exit;
2221 }
2222 }
2223 DataManager::SetProgress(.5);
2224 gui_msg(Msg("repacking_image=Repacking {1}...")(part->Display_Name));
bigbiffad58e1b2020-07-06 20:24:34 -04002225 command = "cd " REPACK_ORIG_DIR " && /system/bin/magiskboot repack " REPACK_ORIG_DIR "boot.img";
Ethan Yonker53796e72019-01-11 22:49:52 -06002226 if (TWFunc::Exec_Cmd(command) != 0) {
2227 gui_msg(Msg(msg::kError, "repack_error=Error repacking image."));
2228 goto exit;
2229 }
2230 DataManager::SetProgress(.75);
2231 std::string path = REPACK_ORIG_DIR;
2232 std::string file = "new-boot.img";
2233 DataManager::SetValue("tw_flash_partition", "/boot;");
2234 if (!PartitionManager.Flash_Image(path, file)) {
2235 LOGINFO("Error flashing new image\n");
2236 goto exit;
2237 }
2238 DataManager::SetProgress(1);
2239 TWFunc::removeDir(REPACK_ORIG_DIR, false);
2240 } else
2241 simulate_progress_bar();
2242 op_status = 0;
2243exit:
2244 operation_end(op_status);
2245 return 0;
2246}
bigbiffdf8436b2020-08-30 16:22:34 -04002247
2248
2249int GUIAction::enableadb(std::string arg __unused) {
2250 android::base::SetProperty("sys.usb.config", "none");
2251 android::base::SetProperty("sys.usb.config", "adb");
2252 return 0;
2253}
2254
2255int GUIAction::enablefastboot(std::string arg __unused) {
2256 android::base::SetProperty("sys.usb.config", "none");
2257 android::base::SetProperty("sys.usb.config", "fastboot");
2258 return 0;
2259}
Mohd Faraz77bbeb02020-08-12 12:29:42 +00002260
2261int GUIAction::changeterminal(std::string arg) {
2262 bool res = true;
2263 std::string resp, cmd = "cd " + arg;
2264 DataManager::GetValue("tw_terminal_location", resp);
2265 if (arg.empty() && !resp.empty()) {
2266 cmd = "cd /";
2267 for (uint8_t iter = 0; iter < cmd.size(); iter++)
2268 term->NotifyCharInput(cmd.at(iter));
2269 term->NotifyCharInput(13);
2270 DataManager::SetValue("tw_terminal_location", "");
2271 return 0;
2272 }
2273 if (term != NULL && !arg.empty()) {
2274 DataManager::SetValue("tw_terminal_location", arg);
2275 if (term->status()) {
2276 for (uint8_t iter = 0; iter < cmd.size(); iter++)
2277 term->NotifyCharInput(cmd.at(iter));
2278 term->NotifyCharInput(13);
2279 }
2280 else if (chdir(arg.c_str()) != 0) {
2281 LOGINFO("Unable to change dir to %s\n", arg.c_str());
2282 res = false;
2283 }
2284 }
2285 else {
2286 res = false;
2287 LOGINFO("Unable to switch to Terminal\n");
2288 }
2289 if (res)
2290 gui_changePage("terminalcommand");
2291 return 0;
2292}
bigbiffcfa875c2021-06-20 16:20:27 -04002293
2294int GUIAction::unmapsuperdevices(std::string arg __unused) {
2295 int op_status = 1;
2296
2297 operation_start("Remove Super Devices");
2298 if (simulate) {
2299 simulate_progress_bar();
2300 } else {
2301 if (PartitionManager.Unmap_Super_Devices()) {
2302 op_status = 0;
2303 }
2304 }
2305
2306 operation_end(op_status);
2307 return 0;
2308}
2309
Captain Throwback16dd81b2021-02-12 19:32:36 -05002310#ifndef TW_EXCLUDE_NANO
2311int GUIAction::editfile(std::string arg) {
2312 if (term != NULL) {
2313 for (uint8_t iter = 0; iter < arg.size(); iter++)
2314 term->NotifyCharInput(arg.at(iter));
2315 term->NotifyCharInput(13);
2316 }
2317 else
2318 LOGINFO("Unable to switch to Terminal\n");
2319 return 0;
2320}
2321#endif
epicXa721f952021-01-04 13:01:31 +05302322
2323int GUIAction::applycustomtwrpfolder(string arg __unused)
2324{
2325 operation_start("ChangingTWRPFolder");
2326 string storageFolder = DataManager::GetSettingsStoragePath();
2327 string newFolder = storageFolder + '/' + arg;
2328 string newBackupFolder = newFolder + "/BACKUPS/" + DataManager::GetStrValue("device_id");
2329 string prevFolder = storageFolder + DataManager::GetStrValue(TW_RECOVERY_FOLDER_VAR);
2330 bool ret = false;
2331
2332 if (TWFunc::Path_Exists(newFolder)) {
2333 gui_msg(Msg(msg::kError, "tw_folder_exists=A folder with that name already exists!"));
2334 } else {
2335 ret = true;
2336 }
2337
2338 if (newFolder != prevFolder && ret) {
2339 ret = TWFunc::Exec_Cmd("mv -f \"" + prevFolder + "\" \"" + newFolder + '\"') != 0 ? false : true;
2340 } else {
2341 gui_msg(Msg(msg::kError, "tw_folder_exists=A folder with that name already exists!"));
2342 }
2343
epicX11e90832021-03-01 00:02:57 +05302344 if (ret) ret = TWFunc::Recursive_Mkdir(newBackupFolder) ? true : false;
epicXa721f952021-01-04 13:01:31 +05302345
2346
2347 if (ret) {
2348 DataManager::SetValue(TW_RECOVERY_FOLDER_VAR, '/' + arg);
2349 DataManager::SetValue(TW_BACKUPS_FOLDER_VAR, newBackupFolder);
2350 DataManager::mBackingFile = newFolder + '/' + TW_SETTINGS_FILE;
2351 }
2352 operation_end((int)!ret);
2353 return 0;
2354}
bigbiffd21252f2021-09-18 15:56:32 -04002355
2356int GUIAction::mergesnapshots(string arg __unused) {
2357 int op_status = 1;
2358 if (PartitionManager.Check_Pending_Merges()) {
2359 op_status = 0;
2360 }
2361 operation_end(op_status);
2362 return 0;
Captain Throwbackd0fcdaf2022-05-26 14:14:12 -04002363}