blob: 0b3829403ad4cc84f211a1741c4ac559344e0171 [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
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -080036#include "../../external/yaffs2/yaffs2/utils/mkyaffs2image.h"
37#include "../../external/yaffs2/yaffs2/utils/unyaffs.h"
38
Koushik K. Dutta78686292010-03-25 17:58:45 -070039#include <sys/vfs.h>
40
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -080041#include "extendedcommands.h"
42#include "nandroid.h"
43
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -070044int print_and_error(char* message) {
45 ui_print(message);
46 return 1;
47}
48
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -080049int yaffs_files_total = 0;
50int yaffs_files_count = 0;
51void yaffs_callback(char* filename)
52{
53 char* justfile = basename(filename);
54 if (strlen(justfile) < 30)
Koushik K. Dutta3fb8d302010-03-15 17:05:27 -070055 ui_print(justfile);
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -080056 yaffs_files_count++;
57 if (yaffs_files_total != 0)
58 ui_set_progress((float)yaffs_files_count / (float)yaffs_files_total);
59 ui_reset_text_col();
60}
61
62void compute_directory_stats(char* directory)
63{
64 char tmp[PATH_MAX];
65 sprintf(tmp, "find %s | wc -l > /tmp/dircount", directory);
66 __system(tmp);
67 char count_text[100];
68 FILE* f = fopen("/tmp/dircount", "r");
69 fread(count_text, 1, sizeof(count_text), f);
70 fclose(f);
71 yaffs_files_count = 0;
72 yaffs_files_total = atoi(count_text);
73 ui_reset_progress();
74 ui_show_progress(1, 0);
75}
76
Koushik Dutta062d6b02010-07-03 13:54:21 -070077int nandroid_backup_partition_extended(const char* backup_path, char* root, int umount_when_finished) {
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -070078 int ret = 0;
79 char mount_point[PATH_MAX];
Koushik K. Dutta68b01902010-04-01 12:20:39 -070080 translate_root_path(root, mount_point, PATH_MAX);
81 char* name = basename(mount_point);
Koushik Dutta58131e72010-06-09 12:41:20 -070082
83 struct stat file_info;
84 mkyaffs2image_callback callback = NULL;
85 if (0 != stat("/sdcard/clockworkmod/.hidenandroidprogress", &file_info)) {
86 callback = yaffs_callback;
87 }
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -070088
89 ui_print("Backing up %s...\n", name);
90 if (0 != (ret = ensure_root_path_mounted(root) != 0)) {
91 ui_print("Can't mount %s!\n", mount_point);
92 return ret;
93 }
94 compute_directory_stats(mount_point);
95 char tmp[PATH_MAX];
96 sprintf(tmp, "%s/%s.img", backup_path, name);
Koushik Dutta58131e72010-06-09 12:41:20 -070097 ret = mkyaffs2image(mount_point, tmp, 0, callback);
Koushik Dutta062d6b02010-07-03 13:54:21 -070098 if (umount_when_finished) {
99 ensure_root_path_unmounted(root);
100 }
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700101 if (0 != ret) {
102 ui_print("Error while making a yaffs2 image of %s!\n", mount_point);
103 return ret;
104 }
105 return 0;
106}
107
Koushik Dutta062d6b02010-07-03 13:54:21 -0700108int nandroid_backup_partition(const char* backup_path, char* root) {
109 return nandroid_backup_partition_extended(backup_path, root, 1);
110}
111
Koushik Dutta2f73e582010-04-18 16:00:21 -0700112int nandroid_backup(const char* backup_path)
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800113{
114 ui_set_background(BACKGROUND_ICON_INSTALLING);
115
116 if (ensure_root_path_mounted("SDCARD:") != 0)
117 return print_and_error("Can't mount /sdcard\n");
118
Koushik K. Dutta78686292010-03-25 17:58:45 -0700119 int ret;
Koushik K. Duttaf3534d02010-04-18 18:06:24 -0700120 struct statfs s;
Koushik K. Dutta78686292010-03-25 17:58:45 -0700121 if (0 != (ret = statfs("/sdcard", &s)))
122 return print_and_error("Unable to stat /sdcard\n");
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700123 uint64_t bavail = s.f_bavail;
124 uint64_t bsize = s.f_bsize;
125 uint64_t sdcard_free = bavail * bsize;
126 uint64_t sdcard_free_mb = sdcard_free / (uint64_t)(1024 * 1024);
127 ui_print("SD Card space free: %lluMB\n", sdcard_free_mb);
128 if (sdcard_free_mb < 150)
Koushik K. Duttac0970932010-03-25 23:19:19 -0700129 ui_print("There may not be enough free space to complete backup... continuing...\n");
Koushik K. Dutta78686292010-03-25 17:58:45 -0700130
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800131 char tmp[PATH_MAX];
132 sprintf(tmp, "mkdir -p %s", backup_path);
133 __system(tmp);
134
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700135#ifndef BOARD_RECOVERY_IGNORE_BOOTABLES
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800136 ui_print("Backing up boot...\n");
137 sprintf(tmp, "%s/%s", backup_path, "boot.img");
Koushik Dutta19447c02010-11-10 10:40:44 -0800138 ret = backup_raw_partition("boot", tmp);
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800139 if (0 != ret)
140 return print_and_error("Error while dumping boot image!\n");
Koushik K. Dutta6923cc32010-03-29 14:46:00 -0700141
142 ui_print("Backing up recovery...\n");
143 sprintf(tmp, "%s/%s", backup_path, "recovery.img");
Koushik Dutta19447c02010-11-10 10:40:44 -0800144 ret = backup_raw_partition("recovery", tmp);
Koushik K. Dutta6923cc32010-03-29 14:46:00 -0700145 if (0 != ret)
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700146 return print_and_error("Error while dumping recovery image!\n");
147#endif
Koushik K. Dutta6923cc32010-03-29 14:46:00 -0700148
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700149 if (0 != (ret = nandroid_backup_partition(backup_path, "SYSTEM:")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700150 return ret;
151
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700152 if (0 != (ret = nandroid_backup_partition(backup_path, "DATA:")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700153 return ret;
154
Koushik Dutta19447c02010-11-10 10:40:44 -0800155#ifdef BOARD_HAS_DATADATA
Koushik Dutta8b5e1852010-06-14 22:04:22 -0700156 if (0 != (ret = nandroid_backup_partition(backup_path, "DATADATA:")))
157 return ret;
158#endif
159
Koushik Dutta062d6b02010-07-03 13:54:21 -0700160 struct stat st;
161 if (0 != stat("/sdcard/.android_secure", &st))
162 {
163 ui_print("No /sdcard/.android_secure found. Skipping backup of applications on external storage.\n");
164 }
165 else
166 {
167 if (0 != (ret = nandroid_backup_partition_extended(backup_path, "SDCARD:/.android_secure", 0)))
168 return ret;
169 }
170
171 if (0 != (ret = nandroid_backup_partition_extended(backup_path, "CACHE:", 0)))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700172 return ret;
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700173
Koushik Dutta19447c02010-11-10 10:40:44 -0800174 if (0 != stat(BOARD_SDEXT_DEVICE, &st))
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700175 {
Koushik K. Dutta71ef11f2010-04-02 22:17:42 -0700176 ui_print("No sd-ext found. Skipping backup of sd-ext.\n");
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700177 }
178 else
179 {
180 if (0 != ensure_root_path_mounted("SDEXT:"))
Koushik Dutta8b5e1852010-06-14 22:04:22 -0700181 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 -0700182 else if (0 != (ret = nandroid_backup_partition(backup_path, "SDEXT:")))
183 return ret;
184 }
Koushik Dutta062d6b02010-07-03 13:54:21 -0700185
Koushik K. Duttad7178922010-03-24 14:38:40 -0700186 ui_print("Generating md5 sum...\n");
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700187 sprintf(tmp, "nandroid-md5.sh %s", backup_path);
Koushik K. Duttab0a25032010-03-24 10:34:38 -0700188 if (0 != (ret = __system(tmp))) {
189 ui_print("Error while generating md5 sum!\n");
190 return ret;
191 }
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800192
Koushik K. Dutta3f38f322010-03-12 23:45:25 -0800193 sync();
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800194 ui_set_background(BACKGROUND_ICON_NONE);
195 ui_reset_progress();
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700196 ui_print("\nBackup complete!\n");
197 return 0;
198}
199
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700200typedef int (*format_function)(char* root);
201
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700202static void ensure_directory(const char* dir) {
203 char tmp[PATH_MAX];
204 sprintf(tmp, "mkdir -p %s", dir);
205 __system(tmp);
206}
207
Koushik Dutta062d6b02010-07-03 13:54:21 -0700208int nandroid_restore_partition_extended(const char* backup_path, const char* root, int umount_when_finished) {
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700209 int ret = 0;
210 char mount_point[PATH_MAX];
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700211 translate_root_path(root, mount_point, PATH_MAX);
212 char* name = basename(mount_point);
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700213
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700214 char tmp[PATH_MAX];
215 sprintf(tmp, "%s/%s.img", backup_path, name);
Koushik Dutta58131e72010-06-09 12:41:20 -0700216 struct stat file_info;
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700217 if (0 != (ret = statfs(tmp, &file_info))) {
Koushik Duttad4060c32010-07-22 20:14:44 -0700218 ui_print("%s.img not found. Skipping restore of %s.\n", name, mount_point);
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700219 return 0;
220 }
221
222 ensure_directory(mount_point);
223
Koushik Dutta58131e72010-06-09 12:41:20 -0700224 unyaffs_callback callback = NULL;
225 if (0 != stat("/sdcard/clockworkmod/.hidenandroidprogress", &file_info)) {
226 callback = yaffs_callback;
227 }
228
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700229 ui_print("Restoring %s...\n", name);
Koushik Dutta852bb422010-07-24 11:18:00 -0700230 /*
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700231 if (0 != (ret = ensure_root_path_unmounted(root))) {
232 ui_print("Can't unmount %s!\n", mount_point);
233 return ret;
234 }
Koushik Dutta852bb422010-07-24 11:18:00 -0700235 */
Koushik Dutta2f73e582010-04-18 16:00:21 -0700236 if (0 != (ret = format_root_device(root))) {
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700237 ui_print("Error while formatting %s!\n", root);
238 return ret;
239 }
240
241 if (0 != (ret = ensure_root_path_mounted(root))) {
242 ui_print("Can't mount %s!\n", mount_point);
243 return ret;
244 }
245
Koushik Dutta58131e72010-06-09 12:41:20 -0700246 if (0 != (ret = unyaffs(tmp, mount_point, callback))) {
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700247 ui_print("Error while restoring %s!\n", mount_point);
248 return ret;
249 }
Koushik Dutta2f73e582010-04-18 16:00:21 -0700250
Koushik Dutta062d6b02010-07-03 13:54:21 -0700251 if (umount_when_finished) {
Koushik Dutta2f73e582010-04-18 16:00:21 -0700252 ensure_root_path_unmounted(root);
253 }
Koushik Dutta062d6b02010-07-03 13:54:21 -0700254
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800255 return 0;
256}
257
Koushik Dutta062d6b02010-07-03 13:54:21 -0700258int nandroid_restore_partition(const char* backup_path, const char* root) {
259 return nandroid_restore_partition_extended(backup_path, root, 1);
260}
261
Koushik Dutta2f73e582010-04-18 16:00:21 -0700262int 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 -0800263{
264 ui_set_background(BACKGROUND_ICON_INSTALLING);
265 ui_show_indeterminate_progress();
266 yaffs_files_total = 0;
267
268 if (ensure_root_path_mounted("SDCARD:") != 0)
269 return print_and_error("Can't mount /sdcard\n");
270
271 char tmp[PATH_MAX];
272
273 ui_print("Checking MD5 sums...\n");
Koushik K. Duttaface5882010-03-13 10:15:55 -0800274 sprintf(tmp, "cd %s && md5sum -c nandroid.md5", backup_path);
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800275 if (0 != __system(tmp))
276 return print_and_error("MD5 mismatch!\n");
277
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700278 int ret;
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700279#ifndef BOARD_RECOVERY_IGNORE_BOOTABLES
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700280 if (restore_boot)
281 {
Koushik K. Dutta84306552010-04-18 21:21:58 -0700282 ui_print("Erasing boot before restore...\n");
283 if (0 != (ret = format_root_device("BOOT:")))
284 return print_and_error("Error while formatting BOOT:!\n");
Koushik Dutta28a41b42010-09-13 14:55:17 -0700285 sprintf(tmp, "%s/boot.img", backup_path);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700286 ui_print("Restoring boot image...\n");
Koushik Dutta19447c02010-11-10 10:40:44 -0800287 if (0 != (ret = restore_raw_partition("boot", tmp))) {
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700288 ui_print("Error while flashing boot image!");
289 return ret;
290 }
Koushik K. Duttaf7215942010-03-16 13:34:51 -0700291 }
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700292#endif
Koushik K. Duttaf7215942010-03-16 13:34:51 -0700293
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700294 if (restore_system && 0 != (ret = nandroid_restore_partition(backup_path, "SYSTEM:")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700295 return ret;
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800296
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700297 if (restore_data && 0 != (ret = nandroid_restore_partition(backup_path, "DATA:")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700298 return ret;
Koushik Dutta8b5e1852010-06-14 22:04:22 -0700299
Koushik Dutta19447c02010-11-10 10:40:44 -0800300#ifdef BOARD_HAS_DATADATA
Koushik Dutta8b5e1852010-06-14 22:04:22 -0700301 if (restore_data && 0 != (ret = nandroid_restore_partition(backup_path, "DATADATA:")))
302 return ret;
303#endif
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800304
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700305 if (restore_data && 0 != (ret = nandroid_restore_partition_extended(backup_path, "SDCARD:/.android_secure", 0)))
306 return ret;
Koushik Dutta062d6b02010-07-03 13:54:21 -0700307
308 if (restore_cache && 0 != (ret = nandroid_restore_partition_extended(backup_path, "CACHE:", 0)))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700309 return ret;
310
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700311 if (restore_sdext && 0 != (ret = nandroid_restore_partition(backup_path, "SDEXT:")))
312 return ret;
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700313
Koushik K. Dutta3f38f322010-03-12 23:45:25 -0800314 sync();
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800315 ui_set_background(BACKGROUND_ICON_NONE);
316 ui_reset_progress();
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700317 ui_print("\nRestore complete!\n");
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800318 return 0;
319}
Koushik Dutta08370912010-06-26 12:25:02 -0700320
321void nandroid_generate_timestamp_path(char* backup_path)
322{
323 time_t t = time(NULL);
324 struct tm *tmp = localtime(&t);
325 if (tmp == NULL)
326 {
327 struct timeval tp;
328 gettimeofday(&tp, NULL);
329 sprintf(backup_path, "/sdcard/clockworkmod/backup/%d", tp.tv_sec);
330 }
331 else
332 {
333 strftime(backup_path, PATH_MAX, "/sdcard/clockworkmod/backup/%F.%H.%M.%S", tmp);
334 }
335}
336
337int nandroid_usage()
338{
339 printf("Usage: nandroid backup\n");
340 printf("Usage: nandroid restore <directory>\n");
341 return 1;
342}
343
344int nandroid_main(int argc, char** argv)
345{
346 if (argc > 3 || argc < 2)
347 return nandroid_usage();
348
349 if (strcmp("backup", argv[1]) == 0)
350 {
351 if (argc != 2)
352 return nandroid_usage();
353
354 char backup_path[PATH_MAX];
355 nandroid_generate_timestamp_path(backup_path);
356 return nandroid_backup(backup_path);
357 }
358
359 if (strcmp("restore", argv[1]) == 0)
360 {
361 if (argc != 3)
362 return nandroid_usage();
363 return nandroid_restore(argv[2], 1, 1, 1, 1, 1);
364 }
365
366 return nandroid_usage();
367}