preludedrew | 38058dc | 2011-01-29 23:30:44 -0700 | [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 | |
| 33 | #include "../../external/yaffs2/yaffs2/utils/mkyaffs2image.h" |
| 34 | #include "../../external/yaffs2/yaffs2/utils/unyaffs.h" |
| 35 | |
| 36 | #include <sys/vfs.h> |
| 37 | |
| 38 | #include "extendedcommands.h" |
| 39 | #include "nandroid.h" |
| 40 | |
| 41 | int print_and_error(const char* message) { |
| 42 | ui_print("%s", message); |
| 43 | return 1; |
| 44 | } |
| 45 | |
| 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) |
| 52 | ui_print("%s", justfile); |
| 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 | |
| 74 | int nandroid_backup_partition_extended(const char* backup_path, const char* mount_point, int umount_when_finished) { |
| 75 | int ret = 0; |
| 76 | char* name = basename(mount_point); |
| 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 | } |
| 83 | |
| 84 | ui_print("Backing up %s...\n", name); |
| 85 | if (0 != (ret = ensure_path_mounted(mount_point) != 0)) { |
| 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); |
| 92 | ret = mkyaffs2image(mount_point, tmp, 0, callback); |
| 93 | if (umount_when_finished) { |
| 94 | ensure_path_unmounted(mount_point); |
| 95 | } |
| 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 | |
| 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; |
| 112 | if (strcmp(vol->fs_type, "mtd") == 0 || |
| 113 | strcmp(vol->fs_type, "emmc") == 0) { |
| 114 | const char* name = basename(root); |
| 115 | sprintf(tmp, "%s/%s.img", backup_path, name); |
| 116 | ui_print("Backing up %s image...\n", name); |
| 117 | if (0 != (ret = backup_raw_partition(vol->device, tmp))) { |
| 118 | ui_print("Error while backing up %s image!", name); |
| 119 | return ret; |
| 120 | } |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | return nandroid_backup_partition_extended(backup_path, root, 1); |
| 125 | } |
| 126 | |
| 127 | int nandroid_backup(const char* backup_path) |
| 128 | { |
| 129 | ui_set_background(BACKGROUND_ICON_INSTALLING); |
| 130 | |
| 131 | if (ensure_path_mounted("/sdcard") != 0) |
| 132 | return print_and_error("Can't mount /sdcard\n"); |
| 133 | |
| 134 | int ret; |
| 135 | struct statfs s; |
| 136 | if (0 != (ret = statfs("/sdcard", &s))) |
| 137 | return print_and_error("Unable to stat /sdcard\n"); |
| 138 | uint64_t bavail = s.f_bavail; |
| 139 | uint64_t bsize = s.f_bsize; |
| 140 | uint64_t sdcard_free = bavail * bsize; |
| 141 | uint64_t sdcard_free_mb = sdcard_free / (uint64_t)(1024 * 1024); |
| 142 | ui_print("SD Card space free: %lluMB\n", sdcard_free_mb); |
| 143 | if (sdcard_free_mb < 150) |
| 144 | ui_print("There may not be enough free space to complete backup... continuing...\n"); |
| 145 | |
| 146 | char tmp[PATH_MAX]; |
| 147 | sprintf(tmp, "mkdir -p %s", backup_path); |
| 148 | __system(tmp); |
| 149 | |
| 150 | if (0 != (ret = nandroid_backup_partition(backup_path, "/boot"))) |
| 151 | return ret; |
| 152 | |
| 153 | if (0 != (ret = nandroid_backup_partition(backup_path, "/recovery"))) |
| 154 | return ret; |
| 155 | |
| 156 | if (0 == (ret = get_partition_device("wimax", tmp))) |
| 157 | { |
| 158 | char serialno[PROPERTY_VALUE_MAX]; |
| 159 | ui_print("Backing up WiMAX...\n"); |
| 160 | serialno[0] = 0; |
| 161 | property_get("ro.serialno", serialno, ""); |
| 162 | sprintf(tmp, "%s/wimax.%s.img", backup_path, serialno); |
| 163 | ret = backup_raw_partition("wimax", tmp); |
| 164 | if (0 != ret) |
| 165 | return print_and_error("Error while dumping WiMAX image!\n"); |
| 166 | } |
| 167 | |
| 168 | if (0 != (ret = nandroid_backup_partition(backup_path, "/system"))) |
| 169 | return ret; |
| 170 | |
| 171 | if (0 != (ret = nandroid_backup_partition(backup_path, "/data"))) |
| 172 | return ret; |
| 173 | |
| 174 | if (has_datadata()) { |
| 175 | if (0 != (ret = nandroid_backup_partition(backup_path, "/datadata"))) |
| 176 | return ret; |
| 177 | } |
| 178 | |
| 179 | struct stat st; |
| 180 | if (0 != stat("/sdcard/.android_secure", &st)) |
| 181 | { |
| 182 | ui_print("No /sdcard/.android_secure found. Skipping backup of applications on external storage.\n"); |
| 183 | } |
| 184 | else |
| 185 | { |
| 186 | if (0 != (ret = nandroid_backup_partition_extended(backup_path, "/sdcard/.android_secure", 0))) |
| 187 | return ret; |
| 188 | } |
| 189 | |
| 190 | if (0 != (ret = nandroid_backup_partition_extended(backup_path, "/cache", 0))) |
| 191 | return ret; |
| 192 | |
| 193 | Volume *vol = volume_for_path("/sd-ext"); |
| 194 | if (vol == NULL || 0 != stat(vol->device, &st)) |
| 195 | { |
| 196 | ui_print("No sd-ext found. Skipping backup of sd-ext.\n"); |
| 197 | } |
| 198 | else |
| 199 | { |
| 200 | if (0 != ensure_path_mounted("/sd-ext")) |
| 201 | ui_print("Could not mount sd-ext. sd-ext backup may not be supported on this device. Skipping backup of sd-ext.\n"); |
| 202 | else if (0 != (ret = nandroid_backup_partition(backup_path, "/sd-ext"))) |
| 203 | return ret; |
| 204 | } |
| 205 | |
| 206 | ui_print("Generating md5 sum...\n"); |
| 207 | sprintf(tmp, "nandroid-md5.sh %s", backup_path); |
| 208 | if (0 != (ret = __system(tmp))) { |
| 209 | ui_print("Error while generating md5 sum!\n"); |
| 210 | return ret; |
| 211 | } |
| 212 | |
| 213 | sync(); |
| 214 | ui_set_background(BACKGROUND_ICON_NONE); |
| 215 | ui_reset_progress(); |
| 216 | ui_print("\nBackup complete!\n"); |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | typedef int (*format_function)(char* root); |
| 221 | |
| 222 | static void ensure_directory(const char* dir) { |
| 223 | char tmp[PATH_MAX]; |
| 224 | sprintf(tmp, "mkdir -p %s", dir); |
| 225 | __system(tmp); |
| 226 | } |
| 227 | |
| 228 | int nandroid_restore_partition_extended(const char* backup_path, const char* mount_point, int umount_when_finished) { |
| 229 | int ret = 0; |
| 230 | char* name = basename(mount_point); |
| 231 | |
| 232 | char tmp[PATH_MAX]; |
| 233 | sprintf(tmp, "%s/%s.img", backup_path, name); |
| 234 | struct stat file_info; |
| 235 | if (0 != (ret = statfs(tmp, &file_info))) { |
| 236 | ui_print("%s.img not found. Skipping restore of %s.\n", name, mount_point); |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | ensure_directory(mount_point); |
| 241 | |
| 242 | unyaffs_callback callback = NULL; |
| 243 | if (0 != stat("/sdcard/clockworkmod/.hidenandroidprogress", &file_info)) { |
| 244 | callback = yaffs_callback; |
| 245 | } |
| 246 | |
| 247 | ui_print("Restoring %s...\n", name); |
| 248 | /* |
| 249 | if (0 != (ret = ensure_root_path_unmounted(root))) { |
| 250 | ui_print("Can't unmount %s!\n", mount_point); |
| 251 | return ret; |
| 252 | } |
| 253 | */ |
| 254 | if (0 != (ret = format_volume(mount_point))) { |
| 255 | ui_print("Error while formatting %s!\n", mount_point); |
| 256 | return ret; |
| 257 | } |
| 258 | |
| 259 | if (0 != (ret = ensure_path_mounted(mount_point))) { |
| 260 | ui_print("Can't mount %s!\n", mount_point); |
| 261 | return ret; |
| 262 | } |
| 263 | |
| 264 | if (0 != (ret = unyaffs(tmp, mount_point, callback))) { |
| 265 | ui_print("Error while restoring %s!\n", mount_point); |
| 266 | return ret; |
| 267 | } |
| 268 | |
| 269 | if (umount_when_finished) { |
| 270 | ensure_path_unmounted(mount_point); |
| 271 | } |
| 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | int nandroid_restore_partition(const char* backup_path, const char* root) { |
| 277 | Volume *vol = volume_for_path(root); |
| 278 | // make sure the volume exists... |
| 279 | if (vol == NULL || vol->fs_type == NULL) |
| 280 | return 0; |
| 281 | |
| 282 | // see if we need a raw restore (mtd) |
| 283 | char tmp[PATH_MAX]; |
| 284 | if (strcmp(vol->fs_type, "mtd") == 0 || |
| 285 | strcmp(vol->fs_type, "emmc") == 0) { |
| 286 | int ret; |
| 287 | const char* name = basename(root); |
| 288 | ui_print("Erasing %s before restore...\n", name); |
| 289 | if (0 != (ret = format_volume(root))) { |
| 290 | ui_print("Error while erasing %s image!", name); |
| 291 | return ret; |
| 292 | } |
| 293 | sprintf(tmp, "%s%s.img", backup_path, root); |
| 294 | ui_print("Restoring %s image...\n", name); |
| 295 | if (0 != (ret = restore_raw_partition(vol->device, tmp))) { |
| 296 | ui_print("Error while flashing %s image!", name); |
| 297 | return ret; |
| 298 | } |
| 299 | return 0; |
| 300 | } |
| 301 | return nandroid_restore_partition_extended(backup_path, root, 1); |
| 302 | } |
| 303 | |
| 304 | 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) |
| 305 | { |
| 306 | ui_set_background(BACKGROUND_ICON_INSTALLING); |
| 307 | ui_show_indeterminate_progress(); |
| 308 | yaffs_files_total = 0; |
| 309 | |
| 310 | if (ensure_path_mounted("/sdcard") != 0) |
| 311 | return print_and_error("Can't mount /sdcard\n"); |
| 312 | |
| 313 | char tmp[PATH_MAX]; |
| 314 | |
| 315 | ui_print("Checking MD5 sums...\n"); |
| 316 | sprintf(tmp, "cd %s && md5sum -c nandroid.md5", backup_path); |
| 317 | if (0 != __system(tmp)) |
| 318 | return print_and_error("MD5 mismatch!\n"); |
| 319 | |
| 320 | int ret; |
| 321 | |
| 322 | if (restore_boot && NULL != volume_for_path("/boot") && 0 != (ret = nandroid_restore_partition(backup_path, "/boot"))) |
| 323 | return ret; |
| 324 | |
| 325 | if (restore_wimax && 0 == (ret = get_partition_device("wimax", tmp))) |
| 326 | { |
| 327 | char serialno[PROPERTY_VALUE_MAX]; |
| 328 | |
| 329 | serialno[0] = 0; |
| 330 | property_get("ro.serialno", serialno, ""); |
| 331 | sprintf(tmp, "%s/wimax.%s.img", backup_path, serialno); |
| 332 | |
| 333 | struct stat st; |
| 334 | if (0 != stat(tmp, &st)) |
| 335 | { |
| 336 | ui_print("WARNING: WiMAX partition exists, but nandroid\n"); |
| 337 | ui_print(" backup does not contain WiMAX image.\n"); |
| 338 | ui_print(" You should create a new backup to\n"); |
| 339 | ui_print(" protect your WiMAX keys.\n"); |
| 340 | } |
| 341 | else |
| 342 | { |
| 343 | ui_print("Erasing WiMAX before restore...\n"); |
| 344 | if (0 != (ret = format_volume("/wimax"))) |
| 345 | return print_and_error("Error while formatting wimax!\n"); |
| 346 | ui_print("Restoring WiMAX image...\n"); |
| 347 | if (0 != (ret = restore_raw_partition("wimax", tmp))) |
| 348 | return ret; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | if (restore_system && 0 != (ret = nandroid_restore_partition(backup_path, "/system"))) |
| 353 | return ret; |
| 354 | |
| 355 | if (restore_data && 0 != (ret = nandroid_restore_partition(backup_path, "/data"))) |
| 356 | return ret; |
| 357 | |
| 358 | if (has_datadata()) { |
| 359 | if (restore_data && 0 != (ret = nandroid_restore_partition(backup_path, "/datadata"))) |
| 360 | return ret; |
| 361 | } |
| 362 | |
| 363 | if (restore_data && 0 != (ret = nandroid_restore_partition_extended(backup_path, "/sdcard/.android_secure", 0))) |
| 364 | return ret; |
| 365 | |
| 366 | if (restore_cache && 0 != (ret = nandroid_restore_partition_extended(backup_path, "/cache", 0))) |
| 367 | return ret; |
| 368 | |
| 369 | if (restore_sdext && 0 != (ret = nandroid_restore_partition(backup_path, "/sd-ext"))) |
| 370 | return ret; |
| 371 | |
| 372 | sync(); |
| 373 | ui_set_background(BACKGROUND_ICON_NONE); |
| 374 | ui_reset_progress(); |
| 375 | ui_print("\nRestore complete!\n"); |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | void nandroid_generate_timestamp_path(char* backup_path) |
| 380 | { |
| 381 | time_t t = time(NULL); |
| 382 | struct tm *tmp = localtime(&t); |
| 383 | if (tmp == NULL) |
| 384 | { |
| 385 | struct timeval tp; |
| 386 | gettimeofday(&tp, NULL); |
| 387 | sprintf(backup_path, "/sdcard/clockworkmod/backup/%d", tp.tv_sec); |
| 388 | } |
| 389 | else |
| 390 | { |
| 391 | strftime(backup_path, PATH_MAX, "/sdcard/clockworkmod/backup/%F.%H.%M.%S", tmp); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | int nandroid_usage() |
| 396 | { |
| 397 | printf("Usage: nandroid backup\n"); |
| 398 | printf("Usage: nandroid restore <directory>\n"); |
| 399 | return 1; |
| 400 | } |
| 401 | |
| 402 | int nandroid_main(int argc, char** argv) |
| 403 | { |
| 404 | if (argc > 3 || argc < 2) |
| 405 | return nandroid_usage(); |
| 406 | |
| 407 | if (strcmp("backup", argv[1]) == 0) |
| 408 | { |
| 409 | if (argc != 2) |
| 410 | return nandroid_usage(); |
| 411 | |
| 412 | char backup_path[PATH_MAX]; |
| 413 | nandroid_generate_timestamp_path(backup_path); |
| 414 | return nandroid_backup(backup_path); |
| 415 | } |
| 416 | |
| 417 | if (strcmp("restore", argv[1]) == 0) |
| 418 | { |
| 419 | if (argc != 3) |
| 420 | return nandroid_usage(); |
| 421 | return nandroid_restore(argv[2], 1, 1, 1, 1, 1, 0); |
| 422 | } |
| 423 | |
| 424 | return nandroid_usage(); |
| 425 | } |