blob: 933faa33be66f9b402bfac16f497116917a994ac [file] [log] [blame]
preludedrew38058dc2011-01-29 23:30:44 -07001#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 "../../external/yaffs2/yaffs2/utils/mkyaffs2image.h"
34#include "../../external/yaffs2/yaffs2/utils/unyaffs.h"
35
36#include <sys/vfs.h>
37
38#include "extendedcommands.h"
39#include "nandroid.h"
40
41int print_and_error(const char* message) {
42 ui_print("%s", message);
43 return 1;
44}
45
46int yaffs_files_total = 0;
47int yaffs_files_count = 0;
48void yaffs_callback(char* filename)
49{
50 char* justfile = basename(filename);
51 if (strlen(justfile) < 30)
52 ui_print("%s", justfile);
53 yaffs_files_count++;
54 if (yaffs_files_total != 0)
55 ui_set_progress((float)yaffs_files_count / (float)yaffs_files_total);
56 ui_reset_text_col();
57}
58
59void compute_directory_stats(char* directory)
60{
61 char tmp[PATH_MAX];
62 sprintf(tmp, "find %s | wc -l > /tmp/dircount", directory);
63 __system(tmp);
64 char count_text[100];
65 FILE* f = fopen("/tmp/dircount", "r");
66 fread(count_text, 1, sizeof(count_text), f);
67 fclose(f);
68 yaffs_files_count = 0;
69 yaffs_files_total = atoi(count_text);
70 ui_reset_progress();
71 ui_show_progress(1, 0);
72}
73
74int nandroid_backup_partition_extended(const char* backup_path, const char* mount_point, int umount_when_finished) {
75 int ret = 0;
76 char* name = basename(mount_point);
77
78 struct stat file_info;
79 mkyaffs2image_callback callback = NULL;
80 if (0 != stat("/sdcard/clockworkmod/.hidenandroidprogress", &file_info)) {
81 callback = yaffs_callback;
82 }
83
84 ui_print("Backing up %s...\n", name);
85 if (0 != (ret = ensure_path_mounted(mount_point) != 0)) {
86 ui_print("Can't mount %s!\n", mount_point);
87 return ret;
88 }
89 compute_directory_stats(mount_point);
90 char tmp[PATH_MAX];
91 sprintf(tmp, "%s/%s.img", backup_path, name);
92 ret = mkyaffs2image(mount_point, tmp, 0, callback);
93 if (umount_when_finished) {
94 ensure_path_unmounted(mount_point);
95 }
96 if (0 != ret) {
97 ui_print("Error while making a yaffs2 image of %s!\n", mount_point);
98 return ret;
99 }
100 return 0;
101}
102
103int nandroid_backup_partition(const char* backup_path, const char* root) {
104 Volume *vol = volume_for_path(root);
105 // make sure the volume exists before attempting anything...
106 if (vol == NULL || vol->fs_type == NULL)
107 return NULL;
108
109 // see if we need a raw backup (mtd)
110 char tmp[PATH_MAX];
111 int ret;
112 if (strcmp(vol->fs_type, "mtd") == 0 ||
andrew.boren1acd15e2011-03-31 21:41:22 -0700113 strcmp(vol->fs_type, "bml") == 0 ||
preludedrew38058dc2011-01-29 23:30:44 -0700114 strcmp(vol->fs_type, "emmc") == 0) {
115 const char* name = basename(root);
116 sprintf(tmp, "%s/%s.img", backup_path, name);
117 ui_print("Backing up %s image...\n", name);
118 if (0 != (ret = backup_raw_partition(vol->device, tmp))) {
119 ui_print("Error while backing up %s image!", name);
120 return ret;
121 }
122 return 0;
123 }
124
125 return nandroid_backup_partition_extended(backup_path, root, 1);
126}
127
128int nandroid_backup(const char* backup_path)
129{
130 ui_set_background(BACKGROUND_ICON_INSTALLING);
131
132 if (ensure_path_mounted("/sdcard") != 0)
133 return print_and_error("Can't mount /sdcard\n");
134
135 int ret;
136 struct statfs s;
137 if (0 != (ret = statfs("/sdcard", &s)))
138 return print_and_error("Unable to stat /sdcard\n");
139 uint64_t bavail = s.f_bavail;
140 uint64_t bsize = s.f_bsize;
141 uint64_t sdcard_free = bavail * bsize;
142 uint64_t sdcard_free_mb = sdcard_free / (uint64_t)(1024 * 1024);
143 ui_print("SD Card space free: %lluMB\n", sdcard_free_mb);
144 if (sdcard_free_mb < 150)
145 ui_print("There may not be enough free space to complete backup... continuing...\n");
146
147 char tmp[PATH_MAX];
148 sprintf(tmp, "mkdir -p %s", backup_path);
149 __system(tmp);
150
151 if (0 != (ret = nandroid_backup_partition(backup_path, "/boot")))
152 return ret;
153
154 if (0 != (ret = nandroid_backup_partition(backup_path, "/recovery")))
155 return ret;
156
andrew.boren1acd15e2011-03-31 21:41:22 -0700157 Volume *vol = volume_for_path("/wimax");
158 if (vol != NULL && 0 == stat(vol->device, &s))
preludedrew38058dc2011-01-29 23:30:44 -0700159 {
160 char serialno[PROPERTY_VALUE_MAX];
161 ui_print("Backing up WiMAX...\n");
162 serialno[0] = 0;
163 property_get("ro.serialno", serialno, "");
164 sprintf(tmp, "%s/wimax.%s.img", backup_path, serialno);
andrew.boren1acd15e2011-03-31 21:41:22 -0700165 ret = backup_raw_partition(vol->device, tmp);
preludedrew38058dc2011-01-29 23:30:44 -0700166 if (0 != ret)
167 return print_and_error("Error while dumping WiMAX image!\n");
168 }
169
170 if (0 != (ret = nandroid_backup_partition(backup_path, "/system")))
171 return ret;
172
173 if (0 != (ret = nandroid_backup_partition(backup_path, "/data")))
174 return ret;
175
176 if (has_datadata()) {
177 if (0 != (ret = nandroid_backup_partition(backup_path, "/datadata")))
178 return ret;
179 }
180
andrew.boren1acd15e2011-03-31 21:41:22 -0700181 if (0 != stat("/sdcard/.android_secure", &s))
preludedrew38058dc2011-01-29 23:30:44 -0700182 {
183 ui_print("No /sdcard/.android_secure found. Skipping backup of applications on external storage.\n");
184 }
185 else
186 {
187 if (0 != (ret = nandroid_backup_partition_extended(backup_path, "/sdcard/.android_secure", 0)))
188 return ret;
189 }
190
191 if (0 != (ret = nandroid_backup_partition_extended(backup_path, "/cache", 0)))
192 return ret;
193
andrew.boren1acd15e2011-03-31 21:41:22 -0700194 vol = volume_for_path("/sd-ext");
195 if (vol == NULL || 0 != stat(vol->device, &s))
preludedrew38058dc2011-01-29 23:30:44 -0700196 {
197 ui_print("No sd-ext found. Skipping backup of sd-ext.\n");
198 }
199 else
200 {
201 if (0 != ensure_path_mounted("/sd-ext"))
202 ui_print("Could not mount sd-ext. sd-ext backup may not be supported on this device. Skipping backup of sd-ext.\n");
203 else if (0 != (ret = nandroid_backup_partition(backup_path, "/sd-ext")))
204 return ret;
205 }
206
207 ui_print("Generating md5 sum...\n");
208 sprintf(tmp, "nandroid-md5.sh %s", backup_path);
209 if (0 != (ret = __system(tmp))) {
210 ui_print("Error while generating md5 sum!\n");
211 return ret;
212 }
213
214 sync();
215 ui_set_background(BACKGROUND_ICON_NONE);
216 ui_reset_progress();
217 ui_print("\nBackup complete!\n");
218 return 0;
219}
220
221typedef int (*format_function)(char* root);
222
223static void ensure_directory(const char* dir) {
224 char tmp[PATH_MAX];
225 sprintf(tmp, "mkdir -p %s", dir);
226 __system(tmp);
227}
228
229int nandroid_restore_partition_extended(const char* backup_path, const char* mount_point, int umount_when_finished) {
230 int ret = 0;
231 char* name = basename(mount_point);
232
233 char tmp[PATH_MAX];
234 sprintf(tmp, "%s/%s.img", backup_path, name);
235 struct stat file_info;
236 if (0 != (ret = statfs(tmp, &file_info))) {
237 ui_print("%s.img not found. Skipping restore of %s.\n", name, mount_point);
238 return 0;
239 }
240
241 ensure_directory(mount_point);
242
243 unyaffs_callback callback = NULL;
244 if (0 != stat("/sdcard/clockworkmod/.hidenandroidprogress", &file_info)) {
245 callback = yaffs_callback;
246 }
247
248 ui_print("Restoring %s...\n", name);
249 /*
250 if (0 != (ret = ensure_root_path_unmounted(root))) {
251 ui_print("Can't unmount %s!\n", mount_point);
252 return ret;
253 }
254 */
255 if (0 != (ret = format_volume(mount_point))) {
256 ui_print("Error while formatting %s!\n", mount_point);
257 return ret;
258 }
259
260 if (0 != (ret = ensure_path_mounted(mount_point))) {
261 ui_print("Can't mount %s!\n", mount_point);
262 return ret;
263 }
264
265 if (0 != (ret = unyaffs(tmp, mount_point, callback))) {
266 ui_print("Error while restoring %s!\n", mount_point);
267 return ret;
268 }
269
270 if (umount_when_finished) {
271 ensure_path_unmounted(mount_point);
272 }
273
274 return 0;
275}
276
277int nandroid_restore_partition(const char* backup_path, const char* root) {
278 Volume *vol = volume_for_path(root);
279 // make sure the volume exists...
280 if (vol == NULL || vol->fs_type == NULL)
281 return 0;
282
283 // see if we need a raw restore (mtd)
284 char tmp[PATH_MAX];
285 if (strcmp(vol->fs_type, "mtd") == 0 ||
andrew.boren1acd15e2011-03-31 21:41:22 -0700286 strcmp(vol->fs_type, "bml") == 0 ||
preludedrew38058dc2011-01-29 23:30:44 -0700287 strcmp(vol->fs_type, "emmc") == 0) {
288 int ret;
289 const char* name = basename(root);
290 ui_print("Erasing %s before restore...\n", name);
291 if (0 != (ret = format_volume(root))) {
292 ui_print("Error while erasing %s image!", name);
293 return ret;
294 }
295 sprintf(tmp, "%s%s.img", backup_path, root);
296 ui_print("Restoring %s image...\n", name);
297 if (0 != (ret = restore_raw_partition(vol->device, tmp))) {
298 ui_print("Error while flashing %s image!", name);
299 return ret;
300 }
301 return 0;
302 }
303 return nandroid_restore_partition_extended(backup_path, root, 1);
304}
305
306int nandroid_restore(const char* backup_path, int restore_boot, int restore_system, int restore_data, int restore_cache, int restore_sdext, int restore_wimax)
307{
308 ui_set_background(BACKGROUND_ICON_INSTALLING);
309 ui_show_indeterminate_progress();
310 yaffs_files_total = 0;
311
312 if (ensure_path_mounted("/sdcard") != 0)
313 return print_and_error("Can't mount /sdcard\n");
314
315 char tmp[PATH_MAX];
316
317 ui_print("Checking MD5 sums...\n");
318 sprintf(tmp, "cd %s && md5sum -c nandroid.md5", backup_path);
319 if (0 != __system(tmp))
320 return print_and_error("MD5 mismatch!\n");
321
322 int ret;
323
324 if (restore_boot && NULL != volume_for_path("/boot") && 0 != (ret = nandroid_restore_partition(backup_path, "/boot")))
325 return ret;
326
andrew.boren1acd15e2011-03-31 21:41:22 -0700327 struct stat s;
328 Volume *vol = volume_for_path("/wimax");
329 if (restore_wimax && vol != NULL && 0 == stat(vol->device, &s))
preludedrew38058dc2011-01-29 23:30:44 -0700330 {
331 char serialno[PROPERTY_VALUE_MAX];
332
333 serialno[0] = 0;
334 property_get("ro.serialno", serialno, "");
335 sprintf(tmp, "%s/wimax.%s.img", backup_path, serialno);
336
337 struct stat st;
338 if (0 != stat(tmp, &st))
339 {
340 ui_print("WARNING: WiMAX partition exists, but nandroid\n");
341 ui_print(" backup does not contain WiMAX image.\n");
342 ui_print(" You should create a new backup to\n");
343 ui_print(" protect your WiMAX keys.\n");
344 }
345 else
346 {
347 ui_print("Erasing WiMAX before restore...\n");
348 if (0 != (ret = format_volume("/wimax")))
349 return print_and_error("Error while formatting wimax!\n");
350 ui_print("Restoring WiMAX image...\n");
andrew.boren1acd15e2011-03-31 21:41:22 -0700351 if (0 != (ret = restore_raw_partition(vol->device, tmp)))
preludedrew38058dc2011-01-29 23:30:44 -0700352 return ret;
353 }
354 }
355
356 if (restore_system && 0 != (ret = nandroid_restore_partition(backup_path, "/system")))
357 return ret;
358
359 if (restore_data && 0 != (ret = nandroid_restore_partition(backup_path, "/data")))
360 return ret;
361
362 if (has_datadata()) {
363 if (restore_data && 0 != (ret = nandroid_restore_partition(backup_path, "/datadata")))
364 return ret;
365 }
366
367 if (restore_data && 0 != (ret = nandroid_restore_partition_extended(backup_path, "/sdcard/.android_secure", 0)))
368 return ret;
369
370 if (restore_cache && 0 != (ret = nandroid_restore_partition_extended(backup_path, "/cache", 0)))
371 return ret;
372
373 if (restore_sdext && 0 != (ret = nandroid_restore_partition(backup_path, "/sd-ext")))
374 return ret;
375
376 sync();
377 ui_set_background(BACKGROUND_ICON_NONE);
378 ui_reset_progress();
379 ui_print("\nRestore complete!\n");
380 return 0;
381}
382
383void nandroid_generate_timestamp_path(char* backup_path)
384{
385 time_t t = time(NULL);
386 struct tm *tmp = localtime(&t);
387 if (tmp == NULL)
388 {
389 struct timeval tp;
390 gettimeofday(&tp, NULL);
391 sprintf(backup_path, "/sdcard/clockworkmod/backup/%d", tp.tv_sec);
392 }
393 else
394 {
395 strftime(backup_path, PATH_MAX, "/sdcard/clockworkmod/backup/%F.%H.%M.%S", tmp);
396 }
397}
398
399int nandroid_usage()
400{
401 printf("Usage: nandroid backup\n");
402 printf("Usage: nandroid restore <directory>\n");
403 return 1;
404}
405
406int nandroid_main(int argc, char** argv)
407{
408 if (argc > 3 || argc < 2)
409 return nandroid_usage();
410
411 if (strcmp("backup", argv[1]) == 0)
412 {
413 if (argc != 2)
414 return nandroid_usage();
415
416 char backup_path[PATH_MAX];
417 nandroid_generate_timestamp_path(backup_path);
418 return nandroid_backup(backup_path);
419 }
420
421 if (strcmp("restore", argv[1]) == 0)
422 {
423 if (argc != 3)
424 return nandroid_usage();
425 return nandroid_restore(argv[2], 1, 1, 1, 1, 1, 0);
426 }
427
428 return nandroid_usage();
429}