Koushik K. Dutta | 6060e5c | 2010-02-11 22:27:06 -0800 | [diff] [blame] | 1 | #include <ctype.h> |
| 2 | #include <errno.h> |
| 3 | #include <fcntl.h> |
| 4 | #include <getopt.h> |
| 5 | #include <limits.h> |
| 6 | #include <linux/input.h> |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <string.h> |
| 10 | #include <sys/reboot.h> |
| 11 | #include <sys/types.h> |
| 12 | #include <time.h> |
| 13 | #include <unistd.h> |
| 14 | |
Koushik K. Dutta | e923487 | 2010-02-12 00:43:24 -0800 | [diff] [blame] | 15 | #include <sys/wait.h> |
| 16 | #include <sys/limits.h> |
| 17 | #include <dirent.h> |
Koushik K. Dutta | 49f5689 | 2010-02-25 14:51:05 -0800 | [diff] [blame] | 18 | #include <sys/stat.h> |
Koushik K. Dutta | e923487 | 2010-02-12 00:43:24 -0800 | [diff] [blame] | 19 | |
Koushik K. Dutta | 33370db | 2010-02-25 11:39:07 -0800 | [diff] [blame] | 20 | #include <signal.h> |
| 21 | #include <sys/wait.h> |
| 22 | |
Koushik K. Dutta | 6060e5c | 2010-02-11 22:27:06 -0800 | [diff] [blame] | 23 | #include "bootloader.h" |
| 24 | #include "common.h" |
| 25 | #include "cutils/properties.h" |
| 26 | #include "firmware.h" |
| 27 | #include "install.h" |
| 28 | #include "minui/minui.h" |
| 29 | #include "minzip/DirUtil.h" |
| 30 | #include "roots.h" |
| 31 | #include "recovery_ui.h" |
| 32 | |
Koushik K. Dutta | f68aaaf | 2010-03-07 13:39:21 -0800 | [diff] [blame] | 33 | #include "commands.h" |
| 34 | #include "amend/amend.h" |
| 35 | |
Koushik K. Dutta | a85d7cc | 2010-03-12 17:00:58 -0800 | [diff] [blame] | 36 | #include "mtdutils/dump_image.h" |
| 37 | #include "../../external/yaffs2/yaffs2/utils/mkyaffs2image.h" |
Koushik K. Dutta | 54305a8 | 2010-03-12 17:43:26 -0800 | [diff] [blame] | 38 | #include "../../external/yaffs2/yaffs2/utils/unyaffs.h" |
Koushik K. Dutta | a85d7cc | 2010-03-12 17:00:58 -0800 | [diff] [blame] | 39 | |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 40 | #include "extendedcommands.h" |
| 41 | #include "nandroid.h" |
| 42 | |
Koushik K. Dutta | 6060e5c | 2010-02-11 22:27:06 -0800 | [diff] [blame] | 43 | int signature_check_enabled = 1; |
| 44 | int script_assert_enabled = 1; |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 45 | static const char *SDCARD_PACKAGE_FILE = "SDCARD:update.zip"; |
Koushik K. Dutta | 6060e5c | 2010-02-11 22:27:06 -0800 | [diff] [blame] | 46 | |
| 47 | void |
| 48 | toggle_signature_check() |
| 49 | { |
| 50 | signature_check_enabled = !signature_check_enabled; |
| 51 | ui_print("Signature Check: %s\n", signature_check_enabled ? "Enabled" : "Disabled"); |
| 52 | } |
| 53 | |
| 54 | void toggle_script_asserts() |
| 55 | { |
| 56 | script_assert_enabled = !script_assert_enabled; |
Koushik K. Dutta | e923487 | 2010-02-12 00:43:24 -0800 | [diff] [blame] | 57 | ui_print("Script Asserts: %s\n", script_assert_enabled ? "Enabled" : "Disabled"); |
| 58 | } |
| 59 | |
Koushik K. Dutta | ea46fe2 | 2010-03-08 02:58:04 -0800 | [diff] [blame] | 60 | int install_zip(const char* packagefilepath) |
Koushik K. Dutta | e923487 | 2010-02-12 00:43:24 -0800 | [diff] [blame] | 61 | { |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 62 | ui_print("\n-- Installing: %s\n", packagefilepath); |
| 63 | set_sdcard_update_bootloader_message(); |
| 64 | int status = install_package(packagefilepath); |
Koushik K. Dutta | f3534d0 | 2010-04-18 18:06:24 -0700 | [diff] [blame] | 65 | ui_reset_progress(); |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 66 | if (status != INSTALL_SUCCESS) { |
| 67 | ui_set_background(BACKGROUND_ICON_ERROR); |
| 68 | ui_print("Installation aborted.\n"); |
Koushik K. Dutta | ea46fe2 | 2010-03-08 02:58:04 -0800 | [diff] [blame] | 69 | return 1; |
Koushik K. Dutta | 79ce82c | 2010-02-25 12:03:17 -0800 | [diff] [blame] | 70 | } |
| 71 | if (firmware_update_pending()) { |
| 72 | ui_print("\nReboot via menu to complete\ninstallation.\n"); |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 73 | } |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 74 | ui_set_background(BACKGROUND_ICON_NONE); |
Koushik K. Dutta | 99fb6fe | 2010-03-03 00:42:58 -0800 | [diff] [blame] | 75 | ui_print("\nInstall from sdcard complete.\n"); |
Koushik K. Dutta | ea46fe2 | 2010-03-08 02:58:04 -0800 | [diff] [blame] | 76 | return 0; |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | char* INSTALL_MENU_ITEMS[] = { "apply sdcard:update.zip", |
| 80 | "choose zip from sdcard", |
| 81 | "toggle signature verification", |
| 82 | "toggle script asserts", |
| 83 | NULL }; |
| 84 | #define ITEM_APPLY_SDCARD 0 |
| 85 | #define ITEM_CHOOSE_ZIP 1 |
| 86 | #define ITEM_SIG_CHECK 2 |
| 87 | #define ITEM_ASSERTS 3 |
| 88 | |
| 89 | void show_install_update_menu() |
| 90 | { |
| 91 | static char* headers[] = { "Apply update from .zip file on SD card", |
| 92 | "", |
| 93 | NULL |
| 94 | }; |
| 95 | for (;;) |
| 96 | { |
| 97 | int chosen_item = get_menu_selection(headers, INSTALL_MENU_ITEMS, 0); |
| 98 | switch (chosen_item) |
| 99 | { |
| 100 | case ITEM_ASSERTS: |
| 101 | toggle_script_asserts(); |
| 102 | break; |
| 103 | case ITEM_SIG_CHECK: |
| 104 | toggle_signature_check(); |
| 105 | break; |
| 106 | case ITEM_APPLY_SDCARD: |
| 107 | install_zip(SDCARD_PACKAGE_FILE); |
| 108 | break; |
| 109 | case ITEM_CHOOSE_ZIP: |
| 110 | show_choose_zip_menu(); |
| 111 | break; |
| 112 | default: |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | } |
| 117 | } |
| 118 | |
Koushik K. Dutta | 1d53c4e | 2010-04-02 16:46:21 -0700 | [diff] [blame] | 119 | void free_string_array(char** array) |
| 120 | { |
| 121 | if (array == NULL) |
| 122 | return; |
| 123 | char* cursor = array[0]; |
| 124 | int i = 0; |
| 125 | while (cursor != NULL) |
| 126 | { |
| 127 | free(cursor); |
| 128 | cursor = array[++i]; |
| 129 | } |
| 130 | free(array); |
| 131 | } |
| 132 | |
Koushik K. Dutta | 49f5689 | 2010-02-25 14:51:05 -0800 | [diff] [blame] | 133 | char** gather_files(const char* directory, const char* fileExtensionOrDirectory, int* numFiles) |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 134 | { |
Koushik K. Dutta | e923487 | 2010-02-12 00:43:24 -0800 | [diff] [blame] | 135 | char path[PATH_MAX] = ""; |
| 136 | DIR *dir; |
| 137 | struct dirent *de; |
| 138 | int total = 0; |
| 139 | int i; |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 140 | char** files = NULL; |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 141 | int pass; |
| 142 | *numFiles = 0; |
| 143 | int dirLen = strlen(directory); |
Koushik K. Dutta | e923487 | 2010-02-12 00:43:24 -0800 | [diff] [blame] | 144 | |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 145 | dir = opendir(directory); |
| 146 | if (dir == NULL) { |
| 147 | ui_print("Couldn't open directory.\n"); |
| 148 | return NULL; |
| 149 | } |
| 150 | |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 151 | int extension_length = 0; |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 152 | if (fileExtensionOrDirectory != NULL) |
| 153 | extension_length = strlen(fileExtensionOrDirectory); |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 154 | |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 155 | int isCounting = 1; |
| 156 | i = 0; |
| 157 | for (pass = 0; pass < 2; pass++) { |
| 158 | while ((de=readdir(dir)) != NULL) { |
| 159 | // skip hidden files |
| 160 | if (de->d_name[0] == '.') |
| 161 | continue; |
| 162 | |
| 163 | // NULL means that we are gathering directories, so skip this |
| 164 | if (fileExtensionOrDirectory != NULL) |
| 165 | { |
| 166 | // make sure that we can have the desired extension (prevent seg fault) |
| 167 | if (strlen(de->d_name) < extension_length) |
| 168 | continue; |
| 169 | // compare the extension |
| 170 | if (strcmp(de->d_name + strlen(de->d_name) - extension_length, fileExtensionOrDirectory) != 0) |
| 171 | continue; |
| 172 | } |
| 173 | else |
| 174 | { |
| 175 | struct stat info; |
Koushik K. Dutta | 1d53c4e | 2010-04-02 16:46:21 -0700 | [diff] [blame] | 176 | char fullFileName[PATH_MAX]; |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 177 | strcpy(fullFileName, directory); |
| 178 | strcat(fullFileName, de->d_name); |
| 179 | stat(fullFileName, &info); |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 180 | // make sure it is a directory |
| 181 | if (!(S_ISDIR(info.st_mode))) |
| 182 | continue; |
| 183 | } |
| 184 | |
| 185 | if (pass == 0) |
| 186 | { |
| 187 | total++; |
| 188 | continue; |
| 189 | } |
| 190 | |
Koushik K. Dutta | 49f5689 | 2010-02-25 14:51:05 -0800 | [diff] [blame] | 191 | files[i] = (char*) malloc(dirLen + strlen(de->d_name) + 2); |
| 192 | strcpy(files[i], directory); |
| 193 | strcat(files[i], de->d_name); |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 194 | if (fileExtensionOrDirectory == NULL) |
| 195 | strcat(files[i], "/"); |
| 196 | i++; |
| 197 | } |
| 198 | if (pass == 1) |
| 199 | break; |
| 200 | if (total == 0) |
| 201 | break; |
| 202 | rewinddir(dir); |
| 203 | *numFiles = total; |
| 204 | files = (char**) malloc((total+1)*sizeof(char*)); |
| 205 | files[total]=NULL; |
| 206 | } |
Koushik K. Dutta | 49f5689 | 2010-02-25 14:51:05 -0800 | [diff] [blame] | 207 | |
| 208 | if(closedir(dir) < 0) { |
| 209 | LOGE("Failed to close directory."); |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | if (total==0) { |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 213 | return NULL; |
| 214 | } |
| 215 | |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 216 | return files; |
Koushik K. Dutta | 49f5689 | 2010-02-25 14:51:05 -0800 | [diff] [blame] | 217 | } |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 218 | |
Koushik K. Dutta | 49f5689 | 2010-02-25 14:51:05 -0800 | [diff] [blame] | 219 | // pass in NULL for fileExtensionOrDirectory and you will get a directory chooser |
| 220 | char* choose_file_menu(const char* directory, const char* fileExtensionOrDirectory, const char* headers[]) |
| 221 | { |
| 222 | char path[PATH_MAX] = ""; |
| 223 | DIR *dir; |
| 224 | struct dirent *de; |
| 225 | int numFiles = 0; |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 226 | int numDirs = 0; |
Koushik K. Dutta | 49f5689 | 2010-02-25 14:51:05 -0800 | [diff] [blame] | 227 | int i; |
Koushik K. Dutta | 1d53c4e | 2010-04-02 16:46:21 -0700 | [diff] [blame] | 228 | char* return_value = NULL; |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 229 | int dir_len = strlen(directory); |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 230 | |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 231 | char** files = gather_files(directory, fileExtensionOrDirectory, &numFiles); |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 232 | char** dirs = NULL; |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 233 | if (fileExtensionOrDirectory != NULL) |
| 234 | dirs = gather_files(directory, NULL, &numDirs); |
| 235 | int total = numDirs + numFiles; |
| 236 | if (total == 0) |
| 237 | { |
| 238 | ui_print("No files found.\n"); |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 239 | } |
Koushik K. Dutta | 1d53c4e | 2010-04-02 16:46:21 -0700 | [diff] [blame] | 240 | else |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 241 | { |
Koushik K. Dutta | 1d53c4e | 2010-04-02 16:46:21 -0700 | [diff] [blame] | 242 | char** list = (char**) malloc((total + 1) * sizeof(char*)); |
| 243 | list[total] = NULL; |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 244 | |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 245 | |
Koushik K. Dutta | 1d53c4e | 2010-04-02 16:46:21 -0700 | [diff] [blame] | 246 | for (i = 0 ; i < numDirs; i++) |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 247 | { |
Koushik K. Dutta | 1d53c4e | 2010-04-02 16:46:21 -0700 | [diff] [blame] | 248 | list[i] = strdup(dirs[i] + dir_len); |
| 249 | } |
| 250 | |
| 251 | for (i = 0 ; i < numFiles; i++) |
| 252 | { |
| 253 | list[numDirs + i] = strdup(files[i] + dir_len); |
| 254 | } |
| 255 | |
| 256 | for (;;) |
| 257 | { |
| 258 | int chosen_item = get_menu_selection(headers, list, 0); |
| 259 | if (chosen_item == GO_BACK) |
| 260 | break; |
| 261 | static char ret[PATH_MAX]; |
| 262 | if (chosen_item < numDirs) |
| 263 | { |
| 264 | char* subret = choose_file_menu(dirs[chosen_item], fileExtensionOrDirectory, headers); |
| 265 | if (subret != NULL) |
| 266 | { |
| 267 | strcpy(ret, subret); |
| 268 | return_value = ret; |
| 269 | break; |
| 270 | } |
| 271 | continue; |
| 272 | } |
| 273 | strcpy(ret, files[chosen_item - numDirs]); |
| 274 | return_value = ret; |
| 275 | break; |
| 276 | } |
| 277 | free_string_array(list); |
Koushik K. Dutta | 981b0cd | 2010-02-22 08:53:34 -0800 | [diff] [blame] | 278 | } |
Koushik K. Dutta | 1d53c4e | 2010-04-02 16:46:21 -0700 | [diff] [blame] | 279 | |
| 280 | free_string_array(files); |
| 281 | free_string_array(dirs); |
| 282 | return return_value; |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | void show_choose_zip_menu() |
| 286 | { |
Koushik K. Dutta | e923487 | 2010-02-12 00:43:24 -0800 | [diff] [blame] | 287 | if (ensure_root_path_mounted("SDCARD:") != 0) { |
| 288 | LOGE ("Can't mount /sdcard\n"); |
| 289 | return; |
| 290 | } |
| 291 | |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 292 | static char* headers[] = { "Choose a zip to apply", |
| 293 | "", |
| 294 | NULL |
| 295 | }; |
| 296 | |
| 297 | char* file = choose_file_menu("/sdcard/", ".zip", headers); |
| 298 | if (file == NULL) |
| 299 | return; |
| 300 | char sdcard_package_file[1024]; |
| 301 | strcpy(sdcard_package_file, "SDCARD:"); |
| 302 | strcat(sdcard_package_file, file + strlen("/sdcard/")); |
| 303 | install_zip(sdcard_package_file); |
| 304 | } |
| 305 | |
Koushik K. Dutta | 33370db | 2010-02-25 11:39:07 -0800 | [diff] [blame] | 306 | // This was pulled from bionic: The default system command always looks |
| 307 | // for shell in /system/bin/sh. This is bad. |
| 308 | #define _PATH_BSHELL "/sbin/sh" |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 309 | |
Koushik K. Dutta | 33370db | 2010-02-25 11:39:07 -0800 | [diff] [blame] | 310 | extern char **environ; |
| 311 | int |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 312 | __system(const char *command) |
Koushik K. Dutta | 33370db | 2010-02-25 11:39:07 -0800 | [diff] [blame] | 313 | { |
| 314 | pid_t pid; |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 315 | sig_t intsave, quitsave; |
| 316 | sigset_t mask, omask; |
| 317 | int pstat; |
| 318 | char *argp[] = {"sh", "-c", NULL, NULL}; |
Koushik K. Dutta | 33370db | 2010-02-25 11:39:07 -0800 | [diff] [blame] | 319 | |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 320 | if (!command) /* just checking... */ |
| 321 | return(1); |
Koushik K. Dutta | 33370db | 2010-02-25 11:39:07 -0800 | [diff] [blame] | 322 | |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 323 | argp[2] = (char *)command; |
Koushik K. Dutta | 33370db | 2010-02-25 11:39:07 -0800 | [diff] [blame] | 324 | |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 325 | sigemptyset(&mask); |
| 326 | sigaddset(&mask, SIGCHLD); |
| 327 | sigprocmask(SIG_BLOCK, &mask, &omask); |
| 328 | switch (pid = vfork()) { |
| 329 | case -1: /* error */ |
| 330 | sigprocmask(SIG_SETMASK, &omask, NULL); |
| 331 | return(-1); |
| 332 | case 0: /* child */ |
| 333 | sigprocmask(SIG_SETMASK, &omask, NULL); |
| 334 | execve(_PATH_BSHELL, argp, environ); |
Koushik K. Dutta | 33370db | 2010-02-25 11:39:07 -0800 | [diff] [blame] | 335 | _exit(127); |
| 336 | } |
| 337 | |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 338 | intsave = (sig_t) bsd_signal(SIGINT, SIG_IGN); |
| 339 | quitsave = (sig_t) bsd_signal(SIGQUIT, SIG_IGN); |
| 340 | pid = waitpid(pid, (int *)&pstat, 0); |
| 341 | sigprocmask(SIG_SETMASK, &omask, NULL); |
| 342 | (void)bsd_signal(SIGINT, intsave); |
| 343 | (void)bsd_signal(SIGQUIT, quitsave); |
| 344 | return (pid == -1 ? -1 : pstat); |
Koushik K. Dutta | 33370db | 2010-02-25 11:39:07 -0800 | [diff] [blame] | 345 | } |
| 346 | |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 347 | void show_nandroid_restore_menu() |
| 348 | { |
Koushik K. Dutta | 54305a8 | 2010-03-12 17:43:26 -0800 | [diff] [blame] | 349 | if (ensure_root_path_mounted("SDCARD:") != 0) { |
| 350 | LOGE ("Can't mount /sdcard\n"); |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 351 | return; |
Koushik K. Dutta | 54305a8 | 2010-03-12 17:43:26 -0800 | [diff] [blame] | 352 | } |
| 353 | |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 354 | static char* headers[] = { "Choose an image to restore", |
| 355 | "", |
| 356 | NULL |
| 357 | }; |
Koushik K. Dutta | e923487 | 2010-02-12 00:43:24 -0800 | [diff] [blame] | 358 | |
Koushik K. Dutta | 49f5689 | 2010-02-25 14:51:05 -0800 | [diff] [blame] | 359 | char* file = choose_file_menu("/sdcard/clockworkmod/backup/", NULL, headers); |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 360 | if (file == NULL) |
| 361 | return; |
Koushik K. Dutta | 68b0190 | 2010-04-01 12:20:39 -0700 | [diff] [blame] | 362 | nandroid_restore(file, 1, 1, 1, 1, 1); |
Koushik K. Dutta | 0317378 | 2010-02-26 14:14:23 -0800 | [diff] [blame] | 363 | } |
| 364 | |
Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 365 | void show_mount_usb_storage_menu() |
Koushik K. Dutta | 0317378 | 2010-02-26 14:14:23 -0800 | [diff] [blame] | 366 | { |
Koushik K. Dutta | f721594 | 2010-03-16 13:34:51 -0700 | [diff] [blame] | 367 | __system("echo /dev/block/mmcblk0 > /sys/devices/platform/usb_mass_storage/lun0/file"); |
Koushik K. Dutta | 0317378 | 2010-02-26 14:14:23 -0800 | [diff] [blame] | 368 | static char* headers[] = { "USB Mass Storage device", |
| 369 | "Leaving this menu unmount", |
| 370 | "your SD card from your PC.", |
| 371 | "", |
| 372 | NULL |
| 373 | }; |
| 374 | |
| 375 | static char* list[] = { "Unmount", NULL }; |
| 376 | |
| 377 | for (;;) |
| 378 | { |
| 379 | int chosen_item = get_menu_selection(headers, list, 0); |
| 380 | if (chosen_item == GO_BACK || chosen_item == 0) |
| 381 | break; |
| 382 | } |
| 383 | |
Koushik K. Dutta | f721594 | 2010-03-16 13:34:51 -0700 | [diff] [blame] | 384 | __system("echo '' > /sys/devices/platform/usb_mass_storage/lun0/file"); |
| 385 | __system("echo 0 > /sys/devices/platform/usb_mass_storage/lun0/enable"); |
Koushik K. Dutta | e81cb75 | 2010-02-26 17:44:33 -0800 | [diff] [blame] | 386 | } |
Koushik K. Dutta | f68aaaf | 2010-03-07 13:39:21 -0800 | [diff] [blame] | 387 | |
Koushik K. Dutta | 16f0b49 | 2010-03-19 14:37:11 -0700 | [diff] [blame] | 388 | int confirm_format() |
| 389 | { |
| 390 | static char* title_headers[] = { "Confirm format?", |
| 391 | " THIS CAN NOT BE UNDONE.", |
| 392 | "", |
| 393 | NULL, |
| 394 | }; |
| 395 | |
| 396 | char* items[] = { " No", |
| 397 | " No", |
| 398 | " No", |
| 399 | " No", |
| 400 | " No", |
| 401 | " No", |
| 402 | " No", |
| 403 | " Yes -- wipe partition", // [7] |
| 404 | " No", |
| 405 | " No", |
| 406 | " No", |
| 407 | NULL }; |
| 408 | |
| 409 | int chosen_item = get_menu_selection(title_headers, items, 0); |
| 410 | return chosen_item == 7; |
| 411 | } |
| 412 | |
Koushik Dutta | 2f73e58 | 2010-04-18 16:00:21 -0700 | [diff] [blame] | 413 | int format_non_mtd_device(const char* root) |
Koushik K. Dutta | 3f99539 | 2010-03-30 23:29:43 -0700 | [diff] [blame] | 414 | { |
Koushik K. Dutta | f3534d0 | 2010-04-18 18:06:24 -0700 | [diff] [blame] | 415 | // if this is SDEXT:, don't worry about it. |
| 416 | if (0 == strcmp(root, "SDEXT:")) |
| 417 | { |
| 418 | struct stat st; |
| 419 | if (0 != stat("/dev/block/mmcblk0p2", &st)) |
| 420 | { |
| 421 | ui_print("No app2sd partition found. Skipping format of /sd-ext.\n"); |
| 422 | return 0; |
| 423 | } |
| 424 | } |
Koushik Dutta | 2f73e58 | 2010-04-18 16:00:21 -0700 | [diff] [blame] | 425 | |
Koushik K. Dutta | 3f99539 | 2010-03-30 23:29:43 -0700 | [diff] [blame] | 426 | char path[PATH_MAX]; |
| 427 | translate_root_path(root, path, PATH_MAX); |
| 428 | if (0 != ensure_root_path_mounted(root)) |
| 429 | { |
| 430 | ui_print("Error mounting %s!\n", path); |
| 431 | return 1; |
| 432 | } |
| 433 | |
| 434 | static char tmp[PATH_MAX]; |
| 435 | sprintf(tmp, "rm -rf %s/*", path); |
| 436 | __system(tmp); |
| 437 | sprintf(tmp, "rm -rf %s/.*", path); |
| 438 | __system(tmp); |
| 439 | |
| 440 | ensure_root_path_unmounted(root); |
| 441 | return 0; |
| 442 | } |
| 443 | |
| 444 | #define MOUNTABLE_COUNT 5 |
| 445 | #define MTD_COUNT 4 |
| 446 | #define MMC_COUNT 2 |
Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 447 | |
Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame] | 448 | void show_partition_menu() |
Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 449 | { |
| 450 | static char* headers[] = { "Mount and unmount partitions", |
| 451 | "", |
| 452 | NULL |
| 453 | }; |
| 454 | |
| 455 | typedef char* string; |
Koushik K. Dutta | 16f0b49 | 2010-03-19 14:37:11 -0700 | [diff] [blame] | 456 | string mounts[MOUNTABLE_COUNT][3] = { |
| 457 | { "mount /system", "unmount /system", "SYSTEM:" }, |
| 458 | { "mount /data", "unmount /data", "DATA:" }, |
| 459 | { "mount /cache", "unmount /cache", "CACHE:" }, |
Koushik K. Dutta | 3f99539 | 2010-03-30 23:29:43 -0700 | [diff] [blame] | 460 | { "mount /sdcard", "unmount /sdcard", "SDCARD:" }, |
| 461 | { "mount /sd-ext", "unmount /sd-ext", "SDEXT:" } |
Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 462 | }; |
| 463 | |
Koushik K. Dutta | 16f0b49 | 2010-03-19 14:37:11 -0700 | [diff] [blame] | 464 | string mtds[MTD_COUNT][2] = { |
Koushik K. Dutta | 3f99539 | 2010-03-30 23:29:43 -0700 | [diff] [blame] | 465 | { "format boot", "BOOT:" }, |
| 466 | { "format system", "SYSTEM:" }, |
| 467 | { "format data", "DATA:" }, |
| 468 | { "format cache", "CACHE:" }, |
| 469 | }; |
| 470 | |
| 471 | string mmcs[MMC_COUNT][3] = { |
| 472 | { "format sdcard", "SDCARD:" }, |
Koushik K. Dutta | a5f64e6 | 2010-04-05 15:44:57 -0700 | [diff] [blame] | 473 | { "format sd-ext", "SDEXT:" } |
Koushik K. Dutta | 16f0b49 | 2010-03-19 14:37:11 -0700 | [diff] [blame] | 474 | }; |
| 475 | |
Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 476 | for (;;) |
| 477 | { |
Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame] | 478 | int ismounted[MOUNTABLE_COUNT]; |
Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 479 | int i; |
Koushik K. Dutta | 3f99539 | 2010-03-30 23:29:43 -0700 | [diff] [blame] | 480 | static string options[MOUNTABLE_COUNT + MTD_COUNT + MMC_COUNT + 1 + 1]; // mountables, format mtds, format mmcs, usb storage, null |
Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame] | 481 | for (i = 0; i < MOUNTABLE_COUNT; i++) |
Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 482 | { |
Koushik K. Dutta | 16f0b49 | 2010-03-19 14:37:11 -0700 | [diff] [blame] | 483 | ismounted[i] = is_root_path_mounted(mounts[i][2]); |
Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 484 | options[i] = ismounted[i] ? mounts[i][1] : mounts[i][0]; |
| 485 | } |
Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame] | 486 | |
| 487 | for (i = 0; i < MTD_COUNT; i++) |
| 488 | { |
Koushik K. Dutta | 16f0b49 | 2010-03-19 14:37:11 -0700 | [diff] [blame] | 489 | options[MOUNTABLE_COUNT + i] = mtds[i][0]; |
Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame] | 490 | } |
Koushik K. Dutta | 3f99539 | 2010-03-30 23:29:43 -0700 | [diff] [blame] | 491 | |
| 492 | for (i = 0; i < MMC_COUNT; i++) |
| 493 | { |
| 494 | options[MOUNTABLE_COUNT + MTD_COUNT + i] = mmcs[i][0]; |
| 495 | } |
Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 496 | |
Koushik K. Dutta | 3f99539 | 2010-03-30 23:29:43 -0700 | [diff] [blame] | 497 | options[MOUNTABLE_COUNT + MTD_COUNT + MMC_COUNT] = "mount USB storage"; |
| 498 | options[MOUNTABLE_COUNT + MTD_COUNT + MMC_COUNT + 1] = NULL; |
Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 499 | |
| 500 | int chosen_item = get_menu_selection(headers, options, 0); |
| 501 | if (chosen_item == GO_BACK) |
| 502 | break; |
Koushik K. Dutta | 3f99539 | 2010-03-30 23:29:43 -0700 | [diff] [blame] | 503 | if (chosen_item == MOUNTABLE_COUNT + MTD_COUNT + MMC_COUNT) |
Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 504 | { |
| 505 | show_mount_usb_storage_menu(); |
| 506 | } |
Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame] | 507 | else if (chosen_item < MOUNTABLE_COUNT) |
Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 508 | { |
| 509 | if (ismounted[chosen_item]) |
| 510 | { |
Koushik K. Dutta | 16f0b49 | 2010-03-19 14:37:11 -0700 | [diff] [blame] | 511 | if (0 != ensure_root_path_unmounted(mounts[chosen_item][2])) |
| 512 | ui_print("Error unmounting %s!\n", mounts[chosen_item][2]); |
Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 513 | } |
| 514 | else |
| 515 | { |
Koushik K. Dutta | 16f0b49 | 2010-03-19 14:37:11 -0700 | [diff] [blame] | 516 | if (0 != ensure_root_path_mounted(mounts[chosen_item][2])) |
| 517 | ui_print("Error mounting %s!\n", mounts[chosen_item][2]); |
Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 518 | } |
| 519 | } |
Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame] | 520 | else if (chosen_item < MOUNTABLE_COUNT + MTD_COUNT) |
| 521 | { |
| 522 | chosen_item = chosen_item - MOUNTABLE_COUNT; |
Koushik K. Dutta | 16f0b49 | 2010-03-19 14:37:11 -0700 | [diff] [blame] | 523 | if (!confirm_format()) |
| 524 | continue; |
| 525 | ui_print("Formatting %s...\n", mtds[chosen_item][1]); |
Koushik K. Dutta | 3f99539 | 2010-03-30 23:29:43 -0700 | [diff] [blame] | 526 | if (0 != format_root_device(mtds[chosen_item][1])) |
| 527 | ui_print("Error formatting %s!\n", mtds[chosen_item][1]); |
| 528 | else |
| 529 | ui_print("Done.\n"); |
| 530 | } |
| 531 | else if (chosen_item < MOUNTABLE_COUNT + MTD_COUNT + MMC_COUNT) |
| 532 | { |
| 533 | chosen_item = chosen_item - MOUNTABLE_COUNT - MTD_COUNT; |
| 534 | if (!confirm_format()) |
| 535 | continue; |
| 536 | ui_print("Formatting %s...\n", mmcs[chosen_item][1]); |
Koushik Dutta | 2f73e58 | 2010-04-18 16:00:21 -0700 | [diff] [blame] | 537 | if (0 != format_non_mtd_device(mmcs[chosen_item][1])) |
Koushik K. Dutta | 3f99539 | 2010-03-30 23:29:43 -0700 | [diff] [blame] | 538 | ui_print("Error formatting %s!\n", mmcs[chosen_item][1]); |
| 539 | else |
| 540 | ui_print("Done.\n"); |
Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame] | 541 | } |
Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 542 | } |
| 543 | } |
| 544 | |
Koushik K. Dutta | 72a1db6 | 2010-03-07 14:10:26 -0800 | [diff] [blame] | 545 | #define EXTENDEDCOMMAND_SCRIPT "/cache/recovery/extendedcommand" |
| 546 | |
| 547 | int extendedcommand_file_exists() |
Koushik K. Dutta | f68aaaf | 2010-03-07 13:39:21 -0800 | [diff] [blame] | 548 | { |
Koushik K. Dutta | f68aaaf | 2010-03-07 13:39:21 -0800 | [diff] [blame] | 549 | struct stat file_info; |
Koushik K. Dutta | 72a1db6 | 2010-03-07 14:10:26 -0800 | [diff] [blame] | 550 | return 0 == stat(EXTENDEDCOMMAND_SCRIPT, &file_info); |
| 551 | } |
| 552 | |
Koushik K. Dutta | 3836f72 | 2010-03-11 22:17:43 -0800 | [diff] [blame] | 553 | int run_script_from_buffer(char* script_data, int script_len, char* filename) |
Koushik K. Dutta | 72a1db6 | 2010-03-07 14:10:26 -0800 | [diff] [blame] | 554 | { |
Koushik K. Dutta | f68aaaf | 2010-03-07 13:39:21 -0800 | [diff] [blame] | 555 | /* Parse the script. Note that the script and parse tree are never freed. |
| 556 | */ |
| 557 | const AmCommandList *commands = parseAmendScript(script_data, script_len); |
| 558 | if (commands == NULL) { |
| 559 | printf("Syntax error in update script\n"); |
| 560 | return 1; |
| 561 | } else { |
Koushik K. Dutta | 72a1db6 | 2010-03-07 14:10:26 -0800 | [diff] [blame] | 562 | printf("Parsed %.*s\n", script_len, filename); |
Koushik K. Dutta | f68aaaf | 2010-03-07 13:39:21 -0800 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | /* Execute the script. |
| 566 | */ |
| 567 | int ret = execCommandList((ExecContext *)1, commands); |
| 568 | if (ret != 0) { |
| 569 | int num = ret; |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 570 | char *line = NULL, *next = script_data; |
Koushik K. Dutta | f68aaaf | 2010-03-07 13:39:21 -0800 | [diff] [blame] | 571 | while (next != NULL && ret-- > 0) { |
| 572 | line = next; |
| 573 | next = memchr(line, '\n', script_data + script_len - line); |
| 574 | if (next != NULL) *next++ = '\0'; |
| 575 | } |
| 576 | printf("Failure at line %d:\n%s\n", num, next ? line : "(not found)"); |
| 577 | return 1; |
| 578 | } |
| 579 | |
| 580 | return 0; |
| 581 | } |
Koushik K. Dutta | 72a1db6 | 2010-03-07 14:10:26 -0800 | [diff] [blame] | 582 | |
Koushik K. Dutta | 6771aca | 2010-04-03 23:28:39 -0700 | [diff] [blame] | 583 | int run_script(char* filename) |
Koushik K. Dutta | 3836f72 | 2010-03-11 22:17:43 -0800 | [diff] [blame] | 584 | { |
| 585 | struct stat file_info; |
| 586 | if (0 != stat(filename, &file_info)) { |
| 587 | printf("Error executing stat on file: %s\n", filename); |
| 588 | return 1; |
| 589 | } |
| 590 | |
| 591 | int script_len = file_info.st_size; |
Koushik K. Dutta | 707fa6d | 2010-03-23 11:44:33 -0700 | [diff] [blame] | 592 | char* script_data = (char*)malloc(script_len + 1); |
Koushik K. Dutta | 3836f72 | 2010-03-11 22:17:43 -0800 | [diff] [blame] | 593 | FILE *file = fopen(filename, "rb"); |
| 594 | fread(script_data, script_len, 1, file); |
Koushik K. Dutta | 707fa6d | 2010-03-23 11:44:33 -0700 | [diff] [blame] | 595 | // supposedly not necessary, but let's be safe. |
| 596 | script_data[script_len] = '\0'; |
Koushik K. Dutta | 3836f72 | 2010-03-11 22:17:43 -0800 | [diff] [blame] | 597 | fclose(file); |
Koushik K. Dutta | 3836f72 | 2010-03-11 22:17:43 -0800 | [diff] [blame] | 598 | |
Koushik K. Dutta | f3534d0 | 2010-04-18 18:06:24 -0700 | [diff] [blame] | 599 | int ret = run_script_from_buffer(script_data, script_len, filename); |
Koushik K. Dutta | 1d53c4e | 2010-04-02 16:46:21 -0700 | [diff] [blame] | 600 | free(script_data); |
| 601 | return ret; |
Koushik K. Dutta | 3836f72 | 2010-03-11 22:17:43 -0800 | [diff] [blame] | 602 | } |
| 603 | |
Koushik K. Dutta | 72a1db6 | 2010-03-07 14:10:26 -0800 | [diff] [blame] | 604 | int run_and_remove_extendedcommand() |
| 605 | { |
Koushik K. Dutta | 6771aca | 2010-04-03 23:28:39 -0700 | [diff] [blame] | 606 | char tmp[PATH_MAX]; |
| 607 | sprintf(tmp, "cp %s /tmp/%s", EXTENDEDCOMMAND_SCRIPT, basename(EXTENDEDCOMMAND_SCRIPT)); |
| 608 | __system(tmp); |
| 609 | remove(EXTENDEDCOMMAND_SCRIPT); |
Koushik K. Dutta | 3a25cf5 | 2010-03-08 19:22:41 -0800 | [diff] [blame] | 610 | int i = 0; |
| 611 | for (i = 20; i > 0; i--) { |
| 612 | ui_print("Waiting for SD Card to mount (%ds)\n", i); |
| 613 | if (ensure_root_path_mounted("SDCARD:") == 0) { |
| 614 | ui_print("SD Card mounted...\n"); |
| 615 | break; |
| 616 | } |
| 617 | sleep(1); |
| 618 | } |
| 619 | if (i == 0) { |
| 620 | ui_print("Timed out waiting for SD card... continuing anyways."); |
| 621 | } |
| 622 | |
Koushik K. Dutta | 6771aca | 2010-04-03 23:28:39 -0700 | [diff] [blame] | 623 | sprintf(tmp, "/tmp/%s", basename(EXTENDEDCOMMAND_SCRIPT)); |
| 624 | return run_script(tmp); |
Koushik K. Dutta | 72a1db6 | 2010-03-07 14:10:26 -0800 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | int amend_main(int argc, char** argv) |
| 628 | { |
| 629 | if (argc != 2) |
| 630 | { |
| 631 | printf("Usage: amend <script>\n"); |
| 632 | return 0; |
| 633 | } |
| 634 | |
| 635 | RecoveryCommandContext ctx = { NULL }; |
| 636 | if (register_update_commands(&ctx)) { |
| 637 | LOGE("Can't install update commands\n"); |
| 638 | } |
Koushik K. Dutta | 6771aca | 2010-04-03 23:28:39 -0700 | [diff] [blame] | 639 | return run_script(argv[1]); |
Koushik K. Dutta | 5306db5 | 2010-03-08 14:09:35 -0800 | [diff] [blame] | 640 | } |
Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame] | 641 | |
Koushik K. Dutta | fe84a7f | 2010-03-25 18:19:23 -0700 | [diff] [blame] | 642 | void show_nandroid_advanced_restore_menu() |
| 643 | { |
| 644 | if (ensure_root_path_mounted("SDCARD:") != 0) { |
| 645 | LOGE ("Can't mount /sdcard\n"); |
| 646 | return; |
| 647 | } |
| 648 | |
| 649 | static char* advancedheaders[] = { "Choose an image to restore", |
| 650 | "", |
| 651 | "Choose an image to restore", |
| 652 | "first. The next menu will", |
| 653 | "you more options.", |
| 654 | "", |
| 655 | NULL |
| 656 | }; |
| 657 | |
| 658 | char* file = choose_file_menu("/sdcard/clockworkmod/backup/", NULL, advancedheaders); |
| 659 | if (file == NULL) |
| 660 | return; |
| 661 | |
| 662 | static char* headers[] = { "Nandroid Advanced Restore", |
| 663 | "", |
| 664 | NULL |
| 665 | }; |
| 666 | |
| 667 | static char* list[] = { "Restore boot", |
| 668 | "Restore system", |
| 669 | "Restore data", |
| 670 | "Restore cache", |
Koushik K. Dutta | 68b0190 | 2010-04-01 12:20:39 -0700 | [diff] [blame] | 671 | "Restore sd-ext", |
Koushik K. Dutta | fe84a7f | 2010-03-25 18:19:23 -0700 | [diff] [blame] | 672 | NULL |
| 673 | }; |
| 674 | |
| 675 | int chosen_item = get_menu_selection(headers, list, 0); |
| 676 | switch (chosen_item) |
| 677 | { |
| 678 | case 0: |
Koushik K. Dutta | 68b0190 | 2010-04-01 12:20:39 -0700 | [diff] [blame] | 679 | nandroid_restore(file, 1, 0, 0, 0, 0); |
Koushik K. Dutta | fe84a7f | 2010-03-25 18:19:23 -0700 | [diff] [blame] | 680 | break; |
| 681 | case 1: |
Koushik K. Dutta | 68b0190 | 2010-04-01 12:20:39 -0700 | [diff] [blame] | 682 | nandroid_restore(file, 0, 1, 0, 0, 0); |
Koushik K. Dutta | fe84a7f | 2010-03-25 18:19:23 -0700 | [diff] [blame] | 683 | break; |
| 684 | case 2: |
Koushik K. Dutta | 68b0190 | 2010-04-01 12:20:39 -0700 | [diff] [blame] | 685 | nandroid_restore(file, 0, 0, 1, 0, 0); |
Koushik K. Dutta | fe84a7f | 2010-03-25 18:19:23 -0700 | [diff] [blame] | 686 | break; |
| 687 | case 3: |
Koushik K. Dutta | 68b0190 | 2010-04-01 12:20:39 -0700 | [diff] [blame] | 688 | nandroid_restore(file, 0, 0, 0, 1, 0); |
Koushik K. Dutta | fe84a7f | 2010-03-25 18:19:23 -0700 | [diff] [blame] | 689 | break; |
Koushik Dutta | 2f73e58 | 2010-04-18 16:00:21 -0700 | [diff] [blame] | 690 | case 4: |
| 691 | nandroid_restore(file, 0, 0, 0, 0, 1); |
| 692 | break; |
Koushik K. Dutta | fe84a7f | 2010-03-25 18:19:23 -0700 | [diff] [blame] | 693 | } |
| 694 | } |
| 695 | |
Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame] | 696 | void show_nandroid_menu() |
| 697 | { |
| 698 | static char* headers[] = { "Nandroid", |
| 699 | "", |
| 700 | NULL |
| 701 | }; |
| 702 | |
| 703 | static char* list[] = { "Backup", |
| 704 | "Restore", |
Koushik K. Dutta | fe84a7f | 2010-03-25 18:19:23 -0700 | [diff] [blame] | 705 | "Advanced Restore", |
Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame] | 706 | NULL |
| 707 | }; |
| 708 | |
| 709 | int chosen_item = get_menu_selection(headers, list, 0); |
| 710 | switch (chosen_item) |
| 711 | { |
| 712 | case 0: |
| 713 | { |
Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame] | 714 | char backup_path[PATH_MAX]; |
Koushik K. Dutta | 6a26e7c | 2010-03-27 15:26:11 -0700 | [diff] [blame] | 715 | time_t t = time(NULL); |
| 716 | struct tm *tmp = localtime(&t); |
| 717 | if (tmp == NULL) |
| 718 | { |
| 719 | struct timeval tp; |
| 720 | gettimeofday(&tp, NULL); |
| 721 | sprintf(backup_path, "/sdcard/clockworkmod/backup/%d", tp.tv_sec); |
| 722 | } |
| 723 | else |
| 724 | { |
| 725 | strftime(backup_path, sizeof(backup_path), "/sdcard/clockworkmod/backup/%F.%H.%M.%S", tmp); |
| 726 | } |
Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame] | 727 | nandroid_backup(backup_path); |
| 728 | } |
| 729 | break; |
| 730 | case 1: |
| 731 | show_nandroid_restore_menu(); |
| 732 | break; |
Koushik K. Dutta | fe84a7f | 2010-03-25 18:19:23 -0700 | [diff] [blame] | 733 | case 2: |
| 734 | show_nandroid_advanced_restore_menu(); |
| 735 | break; |
Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame] | 736 | } |
Koushik K. Dutta | a496b51 | 2010-03-19 14:51:45 -0700 | [diff] [blame] | 737 | } |
| 738 | |
Koushik Dutta | fd1579b | 2010-05-01 12:46:55 -0700 | [diff] [blame^] | 739 | void wipe_battery_stats() |
| 740 | { |
| 741 | ensure_root_path_mounted("DATA:"); |
| 742 | remove("/data/system/batterystats.bin"); |
| 743 | ensure_root_path_unmounted("DATA:"); |
| 744 | } |
| 745 | |
Koushik K. Dutta | a496b51 | 2010-03-19 14:51:45 -0700 | [diff] [blame] | 746 | void show_advanced_menu() |
| 747 | { |
| 748 | static char* headers[] = { "Advanced and Debugging Menu", |
| 749 | "", |
| 750 | NULL |
| 751 | }; |
| 752 | |
Koushik K. Dutta | 7fe4d7b | 2010-04-06 23:04:52 -0700 | [diff] [blame] | 753 | static char* list[] = { "Reboot Recovery", |
Koushik Dutta | fd1579b | 2010-05-01 12:46:55 -0700 | [diff] [blame^] | 754 | "Wipe Battery Stats", |
Koushik K. Dutta | 7fe4d7b | 2010-04-06 23:04:52 -0700 | [diff] [blame] | 755 | "Key Test", |
Koushik K. Dutta | a496b51 | 2010-03-19 14:51:45 -0700 | [diff] [blame] | 756 | NULL |
| 757 | }; |
| 758 | |
| 759 | for (;;) |
| 760 | { |
| 761 | int chosen_item = get_menu_selection(headers, list, 0); |
| 762 | if (chosen_item == GO_BACK) |
| 763 | break; |
| 764 | switch (chosen_item) |
| 765 | { |
| 766 | case 0: |
Koushik K. Dutta | 7fe4d7b | 2010-04-06 23:04:52 -0700 | [diff] [blame] | 767 | __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, "recovery"); |
| 768 | break; |
| 769 | case 1: |
Koushik Dutta | fd1579b | 2010-05-01 12:46:55 -0700 | [diff] [blame^] | 770 | wipe_battery_stats(); |
| 771 | break; |
| 772 | case 2: |
Koushik K. Dutta | a496b51 | 2010-03-19 14:51:45 -0700 | [diff] [blame] | 773 | { |
| 774 | ui_print("Outputting key codes.\n"); |
| 775 | ui_print("Go back to end debugging.\n"); |
| 776 | int key; |
| 777 | int action; |
| 778 | do |
| 779 | { |
| 780 | key = ui_wait_key(); |
| 781 | action = device_handle_key(key, 1); |
| 782 | ui_print("Key: %d\n", key); |
| 783 | } |
| 784 | while (action != GO_BACK); |
| 785 | } |
| 786 | } |
| 787 | } |
Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame] | 788 | } |