Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -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 | |
| 15 | #include <sys/wait.h> |
| 16 | #include <sys/limits.h> |
| 17 | #include <dirent.h> |
| 18 | #include <sys/stat.h> |
| 19 | |
| 20 | #include <signal.h> |
| 21 | #include <sys/wait.h> |
| 22 | |
| 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 | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 33 | #include "../../external/yaffs2/yaffs2/utils/mkyaffs2image.h" |
| 34 | #include "../../external/yaffs2/yaffs2/utils/unyaffs.h" |
| 35 | |
Koushik K. Dutta | 7868629 | 2010-03-25 17:58:45 -0700 | [diff] [blame] | 36 | #include <sys/vfs.h> |
| 37 | |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 38 | #include "extendedcommands.h" |
| 39 | #include "nandroid.h" |
Koushik Dutta | 5082299 | 2011-06-08 19:03:27 -0700 | [diff] [blame] | 40 | #include "mounts.h" |
| 41 | |
Koushik Dutta | 7adeadc | 2011-05-26 11:47:56 -0700 | [diff] [blame] | 42 | #include "flashutils/flashutils.h" |
| 43 | #include <libgen.h> |
| 44 | |
Koushik Dutta | 7adeadc | 2011-05-26 11:47:56 -0700 | [diff] [blame] | 45 | void nandroid_generate_timestamp_path(const char* backup_path) |
| 46 | { |
| 47 | time_t t = time(NULL); |
| 48 | struct tm *tmp = localtime(&t); |
| 49 | if (tmp == NULL) |
| 50 | { |
| 51 | struct timeval tp; |
| 52 | gettimeofday(&tp, NULL); |
| 53 | sprintf(backup_path, "/sdcard/clockworkmod/backup/%d", tp.tv_sec); |
| 54 | } |
| 55 | else |
| 56 | { |
| 57 | strftime(backup_path, PATH_MAX, "/sdcard/clockworkmod/backup/%F.%H.%M.%S", tmp); |
| 58 | } |
| 59 | } |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 60 | |
Koushik Dutta | 5082299 | 2011-06-08 19:03:27 -0700 | [diff] [blame] | 61 | static int print_and_error(const char* message) { |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 62 | ui_print("%s", message); |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 63 | return 1; |
| 64 | } |
| 65 | |
Koushik Dutta | 5082299 | 2011-06-08 19:03:27 -0700 | [diff] [blame] | 66 | static int yaffs_files_total = 0; |
| 67 | static int yaffs_files_count = 0; |
| 68 | static void yaffs_callback(const char* filename) |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 69 | { |
Koushik Dutta | 5082299 | 2011-06-08 19:03:27 -0700 | [diff] [blame] | 70 | if (filename == NULL) |
| 71 | return; |
| 72 | const char* justfile = basename(filename); |
| 73 | char tmp[PATH_MAX]; |
| 74 | strcpy(tmp, justfile); |
| 75 | if (tmp[strlen(tmp) - 1] == '\n') |
| 76 | tmp[strlen(tmp) - 1] = NULL; |
| 77 | if (strlen(tmp) < 30) |
| 78 | ui_print("%s", tmp); |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 79 | yaffs_files_count++; |
| 80 | if (yaffs_files_total != 0) |
| 81 | ui_set_progress((float)yaffs_files_count / (float)yaffs_files_total); |
| 82 | ui_reset_text_col(); |
| 83 | } |
| 84 | |
Koushik Dutta | 5082299 | 2011-06-08 19:03:27 -0700 | [diff] [blame] | 85 | static void compute_directory_stats(const char* directory) |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 86 | { |
| 87 | char tmp[PATH_MAX]; |
| 88 | sprintf(tmp, "find %s | wc -l > /tmp/dircount", directory); |
| 89 | __system(tmp); |
| 90 | char count_text[100]; |
| 91 | FILE* f = fopen("/tmp/dircount", "r"); |
| 92 | fread(count_text, 1, sizeof(count_text), f); |
| 93 | fclose(f); |
| 94 | yaffs_files_count = 0; |
| 95 | yaffs_files_total = atoi(count_text); |
| 96 | ui_reset_progress(); |
| 97 | ui_show_progress(1, 0); |
| 98 | } |
| 99 | |
Koushik Dutta | 5082299 | 2011-06-08 19:03:27 -0700 | [diff] [blame] | 100 | typedef void (*file_event_callback)(const char* filename); |
Koushik Dutta | 55e5e7b | 2011-06-14 23:39:59 -0700 | [diff] [blame] | 101 | typedef int (*nandroid_backup_handler)(const char* backup_path, const char* backup_file_image, int callback); |
Koushik Dutta | 5082299 | 2011-06-08 19:03:27 -0700 | [diff] [blame] | 102 | |
Koushik Dutta | 55e5e7b | 2011-06-14 23:39:59 -0700 | [diff] [blame] | 103 | static int mkyaffs2image_wrapper(const char* backup_path, const char* backup_file_image, int callback) { |
Koushik Dutta | 30a937a | 2011-09-05 21:14:06 -0700 | [diff] [blame] | 104 | char backup_file_image_with_extension[PATH_MAX]; |
| 105 | sprintf(backup_file_image_with_extension, "%s.img", backup_file_image); |
| 106 | return mkyaffs2image(backup_path, backup_file_image_with_extension, 0, callback ? yaffs_callback : NULL); |
Koushik Dutta | 5082299 | 2011-06-08 19:03:27 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Koushik Dutta | 55e5e7b | 2011-06-14 23:39:59 -0700 | [diff] [blame] | 109 | static int tar_compress_wrapper(const char* backup_path, const char* backup_file_image, int callback) { |
Koushik Dutta | 5082299 | 2011-06-08 19:03:27 -0700 | [diff] [blame] | 110 | char tmp[PATH_MAX]; |
| 111 | if (strcmp(backup_path, "/data") == 0 && volume_for_path("/sdcard") == NULL) |
Koushik Dutta | 30a937a | 2011-09-05 21:14:06 -0700 | [diff] [blame] | 112 | sprintf(tmp, "cd $(dirname %s) ; tar cvf %s.tar --exclude 'media' $(basename %s) ; exit $?", backup_path, backup_file_image, backup_path); |
Koushik Dutta | 5082299 | 2011-06-08 19:03:27 -0700 | [diff] [blame] | 113 | else |
Koushik Dutta | 30a937a | 2011-09-05 21:14:06 -0700 | [diff] [blame] | 114 | sprintf(tmp, "cd $(dirname %s) ; tar cvf %s.tar $(basename %s) ; exit $?", backup_path, backup_file_image, backup_path); |
Koushik Dutta | 5082299 | 2011-06-08 19:03:27 -0700 | [diff] [blame] | 115 | |
| 116 | FILE *fp = __popen(tmp, "r"); |
| 117 | if (fp == NULL) { |
| 118 | ui_print("Unable to execute tar.\n"); |
| 119 | return -1; |
| 120 | } |
| 121 | |
| 122 | while (fgets(tmp, PATH_MAX, fp) != NULL) { |
| 123 | tmp[PATH_MAX - 1] = NULL; |
Koushik Dutta | 55e5e7b | 2011-06-14 23:39:59 -0700 | [diff] [blame] | 124 | if (callback) |
Koushik Dutta | 5082299 | 2011-06-08 19:03:27 -0700 | [diff] [blame] | 125 | yaffs_callback(tmp); |
| 126 | } |
| 127 | |
| 128 | return __pclose(fp); |
| 129 | } |
| 130 | |
| 131 | static nandroid_backup_handler get_backup_handler(const char *backup_path) { |
| 132 | Volume *v = volume_for_path(backup_path); |
| 133 | if (v == NULL) { |
| 134 | ui_print("Unable to find volume.\n"); |
| 135 | return NULL; |
| 136 | } |
Koushik Dutta | 5082299 | 2011-06-08 19:03:27 -0700 | [diff] [blame] | 137 | MountedVolume *mv = find_mounted_volume_by_mount_point(v->mount_point); |
| 138 | if (mv == NULL) { |
| 139 | ui_print("Unable to find mounted volume: %s\n", v->mount_point); |
| 140 | return NULL; |
| 141 | } |
| 142 | |
Koushik Dutta | 38a9214 | 2011-06-10 09:45:52 -0700 | [diff] [blame] | 143 | if (strcmp(backup_path, "/data") == 0 && is_data_media()) { |
| 144 | return tar_compress_wrapper; |
| 145 | } |
| 146 | |
Koushik Dutta | 30a937a | 2011-09-05 21:14:06 -0700 | [diff] [blame] | 147 | // cwr5, we prefer tar for everything except yaffs2 |
| 148 | if (strcmp("yaffs2", mv->filesystem) == 0) { |
Koushik Dutta | 38a9214 | 2011-06-10 09:45:52 -0700 | [diff] [blame] | 149 | return mkyaffs2image_wrapper; |
| 150 | } |
| 151 | |
Koushik Dutta | 30a937a | 2011-09-05 21:14:06 -0700 | [diff] [blame] | 152 | char str[255]; |
| 153 | char* partition; |
| 154 | property_get("ro.cwm.prefer_tar", str, "true"); |
| 155 | if (strcmp("true", str) != 0) { |
Koushik Dutta | 5082299 | 2011-06-08 19:03:27 -0700 | [diff] [blame] | 156 | return mkyaffs2image_wrapper; |
| 157 | } |
| 158 | |
| 159 | return tar_compress_wrapper; |
| 160 | } |
| 161 | |
| 162 | |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 163 | int nandroid_backup_partition_extended(const char* backup_path, const char* mount_point, int umount_when_finished) { |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 164 | int ret = 0; |
Koushik K. Dutta | 68b0190 | 2010-04-01 12:20:39 -0700 | [diff] [blame] | 165 | char* name = basename(mount_point); |
Koushik Dutta | 58131e7 | 2010-06-09 12:41:20 -0700 | [diff] [blame] | 166 | |
| 167 | struct stat file_info; |
Koushik Dutta | 30a937a | 2011-09-05 21:14:06 -0700 | [diff] [blame] | 168 | int callback = stat("/sdcard/clockworkmod/.hidenandroidprogress", &file_info) != 0; |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 169 | |
| 170 | ui_print("Backing up %s...\n", name); |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 171 | if (0 != (ret = ensure_path_mounted(mount_point) != 0)) { |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 172 | ui_print("Can't mount %s!\n", mount_point); |
| 173 | return ret; |
| 174 | } |
| 175 | compute_directory_stats(mount_point); |
| 176 | char tmp[PATH_MAX]; |
Koushik Dutta | 30a937a | 2011-09-05 21:14:06 -0700 | [diff] [blame] | 177 | scan_mounted_volumes(); |
| 178 | Volume *v = volume_for_path(mount_point); |
| 179 | MountedVolume *mv = NULL; |
| 180 | if (v != NULL) |
| 181 | mv = find_mounted_volume_by_mount_point(v->mount_point); |
| 182 | if (mv == NULL || mv->filesystem == NULL) |
| 183 | sprintf(tmp, "%s/%s.auto", backup_path, name); |
| 184 | else |
| 185 | sprintf(tmp, "%s/%s.%s", backup_path, name, mv->filesystem); |
Koushik Dutta | 5082299 | 2011-06-08 19:03:27 -0700 | [diff] [blame] | 186 | nandroid_backup_handler backup_handler = get_backup_handler(mount_point); |
| 187 | if (backup_handler == NULL) { |
| 188 | ui_print("Error finding an appropriate backup handler.\n"); |
| 189 | return -2; |
| 190 | } |
| 191 | ret = backup_handler(mount_point, tmp, callback); |
Koushik Dutta | 062d6b0 | 2010-07-03 13:54:21 -0700 | [diff] [blame] | 192 | if (umount_when_finished) { |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 193 | ensure_path_unmounted(mount_point); |
Koushik Dutta | 062d6b0 | 2010-07-03 13:54:21 -0700 | [diff] [blame] | 194 | } |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 195 | if (0 != ret) { |
Koushik Dutta | 5082299 | 2011-06-08 19:03:27 -0700 | [diff] [blame] | 196 | ui_print("Error while making a backup image of %s!\n", mount_point); |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 197 | return ret; |
| 198 | } |
| 199 | return 0; |
| 200 | } |
| 201 | |
Koushik Dutta | 53c9bd8 | 2011-01-04 11:38:31 -0800 | [diff] [blame] | 202 | int nandroid_backup_partition(const char* backup_path, const char* root) { |
| 203 | Volume *vol = volume_for_path(root); |
| 204 | // make sure the volume exists before attempting anything... |
| 205 | if (vol == NULL || vol->fs_type == NULL) |
| 206 | return NULL; |
| 207 | |
| 208 | // see if we need a raw backup (mtd) |
| 209 | char tmp[PATH_MAX]; |
| 210 | int ret; |
Christopher Lais | 066a102 | 2011-01-16 06:02:34 -0600 | [diff] [blame] | 211 | if (strcmp(vol->fs_type, "mtd") == 0 || |
Koushik Dutta | 33e37f3 | 2011-02-27 16:33:32 -0800 | [diff] [blame] | 212 | strcmp(vol->fs_type, "bml") == 0 || |
Christopher Lais | 066a102 | 2011-01-16 06:02:34 -0600 | [diff] [blame] | 213 | strcmp(vol->fs_type, "emmc") == 0) { |
Koushik Dutta | 53c9bd8 | 2011-01-04 11:38:31 -0800 | [diff] [blame] | 214 | const char* name = basename(root); |
| 215 | sprintf(tmp, "%s/%s.img", backup_path, name); |
| 216 | ui_print("Backing up %s image...\n", name); |
Koushik Dutta | 8a6bc77 | 2011-05-26 11:14:15 -0700 | [diff] [blame] | 217 | if (0 != (ret = backup_raw_partition(vol->fs_type, vol->device, tmp))) { |
Koushik Dutta | 53c9bd8 | 2011-01-04 11:38:31 -0800 | [diff] [blame] | 218 | ui_print("Error while backing up %s image!", name); |
| 219 | return ret; |
| 220 | } |
| 221 | return 0; |
| 222 | } |
| 223 | |
Koushik Dutta | 062d6b0 | 2010-07-03 13:54:21 -0700 | [diff] [blame] | 224 | return nandroid_backup_partition_extended(backup_path, root, 1); |
| 225 | } |
| 226 | |
Koushik Dutta | 2f73e58 | 2010-04-18 16:00:21 -0700 | [diff] [blame] | 227 | int nandroid_backup(const char* backup_path) |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 228 | { |
| 229 | ui_set_background(BACKGROUND_ICON_INSTALLING); |
| 230 | |
Koushik Dutta | 89ed0b7 | 2011-10-26 21:25:34 -0700 | [diff] [blame] | 231 | if (ensure_path_mounted(backup_path) != 0) { |
| 232 | return print_and_error("Can't mount backup path.\n"); |
| 233 | } |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 234 | |
Koushik Dutta | 89ed0b7 | 2011-10-26 21:25:34 -0700 | [diff] [blame] | 235 | Volume* volume = volume_for_path(backup_path); |
Prashant Somashekar | 357a2f5 | 2011-11-26 09:53:41 -0500 | [diff] [blame] | 236 | if (NULL == volume) { |
| 237 | if (strstr(backup_path, "/sdcard") == backup_path && is_data_media()) |
| 238 | volume = volume_for_path("/data"); |
| 239 | else |
| 240 | return print_and_error("Unable to find volume for backup path.\n"); |
| 241 | } |
Koushik K. Dutta | 7868629 | 2010-03-25 17:58:45 -0700 | [diff] [blame] | 242 | int ret; |
Koushik K. Dutta | f3534d0 | 2010-04-18 18:06:24 -0700 | [diff] [blame] | 243 | struct statfs s; |
Koushik Dutta | ddc1241 | 2011-11-23 14:06:12 -0800 | [diff] [blame] | 244 | if (NULL != volume) { |
| 245 | if (0 != (ret = statfs(volume->mount_point, &s))) |
| 246 | return print_and_error("Unable to stat backup path.\n"); |
| 247 | uint64_t bavail = s.f_bavail; |
| 248 | uint64_t bsize = s.f_bsize; |
| 249 | uint64_t sdcard_free = bavail * bsize; |
| 250 | uint64_t sdcard_free_mb = sdcard_free / (uint64_t)(1024 * 1024); |
| 251 | ui_print("SD Card space free: %lluMB\n", sdcard_free_mb); |
| 252 | if (sdcard_free_mb < 150) |
| 253 | ui_print("There may not be enough free space to complete backup... continuing...\n"); |
| 254 | } |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 255 | char tmp[PATH_MAX]; |
| 256 | sprintf(tmp, "mkdir -p %s", backup_path); |
| 257 | __system(tmp); |
| 258 | |
Koushik Dutta | 53c9bd8 | 2011-01-04 11:38:31 -0800 | [diff] [blame] | 259 | if (0 != (ret = nandroid_backup_partition(backup_path, "/boot"))) |
| 260 | return ret; |
Koushik K. Dutta | 6923cc3 | 2010-03-29 14:46:00 -0700 | [diff] [blame] | 261 | |
Koushik Dutta | 53c9bd8 | 2011-01-04 11:38:31 -0800 | [diff] [blame] | 262 | if (0 != (ret = nandroid_backup_partition(backup_path, "/recovery"))) |
| 263 | return ret; |
Koushik K. Dutta | 6923cc3 | 2010-03-29 14:46:00 -0700 | [diff] [blame] | 264 | |
Koushik Dutta | 5ee98b2 | 2011-03-02 19:35:05 -0800 | [diff] [blame] | 265 | Volume *vol = volume_for_path("/wimax"); |
| 266 | if (vol != NULL && 0 == stat(vol->device, &s)) |
agrabren | 1f76a5d | 2010-12-29 12:15:18 -0600 | [diff] [blame] | 267 | { |
Joshua Wise | 05d4a09 | 2010-12-31 02:05:35 -0500 | [diff] [blame] | 268 | char serialno[PROPERTY_VALUE_MAX]; |
Joshua Wise | ca889ec | 2010-12-30 02:48:25 -0500 | [diff] [blame] | 269 | ui_print("Backing up WiMAX...\n"); |
Joshua Wise | 05d4a09 | 2010-12-31 02:05:35 -0500 | [diff] [blame] | 270 | serialno[0] = 0; |
| 271 | property_get("ro.serialno", serialno, ""); |
| 272 | sprintf(tmp, "%s/wimax.%s.img", backup_path, serialno); |
Koushik Dutta | e62132b | 2011-05-26 11:29:29 -0700 | [diff] [blame] | 273 | ret = backup_raw_partition(vol->fs_type, vol->device, tmp); |
agrabren | 1f76a5d | 2010-12-29 12:15:18 -0600 | [diff] [blame] | 274 | if (0 != ret) |
Joshua Wise | ca889ec | 2010-12-30 02:48:25 -0500 | [diff] [blame] | 275 | return print_and_error("Error while dumping WiMAX image!\n"); |
agrabren | 1f76a5d | 2010-12-29 12:15:18 -0600 | [diff] [blame] | 276 | } |
| 277 | |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 278 | if (0 != (ret = nandroid_backup_partition(backup_path, "/system"))) |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 279 | return ret; |
| 280 | |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 281 | if (0 != (ret = nandroid_backup_partition(backup_path, "/data"))) |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 282 | return ret; |
| 283 | |
Koushik Dutta | 5460f0c | 2010-12-18 22:37:49 -0800 | [diff] [blame] | 284 | if (has_datadata()) { |
| 285 | if (0 != (ret = nandroid_backup_partition(backup_path, "/datadata"))) |
| 286 | return ret; |
| 287 | } |
Koushik Dutta | 8b5e185 | 2010-06-14 22:04:22 -0700 | [diff] [blame] | 288 | |
Koushik Dutta | 5ee98b2 | 2011-03-02 19:35:05 -0800 | [diff] [blame] | 289 | if (0 != stat("/sdcard/.android_secure", &s)) |
Koushik Dutta | 062d6b0 | 2010-07-03 13:54:21 -0700 | [diff] [blame] | 290 | { |
| 291 | ui_print("No /sdcard/.android_secure found. Skipping backup of applications on external storage.\n"); |
| 292 | } |
| 293 | else |
| 294 | { |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 295 | if (0 != (ret = nandroid_backup_partition_extended(backup_path, "/sdcard/.android_secure", 0))) |
Koushik Dutta | 062d6b0 | 2010-07-03 13:54:21 -0700 | [diff] [blame] | 296 | return ret; |
| 297 | } |
| 298 | |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 299 | if (0 != (ret = nandroid_backup_partition_extended(backup_path, "/cache", 0))) |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 300 | return ret; |
Koushik K. Dutta | 3f99539 | 2010-03-30 23:29:43 -0700 | [diff] [blame] | 301 | |
Koushik Dutta | 5ee98b2 | 2011-03-02 19:35:05 -0800 | [diff] [blame] | 302 | vol = volume_for_path("/sd-ext"); |
| 303 | if (vol == NULL || 0 != stat(vol->device, &s)) |
Koushik K. Dutta | 6771aca | 2010-04-03 23:28:39 -0700 | [diff] [blame] | 304 | { |
Koushik K. Dutta | 71ef11f | 2010-04-02 22:17:42 -0700 | [diff] [blame] | 305 | ui_print("No sd-ext found. Skipping backup of sd-ext.\n"); |
Koushik K. Dutta | 6771aca | 2010-04-03 23:28:39 -0700 | [diff] [blame] | 306 | } |
| 307 | else |
| 308 | { |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 309 | if (0 != ensure_path_mounted("/sd-ext")) |
Koushik Dutta | 8b5e185 | 2010-06-14 22:04:22 -0700 | [diff] [blame] | 310 | ui_print("Could not mount sd-ext. sd-ext backup may not be supported on this device. Skipping backup of sd-ext.\n"); |
Koushik Dutta | e06e539 | 2011-01-16 02:33:04 -0800 | [diff] [blame] | 311 | else if (0 != (ret = nandroid_backup_partition(backup_path, "/sd-ext"))) |
Koushik K. Dutta | 6771aca | 2010-04-03 23:28:39 -0700 | [diff] [blame] | 312 | return ret; |
| 313 | } |
Koushik Dutta | 062d6b0 | 2010-07-03 13:54:21 -0700 | [diff] [blame] | 314 | |
Koushik K. Dutta | d717892 | 2010-03-24 14:38:40 -0700 | [diff] [blame] | 315 | ui_print("Generating md5 sum...\n"); |
Koushik K. Dutta | 68b0190 | 2010-04-01 12:20:39 -0700 | [diff] [blame] | 316 | sprintf(tmp, "nandroid-md5.sh %s", backup_path); |
Koushik K. Dutta | b0a2503 | 2010-03-24 10:34:38 -0700 | [diff] [blame] | 317 | if (0 != (ret = __system(tmp))) { |
| 318 | ui_print("Error while generating md5 sum!\n"); |
| 319 | return ret; |
| 320 | } |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 321 | |
Koushik K. Dutta | 3f38f32 | 2010-03-12 23:45:25 -0800 | [diff] [blame] | 322 | sync(); |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 323 | ui_set_background(BACKGROUND_ICON_NONE); |
| 324 | ui_reset_progress(); |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 325 | ui_print("\nBackup complete!\n"); |
| 326 | return 0; |
| 327 | } |
| 328 | |
Koushik K. Dutta | 68b0190 | 2010-04-01 12:20:39 -0700 | [diff] [blame] | 329 | typedef int (*format_function)(char* root); |
| 330 | |
Koushik Dutta | 5aaa823 | 2010-07-20 16:23:18 -0700 | [diff] [blame] | 331 | static void ensure_directory(const char* dir) { |
| 332 | char tmp[PATH_MAX]; |
| 333 | sprintf(tmp, "mkdir -p %s", dir); |
| 334 | __system(tmp); |
| 335 | } |
| 336 | |
Koushik Dutta | 7905c80 | 2011-06-15 00:00:55 -0700 | [diff] [blame] | 337 | typedef int (*nandroid_restore_handler)(const char* backup_file_image, const char* backup_path, int callback); |
Koushik Dutta | 5082299 | 2011-06-08 19:03:27 -0700 | [diff] [blame] | 338 | |
Koushik Dutta | 7905c80 | 2011-06-15 00:00:55 -0700 | [diff] [blame] | 339 | static int unyaffs_wrapper(const char* backup_file_image, const char* backup_path, int callback) { |
Koushik Dutta | c915937 | 2011-07-20 01:26:02 -0700 | [diff] [blame] | 340 | return unyaffs(backup_file_image, backup_path, callback ? yaffs_callback : NULL); |
Koushik Dutta | 5082299 | 2011-06-08 19:03:27 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Koushik Dutta | 7905c80 | 2011-06-15 00:00:55 -0700 | [diff] [blame] | 343 | static int tar_extract_wrapper(const char* backup_file_image, const char* backup_path, int callback) { |
Koushik Dutta | 5082299 | 2011-06-08 19:03:27 -0700 | [diff] [blame] | 344 | char tmp[PATH_MAX]; |
| 345 | sprintf(tmp, "cd $(dirname %s) ; tar xvf %s ; exit $?", backup_path, backup_file_image); |
| 346 | |
| 347 | char path[PATH_MAX]; |
| 348 | FILE *fp = __popen(tmp, "r"); |
| 349 | if (fp == NULL) { |
| 350 | ui_print("Unable to execute tar.\n"); |
| 351 | return -1; |
| 352 | } |
| 353 | |
| 354 | while (fgets(path, PATH_MAX, fp) != NULL) { |
Koushik Dutta | 7905c80 | 2011-06-15 00:00:55 -0700 | [diff] [blame] | 355 | if (callback) |
| 356 | yaffs_callback(path); |
Koushik Dutta | 5082299 | 2011-06-08 19:03:27 -0700 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | return __pclose(fp); |
| 360 | } |
| 361 | |
| 362 | static nandroid_restore_handler get_restore_handler(const char *backup_path) { |
| 363 | Volume *v = volume_for_path(backup_path); |
| 364 | if (v == NULL) { |
| 365 | ui_print("Unable to find volume.\n"); |
| 366 | return NULL; |
| 367 | } |
| 368 | scan_mounted_volumes(); |
| 369 | MountedVolume *mv = find_mounted_volume_by_mount_point(v->mount_point); |
| 370 | if (mv == NULL) { |
| 371 | ui_print("Unable to find mounted volume: %s\n", v->mount_point); |
| 372 | return NULL; |
| 373 | } |
| 374 | |
Koushik Dutta | 38a9214 | 2011-06-10 09:45:52 -0700 | [diff] [blame] | 375 | if (strcmp(backup_path, "/data") == 0 && is_data_media()) { |
| 376 | return tar_extract_wrapper; |
| 377 | } |
| 378 | |
Koushik Dutta | 30a937a | 2011-09-05 21:14:06 -0700 | [diff] [blame] | 379 | // cwr 5, we prefer tar for everything unless it is yaffs2 |
Koushik Dutta | 38a9214 | 2011-06-10 09:45:52 -0700 | [diff] [blame] | 380 | char str[255]; |
| 381 | char* partition; |
| 382 | property_get("ro.cwm.prefer_tar", str, "false"); |
| 383 | if (strcmp("true", str) != 0) { |
| 384 | return unyaffs_wrapper; |
| 385 | } |
| 386 | |
Koushik Dutta | 5082299 | 2011-06-08 19:03:27 -0700 | [diff] [blame] | 387 | if (strcmp("yaffs2", mv->filesystem) == 0) { |
| 388 | return unyaffs_wrapper; |
| 389 | } |
| 390 | |
| 391 | return tar_extract_wrapper; |
| 392 | } |
| 393 | |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 394 | int nandroid_restore_partition_extended(const char* backup_path, const char* mount_point, int umount_when_finished) { |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 395 | int ret = 0; |
Koushik K. Dutta | 68b0190 | 2010-04-01 12:20:39 -0700 | [diff] [blame] | 396 | char* name = basename(mount_point); |
Koushik Dutta | 30a937a | 2011-09-05 21:14:06 -0700 | [diff] [blame] | 397 | |
| 398 | nandroid_restore_handler restore_handler = NULL; |
Donovan Bartish | a87bb5f | 2011-09-29 15:37:19 -0500 | [diff] [blame] | 399 | const char *filesystems[] = { "yaffs2", "ext2", "ext3", "ext4", "vfat", "rfs", NULL }; |
Koushik Dutta | 30a937a | 2011-09-05 21:14:06 -0700 | [diff] [blame] | 400 | const char* backup_filesystem = NULL; |
| 401 | Volume *vol = volume_for_path(mount_point); |
| 402 | const char *device = NULL; |
| 403 | if (vol != NULL) |
| 404 | device = vol->device; |
| 405 | |
Koushik Dutta | 5aaa823 | 2010-07-20 16:23:18 -0700 | [diff] [blame] | 406 | char tmp[PATH_MAX]; |
| 407 | sprintf(tmp, "%s/%s.img", backup_path, name); |
Koushik Dutta | 58131e7 | 2010-06-09 12:41:20 -0700 | [diff] [blame] | 408 | struct stat file_info; |
Koushik Dutta | 5aaa823 | 2010-07-20 16:23:18 -0700 | [diff] [blame] | 409 | if (0 != (ret = statfs(tmp, &file_info))) { |
Koushik Dutta | 30a937a | 2011-09-05 21:14:06 -0700 | [diff] [blame] | 410 | // can't find the backup, it may be the new backup format? |
| 411 | // iterate through the backup types |
| 412 | printf("couldn't find default\n"); |
| 413 | char *filesystem; |
| 414 | int i = 0; |
| 415 | while ((filesystem = filesystems[i]) != NULL) { |
| 416 | sprintf(tmp, "%s/%s.%s.img", backup_path, name, filesystem); |
| 417 | if (0 == (ret = statfs(tmp, &file_info))) { |
| 418 | backup_filesystem = filesystem; |
| 419 | restore_handler = unyaffs_wrapper; |
| 420 | break; |
| 421 | } |
| 422 | sprintf(tmp, "%s/%s.%s.tar", backup_path, name, filesystem); |
| 423 | if (0 == (ret = statfs(tmp, &file_info))) { |
| 424 | backup_filesystem = filesystem; |
| 425 | restore_handler = tar_extract_wrapper; |
| 426 | break; |
| 427 | } |
| 428 | i++; |
| 429 | } |
| 430 | |
| 431 | if (backup_filesystem == NULL || restore_handler == NULL) { |
| 432 | ui_print("%s.img not found. Skipping restore of %s.\n", name, mount_point); |
| 433 | return 0; |
| 434 | } |
| 435 | else { |
| 436 | printf("Found new backup image: %s\n", tmp); |
| 437 | } |
| 438 | |
Prashant Somashekar | f0c389d | 2011-11-26 13:55:00 -0500 | [diff] [blame] | 439 | // If the fs_type of this volume is "auto" or mount_point is /data |
Koushik Dutta | 9487d29 | 2011-12-18 13:49:52 -0800 | [diff] [blame] | 440 | // and is_data_media (redundantly, and vol for /sdcard is NULL), let's revert |
Prashant Somashekar | f0c389d | 2011-11-26 13:55:00 -0500 | [diff] [blame] | 441 | // to using a rm -rf, rather than trying to do a |
| 442 | // ext3/ext4/whatever format. |
Koushik Dutta | 30a937a | 2011-09-05 21:14:06 -0700 | [diff] [blame] | 443 | // This is because some phones (like DroidX) will freak out if you |
| 444 | // reformat the /system or /data partitions, and not boot due to |
| 445 | // a locked bootloader. |
Koushik Dutta | ddc1241 | 2011-11-23 14:06:12 -0800 | [diff] [blame] | 446 | // Other devices, like the Galaxy Nexus, XOOM, and Galaxy Tab 10.1 |
| 447 | // have a /sdcard symlinked to /data/media. /data is set to "auto" |
| 448 | // so that when the format occurs, /data/media is not erased. |
Koushik Dutta | 30a937a | 2011-09-05 21:14:06 -0700 | [diff] [blame] | 449 | // The "auto" fs type preserves the file system, and does not |
| 450 | // trigger that lock. |
| 451 | // Or of volume does not exist (.android_secure), just rm -rf. |
| 452 | if (vol == NULL || 0 == strcmp(vol->fs_type, "auto")) |
| 453 | backup_filesystem = NULL; |
Prashant Somashekar | f0c389d | 2011-11-26 13:55:00 -0500 | [diff] [blame] | 454 | else if (0 == strcmp(vol->mount_point, "/data") && volume_for_path("/sdcard") == NULL && is_data_media()) |
| 455 | backup_filesystem = NULL; |
Koushik Dutta | 5aaa823 | 2010-07-20 16:23:18 -0700 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | ensure_directory(mount_point); |
| 459 | |
Koushik Dutta | 30a937a | 2011-09-05 21:14:06 -0700 | [diff] [blame] | 460 | int callback = stat("/sdcard/clockworkmod/.hidenandroidprogress", &file_info) != 0; |
Koushik Dutta | 58131e7 | 2010-06-09 12:41:20 -0700 | [diff] [blame] | 461 | |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 462 | ui_print("Restoring %s...\n", name); |
Koushik Dutta | 30a937a | 2011-09-05 21:14:06 -0700 | [diff] [blame] | 463 | if (backup_filesystem == NULL) { |
| 464 | if (0 != (ret = format_volume(mount_point))) { |
| 465 | ui_print("Error while formatting %s!\n", mount_point); |
| 466 | return ret; |
| 467 | } |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 468 | } |
Koushik Dutta | 30a937a | 2011-09-05 21:14:06 -0700 | [diff] [blame] | 469 | else if (0 != (ret = format_device(device, mount_point, backup_filesystem))) { |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 470 | ui_print("Error while formatting %s!\n", mount_point); |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 471 | return ret; |
| 472 | } |
Koushik Dutta | 30a937a | 2011-09-05 21:14:06 -0700 | [diff] [blame] | 473 | |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 474 | if (0 != (ret = ensure_path_mounted(mount_point))) { |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 475 | ui_print("Can't mount %s!\n", mount_point); |
| 476 | return ret; |
| 477 | } |
Koushik Dutta | 30a937a | 2011-09-05 21:14:06 -0700 | [diff] [blame] | 478 | |
| 479 | if (restore_handler == NULL) |
| 480 | restore_handler = get_restore_handler(mount_point); |
Koushik Dutta | 5082299 | 2011-06-08 19:03:27 -0700 | [diff] [blame] | 481 | if (restore_handler == NULL) { |
| 482 | ui_print("Error finding an appropriate restore handler.\n"); |
| 483 | return -2; |
| 484 | } |
| 485 | if (0 != (ret = restore_handler(tmp, mount_point, callback))) { |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 486 | ui_print("Error while restoring %s!\n", mount_point); |
| 487 | return ret; |
| 488 | } |
Koushik Dutta | 2f73e58 | 2010-04-18 16:00:21 -0700 | [diff] [blame] | 489 | |
Koushik Dutta | 062d6b0 | 2010-07-03 13:54:21 -0700 | [diff] [blame] | 490 | if (umount_when_finished) { |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 491 | ensure_path_unmounted(mount_point); |
Koushik Dutta | 2f73e58 | 2010-04-18 16:00:21 -0700 | [diff] [blame] | 492 | } |
Koushik Dutta | 062d6b0 | 2010-07-03 13:54:21 -0700 | [diff] [blame] | 493 | |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 494 | return 0; |
| 495 | } |
| 496 | |
Koushik Dutta | 062d6b0 | 2010-07-03 13:54:21 -0700 | [diff] [blame] | 497 | int nandroid_restore_partition(const char* backup_path, const char* root) { |
Koushik Dutta | 53c9bd8 | 2011-01-04 11:38:31 -0800 | [diff] [blame] | 498 | Volume *vol = volume_for_path(root); |
| 499 | // make sure the volume exists... |
| 500 | if (vol == NULL || vol->fs_type == NULL) |
| 501 | return 0; |
| 502 | |
| 503 | // see if we need a raw restore (mtd) |
| 504 | char tmp[PATH_MAX]; |
Christopher Lais | 066a102 | 2011-01-16 06:02:34 -0600 | [diff] [blame] | 505 | if (strcmp(vol->fs_type, "mtd") == 0 || |
Koushik Dutta | 33e37f3 | 2011-02-27 16:33:32 -0800 | [diff] [blame] | 506 | strcmp(vol->fs_type, "bml") == 0 || |
Christopher Lais | 066a102 | 2011-01-16 06:02:34 -0600 | [diff] [blame] | 507 | strcmp(vol->fs_type, "emmc") == 0) { |
Koushik Dutta | 53c9bd8 | 2011-01-04 11:38:31 -0800 | [diff] [blame] | 508 | int ret; |
| 509 | const char* name = basename(root); |
| 510 | ui_print("Erasing %s before restore...\n", name); |
| 511 | if (0 != (ret = format_volume(root))) { |
| 512 | ui_print("Error while erasing %s image!", name); |
| 513 | return ret; |
| 514 | } |
| 515 | sprintf(tmp, "%s%s.img", backup_path, root); |
| 516 | ui_print("Restoring %s image...\n", name); |
Koushik Dutta | 8a6bc77 | 2011-05-26 11:14:15 -0700 | [diff] [blame] | 517 | if (0 != (ret = restore_raw_partition(vol->fs_type, vol->device, tmp))) { |
Koushik Dutta | 53c9bd8 | 2011-01-04 11:38:31 -0800 | [diff] [blame] | 518 | ui_print("Error while flashing %s image!", name); |
| 519 | return ret; |
| 520 | } |
| 521 | return 0; |
| 522 | } |
Koushik Dutta | 062d6b0 | 2010-07-03 13:54:21 -0700 | [diff] [blame] | 523 | return nandroid_restore_partition_extended(backup_path, root, 1); |
| 524 | } |
| 525 | |
agrabren | 1f76a5d | 2010-12-29 12:15:18 -0600 | [diff] [blame] | 526 | int nandroid_restore(const char* backup_path, int restore_boot, int restore_system, int restore_data, int restore_cache, int restore_sdext, int restore_wimax) |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 527 | { |
| 528 | ui_set_background(BACKGROUND_ICON_INSTALLING); |
| 529 | ui_show_indeterminate_progress(); |
| 530 | yaffs_files_total = 0; |
| 531 | |
Koushik Dutta | 89ed0b7 | 2011-10-26 21:25:34 -0700 | [diff] [blame] | 532 | if (ensure_path_mounted(backup_path) != 0) |
| 533 | return print_and_error("Can't mount backup path\n"); |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 534 | |
| 535 | char tmp[PATH_MAX]; |
| 536 | |
| 537 | ui_print("Checking MD5 sums...\n"); |
Koushik K. Dutta | face588 | 2010-03-13 10:15:55 -0800 | [diff] [blame] | 538 | sprintf(tmp, "cd %s && md5sum -c nandroid.md5", backup_path); |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 539 | if (0 != __system(tmp)) |
| 540 | return print_and_error("MD5 mismatch!\n"); |
| 541 | |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 542 | int ret; |
Koushik Dutta | 53c9bd8 | 2011-01-04 11:38:31 -0800 | [diff] [blame] | 543 | |
| 544 | if (restore_boot && NULL != volume_for_path("/boot") && 0 != (ret = nandroid_restore_partition(backup_path, "/boot"))) |
| 545 | return ret; |
Koushik K. Dutta | f721594 | 2010-03-16 13:34:51 -0700 | [diff] [blame] | 546 | |
Koushik Dutta | 5ee98b2 | 2011-03-02 19:35:05 -0800 | [diff] [blame] | 547 | struct stat s; |
| 548 | Volume *vol = volume_for_path("/wimax"); |
| 549 | if (restore_wimax && vol != NULL && 0 == stat(vol->device, &s)) |
agrabren | 1f76a5d | 2010-12-29 12:15:18 -0600 | [diff] [blame] | 550 | { |
Joshua Wise | 05d4a09 | 2010-12-31 02:05:35 -0500 | [diff] [blame] | 551 | char serialno[PROPERTY_VALUE_MAX]; |
| 552 | |
| 553 | serialno[0] = 0; |
| 554 | property_get("ro.serialno", serialno, ""); |
| 555 | sprintf(tmp, "%s/wimax.%s.img", backup_path, serialno); |
agrabren | 1f76a5d | 2010-12-29 12:15:18 -0600 | [diff] [blame] | 556 | |
| 557 | struct stat st; |
| 558 | if (0 != stat(tmp, &st)) |
| 559 | { |
Joshua Wise | ca889ec | 2010-12-30 02:48:25 -0500 | [diff] [blame] | 560 | ui_print("WARNING: WiMAX partition exists, but nandroid\n"); |
| 561 | ui_print(" backup does not contain WiMAX image.\n"); |
agrabren | 1f76a5d | 2010-12-29 12:15:18 -0600 | [diff] [blame] | 562 | ui_print(" You should create a new backup to\n"); |
Joshua Wise | ca889ec | 2010-12-30 02:48:25 -0500 | [diff] [blame] | 563 | ui_print(" protect your WiMAX keys.\n"); |
agrabren | 1f76a5d | 2010-12-29 12:15:18 -0600 | [diff] [blame] | 564 | } |
| 565 | else |
| 566 | { |
Joshua Wise | 5a3f1d8 | 2010-12-31 02:16:00 -0500 | [diff] [blame] | 567 | ui_print("Erasing WiMAX before restore...\n"); |
Koushik Dutta | a25deae | 2011-01-01 18:17:48 -0800 | [diff] [blame] | 568 | if (0 != (ret = format_volume("/wimax"))) |
Joshua Wise | 5a3f1d8 | 2010-12-31 02:16:00 -0500 | [diff] [blame] | 569 | return print_and_error("Error while formatting wimax!\n"); |
Joshua Wise | 05d4a09 | 2010-12-31 02:05:35 -0500 | [diff] [blame] | 570 | ui_print("Restoring WiMAX image...\n"); |
Koushik Dutta | e62132b | 2011-05-26 11:29:29 -0700 | [diff] [blame] | 571 | if (0 != (ret = restore_raw_partition(vol->fs_type, vol->device, tmp))) |
agrabren | 1f76a5d | 2010-12-29 12:15:18 -0600 | [diff] [blame] | 572 | return ret; |
| 573 | } |
| 574 | } |
| 575 | |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 576 | if (restore_system && 0 != (ret = nandroid_restore_partition(backup_path, "/system"))) |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 577 | return ret; |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 578 | |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 579 | if (restore_data && 0 != (ret = nandroid_restore_partition(backup_path, "/data"))) |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 580 | return ret; |
Koushik Dutta | 8b5e185 | 2010-06-14 22:04:22 -0700 | [diff] [blame] | 581 | |
Koushik Dutta | 5460f0c | 2010-12-18 22:37:49 -0800 | [diff] [blame] | 582 | if (has_datadata()) { |
| 583 | if (restore_data && 0 != (ret = nandroid_restore_partition(backup_path, "/datadata"))) |
| 584 | return ret; |
| 585 | } |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 586 | |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 587 | if (restore_data && 0 != (ret = nandroid_restore_partition_extended(backup_path, "/sdcard/.android_secure", 0))) |
Koushik Dutta | 5aaa823 | 2010-07-20 16:23:18 -0700 | [diff] [blame] | 588 | return ret; |
Koushik Dutta | 062d6b0 | 2010-07-03 13:54:21 -0700 | [diff] [blame] | 589 | |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 590 | if (restore_cache && 0 != (ret = nandroid_restore_partition_extended(backup_path, "/cache", 0))) |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 591 | return ret; |
| 592 | |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 593 | if (restore_sdext && 0 != (ret = nandroid_restore_partition(backup_path, "/sd-ext"))) |
Koushik Dutta | 5aaa823 | 2010-07-20 16:23:18 -0700 | [diff] [blame] | 594 | return ret; |
Koushik K. Dutta | 68b0190 | 2010-04-01 12:20:39 -0700 | [diff] [blame] | 595 | |
Koushik K. Dutta | 3f38f32 | 2010-03-12 23:45:25 -0800 | [diff] [blame] | 596 | sync(); |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 597 | ui_set_background(BACKGROUND_ICON_NONE); |
| 598 | ui_reset_progress(); |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 599 | ui_print("\nRestore complete!\n"); |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 600 | return 0; |
| 601 | } |
Koushik Dutta | 0837091 | 2010-06-26 12:25:02 -0700 | [diff] [blame] | 602 | |
Koushik Dutta | 0837091 | 2010-06-26 12:25:02 -0700 | [diff] [blame] | 603 | int nandroid_usage() |
| 604 | { |
| 605 | printf("Usage: nandroid backup\n"); |
| 606 | printf("Usage: nandroid restore <directory>\n"); |
| 607 | return 1; |
| 608 | } |
| 609 | |
| 610 | int nandroid_main(int argc, char** argv) |
| 611 | { |
| 612 | if (argc > 3 || argc < 2) |
| 613 | return nandroid_usage(); |
| 614 | |
| 615 | if (strcmp("backup", argv[1]) == 0) |
| 616 | { |
| 617 | if (argc != 2) |
| 618 | return nandroid_usage(); |
| 619 | |
| 620 | char backup_path[PATH_MAX]; |
| 621 | nandroid_generate_timestamp_path(backup_path); |
| 622 | return nandroid_backup(backup_path); |
| 623 | } |
| 624 | |
| 625 | if (strcmp("restore", argv[1]) == 0) |
| 626 | { |
| 627 | if (argc != 3) |
| 628 | return nandroid_usage(); |
Koushik Dutta | 5b7f34a | 2010-12-29 23:36:03 -0800 | [diff] [blame] | 629 | return nandroid_restore(argv[2], 1, 1, 1, 1, 1, 0); |
Koushik Dutta | 0837091 | 2010-06-26 12:25:02 -0700 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | return nandroid_usage(); |
| 633 | } |