2010-03-13 07:21:12 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <linux/input.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/reboot.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sys/limits.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
#include "bootloader.h"
|
|
|
|
#include "common.h"
|
|
|
|
#include "cutils/properties.h"
|
|
|
|
#include "firmware.h"
|
|
|
|
#include "install.h"
|
|
|
|
#include "minui/minui.h"
|
|
|
|
#include "minzip/DirUtil.h"
|
|
|
|
#include "roots.h"
|
|
|
|
#include "recovery_ui.h"
|
|
|
|
|
|
|
|
#include "../../external/yaffs2/yaffs2/utils/mkyaffs2image.h"
|
|
|
|
#include "../../external/yaffs2/yaffs2/utils/unyaffs.h"
|
|
|
|
|
2010-03-26 00:58:45 +00:00
|
|
|
#include <sys/vfs.h>
|
|
|
|
|
2010-03-13 07:21:12 +00:00
|
|
|
#include "extendedcommands.h"
|
|
|
|
#include "nandroid.h"
|
2011-06-09 02:03:27 +00:00
|
|
|
#include "mounts.h"
|
|
|
|
|
2011-05-26 18:47:56 +00:00
|
|
|
#include "flashutils/flashutils.h"
|
|
|
|
#include <libgen.h>
|
|
|
|
|
|
|
|
void nandroid_generate_timestamp_path(const char* backup_path)
|
|
|
|
{
|
|
|
|
time_t t = time(NULL);
|
|
|
|
struct tm *tmp = localtime(&t);
|
|
|
|
if (tmp == NULL)
|
|
|
|
{
|
|
|
|
struct timeval tp;
|
|
|
|
gettimeofday(&tp, NULL);
|
|
|
|
sprintf(backup_path, "/sdcard/clockworkmod/backup/%d", tp.tv_sec);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
strftime(backup_path, PATH_MAX, "/sdcard/clockworkmod/backup/%F.%H.%M.%S", tmp);
|
|
|
|
}
|
|
|
|
}
|
2010-03-13 07:21:12 +00:00
|
|
|
|
2011-06-09 02:03:27 +00:00
|
|
|
static int print_and_error(const char* message) {
|
2010-12-19 01:42:31 +00:00
|
|
|
ui_print("%s", message);
|
2010-03-15 23:31:03 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-06-09 02:03:27 +00:00
|
|
|
static int yaffs_files_total = 0;
|
|
|
|
static int yaffs_files_count = 0;
|
|
|
|
static void yaffs_callback(const char* filename)
|
2010-03-13 07:21:12 +00:00
|
|
|
{
|
2011-06-09 02:03:27 +00:00
|
|
|
if (filename == NULL)
|
|
|
|
return;
|
|
|
|
const char* justfile = basename(filename);
|
|
|
|
char tmp[PATH_MAX];
|
|
|
|
strcpy(tmp, justfile);
|
|
|
|
if (tmp[strlen(tmp) - 1] == '\n')
|
|
|
|
tmp[strlen(tmp) - 1] = NULL;
|
|
|
|
if (strlen(tmp) < 30)
|
|
|
|
ui_print("%s", tmp);
|
2010-03-13 07:21:12 +00:00
|
|
|
yaffs_files_count++;
|
|
|
|
if (yaffs_files_total != 0)
|
|
|
|
ui_set_progress((float)yaffs_files_count / (float)yaffs_files_total);
|
|
|
|
ui_reset_text_col();
|
|
|
|
}
|
|
|
|
|
2011-06-09 02:03:27 +00:00
|
|
|
static void compute_directory_stats(const char* directory)
|
2010-03-13 07:21:12 +00:00
|
|
|
{
|
|
|
|
char tmp[PATH_MAX];
|
|
|
|
sprintf(tmp, "find %s | wc -l > /tmp/dircount", directory);
|
|
|
|
__system(tmp);
|
|
|
|
char count_text[100];
|
|
|
|
FILE* f = fopen("/tmp/dircount", "r");
|
|
|
|
fread(count_text, 1, sizeof(count_text), f);
|
|
|
|
fclose(f);
|
|
|
|
yaffs_files_count = 0;
|
|
|
|
yaffs_files_total = atoi(count_text);
|
|
|
|
ui_reset_progress();
|
|
|
|
ui_show_progress(1, 0);
|
|
|
|
}
|
|
|
|
|
2011-06-09 02:03:27 +00:00
|
|
|
typedef void (*file_event_callback)(const char* filename);
|
2011-06-15 06:39:59 +00:00
|
|
|
typedef int (*nandroid_backup_handler)(const char* backup_path, const char* backup_file_image, int callback);
|
2011-06-09 02:03:27 +00:00
|
|
|
|
2011-06-15 06:39:59 +00:00
|
|
|
static int mkyaffs2image_wrapper(const char* backup_path, const char* backup_file_image, int callback) {
|
2011-09-06 04:14:06 +00:00
|
|
|
char backup_file_image_with_extension[PATH_MAX];
|
|
|
|
sprintf(backup_file_image_with_extension, "%s.img", backup_file_image);
|
|
|
|
return mkyaffs2image(backup_path, backup_file_image_with_extension, 0, callback ? yaffs_callback : NULL);
|
2011-06-09 02:03:27 +00:00
|
|
|
}
|
|
|
|
|
2011-06-15 06:39:59 +00:00
|
|
|
static int tar_compress_wrapper(const char* backup_path, const char* backup_file_image, int callback) {
|
2011-06-09 02:03:27 +00:00
|
|
|
char tmp[PATH_MAX];
|
|
|
|
if (strcmp(backup_path, "/data") == 0 && volume_for_path("/sdcard") == NULL)
|
2011-09-06 04:14:06 +00:00
|
|
|
sprintf(tmp, "cd $(dirname %s) ; tar cvf %s.tar --exclude 'media' $(basename %s) ; exit $?", backup_path, backup_file_image, backup_path);
|
2011-06-09 02:03:27 +00:00
|
|
|
else
|
2011-09-06 04:14:06 +00:00
|
|
|
sprintf(tmp, "cd $(dirname %s) ; tar cvf %s.tar $(basename %s) ; exit $?", backup_path, backup_file_image, backup_path);
|
2011-06-09 02:03:27 +00:00
|
|
|
|
|
|
|
FILE *fp = __popen(tmp, "r");
|
|
|
|
if (fp == NULL) {
|
|
|
|
ui_print("Unable to execute tar.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (fgets(tmp, PATH_MAX, fp) != NULL) {
|
|
|
|
tmp[PATH_MAX - 1] = NULL;
|
2011-06-15 06:39:59 +00:00
|
|
|
if (callback)
|
2011-06-09 02:03:27 +00:00
|
|
|
yaffs_callback(tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return __pclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static nandroid_backup_handler get_backup_handler(const char *backup_path) {
|
|
|
|
Volume *v = volume_for_path(backup_path);
|
|
|
|
if (v == NULL) {
|
|
|
|
ui_print("Unable to find volume.\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
MountedVolume *mv = find_mounted_volume_by_mount_point(v->mount_point);
|
|
|
|
if (mv == NULL) {
|
|
|
|
ui_print("Unable to find mounted volume: %s\n", v->mount_point);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-06-10 16:45:52 +00:00
|
|
|
if (strcmp(backup_path, "/data") == 0 && is_data_media()) {
|
|
|
|
return tar_compress_wrapper;
|
|
|
|
}
|
|
|
|
|
2011-09-06 04:14:06 +00:00
|
|
|
// cwr5, we prefer tar for everything except yaffs2
|
|
|
|
if (strcmp("yaffs2", mv->filesystem) == 0) {
|
2011-06-10 16:45:52 +00:00
|
|
|
return mkyaffs2image_wrapper;
|
|
|
|
}
|
|
|
|
|
2011-09-06 04:14:06 +00:00
|
|
|
char str[255];
|
|
|
|
char* partition;
|
|
|
|
property_get("ro.cwm.prefer_tar", str, "true");
|
|
|
|
if (strcmp("true", str) != 0) {
|
2011-06-09 02:03:27 +00:00
|
|
|
return mkyaffs2image_wrapper;
|
|
|
|
}
|
|
|
|
|
|
|
|
return tar_compress_wrapper;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-19 01:42:31 +00:00
|
|
|
int nandroid_backup_partition_extended(const char* backup_path, const char* mount_point, int umount_when_finished) {
|
2010-03-15 23:31:03 +00:00
|
|
|
int ret = 0;
|
2010-04-01 19:20:39 +00:00
|
|
|
char* name = basename(mount_point);
|
2010-06-09 19:41:20 +00:00
|
|
|
|
|
|
|
struct stat file_info;
|
2011-09-06 04:14:06 +00:00
|
|
|
int callback = stat("/sdcard/clockworkmod/.hidenandroidprogress", &file_info) != 0;
|
2010-03-15 23:31:03 +00:00
|
|
|
|
|
|
|
ui_print("Backing up %s...\n", name);
|
2010-12-19 01:42:31 +00:00
|
|
|
if (0 != (ret = ensure_path_mounted(mount_point) != 0)) {
|
2010-03-15 23:31:03 +00:00
|
|
|
ui_print("Can't mount %s!\n", mount_point);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
compute_directory_stats(mount_point);
|
|
|
|
char tmp[PATH_MAX];
|
2011-09-06 04:14:06 +00:00
|
|
|
scan_mounted_volumes();
|
|
|
|
Volume *v = volume_for_path(mount_point);
|
|
|
|
MountedVolume *mv = NULL;
|
|
|
|
if (v != NULL)
|
|
|
|
mv = find_mounted_volume_by_mount_point(v->mount_point);
|
|
|
|
if (mv == NULL || mv->filesystem == NULL)
|
|
|
|
sprintf(tmp, "%s/%s.auto", backup_path, name);
|
|
|
|
else
|
|
|
|
sprintf(tmp, "%s/%s.%s", backup_path, name, mv->filesystem);
|
2011-06-09 02:03:27 +00:00
|
|
|
nandroid_backup_handler backup_handler = get_backup_handler(mount_point);
|
|
|
|
if (backup_handler == NULL) {
|
|
|
|
ui_print("Error finding an appropriate backup handler.\n");
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
ret = backup_handler(mount_point, tmp, callback);
|
2010-07-03 20:54:21 +00:00
|
|
|
if (umount_when_finished) {
|
2010-12-19 01:42:31 +00:00
|
|
|
ensure_path_unmounted(mount_point);
|
2010-07-03 20:54:21 +00:00
|
|
|
}
|
2010-03-15 23:31:03 +00:00
|
|
|
if (0 != ret) {
|
2011-06-09 02:03:27 +00:00
|
|
|
ui_print("Error while making a backup image of %s!\n", mount_point);
|
2010-03-15 23:31:03 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-01-04 19:38:31 +00:00
|
|
|
int nandroid_backup_partition(const char* backup_path, const char* root) {
|
|
|
|
Volume *vol = volume_for_path(root);
|
|
|
|
// make sure the volume exists before attempting anything...
|
|
|
|
if (vol == NULL || vol->fs_type == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// see if we need a raw backup (mtd)
|
|
|
|
char tmp[PATH_MAX];
|
|
|
|
int ret;
|
2011-01-16 12:02:34 +00:00
|
|
|
if (strcmp(vol->fs_type, "mtd") == 0 ||
|
2011-02-28 00:33:32 +00:00
|
|
|
strcmp(vol->fs_type, "bml") == 0 ||
|
2011-01-16 12:02:34 +00:00
|
|
|
strcmp(vol->fs_type, "emmc") == 0) {
|
2011-01-04 19:38:31 +00:00
|
|
|
const char* name = basename(root);
|
|
|
|
sprintf(tmp, "%s/%s.img", backup_path, name);
|
|
|
|
ui_print("Backing up %s image...\n", name);
|
2011-05-26 18:14:15 +00:00
|
|
|
if (0 != (ret = backup_raw_partition(vol->fs_type, vol->device, tmp))) {
|
2011-01-04 19:38:31 +00:00
|
|
|
ui_print("Error while backing up %s image!", name);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-07-03 20:54:21 +00:00
|
|
|
return nandroid_backup_partition_extended(backup_path, root, 1);
|
|
|
|
}
|
|
|
|
|
2010-04-18 23:00:21 +00:00
|
|
|
int nandroid_backup(const char* backup_path)
|
2010-03-13 07:21:12 +00:00
|
|
|
{
|
|
|
|
ui_set_background(BACKGROUND_ICON_INSTALLING);
|
|
|
|
|
2011-10-27 04:25:34 +00:00
|
|
|
if (ensure_path_mounted(backup_path) != 0) {
|
|
|
|
return print_and_error("Can't mount backup path.\n");
|
|
|
|
}
|
2010-03-13 07:21:12 +00:00
|
|
|
|
2011-10-27 04:25:34 +00:00
|
|
|
Volume* volume = volume_for_path(backup_path);
|
2011-11-26 14:53:41 +00:00
|
|
|
if (NULL == volume) {
|
|
|
|
if (strstr(backup_path, "/sdcard") == backup_path && is_data_media())
|
|
|
|
volume = volume_for_path("/data");
|
|
|
|
else
|
|
|
|
return print_and_error("Unable to find volume for backup path.\n");
|
|
|
|
}
|
2010-03-26 00:58:45 +00:00
|
|
|
int ret;
|
2010-04-19 01:06:24 +00:00
|
|
|
struct statfs s;
|
2011-11-23 22:06:12 +00:00
|
|
|
if (NULL != volume) {
|
|
|
|
if (0 != (ret = statfs(volume->mount_point, &s)))
|
|
|
|
return print_and_error("Unable to stat backup path.\n");
|
|
|
|
uint64_t bavail = s.f_bavail;
|
|
|
|
uint64_t bsize = s.f_bsize;
|
|
|
|
uint64_t sdcard_free = bavail * bsize;
|
|
|
|
uint64_t sdcard_free_mb = sdcard_free / (uint64_t)(1024 * 1024);
|
|
|
|
ui_print("SD Card space free: %lluMB\n", sdcard_free_mb);
|
|
|
|
if (sdcard_free_mb < 150)
|
|
|
|
ui_print("There may not be enough free space to complete backup... continuing...\n");
|
|
|
|
}
|
2010-03-13 07:21:12 +00:00
|
|
|
char tmp[PATH_MAX];
|
|
|
|
sprintf(tmp, "mkdir -p %s", backup_path);
|
|
|
|
__system(tmp);
|
|
|
|
|
2011-01-04 19:38:31 +00:00
|
|
|
if (0 != (ret = nandroid_backup_partition(backup_path, "/boot")))
|
|
|
|
return ret;
|
2010-03-29 21:46:00 +00:00
|
|
|
|
2011-01-04 19:38:31 +00:00
|
|
|
if (0 != (ret = nandroid_backup_partition(backup_path, "/recovery")))
|
|
|
|
return ret;
|
2010-03-29 21:46:00 +00:00
|
|
|
|
2011-03-03 03:35:05 +00:00
|
|
|
Volume *vol = volume_for_path("/wimax");
|
|
|
|
if (vol != NULL && 0 == stat(vol->device, &s))
|
2010-12-29 18:15:18 +00:00
|
|
|
{
|
2010-12-31 07:05:35 +00:00
|
|
|
char serialno[PROPERTY_VALUE_MAX];
|
2010-12-30 07:48:25 +00:00
|
|
|
ui_print("Backing up WiMAX...\n");
|
2010-12-31 07:05:35 +00:00
|
|
|
serialno[0] = 0;
|
|
|
|
property_get("ro.serialno", serialno, "");
|
|
|
|
sprintf(tmp, "%s/wimax.%s.img", backup_path, serialno);
|
2011-05-26 18:29:29 +00:00
|
|
|
ret = backup_raw_partition(vol->fs_type, vol->device, tmp);
|
2010-12-29 18:15:18 +00:00
|
|
|
if (0 != ret)
|
2010-12-30 07:48:25 +00:00
|
|
|
return print_and_error("Error while dumping WiMAX image!\n");
|
2010-12-29 18:15:18 +00:00
|
|
|
}
|
|
|
|
|
2010-12-19 01:42:31 +00:00
|
|
|
if (0 != (ret = nandroid_backup_partition(backup_path, "/system")))
|
2010-03-15 23:31:03 +00:00
|
|
|
return ret;
|
|
|
|
|
2010-12-19 01:42:31 +00:00
|
|
|
if (0 != (ret = nandroid_backup_partition(backup_path, "/data")))
|
2010-03-15 23:31:03 +00:00
|
|
|
return ret;
|
|
|
|
|
2010-12-19 06:37:49 +00:00
|
|
|
if (has_datadata()) {
|
|
|
|
if (0 != (ret = nandroid_backup_partition(backup_path, "/datadata")))
|
|
|
|
return ret;
|
|
|
|
}
|
2010-06-15 05:04:22 +00:00
|
|
|
|
2011-03-03 03:35:05 +00:00
|
|
|
if (0 != stat("/sdcard/.android_secure", &s))
|
2010-07-03 20:54:21 +00:00
|
|
|
{
|
|
|
|
ui_print("No /sdcard/.android_secure found. Skipping backup of applications on external storage.\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-12-19 01:42:31 +00:00
|
|
|
if (0 != (ret = nandroid_backup_partition_extended(backup_path, "/sdcard/.android_secure", 0)))
|
2010-07-03 20:54:21 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-12-19 01:42:31 +00:00
|
|
|
if (0 != (ret = nandroid_backup_partition_extended(backup_path, "/cache", 0)))
|
2010-03-15 23:31:03 +00:00
|
|
|
return ret;
|
2010-03-31 06:29:43 +00:00
|
|
|
|
2011-03-03 03:35:05 +00:00
|
|
|
vol = volume_for_path("/sd-ext");
|
|
|
|
if (vol == NULL || 0 != stat(vol->device, &s))
|
2010-04-04 06:28:39 +00:00
|
|
|
{
|
2010-04-03 05:17:42 +00:00
|
|
|
ui_print("No sd-ext found. Skipping backup of sd-ext.\n");
|
2010-04-04 06:28:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-12-19 01:42:31 +00:00
|
|
|
if (0 != ensure_path_mounted("/sd-ext"))
|
2010-06-15 05:04:22 +00:00
|
|
|
ui_print("Could not mount sd-ext. sd-ext backup may not be supported on this device. Skipping backup of sd-ext.\n");
|
2011-01-16 10:33:04 +00:00
|
|
|
else if (0 != (ret = nandroid_backup_partition(backup_path, "/sd-ext")))
|
2010-04-04 06:28:39 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2010-07-03 20:54:21 +00:00
|
|
|
|
2010-03-24 21:38:40 +00:00
|
|
|
ui_print("Generating md5 sum...\n");
|
2010-04-01 19:20:39 +00:00
|
|
|
sprintf(tmp, "nandroid-md5.sh %s", backup_path);
|
2010-03-24 17:34:38 +00:00
|
|
|
if (0 != (ret = __system(tmp))) {
|
|
|
|
ui_print("Error while generating md5 sum!\n");
|
|
|
|
return ret;
|
|
|
|
}
|
2010-03-13 07:21:12 +00:00
|
|
|
|
2010-03-13 07:45:25 +00:00
|
|
|
sync();
|
2010-03-13 07:21:12 +00:00
|
|
|
ui_set_background(BACKGROUND_ICON_NONE);
|
|
|
|
ui_reset_progress();
|
2010-03-15 23:31:03 +00:00
|
|
|
ui_print("\nBackup complete!\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-04-01 19:20:39 +00:00
|
|
|
typedef int (*format_function)(char* root);
|
|
|
|
|
2010-07-20 23:23:18 +00:00
|
|
|
static void ensure_directory(const char* dir) {
|
|
|
|
char tmp[PATH_MAX];
|
|
|
|
sprintf(tmp, "mkdir -p %s", dir);
|
|
|
|
__system(tmp);
|
|
|
|
}
|
|
|
|
|
2011-06-15 07:00:55 +00:00
|
|
|
typedef int (*nandroid_restore_handler)(const char* backup_file_image, const char* backup_path, int callback);
|
2011-06-09 02:03:27 +00:00
|
|
|
|
2011-06-15 07:00:55 +00:00
|
|
|
static int unyaffs_wrapper(const char* backup_file_image, const char* backup_path, int callback) {
|
2011-07-20 08:26:02 +00:00
|
|
|
return unyaffs(backup_file_image, backup_path, callback ? yaffs_callback : NULL);
|
2011-06-09 02:03:27 +00:00
|
|
|
}
|
|
|
|
|
2011-06-15 07:00:55 +00:00
|
|
|
static int tar_extract_wrapper(const char* backup_file_image, const char* backup_path, int callback) {
|
2011-06-09 02:03:27 +00:00
|
|
|
char tmp[PATH_MAX];
|
|
|
|
sprintf(tmp, "cd $(dirname %s) ; tar xvf %s ; exit $?", backup_path, backup_file_image);
|
|
|
|
|
|
|
|
char path[PATH_MAX];
|
|
|
|
FILE *fp = __popen(tmp, "r");
|
|
|
|
if (fp == NULL) {
|
|
|
|
ui_print("Unable to execute tar.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (fgets(path, PATH_MAX, fp) != NULL) {
|
2011-06-15 07:00:55 +00:00
|
|
|
if (callback)
|
|
|
|
yaffs_callback(path);
|
2011-06-09 02:03:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return __pclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static nandroid_restore_handler get_restore_handler(const char *backup_path) {
|
|
|
|
Volume *v = volume_for_path(backup_path);
|
|
|
|
if (v == NULL) {
|
|
|
|
ui_print("Unable to find volume.\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
scan_mounted_volumes();
|
|
|
|
MountedVolume *mv = find_mounted_volume_by_mount_point(v->mount_point);
|
|
|
|
if (mv == NULL) {
|
|
|
|
ui_print("Unable to find mounted volume: %s\n", v->mount_point);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-06-10 16:45:52 +00:00
|
|
|
if (strcmp(backup_path, "/data") == 0 && is_data_media()) {
|
|
|
|
return tar_extract_wrapper;
|
|
|
|
}
|
|
|
|
|
2011-09-06 04:14:06 +00:00
|
|
|
// cwr 5, we prefer tar for everything unless it is yaffs2
|
2011-06-10 16:45:52 +00:00
|
|
|
char str[255];
|
|
|
|
char* partition;
|
|
|
|
property_get("ro.cwm.prefer_tar", str, "false");
|
|
|
|
if (strcmp("true", str) != 0) {
|
|
|
|
return unyaffs_wrapper;
|
|
|
|
}
|
|
|
|
|
2011-06-09 02:03:27 +00:00
|
|
|
if (strcmp("yaffs2", mv->filesystem) == 0) {
|
|
|
|
return unyaffs_wrapper;
|
|
|
|
}
|
|
|
|
|
|
|
|
return tar_extract_wrapper;
|
|
|
|
}
|
|
|
|
|
2010-12-19 01:42:31 +00:00
|
|
|
int nandroid_restore_partition_extended(const char* backup_path, const char* mount_point, int umount_when_finished) {
|
2010-03-15 23:31:03 +00:00
|
|
|
int ret = 0;
|
2010-04-01 19:20:39 +00:00
|
|
|
char* name = basename(mount_point);
|
2011-09-06 04:14:06 +00:00
|
|
|
|
|
|
|
nandroid_restore_handler restore_handler = NULL;
|
2011-09-29 20:37:19 +00:00
|
|
|
const char *filesystems[] = { "yaffs2", "ext2", "ext3", "ext4", "vfat", "rfs", NULL };
|
2011-09-06 04:14:06 +00:00
|
|
|
const char* backup_filesystem = NULL;
|
|
|
|
Volume *vol = volume_for_path(mount_point);
|
|
|
|
const char *device = NULL;
|
|
|
|
if (vol != NULL)
|
|
|
|
device = vol->device;
|
|
|
|
|
2010-07-20 23:23:18 +00:00
|
|
|
char tmp[PATH_MAX];
|
|
|
|
sprintf(tmp, "%s/%s.img", backup_path, name);
|
2010-06-09 19:41:20 +00:00
|
|
|
struct stat file_info;
|
2010-07-20 23:23:18 +00:00
|
|
|
if (0 != (ret = statfs(tmp, &file_info))) {
|
2011-09-06 04:14:06 +00:00
|
|
|
// can't find the backup, it may be the new backup format?
|
|
|
|
// iterate through the backup types
|
|
|
|
printf("couldn't find default\n");
|
|
|
|
char *filesystem;
|
|
|
|
int i = 0;
|
|
|
|
while ((filesystem = filesystems[i]) != NULL) {
|
|
|
|
sprintf(tmp, "%s/%s.%s.img", backup_path, name, filesystem);
|
|
|
|
if (0 == (ret = statfs(tmp, &file_info))) {
|
|
|
|
backup_filesystem = filesystem;
|
|
|
|
restore_handler = unyaffs_wrapper;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sprintf(tmp, "%s/%s.%s.tar", backup_path, name, filesystem);
|
|
|
|
if (0 == (ret = statfs(tmp, &file_info))) {
|
|
|
|
backup_filesystem = filesystem;
|
|
|
|
restore_handler = tar_extract_wrapper;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (backup_filesystem == NULL || restore_handler == NULL) {
|
|
|
|
ui_print("%s.img not found. Skipping restore of %s.\n", name, mount_point);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
printf("Found new backup image: %s\n", tmp);
|
|
|
|
}
|
|
|
|
|
2011-11-26 18:55:00 +00:00
|
|
|
// If the fs_type of this volume is "auto" or mount_point is /data
|
2011-12-18 21:49:52 +00:00
|
|
|
// and is_data_media (redundantly, and vol for /sdcard is NULL), let's revert
|
2011-11-26 18:55:00 +00:00
|
|
|
// to using a rm -rf, rather than trying to do a
|
|
|
|
// ext3/ext4/whatever format.
|
2011-09-06 04:14:06 +00:00
|
|
|
// This is because some phones (like DroidX) will freak out if you
|
|
|
|
// reformat the /system or /data partitions, and not boot due to
|
|
|
|
// a locked bootloader.
|
2011-11-23 22:06:12 +00:00
|
|
|
// Other devices, like the Galaxy Nexus, XOOM, and Galaxy Tab 10.1
|
|
|
|
// have a /sdcard symlinked to /data/media. /data is set to "auto"
|
|
|
|
// so that when the format occurs, /data/media is not erased.
|
2011-09-06 04:14:06 +00:00
|
|
|
// The "auto" fs type preserves the file system, and does not
|
|
|
|
// trigger that lock.
|
|
|
|
// Or of volume does not exist (.android_secure), just rm -rf.
|
|
|
|
if (vol == NULL || 0 == strcmp(vol->fs_type, "auto"))
|
|
|
|
backup_filesystem = NULL;
|
2011-11-26 18:55:00 +00:00
|
|
|
else if (0 == strcmp(vol->mount_point, "/data") && volume_for_path("/sdcard") == NULL && is_data_media())
|
|
|
|
backup_filesystem = NULL;
|
2010-07-20 23:23:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ensure_directory(mount_point);
|
|
|
|
|
2011-09-06 04:14:06 +00:00
|
|
|
int callback = stat("/sdcard/clockworkmod/.hidenandroidprogress", &file_info) != 0;
|
2010-06-09 19:41:20 +00:00
|
|
|
|
2010-03-15 23:31:03 +00:00
|
|
|
ui_print("Restoring %s...\n", name);
|
2011-09-06 04:14:06 +00:00
|
|
|
if (backup_filesystem == NULL) {
|
|
|
|
if (0 != (ret = format_volume(mount_point))) {
|
|
|
|
ui_print("Error while formatting %s!\n", mount_point);
|
|
|
|
return ret;
|
|
|
|
}
|
2010-03-15 23:31:03 +00:00
|
|
|
}
|
2011-09-06 04:14:06 +00:00
|
|
|
else if (0 != (ret = format_device(device, mount_point, backup_filesystem))) {
|
2010-12-19 01:42:31 +00:00
|
|
|
ui_print("Error while formatting %s!\n", mount_point);
|
2010-03-15 23:31:03 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2011-09-06 04:14:06 +00:00
|
|
|
|
2010-12-19 01:42:31 +00:00
|
|
|
if (0 != (ret = ensure_path_mounted(mount_point))) {
|
2010-03-15 23:31:03 +00:00
|
|
|
ui_print("Can't mount %s!\n", mount_point);
|
|
|
|
return ret;
|
|
|
|
}
|
2011-09-06 04:14:06 +00:00
|
|
|
|
|
|
|
if (restore_handler == NULL)
|
|
|
|
restore_handler = get_restore_handler(mount_point);
|
2011-06-09 02:03:27 +00:00
|
|
|
if (restore_handler == NULL) {
|
|
|
|
ui_print("Error finding an appropriate restore handler.\n");
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
if (0 != (ret = restore_handler(tmp, mount_point, callback))) {
|
2010-03-15 23:31:03 +00:00
|
|
|
ui_print("Error while restoring %s!\n", mount_point);
|
|
|
|
return ret;
|
|
|
|
}
|
2010-03-13 07:21:12 +00:00
|
|
|
|
2010-07-03 20:54:21 +00:00
|
|
|
if (umount_when_finished) {
|
2010-12-19 01:42:31 +00:00
|
|
|
ensure_path_unmounted(mount_point);
|
2010-04-18 23:00:21 +00:00
|
|
|
}
|
2010-07-03 20:54:21 +00:00
|
|
|
|
2010-04-18 23:00:21 +00:00
|
|
|
return 0;
|
2010-04-01 19:20:39 +00:00
|
|
|
}
|
|
|
|
|
2010-07-03 20:54:21 +00:00
|
|
|
int nandroid_restore_partition(const char* backup_path, const char* root) {
|
2011-01-04 19:38:31 +00:00
|
|
|
Volume *vol = volume_for_path(root);
|
|
|
|
// make sure the volume exists...
|
|
|
|
if (vol == NULL || vol->fs_type == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// see if we need a raw restore (mtd)
|
|
|
|
char tmp[PATH_MAX];
|
2011-01-16 12:02:34 +00:00
|
|
|
if (strcmp(vol->fs_type, "mtd") == 0 ||
|
2011-02-28 00:33:32 +00:00
|
|
|
strcmp(vol->fs_type, "bml") == 0 ||
|
2011-01-16 12:02:34 +00:00
|
|
|
strcmp(vol->fs_type, "emmc") == 0) {
|
2011-01-04 19:38:31 +00:00
|
|
|
int ret;
|
|
|
|
const char* name = basename(root);
|
|
|
|
ui_print("Erasing %s before restore...\n", name);
|
|
|
|
if (0 != (ret = format_volume(root))) {
|
|
|
|
ui_print("Error while erasing %s image!", name);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
sprintf(tmp, "%s%s.img", backup_path, root);
|
|
|
|
ui_print("Restoring %s image...\n", name);
|
2011-05-26 18:14:15 +00:00
|
|
|
if (0 != (ret = restore_raw_partition(vol->fs_type, vol->device, tmp))) {
|
2011-01-04 19:38:31 +00:00
|
|
|
ui_print("Error while flashing %s image!", name);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2010-07-03 20:54:21 +00:00
|
|
|
return nandroid_restore_partition_extended(backup_path, root, 1);
|
|
|
|
}
|
|
|
|
|
2010-12-29 18:15:18 +00:00
|
|
|
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)
|
2010-03-13 07:21:12 +00:00
|
|
|
{
|
|
|
|
ui_set_background(BACKGROUND_ICON_INSTALLING);
|
|
|
|
ui_show_indeterminate_progress();
|
|
|
|
yaffs_files_total = 0;
|
|
|
|
|
2011-10-27 04:25:34 +00:00
|
|
|
if (ensure_path_mounted(backup_path) != 0)
|
|
|
|
return print_and_error("Can't mount backup path\n");
|
2010-03-13 07:21:12 +00:00
|
|
|
|
|
|
|
char tmp[PATH_MAX];
|
|
|
|
|
|
|
|
ui_print("Checking MD5 sums...\n");
|
2010-03-13 18:15:55 +00:00
|
|
|
sprintf(tmp, "cd %s && md5sum -c nandroid.md5", backup_path);
|
2010-03-13 07:21:12 +00:00
|
|
|
if (0 != __system(tmp))
|
|
|
|
return print_and_error("MD5 mismatch!\n");
|
|
|
|
|
2010-03-15 23:31:03 +00:00
|
|
|
int ret;
|
2011-01-04 19:38:31 +00:00
|
|
|
|
|
|
|
if (restore_boot && NULL != volume_for_path("/boot") && 0 != (ret = nandroid_restore_partition(backup_path, "/boot")))
|
|
|
|
return ret;
|
2010-03-16 20:34:51 +00:00
|
|
|
|
2011-03-03 03:35:05 +00:00
|
|
|
struct stat s;
|
|
|
|
Volume *vol = volume_for_path("/wimax");
|
|
|
|
if (restore_wimax && vol != NULL && 0 == stat(vol->device, &s))
|
2010-12-29 18:15:18 +00:00
|
|
|
{
|
2010-12-31 07:05:35 +00:00
|
|
|
char serialno[PROPERTY_VALUE_MAX];
|
|
|
|
|
|
|
|
serialno[0] = 0;
|
|
|
|
property_get("ro.serialno", serialno, "");
|
|
|
|
sprintf(tmp, "%s/wimax.%s.img", backup_path, serialno);
|
2010-12-29 18:15:18 +00:00
|
|
|
|
|
|
|
struct stat st;
|
|
|
|
if (0 != stat(tmp, &st))
|
|
|
|
{
|
2010-12-30 07:48:25 +00:00
|
|
|
ui_print("WARNING: WiMAX partition exists, but nandroid\n");
|
|
|
|
ui_print(" backup does not contain WiMAX image.\n");
|
2010-12-29 18:15:18 +00:00
|
|
|
ui_print(" You should create a new backup to\n");
|
2010-12-30 07:48:25 +00:00
|
|
|
ui_print(" protect your WiMAX keys.\n");
|
2010-12-29 18:15:18 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-12-31 07:16:00 +00:00
|
|
|
ui_print("Erasing WiMAX before restore...\n");
|
2011-01-02 02:17:48 +00:00
|
|
|
if (0 != (ret = format_volume("/wimax")))
|
2010-12-31 07:16:00 +00:00
|
|
|
return print_and_error("Error while formatting wimax!\n");
|
2010-12-31 07:05:35 +00:00
|
|
|
ui_print("Restoring WiMAX image...\n");
|
2011-05-26 18:29:29 +00:00
|
|
|
if (0 != (ret = restore_raw_partition(vol->fs_type, vol->device, tmp)))
|
2010-12-29 18:15:18 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-19 01:42:31 +00:00
|
|
|
if (restore_system && 0 != (ret = nandroid_restore_partition(backup_path, "/system")))
|
2010-03-15 23:31:03 +00:00
|
|
|
return ret;
|
|
|
|
|
2010-12-19 01:42:31 +00:00
|
|
|
if (restore_data && 0 != (ret = nandroid_restore_partition(backup_path, "/data")))
|
2010-03-15 23:31:03 +00:00
|
|
|
return ret;
|
2010-06-15 05:04:22 +00:00
|
|
|
|
2010-12-19 06:37:49 +00:00
|
|
|
if (has_datadata()) {
|
|
|
|
if (restore_data && 0 != (ret = nandroid_restore_partition(backup_path, "/datadata")))
|
|
|
|
return ret;
|
|
|
|
}
|
2010-03-15 23:31:03 +00:00
|
|
|
|
2010-12-19 01:42:31 +00:00
|
|
|
if (restore_data && 0 != (ret = nandroid_restore_partition_extended(backup_path, "/sdcard/.android_secure", 0)))
|
2010-07-20 23:23:18 +00:00
|
|
|
return ret;
|
2010-07-03 20:54:21 +00:00
|
|
|
|
2010-12-19 01:42:31 +00:00
|
|
|
if (restore_cache && 0 != (ret = nandroid_restore_partition_extended(backup_path, "/cache", 0)))
|
2010-03-15 23:31:03 +00:00
|
|
|
return ret;
|
|
|
|
|
2010-12-19 01:42:31 +00:00
|
|
|
if (restore_sdext && 0 != (ret = nandroid_restore_partition(backup_path, "/sd-ext")))
|
2010-07-20 23:23:18 +00:00
|
|
|
return ret;
|
2010-04-01 19:20:39 +00:00
|
|
|
|
2010-03-13 07:45:25 +00:00
|
|
|
sync();
|
2010-03-13 07:21:12 +00:00
|
|
|
ui_set_background(BACKGROUND_ICON_NONE);
|
|
|
|
ui_reset_progress();
|
2010-03-15 23:31:03 +00:00
|
|
|
ui_print("\nRestore complete!\n");
|
2010-03-13 07:21:12 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2010-06-26 19:25:02 +00:00
|
|
|
|
|
|
|
int nandroid_usage()
|
|
|
|
{
|
|
|
|
printf("Usage: nandroid backup\n");
|
|
|
|
printf("Usage: nandroid restore <directory>\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int nandroid_main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
if (argc > 3 || argc < 2)
|
|
|
|
return nandroid_usage();
|
|
|
|
|
|
|
|
if (strcmp("backup", argv[1]) == 0)
|
|
|
|
{
|
|
|
|
if (argc != 2)
|
|
|
|
return nandroid_usage();
|
|
|
|
|
|
|
|
char backup_path[PATH_MAX];
|
|
|
|
nandroid_generate_timestamp_path(backup_path);
|
|
|
|
return nandroid_backup(backup_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp("restore", argv[1]) == 0)
|
|
|
|
{
|
|
|
|
if (argc != 3)
|
|
|
|
return nandroid_usage();
|
2010-12-30 07:36:03 +00:00
|
|
|
return nandroid_restore(argv[2], 1, 1, 1, 1, 1, 0);
|
2010-06-26 19:25:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nandroid_usage();
|
|
|
|
}
|