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