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; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 211 | string result; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 212 | if (stat("/sbin/installTwrp", &st) == 0) |
| 213 | { |
| 214 | DataManager::SetValue("tw_operation", "Configuring TWRP"); |
| 215 | DataManager::SetValue("tw_partition", ""); |
| 216 | ui_print("Configuring TWRP...\n"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 217 | if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall", result) < 0) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 218 | { |
| 219 | ui_print("Unable to configure TWRP with this kernel.\n"); |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | // Done |
| 225 | DataManager::SetValue("ui_progress", 100); |
| 226 | DataManager::SetValue("ui_progress", 0); |
| 227 | return ret_val; |
| 228 | } |
| 229 | |
| 230 | int GUIAction::doActions() |
| 231 | { |
| 232 | if (mActions.size() < 1) return -1; |
| 233 | if (mActions.size() == 1) |
| 234 | return doAction(mActions.at(0), 0); |
| 235 | |
| 236 | // For multi-action, we always use a thread |
| 237 | pthread_t t; |
| 238 | pthread_create(&t, NULL, thread_start, this); |
| 239 | |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | void* GUIAction::thread_start(void *cookie) |
| 244 | { |
| 245 | GUIAction* ourThis = (GUIAction*) cookie; |
| 246 | |
| 247 | DataManager::SetValue(TW_ACTION_BUSY, 1); |
| 248 | |
| 249 | if (ourThis->mActions.size() > 1) |
| 250 | { |
| 251 | std::vector<Action>::iterator iter; |
| 252 | for (iter = ourThis->mActions.begin(); iter != ourThis->mActions.end(); iter++) |
| 253 | ourThis->doAction(*iter, 1); |
| 254 | } |
| 255 | else |
| 256 | { |
| 257 | ourThis->doAction(ourThis->mActions.at(0), 1); |
| 258 | } |
| 259 | int check = 0; |
| 260 | DataManager::GetValue("tw_background_thread_running", check); |
| 261 | if (check == 0) |
| 262 | DataManager::SetValue(TW_ACTION_BUSY, 0); |
| 263 | return NULL; |
| 264 | } |
| 265 | |
| 266 | void GUIAction::operation_start(const string operation_name) |
| 267 | { |
| 268 | DataManager::SetValue(TW_ACTION_BUSY, 1); |
| 269 | DataManager::SetValue("ui_progress", 0); |
| 270 | DataManager::SetValue("tw_operation", operation_name); |
| 271 | DataManager::SetValue("tw_operation_status", 0); |
| 272 | DataManager::SetValue("tw_operation_state", 0); |
| 273 | } |
| 274 | |
| 275 | void GUIAction::operation_end(const int operation_status, const int simulate) |
| 276 | { |
| 277 | int simulate_fail; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 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 { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 286 | if (operation_status != 0) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 287 | DataManager::SetValue("tw_operation_status", 1); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 288 | } |
| 289 | else { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 290 | DataManager::SetValue("tw_operation_status", 0); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 291 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 292 | } |
| 293 | DataManager::SetValue("tw_operation_state", 1); |
| 294 | DataManager::SetValue(TW_ACTION_BUSY, 0); |
| 295 | } |
| 296 | |
| 297 | int GUIAction::doAction(Action action, int isThreaded /* = 0 */) |
| 298 | { |
| 299 | static string zip_queue[10]; |
| 300 | static int zip_queue_index; |
| 301 | static pthread_t terminal_command; |
| 302 | int simulate; |
| 303 | |
| 304 | std::string arg = gui_parse_text(action.mArg); |
| 305 | |
| 306 | std::string function = gui_parse_text(action.mFunction); |
| 307 | |
| 308 | DataManager::GetValue(TW_SIMULATE_ACTIONS, simulate); |
| 309 | |
| 310 | if (function == "reboot") |
| 311 | { |
| 312 | //curtainClose(); this sometimes causes a crash |
| 313 | |
| 314 | sync(); |
| 315 | |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 316 | if (arg == "recovery") |
| 317 | TWFunc::tw_reboot(rb_recovery); |
| 318 | else if (arg == "poweroff") |
| 319 | TWFunc::tw_reboot(rb_poweroff); |
| 320 | else if (arg == "bootloader") |
| 321 | TWFunc::tw_reboot(rb_bootloader); |
| 322 | else if (arg == "download") |
| 323 | TWFunc::tw_reboot(rb_download); |
| 324 | else |
| 325 | TWFunc::tw_reboot(rb_system); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 326 | |
| 327 | // This should never occur |
| 328 | return -1; |
| 329 | } |
| 330 | if (function == "home") |
| 331 | { |
| 332 | PageManager::SelectPackage("TWRP"); |
| 333 | gui_changePage("main"); |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | if (function == "key") |
| 338 | { |
| 339 | PageManager::NotifyKey(getKeyByName(arg)); |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | if (function == "page") { |
| 344 | std::string page_name = gui_parse_text(arg); |
| 345 | return gui_changePage(page_name); |
| 346 | } |
| 347 | |
| 348 | if (function == "reload") { |
| 349 | int check = 0, ret_val = 0; |
| 350 | std::string theme_path; |
| 351 | |
| 352 | operation_start("Reload Theme"); |
| 353 | theme_path = DataManager::GetSettingsStoragePath(); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 354 | if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 355 | LOGE("Unable to mount %s during reload function startup.\n", theme_path.c_str()); |
| 356 | check = 1; |
| 357 | } |
| 358 | |
| 359 | theme_path += "/TWRP/theme/ui.zip"; |
| 360 | if (check != 0 || PageManager::ReloadPackage("TWRP", theme_path) != 0) |
| 361 | { |
| 362 | // Loading the custom theme failed - try loading the stock theme |
| 363 | LOGI("Attempting to reload stock theme...\n"); |
| 364 | if (PageManager::ReloadPackage("TWRP", "/res/ui.xml")) |
| 365 | { |
| 366 | LOGE("Failed to load base packages.\n"); |
| 367 | ret_val = 1; |
| 368 | } |
| 369 | } |
| 370 | operation_end(ret_val, simulate); |
| 371 | } |
| 372 | |
| 373 | if (function == "readBackup") |
| 374 | { |
| 375 | string Restore_Name; |
| 376 | DataManager::GetValue("tw_restore", Restore_Name); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 377 | PartitionManager.Set_Restore_Files(Restore_Name); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | if (function == "set") |
| 382 | { |
| 383 | if (arg.find('=') != string::npos) |
| 384 | { |
| 385 | string varName = arg.substr(0, arg.find('=')); |
| 386 | string value = arg.substr(arg.find('=') + 1, string::npos); |
| 387 | |
| 388 | DataManager::GetValue(value, value); |
| 389 | DataManager::SetValue(varName, value); |
| 390 | } |
| 391 | else |
| 392 | DataManager::SetValue(arg, "1"); |
| 393 | return 0; |
| 394 | } |
| 395 | if (function == "clear") |
| 396 | { |
| 397 | DataManager::SetValue(arg, "0"); |
| 398 | return 0; |
| 399 | } |
| 400 | |
| 401 | if (function == "mount") |
| 402 | { |
| 403 | if (arg == "usb") |
| 404 | { |
| 405 | DataManager::SetValue(TW_ACTION_BUSY, 1); |
| 406 | if (!simulate) |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 407 | PartitionManager.usb_storage_enable(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 408 | else |
| 409 | ui_print("Simulating actions...\n"); |
| 410 | } |
| 411 | else if (!simulate) |
| 412 | { |
| 413 | string cmd; |
| 414 | if (arg == "EXTERNAL") |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 415 | PartitionManager.Mount_By_Path(DataManager::GetStrValue(TW_EXTERNAL_MOUNT), true); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 416 | else if (arg == "INTERNAL") |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 417 | PartitionManager.Mount_By_Path(DataManager::GetStrValue(TW_INTERNAL_MOUNT), true); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 418 | else |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 419 | PartitionManager.Mount_By_Path(arg, true); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 420 | } else |
| 421 | ui_print("Simulating actions...\n"); |
| 422 | return 0; |
| 423 | } |
| 424 | |
| 425 | if (function == "umount" || function == "unmount") |
| 426 | { |
| 427 | if (arg == "usb") |
| 428 | { |
| 429 | if (!simulate) |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 430 | PartitionManager.usb_storage_disable(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 431 | else |
| 432 | ui_print("Simulating actions...\n"); |
| 433 | DataManager::SetValue(TW_ACTION_BUSY, 0); |
| 434 | } |
| 435 | else if (!simulate) |
| 436 | { |
| 437 | string cmd; |
| 438 | if (arg == "EXTERNAL") |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 439 | PartitionManager.UnMount_By_Path(DataManager::GetStrValue(TW_EXTERNAL_MOUNT), true); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 440 | else if (arg == "INTERNAL") |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 441 | PartitionManager.UnMount_By_Path(DataManager::GetStrValue(TW_INTERNAL_MOUNT), true); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 442 | else |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 443 | PartitionManager.UnMount_By_Path(arg, true); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 444 | } else |
| 445 | ui_print("Simulating actions...\n"); |
| 446 | return 0; |
| 447 | } |
| 448 | |
| 449 | if (function == "restoredefaultsettings") |
| 450 | { |
| 451 | operation_start("Restore Defaults"); |
| 452 | if (simulate) // Simulated so that people don't accidently wipe out the "simulation is on" setting |
| 453 | ui_print("Simulating actions...\n"); |
| 454 | else { |
| 455 | DataManager::ResetDefaults(); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 456 | PartitionManager.Update_System_Details(); |
| 457 | PartitionManager.Mount_Current_Storage(true); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 458 | } |
| 459 | operation_end(0, simulate); |
| 460 | } |
| 461 | |
| 462 | if (function == "copylog") |
| 463 | { |
| 464 | operation_start("Copy Log"); |
| 465 | if (!simulate) |
| 466 | { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 467 | string dst; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 468 | PartitionManager.Mount_Current_Storage(true); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 469 | dst = DataManager::GetCurrentStoragePath() + "/recovery.log"; |
| 470 | TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 471 | sync(); |
| 472 | ui_print("Copied recovery log to %s.\n", DataManager::GetCurrentStoragePath().c_str()); |
| 473 | } else |
| 474 | simulate_progress_bar(); |
| 475 | operation_end(0, simulate); |
| 476 | return 0; |
| 477 | } |
| 478 | |
| 479 | if (function == "compute" || function == "addsubtract") |
| 480 | { |
| 481 | if (arg.find("+") != string::npos) |
| 482 | { |
| 483 | string varName = arg.substr(0, arg.find('+')); |
| 484 | string string_to_add = arg.substr(arg.find('+') + 1, string::npos); |
| 485 | int amount_to_add = atoi(string_to_add.c_str()); |
| 486 | int value; |
| 487 | |
| 488 | DataManager::GetValue(varName, value); |
| 489 | DataManager::SetValue(varName, value + amount_to_add); |
| 490 | return 0; |
| 491 | } |
| 492 | if (arg.find("-") != string::npos) |
| 493 | { |
| 494 | string varName = arg.substr(0, arg.find('-')); |
| 495 | string string_to_subtract = arg.substr(arg.find('-') + 1, string::npos); |
| 496 | int amount_to_subtract = atoi(string_to_subtract.c_str()); |
| 497 | int value; |
| 498 | |
| 499 | DataManager::GetValue(varName, value); |
| 500 | value -= amount_to_subtract; |
| 501 | if (value <= 0) |
| 502 | value = 0; |
| 503 | DataManager::SetValue(varName, value); |
| 504 | return 0; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | if (function == "setguitimezone") |
| 509 | { |
| 510 | string SelectedZone; |
| 511 | DataManager::GetValue(TW_TIME_ZONE_GUISEL, SelectedZone); // read the selected time zone into SelectedZone |
| 512 | string Zone = SelectedZone.substr(0, SelectedZone.find(';')); // parse to get time zone |
| 513 | string DSTZone = SelectedZone.substr(SelectedZone.find(';') + 1, string::npos); // parse to get DST component |
| 514 | |
| 515 | int dst; |
| 516 | DataManager::GetValue(TW_TIME_ZONE_GUIDST, dst); // check wether user chose to use DST |
| 517 | |
| 518 | string offset; |
| 519 | DataManager::GetValue(TW_TIME_ZONE_GUIOFFSET, offset); // pull in offset |
| 520 | |
| 521 | string NewTimeZone = Zone; |
| 522 | if (offset != "0") |
| 523 | NewTimeZone += ":" + offset; |
| 524 | |
| 525 | if (dst != 0) |
| 526 | NewTimeZone += DSTZone; |
| 527 | |
| 528 | DataManager::SetValue(TW_TIME_ZONE_VAR, NewTimeZone); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 529 | DataManager::update_tz_environment_variables(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 530 | return 0; |
| 531 | } |
| 532 | |
| 533 | if (function == "togglestorage") { |
| 534 | if (arg == "internal") { |
| 535 | DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0); |
| 536 | } else if (arg == "external") { |
| 537 | DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1); |
| 538 | } |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 539 | if (PartitionManager.Mount_Current_Storage(true)) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 540 | if (arg == "internal") { |
Dees_Troy | e2920fa | 2012-09-19 16:18:00 -0400 | [diff] [blame] | 541 | string zip_path, zip_root; |
| 542 | DataManager::GetValue(TW_ZIP_INTERNAL_VAR, zip_path); |
| 543 | zip_root = TWFunc::Get_Root_Path(zip_path); |
| 544 | #ifdef RECOVERY_SDCARD_ON_DATA |
| 545 | #ifndef TW_EXTERNAL_STORAGE_PATH |
| 546 | if (zip_root != "/sdcard") |
| 547 | DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/sdcard"); |
| 548 | #else |
| 549 | if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) { |
| 550 | if (zip_root != "/emmc") |
| 551 | DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/emmc"); |
| 552 | } else { |
| 553 | if (zip_root != "/sdcard") |
| 554 | DataManager::SetValue(TW_ZIP_INTERNAL_VAR, "/sdcard"); |
| 555 | } |
| 556 | #endif |
| 557 | #else |
| 558 | if (zip_root != DataManager::GetCurrentStoragePath()) |
| 559 | DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetCurrentStoragePath()); |
| 560 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 561 | // Save the current zip location to the external variable |
| 562 | DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, DataManager::GetStrValue(TW_ZIP_LOCATION_VAR)); |
| 563 | // Change the current zip location to the internal variable |
| 564 | DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetStrValue(TW_ZIP_INTERNAL_VAR)); |
| 565 | } else if (arg == "external") { |
Dees_Troy | e2920fa | 2012-09-19 16:18:00 -0400 | [diff] [blame] | 566 | string zip_path, zip_root; |
| 567 | DataManager::GetValue(TW_ZIP_EXTERNAL_VAR, zip_path); |
| 568 | zip_root = TWFunc::Get_Root_Path(zip_path); |
| 569 | if (zip_root != DataManager::GetCurrentStoragePath()) { |
| 570 | DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, DataManager::GetCurrentStoragePath()); |
| 571 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 572 | // Save the current zip location to the internal variable |
| 573 | DataManager::SetValue(TW_ZIP_INTERNAL_VAR, DataManager::GetStrValue(TW_ZIP_LOCATION_VAR)); |
| 574 | // Change the current zip location to the external variable |
| 575 | DataManager::SetValue(TW_ZIP_LOCATION_VAR, DataManager::GetStrValue(TW_ZIP_EXTERNAL_VAR)); |
| 576 | } |
| 577 | } else { |
| 578 | // We weren't able to toggle for some reason, restore original setting |
| 579 | if (arg == "internal") { |
| 580 | DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 1); |
| 581 | } else if (arg == "external") { |
| 582 | DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0); |
| 583 | } |
| 584 | } |
| 585 | return 0; |
| 586 | } |
| 587 | |
| 588 | if (function == "overlay") |
| 589 | return gui_changeOverlay(arg); |
| 590 | |
| 591 | if (function == "queuezip") |
| 592 | { |
| 593 | if (zip_queue_index >= 10) { |
| 594 | ui_print("Maximum zip queue reached!\n"); |
| 595 | return 0; |
| 596 | } |
| 597 | DataManager::GetValue("tw_filename", zip_queue[zip_queue_index]); |
| 598 | if (strlen(zip_queue[zip_queue_index].c_str()) > 0) { |
| 599 | zip_queue_index++; |
| 600 | DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index); |
| 601 | } |
| 602 | return 0; |
| 603 | } |
| 604 | |
| 605 | if (function == "cancelzip") |
| 606 | { |
| 607 | if (zip_queue_index <= 0) { |
| 608 | ui_print("Minimum zip queue reached!\n"); |
| 609 | return 0; |
| 610 | } else { |
| 611 | zip_queue_index--; |
| 612 | DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index); |
| 613 | } |
| 614 | return 0; |
| 615 | } |
| 616 | |
| 617 | if (function == "queueclear") |
| 618 | { |
| 619 | zip_queue_index = 0; |
| 620 | DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index); |
| 621 | return 0; |
| 622 | } |
| 623 | |
| 624 | if (function == "sleep") |
| 625 | { |
| 626 | operation_start("Sleep"); |
| 627 | usleep(atoi(arg.c_str())); |
| 628 | operation_end(0, simulate); |
| 629 | return 0; |
| 630 | } |
| 631 | |
| 632 | if (isThreaded) |
| 633 | { |
| 634 | if (function == "fileexists") |
| 635 | { |
| 636 | struct stat st; |
| 637 | string newpath = arg + "/."; |
| 638 | |
| 639 | operation_start("FileExists"); |
| 640 | if (stat(arg.c_str(), &st) == 0 || stat(newpath.c_str(), &st) == 0) |
| 641 | operation_end(0, simulate); |
| 642 | else |
| 643 | operation_end(1, simulate); |
| 644 | } |
| 645 | |
| 646 | if (function == "flash") |
| 647 | { |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 648 | int i, ret_val = 0, wipe_cache = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 649 | |
| 650 | for (i=0; i<zip_queue_index; i++) { |
| 651 | operation_start("Flashing"); |
| 652 | DataManager::SetValue("tw_filename", zip_queue[i]); |
| 653 | DataManager::SetValue(TW_ZIP_INDEX, (i + 1)); |
| 654 | |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 655 | ret_val = flash_zip(zip_queue[i], arg, simulate, &wipe_cache); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 656 | if (ret_val != 0) { |
| 657 | ui_print("Error flashing zip '%s'\n", zip_queue[i].c_str()); |
| 658 | i = 10; // Error flashing zip - exit queue |
| 659 | ret_val = 1; |
| 660 | } |
| 661 | } |
| 662 | zip_queue_index = 0; |
| 663 | DataManager::SetValue(TW_ZIP_QUEUE_COUNT, zip_queue_index); |
| 664 | |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 665 | if (wipe_cache) |
| 666 | PartitionManager.Wipe_By_Path("/cache"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 667 | string result; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 668 | if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) { |
| 669 | operation_start("ReinjectTWRP"); |
| 670 | ui_print("Injecting TWRP into boot image...\n"); |
| 671 | if (simulate) { |
| 672 | simulate_progress_bar(); |
| 673 | } else { |
Dees_Troy | 06b4fe9 | 2012-10-16 11:43:20 -0400 | [diff] [blame] | 674 | TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot"); |
| 675 | if (Boot == NULL || Boot->Current_File_System != "emmc") |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 676 | TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash", result); |
Dees_Troy | 06b4fe9 | 2012-10-16 11:43:20 -0400 | [diff] [blame] | 677 | else { |
| 678 | string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 679 | TWFunc::Exec_Cmd(injectcmd, result); |
Dees_Troy | 06b4fe9 | 2012-10-16 11:43:20 -0400 | [diff] [blame] | 680 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 681 | ui_print("TWRP injection complete.\n"); |
| 682 | } |
| 683 | } |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 684 | PartitionManager.Update_System_Details(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 685 | operation_end(ret_val, simulate); |
| 686 | return 0; |
| 687 | } |
| 688 | if (function == "wipe") |
| 689 | { |
| 690 | operation_start("Format"); |
| 691 | DataManager::SetValue("tw_partition", arg); |
| 692 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 693 | int ret_val = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 694 | |
| 695 | if (simulate) { |
| 696 | simulate_progress_bar(); |
| 697 | } else { |
| 698 | if (arg == "data") |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 699 | ret_val = PartitionManager.Factory_Reset(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 700 | else if (arg == "battery") |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 701 | ret_val = PartitionManager.Wipe_Battery_Stats(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 702 | else if (arg == "rotate") |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 703 | ret_val = PartitionManager.Wipe_Rotate_Data(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 704 | else if (arg == "dalvik") |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 705 | ret_val = PartitionManager.Wipe_Dalvik_Cache(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 706 | else if (arg == "DATAMEDIA") { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 707 | ret_val = PartitionManager.Format_Data(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 708 | } else if (arg == "INTERNAL") { |
| 709 | int has_datamedia, dual_storage; |
| 710 | |
| 711 | DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia); |
| 712 | if (has_datamedia) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 713 | ret_val = PartitionManager.Wipe_Media_From_Data(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 714 | } else { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 715 | ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 716 | } |
| 717 | } else if (arg == "EXTERNAL") { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 718 | string External_Path; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 719 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 720 | DataManager::GetValue(TW_EXTERNAL_PATH, External_Path); |
| 721 | ret_val = PartitionManager.Wipe_By_Path(External_Path); |
Dees_Troy | 2ff5a8d | 2012-09-26 14:53:02 -0400 | [diff] [blame] | 722 | } else if (arg == "ANDROIDSECURE") { |
| 723 | ret_val = PartitionManager.Wipe_Android_Secure(); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 724 | } else |
| 725 | ret_val = PartitionManager.Wipe_By_Path(arg); |
| 726 | |
| 727 | if (arg == DataManager::GetSettingsStoragePath()) { |
| 728 | // If we wiped the settings storage path, recreate the TWRP folder and dump the settings |
| 729 | string Storage_Path = DataManager::GetSettingsStoragePath(); |
| 730 | |
| 731 | if (PartitionManager.Mount_By_Path(Storage_Path, true)) { |
| 732 | LOGI("Making TWRP folder and saving settings.\n"); |
| 733 | Storage_Path += "/TWRP"; |
| 734 | mkdir(Storage_Path.c_str(), 0777); |
| 735 | DataManager::Flush(); |
| 736 | } else { |
| 737 | LOGE("Unable to recreate TWRP folder and save settings.\n"); |
| 738 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 739 | } |
| 740 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 741 | PartitionManager.Update_System_Details(); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 742 | if (ret_val) |
| 743 | ret_val = 0; // 0 is success |
| 744 | else |
| 745 | ret_val = 1; // 1 is failure |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 746 | operation_end(ret_val, simulate); |
| 747 | return 0; |
| 748 | } |
| 749 | if (function == "refreshsizes") |
| 750 | { |
| 751 | operation_start("Refreshing Sizes"); |
| 752 | if (simulate) { |
| 753 | simulate_progress_bar(); |
| 754 | } else |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 755 | PartitionManager.Update_System_Details(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 756 | operation_end(0, simulate); |
| 757 | } |
| 758 | if (function == "nandroid") |
| 759 | { |
| 760 | operation_start("Nandroid"); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 761 | int ret = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 762 | |
| 763 | if (simulate) { |
| 764 | DataManager::SetValue("tw_partition", "Simulation"); |
| 765 | simulate_progress_bar(); |
| 766 | } else { |
| 767 | if (arg == "backup") { |
| 768 | string Backup_Name; |
| 769 | DataManager::GetValue(TW_BACKUP_NAME, Backup_Name); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 770 | 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] | 771 | ret = PartitionManager.Run_Backup(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 772 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 773 | else { |
| 774 | operation_end(1, simulate); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 775 | return -1; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 776 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 777 | DataManager::SetValue(TW_BACKUP_NAME, "(Current Date)"); |
| 778 | } else if (arg == "restore") { |
| 779 | string Restore_Name; |
| 780 | DataManager::GetValue("tw_restore", Restore_Name); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 781 | ret = PartitionManager.Run_Restore(Restore_Name); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 782 | } else { |
| 783 | operation_end(1, simulate); |
| 784 | return -1; |
| 785 | } |
| 786 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 787 | if (ret == false) |
| 788 | ret = 1; // 1 for failure |
| 789 | else |
| 790 | ret = 0; // 0 for success |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 791 | operation_end(ret, simulate); |
| 792 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 793 | } |
| 794 | if (function == "fixpermissions") |
| 795 | { |
| 796 | operation_start("Fix Permissions"); |
| 797 | LOGI("fix permissions started!\n"); |
| 798 | if (simulate) { |
| 799 | simulate_progress_bar(); |
Dees_Troy | 4be841b | 2012-09-26 14:07:15 -0400 | [diff] [blame] | 800 | } else { |
Dees_Troy | 6480ce0 | 2012-10-10 10:26:54 -0400 | [diff] [blame] | 801 | int op_status = PartitionManager.Fix_Permissions(); |
| 802 | if (op_status != 0) |
| 803 | op_status = 1; // failure |
| 804 | operation_end(op_status, simulate); |
Dees_Troy | 4be841b | 2012-09-26 14:07:15 -0400 | [diff] [blame] | 805 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 806 | return 0; |
| 807 | } |
| 808 | if (function == "dd") |
| 809 | { |
| 810 | operation_start("imaging"); |
| 811 | |
| 812 | if (simulate) { |
| 813 | simulate_progress_bar(); |
| 814 | } else { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 815 | string result; |
| 816 | string cmd = "dd " + arg; |
| 817 | TWFunc::Exec_Cmd(cmd, result); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 818 | } |
| 819 | operation_end(0, simulate); |
| 820 | return 0; |
| 821 | } |
| 822 | if (function == "partitionsd") |
| 823 | { |
| 824 | operation_start("Partition SD Card"); |
Dees_Troy | 9350b8d | 2012-09-27 12:38:38 -0400 | [diff] [blame] | 825 | int ret_val = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 826 | |
| 827 | if (simulate) { |
| 828 | simulate_progress_bar(); |
| 829 | } else { |
| 830 | int allow_partition; |
| 831 | DataManager::GetValue(TW_ALLOW_PARTITION_SDCARD, allow_partition); |
| 832 | if (allow_partition == 0) { |
| 833 | ui_print("This device does not have a real SD Card!\nAborting!\n"); |
| 834 | } else { |
Dees_Troy | 9350b8d | 2012-09-27 12:38:38 -0400 | [diff] [blame] | 835 | if (!PartitionManager.Partition_SDCard()) |
| 836 | ret_val = 1; // failed |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 837 | } |
| 838 | } |
Dees_Troy | 9350b8d | 2012-09-27 12:38:38 -0400 | [diff] [blame] | 839 | operation_end(ret_val, simulate); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 840 | return 0; |
| 841 | } |
| 842 | if (function == "installhtcdumlock") |
| 843 | { |
| 844 | operation_start("Install HTC Dumlock"); |
| 845 | if (simulate) { |
| 846 | simulate_progress_bar(); |
| 847 | } else |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 848 | TWFunc::install_htc_dumlock(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 849 | |
| 850 | operation_end(0, simulate); |
| 851 | return 0; |
| 852 | } |
| 853 | if (function == "htcdumlockrestoreboot") |
| 854 | { |
| 855 | operation_start("HTC Dumlock Restore Boot"); |
| 856 | if (simulate) { |
| 857 | simulate_progress_bar(); |
| 858 | } else |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 859 | TWFunc::htc_dumlock_restore_original_boot(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 860 | |
| 861 | operation_end(0, simulate); |
| 862 | return 0; |
| 863 | } |
| 864 | if (function == "htcdumlockreflashrecovery") |
| 865 | { |
| 866 | operation_start("HTC Dumlock Reflash Recovery"); |
| 867 | if (simulate) { |
| 868 | simulate_progress_bar(); |
| 869 | } else |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 870 | TWFunc::htc_dumlock_reflash_recovery_to_boot(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 871 | |
| 872 | operation_end(0, simulate); |
| 873 | return 0; |
| 874 | } |
| 875 | if (function == "cmd") |
| 876 | { |
| 877 | int op_status = 0; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 878 | string result; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 879 | |
| 880 | operation_start("Command"); |
| 881 | LOGI("Running command: '%s'\n", arg.c_str()); |
| 882 | if (simulate) { |
| 883 | simulate_progress_bar(); |
| 884 | } else { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 885 | op_status = TWFunc::Exec_Cmd(arg, result); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 886 | if (op_status != 0) |
| 887 | op_status = 1; |
| 888 | } |
| 889 | |
| 890 | operation_end(op_status, simulate); |
| 891 | return 0; |
| 892 | } |
| 893 | if (function == "terminalcommand") |
| 894 | { |
| 895 | int op_status = 0; |
| 896 | string cmdpath, command; |
| 897 | |
| 898 | DataManager::GetValue("tw_terminal_location", cmdpath); |
| 899 | operation_start("CommandOutput"); |
| 900 | ui_print("%s # %s\n", cmdpath.c_str(), arg.c_str()); |
| 901 | if (simulate) { |
| 902 | simulate_progress_bar(); |
| 903 | operation_end(op_status, simulate); |
| 904 | } else { |
Dees_Troy | 4be841b | 2012-09-26 14:07:15 -0400 | [diff] [blame] | 905 | command = "cd \"" + cmdpath + "\" && " + arg + " 2>&1";; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 906 | LOGI("Actual command is: '%s'\n", command.c_str()); |
| 907 | DataManager::SetValue("tw_terminal_command_thread", command); |
| 908 | DataManager::SetValue("tw_terminal_state", 1); |
| 909 | DataManager::SetValue("tw_background_thread_running", 1); |
| 910 | op_status = pthread_create(&terminal_command, NULL, command_thread, NULL); |
| 911 | if (op_status != 0) { |
| 912 | LOGE("Error starting terminal command thread, %i.\n", op_status); |
| 913 | DataManager::SetValue("tw_terminal_state", 0); |
| 914 | DataManager::SetValue("tw_background_thread_running", 0); |
| 915 | operation_end(1, simulate); |
| 916 | } |
| 917 | } |
| 918 | return 0; |
| 919 | } |
| 920 | if (function == "killterminal") |
| 921 | { |
| 922 | int op_status = 0; |
| 923 | |
| 924 | LOGI("Sending kill command...\n"); |
| 925 | operation_start("KillCommand"); |
| 926 | DataManager::SetValue("tw_operation_status", 0); |
| 927 | DataManager::SetValue("tw_operation_state", 1); |
| 928 | DataManager::SetValue("tw_terminal_state", 0); |
| 929 | DataManager::SetValue("tw_background_thread_running", 0); |
| 930 | DataManager::SetValue(TW_ACTION_BUSY, 0); |
| 931 | return 0; |
| 932 | } |
| 933 | if (function == "reinjecttwrp") |
| 934 | { |
| 935 | int op_status = 0; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 936 | string result; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 937 | operation_start("ReinjectTWRP"); |
| 938 | ui_print("Injecting TWRP into boot image...\n"); |
| 939 | if (simulate) { |
| 940 | simulate_progress_bar(); |
| 941 | } else { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 942 | TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash", result); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 943 | ui_print("TWRP injection complete.\n"); |
| 944 | } |
| 945 | |
| 946 | operation_end(op_status, simulate); |
| 947 | return 0; |
| 948 | } |
| 949 | if (function == "checkbackupname") |
| 950 | { |
| 951 | int op_status = 0; |
| 952 | |
| 953 | operation_start("CheckBackupName"); |
| 954 | if (simulate) { |
| 955 | simulate_progress_bar(); |
| 956 | } else { |
Dees_Troy | c9ff7a3 | 2012-09-27 10:09:41 -0400 | [diff] [blame] | 957 | op_status = PartitionManager.Check_Backup_Name(true); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 958 | if (op_status != 0) |
| 959 | op_status = 1; |
| 960 | } |
| 961 | |
| 962 | operation_end(op_status, simulate); |
| 963 | return 0; |
| 964 | } |
| 965 | if (function == "decrypt") |
| 966 | { |
| 967 | int op_status = 0; |
| 968 | |
| 969 | operation_start("Decrypt"); |
| 970 | if (simulate) { |
| 971 | simulate_progress_bar(); |
| 972 | } else { |
| 973 | string Password; |
| 974 | DataManager::GetValue("tw_crypto_password", Password); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 975 | op_status = PartitionManager.Decrypt_Device(Password); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 976 | if (op_status != 0) |
| 977 | op_status = 1; |
| 978 | else { |
| 979 | int load_theme = 1; |
| 980 | |
| 981 | DataManager::SetValue(TW_IS_ENCRYPTED, 0); |
| 982 | DataManager::ReadSettingsFile(); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 983 | if (OpenRecoveryScript::check_for_script_file()) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 984 | ui_print("Processing OpenRecoveryScript file...\n"); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 985 | if (OpenRecoveryScript::run_script_file() == 0) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 986 | usleep(2000000); // Sleep for 2 seconds before rebooting |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 987 | TWFunc::tw_reboot(rb_system); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 988 | load_theme = 0; |
| 989 | } |
| 990 | } |
| 991 | |
| 992 | if (load_theme) { |
| 993 | int has_datamedia; |
| 994 | |
| 995 | // Check for a custom theme and load it if exists |
| 996 | DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia); |
| 997 | if (has_datamedia != 0) { |
| 998 | struct stat st; |
| 999 | int check = 0; |
| 1000 | std::string theme_path; |
| 1001 | |
| 1002 | theme_path = DataManager::GetSettingsStoragePath(); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1003 | if (PartitionManager.Mount_By_Path(theme_path.c_str(), 1) < 0) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1004 | LOGE("Unable to mount %s during reload function startup.\n", theme_path.c_str()); |
| 1005 | check = 1; |
| 1006 | } |
| 1007 | |
| 1008 | theme_path += "/TWRP/theme/ui.zip"; |
| 1009 | if (check == 0 && stat(theme_path.c_str(), &st) == 0) { |
| 1010 | if (PageManager::ReloadPackage("TWRP", theme_path) != 0) |
| 1011 | { |
| 1012 | // Loading the custom theme failed - try loading the stock theme |
| 1013 | LOGI("Attempting to reload stock theme...\n"); |
| 1014 | if (PageManager::ReloadPackage("TWRP", "/res/ui.xml")) |
| 1015 | { |
| 1016 | LOGE("Failed to load base packages.\n"); |
| 1017 | } |
| 1018 | } |
| 1019 | } |
| 1020 | } |
| 1021 | } |
| 1022 | } |
| 1023 | } |
| 1024 | |
| 1025 | operation_end(op_status, simulate); |
| 1026 | return 0; |
| 1027 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1028 | if (function == "adbsideload") |
| 1029 | { |
| 1030 | int ret = 0; |
| 1031 | |
| 1032 | operation_start("Sideload"); |
| 1033 | if (simulate) { |
| 1034 | simulate_progress_bar(); |
| 1035 | } else { |
| 1036 | int wipe_cache = 0; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1037 | string result, Sideload_File; |
Dees_Troy | cfb63ae | 2012-09-19 14:30:17 -0400 | [diff] [blame] | 1038 | |
| 1039 | if (!PartitionManager.Mount_Current_Storage(true)) { |
| 1040 | operation_end(1, simulate); |
| 1041 | return 0; |
| 1042 | } |
Dees_Troy | 9a4b569 | 2012-09-19 15:09:45 -0400 | [diff] [blame] | 1043 | Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip"; |
| 1044 | if (TWFunc::Path_Exists(Sideload_File)) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1045 | unlink(Sideload_File.c_str()); |
Dees_Troy | cfb63ae | 2012-09-19 14:30:17 -0400 | [diff] [blame] | 1046 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1047 | ui_print("Starting ADB sideload feature...\n"); |
Dees_Troy | 9a4b569 | 2012-09-19 15:09:45 -0400 | [diff] [blame] | 1048 | ret = apply_from_adb(ui, &wipe_cache, Sideload_File.c_str()); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1049 | if (ret != 0) |
Dees_Troy | cfb63ae | 2012-09-19 14:30:17 -0400 | [diff] [blame] | 1050 | ret = 1; // failure |
| 1051 | else if (wipe_cache) |
| 1052 | PartitionManager.Wipe_By_Path("/cache"); |
Dees_Troy | 06b4fe9 | 2012-10-16 11:43:20 -0400 | [diff] [blame] | 1053 | if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) { |
| 1054 | operation_start("ReinjectTWRP"); |
| 1055 | ui_print("Injecting TWRP into boot image...\n"); |
| 1056 | if (simulate) { |
| 1057 | simulate_progress_bar(); |
| 1058 | } else { |
| 1059 | TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot"); |
| 1060 | if (Boot == NULL || Boot->Current_File_System != "emmc") |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1061 | TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash", result); |
Dees_Troy | 06b4fe9 | 2012-10-16 11:43:20 -0400 | [diff] [blame] | 1062 | else { |
| 1063 | string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1064 | TWFunc::Exec_Cmd(injectcmd, result); |
Dees_Troy | 06b4fe9 | 2012-10-16 11:43:20 -0400 | [diff] [blame] | 1065 | } |
| 1066 | ui_print("TWRP injection complete.\n"); |
| 1067 | } |
| 1068 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1069 | } |
| 1070 | operation_end(ret, simulate); |
| 1071 | return 0; |
| 1072 | } |
Dees_Troy | cfb63ae | 2012-09-19 14:30:17 -0400 | [diff] [blame] | 1073 | if (function == "adbsideloadcancel") |
| 1074 | { |
| 1075 | int child_pid; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1076 | string Sideload_File; |
Dees_Troy | 9a4b569 | 2012-09-19 15:09:45 -0400 | [diff] [blame] | 1077 | Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip"; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1078 | unlink(Sideload_File.c_str()); |
Dees_Troy | cfb63ae | 2012-09-19 14:30:17 -0400 | [diff] [blame] | 1079 | DataManager::GetValue("tw_child_pid", child_pid); |
| 1080 | ui_print("Cancelling ADB sideload...\n"); |
| 1081 | kill(child_pid, SIGTERM); |
| 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); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 1121 | fp = popen(command.c_str(), "r"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 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 |
Dees_Troy | 4be841b | 2012-09-26 14:07:15 -0400 | [diff] [blame] | 1147 | memset(line, 0, sizeof(line)); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1148 | bytes_read = read(fd, line, sizeof(line)); |
| 1149 | if (bytes_read > 0) |
| 1150 | ui_print("%s", line); // Display output |
| 1151 | else |
| 1152 | keep_going = 0; // Done executing |
| 1153 | } |
| 1154 | } |
| 1155 | fclose(fp); |
| 1156 | } |
| 1157 | DataManager::SetValue("tw_operation_status", 0); |
| 1158 | DataManager::SetValue("tw_operation_state", 1); |
| 1159 | DataManager::SetValue("tw_terminal_state", 0); |
| 1160 | DataManager::SetValue("tw_background_thread_running", 0); |
| 1161 | DataManager::SetValue(TW_ACTION_BUSY, 0); |
| 1162 | return NULL; |
| 1163 | } |