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