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