| 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 | a85d7cc | 2010-03-12 17:00:58 -0800 | [diff] [blame] | 33 | #include "../../external/yaffs2/yaffs2/utils/mkyaffs2image.h" | 
| Koushik K. Dutta | 54305a8 | 2010-03-12 17:43:26 -0800 | [diff] [blame] | 34 | #include "../../external/yaffs2/yaffs2/utils/unyaffs.h" | 
| Koushik K. Dutta | a85d7cc | 2010-03-12 17:00:58 -0800 | [diff] [blame] | 35 |  | 
| Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 36 | #include "extendedcommands.h" | 
 | 37 | #include "nandroid.h" | 
| Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 38 | #include "mounts.h" | 
| Koushik Dutta | 9765a03 | 2010-12-18 21:31:02 -0800 | [diff] [blame] | 39 | #include "flashutils/flashutils.h" | 
| Koushik Dutta | bec0995 | 2010-12-19 20:37:57 -0800 | [diff] [blame] | 40 | #include "edify/expr.h" | 
| Koushik Dutta | 7adeadc | 2011-05-26 11:47:56 -0700 | [diff] [blame] | 41 | #include <libgen.h> | 
 | 42 |  | 
| Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 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 Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 46 | static const char *SDCARD_UPDATE_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); | 
| Koushik Dutta | 9765a03 | 2010-12-18 21:31:02 -0800 | [diff] [blame] | 64 |     if (device_flash_type() == MTD) { | 
 | 65 |         set_sdcard_update_bootloader_message(); | 
 | 66 |     } | 
| Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 67 |     int status = install_package(packagefilepath); | 
| Koushik K. Dutta | f3534d0 | 2010-04-18 18:06:24 -0700 | [diff] [blame] | 68 |     ui_reset_progress(); | 
| Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 69 |     if (status != INSTALL_SUCCESS) { | 
 | 70 |         ui_set_background(BACKGROUND_ICON_ERROR); | 
 | 71 |         ui_print("Installation aborted.\n"); | 
| Koushik K. Dutta | ea46fe2 | 2010-03-08 02:58:04 -0800 | [diff] [blame] | 72 |         return 1; | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [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 |  | 
| Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 79 | char* INSTALL_MENU_ITEMS[] = {  "apply /sdcard/update.zip", | 
| Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 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 |                                 "", | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 93 |                                 NULL | 
| Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 94 |     }; | 
 | 95 |     for (;;) | 
 | 96 |     { | 
| Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 97 |         int chosen_item = get_menu_selection(headers, INSTALL_MENU_ITEMS, 0, 0); | 
| Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 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: | 
| Koushik Dutta | d63eaef | 2010-07-14 21:01:21 -0700 | [diff] [blame] | 107 |             { | 
 | 108 |                 if (confirm_selection("Confirm install?", "Yes - Install /sdcard/update.zip")) | 
| Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 109 |                     install_zip(SDCARD_UPDATE_FILE); | 
| Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 110 |                 break; | 
| Koushik Dutta | d63eaef | 2010-07-14 21:01:21 -0700 | [diff] [blame] | 111 |             } | 
| Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 112 |             case ITEM_CHOOSE_ZIP: | 
 | 113 |                 show_choose_zip_menu(); | 
 | 114 |                 break; | 
 | 115 |             default: | 
 | 116 |                 return; | 
 | 117 |         } | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 118 |  | 
| Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 119 |     } | 
 | 120 | } | 
 | 121 |  | 
| Koushik K. Dutta | 1d53c4e | 2010-04-02 16:46:21 -0700 | [diff] [blame] | 122 | void free_string_array(char** array) | 
 | 123 | { | 
 | 124 |     if (array == NULL) | 
 | 125 |         return; | 
 | 126 |     char* cursor = array[0]; | 
 | 127 |     int i = 0; | 
 | 128 |     while (cursor != NULL) | 
 | 129 |     { | 
 | 130 |         free(cursor); | 
 | 131 |         cursor = array[++i]; | 
 | 132 |     } | 
 | 133 |     free(array); | 
 | 134 | } | 
 | 135 |  | 
| Koushik K. Dutta | 49f5689 | 2010-02-25 14:51:05 -0800 | [diff] [blame] | 136 | char** gather_files(const char* directory, const char* fileExtensionOrDirectory, int* numFiles) | 
| Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 137 | { | 
| Koushik K. Dutta | e923487 | 2010-02-12 00:43:24 -0800 | [diff] [blame] | 138 |     char path[PATH_MAX] = ""; | 
 | 139 |     DIR *dir; | 
 | 140 |     struct dirent *de; | 
 | 141 |     int total = 0; | 
 | 142 |     int i; | 
| Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 143 |     char** files = NULL; | 
| Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 144 |     int pass; | 
 | 145 |     *numFiles = 0; | 
 | 146 |     int dirLen = strlen(directory); | 
| Koushik K. Dutta | e923487 | 2010-02-12 00:43:24 -0800 | [diff] [blame] | 147 |  | 
| Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 148 |     dir = opendir(directory); | 
 | 149 |     if (dir == NULL) { | 
 | 150 |         ui_print("Couldn't open directory.\n"); | 
 | 151 |         return NULL; | 
 | 152 |     } | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 153 |  | 
| Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 154 |     int extension_length = 0; | 
| Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 155 |     if (fileExtensionOrDirectory != NULL) | 
 | 156 |         extension_length = strlen(fileExtensionOrDirectory); | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 157 |  | 
| Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 158 |     int isCounting = 1; | 
 | 159 |     i = 0; | 
 | 160 |     for (pass = 0; pass < 2; pass++) { | 
 | 161 |         while ((de=readdir(dir)) != NULL) { | 
 | 162 |             // skip hidden files | 
 | 163 |             if (de->d_name[0] == '.') | 
 | 164 |                 continue; | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 165 |  | 
| Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 166 |             // NULL means that we are gathering directories, so skip this | 
 | 167 |             if (fileExtensionOrDirectory != NULL) | 
 | 168 |             { | 
 | 169 |                 // make sure that we can have the desired extension (prevent seg fault) | 
 | 170 |                 if (strlen(de->d_name) < extension_length) | 
 | 171 |                     continue; | 
 | 172 |                 // compare the extension | 
 | 173 |                 if (strcmp(de->d_name + strlen(de->d_name) - extension_length, fileExtensionOrDirectory) != 0) | 
 | 174 |                     continue; | 
 | 175 |             } | 
 | 176 |             else | 
 | 177 |             { | 
 | 178 |                 struct stat info; | 
| Koushik K. Dutta | 1d53c4e | 2010-04-02 16:46:21 -0700 | [diff] [blame] | 179 |                 char fullFileName[PATH_MAX]; | 
| Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 180 |                 strcpy(fullFileName, directory); | 
 | 181 |                 strcat(fullFileName, de->d_name); | 
 | 182 |                 stat(fullFileName, &info); | 
| Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 183 |                 // make sure it is a directory | 
 | 184 |                 if (!(S_ISDIR(info.st_mode))) | 
 | 185 |                     continue; | 
 | 186 |             } | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 187 |  | 
| Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 188 |             if (pass == 0) | 
 | 189 |             { | 
 | 190 |                 total++; | 
 | 191 |                 continue; | 
 | 192 |             } | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 193 |  | 
| Koushik K. Dutta | 49f5689 | 2010-02-25 14:51:05 -0800 | [diff] [blame] | 194 |             files[i] = (char*) malloc(dirLen + strlen(de->d_name) + 2); | 
 | 195 |             strcpy(files[i], directory); | 
 | 196 |             strcat(files[i], de->d_name); | 
| Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 197 |             if (fileExtensionOrDirectory == NULL) | 
 | 198 |                 strcat(files[i], "/"); | 
 | 199 |             i++; | 
 | 200 |         } | 
 | 201 |         if (pass == 1) | 
 | 202 |             break; | 
 | 203 |         if (total == 0) | 
 | 204 |             break; | 
 | 205 |         rewinddir(dir); | 
 | 206 |         *numFiles = total; | 
 | 207 |         files = (char**) malloc((total+1)*sizeof(char*)); | 
 | 208 |         files[total]=NULL; | 
 | 209 |     } | 
| Koushik K. Dutta | 49f5689 | 2010-02-25 14:51:05 -0800 | [diff] [blame] | 210 |  | 
 | 211 |     if(closedir(dir) < 0) { | 
 | 212 |         LOGE("Failed to close directory."); | 
| Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 213 |     } | 
 | 214 |  | 
 | 215 |     if (total==0) { | 
| Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 216 |         return NULL; | 
 | 217 |     } | 
 | 218 |  | 
| agrabren | 1f76a5d | 2010-12-29 12:15:18 -0600 | [diff] [blame] | 219 |     // sort the result | 
 | 220 |     if (files != NULL) { | 
 | 221 |         for (i = 0; i < total; i++) { | 
 | 222 |             int curMax = -1; | 
 | 223 |             int j; | 
 | 224 |             for (j = 0; j < total - i; j++) { | 
 | 225 |                 if (curMax == -1 || strcmp(files[curMax], files[j]) < 0) | 
 | 226 |                     curMax = j; | 
 | 227 |             } | 
 | 228 |             char* temp = files[curMax]; | 
 | 229 |             files[curMax] = files[total - i - 1]; | 
 | 230 |             files[total - i - 1] = temp; | 
 | 231 |         } | 
 | 232 |     } | 
| Koushik Dutta | 1e8aaba | 2010-07-01 00:23:25 -0700 | [diff] [blame] | 233 |  | 
| Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 234 |     return files; | 
| Koushik K. Dutta | 49f5689 | 2010-02-25 14:51:05 -0800 | [diff] [blame] | 235 | } | 
| Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 236 |  | 
| Koushik K. Dutta | 49f5689 | 2010-02-25 14:51:05 -0800 | [diff] [blame] | 237 | // pass in NULL for fileExtensionOrDirectory and you will get a directory chooser | 
 | 238 | char* choose_file_menu(const char* directory, const char* fileExtensionOrDirectory, const char* headers[]) | 
 | 239 | { | 
 | 240 |     char path[PATH_MAX] = ""; | 
 | 241 |     DIR *dir; | 
 | 242 |     struct dirent *de; | 
 | 243 |     int numFiles = 0; | 
| Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 244 |     int numDirs = 0; | 
| Koushik K. Dutta | 49f5689 | 2010-02-25 14:51:05 -0800 | [diff] [blame] | 245 |     int i; | 
| Koushik K. Dutta | 1d53c4e | 2010-04-02 16:46:21 -0700 | [diff] [blame] | 246 |     char* return_value = NULL; | 
| Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 247 |     int dir_len = strlen(directory); | 
| Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 248 |  | 
| Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 249 |     char** files = gather_files(directory, fileExtensionOrDirectory, &numFiles); | 
| Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 250 |     char** dirs = NULL; | 
| Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 251 |     if (fileExtensionOrDirectory != NULL) | 
 | 252 |         dirs = gather_files(directory, NULL, &numDirs); | 
 | 253 |     int total = numDirs + numFiles; | 
 | 254 |     if (total == 0) | 
 | 255 |     { | 
 | 256 |         ui_print("No files found.\n"); | 
| Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 257 |     } | 
| Koushik K. Dutta | 1d53c4e | 2010-04-02 16:46:21 -0700 | [diff] [blame] | 258 |     else | 
| Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 259 |     { | 
| Koushik K. Dutta | 1d53c4e | 2010-04-02 16:46:21 -0700 | [diff] [blame] | 260 |         char** list = (char**) malloc((total + 1) * sizeof(char*)); | 
 | 261 |         list[total] = NULL; | 
| Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 262 |  | 
| Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 263 |  | 
| Koushik K. Dutta | 1d53c4e | 2010-04-02 16:46:21 -0700 | [diff] [blame] | 264 |         for (i = 0 ; i < numDirs; i++) | 
| Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 265 |         { | 
| Koushik K. Dutta | 1d53c4e | 2010-04-02 16:46:21 -0700 | [diff] [blame] | 266 |             list[i] = strdup(dirs[i] + dir_len); | 
 | 267 |         } | 
 | 268 |  | 
 | 269 |         for (i = 0 ; i < numFiles; i++) | 
 | 270 |         { | 
 | 271 |             list[numDirs + i] = strdup(files[i] + dir_len); | 
 | 272 |         } | 
 | 273 |  | 
 | 274 |         for (;;) | 
 | 275 |         { | 
| Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 276 |             int chosen_item = get_menu_selection(headers, list, 0, 0); | 
| Koushik K. Dutta | 1d53c4e | 2010-04-02 16:46:21 -0700 | [diff] [blame] | 277 |             if (chosen_item == GO_BACK) | 
 | 278 |                 break; | 
 | 279 |             static char ret[PATH_MAX]; | 
 | 280 |             if (chosen_item < numDirs) | 
 | 281 |             { | 
 | 282 |                 char* subret = choose_file_menu(dirs[chosen_item], fileExtensionOrDirectory, headers); | 
 | 283 |                 if (subret != NULL) | 
 | 284 |                 { | 
 | 285 |                     strcpy(ret, subret); | 
 | 286 |                     return_value = ret; | 
 | 287 |                     break; | 
 | 288 |                 } | 
 | 289 |                 continue; | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 290 |             } | 
| Koushik K. Dutta | 1d53c4e | 2010-04-02 16:46:21 -0700 | [diff] [blame] | 291 |             strcpy(ret, files[chosen_item - numDirs]); | 
 | 292 |             return_value = ret; | 
 | 293 |             break; | 
 | 294 |         } | 
 | 295 |         free_string_array(list); | 
| Koushik K. Dutta | 981b0cd | 2010-02-22 08:53:34 -0800 | [diff] [blame] | 296 |     } | 
| Koushik K. Dutta | 1d53c4e | 2010-04-02 16:46:21 -0700 | [diff] [blame] | 297 |  | 
 | 298 |     free_string_array(files); | 
 | 299 |     free_string_array(dirs); | 
 | 300 |     return return_value; | 
| Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 301 | } | 
 | 302 |  | 
 | 303 | void show_choose_zip_menu() | 
 | 304 | { | 
| Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 305 |     if (ensure_path_mounted("/sdcard") != 0) { | 
| Koushik K. Dutta | e923487 | 2010-02-12 00:43:24 -0800 | [diff] [blame] | 306 |         LOGE ("Can't mount /sdcard\n"); | 
 | 307 |         return; | 
 | 308 |     } | 
 | 309 |  | 
| Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 310 |     static char* headers[] = {  "Choose a zip to apply", | 
 | 311 |                                 "", | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 312 |                                 NULL | 
| Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 313 |     }; | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 314 |  | 
| Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 315 |     char* file = choose_file_menu("/sdcard/", ".zip", headers); | 
 | 316 |     if (file == NULL) | 
 | 317 |         return; | 
| Koushik Dutta | d63eaef | 2010-07-14 21:01:21 -0700 | [diff] [blame] | 318 |     static char* confirm_install  = "Confirm install?"; | 
 | 319 |     static char confirm[PATH_MAX]; | 
 | 320 |     sprintf(confirm, "Yes - Install %s", basename(file)); | 
 | 321 |     if (confirm_selection(confirm_install, confirm)) | 
| Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 322 |         install_zip(file); | 
| Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 323 | } | 
 | 324 |  | 
| Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 325 | void show_nandroid_restore_menu() | 
 | 326 | { | 
| Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 327 |     if (ensure_path_mounted("/sdcard") != 0) { | 
| Koushik K. Dutta | 54305a8 | 2010-03-12 17:43:26 -0800 | [diff] [blame] | 328 |         LOGE ("Can't mount /sdcard\n"); | 
| Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 329 |         return; | 
| Koushik K. Dutta | 54305a8 | 2010-03-12 17:43:26 -0800 | [diff] [blame] | 330 |     } | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 331 |  | 
| Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 332 |     static char* headers[] = {  "Choose an image to restore", | 
 | 333 |                                 "", | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 334 |                                 NULL | 
| Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 335 |     }; | 
| Koushik K. Dutta | e923487 | 2010-02-12 00:43:24 -0800 | [diff] [blame] | 336 |  | 
| Koushik K. Dutta | 49f5689 | 2010-02-25 14:51:05 -0800 | [diff] [blame] | 337 |     char* file = choose_file_menu("/sdcard/clockworkmod/backup/", NULL, headers); | 
| Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 338 |     if (file == NULL) | 
 | 339 |         return; | 
| Koushik Dutta | d63eaef | 2010-07-14 21:01:21 -0700 | [diff] [blame] | 340 |  | 
 | 341 |     if (confirm_selection("Confirm restore?", "Yes - Restore")) | 
| Koushik Dutta | 5b7f34a | 2010-12-29 23:36:03 -0800 | [diff] [blame] | 342 |         nandroid_restore(file, 1, 1, 1, 1, 1, 0); | 
| Koushik K. Dutta | 0317378 | 2010-02-26 14:14:23 -0800 | [diff] [blame] | 343 | } | 
 | 344 |  | 
| jt1134 | 47d86a3 | 2011-02-27 18:54:09 -0600 | [diff] [blame] | 345 | #ifndef BOARD_UMS_LUNFILE | 
 | 346 | #define BOARD_UMS_LUNFILE	"/sys/devices/platform/usb_mass_storage/lun0/file" | 
 | 347 | #endif | 
 | 348 |  | 
| Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 349 | void show_mount_usb_storage_menu() | 
| Koushik K. Dutta | 0317378 | 2010-02-26 14:14:23 -0800 | [diff] [blame] | 350 | { | 
| Koushik Dutta | f1cb0d5 | 2011-01-01 20:03:30 -0800 | [diff] [blame] | 351 |     int fd; | 
| Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 352 |     Volume *vol = volume_for_path("/sdcard"); | 
| jt1134 | 47d86a3 | 2011-02-27 18:54:09 -0600 | [diff] [blame] | 353 |     if ((fd = open(BOARD_UMS_LUNFILE, O_WRONLY)) < 0) { | 
| Koushik Dutta | f1cb0d5 | 2011-01-01 20:03:30 -0800 | [diff] [blame] | 354 |         LOGE("Unable to open ums lunfile (%s)", strerror(errno)); | 
 | 355 |         return -1; | 
 | 356 |     } | 
 | 357 |  | 
| mik_os | df3004b | 2011-04-26 19:15:22 +0300 | [diff] [blame] | 358 |     if ((write(fd, vol->device, strlen(vol->device)) < 0) && | 
 | 359 |         (!vol->device2 || (write(fd, vol->device, strlen(vol->device2)) < 0))) { | 
| Koushik Dutta | f1cb0d5 | 2011-01-01 20:03:30 -0800 | [diff] [blame] | 360 |         LOGE("Unable to write to ums lunfile (%s)", strerror(errno)); | 
 | 361 |         close(fd); | 
 | 362 |         return -1; | 
 | 363 |     } | 
| Koushik K. Dutta | 0317378 | 2010-02-26 14:14:23 -0800 | [diff] [blame] | 364 |     static char* headers[] = {  "USB Mass Storage device", | 
 | 365 |                                 "Leaving this menu unmount", | 
 | 366 |                                 "your SD card from your PC.", | 
 | 367 |                                 "", | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 368 |                                 NULL | 
| Koushik K. Dutta | 0317378 | 2010-02-26 14:14:23 -0800 | [diff] [blame] | 369 |     }; | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 370 |  | 
| Koushik K. Dutta | 0317378 | 2010-02-26 14:14:23 -0800 | [diff] [blame] | 371 |     static char* list[] = { "Unmount", NULL }; | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 372 |  | 
| Koushik K. Dutta | 0317378 | 2010-02-26 14:14:23 -0800 | [diff] [blame] | 373 |     for (;;) | 
 | 374 |     { | 
| Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 375 |         int chosen_item = get_menu_selection(headers, list, 0, 0); | 
| Koushik K. Dutta | 0317378 | 2010-02-26 14:14:23 -0800 | [diff] [blame] | 376 |         if (chosen_item == GO_BACK || chosen_item == 0) | 
 | 377 |             break; | 
 | 378 |     } | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 379 |  | 
| jt1134 | 47d86a3 | 2011-02-27 18:54:09 -0600 | [diff] [blame] | 380 |     if ((fd = open(BOARD_UMS_LUNFILE, O_WRONLY)) < 0) { | 
| Koushik Dutta | f1cb0d5 | 2011-01-01 20:03:30 -0800 | [diff] [blame] | 381 |         LOGE("Unable to open ums lunfile (%s)", strerror(errno)); | 
 | 382 |         return -1; | 
 | 383 |     } | 
 | 384 |  | 
 | 385 |     char ch = 0; | 
 | 386 |     if (write(fd, &ch, 1) < 0) { | 
 | 387 |         LOGE("Unable to write to ums lunfile (%s)", strerror(errno)); | 
 | 388 |         close(fd); | 
 | 389 |         return -1; | 
 | 390 |     } | 
| Koushik K. Dutta | e81cb75 | 2010-02-26 17:44:33 -0800 | [diff] [blame] | 391 | } | 
| Koushik K. Dutta | f68aaaf | 2010-03-07 13:39:21 -0800 | [diff] [blame] | 392 |  | 
| Koushik Dutta | d63eaef | 2010-07-14 21:01:21 -0700 | [diff] [blame] | 393 | int confirm_selection(const char* title, const char* confirm) | 
| Koushik K. Dutta | 16f0b49 | 2010-03-19 14:37:11 -0700 | [diff] [blame] | 394 | { | 
| Koushik Dutta | ceddcd5 | 2010-08-23 16:13:14 -0700 | [diff] [blame] | 395 |     struct stat info; | 
 | 396 |     if (0 == stat("/sdcard/clockworkmod/.no_confirm", &info)) | 
 | 397 |         return 1; | 
 | 398 |  | 
| Koushik Dutta | d63eaef | 2010-07-14 21:01:21 -0700 | [diff] [blame] | 399 |     char* confirm_headers[]  = {  title, "  THIS CAN NOT BE UNDONE.", "", NULL }; | 
 | 400 |     char* items[] = { "No", | 
 | 401 |                       "No", | 
 | 402 |                       "No", | 
 | 403 |                       "No", | 
 | 404 |                       "No", | 
 | 405 |                       "No", | 
 | 406 |                       "No", | 
| Koushik Dutta | ecd32fa | 2010-07-14 18:38:02 -0700 | [diff] [blame] | 407 |                       confirm, //" Yes -- wipe partition",   // [7 | 
| Koushik Dutta | d63eaef | 2010-07-14 21:01:21 -0700 | [diff] [blame] | 408 |                       "No", | 
 | 409 |                       "No", | 
 | 410 |                       "No", | 
| Koushik K. Dutta | 16f0b49 | 2010-03-19 14:37:11 -0700 | [diff] [blame] | 411 |                       NULL }; | 
 | 412 |  | 
| Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 413 |     int chosen_item = get_menu_selection(confirm_headers, items, 0, 0); | 
| Koushik K. Dutta | 16f0b49 | 2010-03-19 14:37:11 -0700 | [diff] [blame] | 414 |     return chosen_item == 7; | 
 | 415 | } | 
 | 416 |  | 
| Koushik Dutta | da32b54 | 2011-01-01 17:55:22 -0800 | [diff] [blame] | 417 | #define MKE2FS_BIN      "/sbin/mke2fs" | 
 | 418 | #define TUNE2FS_BIN     "/sbin/tune2fs" | 
 | 419 | #define E2FSCK_BIN      "/sbin/e2fsck" | 
 | 420 |  | 
| Koushik Dutta | da32b54 | 2011-01-01 17:55:22 -0800 | [diff] [blame] | 421 | int format_unknown_device(const char *device, const char* path, const char *fs_type) | 
| Koushik K. Dutta | 3f99539 | 2010-03-30 23:29:43 -0700 | [diff] [blame] | 422 | { | 
| Koushik Dutta | 2e45449 | 2011-01-01 18:06:17 -0800 | [diff] [blame] | 423 |     LOGI("Formatting unknown device.\n"); | 
| Koushik Dutta | a8708c6 | 2011-01-01 18:00:27 -0800 | [diff] [blame] | 424 |  | 
| Koushik Dutta | da32b54 | 2011-01-01 17:55:22 -0800 | [diff] [blame] | 425 |     // device may simply be a name, like "system" | 
| Koushik Dutta | 4187347 | 2011-05-26 17:30:43 -0700 | [diff] [blame] | 426 |     if (get_flash_type(fs_type) != UNSUPPORTED) | 
| Koushik Dutta | 8a6bc77 | 2011-05-26 11:14:15 -0700 | [diff] [blame] | 427 |         return erase_raw_partition(fs_type, device); | 
| Koushik Dutta | da32b54 | 2011-01-01 17:55:22 -0800 | [diff] [blame] | 428 |  | 
| Koushik Dutta | 9f52e5f | 2011-01-02 14:11:24 -0800 | [diff] [blame] | 429 |     // if this is SDEXT:, don't worry about it if it does not exist. | 
| Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 430 |     if (0 == strcmp(path, "/sd-ext")) | 
| Koushik K. Dutta | f3534d0 | 2010-04-18 18:06:24 -0700 | [diff] [blame] | 431 |     { | 
 | 432 |         struct stat st; | 
| Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 433 |         Volume *vol = volume_for_path("/sd-ext"); | 
 | 434 |         if (vol == NULL || 0 != stat(vol->device, &st)) | 
| Koushik K. Dutta | f3534d0 | 2010-04-18 18:06:24 -0700 | [diff] [blame] | 435 |         { | 
 | 436 |             ui_print("No app2sd partition found. Skipping format of /sd-ext.\n"); | 
 | 437 |             return 0; | 
 | 438 |         } | 
 | 439 |     } | 
| Koushik Dutta | 2f73e58 | 2010-04-18 16:00:21 -0700 | [diff] [blame] | 440 |  | 
| Koushik Dutta | 9f52e5f | 2011-01-02 14:11:24 -0800 | [diff] [blame] | 441 |     if (NULL != fs_type) { | 
 | 442 |         if (strcmp("ext3", fs_type) == 0) { | 
 | 443 |             LOGI("Formatting ext3 device.\n"); | 
 | 444 |             if (0 != ensure_path_unmounted(path)) { | 
 | 445 |                 LOGE("Error while unmounting %s.\n", path); | 
 | 446 |                 return -12; | 
 | 447 |             } | 
 | 448 |             return format_ext3_device(device); | 
 | 449 |         } | 
 | 450 |  | 
 | 451 |         if (strcmp("ext2", fs_type) == 0) { | 
 | 452 |             LOGI("Formatting ext2 device.\n"); | 
 | 453 |             if (0 != ensure_path_unmounted(path)) { | 
 | 454 |                 LOGE("Error while unmounting %s.\n", path); | 
 | 455 |                 return -12; | 
 | 456 |             } | 
 | 457 |             return format_ext2_device(device); | 
 | 458 |         } | 
 | 459 |     } | 
 | 460 |  | 
| Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 461 |     if (0 != ensure_path_mounted(path)) | 
| Koushik K. Dutta | 3f99539 | 2010-03-30 23:29:43 -0700 | [diff] [blame] | 462 |     { | 
 | 463 |         ui_print("Error mounting %s!\n", path); | 
| Koushik Dutta | cd44ab9 | 2010-06-23 00:02:14 -0700 | [diff] [blame] | 464 |         ui_print("Skipping format...\n"); | 
 | 465 |         return 0; | 
| Koushik K. Dutta | 3f99539 | 2010-03-30 23:29:43 -0700 | [diff] [blame] | 466 |     } | 
 | 467 |  | 
 | 468 |     static char tmp[PATH_MAX]; | 
| Koushik Dutta | 5082299 | 2011-06-08 19:03:27 -0700 | [diff] [blame] | 469 |     if (strcmp(path, "/data") == 0) { | 
 | 470 |         sprintf(tmp, "cd /data ; for f in $(ls -a | grep -v ^media$); do rm -rf $f; done"); | 
 | 471 |         __system(tmp); | 
 | 472 |     } | 
 | 473 |     else { | 
 | 474 |         sprintf(tmp, "rm -rf %s/*", path); | 
 | 475 |         __system(tmp); | 
 | 476 |         sprintf(tmp, "rm -rf %s/.*", path); | 
 | 477 |         __system(tmp); | 
 | 478 |     } | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 479 |  | 
| Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 480 |     ensure_path_unmounted(path); | 
| Koushik K. Dutta | 3f99539 | 2010-03-30 23:29:43 -0700 | [diff] [blame] | 481 |     return 0; | 
 | 482 | } | 
 | 483 |  | 
| Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 484 | //#define MOUNTABLE_COUNT 5 | 
 | 485 | //#define DEVICE_COUNT 4 | 
 | 486 | //#define MMC_COUNT 2 | 
| Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 487 |  | 
| Kolja Dummann | 92796ec | 2011-02-13 20:59:30 +0100 | [diff] [blame] | 488 | typedef struct { | 
 | 489 |     char mount[255]; | 
 | 490 |     char unmount[255]; | 
 | 491 |     Volume* v; | 
 | 492 | } MountMenuEntry; | 
 | 493 |  | 
 | 494 | typedef struct { | 
 | 495 |     char txt[255]; | 
 | 496 |     Volume* v; | 
 | 497 | } FormatMenuEntry; | 
 | 498 |  | 
| Steve Kondik | 0d87851 | 2011-04-09 01:41:45 -0400 | [diff] [blame] | 499 | int is_safe_to_format(char* name) | 
 | 500 | { | 
| Brandon Bennett | 9ad8ba2 | 2011-05-07 23:42:58 -0600 | [diff] [blame] | 501 |     char str[255]; | 
 | 502 |     char* partition; | 
| j_r0dd | 14d9e75 | 2011-06-09 18:37:28 -0400 | [diff] [blame] | 503 |     property_get("ro.cwm.forbid_format", str, "/misc,/radio,/bootloader,/recovery,/efs"); | 
| Brandon Bennett | 9ad8ba2 | 2011-05-07 23:42:58 -0600 | [diff] [blame] | 504 |  | 
 | 505 |     partition = strtok(str, ", "); | 
 | 506 |     while (partition != NULL) { | 
 | 507 |         if (strcmp(name, partition) == 0) { | 
 | 508 |             return 0; | 
 | 509 |         } | 
 | 510 |         partition = strtok(NULL, ", "); | 
 | 511 |     } | 
 | 512 |  | 
 | 513 |     return 1; | 
| Steve Kondik | 0d87851 | 2011-04-09 01:41:45 -0400 | [diff] [blame] | 514 | } | 
 | 515 |  | 
| Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame] | 516 | void show_partition_menu() | 
| Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 517 | { | 
| Koushik Dutta | ceddcd5 | 2010-08-23 16:13:14 -0700 | [diff] [blame] | 518 |     static char* headers[] = {  "Mounts and Storage Menu", | 
| Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 519 |                                 "", | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 520 |                                 NULL | 
| Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 521 |     }; | 
 | 522 |  | 
| Kolja Dummann | 92796ec | 2011-02-13 20:59:30 +0100 | [diff] [blame] | 523 |     static MountMenuEntry* mount_menue = NULL; | 
 | 524 |     static FormatMenuEntry* format_menue = NULL; | 
 | 525 |  | 
| Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 526 |     typedef char* string; | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 527 |  | 
| Kolja Dummann | 92796ec | 2011-02-13 20:59:30 +0100 | [diff] [blame] | 528 |     int i, mountable_volumes, formatable_volumes; | 
 | 529 |     int num_volumes; | 
 | 530 |     Volume* device_volumes; | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 531 |  | 
| Kolja Dummann | 92796ec | 2011-02-13 20:59:30 +0100 | [diff] [blame] | 532 |     num_volumes = get_num_volumes(); | 
 | 533 |     device_volumes = get_device_volumes(); | 
 | 534 |  | 
 | 535 |     string options[255]; | 
 | 536 |  | 
 | 537 |     if(!device_volumes) | 
 | 538 | 		return; | 
 | 539 |  | 
 | 540 | 		mountable_volumes = 0; | 
 | 541 | 		formatable_volumes = 0; | 
 | 542 |  | 
 | 543 | 		mount_menue = malloc(num_volumes * sizeof(MountMenuEntry)); | 
 | 544 | 		format_menue = malloc(num_volumes * sizeof(FormatMenuEntry)); | 
 | 545 |  | 
 | 546 | 		for (i = 0; i < num_volumes; ++i) { | 
 | 547 | 			Volume* v = &device_volumes[i]; | 
| Koushik Dutta | 67fa0c3 | 2011-03-17 11:37:21 -0700 | [diff] [blame] | 548 | 			if(strcmp("ramdisk", v->fs_type) != 0 && strcmp("mtd", v->fs_type) != 0 && strcmp("emmc", v->fs_type) != 0 && strcmp("bml", v->fs_type) != 0) | 
| Kolja Dummann | 92796ec | 2011-02-13 20:59:30 +0100 | [diff] [blame] | 549 | 			{ | 
 | 550 | 				sprintf(&mount_menue[mountable_volumes].mount, "mount %s", v->mount_point); | 
 | 551 | 				sprintf(&mount_menue[mountable_volumes].unmount, "unmount %s", v->mount_point); | 
 | 552 | 				mount_menue[mountable_volumes].v = &device_volumes[i]; | 
 | 553 | 				++mountable_volumes; | 
| Steve Kondik | 0d87851 | 2011-04-09 01:41:45 -0400 | [diff] [blame] | 554 | 				if (is_safe_to_format(v->mount_point)) { | 
 | 555 | 					sprintf(&format_menue[formatable_volumes].txt, "format %s", v->mount_point); | 
 | 556 | 					format_menue[formatable_volumes].v = &device_volumes[i]; | 
 | 557 | 					++formatable_volumes; | 
 | 558 | 				} | 
| Kolja Dummann | 92796ec | 2011-02-13 20:59:30 +0100 | [diff] [blame] | 559 | 		    } | 
| Steve Kondik | 0d87851 | 2011-04-09 01:41:45 -0400 | [diff] [blame] | 560 | 		    else if (strcmp("ramdisk", v->fs_type) != 0 && strcmp("mtd", v->fs_type) == 0 && is_safe_to_format(v->mount_point)) | 
| Kolja Dummann | 92796ec | 2011-02-13 20:59:30 +0100 | [diff] [blame] | 561 | 		    { | 
 | 562 | 				sprintf(&format_menue[formatable_volumes].txt, "format %s", v->mount_point); | 
 | 563 | 				format_menue[formatable_volumes].v = &device_volumes[i]; | 
 | 564 | 				++formatable_volumes; | 
 | 565 | 			} | 
 | 566 | 		} | 
 | 567 |  | 
| Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 568 |  | 
| Koushik Dutta | d63eaef | 2010-07-14 21:01:21 -0700 | [diff] [blame] | 569 |     static char* confirm_format  = "Confirm format?"; | 
| Koushik Dutta | ecd32fa | 2010-07-14 18:38:02 -0700 | [diff] [blame] | 570 |     static char* confirm = "Yes - Format"; | 
| wjb | cea6eba | 2011-04-30 21:04:04 -0400 | [diff] [blame] | 571 |     char confirm_string[255]; | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 572 |  | 
| Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 573 |     for (;;) | 
 | 574 |     { | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 575 |  | 
| Kolja Dummann | 92796ec | 2011-02-13 20:59:30 +0100 | [diff] [blame] | 576 | 		for (i = 0; i < mountable_volumes; i++) | 
 | 577 | 		{ | 
 | 578 | 			MountMenuEntry* e = &mount_menue[i]; | 
 | 579 | 			Volume* v = e->v; | 
 | 580 | 			if(is_path_mounted(v->mount_point)) | 
 | 581 | 				options[i] = e->unmount; | 
 | 582 | 			else | 
 | 583 | 				options[i] = e->mount; | 
 | 584 | 		} | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 585 |  | 
| Kolja Dummann | 92796ec | 2011-02-13 20:59:30 +0100 | [diff] [blame] | 586 | 		for (i = 0; i < formatable_volumes; i++) | 
 | 587 | 		{ | 
 | 588 | 			FormatMenuEntry* e = &format_menue[i]; | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 589 |  | 
| Kolja Dummann | 92796ec | 2011-02-13 20:59:30 +0100 | [diff] [blame] | 590 | 			options[mountable_volumes+i] = e->txt; | 
 | 591 | 		} | 
 | 592 |  | 
 | 593 |         options[mountable_volumes+formatable_volumes] = "mount USB storage"; | 
 | 594 |         options[mountable_volumes+formatable_volumes + 1] = NULL; | 
 | 595 |  | 
 | 596 |         int chosen_item = get_menu_selection(headers, &options, 0, 0); | 
| Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 597 |         if (chosen_item == GO_BACK) | 
 | 598 |             break; | 
| Kolja Dummann | 92796ec | 2011-02-13 20:59:30 +0100 | [diff] [blame] | 599 |         if (chosen_item == (mountable_volumes+formatable_volumes)) | 
| Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 600 |         { | 
 | 601 |             show_mount_usb_storage_menu(); | 
 | 602 |         } | 
| Kolja Dummann | 92796ec | 2011-02-13 20:59:30 +0100 | [diff] [blame] | 603 |         else if (chosen_item < mountable_volumes) | 
| Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 604 |         { | 
| Kolja Dummann | 92796ec | 2011-02-13 20:59:30 +0100 | [diff] [blame] | 605 | 			MountMenuEntry* e = &mount_menue[chosen_item]; | 
 | 606 |             Volume* v = e->v; | 
 | 607 |  | 
 | 608 |             if (is_path_mounted(v->mount_point)) | 
| Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 609 |             { | 
| Kolja Dummann | 92796ec | 2011-02-13 20:59:30 +0100 | [diff] [blame] | 610 |                 if (0 != ensure_path_unmounted(v->mount_point)) | 
 | 611 |                     ui_print("Error unmounting %s!\n", v->mount_point); | 
| Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 612 |             } | 
 | 613 |             else | 
 | 614 |             { | 
| Kolja Dummann | 92796ec | 2011-02-13 20:59:30 +0100 | [diff] [blame] | 615 |                 if (0 != ensure_path_mounted(v->mount_point)) | 
 | 616 |                     ui_print("Error mounting %s!\n",  v->mount_point); | 
| Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 617 |             } | 
 | 618 |         } | 
| Kolja Dummann | 92796ec | 2011-02-13 20:59:30 +0100 | [diff] [blame] | 619 |         else if (chosen_item < (mountable_volumes + formatable_volumes)) | 
| Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame] | 620 |         { | 
| Kolja Dummann | 92796ec | 2011-02-13 20:59:30 +0100 | [diff] [blame] | 621 |             chosen_item = chosen_item - mountable_volumes; | 
 | 622 |             FormatMenuEntry* e = &format_menue[chosen_item]; | 
 | 623 |             Volume* v = e->v; | 
 | 624 |  | 
| wjb | cea6eba | 2011-04-30 21:04:04 -0400 | [diff] [blame] | 625 |             sprintf(confirm_string, "%s - %s", v->mount_point, confirm_format); | 
 | 626 |  | 
 | 627 |             if (!confirm_selection(confirm_string, confirm)) | 
| Koushik K. Dutta | 16f0b49 | 2010-03-19 14:37:11 -0700 | [diff] [blame] | 628 |                 continue; | 
| Kolja Dummann | 92796ec | 2011-02-13 20:59:30 +0100 | [diff] [blame] | 629 |             ui_print("Formatting %s...\n", v->mount_point); | 
 | 630 |             if (0 != format_volume(v->mount_point)) | 
 | 631 |                 ui_print("Error formatting %s!\n", v->mount_point); | 
| Koushik K. Dutta | 3f99539 | 2010-03-30 23:29:43 -0700 | [diff] [blame] | 632 |             else | 
 | 633 |                 ui_print("Done.\n"); | 
| Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame] | 634 |         } | 
| Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 635 |     } | 
| Kolja Dummann | 92796ec | 2011-02-13 20:59:30 +0100 | [diff] [blame] | 636 |  | 
 | 637 |     free(mount_menue); | 
 | 638 |     free(format_menue); | 
 | 639 |  | 
| Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 640 | } | 
 | 641 |  | 
| Koushik K. Dutta | 72a1db6 | 2010-03-07 14:10:26 -0800 | [diff] [blame] | 642 | #define EXTENDEDCOMMAND_SCRIPT "/cache/recovery/extendedcommand" | 
 | 643 |  | 
 | 644 | int extendedcommand_file_exists() | 
| Koushik K. Dutta | f68aaaf | 2010-03-07 13:39:21 -0800 | [diff] [blame] | 645 | { | 
| Koushik K. Dutta | f68aaaf | 2010-03-07 13:39:21 -0800 | [diff] [blame] | 646 |     struct stat file_info; | 
| Koushik K. Dutta | 72a1db6 | 2010-03-07 14:10:26 -0800 | [diff] [blame] | 647 |     return 0 == stat(EXTENDEDCOMMAND_SCRIPT, &file_info); | 
 | 648 | } | 
 | 649 |  | 
| Koushik Dutta | bec0995 | 2010-12-19 20:37:57 -0800 | [diff] [blame] | 650 | int edify_main(int argc, char** argv) { | 
 | 651 |     load_volume_table(); | 
 | 652 |     process_volumes(); | 
 | 653 |     RegisterBuiltins(); | 
 | 654 |     RegisterRecoveryHooks(); | 
 | 655 |     FinishRegistration(); | 
| Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 656 |  | 
| Koushik Dutta | bec0995 | 2010-12-19 20:37:57 -0800 | [diff] [blame] | 657 |     if (argc != 2) { | 
 | 658 |         printf("edify <filename>\n"); | 
| Koushik K. Dutta | f68aaaf | 2010-03-07 13:39:21 -0800 | [diff] [blame] | 659 |         return 1; | 
| Koushik K. Dutta | f68aaaf | 2010-03-07 13:39:21 -0800 | [diff] [blame] | 660 |     } | 
 | 661 |  | 
| Koushik Dutta | bec0995 | 2010-12-19 20:37:57 -0800 | [diff] [blame] | 662 |     FILE* f = fopen(argv[1], "r"); | 
 | 663 |     if (f == NULL) { | 
 | 664 |         printf("%s: %s: No such file or directory\n", argv[0], argv[1]); | 
 | 665 |         return 1; | 
 | 666 |     } | 
 | 667 |     char buffer[8192]; | 
 | 668 |     int size = fread(buffer, 1, 8191, f); | 
 | 669 |     fclose(f); | 
 | 670 |     buffer[size] = '\0'; | 
 | 671 |  | 
 | 672 |     Expr* root; | 
 | 673 |     int error_count = 0; | 
 | 674 |     yy_scan_bytes(buffer, size); | 
 | 675 |     int error = yyparse(&root, &error_count); | 
 | 676 |     printf("parse returned %d; %d errors encountered\n", error, error_count); | 
 | 677 |     if (error == 0 || error_count > 0) { | 
 | 678 |  | 
 | 679 |         //ExprDump(0, root, buffer); | 
 | 680 |  | 
 | 681 |         State state; | 
 | 682 |         state.cookie = NULL; | 
 | 683 |         state.script = buffer; | 
 | 684 |         state.errmsg = NULL; | 
 | 685 |  | 
 | 686 |         char* result = Evaluate(&state, root); | 
 | 687 |         if (result == NULL) { | 
 | 688 |             printf("result was NULL, message is: %s\n", | 
 | 689 |                    (state.errmsg == NULL ? "(NULL)" : state.errmsg)); | 
 | 690 |             free(state.errmsg); | 
 | 691 |         } else { | 
 | 692 |             printf("result is [%s]\n", result); | 
| Koushik K. Dutta | f68aaaf | 2010-03-07 13:39:21 -0800 | [diff] [blame] | 693 |         } | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 694 |     } | 
| Koushik Dutta | bec0995 | 2010-12-19 20:37:57 -0800 | [diff] [blame] | 695 |     return 0; | 
| Koushik K. Dutta | f68aaaf | 2010-03-07 13:39:21 -0800 | [diff] [blame] | 696 | } | 
| Koushik K. Dutta | 72a1db6 | 2010-03-07 14:10:26 -0800 | [diff] [blame] | 697 |  | 
| Koushik K. Dutta | 6771aca | 2010-04-03 23:28:39 -0700 | [diff] [blame] | 698 | int run_script(char* filename) | 
| Koushik K. Dutta | 3836f72 | 2010-03-11 22:17:43 -0800 | [diff] [blame] | 699 | { | 
 | 700 |     struct stat file_info; | 
 | 701 |     if (0 != stat(filename, &file_info)) { | 
 | 702 |         printf("Error executing stat on file: %s\n", filename); | 
 | 703 |         return 1; | 
 | 704 |     } | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 705 |  | 
| Koushik K. Dutta | 3836f72 | 2010-03-11 22:17:43 -0800 | [diff] [blame] | 706 |     int script_len = file_info.st_size; | 
| Koushik K. Dutta | 707fa6d | 2010-03-23 11:44:33 -0700 | [diff] [blame] | 707 |     char* script_data = (char*)malloc(script_len + 1); | 
| Koushik K. Dutta | 3836f72 | 2010-03-11 22:17:43 -0800 | [diff] [blame] | 708 |     FILE *file = fopen(filename, "rb"); | 
 | 709 |     fread(script_data, script_len, 1, file); | 
| Koushik K. Dutta | 707fa6d | 2010-03-23 11:44:33 -0700 | [diff] [blame] | 710 |     // supposedly not necessary, but let's be safe. | 
 | 711 |     script_data[script_len] = '\0'; | 
| Koushik K. Dutta | 3836f72 | 2010-03-11 22:17:43 -0800 | [diff] [blame] | 712 |     fclose(file); | 
| Koushik Dutta | e25908b | 2010-06-21 08:16:19 -0700 | [diff] [blame] | 713 |     LOGI("Running script:\n"); | 
 | 714 |     LOGI("\n%s\n", script_data); | 
| Koushik K. Dutta | 3836f72 | 2010-03-11 22:17:43 -0800 | [diff] [blame] | 715 |  | 
| Koushik K. Dutta | f3534d0 | 2010-04-18 18:06:24 -0700 | [diff] [blame] | 716 |     int ret = run_script_from_buffer(script_data, script_len, filename); | 
| Koushik K. Dutta | 1d53c4e | 2010-04-02 16:46:21 -0700 | [diff] [blame] | 717 |     free(script_data); | 
 | 718 |     return ret; | 
| Koushik K. Dutta | 3836f72 | 2010-03-11 22:17:43 -0800 | [diff] [blame] | 719 | } | 
 | 720 |  | 
| Koushik K. Dutta | 72a1db6 | 2010-03-07 14:10:26 -0800 | [diff] [blame] | 721 | int run_and_remove_extendedcommand() | 
 | 722 | { | 
| Koushik K. Dutta | 6771aca | 2010-04-03 23:28:39 -0700 | [diff] [blame] | 723 |     char tmp[PATH_MAX]; | 
 | 724 |     sprintf(tmp, "cp %s /tmp/%s", EXTENDEDCOMMAND_SCRIPT, basename(EXTENDEDCOMMAND_SCRIPT)); | 
 | 725 |     __system(tmp); | 
 | 726 |     remove(EXTENDEDCOMMAND_SCRIPT); | 
| Koushik K. Dutta | 3a25cf5 | 2010-03-08 19:22:41 -0800 | [diff] [blame] | 727 |     int i = 0; | 
 | 728 |     for (i = 20; i > 0; i--) { | 
 | 729 |         ui_print("Waiting for SD Card to mount (%ds)\n", i); | 
| Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 730 |         if (ensure_path_mounted("/sdcard") == 0) { | 
| Koushik K. Dutta | 3a25cf5 | 2010-03-08 19:22:41 -0800 | [diff] [blame] | 731 |             ui_print("SD Card mounted...\n"); | 
 | 732 |             break; | 
 | 733 |         } | 
 | 734 |         sleep(1); | 
 | 735 |     } | 
| Koushik Dutta | 92077c1 | 2010-07-15 00:10:08 -0700 | [diff] [blame] | 736 |     remove("/sdcard/clockworkmod/.recoverycheckpoint"); | 
| Koushik K. Dutta | 3a25cf5 | 2010-03-08 19:22:41 -0800 | [diff] [blame] | 737 |     if (i == 0) { | 
 | 738 |         ui_print("Timed out waiting for SD card... continuing anyways."); | 
 | 739 |     } | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 740 |  | 
| Koushik K. Dutta | 6771aca | 2010-04-03 23:28:39 -0700 | [diff] [blame] | 741 |     sprintf(tmp, "/tmp/%s", basename(EXTENDEDCOMMAND_SCRIPT)); | 
 | 742 |     return run_script(tmp); | 
| Koushik K. Dutta | 72a1db6 | 2010-03-07 14:10:26 -0800 | [diff] [blame] | 743 | } | 
 | 744 |  | 
| Koushik K. Dutta | fe84a7f | 2010-03-25 18:19:23 -0700 | [diff] [blame] | 745 | void show_nandroid_advanced_restore_menu() | 
 | 746 | { | 
| Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 747 |     if (ensure_path_mounted("/sdcard") != 0) { | 
| Koushik K. Dutta | fe84a7f | 2010-03-25 18:19:23 -0700 | [diff] [blame] | 748 |         LOGE ("Can't mount /sdcard\n"); | 
 | 749 |         return; | 
 | 750 |     } | 
 | 751 |  | 
 | 752 |     static char* advancedheaders[] = {  "Choose an image to restore", | 
 | 753 |                                 "", | 
 | 754 |                                 "Choose an image to restore", | 
 | 755 |                                 "first. The next menu will", | 
 | 756 |                                 "you more options.", | 
 | 757 |                                 "", | 
 | 758 |                                 NULL | 
 | 759 |     }; | 
 | 760 |  | 
 | 761 |     char* file = choose_file_menu("/sdcard/clockworkmod/backup/", NULL, advancedheaders); | 
 | 762 |     if (file == NULL) | 
 | 763 |         return; | 
 | 764 |  | 
 | 765 |     static char* headers[] = {  "Nandroid Advanced Restore", | 
 | 766 |                                 "", | 
 | 767 |                                 NULL | 
 | 768 |     }; | 
 | 769 |  | 
 | 770 |     static char* list[] = { "Restore boot", | 
 | 771 |                             "Restore system", | 
 | 772 |                             "Restore data", | 
 | 773 |                             "Restore cache", | 
| Koushik K. Dutta | 68b0190 | 2010-04-01 12:20:39 -0700 | [diff] [blame] | 774 |                             "Restore sd-ext", | 
| agrabren | 1f76a5d | 2010-12-29 12:15:18 -0600 | [diff] [blame] | 775 |                             "Restore wimax", | 
| Koushik K. Dutta | fe84a7f | 2010-03-25 18:19:23 -0700 | [diff] [blame] | 776 |                             NULL | 
 | 777 |     }; | 
| Koushik Dutta | 5b7f34a | 2010-12-29 23:36:03 -0800 | [diff] [blame] | 778 |      | 
 | 779 |     char tmp[PATH_MAX]; | 
 | 780 |     if (0 != get_partition_device("wimax", tmp)) { | 
 | 781 |         // disable wimax restore option | 
 | 782 |         list[5] = NULL; | 
 | 783 |     } | 
| Koushik Dutta | d63eaef | 2010-07-14 21:01:21 -0700 | [diff] [blame] | 784 |  | 
 | 785 |     static char* confirm_restore  = "Confirm restore?"; | 
 | 786 |  | 
| Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 787 |     int chosen_item = get_menu_selection(headers, list, 0, 0); | 
| Koushik K. Dutta | fe84a7f | 2010-03-25 18:19:23 -0700 | [diff] [blame] | 788 |     switch (chosen_item) | 
 | 789 |     { | 
 | 790 |         case 0: | 
| Koushik Dutta | d63eaef | 2010-07-14 21:01:21 -0700 | [diff] [blame] | 791 |             if (confirm_selection(confirm_restore, "Yes - Restore boot")) | 
| agrabren | 1f76a5d | 2010-12-29 12:15:18 -0600 | [diff] [blame] | 792 |                 nandroid_restore(file, 1, 0, 0, 0, 0, 0); | 
| Koushik K. Dutta | fe84a7f | 2010-03-25 18:19:23 -0700 | [diff] [blame] | 793 |             break; | 
 | 794 |         case 1: | 
| Koushik Dutta | d63eaef | 2010-07-14 21:01:21 -0700 | [diff] [blame] | 795 |             if (confirm_selection(confirm_restore, "Yes - Restore system")) | 
| agrabren | 1f76a5d | 2010-12-29 12:15:18 -0600 | [diff] [blame] | 796 |                 nandroid_restore(file, 0, 1, 0, 0, 0, 0); | 
| Koushik K. Dutta | fe84a7f | 2010-03-25 18:19:23 -0700 | [diff] [blame] | 797 |             break; | 
 | 798 |         case 2: | 
| Koushik Dutta | d63eaef | 2010-07-14 21:01:21 -0700 | [diff] [blame] | 799 |             if (confirm_selection(confirm_restore, "Yes - Restore data")) | 
| agrabren | 1f76a5d | 2010-12-29 12:15:18 -0600 | [diff] [blame] | 800 |                 nandroid_restore(file, 0, 0, 1, 0, 0, 0); | 
| Koushik K. Dutta | fe84a7f | 2010-03-25 18:19:23 -0700 | [diff] [blame] | 801 |             break; | 
 | 802 |         case 3: | 
| Koushik Dutta | d63eaef | 2010-07-14 21:01:21 -0700 | [diff] [blame] | 803 |             if (confirm_selection(confirm_restore, "Yes - Restore cache")) | 
| agrabren | 1f76a5d | 2010-12-29 12:15:18 -0600 | [diff] [blame] | 804 |                 nandroid_restore(file, 0, 0, 0, 1, 0, 0); | 
| Koushik K. Dutta | fe84a7f | 2010-03-25 18:19:23 -0700 | [diff] [blame] | 805 |             break; | 
| Koushik Dutta | 2f73e58 | 2010-04-18 16:00:21 -0700 | [diff] [blame] | 806 |         case 4: | 
| Koushik Dutta | d63eaef | 2010-07-14 21:01:21 -0700 | [diff] [blame] | 807 |             if (confirm_selection(confirm_restore, "Yes - Restore sd-ext")) | 
| agrabren | 1f76a5d | 2010-12-29 12:15:18 -0600 | [diff] [blame] | 808 |                 nandroid_restore(file, 0, 0, 0, 0, 1, 0); | 
 | 809 |             break; | 
 | 810 |         case 5: | 
 | 811 |             if (confirm_selection(confirm_restore, "Yes - Restore wimax")) | 
 | 812 |                 nandroid_restore(file, 0, 0, 0, 0, 0, 1); | 
| Koushik Dutta | 2f73e58 | 2010-04-18 16:00:21 -0700 | [diff] [blame] | 813 |             break; | 
| Koushik K. Dutta | fe84a7f | 2010-03-25 18:19:23 -0700 | [diff] [blame] | 814 |     } | 
 | 815 | } | 
 | 816 |  | 
| Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame] | 817 | void show_nandroid_menu() | 
 | 818 | { | 
 | 819 |     static char* headers[] = {  "Nandroid", | 
 | 820 |                                 "", | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 821 |                                 NULL | 
| Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame] | 822 |     }; | 
 | 823 |  | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 824 |     static char* list[] = { "Backup", | 
| Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame] | 825 |                             "Restore", | 
| Koushik K. Dutta | fe84a7f | 2010-03-25 18:19:23 -0700 | [diff] [blame] | 826 |                             "Advanced Restore", | 
| Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame] | 827 |                             NULL | 
 | 828 |     }; | 
 | 829 |  | 
| Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 830 |     int chosen_item = get_menu_selection(headers, list, 0, 0); | 
| Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame] | 831 |     switch (chosen_item) | 
 | 832 |     { | 
 | 833 |         case 0: | 
 | 834 |             { | 
| Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame] | 835 |                 char backup_path[PATH_MAX]; | 
| Koushik K. Dutta | 6a26e7c | 2010-03-27 15:26:11 -0700 | [diff] [blame] | 836 |                 time_t t = time(NULL); | 
 | 837 |                 struct tm *tmp = localtime(&t); | 
 | 838 |                 if (tmp == NULL) | 
 | 839 |                 { | 
 | 840 |                     struct timeval tp; | 
 | 841 |                     gettimeofday(&tp, NULL); | 
 | 842 |                     sprintf(backup_path, "/sdcard/clockworkmod/backup/%d", tp.tv_sec); | 
 | 843 |                 } | 
 | 844 |                 else | 
 | 845 |                 { | 
 | 846 |                     strftime(backup_path, sizeof(backup_path), "/sdcard/clockworkmod/backup/%F.%H.%M.%S", tmp); | 
 | 847 |                 } | 
| Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame] | 848 |                 nandroid_backup(backup_path); | 
 | 849 |             } | 
 | 850 |             break; | 
 | 851 |         case 1: | 
 | 852 |             show_nandroid_restore_menu(); | 
 | 853 |             break; | 
| Koushik K. Dutta | fe84a7f | 2010-03-25 18:19:23 -0700 | [diff] [blame] | 854 |         case 2: | 
 | 855 |             show_nandroid_advanced_restore_menu(); | 
 | 856 |             break; | 
| Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame] | 857 |     } | 
| Koushik K. Dutta | a496b51 | 2010-03-19 14:51:45 -0700 | [diff] [blame] | 858 | } | 
 | 859 |  | 
| Koushik Dutta | fd1579b | 2010-05-01 12:46:55 -0700 | [diff] [blame] | 860 | void wipe_battery_stats() | 
 | 861 | { | 
| Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 862 |     ensure_path_mounted("/data"); | 
| Koushik Dutta | fd1579b | 2010-05-01 12:46:55 -0700 | [diff] [blame] | 863 |     remove("/data/system/batterystats.bin"); | 
| Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 864 |     ensure_path_unmounted("/data"); | 
| Koushik Dutta | fd1579b | 2010-05-01 12:46:55 -0700 | [diff] [blame] | 865 | } | 
 | 866 |  | 
| Koushik K. Dutta | a496b51 | 2010-03-19 14:51:45 -0700 | [diff] [blame] | 867 | void show_advanced_menu() | 
 | 868 | { | 
 | 869 |     static char* headers[] = {  "Advanced and Debugging Menu", | 
 | 870 |                                 "", | 
 | 871 |                                 NULL | 
 | 872 |     }; | 
 | 873 |  | 
| Koushik K. Dutta | 7fe4d7b | 2010-04-06 23:04:52 -0700 | [diff] [blame] | 874 |     static char* list[] = { "Reboot Recovery", | 
| Koushik Dutta | 49af23c | 2010-06-21 13:45:51 -0700 | [diff] [blame] | 875 |                             "Wipe Dalvik Cache", | 
| Koushik Dutta | fd1579b | 2010-05-01 12:46:55 -0700 | [diff] [blame] | 876 |                             "Wipe Battery Stats", | 
| Koushik Dutta | 598cfc7 | 2010-06-20 09:42:47 -0700 | [diff] [blame] | 877 |                             "Report Error", | 
| Koushik K. Dutta | 7fe4d7b | 2010-04-06 23:04:52 -0700 | [diff] [blame] | 878 |                             "Key Test", | 
| Koushik Dutta | e17a78d | 2010-08-29 12:35:10 -0700 | [diff] [blame] | 879 | #ifndef BOARD_HAS_SMALL_RECOVERY | 
| Koushik Dutta | ceddcd5 | 2010-08-23 16:13:14 -0700 | [diff] [blame] | 880 |                             "Partition SD Card", | 
 | 881 |                             "Fix Permissions", | 
| Brint E. Kriebel | d352233 | 2010-12-11 20:52:06 -0700 | [diff] [blame] | 882 | #ifdef BOARD_HAS_SDCARD_INTERNAL | 
 | 883 |                             "Partition Internal SD Card", | 
 | 884 | #endif | 
| Koushik Dutta | e17a78d | 2010-08-29 12:35:10 -0700 | [diff] [blame] | 885 | #endif | 
| Koushik K. Dutta | a496b51 | 2010-03-19 14:51:45 -0700 | [diff] [blame] | 886 |                             NULL | 
 | 887 |     }; | 
 | 888 |  | 
 | 889 |     for (;;) | 
 | 890 |     { | 
| Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 891 |         int chosen_item = get_menu_selection(headers, list, 0, 0); | 
| Koushik K. Dutta | a496b51 | 2010-03-19 14:51:45 -0700 | [diff] [blame] | 892 |         if (chosen_item == GO_BACK) | 
 | 893 |             break; | 
 | 894 |         switch (chosen_item) | 
 | 895 |         { | 
 | 896 |             case 0: | 
| atinm | 54815c9 | 2011-05-03 00:25:01 -0400 | [diff] [blame] | 897 | #ifdef TARGET_RECOVERY_PRE_COMMAND | 
 | 898 |                 __system( TARGET_RECOVERY_PRE_COMMAND ); | 
 | 899 | #endif | 
| Koushik K. Dutta | 7fe4d7b | 2010-04-06 23:04:52 -0700 | [diff] [blame] | 900 |                 __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, "recovery"); | 
 | 901 |                 break; | 
 | 902 |             case 1: | 
| Koushik Dutta | d63eaef | 2010-07-14 21:01:21 -0700 | [diff] [blame] | 903 |             { | 
| Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 904 |                 if (0 != ensure_path_mounted("/data")) | 
| Koushik Dutta | 49af23c | 2010-06-21 13:45:51 -0700 | [diff] [blame] | 905 |                     break; | 
| Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 906 |                 ensure_path_mounted("/sd-ext"); | 
 | 907 |                 ensure_path_mounted("/cache"); | 
| Koushik Dutta | 6c7745d | 2010-08-07 12:17:13 -0700 | [diff] [blame] | 908 |                 if (confirm_selection( "Confirm wipe?", "Yes - Wipe Dalvik Cache")) { | 
| Koushik Dutta | d63eaef | 2010-07-14 21:01:21 -0700 | [diff] [blame] | 909 |                     __system("rm -r /data/dalvik-cache"); | 
| Koushik Dutta | 6c7745d | 2010-08-07 12:17:13 -0700 | [diff] [blame] | 910 |                     __system("rm -r /cache/dalvik-cache"); | 
 | 911 |                     __system("rm -r /sd-ext/dalvik-cache"); | 
 | 912 |                 } | 
| Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 913 |                 ensure_path_unmounted("/data"); | 
| Koushik Dutta | 6c7745d | 2010-08-07 12:17:13 -0700 | [diff] [blame] | 914 |                 ui_print("Dalvik Cache wiped.\n"); | 
| Koushik Dutta | fd1579b | 2010-05-01 12:46:55 -0700 | [diff] [blame] | 915 |                 break; | 
| Koushik Dutta | d63eaef | 2010-07-14 21:01:21 -0700 | [diff] [blame] | 916 |             } | 
| Koushik Dutta | fd1579b | 2010-05-01 12:46:55 -0700 | [diff] [blame] | 917 |             case 2: | 
| Koushik Dutta | d63eaef | 2010-07-14 21:01:21 -0700 | [diff] [blame] | 918 |             { | 
 | 919 |                 if (confirm_selection( "Confirm wipe?", "Yes - Wipe Battery Stats")) | 
 | 920 |                     wipe_battery_stats(); | 
| Koushik Dutta | 598cfc7 | 2010-06-20 09:42:47 -0700 | [diff] [blame] | 921 |                 break; | 
| Koushik Dutta | d63eaef | 2010-07-14 21:01:21 -0700 | [diff] [blame] | 922 |             } | 
| Koushik Dutta | 598cfc7 | 2010-06-20 09:42:47 -0700 | [diff] [blame] | 923 |             case 3: | 
| Koushik Dutta | 49af23c | 2010-06-21 13:45:51 -0700 | [diff] [blame] | 924 |                 handle_failure(1); | 
 | 925 |                 break; | 
 | 926 |             case 4: | 
| Koushik K. Dutta | a496b51 | 2010-03-19 14:51:45 -0700 | [diff] [blame] | 927 |             { | 
 | 928 |                 ui_print("Outputting key codes.\n"); | 
 | 929 |                 ui_print("Go back to end debugging.\n"); | 
 | 930 |                 int key; | 
 | 931 |                 int action; | 
 | 932 |                 do | 
 | 933 |                 { | 
 | 934 |                     key = ui_wait_key(); | 
 | 935 |                     action = device_handle_key(key, 1); | 
 | 936 |                     ui_print("Key: %d\n", key); | 
 | 937 |                 } | 
 | 938 |                 while (action != GO_BACK); | 
| Koushik Dutta | 56606a2 | 2010-08-23 16:15:33 -0700 | [diff] [blame] | 939 |                 break; | 
| Koushik K. Dutta | a496b51 | 2010-03-19 14:51:45 -0700 | [diff] [blame] | 940 |             } | 
| Koushik Dutta | f0e31b8 | 2010-08-17 16:55:38 -0700 | [diff] [blame] | 941 |             case 5: | 
 | 942 |             { | 
| Koushik Dutta | ceddcd5 | 2010-08-23 16:13:14 -0700 | [diff] [blame] | 943 |                 static char* ext_sizes[] = { "128M", | 
 | 944 |                                              "256M", | 
 | 945 |                                              "512M", | 
 | 946 |                                              "1024M", | 
| Brint E. Kriebel | 0b23331 | 2010-11-14 15:31:17 -0700 | [diff] [blame] | 947 |                                              "2048M", | 
 | 948 |                                              "4096M", | 
| Koushik Dutta | ceddcd5 | 2010-08-23 16:13:14 -0700 | [diff] [blame] | 949 |                                              NULL }; | 
 | 950 |  | 
 | 951 |                 static char* swap_sizes[] = { "0M", | 
 | 952 |                                               "32M", | 
 | 953 |                                               "64M", | 
 | 954 |                                               "128M", | 
 | 955 |                                               "256M", | 
 | 956 |                                               NULL }; | 
 | 957 |  | 
 | 958 |                 static char* ext_headers[] = { "Ext Size", "", NULL }; | 
 | 959 |                 static char* swap_headers[] = { "Swap Size", "", NULL }; | 
 | 960 |  | 
| Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 961 |                 int ext_size = get_menu_selection(ext_headers, ext_sizes, 0, 0); | 
| Koushik Dutta | ceddcd5 | 2010-08-23 16:13:14 -0700 | [diff] [blame] | 962 |                 if (ext_size == GO_BACK) | 
 | 963 |                     continue; | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 964 |  | 
| Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 965 |                 int swap_size = get_menu_selection(swap_headers, swap_sizes, 0, 0); | 
| Koushik Dutta | ceddcd5 | 2010-08-23 16:13:14 -0700 | [diff] [blame] | 966 |                 if (swap_size == GO_BACK) | 
 | 967 |                     continue; | 
 | 968 |  | 
 | 969 |                 char sddevice[256]; | 
| Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 970 |                 Volume *vol = volume_for_path("/sdcard"); | 
 | 971 |                 strcpy(sddevice, vol->device); | 
| Koushik Dutta | ceddcd5 | 2010-08-23 16:13:14 -0700 | [diff] [blame] | 972 |                 // we only want the mmcblk, not the partition | 
 | 973 |                 sddevice[strlen("/dev/block/mmcblkX")] = NULL; | 
 | 974 |                 char cmd[PATH_MAX]; | 
 | 975 |                 setenv("SDPATH", sddevice, 1); | 
 | 976 |                 sprintf(cmd, "sdparted -es %s -ss %s -efs ext3 -s", ext_sizes[ext_size], swap_sizes[swap_size]); | 
 | 977 |                 ui_print("Partitioning SD Card... please wait...\n"); | 
 | 978 |                 if (0 == __system(cmd)) | 
 | 979 |                     ui_print("Done!\n"); | 
 | 980 |                 else | 
 | 981 |                     ui_print("An error occured while partitioning your SD Card. Please see /tmp/recovery.log for more details.\n"); | 
| Koushik Dutta | 56606a2 | 2010-08-23 16:15:33 -0700 | [diff] [blame] | 982 |                 break; | 
| Koushik Dutta | f0e31b8 | 2010-08-17 16:55:38 -0700 | [diff] [blame] | 983 |             } | 
| Koushik Dutta | 38e8b2b | 2010-08-17 22:21:53 -0700 | [diff] [blame] | 984 |             case 6: | 
 | 985 |             { | 
| Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 986 |                 ensure_path_mounted("/system"); | 
 | 987 |                 ensure_path_mounted("/data"); | 
| Koushik Dutta | ceddcd5 | 2010-08-23 16:13:14 -0700 | [diff] [blame] | 988 |                 ui_print("Fixing permissions...\n"); | 
 | 989 |                 __system("fix_permissions"); | 
 | 990 |                 ui_print("Done!\n"); | 
| Koushik Dutta | 56606a2 | 2010-08-23 16:15:33 -0700 | [diff] [blame] | 991 |                 break; | 
| Koushik Dutta | 38e8b2b | 2010-08-17 22:21:53 -0700 | [diff] [blame] | 992 |             } | 
| Brint E. Kriebel | d352233 | 2010-12-11 20:52:06 -0700 | [diff] [blame] | 993 |             case 7: | 
 | 994 |             { | 
 | 995 |                 static char* ext_sizes[] = { "128M", | 
 | 996 |                                              "256M", | 
 | 997 |                                              "512M", | 
 | 998 |                                              "1024M", | 
 | 999 |                                              "2048M", | 
 | 1000 |                                              "4096M", | 
 | 1001 |                                              NULL }; | 
 | 1002 |  | 
 | 1003 |                 static char* swap_sizes[] = { "0M", | 
 | 1004 |                                               "32M", | 
 | 1005 |                                               "64M", | 
 | 1006 |                                               "128M", | 
 | 1007 |                                               "256M", | 
 | 1008 |                                               NULL }; | 
 | 1009 |  | 
 | 1010 |                 static char* ext_headers[] = { "Data Size", "", NULL }; | 
 | 1011 |                 static char* swap_headers[] = { "Swap Size", "", NULL }; | 
 | 1012 |  | 
| Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 1013 |                 int ext_size = get_menu_selection(ext_headers, ext_sizes, 0, 0); | 
| Brint E. Kriebel | d352233 | 2010-12-11 20:52:06 -0700 | [diff] [blame] | 1014 |                 if (ext_size == GO_BACK) | 
 | 1015 |                     continue; | 
 | 1016 |  | 
 | 1017 |                 int swap_size = 0; | 
 | 1018 |                 if (swap_size == GO_BACK) | 
 | 1019 |                     continue; | 
 | 1020 |  | 
 | 1021 |                 char sddevice[256]; | 
| Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 1022 |                 Volume *vol = volume_for_path("/emmc"); | 
 | 1023 |                 strcpy(sddevice, vol->device); | 
| Brint E. Kriebel | d352233 | 2010-12-11 20:52:06 -0700 | [diff] [blame] | 1024 |                 // we only want the mmcblk, not the partition | 
 | 1025 |                 sddevice[strlen("/dev/block/mmcblkX")] = NULL; | 
 | 1026 |                 char cmd[PATH_MAX]; | 
 | 1027 |                 setenv("SDPATH", sddevice, 1); | 
 | 1028 |                 sprintf(cmd, "sdparted -es %s -ss %s -efs ext3 -s", ext_sizes[ext_size], swap_sizes[swap_size]); | 
 | 1029 |                 ui_print("Partitioning Internal SD Card... please wait...\n"); | 
 | 1030 |                 if (0 == __system(cmd)) | 
 | 1031 |                     ui_print("Done!\n"); | 
 | 1032 |                 else | 
 | 1033 |                     ui_print("An error occured while partitioning your Internal SD Card. Please see /tmp/recovery.log for more details.\n"); | 
 | 1034 |                 break; | 
 | 1035 |             } | 
| Koushik K. Dutta | a496b51 | 2010-03-19 14:51:45 -0700 | [diff] [blame] | 1036 |         } | 
 | 1037 |     } | 
| Koushik Dutta | 14239d2 | 2010-06-14 15:02:48 -0700 | [diff] [blame] | 1038 | } | 
 | 1039 |  | 
| Koushik Dutta | b643e4f | 2010-12-18 18:39:39 -0800 | [diff] [blame] | 1040 | void write_fstab_root(char *path, FILE *file) | 
| Koushik Dutta | 14239d2 | 2010-06-14 15:02:48 -0700 | [diff] [blame] | 1041 | { | 
| Koushik Dutta | b643e4f | 2010-12-18 18:39:39 -0800 | [diff] [blame] | 1042 |     Volume *vol = volume_for_path(path); | 
 | 1043 |     if (vol == NULL) { | 
| Koushik Dutta | 9765a03 | 2010-12-18 21:31:02 -0800 | [diff] [blame] | 1044 |         LOGW("Unable to get recovery.fstab info for %s during fstab generation!\n", path); | 
| Koushik Dutta | 598cfc7 | 2010-06-20 09:42:47 -0700 | [diff] [blame] | 1045 |         return; | 
| Koushik Dutta | a6522b3 | 2010-06-20 13:16:06 -0700 | [diff] [blame] | 1046 |     } | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 1047 |  | 
| Koushik Dutta | 31b7411 | 2010-12-20 08:49:20 -0800 | [diff] [blame] | 1048 |     char device[200]; | 
 | 1049 |     if (vol->device[0] != '/') | 
 | 1050 |         get_partition_device(vol->device, device); | 
 | 1051 |     else | 
 | 1052 |         strcpy(device, vol->device); | 
 | 1053 |  | 
 | 1054 |     fprintf(file, "%s ", device); | 
| Koushik Dutta | b643e4f | 2010-12-18 18:39:39 -0800 | [diff] [blame] | 1055 |     fprintf(file, "%s ", path); | 
| Koushik Dutta | ab1f8b8 | 2011-02-27 17:28:30 -0800 | [diff] [blame] | 1056 |     // special case rfs cause auto will mount it as vfat on samsung. | 
 | 1057 |     fprintf(file, "%s rw\n", vol->fs_type2 != NULL && strcmp(vol->fs_type, "rfs") != 0 ? "auto" : vol->fs_type); | 
| Koushik Dutta | 14239d2 | 2010-06-14 15:02:48 -0700 | [diff] [blame] | 1058 | } | 
 | 1059 |  | 
 | 1060 | void create_fstab() | 
 | 1061 | { | 
| Koushik Dutta | b643e4f | 2010-12-18 18:39:39 -0800 | [diff] [blame] | 1062 |     struct stat info; | 
| Koushik Dutta | cd44ab9 | 2010-06-23 00:02:14 -0700 | [diff] [blame] | 1063 |     __system("touch /etc/mtab"); | 
| Koushik Dutta | 14239d2 | 2010-06-14 15:02:48 -0700 | [diff] [blame] | 1064 |     FILE *file = fopen("/etc/fstab", "w"); | 
| Koushik Dutta | a6522b3 | 2010-06-20 13:16:06 -0700 | [diff] [blame] | 1065 |     if (file == NULL) { | 
| Koushik Dutta | b643e4f | 2010-12-18 18:39:39 -0800 | [diff] [blame] | 1066 |         LOGW("Unable to create /etc/fstab!\n"); | 
| Koushik Dutta | 598cfc7 | 2010-06-20 09:42:47 -0700 | [diff] [blame] | 1067 |         return; | 
| Koushik Dutta | a6522b3 | 2010-06-20 13:16:06 -0700 | [diff] [blame] | 1068 |     } | 
| Brandon Bennett | 6d0604b | 2011-01-28 13:39:10 -0700 | [diff] [blame] | 1069 |     Volume *vol = volume_for_path("/boot"); | 
| Koushik Dutta | a9f0e7f | 2011-02-12 10:21:11 -0800 | [diff] [blame] | 1070 |     if (NULL != vol && strcmp(vol->fs_type, "mtd") != 0 && strcmp(vol->fs_type, "emmc") != 0 && strcmp(vol->fs_type, "bml") != 0) | 
| Brandon Bennett | 6d0604b | 2011-01-28 13:39:10 -0700 | [diff] [blame] | 1071 |          write_fstab_root("/boot", file); | 
| Koushik Dutta | b643e4f | 2010-12-18 18:39:39 -0800 | [diff] [blame] | 1072 |     write_fstab_root("/cache", file); | 
 | 1073 |     write_fstab_root("/data", file); | 
| Koushik Dutta | 5460f0c | 2010-12-18 22:37:49 -0800 | [diff] [blame] | 1074 |     if (has_datadata()) { | 
 | 1075 |         write_fstab_root("/datadata", file); | 
 | 1076 |     } | 
| Koushik Dutta | b643e4f | 2010-12-18 18:39:39 -0800 | [diff] [blame] | 1077 |     write_fstab_root("/system", file); | 
 | 1078 |     write_fstab_root("/sdcard", file); | 
 | 1079 |     write_fstab_root("/sd-ext", file); | 
| Koushik Dutta | 14239d2 | 2010-06-14 15:02:48 -0700 | [diff] [blame] | 1080 |     fclose(file); | 
| Koushik Dutta | 9765a03 | 2010-12-18 21:31:02 -0800 | [diff] [blame] | 1081 |     LOGI("Completed outputting fstab.\n"); | 
| Koushik Dutta | 598cfc7 | 2010-06-20 09:42:47 -0700 | [diff] [blame] | 1082 | } | 
 | 1083 |  | 
| Koushik Dutta | 4196e0f | 2010-12-19 03:38:29 -0800 | [diff] [blame] | 1084 | int bml_check_volume(const char *path) { | 
 | 1085 |     ui_print("Checking %s...\n", path); | 
 | 1086 |     ensure_path_unmounted(path); | 
 | 1087 |     if (0 == ensure_path_mounted(path)) { | 
 | 1088 |         ensure_path_unmounted(path); | 
 | 1089 |         return 0; | 
 | 1090 |     } | 
 | 1091 |      | 
 | 1092 |     Volume *vol = volume_for_path(path); | 
 | 1093 |     if (vol == NULL) { | 
 | 1094 |         LOGE("Unable process volume! Skipping...\n"); | 
 | 1095 |         return 0; | 
 | 1096 |     } | 
 | 1097 |      | 
| Koushik Dutta | bec0995 | 2010-12-19 20:37:57 -0800 | [diff] [blame] | 1098 |     ui_print("%s may be rfs. Checking...\n", path); | 
| Koushik Dutta | 4196e0f | 2010-12-19 03:38:29 -0800 | [diff] [blame] | 1099 |     char tmp[PATH_MAX]; | 
 | 1100 |     sprintf(tmp, "mount -t rfs %s %s", vol->device, path); | 
 | 1101 |     int ret = __system(tmp); | 
 | 1102 |     printf("%d\n", ret); | 
 | 1103 |     return ret == 0 ? 1 : 0; | 
 | 1104 | } | 
 | 1105 |  | 
 | 1106 | void process_volumes() { | 
 | 1107 |     create_fstab(); | 
| Koushik Dutta | 94caefe | 2011-06-10 09:17:30 -0700 | [diff] [blame^] | 1108 |     if (volume_for_path("/sdcard") == NULL) { | 
 | 1109 |         rmdir("/sdcard"); | 
 | 1110 |         symlink("/data/media", "/sdcard"); | 
 | 1111 |     } | 
 | 1112 |  | 
| Koushik Dutta | e8bc2c8 | 2011-02-27 14:00:19 -0800 | [diff] [blame] | 1113 |     return; | 
 | 1114 |  | 
 | 1115 |     // dead code. | 
| Koushik Dutta | 4196e0f | 2010-12-19 03:38:29 -0800 | [diff] [blame] | 1116 |     if (device_flash_type() != BML) | 
 | 1117 |         return; | 
 | 1118 |  | 
 | 1119 |     ui_print("Checking for ext4 partitions...\n"); | 
 | 1120 |     int ret = 0; | 
 | 1121 |     ret = bml_check_volume("/system"); | 
 | 1122 |     ret |= bml_check_volume("/data"); | 
 | 1123 |     if (has_datadata()) | 
 | 1124 |         ret |= bml_check_volume("/datadata"); | 
 | 1125 |     ret |= bml_check_volume("/cache"); | 
 | 1126 |      | 
 | 1127 |     if (ret == 0) { | 
 | 1128 |         ui_print("Done!\n"); | 
 | 1129 |         return; | 
 | 1130 |     } | 
 | 1131 |      | 
| Koushik Dutta | 745b5ff | 2010-12-21 00:58:43 -0800 | [diff] [blame] | 1132 |     char backup_path[PATH_MAX]; | 
 | 1133 |     time_t t = time(NULL); | 
 | 1134 |     char backup_name[PATH_MAX]; | 
 | 1135 |     struct timeval tp; | 
 | 1136 |     gettimeofday(&tp, NULL); | 
 | 1137 |     sprintf(backup_name, "before-ext4-convert-%d", tp.tv_sec); | 
 | 1138 |     sprintf(backup_path, "/sdcard/clockworkmod/backup/%s", backup_name); | 
 | 1139 |  | 
| Koushik Dutta | 4196e0f | 2010-12-19 03:38:29 -0800 | [diff] [blame] | 1140 |     ui_set_show_text(1); | 
 | 1141 |     ui_print("Filesystems need to be converted to ext4.\n"); | 
 | 1142 |     ui_print("A backup and restore will now take place.\n"); | 
| Koushik Dutta | bec0995 | 2010-12-19 20:37:57 -0800 | [diff] [blame] | 1143 |     ui_print("If anything goes wrong, your backup will be\n"); | 
| Koushik Dutta | 745b5ff | 2010-12-21 00:58:43 -0800 | [diff] [blame] | 1144 |     ui_print("named %s. Try restoring it\n", backup_name); | 
| Koushik Dutta | bec0995 | 2010-12-19 20:37:57 -0800 | [diff] [blame] | 1145 |     ui_print("in case of error.\n"); | 
| Koushik Dutta | 4196e0f | 2010-12-19 03:38:29 -0800 | [diff] [blame] | 1146 |  | 
| Koushik Dutta | 745b5ff | 2010-12-21 00:58:43 -0800 | [diff] [blame] | 1147 |     nandroid_backup(backup_path); | 
| Koushik Dutta | 5b7f34a | 2010-12-29 23:36:03 -0800 | [diff] [blame] | 1148 |     nandroid_restore(backup_path, 1, 1, 1, 1, 1, 0); | 
| Koushik Dutta | 4196e0f | 2010-12-19 03:38:29 -0800 | [diff] [blame] | 1149 |     ui_set_show_text(0); | 
 | 1150 | } | 
 | 1151 |  | 
| Koushik Dutta | 598cfc7 | 2010-06-20 09:42:47 -0700 | [diff] [blame] | 1152 | void handle_failure(int ret) | 
 | 1153 | { | 
 | 1154 |     if (ret == 0) | 
 | 1155 |         return; | 
| Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 1156 |     if (0 != ensure_path_mounted("/sdcard")) | 
| Koushik Dutta | 598cfc7 | 2010-06-20 09:42:47 -0700 | [diff] [blame] | 1157 |         return; | 
| Koushik Dutta | a6522b3 | 2010-06-20 13:16:06 -0700 | [diff] [blame] | 1158 |     mkdir("/sdcard/clockworkmod", S_IRWXU); | 
 | 1159 |     __system("cp /tmp/recovery.log /sdcard/clockworkmod/recovery.log"); | 
| Koushik Dutta | 38e8b2b | 2010-08-17 22:21:53 -0700 | [diff] [blame] | 1160 |     ui_print("/tmp/recovery.log was copied to /sdcard/clockworkmod/recovery.log. Please open ROM Manager to report the issue.\n"); | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 1161 | } | 
| Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 1162 |  | 
| Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 1163 | int is_path_mounted(const char* path) { | 
 | 1164 |     Volume* v = volume_for_path(path); | 
 | 1165 |     if (v == NULL) { | 
 | 1166 |         return 0; | 
 | 1167 |     } | 
 | 1168 |     if (strcmp(v->fs_type, "ramdisk") == 0) { | 
 | 1169 |         // the ramdisk is always mounted. | 
 | 1170 |         return 1; | 
 | 1171 |     } | 
 | 1172 |  | 
 | 1173 |     int result; | 
 | 1174 |     result = scan_mounted_volumes(); | 
 | 1175 |     if (result < 0) { | 
 | 1176 |         LOGE("failed to scan mounted volumes\n"); | 
 | 1177 |         return 0; | 
 | 1178 |     } | 
 | 1179 |  | 
 | 1180 |     const MountedVolume* mv = | 
 | 1181 |         find_mounted_volume_by_mount_point(v->mount_point); | 
 | 1182 |     if (mv) { | 
 | 1183 |         // volume is already mounted | 
 | 1184 |         return 1; | 
 | 1185 |     } | 
 | 1186 |     return 0; | 
 | 1187 | } | 
| Koushik Dutta | 5460f0c | 2010-12-18 22:37:49 -0800 | [diff] [blame] | 1188 |  | 
 | 1189 | int has_datadata() { | 
 | 1190 |     Volume *vol = volume_for_path("/datadata"); | 
 | 1191 |     return vol != NULL; | 
| Koushik Dutta | d8e21c3 | 2011-01-04 10:46:23 -0800 | [diff] [blame] | 1192 | } | 
 | 1193 |  | 
 | 1194 | int volume_main(int argc, char **argv) { | 
 | 1195 |     load_volume_table(); | 
 | 1196 |     return 0; | 
 | 1197 | } |