Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <assert.h> |
| 18 | #include <errno.h> |
| 19 | #include <fcntl.h> |
| 20 | #include <limits.h> |
| 21 | #include <paths.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <sys/types.h> |
| 27 | #include <sys/wait.h> |
| 28 | #include <unistd.h> |
| 29 | |
| 30 | #include <ctype.h> |
| 31 | #include "cutils/misc.h" |
| 32 | #include "cutils/properties.h" |
| 33 | #include <dirent.h> |
| 34 | #include <getopt.h> |
| 35 | #include <linux/input.h> |
| 36 | #include <signal.h> |
| 37 | #include <sys/limits.h> |
| 38 | #include <termios.h> |
| 39 | #include <time.h> |
| 40 | #include <sys/vfs.h> |
| 41 | |
| 42 | #include "tw_reboot.h" |
| 43 | #include "bootloader.h" |
| 44 | #include "common.h" |
| 45 | #include "extra-functions.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 46 | #include "minuitwrp/minui.h" |
| 47 | #include "minzip/DirUtil.h" |
| 48 | #include "minzip/Zip.h" |
| 49 | #include "recovery_ui.h" |
| 50 | #include "roots.h" |
| 51 | #include "data.h" |
| 52 | #include "variables.h" |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 53 | #include "mincrypt/rsa.h" |
| 54 | #include "verifier.h" |
| 55 | #include "mincrypt/sha.h" |
| 56 | |
| 57 | #ifndef PUBLIC_KEYS_FILE |
| 58 | #define PUBLIC_KEYS_FILE "/res/keys" |
| 59 | #endif |
| 60 | #ifndef ASSUMED_UPDATE_BINARY_NAME |
| 61 | #define ASSUMED_UPDATE_BINARY_NAME "META-INF/com/google/android/update-binary" |
| 62 | #endif |
| 63 | enum { INSTALL_SUCCESS, INSTALL_ERROR, INSTALL_CORRUPT }; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 64 | |
| 65 | //kang system() from bionic/libc/unistd and rename it __system() so we can be even more hackish :) |
| 66 | #undef _PATH_BSHELL |
| 67 | #define _PATH_BSHELL "/sbin/sh" |
| 68 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 69 | extern char **environ; |
| 70 | |
| 71 | int __system(const char *command) { |
| 72 | pid_t pid; |
| 73 | sig_t intsave, quitsave; |
| 74 | sigset_t mask, omask; |
| 75 | int pstat; |
| 76 | char *argp[] = {"sh", "-c", NULL, NULL}; |
| 77 | |
| 78 | if (!command) /* just checking... */ |
| 79 | return(1); |
| 80 | |
| 81 | argp[2] = (char *)command; |
| 82 | |
| 83 | sigemptyset(&mask); |
| 84 | sigaddset(&mask, SIGCHLD); |
| 85 | sigprocmask(SIG_BLOCK, &mask, &omask); |
| 86 | switch (pid = vfork()) { |
| 87 | case -1: /* error */ |
| 88 | sigprocmask(SIG_SETMASK, &omask, NULL); |
| 89 | return(-1); |
| 90 | case 0: /* child */ |
| 91 | sigprocmask(SIG_SETMASK, &omask, NULL); |
| 92 | execve(_PATH_BSHELL, argp, environ); |
| 93 | _exit(127); |
| 94 | } |
| 95 | |
| 96 | intsave = (sig_t) bsd_signal(SIGINT, SIG_IGN); |
| 97 | quitsave = (sig_t) bsd_signal(SIGQUIT, SIG_IGN); |
| 98 | pid = waitpid(pid, (int *)&pstat, 0); |
| 99 | sigprocmask(SIG_SETMASK, &omask, NULL); |
| 100 | (void)bsd_signal(SIGINT, intsave); |
| 101 | (void)bsd_signal(SIGQUIT, quitsave); |
| 102 | return (pid == -1 ? -1 : pstat); |
| 103 | } |
| 104 | |
| 105 | static struct pid { |
| 106 | struct pid *next; |
| 107 | FILE *fp; |
| 108 | pid_t pid; |
| 109 | } *pidlist; |
| 110 | |
| 111 | FILE *__popen(const char *program, const char *type) { |
| 112 | struct pid * volatile cur; |
| 113 | FILE *iop; |
| 114 | int pdes[2]; |
| 115 | pid_t pid; |
| 116 | |
| 117 | if ((*type != 'r' && *type != 'w') || type[1] != '\0') { |
| 118 | errno = EINVAL; |
| 119 | return (NULL); |
| 120 | } |
| 121 | |
| 122 | if ((cur = malloc(sizeof(struct pid))) == NULL) |
| 123 | return (NULL); |
| 124 | |
| 125 | if (pipe(pdes) < 0) { |
| 126 | free(cur); |
| 127 | return (NULL); |
| 128 | } |
| 129 | |
| 130 | switch (pid = vfork()) { |
| 131 | case -1: /* Error. */ |
| 132 | (void)close(pdes[0]); |
| 133 | (void)close(pdes[1]); |
| 134 | free(cur); |
| 135 | return (NULL); |
| 136 | /* NOTREACHED */ |
| 137 | case 0: /* Child. */ |
| 138 | { |
| 139 | struct pid *pcur; |
| 140 | /* |
| 141 | * because vfork() instead of fork(), must leak FILE *, |
| 142 | * but luckily we are terminally headed for an execl() |
| 143 | */ |
| 144 | for (pcur = pidlist; pcur; pcur = pcur->next) |
| 145 | close(fileno(pcur->fp)); |
| 146 | |
| 147 | if (*type == 'r') { |
| 148 | int tpdes1 = pdes[1]; |
| 149 | |
| 150 | (void) close(pdes[0]); |
| 151 | /* |
| 152 | * We must NOT modify pdes, due to the |
| 153 | * semantics of vfork. |
| 154 | */ |
| 155 | if (tpdes1 != STDOUT_FILENO) { |
| 156 | (void)dup2(tpdes1, STDOUT_FILENO); |
| 157 | (void)close(tpdes1); |
| 158 | tpdes1 = STDOUT_FILENO; |
| 159 | } |
| 160 | } else { |
| 161 | (void)close(pdes[1]); |
| 162 | if (pdes[0] != STDIN_FILENO) { |
| 163 | (void)dup2(pdes[0], STDIN_FILENO); |
| 164 | (void)close(pdes[0]); |
| 165 | } |
| 166 | } |
| 167 | execl(_PATH_BSHELL, "sh", "-c", program, (char *)NULL); |
| 168 | _exit(127); |
| 169 | /* NOTREACHED */ |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /* Parent; assume fdopen can't fail. */ |
| 174 | if (*type == 'r') { |
| 175 | iop = fdopen(pdes[0], type); |
| 176 | (void)close(pdes[1]); |
| 177 | } else { |
| 178 | iop = fdopen(pdes[1], type); |
| 179 | (void)close(pdes[0]); |
| 180 | } |
| 181 | |
| 182 | /* Link into list of file descriptors. */ |
| 183 | cur->fp = iop; |
| 184 | cur->pid = pid; |
| 185 | cur->next = pidlist; |
| 186 | pidlist = cur; |
| 187 | |
| 188 | return (iop); |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | * pclose -- |
| 193 | * Pclose returns -1 if stream is not associated with a `popened' command, |
| 194 | * if already `pclosed', or waitpid returns an error. |
| 195 | */ |
| 196 | int __pclose(FILE *iop) { |
| 197 | struct pid *cur, *last; |
| 198 | int pstat; |
| 199 | pid_t pid; |
| 200 | |
| 201 | /* Find the appropriate file pointer. */ |
| 202 | for (last = NULL, cur = pidlist; cur; last = cur, cur = cur->next) |
| 203 | if (cur->fp == iop) |
| 204 | break; |
| 205 | |
| 206 | if (cur == NULL) |
| 207 | return (-1); |
| 208 | |
| 209 | (void)fclose(iop); |
| 210 | |
| 211 | do { |
| 212 | pid = waitpid(cur->pid, &pstat, 0); |
| 213 | } while (pid == -1 && errno == EINTR); |
| 214 | |
| 215 | /* Remove the entry from the linked list. */ |
| 216 | if (last == NULL) |
| 217 | pidlist = cur->next; |
| 218 | else |
| 219 | last->next = cur->next; |
| 220 | free(cur); |
| 221 | |
| 222 | return (pid == -1 ? -1 : pstat); |
| 223 | } |
| 224 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 225 | //partial kangbang from system/vold |
| 226 | #ifndef CUSTOM_LUN_FILE |
| 227 | #define CUSTOM_LUN_FILE "/sys/devices/platform/usb_mass_storage/lun%d/file" |
| 228 | #endif |
| 229 | |
| 230 | int usb_storage_enable(void) |
| 231 | { |
| 232 | int fd; |
| 233 | char lun_file[255]; |
| 234 | |
| 235 | if (DataManager_GetIntValue(TW_HAS_DUAL_STORAGE) == 1 && DataManager_GetIntValue(TW_HAS_DATA_MEDIA) == 0) { |
| 236 | Volume *vol = volume_for_path(DataManager_GetSettingsStoragePath()); |
| 237 | if (!vol) |
| 238 | { |
| 239 | LOGE("Unable to locate volume information."); |
| 240 | return -1; |
| 241 | } |
| 242 | |
| 243 | sprintf(lun_file, CUSTOM_LUN_FILE, 0); |
| 244 | |
| 245 | if ((fd = open(lun_file, O_WRONLY)) < 0) |
| 246 | { |
| 247 | LOGE("Unable to open ums lunfile '%s': (%s)\n", lun_file, strerror(errno)); |
| 248 | return -1; |
| 249 | } |
| 250 | |
| 251 | if ((write(fd, vol->device, strlen(vol->device)) < 0) && |
| 252 | (!vol->device2 || (write(fd, vol->device, strlen(vol->device2)) < 0))) { |
| 253 | LOGE("Unable to write to ums lunfile '%s': (%s)\n", lun_file, strerror(errno)); |
| 254 | close(fd); |
| 255 | return -1; |
| 256 | } |
| 257 | close(fd); |
| 258 | |
| 259 | Volume *vol2 = volume_for_path(DataManager_GetStrValue(TW_EXTERNAL_PATH)); |
| 260 | if (!vol) |
| 261 | { |
| 262 | LOGE("Unable to locate volume information.\n"); |
| 263 | return -1; |
| 264 | } |
| 265 | |
| 266 | sprintf(lun_file, CUSTOM_LUN_FILE, 1); |
| 267 | |
| 268 | if ((fd = open(lun_file, O_WRONLY)) < 0) |
| 269 | { |
| 270 | LOGE("Unable to open ums lunfile '%s': (%s)\n", lun_file, strerror(errno)); |
| 271 | return -1; |
| 272 | } |
| 273 | |
| 274 | if ((write(fd, vol2->device, strlen(vol2->device)) < 0) && |
| 275 | (!vol2->device2 || (write(fd, vol2->device, strlen(vol2->device2)) < 0))) { |
| 276 | LOGE("Unable to write to ums lunfile '%s': (%s)\n", lun_file, strerror(errno)); |
| 277 | close(fd); |
| 278 | return -1; |
| 279 | } |
| 280 | close(fd); |
| 281 | } else { |
| 282 | if (DataManager_GetIntValue(TW_HAS_DATA_MEDIA) == 0) |
| 283 | strcpy(lun_file, DataManager_GetCurrentStoragePath()); |
| 284 | else |
| 285 | strcpy(lun_file, DataManager_GetStrValue(TW_EXTERNAL_PATH)); |
| 286 | |
| 287 | Volume *vol = volume_for_path(lun_file); |
| 288 | if (!vol) |
| 289 | { |
| 290 | LOGE("Unable to locate volume information.\n"); |
| 291 | return -1; |
| 292 | } |
| 293 | |
| 294 | sprintf(lun_file, CUSTOM_LUN_FILE, 0); |
| 295 | |
| 296 | if ((fd = open(lun_file, O_WRONLY)) < 0) |
| 297 | { |
| 298 | LOGE("Unable to open ums lunfile '%s': (%s)\n", lun_file, strerror(errno)); |
| 299 | return -1; |
| 300 | } |
| 301 | |
| 302 | if ((write(fd, vol->device, strlen(vol->device)) < 0) && |
| 303 | (!vol->device2 || (write(fd, vol->device, strlen(vol->device2)) < 0))) { |
| 304 | LOGE("Unable to write to ums lunfile '%s': (%s)\n", lun_file, strerror(errno)); |
| 305 | close(fd); |
| 306 | return -1; |
| 307 | } |
| 308 | close(fd); |
| 309 | } |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | int usb_storage_disable(void) |
| 314 | { |
| 315 | int fd, index; |
| 316 | char lun_file[255]; |
| 317 | |
| 318 | for (index=0; index<2; index++) { |
| 319 | sprintf(lun_file, CUSTOM_LUN_FILE, index); |
| 320 | |
| 321 | if ((fd = open(lun_file, O_WRONLY)) < 0) |
| 322 | { |
| 323 | if (index == 0) |
| 324 | LOGE("Unable to open ums lunfile '%s': (%s)", lun_file, strerror(errno)); |
| 325 | return -1; |
| 326 | } |
| 327 | |
| 328 | char ch = 0; |
| 329 | if (write(fd, &ch, 1) < 0) |
| 330 | { |
| 331 | if (index == 0) |
| 332 | LOGE("Unable to write to ums lunfile '%s': (%s)", lun_file, strerror(errno)); |
| 333 | close(fd); |
| 334 | return -1; |
| 335 | } |
| 336 | |
| 337 | close(fd); |
| 338 | } |
| 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | void wipe_dalvik_cache() |
| 343 | { |
| 344 | //ui_set_background(BACKGROUND_ICON_WIPE); |
| 345 | ensure_path_mounted("/data"); |
| 346 | ensure_path_mounted("/cache"); |
| 347 | ui_print("\n-- Wiping Dalvik Cache Directories...\n"); |
| 348 | __system("rm -rf /data/dalvik-cache"); |
| 349 | ui_print("Cleaned: /data/dalvik-cache...\n"); |
| 350 | __system("rm -rf /cache/dalvik-cache"); |
| 351 | ui_print("Cleaned: /cache/dalvik-cache...\n"); |
| 352 | __system("rm -rf /cache/dc"); |
| 353 | ui_print("Cleaned: /cache/dc\n"); |
| 354 | |
| 355 | struct stat st; |
| 356 | LOGE("TODO: Re-implement wipe dalvik into Partition Manager!\n"); |
| 357 | if (1) //if (0 != stat(sde.blk, &st)) |
| 358 | { |
| 359 | ui_print("/sd-ext not present, skipping\n"); |
| 360 | } else { |
| 361 | __system("mount /sd-ext"); |
| 362 | LOGI("Mounting /sd-ext\n"); |
| 363 | if (stat("/sd-ext/dalvik-cache",&st) == 0) |
| 364 | { |
| 365 | __system("rm -rf /sd-ext/dalvik-cache"); |
| 366 | ui_print("Cleaned: /sd-ext/dalvik-cache...\n"); |
| 367 | } |
| 368 | } |
| 369 | ensure_path_unmounted("/data"); |
| 370 | ui_print("-- Dalvik Cache Directories Wipe Complete!\n\n"); |
| 371 | //ui_set_background(BACKGROUND_ICON_MAIN); |
| 372 | //if (!ui_text_visible()) return; |
| 373 | } |
| 374 | |
| 375 | // BATTERY STATS |
| 376 | void wipe_battery_stats() |
| 377 | { |
| 378 | ensure_path_mounted("/data"); |
| 379 | struct stat st; |
| 380 | if (0 != stat("/data/system/batterystats.bin", &st)) |
| 381 | { |
| 382 | ui_print("No Battery Stats Found. No Need To Wipe.\n"); |
| 383 | } else { |
| 384 | //ui_set_background(BACKGROUND_ICON_WIPE); |
| 385 | remove("/data/system/batterystats.bin"); |
| 386 | ui_print("Cleared: Battery Stats...\n"); |
| 387 | ensure_path_unmounted("/data"); |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | // ROTATION SETTINGS |
| 392 | void wipe_rotate_data() |
| 393 | { |
| 394 | //ui_set_background(BACKGROUND_ICON_WIPE); |
| 395 | ensure_path_mounted("/data"); |
| 396 | __system("rm -r /data/misc/akmd*"); |
| 397 | __system("rm -r /data/misc/rild*"); |
| 398 | ui_print("Cleared: Rotatation Data...\n"); |
| 399 | ensure_path_unmounted("/data"); |
| 400 | } |
| 401 | |
| 402 | void fix_perms() |
| 403 | { |
| 404 | ensure_path_mounted("/data"); |
| 405 | ensure_path_mounted("/system"); |
| 406 | //ui_show_progress(1,30); |
| 407 | ui_print("\n-- Fixing Permissions\n"); |
| 408 | ui_print("This may take a few minutes.\n"); |
| 409 | __system("./sbin/fix_permissions.sh"); |
| 410 | ui_print("-- Done.\n\n"); |
| 411 | //ui_reset_progress(); |
| 412 | } |
| 413 | |
| 414 | int get_battery_level(void) |
| 415 | { |
| 416 | static int lastVal = -1; |
| 417 | static time_t nextSecCheck = 0; |
| 418 | |
| 419 | struct timeval curTime; |
| 420 | gettimeofday(&curTime, NULL); |
| 421 | if (curTime.tv_sec > nextSecCheck) |
| 422 | { |
| 423 | char cap_s[4]; |
| 424 | FILE * cap = fopen("/sys/class/power_supply/battery/capacity","rt"); |
| 425 | if (cap) |
| 426 | { |
| 427 | fgets(cap_s, 4, cap); |
| 428 | fclose(cap); |
| 429 | lastVal = atoi(cap_s); |
| 430 | if (lastVal > 100) lastVal = 101; |
| 431 | if (lastVal < 0) lastVal = 0; |
| 432 | } |
| 433 | nextSecCheck = curTime.tv_sec + 60; |
| 434 | } |
| 435 | return lastVal; |
| 436 | } |
| 437 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 438 | void update_tz_environment_variables() { |
| 439 | setenv("TZ", DataManager_GetStrValue(TW_TIME_ZONE_VAR), 1); |
| 440 | tzset(); |
| 441 | } |
| 442 | |
| 443 | 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) |
| 444 | { |
| 445 | ui_print("%s", str1); |
| 446 | //ui_clear_key_queue(); |
| 447 | ui_print("\nPress Power to confirm,"); |
| 448 | ui_print("\nany other key to abort.\n"); |
| 449 | int confirm; |
| 450 | /*if (request_confirm) // this option is used to skip the confirmation when the gui is in use |
| 451 | confirm = ui_wait_key(); |
| 452 | else*/ |
| 453 | confirm = KEY_POWER; |
| 454 | |
| 455 | if (confirm == BTN_MOUSE || confirm == KEY_POWER || confirm == SELECT_ITEM) { |
| 456 | ui_print("%s", str2); |
| 457 | pid_t pid = fork(); |
| 458 | if (pid == 0) { |
| 459 | char *args[] = { "/sbin/sh", "-c", (char*)str3, "1>&2", NULL }; |
| 460 | execv("/sbin/sh", args); |
| 461 | fprintf(stderr, str4, strerror(errno)); |
| 462 | _exit(-1); |
| 463 | } |
| 464 | int status; |
| 465 | while (waitpid(pid, &status, WNOHANG) == 0) { |
| 466 | ui_print("."); |
| 467 | sleep(1); |
| 468 | } |
| 469 | ui_print("\n"); |
| 470 | if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) { |
| 471 | ui_print("%s", str5); |
| 472 | } else { |
| 473 | ui_print("%s", str6); |
| 474 | } |
| 475 | } else { |
| 476 | ui_print("%s", str7); |
| 477 | } |
| 478 | //if (!ui_text_visible()) return; |
| 479 | } |
| 480 | |
| 481 | void install_htc_dumlock(void) |
| 482 | { |
| 483 | struct statfs fs1, fs2; |
| 484 | int need_libs = 0; |
| 485 | |
| 486 | ui_print("Installing HTC Dumlock to system...\n"); |
| 487 | ensure_path_mounted("/system"); |
| 488 | __system("cp /res/htcd/htcdumlocksys /system/bin/htcdumlock && chmod 755 /system/bin/htcdumlock"); |
| 489 | if (statfs("/system/bin/flash_image", &fs1) != 0) { |
| 490 | ui_print("Installing flash_image...\n"); |
| 491 | __system("cp /res/htcd/flash_imagesys /system/bin/flash_image && chmod 755 /system/bin/flash_image"); |
| 492 | need_libs = 1; |
| 493 | } else |
| 494 | ui_print("flash_image is already installed, skipping...\n"); |
| 495 | if (statfs("/system/bin/dump_image", &fs2) != 0) { |
| 496 | ui_print("Installing dump_image...\n"); |
| 497 | __system("cp /res/htcd/dump_imagesys /system/bin/dump_image && chmod 755 /system/bin/dump_image"); |
| 498 | need_libs = 1; |
| 499 | } else |
| 500 | ui_print("dump_image is already installed, skipping...\n"); |
| 501 | if (need_libs) { |
| 502 | ui_print("Installing libs needed for flash_image and dump_image...\n"); |
| 503 | __system("cp /res/htcd/libbmlutils.so /system/lib && chmod 755 /system/lib/libbmlutils.so"); |
| 504 | __system("cp /res/htcd/libflashutils.so /system/lib && chmod 755 /system/lib/libflashutils.so"); |
| 505 | __system("cp /res/htcd/libmmcutils.so /system/lib && chmod 755 /system/lib/libmmcutils.so"); |
| 506 | __system("cp /res/htcd/libmtdutils.so /system/lib && chmod 755 /system/lib/libmtdutils.so"); |
| 507 | } |
| 508 | ui_print("Installing HTC Dumlock app...\n"); |
| 509 | ensure_path_mounted("/data"); |
| 510 | mkdir("/data/app", 0777); |
| 511 | __system("rm /data/app/com.teamwin.htcdumlock*"); |
| 512 | __system("cp /res/htcd/HTCDumlock.apk /data/app/com.teamwin.htcdumlock.apk"); |
| 513 | sync(); |
| 514 | ui_print("HTC Dumlock is installed.\n"); |
| 515 | } |
| 516 | |
| 517 | void htc_dumlock_restore_original_boot(void) |
| 518 | { |
| 519 | ui_print("Restoring original boot...\n"); |
| 520 | __system("htcdumlock restore"); |
| 521 | ui_print("Original boot restored.\n"); |
| 522 | } |
| 523 | |
| 524 | void htc_dumlock_reflash_recovery_to_boot(void) |
| 525 | { |
| 526 | ui_print("Reflashing recovery to boot...\n"); |
| 527 | __system("htcdumlock recovery noreboot"); |
| 528 | ui_print("Recovery is flashed to boot.\n"); |
| 529 | } |
| 530 | |
| 531 | void check_and_run_script(const char* script_file, const char* display_name) |
| 532 | { |
| 533 | // Check for and run startup script if script exists |
| 534 | struct statfs st; |
| 535 | if (statfs(script_file, &st) == 0) { |
| 536 | ui_print("Running %s script...\n", display_name); |
| 537 | char command[255]; |
| 538 | strcpy(command, "chmod 755 "); |
| 539 | strcat(command, script_file); |
| 540 | __system(command); |
| 541 | __system(script_file); |
| 542 | ui_print("\nFinished running %s script.\n", display_name); |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | int check_backup_name(int show_error) { |
| 547 | // Check the backup name to ensure that it is the correct size and contains only valid characters |
| 548 | // and that a backup with that name doesn't already exist |
| 549 | char backup_name[MAX_BACKUP_NAME_LEN]; |
| 550 | char backup_loc[255], tw_image_dir[255]; |
| 551 | int copy_size = strlen(DataManager_GetStrValue(TW_BACKUP_NAME)); |
| 552 | int index, cur_char; |
| 553 | struct statfs st; |
| 554 | |
| 555 | // Check size |
| 556 | if (copy_size > MAX_BACKUP_NAME_LEN) { |
| 557 | if (show_error) |
| 558 | LOGE("Backup name is too long.\n"); |
| 559 | return -2; |
| 560 | } |
| 561 | |
| 562 | // Check characters |
| 563 | strncpy(backup_name, DataManager_GetStrValue(TW_BACKUP_NAME), copy_size); |
| 564 | if (strcmp(backup_name, "0") == 0) |
| 565 | return 0; // A "0" (zero) means to use the current timestamp for the backup name |
| 566 | for (index=0; index<copy_size; index++) { |
| 567 | cur_char = (int)backup_name[index]; |
| 568 | if ((cur_char >= 48 && cur_char <= 57) || (cur_char >= 65 && cur_char <= 91) || cur_char == 93 || cur_char == 95 || (cur_char >= 97 && cur_char <= 123) || cur_char == 125 || cur_char == 45 || cur_char == 46) { |
| 569 | // These are valid characters |
| 570 | // Numbers |
| 571 | // Upper case letters |
| 572 | // Lower case letters |
| 573 | // and -_.{}[] |
| 574 | } else { |
| 575 | if (show_error) |
| 576 | LOGE("Backup name '%s' contains invalid character: '%c'\n", backup_name, (char)cur_char); |
| 577 | return -3; |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | // Check to make sure that a backup with this name doesn't already exist |
| 582 | strcpy(backup_loc, DataManager_GetStrValue(TW_BACKUPS_FOLDER_VAR)); |
| 583 | sprintf(tw_image_dir,"%s/%s/.", backup_loc, backup_name); |
| 584 | if (statfs(tw_image_dir, &st) == 0) { |
| 585 | if (show_error) |
| 586 | LOGE("A backup with this name already exists.\n"); |
| 587 | return -4; |
| 588 | } |
| 589 | |
| 590 | // No problems found, return 0 |
| 591 | return 0; |
| 592 | } |
Dees_Troy | 7d15c25 | 2012-09-05 20:47:21 -0400 | [diff] [blame] | 593 | |
| 594 | static const char *COMMAND_FILE = "/cache/recovery/command"; |
| 595 | static const char *INTENT_FILE = "/cache/recovery/intent"; |
| 596 | static const char *LOG_FILE = "/cache/recovery/log"; |
| 597 | static const char *LAST_LOG_FILE = "/cache/recovery/last_log"; |
| 598 | static const char *LAST_INSTALL_FILE = "/cache/recovery/last_install"; |
| 599 | static const char *CACHE_ROOT = "/cache"; |
| 600 | static const char *SDCARD_ROOT = "/sdcard"; |
| 601 | static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log"; |
| 602 | static const char *TEMPORARY_INSTALL_FILE = "/tmp/last_install"; |
| 603 | |
| 604 | // close a file, log an error if the error indicator is set |
| 605 | static void check_and_fclose(FILE *fp, const char *name) { |
| 606 | fflush(fp); |
| 607 | if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno)); |
| 608 | fclose(fp); |
| 609 | } |
| 610 | |
| 611 | static void copy_log_file(const char* source, const char* destination, int append) { |
| 612 | FILE *log = fopen_path(destination, append ? "a" : "w"); |
| 613 | if (log == NULL) { |
| 614 | LOGE("Can't open %s\n", destination); |
| 615 | } else { |
| 616 | FILE *tmplog = fopen(source, "r"); |
| 617 | if (tmplog != NULL) { |
| 618 | if (append) { |
| 619 | fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write |
| 620 | } |
| 621 | char buf[4096]; |
| 622 | while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log); |
| 623 | if (append) { |
| 624 | tmplog_offset = ftell(tmplog); |
| 625 | } |
| 626 | check_and_fclose(tmplog, source); |
| 627 | } |
| 628 | check_and_fclose(log, destination); |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | // clear the recovery command and prepare to boot a (hopefully working) system, |
| 633 | // copy our log file to cache as well (for the system to read), and |
| 634 | // record any intent we were asked to communicate back to the system. |
| 635 | // this function is idempotent: call it as many times as you like. |
| 636 | void twfinish_recovery(const char *send_intent) { |
| 637 | // By this point, we're ready to return to the main system... |
| 638 | if (send_intent != NULL) { |
| 639 | FILE *fp = fopen_path(INTENT_FILE, "w"); |
| 640 | if (fp == NULL) { |
| 641 | LOGE("Can't open %s\n", INTENT_FILE); |
| 642 | } else { |
| 643 | fputs(send_intent, fp); |
| 644 | check_and_fclose(fp, INTENT_FILE); |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | // Copy logs to cache so the system can find out what happened. |
| 649 | copy_log_file(TEMPORARY_LOG_FILE, LOG_FILE, true); |
| 650 | copy_log_file(TEMPORARY_LOG_FILE, LAST_LOG_FILE, false); |
| 651 | copy_log_file(TEMPORARY_INSTALL_FILE, LAST_INSTALL_FILE, false); |
| 652 | chmod(LOG_FILE, 0600); |
| 653 | chown(LOG_FILE, 1000, 1000); // system user |
| 654 | chmod(LAST_LOG_FILE, 0640); |
| 655 | chmod(LAST_INSTALL_FILE, 0644); |
| 656 | |
| 657 | // Reset to normal system boot so recovery won't cycle indefinitely. |
| 658 | struct bootloader_message boot; |
| 659 | memset(&boot, 0, sizeof(boot)); |
| 660 | set_bootloader_message(&boot); |
| 661 | |
| 662 | // Remove the command file, so recovery won't repeat indefinitely. |
| 663 | if (ensure_path_mounted(COMMAND_FILE) != 0 || |
| 664 | (unlink(COMMAND_FILE) && errno != ENOENT)) { |
| 665 | LOGW("Can't unlink %s\n", COMMAND_FILE); |
| 666 | } |
| 667 | |
| 668 | ensure_path_unmounted(CACHE_ROOT); |
| 669 | sync(); // For good measure. |
| 670 | } |
| 671 | |