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