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