blob: 19c3d0938980b484730ccf2f0a1369088642d925 [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
bigbiffd58ba182020-03-23 10:02:29 -040045#include "install/adb_install.h"
46
47#include "fuse_sideload.h"
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050048#include "blanktimer.hpp"
Ethan Yonker8373cfe2017-09-08 06:50:54 -050049#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"
bigbiffd58ba182020-03-23 10:02:29 -040055#include "install/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;
that3f7b1ac2014-12-30 11:30:13 +010069
that73a52952015-01-28 23:07:34 +010070static void *ActionThread_work_wrapper(void *data);
71
72class ActionThread
73{
74public:
75 ActionThread();
76 ~ActionThread();
77
78 void threadActions(GUIAction *act);
79 void run(void *data);
80private:
81 friend void *ActionThread_work_wrapper(void*);
82 struct ThreadData
83 {
84 ActionThread *this_;
85 GUIAction *act;
86 ThreadData(ActionThread *this_, GUIAction *act) : this_(this_), act(act) {}
87 };
88
89 pthread_t m_thread;
90 bool m_thread_running;
91 pthread_mutex_t m_act_lock;
92};
93
94static ActionThread action_thread; // for all kinds of longer running actions
95static ActionThread cancel_thread; // for longer running "cancel" actions
thatc6085482015-01-09 22:12:43 +010096
97static void *ActionThread_work_wrapper(void *data)
98{
that73a52952015-01-28 23:07:34 +010099 static_cast<ActionThread::ThreadData*>(data)->this_->run(data);
thatc6085482015-01-09 22:12:43 +0100100 return NULL;
101}
102
103ActionThread::ActionThread()
104{
105 m_thread_running = false;
106 pthread_mutex_init(&m_act_lock, NULL);
107}
108
109ActionThread::~ActionThread()
110{
111 pthread_mutex_lock(&m_act_lock);
Matt Mowera8a89d12016-12-30 18:10:37 -0600112 if (m_thread_running) {
thatc6085482015-01-09 22:12:43 +0100113 pthread_mutex_unlock(&m_act_lock);
114 pthread_join(m_thread, NULL);
115 } else {
116 pthread_mutex_unlock(&m_act_lock);
117 }
118 pthread_mutex_destroy(&m_act_lock);
119}
120
that7d3b54f2015-01-09 22:52:51 +0100121void ActionThread::threadActions(GUIAction *act)
thatc6085482015-01-09 22:12:43 +0100122{
123 pthread_mutex_lock(&m_act_lock);
124 if (m_thread_running) {
125 pthread_mutex_unlock(&m_act_lock);
that7d3b54f2015-01-09 22:52:51 +0100126 LOGERR("Another threaded action is already running -- not running %u actions starting with '%s'\n",
127 act->mActions.size(), act->mActions[0].mFunction.c_str());
thatc6085482015-01-09 22:12:43 +0100128 } else {
129 m_thread_running = true;
130 pthread_mutex_unlock(&m_act_lock);
that73a52952015-01-28 23:07:34 +0100131 ThreadData *d = new ThreadData(this, act);
thatc6085482015-01-09 22:12:43 +0100132 pthread_create(&m_thread, NULL, &ActionThread_work_wrapper, d);
133 }
134}
135
136void ActionThread::run(void *data)
137{
138 ThreadData *d = (ThreadData*)data;
139 GUIAction* act = d->act;
140
141 std::vector<GUIAction::Action>::iterator it;
that7d3b54f2015-01-09 22:52:51 +0100142 for (it = act->mActions.begin(); it != act->mActions.end(); ++it)
thatc6085482015-01-09 22:12:43 +0100143 act->doAction(*it);
144
145 pthread_mutex_lock(&m_act_lock);
146 m_thread_running = false;
147 pthread_mutex_unlock(&m_act_lock);
148 delete d;
149}
150
Dees_Troy51a0e822012-09-05 15:24:24 -0400151GUIAction::GUIAction(xml_node<>* node)
Vojtech Bocekede51c52014-02-07 23:58:09 +0100152 : GUIObject(node)
Dees_Troy51a0e822012-09-05 15:24:24 -0400153{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200154 xml_node<>* child;
155 xml_node<>* actions;
156 xml_attribute<>* attr;
Dees_Troy51a0e822012-09-05 15:24:24 -0400157
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200158 if (!node) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400159
that3f7b1ac2014-12-30 11:30:13 +0100160 if (mf.empty()) {
Vojtech Bocekecd89182015-01-15 16:28:54 +0100161#define ADD_ACTION(n) mf[#n] = &GUIAction::n
162#define ADD_ACTION_EX(name, func) mf[name] = &GUIAction::func
thatc6085482015-01-09 22:12:43 +0100163 // These actions will be run in the caller's thread
Vojtech Bocekecd89182015-01-15 16:28:54 +0100164 ADD_ACTION(reboot);
165 ADD_ACTION(home);
166 ADD_ACTION(key);
167 ADD_ACTION(page);
168 ADD_ACTION(reload);
169 ADD_ACTION(readBackup);
170 ADD_ACTION(set);
171 ADD_ACTION(clear);
172 ADD_ACTION(mount);
173 ADD_ACTION(unmount);
174 ADD_ACTION_EX("umount", unmount);
175 ADD_ACTION(restoredefaultsettings);
176 ADD_ACTION(copylog);
177 ADD_ACTION(compute);
178 ADD_ACTION_EX("addsubtract", compute);
179 ADD_ACTION(setguitimezone);
180 ADD_ACTION(overlay);
181 ADD_ACTION(queuezip);
182 ADD_ACTION(cancelzip);
183 ADD_ACTION(queueclear);
184 ADD_ACTION(sleep);
Matt Mower9a2a2052016-05-31 21:31:22 -0500185 ADD_ACTION(sleepcounter);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100186 ADD_ACTION(appenddatetobackupname);
187 ADD_ACTION(generatebackupname);
188 ADD_ACTION(checkpartitionlist);
189 ADD_ACTION(getpartitiondetails);
190 ADD_ACTION(screenshot);
191 ADD_ACTION(setbrightness);
192 ADD_ACTION(fileexists);
193 ADD_ACTION(killterminal);
194 ADD_ACTION(checkbackupname);
195 ADD_ACTION(adbsideloadcancel);
196 ADD_ACTION(fixsu);
197 ADD_ACTION(startmtp);
198 ADD_ACTION(stopmtp);
199 ADD_ACTION(cancelbackup);
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500200 ADD_ACTION(checkpartitionlifetimewrites);
201 ADD_ACTION(mountsystemtoggle);
Ethan Yonker74db1572015-10-28 12:44:49 -0500202 ADD_ACTION(setlanguage);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -0600203 ADD_ACTION(checkforapp);
Matt Mower9472ba12016-01-20 18:12:47 -0600204 ADD_ACTION(togglebacklight);
bigbiffdf8436b2020-08-30 16:22:34 -0400205 ADD_ACTION(enableadb);
206 ADD_ACTION(enablefastboot);
thatc6085482015-01-09 22:12:43 +0100207
208 // remember actions that run in the caller thread
209 for (mapFunc::const_iterator it = mf.begin(); it != mf.end(); ++it)
210 setActionsRunningInCallerThread.insert(it->first);
211
212 // These actions will run in a separate thread
Vojtech Bocekecd89182015-01-15 16:28:54 +0100213 ADD_ACTION(flash);
214 ADD_ACTION(wipe);
215 ADD_ACTION(refreshsizes);
216 ADD_ACTION(nandroid);
Ethan Yonkerb5fab762016-01-28 14:03:33 -0600217 ADD_ACTION(fixcontexts);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100218 ADD_ACTION(fixpermissions);
219 ADD_ACTION(dd);
220 ADD_ACTION(partitionsd);
221 ADD_ACTION(installhtcdumlock);
222 ADD_ACTION(htcdumlockrestoreboot);
223 ADD_ACTION(htcdumlockreflashrecovery);
224 ADD_ACTION(cmd);
225 ADD_ACTION(terminalcommand);
226 ADD_ACTION(reinjecttwrp);
227 ADD_ACTION(decrypt);
228 ADD_ACTION(adbsideload);
229 ADD_ACTION(openrecoveryscript);
230 ADD_ACTION(installsu);
231 ADD_ACTION(decrypt_backup);
232 ADD_ACTION(repair);
Ethan Yonkera2719152015-05-28 09:44:41 -0500233 ADD_ACTION(resize);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100234 ADD_ACTION(changefilesystem);
235 ADD_ACTION(flashimage);
that10ae24f2015-12-26 20:53:51 +0100236 ADD_ACTION(twcmd);
Ethan Yonker1b190162016-12-05 15:25:19 -0600237 ADD_ACTION(setbootslot);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -0600238 ADD_ACTION(installapp);
Ethan Yonker76bbd3a2019-05-10 10:50:04 -0500239 ADD_ACTION(uninstalltwrpsystemapp);
Ethan Yonker53796e72019-01-11 22:49:52 -0600240 ADD_ACTION(repackimage);
241 ADD_ACTION(fixabrecoverybootloop);
that3f7b1ac2014-12-30 11:30:13 +0100242 }
243
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200244 // First, get the action
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600245 actions = FindNode(node, "actions");
246 if (actions) child = FindNode(actions, "action");
247 else child = FindNode(node, "action");
Dees_Troy51a0e822012-09-05 15:24:24 -0400248
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200249 if (!child) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400250
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200251 while (child)
252 {
253 Action action;
Dees_Troy51a0e822012-09-05 15:24:24 -0400254
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200255 attr = child->first_attribute("function");
256 if (!attr) return;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500257
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200258 action.mFunction = attr->value();
259 action.mArg = child->value();
260 mActions.push_back(action);
Dees_Troy51a0e822012-09-05 15:24:24 -0400261
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200262 child = child->next_sibling("action");
263 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400264
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200265 // Now, let's get either the key or region
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600266 child = FindNode(node, "touch");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200267 if (child)
268 {
269 attr = child->first_attribute("key");
270 if (attr)
271 {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100272 std::vector<std::string> keys = TWFunc::Split_String(attr->value(), "+");
Matt Mowera8a89d12016-12-30 18:10:37 -0600273 for (size_t i = 0; i < keys.size(); ++i)
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100274 {
275 const int key = getKeyByName(keys[i]);
276 mKeys[key] = false;
277 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200278 }
279 else
280 {
281 attr = child->first_attribute("x");
282 if (!attr) return;
283 mActionX = atol(attr->value());
284 attr = child->first_attribute("y");
285 if (!attr) return;
286 mActionY = atol(attr->value());
287 attr = child->first_attribute("w");
288 if (!attr) return;
289 mActionW = atol(attr->value());
290 attr = child->first_attribute("h");
291 if (!attr) return;
292 mActionH = atol(attr->value());
293 }
294 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400295}
296
Matt Mower25036b72016-06-24 12:30:18 -0500297int GUIAction::NotifyTouch(TOUCH_STATE state, int x __unused, int y __unused)
Dees_Troy51a0e822012-09-05 15:24:24 -0400298{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200299 if (state == TOUCH_RELEASE)
300 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400301
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200302 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400303}
304
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100305int GUIAction::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -0400306{
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100307 std::map<int, bool>::iterator itr = mKeys.find(key);
Matt Mowera8a89d12016-12-30 18:10:37 -0600308 if (itr == mKeys.end())
that8834a0f2016-01-05 23:29:30 +0100309 return 1;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100310
311 bool prevState = itr->second;
312 itr->second = down;
313
314 // If there is only one key for this action, wait for key up so it
315 // doesn't trigger with multi-key actions.
316 // Else, check if all buttons are pressed, then consume their release events
317 // so they don't trigger one-button actions and reset mKeys pressed status
Matt Mowera8a89d12016-12-30 18:10:37 -0600318 if (mKeys.size() == 1) {
319 if (!down && prevState) {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100320 doActions();
thatd4725ca2016-01-19 00:15:21 +0100321 return 0;
322 }
Matt Mowera8a89d12016-12-30 18:10:37 -0600323 } else if (down) {
324 for (itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
325 if (!itr->second)
that8834a0f2016-01-05 23:29:30 +0100326 return 1;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100327 }
328
329 // Passed, all req buttons are pressed, reset them and consume release events
330 HardwareKeyboard *kb = PageManager::GetHardwareKeyboard();
Matt Mowera8a89d12016-12-30 18:10:37 -0600331 for (itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100332 kb->ConsumeKeyRelease(itr->first);
333 itr->second = false;
334 }
335
336 doActions();
thatd4725ca2016-01-19 00:15:21 +0100337 return 0;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100338 }
339
thatd4725ca2016-01-19 00:15:21 +0100340 return 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400341}
342
Vojtech Bocek07220562014-02-08 02:05:33 +0100343int GUIAction::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400344{
Vojtech Bocek07220562014-02-08 02:05:33 +0100345 GUIObject::NotifyVarChange(varName, value);
346
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100347 if (varName.empty() && !isConditionValid() && mKeys.empty() && !mActionW)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200348 doActions();
Matt Mowera8a89d12016-12-30 18:10:37 -0600349 else if ((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200350 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400351
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200352 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400353}
354
355void GUIAction::simulate_progress_bar(void)
356{
Ethan Yonker74db1572015-10-28 12:44:49 -0500357 gui_msg("simulating=Simulating actions...");
Dees_Troy51a0e822012-09-05 15:24:24 -0400358 for (int i = 0; i < 5; i++)
359 {
bigbiff7abc5fe2015-01-17 16:53:12 -0500360 if (PartitionManager.stop_backup.get_value()) {
361 DataManager::SetValue("tw_cancel_backup", 1);
Matt Mower3c366972015-12-25 19:28:31 -0600362 gui_msg("backup_cancel=Backup Cancelled");
bigbiff7abc5fe2015-01-17 16:53:12 -0500363 DataManager::SetValue("ui_progress", 0);
364 PartitionManager.stop_backup.set_value(0);
365 return;
366 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400367 usleep(500000);
368 DataManager::SetValue("ui_progress", i * 20);
369 }
370}
371
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600372int GUIAction::flash_zip(std::string filename, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400373{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200374 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400375
376 DataManager::SetValue("ui_progress", 0);
Chaosmasterd5364a02020-02-03 15:38:02 +0100377 DataManager::SetValue("ui_portion_size", 0);
378 DataManager::SetValue("ui_portion_start", 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400379
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200380 if (filename.empty())
381 {
382 LOGERR("No file specified.\n");
383 return -1;
384 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400385
Ethan Yonker9598c072016-01-15 21:54:34 -0600386 if (!TWFunc::Path_Exists(filename)) {
387 if (!PartitionManager.Mount_By_Path(filename, true)) {
388 return -1;
389 }
390 if (!TWFunc::Path_Exists(filename)) {
391 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")(filename));
392 return -1;
393 }
394 }
Dees_Troy657c3092012-09-10 20:32:10 -0400395
Dees_Troy51a0e822012-09-05 15:24:24 -0400396 if (simulate) {
397 simulate_progress_bar();
398 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400399 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400400
401 // Now, check if we need to ensure TWRP remains installed...
402 struct stat st;
bigbiffad58e1b2020-07-06 20:24:34 -0400403 if (stat("/system/bin/installTwrp", &st) == 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400404 {
405 DataManager::SetValue("tw_operation", "Configuring TWRP");
406 DataManager::SetValue("tw_partition", "");
Ethan Yonker74db1572015-10-28 12:44:49 -0500407 gui_msg("config_twrp=Configuring TWRP...");
bigbiffad58e1b2020-07-06 20:24:34 -0400408 if (TWFunc::Exec_Cmd("/system/bin/installTwrp reinstall") < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400409 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500410 gui_msg("config_twrp_err=Unable to configure TWRP with this kernel.");
Dees_Troy51a0e822012-09-05 15:24:24 -0400411 }
412 }
413 }
414
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200415 // Done
416 DataManager::SetValue("ui_progress", 100);
417 DataManager::SetValue("ui_progress", 0);
Chaosmasterd5364a02020-02-03 15:38:02 +0100418 DataManager::SetValue("ui_portion_size", 0);
419 DataManager::SetValue("ui_portion_start", 0);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200420 return ret_val;
Dees_Troy51a0e822012-09-05 15:24:24 -0400421}
422
that73a52952015-01-28 23:07:34 +0100423GUIAction::ThreadType GUIAction::getThreadType(const GUIAction::Action& action)
thatc6085482015-01-09 22:12:43 +0100424{
that73a52952015-01-28 23:07:34 +0100425 string func = gui_parse_text(action.mFunction);
426 bool needsThread = setActionsRunningInCallerThread.find(func) == setActionsRunningInCallerThread.end();
427 if (needsThread) {
428 if (func == "cancelbackup")
429 return THREAD_CANCEL;
430 else
431 return THREAD_ACTION;
432 }
433 return THREAD_NONE;
thatc6085482015-01-09 22:12:43 +0100434}
435
Dees_Troy51a0e822012-09-05 15:24:24 -0400436int GUIAction::doActions()
437{
that3f7b1ac2014-12-30 11:30:13 +0100438 if (mActions.size() < 1)
439 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400440
that73a52952015-01-28 23:07:34 +0100441 // Determine in which thread to run the actions.
442 // Do it for all actions at once before starting, so that we can cancel the whole batch if the thread is already busy.
443 ThreadType threadType = THREAD_NONE;
that3f7b1ac2014-12-30 11:30:13 +0100444 std::vector<Action>::iterator it;
that73a52952015-01-28 23:07:34 +0100445 for (it = mActions.begin(); it != mActions.end(); ++it) {
446 ThreadType tt = getThreadType(*it);
447 if (tt == THREAD_NONE)
448 continue;
449 if (threadType == THREAD_NONE)
450 threadType = tt;
451 else if (threadType != tt) {
452 LOGERR("Can't mix normal and cancel actions in the same list.\n"
453 "Running the whole batch in the cancel thread.\n");
454 threadType = THREAD_CANCEL;
thatc6085482015-01-09 22:12:43 +0100455 break;
456 }
that7d3b54f2015-01-09 22:52:51 +0100457 }
that73a52952015-01-28 23:07:34 +0100458
459 // Now run the actions in the desired thread.
460 switch (threadType) {
461 case THREAD_ACTION:
462 action_thread.threadActions(this);
463 break;
464
465 case THREAD_CANCEL:
466 cancel_thread.threadActions(this);
467 break;
468
469 default: {
470 // no iterators here because theme reloading might kill our object
471 const size_t cnt = mActions.size();
472 for (size_t i = 0; i < cnt; ++i)
473 doAction(mActions[i]);
474 }
thatc6085482015-01-09 22:12:43 +0100475 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400476
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200477 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400478}
479
that3f7b1ac2014-12-30 11:30:13 +0100480int GUIAction::doAction(Action action)
Dees_Troy51a0e822012-09-05 15:24:24 -0400481{
that3f7b1ac2014-12-30 11:30:13 +0100482 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400483
that3f7b1ac2014-12-30 11:30:13 +0100484 std::string function = gui_parse_text(action.mFunction);
485 std::string arg = gui_parse_text(action.mArg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400486
that3f7b1ac2014-12-30 11:30:13 +0100487 // find function and execute it
488 mapFunc::const_iterator funcitr = mf.find(function);
489 if (funcitr != mf.end())
490 return (this->*funcitr->second)(arg);
491
492 LOGERR("Unknown action '%s'\n", function.c_str());
493 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400494}
495
496void GUIAction::operation_start(const string operation_name)
497{
that3f7b1ac2014-12-30 11:30:13 +0100498 LOGINFO("operation_start: '%s'\n", operation_name.c_str());
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000499 time(&Start);
Dees_Troy51a0e822012-09-05 15:24:24 -0400500 DataManager::SetValue(TW_ACTION_BUSY, 1);
501 DataManager::SetValue("ui_progress", 0);
Chaosmasterd5364a02020-02-03 15:38:02 +0100502 DataManager::SetValue("ui_portion_size", 0);
503 DataManager::SetValue("ui_portion_start", 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400504 DataManager::SetValue("tw_operation", operation_name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400505 DataManager::SetValue("tw_operation_state", 0);
Ethan Yonkerd83c9ea2015-01-02 15:22:31 -0600506 DataManager::SetValue("tw_operation_status", 0);
bigbiff25d25b92020-06-19 16:07:38 -0400507 bool tw_ab_device = TWFunc::get_log_dir() != CACHE_LOGS_DIR;
bigbiff bigbiff19874f12019-01-08 20:06:57 -0500508 DataManager::SetValue("tw_ab_device", tw_ab_device);
Dees_Troy51a0e822012-09-05 15:24:24 -0400509}
510
that3f7b1ac2014-12-30 11:30:13 +0100511void GUIAction::operation_end(const int operation_status)
Dees_Troy51a0e822012-09-05 15:24:24 -0400512{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000513 time_t Stop;
Dees_Troy51a0e822012-09-05 15:24:24 -0400514 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400515 DataManager::SetValue("ui_progress", 100);
516 if (simulate) {
517 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
518 if (simulate_fail != 0)
519 DataManager::SetValue("tw_operation_status", 1);
520 else
521 DataManager::SetValue("tw_operation_status", 0);
522 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500523 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400524 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500525 }
526 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400527 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500528 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400529 }
530 DataManager::SetValue("tw_operation_state", 1);
531 DataManager::SetValue(TW_ACTION_BUSY, 0);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500532 blankTimer.resetTimerAndUnblank();
LuK133762326f42015-09-30 19:57:46 +0200533 property_set("twrp.action_complete", "1");
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000534 time(&Stop);
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400535
536#ifndef TW_NO_HAPTICS
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000537 if ((int) difftime(Stop, Start) > 10)
Ethan Yonker03db3262014-02-05 08:02:06 -0600538 DataManager::Vibrate("tw_action_vibrate");
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400539#endif
540
that3f7b1ac2014-12-30 11:30:13 +0100541 LOGINFO("operation_end - status=%d\n", operation_status);
Dees_Troy51a0e822012-09-05 15:24:24 -0400542}
543
that3f7b1ac2014-12-30 11:30:13 +0100544int GUIAction::reboot(std::string arg)
Dees_Troy51a0e822012-09-05 15:24:24 -0400545{
that3f7b1ac2014-12-30 11:30:13 +0100546 sync();
547 DataManager::SetValue("tw_gui_done", 1);
548 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400549
that3f7b1ac2014-12-30 11:30:13 +0100550 return 0;
551}
Dees_Troy51a0e822012-09-05 15:24:24 -0400552
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500553int GUIAction::home(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100554{
that3f7b1ac2014-12-30 11:30:13 +0100555 gui_changePage("main");
556 return 0;
557}
Dees_Troy51a0e822012-09-05 15:24:24 -0400558
that3f7b1ac2014-12-30 11:30:13 +0100559int GUIAction::key(std::string arg)
560{
561 const int key = getKeyByName(arg);
562 PageManager::NotifyKey(key, true);
563 PageManager::NotifyKey(key, false);
564 return 0;
565}
566
567int GUIAction::page(std::string arg)
568{
LuK133762326f42015-09-30 19:57:46 +0200569 property_set("twrp.action_complete", "0");
that3f7b1ac2014-12-30 11:30:13 +0100570 std::string page_name = gui_parse_text(arg);
571 return gui_changePage(page_name);
572}
573
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500574int GUIAction::reload(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100575{
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500576 PageManager::RequestReload();
577 // The actual reload is handled in pages.cpp in PageManager::RunReload()
578 // The reload will occur on the next Update or Render call and will
579 // be performed in the rendoer thread instead of the action thread
580 // to prevent crashing which could occur when we start deleting
581 // GUI resources in the action thread while we attempt to render
582 // with those same resources in another thread.
that3f7b1ac2014-12-30 11:30:13 +0100583 return 0;
584}
Dees_Troy51a0e822012-09-05 15:24:24 -0400585
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500586int GUIAction::readBackup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100587{
588 string Restore_Name;
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -0400589
that3f7b1ac2014-12-30 11:30:13 +0100590 DataManager::GetValue("tw_restore", Restore_Name);
591 PartitionManager.Set_Restore_Files(Restore_Name);
592 return 0;
593}
594
595int GUIAction::set(std::string arg)
596{
597 if (arg.find('=') != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200598 {
that3f7b1ac2014-12-30 11:30:13 +0100599 string varName = arg.substr(0, arg.find('='));
600 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400601
that3f7b1ac2014-12-30 11:30:13 +0100602 DataManager::GetValue(value, value);
603 DataManager::SetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200604 }
that3f7b1ac2014-12-30 11:30:13 +0100605 else
606 DataManager::SetValue(arg, "1");
607 return 0;
608}
Dees_Troy51a0e822012-09-05 15:24:24 -0400609
that3f7b1ac2014-12-30 11:30:13 +0100610int GUIAction::clear(std::string arg)
611{
612 DataManager::SetValue(arg, "0");
613 return 0;
614}
Dees_Troy51a0e822012-09-05 15:24:24 -0400615
that3f7b1ac2014-12-30 11:30:13 +0100616int GUIAction::mount(std::string arg)
617{
618 if (arg == "usb") {
619 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400620 if (!simulate)
that3f7b1ac2014-12-30 11:30:13 +0100621 PartitionManager.usb_storage_enable();
622 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500623 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100624 } else if (!simulate) {
625 PartitionManager.Mount_By_Path(arg, true);
626 PartitionManager.Add_MTP_Storage(arg);
627 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500628 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100629 return 0;
630}
631
632int GUIAction::unmount(std::string arg)
633{
634 if (arg == "usb") {
635 if (!simulate)
636 PartitionManager.usb_storage_disable();
637 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500638 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100639 DataManager::SetValue(TW_ACTION_BUSY, 0);
640 } else if (!simulate) {
641 PartitionManager.UnMount_By_Path(arg, true);
642 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500643 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100644 return 0;
645}
646
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500647int GUIAction::restoredefaultsettings(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100648{
649 operation_start("Restore Defaults");
650 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
Ethan Yonker74db1572015-10-28 12:44:49 -0500651 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100652 else {
653 DataManager::ResetDefaults();
654 PartitionManager.Update_System_Details();
655 PartitionManager.Mount_Current_Storage(true);
656 }
657 operation_end(0);
658 return 0;
659}
660
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500661int GUIAction::copylog(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100662{
663 operation_start("Copy Log");
664 if (!simulate)
665 {
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400666 string dst, curr_storage;
667 int copy_kernel_log = 0;
668
669 DataManager::GetValue("tw_include_kernel_log", copy_kernel_log);
that3f7b1ac2014-12-30 11:30:13 +0100670 PartitionManager.Mount_Current_Storage(true);
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400671 curr_storage = DataManager::GetCurrentStoragePath();
672 dst = curr_storage + "/recovery.log";
that3f7b1ac2014-12-30 11:30:13 +0100673 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
674 tw_set_default_metadata(dst.c_str());
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400675 if (copy_kernel_log)
676 TWFunc::copy_kernel_log(curr_storage);
that3f7b1ac2014-12-30 11:30:13 +0100677 sync();
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400678 gui_msg(Msg("copy_log=Copied recovery log to {1}")(dst));
that3f7b1ac2014-12-30 11:30:13 +0100679 } else
680 simulate_progress_bar();
681 operation_end(0);
682 return 0;
683}
684
685
686int GUIAction::compute(std::string arg)
687{
688 if (arg.find("+") != string::npos)
689 {
690 string varName = arg.substr(0, arg.find('+'));
691 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
692 int amount_to_add = atoi(string_to_add.c_str());
693 int value;
694
695 DataManager::GetValue(varName, value);
696 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400697 return 0;
698 }
that3f7b1ac2014-12-30 11:30:13 +0100699 if (arg.find("-") != string::npos)
Dees_Troy51a0e822012-09-05 15:24:24 -0400700 {
that3f7b1ac2014-12-30 11:30:13 +0100701 string varName = arg.substr(0, arg.find('-'));
702 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
703 int amount_to_subtract = atoi(string_to_subtract.c_str());
704 int value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400705
that3f7b1ac2014-12-30 11:30:13 +0100706 DataManager::GetValue(varName, value);
707 value -= amount_to_subtract;
708 if (value <= 0)
709 value = 0;
710 DataManager::SetValue(varName, value);
711 return 0;
712 }
713 if (arg.find("*") != string::npos)
714 {
715 string varName = arg.substr(0, arg.find('*'));
716 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
717 int multiply_by = atoi(multiply_by_str.c_str());
718 int value;
719
720 DataManager::GetValue(varName, value);
721 DataManager::SetValue(varName, value*multiply_by);
722 return 0;
723 }
724 if (arg.find("/") != string::npos)
725 {
726 string varName = arg.substr(0, arg.find('/'));
727 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
728 int divide_by = atoi(divide_by_str.c_str());
729 int value;
730
Matt Mowera8a89d12016-12-30 18:10:37 -0600731 if (divide_by != 0)
that3f7b1ac2014-12-30 11:30:13 +0100732 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400733 DataManager::GetValue(varName, value);
that3f7b1ac2014-12-30 11:30:13 +0100734 DataManager::SetValue(varName, value/divide_by);
Dees_Troy51a0e822012-09-05 15:24:24 -0400735 }
736 return 0;
737 }
that3f7b1ac2014-12-30 11:30:13 +0100738 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
739 return -1;
740}
Dees_Troy51a0e822012-09-05 15:24:24 -0400741
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500742int GUIAction::setguitimezone(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100743{
744 string SelectedZone;
745 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
746 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
747 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
748
749 int dst;
750 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
751
752 string offset;
753 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
754
755 string NewTimeZone = Zone;
756 if (offset != "0")
757 NewTimeZone += ":" + offset;
758
759 if (dst != 0)
760 NewTimeZone += DSTZone;
761
762 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
763 DataManager::update_tz_environment_variables();
764 return 0;
765}
766
767int GUIAction::overlay(std::string arg)
768{
769 return gui_changeOverlay(arg);
770}
771
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500772int GUIAction::queuezip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100773{
774 if (zip_queue_index >= 10) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500775 gui_msg("max_queue=Maximum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400776 return 0;
777 }
that3f7b1ac2014-12-30 11:30:13 +0100778 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
779 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
780 zip_queue_index++;
Dees_Troy51a0e822012-09-05 15:24:24 -0400781 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +0100782 }
783 return 0;
784}
785
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500786int GUIAction::cancelzip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100787{
788 if (zip_queue_index <= 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500789 gui_msg("min_queue=Minimum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400790 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100791 } else {
792 zip_queue_index--;
793 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
Dees_Troy51a0e822012-09-05 15:24:24 -0400794 }
that3f7b1ac2014-12-30 11:30:13 +0100795 return 0;
796}
Dees_Troy51a0e822012-09-05 15:24:24 -0400797
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500798int GUIAction::queueclear(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100799{
800 zip_queue_index = 0;
801 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
802 return 0;
803}
Dees_Troy51a0e822012-09-05 15:24:24 -0400804
that3f7b1ac2014-12-30 11:30:13 +0100805int GUIAction::sleep(std::string arg)
806{
807 operation_start("Sleep");
808 usleep(atoi(arg.c_str()));
809 operation_end(0);
810 return 0;
811}
Dees Troyb21cc642013-09-10 17:36:41 +0000812
Matt Mower9a2a2052016-05-31 21:31:22 -0500813int GUIAction::sleepcounter(std::string arg)
814{
815 operation_start("SleepCounter");
816 // Ensure user notices countdown in case it needs to be cancelled
817 blankTimer.resetTimerAndUnblank();
818 int total = atoi(arg.c_str());
819 for (int t = total; t > 0; t--) {
820 int progress = (int)(((float)(total-t)/(float)total)*100.0);
821 DataManager::SetValue("ui_progress", progress);
822 ::sleep(1);
823 DataManager::SetValue("tw_sleep", t-1);
824 }
825 DataManager::SetValue("ui_progress", 100);
826 operation_end(0);
827 return 0;
828}
829
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500830int GUIAction::appenddatetobackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100831{
832 operation_start("AppendDateToBackupName");
833 string Backup_Name;
834 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
835 Backup_Name += TWFunc::Get_Current_Date();
836 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
837 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
838 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
Matt Mower76b8afc2016-06-24 12:04:27 -0500839 PageManager::NotifyKey(KEY_END, true);
840 PageManager::NotifyKey(KEY_END, false);
that3f7b1ac2014-12-30 11:30:13 +0100841 operation_end(0);
842 return 0;
843}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500844
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500845int GUIAction::generatebackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100846{
847 operation_start("GenerateBackupName");
848 TWFunc::Auto_Generate_Backup_Name();
849 operation_end(0);
850 return 0;
851}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500852
Ethan Yonker483e9f42016-01-11 22:21:18 -0600853int GUIAction::checkpartitionlist(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100854{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600855 string List, part_path;
that3f7b1ac2014-12-30 11:30:13 +0100856 int count = 0;
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500857
Ethan Yonker483e9f42016-01-11 22:21:18 -0600858 if (arg.empty())
859 arg = "tw_wipe_list";
860 DataManager::GetValue(arg, List);
861 LOGINFO("checkpartitionlist list '%s'\n", List.c_str());
862 if (!List.empty()) {
863 size_t start_pos = 0, end_pos = List.find(";", start_pos);
864 while (end_pos != string::npos && start_pos < List.size()) {
865 part_path = List.substr(start_pos, end_pos - start_pos);
866 LOGINFO("checkpartitionlist part_path '%s'\n", part_path.c_str());
867 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100868 // Do nothing
Dees_Troy51a0e822012-09-05 15:24:24 -0400869 } else {
that3f7b1ac2014-12-30 11:30:13 +0100870 count++;
871 }
872 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600873 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100874 }
875 DataManager::SetValue("tw_check_partition_list", count);
876 } else {
877 DataManager::SetValue("tw_check_partition_list", 0);
878 }
Matt Mowera8a89d12016-12-30 18:10:37 -0600879 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100880}
Dees_Troy51a0e822012-09-05 15:24:24 -0400881
Ethan Yonker483e9f42016-01-11 22:21:18 -0600882int GUIAction::getpartitiondetails(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100883{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600884 string List, part_path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400885
Ethan Yonker483e9f42016-01-11 22:21:18 -0600886 if (arg.empty())
887 arg = "tw_wipe_list";
888 DataManager::GetValue(arg, List);
889 LOGINFO("getpartitiondetails list '%s'\n", List.c_str());
890 if (!List.empty()) {
891 size_t start_pos = 0, end_pos = List.find(";", start_pos);
892 part_path = List;
893 while (end_pos != string::npos && start_pos < List.size()) {
894 part_path = List.substr(start_pos, end_pos - start_pos);
895 LOGINFO("getpartitiondetails part_path '%s'\n", part_path.c_str());
896 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100897 // Do nothing
898 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600899 DataManager::SetValue("tw_partition_path", part_path);
that3f7b1ac2014-12-30 11:30:13 +0100900 break;
901 }
902 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600903 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100904 }
Ethan Yonker483e9f42016-01-11 22:21:18 -0600905 if (!part_path.empty()) {
906 TWPartition* Part = PartitionManager.Find_Partition_By_Path(part_path);
that3f7b1ac2014-12-30 11:30:13 +0100907 if (Part) {
908 unsigned long long mb = 1048576;
Dees_Troya13d74f2013-03-24 08:54:55 -0500909
that3f7b1ac2014-12-30 11:30:13 +0100910 DataManager::SetValue("tw_partition_name", Part->Display_Name);
911 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
912 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
913 DataManager::SetValue("tw_partition_size", Part->Size / mb);
914 DataManager::SetValue("tw_partition_used", Part->Used / mb);
915 DataManager::SetValue("tw_partition_free", Part->Free / mb);
916 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
917 DataManager::SetValue("tw_partition_removable", Part->Removable);
918 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
919
920 if (Part->Can_Repair())
921 DataManager::SetValue("tw_partition_can_repair", 1);
922 else
923 DataManager::SetValue("tw_partition_can_repair", 0);
Ethan Yonkera2719152015-05-28 09:44:41 -0500924 if (Part->Can_Resize())
925 DataManager::SetValue("tw_partition_can_resize", 1);
926 else
927 DataManager::SetValue("tw_partition_can_resize", 0);
bigbiffad58e1b2020-07-06 20:24:34 -0400928 if (TWFunc::Path_Exists("/system/bin/mkfs.fat"))
that3f7b1ac2014-12-30 11:30:13 +0100929 DataManager::SetValue("tw_partition_vfat", 1);
930 else
931 DataManager::SetValue("tw_partition_vfat", 0);
bigbiffad58e1b2020-07-06 20:24:34 -0400932 if (TWFunc::Path_Exists("/system/bin/mkexfatfs"))
that3f7b1ac2014-12-30 11:30:13 +0100933 DataManager::SetValue("tw_partition_exfat", 1);
934 else
935 DataManager::SetValue("tw_partition_exfat", 0);
bigbiffad58e1b2020-07-06 20:24:34 -0400936 if (TWFunc::Path_Exists("/system/bin/mkfs.f2fs"))
that3f7b1ac2014-12-30 11:30:13 +0100937 DataManager::SetValue("tw_partition_f2fs", 1);
938 else
939 DataManager::SetValue("tw_partition_f2fs", 0);
bigbiffad58e1b2020-07-06 20:24:34 -0400940 if (TWFunc::Path_Exists("/system/bin/mke2fs"))
that3f7b1ac2014-12-30 11:30:13 +0100941 DataManager::SetValue("tw_partition_ext", 1);
942 else
943 DataManager::SetValue("tw_partition_ext", 0);
944 return 0;
945 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600946 LOGERR("Unable to locate partition: '%s'\n", part_path.c_str());
that3f7b1ac2014-12-30 11:30:13 +0100947 }
948 }
949 }
950 DataManager::SetValue("tw_partition_name", "");
951 DataManager::SetValue("tw_partition_file_system", "");
Ethan Yonker483e9f42016-01-11 22:21:18 -0600952 // Set this to 0 to prevent trying to partition this device, just in case
953 DataManager::SetValue("tw_partition_removable", 0);
that3f7b1ac2014-12-30 11:30:13 +0100954 return 0;
955}
956
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500957int GUIAction::screenshot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100958{
959 time_t tm;
960 char path[256];
961 int path_len;
Matt Mower0c88b842017-01-15 16:00:49 -0600962 uid_t uid = AID_MEDIA_RW;
963 gid_t gid = AID_MEDIA_RW;
that3f7b1ac2014-12-30 11:30:13 +0100964
965 const std::string storage = DataManager::GetCurrentStoragePath();
Matt Mowera8a89d12016-12-30 18:10:37 -0600966 if (PartitionManager.Is_Mounted_By_Path(storage)) {
that3f7b1ac2014-12-30 11:30:13 +0100967 snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
968 } else {
969 strcpy(path, "/tmp/");
970 }
971
Matt Mowera8a89d12016-12-30 18:10:37 -0600972 if (!TWFunc::Create_Dir_Recursive(path, 0775, uid, gid))
that3f7b1ac2014-12-30 11:30:13 +0100973 return 0;
974
975 tm = time(NULL);
976 path_len = strlen(path);
977
978 // Screenshot_2014-01-01-18-21-38.png
979 strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
980
981 int res = gr_save_screenshot(path);
Matt Mowera8a89d12016-12-30 18:10:37 -0600982 if (res == 0) {
that3f7b1ac2014-12-30 11:30:13 +0100983 chmod(path, 0666);
984 chown(path, uid, gid);
985
that677b13f2015-12-26 23:57:31 +0100986 gui_msg(Msg("screenshot_saved=Screenshot was saved to {1}")(path));
that3f7b1ac2014-12-30 11:30:13 +0100987
VDavid0032034a412019-10-18 22:43:20 +0200988 // blink to notify that the screenshot was taken
that3f7b1ac2014-12-30 11:30:13 +0100989 gr_color(255, 255, 255, 255);
990 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
991 gr_flip();
992 gui_forceRender();
993 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500994 gui_err("screenshot_err=Failed to take a screenshot!");
that3f7b1ac2014-12-30 11:30:13 +0100995 }
996 return 0;
997}
998
999int GUIAction::setbrightness(std::string arg)
1000{
1001 return TWFunc::Set_Brightness(arg);
1002}
1003
1004int GUIAction::fileexists(std::string arg)
1005{
1006 struct stat st;
1007 string newpath = arg + "/.";
1008
1009 operation_start("FileExists");
1010 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
1011 operation_end(0);
1012 else
1013 operation_end(1);
1014 return 0;
1015}
1016
thatcc8ddca2015-01-03 01:59:36 +01001017void GUIAction::reinject_after_flash()
1018{
1019 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001020 gui_msg("injecttwrp=Injecting TWRP into boot image...");
thatcc8ddca2015-01-03 01:59:36 +01001021 if (simulate) {
1022 simulate_progress_bar();
1023 } else {
1024 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
1025 if (Boot == NULL || Boot->Current_File_System != "emmc")
1026 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
1027 else {
1028 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
1029 TWFunc::Exec_Cmd(injectcmd);
1030 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001031 gui_msg("done=Done.");
thatcc8ddca2015-01-03 01:59:36 +01001032 }
1033 }
1034}
1035
mauronofrio6d3bf892019-10-26 19:47:55 +02001036int GUIAction::ozip_decrypt(string zip_path)
1037{
bigbiffad58e1b2020-07-06 20:24:34 -04001038 if (!TWFunc::Path_Exists("/system/bin/ozip_decrypt")) {
mauronofrio6d3bf892019-10-26 19:47:55 +02001039 return 1;
1040 }
1041 gui_msg("ozip_decrypt_decryption=Starting Ozip Decryption...");
1042 TWFunc::Exec_Cmd("ozip_decrypt " + (string)TW_OZIP_DECRYPT_KEY + " '" + zip_path + "'");
1043 gui_msg("ozip_decrypt_finish=Ozip Decryption Finished!");
1044 return 0;
1045}
1046
that3f7b1ac2014-12-30 11:30:13 +01001047int GUIAction::flash(std::string arg)
1048{
1049 int i, ret_val = 0, wipe_cache = 0;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001050 // We're going to jump to this page first, like a loading page
1051 gui_changePage(arg);
that3f7b1ac2014-12-30 11:30:13 +01001052 for (i=0; i<zip_queue_index; i++) {
thatc7b631b2015-06-01 23:36:57 +02001053 string zip_path = zip_queue[i];
1054 size_t slashpos = zip_path.find_last_of('/');
1055 string zip_filename = (slashpos == string::npos) ? zip_path : zip_path.substr(slashpos + 1);
that3f7b1ac2014-12-30 11:30:13 +01001056 operation_start("Flashing");
mauronofrio6d3bf892019-10-26 19:47:55 +02001057 if((zip_path.substr(zip_path.size() - 4, 4)) == "ozip")
1058 {
1059 if((ozip_decrypt(zip_path)) != 0)
1060 {
1061 LOGERR("Unable to find ozip_decrypt!");
1062 break;
1063 }
1064 zip_filename = (zip_filename.substr(0, zip_filename.size() - 4)).append("zip");
1065 zip_path = (zip_path.substr(0, zip_path.size() - 4)).append("zip");
1066 if (!TWFunc::Path_Exists(zip_path)) {
1067 LOGERR("Unable to find decrypted zip");
1068 break;
1069 }
1070 }
thatc7b631b2015-06-01 23:36:57 +02001071 DataManager::SetValue("tw_filename", zip_path);
1072 DataManager::SetValue("tw_file", zip_filename);
that3f7b1ac2014-12-30 11:30:13 +01001073 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
1074
1075 TWFunc::SetPerformanceMode(true);
thatc7b631b2015-06-01 23:36:57 +02001076 ret_val = flash_zip(zip_path, &wipe_cache);
that3f7b1ac2014-12-30 11:30:13 +01001077 TWFunc::SetPerformanceMode(false);
1078 if (ret_val != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001079 gui_msg(Msg(msg::kError, "zip_err=Error installing zip file '{1}'")(zip_path));
that3f7b1ac2014-12-30 11:30:13 +01001080 ret_val = 1;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001081 break;
that3f7b1ac2014-12-30 11:30:13 +01001082 }
1083 }
1084 zip_queue_index = 0;
that3f7b1ac2014-12-30 11:30:13 +01001085
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001086 if (wipe_cache) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001087 gui_msg("zip_wipe_cache=One or more zip requested a cache wipe -- Wiping cache now.");
that3f7b1ac2014-12-30 11:30:13 +01001088 PartitionManager.Wipe_By_Path("/cache");
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001089 }
that3f7b1ac2014-12-30 11:30:13 +01001090
thatcc8ddca2015-01-03 01:59:36 +01001091 reinject_after_flash();
that3f7b1ac2014-12-30 11:30:13 +01001092 PartitionManager.Update_System_Details();
1093 operation_end(ret_val);
bigbiffa869fc72016-03-01 19:40:36 -05001094 // 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 -06001095 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +01001096 return 0;
1097}
1098
1099int GUIAction::wipe(std::string arg)
1100{
1101 operation_start("Format");
1102 DataManager::SetValue("tw_partition", arg);
that3f7b1ac2014-12-30 11:30:13 +01001103 int ret_val = false;
1104
1105 if (simulate) {
1106 simulate_progress_bar();
1107 } else {
1108 if (arg == "data")
1109 ret_val = PartitionManager.Factory_Reset();
1110 else if (arg == "battery")
1111 ret_val = PartitionManager.Wipe_Battery_Stats();
1112 else if (arg == "rotate")
1113 ret_val = PartitionManager.Wipe_Rotate_Data();
1114 else if (arg == "dalvik")
1115 ret_val = PartitionManager.Wipe_Dalvik_Cache();
1116 else if (arg == "DATAMEDIA") {
1117 ret_val = PartitionManager.Format_Data();
1118 } else if (arg == "INTERNAL") {
Matt Mower23d8aae2017-01-06 14:30:33 -06001119 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001120
1121 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1122 if (has_datamedia) {
1123 ret_val = PartitionManager.Wipe_Media_From_Data();
1124 } else {
1125 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
1126 }
1127 } else if (arg == "EXTERNAL") {
1128 string External_Path;
1129
1130 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
1131 ret_val = PartitionManager.Wipe_By_Path(External_Path);
1132 } else if (arg == "ANDROIDSECURE") {
1133 ret_val = PartitionManager.Wipe_Android_Secure();
1134 } else if (arg == "LIST") {
1135 string Wipe_List, wipe_path;
1136 bool skip = false;
1137 ret_val = true;
that3f7b1ac2014-12-30 11:30:13 +01001138
1139 DataManager::GetValue("tw_wipe_list", Wipe_List);
1140 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
1141 if (!Wipe_List.empty()) {
1142 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
1143 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
1144 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
1145 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
1146 if (wipe_path == "/and-sec") {
1147 if (!PartitionManager.Wipe_Android_Secure()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001148 gui_msg("and_sec_wipe_err=Unable to wipe android secure");
that3f7b1ac2014-12-30 11:30:13 +01001149 ret_val = false;
1150 break;
1151 } else {
1152 skip = true;
1153 }
1154 } else if (wipe_path == "DALVIK") {
1155 if (!PartitionManager.Wipe_Dalvik_Cache()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001156 gui_err("dalvik_wipe_err=Failed to wipe dalvik");
that3f7b1ac2014-12-30 11:30:13 +01001157 ret_val = false;
1158 break;
1159 } else {
1160 skip = true;
1161 }
1162 } else if (wipe_path == "INTERNAL") {
1163 if (!PartitionManager.Wipe_Media_From_Data()) {
1164 ret_val = false;
1165 break;
1166 } else {
1167 skip = true;
Dees_Troya13d74f2013-03-24 08:54:55 -05001168 }
1169 }
that3f7b1ac2014-12-30 11:30:13 +01001170 if (!skip) {
1171 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001172 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(wipe_path));
that3f7b1ac2014-12-30 11:30:13 +01001173 ret_val = false;
1174 break;
1175 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
1176 arg = wipe_path;
1177 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001178 } else {
that3f7b1ac2014-12-30 11:30:13 +01001179 skip = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001180 }
that3f7b1ac2014-12-30 11:30:13 +01001181 start_pos = end_pos + 1;
1182 end_pos = Wipe_List.find(";", start_pos);
Dees_Troy51a0e822012-09-05 15:24:24 -04001183 }
1184 }
that3f7b1ac2014-12-30 11:30:13 +01001185 } else
1186 ret_val = PartitionManager.Wipe_By_Path(arg);
that73a52952015-01-28 23:07:34 +01001187#ifndef TW_OEM_BUILD
that3f7b1ac2014-12-30 11:30:13 +01001188 if (arg == DataManager::GetSettingsStoragePath()) {
1189 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
1190 string Storage_Path = DataManager::GetSettingsStoragePath();
Dees_Troy51a0e822012-09-05 15:24:24 -04001191
that3f7b1ac2014-12-30 11:30:13 +01001192 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
1193 LOGINFO("Making TWRP folder and saving settings.\n");
1194 Storage_Path += "/TWRP";
1195 mkdir(Storage_Path.c_str(), 0777);
1196 DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001197 } else {
that3f7b1ac2014-12-30 11:30:13 +01001198 LOGERR("Unable to recreate TWRP folder and save settings.\n");
1199 }
1200 }
1201#endif
1202 }
1203 PartitionManager.Update_System_Details();
1204 if (ret_val)
1205 ret_val = 0; // 0 is success
1206 else
1207 ret_val = 1; // 1 is failure
1208 operation_end(ret_val);
1209 return 0;
1210}
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001211
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001212int GUIAction::refreshsizes(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001213{
1214 operation_start("Refreshing Sizes");
1215 if (simulate) {
1216 simulate_progress_bar();
1217 } else
1218 PartitionManager.Update_System_Details();
1219 operation_end(0);
1220 return 0;
1221}
1222
1223int GUIAction::nandroid(std::string arg)
1224{
that3f7b1ac2014-12-30 11:30:13 +01001225 if (simulate) {
that73a52952015-01-28 23:07:34 +01001226 PartitionManager.stop_backup.set_value(0);
that3f7b1ac2014-12-30 11:30:13 +01001227 DataManager::SetValue("tw_partition", "Simulation");
1228 simulate_progress_bar();
that73a52952015-01-28 23:07:34 +01001229 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001230 } else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001231 operation_start("Nandroid");
1232 int ret = 0;
1233
that3f7b1ac2014-12-30 11:30:13 +01001234 if (arg == "backup") {
1235 string Backup_Name;
1236 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Ethan Yonker4adc33e2016-01-22 16:07:11 -06001237 string auto_gen = gui_lookup("auto_generate", "(Auto Generate)");
Ethan Yonker53796e72019-01-11 22:49:52 -06001238 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 -05001239 ret = PartitionManager.Run_Backup(false);
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001240 DataManager::SetValue("tw_encrypt_backup", 0); // reset value so we don't encrypt every subsequent backup
1241 if (!PartitionManager.stop_backup.get_value()) {
1242 if (ret == false)
1243 ret = 1; // 1 for failure
1244 else
1245 ret = 0; // 0 for success
1246 DataManager::SetValue("tw_cancel_backup", 0);
1247 } else {
1248 DataManager::SetValue("tw_cancel_backup", 1);
1249 gui_msg("backup_cancel=Backup Cancelled");
1250 ret = 0;
1251 }
bigbiffce8f83c2015-12-12 18:30:21 -05001252 } else {
that3f7b1ac2014-12-30 11:30:13 +01001253 operation_end(1);
1254 return -1;
that3f7b1ac2014-12-30 11:30:13 +01001255 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001256 DataManager::SetValue(TW_BACKUP_NAME, auto_gen);
that3f7b1ac2014-12-30 11:30:13 +01001257 } else if (arg == "restore") {
1258 string Restore_Name;
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001259 int gui_adb_backup;
1260
that3f7b1ac2014-12-30 11:30:13 +01001261 DataManager::GetValue("tw_restore", Restore_Name);
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001262 DataManager::GetValue("tw_enable_adb_backup", gui_adb_backup);
1263 if (gui_adb_backup) {
1264 DataManager::SetValue("tw_operation_state", 1);
1265 if (TWFunc::stream_adb_backup(Restore_Name) == 0)
1266 ret = 0; // success
1267 else
1268 ret = 1; // failure
1269 DataManager::SetValue("tw_enable_adb_backup", 0);
1270 ret = 0; // assume success???
1271 } else {
1272 if (PartitionManager.Run_Restore(Restore_Name))
1273 ret = 0; // success
1274 else
1275 ret = 1; // failure
1276 }
that3f7b1ac2014-12-30 11:30:13 +01001277 } else {
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001278 operation_end(1); // invalid arg specified, fail
bigbiff7abc5fe2015-01-17 16:53:12 -05001279 return -1;
1280 }
that73a52952015-01-28 23:07:34 +01001281 operation_end(ret);
bigbiff7abc5fe2015-01-17 16:53:12 -05001282 return ret;
1283 }
1284 return 0;
1285}
1286
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001287int GUIAction::cancelbackup(std::string arg __unused) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001288 if (simulate) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001289 PartitionManager.stop_backup.set_value(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001290 }
1291 else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001292 int op_status = PartitionManager.Cancel_Backup();
1293 if (op_status != 0)
1294 op_status = 1; // failure
bigbiff7abc5fe2015-01-17 16:53:12 -05001295 }
1296
1297 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001298}
Dees_Troy51a0e822012-09-05 15:24:24 -04001299
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001300int GUIAction::fixcontexts(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001301{
that73a52952015-01-28 23:07:34 +01001302 int op_status = 0;
1303
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001304 operation_start("Fix Contexts");
1305 LOGINFO("fix contexts started!\n");
that3f7b1ac2014-12-30 11:30:13 +01001306 if (simulate) {
1307 simulate_progress_bar();
1308 } else {
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001309 op_status = PartitionManager.Fix_Contexts();
that3f7b1ac2014-12-30 11:30:13 +01001310 if (op_status != 0)
1311 op_status = 1; // failure
that3f7b1ac2014-12-30 11:30:13 +01001312 }
that73a52952015-01-28 23:07:34 +01001313 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001314 return 0;
1315}
1316
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001317int GUIAction::fixpermissions(std::string arg)
1318{
1319 return fixcontexts(arg);
1320}
1321
that3f7b1ac2014-12-30 11:30:13 +01001322int GUIAction::dd(std::string arg)
1323{
1324 operation_start("imaging");
1325
1326 if (simulate) {
1327 simulate_progress_bar();
1328 } else {
1329 string cmd = "dd " + arg;
1330 TWFunc::Exec_Cmd(cmd);
1331 }
1332 operation_end(0);
1333 return 0;
1334}
1335
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001336int GUIAction::partitionsd(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001337{
1338 operation_start("Partition SD Card");
1339 int ret_val = 0;
1340
1341 if (simulate) {
1342 simulate_progress_bar();
1343 } else {
1344 int allow_partition;
1345 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1346 if (allow_partition == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001347 gui_err("no_real_sdcard=This device does not have a real SD Card! Aborting!");
that3f7b1ac2014-12-30 11:30:13 +01001348 } else {
1349 if (!PartitionManager.Partition_SDCard())
1350 ret_val = 1; // failed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001351 }
that3f7b1ac2014-12-30 11:30:13 +01001352 }
1353 operation_end(ret_val);
1354 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001355
that3f7b1ac2014-12-30 11:30:13 +01001356}
Dees_Troy51a0e822012-09-05 15:24:24 -04001357
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001358int GUIAction::installhtcdumlock(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001359{
1360 operation_start("Install HTC Dumlock");
1361 if (simulate) {
1362 simulate_progress_bar();
1363 } else
1364 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -04001365
that3f7b1ac2014-12-30 11:30:13 +01001366 operation_end(0);
1367 return 0;
1368}
Dees_Troy51a0e822012-09-05 15:24:24 -04001369
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001370int GUIAction::htcdumlockrestoreboot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001371{
1372 operation_start("HTC Dumlock Restore Boot");
1373 if (simulate) {
1374 simulate_progress_bar();
1375 } else
1376 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001377
that3f7b1ac2014-12-30 11:30:13 +01001378 operation_end(0);
1379 return 0;
1380}
Dees_Troy51a0e822012-09-05 15:24:24 -04001381
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001382int GUIAction::htcdumlockreflashrecovery(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001383{
1384 operation_start("HTC Dumlock Reflash Recovery");
1385 if (simulate) {
1386 simulate_progress_bar();
1387 } else
1388 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001389
that3f7b1ac2014-12-30 11:30:13 +01001390 operation_end(0);
1391 return 0;
1392}
Dees_Troy51a0e822012-09-05 15:24:24 -04001393
that3f7b1ac2014-12-30 11:30:13 +01001394int GUIAction::cmd(std::string arg)
1395{
1396 int op_status = 0;
1397
1398 operation_start("Command");
1399 LOGINFO("Running command: '%s'\n", arg.c_str());
1400 if (simulate) {
1401 simulate_progress_bar();
1402 } else {
1403 op_status = TWFunc::Exec_Cmd(arg);
1404 if (op_status != 0)
1405 op_status = 1;
1406 }
1407
1408 operation_end(op_status);
1409 return 0;
1410}
1411
1412int GUIAction::terminalcommand(std::string arg)
1413{
1414 int op_status = 0;
1415 string cmdpath, command;
1416
1417 DataManager::GetValue("tw_terminal_location", cmdpath);
1418 operation_start("CommandOutput");
1419 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
1420 if (simulate) {
1421 simulate_progress_bar();
1422 operation_end(op_status);
Matt Mower5aa29ab2015-02-25 23:50:55 -06001423 } else if (arg == "exit") {
1424 LOGINFO("Exiting terminal\n");
1425 operation_end(op_status);
1426 page("main");
that3f7b1ac2014-12-30 11:30:13 +01001427 } else {
1428 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
1429 LOGINFO("Actual command is: '%s'\n", command.c_str());
that3f7b1ac2014-12-30 11:30:13 +01001430 DataManager::SetValue("tw_terminal_state", 1);
1431 DataManager::SetValue("tw_background_thread_running", 1);
thatc6085482015-01-09 22:12:43 +01001432 FILE* fp;
1433 char line[512];
1434
1435 fp = popen(command.c_str(), "r");
1436 if (fp == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001437 LOGERR("Error opening command to run (%s).\n", strerror(errno));
thatc6085482015-01-09 22:12:43 +01001438 } else {
Matt Mower23d8aae2017-01-06 14:30:33 -06001439 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1;
thatc6085482015-01-09 22:12:43 +01001440 struct timeval timeout;
1441 fd_set fdset;
1442
Matt Mowera8a89d12016-12-30 18:10:37 -06001443 while (keep_going)
thatc6085482015-01-09 22:12:43 +01001444 {
1445 FD_ZERO(&fdset);
1446 FD_SET(fd, &fdset);
1447 timeout.tv_sec = 0;
1448 timeout.tv_usec = 400000;
1449 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1450 if (has_data == 0) {
1451 // Timeout reached
1452 DataManager::GetValue("tw_terminal_state", check);
1453 if (check == 0) {
1454 keep_going = 0;
1455 }
1456 } else if (has_data < 0) {
1457 // End of execution
1458 keep_going = 0;
1459 } else {
1460 // Try to read output
Matt Mowera8a89d12016-12-30 18:10:37 -06001461 if (fgets(line, sizeof(line), fp) != NULL)
thatc6085482015-01-09 22:12:43 +01001462 gui_print("%s", line); // Display output
1463 else
1464 keep_going = 0; // Done executing
1465 }
1466 }
1467 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -04001468 }
thatc6085482015-01-09 22:12:43 +01001469 DataManager::SetValue("tw_operation_status", 0);
1470 DataManager::SetValue("tw_operation_state", 1);
1471 DataManager::SetValue("tw_terminal_state", 0);
1472 DataManager::SetValue("tw_background_thread_running", 0);
1473 DataManager::SetValue(TW_ACTION_BUSY, 0);
that3f7b1ac2014-12-30 11:30:13 +01001474 }
1475 return 0;
1476}
1477
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001478int GUIAction::killterminal(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001479{
that3f7b1ac2014-12-30 11:30:13 +01001480 LOGINFO("Sending kill command...\n");
1481 operation_start("KillCommand");
1482 DataManager::SetValue("tw_operation_status", 0);
1483 DataManager::SetValue("tw_operation_state", 1);
1484 DataManager::SetValue("tw_terminal_state", 0);
1485 DataManager::SetValue("tw_background_thread_running", 0);
1486 DataManager::SetValue(TW_ACTION_BUSY, 0);
1487 return 0;
1488}
1489
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001490int GUIAction::reinjecttwrp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001491{
1492 int op_status = 0;
1493 operation_start("ReinjectTWRP");
Ethan Yonker74db1572015-10-28 12:44:49 -05001494 gui_msg("injecttwrp=Injecting TWRP into boot image...");
that3f7b1ac2014-12-30 11:30:13 +01001495 if (simulate) {
1496 simulate_progress_bar();
1497 } else {
1498 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Ethan Yonker74db1572015-10-28 12:44:49 -05001499 gui_msg("done=Done.");
that3f7b1ac2014-12-30 11:30:13 +01001500 }
1501
1502 operation_end(op_status);
1503 return 0;
1504}
1505
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001506int GUIAction::checkbackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001507{
1508 int op_status = 0;
1509
1510 operation_start("CheckBackupName");
1511 if (simulate) {
1512 simulate_progress_bar();
1513 } else {
Ethan Yonker53796e72019-01-11 22:49:52 -06001514 string Backup_Name;
1515 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
1516 op_status = PartitionManager.Check_Backup_Name(Backup_Name, true, true);
that3f7b1ac2014-12-30 11:30:13 +01001517 if (op_status != 0)
1518 op_status = 1;
1519 }
1520
1521 operation_end(op_status);
1522 return 0;
1523}
1524
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001525int GUIAction::decrypt(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001526{
1527 int op_status = 0;
1528
1529 operation_start("Decrypt");
1530 if (simulate) {
1531 simulate_progress_bar();
1532 } else {
1533 string Password;
1534 DataManager::GetValue("tw_crypto_password", Password);
bigbiffadc599e2020-05-28 19:36:30 +00001535 op_status = PartitionManager.Decrypt_Device(Password);
that3f7b1ac2014-12-30 11:30:13 +01001536 if (op_status != 0)
1537 op_status = 1;
1538 else {
bigbiffadc599e2020-05-28 19:36:30 +00001539
that3f7b1ac2014-12-30 11:30:13 +01001540 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1541
Ethan Yonkercf50da52015-01-12 21:59:07 -06001542 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001543
Ethan Yonkercf50da52015-01-12 21:59:07 -06001544 // Check for a custom theme and load it if exists
1545 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1546 if (has_datamedia != 0) {
1547 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06001548 LOGINFO("Failed to get default contexts and file mode for storage files.\n");
Ethan Yonkercf50da52015-01-12 21:59:07 -06001549 } else {
1550 LOGINFO("Got default contexts and file mode for storage files.\n");
that3f7b1ac2014-12-30 11:30:13 +01001551 }
1552 }
Ethan Yonker66a19492015-12-10 10:19:45 -06001553 PartitionManager.Decrypt_Adopted();
that3f7b1ac2014-12-30 11:30:13 +01001554 }
1555 }
1556
1557 operation_end(op_status);
1558 return 0;
1559}
1560
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001561int GUIAction::adbsideload(std::string arg __unused)
thatcc8ddca2015-01-03 01:59:36 +01001562{
1563 operation_start("Sideload");
1564 if (simulate) {
1565 simulate_progress_bar();
1566 operation_end(0);
1567 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001568 gui_msg("start_sideload=Starting ADB sideload feature...");
thatc6085482015-01-09 22:12:43 +01001569 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
1570
1571 // wait for the adb connection
bigbiffd58ba182020-03-23 10:02:29 -04001572 // int ret = apply_from_adb("/", &sideload_child_pid);
1573 Device::BuiltinAction reboot_action = Device::REBOOT_BOOTLOADER;
1574 int ret = ApplyFromAdb("/", &reboot_action);
thatc6085482015-01-09 22:12:43 +01001575 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
1576
1577 if (ret != 0) {
1578 if (ret == -2)
Ethan Yonker74db1572015-10-28 12:44:49 -05001579 gui_msg("need_new_adb=You need adb 1.0.32 or newer to sideload to this device.");
thatc6085482015-01-09 22:12:43 +01001580 ret = 1; // failure
1581 } else {
1582 int wipe_cache = 0;
1583 int wipe_dalvik = 0;
1584 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
1585
1586 if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) {
1587 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1588 PartitionManager.Wipe_By_Path("/cache");
1589 if (wipe_dalvik)
1590 PartitionManager.Wipe_Dalvik_Cache();
1591 } else {
1592 ret = 1; // failure
1593 }
thatcc8ddca2015-01-03 01:59:36 +01001594 }
thatc6085482015-01-09 22:12:43 +01001595 if (sideload_child_pid) {
1596 LOGINFO("Signaling child sideload process to exit.\n");
1597 struct stat st;
1598 // Calling stat() on this magic filename signals the minadbd
1599 // subprocess to shut down.
1600 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1601 int status;
1602 LOGINFO("Waiting for child sideload process to exit.\n");
1603 waitpid(sideload_child_pid, &status, 0);
1604 }
Dees Troy28a85d22015-04-28 02:03:16 +00001605 property_set("ctl.start", "adbd");
thatc6085482015-01-09 22:12:43 +01001606 TWFunc::Toggle_MTP(mtp_was_enabled);
1607 reinject_after_flash();
1608 operation_end(ret);
thatcc8ddca2015-01-03 01:59:36 +01001609 }
that3f7b1ac2014-12-30 11:30:13 +01001610 return 0;
1611}
1612
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001613int GUIAction::adbsideloadcancel(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001614{
that3f7b1ac2014-12-30 11:30:13 +01001615 struct stat st;
1616 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
Ethan Yonker74db1572015-10-28 12:44:49 -05001617 gui_msg("cancel_sideload=Cancelling ADB sideload...");
thatcc8ddca2015-01-03 01:59:36 +01001618 LOGINFO("Signaling child sideload process to exit.\n");
1619 // Calling stat() on this magic filename signals the minadbd
1620 // subprocess to shut down.
1621 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1622 if (!sideload_child_pid) {
1623 LOGERR("Unable to get child ID\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001624 return 0;
1625 }
thatcc8ddca2015-01-03 01:59:36 +01001626 ::sleep(1);
1627 LOGINFO("Killing child sideload process.\n");
1628 kill(sideload_child_pid, SIGTERM);
1629 int status;
1630 LOGINFO("Waiting for child sideload process to exit.\n");
1631 waitpid(sideload_child_pid, &status, 0);
1632 sideload_child_pid = 0;
that3f7b1ac2014-12-30 11:30:13 +01001633 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
1634 return 0;
1635}
1636
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001637int GUIAction::openrecoveryscript(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001638{
1639 operation_start("OpenRecoveryScript");
1640 if (simulate) {
1641 simulate_progress_bar();
Ethan Yonker24e7eb72015-01-03 16:13:06 -06001642 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001643 } else {
that10ae24f2015-12-26 20:53:51 +01001644 int op_status = OpenRecoveryScript::Run_OpenRecoveryScript_Action();
Ethan Yonkere1abe612015-06-09 11:09:32 -05001645 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001646 }
1647 return 0;
1648}
1649
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001650int GUIAction::installsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001651{
1652 int op_status = 0;
1653
1654 operation_start("Install SuperSU");
1655 if (simulate) {
1656 simulate_progress_bar();
1657 } else {
Ethan Yonkerfa67cbf2018-07-20 12:22:33 -05001658 LOGERR("Installing SuperSU was deprecated from TWRP.\n");
that3f7b1ac2014-12-30 11:30:13 +01001659 }
1660
1661 operation_end(op_status);
1662 return 0;
1663}
1664
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001665int GUIAction::fixsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001666{
1667 int op_status = 0;
1668
1669 operation_start("Fixing Superuser Permissions");
1670 if (simulate) {
1671 simulate_progress_bar();
1672 } else {
1673 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1674 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
1675 }
1676
1677 operation_end(op_status);
1678 return 0;
1679}
1680
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001681int GUIAction::decrypt_backup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001682{
1683 int op_status = 0;
1684
1685 operation_start("Try Restore Decrypt");
1686 if (simulate) {
1687 simulate_progress_bar();
1688 } else {
1689 string Restore_Path, Filename, Password;
1690 DataManager::GetValue("tw_restore", Restore_Path);
1691 Restore_Path += "/";
1692 DataManager::GetValue("tw_restore_password", Password);
1693 TWFunc::SetPerformanceMode(true);
1694 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1695 op_status = 0; // success
1696 else
1697 op_status = 1; // fail
1698 TWFunc::SetPerformanceMode(false);
1699 }
1700
1701 operation_end(op_status);
1702 return 0;
1703}
1704
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001705int GUIAction::repair(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001706{
1707 int op_status = 0;
1708
1709 operation_start("Repair Partition");
1710 if (simulate) {
1711 simulate_progress_bar();
1712 } else {
1713 string part_path;
1714 DataManager::GetValue("tw_partition_mount_point", part_path);
1715 if (PartitionManager.Repair_By_Path(part_path, true)) {
1716 op_status = 0; // success
1717 } else {
that3f7b1ac2014-12-30 11:30:13 +01001718 op_status = 1; // fail
1719 }
1720 }
1721
1722 operation_end(op_status);
1723 return 0;
1724}
1725
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001726int GUIAction::resize(std::string arg __unused)
Ethan Yonkera2719152015-05-28 09:44:41 -05001727{
1728 int op_status = 0;
1729
1730 operation_start("Resize Partition");
1731 if (simulate) {
1732 simulate_progress_bar();
1733 } else {
1734 string part_path;
1735 DataManager::GetValue("tw_partition_mount_point", part_path);
1736 if (PartitionManager.Resize_By_Path(part_path, true)) {
1737 op_status = 0; // success
1738 } else {
Ethan Yonkera2719152015-05-28 09:44:41 -05001739 op_status = 1; // fail
1740 }
1741 }
1742
1743 operation_end(op_status);
1744 return 0;
1745}
1746
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001747int GUIAction::changefilesystem(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001748{
1749 int op_status = 0;
1750
1751 operation_start("Change File System");
1752 if (simulate) {
1753 simulate_progress_bar();
1754 } else {
1755 string part_path, file_system;
1756 DataManager::GetValue("tw_partition_mount_point", part_path);
1757 DataManager::GetValue("tw_action_new_file_system", file_system);
1758 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1759 op_status = 0; // success
1760 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001761 gui_err("change_fs_err=Error changing file system.");
that3f7b1ac2014-12-30 11:30:13 +01001762 op_status = 1; // fail
1763 }
1764 }
1765 PartitionManager.Update_System_Details();
1766 operation_end(op_status);
1767 return 0;
1768}
1769
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001770int GUIAction::startmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001771{
1772 int op_status = 0;
1773
1774 operation_start("Start MTP");
1775 if (PartitionManager.Enable_MTP())
1776 op_status = 0; // success
1777 else
1778 op_status = 1; // fail
1779
1780 operation_end(op_status);
1781 return 0;
1782}
1783
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001784int GUIAction::stopmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001785{
1786 int op_status = 0;
1787
1788 operation_start("Stop MTP");
1789 if (PartitionManager.Disable_MTP())
1790 op_status = 0; // success
1791 else
1792 op_status = 1; // fail
1793
1794 operation_end(op_status);
1795 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001796}
1797
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001798int GUIAction::flashimage(std::string arg __unused)
Ethan Yonker96af84a2015-01-05 14:58:36 -06001799{
1800 int op_status = 0;
1801
1802 operation_start("Flash Image");
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001803 string path, filename;
1804 DataManager::GetValue("tw_zip_location", path);
1805 DataManager::GetValue("tw_file", filename);
Ethan Yonkere080c1f2016-09-19 13:50:25 -05001806 if (PartitionManager.Flash_Image(path, filename))
Ethan Yonker96af84a2015-01-05 14:58:36 -06001807 op_status = 0; // success
1808 else
1809 op_status = 1; // fail
1810
1811 operation_end(op_status);
1812 return 0;
1813}
1814
that10ae24f2015-12-26 20:53:51 +01001815int GUIAction::twcmd(std::string arg)
1816{
1817 operation_start("TWRP CLI Command");
1818 if (simulate)
1819 simulate_progress_bar();
1820 else
1821 OpenRecoveryScript::Run_CLI_Command(arg.c_str());
1822 operation_end(0);
1823 return 0;
1824}
1825
Dees_Troy51a0e822012-09-05 15:24:24 -04001826int GUIAction::getKeyByName(std::string key)
1827{
that8834a0f2016-01-05 23:29:30 +01001828 if (key == "home") return KEY_HOMEPAGE; // note: KEY_HOME is cursor movement (like KEY_END)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001829 else if (key == "menu") return KEY_MENU;
1830 else if (key == "back") return KEY_BACK;
1831 else if (key == "search") return KEY_SEARCH;
1832 else if (key == "voldown") return KEY_VOLUMEDOWN;
1833 else if (key == "volup") return KEY_VOLUMEUP;
1834 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001835 int ret_val;
1836 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1837 if (!ret_val)
1838 return KEY_POWER;
1839 else
1840 return ret_val;
1841 }
1842
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001843 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001844}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001845
1846int GUIAction::checkpartitionlifetimewrites(std::string arg)
1847{
1848 int op_status = 0;
1849 TWPartition* sys = PartitionManager.Find_Partition_By_Path(arg);
1850
1851 operation_start("Check Partition Lifetime Writes");
1852 if (sys) {
1853 if (sys->Check_Lifetime_Writes() != 0)
1854 DataManager::SetValue("tw_lifetime_writes", 1);
1855 else
1856 DataManager::SetValue("tw_lifetime_writes", 0);
1857 op_status = 0; // success
1858 } else {
1859 DataManager::SetValue("tw_lifetime_writes", 1);
1860 op_status = 1; // fail
1861 }
1862
1863 operation_end(op_status);
1864 return 0;
1865}
1866
1867int GUIAction::mountsystemtoggle(std::string arg)
1868{
1869 int op_status = 0;
Captain Throwback9d6feb52018-07-27 10:05:24 -04001870 bool remount_system = PartitionManager.Is_Mounted_By_Path(PartitionManager.Get_Android_Root_Path());
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001871 bool remount_vendor = PartitionManager.Is_Mounted_By_Path("/vendor");
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001872
1873 operation_start("Toggle System Mount");
Captain Throwback9d6feb52018-07-27 10:05:24 -04001874 if (!PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001875 op_status = 1; // fail
1876 } else {
Captain Throwback9d6feb52018-07-27 10:05:24 -04001877 TWPartition* Part = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001878 if (Part) {
Ethan Yonkerd6966f42015-05-30 14:52:16 -05001879 if (arg == "0") {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001880 DataManager::SetValue("tw_mount_system_ro", 0);
1881 Part->Change_Mount_Read_Only(false);
1882 } else {
1883 DataManager::SetValue("tw_mount_system_ro", 1);
1884 Part->Change_Mount_Read_Only(true);
1885 }
1886 if (remount_system) {
1887 Part->Mount(true);
1888 }
1889 op_status = 0; // success
1890 } else {
1891 op_status = 1; // fail
1892 }
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001893 Part = PartitionManager.Find_Partition_By_Path("/vendor");
1894 if (Part) {
1895 if (arg == "0") {
1896 Part->Change_Mount_Read_Only(false);
1897 } else {
1898 Part->Change_Mount_Read_Only(true);
1899 }
1900 if (remount_vendor) {
1901 Part->Mount(true);
1902 }
1903 op_status = 0; // success
1904 } else {
1905 op_status = 1; // fail
1906 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001907 }
1908
1909 operation_end(op_status);
1910 return 0;
1911}
Ethan Yonker74db1572015-10-28 12:44:49 -05001912
1913int GUIAction::setlanguage(std::string arg __unused)
1914{
1915 int op_status = 0;
1916
1917 operation_start("Set Language");
1918 PageManager::LoadLanguage(DataManager::GetStrValue("tw_language"));
1919 PageManager::RequestReload();
1920 op_status = 0; // success
1921
1922 operation_end(op_status);
1923 return 0;
1924}
Ethan Yonker1b190162016-12-05 15:25:19 -06001925
Matt Mower9472ba12016-01-20 18:12:47 -06001926int GUIAction::togglebacklight(std::string arg __unused)
1927{
1928 blankTimer.toggleBlank();
1929 return 0;
1930}
1931
Ethan Yonker1b190162016-12-05 15:25:19 -06001932int GUIAction::setbootslot(std::string arg)
1933{
1934 operation_start("Set Boot Slot");
1935 if (!simulate)
Ethan Yonker1b190162016-12-05 15:25:19 -06001936 PartitionManager.Set_Active_Slot(arg);
Matt Mower029a82d2017-01-08 13:12:38 -06001937 else
Ethan Yonker1b190162016-12-05 15:25:19 -06001938 simulate_progress_bar();
1939 operation_end(0);
1940 return 0;
1941}
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001942
1943int GUIAction::checkforapp(std::string arg __unused)
1944{
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001945 operation_start("Check for TWRP App");
1946 if (!simulate)
1947 {
1948 string sdkverstr = TWFunc::System_Property_Get("ro.build.version.sdk");
1949 int sdkver = 0;
1950 if (!sdkverstr.empty()) {
1951 sdkver = atoi(sdkverstr.c_str());
1952 }
1953 if (sdkver <= 13) {
1954 if (sdkver == 0)
1955 LOGINFO("Unable to read sdk version from build prop\n");
1956 else
1957 LOGINFO("SDK version too low for TWRP app (%i < 14)\n", sdkver);
Ethan Yonker64c5c0a2017-03-06 12:42:54 -06001958 DataManager::SetValue("tw_app_install_status", 1); // 0 = no status, 1 = not installed, 2 = already installed or do not install
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001959 goto exit;
1960 }
Ethan Yonker76bbd3a2019-05-10 10:50:04 -05001961 if (TWFunc::Is_TWRP_App_In_System()) {
1962 DataManager::SetValue("tw_app_install_status", 2); // 0 = no status, 1 = not installed, 2 = already installed or do not install
1963 goto exit;
Ethan Yonker5e1a7f92017-01-18 16:44:54 -06001964 }
1965 if (PartitionManager.Mount_By_Path("/data", false)) {
Ethan Yonker5128d292017-02-04 19:52:56 -06001966 const char parent_path[] = "/data/app";
1967 const char app_prefix[] = "me.twrp.twrpapp-";
1968 DIR *d = opendir(parent_path);
1969 if (d) {
1970 struct dirent *p;
1971 while ((p = readdir(d))) {
1972 if (p->d_type != DT_DIR || strlen(p->d_name) < strlen(app_prefix) || strncmp(p->d_name, app_prefix, strlen(app_prefix)))
1973 continue;
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001974 closedir(d);
Ethan Yonker5128d292017-02-04 19:52:56 -06001975 LOGINFO("App found at '%s/%s'\n", parent_path, p->d_name);
Ethan Yonker64c5c0a2017-03-06 12:42:54 -06001976 DataManager::SetValue("tw_app_install_status", 2); // 0 = no status, 1 = not installed, 2 = already installed or do not install
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001977 goto exit;
1978 }
Ethan Yonker5128d292017-02-04 19:52:56 -06001979 closedir(d);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001980 }
Ethan Yonker64c5c0a2017-03-06 12:42:54 -06001981 } else {
1982 LOGINFO("Data partition cannot be mounted during app check\n");
1983 DataManager::SetValue("tw_app_install_status", 2); // 0 = no status, 1 = not installed, 2 = already installed or do not install
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001984 }
1985 } else
1986 simulate_progress_bar();
1987 LOGINFO("App not installed\n");
1988 DataManager::SetValue("tw_app_install_status", 1); // 0 = no status, 1 = not installed, 2 = already installed
1989exit:
1990 operation_end(0);
1991 return 0;
1992}
1993
1994int GUIAction::installapp(std::string arg __unused)
1995{
1996 int op_status = 1;
1997 operation_start("Install TWRP App");
1998 if (!simulate)
1999 {
2000 if (DataManager::GetIntValue("tw_mount_system_ro") > 0 || DataManager::GetIntValue("tw_app_install_system") == 0) {
2001 if (PartitionManager.Mount_By_Path("/data", true)) {
2002 string install_path = "/data/app";
2003 string context = "u:object_r:apk_data_file:s0";
2004 if (!TWFunc::Path_Exists(install_path)) {
2005 if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
2006 LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno));
2007 goto exit;
2008 }
2009 if (chown(install_path.c_str(), 1000, 1000)) {
2010 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
2011 goto exit;
2012 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002013 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002014 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2015 goto exit;
2016 }
2017 }
2018 install_path += "/me.twrp.twrpapp-1";
2019 if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
2020 LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno));
2021 goto exit;
2022 }
2023 if (chown(install_path.c_str(), 1000, 1000)) {
2024 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
2025 goto exit;
2026 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002027 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002028 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2029 goto exit;
2030 }
2031 install_path += "/base.apk";
bigbiffad58e1b2020-07-06 20:24:34 -04002032 if (TWFunc::copy_file("/system/bin/me.twrp.twrpapp.apk", install_path, 0644)) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002033 LOGERR("Error copying apk file\n");
2034 goto exit;
2035 }
2036 if (chown(install_path.c_str(), 1000, 1000)) {
2037 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
2038 goto exit;
2039 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002040 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002041 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2042 goto exit;
2043 }
2044 sync();
2045 sync();
2046 }
2047 } else {
Captain Throwback9d6feb52018-07-27 10:05:24 -04002048 if (PartitionManager.Mount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) {
2049 string base_path = PartitionManager.Get_Android_Root_Path();
2050 if (TWFunc::Path_Exists(PartitionManager.Get_Android_Root_Path() + "/system"))
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002051 base_path += "/system"; // For devices with system as a root file system (e.g. Pixel)
2052 string install_path = base_path + "/priv-app";
2053 string context = "u:object_r:system_file:s0";
2054 if (!TWFunc::Path_Exists(install_path))
2055 install_path = base_path + "/app";
2056 if (TWFunc::Path_Exists(install_path)) {
2057 install_path += "/twrpapp";
2058 LOGINFO("Installing app to '%s'\n", install_path.c_str());
2059 if (mkdir(install_path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0) {
Ethan Yonker4767caf2017-01-11 10:45:04 -06002060 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002061 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2062 goto exit;
2063 }
2064 install_path += "/me.twrp.twrpapp.apk";
bigbiffad58e1b2020-07-06 20:24:34 -04002065 if (TWFunc::copy_file("/system/bin/me.twrp.twrpapp.apk", install_path, 0644)) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002066 LOGERR("Error copying apk file\n");
2067 goto exit;
2068 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002069 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002070 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2071 goto exit;
2072 }
Stefan Tauner6ad14572020-01-11 22:28:53 +01002073
2074 // System apps require their permissions to be pre-set via an XML file in /etc/permissions
2075 string permission_path = base_path + "/etc/permissions/privapp-permissions-twrpapp.xml";
bigbiffad58e1b2020-07-06 20:24:34 -04002076 if (TWFunc::copy_file("/system/bin/privapp-permissions-twrpapp.xml", permission_path, 0644)) {
Stefan Tauner6ad14572020-01-11 22:28:53 +01002077 LOGERR("Error copying permission file\n");
2078 goto exit;
2079 }
2080 if (chown(permission_path.c_str(), 1000, 1000)) {
2081 LOGERR("chown %s error: %s\n", permission_path.c_str(), strerror(errno));
2082 goto exit;
2083 }
2084 if (setfilecon(permission_path.c_str(), (security_context_t)context.c_str()) < 0) {
2085 LOGERR("setfilecon %s error: %s\n", permission_path.c_str(), strerror(errno));
2086 goto exit;
2087 }
2088
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002089 sync();
2090 sync();
Captain Throwback9d6feb52018-07-27 10:05:24 -04002091 PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002092 op_status = 0;
2093 } else {
2094 LOGERR("Error making app directory '%s': %s\n", strerror(errno));
2095 }
2096 }
2097 }
2098 }
2099 } else
2100 simulate_progress_bar();
2101exit:
2102 operation_end(0);
2103 return 0;
2104}
Ethan Yonker53796e72019-01-11 22:49:52 -06002105
Ethan Yonker76bbd3a2019-05-10 10:50:04 -05002106int GUIAction::uninstalltwrpsystemapp(std::string arg __unused)
2107{
2108 int op_status = 1;
2109 operation_start("Uninstall TWRP System App");
2110 if (!simulate)
2111 {
2112 int Mount_System_RO = DataManager::GetIntValue("tw_mount_system_ro");
2113 TWPartition* Part = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path());
2114 if (!Part) {
2115 LOGERR("Unabled to find system partition.\n");
2116 goto exit;
2117 }
2118 if (!Part->UnMount(true)) {
2119 goto exit;
2120 }
2121 if (Mount_System_RO > 0) {
2122 DataManager::SetValue("tw_mount_system_ro", 0);
2123 Part->Change_Mount_Read_Only(false);
2124 }
2125 if (Part->Mount(true)) {
2126 string base_path = PartitionManager.Get_Android_Root_Path();
2127 if (TWFunc::Path_Exists(PartitionManager.Get_Android_Root_Path() + "/system"))
2128 base_path += "/system"; // For devices with system as a root file system (e.g. Pixel)
2129 string uninstall_path = base_path + "/priv-app";
2130 if (!TWFunc::Path_Exists(uninstall_path))
2131 uninstall_path = base_path + "/app";
2132 uninstall_path += "/twrpapp";
2133 if (TWFunc::Path_Exists(uninstall_path)) {
2134 LOGINFO("Uninstalling TWRP App from '%s'\n", uninstall_path.c_str());
2135 if (TWFunc::removeDir(uninstall_path, false) == 0) {
2136 sync();
2137 op_status = 0;
2138 DataManager::SetValue("tw_app_installed_in_system", 0);
2139 DataManager::SetValue("tw_app_install_status", 0);
2140 } else {
2141 LOGERR("Unable to remove TWRP app from system.\n");
2142 }
2143 } else {
2144 LOGINFO("didn't find TWRP app in '%s'\n", uninstall_path.c_str());
2145 }
2146 }
2147 Part->UnMount(true);
2148 if (Mount_System_RO > 0) {
2149 DataManager::SetValue("tw_mount_system_ro", Mount_System_RO);
2150 Part->Change_Mount_Read_Only(true);
2151 }
2152 } else
2153 simulate_progress_bar();
2154exit:
2155 operation_end(0);
2156 return 0;
2157}
2158
Ethan Yonker53796e72019-01-11 22:49:52 -06002159int GUIAction::repackimage(std::string arg __unused)
2160{
2161 int op_status = 1;
bigbiffad58e1b2020-07-06 20:24:34 -04002162 twrpRepacker repacker;
2163
Ethan Yonker53796e72019-01-11 22:49:52 -06002164 operation_start("Repack Image");
2165 if (!simulate)
2166 {
2167 std::string path = DataManager::GetStrValue("tw_filename");
2168 Repack_Options_struct Repack_Options;
2169 Repack_Options.Disable_Verity = false;
2170 Repack_Options.Disable_Force_Encrypt = false;
2171 Repack_Options.Backup_First = DataManager::GetIntValue("tw_repack_backup_first") != 0;
2172 if (DataManager::GetIntValue("tw_repack_kernel") == 1)
2173 Repack_Options.Type = REPLACE_KERNEL;
2174 else
2175 Repack_Options.Type = REPLACE_RAMDISK;
bigbiffad58e1b2020-07-06 20:24:34 -04002176 if (!repacker.Repack_Image_And_Flash(path, Repack_Options))
Ethan Yonker53796e72019-01-11 22:49:52 -06002177 goto exit;
2178 } else
2179 simulate_progress_bar();
2180 op_status = 0;
2181exit:
2182 operation_end(op_status);
2183 return 0;
2184}
2185
2186int GUIAction::fixabrecoverybootloop(std::string arg __unused)
2187{
2188 int op_status = 1;
bigbiffad58e1b2020-07-06 20:24:34 -04002189 twrpRepacker repacker;
2190
Ethan Yonker53796e72019-01-11 22:49:52 -06002191 operation_start("Repack Image");
2192 if (!simulate)
2193 {
bigbiffad58e1b2020-07-06 20:24:34 -04002194 if (!TWFunc::Path_Exists("/system/bin/magiskboot")) {
Ethan Yonker53796e72019-01-11 22:49:52 -06002195 LOGERR("Image repacking tool not present in this TWRP build!");
2196 goto exit;
2197 }
2198 DataManager::SetProgress(0);
2199 TWPartition* part = PartitionManager.Find_Partition_By_Path("/boot");
2200 if (part)
2201 gui_msg(Msg("unpacking_image=Unpacking {1}...")(part->Display_Name));
2202 else {
2203 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")("/boot"));
2204 goto exit;
2205 }
bigbiffad58e1b2020-07-06 20:24:34 -04002206 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 -06002207 goto exit;
2208 DataManager::SetProgress(.25);
2209 gui_msg("fixing_recovery_loop_patch=Patching kernel...");
bigbiffad58e1b2020-07-06 20:24:34 -04002210 std::string command = "cd " REPACK_ORIG_DIR " && /system/bin/magiskboot hexpatch kernel 77616E745F696E697472616D667300 736B69705F696E697472616D667300";
Ethan Yonker53796e72019-01-11 22:49:52 -06002211 if (TWFunc::Exec_Cmd(command) != 0) {
2212 gui_msg(Msg(msg::kError, "fix_recovery_loop_patch_error=Error patching kernel."));
2213 goto exit;
2214 }
2215 std::string header_path = REPACK_ORIG_DIR;
2216 header_path += "header";
2217 if (TWFunc::Path_Exists(header_path)) {
2218 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";
2219 if (TWFunc::Exec_Cmd(command) != 0) {
2220 gui_msg(Msg(msg::kError, "fix_recovery_loop_patch_error=Error patching kernel."));
2221 goto exit;
2222 }
2223 }
2224 DataManager::SetProgress(.5);
2225 gui_msg(Msg("repacking_image=Repacking {1}...")(part->Display_Name));
bigbiffad58e1b2020-07-06 20:24:34 -04002226 command = "cd " REPACK_ORIG_DIR " && /system/bin/magiskboot repack " REPACK_ORIG_DIR "boot.img";
Ethan Yonker53796e72019-01-11 22:49:52 -06002227 if (TWFunc::Exec_Cmd(command) != 0) {
2228 gui_msg(Msg(msg::kError, "repack_error=Error repacking image."));
2229 goto exit;
2230 }
2231 DataManager::SetProgress(.75);
2232 std::string path = REPACK_ORIG_DIR;
2233 std::string file = "new-boot.img";
2234 DataManager::SetValue("tw_flash_partition", "/boot;");
2235 if (!PartitionManager.Flash_Image(path, file)) {
2236 LOGINFO("Error flashing new image\n");
2237 goto exit;
2238 }
2239 DataManager::SetProgress(1);
2240 TWFunc::removeDir(REPACK_ORIG_DIR, false);
2241 } else
2242 simulate_progress_bar();
2243 op_status = 0;
2244exit:
2245 operation_end(op_status);
2246 return 0;
2247}
bigbiffdf8436b2020-08-30 16:22:34 -04002248
2249
2250int GUIAction::enableadb(std::string arg __unused) {
2251 android::base::SetProperty("sys.usb.config", "none");
2252 android::base::SetProperty("sys.usb.config", "adb");
2253 return 0;
2254}
2255
2256int GUIAction::enablefastboot(std::string arg __unused) {
2257 android::base::SetProperty("sys.usb.config", "none");
2258 android::base::SetProperty("sys.usb.config", "fastboot");
2259 return 0;
2260}