blob: ed4c65a19cd8bfca592dd11d3cafaa8c87d27823 [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. Dutta78686292010-03-25 17:58:45 -070033#include <sys/vfs.h>
34
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -080035#include "extendedcommands.h"
36#include "nandroid.h"
Koushik Dutta50822992011-06-08 19:03:27 -070037#include "mounts.h"
38
Koushik Dutta7adeadc2011-05-26 11:47:56 -070039#include "flashutils/flashutils.h"
40#include <libgen.h>
41
Koushik Dutta7adeadc2011-05-26 11:47:56 -070042void nandroid_generate_timestamp_path(const char* backup_path)
43{
44 time_t t = time(NULL);
45 struct tm *tmp = localtime(&t);
46 if (tmp == NULL)
47 {
48 struct timeval tp;
49 gettimeofday(&tp, NULL);
50 sprintf(backup_path, "/sdcard/clockworkmod/backup/%d", tp.tv_sec);
51 }
52 else
53 {
54 strftime(backup_path, PATH_MAX, "/sdcard/clockworkmod/backup/%F.%H.%M.%S", tmp);
55 }
56}
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -080057
Koushik Dutta47b4d432012-07-20 16:15:43 -070058static void ensure_directory(const char* dir) {
59 char tmp[PATH_MAX];
60 sprintf(tmp, "mkdir -p %s ; chmod 777 %s", dir, dir);
61 __system(tmp);
62}
63
Koushik Dutta50822992011-06-08 19:03:27 -070064static int print_and_error(const char* message) {
Koushik Duttadf1e4062010-12-18 17:42:31 -080065 ui_print("%s", message);
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -070066 return 1;
67}
68
Koushik Dutta214cc5d2012-07-15 15:30:43 -070069static int nandroid_backup_bitfield = 0;
70#define NANDROID_FIELD_DEDUPE_CLEARED_SPACE 1
Koushik Duttaba0b4c62012-07-15 15:40:16 -070071static int nandroid_files_total = 0;
72static int nandroid_files_count = 0;
73static void nandroid_callback(const char* filename)
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -080074{
Koushik Dutta50822992011-06-08 19:03:27 -070075 if (filename == NULL)
76 return;
77 const char* justfile = basename(filename);
78 char tmp[PATH_MAX];
Koushik Dutta02b4b222012-07-02 17:14:52 -070079 strcpy(tmp, justfile);
80 if (tmp[strlen(tmp) - 1] == '\n')
81 tmp[strlen(tmp) - 1] = NULL;
Koushik Duttaf8fc12e2012-07-04 20:08:05 -070082 tmp[ui_get_text_cols() - 1] = '\0';
Koushik Duttaba0b4c62012-07-15 15:40:16 -070083 nandroid_files_count++;
Koushik Dutta4b52b082012-07-15 19:41:24 -070084 ui_increment_frame();
Koushik Duttaf8fc12e2012-07-04 20:08:05 -070085 ui_nice_print("%s\n", tmp);
Koushik Duttaba0b4c62012-07-15 15:40:16 -070086 if (!ui_was_niced() && nandroid_files_total != 0)
87 ui_set_progress((float)nandroid_files_count / (float)nandroid_files_total);
Koushik Duttaf8fc12e2012-07-04 20:08:05 -070088 if (!ui_was_niced())
89 ui_delete_line();
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -080090}
91
Koushik Dutta50822992011-06-08 19:03:27 -070092static void compute_directory_stats(const char* directory)
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -080093{
94 char tmp[PATH_MAX];
Koushik Dutta4b52b082012-07-15 19:41:24 -070095 sprintf(tmp, "find %s | %s wc -l > /tmp/dircount", directory, strcmp(directory, "/data") == 0 && is_data_media() ? "grep -v /data/media |" : "");
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -080096 __system(tmp);
97 char count_text[100];
98 FILE* f = fopen("/tmp/dircount", "r");
99 fread(count_text, 1, sizeof(count_text), f);
100 fclose(f);
Koushik Duttaba0b4c62012-07-15 15:40:16 -0700101 nandroid_files_count = 0;
102 nandroid_files_total = atoi(count_text);
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800103 ui_reset_progress();
104 ui_show_progress(1, 0);
105}
106
Koushik Dutta50822992011-06-08 19:03:27 -0700107typedef void (*file_event_callback)(const char* filename);
Koushik Dutta55e5e7b2011-06-14 23:39:59 -0700108typedef int (*nandroid_backup_handler)(const char* backup_path, const char* backup_file_image, int callback);
Koushik Dutta50822992011-06-08 19:03:27 -0700109
Koushik Dutta55e5e7b2011-06-14 23:39:59 -0700110static int mkyaffs2image_wrapper(const char* backup_path, const char* backup_file_image, int callback) {
Koushik Duttaa96fdfd2012-07-15 15:50:13 -0700111 char tmp[PATH_MAX];
Pawit Pornkitprasan03ea8a42012-07-18 21:47:54 +0700112 sprintf(tmp, "cd %s ; mkyaffs2image . %s.img ; exit $?", backup_path, backup_file_image);
Koushik Duttaa96fdfd2012-07-15 15:50:13 -0700113
114 FILE *fp = __popen(tmp, "r");
115 if (fp == NULL) {
116 ui_print("Unable to execute mkyaffs2image.\n");
117 return -1;
118 }
119
120 while (fgets(tmp, PATH_MAX, fp) != NULL) {
121 tmp[PATH_MAX - 1] = NULL;
122 if (callback)
123 nandroid_callback(tmp);
124 }
125
126 return __pclose(fp);
Koushik Dutta50822992011-06-08 19:03:27 -0700127}
128
Koushik Dutta55e5e7b2011-06-14 23:39:59 -0700129static int tar_compress_wrapper(const char* backup_path, const char* backup_file_image, int callback) {
Koushik Dutta50822992011-06-08 19:03:27 -0700130 char tmp[PATH_MAX];
Koushik Duttaf8fc12e2012-07-04 20:08:05 -0700131 sprintf(tmp, "cd $(dirname %s) ; touch %s.tar ; (tar cv %s $(basename %s) | split -a 1 -b 1000000000 /proc/self/fd/0 %s.tar.) 2> /proc/self/fd/1 ; exit $?", backup_path, backup_file_image, strcmp(backup_path, "/data") == 0 && is_data_media() ? "--exclude 'media'" : "", backup_path, backup_file_image);
Koushik Dutta50822992011-06-08 19:03:27 -0700132
133 FILE *fp = __popen(tmp, "r");
134 if (fp == NULL) {
Koushik Duttaa96fdfd2012-07-15 15:50:13 -0700135 ui_print("Unable to execute tar.\n");
Koushik Dutta214cc5d2012-07-15 15:30:43 -0700136 return -1;
137 }
138
139 while (fgets(tmp, PATH_MAX, fp) != NULL) {
140 tmp[PATH_MAX - 1] = NULL;
141 if (callback)
Koushik Duttaba0b4c62012-07-15 15:40:16 -0700142 nandroid_callback(tmp);
Koushik Dutta214cc5d2012-07-15 15:30:43 -0700143 }
144
145 return __pclose(fp);
146}
147
Koushik Duttacca3a4d2012-08-08 13:05:14 -0700148void nandroid_dedupe_gc(const char* blob_dir) {
Koushik Duttac2adbf92012-07-19 23:17:19 -0700149 char backup_dir[PATH_MAX];
150 strcpy(backup_dir, blob_dir);
151 char *d = dirname(backup_dir);
152 strcpy(backup_dir, d);
153 strcat(backup_dir, "/backup");
154 ui_print("Freeing space...\n");
155 char tmp[PATH_MAX];
156 sprintf(tmp, "dedupe gc %s $(find %s -name '*.dup')", blob_dir, backup_dir);
157 __system(tmp);
158 ui_print("Done freeing space.\n");
159}
160
Koushik Dutta214cc5d2012-07-15 15:30:43 -0700161static int dedupe_compress_wrapper(const char* backup_path, const char* backup_file_image, int callback) {
162 char tmp[PATH_MAX];
163 char blob_dir[PATH_MAX];
164 strcpy(blob_dir, backup_file_image);
165 char *d = dirname(blob_dir);
166 strcpy(blob_dir, d);
167 d = dirname(blob_dir);
168 strcpy(blob_dir, d);
169 d = dirname(blob_dir);
170 strcpy(blob_dir, d);
171 strcat(blob_dir, "/blobs");
Koushik Dutta47b4d432012-07-20 16:15:43 -0700172 ensure_directory(blob_dir);
Koushik Dutta214cc5d2012-07-15 15:30:43 -0700173
174 if (!(nandroid_backup_bitfield & NANDROID_FIELD_DEDUPE_CLEARED_SPACE)) {
175 nandroid_backup_bitfield |= NANDROID_FIELD_DEDUPE_CLEARED_SPACE;
Koushik Duttacca3a4d2012-08-08 13:05:14 -0700176 nandroid_dedupe_gc(blob_dir);
Koushik Dutta214cc5d2012-07-15 15:30:43 -0700177 }
178
179 sprintf(tmp, "dedupe c %s %s %s.dup %s", backup_path, blob_dir, backup_file_image, strcmp(backup_path, "/data") == 0 && is_data_media() ? "./media" : "");
180
181 FILE *fp = __popen(tmp, "r");
182 if (fp == NULL) {
183 ui_print("Unable to execute dedupe.\n");
Koushik Dutta50822992011-06-08 19:03:27 -0700184 return -1;
185 }
186
187 while (fgets(tmp, PATH_MAX, fp) != NULL) {
188 tmp[PATH_MAX - 1] = NULL;
Koushik Dutta55e5e7b2011-06-14 23:39:59 -0700189 if (callback)
Koushik Duttaba0b4c62012-07-15 15:40:16 -0700190 nandroid_callback(tmp);
Koushik Dutta50822992011-06-08 19:03:27 -0700191 }
192
193 return __pclose(fp);
194}
195
Koushik Dutta50f5fb02012-12-04 23:21:02 -0800196static nandroid_backup_handler default_backup_handler = tar_compress_wrapper;
Koushik Duttacca3a4d2012-08-08 13:05:14 -0700197static char forced_backup_format[5] = "";
198void nandroid_force_backup_format(const char* fmt) {
199 strcpy(forced_backup_format, fmt);
200}
Koushik Duttaa4385de2012-08-04 15:53:18 -0700201static void refresh_default_backup_handler() {
202 char fmt[5];
Koushik Duttacca3a4d2012-08-08 13:05:14 -0700203 if (strlen(forced_backup_format) > 0) {
204 strcpy(fmt, forced_backup_format);
205 }
206 else {
207 ensure_path_mounted("/sdcard");
Koushik Dutta014d0202012-08-08 17:56:19 -0700208 FILE* f = fopen(NANDROID_BACKUP_FORMAT_FILE, "r");
Koushik Duttaa3870b12012-12-28 10:52:08 -0800209 if (NULL == f) {
210 default_backup_handler = tar_compress_wrapper;
Koushik Dutta190a48b2012-12-28 10:52:29 -0800211 return;
Koushik Duttaa3870b12012-12-28 10:52:08 -0800212 }
Koushik Duttacca3a4d2012-08-08 13:05:14 -0700213 fread(fmt, 1, sizeof(fmt), f);
214 fclose(f);
215 }
Koushik Duttaa4385de2012-08-04 15:53:18 -0700216 fmt[3] = NULL;
Koushik Dutta7322ac12012-11-28 12:07:49 -0800217 if (0 == strcmp(fmt, "dup"))
Koushik Duttaa4385de2012-08-04 15:53:18 -0700218 default_backup_handler = dedupe_compress_wrapper;
Koushik Dutta7322ac12012-11-28 12:07:49 -0800219 else
220 default_backup_handler = tar_compress_wrapper;
Koushik Duttaa4385de2012-08-04 15:53:18 -0700221}
222
James Sullinsae4a2b22012-12-06 00:37:49 -0600223unsigned nandroid_get_default_backup_format() {
224 refresh_default_backup_handler();
225 if (default_backup_handler == dedupe_compress_wrapper) {
226 return NANDROID_BACKUP_FORMAT_DUP;
227 } else {
228 return NANDROID_BACKUP_FORMAT_TAR;
229 }
230}
231
Koushik Dutta50822992011-06-08 19:03:27 -0700232static nandroid_backup_handler get_backup_handler(const char *backup_path) {
233 Volume *v = volume_for_path(backup_path);
234 if (v == NULL) {
235 ui_print("Unable to find volume.\n");
236 return NULL;
237 }
Koushik Dutta50822992011-06-08 19:03:27 -0700238 MountedVolume *mv = find_mounted_volume_by_mount_point(v->mount_point);
239 if (mv == NULL) {
240 ui_print("Unable to find mounted volume: %s\n", v->mount_point);
241 return NULL;
242 }
243
Koushik Dutta38a92142011-06-10 09:45:52 -0700244 if (strcmp(backup_path, "/data") == 0 && is_data_media()) {
Koushik Duttaa4385de2012-08-04 15:53:18 -0700245 return default_backup_handler;
Koushik Dutta38a92142011-06-10 09:45:52 -0700246 }
247
Koushik Duttacca3a4d2012-08-08 13:05:14 -0700248 if (strlen(forced_backup_format) > 0)
249 return default_backup_handler;
250
Koushik Dutta214cc5d2012-07-15 15:30:43 -0700251 // cwr5, we prefer dedupe for everything except yaffs2
Koushik Dutta30a937a2011-09-05 21:14:06 -0700252 if (strcmp("yaffs2", mv->filesystem) == 0) {
Koushik Dutta38a92142011-06-10 09:45:52 -0700253 return mkyaffs2image_wrapper;
254 }
255
Koushik Duttaa4385de2012-08-04 15:53:18 -0700256 return default_backup_handler;
Koushik Dutta50822992011-06-08 19:03:27 -0700257}
258
259
Koushik Duttadf1e4062010-12-18 17:42:31 -0800260int nandroid_backup_partition_extended(const char* backup_path, const char* mount_point, int umount_when_finished) {
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700261 int ret = 0;
Koushik Dutta4acc3092012-11-18 20:40:42 -0800262 char name[PATH_MAX];
263 strcpy(name, basename(mount_point));
Koushik Dutta58131e72010-06-09 12:41:20 -0700264
265 struct stat file_info;
Koushik Dutta30a937a2011-09-05 21:14:06 -0700266 int callback = stat("/sdcard/clockworkmod/.hidenandroidprogress", &file_info) != 0;
Koushik Dutta214cc5d2012-07-15 15:30:43 -0700267
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700268 ui_print("Backing up %s...\n", name);
Koushik Duttadf1e4062010-12-18 17:42:31 -0800269 if (0 != (ret = ensure_path_mounted(mount_point) != 0)) {
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700270 ui_print("Can't mount %s!\n", mount_point);
271 return ret;
272 }
273 compute_directory_stats(mount_point);
274 char tmp[PATH_MAX];
Koushik Dutta30a937a2011-09-05 21:14:06 -0700275 scan_mounted_volumes();
276 Volume *v = volume_for_path(mount_point);
277 MountedVolume *mv = NULL;
278 if (v != NULL)
279 mv = find_mounted_volume_by_mount_point(v->mount_point);
280 if (mv == NULL || mv->filesystem == NULL)
281 sprintf(tmp, "%s/%s.auto", backup_path, name);
282 else
283 sprintf(tmp, "%s/%s.%s", backup_path, name, mv->filesystem);
Koushik Dutta50822992011-06-08 19:03:27 -0700284 nandroid_backup_handler backup_handler = get_backup_handler(mount_point);
285 if (backup_handler == NULL) {
286 ui_print("Error finding an appropriate backup handler.\n");
287 return -2;
288 }
289 ret = backup_handler(mount_point, tmp, callback);
Koushik Dutta062d6b02010-07-03 13:54:21 -0700290 if (umount_when_finished) {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800291 ensure_path_unmounted(mount_point);
Koushik Dutta062d6b02010-07-03 13:54:21 -0700292 }
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700293 if (0 != ret) {
Koushik Dutta50822992011-06-08 19:03:27 -0700294 ui_print("Error while making a backup image of %s!\n", mount_point);
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700295 return ret;
296 }
Koushik Dutta77013cc2012-11-17 12:13:35 -0800297 ui_print("Backup of %s completed.\n", name);
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700298 return 0;
299}
300
Koushik Dutta53c9bd82011-01-04 11:38:31 -0800301int nandroid_backup_partition(const char* backup_path, const char* root) {
302 Volume *vol = volume_for_path(root);
303 // make sure the volume exists before attempting anything...
304 if (vol == NULL || vol->fs_type == NULL)
Koushik Dutta77013cc2012-11-17 12:13:35 -0800305 return 0;
Koushik Dutta53c9bd82011-01-04 11:38:31 -0800306
307 // see if we need a raw backup (mtd)
308 char tmp[PATH_MAX];
309 int ret;
Christopher Lais066a1022011-01-16 06:02:34 -0600310 if (strcmp(vol->fs_type, "mtd") == 0 ||
Koushik Dutta33e37f32011-02-27 16:33:32 -0800311 strcmp(vol->fs_type, "bml") == 0 ||
Christopher Lais066a1022011-01-16 06:02:34 -0600312 strcmp(vol->fs_type, "emmc") == 0) {
Koushik Dutta53c9bd82011-01-04 11:38:31 -0800313 const char* name = basename(root);
314 sprintf(tmp, "%s/%s.img", backup_path, name);
315 ui_print("Backing up %s image...\n", name);
Koushik Dutta8a6bc772011-05-26 11:14:15 -0700316 if (0 != (ret = backup_raw_partition(vol->fs_type, vol->device, tmp))) {
Koushik Dutta53c9bd82011-01-04 11:38:31 -0800317 ui_print("Error while backing up %s image!", name);
318 return ret;
319 }
Koushik Dutta77013cc2012-11-17 12:13:35 -0800320 ui_print("Backup of %s image completed.\n", name);
Koushik Dutta53c9bd82011-01-04 11:38:31 -0800321 return 0;
322 }
323
Koushik Dutta062d6b02010-07-03 13:54:21 -0700324 return nandroid_backup_partition_extended(backup_path, root, 1);
325}
326
Koushik Dutta2f73e582010-04-18 16:00:21 -0700327int nandroid_backup(const char* backup_path)
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800328{
Koushik Dutta214cc5d2012-07-15 15:30:43 -0700329 nandroid_backup_bitfield = 0;
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800330 ui_set_background(BACKGROUND_ICON_INSTALLING);
Koushik Duttaa4385de2012-08-04 15:53:18 -0700331 refresh_default_backup_handler();
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800332
Koushik Dutta89ed0b72011-10-26 21:25:34 -0700333 if (ensure_path_mounted(backup_path) != 0) {
334 return print_and_error("Can't mount backup path.\n");
335 }
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800336
Koushik Dutta89ed0b72011-10-26 21:25:34 -0700337 Volume* volume = volume_for_path(backup_path);
Koushik Dutta407183a2012-06-17 20:22:22 -0700338 if (NULL == volume)
339 return print_and_error("Unable to find volume for backup path.\n");
340 if (is_data_media_volume_path(volume->mount_point))
341 volume = volume_for_path("/data");
Koushik K. Dutta78686292010-03-25 17:58:45 -0700342 int ret;
Koushik K. Duttaf3534d02010-04-18 18:06:24 -0700343 struct statfs s;
Koushik Duttaddc12412011-11-23 14:06:12 -0800344 if (NULL != volume) {
345 if (0 != (ret = statfs(volume->mount_point, &s)))
346 return print_and_error("Unable to stat backup path.\n");
347 uint64_t bavail = s.f_bavail;
348 uint64_t bsize = s.f_bsize;
349 uint64_t sdcard_free = bavail * bsize;
350 uint64_t sdcard_free_mb = sdcard_free / (uint64_t)(1024 * 1024);
351 ui_print("SD Card space free: %lluMB\n", sdcard_free_mb);
352 if (sdcard_free_mb < 150)
353 ui_print("There may not be enough free space to complete backup... continuing...\n");
354 }
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800355 char tmp[PATH_MAX];
Koushik Dutta47b4d432012-07-20 16:15:43 -0700356 ensure_directory(backup_path);
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800357
Koushik Dutta53c9bd82011-01-04 11:38:31 -0800358 if (0 != (ret = nandroid_backup_partition(backup_path, "/boot")))
359 return ret;
Koushik K. Dutta6923cc32010-03-29 14:46:00 -0700360
Koushik Dutta53c9bd82011-01-04 11:38:31 -0800361 if (0 != (ret = nandroid_backup_partition(backup_path, "/recovery")))
362 return ret;
Koushik K. Dutta6923cc32010-03-29 14:46:00 -0700363
Koushik Dutta5ee98b22011-03-02 19:35:05 -0800364 Volume *vol = volume_for_path("/wimax");
365 if (vol != NULL && 0 == stat(vol->device, &s))
agrabren1f76a5d2010-12-29 12:15:18 -0600366 {
Joshua Wise05d4a092010-12-31 02:05:35 -0500367 char serialno[PROPERTY_VALUE_MAX];
Joshua Wiseca889ec2010-12-30 02:48:25 -0500368 ui_print("Backing up WiMAX...\n");
Joshua Wise05d4a092010-12-31 02:05:35 -0500369 serialno[0] = 0;
370 property_get("ro.serialno", serialno, "");
371 sprintf(tmp, "%s/wimax.%s.img", backup_path, serialno);
Koushik Duttae62132b2011-05-26 11:29:29 -0700372 ret = backup_raw_partition(vol->fs_type, vol->device, tmp);
agrabren1f76a5d2010-12-29 12:15:18 -0600373 if (0 != ret)
Joshua Wiseca889ec2010-12-30 02:48:25 -0500374 return print_and_error("Error while dumping WiMAX image!\n");
agrabren1f76a5d2010-12-29 12:15:18 -0600375 }
376
Koushik Duttadf1e4062010-12-18 17:42:31 -0800377 if (0 != (ret = nandroid_backup_partition(backup_path, "/system")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700378 return ret;
379
Koushik Duttadf1e4062010-12-18 17:42:31 -0800380 if (0 != (ret = nandroid_backup_partition(backup_path, "/data")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700381 return ret;
382
Koushik Dutta5460f0c2010-12-18 22:37:49 -0800383 if (has_datadata()) {
384 if (0 != (ret = nandroid_backup_partition(backup_path, "/datadata")))
385 return ret;
386 }
Koushik Dutta8b5e1852010-06-14 22:04:22 -0700387
Koushik Dutta5ca6fa92012-12-28 11:03:26 -0800388 if (is_data_media() || 0 != stat("/sdcard/.android_secure", &s)) {
Koushik Dutta062d6b02010-07-03 13:54:21 -0700389 ui_print("No /sdcard/.android_secure found. Skipping backup of applications on external storage.\n");
390 }
Koushik Dutta5ca6fa92012-12-28 11:03:26 -0800391 else {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800392 if (0 != (ret = nandroid_backup_partition_extended(backup_path, "/sdcard/.android_secure", 0)))
Koushik Dutta062d6b02010-07-03 13:54:21 -0700393 return ret;
394 }
395
Koushik Duttadf1e4062010-12-18 17:42:31 -0800396 if (0 != (ret = nandroid_backup_partition_extended(backup_path, "/cache", 0)))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700397 return ret;
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700398
Koushik Dutta5ee98b22011-03-02 19:35:05 -0800399 vol = volume_for_path("/sd-ext");
400 if (vol == NULL || 0 != stat(vol->device, &s))
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700401 {
Koushik K. Dutta71ef11f2010-04-02 22:17:42 -0700402 ui_print("No sd-ext found. Skipping backup of sd-ext.\n");
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700403 }
404 else
405 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800406 if (0 != ensure_path_mounted("/sd-ext"))
Koushik Dutta8b5e1852010-06-14 22:04:22 -0700407 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 -0800408 else if (0 != (ret = nandroid_backup_partition(backup_path, "/sd-ext")))
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700409 return ret;
410 }
Koushik Dutta062d6b02010-07-03 13:54:21 -0700411
Koushik K. Duttad7178922010-03-24 14:38:40 -0700412 ui_print("Generating md5 sum...\n");
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700413 sprintf(tmp, "nandroid-md5.sh %s", backup_path);
Koushik K. Duttab0a25032010-03-24 10:34:38 -0700414 if (0 != (ret = __system(tmp))) {
415 ui_print("Error while generating md5 sum!\n");
416 return ret;
417 }
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800418
Koushik Dutta382525c2012-07-17 09:50:54 -0700419 sprintf(tmp, "chmod -R 777 %s ; chmod -R u+r,u+w,g+r,g+w,o+r,o+w /sdcard/clockworkmod ; chmod u+x,g+x,o+x /sdcard/clockworkmod/backup ; chmod u+x,g+x,o+x /sdcard/clockworkmod/blobs", backup_path);
Koushik Duttad11af822012-07-17 00:49:01 -0700420 __system(tmp);
Koushik K. Dutta3f38f322010-03-12 23:45:25 -0800421 sync();
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800422 ui_set_background(BACKGROUND_ICON_NONE);
423 ui_reset_progress();
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700424 ui_print("\nBackup complete!\n");
425 return 0;
426}
427
Koushik Dutta7905c802011-06-15 00:00:55 -0700428typedef int (*nandroid_restore_handler)(const char* backup_file_image, const char* backup_path, int callback);
Koushik Dutta50822992011-06-08 19:03:27 -0700429
Koushik Dutta7905c802011-06-15 00:00:55 -0700430static int unyaffs_wrapper(const char* backup_file_image, const char* backup_path, int callback) {
Koushik Duttaa96fdfd2012-07-15 15:50:13 -0700431 char tmp[PATH_MAX];
Pawit Pornkitprasan03ea8a42012-07-18 21:47:54 +0700432 sprintf(tmp, "cd %s ; unyaffs %s ; exit $?", backup_path, backup_file_image);
Koushik Duttaa96fdfd2012-07-15 15:50:13 -0700433 FILE *fp = __popen(tmp, "r");
434 if (fp == NULL) {
435 ui_print("Unable to execute unyaffs.\n");
436 return -1;
437 }
438
439 while (fgets(tmp, PATH_MAX, fp) != NULL) {
440 tmp[PATH_MAX - 1] = NULL;
441 if (callback)
442 nandroid_callback(tmp);
443 }
444
445 return __pclose(fp);
Koushik Dutta50822992011-06-08 19:03:27 -0700446}
447
Koushik Dutta7905c802011-06-15 00:00:55 -0700448static int tar_extract_wrapper(const char* backup_file_image, const char* backup_path, int callback) {
Koushik Dutta50822992011-06-08 19:03:27 -0700449 char tmp[PATH_MAX];
Koushik Duttad9425b12012-07-04 18:51:17 -0700450 sprintf(tmp, "cd $(dirname %s) ; cat %s* | tar xv ; exit $?", backup_path, backup_file_image);
Koushik Dutta214cc5d2012-07-15 15:30:43 -0700451 FILE *fp = __popen(tmp, "r");
452 if (fp == NULL) {
Koushik Duttaa96fdfd2012-07-15 15:50:13 -0700453 ui_print("Unable to execute tar.\n");
Koushik Dutta214cc5d2012-07-15 15:30:43 -0700454 return -1;
455 }
456
457 while (fgets(tmp, PATH_MAX, fp) != NULL) {
458 tmp[PATH_MAX - 1] = NULL;
459 if (callback)
Koushik Duttaba0b4c62012-07-15 15:40:16 -0700460 nandroid_callback(tmp);
Koushik Dutta214cc5d2012-07-15 15:30:43 -0700461 }
462
463 return __pclose(fp);
464}
465
466static int dedupe_extract_wrapper(const char* backup_file_image, const char* backup_path, int callback) {
467 char tmp[PATH_MAX];
468 char blob_dir[PATH_MAX];
469 strcpy(blob_dir, backup_file_image);
470 char *bd = dirname(blob_dir);
471 strcpy(blob_dir, bd);
472 bd = dirname(blob_dir);
473 strcpy(blob_dir, bd);
474 bd = dirname(blob_dir);
475 sprintf(tmp, "dedupe x %s %s/blobs %s; exit $?", backup_file_image, bd, backup_path);
Koushik Dutta50822992011-06-08 19:03:27 -0700476
477 char path[PATH_MAX];
478 FILE *fp = __popen(tmp, "r");
479 if (fp == NULL) {
Koushik Dutta214cc5d2012-07-15 15:30:43 -0700480 ui_print("Unable to execute dedupe.\n");
Koushik Dutta50822992011-06-08 19:03:27 -0700481 return -1;
482 }
483
484 while (fgets(path, PATH_MAX, fp) != NULL) {
Koushik Dutta7905c802011-06-15 00:00:55 -0700485 if (callback)
Koushik Duttaba0b4c62012-07-15 15:40:16 -0700486 nandroid_callback(path);
Koushik Dutta50822992011-06-08 19:03:27 -0700487 }
488
489 return __pclose(fp);
490}
491
492static nandroid_restore_handler get_restore_handler(const char *backup_path) {
493 Volume *v = volume_for_path(backup_path);
494 if (v == NULL) {
495 ui_print("Unable to find volume.\n");
496 return NULL;
497 }
498 scan_mounted_volumes();
499 MountedVolume *mv = find_mounted_volume_by_mount_point(v->mount_point);
500 if (mv == NULL) {
501 ui_print("Unable to find mounted volume: %s\n", v->mount_point);
502 return NULL;
503 }
504
Koushik Dutta38a92142011-06-10 09:45:52 -0700505 if (strcmp(backup_path, "/data") == 0 && is_data_media()) {
506 return tar_extract_wrapper;
507 }
508
Koushik Dutta30a937a2011-09-05 21:14:06 -0700509 // cwr 5, we prefer tar for everything unless it is yaffs2
Koushik Dutta38a92142011-06-10 09:45:52 -0700510 char str[255];
511 char* partition;
512 property_get("ro.cwm.prefer_tar", str, "false");
513 if (strcmp("true", str) != 0) {
514 return unyaffs_wrapper;
515 }
516
Koushik Dutta50822992011-06-08 19:03:27 -0700517 if (strcmp("yaffs2", mv->filesystem) == 0) {
518 return unyaffs_wrapper;
519 }
520
521 return tar_extract_wrapper;
522}
523
Koushik Duttadf1e4062010-12-18 17:42:31 -0800524int nandroid_restore_partition_extended(const char* backup_path, const char* mount_point, int umount_when_finished) {
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700525 int ret = 0;
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700526 char* name = basename(mount_point);
Koushik Dutta30a937a2011-09-05 21:14:06 -0700527
528 nandroid_restore_handler restore_handler = NULL;
Donovan Bartisha87bb5f2011-09-29 15:37:19 -0500529 const char *filesystems[] = { "yaffs2", "ext2", "ext3", "ext4", "vfat", "rfs", NULL };
Koushik Dutta30a937a2011-09-05 21:14:06 -0700530 const char* backup_filesystem = NULL;
531 Volume *vol = volume_for_path(mount_point);
532 const char *device = NULL;
533 if (vol != NULL)
534 device = vol->device;
535
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700536 char tmp[PATH_MAX];
537 sprintf(tmp, "%s/%s.img", backup_path, name);
Koushik Dutta58131e72010-06-09 12:41:20 -0700538 struct stat file_info;
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700539 if (0 != (ret = statfs(tmp, &file_info))) {
Koushik Dutta30a937a2011-09-05 21:14:06 -0700540 // can't find the backup, it may be the new backup format?
541 // iterate through the backup types
542 printf("couldn't find default\n");
543 char *filesystem;
544 int i = 0;
545 while ((filesystem = filesystems[i]) != NULL) {
546 sprintf(tmp, "%s/%s.%s.img", backup_path, name, filesystem);
547 if (0 == (ret = statfs(tmp, &file_info))) {
548 backup_filesystem = filesystem;
549 restore_handler = unyaffs_wrapper;
550 break;
551 }
552 sprintf(tmp, "%s/%s.%s.tar", backup_path, name, filesystem);
553 if (0 == (ret = statfs(tmp, &file_info))) {
554 backup_filesystem = filesystem;
555 restore_handler = tar_extract_wrapper;
556 break;
557 }
Koushik Dutta214cc5d2012-07-15 15:30:43 -0700558 sprintf(tmp, "%s/%s.%s.dup", backup_path, name, filesystem);
559 if (0 == (ret = statfs(tmp, &file_info))) {
560 backup_filesystem = filesystem;
561 restore_handler = dedupe_extract_wrapper;
562 break;
563 }
Koushik Dutta30a937a2011-09-05 21:14:06 -0700564 i++;
565 }
566
567 if (backup_filesystem == NULL || restore_handler == NULL) {
568 ui_print("%s.img not found. Skipping restore of %s.\n", name, mount_point);
569 return 0;
570 }
571 else {
572 printf("Found new backup image: %s\n", tmp);
573 }
574
Prashant Somashekarf0c389d2011-11-26 13:55:00 -0500575 // If the fs_type of this volume is "auto" or mount_point is /data
Koushik Dutta407183a2012-06-17 20:22:22 -0700576 // and is_data_media, let's revert
Prashant Somashekarf0c389d2011-11-26 13:55:00 -0500577 // to using a rm -rf, rather than trying to do a
578 // ext3/ext4/whatever format.
Koushik Dutta30a937a2011-09-05 21:14:06 -0700579 // This is because some phones (like DroidX) will freak out if you
580 // reformat the /system or /data partitions, and not boot due to
581 // a locked bootloader.
Koushik Duttaddc12412011-11-23 14:06:12 -0800582 // Other devices, like the Galaxy Nexus, XOOM, and Galaxy Tab 10.1
Koushik Dutta407183a2012-06-17 20:22:22 -0700583 // have a /sdcard symlinked to /data/media.
Koushik Dutta30a937a2011-09-05 21:14:06 -0700584 // Or of volume does not exist (.android_secure), just rm -rf.
585 if (vol == NULL || 0 == strcmp(vol->fs_type, "auto"))
586 backup_filesystem = NULL;
Koushik Dutta407183a2012-06-17 20:22:22 -0700587 if (0 == strcmp(vol->mount_point, "/data") && is_data_media())
588 backup_filesystem = NULL;
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700589 }
590
591 ensure_directory(mount_point);
592
Koushik Dutta30a937a2011-09-05 21:14:06 -0700593 int callback = stat("/sdcard/clockworkmod/.hidenandroidprogress", &file_info) != 0;
Koushik Dutta58131e72010-06-09 12:41:20 -0700594
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700595 ui_print("Restoring %s...\n", name);
Koushik Dutta30a937a2011-09-05 21:14:06 -0700596 if (backup_filesystem == NULL) {
597 if (0 != (ret = format_volume(mount_point))) {
598 ui_print("Error while formatting %s!\n", mount_point);
599 return ret;
600 }
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700601 }
Koushik Dutta30a937a2011-09-05 21:14:06 -0700602 else if (0 != (ret = format_device(device, mount_point, backup_filesystem))) {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800603 ui_print("Error while formatting %s!\n", mount_point);
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700604 return ret;
605 }
Koushik Dutta30a937a2011-09-05 21:14:06 -0700606
Koushik Duttadf1e4062010-12-18 17:42:31 -0800607 if (0 != (ret = ensure_path_mounted(mount_point))) {
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700608 ui_print("Can't mount %s!\n", mount_point);
609 return ret;
610 }
Koushik Dutta30a937a2011-09-05 21:14:06 -0700611
612 if (restore_handler == NULL)
613 restore_handler = get_restore_handler(mount_point);
Koushik Dutta50822992011-06-08 19:03:27 -0700614 if (restore_handler == NULL) {
615 ui_print("Error finding an appropriate restore handler.\n");
616 return -2;
617 }
618 if (0 != (ret = restore_handler(tmp, mount_point, callback))) {
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700619 ui_print("Error while restoring %s!\n", mount_point);
620 return ret;
621 }
Koushik Dutta2f73e582010-04-18 16:00:21 -0700622
Koushik Dutta062d6b02010-07-03 13:54:21 -0700623 if (umount_when_finished) {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800624 ensure_path_unmounted(mount_point);
Koushik Dutta2f73e582010-04-18 16:00:21 -0700625 }
Koushik Dutta062d6b02010-07-03 13:54:21 -0700626
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800627 return 0;
628}
629
Koushik Dutta062d6b02010-07-03 13:54:21 -0700630int nandroid_restore_partition(const char* backup_path, const char* root) {
Koushik Dutta53c9bd82011-01-04 11:38:31 -0800631 Volume *vol = volume_for_path(root);
632 // make sure the volume exists...
633 if (vol == NULL || vol->fs_type == NULL)
634 return 0;
635
636 // see if we need a raw restore (mtd)
637 char tmp[PATH_MAX];
Christopher Lais066a1022011-01-16 06:02:34 -0600638 if (strcmp(vol->fs_type, "mtd") == 0 ||
Koushik Dutta33e37f32011-02-27 16:33:32 -0800639 strcmp(vol->fs_type, "bml") == 0 ||
Christopher Lais066a1022011-01-16 06:02:34 -0600640 strcmp(vol->fs_type, "emmc") == 0) {
Koushik Dutta53c9bd82011-01-04 11:38:31 -0800641 int ret;
642 const char* name = basename(root);
643 ui_print("Erasing %s before restore...\n", name);
644 if (0 != (ret = format_volume(root))) {
645 ui_print("Error while erasing %s image!", name);
646 return ret;
647 }
648 sprintf(tmp, "%s%s.img", backup_path, root);
649 ui_print("Restoring %s image...\n", name);
Koushik Dutta8a6bc772011-05-26 11:14:15 -0700650 if (0 != (ret = restore_raw_partition(vol->fs_type, vol->device, tmp))) {
Koushik Dutta53c9bd82011-01-04 11:38:31 -0800651 ui_print("Error while flashing %s image!", name);
652 return ret;
653 }
654 return 0;
655 }
Koushik Dutta062d6b02010-07-03 13:54:21 -0700656 return nandroid_restore_partition_extended(backup_path, root, 1);
657}
658
agrabren1f76a5d2010-12-29 12:15:18 -0600659int 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 -0800660{
661 ui_set_background(BACKGROUND_ICON_INSTALLING);
662 ui_show_indeterminate_progress();
Koushik Duttaba0b4c62012-07-15 15:40:16 -0700663 nandroid_files_total = 0;
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800664
Koushik Dutta89ed0b72011-10-26 21:25:34 -0700665 if (ensure_path_mounted(backup_path) != 0)
666 return print_and_error("Can't mount backup path\n");
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800667
668 char tmp[PATH_MAX];
669
670 ui_print("Checking MD5 sums...\n");
Koushik K. Duttaface5882010-03-13 10:15:55 -0800671 sprintf(tmp, "cd %s && md5sum -c nandroid.md5", backup_path);
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800672 if (0 != __system(tmp))
673 return print_and_error("MD5 mismatch!\n");
674
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700675 int ret;
Koushik Dutta53c9bd82011-01-04 11:38:31 -0800676
677 if (restore_boot && NULL != volume_for_path("/boot") && 0 != (ret = nandroid_restore_partition(backup_path, "/boot")))
678 return ret;
Koushik K. Duttaf7215942010-03-16 13:34:51 -0700679
Koushik Dutta5ee98b22011-03-02 19:35:05 -0800680 struct stat s;
681 Volume *vol = volume_for_path("/wimax");
682 if (restore_wimax && vol != NULL && 0 == stat(vol->device, &s))
agrabren1f76a5d2010-12-29 12:15:18 -0600683 {
Joshua Wise05d4a092010-12-31 02:05:35 -0500684 char serialno[PROPERTY_VALUE_MAX];
685
686 serialno[0] = 0;
687 property_get("ro.serialno", serialno, "");
688 sprintf(tmp, "%s/wimax.%s.img", backup_path, serialno);
agrabren1f76a5d2010-12-29 12:15:18 -0600689
690 struct stat st;
691 if (0 != stat(tmp, &st))
692 {
Joshua Wiseca889ec2010-12-30 02:48:25 -0500693 ui_print("WARNING: WiMAX partition exists, but nandroid\n");
694 ui_print(" backup does not contain WiMAX image.\n");
agrabren1f76a5d2010-12-29 12:15:18 -0600695 ui_print(" You should create a new backup to\n");
Joshua Wiseca889ec2010-12-30 02:48:25 -0500696 ui_print(" protect your WiMAX keys.\n");
agrabren1f76a5d2010-12-29 12:15:18 -0600697 }
698 else
699 {
Joshua Wise5a3f1d82010-12-31 02:16:00 -0500700 ui_print("Erasing WiMAX before restore...\n");
Koushik Duttaa25deae2011-01-01 18:17:48 -0800701 if (0 != (ret = format_volume("/wimax")))
Joshua Wise5a3f1d82010-12-31 02:16:00 -0500702 return print_and_error("Error while formatting wimax!\n");
Joshua Wise05d4a092010-12-31 02:05:35 -0500703 ui_print("Restoring WiMAX image...\n");
Koushik Duttae62132b2011-05-26 11:29:29 -0700704 if (0 != (ret = restore_raw_partition(vol->fs_type, vol->device, tmp)))
agrabren1f76a5d2010-12-29 12:15:18 -0600705 return ret;
706 }
707 }
708
Koushik Duttadf1e4062010-12-18 17:42:31 -0800709 if (restore_system && 0 != (ret = nandroid_restore_partition(backup_path, "/system")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700710 return ret;
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800711
Koushik Duttadf1e4062010-12-18 17:42:31 -0800712 if (restore_data && 0 != (ret = nandroid_restore_partition(backup_path, "/data")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700713 return ret;
Koushik Dutta8b5e1852010-06-14 22:04:22 -0700714
Koushik Dutta5460f0c2010-12-18 22:37:49 -0800715 if (has_datadata()) {
716 if (restore_data && 0 != (ret = nandroid_restore_partition(backup_path, "/datadata")))
717 return ret;
718 }
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800719
Koushik Duttadf1e4062010-12-18 17:42:31 -0800720 if (restore_data && 0 != (ret = nandroid_restore_partition_extended(backup_path, "/sdcard/.android_secure", 0)))
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700721 return ret;
Koushik Dutta062d6b02010-07-03 13:54:21 -0700722
Koushik Duttadf1e4062010-12-18 17:42:31 -0800723 if (restore_cache && 0 != (ret = nandroid_restore_partition_extended(backup_path, "/cache", 0)))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700724 return ret;
725
Koushik Duttadf1e4062010-12-18 17:42:31 -0800726 if (restore_sdext && 0 != (ret = nandroid_restore_partition(backup_path, "/sd-ext")))
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700727 return ret;
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700728
Koushik K. Dutta3f38f322010-03-12 23:45:25 -0800729 sync();
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800730 ui_set_background(BACKGROUND_ICON_NONE);
731 ui_reset_progress();
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700732 ui_print("\nRestore complete!\n");
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800733 return 0;
734}
Koushik Dutta08370912010-06-26 12:25:02 -0700735
Koushik Dutta08370912010-06-26 12:25:02 -0700736int nandroid_usage()
737{
738 printf("Usage: nandroid backup\n");
739 printf("Usage: nandroid restore <directory>\n");
740 return 1;
741}
742
743int nandroid_main(int argc, char** argv)
744{
745 if (argc > 3 || argc < 2)
746 return nandroid_usage();
747
748 if (strcmp("backup", argv[1]) == 0)
749 {
750 if (argc != 2)
751 return nandroid_usage();
752
753 char backup_path[PATH_MAX];
754 nandroid_generate_timestamp_path(backup_path);
755 return nandroid_backup(backup_path);
756 }
757
758 if (strcmp("restore", argv[1]) == 0)
759 {
760 if (argc != 3)
761 return nandroid_usage();
Koushik Dutta5b7f34a2010-12-29 23:36:03 -0800762 return nandroid_restore(argv[2], 1, 1, 1, 1, 1, 0);
Koushik Dutta08370912010-06-26 12:25:02 -0700763 }
764
765 return nandroid_usage();
766}