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 | 99fb6fe | 2010-03-03 00:42:58 -0800 | [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 | 49f5689 | 2010-02-25 14:51:05 -0800 | [diff] [blame] | 119 | char** gather_files(const char* directory, const char* fileExtensionOrDirectory, int* numFiles) |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 120 | { |
Koushik K. Dutta | e923487 | 2010-02-12 00:43:24 -0800 | [diff] [blame] | 121 | char path[PATH_MAX] = ""; |
| 122 | DIR *dir; |
| 123 | struct dirent *de; |
| 124 | int total = 0; |
| 125 | int i; |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 126 | char** files = NULL; |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 127 | int pass; |
| 128 | *numFiles = 0; |
| 129 | int dirLen = strlen(directory); |
Koushik K. Dutta | e923487 | 2010-02-12 00:43:24 -0800 | [diff] [blame] | 130 | |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 131 | dir = opendir(directory); |
| 132 | if (dir == NULL) { |
| 133 | ui_print("Couldn't open directory.\n"); |
| 134 | return NULL; |
| 135 | } |
| 136 | |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 137 | int extension_length = 0; |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 138 | if (fileExtensionOrDirectory != NULL) |
| 139 | extension_length = strlen(fileExtensionOrDirectory); |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 140 | |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 141 | int isCounting = 1; |
| 142 | i = 0; |
| 143 | for (pass = 0; pass < 2; pass++) { |
| 144 | while ((de=readdir(dir)) != NULL) { |
| 145 | // skip hidden files |
| 146 | if (de->d_name[0] == '.') |
| 147 | continue; |
| 148 | |
| 149 | // NULL means that we are gathering directories, so skip this |
| 150 | if (fileExtensionOrDirectory != NULL) |
| 151 | { |
| 152 | // make sure that we can have the desired extension (prevent seg fault) |
| 153 | if (strlen(de->d_name) < extension_length) |
| 154 | continue; |
| 155 | // compare the extension |
| 156 | if (strcmp(de->d_name + strlen(de->d_name) - extension_length, fileExtensionOrDirectory) != 0) |
| 157 | continue; |
| 158 | } |
| 159 | else |
| 160 | { |
| 161 | struct stat info; |
| 162 | char* fullFileName = (char*)malloc(strlen(de->d_name) + dirLen + 1); |
| 163 | strcpy(fullFileName, directory); |
| 164 | strcat(fullFileName, de->d_name); |
| 165 | stat(fullFileName, &info); |
| 166 | free(fullFileName); |
| 167 | // make sure it is a directory |
| 168 | if (!(S_ISDIR(info.st_mode))) |
| 169 | continue; |
| 170 | } |
| 171 | |
| 172 | if (pass == 0) |
| 173 | { |
| 174 | total++; |
| 175 | continue; |
| 176 | } |
| 177 | |
Koushik K. Dutta | 49f5689 | 2010-02-25 14:51:05 -0800 | [diff] [blame] | 178 | files[i] = (char*) malloc(dirLen + strlen(de->d_name) + 2); |
| 179 | strcpy(files[i], directory); |
| 180 | strcat(files[i], de->d_name); |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 181 | if (fileExtensionOrDirectory == NULL) |
| 182 | strcat(files[i], "/"); |
| 183 | i++; |
| 184 | } |
| 185 | if (pass == 1) |
| 186 | break; |
| 187 | if (total == 0) |
| 188 | break; |
| 189 | rewinddir(dir); |
| 190 | *numFiles = total; |
| 191 | files = (char**) malloc((total+1)*sizeof(char*)); |
| 192 | files[total]=NULL; |
| 193 | } |
Koushik K. Dutta | 49f5689 | 2010-02-25 14:51:05 -0800 | [diff] [blame] | 194 | |
| 195 | if(closedir(dir) < 0) { |
| 196 | LOGE("Failed to close directory."); |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | if (total==0) { |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 200 | return NULL; |
| 201 | } |
| 202 | |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 203 | return files; |
Koushik K. Dutta | 49f5689 | 2010-02-25 14:51:05 -0800 | [diff] [blame] | 204 | } |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 205 | |
Koushik K. Dutta | 49f5689 | 2010-02-25 14:51:05 -0800 | [diff] [blame] | 206 | void free_string_array(char** array) |
| 207 | { |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 208 | char* cursor = array[0]; |
| 209 | int i = 0; |
| 210 | while (cursor != NULL) |
| 211 | { |
| 212 | free(cursor); |
| 213 | cursor = array[++i]; |
| 214 | } |
| 215 | free(array); |
Koushik K. Dutta | 49f5689 | 2010-02-25 14:51:05 -0800 | [diff] [blame] | 216 | } |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 217 | |
Koushik K. Dutta | 49f5689 | 2010-02-25 14:51:05 -0800 | [diff] [blame] | 218 | // pass in NULL for fileExtensionOrDirectory and you will get a directory chooser |
| 219 | char* choose_file_menu(const char* directory, const char* fileExtensionOrDirectory, const char* headers[]) |
| 220 | { |
| 221 | char path[PATH_MAX] = ""; |
| 222 | DIR *dir; |
| 223 | struct dirent *de; |
| 224 | int numFiles = 0; |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 225 | int numDirs = 0; |
Koushik K. Dutta | 49f5689 | 2010-02-25 14:51:05 -0800 | [diff] [blame] | 226 | int i; |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 227 | |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 228 | int dir_len = strlen(directory); |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 229 | |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 230 | char** files = gather_files(directory, fileExtensionOrDirectory, &numFiles); |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 231 | char** dirs = NULL; |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 232 | if (fileExtensionOrDirectory != NULL) |
| 233 | dirs = gather_files(directory, NULL, &numDirs); |
| 234 | int total = numDirs + numFiles; |
| 235 | if (total == 0) |
| 236 | { |
| 237 | ui_print("No files found.\n"); |
| 238 | return NULL; |
| 239 | } |
| 240 | char** list = (char**) malloc((total + 1) * sizeof(char*)); |
| 241 | list[total] = NULL; |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 242 | |
Koushik K. Dutta | 49f5689 | 2010-02-25 14:51:05 -0800 | [diff] [blame] | 243 | |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 244 | for (i = 0 ; i < numDirs; i++) |
| 245 | { |
| 246 | list[i] = strdup(dirs[i] + dir_len); |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 247 | } |
| 248 | |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 249 | for (i = 0 ; i < numFiles; i++) |
| 250 | { |
| 251 | list[numDirs + i] = strdup(files[i] + dir_len); |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 252 | } |
| 253 | |
Koushik K. Dutta | 981b0cd | 2010-02-22 08:53:34 -0800 | [diff] [blame] | 254 | for (;;) |
| 255 | { |
| 256 | int chosen_item = get_menu_selection(headers, list, 0); |
| 257 | if (chosen_item == GO_BACK) |
| 258 | break; |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 259 | if (chosen_item < numDirs) |
| 260 | { |
| 261 | char* subret = choose_file_menu(dirs[chosen_item], fileExtensionOrDirectory, headers); |
| 262 | if (subret != NULL) |
| 263 | return subret; |
| 264 | continue; |
| 265 | } |
Koushik K. Dutta | 981b0cd | 2010-02-22 08:53:34 -0800 | [diff] [blame] | 266 | static char ret[PATH_MAX]; |
Koushik K. Dutta | 49f5689 | 2010-02-25 14:51:05 -0800 | [diff] [blame] | 267 | strcpy(ret, files[chosen_item - numDirs]); |
Koushik K. Dutta | 981b0cd | 2010-02-22 08:53:34 -0800 | [diff] [blame] | 268 | return ret; |
| 269 | } |
| 270 | return NULL; |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | void show_choose_zip_menu() |
| 274 | { |
Koushik K. Dutta | e923487 | 2010-02-12 00:43:24 -0800 | [diff] [blame] | 275 | if (ensure_root_path_mounted("SDCARD:") != 0) { |
| 276 | LOGE ("Can't mount /sdcard\n"); |
| 277 | return; |
| 278 | } |
| 279 | |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 280 | static char* headers[] = { "Choose a zip to apply", |
| 281 | "", |
| 282 | NULL |
| 283 | }; |
| 284 | |
| 285 | char* file = choose_file_menu("/sdcard/", ".zip", headers); |
| 286 | if (file == NULL) |
| 287 | return; |
| 288 | char sdcard_package_file[1024]; |
| 289 | strcpy(sdcard_package_file, "SDCARD:"); |
| 290 | strcat(sdcard_package_file, file + strlen("/sdcard/")); |
| 291 | install_zip(sdcard_package_file); |
| 292 | } |
| 293 | |
Koushik K. Dutta | 33370db | 2010-02-25 11:39:07 -0800 | [diff] [blame] | 294 | // This was pulled from bionic: The default system command always looks |
| 295 | // for shell in /system/bin/sh. This is bad. |
| 296 | #define _PATH_BSHELL "/sbin/sh" |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 297 | |
Koushik K. Dutta | 33370db | 2010-02-25 11:39:07 -0800 | [diff] [blame] | 298 | extern char **environ; |
| 299 | int |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 300 | __system(const char *command) |
Koushik K. Dutta | 33370db | 2010-02-25 11:39:07 -0800 | [diff] [blame] | 301 | { |
| 302 | pid_t pid; |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 303 | sig_t intsave, quitsave; |
| 304 | sigset_t mask, omask; |
| 305 | int pstat; |
| 306 | char *argp[] = {"sh", "-c", NULL, NULL}; |
Koushik K. Dutta | 33370db | 2010-02-25 11:39:07 -0800 | [diff] [blame] | 307 | |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 308 | if (!command) /* just checking... */ |
| 309 | return(1); |
Koushik K. Dutta | 33370db | 2010-02-25 11:39:07 -0800 | [diff] [blame] | 310 | |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 311 | argp[2] = (char *)command; |
Koushik K. Dutta | 33370db | 2010-02-25 11:39:07 -0800 | [diff] [blame] | 312 | |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 313 | sigemptyset(&mask); |
| 314 | sigaddset(&mask, SIGCHLD); |
| 315 | sigprocmask(SIG_BLOCK, &mask, &omask); |
| 316 | switch (pid = vfork()) { |
| 317 | case -1: /* error */ |
| 318 | sigprocmask(SIG_SETMASK, &omask, NULL); |
| 319 | return(-1); |
| 320 | case 0: /* child */ |
| 321 | sigprocmask(SIG_SETMASK, &omask, NULL); |
| 322 | execve(_PATH_BSHELL, argp, environ); |
Koushik K. Dutta | 33370db | 2010-02-25 11:39:07 -0800 | [diff] [blame] | 323 | _exit(127); |
| 324 | } |
| 325 | |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 326 | intsave = (sig_t) bsd_signal(SIGINT, SIG_IGN); |
| 327 | quitsave = (sig_t) bsd_signal(SIGQUIT, SIG_IGN); |
| 328 | pid = waitpid(pid, (int *)&pstat, 0); |
| 329 | sigprocmask(SIG_SETMASK, &omask, NULL); |
| 330 | (void)bsd_signal(SIGINT, intsave); |
| 331 | (void)bsd_signal(SIGQUIT, quitsave); |
| 332 | return (pid == -1 ? -1 : pstat); |
Koushik K. Dutta | 33370db | 2010-02-25 11:39:07 -0800 | [diff] [blame] | 333 | } |
| 334 | |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 335 | void show_nandroid_restore_menu() |
| 336 | { |
Koushik K. Dutta | 54305a8 | 2010-03-12 17:43:26 -0800 | [diff] [blame] | 337 | if (ensure_root_path_mounted("SDCARD:") != 0) { |
| 338 | LOGE ("Can't mount /sdcard\n"); |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 339 | return; |
Koushik K. Dutta | 54305a8 | 2010-03-12 17:43:26 -0800 | [diff] [blame] | 340 | } |
| 341 | |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 342 | static char* headers[] = { "Choose an image to restore", |
| 343 | "", |
| 344 | NULL |
| 345 | }; |
Koushik K. Dutta | e923487 | 2010-02-12 00:43:24 -0800 | [diff] [blame] | 346 | |
Koushik K. Dutta | 49f5689 | 2010-02-25 14:51:05 -0800 | [diff] [blame] | 347 | char* file = choose_file_menu("/sdcard/clockworkmod/backup/", NULL, headers); |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 348 | if (file == NULL) |
| 349 | return; |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 350 | nandroid_restore(file); |
Koushik K. Dutta | 0317378 | 2010-02-26 14:14:23 -0800 | [diff] [blame] | 351 | } |
| 352 | |
Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 353 | void show_mount_usb_storage_menu() |
Koushik K. Dutta | 0317378 | 2010-02-26 14:14:23 -0800 | [diff] [blame] | 354 | { |
Koushik K. Dutta | f721594 | 2010-03-16 13:34:51 -0700 | [diff] [blame] | 355 | __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] | 356 | static char* headers[] = { "USB Mass Storage device", |
| 357 | "Leaving this menu unmount", |
| 358 | "your SD card from your PC.", |
| 359 | "", |
| 360 | NULL |
| 361 | }; |
| 362 | |
| 363 | static char* list[] = { "Unmount", NULL }; |
| 364 | |
| 365 | for (;;) |
| 366 | { |
| 367 | int chosen_item = get_menu_selection(headers, list, 0); |
| 368 | if (chosen_item == GO_BACK || chosen_item == 0) |
| 369 | break; |
| 370 | } |
| 371 | |
Koushik K. Dutta | f721594 | 2010-03-16 13:34:51 -0700 | [diff] [blame] | 372 | __system("echo '' > /sys/devices/platform/usb_mass_storage/lun0/file"); |
| 373 | __system("echo 0 > /sys/devices/platform/usb_mass_storage/lun0/enable"); |
Koushik K. Dutta | e81cb75 | 2010-02-26 17:44:33 -0800 | [diff] [blame] | 374 | } |
Koushik K. Dutta | f68aaaf | 2010-03-07 13:39:21 -0800 | [diff] [blame] | 375 | |
Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame^] | 376 | #define MOUNTABLE_COUNT 4 |
| 377 | #define MTD_COUNT 3 |
Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 378 | |
Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame^] | 379 | void show_partition_menu() |
Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 380 | { |
| 381 | static char* headers[] = { "Mount and unmount partitions", |
| 382 | "", |
| 383 | NULL |
| 384 | }; |
| 385 | |
| 386 | typedef char* string; |
Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame^] | 387 | string mounts[MOUNTABLE_COUNT][4] = { |
| 388 | { "mount /system", "unmount /system", "format SYSTEM:", "SYSTEM:" }, |
| 389 | { "mount /data", "unmount /data", "format DATA:", "DATA:" }, |
| 390 | { "mount /cache", "unmount /cache", "format CACHE:", "CACHE:" }, |
| 391 | { "mount /sdcard", "unmount /sdcard", NULL, "SDCARD:" } |
Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 392 | }; |
| 393 | |
| 394 | for (;;) |
| 395 | { |
Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame^] | 396 | int ismounted[MOUNTABLE_COUNT]; |
Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 397 | int i; |
Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame^] | 398 | static string options[MOUNTABLE_COUNT + MTD_COUNT + 1 + 1]; // mountables, format mtds, usb storage, null |
| 399 | for (i = 0; i < MOUNTABLE_COUNT; i++) |
Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 400 | { |
Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame^] | 401 | ismounted[i] = is_root_path_mounted(mounts[i][3]); |
Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 402 | options[i] = ismounted[i] ? mounts[i][1] : mounts[i][0]; |
| 403 | } |
Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame^] | 404 | |
| 405 | for (i = 0; i < MTD_COUNT; i++) |
| 406 | { |
| 407 | options[MOUNTABLE_COUNT + i] = mounts[i][2]; |
| 408 | } |
Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 409 | |
Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame^] | 410 | options[MOUNTABLE_COUNT + MTD_COUNT] = "mount USB storage"; |
| 411 | options[MOUNTABLE_COUNT + MTD_COUNT + 1] = NULL; |
Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 412 | |
| 413 | int chosen_item = get_menu_selection(headers, options, 0); |
| 414 | if (chosen_item == GO_BACK) |
| 415 | break; |
Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame^] | 416 | if (chosen_item == MOUNTABLE_COUNT + MTD_COUNT) |
Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 417 | { |
| 418 | show_mount_usb_storage_menu(); |
| 419 | } |
Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame^] | 420 | else if (chosen_item < MOUNTABLE_COUNT) |
Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 421 | { |
| 422 | if (ismounted[chosen_item]) |
| 423 | { |
Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame^] | 424 | if (0 != ensure_root_path_unmounted(mounts[chosen_item][3])) |
| 425 | ui_print("Error unmounting %s!\n", mounts[chosen_item][3]); |
Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 426 | } |
| 427 | else |
| 428 | { |
Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame^] | 429 | if (0 != ensure_root_path_mounted(mounts[chosen_item][3])) |
| 430 | ui_print("Error mounting %s!\n", mounts[chosen_item][3]); |
Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 431 | } |
| 432 | } |
Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame^] | 433 | else if (chosen_item < MOUNTABLE_COUNT + MTD_COUNT) |
| 434 | { |
| 435 | chosen_item = chosen_item - MOUNTABLE_COUNT; |
| 436 | } |
Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | |
Koushik K. Dutta | 72a1db6 | 2010-03-07 14:10:26 -0800 | [diff] [blame] | 440 | #define EXTENDEDCOMMAND_SCRIPT "/cache/recovery/extendedcommand" |
| 441 | |
| 442 | int extendedcommand_file_exists() |
Koushik K. Dutta | f68aaaf | 2010-03-07 13:39:21 -0800 | [diff] [blame] | 443 | { |
Koushik K. Dutta | f68aaaf | 2010-03-07 13:39:21 -0800 | [diff] [blame] | 444 | struct stat file_info; |
Koushik K. Dutta | 72a1db6 | 2010-03-07 14:10:26 -0800 | [diff] [blame] | 445 | return 0 == stat(EXTENDEDCOMMAND_SCRIPT, &file_info); |
| 446 | } |
| 447 | |
Koushik K. Dutta | 3836f72 | 2010-03-11 22:17:43 -0800 | [diff] [blame] | 448 | 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] | 449 | { |
Koushik K. Dutta | f68aaaf | 2010-03-07 13:39:21 -0800 | [diff] [blame] | 450 | /* Parse the script. Note that the script and parse tree are never freed. |
| 451 | */ |
| 452 | const AmCommandList *commands = parseAmendScript(script_data, script_len); |
| 453 | if (commands == NULL) { |
| 454 | printf("Syntax error in update script\n"); |
| 455 | return 1; |
| 456 | } else { |
Koushik K. Dutta | 72a1db6 | 2010-03-07 14:10:26 -0800 | [diff] [blame] | 457 | printf("Parsed %.*s\n", script_len, filename); |
Koushik K. Dutta | f68aaaf | 2010-03-07 13:39:21 -0800 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | /* Execute the script. |
| 461 | */ |
| 462 | int ret = execCommandList((ExecContext *)1, commands); |
| 463 | if (ret != 0) { |
| 464 | int num = ret; |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 465 | char *line = NULL, *next = script_data; |
Koushik K. Dutta | f68aaaf | 2010-03-07 13:39:21 -0800 | [diff] [blame] | 466 | while (next != NULL && ret-- > 0) { |
| 467 | line = next; |
| 468 | next = memchr(line, '\n', script_data + script_len - line); |
| 469 | if (next != NULL) *next++ = '\0'; |
| 470 | } |
| 471 | printf("Failure at line %d:\n%s\n", num, next ? line : "(not found)"); |
| 472 | return 1; |
| 473 | } |
| 474 | |
| 475 | return 0; |
| 476 | } |
Koushik K. Dutta | 72a1db6 | 2010-03-07 14:10:26 -0800 | [diff] [blame] | 477 | |
Koushik K. Dutta | 3836f72 | 2010-03-11 22:17:43 -0800 | [diff] [blame] | 478 | int run_script(char* filename, int delete_file) |
| 479 | { |
| 480 | struct stat file_info; |
| 481 | if (0 != stat(filename, &file_info)) { |
| 482 | printf("Error executing stat on file: %s\n", filename); |
| 483 | return 1; |
| 484 | } |
| 485 | |
| 486 | int script_len = file_info.st_size; |
| 487 | char* script_data = (char*)malloc(script_len); |
| 488 | FILE *file = fopen(filename, "rb"); |
| 489 | fread(script_data, script_len, 1, file); |
| 490 | fclose(file); |
| 491 | if (delete_file) |
| 492 | remove(filename); |
| 493 | |
| 494 | return run_script_from_buffer(script_data, script_len, filename); |
| 495 | } |
| 496 | |
Koushik K. Dutta | 72a1db6 | 2010-03-07 14:10:26 -0800 | [diff] [blame] | 497 | int run_and_remove_extendedcommand() |
| 498 | { |
Koushik K. Dutta | 3a25cf5 | 2010-03-08 19:22:41 -0800 | [diff] [blame] | 499 | int i = 0; |
| 500 | for (i = 20; i > 0; i--) { |
| 501 | ui_print("Waiting for SD Card to mount (%ds)\n", i); |
| 502 | if (ensure_root_path_mounted("SDCARD:") == 0) { |
| 503 | ui_print("SD Card mounted...\n"); |
| 504 | break; |
| 505 | } |
| 506 | sleep(1); |
| 507 | } |
| 508 | if (i == 0) { |
| 509 | ui_print("Timed out waiting for SD card... continuing anyways."); |
| 510 | } |
| 511 | |
Koushik K. Dutta | 3836f72 | 2010-03-11 22:17:43 -0800 | [diff] [blame] | 512 | return run_script(EXTENDEDCOMMAND_SCRIPT, 1); |
Koushik K. Dutta | 72a1db6 | 2010-03-07 14:10:26 -0800 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | int amend_main(int argc, char** argv) |
| 516 | { |
| 517 | if (argc != 2) |
| 518 | { |
| 519 | printf("Usage: amend <script>\n"); |
| 520 | return 0; |
| 521 | } |
| 522 | |
| 523 | RecoveryCommandContext ctx = { NULL }; |
| 524 | if (register_update_commands(&ctx)) { |
| 525 | LOGE("Can't install update commands\n"); |
| 526 | } |
Koushik K. Dutta | 3836f72 | 2010-03-11 22:17:43 -0800 | [diff] [blame] | 527 | return run_script(argv[1], 0); |
Koushik K. Dutta | 5306db5 | 2010-03-08 14:09:35 -0800 | [diff] [blame] | 528 | } |
Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame^] | 529 | |
| 530 | void show_nandroid_menu() |
| 531 | { |
| 532 | static char* headers[] = { "Nandroid", |
| 533 | "", |
| 534 | NULL |
| 535 | }; |
| 536 | |
| 537 | static char* list[] = { "Backup", |
| 538 | "Restore", |
| 539 | NULL |
| 540 | }; |
| 541 | |
| 542 | int chosen_item = get_menu_selection(headers, list, 0); |
| 543 | switch (chosen_item) |
| 544 | { |
| 545 | case 0: |
| 546 | { |
| 547 | struct timeval tp; |
| 548 | gettimeofday(&tp, NULL); |
| 549 | char backup_path[PATH_MAX]; |
| 550 | sprintf(backup_path, "/sdcard/clockworkmod/backup/%d", tp.tv_sec); |
| 551 | nandroid_backup(backup_path); |
| 552 | } |
| 553 | break; |
| 554 | case 1: |
| 555 | show_nandroid_restore_menu(); |
| 556 | break; |
| 557 | } |
| 558 | } |