blob: 56c633dbd034d8eb9f76cd1beabb4d403032ec53 [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
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -080033#include "../../external/yaffs2/yaffs2/utils/mkyaffs2image.h"
34#include "../../external/yaffs2/yaffs2/utils/unyaffs.h"
35
Koushik K. Dutta78686292010-03-25 17:58:45 -070036#include <sys/vfs.h>
37
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -080038#include "extendedcommands.h"
39#include "nandroid.h"
Koushik Dutta50822992011-06-08 19:03:27 -070040#include "mounts.h"
41
Koushik Dutta7adeadc2011-05-26 11:47:56 -070042#include "flashutils/flashutils.h"
43#include <libgen.h>
44
Koushik Dutta7adeadc2011-05-26 11:47:56 -070045void nandroid_generate_timestamp_path(const char* backup_path)
46{
47 time_t t = time(NULL);
48 struct tm *tmp = localtime(&t);
49 if (tmp == NULL)
50 {
51 struct timeval tp;
52 gettimeofday(&tp, NULL);
53 sprintf(backup_path, "/sdcard/clockworkmod/backup/%d", tp.tv_sec);
54 }
55 else
56 {
57 strftime(backup_path, PATH_MAX, "/sdcard/clockworkmod/backup/%F.%H.%M.%S", tmp);
58 }
59}
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -080060
Koushik Dutta50822992011-06-08 19:03:27 -070061static int print_and_error(const char* message) {
Koushik Duttadf1e4062010-12-18 17:42:31 -080062 ui_print("%s", message);
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -070063 return 1;
64}
65
Koushik Dutta50822992011-06-08 19:03:27 -070066static int yaffs_files_total = 0;
67static int yaffs_files_count = 0;
68static void yaffs_callback(const char* filename)
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -080069{
Koushik Dutta50822992011-06-08 19:03:27 -070070 if (filename == NULL)
71 return;
72 const char* justfile = basename(filename);
73 char tmp[PATH_MAX];
74 strcpy(tmp, justfile);
75 if (tmp[strlen(tmp) - 1] == '\n')
76 tmp[strlen(tmp) - 1] = NULL;
77 if (strlen(tmp) < 30)
78 ui_print("%s", tmp);
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -080079 yaffs_files_count++;
80 if (yaffs_files_total != 0)
81 ui_set_progress((float)yaffs_files_count / (float)yaffs_files_total);
82 ui_reset_text_col();
83}
84
Koushik Dutta50822992011-06-08 19:03:27 -070085static void compute_directory_stats(const char* directory)
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -080086{
87 char tmp[PATH_MAX];
88 sprintf(tmp, "find %s | wc -l > /tmp/dircount", directory);
89 __system(tmp);
90 char count_text[100];
91 FILE* f = fopen("/tmp/dircount", "r");
92 fread(count_text, 1, sizeof(count_text), f);
93 fclose(f);
94 yaffs_files_count = 0;
95 yaffs_files_total = atoi(count_text);
96 ui_reset_progress();
97 ui_show_progress(1, 0);
98}
99
Koushik Dutta50822992011-06-08 19:03:27 -0700100typedef void (*file_event_callback)(const char* filename);
Koushik Dutta55e5e7b2011-06-14 23:39:59 -0700101typedef int (*nandroid_backup_handler)(const char* backup_path, const char* backup_file_image, int callback);
Koushik Dutta50822992011-06-08 19:03:27 -0700102
Koushik Dutta55e5e7b2011-06-14 23:39:59 -0700103static int mkyaffs2image_wrapper(const char* backup_path, const char* backup_file_image, int callback) {
Koushik Dutta30a937a2011-09-05 21:14:06 -0700104 char backup_file_image_with_extension[PATH_MAX];
105 sprintf(backup_file_image_with_extension, "%s.img", backup_file_image);
106 return mkyaffs2image(backup_path, backup_file_image_with_extension, 0, callback ? yaffs_callback : NULL);
Koushik Dutta50822992011-06-08 19:03:27 -0700107}
108
Koushik Dutta55e5e7b2011-06-14 23:39:59 -0700109static int tar_compress_wrapper(const char* backup_path, const char* backup_file_image, int callback) {
Koushik Dutta50822992011-06-08 19:03:27 -0700110 char tmp[PATH_MAX];
111 if (strcmp(backup_path, "/data") == 0 && volume_for_path("/sdcard") == NULL)
Koushik Dutta30a937a2011-09-05 21:14:06 -0700112 sprintf(tmp, "cd $(dirname %s) ; tar cvf %s.tar --exclude 'media' $(basename %s) ; exit $?", backup_path, backup_file_image, backup_path);
Koushik Dutta50822992011-06-08 19:03:27 -0700113 else
Koushik Dutta30a937a2011-09-05 21:14:06 -0700114 sprintf(tmp, "cd $(dirname %s) ; tar cvf %s.tar $(basename %s) ; exit $?", backup_path, backup_file_image, backup_path);
Koushik Dutta50822992011-06-08 19:03:27 -0700115
116 FILE *fp = __popen(tmp, "r");
117 if (fp == NULL) {
118 ui_print("Unable to execute tar.\n");
119 return -1;
120 }
121
122 while (fgets(tmp, PATH_MAX, fp) != NULL) {
123 tmp[PATH_MAX - 1] = NULL;
Koushik Dutta55e5e7b2011-06-14 23:39:59 -0700124 if (callback)
Koushik Dutta50822992011-06-08 19:03:27 -0700125 yaffs_callback(tmp);
126 }
127
128 return __pclose(fp);
129}
130
131static nandroid_backup_handler get_backup_handler(const char *backup_path) {
132 Volume *v = volume_for_path(backup_path);
133 if (v == NULL) {
134 ui_print("Unable to find volume.\n");
135 return NULL;
136 }
Koushik Dutta50822992011-06-08 19:03:27 -0700137 MountedVolume *mv = find_mounted_volume_by_mount_point(v->mount_point);
138 if (mv == NULL) {
139 ui_print("Unable to find mounted volume: %s\n", v->mount_point);
140 return NULL;
141 }
142
Koushik Dutta38a92142011-06-10 09:45:52 -0700143 if (strcmp(backup_path, "/data") == 0 && is_data_media()) {
144 return tar_compress_wrapper;
145 }
146
Koushik Dutta30a937a2011-09-05 21:14:06 -0700147 // cwr5, we prefer tar for everything except yaffs2
148 if (strcmp("yaffs2", mv->filesystem) == 0) {
Koushik Dutta38a92142011-06-10 09:45:52 -0700149 return mkyaffs2image_wrapper;
150 }
151
Koushik Dutta30a937a2011-09-05 21:14:06 -0700152 char str[255];
153 char* partition;
154 property_get("ro.cwm.prefer_tar", str, "true");
155 if (strcmp("true", str) != 0) {
Koushik Dutta50822992011-06-08 19:03:27 -0700156 return mkyaffs2image_wrapper;
157 }
158
159 return tar_compress_wrapper;
160}
161
162
Koushik Duttadf1e4062010-12-18 17:42:31 -0800163int nandroid_backup_partition_extended(const char* backup_path, const char* mount_point, int umount_when_finished) {
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700164 int ret = 0;
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700165 char* name = basename(mount_point);
Koushik Dutta58131e72010-06-09 12:41:20 -0700166
167 struct stat file_info;
Koushik Dutta30a937a2011-09-05 21:14:06 -0700168 int callback = stat("/sdcard/clockworkmod/.hidenandroidprogress", &file_info) != 0;
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700169
170 ui_print("Backing up %s...\n", name);
Koushik Duttadf1e4062010-12-18 17:42:31 -0800171 if (0 != (ret = ensure_path_mounted(mount_point) != 0)) {
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700172 ui_print("Can't mount %s!\n", mount_point);
173 return ret;
174 }
175 compute_directory_stats(mount_point);
176 char tmp[PATH_MAX];
Koushik Dutta30a937a2011-09-05 21:14:06 -0700177 scan_mounted_volumes();
178 Volume *v = volume_for_path(mount_point);
179 MountedVolume *mv = NULL;
180 if (v != NULL)
181 mv = find_mounted_volume_by_mount_point(v->mount_point);
182 if (mv == NULL || mv->filesystem == NULL)
183 sprintf(tmp, "%s/%s.auto", backup_path, name);
184 else
185 sprintf(tmp, "%s/%s.%s", backup_path, name, mv->filesystem);
Koushik Dutta50822992011-06-08 19:03:27 -0700186 nandroid_backup_handler backup_handler = get_backup_handler(mount_point);
187 if (backup_handler == NULL) {
188 ui_print("Error finding an appropriate backup handler.\n");
189 return -2;
190 }
191 ret = backup_handler(mount_point, tmp, callback);
Koushik Dutta062d6b02010-07-03 13:54:21 -0700192 if (umount_when_finished) {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800193 ensure_path_unmounted(mount_point);
Koushik Dutta062d6b02010-07-03 13:54:21 -0700194 }
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700195 if (0 != ret) {
Koushik Dutta50822992011-06-08 19:03:27 -0700196 ui_print("Error while making a backup image of %s!\n", mount_point);
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700197 return ret;
198 }
199 return 0;
200}
201
Koushik Dutta53c9bd82011-01-04 11:38:31 -0800202int nandroid_backup_partition(const char* backup_path, const char* root) {
203 Volume *vol = volume_for_path(root);
204 // make sure the volume exists before attempting anything...
205 if (vol == NULL || vol->fs_type == NULL)
206 return NULL;
207
208 // see if we need a raw backup (mtd)
209 char tmp[PATH_MAX];
210 int ret;
Christopher Lais066a1022011-01-16 06:02:34 -0600211 if (strcmp(vol->fs_type, "mtd") == 0 ||
Koushik Dutta33e37f32011-02-27 16:33:32 -0800212 strcmp(vol->fs_type, "bml") == 0 ||
Christopher Lais066a1022011-01-16 06:02:34 -0600213 strcmp(vol->fs_type, "emmc") == 0) {
Koushik Dutta53c9bd82011-01-04 11:38:31 -0800214 const char* name = basename(root);
215 sprintf(tmp, "%s/%s.img", backup_path, name);
216 ui_print("Backing up %s image...\n", name);
Koushik Dutta8a6bc772011-05-26 11:14:15 -0700217 if (0 != (ret = backup_raw_partition(vol->fs_type, vol->device, tmp))) {
Koushik Dutta53c9bd82011-01-04 11:38:31 -0800218 ui_print("Error while backing up %s image!", name);
219 return ret;
220 }
221 return 0;
222 }
223
Koushik Dutta062d6b02010-07-03 13:54:21 -0700224 return nandroid_backup_partition_extended(backup_path, root, 1);
225}
226
Koushik Dutta2f73e582010-04-18 16:00:21 -0700227int nandroid_backup(const char* backup_path)
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800228{
229 ui_set_background(BACKGROUND_ICON_INSTALLING);
230
Koushik Dutta89ed0b72011-10-26 21:25:34 -0700231 if (ensure_path_mounted(backup_path) != 0) {
232 return print_and_error("Can't mount backup path.\n");
233 }
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800234
Koushik Dutta89ed0b72011-10-26 21:25:34 -0700235 Volume* volume = volume_for_path(backup_path);
Koushik K. Dutta78686292010-03-25 17:58:45 -0700236 int ret;
Koushik K. Duttaf3534d02010-04-18 18:06:24 -0700237 struct statfs s;
Koushik Duttaddc12412011-11-23 14:06:12 -0800238 if (NULL != volume) {
239 if (0 != (ret = statfs(volume->mount_point, &s)))
240 return print_and_error("Unable to stat backup path.\n");
241 uint64_t bavail = s.f_bavail;
242 uint64_t bsize = s.f_bsize;
243 uint64_t sdcard_free = bavail * bsize;
244 uint64_t sdcard_free_mb = sdcard_free / (uint64_t)(1024 * 1024);
245 ui_print("SD Card space free: %lluMB\n", sdcard_free_mb);
246 if (sdcard_free_mb < 150)
247 ui_print("There may not be enough free space to complete backup... continuing...\n");
248 }
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800249 char tmp[PATH_MAX];
250 sprintf(tmp, "mkdir -p %s", backup_path);
251 __system(tmp);
252
Koushik Dutta53c9bd82011-01-04 11:38:31 -0800253 if (0 != (ret = nandroid_backup_partition(backup_path, "/boot")))
254 return ret;
Koushik K. Dutta6923cc32010-03-29 14:46:00 -0700255
Koushik Dutta53c9bd82011-01-04 11:38:31 -0800256 if (0 != (ret = nandroid_backup_partition(backup_path, "/recovery")))
257 return ret;
Koushik K. Dutta6923cc32010-03-29 14:46:00 -0700258
Koushik Dutta5ee98b22011-03-02 19:35:05 -0800259 Volume *vol = volume_for_path("/wimax");
260 if (vol != NULL && 0 == stat(vol->device, &s))
agrabren1f76a5d2010-12-29 12:15:18 -0600261 {
Joshua Wise05d4a092010-12-31 02:05:35 -0500262 char serialno[PROPERTY_VALUE_MAX];
Joshua Wiseca889ec2010-12-30 02:48:25 -0500263 ui_print("Backing up WiMAX...\n");
Joshua Wise05d4a092010-12-31 02:05:35 -0500264 serialno[0] = 0;
265 property_get("ro.serialno", serialno, "");
266 sprintf(tmp, "%s/wimax.%s.img", backup_path, serialno);
Koushik Duttae62132b2011-05-26 11:29:29 -0700267 ret = backup_raw_partition(vol->fs_type, vol->device, tmp);
agrabren1f76a5d2010-12-29 12:15:18 -0600268 if (0 != ret)
Joshua Wiseca889ec2010-12-30 02:48:25 -0500269 return print_and_error("Error while dumping WiMAX image!\n");
agrabren1f76a5d2010-12-29 12:15:18 -0600270 }
271
Koushik Duttadf1e4062010-12-18 17:42:31 -0800272 if (0 != (ret = nandroid_backup_partition(backup_path, "/system")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700273 return ret;
274
Koushik Duttadf1e4062010-12-18 17:42:31 -0800275 if (0 != (ret = nandroid_backup_partition(backup_path, "/data")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700276 return ret;
277
Koushik Dutta5460f0c2010-12-18 22:37:49 -0800278 if (has_datadata()) {
279 if (0 != (ret = nandroid_backup_partition(backup_path, "/datadata")))
280 return ret;
281 }
Koushik Dutta8b5e1852010-06-14 22:04:22 -0700282
Koushik Dutta5ee98b22011-03-02 19:35:05 -0800283 if (0 != stat("/sdcard/.android_secure", &s))
Koushik Dutta062d6b02010-07-03 13:54:21 -0700284 {
285 ui_print("No /sdcard/.android_secure found. Skipping backup of applications on external storage.\n");
286 }
287 else
288 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800289 if (0 != (ret = nandroid_backup_partition_extended(backup_path, "/sdcard/.android_secure", 0)))
Koushik Dutta062d6b02010-07-03 13:54:21 -0700290 return ret;
291 }
292
Koushik Duttadf1e4062010-12-18 17:42:31 -0800293 if (0 != (ret = nandroid_backup_partition_extended(backup_path, "/cache", 0)))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700294 return ret;
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700295
Koushik Dutta5ee98b22011-03-02 19:35:05 -0800296 vol = volume_for_path("/sd-ext");
297 if (vol == NULL || 0 != stat(vol->device, &s))
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700298 {
Koushik K. Dutta71ef11f2010-04-02 22:17:42 -0700299 ui_print("No sd-ext found. Skipping backup of sd-ext.\n");
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700300 }
301 else
302 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800303 if (0 != ensure_path_mounted("/sd-ext"))
Koushik Dutta8b5e1852010-06-14 22:04:22 -0700304 ui_print("Could not mount sd-ext. sd-ext backup may not be supported on this device. Skipping backup of sd-ext.\n");
Koushik Duttae06e5392011-01-16 02:33:04 -0800305 else if (0 != (ret = nandroid_backup_partition(backup_path, "/sd-ext")))
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700306 return ret;
307 }
Koushik Dutta062d6b02010-07-03 13:54:21 -0700308
Koushik K. Duttad7178922010-03-24 14:38:40 -0700309 ui_print("Generating md5 sum...\n");
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700310 sprintf(tmp, "nandroid-md5.sh %s", backup_path);
Koushik K. Duttab0a25032010-03-24 10:34:38 -0700311 if (0 != (ret = __system(tmp))) {
312 ui_print("Error while generating md5 sum!\n");
313 return ret;
314 }
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800315
Koushik K. Dutta3f38f322010-03-12 23:45:25 -0800316 sync();
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800317 ui_set_background(BACKGROUND_ICON_NONE);
318 ui_reset_progress();
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700319 ui_print("\nBackup complete!\n");
320 return 0;
321}
322
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700323typedef int (*format_function)(char* root);
324
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700325static void ensure_directory(const char* dir) {
326 char tmp[PATH_MAX];
327 sprintf(tmp, "mkdir -p %s", dir);
328 __system(tmp);
329}
330
Koushik Dutta7905c802011-06-15 00:00:55 -0700331typedef int (*nandroid_restore_handler)(const char* backup_file_image, const char* backup_path, int callback);
Koushik Dutta50822992011-06-08 19:03:27 -0700332
Koushik Dutta7905c802011-06-15 00:00:55 -0700333static int unyaffs_wrapper(const char* backup_file_image, const char* backup_path, int callback) {
Koushik Duttac9159372011-07-20 01:26:02 -0700334 return unyaffs(backup_file_image, backup_path, callback ? yaffs_callback : NULL);
Koushik Dutta50822992011-06-08 19:03:27 -0700335}
336
Koushik Dutta7905c802011-06-15 00:00:55 -0700337static int tar_extract_wrapper(const char* backup_file_image, const char* backup_path, int callback) {
Koushik Dutta50822992011-06-08 19:03:27 -0700338 char tmp[PATH_MAX];
339 sprintf(tmp, "cd $(dirname %s) ; tar xvf %s ; exit $?", backup_path, backup_file_image);
340
341 char path[PATH_MAX];
342 FILE *fp = __popen(tmp, "r");
343 if (fp == NULL) {
344 ui_print("Unable to execute tar.\n");
345 return -1;
346 }
347
348 while (fgets(path, PATH_MAX, fp) != NULL) {
Koushik Dutta7905c802011-06-15 00:00:55 -0700349 if (callback)
350 yaffs_callback(path);
Koushik Dutta50822992011-06-08 19:03:27 -0700351 }
352
353 return __pclose(fp);
354}
355
356static nandroid_restore_handler get_restore_handler(const char *backup_path) {
357 Volume *v = volume_for_path(backup_path);
358 if (v == NULL) {
359 ui_print("Unable to find volume.\n");
360 return NULL;
361 }
362 scan_mounted_volumes();
363 MountedVolume *mv = find_mounted_volume_by_mount_point(v->mount_point);
364 if (mv == NULL) {
365 ui_print("Unable to find mounted volume: %s\n", v->mount_point);
366 return NULL;
367 }
368
Koushik Dutta38a92142011-06-10 09:45:52 -0700369 if (strcmp(backup_path, "/data") == 0 && is_data_media()) {
370 return tar_extract_wrapper;
371 }
372
Koushik Dutta30a937a2011-09-05 21:14:06 -0700373 // cwr 5, we prefer tar for everything unless it is yaffs2
Koushik Dutta38a92142011-06-10 09:45:52 -0700374 char str[255];
375 char* partition;
376 property_get("ro.cwm.prefer_tar", str, "false");
377 if (strcmp("true", str) != 0) {
378 return unyaffs_wrapper;
379 }
380
Koushik Dutta50822992011-06-08 19:03:27 -0700381 if (strcmp("yaffs2", mv->filesystem) == 0) {
382 return unyaffs_wrapper;
383 }
384
385 return tar_extract_wrapper;
386}
387
Koushik Duttadf1e4062010-12-18 17:42:31 -0800388int nandroid_restore_partition_extended(const char* backup_path, const char* mount_point, int umount_when_finished) {
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700389 int ret = 0;
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700390 char* name = basename(mount_point);
Koushik Dutta30a937a2011-09-05 21:14:06 -0700391
392 nandroid_restore_handler restore_handler = NULL;
Donovan Bartisha87bb5f2011-09-29 15:37:19 -0500393 const char *filesystems[] = { "yaffs2", "ext2", "ext3", "ext4", "vfat", "rfs", NULL };
Koushik Dutta30a937a2011-09-05 21:14:06 -0700394 const char* backup_filesystem = NULL;
395 Volume *vol = volume_for_path(mount_point);
396 const char *device = NULL;
397 if (vol != NULL)
398 device = vol->device;
399
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700400 char tmp[PATH_MAX];
401 sprintf(tmp, "%s/%s.img", backup_path, name);
Koushik Dutta58131e72010-06-09 12:41:20 -0700402 struct stat file_info;
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700403 if (0 != (ret = statfs(tmp, &file_info))) {
Koushik Dutta30a937a2011-09-05 21:14:06 -0700404 // can't find the backup, it may be the new backup format?
405 // iterate through the backup types
406 printf("couldn't find default\n");
407 char *filesystem;
408 int i = 0;
409 while ((filesystem = filesystems[i]) != NULL) {
410 sprintf(tmp, "%s/%s.%s.img", backup_path, name, filesystem);
411 if (0 == (ret = statfs(tmp, &file_info))) {
412 backup_filesystem = filesystem;
413 restore_handler = unyaffs_wrapper;
414 break;
415 }
416 sprintf(tmp, "%s/%s.%s.tar", backup_path, name, filesystem);
417 if (0 == (ret = statfs(tmp, &file_info))) {
418 backup_filesystem = filesystem;
419 restore_handler = tar_extract_wrapper;
420 break;
421 }
422 i++;
423 }
424
425 if (backup_filesystem == NULL || restore_handler == NULL) {
426 ui_print("%s.img not found. Skipping restore of %s.\n", name, mount_point);
427 return 0;
428 }
429 else {
430 printf("Found new backup image: %s\n", tmp);
431 }
432
433 // If the fs_type of this volume is "auto", let's revert to using a
434 // rm -rf, rather than trying to do a ext3/ext4/whatever format.
435 // This is because some phones (like DroidX) will freak out if you
436 // reformat the /system or /data partitions, and not boot due to
437 // a locked bootloader.
Koushik Duttaddc12412011-11-23 14:06:12 -0800438 // Other devices, like the Galaxy Nexus, XOOM, and Galaxy Tab 10.1
439 // have a /sdcard symlinked to /data/media. /data is set to "auto"
440 // so that when the format occurs, /data/media is not erased.
Koushik Dutta30a937a2011-09-05 21:14:06 -0700441 // The "auto" fs type preserves the file system, and does not
442 // trigger that lock.
443 // Or of volume does not exist (.android_secure), just rm -rf.
444 if (vol == NULL || 0 == strcmp(vol->fs_type, "auto"))
445 backup_filesystem = NULL;
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700446 }
447
448 ensure_directory(mount_point);
449
Koushik Dutta30a937a2011-09-05 21:14:06 -0700450 int callback = stat("/sdcard/clockworkmod/.hidenandroidprogress", &file_info) != 0;
Koushik Dutta58131e72010-06-09 12:41:20 -0700451
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700452 ui_print("Restoring %s...\n", name);
Koushik Dutta30a937a2011-09-05 21:14:06 -0700453 if (backup_filesystem == NULL) {
454 if (0 != (ret = format_volume(mount_point))) {
455 ui_print("Error while formatting %s!\n", mount_point);
456 return ret;
457 }
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700458 }
Koushik Dutta30a937a2011-09-05 21:14:06 -0700459 else if (0 != (ret = format_device(device, mount_point, backup_filesystem))) {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800460 ui_print("Error while formatting %s!\n", mount_point);
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700461 return ret;
462 }
Koushik Dutta30a937a2011-09-05 21:14:06 -0700463
Koushik Duttadf1e4062010-12-18 17:42:31 -0800464 if (0 != (ret = ensure_path_mounted(mount_point))) {
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700465 ui_print("Can't mount %s!\n", mount_point);
466 return ret;
467 }
Koushik Dutta30a937a2011-09-05 21:14:06 -0700468
469 if (restore_handler == NULL)
470 restore_handler = get_restore_handler(mount_point);
Koushik Dutta50822992011-06-08 19:03:27 -0700471 if (restore_handler == NULL) {
472 ui_print("Error finding an appropriate restore handler.\n");
473 return -2;
474 }
475 if (0 != (ret = restore_handler(tmp, mount_point, callback))) {
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700476 ui_print("Error while restoring %s!\n", mount_point);
477 return ret;
478 }
Koushik Dutta2f73e582010-04-18 16:00:21 -0700479
Koushik Dutta062d6b02010-07-03 13:54:21 -0700480 if (umount_when_finished) {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800481 ensure_path_unmounted(mount_point);
Koushik Dutta2f73e582010-04-18 16:00:21 -0700482 }
Koushik Dutta062d6b02010-07-03 13:54:21 -0700483
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800484 return 0;
485}
486
Koushik Dutta062d6b02010-07-03 13:54:21 -0700487int nandroid_restore_partition(const char* backup_path, const char* root) {
Koushik Dutta53c9bd82011-01-04 11:38:31 -0800488 Volume *vol = volume_for_path(root);
489 // make sure the volume exists...
490 if (vol == NULL || vol->fs_type == NULL)
491 return 0;
492
493 // see if we need a raw restore (mtd)
494 char tmp[PATH_MAX];
Christopher Lais066a1022011-01-16 06:02:34 -0600495 if (strcmp(vol->fs_type, "mtd") == 0 ||
Koushik Dutta33e37f32011-02-27 16:33:32 -0800496 strcmp(vol->fs_type, "bml") == 0 ||
Christopher Lais066a1022011-01-16 06:02:34 -0600497 strcmp(vol->fs_type, "emmc") == 0) {
Koushik Dutta53c9bd82011-01-04 11:38:31 -0800498 int ret;
499 const char* name = basename(root);
500 ui_print("Erasing %s before restore...\n", name);
501 if (0 != (ret = format_volume(root))) {
502 ui_print("Error while erasing %s image!", name);
503 return ret;
504 }
505 sprintf(tmp, "%s%s.img", backup_path, root);
506 ui_print("Restoring %s image...\n", name);
Koushik Dutta8a6bc772011-05-26 11:14:15 -0700507 if (0 != (ret = restore_raw_partition(vol->fs_type, vol->device, tmp))) {
Koushik Dutta53c9bd82011-01-04 11:38:31 -0800508 ui_print("Error while flashing %s image!", name);
509 return ret;
510 }
511 return 0;
512 }
Koushik Dutta062d6b02010-07-03 13:54:21 -0700513 return nandroid_restore_partition_extended(backup_path, root, 1);
514}
515
agrabren1f76a5d2010-12-29 12:15:18 -0600516int 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. Duttaee57bbc2010-03-12 23:21:12 -0800517{
518 ui_set_background(BACKGROUND_ICON_INSTALLING);
519 ui_show_indeterminate_progress();
520 yaffs_files_total = 0;
521
Koushik Dutta89ed0b72011-10-26 21:25:34 -0700522 if (ensure_path_mounted(backup_path) != 0)
523 return print_and_error("Can't mount backup path\n");
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800524
525 char tmp[PATH_MAX];
526
527 ui_print("Checking MD5 sums...\n");
Koushik K. Duttaface5882010-03-13 10:15:55 -0800528 sprintf(tmp, "cd %s && md5sum -c nandroid.md5", backup_path);
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800529 if (0 != __system(tmp))
530 return print_and_error("MD5 mismatch!\n");
531
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700532 int ret;
Koushik Dutta53c9bd82011-01-04 11:38:31 -0800533
534 if (restore_boot && NULL != volume_for_path("/boot") && 0 != (ret = nandroid_restore_partition(backup_path, "/boot")))
535 return ret;
Koushik K. Duttaf7215942010-03-16 13:34:51 -0700536
Koushik Dutta5ee98b22011-03-02 19:35:05 -0800537 struct stat s;
538 Volume *vol = volume_for_path("/wimax");
539 if (restore_wimax && vol != NULL && 0 == stat(vol->device, &s))
agrabren1f76a5d2010-12-29 12:15:18 -0600540 {
Joshua Wise05d4a092010-12-31 02:05:35 -0500541 char serialno[PROPERTY_VALUE_MAX];
542
543 serialno[0] = 0;
544 property_get("ro.serialno", serialno, "");
545 sprintf(tmp, "%s/wimax.%s.img", backup_path, serialno);
agrabren1f76a5d2010-12-29 12:15:18 -0600546
547 struct stat st;
548 if (0 != stat(tmp, &st))
549 {
Joshua Wiseca889ec2010-12-30 02:48:25 -0500550 ui_print("WARNING: WiMAX partition exists, but nandroid\n");
551 ui_print(" backup does not contain WiMAX image.\n");
agrabren1f76a5d2010-12-29 12:15:18 -0600552 ui_print(" You should create a new backup to\n");
Joshua Wiseca889ec2010-12-30 02:48:25 -0500553 ui_print(" protect your WiMAX keys.\n");
agrabren1f76a5d2010-12-29 12:15:18 -0600554 }
555 else
556 {
Joshua Wise5a3f1d82010-12-31 02:16:00 -0500557 ui_print("Erasing WiMAX before restore...\n");
Koushik Duttaa25deae2011-01-01 18:17:48 -0800558 if (0 != (ret = format_volume("/wimax")))
Joshua Wise5a3f1d82010-12-31 02:16:00 -0500559 return print_and_error("Error while formatting wimax!\n");
Joshua Wise05d4a092010-12-31 02:05:35 -0500560 ui_print("Restoring WiMAX image...\n");
Koushik Duttae62132b2011-05-26 11:29:29 -0700561 if (0 != (ret = restore_raw_partition(vol->fs_type, vol->device, tmp)))
agrabren1f76a5d2010-12-29 12:15:18 -0600562 return ret;
563 }
564 }
565
Koushik Duttadf1e4062010-12-18 17:42:31 -0800566 if (restore_system && 0 != (ret = nandroid_restore_partition(backup_path, "/system")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700567 return ret;
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800568
Koushik Duttadf1e4062010-12-18 17:42:31 -0800569 if (restore_data && 0 != (ret = nandroid_restore_partition(backup_path, "/data")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700570 return ret;
Koushik Dutta8b5e1852010-06-14 22:04:22 -0700571
Koushik Dutta5460f0c2010-12-18 22:37:49 -0800572 if (has_datadata()) {
573 if (restore_data && 0 != (ret = nandroid_restore_partition(backup_path, "/datadata")))
574 return ret;
575 }
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800576
Koushik Duttadf1e4062010-12-18 17:42:31 -0800577 if (restore_data && 0 != (ret = nandroid_restore_partition_extended(backup_path, "/sdcard/.android_secure", 0)))
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700578 return ret;
Koushik Dutta062d6b02010-07-03 13:54:21 -0700579
Koushik Duttadf1e4062010-12-18 17:42:31 -0800580 if (restore_cache && 0 != (ret = nandroid_restore_partition_extended(backup_path, "/cache", 0)))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700581 return ret;
582
Koushik Duttadf1e4062010-12-18 17:42:31 -0800583 if (restore_sdext && 0 != (ret = nandroid_restore_partition(backup_path, "/sd-ext")))
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700584 return ret;
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700585
Koushik K. Dutta3f38f322010-03-12 23:45:25 -0800586 sync();
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800587 ui_set_background(BACKGROUND_ICON_NONE);
588 ui_reset_progress();
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700589 ui_print("\nRestore complete!\n");
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800590 return 0;
591}
Koushik Dutta08370912010-06-26 12:25:02 -0700592
Koushik Dutta08370912010-06-26 12:25:02 -0700593int nandroid_usage()
594{
595 printf("Usage: nandroid backup\n");
596 printf("Usage: nandroid restore <directory>\n");
597 return 1;
598}
599
600int nandroid_main(int argc, char** argv)
601{
602 if (argc > 3 || argc < 2)
603 return nandroid_usage();
604
605 if (strcmp("backup", argv[1]) == 0)
606 {
607 if (argc != 2)
608 return nandroid_usage();
609
610 char backup_path[PATH_MAX];
611 nandroid_generate_timestamp_path(backup_path);
612 return nandroid_backup(backup_path);
613 }
614
615 if (strcmp("restore", argv[1]) == 0)
616 {
617 if (argc != 3)
618 return nandroid_usage();
Koushik Dutta5b7f34a2010-12-29 23:36:03 -0800619 return nandroid_restore(argv[2], 1, 1, 1, 1, 1, 0);
Koushik Dutta08370912010-06-26 12:25:02 -0700620 }
621
622 return nandroid_usage();
623}