blob: ae99cd83badec3f4c2f105342f820b63b51ec594 [file] [log] [blame]
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -08001#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 "commands.h"
34#include "amend/amend.h"
35
36#include "mtdutils/dump_image.h"
37#include "../../external/yaffs2/yaffs2/utils/mkyaffs2image.h"
38#include "../../external/yaffs2/yaffs2/utils/unyaffs.h"
39
40#include "extendedcommands.h"
41#include "nandroid.h"
42
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -070043int print_and_error(char* message) {
44 ui_print(message);
45 return 1;
46}
47
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -080048int yaffs_files_total = 0;
49int yaffs_files_count = 0;
50void yaffs_callback(char* filename)
51{
52 char* justfile = basename(filename);
53 if (strlen(justfile) < 30)
54 ui_print(basename(filename));
55 yaffs_files_count++;
56 if (yaffs_files_total != 0)
57 ui_set_progress((float)yaffs_files_count / (float)yaffs_files_total);
58 ui_reset_text_col();
59}
60
61void compute_directory_stats(char* directory)
62{
63 char tmp[PATH_MAX];
64 sprintf(tmp, "find %s | wc -l > /tmp/dircount", directory);
65 __system(tmp);
66 char count_text[100];
67 FILE* f = fopen("/tmp/dircount", "r");
68 fread(count_text, 1, sizeof(count_text), f);
69 fclose(f);
70 yaffs_files_count = 0;
71 yaffs_files_total = atoi(count_text);
72 ui_reset_progress();
73 ui_show_progress(1, 0);
74}
75
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -070076int nandroid_backup_partition(char* backup_path, char* root, char* name) {
77 int ret = 0;
78 char mount_point[PATH_MAX];
79 sprintf(mount_point, "/%s", name);
80
81 ui_print("Backing up %s...\n", name);
82 if (0 != (ret = ensure_root_path_mounted(root) != 0)) {
83 ui_print("Can't mount %s!\n", mount_point);
84 return ret;
85 }
86 compute_directory_stats(mount_point);
87 char tmp[PATH_MAX];
88 sprintf(tmp, "%s/%s.img", backup_path, name);
89 ret = mkyaffs2image(mount_point, tmp, 0, yaffs_callback);
90 ensure_root_path_unmounted(root);
91 if (0 != ret) {
92 ui_print("Error while making a yaffs2 image of %s!\n", mount_point);
93 return ret;
94 }
95 return 0;
96}
97
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -080098int nandroid_backup(char* backup_path)
99{
100 ui_set_background(BACKGROUND_ICON_INSTALLING);
101
102 if (ensure_root_path_mounted("SDCARD:") != 0)
103 return print_and_error("Can't mount /sdcard\n");
104
105 char tmp[PATH_MAX];
106 sprintf(tmp, "mkdir -p %s", backup_path);
107 __system(tmp);
108
109 int ret;
110 ui_print("Backing up boot...\n");
111 sprintf(tmp, "%s/%s", backup_path, "boot.img");
112 ret = dump_image("boot", tmp, NULL);
113 if (0 != ret)
114 return print_and_error("Error while dumping boot image!\n");
115
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700116 if (0 != (ret = nandroid_backup_partition(backup_path, "SYSTEM:", "system")))
117 return ret;
118
119 if (0 != (ret = nandroid_backup_partition(backup_path, "DATA:", "data")))
120 return ret;
121
122 if (0 != (ret = nandroid_backup_partition(backup_path, "CACHE:", "cache")))
123 return ret;
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800124
Koushik K. Duttaface5882010-03-13 10:15:55 -0800125 sprintf(tmp, "cd %s && md5sum *img > nandroid.md5", backup_path);
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800126 __system(tmp);
127
Koushik K. Dutta3f38f322010-03-12 23:45:25 -0800128 sync();
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800129 ui_set_background(BACKGROUND_ICON_NONE);
130 ui_reset_progress();
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700131 ui_print("\nBackup complete!\n");
132 return 0;
133}
134
135int nandroid_restore_partition(char* backup_path, char* root, char* name) {
136 int ret = 0;
137 char mount_point[PATH_MAX];
138 sprintf(mount_point, "/%s", name);
139
140 ui_print("Restoring %s...\n", name);
141 if (0 != (ret = ensure_root_path_unmounted(root))) {
142 ui_print("Can't unmount %s!\n", mount_point);
143 return ret;
144 }
145 if (0 != (ret = format_root_device(root))) {
146 ui_print("Error while formatting %s!\n", root);
147 return ret;
148 }
149
150 if (0 != (ret = ensure_root_path_mounted(root))) {
151 ui_print("Can't mount %s!\n", mount_point);
152 return ret;
153 }
154
155 char tmp[PATH_MAX];
156 sprintf(tmp, "%s/%s.img", backup_path, name);
157 if (0 != (ret = unyaffs(tmp, mount_point, yaffs_callback))) {
158 ui_print("Error while restoring %s!\n", mount_point);
159 return ret;
160 }
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800161 return 0;
162}
163
164int nandroid_restore(char* backup_path)
165{
166 ui_set_background(BACKGROUND_ICON_INSTALLING);
167 ui_show_indeterminate_progress();
168 yaffs_files_total = 0;
169
170 if (ensure_root_path_mounted("SDCARD:") != 0)
171 return print_and_error("Can't mount /sdcard\n");
172
173 char tmp[PATH_MAX];
174
175 ui_print("Checking MD5 sums...\n");
Koushik K. Duttaface5882010-03-13 10:15:55 -0800176 sprintf(tmp, "cd %s && md5sum -c nandroid.md5", backup_path);
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800177 if (0 != __system(tmp))
178 return print_and_error("MD5 mismatch!\n");
179
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700180 int ret;
181 if (0 != (ret = nandroid_restore_partition(backup_path, "SYSTEM:", "system")))
182 return ret;
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800183
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700184 if (0 != (ret = nandroid_restore_partition(backup_path, "DATA:", "data")))
185 return ret;
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800186
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700187 if (0 != (ret = nandroid_restore_partition(backup_path, "CACHE:", "cache")))
188 return ret;
189
Koushik K. Dutta3f38f322010-03-12 23:45:25 -0800190 sync();
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800191 ui_set_background(BACKGROUND_ICON_NONE);
192 ui_reset_progress();
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700193 ui_print("\nRestore complete!\n");
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800194 return 0;
195}