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