blob: 5e842d50cce96211c56118a3fb4bc062131d5a16 [file] [log] [blame]
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06001/*
Dees_Troya13d74f2013-03-24 08:54:55 -05002 Copyright 2013 bigbiff/Dees_Troy TeamWin
3 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
Dees_Troy51a0e822012-09-05 15:24:24 -040018
19#include <stdarg.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <fcntl.h>
24#include <sys/stat.h>
25#include <sys/time.h>
26#include <sys/mman.h>
27#include <sys/types.h>
28#include <sys/ioctl.h>
29#include <linux/input.h>
30#include <time.h>
31#include <unistd.h>
32#include <stdlib.h>
Dees_Troy657c3092012-09-10 20:32:10 -040033#include <sys/wait.h>
Dees_Troy83bd4832013-05-04 12:39:56 +000034#include <dirent.h>
Matt Mower0c88b842017-01-15 16:00:49 -060035#include <private/android_filesystem_config.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040036
37#include <string>
38#include <sstream>
39#include "../partitions.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040040#include "../twrp-functions.hpp"
bigbiffad58e1b2020-07-06 20:24:34 -040041#include "../twrpRepacker.hpp"
Dees_Troy812660f2012-09-20 09:55:17 -040042#include "../openrecoveryscript.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040043
bigbiffd58ba182020-03-23 10:02:29 -040044#include "install/adb_install.h"
45
46#include "fuse_sideload.h"
bigbiff bigbiff8a68c312013-02-10 14:28:30 -050047#include "blanktimer.hpp"
Ethan Yonker8373cfe2017-09-08 06:50:54 -050048#include "../twinstall.h"
Ethan Yonkerfbb43532015-12-28 21:54:50 +010049
Dees_Troy51a0e822012-09-05 15:24:24 -040050extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000051#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040052#include "../variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000053#include "cutils/properties.h"
bigbiffd58ba182020-03-23 10:02:29 -040054#include "install/adb_install.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040055};
bigbiffd58ba182020-03-23 10:02:29 -040056#include "set_metadata.h"
Ethan Yonkerfbb43532015-12-28 21:54:50 +010057#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040058
59#include "rapidxml.hpp"
60#include "objects.hpp"
bigbiffd58ba182020-03-23 10:02:29 -040061#include "tw_atomic.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040062
that3f7b1ac2014-12-30 11:30:13 +010063GUIAction::mapFunc GUIAction::mf;
thatc6085482015-01-09 22:12:43 +010064std::set<string> GUIAction::setActionsRunningInCallerThread;
that3f7b1ac2014-12-30 11:30:13 +010065static string zip_queue[10];
66static int zip_queue_index;
thatcc8ddca2015-01-03 01:59:36 +010067pid_t sideload_child_pid;
that3f7b1ac2014-12-30 11:30:13 +010068
that73a52952015-01-28 23:07:34 +010069static void *ActionThread_work_wrapper(void *data);
70
71class ActionThread
72{
73public:
74 ActionThread();
75 ~ActionThread();
76
77 void threadActions(GUIAction *act);
78 void run(void *data);
79private:
80 friend void *ActionThread_work_wrapper(void*);
81 struct ThreadData
82 {
83 ActionThread *this_;
84 GUIAction *act;
85 ThreadData(ActionThread *this_, GUIAction *act) : this_(this_), act(act) {}
86 };
87
88 pthread_t m_thread;
89 bool m_thread_running;
90 pthread_mutex_t m_act_lock;
91};
92
93static ActionThread action_thread; // for all kinds of longer running actions
94static ActionThread cancel_thread; // for longer running "cancel" actions
thatc6085482015-01-09 22:12:43 +010095
96static void *ActionThread_work_wrapper(void *data)
97{
that73a52952015-01-28 23:07:34 +010098 static_cast<ActionThread::ThreadData*>(data)->this_->run(data);
thatc6085482015-01-09 22:12:43 +010099 return NULL;
100}
101
102ActionThread::ActionThread()
103{
104 m_thread_running = false;
105 pthread_mutex_init(&m_act_lock, NULL);
106}
107
108ActionThread::~ActionThread()
109{
110 pthread_mutex_lock(&m_act_lock);
Matt Mowera8a89d12016-12-30 18:10:37 -0600111 if (m_thread_running) {
thatc6085482015-01-09 22:12:43 +0100112 pthread_mutex_unlock(&m_act_lock);
113 pthread_join(m_thread, NULL);
114 } else {
115 pthread_mutex_unlock(&m_act_lock);
116 }
117 pthread_mutex_destroy(&m_act_lock);
118}
119
that7d3b54f2015-01-09 22:52:51 +0100120void ActionThread::threadActions(GUIAction *act)
thatc6085482015-01-09 22:12:43 +0100121{
122 pthread_mutex_lock(&m_act_lock);
123 if (m_thread_running) {
124 pthread_mutex_unlock(&m_act_lock);
that7d3b54f2015-01-09 22:52:51 +0100125 LOGERR("Another threaded action is already running -- not running %u actions starting with '%s'\n",
126 act->mActions.size(), act->mActions[0].mFunction.c_str());
thatc6085482015-01-09 22:12:43 +0100127 } else {
128 m_thread_running = true;
129 pthread_mutex_unlock(&m_act_lock);
that73a52952015-01-28 23:07:34 +0100130 ThreadData *d = new ThreadData(this, act);
thatc6085482015-01-09 22:12:43 +0100131 pthread_create(&m_thread, NULL, &ActionThread_work_wrapper, d);
132 }
133}
134
135void ActionThread::run(void *data)
136{
137 ThreadData *d = (ThreadData*)data;
138 GUIAction* act = d->act;
139
140 std::vector<GUIAction::Action>::iterator it;
that7d3b54f2015-01-09 22:52:51 +0100141 for (it = act->mActions.begin(); it != act->mActions.end(); ++it)
thatc6085482015-01-09 22:12:43 +0100142 act->doAction(*it);
143
144 pthread_mutex_lock(&m_act_lock);
145 m_thread_running = false;
146 pthread_mutex_unlock(&m_act_lock);
147 delete d;
148}
149
Dees_Troy51a0e822012-09-05 15:24:24 -0400150GUIAction::GUIAction(xml_node<>* node)
Vojtech Bocekede51c52014-02-07 23:58:09 +0100151 : GUIObject(node)
Dees_Troy51a0e822012-09-05 15:24:24 -0400152{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200153 xml_node<>* child;
154 xml_node<>* actions;
155 xml_attribute<>* attr;
Dees_Troy51a0e822012-09-05 15:24:24 -0400156
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200157 if (!node) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400158
that3f7b1ac2014-12-30 11:30:13 +0100159 if (mf.empty()) {
Vojtech Bocekecd89182015-01-15 16:28:54 +0100160#define ADD_ACTION(n) mf[#n] = &GUIAction::n
161#define ADD_ACTION_EX(name, func) mf[name] = &GUIAction::func
thatc6085482015-01-09 22:12:43 +0100162 // These actions will be run in the caller's thread
Vojtech Bocekecd89182015-01-15 16:28:54 +0100163 ADD_ACTION(reboot);
164 ADD_ACTION(home);
165 ADD_ACTION(key);
166 ADD_ACTION(page);
167 ADD_ACTION(reload);
168 ADD_ACTION(readBackup);
169 ADD_ACTION(set);
170 ADD_ACTION(clear);
171 ADD_ACTION(mount);
172 ADD_ACTION(unmount);
173 ADD_ACTION_EX("umount", unmount);
174 ADD_ACTION(restoredefaultsettings);
175 ADD_ACTION(copylog);
176 ADD_ACTION(compute);
177 ADD_ACTION_EX("addsubtract", compute);
178 ADD_ACTION(setguitimezone);
179 ADD_ACTION(overlay);
180 ADD_ACTION(queuezip);
181 ADD_ACTION(cancelzip);
182 ADD_ACTION(queueclear);
183 ADD_ACTION(sleep);
Matt Mower9a2a2052016-05-31 21:31:22 -0500184 ADD_ACTION(sleepcounter);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100185 ADD_ACTION(appenddatetobackupname);
186 ADD_ACTION(generatebackupname);
187 ADD_ACTION(checkpartitionlist);
188 ADD_ACTION(getpartitiondetails);
189 ADD_ACTION(screenshot);
190 ADD_ACTION(setbrightness);
191 ADD_ACTION(fileexists);
192 ADD_ACTION(killterminal);
193 ADD_ACTION(checkbackupname);
194 ADD_ACTION(adbsideloadcancel);
195 ADD_ACTION(fixsu);
196 ADD_ACTION(startmtp);
197 ADD_ACTION(stopmtp);
198 ADD_ACTION(cancelbackup);
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500199 ADD_ACTION(checkpartitionlifetimewrites);
200 ADD_ACTION(mountsystemtoggle);
Ethan Yonker74db1572015-10-28 12:44:49 -0500201 ADD_ACTION(setlanguage);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -0600202 ADD_ACTION(checkforapp);
Matt Mower9472ba12016-01-20 18:12:47 -0600203 ADD_ACTION(togglebacklight);
thatc6085482015-01-09 22:12:43 +0100204
205 // remember actions that run in the caller thread
206 for (mapFunc::const_iterator it = mf.begin(); it != mf.end(); ++it)
207 setActionsRunningInCallerThread.insert(it->first);
208
209 // These actions will run in a separate thread
Vojtech Bocekecd89182015-01-15 16:28:54 +0100210 ADD_ACTION(flash);
211 ADD_ACTION(wipe);
212 ADD_ACTION(refreshsizes);
213 ADD_ACTION(nandroid);
Ethan Yonkerb5fab762016-01-28 14:03:33 -0600214 ADD_ACTION(fixcontexts);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100215 ADD_ACTION(fixpermissions);
216 ADD_ACTION(dd);
217 ADD_ACTION(partitionsd);
218 ADD_ACTION(installhtcdumlock);
219 ADD_ACTION(htcdumlockrestoreboot);
220 ADD_ACTION(htcdumlockreflashrecovery);
221 ADD_ACTION(cmd);
222 ADD_ACTION(terminalcommand);
223 ADD_ACTION(reinjecttwrp);
224 ADD_ACTION(decrypt);
225 ADD_ACTION(adbsideload);
226 ADD_ACTION(openrecoveryscript);
227 ADD_ACTION(installsu);
228 ADD_ACTION(decrypt_backup);
229 ADD_ACTION(repair);
Ethan Yonkera2719152015-05-28 09:44:41 -0500230 ADD_ACTION(resize);
Vojtech Bocekecd89182015-01-15 16:28:54 +0100231 ADD_ACTION(changefilesystem);
232 ADD_ACTION(flashimage);
that10ae24f2015-12-26 20:53:51 +0100233 ADD_ACTION(twcmd);
Ethan Yonker1b190162016-12-05 15:25:19 -0600234 ADD_ACTION(setbootslot);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -0600235 ADD_ACTION(installapp);
Ethan Yonker76bbd3a2019-05-10 10:50:04 -0500236 ADD_ACTION(uninstalltwrpsystemapp);
Ethan Yonker53796e72019-01-11 22:49:52 -0600237 ADD_ACTION(repackimage);
238 ADD_ACTION(fixabrecoverybootloop);
that3f7b1ac2014-12-30 11:30:13 +0100239 }
240
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200241 // First, get the action
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600242 actions = FindNode(node, "actions");
243 if (actions) child = FindNode(actions, "action");
244 else child = FindNode(node, "action");
Dees_Troy51a0e822012-09-05 15:24:24 -0400245
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200246 if (!child) return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400247
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200248 while (child)
249 {
250 Action action;
Dees_Troy51a0e822012-09-05 15:24:24 -0400251
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200252 attr = child->first_attribute("function");
253 if (!attr) return;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500254
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200255 action.mFunction = attr->value();
256 action.mArg = child->value();
257 mActions.push_back(action);
Dees_Troy51a0e822012-09-05 15:24:24 -0400258
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200259 child = child->next_sibling("action");
260 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400261
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200262 // Now, let's get either the key or region
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600263 child = FindNode(node, "touch");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200264 if (child)
265 {
266 attr = child->first_attribute("key");
267 if (attr)
268 {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100269 std::vector<std::string> keys = TWFunc::Split_String(attr->value(), "+");
Matt Mowera8a89d12016-12-30 18:10:37 -0600270 for (size_t i = 0; i < keys.size(); ++i)
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100271 {
272 const int key = getKeyByName(keys[i]);
273 mKeys[key] = false;
274 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200275 }
276 else
277 {
278 attr = child->first_attribute("x");
279 if (!attr) return;
280 mActionX = atol(attr->value());
281 attr = child->first_attribute("y");
282 if (!attr) return;
283 mActionY = atol(attr->value());
284 attr = child->first_attribute("w");
285 if (!attr) return;
286 mActionW = atol(attr->value());
287 attr = child->first_attribute("h");
288 if (!attr) return;
289 mActionH = atol(attr->value());
290 }
291 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400292}
293
Matt Mower25036b72016-06-24 12:30:18 -0500294int GUIAction::NotifyTouch(TOUCH_STATE state, int x __unused, int y __unused)
Dees_Troy51a0e822012-09-05 15:24:24 -0400295{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200296 if (state == TOUCH_RELEASE)
297 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400298
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200299 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400300}
301
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100302int GUIAction::NotifyKey(int key, bool down)
Dees_Troy51a0e822012-09-05 15:24:24 -0400303{
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100304 std::map<int, bool>::iterator itr = mKeys.find(key);
Matt Mowera8a89d12016-12-30 18:10:37 -0600305 if (itr == mKeys.end())
that8834a0f2016-01-05 23:29:30 +0100306 return 1;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100307
308 bool prevState = itr->second;
309 itr->second = down;
310
311 // If there is only one key for this action, wait for key up so it
312 // doesn't trigger with multi-key actions.
313 // Else, check if all buttons are pressed, then consume their release events
314 // so they don't trigger one-button actions and reset mKeys pressed status
Matt Mowera8a89d12016-12-30 18:10:37 -0600315 if (mKeys.size() == 1) {
316 if (!down && prevState) {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100317 doActions();
thatd4725ca2016-01-19 00:15:21 +0100318 return 0;
319 }
Matt Mowera8a89d12016-12-30 18:10:37 -0600320 } else if (down) {
321 for (itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
322 if (!itr->second)
that8834a0f2016-01-05 23:29:30 +0100323 return 1;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100324 }
325
326 // Passed, all req buttons are pressed, reset them and consume release events
327 HardwareKeyboard *kb = PageManager::GetHardwareKeyboard();
Matt Mowera8a89d12016-12-30 18:10:37 -0600328 for (itr = mKeys.begin(); itr != mKeys.end(); ++itr) {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100329 kb->ConsumeKeyRelease(itr->first);
330 itr->second = false;
331 }
332
333 doActions();
thatd4725ca2016-01-19 00:15:21 +0100334 return 0;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100335 }
336
thatd4725ca2016-01-19 00:15:21 +0100337 return 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400338}
339
Vojtech Bocek07220562014-02-08 02:05:33 +0100340int GUIAction::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400341{
Vojtech Bocek07220562014-02-08 02:05:33 +0100342 GUIObject::NotifyVarChange(varName, value);
343
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100344 if (varName.empty() && !isConditionValid() && mKeys.empty() && !mActionW)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200345 doActions();
Matt Mowera8a89d12016-12-30 18:10:37 -0600346 else if ((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200347 doActions();
Dees_Troy51a0e822012-09-05 15:24:24 -0400348
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200349 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400350}
351
352void GUIAction::simulate_progress_bar(void)
353{
Ethan Yonker74db1572015-10-28 12:44:49 -0500354 gui_msg("simulating=Simulating actions...");
Dees_Troy51a0e822012-09-05 15:24:24 -0400355 for (int i = 0; i < 5; i++)
356 {
bigbiff7abc5fe2015-01-17 16:53:12 -0500357 if (PartitionManager.stop_backup.get_value()) {
358 DataManager::SetValue("tw_cancel_backup", 1);
Matt Mower3c366972015-12-25 19:28:31 -0600359 gui_msg("backup_cancel=Backup Cancelled");
bigbiff7abc5fe2015-01-17 16:53:12 -0500360 DataManager::SetValue("ui_progress", 0);
361 PartitionManager.stop_backup.set_value(0);
362 return;
363 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400364 usleep(500000);
365 DataManager::SetValue("ui_progress", i * 20);
366 }
367}
368
Ethan Yonker0d47eb52015-01-09 11:23:19 -0600369int GUIAction::flash_zip(std::string filename, int* wipe_cache)
Dees_Troy51a0e822012-09-05 15:24:24 -0400370{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200371 int ret_val = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400372
373 DataManager::SetValue("ui_progress", 0);
Chaosmasterd5364a02020-02-03 15:38:02 +0100374 DataManager::SetValue("ui_portion_size", 0);
375 DataManager::SetValue("ui_portion_start", 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400376
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200377 if (filename.empty())
378 {
379 LOGERR("No file specified.\n");
380 return -1;
381 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400382
Ethan Yonker9598c072016-01-15 21:54:34 -0600383 if (!TWFunc::Path_Exists(filename)) {
384 if (!PartitionManager.Mount_By_Path(filename, true)) {
385 return -1;
386 }
387 if (!TWFunc::Path_Exists(filename)) {
388 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")(filename));
389 return -1;
390 }
391 }
Dees_Troy657c3092012-09-10 20:32:10 -0400392
Dees_Troy51a0e822012-09-05 15:24:24 -0400393 if (simulate) {
394 simulate_progress_bar();
395 } else {
Dees_Troy657c3092012-09-10 20:32:10 -0400396 ret_val = TWinstall_zip(filename.c_str(), wipe_cache);
Dees_Troy51a0e822012-09-05 15:24:24 -0400397
398 // Now, check if we need to ensure TWRP remains installed...
399 struct stat st;
bigbiffad58e1b2020-07-06 20:24:34 -0400400 if (stat("/system/bin/installTwrp", &st) == 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400401 {
402 DataManager::SetValue("tw_operation", "Configuring TWRP");
403 DataManager::SetValue("tw_partition", "");
Ethan Yonker74db1572015-10-28 12:44:49 -0500404 gui_msg("config_twrp=Configuring TWRP...");
bigbiffad58e1b2020-07-06 20:24:34 -0400405 if (TWFunc::Exec_Cmd("/system/bin/installTwrp reinstall") < 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400406 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500407 gui_msg("config_twrp_err=Unable to configure TWRP with this kernel.");
Dees_Troy51a0e822012-09-05 15:24:24 -0400408 }
409 }
410 }
411
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200412 // Done
413 DataManager::SetValue("ui_progress", 100);
414 DataManager::SetValue("ui_progress", 0);
Chaosmasterd5364a02020-02-03 15:38:02 +0100415 DataManager::SetValue("ui_portion_size", 0);
416 DataManager::SetValue("ui_portion_start", 0);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200417 return ret_val;
Dees_Troy51a0e822012-09-05 15:24:24 -0400418}
419
that73a52952015-01-28 23:07:34 +0100420GUIAction::ThreadType GUIAction::getThreadType(const GUIAction::Action& action)
thatc6085482015-01-09 22:12:43 +0100421{
that73a52952015-01-28 23:07:34 +0100422 string func = gui_parse_text(action.mFunction);
423 bool needsThread = setActionsRunningInCallerThread.find(func) == setActionsRunningInCallerThread.end();
424 if (needsThread) {
425 if (func == "cancelbackup")
426 return THREAD_CANCEL;
427 else
428 return THREAD_ACTION;
429 }
430 return THREAD_NONE;
thatc6085482015-01-09 22:12:43 +0100431}
432
Dees_Troy51a0e822012-09-05 15:24:24 -0400433int GUIAction::doActions()
434{
that3f7b1ac2014-12-30 11:30:13 +0100435 if (mActions.size() < 1)
436 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400437
that73a52952015-01-28 23:07:34 +0100438 // Determine in which thread to run the actions.
439 // Do it for all actions at once before starting, so that we can cancel the whole batch if the thread is already busy.
440 ThreadType threadType = THREAD_NONE;
that3f7b1ac2014-12-30 11:30:13 +0100441 std::vector<Action>::iterator it;
that73a52952015-01-28 23:07:34 +0100442 for (it = mActions.begin(); it != mActions.end(); ++it) {
443 ThreadType tt = getThreadType(*it);
444 if (tt == THREAD_NONE)
445 continue;
446 if (threadType == THREAD_NONE)
447 threadType = tt;
448 else if (threadType != tt) {
449 LOGERR("Can't mix normal and cancel actions in the same list.\n"
450 "Running the whole batch in the cancel thread.\n");
451 threadType = THREAD_CANCEL;
thatc6085482015-01-09 22:12:43 +0100452 break;
453 }
that7d3b54f2015-01-09 22:52:51 +0100454 }
that73a52952015-01-28 23:07:34 +0100455
456 // Now run the actions in the desired thread.
457 switch (threadType) {
458 case THREAD_ACTION:
459 action_thread.threadActions(this);
460 break;
461
462 case THREAD_CANCEL:
463 cancel_thread.threadActions(this);
464 break;
465
466 default: {
467 // no iterators here because theme reloading might kill our object
468 const size_t cnt = mActions.size();
469 for (size_t i = 0; i < cnt; ++i)
470 doAction(mActions[i]);
471 }
thatc6085482015-01-09 22:12:43 +0100472 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400473
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200474 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400475}
476
that3f7b1ac2014-12-30 11:30:13 +0100477int GUIAction::doAction(Action action)
Dees_Troy51a0e822012-09-05 15:24:24 -0400478{
that3f7b1ac2014-12-30 11:30:13 +0100479 DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate);
Dees_Troy51a0e822012-09-05 15:24:24 -0400480
that3f7b1ac2014-12-30 11:30:13 +0100481 std::string function = gui_parse_text(action.mFunction);
482 std::string arg = gui_parse_text(action.mArg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400483
that3f7b1ac2014-12-30 11:30:13 +0100484 // find function and execute it
485 mapFunc::const_iterator funcitr = mf.find(function);
486 if (funcitr != mf.end())
487 return (this->*funcitr->second)(arg);
488
489 LOGERR("Unknown action '%s'\n", function.c_str());
490 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400491}
492
493void GUIAction::operation_start(const string operation_name)
494{
that3f7b1ac2014-12-30 11:30:13 +0100495 LOGINFO("operation_start: '%s'\n", operation_name.c_str());
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000496 time(&Start);
Dees_Troy51a0e822012-09-05 15:24:24 -0400497 DataManager::SetValue(TW_ACTION_BUSY, 1);
498 DataManager::SetValue("ui_progress", 0);
Chaosmasterd5364a02020-02-03 15:38:02 +0100499 DataManager::SetValue("ui_portion_size", 0);
500 DataManager::SetValue("ui_portion_start", 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400501 DataManager::SetValue("tw_operation", operation_name);
Dees_Troy51a0e822012-09-05 15:24:24 -0400502 DataManager::SetValue("tw_operation_state", 0);
Ethan Yonkerd83c9ea2015-01-02 15:22:31 -0600503 DataManager::SetValue("tw_operation_status", 0);
bigbiff25d25b92020-06-19 16:07:38 -0400504 bool tw_ab_device = TWFunc::get_log_dir() != CACHE_LOGS_DIR;
bigbiff bigbiff19874f12019-01-08 20:06:57 -0500505 DataManager::SetValue("tw_ab_device", tw_ab_device);
Dees_Troy51a0e822012-09-05 15:24:24 -0400506}
507
that3f7b1ac2014-12-30 11:30:13 +0100508void GUIAction::operation_end(const int operation_status)
Dees_Troy51a0e822012-09-05 15:24:24 -0400509{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000510 time_t Stop;
Dees_Troy51a0e822012-09-05 15:24:24 -0400511 int simulate_fail;
Dees_Troy51a0e822012-09-05 15:24:24 -0400512 DataManager::SetValue("ui_progress", 100);
513 if (simulate) {
514 DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail);
515 if (simulate_fail != 0)
516 DataManager::SetValue("tw_operation_status", 1);
517 else
518 DataManager::SetValue("tw_operation_status", 0);
519 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500520 if (operation_status != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400521 DataManager::SetValue("tw_operation_status", 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500522 }
523 else {
Dees_Troy51a0e822012-09-05 15:24:24 -0400524 DataManager::SetValue("tw_operation_status", 0);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500525 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400526 }
527 DataManager::SetValue("tw_operation_state", 1);
528 DataManager::SetValue(TW_ACTION_BUSY, 0);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500529 blankTimer.resetTimerAndUnblank();
LuK133762326f42015-09-30 19:57:46 +0200530 property_set("twrp.action_complete", "1");
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000531 time(&Stop);
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400532
533#ifndef TW_NO_HAPTICS
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000534 if ((int) difftime(Stop, Start) > 10)
Ethan Yonker03db3262014-02-05 08:02:06 -0600535 DataManager::Vibrate("tw_action_vibrate");
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400536#endif
537
that3f7b1ac2014-12-30 11:30:13 +0100538 LOGINFO("operation_end - status=%d\n", operation_status);
Dees_Troy51a0e822012-09-05 15:24:24 -0400539}
540
that3f7b1ac2014-12-30 11:30:13 +0100541int GUIAction::reboot(std::string arg)
Dees_Troy51a0e822012-09-05 15:24:24 -0400542{
that3f7b1ac2014-12-30 11:30:13 +0100543 sync();
544 DataManager::SetValue("tw_gui_done", 1);
545 DataManager::SetValue("tw_reboot_arg", arg);
Dees_Troy51a0e822012-09-05 15:24:24 -0400546
that3f7b1ac2014-12-30 11:30:13 +0100547 return 0;
548}
Dees_Troy51a0e822012-09-05 15:24:24 -0400549
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500550int GUIAction::home(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100551{
that3f7b1ac2014-12-30 11:30:13 +0100552 gui_changePage("main");
553 return 0;
554}
Dees_Troy51a0e822012-09-05 15:24:24 -0400555
that3f7b1ac2014-12-30 11:30:13 +0100556int GUIAction::key(std::string arg)
557{
558 const int key = getKeyByName(arg);
559 PageManager::NotifyKey(key, true);
560 PageManager::NotifyKey(key, false);
561 return 0;
562}
563
564int GUIAction::page(std::string arg)
565{
LuK133762326f42015-09-30 19:57:46 +0200566 property_set("twrp.action_complete", "0");
that3f7b1ac2014-12-30 11:30:13 +0100567 std::string page_name = gui_parse_text(arg);
568 return gui_changePage(page_name);
569}
570
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500571int GUIAction::reload(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100572{
Ethan Yonkere0f1f3b2015-10-27 09:49:01 -0500573 PageManager::RequestReload();
574 // The actual reload is handled in pages.cpp in PageManager::RunReload()
575 // The reload will occur on the next Update or Render call and will
576 // be performed in the rendoer thread instead of the action thread
577 // to prevent crashing which could occur when we start deleting
578 // GUI resources in the action thread while we attempt to render
579 // with those same resources in another thread.
that3f7b1ac2014-12-30 11:30:13 +0100580 return 0;
581}
Dees_Troy51a0e822012-09-05 15:24:24 -0400582
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500583int GUIAction::readBackup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100584{
585 string Restore_Name;
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -0400586
that3f7b1ac2014-12-30 11:30:13 +0100587 DataManager::GetValue("tw_restore", Restore_Name);
588 PartitionManager.Set_Restore_Files(Restore_Name);
589 return 0;
590}
591
592int GUIAction::set(std::string arg)
593{
594 if (arg.find('=') != string::npos)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200595 {
that3f7b1ac2014-12-30 11:30:13 +0100596 string varName = arg.substr(0, arg.find('='));
597 string value = arg.substr(arg.find('=') + 1, string::npos);
Dees_Troy51a0e822012-09-05 15:24:24 -0400598
that3f7b1ac2014-12-30 11:30:13 +0100599 DataManager::GetValue(value, value);
600 DataManager::SetValue(varName, value);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200601 }
that3f7b1ac2014-12-30 11:30:13 +0100602 else
603 DataManager::SetValue(arg, "1");
604 return 0;
605}
Dees_Troy51a0e822012-09-05 15:24:24 -0400606
that3f7b1ac2014-12-30 11:30:13 +0100607int GUIAction::clear(std::string arg)
608{
609 DataManager::SetValue(arg, "0");
610 return 0;
611}
Dees_Troy51a0e822012-09-05 15:24:24 -0400612
that3f7b1ac2014-12-30 11:30:13 +0100613int GUIAction::mount(std::string arg)
614{
615 if (arg == "usb") {
616 DataManager::SetValue(TW_ACTION_BUSY, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400617 if (!simulate)
that3f7b1ac2014-12-30 11:30:13 +0100618 PartitionManager.usb_storage_enable();
619 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500620 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100621 } else if (!simulate) {
622 PartitionManager.Mount_By_Path(arg, true);
623 PartitionManager.Add_MTP_Storage(arg);
624 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500625 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100626 return 0;
627}
628
629int GUIAction::unmount(std::string arg)
630{
631 if (arg == "usb") {
632 if (!simulate)
633 PartitionManager.usb_storage_disable();
634 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500635 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100636 DataManager::SetValue(TW_ACTION_BUSY, 0);
637 } else if (!simulate) {
638 PartitionManager.UnMount_By_Path(arg, true);
639 } else
Ethan Yonker74db1572015-10-28 12:44:49 -0500640 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100641 return 0;
642}
643
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500644int GUIAction::restoredefaultsettings(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100645{
646 operation_start("Restore Defaults");
647 if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting
Ethan Yonker74db1572015-10-28 12:44:49 -0500648 gui_msg("simulating=Simulating actions...");
that3f7b1ac2014-12-30 11:30:13 +0100649 else {
650 DataManager::ResetDefaults();
651 PartitionManager.Update_System_Details();
652 PartitionManager.Mount_Current_Storage(true);
653 }
654 operation_end(0);
655 return 0;
656}
657
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500658int GUIAction::copylog(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100659{
660 operation_start("Copy Log");
661 if (!simulate)
662 {
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400663 string dst, curr_storage;
664 int copy_kernel_log = 0;
665
666 DataManager::GetValue("tw_include_kernel_log", copy_kernel_log);
that3f7b1ac2014-12-30 11:30:13 +0100667 PartitionManager.Mount_Current_Storage(true);
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400668 curr_storage = DataManager::GetCurrentStoragePath();
669 dst = curr_storage + "/recovery.log";
that3f7b1ac2014-12-30 11:30:13 +0100670 TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
671 tw_set_default_metadata(dst.c_str());
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400672 if (copy_kernel_log)
673 TWFunc::copy_kernel_log(curr_storage);
that3f7b1ac2014-12-30 11:30:13 +0100674 sync();
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400675 gui_msg(Msg("copy_log=Copied recovery log to {1}")(dst));
that3f7b1ac2014-12-30 11:30:13 +0100676 } else
677 simulate_progress_bar();
678 operation_end(0);
679 return 0;
680}
681
682
683int GUIAction::compute(std::string arg)
684{
685 if (arg.find("+") != string::npos)
686 {
687 string varName = arg.substr(0, arg.find('+'));
688 string string_to_add = arg.substr(arg.find('+') + 1, string::npos);
689 int amount_to_add = atoi(string_to_add.c_str());
690 int value;
691
692 DataManager::GetValue(varName, value);
693 DataManager::SetValue(varName, value + amount_to_add);
Dees_Troy51a0e822012-09-05 15:24:24 -0400694 return 0;
695 }
that3f7b1ac2014-12-30 11:30:13 +0100696 if (arg.find("-") != string::npos)
Dees_Troy51a0e822012-09-05 15:24:24 -0400697 {
that3f7b1ac2014-12-30 11:30:13 +0100698 string varName = arg.substr(0, arg.find('-'));
699 string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos);
700 int amount_to_subtract = atoi(string_to_subtract.c_str());
701 int value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400702
that3f7b1ac2014-12-30 11:30:13 +0100703 DataManager::GetValue(varName, value);
704 value -= amount_to_subtract;
705 if (value <= 0)
706 value = 0;
707 DataManager::SetValue(varName, value);
708 return 0;
709 }
710 if (arg.find("*") != string::npos)
711 {
712 string varName = arg.substr(0, arg.find('*'));
713 string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos));
714 int multiply_by = atoi(multiply_by_str.c_str());
715 int value;
716
717 DataManager::GetValue(varName, value);
718 DataManager::SetValue(varName, value*multiply_by);
719 return 0;
720 }
721 if (arg.find("/") != string::npos)
722 {
723 string varName = arg.substr(0, arg.find('/'));
724 string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos));
725 int divide_by = atoi(divide_by_str.c_str());
726 int value;
727
Matt Mowera8a89d12016-12-30 18:10:37 -0600728 if (divide_by != 0)
that3f7b1ac2014-12-30 11:30:13 +0100729 {
Dees_Troy51a0e822012-09-05 15:24:24 -0400730 DataManager::GetValue(varName, value);
that3f7b1ac2014-12-30 11:30:13 +0100731 DataManager::SetValue(varName, value/divide_by);
Dees_Troy51a0e822012-09-05 15:24:24 -0400732 }
733 return 0;
734 }
that3f7b1ac2014-12-30 11:30:13 +0100735 LOGERR("Unable to perform compute '%s'\n", arg.c_str());
736 return -1;
737}
Dees_Troy51a0e822012-09-05 15:24:24 -0400738
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500739int GUIAction::setguitimezone(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100740{
741 string SelectedZone;
742 DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone
743 string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone
744 string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component
745
746 int dst;
747 DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST
748
749 string offset;
750 DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset
751
752 string NewTimeZone = Zone;
753 if (offset != "0")
754 NewTimeZone += ":" + offset;
755
756 if (dst != 0)
757 NewTimeZone += DSTZone;
758
759 DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone);
760 DataManager::update_tz_environment_variables();
761 return 0;
762}
763
764int GUIAction::overlay(std::string arg)
765{
766 return gui_changeOverlay(arg);
767}
768
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500769int GUIAction::queuezip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100770{
771 if (zip_queue_index >= 10) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500772 gui_msg("max_queue=Maximum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400773 return 0;
774 }
that3f7b1ac2014-12-30 11:30:13 +0100775 DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]);
776 if (strlen(zip_queue[zip_queue_index].c_str()) > 0) {
777 zip_queue_index++;
Dees_Troy51a0e822012-09-05 15:24:24 -0400778 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +0100779 }
780 return 0;
781}
782
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500783int GUIAction::cancelzip(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100784{
785 if (zip_queue_index <= 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500786 gui_msg("min_queue=Minimum zip queue reached!");
Dees_Troy51a0e822012-09-05 15:24:24 -0400787 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100788 } else {
789 zip_queue_index--;
790 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
Dees_Troy51a0e822012-09-05 15:24:24 -0400791 }
that3f7b1ac2014-12-30 11:30:13 +0100792 return 0;
793}
Dees_Troy51a0e822012-09-05 15:24:24 -0400794
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500795int GUIAction::queueclear(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100796{
797 zip_queue_index = 0;
798 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
799 return 0;
800}
Dees_Troy51a0e822012-09-05 15:24:24 -0400801
that3f7b1ac2014-12-30 11:30:13 +0100802int GUIAction::sleep(std::string arg)
803{
804 operation_start("Sleep");
805 usleep(atoi(arg.c_str()));
806 operation_end(0);
807 return 0;
808}
Dees Troyb21cc642013-09-10 17:36:41 +0000809
Matt Mower9a2a2052016-05-31 21:31:22 -0500810int GUIAction::sleepcounter(std::string arg)
811{
812 operation_start("SleepCounter");
813 // Ensure user notices countdown in case it needs to be cancelled
814 blankTimer.resetTimerAndUnblank();
815 int total = atoi(arg.c_str());
816 for (int t = total; t > 0; t--) {
817 int progress = (int)(((float)(total-t)/(float)total)*100.0);
818 DataManager::SetValue("ui_progress", progress);
819 ::sleep(1);
820 DataManager::SetValue("tw_sleep", t-1);
821 }
822 DataManager::SetValue("ui_progress", 100);
823 operation_end(0);
824 return 0;
825}
826
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500827int GUIAction::appenddatetobackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100828{
829 operation_start("AppendDateToBackupName");
830 string Backup_Name;
831 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
832 Backup_Name += TWFunc::Get_Current_Date();
833 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
834 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
835 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
Matt Mower76b8afc2016-06-24 12:04:27 -0500836 PageManager::NotifyKey(KEY_END, true);
837 PageManager::NotifyKey(KEY_END, false);
that3f7b1ac2014-12-30 11:30:13 +0100838 operation_end(0);
839 return 0;
840}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500841
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500842int GUIAction::generatebackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100843{
844 operation_start("GenerateBackupName");
845 TWFunc::Auto_Generate_Backup_Name();
846 operation_end(0);
847 return 0;
848}
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500849
Ethan Yonker483e9f42016-01-11 22:21:18 -0600850int GUIAction::checkpartitionlist(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100851{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600852 string List, part_path;
that3f7b1ac2014-12-30 11:30:13 +0100853 int count = 0;
Ethan Yonker87c7bac2014-05-25 21:41:08 -0500854
Ethan Yonker483e9f42016-01-11 22:21:18 -0600855 if (arg.empty())
856 arg = "tw_wipe_list";
857 DataManager::GetValue(arg, List);
858 LOGINFO("checkpartitionlist list '%s'\n", List.c_str());
859 if (!List.empty()) {
860 size_t start_pos = 0, end_pos = List.find(";", start_pos);
861 while (end_pos != string::npos && start_pos < List.size()) {
862 part_path = List.substr(start_pos, end_pos - start_pos);
863 LOGINFO("checkpartitionlist part_path '%s'\n", part_path.c_str());
864 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100865 // Do nothing
Dees_Troy51a0e822012-09-05 15:24:24 -0400866 } else {
that3f7b1ac2014-12-30 11:30:13 +0100867 count++;
868 }
869 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600870 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100871 }
872 DataManager::SetValue("tw_check_partition_list", count);
873 } else {
874 DataManager::SetValue("tw_check_partition_list", 0);
875 }
Matt Mowera8a89d12016-12-30 18:10:37 -0600876 return 0;
that3f7b1ac2014-12-30 11:30:13 +0100877}
Dees_Troy51a0e822012-09-05 15:24:24 -0400878
Ethan Yonker483e9f42016-01-11 22:21:18 -0600879int GUIAction::getpartitiondetails(std::string arg)
that3f7b1ac2014-12-30 11:30:13 +0100880{
Ethan Yonker483e9f42016-01-11 22:21:18 -0600881 string List, part_path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400882
Ethan Yonker483e9f42016-01-11 22:21:18 -0600883 if (arg.empty())
884 arg = "tw_wipe_list";
885 DataManager::GetValue(arg, List);
886 LOGINFO("getpartitiondetails list '%s'\n", List.c_str());
887 if (!List.empty()) {
888 size_t start_pos = 0, end_pos = List.find(";", start_pos);
889 part_path = List;
890 while (end_pos != string::npos && start_pos < List.size()) {
891 part_path = List.substr(start_pos, end_pos - start_pos);
892 LOGINFO("getpartitiondetails part_path '%s'\n", part_path.c_str());
893 if (part_path == "/and-sec" || part_path == "DALVIK" || part_path == "INTERNAL") {
that3f7b1ac2014-12-30 11:30:13 +0100894 // Do nothing
895 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600896 DataManager::SetValue("tw_partition_path", part_path);
that3f7b1ac2014-12-30 11:30:13 +0100897 break;
898 }
899 start_pos = end_pos + 1;
Ethan Yonker483e9f42016-01-11 22:21:18 -0600900 end_pos = List.find(";", start_pos);
that3f7b1ac2014-12-30 11:30:13 +0100901 }
Ethan Yonker483e9f42016-01-11 22:21:18 -0600902 if (!part_path.empty()) {
903 TWPartition* Part = PartitionManager.Find_Partition_By_Path(part_path);
that3f7b1ac2014-12-30 11:30:13 +0100904 if (Part) {
905 unsigned long long mb = 1048576;
Dees_Troya13d74f2013-03-24 08:54:55 -0500906
that3f7b1ac2014-12-30 11:30:13 +0100907 DataManager::SetValue("tw_partition_name", Part->Display_Name);
908 DataManager::SetValue("tw_partition_mount_point", Part->Mount_Point);
909 DataManager::SetValue("tw_partition_file_system", Part->Current_File_System);
910 DataManager::SetValue("tw_partition_size", Part->Size / mb);
911 DataManager::SetValue("tw_partition_used", Part->Used / mb);
912 DataManager::SetValue("tw_partition_free", Part->Free / mb);
913 DataManager::SetValue("tw_partition_backup_size", Part->Backup_Size / mb);
914 DataManager::SetValue("tw_partition_removable", Part->Removable);
915 DataManager::SetValue("tw_partition_is_present", Part->Is_Present);
916
917 if (Part->Can_Repair())
918 DataManager::SetValue("tw_partition_can_repair", 1);
919 else
920 DataManager::SetValue("tw_partition_can_repair", 0);
Ethan Yonkera2719152015-05-28 09:44:41 -0500921 if (Part->Can_Resize())
922 DataManager::SetValue("tw_partition_can_resize", 1);
923 else
924 DataManager::SetValue("tw_partition_can_resize", 0);
bigbiffad58e1b2020-07-06 20:24:34 -0400925 if (TWFunc::Path_Exists("/system/bin/mkfs.fat"))
that3f7b1ac2014-12-30 11:30:13 +0100926 DataManager::SetValue("tw_partition_vfat", 1);
927 else
928 DataManager::SetValue("tw_partition_vfat", 0);
bigbiffad58e1b2020-07-06 20:24:34 -0400929 if (TWFunc::Path_Exists("/system/bin/mkexfatfs"))
that3f7b1ac2014-12-30 11:30:13 +0100930 DataManager::SetValue("tw_partition_exfat", 1);
931 else
932 DataManager::SetValue("tw_partition_exfat", 0);
bigbiffad58e1b2020-07-06 20:24:34 -0400933 if (TWFunc::Path_Exists("/system/bin/mkfs.f2fs"))
that3f7b1ac2014-12-30 11:30:13 +0100934 DataManager::SetValue("tw_partition_f2fs", 1);
935 else
936 DataManager::SetValue("tw_partition_f2fs", 0);
bigbiffad58e1b2020-07-06 20:24:34 -0400937 if (TWFunc::Path_Exists("/system/bin/mke2fs"))
that3f7b1ac2014-12-30 11:30:13 +0100938 DataManager::SetValue("tw_partition_ext", 1);
939 else
940 DataManager::SetValue("tw_partition_ext", 0);
941 return 0;
942 } else {
Ethan Yonker483e9f42016-01-11 22:21:18 -0600943 LOGERR("Unable to locate partition: '%s'\n", part_path.c_str());
that3f7b1ac2014-12-30 11:30:13 +0100944 }
945 }
946 }
947 DataManager::SetValue("tw_partition_name", "");
948 DataManager::SetValue("tw_partition_file_system", "");
Ethan Yonker483e9f42016-01-11 22:21:18 -0600949 // Set this to 0 to prevent trying to partition this device, just in case
950 DataManager::SetValue("tw_partition_removable", 0);
that3f7b1ac2014-12-30 11:30:13 +0100951 return 0;
952}
953
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500954int GUIAction::screenshot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +0100955{
956 time_t tm;
957 char path[256];
958 int path_len;
Matt Mower0c88b842017-01-15 16:00:49 -0600959 uid_t uid = AID_MEDIA_RW;
960 gid_t gid = AID_MEDIA_RW;
that3f7b1ac2014-12-30 11:30:13 +0100961
962 const std::string storage = DataManager::GetCurrentStoragePath();
Matt Mowera8a89d12016-12-30 18:10:37 -0600963 if (PartitionManager.Is_Mounted_By_Path(storage)) {
that3f7b1ac2014-12-30 11:30:13 +0100964 snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
965 } else {
966 strcpy(path, "/tmp/");
967 }
968
Matt Mowera8a89d12016-12-30 18:10:37 -0600969 if (!TWFunc::Create_Dir_Recursive(path, 0775, uid, gid))
that3f7b1ac2014-12-30 11:30:13 +0100970 return 0;
971
972 tm = time(NULL);
973 path_len = strlen(path);
974
975 // Screenshot_2014-01-01-18-21-38.png
976 strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
977
978 int res = gr_save_screenshot(path);
Matt Mowera8a89d12016-12-30 18:10:37 -0600979 if (res == 0) {
that3f7b1ac2014-12-30 11:30:13 +0100980 chmod(path, 0666);
981 chown(path, uid, gid);
982
that677b13f2015-12-26 23:57:31 +0100983 gui_msg(Msg("screenshot_saved=Screenshot was saved to {1}")(path));
that3f7b1ac2014-12-30 11:30:13 +0100984
VDavid0032034a412019-10-18 22:43:20 +0200985 // blink to notify that the screenshot was taken
that3f7b1ac2014-12-30 11:30:13 +0100986 gr_color(255, 255, 255, 255);
987 gr_fill(0, 0, gr_fb_width(), gr_fb_height());
988 gr_flip();
989 gui_forceRender();
990 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500991 gui_err("screenshot_err=Failed to take a screenshot!");
that3f7b1ac2014-12-30 11:30:13 +0100992 }
993 return 0;
994}
995
996int GUIAction::setbrightness(std::string arg)
997{
998 return TWFunc::Set_Brightness(arg);
999}
1000
1001int GUIAction::fileexists(std::string arg)
1002{
1003 struct stat st;
1004 string newpath = arg + "/.";
1005
1006 operation_start("FileExists");
1007 if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0)
1008 operation_end(0);
1009 else
1010 operation_end(1);
1011 return 0;
1012}
1013
thatcc8ddca2015-01-03 01:59:36 +01001014void GUIAction::reinject_after_flash()
1015{
1016 if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001017 gui_msg("injecttwrp=Injecting TWRP into boot image...");
thatcc8ddca2015-01-03 01:59:36 +01001018 if (simulate) {
1019 simulate_progress_bar();
1020 } else {
1021 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
1022 if (Boot == NULL || Boot->Current_File_System != "emmc")
1023 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
1024 else {
1025 string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device;
1026 TWFunc::Exec_Cmd(injectcmd);
1027 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001028 gui_msg("done=Done.");
thatcc8ddca2015-01-03 01:59:36 +01001029 }
1030 }
1031}
1032
mauronofrio6d3bf892019-10-26 19:47:55 +02001033int GUIAction::ozip_decrypt(string zip_path)
1034{
bigbiffad58e1b2020-07-06 20:24:34 -04001035 if (!TWFunc::Path_Exists("/system/bin/ozip_decrypt")) {
mauronofrio6d3bf892019-10-26 19:47:55 +02001036 return 1;
1037 }
1038 gui_msg("ozip_decrypt_decryption=Starting Ozip Decryption...");
1039 TWFunc::Exec_Cmd("ozip_decrypt " + (string)TW_OZIP_DECRYPT_KEY + " '" + zip_path + "'");
1040 gui_msg("ozip_decrypt_finish=Ozip Decryption Finished!");
1041 return 0;
1042}
1043
that3f7b1ac2014-12-30 11:30:13 +01001044int GUIAction::flash(std::string arg)
1045{
1046 int i, ret_val = 0, wipe_cache = 0;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001047 // We're going to jump to this page first, like a loading page
1048 gui_changePage(arg);
that3f7b1ac2014-12-30 11:30:13 +01001049 for (i=0; i<zip_queue_index; i++) {
thatc7b631b2015-06-01 23:36:57 +02001050 string zip_path = zip_queue[i];
1051 size_t slashpos = zip_path.find_last_of('/');
1052 string zip_filename = (slashpos == string::npos) ? zip_path : zip_path.substr(slashpos + 1);
that3f7b1ac2014-12-30 11:30:13 +01001053 operation_start("Flashing");
mauronofrio6d3bf892019-10-26 19:47:55 +02001054 if((zip_path.substr(zip_path.size() - 4, 4)) == "ozip")
1055 {
1056 if((ozip_decrypt(zip_path)) != 0)
1057 {
1058 LOGERR("Unable to find ozip_decrypt!");
1059 break;
1060 }
1061 zip_filename = (zip_filename.substr(0, zip_filename.size() - 4)).append("zip");
1062 zip_path = (zip_path.substr(0, zip_path.size() - 4)).append("zip");
1063 if (!TWFunc::Path_Exists(zip_path)) {
1064 LOGERR("Unable to find decrypted zip");
1065 break;
1066 }
1067 }
thatc7b631b2015-06-01 23:36:57 +02001068 DataManager::SetValue("tw_filename", zip_path);
1069 DataManager::SetValue("tw_file", zip_filename);
that3f7b1ac2014-12-30 11:30:13 +01001070 DataManager::SetValue(TW_ZIP_INDEX, (i + 1));
1071
1072 TWFunc::SetPerformanceMode(true);
thatc7b631b2015-06-01 23:36:57 +02001073 ret_val = flash_zip(zip_path, &wipe_cache);
that3f7b1ac2014-12-30 11:30:13 +01001074 TWFunc::SetPerformanceMode(false);
1075 if (ret_val != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001076 gui_msg(Msg(msg::kError, "zip_err=Error installing zip file '{1}'")(zip_path));
that3f7b1ac2014-12-30 11:30:13 +01001077 ret_val = 1;
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001078 break;
that3f7b1ac2014-12-30 11:30:13 +01001079 }
1080 }
1081 zip_queue_index = 0;
that3f7b1ac2014-12-30 11:30:13 +01001082
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001083 if (wipe_cache) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001084 gui_msg("zip_wipe_cache=One or more zip requested a cache wipe -- Wiping cache now.");
that3f7b1ac2014-12-30 11:30:13 +01001085 PartitionManager.Wipe_By_Path("/cache");
Ethan Yonker0d47eb52015-01-09 11:23:19 -06001086 }
that3f7b1ac2014-12-30 11:30:13 +01001087
thatcc8ddca2015-01-03 01:59:36 +01001088 reinject_after_flash();
that3f7b1ac2014-12-30 11:30:13 +01001089 PartitionManager.Update_System_Details();
1090 operation_end(ret_val);
bigbiffa869fc72016-03-01 19:40:36 -05001091 // 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 -06001092 DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index);
that3f7b1ac2014-12-30 11:30:13 +01001093 return 0;
1094}
1095
1096int GUIAction::wipe(std::string arg)
1097{
1098 operation_start("Format");
1099 DataManager::SetValue("tw_partition", arg);
that3f7b1ac2014-12-30 11:30:13 +01001100 int ret_val = false;
1101
1102 if (simulate) {
1103 simulate_progress_bar();
1104 } else {
1105 if (arg == "data")
1106 ret_val = PartitionManager.Factory_Reset();
1107 else if (arg == "battery")
1108 ret_val = PartitionManager.Wipe_Battery_Stats();
1109 else if (arg == "rotate")
1110 ret_val = PartitionManager.Wipe_Rotate_Data();
1111 else if (arg == "dalvik")
1112 ret_val = PartitionManager.Wipe_Dalvik_Cache();
1113 else if (arg == "DATAMEDIA") {
1114 ret_val = PartitionManager.Format_Data();
1115 } else if (arg == "INTERNAL") {
Matt Mower23d8aae2017-01-06 14:30:33 -06001116 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001117
1118 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1119 if (has_datamedia) {
1120 ret_val = PartitionManager.Wipe_Media_From_Data();
1121 } else {
1122 ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
1123 }
1124 } else if (arg == "EXTERNAL") {
1125 string External_Path;
1126
1127 DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
1128 ret_val = PartitionManager.Wipe_By_Path(External_Path);
1129 } else if (arg == "ANDROIDSECURE") {
1130 ret_val = PartitionManager.Wipe_Android_Secure();
1131 } else if (arg == "LIST") {
1132 string Wipe_List, wipe_path;
1133 bool skip = false;
1134 ret_val = true;
that3f7b1ac2014-12-30 11:30:13 +01001135
1136 DataManager::GetValue("tw_wipe_list", Wipe_List);
1137 LOGINFO("wipe list '%s'\n", Wipe_List.c_str());
1138 if (!Wipe_List.empty()) {
1139 size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos);
1140 while (end_pos != string::npos && start_pos < Wipe_List.size()) {
1141 wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos);
1142 LOGINFO("wipe_path '%s'\n", wipe_path.c_str());
1143 if (wipe_path == "/and-sec") {
1144 if (!PartitionManager.Wipe_Android_Secure()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001145 gui_msg("and_sec_wipe_err=Unable to wipe android secure");
that3f7b1ac2014-12-30 11:30:13 +01001146 ret_val = false;
1147 break;
1148 } else {
1149 skip = true;
1150 }
1151 } else if (wipe_path == "DALVIK") {
1152 if (!PartitionManager.Wipe_Dalvik_Cache()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001153 gui_err("dalvik_wipe_err=Failed to wipe dalvik");
that3f7b1ac2014-12-30 11:30:13 +01001154 ret_val = false;
1155 break;
1156 } else {
1157 skip = true;
1158 }
1159 } else if (wipe_path == "INTERNAL") {
1160 if (!PartitionManager.Wipe_Media_From_Data()) {
1161 ret_val = false;
1162 break;
1163 } else {
1164 skip = true;
Dees_Troya13d74f2013-03-24 08:54:55 -05001165 }
1166 }
that3f7b1ac2014-12-30 11:30:13 +01001167 if (!skip) {
1168 if (!PartitionManager.Wipe_By_Path(wipe_path)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001169 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(wipe_path));
that3f7b1ac2014-12-30 11:30:13 +01001170 ret_val = false;
1171 break;
1172 } else if (wipe_path == DataManager::GetSettingsStoragePath()) {
1173 arg = wipe_path;
1174 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001175 } else {
that3f7b1ac2014-12-30 11:30:13 +01001176 skip = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001177 }
that3f7b1ac2014-12-30 11:30:13 +01001178 start_pos = end_pos + 1;
1179 end_pos = Wipe_List.find(";", start_pos);
Dees_Troy51a0e822012-09-05 15:24:24 -04001180 }
1181 }
that3f7b1ac2014-12-30 11:30:13 +01001182 } else
1183 ret_val = PartitionManager.Wipe_By_Path(arg);
that73a52952015-01-28 23:07:34 +01001184#ifndef TW_OEM_BUILD
that3f7b1ac2014-12-30 11:30:13 +01001185 if (arg == DataManager::GetSettingsStoragePath()) {
1186 // If we wiped the settings storage path, recreate the TWRP folder and dump the settings
1187 string Storage_Path = DataManager::GetSettingsStoragePath();
Dees_Troy51a0e822012-09-05 15:24:24 -04001188
that3f7b1ac2014-12-30 11:30:13 +01001189 if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
1190 LOGINFO("Making TWRP folder and saving settings.\n");
1191 Storage_Path += "/TWRP";
1192 mkdir(Storage_Path.c_str(), 0777);
1193 DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001194 } else {
that3f7b1ac2014-12-30 11:30:13 +01001195 LOGERR("Unable to recreate TWRP folder and save settings.\n");
1196 }
1197 }
1198#endif
1199 }
1200 PartitionManager.Update_System_Details();
1201 if (ret_val)
1202 ret_val = 0; // 0 is success
1203 else
1204 ret_val = 1; // 1 is failure
1205 operation_end(ret_val);
1206 return 0;
1207}
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001208
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001209int GUIAction::refreshsizes(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001210{
1211 operation_start("Refreshing Sizes");
1212 if (simulate) {
1213 simulate_progress_bar();
1214 } else
1215 PartitionManager.Update_System_Details();
1216 operation_end(0);
1217 return 0;
1218}
1219
1220int GUIAction::nandroid(std::string arg)
1221{
that3f7b1ac2014-12-30 11:30:13 +01001222 if (simulate) {
that73a52952015-01-28 23:07:34 +01001223 PartitionManager.stop_backup.set_value(0);
that3f7b1ac2014-12-30 11:30:13 +01001224 DataManager::SetValue("tw_partition", "Simulation");
1225 simulate_progress_bar();
that73a52952015-01-28 23:07:34 +01001226 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001227 } else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001228 operation_start("Nandroid");
1229 int ret = 0;
1230
that3f7b1ac2014-12-30 11:30:13 +01001231 if (arg == "backup") {
1232 string Backup_Name;
1233 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Ethan Yonker4adc33e2016-01-22 16:07:11 -06001234 string auto_gen = gui_lookup("auto_generate", "(Auto Generate)");
Ethan Yonker53796e72019-01-11 22:49:52 -06001235 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 -05001236 ret = PartitionManager.Run_Backup(false);
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001237 DataManager::SetValue("tw_encrypt_backup", 0); // reset value so we don't encrypt every subsequent backup
1238 if (!PartitionManager.stop_backup.get_value()) {
1239 if (ret == false)
1240 ret = 1; // 1 for failure
1241 else
1242 ret = 0; // 0 for success
1243 DataManager::SetValue("tw_cancel_backup", 0);
1244 } else {
1245 DataManager::SetValue("tw_cancel_backup", 1);
1246 gui_msg("backup_cancel=Backup Cancelled");
1247 ret = 0;
1248 }
bigbiffce8f83c2015-12-12 18:30:21 -05001249 } else {
that3f7b1ac2014-12-30 11:30:13 +01001250 operation_end(1);
1251 return -1;
that3f7b1ac2014-12-30 11:30:13 +01001252 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001253 DataManager::SetValue(TW_BACKUP_NAME, auto_gen);
that3f7b1ac2014-12-30 11:30:13 +01001254 } else if (arg == "restore") {
1255 string Restore_Name;
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001256 int gui_adb_backup;
1257
that3f7b1ac2014-12-30 11:30:13 +01001258 DataManager::GetValue("tw_restore", Restore_Name);
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001259 DataManager::GetValue("tw_enable_adb_backup", gui_adb_backup);
1260 if (gui_adb_backup) {
1261 DataManager::SetValue("tw_operation_state", 1);
1262 if (TWFunc::stream_adb_backup(Restore_Name) == 0)
1263 ret = 0; // success
1264 else
1265 ret = 1; // failure
1266 DataManager::SetValue("tw_enable_adb_backup", 0);
1267 ret = 0; // assume success???
1268 } else {
1269 if (PartitionManager.Run_Restore(Restore_Name))
1270 ret = 0; // success
1271 else
1272 ret = 1; // failure
1273 }
that3f7b1ac2014-12-30 11:30:13 +01001274 } else {
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001275 operation_end(1); // invalid arg specified, fail
bigbiff7abc5fe2015-01-17 16:53:12 -05001276 return -1;
1277 }
that73a52952015-01-28 23:07:34 +01001278 operation_end(ret);
bigbiff7abc5fe2015-01-17 16:53:12 -05001279 return ret;
1280 }
1281 return 0;
1282}
1283
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001284int GUIAction::cancelbackup(std::string arg __unused) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001285 if (simulate) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001286 PartitionManager.stop_backup.set_value(1);
bigbiff7abc5fe2015-01-17 16:53:12 -05001287 }
1288 else {
bigbiff7abc5fe2015-01-17 16:53:12 -05001289 int op_status = PartitionManager.Cancel_Backup();
1290 if (op_status != 0)
1291 op_status = 1; // failure
bigbiff7abc5fe2015-01-17 16:53:12 -05001292 }
1293
1294 return 0;
that3f7b1ac2014-12-30 11:30:13 +01001295}
Dees_Troy51a0e822012-09-05 15:24:24 -04001296
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001297int GUIAction::fixcontexts(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001298{
that73a52952015-01-28 23:07:34 +01001299 int op_status = 0;
1300
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001301 operation_start("Fix Contexts");
1302 LOGINFO("fix contexts started!\n");
that3f7b1ac2014-12-30 11:30:13 +01001303 if (simulate) {
1304 simulate_progress_bar();
1305 } else {
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001306 op_status = PartitionManager.Fix_Contexts();
that3f7b1ac2014-12-30 11:30:13 +01001307 if (op_status != 0)
1308 op_status = 1; // failure
that3f7b1ac2014-12-30 11:30:13 +01001309 }
that73a52952015-01-28 23:07:34 +01001310 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001311 return 0;
1312}
1313
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001314int GUIAction::fixpermissions(std::string arg)
1315{
1316 return fixcontexts(arg);
1317}
1318
that3f7b1ac2014-12-30 11:30:13 +01001319int GUIAction::dd(std::string arg)
1320{
1321 operation_start("imaging");
1322
1323 if (simulate) {
1324 simulate_progress_bar();
1325 } else {
1326 string cmd = "dd " + arg;
1327 TWFunc::Exec_Cmd(cmd);
1328 }
1329 operation_end(0);
1330 return 0;
1331}
1332
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001333int GUIAction::partitionsd(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001334{
1335 operation_start("Partition SD Card");
1336 int ret_val = 0;
1337
1338 if (simulate) {
1339 simulate_progress_bar();
1340 } else {
1341 int allow_partition;
1342 DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition);
1343 if (allow_partition == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001344 gui_err("no_real_sdcard=This device does not have a real SD Card! Aborting!");
that3f7b1ac2014-12-30 11:30:13 +01001345 } else {
1346 if (!PartitionManager.Partition_SDCard())
1347 ret_val = 1; // failed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001348 }
that3f7b1ac2014-12-30 11:30:13 +01001349 }
1350 operation_end(ret_val);
1351 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001352
that3f7b1ac2014-12-30 11:30:13 +01001353}
Dees_Troy51a0e822012-09-05 15:24:24 -04001354
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001355int GUIAction::installhtcdumlock(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001356{
1357 operation_start("Install HTC Dumlock");
1358 if (simulate) {
1359 simulate_progress_bar();
1360 } else
1361 TWFunc::install_htc_dumlock();
Dees_Troy51a0e822012-09-05 15:24:24 -04001362
that3f7b1ac2014-12-30 11:30:13 +01001363 operation_end(0);
1364 return 0;
1365}
Dees_Troy51a0e822012-09-05 15:24:24 -04001366
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001367int GUIAction::htcdumlockrestoreboot(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001368{
1369 operation_start("HTC Dumlock Restore Boot");
1370 if (simulate) {
1371 simulate_progress_bar();
1372 } else
1373 TWFunc::htc_dumlock_restore_original_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001374
that3f7b1ac2014-12-30 11:30:13 +01001375 operation_end(0);
1376 return 0;
1377}
Dees_Troy51a0e822012-09-05 15:24:24 -04001378
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001379int GUIAction::htcdumlockreflashrecovery(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001380{
1381 operation_start("HTC Dumlock Reflash Recovery");
1382 if (simulate) {
1383 simulate_progress_bar();
1384 } else
1385 TWFunc::htc_dumlock_reflash_recovery_to_boot();
Dees_Troy51a0e822012-09-05 15:24:24 -04001386
that3f7b1ac2014-12-30 11:30:13 +01001387 operation_end(0);
1388 return 0;
1389}
Dees_Troy51a0e822012-09-05 15:24:24 -04001390
that3f7b1ac2014-12-30 11:30:13 +01001391int GUIAction::cmd(std::string arg)
1392{
1393 int op_status = 0;
1394
1395 operation_start("Command");
1396 LOGINFO("Running command: '%s'\n", arg.c_str());
1397 if (simulate) {
1398 simulate_progress_bar();
1399 } else {
1400 op_status = TWFunc::Exec_Cmd(arg);
1401 if (op_status != 0)
1402 op_status = 1;
1403 }
1404
1405 operation_end(op_status);
1406 return 0;
1407}
1408
1409int GUIAction::terminalcommand(std::string arg)
1410{
1411 int op_status = 0;
1412 string cmdpath, command;
1413
1414 DataManager::GetValue("tw_terminal_location", cmdpath);
1415 operation_start("CommandOutput");
1416 gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str());
1417 if (simulate) {
1418 simulate_progress_bar();
1419 operation_end(op_status);
Matt Mower5aa29ab2015-02-25 23:50:55 -06001420 } else if (arg == "exit") {
1421 LOGINFO("Exiting terminal\n");
1422 operation_end(op_status);
1423 page("main");
that3f7b1ac2014-12-30 11:30:13 +01001424 } else {
1425 command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";;
1426 LOGINFO("Actual command is: '%s'\n", command.c_str());
that3f7b1ac2014-12-30 11:30:13 +01001427 DataManager::SetValue("tw_terminal_state", 1);
1428 DataManager::SetValue("tw_background_thread_running", 1);
thatc6085482015-01-09 22:12:43 +01001429 FILE* fp;
1430 char line[512];
1431
1432 fp = popen(command.c_str(), "r");
1433 if (fp == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001434 LOGERR("Error opening command to run (%s).\n", strerror(errno));
thatc6085482015-01-09 22:12:43 +01001435 } else {
Matt Mower23d8aae2017-01-06 14:30:33 -06001436 int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1;
thatc6085482015-01-09 22:12:43 +01001437 struct timeval timeout;
1438 fd_set fdset;
1439
Matt Mowera8a89d12016-12-30 18:10:37 -06001440 while (keep_going)
thatc6085482015-01-09 22:12:43 +01001441 {
1442 FD_ZERO(&fdset);
1443 FD_SET(fd, &fdset);
1444 timeout.tv_sec = 0;
1445 timeout.tv_usec = 400000;
1446 has_data = select(fd+1, &fdset, NULL, NULL, &timeout);
1447 if (has_data == 0) {
1448 // Timeout reached
1449 DataManager::GetValue("tw_terminal_state", check);
1450 if (check == 0) {
1451 keep_going = 0;
1452 }
1453 } else if (has_data < 0) {
1454 // End of execution
1455 keep_going = 0;
1456 } else {
1457 // Try to read output
Matt Mowera8a89d12016-12-30 18:10:37 -06001458 if (fgets(line, sizeof(line), fp) != NULL)
thatc6085482015-01-09 22:12:43 +01001459 gui_print("%s", line); // Display output
1460 else
1461 keep_going = 0; // Done executing
1462 }
1463 }
1464 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -04001465 }
thatc6085482015-01-09 22:12:43 +01001466 DataManager::SetValue("tw_operation_status", 0);
1467 DataManager::SetValue("tw_operation_state", 1);
1468 DataManager::SetValue("tw_terminal_state", 0);
1469 DataManager::SetValue("tw_background_thread_running", 0);
1470 DataManager::SetValue(TW_ACTION_BUSY, 0);
that3f7b1ac2014-12-30 11:30:13 +01001471 }
1472 return 0;
1473}
1474
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001475int GUIAction::killterminal(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001476{
that3f7b1ac2014-12-30 11:30:13 +01001477 LOGINFO("Sending kill command...\n");
1478 operation_start("KillCommand");
1479 DataManager::SetValue("tw_operation_status", 0);
1480 DataManager::SetValue("tw_operation_state", 1);
1481 DataManager::SetValue("tw_terminal_state", 0);
1482 DataManager::SetValue("tw_background_thread_running", 0);
1483 DataManager::SetValue(TW_ACTION_BUSY, 0);
1484 return 0;
1485}
1486
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001487int GUIAction::reinjecttwrp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001488{
1489 int op_status = 0;
1490 operation_start("ReinjectTWRP");
Ethan Yonker74db1572015-10-28 12:44:49 -05001491 gui_msg("injecttwrp=Injecting TWRP into boot image...");
that3f7b1ac2014-12-30 11:30:13 +01001492 if (simulate) {
1493 simulate_progress_bar();
1494 } else {
1495 TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash");
Ethan Yonker74db1572015-10-28 12:44:49 -05001496 gui_msg("done=Done.");
that3f7b1ac2014-12-30 11:30:13 +01001497 }
1498
1499 operation_end(op_status);
1500 return 0;
1501}
1502
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001503int GUIAction::checkbackupname(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001504{
1505 int op_status = 0;
1506
1507 operation_start("CheckBackupName");
1508 if (simulate) {
1509 simulate_progress_bar();
1510 } else {
Ethan Yonker53796e72019-01-11 22:49:52 -06001511 string Backup_Name;
1512 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
1513 op_status = PartitionManager.Check_Backup_Name(Backup_Name, true, true);
that3f7b1ac2014-12-30 11:30:13 +01001514 if (op_status != 0)
1515 op_status = 1;
1516 }
1517
1518 operation_end(op_status);
1519 return 0;
1520}
1521
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001522int GUIAction::decrypt(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001523{
1524 int op_status = 0;
1525
1526 operation_start("Decrypt");
1527 if (simulate) {
1528 simulate_progress_bar();
1529 } else {
1530 string Password;
1531 DataManager::GetValue("tw_crypto_password", Password);
bigbiffadc599e2020-05-28 19:36:30 +00001532 op_status = PartitionManager.Decrypt_Device(Password);
that3f7b1ac2014-12-30 11:30:13 +01001533 if (op_status != 0)
1534 op_status = 1;
1535 else {
bigbiffadc599e2020-05-28 19:36:30 +00001536
that3f7b1ac2014-12-30 11:30:13 +01001537 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1538
Ethan Yonkercf50da52015-01-12 21:59:07 -06001539 int has_datamedia;
that3f7b1ac2014-12-30 11:30:13 +01001540
Ethan Yonkercf50da52015-01-12 21:59:07 -06001541 // Check for a custom theme and load it if exists
1542 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
1543 if (has_datamedia != 0) {
1544 if (tw_get_default_metadata(DataManager::GetSettingsStoragePath().c_str()) != 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06001545 LOGINFO("Failed to get default contexts and file mode for storage files.\n");
Ethan Yonkercf50da52015-01-12 21:59:07 -06001546 } else {
1547 LOGINFO("Got default contexts and file mode for storage files.\n");
that3f7b1ac2014-12-30 11:30:13 +01001548 }
1549 }
Ethan Yonker66a19492015-12-10 10:19:45 -06001550 PartitionManager.Decrypt_Adopted();
that3f7b1ac2014-12-30 11:30:13 +01001551 }
1552 }
1553
1554 operation_end(op_status);
1555 return 0;
1556}
1557
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001558int GUIAction::adbsideload(std::string arg __unused)
thatcc8ddca2015-01-03 01:59:36 +01001559{
1560 operation_start("Sideload");
1561 if (simulate) {
1562 simulate_progress_bar();
1563 operation_end(0);
1564 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001565 gui_msg("start_sideload=Starting ADB sideload feature...");
thatc6085482015-01-09 22:12:43 +01001566 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
1567
1568 // wait for the adb connection
bigbiffd58ba182020-03-23 10:02:29 -04001569 // int ret = apply_from_adb("/", &sideload_child_pid);
1570 Device::BuiltinAction reboot_action = Device::REBOOT_BOOTLOADER;
1571 int ret = ApplyFromAdb("/", &reboot_action);
thatc6085482015-01-09 22:12:43 +01001572 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start
1573
1574 if (ret != 0) {
1575 if (ret == -2)
Ethan Yonker74db1572015-10-28 12:44:49 -05001576 gui_msg("need_new_adb=You need adb 1.0.32 or newer to sideload to this device.");
thatc6085482015-01-09 22:12:43 +01001577 ret = 1; // failure
1578 } else {
1579 int wipe_cache = 0;
1580 int wipe_dalvik = 0;
1581 DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik);
1582
1583 if (TWinstall_zip(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache) == 0) {
1584 if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache"))
1585 PartitionManager.Wipe_By_Path("/cache");
1586 if (wipe_dalvik)
1587 PartitionManager.Wipe_Dalvik_Cache();
1588 } else {
1589 ret = 1; // failure
1590 }
thatcc8ddca2015-01-03 01:59:36 +01001591 }
thatc6085482015-01-09 22:12:43 +01001592 if (sideload_child_pid) {
1593 LOGINFO("Signaling child sideload process to exit.\n");
1594 struct stat st;
1595 // Calling stat() on this magic filename signals the minadbd
1596 // subprocess to shut down.
1597 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1598 int status;
1599 LOGINFO("Waiting for child sideload process to exit.\n");
1600 waitpid(sideload_child_pid, &status, 0);
1601 }
Dees Troy28a85d22015-04-28 02:03:16 +00001602 property_set("ctl.start", "adbd");
thatc6085482015-01-09 22:12:43 +01001603 TWFunc::Toggle_MTP(mtp_was_enabled);
1604 reinject_after_flash();
1605 operation_end(ret);
thatcc8ddca2015-01-03 01:59:36 +01001606 }
that3f7b1ac2014-12-30 11:30:13 +01001607 return 0;
1608}
1609
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001610int GUIAction::adbsideloadcancel(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001611{
that3f7b1ac2014-12-30 11:30:13 +01001612 struct stat st;
1613 DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui
Ethan Yonker74db1572015-10-28 12:44:49 -05001614 gui_msg("cancel_sideload=Cancelling ADB sideload...");
thatcc8ddca2015-01-03 01:59:36 +01001615 LOGINFO("Signaling child sideload process to exit.\n");
1616 // Calling stat() on this magic filename signals the minadbd
1617 // subprocess to shut down.
1618 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
1619 if (!sideload_child_pid) {
1620 LOGERR("Unable to get child ID\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001621 return 0;
1622 }
thatcc8ddca2015-01-03 01:59:36 +01001623 ::sleep(1);
1624 LOGINFO("Killing child sideload process.\n");
1625 kill(sideload_child_pid, SIGTERM);
1626 int status;
1627 LOGINFO("Waiting for child sideload process to exit.\n");
1628 waitpid(sideload_child_pid, &status, 0);
1629 sideload_child_pid = 0;
that3f7b1ac2014-12-30 11:30:13 +01001630 DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support
1631 return 0;
1632}
1633
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001634int GUIAction::openrecoveryscript(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001635{
1636 operation_start("OpenRecoveryScript");
1637 if (simulate) {
1638 simulate_progress_bar();
Ethan Yonker24e7eb72015-01-03 16:13:06 -06001639 operation_end(0);
that3f7b1ac2014-12-30 11:30:13 +01001640 } else {
that10ae24f2015-12-26 20:53:51 +01001641 int op_status = OpenRecoveryScript::Run_OpenRecoveryScript_Action();
Ethan Yonkere1abe612015-06-09 11:09:32 -05001642 operation_end(op_status);
that3f7b1ac2014-12-30 11:30:13 +01001643 }
1644 return 0;
1645}
1646
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001647int GUIAction::installsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001648{
1649 int op_status = 0;
1650
1651 operation_start("Install SuperSU");
1652 if (simulate) {
1653 simulate_progress_bar();
1654 } else {
Ethan Yonkerfa67cbf2018-07-20 12:22:33 -05001655 LOGERR("Installing SuperSU was deprecated from TWRP.\n");
that3f7b1ac2014-12-30 11:30:13 +01001656 }
1657
1658 operation_end(op_status);
1659 return 0;
1660}
1661
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001662int GUIAction::fixsu(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001663{
1664 int op_status = 0;
1665
1666 operation_start("Fixing Superuser Permissions");
1667 if (simulate) {
1668 simulate_progress_bar();
1669 } else {
1670 LOGERR("Fixing su permissions was deprecated from TWRP.\n");
1671 LOGERR("4.3+ ROMs with SELinux will always lose su perms.\n");
1672 }
1673
1674 operation_end(op_status);
1675 return 0;
1676}
1677
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001678int GUIAction::decrypt_backup(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001679{
1680 int op_status = 0;
1681
1682 operation_start("Try Restore Decrypt");
1683 if (simulate) {
1684 simulate_progress_bar();
1685 } else {
1686 string Restore_Path, Filename, Password;
1687 DataManager::GetValue("tw_restore", Restore_Path);
1688 Restore_Path += "/";
1689 DataManager::GetValue("tw_restore_password", Password);
1690 TWFunc::SetPerformanceMode(true);
1691 if (TWFunc::Try_Decrypting_Backup(Restore_Path, Password))
1692 op_status = 0; // success
1693 else
1694 op_status = 1; // fail
1695 TWFunc::SetPerformanceMode(false);
1696 }
1697
1698 operation_end(op_status);
1699 return 0;
1700}
1701
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001702int GUIAction::repair(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001703{
1704 int op_status = 0;
1705
1706 operation_start("Repair Partition");
1707 if (simulate) {
1708 simulate_progress_bar();
1709 } else {
1710 string part_path;
1711 DataManager::GetValue("tw_partition_mount_point", part_path);
1712 if (PartitionManager.Repair_By_Path(part_path, true)) {
1713 op_status = 0; // success
1714 } else {
that3f7b1ac2014-12-30 11:30:13 +01001715 op_status = 1; // fail
1716 }
1717 }
1718
1719 operation_end(op_status);
1720 return 0;
1721}
1722
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001723int GUIAction::resize(std::string arg __unused)
Ethan Yonkera2719152015-05-28 09:44:41 -05001724{
1725 int op_status = 0;
1726
1727 operation_start("Resize Partition");
1728 if (simulate) {
1729 simulate_progress_bar();
1730 } else {
1731 string part_path;
1732 DataManager::GetValue("tw_partition_mount_point", part_path);
1733 if (PartitionManager.Resize_By_Path(part_path, true)) {
1734 op_status = 0; // success
1735 } else {
Ethan Yonkera2719152015-05-28 09:44:41 -05001736 op_status = 1; // fail
1737 }
1738 }
1739
1740 operation_end(op_status);
1741 return 0;
1742}
1743
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001744int GUIAction::changefilesystem(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001745{
1746 int op_status = 0;
1747
1748 operation_start("Change File System");
1749 if (simulate) {
1750 simulate_progress_bar();
1751 } else {
1752 string part_path, file_system;
1753 DataManager::GetValue("tw_partition_mount_point", part_path);
1754 DataManager::GetValue("tw_action_new_file_system", file_system);
1755 if (PartitionManager.Wipe_By_Path(part_path, file_system)) {
1756 op_status = 0; // success
1757 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001758 gui_err("change_fs_err=Error changing file system.");
that3f7b1ac2014-12-30 11:30:13 +01001759 op_status = 1; // fail
1760 }
1761 }
1762 PartitionManager.Update_System_Details();
1763 operation_end(op_status);
1764 return 0;
1765}
1766
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001767int GUIAction::startmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001768{
1769 int op_status = 0;
1770
1771 operation_start("Start MTP");
1772 if (PartitionManager.Enable_MTP())
1773 op_status = 0; // success
1774 else
1775 op_status = 1; // fail
1776
1777 operation_end(op_status);
1778 return 0;
1779}
1780
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001781int GUIAction::stopmtp(std::string arg __unused)
that3f7b1ac2014-12-30 11:30:13 +01001782{
1783 int op_status = 0;
1784
1785 operation_start("Stop MTP");
1786 if (PartitionManager.Disable_MTP())
1787 op_status = 0; // success
1788 else
1789 op_status = 1; // fail
1790
1791 operation_end(op_status);
1792 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001793}
1794
Ethan Yonkerd0514ba2015-10-22 14:17:47 -05001795int GUIAction::flashimage(std::string arg __unused)
Ethan Yonker96af84a2015-01-05 14:58:36 -06001796{
1797 int op_status = 0;
1798
1799 operation_start("Flash Image");
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001800 string path, filename;
1801 DataManager::GetValue("tw_zip_location", path);
1802 DataManager::GetValue("tw_file", filename);
Ethan Yonkere080c1f2016-09-19 13:50:25 -05001803 if (PartitionManager.Flash_Image(path, filename))
Ethan Yonker96af84a2015-01-05 14:58:36 -06001804 op_status = 0; // success
1805 else
1806 op_status = 1; // fail
1807
1808 operation_end(op_status);
1809 return 0;
1810}
1811
that10ae24f2015-12-26 20:53:51 +01001812int GUIAction::twcmd(std::string arg)
1813{
1814 operation_start("TWRP CLI Command");
1815 if (simulate)
1816 simulate_progress_bar();
1817 else
1818 OpenRecoveryScript::Run_CLI_Command(arg.c_str());
1819 operation_end(0);
1820 return 0;
1821}
1822
Dees_Troy51a0e822012-09-05 15:24:24 -04001823int GUIAction::getKeyByName(std::string key)
1824{
that8834a0f2016-01-05 23:29:30 +01001825 if (key == "home") return KEY_HOMEPAGE; // note: KEY_HOME is cursor movement (like KEY_END)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001826 else if (key == "menu") return KEY_MENU;
1827 else if (key == "back") return KEY_BACK;
1828 else if (key == "search") return KEY_SEARCH;
1829 else if (key == "voldown") return KEY_VOLUMEDOWN;
1830 else if (key == "volup") return KEY_VOLUMEUP;
1831 else if (key == "power") {
Dees_Troy51a0e822012-09-05 15:24:24 -04001832 int ret_val;
1833 DataManager::GetValue(TW_POWER_BUTTON, ret_val);
1834 if (!ret_val)
1835 return KEY_POWER;
1836 else
1837 return ret_val;
1838 }
1839
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001840 return atol(key.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001841}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001842
1843int GUIAction::checkpartitionlifetimewrites(std::string arg)
1844{
1845 int op_status = 0;
1846 TWPartition* sys = PartitionManager.Find_Partition_By_Path(arg);
1847
1848 operation_start("Check Partition Lifetime Writes");
1849 if (sys) {
1850 if (sys->Check_Lifetime_Writes() != 0)
1851 DataManager::SetValue("tw_lifetime_writes", 1);
1852 else
1853 DataManager::SetValue("tw_lifetime_writes", 0);
1854 op_status = 0; // success
1855 } else {
1856 DataManager::SetValue("tw_lifetime_writes", 1);
1857 op_status = 1; // fail
1858 }
1859
1860 operation_end(op_status);
1861 return 0;
1862}
1863
1864int GUIAction::mountsystemtoggle(std::string arg)
1865{
1866 int op_status = 0;
Captain Throwback9d6feb52018-07-27 10:05:24 -04001867 bool remount_system = PartitionManager.Is_Mounted_By_Path(PartitionManager.Get_Android_Root_Path());
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001868 bool remount_vendor = PartitionManager.Is_Mounted_By_Path("/vendor");
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001869
1870 operation_start("Toggle System Mount");
Captain Throwback9d6feb52018-07-27 10:05:24 -04001871 if (!PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001872 op_status = 1; // fail
1873 } else {
Captain Throwback9d6feb52018-07-27 10:05:24 -04001874 TWPartition* Part = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001875 if (Part) {
Ethan Yonkerd6966f42015-05-30 14:52:16 -05001876 if (arg == "0") {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001877 DataManager::SetValue("tw_mount_system_ro", 0);
1878 Part->Change_Mount_Read_Only(false);
1879 } else {
1880 DataManager::SetValue("tw_mount_system_ro", 1);
1881 Part->Change_Mount_Read_Only(true);
1882 }
1883 if (remount_system) {
1884 Part->Mount(true);
1885 }
1886 op_status = 0; // success
1887 } else {
1888 op_status = 1; // fail
1889 }
Ethan Yonker1673e3d2015-10-26 21:51:58 -05001890 Part = PartitionManager.Find_Partition_By_Path("/vendor");
1891 if (Part) {
1892 if (arg == "0") {
1893 Part->Change_Mount_Read_Only(false);
1894 } else {
1895 Part->Change_Mount_Read_Only(true);
1896 }
1897 if (remount_vendor) {
1898 Part->Mount(true);
1899 }
1900 op_status = 0; // success
1901 } else {
1902 op_status = 1; // fail
1903 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001904 }
1905
1906 operation_end(op_status);
1907 return 0;
1908}
Ethan Yonker74db1572015-10-28 12:44:49 -05001909
1910int GUIAction::setlanguage(std::string arg __unused)
1911{
1912 int op_status = 0;
1913
1914 operation_start("Set Language");
1915 PageManager::LoadLanguage(DataManager::GetStrValue("tw_language"));
1916 PageManager::RequestReload();
1917 op_status = 0; // success
1918
1919 operation_end(op_status);
1920 return 0;
1921}
Ethan Yonker1b190162016-12-05 15:25:19 -06001922
Matt Mower9472ba12016-01-20 18:12:47 -06001923int GUIAction::togglebacklight(std::string arg __unused)
1924{
1925 blankTimer.toggleBlank();
1926 return 0;
1927}
1928
Ethan Yonker1b190162016-12-05 15:25:19 -06001929int GUIAction::setbootslot(std::string arg)
1930{
1931 operation_start("Set Boot Slot");
1932 if (!simulate)
Ethan Yonker1b190162016-12-05 15:25:19 -06001933 PartitionManager.Set_Active_Slot(arg);
Matt Mower029a82d2017-01-08 13:12:38 -06001934 else
Ethan Yonker1b190162016-12-05 15:25:19 -06001935 simulate_progress_bar();
1936 operation_end(0);
1937 return 0;
1938}
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001939
1940int GUIAction::checkforapp(std::string arg __unused)
1941{
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001942 operation_start("Check for TWRP App");
1943 if (!simulate)
1944 {
1945 string sdkverstr = TWFunc::System_Property_Get("ro.build.version.sdk");
1946 int sdkver = 0;
1947 if (!sdkverstr.empty()) {
1948 sdkver = atoi(sdkverstr.c_str());
1949 }
1950 if (sdkver <= 13) {
1951 if (sdkver == 0)
1952 LOGINFO("Unable to read sdk version from build prop\n");
1953 else
1954 LOGINFO("SDK version too low for TWRP app (%i < 14)\n", sdkver);
Ethan Yonker64c5c0a2017-03-06 12:42:54 -06001955 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 -06001956 goto exit;
1957 }
Ethan Yonker76bbd3a2019-05-10 10:50:04 -05001958 if (TWFunc::Is_TWRP_App_In_System()) {
1959 DataManager::SetValue("tw_app_install_status", 2); // 0 = no status, 1 = not installed, 2 = already installed or do not install
1960 goto exit;
Ethan Yonker5e1a7f92017-01-18 16:44:54 -06001961 }
1962 if (PartitionManager.Mount_By_Path("/data", false)) {
Ethan Yonker5128d292017-02-04 19:52:56 -06001963 const char parent_path[] = "/data/app";
1964 const char app_prefix[] = "me.twrp.twrpapp-";
1965 DIR *d = opendir(parent_path);
1966 if (d) {
1967 struct dirent *p;
1968 while ((p = readdir(d))) {
1969 if (p->d_type != DT_DIR || strlen(p->d_name) < strlen(app_prefix) || strncmp(p->d_name, app_prefix, strlen(app_prefix)))
1970 continue;
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001971 closedir(d);
Ethan Yonker5128d292017-02-04 19:52:56 -06001972 LOGINFO("App found at '%s/%s'\n", parent_path, p->d_name);
Ethan Yonker64c5c0a2017-03-06 12:42:54 -06001973 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 -06001974 goto exit;
1975 }
Ethan Yonker5128d292017-02-04 19:52:56 -06001976 closedir(d);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06001977 }
Ethan Yonker64c5c0a2017-03-06 12:42:54 -06001978 } else {
1979 LOGINFO("Data partition cannot be mounted during app check\n");
1980 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 -06001981 }
1982 } else
1983 simulate_progress_bar();
1984 LOGINFO("App not installed\n");
1985 DataManager::SetValue("tw_app_install_status", 1); // 0 = no status, 1 = not installed, 2 = already installed
1986exit:
1987 operation_end(0);
1988 return 0;
1989}
1990
1991int GUIAction::installapp(std::string arg __unused)
1992{
1993 int op_status = 1;
1994 operation_start("Install TWRP App");
1995 if (!simulate)
1996 {
1997 if (DataManager::GetIntValue("tw_mount_system_ro") > 0 || DataManager::GetIntValue("tw_app_install_system") == 0) {
1998 if (PartitionManager.Mount_By_Path("/data", true)) {
1999 string install_path = "/data/app";
2000 string context = "u:object_r:apk_data_file:s0";
2001 if (!TWFunc::Path_Exists(install_path)) {
2002 if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
2003 LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno));
2004 goto exit;
2005 }
2006 if (chown(install_path.c_str(), 1000, 1000)) {
2007 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
2008 goto exit;
2009 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002010 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002011 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2012 goto exit;
2013 }
2014 }
2015 install_path += "/me.twrp.twrpapp-1";
2016 if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
2017 LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno));
2018 goto exit;
2019 }
2020 if (chown(install_path.c_str(), 1000, 1000)) {
2021 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
2022 goto exit;
2023 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002024 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002025 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2026 goto exit;
2027 }
2028 install_path += "/base.apk";
bigbiffad58e1b2020-07-06 20:24:34 -04002029 if (TWFunc::copy_file("/system/bin/me.twrp.twrpapp.apk", install_path, 0644)) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002030 LOGERR("Error copying apk file\n");
2031 goto exit;
2032 }
2033 if (chown(install_path.c_str(), 1000, 1000)) {
2034 LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
2035 goto exit;
2036 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002037 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002038 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2039 goto exit;
2040 }
2041 sync();
2042 sync();
2043 }
2044 } else {
Captain Throwback9d6feb52018-07-27 10:05:24 -04002045 if (PartitionManager.Mount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) {
2046 string base_path = PartitionManager.Get_Android_Root_Path();
2047 if (TWFunc::Path_Exists(PartitionManager.Get_Android_Root_Path() + "/system"))
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002048 base_path += "/system"; // For devices with system as a root file system (e.g. Pixel)
2049 string install_path = base_path + "/priv-app";
2050 string context = "u:object_r:system_file:s0";
2051 if (!TWFunc::Path_Exists(install_path))
2052 install_path = base_path + "/app";
2053 if (TWFunc::Path_Exists(install_path)) {
2054 install_path += "/twrpapp";
2055 LOGINFO("Installing app to '%s'\n", install_path.c_str());
2056 if (mkdir(install_path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0) {
Ethan Yonker4767caf2017-01-11 10:45:04 -06002057 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002058 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2059 goto exit;
2060 }
2061 install_path += "/me.twrp.twrpapp.apk";
bigbiffad58e1b2020-07-06 20:24:34 -04002062 if (TWFunc::copy_file("/system/bin/me.twrp.twrpapp.apk", install_path, 0644)) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002063 LOGERR("Error copying apk file\n");
2064 goto exit;
2065 }
Ethan Yonker4767caf2017-01-11 10:45:04 -06002066 if (setfilecon(install_path.c_str(), (security_context_t)context.c_str()) < 0) {
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002067 LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
2068 goto exit;
2069 }
Stefan Tauner6ad14572020-01-11 22:28:53 +01002070
2071 // System apps require their permissions to be pre-set via an XML file in /etc/permissions
2072 string permission_path = base_path + "/etc/permissions/privapp-permissions-twrpapp.xml";
bigbiffad58e1b2020-07-06 20:24:34 -04002073 if (TWFunc::copy_file("/system/bin/privapp-permissions-twrpapp.xml", permission_path, 0644)) {
Stefan Tauner6ad14572020-01-11 22:28:53 +01002074 LOGERR("Error copying permission file\n");
2075 goto exit;
2076 }
2077 if (chown(permission_path.c_str(), 1000, 1000)) {
2078 LOGERR("chown %s error: %s\n", permission_path.c_str(), strerror(errno));
2079 goto exit;
2080 }
2081 if (setfilecon(permission_path.c_str(), (security_context_t)context.c_str()) < 0) {
2082 LOGERR("setfilecon %s error: %s\n", permission_path.c_str(), strerror(errno));
2083 goto exit;
2084 }
2085
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002086 sync();
2087 sync();
Captain Throwback9d6feb52018-07-27 10:05:24 -04002088 PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true);
Ethan Yonkerb4bff5e2016-12-16 07:47:58 -06002089 op_status = 0;
2090 } else {
2091 LOGERR("Error making app directory '%s': %s\n", strerror(errno));
2092 }
2093 }
2094 }
2095 }
2096 } else
2097 simulate_progress_bar();
2098exit:
2099 operation_end(0);
2100 return 0;
2101}
Ethan Yonker53796e72019-01-11 22:49:52 -06002102
Ethan Yonker76bbd3a2019-05-10 10:50:04 -05002103int GUIAction::uninstalltwrpsystemapp(std::string arg __unused)
2104{
2105 int op_status = 1;
2106 operation_start("Uninstall TWRP System App");
2107 if (!simulate)
2108 {
2109 int Mount_System_RO = DataManager::GetIntValue("tw_mount_system_ro");
2110 TWPartition* Part = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path());
2111 if (!Part) {
2112 LOGERR("Unabled to find system partition.\n");
2113 goto exit;
2114 }
2115 if (!Part->UnMount(true)) {
2116 goto exit;
2117 }
2118 if (Mount_System_RO > 0) {
2119 DataManager::SetValue("tw_mount_system_ro", 0);
2120 Part->Change_Mount_Read_Only(false);
2121 }
2122 if (Part->Mount(true)) {
2123 string base_path = PartitionManager.Get_Android_Root_Path();
2124 if (TWFunc::Path_Exists(PartitionManager.Get_Android_Root_Path() + "/system"))
2125 base_path += "/system"; // For devices with system as a root file system (e.g. Pixel)
2126 string uninstall_path = base_path + "/priv-app";
2127 if (!TWFunc::Path_Exists(uninstall_path))
2128 uninstall_path = base_path + "/app";
2129 uninstall_path += "/twrpapp";
2130 if (TWFunc::Path_Exists(uninstall_path)) {
2131 LOGINFO("Uninstalling TWRP App from '%s'\n", uninstall_path.c_str());
2132 if (TWFunc::removeDir(uninstall_path, false) == 0) {
2133 sync();
2134 op_status = 0;
2135 DataManager::SetValue("tw_app_installed_in_system", 0);
2136 DataManager::SetValue("tw_app_install_status", 0);
2137 } else {
2138 LOGERR("Unable to remove TWRP app from system.\n");
2139 }
2140 } else {
2141 LOGINFO("didn't find TWRP app in '%s'\n", uninstall_path.c_str());
2142 }
2143 }
2144 Part->UnMount(true);
2145 if (Mount_System_RO > 0) {
2146 DataManager::SetValue("tw_mount_system_ro", Mount_System_RO);
2147 Part->Change_Mount_Read_Only(true);
2148 }
2149 } else
2150 simulate_progress_bar();
2151exit:
2152 operation_end(0);
2153 return 0;
2154}
2155
Ethan Yonker53796e72019-01-11 22:49:52 -06002156int GUIAction::repackimage(std::string arg __unused)
2157{
2158 int op_status = 1;
bigbiffad58e1b2020-07-06 20:24:34 -04002159 twrpRepacker repacker;
2160
Ethan Yonker53796e72019-01-11 22:49:52 -06002161 operation_start("Repack Image");
2162 if (!simulate)
2163 {
2164 std::string path = DataManager::GetStrValue("tw_filename");
2165 Repack_Options_struct Repack_Options;
2166 Repack_Options.Disable_Verity = false;
2167 Repack_Options.Disable_Force_Encrypt = false;
2168 Repack_Options.Backup_First = DataManager::GetIntValue("tw_repack_backup_first") != 0;
2169 if (DataManager::GetIntValue("tw_repack_kernel") == 1)
2170 Repack_Options.Type = REPLACE_KERNEL;
2171 else
2172 Repack_Options.Type = REPLACE_RAMDISK;
bigbiffad58e1b2020-07-06 20:24:34 -04002173 if (!repacker.Repack_Image_And_Flash(path, Repack_Options))
Ethan Yonker53796e72019-01-11 22:49:52 -06002174 goto exit;
2175 } else
2176 simulate_progress_bar();
2177 op_status = 0;
2178exit:
2179 operation_end(op_status);
2180 return 0;
2181}
2182
2183int GUIAction::fixabrecoverybootloop(std::string arg __unused)
2184{
2185 int op_status = 1;
bigbiffad58e1b2020-07-06 20:24:34 -04002186 twrpRepacker repacker;
2187
Ethan Yonker53796e72019-01-11 22:49:52 -06002188 operation_start("Repack Image");
2189 if (!simulate)
2190 {
bigbiffad58e1b2020-07-06 20:24:34 -04002191 if (!TWFunc::Path_Exists("/system/bin/magiskboot")) {
Ethan Yonker53796e72019-01-11 22:49:52 -06002192 LOGERR("Image repacking tool not present in this TWRP build!");
2193 goto exit;
2194 }
2195 DataManager::SetProgress(0);
2196 TWPartition* part = PartitionManager.Find_Partition_By_Path("/boot");
2197 if (part)
2198 gui_msg(Msg("unpacking_image=Unpacking {1}...")(part->Display_Name));
2199 else {
2200 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")("/boot"));
2201 goto exit;
2202 }
bigbiffad58e1b2020-07-06 20:24:34 -04002203 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 -06002204 goto exit;
2205 DataManager::SetProgress(.25);
2206 gui_msg("fixing_recovery_loop_patch=Patching kernel...");
bigbiffad58e1b2020-07-06 20:24:34 -04002207 std::string command = "cd " REPACK_ORIG_DIR " && /system/bin/magiskboot hexpatch kernel 77616E745F696E697472616D667300 736B69705F696E697472616D667300";
Ethan Yonker53796e72019-01-11 22:49:52 -06002208 if (TWFunc::Exec_Cmd(command) != 0) {
2209 gui_msg(Msg(msg::kError, "fix_recovery_loop_patch_error=Error patching kernel."));
2210 goto exit;
2211 }
2212 std::string header_path = REPACK_ORIG_DIR;
2213 header_path += "header";
2214 if (TWFunc::Path_Exists(header_path)) {
2215 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";
2216 if (TWFunc::Exec_Cmd(command) != 0) {
2217 gui_msg(Msg(msg::kError, "fix_recovery_loop_patch_error=Error patching kernel."));
2218 goto exit;
2219 }
2220 }
2221 DataManager::SetProgress(.5);
2222 gui_msg(Msg("repacking_image=Repacking {1}...")(part->Display_Name));
bigbiffad58e1b2020-07-06 20:24:34 -04002223 command = "cd " REPACK_ORIG_DIR " && /system/bin/magiskboot repack " REPACK_ORIG_DIR "boot.img";
Ethan Yonker53796e72019-01-11 22:49:52 -06002224 if (TWFunc::Exec_Cmd(command) != 0) {
2225 gui_msg(Msg(msg::kError, "repack_error=Error repacking image."));
2226 goto exit;
2227 }
2228 DataManager::SetProgress(.75);
2229 std::string path = REPACK_ORIG_DIR;
2230 std::string file = "new-boot.img";
2231 DataManager::SetValue("tw_flash_partition", "/boot;");
2232 if (!PartitionManager.Flash_Image(path, file)) {
2233 LOGINFO("Error flashing new image\n");
2234 goto exit;
2235 }
2236 DataManager::SetProgress(1);
2237 TWFunc::removeDir(REPACK_ORIG_DIR, false);
2238 } else
2239 simulate_progress_bar();
2240 op_status = 0;
2241exit:
2242 operation_end(op_status);
2243 return 0;
2244}