blob: 39569c501b4c38e195389b67c3ec6129a6123344 [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 K. Dutta4b249cd2010-03-15 16:31:03 -070078int nandroid_backup_partition(char* backup_path, char* root, char* name) {
79 int ret = 0;
80 char mount_point[PATH_MAX];
81 sprintf(mount_point, "/%s", name);
82
83 ui_print("Backing up %s...\n", name);
84 if (0 != (ret = ensure_root_path_mounted(root) != 0)) {
85 ui_print("Can't mount %s!\n", mount_point);
86 return ret;
87 }
88 compute_directory_stats(mount_point);
89 char tmp[PATH_MAX];
90 sprintf(tmp, "%s/%s.img", backup_path, name);
91 ret = mkyaffs2image(mount_point, tmp, 0, yaffs_callback);
92 ensure_root_path_unmounted(root);
93 if (0 != ret) {
94 ui_print("Error while making a yaffs2 image of %s!\n", mount_point);
95 return ret;
96 }
97 return 0;
98}
99
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800100int nandroid_backup(char* backup_path)
101{
102 ui_set_background(BACKGROUND_ICON_INSTALLING);
103
104 if (ensure_root_path_mounted("SDCARD:") != 0)
105 return print_and_error("Can't mount /sdcard\n");
106
Koushik K. Dutta78686292010-03-25 17:58:45 -0700107 int ret;
108 struct statfs s;
109 if (0 != (ret = statfs("/sdcard", &s)))
110 return print_and_error("Unable to stat /sdcard\n");
Koushik K. Duttac0970932010-03-25 23:19:19 -0700111 long sdcard_free = s.f_bavail * s.f_bsize;
Koushik K. Dutta04159f72010-03-25 19:04:08 -0700112 if (sdcard_free < 150000000L)
Koushik K. Duttac0970932010-03-25 23:19:19 -0700113 ui_print("There may not be enough free space to complete backup... continuing...\n");
114 // return print_and_error("There is not enough free space on the SD Card! At least 150MB is required to create a backup.\n");
Koushik K. Dutta78686292010-03-25 17:58:45 -0700115
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800116 char tmp[PATH_MAX];
117 sprintf(tmp, "mkdir -p %s", backup_path);
118 __system(tmp);
119
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800120 ui_print("Backing up boot...\n");
121 sprintf(tmp, "%s/%s", backup_path, "boot.img");
122 ret = dump_image("boot", tmp, NULL);
123 if (0 != ret)
124 return print_and_error("Error while dumping boot image!\n");
125
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700126 if (0 != (ret = nandroid_backup_partition(backup_path, "SYSTEM:", "system")))
127 return ret;
128
129 if (0 != (ret = nandroid_backup_partition(backup_path, "DATA:", "data")))
130 return ret;
131
132 if (0 != (ret = nandroid_backup_partition(backup_path, "CACHE:", "cache")))
133 return ret;
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800134
Koushik K. Duttad7178922010-03-24 14:38:40 -0700135 ui_print("Generating md5 sum...\n");
136 sprintf(tmp, "cd %s && (md5sum *img > nandroid.md5)", backup_path);
Koushik K. Duttab0a25032010-03-24 10:34:38 -0700137 if (0 != (ret = __system(tmp))) {
138 ui_print("Error while generating md5 sum!\n");
139 return ret;
140 }
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800141
Koushik K. Dutta3f38f322010-03-12 23:45:25 -0800142 sync();
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800143 ui_set_background(BACKGROUND_ICON_NONE);
144 ui_reset_progress();
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700145 ui_print("\nBackup complete!\n");
146 return 0;
147}
148
149int nandroid_restore_partition(char* backup_path, char* root, char* name) {
150 int ret = 0;
151 char mount_point[PATH_MAX];
152 sprintf(mount_point, "/%s", name);
153
154 ui_print("Restoring %s...\n", name);
155 if (0 != (ret = ensure_root_path_unmounted(root))) {
156 ui_print("Can't unmount %s!\n", mount_point);
157 return ret;
158 }
159 if (0 != (ret = format_root_device(root))) {
160 ui_print("Error while formatting %s!\n", root);
161 return ret;
162 }
163
164 if (0 != (ret = ensure_root_path_mounted(root))) {
165 ui_print("Can't mount %s!\n", mount_point);
166 return ret;
167 }
168
169 char tmp[PATH_MAX];
170 sprintf(tmp, "%s/%s.img", backup_path, name);
171 if (0 != (ret = unyaffs(tmp, mount_point, yaffs_callback))) {
172 ui_print("Error while restoring %s!\n", mount_point);
173 return ret;
174 }
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800175 return 0;
176}
177
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700178int nandroid_restore(char* backup_path, int restore_boot, int restore_system, int restore_data, int restore_cache)
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800179{
180 ui_set_background(BACKGROUND_ICON_INSTALLING);
181 ui_show_indeterminate_progress();
182 yaffs_files_total = 0;
183
184 if (ensure_root_path_mounted("SDCARD:") != 0)
185 return print_and_error("Can't mount /sdcard\n");
186
187 char tmp[PATH_MAX];
188
189 ui_print("Checking MD5 sums...\n");
Koushik K. Duttaface5882010-03-13 10:15:55 -0800190 sprintf(tmp, "cd %s && md5sum -c nandroid.md5", backup_path);
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800191 if (0 != __system(tmp))
192 return print_and_error("MD5 mismatch!\n");
193
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700194 int ret;
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700195 if (restore_boot)
196 {
197 sprintf(tmp, "flash_image boot %s/boot.img", backup_path);
198 ui_print("Restoring boot image...\n");
199 if (0 != (ret = __system(tmp))) {
200 ui_print("Error while flashing boot image!");
201 return ret;
202 }
Koushik K. Duttaf7215942010-03-16 13:34:51 -0700203 }
204
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700205 if (restore_system && 0 != (ret = nandroid_restore_partition(backup_path, "SYSTEM:", "system")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700206 return ret;
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800207
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700208 if (restore_data && 0 != (ret = nandroid_restore_partition(backup_path, "DATA:", "data")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700209 return ret;
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800210
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700211 if (restore_cache && 0 != (ret = nandroid_restore_partition(backup_path, "CACHE:", "cache")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700212 return ret;
213
Koushik K. Dutta3f38f322010-03-12 23:45:25 -0800214 sync();
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800215 ui_set_background(BACKGROUND_ICON_NONE);
216 ui_reset_progress();
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700217 ui_print("\nRestore complete!\n");
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800218 return 0;
219}