blob: 933faa33be66f9b402bfac16f497116917a994ac [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"
40
Koushik Duttadf1e4062010-12-18 17:42:31 -080041int print_and_error(const char* message) {
42 ui_print("%s", message);
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -070043 return 1;
44}
45
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -080046int yaffs_files_total = 0;
47int yaffs_files_count = 0;
48void yaffs_callback(char* filename)
49{
50 char* justfile = basename(filename);
51 if (strlen(justfile) < 30)
Koushik Duttadf1e4062010-12-18 17:42:31 -080052 ui_print("%s", justfile);
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -080053 yaffs_files_count++;
54 if (yaffs_files_total != 0)
55 ui_set_progress((float)yaffs_files_count / (float)yaffs_files_total);
56 ui_reset_text_col();
57}
58
59void compute_directory_stats(char* directory)
60{
61 char tmp[PATH_MAX];
62 sprintf(tmp, "find %s | wc -l > /tmp/dircount", directory);
63 __system(tmp);
64 char count_text[100];
65 FILE* f = fopen("/tmp/dircount", "r");
66 fread(count_text, 1, sizeof(count_text), f);
67 fclose(f);
68 yaffs_files_count = 0;
69 yaffs_files_total = atoi(count_text);
70 ui_reset_progress();
71 ui_show_progress(1, 0);
72}
73
Koushik Duttadf1e4062010-12-18 17:42:31 -080074int nandroid_backup_partition_extended(const char* backup_path, const char* mount_point, int umount_when_finished) {
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -070075 int ret = 0;
Koushik K. Dutta68b01902010-04-01 12:20:39 -070076 char* name = basename(mount_point);
Koushik Dutta58131e72010-06-09 12:41:20 -070077
78 struct stat file_info;
79 mkyaffs2image_callback callback = NULL;
80 if (0 != stat("/sdcard/clockworkmod/.hidenandroidprogress", &file_info)) {
81 callback = yaffs_callback;
82 }
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -070083
84 ui_print("Backing up %s...\n", name);
Koushik Duttadf1e4062010-12-18 17:42:31 -080085 if (0 != (ret = ensure_path_mounted(mount_point) != 0)) {
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -070086 ui_print("Can't mount %s!\n", mount_point);
87 return ret;
88 }
89 compute_directory_stats(mount_point);
90 char tmp[PATH_MAX];
91 sprintf(tmp, "%s/%s.img", backup_path, name);
Koushik Dutta58131e72010-06-09 12:41:20 -070092 ret = mkyaffs2image(mount_point, tmp, 0, callback);
Koushik Dutta062d6b02010-07-03 13:54:21 -070093 if (umount_when_finished) {
Koushik Duttadf1e4062010-12-18 17:42:31 -080094 ensure_path_unmounted(mount_point);
Koushik Dutta062d6b02010-07-03 13:54:21 -070095 }
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -070096 if (0 != ret) {
97 ui_print("Error while making a yaffs2 image of %s!\n", mount_point);
98 return ret;
99 }
100 return 0;
101}
102
Koushik Dutta53c9bd82011-01-04 11:38:31 -0800103int nandroid_backup_partition(const char* backup_path, const char* root) {
104 Volume *vol = volume_for_path(root);
105 // make sure the volume exists before attempting anything...
106 if (vol == NULL || vol->fs_type == NULL)
107 return NULL;
108
109 // see if we need a raw backup (mtd)
110 char tmp[PATH_MAX];
111 int ret;
Christopher Lais066a1022011-01-16 06:02:34 -0600112 if (strcmp(vol->fs_type, "mtd") == 0 ||
Koushik Dutta33e37f32011-02-27 16:33:32 -0800113 strcmp(vol->fs_type, "bml") == 0 ||
Christopher Lais066a1022011-01-16 06:02:34 -0600114 strcmp(vol->fs_type, "emmc") == 0) {
Koushik Dutta53c9bd82011-01-04 11:38:31 -0800115 const char* name = basename(root);
116 sprintf(tmp, "%s/%s.img", backup_path, name);
117 ui_print("Backing up %s image...\n", name);
Koushik Duttafdd47662011-04-17 00:49:15 -0700118 if (0 != (ret = backup_raw_partition(vol->device, tmp))) {
Koushik Dutta53c9bd82011-01-04 11:38:31 -0800119 ui_print("Error while backing up %s image!", name);
120 return ret;
121 }
122 return 0;
123 }
124
Koushik Dutta062d6b02010-07-03 13:54:21 -0700125 return nandroid_backup_partition_extended(backup_path, root, 1);
126}
127
Koushik Dutta2f73e582010-04-18 16:00:21 -0700128int nandroid_backup(const char* backup_path)
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800129{
130 ui_set_background(BACKGROUND_ICON_INSTALLING);
131
Koushik Duttadf1e4062010-12-18 17:42:31 -0800132 if (ensure_path_mounted("/sdcard") != 0)
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800133 return print_and_error("Can't mount /sdcard\n");
134
Koushik K. Dutta78686292010-03-25 17:58:45 -0700135 int ret;
Koushik K. Duttaf3534d02010-04-18 18:06:24 -0700136 struct statfs s;
Koushik K. Dutta78686292010-03-25 17:58:45 -0700137 if (0 != (ret = statfs("/sdcard", &s)))
138 return print_and_error("Unable to stat /sdcard\n");
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700139 uint64_t bavail = s.f_bavail;
140 uint64_t bsize = s.f_bsize;
141 uint64_t sdcard_free = bavail * bsize;
142 uint64_t sdcard_free_mb = sdcard_free / (uint64_t)(1024 * 1024);
143 ui_print("SD Card space free: %lluMB\n", sdcard_free_mb);
144 if (sdcard_free_mb < 150)
Koushik K. Duttac0970932010-03-25 23:19:19 -0700145 ui_print("There may not be enough free space to complete backup... continuing...\n");
Koushik K. Dutta78686292010-03-25 17:58:45 -0700146
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800147 char tmp[PATH_MAX];
148 sprintf(tmp, "mkdir -p %s", backup_path);
149 __system(tmp);
150
Koushik Dutta53c9bd82011-01-04 11:38:31 -0800151 if (0 != (ret = nandroid_backup_partition(backup_path, "/boot")))
152 return ret;
Koushik K. Dutta6923cc32010-03-29 14:46:00 -0700153
Koushik Dutta53c9bd82011-01-04 11:38:31 -0800154 if (0 != (ret = nandroid_backup_partition(backup_path, "/recovery")))
155 return ret;
Koushik K. Dutta6923cc32010-03-29 14:46:00 -0700156
Koushik Dutta5ee98b22011-03-02 19:35:05 -0800157 Volume *vol = volume_for_path("/wimax");
158 if (vol != NULL && 0 == stat(vol->device, &s))
agrabren1f76a5d2010-12-29 12:15:18 -0600159 {
Joshua Wise05d4a092010-12-31 02:05:35 -0500160 char serialno[PROPERTY_VALUE_MAX];
Joshua Wiseca889ec2010-12-30 02:48:25 -0500161 ui_print("Backing up WiMAX...\n");
Joshua Wise05d4a092010-12-31 02:05:35 -0500162 serialno[0] = 0;
163 property_get("ro.serialno", serialno, "");
164 sprintf(tmp, "%s/wimax.%s.img", backup_path, serialno);
Koushik Dutta5ee98b22011-03-02 19:35:05 -0800165 ret = backup_raw_partition(vol->device, tmp);
agrabren1f76a5d2010-12-29 12:15:18 -0600166 if (0 != ret)
Joshua Wiseca889ec2010-12-30 02:48:25 -0500167 return print_and_error("Error while dumping WiMAX image!\n");
agrabren1f76a5d2010-12-29 12:15:18 -0600168 }
169
Koushik Duttadf1e4062010-12-18 17:42:31 -0800170 if (0 != (ret = nandroid_backup_partition(backup_path, "/system")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700171 return ret;
172
Koushik Duttadf1e4062010-12-18 17:42:31 -0800173 if (0 != (ret = nandroid_backup_partition(backup_path, "/data")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700174 return ret;
175
Koushik Dutta5460f0c2010-12-18 22:37:49 -0800176 if (has_datadata()) {
177 if (0 != (ret = nandroid_backup_partition(backup_path, "/datadata")))
178 return ret;
179 }
Koushik Dutta8b5e1852010-06-14 22:04:22 -0700180
Koushik Dutta5ee98b22011-03-02 19:35:05 -0800181 if (0 != stat("/sdcard/.android_secure", &s))
Koushik Dutta062d6b02010-07-03 13:54:21 -0700182 {
183 ui_print("No /sdcard/.android_secure found. Skipping backup of applications on external storage.\n");
184 }
185 else
186 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800187 if (0 != (ret = nandroid_backup_partition_extended(backup_path, "/sdcard/.android_secure", 0)))
Koushik Dutta062d6b02010-07-03 13:54:21 -0700188 return ret;
189 }
190
Koushik Duttadf1e4062010-12-18 17:42:31 -0800191 if (0 != (ret = nandroid_backup_partition_extended(backup_path, "/cache", 0)))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700192 return ret;
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700193
Koushik Dutta5ee98b22011-03-02 19:35:05 -0800194 vol = volume_for_path("/sd-ext");
195 if (vol == NULL || 0 != stat(vol->device, &s))
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700196 {
Koushik K. Dutta71ef11f2010-04-02 22:17:42 -0700197 ui_print("No sd-ext found. Skipping backup of sd-ext.\n");
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700198 }
199 else
200 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800201 if (0 != ensure_path_mounted("/sd-ext"))
Koushik Dutta8b5e1852010-06-14 22:04:22 -0700202 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 -0800203 else if (0 != (ret = nandroid_backup_partition(backup_path, "/sd-ext")))
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700204 return ret;
205 }
Koushik Dutta062d6b02010-07-03 13:54:21 -0700206
Koushik K. Duttad7178922010-03-24 14:38:40 -0700207 ui_print("Generating md5 sum...\n");
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700208 sprintf(tmp, "nandroid-md5.sh %s", backup_path);
Koushik K. Duttab0a25032010-03-24 10:34:38 -0700209 if (0 != (ret = __system(tmp))) {
210 ui_print("Error while generating md5 sum!\n");
211 return ret;
212 }
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800213
Koushik K. Dutta3f38f322010-03-12 23:45:25 -0800214 sync();
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800215 ui_set_background(BACKGROUND_ICON_NONE);
216 ui_reset_progress();
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700217 ui_print("\nBackup complete!\n");
218 return 0;
219}
220
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700221typedef int (*format_function)(char* root);
222
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700223static void ensure_directory(const char* dir) {
224 char tmp[PATH_MAX];
225 sprintf(tmp, "mkdir -p %s", dir);
226 __system(tmp);
227}
228
Koushik Duttadf1e4062010-12-18 17:42:31 -0800229int nandroid_restore_partition_extended(const char* backup_path, const char* mount_point, int umount_when_finished) {
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700230 int ret = 0;
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700231 char* name = basename(mount_point);
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700232
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700233 char tmp[PATH_MAX];
234 sprintf(tmp, "%s/%s.img", backup_path, name);
Koushik Dutta58131e72010-06-09 12:41:20 -0700235 struct stat file_info;
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700236 if (0 != (ret = statfs(tmp, &file_info))) {
Koushik Duttad4060c32010-07-22 20:14:44 -0700237 ui_print("%s.img not found. Skipping restore of %s.\n", name, mount_point);
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700238 return 0;
239 }
240
241 ensure_directory(mount_point);
242
Koushik Dutta58131e72010-06-09 12:41:20 -0700243 unyaffs_callback callback = NULL;
244 if (0 != stat("/sdcard/clockworkmod/.hidenandroidprogress", &file_info)) {
245 callback = yaffs_callback;
246 }
247
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700248 ui_print("Restoring %s...\n", name);
Koushik Dutta852bb422010-07-24 11:18:00 -0700249 /*
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700250 if (0 != (ret = ensure_root_path_unmounted(root))) {
251 ui_print("Can't unmount %s!\n", mount_point);
252 return ret;
253 }
Koushik Dutta852bb422010-07-24 11:18:00 -0700254 */
Koushik Duttabf4444f2010-12-18 18:57:47 -0800255 if (0 != (ret = format_volume(mount_point))) {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800256 ui_print("Error while formatting %s!\n", mount_point);
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700257 return ret;
258 }
259
Koushik Duttadf1e4062010-12-18 17:42:31 -0800260 if (0 != (ret = ensure_path_mounted(mount_point))) {
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700261 ui_print("Can't mount %s!\n", mount_point);
262 return ret;
263 }
264
Koushik Dutta58131e72010-06-09 12:41:20 -0700265 if (0 != (ret = unyaffs(tmp, mount_point, callback))) {
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700266 ui_print("Error while restoring %s!\n", mount_point);
267 return ret;
268 }
Koushik Dutta2f73e582010-04-18 16:00:21 -0700269
Koushik Dutta062d6b02010-07-03 13:54:21 -0700270 if (umount_when_finished) {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800271 ensure_path_unmounted(mount_point);
Koushik Dutta2f73e582010-04-18 16:00:21 -0700272 }
Koushik Dutta062d6b02010-07-03 13:54:21 -0700273
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800274 return 0;
275}
276
Koushik Dutta062d6b02010-07-03 13:54:21 -0700277int nandroid_restore_partition(const char* backup_path, const char* root) {
Koushik Dutta53c9bd82011-01-04 11:38:31 -0800278 Volume *vol = volume_for_path(root);
279 // make sure the volume exists...
280 if (vol == NULL || vol->fs_type == NULL)
281 return 0;
282
283 // see if we need a raw restore (mtd)
284 char tmp[PATH_MAX];
Christopher Lais066a1022011-01-16 06:02:34 -0600285 if (strcmp(vol->fs_type, "mtd") == 0 ||
Koushik Dutta33e37f32011-02-27 16:33:32 -0800286 strcmp(vol->fs_type, "bml") == 0 ||
Christopher Lais066a1022011-01-16 06:02:34 -0600287 strcmp(vol->fs_type, "emmc") == 0) {
Koushik Dutta53c9bd82011-01-04 11:38:31 -0800288 int ret;
289 const char* name = basename(root);
290 ui_print("Erasing %s before restore...\n", name);
291 if (0 != (ret = format_volume(root))) {
292 ui_print("Error while erasing %s image!", name);
293 return ret;
294 }
295 sprintf(tmp, "%s%s.img", backup_path, root);
296 ui_print("Restoring %s image...\n", name);
Koushik Duttafdd47662011-04-17 00:49:15 -0700297 if (0 != (ret = restore_raw_partition(vol->device, tmp))) {
Koushik Dutta53c9bd82011-01-04 11:38:31 -0800298 ui_print("Error while flashing %s image!", name);
299 return ret;
300 }
301 return 0;
302 }
Koushik Dutta062d6b02010-07-03 13:54:21 -0700303 return nandroid_restore_partition_extended(backup_path, root, 1);
304}
305
agrabren1f76a5d2010-12-29 12:15:18 -0600306int 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 -0800307{
308 ui_set_background(BACKGROUND_ICON_INSTALLING);
309 ui_show_indeterminate_progress();
310 yaffs_files_total = 0;
311
Koushik Duttadf1e4062010-12-18 17:42:31 -0800312 if (ensure_path_mounted("/sdcard") != 0)
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800313 return print_and_error("Can't mount /sdcard\n");
314
315 char tmp[PATH_MAX];
316
317 ui_print("Checking MD5 sums...\n");
Koushik K. Duttaface5882010-03-13 10:15:55 -0800318 sprintf(tmp, "cd %s && md5sum -c nandroid.md5", backup_path);
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800319 if (0 != __system(tmp))
320 return print_and_error("MD5 mismatch!\n");
321
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700322 int ret;
Koushik Dutta53c9bd82011-01-04 11:38:31 -0800323
324 if (restore_boot && NULL != volume_for_path("/boot") && 0 != (ret = nandroid_restore_partition(backup_path, "/boot")))
325 return ret;
Koushik K. Duttaf7215942010-03-16 13:34:51 -0700326
Koushik Dutta5ee98b22011-03-02 19:35:05 -0800327 struct stat s;
328 Volume *vol = volume_for_path("/wimax");
329 if (restore_wimax && vol != NULL && 0 == stat(vol->device, &s))
agrabren1f76a5d2010-12-29 12:15:18 -0600330 {
Joshua Wise05d4a092010-12-31 02:05:35 -0500331 char serialno[PROPERTY_VALUE_MAX];
332
333 serialno[0] = 0;
334 property_get("ro.serialno", serialno, "");
335 sprintf(tmp, "%s/wimax.%s.img", backup_path, serialno);
agrabren1f76a5d2010-12-29 12:15:18 -0600336
337 struct stat st;
338 if (0 != stat(tmp, &st))
339 {
Joshua Wiseca889ec2010-12-30 02:48:25 -0500340 ui_print("WARNING: WiMAX partition exists, but nandroid\n");
341 ui_print(" backup does not contain WiMAX image.\n");
agrabren1f76a5d2010-12-29 12:15:18 -0600342 ui_print(" You should create a new backup to\n");
Joshua Wiseca889ec2010-12-30 02:48:25 -0500343 ui_print(" protect your WiMAX keys.\n");
agrabren1f76a5d2010-12-29 12:15:18 -0600344 }
345 else
346 {
Joshua Wise5a3f1d82010-12-31 02:16:00 -0500347 ui_print("Erasing WiMAX before restore...\n");
Koushik Duttaa25deae2011-01-01 18:17:48 -0800348 if (0 != (ret = format_volume("/wimax")))
Joshua Wise5a3f1d82010-12-31 02:16:00 -0500349 return print_and_error("Error while formatting wimax!\n");
Joshua Wise05d4a092010-12-31 02:05:35 -0500350 ui_print("Restoring WiMAX image...\n");
Koushik Dutta5ee98b22011-03-02 19:35:05 -0800351 if (0 != (ret = restore_raw_partition(vol->device, tmp)))
agrabren1f76a5d2010-12-29 12:15:18 -0600352 return ret;
353 }
354 }
355
Koushik Duttadf1e4062010-12-18 17:42:31 -0800356 if (restore_system && 0 != (ret = nandroid_restore_partition(backup_path, "/system")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700357 return ret;
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800358
Koushik Duttadf1e4062010-12-18 17:42:31 -0800359 if (restore_data && 0 != (ret = nandroid_restore_partition(backup_path, "/data")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700360 return ret;
Koushik Dutta8b5e1852010-06-14 22:04:22 -0700361
Koushik Dutta5460f0c2010-12-18 22:37:49 -0800362 if (has_datadata()) {
363 if (restore_data && 0 != (ret = nandroid_restore_partition(backup_path, "/datadata")))
364 return ret;
365 }
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800366
Koushik Duttadf1e4062010-12-18 17:42:31 -0800367 if (restore_data && 0 != (ret = nandroid_restore_partition_extended(backup_path, "/sdcard/.android_secure", 0)))
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700368 return ret;
Koushik Dutta062d6b02010-07-03 13:54:21 -0700369
Koushik Duttadf1e4062010-12-18 17:42:31 -0800370 if (restore_cache && 0 != (ret = nandroid_restore_partition_extended(backup_path, "/cache", 0)))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700371 return ret;
372
Koushik Duttadf1e4062010-12-18 17:42:31 -0800373 if (restore_sdext && 0 != (ret = nandroid_restore_partition(backup_path, "/sd-ext")))
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700374 return ret;
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700375
Koushik K. Dutta3f38f322010-03-12 23:45:25 -0800376 sync();
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800377 ui_set_background(BACKGROUND_ICON_NONE);
378 ui_reset_progress();
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700379 ui_print("\nRestore complete!\n");
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800380 return 0;
381}
Koushik Dutta08370912010-06-26 12:25:02 -0700382
383void nandroid_generate_timestamp_path(char* backup_path)
384{
385 time_t t = time(NULL);
386 struct tm *tmp = localtime(&t);
387 if (tmp == NULL)
388 {
389 struct timeval tp;
390 gettimeofday(&tp, NULL);
391 sprintf(backup_path, "/sdcard/clockworkmod/backup/%d", tp.tv_sec);
392 }
393 else
394 {
395 strftime(backup_path, PATH_MAX, "/sdcard/clockworkmod/backup/%F.%H.%M.%S", tmp);
396 }
397}
398
399int nandroid_usage()
400{
401 printf("Usage: nandroid backup\n");
402 printf("Usage: nandroid restore <directory>\n");
403 return 1;
404}
405
406int nandroid_main(int argc, char** argv)
407{
408 if (argc > 3 || argc < 2)
409 return nandroid_usage();
410
411 if (strcmp("backup", argv[1]) == 0)
412 {
413 if (argc != 2)
414 return nandroid_usage();
415
416 char backup_path[PATH_MAX];
417 nandroid_generate_timestamp_path(backup_path);
418 return nandroid_backup(backup_path);
419 }
420
421 if (strcmp("restore", argv[1]) == 0)
422 {
423 if (argc != 3)
424 return nandroid_usage();
Koushik Dutta5b7f34a2010-12-29 23:36:03 -0800425 return nandroid_restore(argv[2], 1, 1, 1, 1, 1, 0);
Koushik Dutta08370912010-06-26 12:25:02 -0700426 }
427
428 return nandroid_usage();
429}