blob: 9c683a35acabe398a2ead8335ceadff60104dca5 [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");
Koushik K. Dutta6923cc32010-03-29 14:46:00 -0700125
126 ui_print("Backing up recovery...\n");
127 sprintf(tmp, "%s/%s", backup_path, "recovery.img");
128 ret = dump_image("recovery", tmp, NULL);
129 if (0 != ret)
130 return print_and_error("Error while dumping boot image!\n");
131
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700132 if (0 != (ret = nandroid_backup_partition(backup_path, "SYSTEM:", "system")))
133 return ret;
134
135 if (0 != (ret = nandroid_backup_partition(backup_path, "DATA:", "data")))
136 return ret;
137
138 if (0 != (ret = nandroid_backup_partition(backup_path, "CACHE:", "cache")))
139 return ret;
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700140
141 if (0 != ensure_root_path_mounted("SDEXT:"))
142 ui_print("No sd-ext found. Skipping backup.\n");
143 else if (0 != (ret = nandroid_backup_partition(backup_path, "SDEXT:", "sd-ext")))
144 return ret;
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800145
Koushik K. Duttad7178922010-03-24 14:38:40 -0700146 ui_print("Generating md5 sum...\n");
147 sprintf(tmp, "cd %s && (md5sum *img > nandroid.md5)", backup_path);
Koushik K. Duttab0a25032010-03-24 10:34:38 -0700148 if (0 != (ret = __system(tmp))) {
149 ui_print("Error while generating md5 sum!\n");
150 return ret;
151 }
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800152
Koushik K. Dutta3f38f322010-03-12 23:45:25 -0800153 sync();
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800154 ui_set_background(BACKGROUND_ICON_NONE);
155 ui_reset_progress();
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700156 ui_print("\nBackup complete!\n");
157 return 0;
158}
159
160int nandroid_restore_partition(char* backup_path, char* root, char* name) {
161 int ret = 0;
162 char mount_point[PATH_MAX];
163 sprintf(mount_point, "/%s", name);
164
165 ui_print("Restoring %s...\n", name);
166 if (0 != (ret = ensure_root_path_unmounted(root))) {
167 ui_print("Can't unmount %s!\n", mount_point);
168 return ret;
169 }
170 if (0 != (ret = format_root_device(root))) {
171 ui_print("Error while formatting %s!\n", root);
172 return ret;
173 }
174
175 if (0 != (ret = ensure_root_path_mounted(root))) {
176 ui_print("Can't mount %s!\n", mount_point);
177 return ret;
178 }
179
180 char tmp[PATH_MAX];
181 sprintf(tmp, "%s/%s.img", backup_path, name);
182 if (0 != (ret = unyaffs(tmp, mount_point, yaffs_callback))) {
183 ui_print("Error while restoring %s!\n", mount_point);
184 return ret;
185 }
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800186 return 0;
187}
188
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700189int 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 -0800190{
191 ui_set_background(BACKGROUND_ICON_INSTALLING);
192 ui_show_indeterminate_progress();
193 yaffs_files_total = 0;
194
195 if (ensure_root_path_mounted("SDCARD:") != 0)
196 return print_and_error("Can't mount /sdcard\n");
197
198 char tmp[PATH_MAX];
199
200 ui_print("Checking MD5 sums...\n");
Koushik K. Duttaface5882010-03-13 10:15:55 -0800201 sprintf(tmp, "cd %s && md5sum -c nandroid.md5", backup_path);
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800202 if (0 != __system(tmp))
203 return print_and_error("MD5 mismatch!\n");
204
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700205 int ret;
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700206 if (restore_boot)
207 {
208 sprintf(tmp, "flash_image boot %s/boot.img", backup_path);
209 ui_print("Restoring boot image...\n");
210 if (0 != (ret = __system(tmp))) {
211 ui_print("Error while flashing boot image!");
212 return ret;
213 }
Koushik K. Duttaf7215942010-03-16 13:34:51 -0700214 }
215
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700216 if (restore_system && 0 != (ret = nandroid_restore_partition(backup_path, "SYSTEM:", "system")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700217 return ret;
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800218
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700219 if (restore_data && 0 != (ret = nandroid_restore_partition(backup_path, "DATA:", "data")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700220 return ret;
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800221
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700222 if (restore_cache && 0 != (ret = nandroid_restore_partition(backup_path, "CACHE:", "cache")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700223 return ret;
224
Koushik K. Dutta3f38f322010-03-12 23:45:25 -0800225 sync();
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800226 ui_set_background(BACKGROUND_ICON_NONE);
227 ui_reset_progress();
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700228 ui_print("\nRestore complete!\n");
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800229 return 0;
230}