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 "commands.h"
|
|
|
|
#include "amend/amend.h"
|
|
|
|
|
|
|
|
#include "mtdutils/dump_image.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"
|
|
|
|
|
2010-03-15 23:31:03 +00:00
|
|
|
int print_and_error(char* message) {
|
|
|
|
ui_print(message);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-03-13 07:21:12 +00:00
|
|
|
int yaffs_files_total = 0;
|
|
|
|
int yaffs_files_count = 0;
|
|
|
|
void yaffs_callback(char* filename)
|
|
|
|
{
|
|
|
|
char* justfile = basename(filename);
|
|
|
|
if (strlen(justfile) < 30)
|
2010-03-16 00:05:27 +00:00
|
|
|
ui_print(justfile);
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
void compute_directory_stats(char* directory)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2010-04-18 23:00:21 +00:00
|
|
|
int nandroid_backup_partition(const char* backup_path, char* root) {
|
2010-03-15 23:31:03 +00:00
|
|
|
int ret = 0;
|
|
|
|
char mount_point[PATH_MAX];
|
2010-04-01 19:20:39 +00:00
|
|
|
translate_root_path(root, mount_point, PATH_MAX);
|
|
|
|
char* name = basename(mount_point);
|
2010-06-09 19:41:20 +00:00
|
|
|
|
|
|
|
struct stat file_info;
|
|
|
|
mkyaffs2image_callback callback = NULL;
|
|
|
|
if (0 != stat("/sdcard/clockworkmod/.hidenandroidprogress", &file_info)) {
|
|
|
|
callback = yaffs_callback;
|
|
|
|
}
|
2010-03-15 23:31:03 +00:00
|
|
|
|
|
|
|
ui_print("Backing up %s...\n", name);
|
|
|
|
if (0 != (ret = ensure_root_path_mounted(root) != 0)) {
|
|
|
|
ui_print("Can't mount %s!\n", mount_point);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
compute_directory_stats(mount_point);
|
|
|
|
char tmp[PATH_MAX];
|
|
|
|
sprintf(tmp, "%s/%s.img", backup_path, name);
|
2010-06-09 19:41:20 +00:00
|
|
|
ret = mkyaffs2image(mount_point, tmp, 0, callback);
|
2010-03-15 23:31:03 +00:00
|
|
|
ensure_root_path_unmounted(root);
|
|
|
|
if (0 != ret) {
|
|
|
|
ui_print("Error while making a yaffs2 image of %s!\n", mount_point);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (ensure_root_path_mounted("SDCARD:") != 0)
|
|
|
|
return print_and_error("Can't mount /sdcard\n");
|
|
|
|
|
2010-03-26 00:58:45 +00:00
|
|
|
int ret;
|
2010-04-19 01:06:24 +00:00
|
|
|
struct statfs s;
|
2010-03-26 00:58:45 +00:00
|
|
|
if (0 != (ret = statfs("/sdcard", &s)))
|
|
|
|
return print_and_error("Unable to stat /sdcard\n");
|
2010-04-01 19:20:39 +00:00
|
|
|
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)
|
2010-03-26 06:19:19 +00:00
|
|
|
ui_print("There may not be enough free space to complete backup... continuing...\n");
|
2010-03-26 00:58:45 +00:00
|
|
|
|
2010-03-13 07:21:12 +00:00
|
|
|
char tmp[PATH_MAX];
|
|
|
|
sprintf(tmp, "mkdir -p %s", backup_path);
|
|
|
|
__system(tmp);
|
|
|
|
|
|
|
|
ui_print("Backing up boot...\n");
|
|
|
|
sprintf(tmp, "%s/%s", backup_path, "boot.img");
|
|
|
|
ret = dump_image("boot", tmp, NULL);
|
|
|
|
if (0 != ret)
|
|
|
|
return print_and_error("Error while dumping boot image!\n");
|
2010-03-29 21:46:00 +00:00
|
|
|
|
|
|
|
ui_print("Backing up recovery...\n");
|
|
|
|
sprintf(tmp, "%s/%s", backup_path, "recovery.img");
|
|
|
|
ret = dump_image("recovery", tmp, NULL);
|
|
|
|
if (0 != ret)
|
|
|
|
return print_and_error("Error while dumping boot image!\n");
|
|
|
|
|
2010-04-01 19:20:39 +00:00
|
|
|
if (0 != (ret = nandroid_backup_partition(backup_path, "SYSTEM:")))
|
2010-03-15 23:31:03 +00:00
|
|
|
return ret;
|
|
|
|
|
2010-04-01 19:20:39 +00:00
|
|
|
if (0 != (ret = nandroid_backup_partition(backup_path, "DATA:")))
|
2010-03-15 23:31:03 +00:00
|
|
|
return ret;
|
|
|
|
|
2010-06-15 05:04:22 +00:00
|
|
|
#ifdef HAS_DATADATA
|
|
|
|
if (0 != (ret = nandroid_backup_partition(backup_path, "DATADATA:")))
|
|
|
|
return ret;
|
|
|
|
#endif
|
|
|
|
|
2010-04-01 19:20:39 +00:00
|
|
|
if (0 != (ret = nandroid_backup_partition(backup_path, "CACHE:")))
|
2010-03-15 23:31:03 +00:00
|
|
|
return ret;
|
2010-03-31 06:29:43 +00:00
|
|
|
|
2010-04-03 05:17:42 +00:00
|
|
|
struct stat st;
|
2010-06-21 19:11:13 +00:00
|
|
|
if (0 != stat(SDEXT_DEVICE, &st))
|
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
|
|
|
|
{
|
|
|
|
if (0 != ensure_root_path_mounted("SDEXT:"))
|
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");
|
2010-04-04 06:28:39 +00:00
|
|
|
else if (0 != (ret = nandroid_backup_partition(backup_path, "SDEXT:")))
|
|
|
|
return ret;
|
|
|
|
}
|
2010-03-13 07:21:12 +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-04-18 23:00:21 +00:00
|
|
|
int nandroid_restore_partition(const char* backup_path, const char* root) {
|
2010-03-15 23:31:03 +00:00
|
|
|
int ret = 0;
|
|
|
|
char mount_point[PATH_MAX];
|
2010-04-01 19:20:39 +00:00
|
|
|
translate_root_path(root, mount_point, PATH_MAX);
|
|
|
|
char* name = basename(mount_point);
|
2010-03-15 23:31:03 +00:00
|
|
|
|
2010-06-09 19:41:20 +00:00
|
|
|
struct stat file_info;
|
|
|
|
unyaffs_callback callback = NULL;
|
|
|
|
if (0 != stat("/sdcard/clockworkmod/.hidenandroidprogress", &file_info)) {
|
|
|
|
callback = yaffs_callback;
|
|
|
|
}
|
|
|
|
|
2010-03-15 23:31:03 +00:00
|
|
|
ui_print("Restoring %s...\n", name);
|
|
|
|
if (0 != (ret = ensure_root_path_unmounted(root))) {
|
|
|
|
ui_print("Can't unmount %s!\n", mount_point);
|
|
|
|
return ret;
|
|
|
|
}
|
2010-04-18 23:00:21 +00:00
|
|
|
if (0 != (ret = format_root_device(root))) {
|
2010-03-15 23:31:03 +00:00
|
|
|
ui_print("Error while formatting %s!\n", root);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 != (ret = ensure_root_path_mounted(root))) {
|
|
|
|
ui_print("Can't mount %s!\n", mount_point);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
char tmp[PATH_MAX];
|
|
|
|
sprintf(tmp, "%s/%s.img", backup_path, name);
|
2010-06-09 19:41:20 +00:00
|
|
|
if (0 != (ret = unyaffs(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-04-18 23:00:21 +00:00
|
|
|
if (0 != strcmp(root, "CACHE")) {
|
|
|
|
ensure_root_path_unmounted(root);
|
|
|
|
}
|
|
|
|
return 0;
|
2010-04-01 19:20:39 +00:00
|
|
|
}
|
|
|
|
|
2010-04-18 23:00:21 +00:00
|
|
|
int nandroid_restore(const char* backup_path, int restore_boot, int restore_system, int restore_data, int restore_cache, int restore_sdext)
|
2010-03-13 07:21:12 +00:00
|
|
|
{
|
|
|
|
ui_set_background(BACKGROUND_ICON_INSTALLING);
|
|
|
|
ui_show_indeterminate_progress();
|
|
|
|
yaffs_files_total = 0;
|
|
|
|
|
|
|
|
if (ensure_root_path_mounted("SDCARD:") != 0)
|
|
|
|
return print_and_error("Can't mount /sdcard\n");
|
|
|
|
|
|
|
|
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;
|
2010-03-26 01:19:23 +00:00
|
|
|
if (restore_boot)
|
|
|
|
{
|
2010-04-19 04:21:58 +00:00
|
|
|
ui_print("Erasing boot before restore...\n");
|
|
|
|
if (0 != (ret = format_root_device("BOOT:")))
|
|
|
|
return print_and_error("Error while formatting BOOT:!\n");
|
2010-03-26 01:19:23 +00:00
|
|
|
sprintf(tmp, "flash_image boot %s/boot.img", backup_path);
|
|
|
|
ui_print("Restoring boot image...\n");
|
|
|
|
if (0 != (ret = __system(tmp))) {
|
|
|
|
ui_print("Error while flashing boot image!");
|
|
|
|
return ret;
|
|
|
|
}
|
2010-03-16 20:34:51 +00:00
|
|
|
}
|
|
|
|
|
2010-04-01 19:20:39 +00:00
|
|
|
if (restore_system && 0 != (ret = nandroid_restore_partition(backup_path, "SYSTEM:")))
|
2010-03-15 23:31:03 +00:00
|
|
|
return ret;
|
|
|
|
|
2010-04-01 19:20:39 +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
|
|
|
|
|
|
|
#ifdef HAS_DATADATA
|
|
|
|
if (restore_data && 0 != (ret = nandroid_restore_partition(backup_path, "DATADATA:")))
|
|
|
|
return ret;
|
|
|
|
#endif
|
2010-03-15 23:31:03 +00:00
|
|
|
|
2010-04-01 19:20:39 +00:00
|
|
|
if (restore_cache && 0 != (ret = nandroid_restore_partition(backup_path, "CACHE:")))
|
2010-03-15 23:31:03 +00:00
|
|
|
return ret;
|
|
|
|
|
2010-04-01 19:20:39 +00:00
|
|
|
if (restore_sdext)
|
|
|
|
{
|
|
|
|
struct statfs s;
|
|
|
|
sprintf(tmp, "%s/sd-ext.img", backup_path);
|
|
|
|
if (0 != (ret = statfs(tmp, &s)))
|
|
|
|
ui_print("sd-ext.img not found. Skipping restore of /sd-ext.");
|
2010-04-18 23:00:21 +00:00
|
|
|
else if (0 != (ret = nandroid_restore_partition(backup_path, "SDEXT:")))
|
2010-04-01 19:20:39 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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-07-03 20:56:45 +00:00
|
|
|
|
|
|
|
void nandroid_generate_timestamp_path(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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
return nandroid_restore(argv[2], 1, 1, 1, 1, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return nandroid_usage();
|
|
|
|
}
|