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