#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "bootloader.h" #include "common.h" #include "cutils/properties.h" #include "firmware.h" #include "install.h" #include "minui/minui.h" #include "minzip/DirUtil.h" #include "roots.h" #include "recovery_ui.h" int signature_check_enabled = 1; int script_assert_enabled = 1; static const char *SDCARD_PACKAGE_FILE = "SDCARD:update.zip"; void toggle_signature_check() { signature_check_enabled = !signature_check_enabled; ui_print("Signature Check: %s\n", signature_check_enabled ? "Enabled" : "Disabled"); } void toggle_script_asserts() { script_assert_enabled = !script_assert_enabled; ui_print("Script Asserts: %s\n", script_assert_enabled ? "Enabled" : "Disabled"); } void install_zip(const char* packagefilepath) { ui_print("\n-- Installing: %s\n", packagefilepath); set_sdcard_update_bootloader_message(); int status = install_package(packagefilepath); if (status != INSTALL_SUCCESS) { ui_set_background(BACKGROUND_ICON_ERROR); ui_print("Installation aborted.\n"); return; } if (firmware_update_pending()) { ui_print("\nReboot via menu to complete\ninstallation.\n"); } ui_print("\nInstall from sdcard complete.\n"); ui_set_background(BACKGROUND_ICON_NONE); } char* INSTALL_MENU_ITEMS[] = { "apply sdcard:update.zip", "choose zip from sdcard", "toggle signature verification", "toggle script asserts", NULL }; #define ITEM_APPLY_SDCARD 0 #define ITEM_CHOOSE_ZIP 1 #define ITEM_SIG_CHECK 2 #define ITEM_ASSERTS 3 void show_install_update_menu() { static char* headers[] = { "Apply update from .zip file on SD card", "", NULL }; for (;;) { int chosen_item = get_menu_selection(headers, INSTALL_MENU_ITEMS, 0); switch (chosen_item) { case ITEM_ASSERTS: toggle_script_asserts(); break; case ITEM_SIG_CHECK: toggle_signature_check(); break; case ITEM_APPLY_SDCARD: install_zip(SDCARD_PACKAGE_FILE); break; case ITEM_CHOOSE_ZIP: show_choose_zip_menu(); break; default: return; } } } char** gather_files(const char* directory, const char* fileExtensionOrDirectory, int* numFiles) { char path[PATH_MAX] = ""; DIR *dir; struct dirent *de; int total = 0; int i; char** files; int pass; *numFiles = 0; int dirLen = strlen(directory); dir = opendir(directory); if (dir == NULL) { ui_print("Couldn't open directory.\n"); return NULL; } int extension_length; if (fileExtensionOrDirectory != NULL) extension_length = strlen(fileExtensionOrDirectory); int isCounting = 1; i = 0; for (pass = 0; pass < 2; pass++) { while ((de=readdir(dir)) != NULL) { // skip hidden files if (de->d_name[0] == '.') continue; // NULL means that we are gathering directories, so skip this if (fileExtensionOrDirectory != NULL) { // make sure that we can have the desired extension (prevent seg fault) if (strlen(de->d_name) < extension_length) continue; // compare the extension if (strcmp(de->d_name + strlen(de->d_name) - extension_length, fileExtensionOrDirectory) != 0) continue; } else { struct stat info; char* fullFileName = (char*)malloc(strlen(de->d_name) + dirLen + 1); strcpy(fullFileName, directory); strcat(fullFileName, de->d_name); stat(fullFileName, &info); free(fullFileName); // make sure it is a directory if (!(S_ISDIR(info.st_mode))) continue; } if (pass == 0) { total++; continue; } files[i] = (char*) malloc(dirLen + strlen(de->d_name) + 2); strcpy(files[i], directory); strcat(files[i], de->d_name); if (fileExtensionOrDirectory == NULL) strcat(files[i], "/"); i++; } if (pass == 1) break; if (total == 0) break; rewinddir(dir); *numFiles = total; files = (char**) malloc((total+1)*sizeof(char*)); files[total]=NULL; } if(closedir(dir) < 0) { LOGE("Failed to close directory."); } if (total==0) { return NULL; } return files; } void free_string_array(char** array) { char* cursor = array[0]; int i = 0; while (cursor != NULL) { free(cursor); cursor = array[++i]; } free(array); } // pass in NULL for fileExtensionOrDirectory and you will get a directory chooser char* choose_file_menu(const char* directory, const char* fileExtensionOrDirectory, const char* headers[]) { char path[PATH_MAX] = ""; DIR *dir; struct dirent *de; int numFiles = 0; int numDirs = 0; int i; int dir_len = strlen(directory); char** files = gather_files(directory, fileExtensionOrDirectory, &numFiles); char** dirs; if (fileExtensionOrDirectory != NULL) dirs = gather_files(directory, NULL, &numDirs); int total = numDirs + numFiles; if (total == 0) { ui_print("No files found.\n"); return NULL; } char** list = (char**) malloc((total + 1) * sizeof(char*)); list[total] = NULL; for (i = 0 ; i < numDirs; i++) { list[i] = strdup(dirs[i] + dir_len); } for (i = 0 ; i < numFiles; i++) { list[numDirs + i] = strdup(files[i] + dir_len); } for (;;) { int chosen_item = get_menu_selection(headers, list, 0); if (chosen_item == GO_BACK) break; if (chosen_item < numDirs) { char* subret = choose_file_menu(dirs[chosen_item], fileExtensionOrDirectory, headers); if (subret != NULL) return subret; continue; } static char ret[PATH_MAX]; strcpy(ret, files[chosen_item - numDirs]); ui_print("File chosen: %s\n", ret); return ret; } return NULL; } void show_choose_zip_menu() { if (ensure_root_path_mounted("SDCARD:") != 0) { LOGE ("Can't mount /sdcard\n"); return; } static char* headers[] = { "Choose a zip to apply", "", NULL }; char* file = choose_file_menu("/sdcard/", ".zip", headers); if (file == NULL) return; char sdcard_package_file[1024]; strcpy(sdcard_package_file, "SDCARD:"); strcat(sdcard_package_file, file + strlen("/sdcard/")); install_zip(sdcard_package_file); } // This was pulled from bionic: The default system command always looks // for shell in /system/bin/sh. This is bad. #define _PATH_BSHELL "/sbin/sh" #define system recovery_system extern char **environ; int system(const char *command) { pid_t pid; sig_t intsave, quitsave; sigset_t mask, omask; int pstat; char *argp[] = {"sh", "-c", NULL, NULL}; if (!command) /* just checking... */ return(1); argp[2] = (char *)command; sigemptyset(&mask); sigaddset(&mask, SIGCHLD); sigprocmask(SIG_BLOCK, &mask, &omask); switch (pid = vfork()) { case -1: /* error */ sigprocmask(SIG_SETMASK, &omask, NULL); return(-1); case 0: /* child */ sigprocmask(SIG_SETMASK, &omask, NULL); execve(_PATH_BSHELL, argp, environ); _exit(127); } intsave = (sig_t) bsd_signal(SIGINT, SIG_IGN); quitsave = (sig_t) bsd_signal(SIGQUIT, SIG_IGN); pid = waitpid(pid, (int *)&pstat, 0); sigprocmask(SIG_SETMASK, &omask, NULL); (void)bsd_signal(SIGINT, intsave); (void)bsd_signal(SIGQUIT, quitsave); return (pid == -1 ? -1 : pstat); } void do_nandroid_backup() { ui_print("Performing backup...\n"); int ret = system("/sbin/nandroid-mobile.sh backup /sdcard/clockworkmod/backup/"); if (ret != 0) { ui_print("Error while backing up! Error code: %d\n", ret); return; } ui_print("Backup complete.\n"); } void show_nandroid_restore_menu() { if (ensure_root_path_mounted("SDCARD:") != 0) { LOGE ("Can't mount /sdcard\n"); return; } static char* headers[] = { "Choose an image to restore", "", NULL }; char* file = choose_file_menu("/sdcard/clockworkmod/backup/", NULL, headers); if (file == NULL) return; char* command[PATH_MAX]; sprintf(command, "nandroid-mobile.sh restore %s", file); ui_print("Performing restore...\n"); int ret = system(command); if (ret != 0) { ui_print("Error while restoring!\n"); return; } ui_print("Restore complete.\n"); }