blob: e785287639eed3dc3bd1a8d4a7abdc61a378693f [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"
Ethan Yonkerfbb43532015-12-28 21:54:50 +010058#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);
thatc6085482015-01-09 22:12:43 +0100210
211 // remember actions that run in the caller thread
212 for (mapFunc::const_iterator it = mf.begin(); it != mf.end(); ++it)
213 setActionsRunningInCallerThread.insert(it->first);
214
215 // These actions will run in a separate thread
Vojtech Bocekecd89182015-01-15 16:28:54 +0100216 ADD_ACTION(flash);
217 ADD_ACTION(wipe);
218 ADD_ACTION(refreshsizes);
219 ADD_ACTION(nandroid);
Ethan Yonkerb5fab762016-01-28 14:03:33 -0600220 ADD_ACTION(fixcontexts);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100221 ADD_ACTION(fixpermissions);
222 ADD_ACTION(dd);
223 ADD_ACTION(partitionsd);
224 ADD_ACTION(installhtcdumlock);
225 ADD_ACTION(htcdumlockrestoreboot);
226 ADD_ACTION(htcdumlockreflashrecovery);
227 ADD_ACTION(cmd);
228 ADD_ACTION(terminalcommand);
229 ADD_ACTION(reinjecttwrp);
230 ADD_ACTION(decrypt);
231 ADD_ACTION(adbsideload);
232 ADD_ACTION(openrecoveryscript);
233 ADD_ACTION(installsu);
234 ADD_ACTION(decrypt_backup);
235 ADD_ACTION(repair);
Ethan Yonkera2719152015-05-28 09:44:41 -0500236 ADD_ACTION(resize);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100237 ADD_ACTION(changefilesystem);
238 ADD_ACTION(flashimage);
that10ae24f2015-12-26 20:53:51 +0100239 ADD_ACTION(twcmd);
Ethan Yonker1b190162016-12-05 15:25:19 -0600240 ADD_ACTION(setbootslot);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -0600241 ADD_ACTION(installapp);
Ethan Yonker76bbd3a2019-05-10 10:50:04 -0500242 ADD_ACTION(uninstalltwrpsystemapp);
Ethan Yonker53796e72019-01-11 22:49:52 -0600243 ADD_ACTION(repackimage);
244 ADD_ACTION(fixabrecoverybootloop);
epicXa721f952021-01-04 13:01:31 +0530245 ADD_ACTION(applycustomtwrpfolder);
Captain Throwback16dd81b2021-02-12 19:32:36 -0500246#ifndef TW_EXCLUDE_NANO
247 ADD_ACTION(editfile);
248#endif
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 {
Dees_Troy657c3092012-09-10 20:32:10 -0400406 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
bigbiff1f9e4842020-10-31 11:33:15 -0400407 PartitionManager.Unlock_Block_Partitions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400408
409 // Now, check if we need to ensure TWRP remains installed...
410 struct stat st;
bigbiffad58e1b2020-07-06 20:24:34 -0400411 if (stat("/system/bin/installTwrp", &st) == 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400412 {
413 DataManager::SetValue("tw_operation", "Configuring TWRP");
414 DataManager::SetValue("tw_partition", "");
Ethan Yonker74db1572015-10-28 12:44:49 -0500415 gui_msg("config_twrp=Configuring TWRP...");
bigbiffad58e1b2020-07-06 20:24:34 -0400416 if (TWFunc::Exec_Cmd("/system/bin/installTwrp reinstall") < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400417 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500418 gui_msg("config_twrp_err=Unable to configure TWRP with this kernel.");
Dees_Troy51a0e822012-09-05 15:24:24 -0400419 }
420 }
421 }
422
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200423 // Done
424 DataManager::SetValue("ui_progress", 100);
425 DataManager::SetValue("ui_progress", 0);
Chaosmasterd5364a02020-02-03 15:38:02 +0100426 DataManager::SetValue("ui_portion_size", 0);
427 DataManager::SetValue("ui_portion_start", 0);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200428 return ret_val;
Dees_Troy51a0e822012-09-05 15:24:24 -0400429}
430
that73a52952015-01-28 23:07:34 +0100431GUIAction::ThreadType GUIAction::getThreadType(const GUIAction::Action& action)
thatc6085482015-01-09 22:12:43 +0100432{
that73a52952015-01-28 23:07:34 +0100433 string func = gui_parse_text(action.mFunction);
434 bool needsThread = setActionsRunningInCallerThread.find(func) == setActionsRunningInCallerThread.end();
435 if (needsThread) {
436 if (func == "cancelbackup")
437 return THREAD_CANCEL;
438 else
439 return THREAD_ACTION;
440 }
441 return THREAD_NONE;
thatc6085482015-01-09 22:12:43 +0100442}
443
Dees_Troy51a0e822012-09-05 15:24:24 -0400444int GUIAction::doActions()
445{
that3f7b1ac2014-12-30 11:30:13 +0100446 if (mActions.size() < 1)
447 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400448
that73a52952015-01-28 23:07:34 +0100449 // Determine in which thread to run the actions.
450 // Do it for all actions at once before starting, so that we can cancel the whole batch if the thread is already busy.
451 ThreadType threadType = THREAD_NONE;
that3f7b1ac2014-12-30 11:30:13 +0100452 std::vector<Action>::iterator it;
that73a52952015-01-28 23:07:34 +0100453 for (it = mActions.begin(); it != mActions.end(); ++it) {
454 ThreadType tt = getThreadType(*it);
455 if (tt == THREAD_NONE)
456 continue;
457 if (threadType == THREAD_NONE)
458 threadType = tt;
459 else if (threadType != tt) {
460 LOGERR("Can't mix normal and cancel actions in the same list.\n"
461 "Running the whole batch in the cancel thread.\n");
462 threadType = THREAD_CANCEL;
thatc6085482015-01-09 22:12:43 +0100463 break;
464 }
that7d3b54f2015-01-09 22:52:51 +0100465 }
that73a52952015-01-28 23:07:34 +0100466
467 // Now run the actions in the desired thread.
468 switch (threadType) {
469 case THREAD_ACTION:
470 action_thread.threadActions(this);
471 break;
472
473 case THREAD_CANCEL:
474 cancel_thread.threadActions(this);
475 break;
476
477 default: {
478 // no iterators here because theme reloading might kill our object
479 const size_t cnt = mActions.size();
480 for (size_t i = 0; i < cnt; ++i)
481 doAction(mActions[i]);
482 }
thatc6085482015-01-09 22:12:43 +0100483 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400484
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200485 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400486}
487
that3f7b1ac2014-12-30 11:30:13 +0100488int GUIAction::doAction(Action action)
Dees_Troy51a0e822012-09-05 15:24:24 -0400489{
that3f7b1ac2014-12-30 11:30:13 +0100490 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400491
that3f7b1ac2014-12-30 11:30:13 +0100492 std::string function = gui_parse_text(action.mFunction);
493 std::string arg = gui_parse_text(action.mArg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400494
that3f7b1ac2014-12-30 11:30:13 +0100495 // find function and execute it
496 mapFunc::const_iterator funcitr = mf.find(function);
497 if (funcitr != mf.end())
498 return (this->*funcitr->second)(arg);
499
500 LOGERR("Unknown action '%s'\n", function.c_str());
501 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400502}
503
504void GUIAction::operation_start(const string operation_name)
505{
that3f7b1ac2014-12-30 11:30:13 +0100506 LOGINFO("operation_start: '%s'\n", operation_name.c_str());
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000507 time(&Start);
Dees_Troy51a0e822012-09-05 15:24:24 -0400508 DataManager::SetValue(TW_ACTION_BUSY, 1);
509 DataManager::SetValue("ui_progress", 0);
Chaosmasterd5364a02020-02-03 15:38:02 +0100510 DataManager::SetValue("ui_portion_size", 0);
511 DataManager::SetValue("ui_portion_start", 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400512 DataManager::SetValue("tw_operation", operation_name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400513 DataManager::SetValue("tw_operation_state", 0);
Ethan Yonkerd83c9ea2015-01-02 15:22:31 -0600514 DataManager::SetValue("tw_operation_status", 0);
bigbiff25d25b92020-06-19 16:07:38 -0400515 bool tw_ab_device = TWFunc::get_log_dir() != CACHE_LOGS_DIR;
bigbiff bigbiff19874f12019-01-08 20:06:57 -0500516 DataManager::SetValue("tw_ab_device", tw_ab_device);
Dees_Troy51a0e822012-09-05 15:24:24 -0400517}
518
that3f7b1ac2014-12-30 11:30:13 +0100519void GUIAction::operation_end(const int operation_status)
Dees_Troy51a0e822012-09-05 15:24:24 -0400520{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000521 time_t Stop;
Dees_Troy51a0e822012-09-05 15:24:24 -0400522 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400523 DataManager::SetValue("ui_progress", 100);
524 if (simulate) {
525 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
526 if (simulate_fail != 0)
527 DataManager::SetValue("tw_operation_status", 1);
528 else
529 DataManager::SetValue("tw_operation_status", 0);
530 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500531 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400532 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500533 }
534 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400535 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500536 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400537 }
538 DataManager::SetValue("tw_operation_state", 1);
539 DataManager::SetValue(TW_ACTION_BUSY, 0);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500540 blankTimer.resetTimerAndUnblank();
LuK133762326f42015-09-30 19:57:46 +0200541 property_set("twrp.action_complete", "1");
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000542 time(&Stop);
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400543
544#ifndef TW_NO_HAPTICS
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000545 if ((int) difftime(Stop, Start) > 10)
Ethan Yonker03db3262014-02-05 08:02:06 -0600546 DataManager::Vibrate("tw_action_vibrate");
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400547#endif
548
that3f7b1ac2014-12-30 11:30:13 +0100549 LOGINFO("operation_end - status=%d\n", operation_status);
Dees_Troy51a0e822012-09-05 15:24:24 -0400550}
551
that3f7b1ac2014-12-30 11:30:13 +0100552int GUIAction::reboot(std::string arg)
Dees_Troy51a0e822012-09-05 15:24:24 -0400553{
that3f7b1ac2014-12-30 11:30:13 +0100554 sync();
555 DataManager::SetValue("tw_gui_done", 1);
556 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400557
that3f7b1ac2014-12-30 11:30:13 +0100558 return 0;
559}
Dees_Troy51a0e822012-09-05 15:24:24 -0400560
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500561int GUIAction::home(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100562{
that3f7b1ac2014-12-30 11:30:13 +0100563 gui_changePage("main");
564 return 0;
565}
Dees_Troy51a0e822012-09-05 15:24:24 -0400566
that3f7b1ac2014-12-30 11:30:13 +0100567int GUIAction::key(std::string arg)
568{
569 const int key = getKeyByName(arg);
570 PageManager::NotifyKey(key, true);
571 PageManager::NotifyKey(key, false);
572 return 0;
573}
574
575int GUIAction::page(std::string arg)
576{
LuK133762326f42015-09-30 19:57:46 +0200577 property_set("twrp.action_complete", "0");
that3f7b1ac2014-12-30 11:30:13 +0100578 std::string page_name = gui_parse_text(arg);
579 return gui_changePage(page_name);
580}
581
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500582int GUIAction::reload(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100583{
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500584 PageManager::RequestReload();
585 // The actual reload is handled in pages.cpp in PageManager::RunReload()
586 // The reload will occur on the next Update or Render call and will
587 // be performed in the rendoer thread instead of the action thread
588 // to prevent crashing which could occur when we start deleting
589 // GUI resources in the action thread while we attempt to render
590 // with those same resources in another thread.
that3f7b1ac2014-12-30 11:30:13 +0100591 return 0;
592}
Dees_Troy51a0e822012-09-05 15:24:24 -0400593
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500594int GUIAction::readBackup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100595{
596 string Restore_Name;
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -0400597
that3f7b1ac2014-12-30 11:30:13 +0100598 DataManager::GetValue("tw_restore", Restore_Name);
599 PartitionManager.Set_Restore_Files(Restore_Name);
600 return 0;
601}
602
603int GUIAction::set(std::string arg)
604{
605 if (arg.find('=') != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200606 {
that3f7b1ac2014-12-30 11:30:13 +0100607 string varName = arg.substr(0, arg.find('='));
608 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400609
that3f7b1ac2014-12-30 11:30:13 +0100610 DataManager::GetValue(value, value);
611 DataManager::SetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200612 }
that3f7b1ac2014-12-30 11:30:13 +0100613 else
614 DataManager::SetValue(arg, "1");
615 return 0;
616}
Dees_Troy51a0e822012-09-05 15:24:24 -0400617
that3f7b1ac2014-12-30 11:30:13 +0100618int GUIAction::clear(std::string arg)
619{
620 DataManager::SetValue(arg, "0");
621 return 0;
622}
Dees_Troy51a0e822012-09-05 15:24:24 -0400623
that3f7b1ac2014-12-30 11:30:13 +0100624int GUIAction::mount(std::string arg)
625{
626 if (arg == "usb") {
627 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400628 if (!simulate)
that3f7b1ac2014-12-30 11:30:13 +0100629 PartitionManager.usb_storage_enable();
630 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500631 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100632 } else if (!simulate) {
633 PartitionManager.Mount_By_Path(arg, true);
634 PartitionManager.Add_MTP_Storage(arg);
635 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500636 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100637 return 0;
638}
639
640int GUIAction::unmount(std::string arg)
641{
642 if (arg == "usb") {
643 if (!simulate)
644 PartitionManager.usb_storage_disable();
645 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500646 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100647 DataManager::SetValue(TW_ACTION_BUSY, 0);
648 } else if (!simulate) {
649 PartitionManager.UnMount_By_Path(arg, true);
650 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500651 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100652 return 0;
653}
654
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500655int GUIAction::restoredefaultsettings(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100656{
657 operation_start("Restore Defaults");
658 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
Ethan Yonker74db1572015-10-28 12:44:49 -0500659 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100660 else {
661 DataManager::ResetDefaults();
662 PartitionManager.Update_System_Details();
663 PartitionManager.Mount_Current_Storage(true);
664 }
665 operation_end(0);
666 return 0;
667}
668
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500669int GUIAction::copylog(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100670{
671 operation_start("Copy Log");
672 if (!simulate)
673 {
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400674 string dst, curr_storage;
675 int copy_kernel_log = 0;
676
677 DataManager::GetValue("tw_include_kernel_log", copy_kernel_log);
that3f7b1ac2014-12-30 11:30:13 +0100678 PartitionManager.Mount_Current_Storage(true);
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400679 curr_storage = DataManager::GetCurrentStoragePath();
680 dst = curr_storage + "/recovery.log";
that3f7b1ac2014-12-30 11:30:13 +0100681 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
682 tw_set_default_metadata(dst.c_str());
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400683 if (copy_kernel_log)
684 TWFunc::copy_kernel_log(curr_storage);
that3f7b1ac2014-12-30 11:30:13 +0100685 sync();
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400686 gui_msg(Msg("copy_log=Copied recovery log to {1}")(dst));
that3f7b1ac2014-12-30 11:30:13 +0100687 } else
688 simulate_progress_bar();
689 operation_end(0);
690 return 0;
691}
692
693
694int GUIAction::compute(std::string arg)
695{
696 if (arg.find("+") != string::npos)
697 {
698 string varName = arg.substr(0, arg.find('+'));
699 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
700 int amount_to_add = atoi(string_to_add.c_str());
701 int value;
702
703 DataManager::GetValue(varName, value);
704 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400705 return 0;
706 }
that3f7b1ac2014-12-30 11:30:13 +0100707 if (arg.find("-") != string::npos)
Dees_Troy51a0e822012-09-05 15:24:24 -0400708 {
that3f7b1ac2014-12-30 11:30:13 +0100709 string varName = arg.substr(0, arg.find('-'));
710 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
711 int amount_to_subtract = atoi(string_to_subtract.c_str());
712 int value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400713
that3f7b1ac2014-12-30 11:30:13 +0100714 DataManager::GetValue(varName, value);
715 value -= amount_to_subtract;
716 if (value <= 0)
717 value = 0;
718 DataManager::SetValue(varName, value);
719 return 0;
720 }
721 if (arg.find("*") != string::npos)
722 {
723 string varName = arg.substr(0, arg.find('*'));
724 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
725 int multiply_by = atoi(multiply_by_str.c_str());
726 int value;
727
728 DataManager::GetValue(varName, value);
729 DataManager::SetValue(varName, value*multiply_by);
730 return 0;
731 }
732 if (arg.find("/") != string::npos)
733 {
734 string varName = arg.substr(0, arg.find('/'));
735 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
736 int divide_by = atoi(divide_by_str.c_str());
737 int value;
738
Matt Mowera8a89d12016-12-30 18:10:37 -0600739 if (divide_by != 0)
that3f7b1ac2014-12-30 11:30:13 +0100740 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400741 DataManager::GetValue(varName, value);
that3f7b1ac2014-12-30 11:30:13 +0100742 DataManager::SetValue(varName, value/divide_by);
Dees_Troy51a0e822012-09-05 15:24:24 -0400743 }
744 return 0;
745 }
that3f7b1ac2014-12-30 11:30:13 +0100746 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
747 return -1;
748}
Dees_Troy51a0e822012-09-05 15:24:24 -0400749
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500750int GUIAction::setguitimezone(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100751{
752 string SelectedZone;
753 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
754 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
755 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
756
757 int dst;
758 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
759
760 string offset;
761 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
762
763 string NewTimeZone = Zone;
764 if (offset != "0")
765 NewTimeZone += ":" + offset;
766
767 if (dst != 0)
768 NewTimeZone += DSTZone;
769
770 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
771 DataManager::update_tz_environment_variables();
772 return 0;
773}
774
775int GUIAction::overlay(std::string arg)
776{
777 return gui_changeOverlay(arg);
778}
779
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500780int GUIAction::queuezip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100781{
782 if (zip_queue_index >= 10) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500783 gui_msg("max_queue=Maximum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400784 return 0;
785 }
that3f7b1ac2014-12-30 11:30:13 +0100786 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
787 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
788 zip_queue_index++;
Dees_Troy51a0e822012-09-05 15:24:24 -0400789 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +0100790 }
791 return 0;
792}
793
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500794int GUIAction::cancelzip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100795{
796 if (zip_queue_index <= 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500797 gui_msg("min_queue=Minimum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400798 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100799 } else {
800 zip_queue_index--;
801 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
Dees_Troy51a0e822012-09-05 15:24:24 -0400802 }
that3f7b1ac2014-12-30 11:30:13 +0100803 return 0;
804}
Dees_Troy51a0e822012-09-05 15:24:24 -0400805
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500806int GUIAction::queueclear(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100807{
808 zip_queue_index = 0;
809 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
810 return 0;
811}
Dees_Troy51a0e822012-09-05 15:24:24 -0400812
that3f7b1ac2014-12-30 11:30:13 +0100813int GUIAction::sleep(std::string arg)
814{
815 operation_start("Sleep");
816 usleep(atoi(arg.c_str()));
817 operation_end(0);
818 return 0;
819}
Dees Troyb21cc642013-09-10 17:36:41 +0000820
Matt Mower9a2a2052016-05-31 21:31:22 -0500821int GUIAction::sleepcounter(std::string arg)
822{
823 operation_start("SleepCounter");
824 // Ensure user notices countdown in case it needs to be cancelled
825 blankTimer.resetTimerAndUnblank();
826 int total = atoi(arg.c_str());
827 for (int t = total; t > 0; t--) {
828 int progress = (int)(((float)(total-t)/(float)total)*100.0);
829 DataManager::SetValue("ui_progress", progress);
830 ::sleep(1);
831 DataManager::SetValue("tw_sleep", t-1);
832 }
833 DataManager::SetValue("ui_progress", 100);
834 operation_end(0);
835 return 0;
836}
837
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500838int GUIAction::appenddatetobackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100839{
840 operation_start("AppendDateToBackupName");
841 string Backup_Name;
842 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
843 Backup_Name += TWFunc::Get_Current_Date();
844 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
845 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
846 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
Matt Mower76b8afc2016-06-24 12:04:27 -0500847 PageManager::NotifyKey(KEY_END, true);
848 PageManager::NotifyKey(KEY_END, false);
that3f7b1ac2014-12-30 11:30:13 +0100849 operation_end(0);
850 return 0;
851}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500852
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500853int GUIAction::generatebackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100854{
855 operation_start("GenerateBackupName");
856 TWFunc::Auto_Generate_Backup_Name();
857 operation_end(0);
858 return 0;
859}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500860
Ethan Yonker483e9f42016-01-11 22:21:18 -0600861int GUIAction::checkpartitionlist(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100862{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600863 string List, part_path;
that3f7b1ac2014-12-30 11:30:13 +0100864 int count = 0;
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500865
Ethan Yonker483e9f42016-01-11 22:21:18 -0600866 if (arg.empty())
867 arg = "tw_wipe_list";
868 DataManager::GetValue(arg, List);
869 LOGINFO("checkpartitionlist list '%s'\n", List.c_str());
870 if (!List.empty()) {
871 size_t start_pos = 0, end_pos = List.find(";", start_pos);
872 while (end_pos != string::npos && start_pos < List.size()) {
873 part_path = List.substr(start_pos, end_pos - start_pos);
874 LOGINFO("checkpartitionlist part_path '%s'\n", part_path.c_str());
875 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100876 // Do nothing
Dees_Troy51a0e822012-09-05 15:24:24 -0400877 } else {
that3f7b1ac2014-12-30 11:30:13 +0100878 count++;
879 }
880 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600881 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100882 }
883 DataManager::SetValue("tw_check_partition_list", count);
884 } else {
885 DataManager::SetValue("tw_check_partition_list", 0);
886 }
Matt Mowera8a89d12016-12-30 18:10:37 -0600887 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100888}
Dees_Troy51a0e822012-09-05 15:24:24 -0400889
Ethan Yonker483e9f42016-01-11 22:21:18 -0600890int GUIAction::getpartitiondetails(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100891{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600892 string List, part_path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400893
Ethan Yonker483e9f42016-01-11 22:21:18 -0600894 if (arg.empty())
895 arg = "tw_wipe_list";
896 DataManager::GetValue(arg, List);
897 LOGINFO("getpartitiondetails list '%s'\n", List.c_str());
898 if (!List.empty()) {
899 size_t start_pos = 0, end_pos = List.find(";", start_pos);
900 part_path = List;
901 while (end_pos != string::npos && start_pos < List.size()) {
902 part_path = List.substr(start_pos, end_pos - start_pos);
903 LOGINFO("getpartitiondetails part_path '%s'\n", part_path.c_str());
904 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100905 // Do nothing
906 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600907 DataManager::SetValue("tw_partition_path", part_path);
that3f7b1ac2014-12-30 11:30:13 +0100908 break;
909 }
910 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600911 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100912 }
Ethan Yonker483e9f42016-01-11 22:21:18 -0600913 if (!part_path.empty()) {
914 TWPartition* Part = PartitionManager.Find_Partition_By_Path(part_path);
that3f7b1ac2014-12-30 11:30:13 +0100915 if (Part) {
916 unsigned long long mb = 1048576;
Dees_Troya13d74f2013-03-24 08:54:55 -0500917
that3f7b1ac2014-12-30 11:30:13 +0100918 DataManager::SetValue("tw_partition_name", Part->Display_Name);
919 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
920 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
921 DataManager::SetValue("tw_partition_size", Part->Size / mb);
922 DataManager::SetValue("tw_partition_used", Part->Used / mb);
923 DataManager::SetValue("tw_partition_free", Part->Free / mb);
924 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
925 DataManager::SetValue("tw_partition_removable", Part->Removable);
926 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
927
928 if (Part->Can_Repair())
929 DataManager::SetValue("tw_partition_can_repair", 1);
930 else
931 DataManager::SetValue("tw_partition_can_repair", 0);
Ethan Yonkera2719152015-05-28 09:44:41 -0500932 if (Part->Can_Resize())
933 DataManager::SetValue("tw_partition_can_resize", 1);
934 else
935 DataManager::SetValue("tw_partition_can_resize", 0);
bigbiffad58e1b2020-07-06 20:24:34 -0400936 if (TWFunc::Path_Exists("/system/bin/mkfs.fat"))
that3f7b1ac2014-12-30 11:30:13 +0100937 DataManager::SetValue("tw_partition_vfat", 1);
938 else
939 DataManager::SetValue("tw_partition_vfat", 0);
bigbiffad58e1b2020-07-06 20:24:34 -0400940 if (TWFunc::Path_Exists("/system/bin/mkexfatfs"))
that3f7b1ac2014-12-30 11:30:13 +0100941 DataManager::SetValue("tw_partition_exfat", 1);
942 else
943 DataManager::SetValue("tw_partition_exfat", 0);
whylef06b1652020-11-22 00:25:18 +0100944 if (TWFunc::Path_Exists("/system/bin/mkfs.f2fs") || TWFunc::Path_Exists("/system/bin/make_f2fs"))
that3f7b1ac2014-12-30 11:30:13 +0100945 DataManager::SetValue("tw_partition_f2fs", 1);
946 else
947 DataManager::SetValue("tw_partition_f2fs", 0);
bigbiffad58e1b2020-07-06 20:24:34 -0400948 if (TWFunc::Path_Exists("/system/bin/mke2fs"))
that3f7b1ac2014-12-30 11:30:13 +0100949 DataManager::SetValue("tw_partition_ext", 1);
950 else
951 DataManager::SetValue("tw_partition_ext", 0);
952 return 0;
953 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600954 LOGERR("Unable to locate partition: '%s'\n", part_path.c_str());
that3f7b1ac2014-12-30 11:30:13 +0100955 }
956 }
957 }
958 DataManager::SetValue("tw_partition_name", "");
959 DataManager::SetValue("tw_partition_file_system", "");
Ethan Yonker483e9f42016-01-11 22:21:18 -0600960 // Set this to 0 to prevent trying to partition this device, just in case
961 DataManager::SetValue("tw_partition_removable", 0);
that3f7b1ac2014-12-30 11:30:13 +0100962 return 0;
963}
964
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500965int GUIAction::screenshot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100966{
967 time_t tm;
968 char path[256];
969 int path_len;
Matt Mower0c88b842017-01-15 16:00:49 -0600970 uid_t uid = AID_MEDIA_RW;
971 gid_t gid = AID_MEDIA_RW;
that3f7b1ac2014-12-30 11:30:13 +0100972
973 const std::string storage = DataManager::GetCurrentStoragePath();
Matt Mowera8a89d12016-12-30 18:10:37 -0600974 if (PartitionManager.Is_Mounted_By_Path(storage)) {
that3f7b1ac2014-12-30 11:30:13 +0100975 snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
976 } else {
977 strcpy(path, "/tmp/");
978 }
979
Matt Mowera8a89d12016-12-30 18:10:37 -0600980 if (!TWFunc::Create_Dir_Recursive(path, 0775, uid, gid))
that3f7b1ac2014-12-30 11:30:13 +0100981 return 0;
982
983 tm = time(NULL);
984 path_len = strlen(path);
985
986 // Screenshot_2014-01-01-18-21-38.png
987 strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
988
989 int res = gr_save_screenshot(path);
Matt Mowera8a89d12016-12-30 18:10:37 -0600990 if (res == 0) {
that3f7b1ac2014-12-30 11:30:13 +0100991 chmod(path, 0666);
992 chown(path, uid, gid);
993
that677b13f2015-12-26 23:57:31 +0100994 gui_msg(Msg("screenshot_saved=Screenshot was saved to {1}")(path));
that3f7b1ac2014-12-30 11:30:13 +0100995
VDavid0032034a412019-10-18 22:43:20 +0200996 // blink to notify that the screenshot was taken
that3f7b1ac2014-12-30 11:30:13 +0100997 gr_color(255, 255, 255, 255);
998 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
999 gr_flip();
1000 gui_forceRender();
1001 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001002 gui_err("screenshot_err=Failed to take a screenshot!");
that3f7b1ac2014-12-30 11:30:13 +01001003 }
1004 return 0;
1005}
1006
1007int GUIAction::setbrightness(std::string arg)
1008{
1009 return TWFunc::Set_Brightness(arg);
1010}
1011
1012int GUIAction::fileexists(std::string arg)
1013{
1014 struct stat st;
1015 string newpath = arg + "/.";
1016
1017 operation_start("FileExists");
1018 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
1019 operation_end(0);
1020 else
1021 operation_end(1);
1022 return 0;
1023}
1024
thatcc8ddca2015-01-03 01:59:36 +01001025void GUIAction::reinject_after_flash()
1026{
1027 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001028 gui_msg("injecttwrp=Injecting TWRP into boot image...");
thatcc8ddca2015-01-03 01:59:36 +01001029 if (simulate) {
1030 simulate_progress_bar();
1031 } else {
1032 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
1033 if (Boot == NULL || Boot->Current_File_System != "emmc")
1034 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
1035 else {
1036 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
1037 TWFunc::Exec_Cmd(injectcmd);
1038 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001039 gui_msg("done=Done.");
thatcc8ddca2015-01-03 01:59:36 +01001040 }
1041 }
1042}
1043
mauronofrio6d3bf892019-10-26 19:47:55 +02001044int GUIAction::ozip_decrypt(string zip_path)
1045{
bigbiffad58e1b2020-07-06 20:24:34 -04001046 if (!TWFunc::Path_Exists("/system/bin/ozip_decrypt")) {
mauronofrio6d3bf892019-10-26 19:47:55 +02001047 return 1;
1048 }
1049 gui_msg("ozip_decrypt_decryption=Starting Ozip Decryption...");
1050 TWFunc::Exec_Cmd("ozip_decrypt " + (string)TW_OZIP_DECRYPT_KEY + " '" + zip_path + "'");
1051 gui_msg("ozip_decrypt_finish=Ozip Decryption Finished!");
1052 return 0;
1053}
1054
that3f7b1ac2014-12-30 11:30:13 +01001055int GUIAction::flash(std::string arg)
1056{
1057 int i, ret_val = 0, wipe_cache = 0;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001058 // We're going to jump to this page first, like a loading page
1059 gui_changePage(arg);
that3f7b1ac2014-12-30 11:30:13 +01001060 for (i=0; i<zip_queue_index; i++) {
thatc7b631b2015-06-01 23:36:57 +02001061 string zip_path = zip_queue[i];
1062 size_t slashpos = zip_path.find_last_of('/');
1063 string zip_filename = (slashpos == string::npos) ? zip_path : zip_path.substr(slashpos + 1);
that3f7b1ac2014-12-30 11:30:13 +01001064 operation_start("Flashing");
mauronofrio6d3bf892019-10-26 19:47:55 +02001065 if((zip_path.substr(zip_path.size() - 4, 4)) == "ozip")
1066 {
1067 if((ozip_decrypt(zip_path)) != 0)
1068 {
1069 LOGERR("Unable to find ozip_decrypt!");
1070 break;
1071 }
1072 zip_filename = (zip_filename.substr(0, zip_filename.size() - 4)).append("zip");
1073 zip_path = (zip_path.substr(0, zip_path.size() - 4)).append("zip");
1074 if (!TWFunc::Path_Exists(zip_path)) {
1075 LOGERR("Unable to find decrypted zip");
1076 break;
1077 }
1078 }
thatc7b631b2015-06-01 23:36:57 +02001079 DataManager::SetValue("tw_filename", zip_path);
1080 DataManager::SetValue("tw_file", zip_filename);
that3f7b1ac2014-12-30 11:30:13 +01001081 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
1082
1083 TWFunc::SetPerformanceMode(true);
thatc7b631b2015-06-01 23:36:57 +02001084 ret_val = flash_zip(zip_path, &wipe_cache);
that3f7b1ac2014-12-30 11:30:13 +01001085 TWFunc::SetPerformanceMode(false);
1086 if (ret_val != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001087 gui_msg(Msg(msg::kError, "zip_err=Error installing zip file '{1}'")(zip_path));
that3f7b1ac2014-12-30 11:30:13 +01001088 ret_val = 1;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001089 break;
that3f7b1ac2014-12-30 11:30:13 +01001090 }
1091 }
1092 zip_queue_index = 0;
that3f7b1ac2014-12-30 11:30:13 +01001093
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001094 if (wipe_cache) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001095 gui_msg("zip_wipe_cache=One or more zip requested a cache wipe -- Wiping cache now.");
that3f7b1ac2014-12-30 11:30:13 +01001096 PartitionManager.Wipe_By_Path("/cache");
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001097 }
that3f7b1ac2014-12-30 11:30:13 +01001098
thatcc8ddca2015-01-03 01:59:36 +01001099 reinject_after_flash();
that3f7b1ac2014-12-30 11:30:13 +01001100 PartitionManager.Update_System_Details();
1101 operation_end(ret_val);
bigbiffa869fc72016-03-01 19:40:36 -05001102 // 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 -06001103 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +01001104 return 0;
1105}
1106
1107int GUIAction::wipe(std::string arg)
1108{
1109 operation_start("Format");
1110 DataManager::SetValue("tw_partition", arg);
that3f7b1ac2014-12-30 11:30:13 +01001111 int ret_val = false;
1112
1113 if (simulate) {
1114 simulate_progress_bar();
1115 } else {
1116 if (arg == "data")
1117 ret_val = PartitionManager.Factory_Reset();
1118 else if (arg == "battery")
1119 ret_val = PartitionManager.Wipe_Battery_Stats();
1120 else if (arg == "rotate")
1121 ret_val = PartitionManager.Wipe_Rotate_Data();
1122 else if (arg == "dalvik")
1123 ret_val = PartitionManager.Wipe_Dalvik_Cache();
1124 else if (arg == "DATAMEDIA") {
1125 ret_val = PartitionManager.Format_Data();
1126 } else if (arg == "INTERNAL") {
Matt Mower23d8aae2017-01-06 14:30:33 -06001127 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001128
1129 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1130 if (has_datamedia) {
1131 ret_val = PartitionManager.Wipe_Media_From_Data();
1132 } else {
1133 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
1134 }
1135 } else if (arg == "EXTERNAL") {
1136 string External_Path;
1137
1138 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
1139 ret_val = PartitionManager.Wipe_By_Path(External_Path);
1140 } else if (arg == "ANDROIDSECURE") {
1141 ret_val = PartitionManager.Wipe_Android_Secure();
1142 } else if (arg == "LIST") {
1143 string Wipe_List, wipe_path;
1144 bool skip = false;
1145 ret_val = true;
that3f7b1ac2014-12-30 11:30:13 +01001146
1147 DataManager::GetValue("tw_wipe_list", Wipe_List);
1148 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
1149 if (!Wipe_List.empty()) {
1150 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
1151 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
1152 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
1153 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
1154 if (wipe_path == "/and-sec") {
1155 if (!PartitionManager.Wipe_Android_Secure()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001156 gui_msg("and_sec_wipe_err=Unable to wipe android secure");
that3f7b1ac2014-12-30 11:30:13 +01001157 ret_val = false;
1158 break;
1159 } else {
1160 skip = true;
1161 }
1162 } else if (wipe_path == "DALVIK") {
1163 if (!PartitionManager.Wipe_Dalvik_Cache()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001164 gui_err("dalvik_wipe_err=Failed to wipe dalvik");
that3f7b1ac2014-12-30 11:30:13 +01001165 ret_val = false;
1166 break;
1167 } else {
1168 skip = true;
1169 }
1170 } else if (wipe_path == "INTERNAL") {
1171 if (!PartitionManager.Wipe_Media_From_Data()) {
1172 ret_val = false;
1173 break;
1174 } else {
1175 skip = true;
Dees_Troya13d74f2013-03-24 08:54:55 -05001176 }
1177 }
that3f7b1ac2014-12-30 11:30:13 +01001178 if (!skip) {
1179 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001180 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(wipe_path));
that3f7b1ac2014-12-30 11:30:13 +01001181 ret_val = false;
1182 break;
1183 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
1184 arg = wipe_path;
1185 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001186 } else {
that3f7b1ac2014-12-30 11:30:13 +01001187 skip = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001188 }
that3f7b1ac2014-12-30 11:30:13 +01001189 start_pos = end_pos + 1;
1190 end_pos = Wipe_List.find(";", start_pos);
Dees_Troy51a0e822012-09-05 15:24:24 -04001191 }
1192 }
that3f7b1ac2014-12-30 11:30:13 +01001193 } else
1194 ret_val = PartitionManager.Wipe_By_Path(arg);
that73a52952015-01-28 23:07:34 +01001195#ifndef TW_OEM_BUILD
that3f7b1ac2014-12-30 11:30:13 +01001196 if (arg == DataManager::GetSettingsStoragePath()) {
1197 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
1198 string Storage_Path = DataManager::GetSettingsStoragePath();
Dees_Troy51a0e822012-09-05 15:24:24 -04001199
that3f7b1ac2014-12-30 11:30:13 +01001200 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
1201 LOGINFO("Making TWRP folder and saving settings.\n");
1202 Storage_Path += "/TWRP";
1203 mkdir(Storage_Path.c_str(), 0777);
1204 DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001205 } else {
that3f7b1ac2014-12-30 11:30:13 +01001206 LOGERR("Unable to recreate TWRP folder and save settings.\n");
1207 }
1208 }
1209#endif
1210 }
1211 PartitionManager.Update_System_Details();
1212 if (ret_val)
1213 ret_val = 0; // 0 is success
1214 else
1215 ret_val = 1; // 1 is failure
1216 operation_end(ret_val);
1217 return 0;
1218}
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001219
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001220int GUIAction::refreshsizes(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001221{
1222 operation_start("Refreshing Sizes");
1223 if (simulate) {
1224 simulate_progress_bar();
1225 } else
1226 PartitionManager.Update_System_Details();
1227 operation_end(0);
1228 return 0;
1229}
1230
1231int GUIAction::nandroid(std::string arg)
1232{
that3f7b1ac2014-12-30 11:30:13 +01001233 if (simulate) {
that73a52952015-01-28 23:07:34 +01001234 PartitionManager.stop_backup.set_value(0);
that3f7b1ac2014-12-30 11:30:13 +01001235 DataManager::SetValue("tw_partition", "Simulation");
1236 simulate_progress_bar();
that73a52952015-01-28 23:07:34 +01001237 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001238 } else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001239 operation_start("Nandroid");
1240 int ret = 0;
1241
that3f7b1ac2014-12-30 11:30:13 +01001242 if (arg == "backup") {
1243 string Backup_Name;
1244 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Ethan Yonker4adc33e2016-01-22 16:07:11 -06001245 string auto_gen = gui_lookup("auto_generate", "(Auto Generate)");
Ethan Yonker53796e72019-01-11 22:49:52 -06001246 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 -05001247 ret = PartitionManager.Run_Backup(false);
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001248 DataManager::SetValue("tw_encrypt_backup", 0); // reset value so we don't encrypt every subsequent backup
1249 if (!PartitionManager.stop_backup.get_value()) {
1250 if (ret == false)
1251 ret = 1; // 1 for failure
1252 else
1253 ret = 0; // 0 for success
1254 DataManager::SetValue("tw_cancel_backup", 0);
1255 } else {
1256 DataManager::SetValue("tw_cancel_backup", 1);
1257 gui_msg("backup_cancel=Backup Cancelled");
1258 ret = 0;
1259 }
bigbiffce8f83c2015-12-12 18:30:21 -05001260 } else {
that3f7b1ac2014-12-30 11:30:13 +01001261 operation_end(1);
1262 return -1;
that3f7b1ac2014-12-30 11:30:13 +01001263 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001264 DataManager::SetValue(TW_BACKUP_NAME, auto_gen);
that3f7b1ac2014-12-30 11:30:13 +01001265 } else if (arg == "restore") {
1266 string Restore_Name;
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001267 int gui_adb_backup;
1268
that3f7b1ac2014-12-30 11:30:13 +01001269 DataManager::GetValue("tw_restore", Restore_Name);
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001270 DataManager::GetValue("tw_enable_adb_backup", gui_adb_backup);
1271 if (gui_adb_backup) {
1272 DataManager::SetValue("tw_operation_state", 1);
1273 if (TWFunc::stream_adb_backup(Restore_Name) == 0)
1274 ret = 0; // success
1275 else
1276 ret = 1; // failure
1277 DataManager::SetValue("tw_enable_adb_backup", 0);
1278 ret = 0; // assume success???
1279 } else {
1280 if (PartitionManager.Run_Restore(Restore_Name))
1281 ret = 0; // success
1282 else
1283 ret = 1; // failure
1284 }
that3f7b1ac2014-12-30 11:30:13 +01001285 } else {
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001286 operation_end(1); // invalid arg specified, fail
bigbiff7abc5fe2015-01-17 16:53:12 -05001287 return -1;
1288 }
that73a52952015-01-28 23:07:34 +01001289 operation_end(ret);
bigbiff7abc5fe2015-01-17 16:53:12 -05001290 return ret;
1291 }
1292 return 0;
1293}
1294
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001295int GUIAction::cancelbackup(std::string arg __unused) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001296 if (simulate) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001297 PartitionManager.stop_backup.set_value(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001298 }
1299 else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001300 int op_status = PartitionManager.Cancel_Backup();
1301 if (op_status != 0)
1302 op_status = 1; // failure
bigbiff7abc5fe2015-01-17 16:53:12 -05001303 }
1304
1305 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001306}
Dees_Troy51a0e822012-09-05 15:24:24 -04001307
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001308int GUIAction::fixcontexts(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001309{
that73a52952015-01-28 23:07:34 +01001310 int op_status = 0;
1311
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001312 operation_start("Fix Contexts");
1313 LOGINFO("fix contexts started!\n");
that3f7b1ac2014-12-30 11:30:13 +01001314 if (simulate) {
1315 simulate_progress_bar();
1316 } else {
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001317 op_status = PartitionManager.Fix_Contexts();
that3f7b1ac2014-12-30 11:30:13 +01001318 if (op_status != 0)
1319 op_status = 1; // failure
that3f7b1ac2014-12-30 11:30:13 +01001320 }
that73a52952015-01-28 23:07:34 +01001321 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001322 return 0;
1323}
1324
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001325int GUIAction::fixpermissions(std::string arg)
1326{
1327 return fixcontexts(arg);
1328}
1329
that3f7b1ac2014-12-30 11:30:13 +01001330int GUIAction::dd(std::string arg)
1331{
1332 operation_start("imaging");
1333
1334 if (simulate) {
1335 simulate_progress_bar();
1336 } else {
1337 string cmd = "dd " + arg;
1338 TWFunc::Exec_Cmd(cmd);
1339 }
1340 operation_end(0);
1341 return 0;
1342}
1343
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001344int GUIAction::partitionsd(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001345{
1346 operation_start("Partition SD Card");
1347 int ret_val = 0;
1348
1349 if (simulate) {
1350 simulate_progress_bar();
1351 } else {
1352 int allow_partition;
1353 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1354 if (allow_partition == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001355 gui_err("no_real_sdcard=This device does not have a real SD Card! Aborting!");
that3f7b1ac2014-12-30 11:30:13 +01001356 } else {
1357 if (!PartitionManager.Partition_SDCard())
1358 ret_val = 1; // failed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001359 }
that3f7b1ac2014-12-30 11:30:13 +01001360 }
1361 operation_end(ret_val);
1362 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001363
that3f7b1ac2014-12-30 11:30:13 +01001364}
Dees_Troy51a0e822012-09-05 15:24:24 -04001365
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001366int GUIAction::installhtcdumlock(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001367{
1368 operation_start("Install HTC Dumlock");
1369 if (simulate) {
1370 simulate_progress_bar();
1371 } else
1372 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -04001373
that3f7b1ac2014-12-30 11:30:13 +01001374 operation_end(0);
1375 return 0;
1376}
Dees_Troy51a0e822012-09-05 15:24:24 -04001377
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001378int GUIAction::htcdumlockrestoreboot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001379{
1380 operation_start("HTC Dumlock Restore Boot");
1381 if (simulate) {
1382 simulate_progress_bar();
1383 } else
1384 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001385
that3f7b1ac2014-12-30 11:30:13 +01001386 operation_end(0);
1387 return 0;
1388}
Dees_Troy51a0e822012-09-05 15:24:24 -04001389
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001390int GUIAction::htcdumlockreflashrecovery(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001391{
1392 operation_start("HTC Dumlock Reflash Recovery");
1393 if (simulate) {
1394 simulate_progress_bar();
1395 } else
1396 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001397
that3f7b1ac2014-12-30 11:30:13 +01001398 operation_end(0);
1399 return 0;
1400}
Dees_Troy51a0e822012-09-05 15:24:24 -04001401
that3f7b1ac2014-12-30 11:30:13 +01001402int GUIAction::cmd(std::string arg)
1403{
1404 int op_status = 0;
1405
1406 operation_start("Command");
1407 LOGINFO("Running command: '%s'\n", arg.c_str());
1408 if (simulate) {
1409 simulate_progress_bar();
1410 } else {
1411 op_status = TWFunc::Exec_Cmd(arg);
1412 if (op_status != 0)
1413 op_status = 1;
1414 }
1415
1416 operation_end(op_status);
1417 return 0;
1418}
1419
1420int GUIAction::terminalcommand(std::string arg)
1421{
1422 int op_status = 0;
1423 string cmdpath, command;
1424
1425 DataManager::GetValue("tw_terminal_location", cmdpath);
1426 operation_start("CommandOutput");
1427 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
1428 if (simulate) {
1429 simulate_progress_bar();
1430 operation_end(op_status);
Matt Mower5aa29ab2015-02-25 23:50:55 -06001431 } else if (arg == "exit") {
1432 LOGINFO("Exiting terminal\n");
1433 operation_end(op_status);
1434 page("main");
that3f7b1ac2014-12-30 11:30:13 +01001435 } else {
1436 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
1437 LOGINFO("Actual command is: '%s'\n", command.c_str());
that3f7b1ac2014-12-30 11:30:13 +01001438 DataManager::SetValue("tw_terminal_state", 1);
1439 DataManager::SetValue("tw_background_thread_running", 1);
thatc6085482015-01-09 22:12:43 +01001440 FILE* fp;
1441 char line[512];
1442
1443 fp = popen(command.c_str(), "r");
1444 if (fp == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001445 LOGERR("Error opening command to run (%s).\n", strerror(errno));
thatc6085482015-01-09 22:12:43 +01001446 } else {
Matt Mower23d8aae2017-01-06 14:30:33 -06001447 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1;
thatc6085482015-01-09 22:12:43 +01001448 struct timeval timeout;
1449 fd_set fdset;
1450
Matt Mowera8a89d12016-12-30 18:10:37 -06001451 while (keep_going)
thatc6085482015-01-09 22:12:43 +01001452 {
1453 FD_ZERO(&fdset);
1454 FD_SET(fd, &fdset);
1455 timeout.tv_sec = 0;
1456 timeout.tv_usec = 400000;
1457 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1458 if (has_data == 0) {
1459 // Timeout reached
1460 DataManager::GetValue("tw_terminal_state", check);
1461 if (check == 0) {
1462 keep_going = 0;
1463 }
1464 } else if (has_data < 0) {
1465 // End of execution
1466 keep_going = 0;
1467 } else {
1468 // Try to read output
Matt Mowera8a89d12016-12-30 18:10:37 -06001469 if (fgets(line, sizeof(line), fp) != NULL)
thatc6085482015-01-09 22:12:43 +01001470 gui_print("%s", line); // Display output
1471 else
1472 keep_going = 0; // Done executing
1473 }
1474 }
1475 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -04001476 }
thatc6085482015-01-09 22:12:43 +01001477 DataManager::SetValue("tw_operation_status", 0);
1478 DataManager::SetValue("tw_operation_state", 1);
1479 DataManager::SetValue("tw_terminal_state", 0);
1480 DataManager::SetValue("tw_background_thread_running", 0);
1481 DataManager::SetValue(TW_ACTION_BUSY, 0);
that3f7b1ac2014-12-30 11:30:13 +01001482 }
1483 return 0;
1484}
1485
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001486int GUIAction::killterminal(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001487{
that3f7b1ac2014-12-30 11:30:13 +01001488 LOGINFO("Sending kill command...\n");
1489 operation_start("KillCommand");
1490 DataManager::SetValue("tw_operation_status", 0);
1491 DataManager::SetValue("tw_operation_state", 1);
1492 DataManager::SetValue("tw_terminal_state", 0);
1493 DataManager::SetValue("tw_background_thread_running", 0);
1494 DataManager::SetValue(TW_ACTION_BUSY, 0);
1495 return 0;
1496}
1497
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001498int GUIAction::reinjecttwrp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001499{
1500 int op_status = 0;
1501 operation_start("ReinjectTWRP");
Ethan Yonker74db1572015-10-28 12:44:49 -05001502 gui_msg("injecttwrp=Injecting TWRP into boot image...");
that3f7b1ac2014-12-30 11:30:13 +01001503 if (simulate) {
1504 simulate_progress_bar();
1505 } else {
1506 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Ethan Yonker74db1572015-10-28 12:44:49 -05001507 gui_msg("done=Done.");
that3f7b1ac2014-12-30 11:30:13 +01001508 }
1509
1510 operation_end(op_status);
1511 return 0;
1512}
1513
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001514int GUIAction::checkbackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001515{
1516 int op_status = 0;
1517
1518 operation_start("CheckBackupName");
1519 if (simulate) {
1520 simulate_progress_bar();
1521 } else {
Ethan Yonker53796e72019-01-11 22:49:52 -06001522 string Backup_Name;
1523 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
1524 op_status = PartitionManager.Check_Backup_Name(Backup_Name, true, true);
that3f7b1ac2014-12-30 11:30:13 +01001525 if (op_status != 0)
1526 op_status = 1;
1527 }
1528
1529 operation_end(op_status);
1530 return 0;
1531}
1532
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001533int GUIAction::decrypt(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001534{
1535 int op_status = 0;
1536
1537 operation_start("Decrypt");
1538 if (simulate) {
1539 simulate_progress_bar();
1540 } else {
1541 string Password;
Noah Jacobson81d638d2019-04-28 00:10:07 -04001542 string userID;
that3f7b1ac2014-12-30 11:30:13 +01001543 DataManager::GetValue("tw_crypto_password", Password);
Noah Jacobson81d638d2019-04-28 00:10:07 -04001544
1545 if (DataManager::GetIntValue(TW_IS_FBE)) { // for FBE
1546 DataManager::GetValue("tw_crypto_user_id", userID);
1547 if (userID != "") {
1548 op_status = PartitionManager.Decrypt_Device(Password, atoi(userID.c_str()));
1549 if (userID != "0") {
1550 if (op_status != 0)
1551 op_status = 1;
1552 operation_end(op_status);
1553 return 0;
1554 }
1555 } else {
1556 LOGINFO("User ID not found\n");
1557 op_status = 1;
1558 }
1559 ::sleep(1);
1560 } else { // for FDE
1561 op_status = PartitionManager.Decrypt_Device(Password);
1562 }
1563
that3f7b1ac2014-12-30 11:30:13 +01001564 if (op_status != 0)
1565 op_status = 1;
1566 else {
that3f7b1ac2014-12-30 11:30:13 +01001567 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1568
Ethan Yonkercf50da52015-01-12 21:59:07 -06001569 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001570
Ethan Yonkercf50da52015-01-12 21:59:07 -06001571 // Check for a custom theme and load it if exists
1572 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1573 if (has_datamedia != 0) {
1574 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06001575 LOGINFO("Failed to get default contexts and file mode for storage files.\n");
Ethan Yonkercf50da52015-01-12 21:59:07 -06001576 } else {
1577 LOGINFO("Got default contexts and file mode for storage files.\n");
that3f7b1ac2014-12-30 11:30:13 +01001578 }
1579 }
Ethan Yonker66a19492015-12-10 10:19:45 -06001580 PartitionManager.Decrypt_Adopted();
that3f7b1ac2014-12-30 11:30:13 +01001581 }
1582 }
1583
1584 operation_end(op_status);
1585 return 0;
1586}
1587
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001588int GUIAction::adbsideload(std::string arg __unused)
thatcc8ddca2015-01-03 01:59:36 +01001589{
1590 operation_start("Sideload");
1591 if (simulate) {
1592 simulate_progress_bar();
1593 operation_end(0);
1594 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001595 gui_msg("start_sideload=Starting ADB sideload feature...");
thatc6085482015-01-09 22:12:43 +01001596 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
1597
1598 // wait for the adb connection
bigbiffd58ba182020-03-23 10:02:29 -04001599 Device::BuiltinAction reboot_action = Device::REBOOT_BOOTLOADER;
bigbiff1f9e4842020-10-31 11:33:15 -04001600 int ret = twrp_sideload("/", &reboot_action);
1601 sideload_child_pid = GetMiniAdbdPid();
thatc6085482015-01-09 22:12:43 +01001602 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
1603
1604 if (ret != 0) {
1605 if (ret == -2)
Ethan Yonker74db1572015-10-28 12:44:49 -05001606 gui_msg("need_new_adb=You need adb 1.0.32 or newer to sideload to this device.");
thatc6085482015-01-09 22:12:43 +01001607 ret = 1; // failure
1608 } else {
1609 int wipe_cache = 0;
1610 int wipe_dalvik = 0;
1611 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
bigbiff1f9e4842020-10-31 11:33:15 -04001612 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1613 PartitionManager.Wipe_By_Path("/cache");
1614 if (wipe_dalvik)
1615 PartitionManager.Wipe_Dalvik_Cache();
thatcc8ddca2015-01-03 01:59:36 +01001616 }
thatc6085482015-01-09 22:12:43 +01001617 TWFunc::Toggle_MTP(mtp_was_enabled);
1618 reinject_after_flash();
1619 operation_end(ret);
thatcc8ddca2015-01-03 01:59:36 +01001620 }
that3f7b1ac2014-12-30 11:30:13 +01001621 return 0;
1622}
1623
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001624int GUIAction::adbsideloadcancel(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001625{
that3f7b1ac2014-12-30 11:30:13 +01001626 struct stat st;
1627 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
Ethan Yonker74db1572015-10-28 12:44:49 -05001628 gui_msg("cancel_sideload=Cancelling ADB sideload...");
thatcc8ddca2015-01-03 01:59:36 +01001629 LOGINFO("Signaling child sideload process to exit.\n");
1630 // Calling stat() on this magic filename signals the minadbd
1631 // subprocess to shut down.
1632 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
bigbiff1f9e4842020-10-31 11:33:15 -04001633 sideload_child_pid = GetMiniAdbdPid();
thatcc8ddca2015-01-03 01:59:36 +01001634 if (!sideload_child_pid) {
1635 LOGERR("Unable to get child ID\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001636 return 0;
1637 }
thatcc8ddca2015-01-03 01:59:36 +01001638 ::sleep(1);
1639 LOGINFO("Killing child sideload process.\n");
1640 kill(sideload_child_pid, SIGTERM);
1641 int status;
1642 LOGINFO("Waiting for child sideload process to exit.\n");
1643 waitpid(sideload_child_pid, &status, 0);
1644 sideload_child_pid = 0;
that3f7b1ac2014-12-30 11:30:13 +01001645 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
1646 return 0;
1647}
1648
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001649int GUIAction::openrecoveryscript(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001650{
1651 operation_start("OpenRecoveryScript");
1652 if (simulate) {
1653 simulate_progress_bar();
Ethan Yonker24e7eb72015-01-03 16:13:06 -06001654 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001655 } else {
that10ae24f2015-12-26 20:53:51 +01001656 int op_status = OpenRecoveryScript::Run_OpenRecoveryScript_Action();
Ethan Yonkere1abe612015-06-09 11:09:32 -05001657 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001658 }
1659 return 0;
1660}
1661
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001662int GUIAction::installsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001663{
1664 int op_status = 0;
1665
1666 operation_start("Install SuperSU");
1667 if (simulate) {
1668 simulate_progress_bar();
1669 } else {
Ethan Yonkerfa67cbf2018-07-20 12:22:33 -05001670 LOGERR("Installing SuperSU was deprecated from TWRP.\n");
that3f7b1ac2014-12-30 11:30:13 +01001671 }
1672
1673 operation_end(op_status);
1674 return 0;
1675}
1676
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001677int GUIAction::fixsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001678{
1679 int op_status = 0;
1680
1681 operation_start("Fixing Superuser Permissions");
1682 if (simulate) {
1683 simulate_progress_bar();
1684 } else {
1685 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1686 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
1687 }
1688
1689 operation_end(op_status);
1690 return 0;
1691}
1692
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001693int GUIAction::decrypt_backup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001694{
1695 int op_status = 0;
1696
1697 operation_start("Try Restore Decrypt");
1698 if (simulate) {
1699 simulate_progress_bar();
1700 } else {
1701 string Restore_Path, Filename, Password;
1702 DataManager::GetValue("tw_restore", Restore_Path);
1703 Restore_Path += "/";
1704 DataManager::GetValue("tw_restore_password", Password);
1705 TWFunc::SetPerformanceMode(true);
1706 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1707 op_status = 0; // success
1708 else
1709 op_status = 1; // fail
1710 TWFunc::SetPerformanceMode(false);
1711 }
1712
1713 operation_end(op_status);
1714 return 0;
1715}
1716
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001717int GUIAction::repair(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001718{
1719 int op_status = 0;
1720
1721 operation_start("Repair Partition");
1722 if (simulate) {
1723 simulate_progress_bar();
1724 } else {
1725 string part_path;
1726 DataManager::GetValue("tw_partition_mount_point", part_path);
1727 if (PartitionManager.Repair_By_Path(part_path, true)) {
1728 op_status = 0; // success
1729 } else {
that3f7b1ac2014-12-30 11:30:13 +01001730 op_status = 1; // fail
1731 }
1732 }
1733
1734 operation_end(op_status);
1735 return 0;
1736}
1737
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001738int GUIAction::resize(std::string arg __unused)
Ethan Yonkera2719152015-05-28 09:44:41 -05001739{
1740 int op_status = 0;
1741
1742 operation_start("Resize Partition");
1743 if (simulate) {
1744 simulate_progress_bar();
1745 } else {
1746 string part_path;
1747 DataManager::GetValue("tw_partition_mount_point", part_path);
1748 if (PartitionManager.Resize_By_Path(part_path, true)) {
1749 op_status = 0; // success
1750 } else {
Ethan Yonkera2719152015-05-28 09:44:41 -05001751 op_status = 1; // fail
1752 }
1753 }
1754
1755 operation_end(op_status);
1756 return 0;
1757}
1758
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001759int GUIAction::changefilesystem(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001760{
1761 int op_status = 0;
1762
1763 operation_start("Change File System");
1764 if (simulate) {
1765 simulate_progress_bar();
1766 } else {
1767 string part_path, file_system;
1768 DataManager::GetValue("tw_partition_mount_point", part_path);
1769 DataManager::GetValue("tw_action_new_file_system", file_system);
1770 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1771 op_status = 0; // success
1772 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001773 gui_err("change_fs_err=Error changing file system.");
that3f7b1ac2014-12-30 11:30:13 +01001774 op_status = 1; // fail
1775 }
1776 }
1777 PartitionManager.Update_System_Details();
1778 operation_end(op_status);
1779 return 0;
1780}
1781
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001782int GUIAction::startmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001783{
1784 int op_status = 0;
1785
1786 operation_start("Start MTP");
1787 if (PartitionManager.Enable_MTP())
1788 op_status = 0; // success
1789 else
1790 op_status = 1; // fail
1791
1792 operation_end(op_status);
1793 return 0;
1794}
1795
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001796int GUIAction::stopmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001797{
1798 int op_status = 0;
1799
1800 operation_start("Stop MTP");
1801 if (PartitionManager.Disable_MTP())
1802 op_status = 0; // success
1803 else
1804 op_status = 1; // fail
1805
1806 operation_end(op_status);
1807 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001808}
1809
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001810int GUIAction::flashimage(std::string arg __unused)
Ethan Yonker96af84a2015-01-05 14:58:36 -06001811{
1812 int op_status = 0;
epicX8f52c0a2021-02-24 23:12:08 +05301813 bool flag = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001814
1815 operation_start("Flash Image");
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001816 string path, filename;
1817 DataManager::GetValue("tw_zip_location", path);
1818 DataManager::GetValue("tw_file", filename);
Ethan Yonker96af84a2015-01-05 14:58:36 -06001819
epicX8f52c0a2021-02-24 23:12:08 +05301820#ifdef AB_OTA_UPDATER
1821 string target = DataManager::GetStrValue("tw_flash_partition");
1822 unsigned int pos = target.find_last_of(';');
1823 string mount_point = pos != string::npos ? target.substr(0, pos) : "";
1824 TWPartition* t_part = PartitionManager.Find_Partition_By_Path(mount_point);
1825 bool flash_in_both_slots = DataManager::GetIntValue("tw_flash_both_slots") ? true : false;
1826
1827 if (t_part != NULL && (flash_in_both_slots && t_part->SlotSelect))
1828 {
1829 string current_slot = PartitionManager.Get_Active_Slot_Display();
1830 bool pre_op_status = PartitionManager.Flash_Image(path, filename);
1831
1832 PartitionManager.Set_Active_Slot(current_slot == "A" ? "B" : "A");
1833 op_status = (int) !(pre_op_status && PartitionManager.Flash_Image(path, filename));
1834 PartitionManager.Set_Active_Slot(current_slot);
1835
1836 DataManager::SetValue("tw_flash_both_slots", 0);
1837 flag = false;
1838 }
1839#endif
1840 if (flag)
1841 {
1842 if (PartitionManager.Flash_Image(path, filename))
1843 op_status = 0; // success
1844 else
1845 op_status = 1; // fail
1846 }
1847
Ethan Yonker96af84a2015-01-05 14:58:36 -06001848 operation_end(op_status);
1849 return 0;
1850}
1851
that10ae24f2015-12-26 20:53:51 +01001852int GUIAction::twcmd(std::string arg)
1853{
1854 operation_start("TWRP CLI Command");
1855 if (simulate)
1856 simulate_progress_bar();
1857 else
1858 OpenRecoveryScript::Run_CLI_Command(arg.c_str());
1859 operation_end(0);
1860 return 0;
1861}
1862
Dees_Troy51a0e822012-09-05 15:24:24 -04001863int GUIAction::getKeyByName(std::string key)
1864{
that8834a0f2016-01-05 23:29:30 +01001865 if (key == "home") return KEY_HOMEPAGE; // note: KEY_HOME is cursor movement (like KEY_END)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001866 else if (key == "menu") return KEY_MENU;
1867 else if (key == "back") return KEY_BACK;
1868 else if (key == "search") return KEY_SEARCH;
1869 else if (key == "voldown") return KEY_VOLUMEDOWN;
1870 else if (key == "volup") return KEY_VOLUMEUP;
1871 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001872 int ret_val;
1873 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1874 if (!ret_val)
1875 return KEY_POWER;
1876 else
1877 return ret_val;
1878 }
1879
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001880 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001881}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001882
1883int GUIAction::checkpartitionlifetimewrites(std::string arg)
1884{
1885 int op_status = 0;
1886 TWPartition* sys = PartitionManager.Find_Partition_By_Path(arg);
1887
1888 operation_start("Check Partition Lifetime Writes");
1889 if (sys) {
1890 if (sys->Check_Lifetime_Writes() != 0)
1891 DataManager::SetValue("tw_lifetime_writes", 1);
1892 else
1893 DataManager::SetValue("tw_lifetime_writes", 0);
1894 op_status = 0; // success
1895 } else {
1896 DataManager::SetValue("tw_lifetime_writes", 1);
1897 op_status = 1; // fail
1898 }
1899
1900 operation_end(op_status);
1901 return 0;
1902}
1903
1904int GUIAction::mountsystemtoggle(std::string arg)
1905{
1906 int op_status = 0;
Captain Throwback9d6feb52018-07-27 10:05:24 -04001907 bool remount_system = PartitionManager.Is_Mounted_By_Path(PartitionManager.Get_Android_Root_Path());
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001908 bool remount_vendor = PartitionManager.Is_Mounted_By_Path("/vendor");
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001909
1910 operation_start("Toggle System Mount");
Captain Throwback9d6feb52018-07-27 10:05:24 -04001911 if (!PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001912 op_status = 1; // fail
1913 } else {
Captain Throwback9d6feb52018-07-27 10:05:24 -04001914 TWPartition* Part = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001915 if (Part) {
Ethan Yonkerd6966f42015-05-30 14:52:16 -05001916 if (arg == "0") {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001917 DataManager::SetValue("tw_mount_system_ro", 0);
1918 Part->Change_Mount_Read_Only(false);
1919 } else {
1920 DataManager::SetValue("tw_mount_system_ro", 1);
1921 Part->Change_Mount_Read_Only(true);
1922 }
1923 if (remount_system) {
1924 Part->Mount(true);
1925 }
1926 op_status = 0; // success
1927 } else {
1928 op_status = 1; // fail
1929 }
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001930 Part = PartitionManager.Find_Partition_By_Path("/vendor");
1931 if (Part) {
1932 if (arg == "0") {
1933 Part->Change_Mount_Read_Only(false);
1934 } else {
1935 Part->Change_Mount_Read_Only(true);
1936 }
1937 if (remount_vendor) {
1938 Part->Mount(true);
1939 }
1940 op_status = 0; // success
1941 } else {
1942 op_status = 1; // fail
1943 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001944 }
1945
1946 operation_end(op_status);
1947 return 0;
1948}
Ethan Yonker74db1572015-10-28 12:44:49 -05001949
1950int GUIAction::setlanguage(std::string arg __unused)
1951{
1952 int op_status = 0;
1953
1954 operation_start("Set Language");
1955 PageManager::LoadLanguage(DataManager::GetStrValue("tw_language"));
1956 PageManager::RequestReload();
1957 op_status = 0; // success
1958
1959 operation_end(op_status);
1960 return 0;
1961}
Ethan Yonker1b190162016-12-05 15:25:19 -06001962
Matt Mower9472ba12016-01-20 18:12:47 -06001963int GUIAction::togglebacklight(std::string arg __unused)
1964{
1965 blankTimer.toggleBlank();
1966 return 0;
1967}
1968
Ethan Yonker1b190162016-12-05 15:25:19 -06001969int GUIAction::setbootslot(std::string arg)
1970{
1971 operation_start("Set Boot Slot");
Captain Throwbackb60a2732020-10-13 17:55:43 -04001972 if (!simulate) {
1973 if (!PartitionManager.UnMount_By_Path("/vendor", false)) {
1974 // PartitionManager failed to unmount /vendor, this should not happen,
1975 // but in case it does, do a lazy unmount
1976 LOGINFO("WARNING: vendor partition could not be unmounted normally!\n");
1977 umount2("/vendor", MNT_DETACH);
1978 PartitionManager.Set_Active_Slot(arg);
1979 } else {
1980 PartitionManager.Set_Active_Slot(arg);
1981 }
1982 } else {
Ethan Yonker1b190162016-12-05 15:25:19 -06001983 simulate_progress_bar();
Captain Throwbackb60a2732020-10-13 17:55:43 -04001984 }
Ethan Yonker1b190162016-12-05 15:25:19 -06001985 operation_end(0);
1986 return 0;
1987}
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001988
1989int GUIAction::checkforapp(std::string arg __unused)
1990{
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001991 operation_start("Check for TWRP App");
1992 if (!simulate)
1993 {
epicX271bb3a2020-12-30 01:03:18 +05301994 TWFunc::checkforapp();
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001995 } else
1996 simulate_progress_bar();
epicX271bb3a2020-12-30 01:03:18 +05301997
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001998 operation_end(0);
1999 return 0;
2000}
2001
2002int GUIAction::installapp(std::string arg __unused)
2003{
2004 int op_status = 1;
2005 operation_start("Install TWRP App");
2006 if (!simulate)
2007 {
2008 if (DataManager::GetIntValue("tw_mount_system_ro") > 0 || DataManager::GetIntValue("tw_app_install_system") == 0) {
2009 if (PartitionManager.Mount_By_Path("/data", true)) {
2010 string install_path = "/data/app";
2011 string context = "u:object_r:apk_data_file:s0";
2012 if (!TWFunc::Path_Exists(install_path)) {
2013 if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
2014 LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno));
2015 goto exit;
2016 }
2017 if (chown(install_path.c_str(), 1000, 1000)) {
2018 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
2019 goto exit;
2020 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002021 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002022 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2023 goto exit;
2024 }
2025 }
2026 install_path += "/me.twrp.twrpapp-1";
2027 if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
2028 LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno));
2029 goto exit;
2030 }
2031 if (chown(install_path.c_str(), 1000, 1000)) {
2032 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
2033 goto exit;
2034 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002035 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002036 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2037 goto exit;
2038 }
2039 install_path += "/base.apk";
bigbiffad58e1b2020-07-06 20:24:34 -04002040 if (TWFunc::copy_file("/system/bin/me.twrp.twrpapp.apk", install_path, 0644)) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002041 LOGERR("Error copying apk file\n");
2042 goto exit;
2043 }
2044 if (chown(install_path.c_str(), 1000, 1000)) {
2045 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
2046 goto exit;
2047 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002048 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002049 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2050 goto exit;
2051 }
2052 sync();
2053 sync();
2054 }
2055 } else {
Captain Throwback9d6feb52018-07-27 10:05:24 -04002056 if (PartitionManager.Mount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) {
2057 string base_path = PartitionManager.Get_Android_Root_Path();
2058 if (TWFunc::Path_Exists(PartitionManager.Get_Android_Root_Path() + "/system"))
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002059 base_path += "/system"; // For devices with system as a root file system (e.g. Pixel)
2060 string install_path = base_path + "/priv-app";
2061 string context = "u:object_r:system_file:s0";
2062 if (!TWFunc::Path_Exists(install_path))
2063 install_path = base_path + "/app";
2064 if (TWFunc::Path_Exists(install_path)) {
2065 install_path += "/twrpapp";
2066 LOGINFO("Installing app to '%s'\n", install_path.c_str());
2067 if (mkdir(install_path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0) {
Ethan Yonker4767caf2017-01-11 10:45:04 -06002068 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002069 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2070 goto exit;
2071 }
2072 install_path += "/me.twrp.twrpapp.apk";
bigbiffad58e1b2020-07-06 20:24:34 -04002073 if (TWFunc::copy_file("/system/bin/me.twrp.twrpapp.apk", install_path, 0644)) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002074 LOGERR("Error copying apk file\n");
2075 goto exit;
2076 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002077 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002078 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2079 goto exit;
2080 }
Stefan Tauner6ad14572020-01-11 22:28:53 +01002081
2082 // System apps require their permissions to be pre-set via an XML file in /etc/permissions
2083 string permission_path = base_path + "/etc/permissions/privapp-permissions-twrpapp.xml";
bigbiffad58e1b2020-07-06 20:24:34 -04002084 if (TWFunc::copy_file("/system/bin/privapp-permissions-twrpapp.xml", permission_path, 0644)) {
Stefan Tauner6ad14572020-01-11 22:28:53 +01002085 LOGERR("Error copying permission file\n");
2086 goto exit;
2087 }
2088 if (chown(permission_path.c_str(), 1000, 1000)) {
2089 LOGERR("chown %s error: %s\n", permission_path.c_str(), strerror(errno));
2090 goto exit;
2091 }
2092 if (setfilecon(permission_path.c_str(), (security_context_t)context.c_str()) < 0) {
2093 LOGERR("setfilecon %s error: %s\n", permission_path.c_str(), strerror(errno));
2094 goto exit;
2095 }
2096
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002097 sync();
2098 sync();
Captain Throwback9d6feb52018-07-27 10:05:24 -04002099 PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002100 op_status = 0;
2101 } else {
2102 LOGERR("Error making app directory '%s': %s\n", strerror(errno));
2103 }
2104 }
2105 }
2106 }
2107 } else
2108 simulate_progress_bar();
2109exit:
epicX271bb3a2020-12-30 01:03:18 +05302110 TWFunc::checkforapp();
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002111 operation_end(0);
2112 return 0;
2113}
Ethan Yonker53796e72019-01-11 22:49:52 -06002114
Ethan Yonker76bbd3a2019-05-10 10:50:04 -05002115int GUIAction::uninstalltwrpsystemapp(std::string arg __unused)
2116{
2117 int op_status = 1;
2118 operation_start("Uninstall TWRP System App");
2119 if (!simulate)
2120 {
2121 int Mount_System_RO = DataManager::GetIntValue("tw_mount_system_ro");
2122 TWPartition* Part = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path());
2123 if (!Part) {
2124 LOGERR("Unabled to find system partition.\n");
2125 goto exit;
2126 }
2127 if (!Part->UnMount(true)) {
2128 goto exit;
2129 }
2130 if (Mount_System_RO > 0) {
2131 DataManager::SetValue("tw_mount_system_ro", 0);
2132 Part->Change_Mount_Read_Only(false);
2133 }
2134 if (Part->Mount(true)) {
2135 string base_path = PartitionManager.Get_Android_Root_Path();
2136 if (TWFunc::Path_Exists(PartitionManager.Get_Android_Root_Path() + "/system"))
2137 base_path += "/system"; // For devices with system as a root file system (e.g. Pixel)
2138 string uninstall_path = base_path + "/priv-app";
2139 if (!TWFunc::Path_Exists(uninstall_path))
2140 uninstall_path = base_path + "/app";
2141 uninstall_path += "/twrpapp";
2142 if (TWFunc::Path_Exists(uninstall_path)) {
2143 LOGINFO("Uninstalling TWRP App from '%s'\n", uninstall_path.c_str());
2144 if (TWFunc::removeDir(uninstall_path, false) == 0) {
2145 sync();
2146 op_status = 0;
2147 DataManager::SetValue("tw_app_installed_in_system", 0);
2148 DataManager::SetValue("tw_app_install_status", 0);
2149 } else {
2150 LOGERR("Unable to remove TWRP app from system.\n");
2151 }
2152 } else {
2153 LOGINFO("didn't find TWRP app in '%s'\n", uninstall_path.c_str());
2154 }
2155 }
2156 Part->UnMount(true);
2157 if (Mount_System_RO > 0) {
2158 DataManager::SetValue("tw_mount_system_ro", Mount_System_RO);
2159 Part->Change_Mount_Read_Only(true);
2160 }
2161 } else
2162 simulate_progress_bar();
2163exit:
epicX271bb3a2020-12-30 01:03:18 +05302164 TWFunc::checkforapp();
Ethan Yonker76bbd3a2019-05-10 10:50:04 -05002165 operation_end(0);
2166 return 0;
2167}
2168
Ethan Yonker53796e72019-01-11 22:49:52 -06002169int GUIAction::repackimage(std::string arg __unused)
2170{
2171 int op_status = 1;
bigbiffad58e1b2020-07-06 20:24:34 -04002172 twrpRepacker repacker;
2173
Ethan Yonker53796e72019-01-11 22:49:52 -06002174 operation_start("Repack Image");
2175 if (!simulate)
2176 {
2177 std::string path = DataManager::GetStrValue("tw_filename");
2178 Repack_Options_struct Repack_Options;
2179 Repack_Options.Disable_Verity = false;
2180 Repack_Options.Disable_Force_Encrypt = false;
2181 Repack_Options.Backup_First = DataManager::GetIntValue("tw_repack_backup_first") != 0;
2182 if (DataManager::GetIntValue("tw_repack_kernel") == 1)
2183 Repack_Options.Type = REPLACE_KERNEL;
2184 else
2185 Repack_Options.Type = REPLACE_RAMDISK;
bigbiffad58e1b2020-07-06 20:24:34 -04002186 if (!repacker.Repack_Image_And_Flash(path, Repack_Options))
Ethan Yonker53796e72019-01-11 22:49:52 -06002187 goto exit;
2188 } else
2189 simulate_progress_bar();
2190 op_status = 0;
2191exit:
2192 operation_end(op_status);
2193 return 0;
2194}
2195
2196int GUIAction::fixabrecoverybootloop(std::string arg __unused)
2197{
2198 int op_status = 1;
bigbiffad58e1b2020-07-06 20:24:34 -04002199 twrpRepacker repacker;
2200
Ethan Yonker53796e72019-01-11 22:49:52 -06002201 operation_start("Repack Image");
2202 if (!simulate)
2203 {
bigbiffad58e1b2020-07-06 20:24:34 -04002204 if (!TWFunc::Path_Exists("/system/bin/magiskboot")) {
Ethan Yonker53796e72019-01-11 22:49:52 -06002205 LOGERR("Image repacking tool not present in this TWRP build!");
2206 goto exit;
2207 }
2208 DataManager::SetProgress(0);
2209 TWPartition* part = PartitionManager.Find_Partition_By_Path("/boot");
2210 if (part)
2211 gui_msg(Msg("unpacking_image=Unpacking {1}...")(part->Display_Name));
2212 else {
2213 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")("/boot"));
2214 goto exit;
2215 }
bigbiffad58e1b2020-07-06 20:24:34 -04002216 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 -06002217 goto exit;
2218 DataManager::SetProgress(.25);
2219 gui_msg("fixing_recovery_loop_patch=Patching kernel...");
bigbiffad58e1b2020-07-06 20:24:34 -04002220 std::string command = "cd " REPACK_ORIG_DIR " && /system/bin/magiskboot hexpatch kernel 77616E745F696E697472616D667300 736B69705F696E697472616D667300";
Ethan Yonker53796e72019-01-11 22:49:52 -06002221 if (TWFunc::Exec_Cmd(command) != 0) {
2222 gui_msg(Msg(msg::kError, "fix_recovery_loop_patch_error=Error patching kernel."));
2223 goto exit;
2224 }
2225 std::string header_path = REPACK_ORIG_DIR;
2226 header_path += "header";
2227 if (TWFunc::Path_Exists(header_path)) {
2228 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";
2229 if (TWFunc::Exec_Cmd(command) != 0) {
2230 gui_msg(Msg(msg::kError, "fix_recovery_loop_patch_error=Error patching kernel."));
2231 goto exit;
2232 }
2233 }
2234 DataManager::SetProgress(.5);
2235 gui_msg(Msg("repacking_image=Repacking {1}...")(part->Display_Name));
bigbiffad58e1b2020-07-06 20:24:34 -04002236 command = "cd " REPACK_ORIG_DIR " && /system/bin/magiskboot repack " REPACK_ORIG_DIR "boot.img";
Ethan Yonker53796e72019-01-11 22:49:52 -06002237 if (TWFunc::Exec_Cmd(command) != 0) {
2238 gui_msg(Msg(msg::kError, "repack_error=Error repacking image."));
2239 goto exit;
2240 }
2241 DataManager::SetProgress(.75);
2242 std::string path = REPACK_ORIG_DIR;
2243 std::string file = "new-boot.img";
2244 DataManager::SetValue("tw_flash_partition", "/boot;");
2245 if (!PartitionManager.Flash_Image(path, file)) {
2246 LOGINFO("Error flashing new image\n");
2247 goto exit;
2248 }
2249 DataManager::SetProgress(1);
2250 TWFunc::removeDir(REPACK_ORIG_DIR, false);
2251 } else
2252 simulate_progress_bar();
2253 op_status = 0;
2254exit:
2255 operation_end(op_status);
2256 return 0;
2257}
bigbiffdf8436b2020-08-30 16:22:34 -04002258
2259
2260int GUIAction::enableadb(std::string arg __unused) {
2261 android::base::SetProperty("sys.usb.config", "none");
2262 android::base::SetProperty("sys.usb.config", "adb");
2263 return 0;
2264}
2265
2266int GUIAction::enablefastboot(std::string arg __unused) {
2267 android::base::SetProperty("sys.usb.config", "none");
2268 android::base::SetProperty("sys.usb.config", "fastboot");
2269 return 0;
2270}
Mohd Faraz77bbeb02020-08-12 12:29:42 +00002271
2272int GUIAction::changeterminal(std::string arg) {
2273 bool res = true;
2274 std::string resp, cmd = "cd " + arg;
2275 DataManager::GetValue("tw_terminal_location", resp);
2276 if (arg.empty() && !resp.empty()) {
2277 cmd = "cd /";
2278 for (uint8_t iter = 0; iter < cmd.size(); iter++)
2279 term->NotifyCharInput(cmd.at(iter));
2280 term->NotifyCharInput(13);
2281 DataManager::SetValue("tw_terminal_location", "");
2282 return 0;
2283 }
2284 if (term != NULL && !arg.empty()) {
2285 DataManager::SetValue("tw_terminal_location", arg);
2286 if (term->status()) {
2287 for (uint8_t iter = 0; iter < cmd.size(); iter++)
2288 term->NotifyCharInput(cmd.at(iter));
2289 term->NotifyCharInput(13);
2290 }
2291 else if (chdir(arg.c_str()) != 0) {
2292 LOGINFO("Unable to change dir to %s\n", arg.c_str());
2293 res = false;
2294 }
2295 }
2296 else {
2297 res = false;
2298 LOGINFO("Unable to switch to Terminal\n");
2299 }
2300 if (res)
2301 gui_changePage("terminalcommand");
2302 return 0;
2303}
Captain Throwback16dd81b2021-02-12 19:32:36 -05002304#ifndef TW_EXCLUDE_NANO
2305int GUIAction::editfile(std::string arg) {
2306 if (term != NULL) {
2307 for (uint8_t iter = 0; iter < arg.size(); iter++)
2308 term->NotifyCharInput(arg.at(iter));
2309 term->NotifyCharInput(13);
2310 }
2311 else
2312 LOGINFO("Unable to switch to Terminal\n");
2313 return 0;
2314}
2315#endif
epicXa721f952021-01-04 13:01:31 +05302316
2317int GUIAction::applycustomtwrpfolder(string arg __unused)
2318{
2319 operation_start("ChangingTWRPFolder");
2320 string storageFolder = DataManager::GetSettingsStoragePath();
2321 string newFolder = storageFolder + '/' + arg;
2322 string newBackupFolder = newFolder + "/BACKUPS/" + DataManager::GetStrValue("device_id");
2323 string prevFolder = storageFolder + DataManager::GetStrValue(TW_RECOVERY_FOLDER_VAR);
2324 bool ret = false;
2325
2326 if (TWFunc::Path_Exists(newFolder)) {
2327 gui_msg(Msg(msg::kError, "tw_folder_exists=A folder with that name already exists!"));
2328 } else {
2329 ret = true;
2330 }
2331
2332 if (newFolder != prevFolder && ret) {
2333 ret = TWFunc::Exec_Cmd("mv -f \"" + prevFolder + "\" \"" + newFolder + '\"') != 0 ? false : true;
2334 } else {
2335 gui_msg(Msg(msg::kError, "tw_folder_exists=A folder with that name already exists!"));
2336 }
2337
epicX11e90832021-03-01 00:02:57 +05302338 if (ret) ret = TWFunc::Recursive_Mkdir(newBackupFolder) ? true : false;
epicXa721f952021-01-04 13:01:31 +05302339
2340
2341 if (ret) {
2342 DataManager::SetValue(TW_RECOVERY_FOLDER_VAR, '/' + arg);
2343 DataManager::SetValue(TW_BACKUPS_FOLDER_VAR, newBackupFolder);
2344 DataManager::mBackingFile = newFolder + '/' + TW_SETTINGS_FILE;
2345 }
2346 operation_end((int)!ret);
2347 return 0;
2348}