Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 1 | /* |
| 2 | 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 18 | |
| 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_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 33 | #include <sys/wait.h> |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 34 | |
| 35 | #include <string> |
| 36 | #include <sstream> |
| 37 | #include "../partitions.hpp" |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 38 | #include "../twrp-functions.hpp" |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 39 | #include "../openrecoveryscript.hpp" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 40 | |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 41 | #include "../adb_install.h" |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 42 | #include "blanktimer.hpp" |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 43 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 44 | extern "C" { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 45 | #include "../twcommon.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 46 | #include "../minuitwrp/minui.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 47 | #include "../variables.h" |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 48 | #include "../twinstall.h" |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 49 | #include "cutils/properties.h" |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 50 | #include "../minadbd/adb.h" |
| 51 | |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 52 | int TWinstall_zip(const char* path, int* wipe_cache); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 53 | void run_script(const char *str1, const char *str2, const char *str3, const char *str4, const char *str5, const char *str6, const char *str7, int request_confirm); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 54 | int gui_console_only(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 55 | int gui_start(); |
| 56 | }; |
| 57 | |
| 58 | #include "rapidxml.hpp" |
| 59 | #include "objects.hpp" |
| 60 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 61 | extern blanktimer blankTimer; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 62 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 63 | void curtainClose(void); |
| 64 | |
| 65 | GUIAction::GUIAction(xml_node<>* node) |
| 66 | : Conditional(node) |
| 67 | { |
| 68 | xml_node<>* child; |
| 69 | xml_node<>* actions; |
| 70 | xml_attribute<>* attr; |
| 71 | |
| 72 | mKey = 0; |
| 73 | |
| 74 | if (!node) return; |
| 75 | |
| 76 | // First, get the action |
| 77 | actions = node->first_node("actions"); |
| 78 | if (actions) child = actions->first_node("action"); |
| 79 | else child = node->first_node("action"); |
| 80 | |
| 81 | if (!child) return; |
| 82 | |
| 83 | while (child) |
| 84 | { |
| 85 | Action action; |
| 86 | |
| 87 | attr = child->first_attribute("function"); |
| 88 | if (!attr) return; |
| 89 | |
| 90 | action.mFunction = attr->value(); |
| 91 | action.mArg = child->value(); |
| 92 | mActions.push_back(action); |
| 93 | |
| 94 | child = child->next_sibling("action"); |
| 95 | } |
| 96 | |
| 97 | // Now, let's get either the key or region |
| 98 | child = node->first_node("touch"); |
| 99 | if (child) |
| 100 | { |
| 101 | attr = child->first_attribute("key"); |
| 102 | if (attr) |
| 103 | { |
| 104 | std::string key = attr->value(); |
| 105 | |
| 106 | mKey = getKeyByName(key); |
| 107 | } |
| 108 | else |
| 109 | { |
| 110 | attr = child->first_attribute("x"); |
| 111 | if (!attr) return; |
| 112 | mActionX = atol(attr->value()); |
| 113 | attr = child->first_attribute("y"); |
| 114 | if (!attr) return; |
| 115 | mActionY = atol(attr->value()); |
| 116 | attr = child->first_attribute("w"); |
| 117 | if (!attr) return; |
| 118 | mActionW = atol(attr->value()); |
| 119 | attr = child->first_attribute("h"); |
| 120 | if (!attr) return; |
| 121 | mActionH = atol(attr->value()); |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | int GUIAction::NotifyTouch(TOUCH_STATE state, int x, int y) |
| 127 | { |
| 128 | if (state == TOUCH_RELEASE) |
| 129 | doActions(); |
| 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | int GUIAction::NotifyKey(int key) |
| 135 | { |
| 136 | if (!mKey || key != mKey) |
| 137 | return 1; |
| 138 | |
| 139 | doActions(); |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | int GUIAction::NotifyVarChange(std::string varName, std::string value) |
| 144 | { |
| 145 | if (varName.empty() && !isConditionValid() && !mKey && !mActionW) |
| 146 | doActions(); |
| 147 | |
| 148 | // This handles notifying the condition system of page start |
| 149 | if (varName.empty() && isConditionValid()) |
| 150 | NotifyPageSet(); |
| 151 | |
| 152 | if ((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue()) |
| 153 | doActions(); |
| 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | void GUIAction::simulate_progress_bar(void) |
| 159 | { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 160 | gui_print("Simulating actions...\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 161 | for (int i = 0; i < 5; i++) |
| 162 | { |
| 163 | usleep(500000); |
| 164 | DataManager::SetValue("ui_progress", i * 20); |
| 165 | } |
| 166 | } |
| 167 | |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 168 | int GUIAction::flash_zip(std::string filename, std::string pageName, const int simulate, int* wipe_cache) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 169 | { |
| 170 | int ret_val = 0; |
| 171 | |
| 172 | DataManager::SetValue("ui_progress", 0); |
| 173 | |
| 174 | if (filename.empty()) |
| 175 | { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 176 | LOGERR("No file specified.\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 177 | return -1; |
| 178 | } |
| 179 | |
| 180 | // We're going to jump to this page first, like a loading page |
| 181 | gui_changePage(pageName); |
| 182 | |
| 183 | int fd = -1; |
| 184 | ZipArchive zip; |
| 185 | |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 186 | if (!PartitionManager.Mount_By_Path(filename, true)) |
| 187 | return -1; |
| 188 | |
| 189 | if (mzOpenZipArchive(filename.c_str(), &zip)) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 190 | { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 191 | LOGERR("Unable to open zip file.\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 192 | return -1; |
| 193 | } |
| 194 | |
| 195 | // Check the zip to see if it has a custom installer theme |
| 196 | const ZipEntry* twrp = mzFindZipEntry(&zip, "META-INF/teamwin/twrp.zip"); |
| 197 | if (twrp != NULL) |
| 198 | { |
| 199 | unlink("/tmp/twrp.zip"); |
| 200 | fd = creat("/tmp/twrp.zip", 0666); |
| 201 | } |
| 202 | if (fd >= 0 && twrp != NULL && |
| 203 | mzExtractZipEntryToFile(&zip, twrp, fd) && |
| 204 | !PageManager::LoadPackage("install", "/tmp/twrp.zip", "main")) |
| 205 | { |
| 206 | mzCloseZipArchive(&zip); |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 207 | PageManager::SelectPackage("install"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 208 | gui_changePage("main"); |
| 209 | } |
| 210 | else |
| 211 | { |
| 212 | // In this case, we just use the default page |
| 213 | mzCloseZipArchive(&zip); |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 214 | gui_changePage(pageName); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 215 | } |
| 216 | if (fd >= 0) |
| 217 | close(fd); |
| 218 | |
| 219 | if (simulate) { |
| 220 | simulate_progress_bar(); |
| 221 | } else { |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 222 | ret_val = TWinstall_zip(filename.c_str(), wipe_cache); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 223 | |
| 224 | // Now, check if we need to ensure TWRP remains installed... |
| 225 | struct stat st; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 226 | string result; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 227 | if (stat("/sbin/installTwrp", &st) == 0) |
| 228 | { |
| 229 | DataManager::SetValue("tw_operation", "Configuring TWRP"); |
| 230 | DataManager::SetValue("tw_partition", ""); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 231 | gui_print("Configuring TWRP...\n"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 232 | if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall", result) < 0) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 233 | { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 234 | gui_print("Unable to configure TWRP with this kernel.\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | // Done |
| 240 | DataManager::SetValue("ui_progress", 100); |
| 241 | DataManager::SetValue("ui_progress", 0); |
| 242 | return ret_val; |
| 243 | } |
| 244 | |
| 245 | int GUIAction::doActions() |
| 246 | { |
| 247 | if (mActions.size() < 1) return -1; |
| 248 | if (mActions.size() == 1) |
| 249 | return doAction(mActions.at(0), 0); |
| 250 | |
| 251 | // For multi-action, we always use a thread |
| 252 | pthread_t t; |
Dees_Troy | ab4963c | 2013-01-16 20:35:51 +0000 | [diff] [blame] | 253 | pthread_attr_t tattr; |
| 254 | |
| 255 | if (pthread_attr_init(&tattr)) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 256 | LOGERR("Unable to pthread_attr_init\n"); |
Dees_Troy | ab4963c | 2013-01-16 20:35:51 +0000 | [diff] [blame] | 257 | return -1; |
| 258 | } |
| 259 | if (pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_JOINABLE)) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 260 | LOGERR("Error setting pthread_attr_setdetachstate\n"); |
Dees_Troy | ab4963c | 2013-01-16 20:35:51 +0000 | [diff] [blame] | 261 | return -1; |
| 262 | } |
| 263 | if (pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM)) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 264 | LOGERR("Error setting pthread_attr_setscope\n"); |
Dees_Troy | ab4963c | 2013-01-16 20:35:51 +0000 | [diff] [blame] | 265 | return -1; |
| 266 | } |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 267 | /*if (pthread_attr_setstacksize(&tattr, 524288)) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 268 | LOGERR("Error setting pthread_attr_setstacksize\n"); |
Dees_Troy | ab4963c | 2013-01-16 20:35:51 +0000 | [diff] [blame] | 269 | return -1; |
| 270 | } |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 271 | */ |
Dees_Troy | ab4963c | 2013-01-16 20:35:51 +0000 | [diff] [blame] | 272 | int ret = pthread_create(&t, &tattr, thread_start, this); |
Dees_Troy | ab4963c | 2013-01-16 20:35:51 +0000 | [diff] [blame] | 273 | if (ret) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 274 | LOGERR("Unable to create more threads for actions... continuing in same thread! %i\n", ret); |
Dees_Troy | ab4963c | 2013-01-16 20:35:51 +0000 | [diff] [blame] | 275 | thread_start(this); |
| 276 | } else { |
| 277 | if (pthread_join(t, NULL)) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 278 | LOGERR("Error joining threads\n"); |
Dees_Troy | ab4963c | 2013-01-16 20:35:51 +0000 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | if (pthread_attr_destroy(&tattr)) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 282 | LOGERR("Failed to pthread_attr_destroy\n"); |
Dees_Troy | ab4963c | 2013-01-16 20:35:51 +0000 | [diff] [blame] | 283 | return -1; |
| 284 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 285 | |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | void* GUIAction::thread_start(void *cookie) |
| 290 | { |
| 291 | GUIAction* ourThis = (GUIAction*) cookie; |
| 292 | |
| 293 | DataManager::SetValue(TW_ACTION_BUSY, 1); |
| 294 | |
| 295 | if (ourThis->mActions.size() > 1) |
| 296 | { |
| 297 | std::vector<Action>::iterator iter; |
| 298 | for (iter = ourThis->mActions.begin(); iter != ourThis->mActions.end(); iter++) |
| 299 | ourThis->doAction(*iter, 1); |
| 300 | } |
| 301 | else |
| 302 | { |
| 303 | ourThis->doAction(ourThis->mActions.at(0), 1); |
| 304 | } |
| 305 | int check = 0; |
| 306 | DataManager::GetValue("tw_background_thread_running", check); |
| 307 | if (check == 0) |
| 308 | DataManager::SetValue(TW_ACTION_BUSY, 0); |
| 309 | return NULL; |
| 310 | } |
| 311 | |
| 312 | void GUIAction::operation_start(const string operation_name) |
| 313 | { |
| 314 | DataManager::SetValue(TW_ACTION_BUSY, 1); |
| 315 | DataManager::SetValue("ui_progress", 0); |
| 316 | DataManager::SetValue("tw_operation", operation_name); |
| 317 | DataManager::SetValue("tw_operation_status", 0); |
| 318 | DataManager::SetValue("tw_operation_state", 0); |
| 319 | } |
| 320 | |
| 321 | void GUIAction::operation_end(const int operation_status, const int simulate) |
| 322 | { |
| 323 | int simulate_fail; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 324 | DataManager::SetValue("ui_progress", 100); |
| 325 | if (simulate) { |
| 326 | DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail); |
| 327 | if (simulate_fail != 0) |
| 328 | DataManager::SetValue("tw_operation_status", 1); |
| 329 | else |
| 330 | DataManager::SetValue("tw_operation_status", 0); |
| 331 | } else { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 332 | if (operation_status != 0) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 333 | DataManager::SetValue("tw_operation_status", 1); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 334 | } |
| 335 | else { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 336 | DataManager::SetValue("tw_operation_status", 0); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 337 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 338 | } |
| 339 | DataManager::SetValue("tw_operation_state", 1); |
| 340 | DataManager::SetValue(TW_ACTION_BUSY, 0); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 341 | blankTimer.resetTimerAndUnblank(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | int GUIAction::doAction(Action action, int isThreaded /* = 0 */) |
| 345 | { |
| 346 | static string zip_queue[10]; |
| 347 | static int zip_queue_index; |
| 348 | static pthread_t terminal_command; |
| 349 | int simulate; |
| 350 | |
| 351 | std::string arg = gui_parse_text(action.mArg); |
| 352 | |
| 353 | std::string function = gui_parse_text(action.mFunction); |
| 354 | |
| 355 | DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate); |
| 356 | |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 357 | if (function == "reboot") |
| 358 | { |
| 359 | //curtainClose(); this sometimes causes a crash |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 360 | |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 361 | sync(); |
| 362 | DataManager::SetValue("tw_gui_done", 1); |
| 363 | DataManager::SetValue("tw_reboot_arg", arg); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 364 | |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 365 | return 0; |
| 366 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 367 | if (function == "home") |
| 368 | { |
| 369 | PageManager::SelectPackage("TWRP"); |
| 370 | gui_changePage("main"); |
| 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | if (function == "key") |
| 375 | { |
| 376 | PageManager::NotifyKey(getKeyByName(arg)); |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | if (function == "page") { |
| 381 | std::string page_name = gui_parse_text(arg); |
| 382 | return gui_changePage(page_name); |
| 383 | } |
| 384 | |
| 385 | if (function == "reload") { |
| 386 | int check = 0, ret_val = 0; |
| 387 | std::string theme_path; |
| 388 | |
| 389 | operation_start("Reload Theme"); |
| 390 | theme_path = DataManager::GetSettingsStoragePath(); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 391 | if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 392 | LOGERR("Unable to mount %s during reload function startup.\n", theme_path.c_str()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 393 | check = 1; |
| 394 | } |
| 395 | |
| 396 | theme_path += "/TWRP/theme/ui.zip"; |
| 397 | if (check != 0 || PageManager::ReloadPackage("TWRP", theme_path) != 0) |
| 398 | { |
| 399 | // Loading the custom theme failed - try loading the stock theme |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 400 | LOGINFO("Attempting to reload stock theme...\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 401 | if (PageManager::ReloadPackage("TWRP", "/res/ui.xml")) |
| 402 | { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 403 | LOGERR("Failed to load base packages.\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 404 | ret_val = 1; |
| 405 | } |
| 406 | } |
| 407 | operation_end(ret_val, simulate); |
| 408 | } |
| 409 | |
| 410 | if (function == "readBackup") |
| 411 | { |
| 412 | string Restore_Name; |
| 413 | DataManager::GetValue("tw_restore", Restore_Name); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 414 | PartitionManager.Set_Restore_Files(Restore_Name); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | if (function == "set") |
| 419 | { |
| 420 | if (arg.find('=') != string::npos) |
| 421 | { |
| 422 | string varName = arg.substr(0, arg.find('=')); |
| 423 | string value = arg.substr(arg.find('=') + 1, string::npos); |
| 424 | |
| 425 | DataManager::GetValue(value, value); |
| 426 | DataManager::SetValue(varName, value); |
| 427 | } |
| 428 | else |
| 429 | DataManager::SetValue(arg, "1"); |
| 430 | return 0; |
| 431 | } |
| 432 | if (function == "clear") |
| 433 | { |
| 434 | DataManager::SetValue(arg, "0"); |
| 435 | return 0; |
| 436 | } |
| 437 | |
| 438 | if (function == "mount") |
| 439 | { |
| 440 | if (arg == "usb") |
| 441 | { |
| 442 | DataManager::SetValue(TW_ACTION_BUSY, 1); |
| 443 | if (!simulate) |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 444 | PartitionManager.usb_storage_enable(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 445 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 446 | gui_print("Simulating actions...\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 447 | } |
| 448 | else if (!simulate) |
| 449 | { |
| 450 | string cmd; |
| 451 | if (arg == "EXTERNAL") |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 452 | PartitionManager.Mount_By_Path(DataManager::GetStrValue(TW_EXTERNAL_MOUNT), true); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 453 | else if (arg == "INTERNAL") |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 454 | PartitionManager.Mount_By_Path(DataManager::GetStrValue(TW_INTERNAL_MOUNT), true); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 455 | else |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 456 | PartitionManager.Mount_By_Path(arg, true); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 457 | } else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 458 | gui_print("Simulating actions...\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 459 | return 0; |
| 460 | } |
| 461 | |
| 462 | if (function == "umount" || function == "unmount") |
| 463 | { |
| 464 | if (arg == "usb") |
| 465 | { |
| 466 | if (!simulate) |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 467 | PartitionManager.usb_storage_disable(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 468 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 469 | gui_print("Simulating actions...\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 470 | DataManager::SetValue(TW_ACTION_BUSY, 0); |
| 471 | } |
| 472 | else if (!simulate) |
| 473 | { |
| 474 | string cmd; |
| 475 | if (arg == "EXTERNAL") |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 476 | PartitionManager.UnMount_By_Path(DataManager::GetStrValue(TW_EXTERNAL_MOUNT), true); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 477 | else if (arg == "INTERNAL") |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 478 | PartitionManager.UnMount_By_Path(DataManager::GetStrValue(TW_INTERNAL_MOUNT), true); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 479 | else |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 480 | PartitionManager.UnMount_By_Path(arg, true); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 481 | } else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 482 | gui_print("Simulating actions...\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 483 | return 0; |
| 484 | } |
| 485 | |
| 486 | if (function == "restoredefaultsettings") |
| 487 | { |
| 488 | operation_start("Restore Defaults"); |
| 489 | if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 490 | gui_print("Simulating actions...\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 491 | else { |
| 492 | DataManager::ResetDefaults(); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 493 | PartitionManager.Update_System_Details(); |
| 494 | PartitionManager.Mount_Current_Storage(true); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 495 | } |
| 496 | operation_end(0, simulate); |
| 497 | } |
| 498 | |
| 499 | if (function == "copylog") |
| 500 | { |
| 501 | operation_start("Copy Log"); |
| 502 | if (!simulate) |
| 503 | { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 504 | string dst; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 505 | PartitionManager.Mount_Current_Storage(true); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 506 | dst = DataManager::GetCurrentStoragePath() + "/recovery.log"; |
| 507 | TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 508 | sync(); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 509 | gui_print("Copied recovery log to %s.\n", DataManager::GetCurrentStoragePath().c_str()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 510 | } else |
| 511 | simulate_progress_bar(); |
| 512 | operation_end(0, simulate); |
| 513 | return 0; |
| 514 | } |
| 515 | |
| 516 | if (function == "compute" || function == "addsubtract") |
| 517 | { |
| 518 | if (arg.find("+") != string::npos) |
| 519 | { |
| 520 | string varName = arg.substr(0, arg.find('+')); |
| 521 | string string_to_add = arg.substr(arg.find('+') + 1, string::npos); |
| 522 | int amount_to_add = atoi(string_to_add.c_str()); |
| 523 | int value; |
| 524 | |
| 525 | DataManager::GetValue(varName, value); |
| 526 | DataManager::SetValue(varName, value + amount_to_add); |
| 527 | return 0; |
| 528 | } |
| 529 | if (arg.find("-") != string::npos) |
| 530 | { |
| 531 | string varName = arg.substr(0, arg.find('-')); |
| 532 | string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos); |
| 533 | int amount_to_subtract = atoi(string_to_subtract.c_str()); |
| 534 | int value; |
| 535 | |
| 536 | DataManager::GetValue(varName, value); |
| 537 | value -= amount_to_subtract; |
| 538 | if (value <= 0) |
| 539 | value = 0; |
| 540 | DataManager::SetValue(varName, value); |
| 541 | return 0; |
| 542 | } |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 543 | if (arg.find("*") != string::npos) |
| 544 | { |
| 545 | string varName = arg.substr(0, arg.find('*')); |
| 546 | string multiply_by_str = gui_parse_text(arg.substr(arg.find('*') + 1, string::npos)); |
| 547 | int multiply_by = atoi(multiply_by_str.c_str()); |
| 548 | int value; |
| 549 | |
| 550 | DataManager::GetValue(varName, value); |
| 551 | DataManager::SetValue(varName, value*multiply_by); |
| 552 | return 0; |
| 553 | } |
| 554 | if (arg.find("/") != string::npos) |
| 555 | { |
| 556 | string varName = arg.substr(0, arg.find('/')); |
| 557 | string divide_by_str = gui_parse_text(arg.substr(arg.find('/') + 1, string::npos)); |
| 558 | int divide_by = atoi(divide_by_str.c_str()); |
| 559 | int value; |
| 560 | |
| 561 | if(divide_by != 0) |
| 562 | { |
| 563 | DataManager::GetValue(varName, value); |
| 564 | DataManager::SetValue(varName, value/divide_by); |
| 565 | } |
| 566 | return 0; |
| 567 | } |
| 568 | LOGERR("Unable to perform compute '%s'\n", arg.c_str()); |
| 569 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | if (function == "setguitimezone") |
| 573 | { |
| 574 | string SelectedZone; |
| 575 | DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone |
| 576 | string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone |
| 577 | string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component |
| 578 | |
| 579 | int dst; |
| 580 | DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST |
| 581 | |
| 582 | string offset; |
| 583 | DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset |
| 584 | |
| 585 | string NewTimeZone = Zone; |
| 586 | if (offset != "0") |
| 587 | NewTimeZone += ":" + offset; |
| 588 | |
| 589 | if (dst != 0) |
| 590 | NewTimeZone += DSTZone; |
| 591 | |
| 592 | DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 593 | DataManager::update_tz_environment_variables(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 594 | return 0; |
| 595 | } |
| 596 | |
| 597 | if (function == "togglestorage") { |
| 598 | if (arg == "internal") { |
| 599 | DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0); |
| 600 | } else if (arg == "external") { |
| 601 | DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1); |
| 602 | } |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 603 | if (PartitionManager.Mount_Current_Storage(true)) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 604 | if (arg == "internal") { |
Dees_Troy | e2920fa | 2012-09-19 16:18:00 -0400 | [diff] [blame] | 605 | string zip_path, zip_root; |
| 606 | DataManager::GetValue(TW_ZIP_INTERNAL_VAR, zip_path); |
| 607 | zip_root = TWFunc::Get_Root_Path(zip_path); |
| 608 | #ifdef RECOVERY_SDCARD_ON_DATA |
| 609 | #ifndef TW_EXTERNAL_STORAGE_PATH |
| 610 | if (zip_root != "/sdcard") |
| 611 | DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/sdcard"); |
| 612 | #else |
| 613 | if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) { |
| 614 | if (zip_root != "/emmc") |
| 615 | DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/emmc"); |
| 616 | } else { |
| 617 | if (zip_root != "/sdcard") |
| 618 | DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/sdcard"); |
| 619 | } |
| 620 | #endif |
| 621 | #else |
| 622 | if (zip_root != DataManager::GetCurrentStoragePath()) |
| 623 | DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetCurrentStoragePath()); |
| 624 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 625 | // Save the current zip location to the external variable |
| 626 | DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, DataManager::GetStrValue(TW_ZIP_LOCATION_VAR)); |
| 627 | // Change the current zip location to the internal variable |
| 628 | DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetStrValue(TW_ZIP_INTERNAL_VAR)); |
| 629 | } else if (arg == "external") { |
Dees_Troy | e2920fa | 2012-09-19 16:18:00 -0400 | [diff] [blame] | 630 | string zip_path, zip_root; |
| 631 | DataManager::GetValue(TW_ZIP_EXTERNAL_VAR, zip_path); |
| 632 | zip_root = TWFunc::Get_Root_Path(zip_path); |
| 633 | if (zip_root != DataManager::GetCurrentStoragePath()) { |
| 634 | DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, DataManager::GetCurrentStoragePath()); |
| 635 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 636 | // Save the current zip location to the internal variable |
| 637 | DataManager::SetValue(TW_ZIP_INTERNAL_VAR, DataManager::GetStrValue(TW_ZIP_LOCATION_VAR)); |
| 638 | // Change the current zip location to the external variable |
| 639 | DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetStrValue(TW_ZIP_EXTERNAL_VAR)); |
| 640 | } |
| 641 | } else { |
| 642 | // We weren't able to toggle for some reason, restore original setting |
| 643 | if (arg == "internal") { |
| 644 | DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1); |
| 645 | } else if (arg == "external") { |
| 646 | DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0); |
| 647 | } |
| 648 | } |
| 649 | return 0; |
| 650 | } |
| 651 | |
| 652 | if (function == "overlay") |
| 653 | return gui_changeOverlay(arg); |
| 654 | |
| 655 | if (function == "queuezip") |
| 656 | { |
| 657 | if (zip_queue_index >= 10) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 658 | gui_print("Maximum zip queue reached!\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 659 | return 0; |
| 660 | } |
| 661 | DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]); |
| 662 | if (strlen(zip_queue[zip_queue_index].c_str()) > 0) { |
| 663 | zip_queue_index++; |
| 664 | DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index); |
| 665 | } |
| 666 | return 0; |
| 667 | } |
| 668 | |
| 669 | if (function == "cancelzip") |
| 670 | { |
| 671 | if (zip_queue_index <= 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 672 | gui_print("Minimum zip queue reached!\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 673 | return 0; |
| 674 | } else { |
| 675 | zip_queue_index--; |
| 676 | DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index); |
| 677 | } |
| 678 | return 0; |
| 679 | } |
| 680 | |
| 681 | if (function == "queueclear") |
| 682 | { |
| 683 | zip_queue_index = 0; |
| 684 | DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index); |
| 685 | return 0; |
| 686 | } |
| 687 | |
| 688 | if (function == "sleep") |
| 689 | { |
| 690 | operation_start("Sleep"); |
| 691 | usleep(atoi(arg.c_str())); |
| 692 | operation_end(0, simulate); |
| 693 | return 0; |
| 694 | } |
| 695 | |
| 696 | if (isThreaded) |
| 697 | { |
| 698 | if (function == "fileexists") |
| 699 | { |
| 700 | struct stat st; |
| 701 | string newpath = arg + "/."; |
| 702 | |
| 703 | operation_start("FileExists"); |
| 704 | if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0) |
| 705 | operation_end(0, simulate); |
| 706 | else |
| 707 | operation_end(1, simulate); |
| 708 | } |
| 709 | |
| 710 | if (function == "flash") |
| 711 | { |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 712 | int i, ret_val = 0, wipe_cache = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 713 | |
| 714 | for (i=0; i<zip_queue_index; i++) { |
| 715 | operation_start("Flashing"); |
| 716 | DataManager::SetValue("tw_filename", zip_queue[i]); |
| 717 | DataManager::SetValue(TW_ZIP_INDEX, (i + 1)); |
| 718 | |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 719 | ret_val = flash_zip(zip_queue[i], arg, simulate, &wipe_cache); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 720 | if (ret_val != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 721 | gui_print("Error flashing zip '%s'\n", zip_queue[i].c_str()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 722 | i = 10; // Error flashing zip - exit queue |
| 723 | ret_val = 1; |
| 724 | } |
| 725 | } |
| 726 | zip_queue_index = 0; |
| 727 | DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index); |
| 728 | |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 729 | if (wipe_cache) |
| 730 | PartitionManager.Wipe_By_Path("/cache"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 731 | string result; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 732 | if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) { |
| 733 | operation_start("ReinjectTWRP"); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 734 | gui_print("Injecting TWRP into boot image...\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 735 | if (simulate) { |
| 736 | simulate_progress_bar(); |
| 737 | } else { |
Dees_Troy | 06b4fe9 | 2012-10-16 11:43:20 -0400 | [diff] [blame] | 738 | TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot"); |
| 739 | if (Boot == NULL || Boot->Current_File_System != "emmc") |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 740 | TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash", result); |
Dees_Troy | 06b4fe9 | 2012-10-16 11:43:20 -0400 | [diff] [blame] | 741 | else { |
| 742 | string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 743 | TWFunc::Exec_Cmd(injectcmd, result); |
Dees_Troy | 06b4fe9 | 2012-10-16 11:43:20 -0400 | [diff] [blame] | 744 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 745 | gui_print("TWRP injection complete.\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 746 | } |
| 747 | } |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 748 | PartitionManager.Update_System_Details(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 749 | operation_end(ret_val, simulate); |
| 750 | return 0; |
| 751 | } |
| 752 | if (function == "wipe") |
| 753 | { |
| 754 | operation_start("Format"); |
| 755 | DataManager::SetValue("tw_partition", arg); |
| 756 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 757 | int ret_val = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 758 | |
| 759 | if (simulate) { |
| 760 | simulate_progress_bar(); |
| 761 | } else { |
| 762 | if (arg == "data") |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 763 | ret_val = PartitionManager.Factory_Reset(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 764 | else if (arg == "battery") |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 765 | ret_val = PartitionManager.Wipe_Battery_Stats(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 766 | else if (arg == "rotate") |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 767 | ret_val = PartitionManager.Wipe_Rotate_Data(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 768 | else if (arg == "dalvik") |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 769 | ret_val = PartitionManager.Wipe_Dalvik_Cache(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 770 | else if (arg == "DATAMEDIA") { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 771 | ret_val = PartitionManager.Format_Data(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 772 | } else if (arg == "INTERNAL") { |
| 773 | int has_datamedia, dual_storage; |
| 774 | |
| 775 | DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia); |
| 776 | if (has_datamedia) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 777 | ret_val = PartitionManager.Wipe_Media_From_Data(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 778 | } else { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 779 | ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 780 | } |
| 781 | } else if (arg == "EXTERNAL") { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 782 | string External_Path; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 783 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 784 | DataManager::GetValue(TW_EXTERNAL_PATH, External_Path); |
| 785 | ret_val = PartitionManager.Wipe_By_Path(External_Path); |
Dees_Troy | 2ff5a8d | 2012-09-26 14:53:02 -0400 | [diff] [blame] | 786 | } else if (arg == "ANDROIDSECURE") { |
| 787 | ret_val = PartitionManager.Wipe_Android_Secure(); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 788 | } else if (arg == "LIST") { |
| 789 | string Wipe_List, wipe_path; |
| 790 | bool skip = false; |
| 791 | ret_val = true; |
| 792 | TWPartition* wipe_part = NULL; |
| 793 | |
| 794 | DataManager::GetValue("tw_wipe_list", Wipe_List); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 795 | LOGINFO("wipe list '%s'\n", Wipe_List.c_str()); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 796 | if (!Wipe_List.empty()) { |
| 797 | size_t start_pos = 0, end_pos = Wipe_List.find(";", start_pos); |
| 798 | while (end_pos != string::npos && start_pos < Wipe_List.size()) { |
| 799 | wipe_path = Wipe_List.substr(start_pos, end_pos - start_pos); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 800 | LOGINFO("wipe_path '%s'\n", wipe_path.c_str()); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 801 | if (wipe_path == "/and-sec") { |
| 802 | if (!PartitionManager.Wipe_Android_Secure()) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 803 | LOGERR("Unable to wipe android secure\n"); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 804 | ret_val = false; |
| 805 | break; |
| 806 | } else { |
| 807 | skip = true; |
| 808 | } |
| 809 | } else if (wipe_path == "DALVIK") { |
| 810 | if (!PartitionManager.Wipe_Dalvik_Cache()) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 811 | LOGERR("Failed to wipe dalvik\n"); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 812 | ret_val = false; |
| 813 | break; |
| 814 | } else { |
| 815 | skip = true; |
| 816 | } |
| 817 | } |
| 818 | if (!skip) { |
| 819 | if (!PartitionManager.Wipe_By_Path(wipe_path)) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 820 | LOGERR("Unable to wipe '%s'\n", wipe_path.c_str()); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 821 | ret_val = false; |
| 822 | break; |
| 823 | } else if (wipe_path == DataManager::GetSettingsStoragePath()) { |
| 824 | arg = wipe_path; |
| 825 | } |
| 826 | } else { |
| 827 | skip = false; |
| 828 | } |
| 829 | start_pos = end_pos + 1; |
| 830 | end_pos = Wipe_List.find(";", start_pos); |
| 831 | } |
| 832 | } |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 833 | } else |
| 834 | ret_val = PartitionManager.Wipe_By_Path(arg); |
| 835 | |
| 836 | if (arg == DataManager::GetSettingsStoragePath()) { |
| 837 | // If we wiped the settings storage path, recreate the TWRP folder and dump the settings |
| 838 | string Storage_Path = DataManager::GetSettingsStoragePath(); |
| 839 | |
| 840 | if (PartitionManager.Mount_By_Path(Storage_Path, true)) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 841 | LOGINFO("Making TWRP folder and saving settings.\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 842 | Storage_Path += "/TWRP"; |
| 843 | mkdir(Storage_Path.c_str(), 0777); |
| 844 | DataManager::Flush(); |
| 845 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 846 | LOGERR("Unable to recreate TWRP folder and save settings.\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 847 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 848 | } |
| 849 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 850 | PartitionManager.Update_System_Details(); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 851 | if (ret_val) |
| 852 | ret_val = 0; // 0 is success |
| 853 | else |
| 854 | ret_val = 1; // 1 is failure |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 855 | operation_end(ret_val, simulate); |
| 856 | return 0; |
| 857 | } |
| 858 | if (function == "refreshsizes") |
| 859 | { |
| 860 | operation_start("Refreshing Sizes"); |
| 861 | if (simulate) { |
| 862 | simulate_progress_bar(); |
| 863 | } else |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 864 | PartitionManager.Update_System_Details(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 865 | operation_end(0, simulate); |
| 866 | } |
| 867 | if (function == "nandroid") |
| 868 | { |
| 869 | operation_start("Nandroid"); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 870 | int ret = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 871 | |
| 872 | if (simulate) { |
| 873 | DataManager::SetValue("tw_partition", "Simulation"); |
| 874 | simulate_progress_bar(); |
| 875 | } else { |
| 876 | if (arg == "backup") { |
| 877 | string Backup_Name; |
| 878 | DataManager::GetValue(TW_BACKUP_NAME, Backup_Name); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 879 | if (Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(true) == 0) { |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 880 | ret = PartitionManager.Run_Backup(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 881 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 882 | else { |
| 883 | operation_end(1, simulate); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 884 | return -1; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 885 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 886 | DataManager::SetValue(TW_BACKUP_NAME, "(Current Date)"); |
| 887 | } else if (arg == "restore") { |
| 888 | string Restore_Name; |
| 889 | DataManager::GetValue("tw_restore", Restore_Name); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 890 | ret = PartitionManager.Run_Restore(Restore_Name); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 891 | } else { |
| 892 | operation_end(1, simulate); |
| 893 | return -1; |
| 894 | } |
| 895 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 896 | if (ret == false) |
| 897 | ret = 1; // 1 for failure |
| 898 | else |
| 899 | ret = 0; // 0 for success |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 900 | operation_end(ret, simulate); |
| 901 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 902 | } |
| 903 | if (function == "fixpermissions") |
| 904 | { |
| 905 | operation_start("Fix Permissions"); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 906 | LOGINFO("fix permissions started!\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 907 | if (simulate) { |
| 908 | simulate_progress_bar(); |
Dees_Troy | 4be841b | 2012-09-26 14:07:15 -0400 | [diff] [blame] | 909 | } else { |
Dees_Troy | 6480ce0 | 2012-10-10 10:26:54 -0400 | [diff] [blame] | 910 | int op_status = PartitionManager.Fix_Permissions(); |
| 911 | if (op_status != 0) |
| 912 | op_status = 1; // failure |
| 913 | operation_end(op_status, simulate); |
Dees_Troy | 4be841b | 2012-09-26 14:07:15 -0400 | [diff] [blame] | 914 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 915 | return 0; |
| 916 | } |
| 917 | if (function == "dd") |
| 918 | { |
| 919 | operation_start("imaging"); |
| 920 | |
| 921 | if (simulate) { |
| 922 | simulate_progress_bar(); |
| 923 | } else { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 924 | string result; |
| 925 | string cmd = "dd " + arg; |
| 926 | TWFunc::Exec_Cmd(cmd, result); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 927 | } |
| 928 | operation_end(0, simulate); |
| 929 | return 0; |
| 930 | } |
| 931 | if (function == "partitionsd") |
| 932 | { |
| 933 | operation_start("Partition SD Card"); |
Dees_Troy | 9350b8d | 2012-09-27 12:38:38 -0400 | [diff] [blame] | 934 | int ret_val = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 935 | |
| 936 | if (simulate) { |
| 937 | simulate_progress_bar(); |
| 938 | } else { |
| 939 | int allow_partition; |
| 940 | DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition); |
| 941 | if (allow_partition == 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 942 | gui_print("This device does not have a real SD Card!\nAborting!\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 943 | } else { |
Dees_Troy | 9350b8d | 2012-09-27 12:38:38 -0400 | [diff] [blame] | 944 | if (!PartitionManager.Partition_SDCard()) |
| 945 | ret_val = 1; // failed |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 946 | } |
| 947 | } |
Dees_Troy | 9350b8d | 2012-09-27 12:38:38 -0400 | [diff] [blame] | 948 | operation_end(ret_val, simulate); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 949 | return 0; |
| 950 | } |
| 951 | if (function == "installhtcdumlock") |
| 952 | { |
| 953 | operation_start("Install HTC Dumlock"); |
| 954 | if (simulate) { |
| 955 | simulate_progress_bar(); |
| 956 | } else |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 957 | TWFunc::install_htc_dumlock(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 958 | |
| 959 | operation_end(0, simulate); |
| 960 | return 0; |
| 961 | } |
| 962 | if (function == "htcdumlockrestoreboot") |
| 963 | { |
| 964 | operation_start("HTC Dumlock Restore Boot"); |
| 965 | if (simulate) { |
| 966 | simulate_progress_bar(); |
| 967 | } else |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 968 | TWFunc::htc_dumlock_restore_original_boot(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 969 | |
| 970 | operation_end(0, simulate); |
| 971 | return 0; |
| 972 | } |
| 973 | if (function == "htcdumlockreflashrecovery") |
| 974 | { |
| 975 | operation_start("HTC Dumlock Reflash Recovery"); |
| 976 | if (simulate) { |
| 977 | simulate_progress_bar(); |
| 978 | } else |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 979 | TWFunc::htc_dumlock_reflash_recovery_to_boot(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 980 | |
| 981 | operation_end(0, simulate); |
| 982 | return 0; |
| 983 | } |
| 984 | if (function == "cmd") |
| 985 | { |
| 986 | int op_status = 0; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 987 | string result; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 988 | |
| 989 | operation_start("Command"); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 990 | LOGINFO("Running command: '%s'\n", arg.c_str()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 991 | if (simulate) { |
| 992 | simulate_progress_bar(); |
| 993 | } else { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 994 | op_status = TWFunc::Exec_Cmd(arg, result); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 995 | if (op_status != 0) |
| 996 | op_status = 1; |
| 997 | } |
| 998 | |
| 999 | operation_end(op_status, simulate); |
| 1000 | return 0; |
| 1001 | } |
| 1002 | if (function == "terminalcommand") |
| 1003 | { |
| 1004 | int op_status = 0; |
| 1005 | string cmdpath, command; |
| 1006 | |
| 1007 | DataManager::GetValue("tw_terminal_location", cmdpath); |
| 1008 | operation_start("CommandOutput"); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1009 | gui_print("%s # %s\n", cmdpath.c_str(), arg.c_str()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1010 | if (simulate) { |
| 1011 | simulate_progress_bar(); |
| 1012 | operation_end(op_status, simulate); |
| 1013 | } else { |
Dees_Troy | 4be841b | 2012-09-26 14:07:15 -0400 | [diff] [blame] | 1014 | command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1015 | LOGINFO("Actual command is: '%s'\n", command.c_str()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1016 | DataManager::SetValue("tw_terminal_command_thread", command); |
| 1017 | DataManager::SetValue("tw_terminal_state", 1); |
| 1018 | DataManager::SetValue("tw_background_thread_running", 1); |
| 1019 | op_status = pthread_create(&terminal_command, NULL, command_thread, NULL); |
| 1020 | if (op_status != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1021 | LOGERR("Error starting terminal command thread, %i.\n", op_status); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1022 | DataManager::SetValue("tw_terminal_state", 0); |
| 1023 | DataManager::SetValue("tw_background_thread_running", 0); |
| 1024 | operation_end(1, simulate); |
| 1025 | } |
| 1026 | } |
| 1027 | return 0; |
| 1028 | } |
| 1029 | if (function == "killterminal") |
| 1030 | { |
| 1031 | int op_status = 0; |
| 1032 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1033 | LOGINFO("Sending kill command...\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1034 | operation_start("KillCommand"); |
| 1035 | DataManager::SetValue("tw_operation_status", 0); |
| 1036 | DataManager::SetValue("tw_operation_state", 1); |
| 1037 | DataManager::SetValue("tw_terminal_state", 0); |
| 1038 | DataManager::SetValue("tw_background_thread_running", 0); |
| 1039 | DataManager::SetValue(TW_ACTION_BUSY, 0); |
| 1040 | return 0; |
| 1041 | } |
| 1042 | if (function == "reinjecttwrp") |
| 1043 | { |
| 1044 | int op_status = 0; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1045 | string result; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1046 | operation_start("ReinjectTWRP"); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1047 | gui_print("Injecting TWRP into boot image...\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1048 | if (simulate) { |
| 1049 | simulate_progress_bar(); |
| 1050 | } else { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1051 | TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash", result); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1052 | gui_print("TWRP injection complete.\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | operation_end(op_status, simulate); |
| 1056 | return 0; |
| 1057 | } |
| 1058 | if (function == "checkbackupname") |
| 1059 | { |
| 1060 | int op_status = 0; |
| 1061 | |
| 1062 | operation_start("CheckBackupName"); |
| 1063 | if (simulate) { |
| 1064 | simulate_progress_bar(); |
| 1065 | } else { |
Dees_Troy | c9ff7a3 | 2012-09-27 10:09:41 -0400 | [diff] [blame] | 1066 | op_status = PartitionManager.Check_Backup_Name(true); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1067 | if (op_status != 0) |
| 1068 | op_status = 1; |
| 1069 | } |
| 1070 | |
| 1071 | operation_end(op_status, simulate); |
| 1072 | return 0; |
| 1073 | } |
| 1074 | if (function == "decrypt") |
| 1075 | { |
| 1076 | int op_status = 0; |
| 1077 | |
| 1078 | operation_start("Decrypt"); |
| 1079 | if (simulate) { |
| 1080 | simulate_progress_bar(); |
| 1081 | } else { |
| 1082 | string Password; |
| 1083 | DataManager::GetValue("tw_crypto_password", Password); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1084 | op_status = PartitionManager.Decrypt_Device(Password); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1085 | if (op_status != 0) |
| 1086 | op_status = 1; |
| 1087 | else { |
| 1088 | int load_theme = 1; |
| 1089 | |
| 1090 | DataManager::SetValue(TW_IS_ENCRYPTED, 0); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1091 | |
| 1092 | if (load_theme) { |
| 1093 | int has_datamedia; |
| 1094 | |
| 1095 | // Check for a custom theme and load it if exists |
| 1096 | DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia); |
| 1097 | if (has_datamedia != 0) { |
| 1098 | struct stat st; |
| 1099 | int check = 0; |
| 1100 | std::string theme_path; |
| 1101 | |
| 1102 | theme_path = DataManager::GetSettingsStoragePath(); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1103 | if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1104 | LOGERR("Unable to mount %s during reload function startup.\n", theme_path.c_str()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1105 | check = 1; |
| 1106 | } |
| 1107 | |
| 1108 | theme_path += "/TWRP/theme/ui.zip"; |
| 1109 | if (check == 0 && stat(theme_path.c_str(), &st) == 0) { |
| 1110 | if (PageManager::ReloadPackage("TWRP", theme_path) != 0) |
| 1111 | { |
| 1112 | // Loading the custom theme failed - try loading the stock theme |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1113 | LOGINFO("Attempting to reload stock theme...\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1114 | if (PageManager::ReloadPackage("TWRP", "/res/ui.xml")) |
| 1115 | { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1116 | LOGERR("Failed to load base packages.\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1117 | } |
| 1118 | } |
| 1119 | } |
| 1120 | } |
| 1121 | } |
| 1122 | } |
| 1123 | } |
| 1124 | |
| 1125 | operation_end(op_status, simulate); |
| 1126 | return 0; |
| 1127 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1128 | if (function == "adbsideload") |
| 1129 | { |
| 1130 | int ret = 0; |
| 1131 | |
| 1132 | operation_start("Sideload"); |
| 1133 | if (simulate) { |
| 1134 | simulate_progress_bar(); |
| 1135 | } else { |
| 1136 | int wipe_cache = 0; |
bigbiff bigbiff | 7ce7f0c | 2013-01-25 09:54:04 -0500 | [diff] [blame] | 1137 | int wipe_dalvik = 0; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1138 | string result, Sideload_File; |
Dees_Troy | cfb63ae | 2012-09-19 14:30:17 -0400 | [diff] [blame] | 1139 | |
| 1140 | if (!PartitionManager.Mount_Current_Storage(true)) { |
| 1141 | operation_end(1, simulate); |
| 1142 | return 0; |
| 1143 | } |
Dees_Troy | 9a4b569 | 2012-09-19 15:09:45 -0400 | [diff] [blame] | 1144 | Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip"; |
| 1145 | if (TWFunc::Path_Exists(Sideload_File)) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1146 | unlink(Sideload_File.c_str()); |
Dees_Troy | cfb63ae | 2012-09-19 14:30:17 -0400 | [diff] [blame] | 1147 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1148 | gui_print("Starting ADB sideload feature...\n"); |
bigbiff bigbiff | 7ce7f0c | 2013-01-25 09:54:04 -0500 | [diff] [blame] | 1149 | DataManager::GetValue("tw_wipe_dalvik", wipe_dalvik); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1150 | ret = apply_from_adb(Sideload_File.c_str()); |
| 1151 | DataManager::SetValue("tw_has_cancel", 0); // Remove cancel button from gui now that the zip install is going to start |
bigbiff bigbiff | 7ce7f0c | 2013-01-25 09:54:04 -0500 | [diff] [blame] | 1152 | if (ret != 0) { |
Dees_Troy | cfb63ae | 2012-09-19 14:30:17 -0400 | [diff] [blame] | 1153 | ret = 1; // failure |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1154 | } else if (TWinstall_zip(Sideload_File.c_str(), &wipe_cache) == 0) { |
bigbiff bigbiff | 7ce7f0c | 2013-01-25 09:54:04 -0500 | [diff] [blame] | 1155 | if (wipe_cache || DataManager::GetIntValue("tw_wipe_cache")) |
| 1156 | PartitionManager.Wipe_By_Path("/cache"); |
| 1157 | if (wipe_dalvik) |
| 1158 | PartitionManager.Wipe_Dalvik_Cache(); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1159 | } else { |
| 1160 | ret = 1; // failure |
bigbiff bigbiff | 7ce7f0c | 2013-01-25 09:54:04 -0500 | [diff] [blame] | 1161 | } |
Dees_Troy | 06b4fe9 | 2012-10-16 11:43:20 -0400 | [diff] [blame] | 1162 | if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) { |
| 1163 | operation_start("ReinjectTWRP"); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1164 | gui_print("Injecting TWRP into boot image...\n"); |
Dees_Troy | 06b4fe9 | 2012-10-16 11:43:20 -0400 | [diff] [blame] | 1165 | if (simulate) { |
| 1166 | simulate_progress_bar(); |
| 1167 | } else { |
| 1168 | TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot"); |
| 1169 | if (Boot == NULL || Boot->Current_File_System != "emmc") |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1170 | TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash", result); |
Dees_Troy | 06b4fe9 | 2012-10-16 11:43:20 -0400 | [diff] [blame] | 1171 | else { |
| 1172 | string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1173 | TWFunc::Exec_Cmd(injectcmd, result); |
Dees_Troy | 06b4fe9 | 2012-10-16 11:43:20 -0400 | [diff] [blame] | 1174 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1175 | gui_print("TWRP injection complete.\n"); |
Dees_Troy | 06b4fe9 | 2012-10-16 11:43:20 -0400 | [diff] [blame] | 1176 | } |
| 1177 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1178 | } |
| 1179 | operation_end(ret, simulate); |
| 1180 | return 0; |
| 1181 | } |
Dees_Troy | cfb63ae | 2012-09-19 14:30:17 -0400 | [diff] [blame] | 1182 | if (function == "adbsideloadcancel") |
| 1183 | { |
| 1184 | int child_pid; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1185 | char child_prop[PROPERTY_VALUE_MAX]; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1186 | string Sideload_File; |
Dees_Troy | 9a4b569 | 2012-09-19 15:09:45 -0400 | [diff] [blame] | 1187 | Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip"; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1188 | unlink(Sideload_File.c_str()); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1189 | property_get("tw_child_pid", child_prop, "error"); |
| 1190 | if (strcmp(child_prop, "error") == 0) { |
| 1191 | LOGERR("Unable to get child ID from prop\n"); |
| 1192 | return 0; |
| 1193 | } |
| 1194 | child_pid = atoi(child_prop); |
| 1195 | gui_print("Cancelling ADB sideload...\n"); |
Dees_Troy | cfb63ae | 2012-09-19 14:30:17 -0400 | [diff] [blame] | 1196 | kill(child_pid, SIGTERM); |
Dees_Troy | 4bc09ae | 2013-01-18 17:00:54 +0000 | [diff] [blame] | 1197 | DataManager::SetValue("tw_page_done", "1"); // For OpenRecoveryScript support |
Dees_Troy | cfb63ae | 2012-09-19 14:30:17 -0400 | [diff] [blame] | 1198 | return 0; |
| 1199 | } |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 1200 | if (function == "openrecoveryscript") { |
| 1201 | operation_start("OpenRecoveryScript"); |
| 1202 | if (simulate) { |
| 1203 | simulate_progress_bar(); |
| 1204 | } else { |
| 1205 | // Check for the SCRIPT_FILE_TMP first as these are AOSP recovery commands |
| 1206 | // that we converted to ORS commands during boot in recovery.cpp. |
| 1207 | // Run those first. |
| 1208 | int reboot = 0; |
| 1209 | if (TWFunc::Path_Exists(SCRIPT_FILE_TMP)) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1210 | gui_print("Processing AOSP recovery commands...\n"); |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 1211 | if (OpenRecoveryScript::run_script_file() == 0) { |
| 1212 | reboot = 1; |
| 1213 | } |
| 1214 | } |
| 1215 | // Check for the ORS file in /cache and attempt to run those commands. |
| 1216 | if (OpenRecoveryScript::check_for_script_file()) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1217 | gui_print("Processing OpenRecoveryScript file...\n"); |
Dees_Troy | 6ed34b7 | 2013-01-25 15:01:29 +0000 | [diff] [blame] | 1218 | if (OpenRecoveryScript::run_script_file() == 0) { |
| 1219 | reboot = 1; |
| 1220 | } |
| 1221 | } |
| 1222 | if (reboot) { |
| 1223 | usleep(2000000); // Sleep for 2 seconds before rebooting |
| 1224 | TWFunc::tw_reboot(rb_system); |
| 1225 | } else { |
| 1226 | DataManager::SetValue("tw_page_done", 1); |
| 1227 | } |
| 1228 | } |
| 1229 | } |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 1230 | if (function == "installsu") |
| 1231 | { |
| 1232 | int op_status = 0; |
| 1233 | |
| 1234 | operation_start("Install SuperSU"); |
| 1235 | if (simulate) { |
| 1236 | simulate_progress_bar(); |
| 1237 | } else { |
| 1238 | if (!TWFunc::Install_SuperSU()) |
| 1239 | op_status = 1; |
| 1240 | } |
| 1241 | |
| 1242 | operation_end(op_status, simulate); |
| 1243 | return 0; |
| 1244 | } |
| 1245 | if (function == "fixsu") |
| 1246 | { |
| 1247 | int op_status = 0; |
| 1248 | |
| 1249 | operation_start("Fixing Superuser Permissions"); |
| 1250 | if (simulate) { |
| 1251 | simulate_progress_bar(); |
| 1252 | } else { |
| 1253 | if (!TWFunc::Fix_su_Perms()) |
| 1254 | op_status = 1; |
| 1255 | } |
| 1256 | |
| 1257 | operation_end(op_status, simulate); |
| 1258 | return 0; |
| 1259 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1260 | } |
| 1261 | else |
| 1262 | { |
| 1263 | pthread_t t; |
| 1264 | pthread_create(&t, NULL, thread_start, this); |
| 1265 | return 0; |
| 1266 | } |
| 1267 | return -1; |
| 1268 | } |
| 1269 | |
| 1270 | int GUIAction::getKeyByName(std::string key) |
| 1271 | { |
| 1272 | if (key == "home") return KEY_HOME; |
| 1273 | else if (key == "menu") return KEY_MENU; |
| 1274 | else if (key == "back") return KEY_BACK; |
| 1275 | else if (key == "search") return KEY_SEARCH; |
| 1276 | else if (key == "voldown") return KEY_VOLUMEDOWN; |
| 1277 | else if (key == "volup") return KEY_VOLUMEUP; |
| 1278 | else if (key == "power") { |
| 1279 | int ret_val; |
| 1280 | DataManager::GetValue(TW_POWER_BUTTON, ret_val); |
| 1281 | if (!ret_val) |
| 1282 | return KEY_POWER; |
| 1283 | else |
| 1284 | return ret_val; |
| 1285 | } |
| 1286 | |
| 1287 | return atol(key.c_str()); |
| 1288 | } |
| 1289 | |
| 1290 | void* GUIAction::command_thread(void *cookie) |
| 1291 | { |
| 1292 | string command; |
| 1293 | FILE* fp; |
| 1294 | char line[512]; |
| 1295 | |
| 1296 | DataManager::GetValue("tw_terminal_command_thread", command); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 1297 | fp = popen(command.c_str(), "r"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1298 | if (fp == NULL) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1299 | LOGERR("Error opening command to run.\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1300 | } else { |
| 1301 | int fd = fileno(fp), has_data = 0, check = 0, keep_going = -1, bytes_read = 0; |
| 1302 | struct timeval timeout; |
| 1303 | fd_set fdset; |
| 1304 | |
| 1305 | while(keep_going) |
| 1306 | { |
| 1307 | FD_ZERO(&fdset); |
| 1308 | FD_SET(fd, &fdset); |
| 1309 | timeout.tv_sec = 0; |
| 1310 | timeout.tv_usec = 400000; |
| 1311 | has_data = select(fd+1, &fdset, NULL, NULL, &timeout); |
| 1312 | if (has_data == 0) { |
| 1313 | // Timeout reached |
| 1314 | DataManager::GetValue("tw_terminal_state", check); |
| 1315 | if (check == 0) { |
| 1316 | keep_going = 0; |
| 1317 | } |
| 1318 | } else if (has_data < 0) { |
| 1319 | // End of execution |
| 1320 | keep_going = 0; |
| 1321 | } else { |
| 1322 | // Try to read output |
Dees_Troy | 4be841b | 2012-09-26 14:07:15 -0400 | [diff] [blame] | 1323 | memset(line, 0, sizeof(line)); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1324 | bytes_read = read(fd, line, sizeof(line)); |
| 1325 | if (bytes_read > 0) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1326 | gui_print("%s", line); // Display output |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1327 | else |
| 1328 | keep_going = 0; // Done executing |
| 1329 | } |
| 1330 | } |
| 1331 | fclose(fp); |
| 1332 | } |
| 1333 | DataManager::SetValue("tw_operation_status", 0); |
| 1334 | DataManager::SetValue("tw_operation_state", 1); |
| 1335 | DataManager::SetValue("tw_terminal_state", 0); |
| 1336 | DataManager::SetValue("tw_background_thread_running", 0); |
| 1337 | DataManager::SetValue(TW_ACTION_BUSY, 0); |
| 1338 | return NULL; |
| 1339 | } |