blob: 33899d1ae3928a5e95ca60470e1860cee1708ba7 [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
Koushik K. Dutta78686292010-03-25 17:58:45 -070040#include <sys/vfs.h>
41
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -080042#include "extendedcommands.h"
43#include "nandroid.h"
44
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -070045int print_and_error(char* message) {
46 ui_print(message);
47 return 1;
48}
49
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -080050int yaffs_files_total = 0;
51int yaffs_files_count = 0;
52void yaffs_callback(char* filename)
53{
54 char* justfile = basename(filename);
55 if (strlen(justfile) < 30)
Koushik K. Dutta3fb8d302010-03-15 17:05:27 -070056 ui_print(justfile);
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -080057 yaffs_files_count++;
58 if (yaffs_files_total != 0)
59 ui_set_progress((float)yaffs_files_count / (float)yaffs_files_total);
60 ui_reset_text_col();
61}
62
63void compute_directory_stats(char* directory)
64{
65 char tmp[PATH_MAX];
66 sprintf(tmp, "find %s | wc -l > /tmp/dircount", directory);
67 __system(tmp);
68 char count_text[100];
69 FILE* f = fopen("/tmp/dircount", "r");
70 fread(count_text, 1, sizeof(count_text), f);
71 fclose(f);
72 yaffs_files_count = 0;
73 yaffs_files_total = atoi(count_text);
74 ui_reset_progress();
75 ui_show_progress(1, 0);
76}
77
Koushik Dutta062d6b02010-07-03 13:54:21 -070078int nandroid_backup_partition_extended(const char* backup_path, char* root, int umount_when_finished) {
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -070079 int ret = 0;
80 char mount_point[PATH_MAX];
Koushik K. Dutta68b01902010-04-01 12:20:39 -070081 translate_root_path(root, mount_point, PATH_MAX);
82 char* name = basename(mount_point);
Koushik Dutta58131e72010-06-09 12:41:20 -070083
84 struct stat file_info;
85 mkyaffs2image_callback callback = NULL;
86 if (0 != stat("/sdcard/clockworkmod/.hidenandroidprogress", &file_info)) {
87 callback = yaffs_callback;
88 }
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -070089
90 ui_print("Backing up %s...\n", name);
91 if (0 != (ret = ensure_root_path_mounted(root) != 0)) {
92 ui_print("Can't mount %s!\n", mount_point);
93 return ret;
94 }
95 compute_directory_stats(mount_point);
96 char tmp[PATH_MAX];
97 sprintf(tmp, "%s/%s.img", backup_path, name);
Koushik Dutta58131e72010-06-09 12:41:20 -070098 ret = mkyaffs2image(mount_point, tmp, 0, callback);
Koushik Dutta062d6b02010-07-03 13:54:21 -070099 if (umount_when_finished) {
100 ensure_root_path_unmounted(root);
101 }
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700102 if (0 != ret) {
103 ui_print("Error while making a yaffs2 image of %s!\n", mount_point);
104 return ret;
105 }
106 return 0;
107}
108
Koushik Dutta062d6b02010-07-03 13:54:21 -0700109int nandroid_backup_partition(const char* backup_path, char* root) {
110 return nandroid_backup_partition_extended(backup_path, root, 1);
111}
112
Koushik Dutta2f73e582010-04-18 16:00:21 -0700113int nandroid_backup(const char* backup_path)
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800114{
115 ui_set_background(BACKGROUND_ICON_INSTALLING);
116
117 if (ensure_root_path_mounted("SDCARD:") != 0)
118 return print_and_error("Can't mount /sdcard\n");
119
Koushik K. Dutta78686292010-03-25 17:58:45 -0700120 int ret;
Koushik K. Duttaf3534d02010-04-18 18:06:24 -0700121 struct statfs s;
Koushik K. Dutta78686292010-03-25 17:58:45 -0700122 if (0 != (ret = statfs("/sdcard", &s)))
123 return print_and_error("Unable to stat /sdcard\n");
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700124 uint64_t bavail = s.f_bavail;
125 uint64_t bsize = s.f_bsize;
126 uint64_t sdcard_free = bavail * bsize;
127 uint64_t sdcard_free_mb = sdcard_free / (uint64_t)(1024 * 1024);
128 ui_print("SD Card space free: %lluMB\n", sdcard_free_mb);
129 if (sdcard_free_mb < 150)
Koushik K. Duttac0970932010-03-25 23:19:19 -0700130 ui_print("There may not be enough free space to complete backup... continuing...\n");
Koushik K. Dutta78686292010-03-25 17:58:45 -0700131
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800132 char tmp[PATH_MAX];
133 sprintf(tmp, "mkdir -p %s", backup_path);
134 __system(tmp);
135
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700136#ifndef BOARD_RECOVERY_IGNORE_BOOTABLES
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800137 ui_print("Backing up boot...\n");
138 sprintf(tmp, "%s/%s", backup_path, "boot.img");
139 ret = dump_image("boot", tmp, NULL);
140 if (0 != ret)
141 return print_and_error("Error while dumping boot image!\n");
Koushik K. Dutta6923cc32010-03-29 14:46:00 -0700142
143 ui_print("Backing up recovery...\n");
144 sprintf(tmp, "%s/%s", backup_path, "recovery.img");
145 ret = dump_image("recovery", tmp, NULL);
146 if (0 != ret)
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700147 return print_and_error("Error while dumping recovery image!\n");
148#endif
Koushik K. Dutta6923cc32010-03-29 14:46:00 -0700149
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700150 if (0 != (ret = nandroid_backup_partition(backup_path, "SYSTEM:")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700151 return ret;
152
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700153 if (0 != (ret = nandroid_backup_partition(backup_path, "DATA:")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700154 return ret;
155
Koushik Dutta8b5e1852010-06-14 22:04:22 -0700156#ifdef HAS_DATADATA
157 if (0 != (ret = nandroid_backup_partition(backup_path, "DATADATA:")))
158 return ret;
159#endif
160
Koushik Dutta062d6b02010-07-03 13:54:21 -0700161 struct stat st;
162 if (0 != stat("/sdcard/.android_secure", &st))
163 {
164 ui_print("No /sdcard/.android_secure found. Skipping backup of applications on external storage.\n");
165 }
166 else
167 {
168 if (0 != (ret = nandroid_backup_partition_extended(backup_path, "SDCARD:/.android_secure", 0)))
169 return ret;
170 }
171
172 if (0 != (ret = nandroid_backup_partition_extended(backup_path, "CACHE:", 0)))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700173 return ret;
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700174
Koushik Dutta52d3f202010-06-21 12:11:13 -0700175 if (0 != stat(SDEXT_DEVICE, &st))
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700176 {
Koushik K. Dutta71ef11f2010-04-02 22:17:42 -0700177 ui_print("No sd-ext found. Skipping backup of sd-ext.\n");
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700178 }
179 else
180 {
181 if (0 != ensure_root_path_mounted("SDEXT:"))
Koushik Dutta8b5e1852010-06-14 22:04:22 -0700182 ui_print("Could not mount sd-ext. sd-ext backup may not be supported on this device. Skipping backup of sd-ext.\n");
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700183 else if (0 != (ret = nandroid_backup_partition(backup_path, "SDEXT:")))
184 return ret;
185 }
Koushik Dutta062d6b02010-07-03 13:54:21 -0700186
Koushik K. Duttad7178922010-03-24 14:38:40 -0700187 ui_print("Generating md5 sum...\n");
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700188 sprintf(tmp, "nandroid-md5.sh %s", backup_path);
Koushik K. Duttab0a25032010-03-24 10:34:38 -0700189 if (0 != (ret = __system(tmp))) {
190 ui_print("Error while generating md5 sum!\n");
191 return ret;
192 }
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800193
Koushik K. Dutta3f38f322010-03-12 23:45:25 -0800194 sync();
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800195 ui_set_background(BACKGROUND_ICON_NONE);
196 ui_reset_progress();
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700197 ui_print("\nBackup complete!\n");
198 return 0;
199}
200
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700201typedef int (*format_function)(char* root);
202
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700203static void ensure_directory(const char* dir) {
204 char tmp[PATH_MAX];
205 sprintf(tmp, "mkdir -p %s", dir);
206 __system(tmp);
207}
208
Koushik Dutta062d6b02010-07-03 13:54:21 -0700209int nandroid_restore_partition_extended(const char* backup_path, const char* root, int umount_when_finished) {
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700210 int ret = 0;
211 char mount_point[PATH_MAX];
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700212 translate_root_path(root, mount_point, PATH_MAX);
213 char* name = basename(mount_point);
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700214
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700215 char tmp[PATH_MAX];
216 sprintf(tmp, "%s/%s.img", backup_path, name);
Koushik Dutta58131e72010-06-09 12:41:20 -0700217 struct stat file_info;
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700218 if (0 != (ret = statfs(tmp, &file_info))) {
219 ui_print("%s.img not found. Skipping restore of /sd-ext.", name);
220 return 0;
221 }
222
223 ensure_directory(mount_point);
224
Koushik Dutta58131e72010-06-09 12:41:20 -0700225 unyaffs_callback callback = NULL;
226 if (0 != stat("/sdcard/clockworkmod/.hidenandroidprogress", &file_info)) {
227 callback = yaffs_callback;
228 }
229
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700230 ui_print("Restoring %s...\n", name);
231 if (0 != (ret = ensure_root_path_unmounted(root))) {
232 ui_print("Can't unmount %s!\n", mount_point);
233 return ret;
234 }
Koushik Dutta2f73e582010-04-18 16:00:21 -0700235 if (0 != (ret = format_root_device(root))) {
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700236 ui_print("Error while formatting %s!\n", root);
237 return ret;
238 }
239
240 if (0 != (ret = ensure_root_path_mounted(root))) {
241 ui_print("Can't mount %s!\n", mount_point);
242 return ret;
243 }
244
Koushik Dutta58131e72010-06-09 12:41:20 -0700245 if (0 != (ret = unyaffs(tmp, mount_point, callback))) {
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700246 ui_print("Error while restoring %s!\n", mount_point);
247 return ret;
248 }
Koushik Dutta2f73e582010-04-18 16:00:21 -0700249
Koushik Dutta062d6b02010-07-03 13:54:21 -0700250 if (umount_when_finished) {
Koushik Dutta2f73e582010-04-18 16:00:21 -0700251 ensure_root_path_unmounted(root);
252 }
Koushik Dutta062d6b02010-07-03 13:54:21 -0700253
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800254 return 0;
255}
256
Koushik Dutta062d6b02010-07-03 13:54:21 -0700257int nandroid_restore_partition(const char* backup_path, const char* root) {
258 return nandroid_restore_partition_extended(backup_path, root, 1);
259}
260
Koushik Dutta2f73e582010-04-18 16:00:21 -0700261int nandroid_restore(const char* backup_path, int restore_boot, int restore_system, int restore_data, int restore_cache, int restore_sdext)
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800262{
263 ui_set_background(BACKGROUND_ICON_INSTALLING);
264 ui_show_indeterminate_progress();
265 yaffs_files_total = 0;
266
267 if (ensure_root_path_mounted("SDCARD:") != 0)
268 return print_and_error("Can't mount /sdcard\n");
269
270 char tmp[PATH_MAX];
271
272 ui_print("Checking MD5 sums...\n");
Koushik K. Duttaface5882010-03-13 10:15:55 -0800273 sprintf(tmp, "cd %s && md5sum -c nandroid.md5", backup_path);
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800274 if (0 != __system(tmp))
275 return print_and_error("MD5 mismatch!\n");
276
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700277 int ret;
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700278#ifndef BOARD_RECOVERY_IGNORE_BOOTABLES
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700279 if (restore_boot)
280 {
Koushik K. Dutta84306552010-04-18 21:21:58 -0700281 ui_print("Erasing boot before restore...\n");
282 if (0 != (ret = format_root_device("BOOT:")))
283 return print_and_error("Error while formatting BOOT:!\n");
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700284 sprintf(tmp, "flash_image boot %s/boot.img", backup_path);
285 ui_print("Restoring boot image...\n");
286 if (0 != (ret = __system(tmp))) {
287 ui_print("Error while flashing boot image!");
288 return ret;
289 }
Koushik K. Duttaf7215942010-03-16 13:34:51 -0700290 }
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700291#endif
Koushik K. Duttaf7215942010-03-16 13:34:51 -0700292
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700293 if (restore_system && 0 != (ret = nandroid_restore_partition(backup_path, "SYSTEM:")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700294 return ret;
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800295
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700296 if (restore_data && 0 != (ret = nandroid_restore_partition(backup_path, "DATA:")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700297 return ret;
Koushik Dutta8b5e1852010-06-14 22:04:22 -0700298
299#ifdef HAS_DATADATA
300 if (restore_data && 0 != (ret = nandroid_restore_partition(backup_path, "DATADATA:")))
301 return ret;
302#endif
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800303
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700304 if (restore_data && 0 != (ret = nandroid_restore_partition_extended(backup_path, "SDCARD:/.android_secure", 0)))
305 return ret;
Koushik Dutta062d6b02010-07-03 13:54:21 -0700306
307 if (restore_cache && 0 != (ret = nandroid_restore_partition_extended(backup_path, "CACHE:", 0)))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700308 return ret;
309
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700310 if (restore_sdext && 0 != (ret = nandroid_restore_partition(backup_path, "SDEXT:")))
311 return ret;
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700312
Koushik K. Dutta3f38f322010-03-12 23:45:25 -0800313 sync();
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800314 ui_set_background(BACKGROUND_ICON_NONE);
315 ui_reset_progress();
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700316 ui_print("\nRestore complete!\n");
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800317 return 0;
318}
Koushik Dutta08370912010-06-26 12:25:02 -0700319
320void nandroid_generate_timestamp_path(char* backup_path)
321{
322 time_t t = time(NULL);
323 struct tm *tmp = localtime(&t);
324 if (tmp == NULL)
325 {
326 struct timeval tp;
327 gettimeofday(&tp, NULL);
328 sprintf(backup_path, "/sdcard/clockworkmod/backup/%d", tp.tv_sec);
329 }
330 else
331 {
332 strftime(backup_path, PATH_MAX, "/sdcard/clockworkmod/backup/%F.%H.%M.%S", tmp);
333 }
334}
335
336int nandroid_usage()
337{
338 printf("Usage: nandroid backup\n");
339 printf("Usage: nandroid restore <directory>\n");
340 return 1;
341}
342
343int nandroid_main(int argc, char** argv)
344{
345 if (argc > 3 || argc < 2)
346 return nandroid_usage();
347
348 if (strcmp("backup", argv[1]) == 0)
349 {
350 if (argc != 2)
351 return nandroid_usage();
352
353 char backup_path[PATH_MAX];
354 nandroid_generate_timestamp_path(backup_path);
355 return nandroid_backup(backup_path);
356 }
357
358 if (strcmp("restore", argv[1]) == 0)
359 {
360 if (argc != 3)
361 return nandroid_usage();
362 return nandroid_restore(argv[2], 1, 1, 1, 1, 1);
363 }
364
365 return nandroid_usage();
366}