blob: 4a92e330f958a42a8cd0eaf39926aea4cff55d5d [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
agrabren1f76a5d2010-12-29 12:15:18 -0600144 if (0 == (ret = get_partition_device("wimax", tmp)))
145 {
146 ui_print("Backing up wimax...\n");
147 sprintf(tmp, "%s/%s", backup_path, "wimax.img");
148 ret = backup_raw_partition("wimax", tmp);
149 if (0 != ret)
150 return print_and_error("Error while dumping wimax image!\n");
151 }
152
Koushik Duttadf1e4062010-12-18 17:42:31 -0800153 if (0 != (ret = nandroid_backup_partition(backup_path, "/system")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700154 return ret;
155
Koushik Duttadf1e4062010-12-18 17:42:31 -0800156 if (0 != (ret = nandroid_backup_partition(backup_path, "/data")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700157 return ret;
158
Koushik Dutta5460f0c2010-12-18 22:37:49 -0800159 if (has_datadata()) {
160 if (0 != (ret = nandroid_backup_partition(backup_path, "/datadata")))
161 return ret;
162 }
Koushik Dutta8b5e1852010-06-14 22:04:22 -0700163
Koushik Dutta062d6b02010-07-03 13:54:21 -0700164 struct stat st;
165 if (0 != stat("/sdcard/.android_secure", &st))
166 {
167 ui_print("No /sdcard/.android_secure found. Skipping backup of applications on external storage.\n");
168 }
169 else
170 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800171 if (0 != (ret = nandroid_backup_partition_extended(backup_path, "/sdcard/.android_secure", 0)))
Koushik Dutta062d6b02010-07-03 13:54:21 -0700172 return ret;
173 }
174
Koushik Duttadf1e4062010-12-18 17:42:31 -0800175 if (0 != (ret = nandroid_backup_partition_extended(backup_path, "/cache", 0)))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700176 return ret;
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700177
Koushik Duttadf1e4062010-12-18 17:42:31 -0800178 Volume *vol = volume_for_path("/sd-ext");
179 if (vol == NULL || 0 != stat(vol->device, &st))
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700180 {
Koushik K. Dutta71ef11f2010-04-02 22:17:42 -0700181 ui_print("No sd-ext found. Skipping backup of sd-ext.\n");
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700182 }
183 else
184 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800185 if (0 != ensure_path_mounted("/sd-ext"))
Koushik Dutta8b5e1852010-06-14 22:04:22 -0700186 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 -0700187 else if (0 != (ret = nandroid_backup_partition(backup_path, "SDEXT:")))
188 return ret;
189 }
Koushik Dutta062d6b02010-07-03 13:54:21 -0700190
Koushik K. Duttad7178922010-03-24 14:38:40 -0700191 ui_print("Generating md5 sum...\n");
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700192 sprintf(tmp, "nandroid-md5.sh %s", backup_path);
Koushik K. Duttab0a25032010-03-24 10:34:38 -0700193 if (0 != (ret = __system(tmp))) {
194 ui_print("Error while generating md5 sum!\n");
195 return ret;
196 }
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800197
Koushik K. Dutta3f38f322010-03-12 23:45:25 -0800198 sync();
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800199 ui_set_background(BACKGROUND_ICON_NONE);
200 ui_reset_progress();
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700201 ui_print("\nBackup complete!\n");
202 return 0;
203}
204
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700205typedef int (*format_function)(char* root);
206
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700207static void ensure_directory(const char* dir) {
208 char tmp[PATH_MAX];
209 sprintf(tmp, "mkdir -p %s", dir);
210 __system(tmp);
211}
212
Koushik Duttadf1e4062010-12-18 17:42:31 -0800213int nandroid_restore_partition_extended(const char* backup_path, const char* mount_point, int umount_when_finished) {
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700214 int ret = 0;
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700215 char* name = basename(mount_point);
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700216
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700217 char tmp[PATH_MAX];
218 sprintf(tmp, "%s/%s.img", backup_path, name);
Koushik Dutta58131e72010-06-09 12:41:20 -0700219 struct stat file_info;
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700220 if (0 != (ret = statfs(tmp, &file_info))) {
Koushik Duttad4060c32010-07-22 20:14:44 -0700221 ui_print("%s.img not found. Skipping restore of %s.\n", name, mount_point);
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700222 return 0;
223 }
224
225 ensure_directory(mount_point);
226
Koushik Dutta58131e72010-06-09 12:41:20 -0700227 unyaffs_callback callback = NULL;
228 if (0 != stat("/sdcard/clockworkmod/.hidenandroidprogress", &file_info)) {
229 callback = yaffs_callback;
230 }
231
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700232 ui_print("Restoring %s...\n", name);
Koushik Dutta852bb422010-07-24 11:18:00 -0700233 /*
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700234 if (0 != (ret = ensure_root_path_unmounted(root))) {
235 ui_print("Can't unmount %s!\n", mount_point);
236 return ret;
237 }
Koushik Dutta852bb422010-07-24 11:18:00 -0700238 */
Koushik Duttabf4444f2010-12-18 18:57:47 -0800239 if (0 != (ret = format_volume(mount_point))) {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800240 ui_print("Error while formatting %s!\n", mount_point);
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700241 return ret;
242 }
243
Koushik Duttadf1e4062010-12-18 17:42:31 -0800244 if (0 != (ret = ensure_path_mounted(mount_point))) {
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700245 ui_print("Can't mount %s!\n", mount_point);
246 return ret;
247 }
248
Koushik Dutta58131e72010-06-09 12:41:20 -0700249 if (0 != (ret = unyaffs(tmp, mount_point, callback))) {
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700250 ui_print("Error while restoring %s!\n", mount_point);
251 return ret;
252 }
Koushik Dutta2f73e582010-04-18 16:00:21 -0700253
Koushik Dutta062d6b02010-07-03 13:54:21 -0700254 if (umount_when_finished) {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800255 ensure_path_unmounted(mount_point);
Koushik Dutta2f73e582010-04-18 16:00:21 -0700256 }
Koushik Dutta062d6b02010-07-03 13:54:21 -0700257
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800258 return 0;
259}
260
Koushik Dutta062d6b02010-07-03 13:54:21 -0700261int nandroid_restore_partition(const char* backup_path, const char* root) {
262 return nandroid_restore_partition_extended(backup_path, root, 1);
263}
264
agrabren1f76a5d2010-12-29 12:15:18 -0600265int 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 -0800266{
267 ui_set_background(BACKGROUND_ICON_INSTALLING);
268 ui_show_indeterminate_progress();
269 yaffs_files_total = 0;
270
Koushik Duttadf1e4062010-12-18 17:42:31 -0800271 if (ensure_path_mounted("/sdcard") != 0)
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800272 return print_and_error("Can't mount /sdcard\n");
273
274 char tmp[PATH_MAX];
275
276 ui_print("Checking MD5 sums...\n");
Koushik K. Duttaface5882010-03-13 10:15:55 -0800277 sprintf(tmp, "cd %s && md5sum -c nandroid.md5", backup_path);
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800278 if (0 != __system(tmp))
279 return print_and_error("MD5 mismatch!\n");
280
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700281 int ret;
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700282#ifndef BOARD_RECOVERY_IGNORE_BOOTABLES
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700283 if (restore_boot)
284 {
Koushik K. Dutta84306552010-04-18 21:21:58 -0700285 ui_print("Erasing boot before restore...\n");
Koushik Duttadf1e4062010-12-18 17:42:31 -0800286 if (0 != (ret = format_device("boot")))
Koushik K. Dutta84306552010-04-18 21:21:58 -0700287 return print_and_error("Error while formatting BOOT:!\n");
Koushik Dutta28a41b42010-09-13 14:55:17 -0700288 sprintf(tmp, "%s/boot.img", backup_path);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700289 ui_print("Restoring boot image...\n");
Koushik Dutta19447c02010-11-10 10:40:44 -0800290 if (0 != (ret = restore_raw_partition("boot", tmp))) {
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700291 ui_print("Error while flashing boot image!");
292 return ret;
293 }
Koushik K. Duttaf7215942010-03-16 13:34:51 -0700294 }
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700295#endif
Koushik K. Duttaf7215942010-03-16 13:34:51 -0700296
agrabren1f76a5d2010-12-29 12:15:18 -0600297 if (restore_wimax && 0 == (ret = get_partition_device("wimax", tmp)))
298 {
299 sprintf(tmp, "%s/wimax.img", backup_path);
300
301 struct stat st;
302 if (0 != stat(tmp, &st))
303 {
304 ui_print("WARNING: Wimax partition exists, but nandroid\n");
305 ui_print(" backup does not contain wimax image.\n");
306 ui_print(" You should create a new backup to\n");
307 ui_print(" protect your wimax data.\n");
308 }
309 else
310 {
311 ui_print("Restoring wimax image...\n");
312 if (0 != (ret = restore_raw_partition("wimax", tmp)))
313 return ret;
314 }
315 }
316
Koushik Duttadf1e4062010-12-18 17:42:31 -0800317 if (restore_system && 0 != (ret = nandroid_restore_partition(backup_path, "/system")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700318 return ret;
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800319
Koushik Duttadf1e4062010-12-18 17:42:31 -0800320 if (restore_data && 0 != (ret = nandroid_restore_partition(backup_path, "/data")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700321 return ret;
Koushik Dutta8b5e1852010-06-14 22:04:22 -0700322
Koushik Dutta5460f0c2010-12-18 22:37:49 -0800323 if (has_datadata()) {
324 if (restore_data && 0 != (ret = nandroid_restore_partition(backup_path, "/datadata")))
325 return ret;
326 }
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800327
Koushik Duttadf1e4062010-12-18 17:42:31 -0800328 if (restore_data && 0 != (ret = nandroid_restore_partition_extended(backup_path, "/sdcard/.android_secure", 0)))
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700329 return ret;
Koushik Dutta062d6b02010-07-03 13:54:21 -0700330
Koushik Duttadf1e4062010-12-18 17:42:31 -0800331 if (restore_cache && 0 != (ret = nandroid_restore_partition_extended(backup_path, "/cache", 0)))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700332 return ret;
333
Koushik Duttadf1e4062010-12-18 17:42:31 -0800334 if (restore_sdext && 0 != (ret = nandroid_restore_partition(backup_path, "/sd-ext")))
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700335 return ret;
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700336
Koushik K. Dutta3f38f322010-03-12 23:45:25 -0800337 sync();
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800338 ui_set_background(BACKGROUND_ICON_NONE);
339 ui_reset_progress();
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700340 ui_print("\nRestore complete!\n");
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800341 return 0;
342}
Koushik Dutta08370912010-06-26 12:25:02 -0700343
344void nandroid_generate_timestamp_path(char* backup_path)
345{
346 time_t t = time(NULL);
347 struct tm *tmp = localtime(&t);
348 if (tmp == NULL)
349 {
350 struct timeval tp;
351 gettimeofday(&tp, NULL);
352 sprintf(backup_path, "/sdcard/clockworkmod/backup/%d", tp.tv_sec);
353 }
354 else
355 {
356 strftime(backup_path, PATH_MAX, "/sdcard/clockworkmod/backup/%F.%H.%M.%S", tmp);
357 }
358}
359
360int nandroid_usage()
361{
362 printf("Usage: nandroid backup\n");
363 printf("Usage: nandroid restore <directory>\n");
364 return 1;
365}
366
367int nandroid_main(int argc, char** argv)
368{
369 if (argc > 3 || argc < 2)
370 return nandroid_usage();
371
372 if (strcmp("backup", argv[1]) == 0)
373 {
374 if (argc != 2)
375 return nandroid_usage();
376
377 char backup_path[PATH_MAX];
378 nandroid_generate_timestamp_path(backup_path);
379 return nandroid_backup(backup_path);
380 }
381
382 if (strcmp("restore", argv[1]) == 0)
383 {
384 if (argc != 3)
385 return nandroid_usage();
agrabren1f76a5d2010-12-29 12:15:18 -0600386 return nandroid_restore(argv[2], 1, 1, 1, 1, 1, 1);
Koushik Dutta08370912010-06-26 12:25:02 -0700387 }
388
389 return nandroid_usage();
390}