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