blob: 763ce5b01091e8c2ed5520c5a2019fd541d1048f [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 Dutta2f73e582010-04-18 16:00:21 -070078int nandroid_backup_partition(const char* backup_path, char* root) {
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -070079 int ret = 0;
80 char mount_point[PATH_MAX];
Koushik K. Dutta68b01902010-04-01 12:20:39 -070081 translate_root_path(root, mount_point, PATH_MAX);
82 char* name = basename(mount_point);
Koushik Dutta58131e72010-06-09 12:41:20 -070083
84 struct stat file_info;
85 mkyaffs2image_callback callback = NULL;
86 if (0 != stat("/sdcard/clockworkmod/.hidenandroidprogress", &file_info)) {
87 callback = yaffs_callback;
88 }
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -070089
90 ui_print("Backing up %s...\n", name);
91 if (0 != (ret = ensure_root_path_mounted(root) != 0)) {
92 ui_print("Can't mount %s!\n", mount_point);
93 return ret;
94 }
95 compute_directory_stats(mount_point);
96 char tmp[PATH_MAX];
97 sprintf(tmp, "%s/%s.img", backup_path, name);
Koushik Dutta58131e72010-06-09 12:41:20 -070098 ret = mkyaffs2image(mount_point, tmp, 0, callback);
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -070099 ensure_root_path_unmounted(root);
100 if (0 != ret) {
101 ui_print("Error while making a yaffs2 image of %s!\n", mount_point);
102 return ret;
103 }
104 return 0;
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
111 if (ensure_root_path_mounted("SDCARD:") != 0)
112 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 K. Duttaee57bbc2010-03-12 23:21:12 -0800130 ui_print("Backing up boot...\n");
131 sprintf(tmp, "%s/%s", backup_path, "boot.img");
132 ret = dump_image("boot", tmp, NULL);
133 if (0 != ret)
134 return print_and_error("Error while dumping boot image!\n");
Koushik K. Dutta6923cc32010-03-29 14:46:00 -0700135
136 ui_print("Backing up recovery...\n");
137 sprintf(tmp, "%s/%s", backup_path, "recovery.img");
138 ret = dump_image("recovery", tmp, NULL);
139 if (0 != ret)
140 return print_and_error("Error while dumping boot image!\n");
141
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700142 if (0 != (ret = nandroid_backup_partition(backup_path, "SYSTEM:")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700143 return ret;
144
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700145 if (0 != (ret = nandroid_backup_partition(backup_path, "DATA:")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700146 return ret;
147
Koushik Dutta8b5e1852010-06-14 22:04:22 -0700148#ifdef HAS_DATADATA
149 if (0 != (ret = nandroid_backup_partition(backup_path, "DATADATA:")))
150 return ret;
151#endif
152
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700153 if (0 != (ret = nandroid_backup_partition(backup_path, "CACHE:")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700154 return ret;
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700155
Koushik K. Dutta71ef11f2010-04-02 22:17:42 -0700156 struct stat st;
Koushik Dutta52d3f202010-06-21 12:11:13 -0700157 if (0 != stat(SDEXT_DEVICE, &st))
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700158 {
Koushik K. Dutta71ef11f2010-04-02 22:17:42 -0700159 ui_print("No sd-ext found. Skipping backup of sd-ext.\n");
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700160 }
161 else
162 {
163 if (0 != ensure_root_path_mounted("SDEXT:"))
Koushik Dutta8b5e1852010-06-14 22:04:22 -0700164 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 -0700165 else if (0 != (ret = nandroid_backup_partition(backup_path, "SDEXT:")))
166 return ret;
167 }
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800168
Koushik K. Duttad7178922010-03-24 14:38:40 -0700169 ui_print("Generating md5 sum...\n");
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700170 sprintf(tmp, "nandroid-md5.sh %s", backup_path);
Koushik K. Duttab0a25032010-03-24 10:34:38 -0700171 if (0 != (ret = __system(tmp))) {
172 ui_print("Error while generating md5 sum!\n");
173 return ret;
174 }
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800175
Koushik K. Dutta3f38f322010-03-12 23:45:25 -0800176 sync();
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800177 ui_set_background(BACKGROUND_ICON_NONE);
178 ui_reset_progress();
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700179 ui_print("\nBackup complete!\n");
180 return 0;
181}
182
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700183typedef int (*format_function)(char* root);
184
Koushik Dutta2f73e582010-04-18 16:00:21 -0700185int nandroid_restore_partition(const char* backup_path, const char* root) {
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700186 int ret = 0;
187 char mount_point[PATH_MAX];
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700188 translate_root_path(root, mount_point, PATH_MAX);
189 char* name = basename(mount_point);
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700190
Koushik Dutta58131e72010-06-09 12:41:20 -0700191 struct stat file_info;
192 unyaffs_callback callback = NULL;
193 if (0 != stat("/sdcard/clockworkmod/.hidenandroidprogress", &file_info)) {
194 callback = yaffs_callback;
195 }
196
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700197 ui_print("Restoring %s...\n", name);
198 if (0 != (ret = ensure_root_path_unmounted(root))) {
199 ui_print("Can't unmount %s!\n", mount_point);
200 return ret;
201 }
Koushik Dutta2f73e582010-04-18 16:00:21 -0700202 if (0 != (ret = format_root_device(root))) {
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700203 ui_print("Error while formatting %s!\n", root);
204 return ret;
205 }
206
207 if (0 != (ret = ensure_root_path_mounted(root))) {
208 ui_print("Can't mount %s!\n", mount_point);
209 return ret;
210 }
211
212 char tmp[PATH_MAX];
213 sprintf(tmp, "%s/%s.img", backup_path, name);
Koushik Dutta58131e72010-06-09 12:41:20 -0700214 if (0 != (ret = unyaffs(tmp, mount_point, callback))) {
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700215 ui_print("Error while restoring %s!\n", mount_point);
216 return ret;
217 }
Koushik Dutta2f73e582010-04-18 16:00:21 -0700218
219 if (0 != strcmp(root, "CACHE")) {
220 ensure_root_path_unmounted(root);
221 }
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800222 return 0;
223}
224
Koushik Dutta2f73e582010-04-18 16:00:21 -0700225int 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 -0800226{
227 ui_set_background(BACKGROUND_ICON_INSTALLING);
228 ui_show_indeterminate_progress();
229 yaffs_files_total = 0;
230
231 if (ensure_root_path_mounted("SDCARD:") != 0)
232 return print_and_error("Can't mount /sdcard\n");
233
234 char tmp[PATH_MAX];
235
236 ui_print("Checking MD5 sums...\n");
Koushik K. Duttaface5882010-03-13 10:15:55 -0800237 sprintf(tmp, "cd %s && md5sum -c nandroid.md5", backup_path);
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800238 if (0 != __system(tmp))
239 return print_and_error("MD5 mismatch!\n");
240
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700241 int ret;
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700242 if (restore_boot)
243 {
Koushik K. Dutta84306552010-04-18 21:21:58 -0700244 ui_print("Erasing boot before restore...\n");
245 if (0 != (ret = format_root_device("BOOT:")))
246 return print_and_error("Error while formatting BOOT:!\n");
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700247 sprintf(tmp, "flash_image boot %s/boot.img", backup_path);
248 ui_print("Restoring boot image...\n");
249 if (0 != (ret = __system(tmp))) {
250 ui_print("Error while flashing boot image!");
251 return ret;
252 }
Koushik K. Duttaf7215942010-03-16 13:34:51 -0700253 }
254
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700255 if (restore_system && 0 != (ret = nandroid_restore_partition(backup_path, "SYSTEM:")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700256 return ret;
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800257
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700258 if (restore_data && 0 != (ret = nandroid_restore_partition(backup_path, "DATA:")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700259 return ret;
Koushik Dutta8b5e1852010-06-14 22:04:22 -0700260
261#ifdef HAS_DATADATA
262 if (restore_data && 0 != (ret = nandroid_restore_partition(backup_path, "DATADATA:")))
263 return ret;
264#endif
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800265
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700266 if (restore_cache && 0 != (ret = nandroid_restore_partition(backup_path, "CACHE:")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700267 return ret;
268
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700269 if (restore_sdext)
270 {
271 struct statfs s;
272 sprintf(tmp, "%s/sd-ext.img", backup_path);
273 if (0 != (ret = statfs(tmp, &s)))
274 ui_print("sd-ext.img not found. Skipping restore of /sd-ext.");
Koushik Dutta2f73e582010-04-18 16:00:21 -0700275 else if (0 != (ret = nandroid_restore_partition(backup_path, "SDEXT:")))
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700276 return ret;
277 }
278
Koushik K. Dutta3f38f322010-03-12 23:45:25 -0800279 sync();
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800280 ui_set_background(BACKGROUND_ICON_NONE);
281 ui_reset_progress();
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700282 ui_print("\nRestore complete!\n");
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800283 return 0;
284}
Koushik Dutta08370912010-06-26 12:25:02 -0700285
286void nandroid_generate_timestamp_path(char* backup_path)
287{
288 time_t t = time(NULL);
289 struct tm *tmp = localtime(&t);
290 if (tmp == NULL)
291 {
292 struct timeval tp;
293 gettimeofday(&tp, NULL);
294 sprintf(backup_path, "/sdcard/clockworkmod/backup/%d", tp.tv_sec);
295 }
296 else
297 {
298 strftime(backup_path, PATH_MAX, "/sdcard/clockworkmod/backup/%F.%H.%M.%S", tmp);
299 }
300}
301
302int nandroid_usage()
303{
304 printf("Usage: nandroid backup\n");
305 printf("Usage: nandroid restore <directory>\n");
306 return 1;
307}
308
309int nandroid_main(int argc, char** argv)
310{
311 if (argc > 3 || argc < 2)
312 return nandroid_usage();
313
314 if (strcmp("backup", argv[1]) == 0)
315 {
316 if (argc != 2)
317 return nandroid_usage();
318
319 char backup_path[PATH_MAX];
320 nandroid_generate_timestamp_path(backup_path);
321 return nandroid_backup(backup_path);
322 }
323
324 if (strcmp("restore", argv[1]) == 0)
325 {
326 if (argc != 3)
327 return nandroid_usage();
328 return nandroid_restore(argv[2], 1, 1, 1, 1, 1);
329 }
330
331 return nandroid_usage();
332}