blob: 6bf6e91c085d4e4ad1b86ec39922d4fc52b0f974 [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 Dutta28a41b42010-09-13 14:55:17 -070045#ifndef BOARD_USES_BMLUTILS
46int write_raw_image(const char* partition, const char* filename) {
47 char tmp[PATH_MAX];
48 sprintf(tmp, "flash_image boot %s", filename);
49 return __system(tmp);
50}
51#endif
52
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -070053int print_and_error(char* message) {
54 ui_print(message);
55 return 1;
56}
57
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -080058int yaffs_files_total = 0;
59int yaffs_files_count = 0;
60void yaffs_callback(char* filename)
61{
62 char* justfile = basename(filename);
63 if (strlen(justfile) < 30)
Koushik K. Dutta3fb8d302010-03-15 17:05:27 -070064 ui_print(justfile);
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -080065 yaffs_files_count++;
66 if (yaffs_files_total != 0)
67 ui_set_progress((float)yaffs_files_count / (float)yaffs_files_total);
68 ui_reset_text_col();
69}
70
71void compute_directory_stats(char* directory)
72{
73 char tmp[PATH_MAX];
74 sprintf(tmp, "find %s | wc -l > /tmp/dircount", directory);
75 __system(tmp);
76 char count_text[100];
77 FILE* f = fopen("/tmp/dircount", "r");
78 fread(count_text, 1, sizeof(count_text), f);
79 fclose(f);
80 yaffs_files_count = 0;
81 yaffs_files_total = atoi(count_text);
82 ui_reset_progress();
83 ui_show_progress(1, 0);
84}
85
Koushik Dutta062d6b02010-07-03 13:54:21 -070086int nandroid_backup_partition_extended(const char* backup_path, char* root, int umount_when_finished) {
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -070087 int ret = 0;
88 char mount_point[PATH_MAX];
Koushik K. Dutta68b01902010-04-01 12:20:39 -070089 translate_root_path(root, mount_point, PATH_MAX);
90 char* name = basename(mount_point);
Koushik Dutta58131e72010-06-09 12:41:20 -070091
92 struct stat file_info;
93 mkyaffs2image_callback callback = NULL;
94 if (0 != stat("/sdcard/clockworkmod/.hidenandroidprogress", &file_info)) {
95 callback = yaffs_callback;
96 }
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -070097
98 ui_print("Backing up %s...\n", name);
99 if (0 != (ret = ensure_root_path_mounted(root) != 0)) {
100 ui_print("Can't mount %s!\n", mount_point);
101 return ret;
102 }
103 compute_directory_stats(mount_point);
104 char tmp[PATH_MAX];
105 sprintf(tmp, "%s/%s.img", backup_path, name);
Koushik Dutta58131e72010-06-09 12:41:20 -0700106 ret = mkyaffs2image(mount_point, tmp, 0, callback);
Koushik Dutta062d6b02010-07-03 13:54:21 -0700107 if (umount_when_finished) {
108 ensure_root_path_unmounted(root);
109 }
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700110 if (0 != ret) {
111 ui_print("Error while making a yaffs2 image of %s!\n", mount_point);
112 return ret;
113 }
114 return 0;
115}
116
Koushik Dutta062d6b02010-07-03 13:54:21 -0700117int nandroid_backup_partition(const char* backup_path, char* root) {
118 return nandroid_backup_partition_extended(backup_path, root, 1);
119}
120
Koushik Dutta2f73e582010-04-18 16:00:21 -0700121int nandroid_backup(const char* backup_path)
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800122{
123 ui_set_background(BACKGROUND_ICON_INSTALLING);
124
125 if (ensure_root_path_mounted("SDCARD:") != 0)
126 return print_and_error("Can't mount /sdcard\n");
127
Koushik K. Dutta78686292010-03-25 17:58:45 -0700128 int ret;
Koushik K. Duttaf3534d02010-04-18 18:06:24 -0700129 struct statfs s;
Koushik K. Dutta78686292010-03-25 17:58:45 -0700130 if (0 != (ret = statfs("/sdcard", &s)))
131 return print_and_error("Unable to stat /sdcard\n");
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700132 uint64_t bavail = s.f_bavail;
133 uint64_t bsize = s.f_bsize;
134 uint64_t sdcard_free = bavail * bsize;
135 uint64_t sdcard_free_mb = sdcard_free / (uint64_t)(1024 * 1024);
136 ui_print("SD Card space free: %lluMB\n", sdcard_free_mb);
137 if (sdcard_free_mb < 150)
Koushik K. Duttac0970932010-03-25 23:19:19 -0700138 ui_print("There may not be enough free space to complete backup... continuing...\n");
Koushik K. Dutta78686292010-03-25 17:58:45 -0700139
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800140 char tmp[PATH_MAX];
141 sprintf(tmp, "mkdir -p %s", backup_path);
142 __system(tmp);
143
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700144#ifndef BOARD_RECOVERY_IGNORE_BOOTABLES
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800145 ui_print("Backing up boot...\n");
146 sprintf(tmp, "%s/%s", backup_path, "boot.img");
147 ret = dump_image("boot", tmp, NULL);
148 if (0 != ret)
149 return print_and_error("Error while dumping boot image!\n");
Koushik K. Dutta6923cc32010-03-29 14:46:00 -0700150
151 ui_print("Backing up recovery...\n");
152 sprintf(tmp, "%s/%s", backup_path, "recovery.img");
153 ret = dump_image("recovery", tmp, NULL);
154 if (0 != ret)
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700155 return print_and_error("Error while dumping recovery image!\n");
156#endif
Koushik K. Dutta6923cc32010-03-29 14:46:00 -0700157
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700158 if (0 != (ret = nandroid_backup_partition(backup_path, "SYSTEM:")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700159 return ret;
160
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700161 if (0 != (ret = nandroid_backup_partition(backup_path, "DATA:")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700162 return ret;
163
Koushik Dutta8b5e1852010-06-14 22:04:22 -0700164#ifdef HAS_DATADATA
165 if (0 != (ret = nandroid_backup_partition(backup_path, "DATADATA:")))
166 return ret;
167#endif
168
Koushik Dutta062d6b02010-07-03 13:54:21 -0700169 struct stat st;
170 if (0 != stat("/sdcard/.android_secure", &st))
171 {
172 ui_print("No /sdcard/.android_secure found. Skipping backup of applications on external storage.\n");
173 }
174 else
175 {
176 if (0 != (ret = nandroid_backup_partition_extended(backup_path, "SDCARD:/.android_secure", 0)))
177 return ret;
178 }
179
180 if (0 != (ret = nandroid_backup_partition_extended(backup_path, "CACHE:", 0)))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700181 return ret;
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700182
Koushik Dutta52d3f202010-06-21 12:11:13 -0700183 if (0 != stat(SDEXT_DEVICE, &st))
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700184 {
Koushik K. Dutta71ef11f2010-04-02 22:17:42 -0700185 ui_print("No sd-ext found. Skipping backup of sd-ext.\n");
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700186 }
187 else
188 {
189 if (0 != ensure_root_path_mounted("SDEXT:"))
Koushik Dutta8b5e1852010-06-14 22:04:22 -0700190 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 -0700191 else if (0 != (ret = nandroid_backup_partition(backup_path, "SDEXT:")))
192 return ret;
193 }
Koushik Dutta062d6b02010-07-03 13:54:21 -0700194
Koushik K. Duttad7178922010-03-24 14:38:40 -0700195 ui_print("Generating md5 sum...\n");
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700196 sprintf(tmp, "nandroid-md5.sh %s", backup_path);
Koushik K. Duttab0a25032010-03-24 10:34:38 -0700197 if (0 != (ret = __system(tmp))) {
198 ui_print("Error while generating md5 sum!\n");
199 return ret;
200 }
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800201
Koushik K. Dutta3f38f322010-03-12 23:45:25 -0800202 sync();
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800203 ui_set_background(BACKGROUND_ICON_NONE);
204 ui_reset_progress();
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700205 ui_print("\nBackup complete!\n");
206 return 0;
207}
208
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700209typedef int (*format_function)(char* root);
210
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700211static void ensure_directory(const char* dir) {
212 char tmp[PATH_MAX];
213 sprintf(tmp, "mkdir -p %s", dir);
214 __system(tmp);
215}
216
Koushik Dutta062d6b02010-07-03 13:54:21 -0700217int nandroid_restore_partition_extended(const char* backup_path, const char* root, int umount_when_finished) {
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700218 int ret = 0;
219 char mount_point[PATH_MAX];
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700220 translate_root_path(root, mount_point, PATH_MAX);
221 char* name = basename(mount_point);
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700222
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700223 char tmp[PATH_MAX];
224 sprintf(tmp, "%s/%s.img", backup_path, name);
Koushik Dutta58131e72010-06-09 12:41:20 -0700225 struct stat file_info;
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700226 if (0 != (ret = statfs(tmp, &file_info))) {
Koushik Duttad4060c32010-07-22 20:14:44 -0700227 ui_print("%s.img not found. Skipping restore of %s.\n", name, mount_point);
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700228 return 0;
229 }
230
231 ensure_directory(mount_point);
232
Koushik Dutta58131e72010-06-09 12:41:20 -0700233 unyaffs_callback callback = NULL;
234 if (0 != stat("/sdcard/clockworkmod/.hidenandroidprogress", &file_info)) {
235 callback = yaffs_callback;
236 }
237
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700238 ui_print("Restoring %s...\n", name);
Koushik Dutta852bb422010-07-24 11:18:00 -0700239 /*
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700240 if (0 != (ret = ensure_root_path_unmounted(root))) {
241 ui_print("Can't unmount %s!\n", mount_point);
242 return ret;
243 }
Koushik Dutta852bb422010-07-24 11:18:00 -0700244 */
Koushik Dutta2f73e582010-04-18 16:00:21 -0700245 if (0 != (ret = format_root_device(root))) {
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700246 ui_print("Error while formatting %s!\n", root);
247 return ret;
248 }
249
250 if (0 != (ret = ensure_root_path_mounted(root))) {
251 ui_print("Can't mount %s!\n", mount_point);
252 return ret;
253 }
254
Koushik Dutta58131e72010-06-09 12:41:20 -0700255 if (0 != (ret = unyaffs(tmp, mount_point, callback))) {
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700256 ui_print("Error while restoring %s!\n", mount_point);
257 return ret;
258 }
Koushik Dutta2f73e582010-04-18 16:00:21 -0700259
Koushik Dutta062d6b02010-07-03 13:54:21 -0700260 if (umount_when_finished) {
Koushik Dutta2f73e582010-04-18 16:00:21 -0700261 ensure_root_path_unmounted(root);
262 }
Koushik Dutta062d6b02010-07-03 13:54:21 -0700263
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800264 return 0;
265}
266
Koushik Dutta062d6b02010-07-03 13:54:21 -0700267int nandroid_restore_partition(const char* backup_path, const char* root) {
268 return nandroid_restore_partition_extended(backup_path, root, 1);
269}
270
Koushik Dutta2f73e582010-04-18 16:00:21 -0700271int 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 -0800272{
273 ui_set_background(BACKGROUND_ICON_INSTALLING);
274 ui_show_indeterminate_progress();
275 yaffs_files_total = 0;
276
277 if (ensure_root_path_mounted("SDCARD:") != 0)
278 return print_and_error("Can't mount /sdcard\n");
279
280 char tmp[PATH_MAX];
281
282 ui_print("Checking MD5 sums...\n");
Koushik K. Duttaface5882010-03-13 10:15:55 -0800283 sprintf(tmp, "cd %s && md5sum -c nandroid.md5", backup_path);
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800284 if (0 != __system(tmp))
285 return print_and_error("MD5 mismatch!\n");
286
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700287 int ret;
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700288#ifndef BOARD_RECOVERY_IGNORE_BOOTABLES
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700289 if (restore_boot)
290 {
Koushik K. Dutta84306552010-04-18 21:21:58 -0700291 ui_print("Erasing boot before restore...\n");
292 if (0 != (ret = format_root_device("BOOT:")))
293 return print_and_error("Error while formatting BOOT:!\n");
Koushik Dutta28a41b42010-09-13 14:55:17 -0700294 sprintf(tmp, "%s/boot.img", backup_path);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700295 ui_print("Restoring boot image...\n");
Koushik Dutta28a41b42010-09-13 14:55:17 -0700296 if (0 != (ret = write_raw_image("boot", tmp))) {
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700297 ui_print("Error while flashing boot image!");
298 return ret;
299 }
Koushik K. Duttaf7215942010-03-16 13:34:51 -0700300 }
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700301#endif
Koushik K. Duttaf7215942010-03-16 13:34:51 -0700302
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700303 if (restore_system && 0 != (ret = nandroid_restore_partition(backup_path, "SYSTEM:")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700304 return ret;
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800305
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700306 if (restore_data && 0 != (ret = nandroid_restore_partition(backup_path, "DATA:")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700307 return ret;
Koushik Dutta8b5e1852010-06-14 22:04:22 -0700308
309#ifdef HAS_DATADATA
310 if (restore_data && 0 != (ret = nandroid_restore_partition(backup_path, "DATADATA:")))
311 return ret;
312#endif
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800313
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700314 if (restore_data && 0 != (ret = nandroid_restore_partition_extended(backup_path, "SDCARD:/.android_secure", 0)))
315 return ret;
Koushik Dutta062d6b02010-07-03 13:54:21 -0700316
317 if (restore_cache && 0 != (ret = nandroid_restore_partition_extended(backup_path, "CACHE:", 0)))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700318 return ret;
319
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700320 if (restore_sdext && 0 != (ret = nandroid_restore_partition(backup_path, "SDEXT:")))
321 return ret;
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700322
Koushik K. Dutta3f38f322010-03-12 23:45:25 -0800323 sync();
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800324 ui_set_background(BACKGROUND_ICON_NONE);
325 ui_reset_progress();
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700326 ui_print("\nRestore complete!\n");
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800327 return 0;
328}
Koushik Dutta08370912010-06-26 12:25:02 -0700329
330void nandroid_generate_timestamp_path(char* backup_path)
331{
332 time_t t = time(NULL);
333 struct tm *tmp = localtime(&t);
334 if (tmp == NULL)
335 {
336 struct timeval tp;
337 gettimeofday(&tp, NULL);
338 sprintf(backup_path, "/sdcard/clockworkmod/backup/%d", tp.tv_sec);
339 }
340 else
341 {
342 strftime(backup_path, PATH_MAX, "/sdcard/clockworkmod/backup/%F.%H.%M.%S", tmp);
343 }
344}
345
346int nandroid_usage()
347{
348 printf("Usage: nandroid backup\n");
349 printf("Usage: nandroid restore <directory>\n");
350 return 1;
351}
352
353int nandroid_main(int argc, char** argv)
354{
355 if (argc > 3 || argc < 2)
356 return nandroid_usage();
357
358 if (strcmp("backup", argv[1]) == 0)
359 {
360 if (argc != 2)
361 return nandroid_usage();
362
363 char backup_path[PATH_MAX];
364 nandroid_generate_timestamp_path(backup_path);
365 return nandroid_backup(backup_path);
366 }
367
368 if (strcmp("restore", argv[1]) == 0)
369 {
370 if (argc != 3)
371 return nandroid_usage();
372 return nandroid_restore(argv[2], 1, 1, 1, 1, 1);
373 }
374
375 return nandroid_usage();
376}