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 | 7adeadc | 2011-05-26 11:47:56 -0700 | [diff] [blame] | 40 | #include "flashutils/flashutils.h" |
| 41 | #include <libgen.h> |
| 42 | |
| 43 | |
| 44 | void nandroid_generate_timestamp_path(const char* backup_path) |
| 45 | { |
| 46 | time_t t = time(NULL); |
| 47 | struct tm *tmp = localtime(&t); |
| 48 | if (tmp == NULL) |
| 49 | { |
| 50 | struct timeval tp; |
| 51 | gettimeofday(&tp, NULL); |
| 52 | sprintf(backup_path, "/sdcard/clockworkmod/backup/%d", tp.tv_sec); |
| 53 | } |
| 54 | else |
| 55 | { |
| 56 | strftime(backup_path, PATH_MAX, "/sdcard/clockworkmod/backup/%F.%H.%M.%S", tmp); |
| 57 | } |
| 58 | } |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 59 | |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 60 | int print_and_error(const char* message) { |
| 61 | ui_print("%s", message); |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 62 | return 1; |
| 63 | } |
| 64 | |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 65 | int yaffs_files_total = 0; |
| 66 | int yaffs_files_count = 0; |
Koushik Dutta | 7adeadc | 2011-05-26 11:47:56 -0700 | [diff] [blame] | 67 | void yaffs_callback(const char* filename) |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 68 | { |
| 69 | char* justfile = basename(filename); |
| 70 | if (strlen(justfile) < 30) |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 71 | ui_print("%s", justfile); |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 72 | yaffs_files_count++; |
| 73 | if (yaffs_files_total != 0) |
| 74 | ui_set_progress((float)yaffs_files_count / (float)yaffs_files_total); |
| 75 | ui_reset_text_col(); |
| 76 | } |
| 77 | |
Koushik Dutta | 7adeadc | 2011-05-26 11:47:56 -0700 | [diff] [blame] | 78 | void compute_directory_stats(const char* directory) |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 79 | { |
| 80 | char tmp[PATH_MAX]; |
| 81 | sprintf(tmp, "find %s | wc -l > /tmp/dircount", directory); |
| 82 | __system(tmp); |
| 83 | char count_text[100]; |
| 84 | FILE* f = fopen("/tmp/dircount", "r"); |
| 85 | fread(count_text, 1, sizeof(count_text), f); |
| 86 | fclose(f); |
| 87 | yaffs_files_count = 0; |
| 88 | yaffs_files_total = atoi(count_text); |
| 89 | ui_reset_progress(); |
| 90 | ui_show_progress(1, 0); |
| 91 | } |
| 92 | |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 93 | 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] | 94 | int ret = 0; |
Koushik K. Dutta | 68b0190 | 2010-04-01 12:20:39 -0700 | [diff] [blame] | 95 | char* name = basename(mount_point); |
Koushik Dutta | 58131e7 | 2010-06-09 12:41:20 -0700 | [diff] [blame] | 96 | |
| 97 | struct stat file_info; |
| 98 | mkyaffs2image_callback callback = NULL; |
| 99 | if (0 != stat("/sdcard/clockworkmod/.hidenandroidprogress", &file_info)) { |
| 100 | callback = yaffs_callback; |
| 101 | } |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 102 | |
| 103 | ui_print("Backing up %s...\n", name); |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 104 | if (0 != (ret = ensure_path_mounted(mount_point) != 0)) { |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 105 | ui_print("Can't mount %s!\n", mount_point); |
| 106 | return ret; |
| 107 | } |
| 108 | compute_directory_stats(mount_point); |
| 109 | char tmp[PATH_MAX]; |
| 110 | sprintf(tmp, "%s/%s.img", backup_path, name); |
Koushik Dutta | 58131e7 | 2010-06-09 12:41:20 -0700 | [diff] [blame] | 111 | ret = mkyaffs2image(mount_point, tmp, 0, callback); |
Koushik Dutta | 062d6b0 | 2010-07-03 13:54:21 -0700 | [diff] [blame] | 112 | if (umount_when_finished) { |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 113 | ensure_path_unmounted(mount_point); |
Koushik Dutta | 062d6b0 | 2010-07-03 13:54:21 -0700 | [diff] [blame] | 114 | } |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 115 | if (0 != ret) { |
| 116 | ui_print("Error while making a yaffs2 image of %s!\n", mount_point); |
| 117 | return ret; |
| 118 | } |
| 119 | return 0; |
| 120 | } |
| 121 | |
Koushik Dutta | 53c9bd8 | 2011-01-04 11:38:31 -0800 | [diff] [blame] | 122 | int nandroid_backup_partition(const char* backup_path, const char* root) { |
| 123 | Volume *vol = volume_for_path(root); |
| 124 | // make sure the volume exists before attempting anything... |
| 125 | if (vol == NULL || vol->fs_type == NULL) |
| 126 | return NULL; |
| 127 | |
| 128 | // see if we need a raw backup (mtd) |
| 129 | char tmp[PATH_MAX]; |
| 130 | int ret; |
Christopher Lais | 066a102 | 2011-01-16 06:02:34 -0600 | [diff] [blame] | 131 | if (strcmp(vol->fs_type, "mtd") == 0 || |
Koushik Dutta | 33e37f3 | 2011-02-27 16:33:32 -0800 | [diff] [blame] | 132 | strcmp(vol->fs_type, "bml") == 0 || |
Christopher Lais | 066a102 | 2011-01-16 06:02:34 -0600 | [diff] [blame] | 133 | strcmp(vol->fs_type, "emmc") == 0) { |
Koushik Dutta | 53c9bd8 | 2011-01-04 11:38:31 -0800 | [diff] [blame] | 134 | const char* name = basename(root); |
| 135 | sprintf(tmp, "%s/%s.img", backup_path, name); |
| 136 | ui_print("Backing up %s image...\n", name); |
Koushik Dutta | 8a6bc77 | 2011-05-26 11:14:15 -0700 | [diff] [blame] | 137 | if (0 != (ret = backup_raw_partition(vol->fs_type, vol->device, tmp))) { |
Koushik Dutta | 53c9bd8 | 2011-01-04 11:38:31 -0800 | [diff] [blame] | 138 | ui_print("Error while backing up %s image!", name); |
| 139 | return ret; |
| 140 | } |
| 141 | return 0; |
| 142 | } |
| 143 | |
Koushik Dutta | 062d6b0 | 2010-07-03 13:54:21 -0700 | [diff] [blame] | 144 | return nandroid_backup_partition_extended(backup_path, root, 1); |
| 145 | } |
| 146 | |
Koushik Dutta | 2f73e58 | 2010-04-18 16:00:21 -0700 | [diff] [blame] | 147 | int nandroid_backup(const char* backup_path) |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 148 | { |
| 149 | ui_set_background(BACKGROUND_ICON_INSTALLING); |
| 150 | |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 151 | if (ensure_path_mounted("/sdcard") != 0) |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 152 | return print_and_error("Can't mount /sdcard\n"); |
| 153 | |
Koushik K. Dutta | 7868629 | 2010-03-25 17:58:45 -0700 | [diff] [blame] | 154 | int ret; |
Koushik K. Dutta | f3534d0 | 2010-04-18 18:06:24 -0700 | [diff] [blame] | 155 | struct statfs s; |
Koushik K. Dutta | 7868629 | 2010-03-25 17:58:45 -0700 | [diff] [blame] | 156 | if (0 != (ret = statfs("/sdcard", &s))) |
| 157 | return print_and_error("Unable to stat /sdcard\n"); |
Koushik K. Dutta | 68b0190 | 2010-04-01 12:20:39 -0700 | [diff] [blame] | 158 | uint64_t bavail = s.f_bavail; |
| 159 | uint64_t bsize = s.f_bsize; |
| 160 | uint64_t sdcard_free = bavail * bsize; |
| 161 | uint64_t sdcard_free_mb = sdcard_free / (uint64_t)(1024 * 1024); |
| 162 | ui_print("SD Card space free: %lluMB\n", sdcard_free_mb); |
| 163 | if (sdcard_free_mb < 150) |
Koushik K. Dutta | c097093 | 2010-03-25 23:19:19 -0700 | [diff] [blame] | 164 | ui_print("There may not be enough free space to complete backup... continuing...\n"); |
Koushik K. Dutta | 7868629 | 2010-03-25 17:58:45 -0700 | [diff] [blame] | 165 | |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 166 | char tmp[PATH_MAX]; |
| 167 | sprintf(tmp, "mkdir -p %s", backup_path); |
| 168 | __system(tmp); |
| 169 | |
Koushik Dutta | 53c9bd8 | 2011-01-04 11:38:31 -0800 | [diff] [blame] | 170 | if (0 != (ret = nandroid_backup_partition(backup_path, "/boot"))) |
| 171 | return ret; |
Koushik K. Dutta | 6923cc3 | 2010-03-29 14:46:00 -0700 | [diff] [blame] | 172 | |
Koushik Dutta | 53c9bd8 | 2011-01-04 11:38:31 -0800 | [diff] [blame] | 173 | if (0 != (ret = nandroid_backup_partition(backup_path, "/recovery"))) |
| 174 | return ret; |
Koushik K. Dutta | 6923cc3 | 2010-03-29 14:46:00 -0700 | [diff] [blame] | 175 | |
Koushik Dutta | 5ee98b2 | 2011-03-02 19:35:05 -0800 | [diff] [blame] | 176 | Volume *vol = volume_for_path("/wimax"); |
| 177 | if (vol != NULL && 0 == stat(vol->device, &s)) |
agrabren | 1f76a5d | 2010-12-29 12:15:18 -0600 | [diff] [blame] | 178 | { |
Joshua Wise | 05d4a09 | 2010-12-31 02:05:35 -0500 | [diff] [blame] | 179 | char serialno[PROPERTY_VALUE_MAX]; |
Joshua Wise | ca889ec | 2010-12-30 02:48:25 -0500 | [diff] [blame] | 180 | ui_print("Backing up WiMAX...\n"); |
Joshua Wise | 05d4a09 | 2010-12-31 02:05:35 -0500 | [diff] [blame] | 181 | serialno[0] = 0; |
| 182 | property_get("ro.serialno", serialno, ""); |
| 183 | sprintf(tmp, "%s/wimax.%s.img", backup_path, serialno); |
Koushik Dutta | e62132b | 2011-05-26 11:29:29 -0700 | [diff] [blame] | 184 | ret = backup_raw_partition(vol->fs_type, vol->device, tmp); |
agrabren | 1f76a5d | 2010-12-29 12:15:18 -0600 | [diff] [blame] | 185 | if (0 != ret) |
Joshua Wise | ca889ec | 2010-12-30 02:48:25 -0500 | [diff] [blame] | 186 | return print_and_error("Error while dumping WiMAX image!\n"); |
agrabren | 1f76a5d | 2010-12-29 12:15:18 -0600 | [diff] [blame] | 187 | } |
| 188 | |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 189 | if (0 != (ret = nandroid_backup_partition(backup_path, "/system"))) |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 190 | return ret; |
| 191 | |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 192 | if (0 != (ret = nandroid_backup_partition(backup_path, "/data"))) |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 193 | return ret; |
| 194 | |
Koushik Dutta | 5460f0c | 2010-12-18 22:37:49 -0800 | [diff] [blame] | 195 | if (has_datadata()) { |
| 196 | if (0 != (ret = nandroid_backup_partition(backup_path, "/datadata"))) |
| 197 | return ret; |
| 198 | } |
Koushik Dutta | 8b5e185 | 2010-06-14 22:04:22 -0700 | [diff] [blame] | 199 | |
Koushik Dutta | 5ee98b2 | 2011-03-02 19:35:05 -0800 | [diff] [blame] | 200 | if (0 != stat("/sdcard/.android_secure", &s)) |
Koushik Dutta | 062d6b0 | 2010-07-03 13:54:21 -0700 | [diff] [blame] | 201 | { |
| 202 | ui_print("No /sdcard/.android_secure found. Skipping backup of applications on external storage.\n"); |
| 203 | } |
| 204 | else |
| 205 | { |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 206 | 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] | 207 | return ret; |
| 208 | } |
| 209 | |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 210 | if (0 != (ret = nandroid_backup_partition_extended(backup_path, "/cache", 0))) |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 211 | return ret; |
Koushik K. Dutta | 3f99539 | 2010-03-30 23:29:43 -0700 | [diff] [blame] | 212 | |
Koushik Dutta | 5ee98b2 | 2011-03-02 19:35:05 -0800 | [diff] [blame] | 213 | vol = volume_for_path("/sd-ext"); |
| 214 | if (vol == NULL || 0 != stat(vol->device, &s)) |
Koushik K. Dutta | 6771aca | 2010-04-03 23:28:39 -0700 | [diff] [blame] | 215 | { |
Koushik K. Dutta | 71ef11f | 2010-04-02 22:17:42 -0700 | [diff] [blame] | 216 | 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] | 217 | } |
| 218 | else |
| 219 | { |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 220 | if (0 != ensure_path_mounted("/sd-ext")) |
Koushik Dutta | 8b5e185 | 2010-06-14 22:04:22 -0700 | [diff] [blame] | 221 | 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] | 222 | else if (0 != (ret = nandroid_backup_partition(backup_path, "/sd-ext"))) |
Koushik K. Dutta | 6771aca | 2010-04-03 23:28:39 -0700 | [diff] [blame] | 223 | return ret; |
| 224 | } |
Koushik Dutta | 062d6b0 | 2010-07-03 13:54:21 -0700 | [diff] [blame] | 225 | |
Koushik K. Dutta | d717892 | 2010-03-24 14:38:40 -0700 | [diff] [blame] | 226 | ui_print("Generating md5 sum...\n"); |
Koushik K. Dutta | 68b0190 | 2010-04-01 12:20:39 -0700 | [diff] [blame] | 227 | sprintf(tmp, "nandroid-md5.sh %s", backup_path); |
Koushik K. Dutta | b0a2503 | 2010-03-24 10:34:38 -0700 | [diff] [blame] | 228 | if (0 != (ret = __system(tmp))) { |
| 229 | ui_print("Error while generating md5 sum!\n"); |
| 230 | return ret; |
| 231 | } |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 232 | |
Koushik K. Dutta | 3f38f32 | 2010-03-12 23:45:25 -0800 | [diff] [blame] | 233 | sync(); |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 234 | ui_set_background(BACKGROUND_ICON_NONE); |
| 235 | ui_reset_progress(); |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 236 | ui_print("\nBackup complete!\n"); |
| 237 | return 0; |
| 238 | } |
| 239 | |
Koushik K. Dutta | 68b0190 | 2010-04-01 12:20:39 -0700 | [diff] [blame] | 240 | typedef int (*format_function)(char* root); |
| 241 | |
Koushik Dutta | 5aaa823 | 2010-07-20 16:23:18 -0700 | [diff] [blame] | 242 | static void ensure_directory(const char* dir) { |
| 243 | char tmp[PATH_MAX]; |
| 244 | sprintf(tmp, "mkdir -p %s", dir); |
| 245 | __system(tmp); |
| 246 | } |
| 247 | |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 248 | 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] | 249 | int ret = 0; |
Koushik K. Dutta | 68b0190 | 2010-04-01 12:20:39 -0700 | [diff] [blame] | 250 | char* name = basename(mount_point); |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 251 | |
Koushik Dutta | 5aaa823 | 2010-07-20 16:23:18 -0700 | [diff] [blame] | 252 | char tmp[PATH_MAX]; |
| 253 | sprintf(tmp, "%s/%s.img", backup_path, name); |
Koushik Dutta | 58131e7 | 2010-06-09 12:41:20 -0700 | [diff] [blame] | 254 | struct stat file_info; |
Koushik Dutta | 5aaa823 | 2010-07-20 16:23:18 -0700 | [diff] [blame] | 255 | if (0 != (ret = statfs(tmp, &file_info))) { |
Koushik Dutta | d4060c3 | 2010-07-22 20:14:44 -0700 | [diff] [blame] | 256 | ui_print("%s.img not found. Skipping restore of %s.\n", name, mount_point); |
Koushik Dutta | 5aaa823 | 2010-07-20 16:23:18 -0700 | [diff] [blame] | 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | ensure_directory(mount_point); |
| 261 | |
Koushik Dutta | 58131e7 | 2010-06-09 12:41:20 -0700 | [diff] [blame] | 262 | unyaffs_callback callback = NULL; |
| 263 | if (0 != stat("/sdcard/clockworkmod/.hidenandroidprogress", &file_info)) { |
| 264 | callback = yaffs_callback; |
| 265 | } |
| 266 | |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 267 | ui_print("Restoring %s...\n", name); |
Koushik Dutta | 852bb42 | 2010-07-24 11:18:00 -0700 | [diff] [blame] | 268 | /* |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 269 | if (0 != (ret = ensure_root_path_unmounted(root))) { |
| 270 | ui_print("Can't unmount %s!\n", mount_point); |
| 271 | return ret; |
| 272 | } |
Koushik Dutta | 852bb42 | 2010-07-24 11:18:00 -0700 | [diff] [blame] | 273 | */ |
Koushik Dutta | bf4444f | 2010-12-18 18:57:47 -0800 | [diff] [blame] | 274 | if (0 != (ret = format_volume(mount_point))) { |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 275 | ui_print("Error while formatting %s!\n", mount_point); |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 276 | return ret; |
| 277 | } |
| 278 | |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 279 | if (0 != (ret = ensure_path_mounted(mount_point))) { |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 280 | ui_print("Can't mount %s!\n", mount_point); |
| 281 | return ret; |
| 282 | } |
| 283 | |
Koushik Dutta | 58131e7 | 2010-06-09 12:41:20 -0700 | [diff] [blame] | 284 | if (0 != (ret = unyaffs(tmp, mount_point, callback))) { |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 285 | ui_print("Error while restoring %s!\n", mount_point); |
| 286 | return ret; |
| 287 | } |
Koushik Dutta | 2f73e58 | 2010-04-18 16:00:21 -0700 | [diff] [blame] | 288 | |
Koushik Dutta | 062d6b0 | 2010-07-03 13:54:21 -0700 | [diff] [blame] | 289 | if (umount_when_finished) { |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 290 | ensure_path_unmounted(mount_point); |
Koushik Dutta | 2f73e58 | 2010-04-18 16:00:21 -0700 | [diff] [blame] | 291 | } |
Koushik Dutta | 062d6b0 | 2010-07-03 13:54:21 -0700 | [diff] [blame] | 292 | |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 293 | return 0; |
| 294 | } |
| 295 | |
Koushik Dutta | 062d6b0 | 2010-07-03 13:54:21 -0700 | [diff] [blame] | 296 | int nandroid_restore_partition(const char* backup_path, const char* root) { |
Koushik Dutta | 53c9bd8 | 2011-01-04 11:38:31 -0800 | [diff] [blame] | 297 | Volume *vol = volume_for_path(root); |
| 298 | // make sure the volume exists... |
| 299 | if (vol == NULL || vol->fs_type == NULL) |
| 300 | return 0; |
| 301 | |
| 302 | // see if we need a raw restore (mtd) |
| 303 | char tmp[PATH_MAX]; |
Christopher Lais | 066a102 | 2011-01-16 06:02:34 -0600 | [diff] [blame] | 304 | if (strcmp(vol->fs_type, "mtd") == 0 || |
Koushik Dutta | 33e37f3 | 2011-02-27 16:33:32 -0800 | [diff] [blame] | 305 | strcmp(vol->fs_type, "bml") == 0 || |
Christopher Lais | 066a102 | 2011-01-16 06:02:34 -0600 | [diff] [blame] | 306 | strcmp(vol->fs_type, "emmc") == 0) { |
Koushik Dutta | 53c9bd8 | 2011-01-04 11:38:31 -0800 | [diff] [blame] | 307 | int ret; |
| 308 | const char* name = basename(root); |
| 309 | ui_print("Erasing %s before restore...\n", name); |
| 310 | if (0 != (ret = format_volume(root))) { |
| 311 | ui_print("Error while erasing %s image!", name); |
| 312 | return ret; |
| 313 | } |
| 314 | sprintf(tmp, "%s%s.img", backup_path, root); |
| 315 | ui_print("Restoring %s image...\n", name); |
Koushik Dutta | 8a6bc77 | 2011-05-26 11:14:15 -0700 | [diff] [blame] | 316 | if (0 != (ret = restore_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 flashing %s image!", name); |
| 318 | return ret; |
| 319 | } |
| 320 | return 0; |
| 321 | } |
Koushik Dutta | 062d6b0 | 2010-07-03 13:54:21 -0700 | [diff] [blame] | 322 | return nandroid_restore_partition_extended(backup_path, root, 1); |
| 323 | } |
| 324 | |
agrabren | 1f76a5d | 2010-12-29 12:15:18 -0600 | [diff] [blame] | 325 | 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] | 326 | { |
| 327 | ui_set_background(BACKGROUND_ICON_INSTALLING); |
| 328 | ui_show_indeterminate_progress(); |
| 329 | yaffs_files_total = 0; |
| 330 | |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 331 | if (ensure_path_mounted("/sdcard") != 0) |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 332 | return print_and_error("Can't mount /sdcard\n"); |
| 333 | |
| 334 | char tmp[PATH_MAX]; |
| 335 | |
| 336 | ui_print("Checking MD5 sums...\n"); |
Koushik K. Dutta | face588 | 2010-03-13 10:15:55 -0800 | [diff] [blame] | 337 | sprintf(tmp, "cd %s && md5sum -c nandroid.md5", backup_path); |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 338 | if (0 != __system(tmp)) |
| 339 | return print_and_error("MD5 mismatch!\n"); |
| 340 | |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 341 | int ret; |
Koushik Dutta | 53c9bd8 | 2011-01-04 11:38:31 -0800 | [diff] [blame] | 342 | |
| 343 | if (restore_boot && NULL != volume_for_path("/boot") && 0 != (ret = nandroid_restore_partition(backup_path, "/boot"))) |
| 344 | return ret; |
Koushik K. Dutta | f721594 | 2010-03-16 13:34:51 -0700 | [diff] [blame] | 345 | |
Koushik Dutta | 5ee98b2 | 2011-03-02 19:35:05 -0800 | [diff] [blame] | 346 | struct stat s; |
| 347 | Volume *vol = volume_for_path("/wimax"); |
| 348 | if (restore_wimax && vol != NULL && 0 == stat(vol->device, &s)) |
agrabren | 1f76a5d | 2010-12-29 12:15:18 -0600 | [diff] [blame] | 349 | { |
Joshua Wise | 05d4a09 | 2010-12-31 02:05:35 -0500 | [diff] [blame] | 350 | char serialno[PROPERTY_VALUE_MAX]; |
| 351 | |
| 352 | serialno[0] = 0; |
| 353 | property_get("ro.serialno", serialno, ""); |
| 354 | sprintf(tmp, "%s/wimax.%s.img", backup_path, serialno); |
agrabren | 1f76a5d | 2010-12-29 12:15:18 -0600 | [diff] [blame] | 355 | |
| 356 | struct stat st; |
| 357 | if (0 != stat(tmp, &st)) |
| 358 | { |
Joshua Wise | ca889ec | 2010-12-30 02:48:25 -0500 | [diff] [blame] | 359 | ui_print("WARNING: WiMAX partition exists, but nandroid\n"); |
| 360 | ui_print(" backup does not contain WiMAX image.\n"); |
agrabren | 1f76a5d | 2010-12-29 12:15:18 -0600 | [diff] [blame] | 361 | ui_print(" You should create a new backup to\n"); |
Joshua Wise | ca889ec | 2010-12-30 02:48:25 -0500 | [diff] [blame] | 362 | ui_print(" protect your WiMAX keys.\n"); |
agrabren | 1f76a5d | 2010-12-29 12:15:18 -0600 | [diff] [blame] | 363 | } |
| 364 | else |
| 365 | { |
Joshua Wise | 5a3f1d8 | 2010-12-31 02:16:00 -0500 | [diff] [blame] | 366 | ui_print("Erasing WiMAX before restore...\n"); |
Koushik Dutta | a25deae | 2011-01-01 18:17:48 -0800 | [diff] [blame] | 367 | if (0 != (ret = format_volume("/wimax"))) |
Joshua Wise | 5a3f1d8 | 2010-12-31 02:16:00 -0500 | [diff] [blame] | 368 | return print_and_error("Error while formatting wimax!\n"); |
Joshua Wise | 05d4a09 | 2010-12-31 02:05:35 -0500 | [diff] [blame] | 369 | ui_print("Restoring WiMAX image...\n"); |
Koushik Dutta | e62132b | 2011-05-26 11:29:29 -0700 | [diff] [blame] | 370 | if (0 != (ret = restore_raw_partition(vol->fs_type, vol->device, tmp))) |
agrabren | 1f76a5d | 2010-12-29 12:15:18 -0600 | [diff] [blame] | 371 | return ret; |
| 372 | } |
| 373 | } |
| 374 | |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 375 | if (restore_system && 0 != (ret = nandroid_restore_partition(backup_path, "/system"))) |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 376 | return ret; |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 377 | |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 378 | if (restore_data && 0 != (ret = nandroid_restore_partition(backup_path, "/data"))) |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 379 | return ret; |
Koushik Dutta | 8b5e185 | 2010-06-14 22:04:22 -0700 | [diff] [blame] | 380 | |
Koushik Dutta | 5460f0c | 2010-12-18 22:37:49 -0800 | [diff] [blame] | 381 | if (has_datadata()) { |
| 382 | if (restore_data && 0 != (ret = nandroid_restore_partition(backup_path, "/datadata"))) |
| 383 | return ret; |
| 384 | } |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 385 | |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 386 | 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] | 387 | return ret; |
Koushik Dutta | 062d6b0 | 2010-07-03 13:54:21 -0700 | [diff] [blame] | 388 | |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 389 | 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] | 390 | return ret; |
| 391 | |
Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 392 | if (restore_sdext && 0 != (ret = nandroid_restore_partition(backup_path, "/sd-ext"))) |
Koushik Dutta | 5aaa823 | 2010-07-20 16:23:18 -0700 | [diff] [blame] | 393 | return ret; |
Koushik K. Dutta | 68b0190 | 2010-04-01 12:20:39 -0700 | [diff] [blame] | 394 | |
Koushik K. Dutta | 3f38f32 | 2010-03-12 23:45:25 -0800 | [diff] [blame] | 395 | sync(); |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 396 | ui_set_background(BACKGROUND_ICON_NONE); |
| 397 | ui_reset_progress(); |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 398 | ui_print("\nRestore complete!\n"); |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 399 | return 0; |
| 400 | } |
Koushik Dutta | 0837091 | 2010-06-26 12:25:02 -0700 | [diff] [blame] | 401 | |
Koushik Dutta | 0837091 | 2010-06-26 12:25:02 -0700 | [diff] [blame] | 402 | int nandroid_usage() |
| 403 | { |
| 404 | printf("Usage: nandroid backup\n"); |
| 405 | printf("Usage: nandroid restore <directory>\n"); |
| 406 | return 1; |
| 407 | } |
| 408 | |
| 409 | int nandroid_main(int argc, char** argv) |
| 410 | { |
| 411 | if (argc > 3 || argc < 2) |
| 412 | return nandroid_usage(); |
| 413 | |
| 414 | if (strcmp("backup", argv[1]) == 0) |
| 415 | { |
| 416 | if (argc != 2) |
| 417 | return nandroid_usage(); |
| 418 | |
| 419 | char backup_path[PATH_MAX]; |
| 420 | nandroid_generate_timestamp_path(backup_path); |
| 421 | return nandroid_backup(backup_path); |
| 422 | } |
| 423 | |
| 424 | if (strcmp("restore", argv[1]) == 0) |
| 425 | { |
| 426 | if (argc != 3) |
| 427 | return nandroid_usage(); |
Koushik Dutta | 5b7f34a | 2010-12-29 23:36:03 -0800 | [diff] [blame] | 428 | return nandroid_restore(argv[2], 1, 1, 1, 1, 1, 0); |
Koushik Dutta | 0837091 | 2010-06-26 12:25:02 -0700 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | return nandroid_usage(); |
| 432 | } |