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