blob: 4f7724aa79238c058fcd9a649ad56ff470bdccf5 [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 Dutta062d6b02010-07-03 13:54:21 -0700103int nandroid_backup_partition(const char* backup_path, char* root) {
104 return nandroid_backup_partition_extended(backup_path, root, 1);
105}
106
Koushik Dutta2f73e582010-04-18 16:00:21 -0700107int nandroid_backup(const char* backup_path)
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800108{
109 ui_set_background(BACKGROUND_ICON_INSTALLING);
110
Koushik Duttadf1e4062010-12-18 17:42:31 -0800111 if (ensure_path_mounted("/sdcard") != 0)
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800112 return print_and_error("Can't mount /sdcard\n");
113
Koushik K. Dutta78686292010-03-25 17:58:45 -0700114 int ret;
Koushik K. Duttaf3534d02010-04-18 18:06:24 -0700115 struct statfs s;
Koushik K. Dutta78686292010-03-25 17:58:45 -0700116 if (0 != (ret = statfs("/sdcard", &s)))
117 return print_and_error("Unable to stat /sdcard\n");
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700118 uint64_t bavail = s.f_bavail;
119 uint64_t bsize = s.f_bsize;
120 uint64_t sdcard_free = bavail * bsize;
121 uint64_t sdcard_free_mb = sdcard_free / (uint64_t)(1024 * 1024);
122 ui_print("SD Card space free: %lluMB\n", sdcard_free_mb);
123 if (sdcard_free_mb < 150)
Koushik K. Duttac0970932010-03-25 23:19:19 -0700124 ui_print("There may not be enough free space to complete backup... continuing...\n");
Koushik K. Dutta78686292010-03-25 17:58:45 -0700125
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800126 char tmp[PATH_MAX];
127 sprintf(tmp, "mkdir -p %s", backup_path);
128 __system(tmp);
129
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700130#ifndef BOARD_RECOVERY_IGNORE_BOOTABLES
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800131 ui_print("Backing up boot...\n");
132 sprintf(tmp, "%s/%s", backup_path, "boot.img");
Koushik Dutta19447c02010-11-10 10:40:44 -0800133 ret = backup_raw_partition("boot", tmp);
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800134 if (0 != ret)
135 return print_and_error("Error while dumping boot image!\n");
Koushik K. Dutta6923cc32010-03-29 14:46:00 -0700136
137 ui_print("Backing up recovery...\n");
138 sprintf(tmp, "%s/%s", backup_path, "recovery.img");
Koushik Dutta19447c02010-11-10 10:40:44 -0800139 ret = backup_raw_partition("recovery", tmp);
Koushik K. Dutta6923cc32010-03-29 14:46:00 -0700140 if (0 != ret)
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700141 return print_and_error("Error while dumping recovery image!\n");
142#endif
Koushik K. Dutta6923cc32010-03-29 14:46:00 -0700143
Koushik Duttadf1e4062010-12-18 17:42:31 -0800144 if (0 != (ret = nandroid_backup_partition(backup_path, "/system")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700145 return ret;
146
Koushik Duttadf1e4062010-12-18 17:42:31 -0800147 if (0 != (ret = nandroid_backup_partition(backup_path, "/data")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700148 return ret;
149
Koushik Dutta19447c02010-11-10 10:40:44 -0800150#ifdef BOARD_HAS_DATADATA
Koushik Duttadf1e4062010-12-18 17:42:31 -0800151 if (0 != (ret = nandroid_backup_partition(backup_path, "/datadata")))
Koushik Dutta8b5e1852010-06-14 22:04:22 -0700152 return ret;
153#endif
154
Koushik Dutta062d6b02010-07-03 13:54:21 -0700155 struct stat st;
156 if (0 != stat("/sdcard/.android_secure", &st))
157 {
158 ui_print("No /sdcard/.android_secure found. Skipping backup of applications on external storage.\n");
159 }
160 else
161 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800162 if (0 != (ret = nandroid_backup_partition_extended(backup_path, "/sdcard/.android_secure", 0)))
Koushik Dutta062d6b02010-07-03 13:54:21 -0700163 return ret;
164 }
165
Koushik Duttadf1e4062010-12-18 17:42:31 -0800166 if (0 != (ret = nandroid_backup_partition_extended(backup_path, "/cache", 0)))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700167 return ret;
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700168
Koushik Duttadf1e4062010-12-18 17:42:31 -0800169 Volume *vol = volume_for_path("/sd-ext");
170 if (vol == NULL || 0 != stat(vol->device, &st))
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700171 {
Koushik K. Dutta71ef11f2010-04-02 22:17:42 -0700172 ui_print("No sd-ext found. Skipping backup of sd-ext.\n");
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700173 }
174 else
175 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800176 if (0 != ensure_path_mounted("/sd-ext"))
Koushik Dutta8b5e1852010-06-14 22:04:22 -0700177 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 -0700178 else if (0 != (ret = nandroid_backup_partition(backup_path, "SDEXT:")))
179 return ret;
180 }
Koushik Dutta062d6b02010-07-03 13:54:21 -0700181
Koushik K. Duttad7178922010-03-24 14:38:40 -0700182 ui_print("Generating md5 sum...\n");
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700183 sprintf(tmp, "nandroid-md5.sh %s", backup_path);
Koushik K. Duttab0a25032010-03-24 10:34:38 -0700184 if (0 != (ret = __system(tmp))) {
185 ui_print("Error while generating md5 sum!\n");
186 return ret;
187 }
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800188
Koushik K. Dutta3f38f322010-03-12 23:45:25 -0800189 sync();
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800190 ui_set_background(BACKGROUND_ICON_NONE);
191 ui_reset_progress();
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700192 ui_print("\nBackup complete!\n");
193 return 0;
194}
195
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700196typedef int (*format_function)(char* root);
197
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700198static void ensure_directory(const char* dir) {
199 char tmp[PATH_MAX];
200 sprintf(tmp, "mkdir -p %s", dir);
201 __system(tmp);
202}
203
Koushik Duttadf1e4062010-12-18 17:42:31 -0800204int nandroid_restore_partition_extended(const char* backup_path, const char* mount_point, int umount_when_finished) {
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700205 int ret = 0;
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700206 char* name = basename(mount_point);
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700207
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700208 char tmp[PATH_MAX];
209 sprintf(tmp, "%s/%s.img", backup_path, name);
Koushik Dutta58131e72010-06-09 12:41:20 -0700210 struct stat file_info;
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700211 if (0 != (ret = statfs(tmp, &file_info))) {
Koushik Duttad4060c32010-07-22 20:14:44 -0700212 ui_print("%s.img not found. Skipping restore of %s.\n", name, mount_point);
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700213 return 0;
214 }
215
216 ensure_directory(mount_point);
217
Koushik Dutta58131e72010-06-09 12:41:20 -0700218 unyaffs_callback callback = NULL;
219 if (0 != stat("/sdcard/clockworkmod/.hidenandroidprogress", &file_info)) {
220 callback = yaffs_callback;
221 }
222
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700223 ui_print("Restoring %s...\n", name);
Koushik Dutta852bb422010-07-24 11:18:00 -0700224 /*
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700225 if (0 != (ret = ensure_root_path_unmounted(root))) {
226 ui_print("Can't unmount %s!\n", mount_point);
227 return ret;
228 }
Koushik Dutta852bb422010-07-24 11:18:00 -0700229 */
Koushik Duttadf1e4062010-12-18 17:42:31 -0800230 if (0 != (ret = format_device(mount_point))) {
231 ui_print("Error while formatting %s!\n", mount_point);
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700232 return ret;
233 }
234
Koushik Duttadf1e4062010-12-18 17:42:31 -0800235 if (0 != (ret = ensure_path_mounted(mount_point))) {
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700236 ui_print("Can't mount %s!\n", mount_point);
237 return ret;
238 }
239
Koushik Dutta58131e72010-06-09 12:41:20 -0700240 if (0 != (ret = unyaffs(tmp, mount_point, callback))) {
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700241 ui_print("Error while restoring %s!\n", mount_point);
242 return ret;
243 }
Koushik Dutta2f73e582010-04-18 16:00:21 -0700244
Koushik Dutta062d6b02010-07-03 13:54:21 -0700245 if (umount_when_finished) {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800246 ensure_path_unmounted(mount_point);
Koushik Dutta2f73e582010-04-18 16:00:21 -0700247 }
Koushik Dutta062d6b02010-07-03 13:54:21 -0700248
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800249 return 0;
250}
251
Koushik Dutta062d6b02010-07-03 13:54:21 -0700252int nandroid_restore_partition(const char* backup_path, const char* root) {
253 return nandroid_restore_partition_extended(backup_path, root, 1);
254}
255
Koushik Dutta2f73e582010-04-18 16:00:21 -0700256int 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 -0800257{
258 ui_set_background(BACKGROUND_ICON_INSTALLING);
259 ui_show_indeterminate_progress();
260 yaffs_files_total = 0;
261
Koushik Duttadf1e4062010-12-18 17:42:31 -0800262 if (ensure_path_mounted("/sdcard") != 0)
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800263 return print_and_error("Can't mount /sdcard\n");
264
265 char tmp[PATH_MAX];
266
267 ui_print("Checking MD5 sums...\n");
Koushik K. Duttaface5882010-03-13 10:15:55 -0800268 sprintf(tmp, "cd %s && md5sum -c nandroid.md5", backup_path);
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800269 if (0 != __system(tmp))
270 return print_and_error("MD5 mismatch!\n");
271
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700272 int ret;
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700273#ifndef BOARD_RECOVERY_IGNORE_BOOTABLES
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700274 if (restore_boot)
275 {
Koushik K. Dutta84306552010-04-18 21:21:58 -0700276 ui_print("Erasing boot before restore...\n");
Koushik Duttadf1e4062010-12-18 17:42:31 -0800277 if (0 != (ret = format_device("boot")))
Koushik K. Dutta84306552010-04-18 21:21:58 -0700278 return print_and_error("Error while formatting BOOT:!\n");
Koushik Dutta28a41b42010-09-13 14:55:17 -0700279 sprintf(tmp, "%s/boot.img", backup_path);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700280 ui_print("Restoring boot image...\n");
Koushik Dutta19447c02010-11-10 10:40:44 -0800281 if (0 != (ret = restore_raw_partition("boot", tmp))) {
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700282 ui_print("Error while flashing boot image!");
283 return ret;
284 }
Koushik K. Duttaf7215942010-03-16 13:34:51 -0700285 }
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700286#endif
Koushik K. Duttaf7215942010-03-16 13:34:51 -0700287
Koushik Duttadf1e4062010-12-18 17:42:31 -0800288 if (restore_system && 0 != (ret = nandroid_restore_partition(backup_path, "/system")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700289 return ret;
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800290
Koushik Duttadf1e4062010-12-18 17:42:31 -0800291 if (restore_data && 0 != (ret = nandroid_restore_partition(backup_path, "/data")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700292 return ret;
Koushik Dutta8b5e1852010-06-14 22:04:22 -0700293
Koushik Dutta19447c02010-11-10 10:40:44 -0800294#ifdef BOARD_HAS_DATADATA
Koushik Duttadf1e4062010-12-18 17:42:31 -0800295 if (restore_data && 0 != (ret = nandroid_restore_partition(backup_path, "/datadata")))
Koushik Dutta8b5e1852010-06-14 22:04:22 -0700296 return ret;
297#endif
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800298
Koushik Duttadf1e4062010-12-18 17:42:31 -0800299 if (restore_data && 0 != (ret = nandroid_restore_partition_extended(backup_path, "/sdcard/.android_secure", 0)))
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700300 return ret;
Koushik Dutta062d6b02010-07-03 13:54:21 -0700301
Koushik Duttadf1e4062010-12-18 17:42:31 -0800302 if (restore_cache && 0 != (ret = nandroid_restore_partition_extended(backup_path, "/cache", 0)))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700303 return ret;
304
Koushik Duttadf1e4062010-12-18 17:42:31 -0800305 if (restore_sdext && 0 != (ret = nandroid_restore_partition(backup_path, "/sd-ext")))
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700306 return ret;
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700307
Koushik K. Dutta3f38f322010-03-12 23:45:25 -0800308 sync();
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800309 ui_set_background(BACKGROUND_ICON_NONE);
310 ui_reset_progress();
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700311 ui_print("\nRestore complete!\n");
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800312 return 0;
313}
Koushik Dutta08370912010-06-26 12:25:02 -0700314
315void nandroid_generate_timestamp_path(char* backup_path)
316{
317 time_t t = time(NULL);
318 struct tm *tmp = localtime(&t);
319 if (tmp == NULL)
320 {
321 struct timeval tp;
322 gettimeofday(&tp, NULL);
323 sprintf(backup_path, "/sdcard/clockworkmod/backup/%d", tp.tv_sec);
324 }
325 else
326 {
327 strftime(backup_path, PATH_MAX, "/sdcard/clockworkmod/backup/%F.%H.%M.%S", tmp);
328 }
329}
330
331int nandroid_usage()
332{
333 printf("Usage: nandroid backup\n");
334 printf("Usage: nandroid restore <directory>\n");
335 return 1;
336}
337
338int nandroid_main(int argc, char** argv)
339{
340 if (argc > 3 || argc < 2)
341 return nandroid_usage();
342
343 if (strcmp("backup", argv[1]) == 0)
344 {
345 if (argc != 2)
346 return nandroid_usage();
347
348 char backup_path[PATH_MAX];
349 nandroid_generate_timestamp_path(backup_path);
350 return nandroid_backup(backup_path);
351 }
352
353 if (strcmp("restore", argv[1]) == 0)
354 {
355 if (argc != 3)
356 return nandroid_usage();
357 return nandroid_restore(argv[2], 1, 1, 1, 1, 1);
358 }
359
360 return nandroid_usage();
361}