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 K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 39 | |
Koushik K. Dutta | 6060e5c | 2010-02-11 22:27:06 -0800 | [diff] [blame] | 40 | int signature_check_enabled = 1; |
| 41 | int script_assert_enabled = 1; |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 42 | static const char *SDCARD_UPDATE_FILE = "/sdcard/update.zip"; |
Koushik K. Dutta | 6060e5c | 2010-02-11 22:27:06 -0800 | [diff] [blame] | 43 | |
| 44 | void |
| 45 | toggle_signature_check() |
| 46 | { |
| 47 | signature_check_enabled = !signature_check_enabled; |
| 48 | ui_print("Signature Check: %s\n", signature_check_enabled ? "Enabled" : "Disabled"); |
| 49 | } |
| 50 | |
| 51 | void toggle_script_asserts() |
| 52 | { |
| 53 | script_assert_enabled = !script_assert_enabled; |
Koushik K. Dutta | e923487 | 2010-02-12 00:43:24 -0800 | [diff] [blame] | 54 | ui_print("Script Asserts: %s\n", script_assert_enabled ? "Enabled" : "Disabled"); |
| 55 | } |
| 56 | |
Koushik K. Dutta | ea46fe2 | 2010-03-08 02:58:04 -0800 | [diff] [blame] | 57 | int install_zip(const char* packagefilepath) |
Koushik K. Dutta | e923487 | 2010-02-12 00:43:24 -0800 | [diff] [blame] | 58 | { |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 59 | ui_print("\n-- Installing: %s\n", packagefilepath); |
Koushik Dutta | 5aaa823 | 2010-07-20 16:23:18 -0700 | [diff] [blame] | 60 | #ifndef BOARD_HAS_NO_MISC_PARTITION |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 61 | set_sdcard_update_bootloader_message(); |
Koushik Dutta | 5aaa823 | 2010-07-20 16:23:18 -0700 | [diff] [blame] | 62 | #endif |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 63 | int status = install_package(packagefilepath); |
Koushik K. Dutta | f3534d0 | 2010-04-18 18:06:24 -0700 | [diff] [blame] | 64 | ui_reset_progress(); |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 65 | if (status != INSTALL_SUCCESS) { |
| 66 | ui_set_background(BACKGROUND_ICON_ERROR); |
| 67 | ui_print("Installation aborted.\n"); |
Koushik K. Dutta | ea46fe2 | 2010-03-08 02:58:04 -0800 | [diff] [blame] | 68 | return 1; |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 69 | } |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 70 | ui_set_background(BACKGROUND_ICON_NONE); |
Koushik K. Dutta | 99fb6fe | 2010-03-03 00:42:58 -0800 | [diff] [blame] | 71 | ui_print("\nInstall from sdcard complete.\n"); |
Koushik K. Dutta | ea46fe2 | 2010-03-08 02:58:04 -0800 | [diff] [blame] | 72 | return 0; |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 75 | char* INSTALL_MENU_ITEMS[] = { "apply /sdcard/update.zip", |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 76 | "choose zip from sdcard", |
| 77 | "toggle signature verification", |
| 78 | "toggle script asserts", |
| 79 | NULL }; |
| 80 | #define ITEM_APPLY_SDCARD 0 |
| 81 | #define ITEM_CHOOSE_ZIP 1 |
| 82 | #define ITEM_SIG_CHECK 2 |
| 83 | #define ITEM_ASSERTS 3 |
| 84 | |
| 85 | void show_install_update_menu() |
| 86 | { |
| 87 | static char* headers[] = { "Apply update from .zip file on SD card", |
| 88 | "", |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 89 | NULL |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 90 | }; |
| 91 | for (;;) |
| 92 | { |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 93 | 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] | 94 | switch (chosen_item) |
| 95 | { |
| 96 | case ITEM_ASSERTS: |
| 97 | toggle_script_asserts(); |
| 98 | break; |
| 99 | case ITEM_SIG_CHECK: |
| 100 | toggle_signature_check(); |
| 101 | break; |
| 102 | case ITEM_APPLY_SDCARD: |
Koushik Dutta | d63eaef | 2010-07-14 21:01:21 -0700 | [diff] [blame] | 103 | { |
| 104 | if (confirm_selection("Confirm install?", "Yes - Install /sdcard/update.zip")) |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 105 | install_zip(SDCARD_UPDATE_FILE); |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 106 | break; |
Koushik Dutta | d63eaef | 2010-07-14 21:01:21 -0700 | [diff] [blame] | 107 | } |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 108 | case ITEM_CHOOSE_ZIP: |
| 109 | show_choose_zip_menu(); |
| 110 | break; |
| 111 | default: |
| 112 | return; |
| 113 | } |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 114 | |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
Koushik K. Dutta | 1d53c4e | 2010-04-02 16:46:21 -0700 | [diff] [blame] | 118 | void free_string_array(char** array) |
| 119 | { |
| 120 | if (array == NULL) |
| 121 | return; |
| 122 | char* cursor = array[0]; |
| 123 | int i = 0; |
| 124 | while (cursor != NULL) |
| 125 | { |
| 126 | free(cursor); |
| 127 | cursor = array[++i]; |
| 128 | } |
| 129 | free(array); |
| 130 | } |
| 131 | |
Koushik K. Dutta | 49f5689 | 2010-02-25 14:51:05 -0800 | [diff] [blame] | 132 | char** gather_files(const char* directory, const char* fileExtensionOrDirectory, int* numFiles) |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 133 | { |
Koushik K. Dutta | e923487 | 2010-02-12 00:43:24 -0800 | [diff] [blame] | 134 | char path[PATH_MAX] = ""; |
| 135 | DIR *dir; |
| 136 | struct dirent *de; |
| 137 | int total = 0; |
| 138 | int i; |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 139 | char** files = NULL; |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 140 | int pass; |
| 141 | *numFiles = 0; |
| 142 | int dirLen = strlen(directory); |
Koushik K. Dutta | e923487 | 2010-02-12 00:43:24 -0800 | [diff] [blame] | 143 | |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 144 | dir = opendir(directory); |
| 145 | if (dir == NULL) { |
| 146 | ui_print("Couldn't open directory.\n"); |
| 147 | return NULL; |
| 148 | } |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 149 | |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 150 | int extension_length = 0; |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 151 | if (fileExtensionOrDirectory != NULL) |
| 152 | extension_length = strlen(fileExtensionOrDirectory); |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 153 | |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 154 | int isCounting = 1; |
| 155 | i = 0; |
| 156 | for (pass = 0; pass < 2; pass++) { |
| 157 | while ((de=readdir(dir)) != NULL) { |
| 158 | // skip hidden files |
| 159 | if (de->d_name[0] == '.') |
| 160 | continue; |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 161 | |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 162 | // NULL means that we are gathering directories, so skip this |
| 163 | if (fileExtensionOrDirectory != NULL) |
| 164 | { |
| 165 | // make sure that we can have the desired extension (prevent seg fault) |
| 166 | if (strlen(de->d_name) < extension_length) |
| 167 | continue; |
| 168 | // compare the extension |
| 169 | if (strcmp(de->d_name + strlen(de->d_name) - extension_length, fileExtensionOrDirectory) != 0) |
| 170 | continue; |
| 171 | } |
| 172 | else |
| 173 | { |
| 174 | struct stat info; |
Koushik K. Dutta | 1d53c4e | 2010-04-02 16:46:21 -0700 | [diff] [blame] | 175 | char fullFileName[PATH_MAX]; |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 176 | strcpy(fullFileName, directory); |
| 177 | strcat(fullFileName, de->d_name); |
| 178 | stat(fullFileName, &info); |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 179 | // make sure it is a directory |
| 180 | if (!(S_ISDIR(info.st_mode))) |
| 181 | continue; |
| 182 | } |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 183 | |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 184 | if (pass == 0) |
| 185 | { |
| 186 | total++; |
| 187 | continue; |
| 188 | } |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 189 | |
Koushik K. Dutta | 49f5689 | 2010-02-25 14:51:05 -0800 | [diff] [blame] | 190 | files[i] = (char*) malloc(dirLen + strlen(de->d_name) + 2); |
| 191 | strcpy(files[i], directory); |
| 192 | strcat(files[i], de->d_name); |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 193 | if (fileExtensionOrDirectory == NULL) |
| 194 | strcat(files[i], "/"); |
| 195 | i++; |
| 196 | } |
| 197 | if (pass == 1) |
| 198 | break; |
| 199 | if (total == 0) |
| 200 | break; |
| 201 | rewinddir(dir); |
| 202 | *numFiles = total; |
| 203 | files = (char**) malloc((total+1)*sizeof(char*)); |
| 204 | files[total]=NULL; |
| 205 | } |
Koushik K. Dutta | 49f5689 | 2010-02-25 14:51:05 -0800 | [diff] [blame] | 206 | |
| 207 | if(closedir(dir) < 0) { |
| 208 | LOGE("Failed to close directory."); |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | if (total==0) { |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 212 | return NULL; |
| 213 | } |
| 214 | |
Koushik Dutta | 1e8aaba | 2010-07-01 00:23:25 -0700 | [diff] [blame] | 215 | // sort the result |
| 216 | if (files != NULL) { |
| 217 | for (i = 0; i < total; i++) { |
| 218 | int curMax = -1; |
| 219 | int j; |
| 220 | for (j = 0; j < total - i; j++) { |
| 221 | if (curMax == -1 || strcmp(files[curMax], files[j]) < 0) |
| 222 | curMax = j; |
| 223 | } |
| 224 | char* temp = files[curMax]; |
| 225 | files[curMax] = files[total - i - 1]; |
| 226 | files[total - i - 1] = temp; |
| 227 | } |
| 228 | } |
| 229 | |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 230 | return files; |
Koushik K. Dutta | 49f5689 | 2010-02-25 14:51:05 -0800 | [diff] [blame] | 231 | } |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 232 | |
Koushik K. Dutta | 49f5689 | 2010-02-25 14:51:05 -0800 | [diff] [blame] | 233 | // pass in NULL for fileExtensionOrDirectory and you will get a directory chooser |
| 234 | char* choose_file_menu(const char* directory, const char* fileExtensionOrDirectory, const char* headers[]) |
| 235 | { |
| 236 | char path[PATH_MAX] = ""; |
| 237 | DIR *dir; |
| 238 | struct dirent *de; |
| 239 | int numFiles = 0; |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 240 | int numDirs = 0; |
Koushik K. Dutta | 49f5689 | 2010-02-25 14:51:05 -0800 | [diff] [blame] | 241 | int i; |
Koushik K. Dutta | 1d53c4e | 2010-04-02 16:46:21 -0700 | [diff] [blame] | 242 | char* return_value = NULL; |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 243 | int dir_len = strlen(directory); |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 244 | |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 245 | char** files = gather_files(directory, fileExtensionOrDirectory, &numFiles); |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 246 | char** dirs = NULL; |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 247 | if (fileExtensionOrDirectory != NULL) |
| 248 | dirs = gather_files(directory, NULL, &numDirs); |
| 249 | int total = numDirs + numFiles; |
| 250 | if (total == 0) |
| 251 | { |
| 252 | ui_print("No files found.\n"); |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 253 | } |
Koushik K. Dutta | 1d53c4e | 2010-04-02 16:46:21 -0700 | [diff] [blame] | 254 | else |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 255 | { |
Koushik K. Dutta | 1d53c4e | 2010-04-02 16:46:21 -0700 | [diff] [blame] | 256 | char** list = (char**) malloc((total + 1) * sizeof(char*)); |
| 257 | list[total] = NULL; |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 258 | |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 259 | |
Koushik K. Dutta | 1d53c4e | 2010-04-02 16:46:21 -0700 | [diff] [blame] | 260 | for (i = 0 ; i < numDirs; i++) |
Koushik K. Dutta | 001c5b5 | 2010-02-25 14:53:57 -0800 | [diff] [blame] | 261 | { |
Koushik K. Dutta | 1d53c4e | 2010-04-02 16:46:21 -0700 | [diff] [blame] | 262 | list[i] = strdup(dirs[i] + dir_len); |
| 263 | } |
| 264 | |
| 265 | for (i = 0 ; i < numFiles; i++) |
| 266 | { |
| 267 | list[numDirs + i] = strdup(files[i] + dir_len); |
| 268 | } |
| 269 | |
| 270 | for (;;) |
| 271 | { |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 272 | int chosen_item = get_menu_selection(headers, list, 0, 0); |
Koushik K. Dutta | 1d53c4e | 2010-04-02 16:46:21 -0700 | [diff] [blame] | 273 | if (chosen_item == GO_BACK) |
| 274 | break; |
| 275 | static char ret[PATH_MAX]; |
| 276 | if (chosen_item < numDirs) |
| 277 | { |
| 278 | char* subret = choose_file_menu(dirs[chosen_item], fileExtensionOrDirectory, headers); |
| 279 | if (subret != NULL) |
| 280 | { |
| 281 | strcpy(ret, subret); |
| 282 | return_value = ret; |
| 283 | break; |
| 284 | } |
| 285 | continue; |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 286 | } |
Koushik K. Dutta | 1d53c4e | 2010-04-02 16:46:21 -0700 | [diff] [blame] | 287 | strcpy(ret, files[chosen_item - numDirs]); |
| 288 | return_value = ret; |
| 289 | break; |
| 290 | } |
| 291 | free_string_array(list); |
Koushik K. Dutta | 981b0cd | 2010-02-22 08:53:34 -0800 | [diff] [blame] | 292 | } |
Koushik K. Dutta | 1d53c4e | 2010-04-02 16:46:21 -0700 | [diff] [blame] | 293 | |
| 294 | free_string_array(files); |
| 295 | free_string_array(dirs); |
| 296 | return return_value; |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | void show_choose_zip_menu() |
| 300 | { |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 301 | if (ensure_path_mounted("/sdcard") != 0) { |
Koushik K. Dutta | e923487 | 2010-02-12 00:43:24 -0800 | [diff] [blame] | 302 | LOGE ("Can't mount /sdcard\n"); |
| 303 | return; |
| 304 | } |
| 305 | |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 306 | static char* headers[] = { "Choose a zip to apply", |
| 307 | "", |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 308 | NULL |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 309 | }; |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 310 | |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 311 | char* file = choose_file_menu("/sdcard/", ".zip", headers); |
| 312 | if (file == NULL) |
| 313 | return; |
Koushik Dutta | d63eaef | 2010-07-14 21:01:21 -0700 | [diff] [blame] | 314 | static char* confirm_install = "Confirm install?"; |
| 315 | static char confirm[PATH_MAX]; |
| 316 | sprintf(confirm, "Yes - Install %s", basename(file)); |
| 317 | if (confirm_selection(confirm_install, confirm)) |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 318 | install_zip(file); |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 319 | } |
| 320 | |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 321 | void show_nandroid_restore_menu() |
| 322 | { |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 323 | if (ensure_path_mounted("/sdcard") != 0) { |
Koushik K. Dutta | 54305a8 | 2010-03-12 17:43:26 -0800 | [diff] [blame] | 324 | LOGE ("Can't mount /sdcard\n"); |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 325 | return; |
Koushik K. Dutta | 54305a8 | 2010-03-12 17:43:26 -0800 | [diff] [blame] | 326 | } |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 327 | |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 328 | static char* headers[] = { "Choose an image to restore", |
| 329 | "", |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 330 | NULL |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 331 | }; |
Koushik K. Dutta | e923487 | 2010-02-12 00:43:24 -0800 | [diff] [blame] | 332 | |
Koushik K. Dutta | 49f5689 | 2010-02-25 14:51:05 -0800 | [diff] [blame] | 333 | char* file = choose_file_menu("/sdcard/clockworkmod/backup/", NULL, headers); |
Koushik K. Dutta | bcdd003 | 2010-02-21 21:10:25 -0800 | [diff] [blame] | 334 | if (file == NULL) |
| 335 | return; |
Koushik Dutta | d63eaef | 2010-07-14 21:01:21 -0700 | [diff] [blame] | 336 | |
| 337 | if (confirm_selection("Confirm restore?", "Yes - Restore")) |
| 338 | nandroid_restore(file, 1, 1, 1, 1, 1); |
Koushik K. Dutta | 0317378 | 2010-02-26 14:14:23 -0800 | [diff] [blame] | 339 | } |
| 340 | |
Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 341 | void show_mount_usb_storage_menu() |
Koushik K. Dutta | 0317378 | 2010-02-26 14:14:23 -0800 | [diff] [blame] | 342 | { |
Koushik Dutta | 598cfc7 | 2010-06-20 09:42:47 -0700 | [diff] [blame] | 343 | char command[PATH_MAX]; |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 344 | Volume *vol = volume_for_path("/sdcard"); |
| 345 | sprintf(command, "echo %s > /sys/devices/platform/usb_mass_storage/lun0/file", vol->device); |
Koushik Dutta | 598cfc7 | 2010-06-20 09:42:47 -0700 | [diff] [blame] | 346 | __system(command); |
Koushik K. Dutta | 0317378 | 2010-02-26 14:14:23 -0800 | [diff] [blame] | 347 | static char* headers[] = { "USB Mass Storage device", |
| 348 | "Leaving this menu unmount", |
| 349 | "your SD card from your PC.", |
| 350 | "", |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 351 | NULL |
Koushik K. Dutta | 0317378 | 2010-02-26 14:14:23 -0800 | [diff] [blame] | 352 | }; |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 353 | |
Koushik K. Dutta | 0317378 | 2010-02-26 14:14:23 -0800 | [diff] [blame] | 354 | static char* list[] = { "Unmount", NULL }; |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 355 | |
Koushik K. Dutta | 0317378 | 2010-02-26 14:14:23 -0800 | [diff] [blame] | 356 | for (;;) |
| 357 | { |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 358 | int chosen_item = get_menu_selection(headers, list, 0, 0); |
Koushik K. Dutta | 0317378 | 2010-02-26 14:14:23 -0800 | [diff] [blame] | 359 | if (chosen_item == GO_BACK || chosen_item == 0) |
| 360 | break; |
| 361 | } |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 362 | |
Koushik K. Dutta | f721594 | 2010-03-16 13:34:51 -0700 | [diff] [blame] | 363 | __system("echo '' > /sys/devices/platform/usb_mass_storage/lun0/file"); |
| 364 | __system("echo 0 > /sys/devices/platform/usb_mass_storage/lun0/enable"); |
Koushik K. Dutta | e81cb75 | 2010-02-26 17:44:33 -0800 | [diff] [blame] | 365 | } |
Koushik K. Dutta | f68aaaf | 2010-03-07 13:39:21 -0800 | [diff] [blame] | 366 | |
Koushik Dutta | d63eaef | 2010-07-14 21:01:21 -0700 | [diff] [blame] | 367 | int confirm_selection(const char* title, const char* confirm) |
Koushik K. Dutta | 16f0b49 | 2010-03-19 14:37:11 -0700 | [diff] [blame] | 368 | { |
Koushik Dutta | ceddcd5 | 2010-08-23 16:13:14 -0700 | [diff] [blame] | 369 | struct stat info; |
| 370 | if (0 == stat("/sdcard/clockworkmod/.no_confirm", &info)) |
| 371 | return 1; |
| 372 | |
Koushik Dutta | d63eaef | 2010-07-14 21:01:21 -0700 | [diff] [blame] | 373 | char* confirm_headers[] = { title, " THIS CAN NOT BE UNDONE.", "", NULL }; |
| 374 | char* items[] = { "No", |
| 375 | "No", |
| 376 | "No", |
| 377 | "No", |
| 378 | "No", |
| 379 | "No", |
| 380 | "No", |
Koushik Dutta | ecd32fa | 2010-07-14 18:38:02 -0700 | [diff] [blame] | 381 | confirm, //" Yes -- wipe partition", // [7 |
Koushik Dutta | d63eaef | 2010-07-14 21:01:21 -0700 | [diff] [blame] | 382 | "No", |
| 383 | "No", |
| 384 | "No", |
Koushik K. Dutta | 16f0b49 | 2010-03-19 14:37:11 -0700 | [diff] [blame] | 385 | NULL }; |
| 386 | |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 387 | int chosen_item = get_menu_selection(confirm_headers, items, 0, 0); |
Koushik K. Dutta | 16f0b49 | 2010-03-19 14:37:11 -0700 | [diff] [blame] | 388 | return chosen_item == 7; |
| 389 | } |
| 390 | |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 391 | int format_unknown_device(const char* path) |
Koushik K. Dutta | 3f99539 | 2010-03-30 23:29:43 -0700 | [diff] [blame] | 392 | { |
Koushik K. Dutta | f3534d0 | 2010-04-18 18:06:24 -0700 | [diff] [blame] | 393 | // if this is SDEXT:, don't worry about it. |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 394 | if (0 == strcmp(path, "/sd-ext")) |
Koushik K. Dutta | f3534d0 | 2010-04-18 18:06:24 -0700 | [diff] [blame] | 395 | { |
| 396 | struct stat st; |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 397 | Volume *vol = volume_for_path("/sd-ext"); |
| 398 | if (vol == NULL || 0 != stat(vol->device, &st)) |
Koushik K. Dutta | f3534d0 | 2010-04-18 18:06:24 -0700 | [diff] [blame] | 399 | { |
| 400 | ui_print("No app2sd partition found. Skipping format of /sd-ext.\n"); |
| 401 | return 0; |
| 402 | } |
| 403 | } |
Koushik Dutta | 2f73e58 | 2010-04-18 16:00:21 -0700 | [diff] [blame] | 404 | |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 405 | if (0 != ensure_path_mounted(path)) |
Koushik K. Dutta | 3f99539 | 2010-03-30 23:29:43 -0700 | [diff] [blame] | 406 | { |
| 407 | ui_print("Error mounting %s!\n", path); |
Koushik Dutta | cd44ab9 | 2010-06-23 00:02:14 -0700 | [diff] [blame] | 408 | ui_print("Skipping format...\n"); |
| 409 | return 0; |
Koushik K. Dutta | 3f99539 | 2010-03-30 23:29:43 -0700 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | static char tmp[PATH_MAX]; |
| 413 | sprintf(tmp, "rm -rf %s/*", path); |
| 414 | __system(tmp); |
| 415 | sprintf(tmp, "rm -rf %s/.*", path); |
| 416 | __system(tmp); |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 417 | |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 418 | ensure_path_unmounted(path); |
Koushik K. Dutta | 3f99539 | 2010-03-30 23:29:43 -0700 | [diff] [blame] | 419 | return 0; |
| 420 | } |
| 421 | |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 422 | //#define MOUNTABLE_COUNT 5 |
| 423 | //#define DEVICE_COUNT 4 |
| 424 | //#define MMC_COUNT 2 |
Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 425 | |
Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame] | 426 | void show_partition_menu() |
Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 427 | { |
Koushik Dutta | ceddcd5 | 2010-08-23 16:13:14 -0700 | [diff] [blame] | 428 | static char* headers[] = { "Mounts and Storage Menu", |
Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 429 | "", |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 430 | NULL |
Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 431 | }; |
| 432 | |
| 433 | typedef char* string; |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 434 | string mounts[][3] = { |
| 435 | { "mount /system", "unmount /system", "/system" }, |
| 436 | { "mount /data", "unmount /data", "/data" }, |
| 437 | { "mount /cache", "unmount /cache", "/cache" }, |
| 438 | { "mount /sdcard", "unmount /sdcard", "/sdcard:" }, |
Brint E. Kriebel | d352233 | 2010-12-11 20:52:06 -0700 | [diff] [blame] | 439 | #ifdef BOARD_HAS_SDCARD_INTERNAL |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 440 | { "mount /emmc", "unmount /emmc", "/emmc" }, |
Brint E. Kriebel | d352233 | 2010-12-11 20:52:06 -0700 | [diff] [blame] | 441 | #endif |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 442 | { "mount /sd-ext", "unmount /sd-ext", "/sd-ext" } |
Koushik K. Dutta | 3f99539 | 2010-03-30 23:29:43 -0700 | [diff] [blame] | 443 | }; |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 444 | |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 445 | string devices[][2] = { |
| 446 | { "format boot", "boot:" }, |
| 447 | { "format system", "/system" }, |
| 448 | { "format data", "/data" }, |
| 449 | { "format cache", "/cache" }, |
| 450 | { "format sdcard", "/sdcard" }, |
| 451 | { "format sd-ext", "/sd-ext" }, |
Brint E. Kriebel | d352233 | 2010-12-11 20:52:06 -0700 | [diff] [blame] | 452 | #ifdef BOARD_HAS_SDCARD_INTERNAL |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 453 | { "format internal sdcard", "/emmc" } |
Brint E. Kriebel | d352233 | 2010-12-11 20:52:06 -0700 | [diff] [blame] | 454 | #endif |
Koushik K. Dutta | 16f0b49 | 2010-03-19 14:37:11 -0700 | [diff] [blame] | 455 | }; |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 456 | |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 457 | const int MOUNTABLE_COUNT = sizeof(mounts) / sizeof(string) / 3; |
| 458 | const int DEVICE_COUNT = sizeof(devices) / sizeof(string) / 2; |
| 459 | |
Koushik Dutta | d63eaef | 2010-07-14 21:01:21 -0700 | [diff] [blame] | 460 | static char* confirm_format = "Confirm format?"; |
Koushik Dutta | ecd32fa | 2010-07-14 18:38:02 -0700 | [diff] [blame] | 461 | static char* confirm = "Yes - Format"; |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 462 | |
Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 463 | for (;;) |
| 464 | { |
Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame] | 465 | int ismounted[MOUNTABLE_COUNT]; |
Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 466 | int i; |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 467 | static string options[MOUNTABLE_COUNT + DEVICE_COUNT + 1 + 1]; // mountables, format mtds, format mmcs, usb storage, null |
Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame] | 468 | for (i = 0; i < MOUNTABLE_COUNT; i++) |
Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 469 | { |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 470 | ismounted[i] = is_path_mounted(mounts[i][2]); |
Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 471 | options[i] = ismounted[i] ? mounts[i][1] : mounts[i][0]; |
| 472 | } |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 473 | |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 474 | for (i = 0; i < DEVICE_COUNT; i++) |
Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame] | 475 | { |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 476 | options[DEVICE_COUNT + i] = devices[i][0]; |
Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame] | 477 | } |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 478 | |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 479 | options[MOUNTABLE_COUNT + DEVICE_COUNT] = "mount USB storage"; |
| 480 | options[MOUNTABLE_COUNT + DEVICE_COUNT + 1] = NULL; |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 481 | |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 482 | int chosen_item = get_menu_selection(headers, options, 0, 0); |
Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 483 | if (chosen_item == GO_BACK) |
| 484 | break; |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 485 | if (chosen_item == MOUNTABLE_COUNT + DEVICE_COUNT) |
Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 486 | { |
| 487 | show_mount_usb_storage_menu(); |
| 488 | } |
Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame] | 489 | else if (chosen_item < MOUNTABLE_COUNT) |
Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 490 | { |
| 491 | if (ismounted[chosen_item]) |
| 492 | { |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 493 | if (0 != ensure_path_unmounted(mounts[chosen_item][2])) |
Koushik K. Dutta | 16f0b49 | 2010-03-19 14:37:11 -0700 | [diff] [blame] | 494 | ui_print("Error unmounting %s!\n", mounts[chosen_item][2]); |
Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 495 | } |
| 496 | else |
| 497 | { |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 498 | if (0 != ensure_path_mounted(mounts[chosen_item][2])) |
Koushik K. Dutta | 16f0b49 | 2010-03-19 14:37:11 -0700 | [diff] [blame] | 499 | ui_print("Error mounting %s!\n", mounts[chosen_item][2]); |
Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 500 | } |
| 501 | } |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 502 | else if (chosen_item < MOUNTABLE_COUNT + DEVICE_COUNT) |
Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame] | 503 | { |
| 504 | chosen_item = chosen_item - MOUNTABLE_COUNT; |
Koushik Dutta | ecd32fa | 2010-07-14 18:38:02 -0700 | [diff] [blame] | 505 | if (!confirm_selection(confirm_format, confirm)) |
Koushik K. Dutta | 16f0b49 | 2010-03-19 14:37:11 -0700 | [diff] [blame] | 506 | continue; |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 507 | ui_print("Formatting %s...\n", devices[chosen_item][1]); |
| 508 | if (0 != format_device(devices[chosen_item][1])) |
| 509 | ui_print("Error formatting %s!\n", devices[chosen_item][1]); |
Koushik K. Dutta | 3f99539 | 2010-03-30 23:29:43 -0700 | [diff] [blame] | 510 | else |
| 511 | ui_print("Done.\n"); |
Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame] | 512 | } |
Koushik K. Dutta | b9546a8 | 2010-03-14 22:42:30 -0700 | [diff] [blame] | 513 | } |
| 514 | } |
| 515 | |
Koushik K. Dutta | 72a1db6 | 2010-03-07 14:10:26 -0800 | [diff] [blame] | 516 | #define EXTENDEDCOMMAND_SCRIPT "/cache/recovery/extendedcommand" |
| 517 | |
| 518 | int extendedcommand_file_exists() |
Koushik K. Dutta | f68aaaf | 2010-03-07 13:39:21 -0800 | [diff] [blame] | 519 | { |
Koushik K. Dutta | f68aaaf | 2010-03-07 13:39:21 -0800 | [diff] [blame] | 520 | struct stat file_info; |
Koushik K. Dutta | 72a1db6 | 2010-03-07 14:10:26 -0800 | [diff] [blame] | 521 | return 0 == stat(EXTENDEDCOMMAND_SCRIPT, &file_info); |
| 522 | } |
| 523 | |
Koushik K. Dutta | 3836f72 | 2010-03-11 22:17:43 -0800 | [diff] [blame] | 524 | int run_script_from_buffer(char* script_data, int script_len, char* filename) |
Koushik K. Dutta | 72a1db6 | 2010-03-07 14:10:26 -0800 | [diff] [blame] | 525 | { |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 526 | ui_print("not yet implemented.\n"); |
| 527 | return -1; |
| 528 | |
| 529 | /* |
Koushik K. Dutta | f68aaaf | 2010-03-07 13:39:21 -0800 | [diff] [blame] | 530 | const AmCommandList *commands = parseAmendScript(script_data, script_len); |
| 531 | if (commands == NULL) { |
| 532 | printf("Syntax error in update script\n"); |
| 533 | return 1; |
| 534 | } else { |
Koushik K. Dutta | 72a1db6 | 2010-03-07 14:10:26 -0800 | [diff] [blame] | 535 | printf("Parsed %.*s\n", script_len, filename); |
Koushik K. Dutta | f68aaaf | 2010-03-07 13:39:21 -0800 | [diff] [blame] | 536 | } |
| 537 | |
Koushik K. Dutta | f68aaaf | 2010-03-07 13:39:21 -0800 | [diff] [blame] | 538 | int ret = execCommandList((ExecContext *)1, commands); |
| 539 | if (ret != 0) { |
| 540 | int num = ret; |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 541 | char *line = NULL, *next = script_data; |
Koushik K. Dutta | f68aaaf | 2010-03-07 13:39:21 -0800 | [diff] [blame] | 542 | while (next != NULL && ret-- > 0) { |
| 543 | line = next; |
| 544 | next = memchr(line, '\n', script_data + script_len - line); |
| 545 | if (next != NULL) *next++ = '\0'; |
| 546 | } |
| 547 | printf("Failure at line %d:\n%s\n", num, next ? line : "(not found)"); |
| 548 | return 1; |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 549 | } |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 550 | */ |
Koushik K. Dutta | f68aaaf | 2010-03-07 13:39:21 -0800 | [diff] [blame] | 551 | } |
Koushik K. Dutta | 72a1db6 | 2010-03-07 14:10:26 -0800 | [diff] [blame] | 552 | |
Koushik K. Dutta | 6771aca | 2010-04-03 23:28:39 -0700 | [diff] [blame] | 553 | int run_script(char* filename) |
Koushik K. Dutta | 3836f72 | 2010-03-11 22:17:43 -0800 | [diff] [blame] | 554 | { |
| 555 | struct stat file_info; |
| 556 | if (0 != stat(filename, &file_info)) { |
| 557 | printf("Error executing stat on file: %s\n", filename); |
| 558 | return 1; |
| 559 | } |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 560 | |
Koushik K. Dutta | 3836f72 | 2010-03-11 22:17:43 -0800 | [diff] [blame] | 561 | int script_len = file_info.st_size; |
Koushik K. Dutta | 707fa6d | 2010-03-23 11:44:33 -0700 | [diff] [blame] | 562 | char* script_data = (char*)malloc(script_len + 1); |
Koushik K. Dutta | 3836f72 | 2010-03-11 22:17:43 -0800 | [diff] [blame] | 563 | FILE *file = fopen(filename, "rb"); |
| 564 | fread(script_data, script_len, 1, file); |
Koushik K. Dutta | 707fa6d | 2010-03-23 11:44:33 -0700 | [diff] [blame] | 565 | // supposedly not necessary, but let's be safe. |
| 566 | script_data[script_len] = '\0'; |
Koushik K. Dutta | 3836f72 | 2010-03-11 22:17:43 -0800 | [diff] [blame] | 567 | fclose(file); |
Koushik Dutta | e25908b | 2010-06-21 08:16:19 -0700 | [diff] [blame] | 568 | LOGI("Running script:\n"); |
| 569 | LOGI("\n%s\n", script_data); |
Koushik K. Dutta | 3836f72 | 2010-03-11 22:17:43 -0800 | [diff] [blame] | 570 | |
Koushik K. Dutta | f3534d0 | 2010-04-18 18:06:24 -0700 | [diff] [blame] | 571 | int ret = run_script_from_buffer(script_data, script_len, filename); |
Koushik K. Dutta | 1d53c4e | 2010-04-02 16:46:21 -0700 | [diff] [blame] | 572 | free(script_data); |
| 573 | return ret; |
Koushik K. Dutta | 3836f72 | 2010-03-11 22:17:43 -0800 | [diff] [blame] | 574 | } |
| 575 | |
Koushik K. Dutta | 72a1db6 | 2010-03-07 14:10:26 -0800 | [diff] [blame] | 576 | int run_and_remove_extendedcommand() |
| 577 | { |
Koushik K. Dutta | 6771aca | 2010-04-03 23:28:39 -0700 | [diff] [blame] | 578 | char tmp[PATH_MAX]; |
| 579 | sprintf(tmp, "cp %s /tmp/%s", EXTENDEDCOMMAND_SCRIPT, basename(EXTENDEDCOMMAND_SCRIPT)); |
| 580 | __system(tmp); |
| 581 | remove(EXTENDEDCOMMAND_SCRIPT); |
Koushik K. Dutta | 3a25cf5 | 2010-03-08 19:22:41 -0800 | [diff] [blame] | 582 | int i = 0; |
| 583 | for (i = 20; i > 0; i--) { |
| 584 | ui_print("Waiting for SD Card to mount (%ds)\n", i); |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 585 | if (ensure_path_mounted("/sdcard") == 0) { |
Koushik K. Dutta | 3a25cf5 | 2010-03-08 19:22:41 -0800 | [diff] [blame] | 586 | ui_print("SD Card mounted...\n"); |
| 587 | break; |
| 588 | } |
| 589 | sleep(1); |
| 590 | } |
Koushik Dutta | 92077c1 | 2010-07-15 00:10:08 -0700 | [diff] [blame] | 591 | remove("/sdcard/clockworkmod/.recoverycheckpoint"); |
Koushik K. Dutta | 3a25cf5 | 2010-03-08 19:22:41 -0800 | [diff] [blame] | 592 | if (i == 0) { |
| 593 | ui_print("Timed out waiting for SD card... continuing anyways."); |
| 594 | } |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 595 | |
Koushik K. Dutta | 6771aca | 2010-04-03 23:28:39 -0700 | [diff] [blame] | 596 | sprintf(tmp, "/tmp/%s", basename(EXTENDEDCOMMAND_SCRIPT)); |
| 597 | return run_script(tmp); |
Koushik K. Dutta | 72a1db6 | 2010-03-07 14:10:26 -0800 | [diff] [blame] | 598 | } |
| 599 | |
Koushik K. Dutta | fe84a7f | 2010-03-25 18:19:23 -0700 | [diff] [blame] | 600 | void show_nandroid_advanced_restore_menu() |
| 601 | { |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 602 | if (ensure_path_mounted("/sdcard") != 0) { |
Koushik K. Dutta | fe84a7f | 2010-03-25 18:19:23 -0700 | [diff] [blame] | 603 | LOGE ("Can't mount /sdcard\n"); |
| 604 | return; |
| 605 | } |
| 606 | |
| 607 | static char* advancedheaders[] = { "Choose an image to restore", |
| 608 | "", |
| 609 | "Choose an image to restore", |
| 610 | "first. The next menu will", |
| 611 | "you more options.", |
| 612 | "", |
| 613 | NULL |
| 614 | }; |
| 615 | |
| 616 | char* file = choose_file_menu("/sdcard/clockworkmod/backup/", NULL, advancedheaders); |
| 617 | if (file == NULL) |
| 618 | return; |
| 619 | |
| 620 | static char* headers[] = { "Nandroid Advanced Restore", |
| 621 | "", |
| 622 | NULL |
| 623 | }; |
| 624 | |
| 625 | static char* list[] = { "Restore boot", |
| 626 | "Restore system", |
| 627 | "Restore data", |
| 628 | "Restore cache", |
Koushik K. Dutta | 68b0190 | 2010-04-01 12:20:39 -0700 | [diff] [blame] | 629 | "Restore sd-ext", |
Koushik K. Dutta | fe84a7f | 2010-03-25 18:19:23 -0700 | [diff] [blame] | 630 | NULL |
| 631 | }; |
| 632 | |
Koushik Dutta | d63eaef | 2010-07-14 21:01:21 -0700 | [diff] [blame] | 633 | |
| 634 | static char* confirm_restore = "Confirm restore?"; |
| 635 | |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 636 | int chosen_item = get_menu_selection(headers, list, 0, 0); |
Koushik K. Dutta | fe84a7f | 2010-03-25 18:19:23 -0700 | [diff] [blame] | 637 | switch (chosen_item) |
| 638 | { |
| 639 | case 0: |
Koushik Dutta | d63eaef | 2010-07-14 21:01:21 -0700 | [diff] [blame] | 640 | if (confirm_selection(confirm_restore, "Yes - Restore boot")) |
| 641 | nandroid_restore(file, 1, 0, 0, 0, 0); |
Koushik K. Dutta | fe84a7f | 2010-03-25 18:19:23 -0700 | [diff] [blame] | 642 | break; |
| 643 | case 1: |
Koushik Dutta | d63eaef | 2010-07-14 21:01:21 -0700 | [diff] [blame] | 644 | if (confirm_selection(confirm_restore, "Yes - Restore system")) |
| 645 | nandroid_restore(file, 0, 1, 0, 0, 0); |
Koushik K. Dutta | fe84a7f | 2010-03-25 18:19:23 -0700 | [diff] [blame] | 646 | break; |
| 647 | case 2: |
Koushik Dutta | d63eaef | 2010-07-14 21:01:21 -0700 | [diff] [blame] | 648 | if (confirm_selection(confirm_restore, "Yes - Restore data")) |
| 649 | nandroid_restore(file, 0, 0, 1, 0, 0); |
Koushik K. Dutta | fe84a7f | 2010-03-25 18:19:23 -0700 | [diff] [blame] | 650 | break; |
| 651 | case 3: |
Koushik Dutta | d63eaef | 2010-07-14 21:01:21 -0700 | [diff] [blame] | 652 | if (confirm_selection(confirm_restore, "Yes - Restore cache")) |
| 653 | nandroid_restore(file, 0, 0, 0, 1, 0); |
Koushik K. Dutta | fe84a7f | 2010-03-25 18:19:23 -0700 | [diff] [blame] | 654 | break; |
Koushik Dutta | 2f73e58 | 2010-04-18 16:00:21 -0700 | [diff] [blame] | 655 | case 4: |
Koushik Dutta | d63eaef | 2010-07-14 21:01:21 -0700 | [diff] [blame] | 656 | if (confirm_selection(confirm_restore, "Yes - Restore sd-ext")) |
| 657 | nandroid_restore(file, 0, 0, 0, 0, 1); |
Koushik Dutta | 2f73e58 | 2010-04-18 16:00:21 -0700 | [diff] [blame] | 658 | break; |
Koushik K. Dutta | fe84a7f | 2010-03-25 18:19:23 -0700 | [diff] [blame] | 659 | } |
| 660 | } |
| 661 | |
Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame] | 662 | void show_nandroid_menu() |
| 663 | { |
| 664 | static char* headers[] = { "Nandroid", |
| 665 | "", |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 666 | NULL |
Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame] | 667 | }; |
| 668 | |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 669 | static char* list[] = { "Backup", |
Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame] | 670 | "Restore", |
Koushik K. Dutta | fe84a7f | 2010-03-25 18:19:23 -0700 | [diff] [blame] | 671 | "Advanced Restore", |
Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame] | 672 | NULL |
| 673 | }; |
| 674 | |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 675 | int chosen_item = get_menu_selection(headers, list, 0, 0); |
Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame] | 676 | switch (chosen_item) |
| 677 | { |
| 678 | case 0: |
| 679 | { |
Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame] | 680 | char backup_path[PATH_MAX]; |
Koushik K. Dutta | 6a26e7c | 2010-03-27 15:26:11 -0700 | [diff] [blame] | 681 | time_t t = time(NULL); |
| 682 | struct tm *tmp = localtime(&t); |
| 683 | if (tmp == NULL) |
| 684 | { |
| 685 | struct timeval tp; |
| 686 | gettimeofday(&tp, NULL); |
| 687 | sprintf(backup_path, "/sdcard/clockworkmod/backup/%d", tp.tv_sec); |
| 688 | } |
| 689 | else |
| 690 | { |
| 691 | strftime(backup_path, sizeof(backup_path), "/sdcard/clockworkmod/backup/%F.%H.%M.%S", tmp); |
| 692 | } |
Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame] | 693 | nandroid_backup(backup_path); |
| 694 | } |
| 695 | break; |
| 696 | case 1: |
| 697 | show_nandroid_restore_menu(); |
| 698 | break; |
Koushik K. Dutta | fe84a7f | 2010-03-25 18:19:23 -0700 | [diff] [blame] | 699 | case 2: |
| 700 | show_nandroid_advanced_restore_menu(); |
| 701 | break; |
Koushik K. Dutta | 5899ac9 | 2010-03-19 13:34:36 -0700 | [diff] [blame] | 702 | } |
Koushik K. Dutta | a496b51 | 2010-03-19 14:51:45 -0700 | [diff] [blame] | 703 | } |
| 704 | |
Koushik Dutta | fd1579b | 2010-05-01 12:46:55 -0700 | [diff] [blame] | 705 | void wipe_battery_stats() |
| 706 | { |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 707 | ensure_path_mounted("/data"); |
Koushik Dutta | fd1579b | 2010-05-01 12:46:55 -0700 | [diff] [blame] | 708 | remove("/data/system/batterystats.bin"); |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 709 | ensure_path_unmounted("/data"); |
Koushik Dutta | fd1579b | 2010-05-01 12:46:55 -0700 | [diff] [blame] | 710 | } |
| 711 | |
Koushik K. Dutta | a496b51 | 2010-03-19 14:51:45 -0700 | [diff] [blame] | 712 | void show_advanced_menu() |
| 713 | { |
| 714 | static char* headers[] = { "Advanced and Debugging Menu", |
| 715 | "", |
| 716 | NULL |
| 717 | }; |
| 718 | |
Koushik K. Dutta | 7fe4d7b | 2010-04-06 23:04:52 -0700 | [diff] [blame] | 719 | static char* list[] = { "Reboot Recovery", |
Koushik Dutta | 49af23c | 2010-06-21 13:45:51 -0700 | [diff] [blame] | 720 | "Wipe Dalvik Cache", |
Koushik Dutta | fd1579b | 2010-05-01 12:46:55 -0700 | [diff] [blame] | 721 | "Wipe Battery Stats", |
Koushik Dutta | 598cfc7 | 2010-06-20 09:42:47 -0700 | [diff] [blame] | 722 | "Report Error", |
Koushik K. Dutta | 7fe4d7b | 2010-04-06 23:04:52 -0700 | [diff] [blame] | 723 | "Key Test", |
Koushik Dutta | e17a78d | 2010-08-29 12:35:10 -0700 | [diff] [blame] | 724 | #ifndef BOARD_HAS_SMALL_RECOVERY |
Koushik Dutta | ceddcd5 | 2010-08-23 16:13:14 -0700 | [diff] [blame] | 725 | "Partition SD Card", |
| 726 | "Fix Permissions", |
Brint E. Kriebel | d352233 | 2010-12-11 20:52:06 -0700 | [diff] [blame] | 727 | #ifdef BOARD_HAS_SDCARD_INTERNAL |
| 728 | "Partition Internal SD Card", |
| 729 | #endif |
Koushik Dutta | e17a78d | 2010-08-29 12:35:10 -0700 | [diff] [blame] | 730 | #endif |
Koushik K. Dutta | a496b51 | 2010-03-19 14:51:45 -0700 | [diff] [blame] | 731 | NULL |
| 732 | }; |
| 733 | |
| 734 | for (;;) |
| 735 | { |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 736 | int chosen_item = get_menu_selection(headers, list, 0, 0); |
Koushik K. Dutta | a496b51 | 2010-03-19 14:51:45 -0700 | [diff] [blame] | 737 | if (chosen_item == GO_BACK) |
| 738 | break; |
| 739 | switch (chosen_item) |
| 740 | { |
| 741 | case 0: |
Koushik K. Dutta | 7fe4d7b | 2010-04-06 23:04:52 -0700 | [diff] [blame] | 742 | __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, "recovery"); |
| 743 | break; |
| 744 | case 1: |
Koushik Dutta | d63eaef | 2010-07-14 21:01:21 -0700 | [diff] [blame] | 745 | { |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 746 | if (0 != ensure_path_mounted("/data")) |
Koushik Dutta | 49af23c | 2010-06-21 13:45:51 -0700 | [diff] [blame] | 747 | break; |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 748 | ensure_path_mounted("/sd-ext"); |
| 749 | ensure_path_mounted("/cache"); |
Koushik Dutta | 6c7745d | 2010-08-07 12:17:13 -0700 | [diff] [blame] | 750 | if (confirm_selection( "Confirm wipe?", "Yes - Wipe Dalvik Cache")) { |
Koushik Dutta | d63eaef | 2010-07-14 21:01:21 -0700 | [diff] [blame] | 751 | __system("rm -r /data/dalvik-cache"); |
Koushik Dutta | 6c7745d | 2010-08-07 12:17:13 -0700 | [diff] [blame] | 752 | __system("rm -r /cache/dalvik-cache"); |
| 753 | __system("rm -r /sd-ext/dalvik-cache"); |
| 754 | } |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 755 | ensure_path_unmounted("/data"); |
Koushik Dutta | 6c7745d | 2010-08-07 12:17:13 -0700 | [diff] [blame] | 756 | ui_print("Dalvik Cache wiped.\n"); |
Koushik Dutta | fd1579b | 2010-05-01 12:46:55 -0700 | [diff] [blame] | 757 | break; |
Koushik Dutta | d63eaef | 2010-07-14 21:01:21 -0700 | [diff] [blame] | 758 | } |
Koushik Dutta | fd1579b | 2010-05-01 12:46:55 -0700 | [diff] [blame] | 759 | case 2: |
Koushik Dutta | d63eaef | 2010-07-14 21:01:21 -0700 | [diff] [blame] | 760 | { |
| 761 | if (confirm_selection( "Confirm wipe?", "Yes - Wipe Battery Stats")) |
| 762 | wipe_battery_stats(); |
Koushik Dutta | 598cfc7 | 2010-06-20 09:42:47 -0700 | [diff] [blame] | 763 | break; |
Koushik Dutta | d63eaef | 2010-07-14 21:01:21 -0700 | [diff] [blame] | 764 | } |
Koushik Dutta | 598cfc7 | 2010-06-20 09:42:47 -0700 | [diff] [blame] | 765 | case 3: |
Koushik Dutta | 49af23c | 2010-06-21 13:45:51 -0700 | [diff] [blame] | 766 | handle_failure(1); |
| 767 | break; |
| 768 | case 4: |
Koushik K. Dutta | a496b51 | 2010-03-19 14:51:45 -0700 | [diff] [blame] | 769 | { |
| 770 | ui_print("Outputting key codes.\n"); |
| 771 | ui_print("Go back to end debugging.\n"); |
| 772 | int key; |
| 773 | int action; |
| 774 | do |
| 775 | { |
| 776 | key = ui_wait_key(); |
| 777 | action = device_handle_key(key, 1); |
| 778 | ui_print("Key: %d\n", key); |
| 779 | } |
| 780 | while (action != GO_BACK); |
Koushik Dutta | 56606a2 | 2010-08-23 16:15:33 -0700 | [diff] [blame] | 781 | break; |
Koushik K. Dutta | a496b51 | 2010-03-19 14:51:45 -0700 | [diff] [blame] | 782 | } |
Koushik Dutta | f0e31b8 | 2010-08-17 16:55:38 -0700 | [diff] [blame] | 783 | case 5: |
| 784 | { |
Koushik Dutta | ceddcd5 | 2010-08-23 16:13:14 -0700 | [diff] [blame] | 785 | static char* ext_sizes[] = { "128M", |
| 786 | "256M", |
| 787 | "512M", |
| 788 | "1024M", |
Brint E. Kriebel | 0b23331 | 2010-11-14 15:31:17 -0700 | [diff] [blame] | 789 | "2048M", |
| 790 | "4096M", |
Koushik Dutta | ceddcd5 | 2010-08-23 16:13:14 -0700 | [diff] [blame] | 791 | NULL }; |
| 792 | |
| 793 | static char* swap_sizes[] = { "0M", |
| 794 | "32M", |
| 795 | "64M", |
| 796 | "128M", |
| 797 | "256M", |
| 798 | NULL }; |
| 799 | |
| 800 | static char* ext_headers[] = { "Ext Size", "", NULL }; |
| 801 | static char* swap_headers[] = { "Swap Size", "", NULL }; |
| 802 | |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 803 | int ext_size = get_menu_selection(ext_headers, ext_sizes, 0, 0); |
Koushik Dutta | ceddcd5 | 2010-08-23 16:13:14 -0700 | [diff] [blame] | 804 | if (ext_size == GO_BACK) |
| 805 | continue; |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 806 | |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 807 | int swap_size = get_menu_selection(swap_headers, swap_sizes, 0, 0); |
Koushik Dutta | ceddcd5 | 2010-08-23 16:13:14 -0700 | [diff] [blame] | 808 | if (swap_size == GO_BACK) |
| 809 | continue; |
| 810 | |
| 811 | char sddevice[256]; |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 812 | Volume *vol = volume_for_path("/sdcard"); |
| 813 | strcpy(sddevice, vol->device); |
Koushik Dutta | ceddcd5 | 2010-08-23 16:13:14 -0700 | [diff] [blame] | 814 | // we only want the mmcblk, not the partition |
| 815 | sddevice[strlen("/dev/block/mmcblkX")] = NULL; |
| 816 | char cmd[PATH_MAX]; |
| 817 | setenv("SDPATH", sddevice, 1); |
| 818 | sprintf(cmd, "sdparted -es %s -ss %s -efs ext3 -s", ext_sizes[ext_size], swap_sizes[swap_size]); |
| 819 | ui_print("Partitioning SD Card... please wait...\n"); |
| 820 | if (0 == __system(cmd)) |
| 821 | ui_print("Done!\n"); |
| 822 | else |
| 823 | 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] | 824 | break; |
Koushik Dutta | f0e31b8 | 2010-08-17 16:55:38 -0700 | [diff] [blame] | 825 | } |
Koushik Dutta | 38e8b2b | 2010-08-17 22:21:53 -0700 | [diff] [blame] | 826 | case 6: |
| 827 | { |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 828 | ensure_path_mounted("/system"); |
| 829 | ensure_path_mounted("/data"); |
Koushik Dutta | ceddcd5 | 2010-08-23 16:13:14 -0700 | [diff] [blame] | 830 | ui_print("Fixing permissions...\n"); |
| 831 | __system("fix_permissions"); |
| 832 | ui_print("Done!\n"); |
Koushik Dutta | 56606a2 | 2010-08-23 16:15:33 -0700 | [diff] [blame] | 833 | break; |
Koushik Dutta | 38e8b2b | 2010-08-17 22:21:53 -0700 | [diff] [blame] | 834 | } |
Brint E. Kriebel | d352233 | 2010-12-11 20:52:06 -0700 | [diff] [blame] | 835 | case 7: |
| 836 | { |
| 837 | static char* ext_sizes[] = { "128M", |
| 838 | "256M", |
| 839 | "512M", |
| 840 | "1024M", |
| 841 | "2048M", |
| 842 | "4096M", |
| 843 | NULL }; |
| 844 | |
| 845 | static char* swap_sizes[] = { "0M", |
| 846 | "32M", |
| 847 | "64M", |
| 848 | "128M", |
| 849 | "256M", |
| 850 | NULL }; |
| 851 | |
| 852 | static char* ext_headers[] = { "Data Size", "", NULL }; |
| 853 | static char* swap_headers[] = { "Swap Size", "", NULL }; |
| 854 | |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 855 | 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] | 856 | if (ext_size == GO_BACK) |
| 857 | continue; |
| 858 | |
| 859 | int swap_size = 0; |
| 860 | if (swap_size == GO_BACK) |
| 861 | continue; |
| 862 | |
| 863 | char sddevice[256]; |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 864 | Volume *vol = volume_for_path("/emmc"); |
| 865 | strcpy(sddevice, vol->device); |
Brint E. Kriebel | d352233 | 2010-12-11 20:52:06 -0700 | [diff] [blame] | 866 | // we only want the mmcblk, not the partition |
| 867 | sddevice[strlen("/dev/block/mmcblkX")] = NULL; |
| 868 | char cmd[PATH_MAX]; |
| 869 | setenv("SDPATH", sddevice, 1); |
| 870 | sprintf(cmd, "sdparted -es %s -ss %s -efs ext3 -s", ext_sizes[ext_size], swap_sizes[swap_size]); |
| 871 | ui_print("Partitioning Internal SD Card... please wait...\n"); |
| 872 | if (0 == __system(cmd)) |
| 873 | ui_print("Done!\n"); |
| 874 | else |
| 875 | ui_print("An error occured while partitioning your Internal SD Card. Please see /tmp/recovery.log for more details.\n"); |
| 876 | break; |
| 877 | } |
Koushik K. Dutta | a496b51 | 2010-03-19 14:51:45 -0700 | [diff] [blame] | 878 | } |
| 879 | } |
Koushik Dutta | 14239d2 | 2010-06-14 15:02:48 -0700 | [diff] [blame] | 880 | } |
| 881 | |
Koushik Dutta | b643e4f | 2010-12-18 18:39:39 -0800 | [diff] [blame^] | 882 | void write_fstab_root(char *path, FILE *file) |
Koushik Dutta | 14239d2 | 2010-06-14 15:02:48 -0700 | [diff] [blame] | 883 | { |
Koushik Dutta | b643e4f | 2010-12-18 18:39:39 -0800 | [diff] [blame^] | 884 | Volume *vol = volume_for_path(path); |
| 885 | if (vol == NULL) { |
| 886 | LOGW("Unable to get recovery.fstab info for %s during fstab generation!", path); |
Koushik Dutta | 598cfc7 | 2010-06-20 09:42:47 -0700 | [diff] [blame] | 887 | return; |
Koushik Dutta | a6522b3 | 2010-06-20 13:16:06 -0700 | [diff] [blame] | 888 | } |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 889 | |
Koushik Dutta | b643e4f | 2010-12-18 18:39:39 -0800 | [diff] [blame^] | 890 | fprintf(file, "%s ", vol->device); |
| 891 | fprintf(file, "%s ", path); |
| 892 | fprintf(file, "%s rw\n", vol->fs_type); |
Koushik Dutta | 14239d2 | 2010-06-14 15:02:48 -0700 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | void create_fstab() |
| 896 | { |
Koushik Dutta | b643e4f | 2010-12-18 18:39:39 -0800 | [diff] [blame^] | 897 | struct stat info; |
Koushik Dutta | cd44ab9 | 2010-06-23 00:02:14 -0700 | [diff] [blame] | 898 | __system("touch /etc/mtab"); |
Koushik Dutta | 14239d2 | 2010-06-14 15:02:48 -0700 | [diff] [blame] | 899 | FILE *file = fopen("/etc/fstab", "w"); |
Koushik Dutta | a6522b3 | 2010-06-20 13:16:06 -0700 | [diff] [blame] | 900 | if (file == NULL) { |
Koushik Dutta | b643e4f | 2010-12-18 18:39:39 -0800 | [diff] [blame^] | 901 | LOGW("Unable to create /etc/fstab!\n"); |
Koushik Dutta | 598cfc7 | 2010-06-20 09:42:47 -0700 | [diff] [blame] | 902 | return; |
Koushik Dutta | a6522b3 | 2010-06-20 13:16:06 -0700 | [diff] [blame] | 903 | } |
Koushik Dutta | b643e4f | 2010-12-18 18:39:39 -0800 | [diff] [blame^] | 904 | write_fstab_root("/cache", file); |
| 905 | write_fstab_root("/data", file); |
Koushik Dutta | 19447c0 | 2010-11-10 10:40:44 -0800 | [diff] [blame] | 906 | #ifdef BOARD_HAS_DATADATA |
Koushik Dutta | b643e4f | 2010-12-18 18:39:39 -0800 | [diff] [blame^] | 907 | write_fstab_root("/datadata", file); |
Koushik Dutta | a6522b3 | 2010-06-20 13:16:06 -0700 | [diff] [blame] | 908 | #endif |
Koushik Dutta | b643e4f | 2010-12-18 18:39:39 -0800 | [diff] [blame^] | 909 | write_fstab_root("/system", file); |
| 910 | write_fstab_root("/sdcard", file); |
| 911 | write_fstab_root("/sd-ext", file); |
Koushik Dutta | 14239d2 | 2010-06-14 15:02:48 -0700 | [diff] [blame] | 912 | fclose(file); |
Koushik Dutta | 598cfc7 | 2010-06-20 09:42:47 -0700 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | void handle_failure(int ret) |
| 916 | { |
| 917 | if (ret == 0) |
| 918 | return; |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 919 | if (0 != ensure_path_mounted("/sdcard")) |
Koushik Dutta | 598cfc7 | 2010-06-20 09:42:47 -0700 | [diff] [blame] | 920 | return; |
Koushik Dutta | a6522b3 | 2010-06-20 13:16:06 -0700 | [diff] [blame] | 921 | mkdir("/sdcard/clockworkmod", S_IRWXU); |
| 922 | __system("cp /tmp/recovery.log /sdcard/clockworkmod/recovery.log"); |
Koushik Dutta | 38e8b2b | 2010-08-17 22:21:53 -0700 | [diff] [blame] | 923 | 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] | 924 | } |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 925 | |
| 926 | int format_device(const char* device) { |
| 927 | if (device == NULL) |
| 928 | return -1; |
| 929 | if (device[0] == '/') { |
| 930 | Volume *vol = volume_for_path(device); |
| 931 | if (vol == NULL) |
| 932 | return -1; |
| 933 | return erase_partition(device, vol->fs_type); |
| 934 | } |
| 935 | return erase_raw_partition(device); |
| 936 | } |
| 937 | |
| 938 | int is_path_mounted(const char* path) { |
| 939 | Volume* v = volume_for_path(path); |
| 940 | if (v == NULL) { |
| 941 | return 0; |
| 942 | } |
| 943 | if (strcmp(v->fs_type, "ramdisk") == 0) { |
| 944 | // the ramdisk is always mounted. |
| 945 | return 1; |
| 946 | } |
| 947 | |
| 948 | int result; |
| 949 | result = scan_mounted_volumes(); |
| 950 | if (result < 0) { |
| 951 | LOGE("failed to scan mounted volumes\n"); |
| 952 | return 0; |
| 953 | } |
| 954 | |
| 955 | const MountedVolume* mv = |
| 956 | find_mounted_volume_by_mount_point(v->mount_point); |
| 957 | if (mv) { |
| 958 | // volume is already mounted |
| 959 | return 1; |
| 960 | } |
| 961 | return 0; |
| 962 | } |