blob: 35ad6415b880446635d498397d4862828d424564 [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))) {
Koushik Duttad4060c32010-07-22 20:14:44 -0700219 ui_print("%s.img not found. Skipping restore of %s.\n", name, mount_point);
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700220 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);
Koushik Dutta852bb422010-07-24 11:18:00 -0700231 /*
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700232 if (0 != (ret = ensure_root_path_unmounted(root))) {
233 ui_print("Can't unmount %s!\n", mount_point);
234 return ret;
235 }
Koushik Dutta852bb422010-07-24 11:18:00 -0700236 */
Koushik Dutta2f73e582010-04-18 16:00:21 -0700237 if (0 != (ret = format_root_device(root))) {
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700238 ui_print("Error while formatting %s!\n", root);
239 return ret;
240 }
241
242 if (0 != (ret = ensure_root_path_mounted(root))) {
243 ui_print("Can't mount %s!\n", mount_point);
244 return ret;
245 }
246
Koushik Dutta58131e72010-06-09 12:41:20 -0700247 if (0 != (ret = unyaffs(tmp, mount_point, callback))) {
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700248 ui_print("Error while restoring %s!\n", mount_point);
249 return ret;
250 }
Koushik Dutta2f73e582010-04-18 16:00:21 -0700251
Koushik Dutta062d6b02010-07-03 13:54:21 -0700252 if (umount_when_finished) {
Koushik Dutta2f73e582010-04-18 16:00:21 -0700253 ensure_root_path_unmounted(root);
254 }
Koushik Dutta062d6b02010-07-03 13:54:21 -0700255
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800256 return 0;
257}
258
Koushik Dutta062d6b02010-07-03 13:54:21 -0700259int nandroid_restore_partition(const char* backup_path, const char* root) {
260 return nandroid_restore_partition_extended(backup_path, root, 1);
261}
262
Koushik Dutta2f73e582010-04-18 16:00:21 -0700263int 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 -0800264{
265 ui_set_background(BACKGROUND_ICON_INSTALLING);
266 ui_show_indeterminate_progress();
267 yaffs_files_total = 0;
268
269 if (ensure_root_path_mounted("SDCARD:") != 0)
270 return print_and_error("Can't mount /sdcard\n");
271
272 char tmp[PATH_MAX];
273
274 ui_print("Checking MD5 sums...\n");
Koushik K. Duttaface5882010-03-13 10:15:55 -0800275 sprintf(tmp, "cd %s && md5sum -c nandroid.md5", backup_path);
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800276 if (0 != __system(tmp))
277 return print_and_error("MD5 mismatch!\n");
278
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700279 int ret;
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700280#ifndef BOARD_RECOVERY_IGNORE_BOOTABLES
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700281 if (restore_boot)
282 {
Koushik K. Dutta84306552010-04-18 21:21:58 -0700283 ui_print("Erasing boot before restore...\n");
284 if (0 != (ret = format_root_device("BOOT:")))
285 return print_and_error("Error while formatting BOOT:!\n");
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700286 sprintf(tmp, "flash_image boot %s/boot.img", backup_path);
287 ui_print("Restoring boot image...\n");
288 if (0 != (ret = __system(tmp))) {
289 ui_print("Error while flashing boot image!");
290 return ret;
291 }
Koushik K. Duttaf7215942010-03-16 13:34:51 -0700292 }
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700293#endif
Koushik K. Duttaf7215942010-03-16 13:34:51 -0700294
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700295 if (restore_system && 0 != (ret = nandroid_restore_partition(backup_path, "SYSTEM:")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700296 return ret;
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800297
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700298 if (restore_data && 0 != (ret = nandroid_restore_partition(backup_path, "DATA:")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700299 return ret;
Koushik Dutta8b5e1852010-06-14 22:04:22 -0700300
301#ifdef HAS_DATADATA
302 if (restore_data && 0 != (ret = nandroid_restore_partition(backup_path, "DATADATA:")))
303 return ret;
304#endif
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800305
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700306 if (restore_data && 0 != (ret = nandroid_restore_partition_extended(backup_path, "SDCARD:/.android_secure", 0)))
307 return ret;
Koushik Dutta062d6b02010-07-03 13:54:21 -0700308
309 if (restore_cache && 0 != (ret = nandroid_restore_partition_extended(backup_path, "CACHE:", 0)))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700310 return ret;
311
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700312 if (restore_sdext && 0 != (ret = nandroid_restore_partition(backup_path, "SDEXT:")))
313 return ret;
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700314
Koushik K. Dutta3f38f322010-03-12 23:45:25 -0800315 sync();
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800316 ui_set_background(BACKGROUND_ICON_NONE);
317 ui_reset_progress();
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700318 ui_print("\nRestore complete!\n");
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800319 return 0;
320}
Koushik Dutta08370912010-06-26 12:25:02 -0700321
322void nandroid_generate_timestamp_path(char* backup_path)
323{
324 time_t t = time(NULL);
325 struct tm *tmp = localtime(&t);
326 if (tmp == NULL)
327 {
328 struct timeval tp;
329 gettimeofday(&tp, NULL);
330 sprintf(backup_path, "/sdcard/clockworkmod/backup/%d", tp.tv_sec);
331 }
332 else
333 {
334 strftime(backup_path, PATH_MAX, "/sdcard/clockworkmod/backup/%F.%H.%M.%S", tmp);
335 }
336}
337
338int nandroid_usage()
339{
340 printf("Usage: nandroid backup\n");
341 printf("Usage: nandroid restore <directory>\n");
342 return 1;
343}
344
345int nandroid_main(int argc, char** argv)
346{
347 if (argc > 3 || argc < 2)
348 return nandroid_usage();
349
350 if (strcmp("backup", argv[1]) == 0)
351 {
352 if (argc != 2)
353 return nandroid_usage();
354
355 char backup_path[PATH_MAX];
356 nandroid_generate_timestamp_path(backup_path);
357 return nandroid_backup(backup_path);
358 }
359
360 if (strcmp("restore", argv[1]) == 0)
361 {
362 if (argc != 3)
363 return nandroid_usage();
364 return nandroid_restore(argv[2], 1, 1, 1, 1, 1);
365 }
366
367 return nandroid_usage();
368}