blob: b47a2690066b919e13fff1bc7778903b5e75ce9b [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 Dutta28a41b42010-09-13 14:55:17 -070045#ifndef BOARD_USES_BMLUTILS
46int write_raw_image(const char* partition, const char* filename) {
47 char tmp[PATH_MAX];
48 sprintf(tmp, "flash_image boot %s", filename);
49 return __system(tmp);
50}
Koushik Dutta56c1b3b2010-09-13 15:08:49 -070051
52int read_raw_image(const char* partition, const char* filename) {
53 return dump_image(partition, filename, NULL);
54}
Koushik Dutta28a41b42010-09-13 14:55:17 -070055#endif
56
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -070057int print_and_error(char* message) {
58 ui_print(message);
59 return 1;
60}
61
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -080062int yaffs_files_total = 0;
63int yaffs_files_count = 0;
64void yaffs_callback(char* filename)
65{
66 char* justfile = basename(filename);
67 if (strlen(justfile) < 30)
Koushik K. Dutta3fb8d302010-03-15 17:05:27 -070068 ui_print(justfile);
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -080069 yaffs_files_count++;
70 if (yaffs_files_total != 0)
71 ui_set_progress((float)yaffs_files_count / (float)yaffs_files_total);
72 ui_reset_text_col();
73}
74
75void compute_directory_stats(char* directory)
76{
77 char tmp[PATH_MAX];
78 sprintf(tmp, "find %s | wc -l > /tmp/dircount", directory);
79 __system(tmp);
80 char count_text[100];
81 FILE* f = fopen("/tmp/dircount", "r");
82 fread(count_text, 1, sizeof(count_text), f);
83 fclose(f);
84 yaffs_files_count = 0;
85 yaffs_files_total = atoi(count_text);
86 ui_reset_progress();
87 ui_show_progress(1, 0);
88}
89
Koushik Dutta062d6b02010-07-03 13:54:21 -070090int nandroid_backup_partition_extended(const char* backup_path, char* root, int umount_when_finished) {
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -070091 int ret = 0;
92 char mount_point[PATH_MAX];
Koushik K. Dutta68b01902010-04-01 12:20:39 -070093 translate_root_path(root, mount_point, PATH_MAX);
94 char* name = basename(mount_point);
Koushik Dutta58131e72010-06-09 12:41:20 -070095
96 struct stat file_info;
97 mkyaffs2image_callback callback = NULL;
98 if (0 != stat("/sdcard/clockworkmod/.hidenandroidprogress", &file_info)) {
99 callback = yaffs_callback;
100 }
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700101
102 ui_print("Backing up %s...\n", name);
103 if (0 != (ret = ensure_root_path_mounted(root) != 0)) {
104 ui_print("Can't mount %s!\n", mount_point);
105 return ret;
106 }
107 compute_directory_stats(mount_point);
108 char tmp[PATH_MAX];
109 sprintf(tmp, "%s/%s.img", backup_path, name);
Koushik Dutta58131e72010-06-09 12:41:20 -0700110 ret = mkyaffs2image(mount_point, tmp, 0, callback);
Koushik Dutta062d6b02010-07-03 13:54:21 -0700111 if (umount_when_finished) {
112 ensure_root_path_unmounted(root);
113 }
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700114 if (0 != ret) {
115 ui_print("Error while making a yaffs2 image of %s!\n", mount_point);
116 return ret;
117 }
118 return 0;
119}
120
Koushik Dutta062d6b02010-07-03 13:54:21 -0700121int nandroid_backup_partition(const char* backup_path, char* root) {
122 return nandroid_backup_partition_extended(backup_path, root, 1);
123}
124
Koushik Dutta2f73e582010-04-18 16:00:21 -0700125int nandroid_backup(const char* backup_path)
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800126{
127 ui_set_background(BACKGROUND_ICON_INSTALLING);
128
129 if (ensure_root_path_mounted("SDCARD:") != 0)
130 return print_and_error("Can't mount /sdcard\n");
131
Koushik K. Dutta78686292010-03-25 17:58:45 -0700132 int ret;
Koushik K. Duttaf3534d02010-04-18 18:06:24 -0700133 struct statfs s;
Koushik K. Dutta78686292010-03-25 17:58:45 -0700134 if (0 != (ret = statfs("/sdcard", &s)))
135 return print_and_error("Unable to stat /sdcard\n");
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700136 uint64_t bavail = s.f_bavail;
137 uint64_t bsize = s.f_bsize;
138 uint64_t sdcard_free = bavail * bsize;
139 uint64_t sdcard_free_mb = sdcard_free / (uint64_t)(1024 * 1024);
140 ui_print("SD Card space free: %lluMB\n", sdcard_free_mb);
141 if (sdcard_free_mb < 150)
Koushik K. Duttac0970932010-03-25 23:19:19 -0700142 ui_print("There may not be enough free space to complete backup... continuing...\n");
Koushik K. Dutta78686292010-03-25 17:58:45 -0700143
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800144 char tmp[PATH_MAX];
145 sprintf(tmp, "mkdir -p %s", backup_path);
146 __system(tmp);
147
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700148#ifndef BOARD_RECOVERY_IGNORE_BOOTABLES
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800149 ui_print("Backing up boot...\n");
150 sprintf(tmp, "%s/%s", backup_path, "boot.img");
Koushik Dutta56c1b3b2010-09-13 15:08:49 -0700151 ret = read_raw_image("boot", tmp);
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800152 if (0 != ret)
153 return print_and_error("Error while dumping boot image!\n");
Koushik K. Dutta6923cc32010-03-29 14:46:00 -0700154
155 ui_print("Backing up recovery...\n");
156 sprintf(tmp, "%s/%s", backup_path, "recovery.img");
Koushik Dutta56c1b3b2010-09-13 15:08:49 -0700157 ret = read_raw_image("recovery", tmp);
Koushik K. Dutta6923cc32010-03-29 14:46:00 -0700158 if (0 != ret)
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700159 return print_and_error("Error while dumping recovery image!\n");
160#endif
Koushik K. Dutta6923cc32010-03-29 14:46:00 -0700161
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700162 if (0 != (ret = nandroid_backup_partition(backup_path, "SYSTEM:")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700163 return ret;
164
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700165 if (0 != (ret = nandroid_backup_partition(backup_path, "DATA:")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700166 return ret;
167
Koushik Dutta8b5e1852010-06-14 22:04:22 -0700168#ifdef HAS_DATADATA
169 if (0 != (ret = nandroid_backup_partition(backup_path, "DATADATA:")))
170 return ret;
171#endif
172
Koushik Dutta062d6b02010-07-03 13:54:21 -0700173 struct stat st;
174 if (0 != stat("/sdcard/.android_secure", &st))
175 {
176 ui_print("No /sdcard/.android_secure found. Skipping backup of applications on external storage.\n");
177 }
178 else
179 {
180 if (0 != (ret = nandroid_backup_partition_extended(backup_path, "SDCARD:/.android_secure", 0)))
181 return ret;
182 }
183
184 if (0 != (ret = nandroid_backup_partition_extended(backup_path, "CACHE:", 0)))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700185 return ret;
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700186
Koushik Dutta52d3f202010-06-21 12:11:13 -0700187 if (0 != stat(SDEXT_DEVICE, &st))
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700188 {
Koushik K. Dutta71ef11f2010-04-02 22:17:42 -0700189 ui_print("No sd-ext found. Skipping backup of sd-ext.\n");
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700190 }
191 else
192 {
193 if (0 != ensure_root_path_mounted("SDEXT:"))
Koushik Dutta8b5e1852010-06-14 22:04:22 -0700194 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 -0700195 else if (0 != (ret = nandroid_backup_partition(backup_path, "SDEXT:")))
196 return ret;
197 }
Koushik Dutta062d6b02010-07-03 13:54:21 -0700198
Koushik K. Duttad7178922010-03-24 14:38:40 -0700199 ui_print("Generating md5 sum...\n");
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700200 sprintf(tmp, "nandroid-md5.sh %s", backup_path);
Koushik K. Duttab0a25032010-03-24 10:34:38 -0700201 if (0 != (ret = __system(tmp))) {
202 ui_print("Error while generating md5 sum!\n");
203 return ret;
204 }
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800205
Koushik K. Dutta3f38f322010-03-12 23:45:25 -0800206 sync();
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800207 ui_set_background(BACKGROUND_ICON_NONE);
208 ui_reset_progress();
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700209 ui_print("\nBackup complete!\n");
210 return 0;
211}
212
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700213typedef int (*format_function)(char* root);
214
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700215static void ensure_directory(const char* dir) {
216 char tmp[PATH_MAX];
217 sprintf(tmp, "mkdir -p %s", dir);
218 __system(tmp);
219}
220
Koushik Dutta062d6b02010-07-03 13:54:21 -0700221int nandroid_restore_partition_extended(const char* backup_path, const char* root, int umount_when_finished) {
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700222 int ret = 0;
223 char mount_point[PATH_MAX];
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700224 translate_root_path(root, mount_point, PATH_MAX);
225 char* name = basename(mount_point);
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700226
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700227 char tmp[PATH_MAX];
228 sprintf(tmp, "%s/%s.img", backup_path, name);
Koushik Dutta58131e72010-06-09 12:41:20 -0700229 struct stat file_info;
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700230 if (0 != (ret = statfs(tmp, &file_info))) {
Koushik Duttad4060c32010-07-22 20:14:44 -0700231 ui_print("%s.img not found. Skipping restore of %s.\n", name, mount_point);
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700232 return 0;
233 }
234
235 ensure_directory(mount_point);
236
Koushik Dutta58131e72010-06-09 12:41:20 -0700237 unyaffs_callback callback = NULL;
238 if (0 != stat("/sdcard/clockworkmod/.hidenandroidprogress", &file_info)) {
239 callback = yaffs_callback;
240 }
241
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700242 ui_print("Restoring %s...\n", name);
Koushik Dutta852bb422010-07-24 11:18:00 -0700243 /*
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700244 if (0 != (ret = ensure_root_path_unmounted(root))) {
245 ui_print("Can't unmount %s!\n", mount_point);
246 return ret;
247 }
Koushik Dutta852bb422010-07-24 11:18:00 -0700248 */
Koushik Dutta2f73e582010-04-18 16:00:21 -0700249 if (0 != (ret = format_root_device(root))) {
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700250 ui_print("Error while formatting %s!\n", root);
251 return ret;
252 }
253
254 if (0 != (ret = ensure_root_path_mounted(root))) {
255 ui_print("Can't mount %s!\n", mount_point);
256 return ret;
257 }
258
Koushik Dutta58131e72010-06-09 12:41:20 -0700259 if (0 != (ret = unyaffs(tmp, mount_point, callback))) {
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700260 ui_print("Error while restoring %s!\n", mount_point);
261 return ret;
262 }
Koushik Dutta2f73e582010-04-18 16:00:21 -0700263
Koushik Dutta062d6b02010-07-03 13:54:21 -0700264 if (umount_when_finished) {
Koushik Dutta2f73e582010-04-18 16:00:21 -0700265 ensure_root_path_unmounted(root);
266 }
Koushik Dutta062d6b02010-07-03 13:54:21 -0700267
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800268 return 0;
269}
270
Koushik Dutta062d6b02010-07-03 13:54:21 -0700271int nandroid_restore_partition(const char* backup_path, const char* root) {
272 return nandroid_restore_partition_extended(backup_path, root, 1);
273}
274
Koushik Dutta2f73e582010-04-18 16:00:21 -0700275int 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 -0800276{
277 ui_set_background(BACKGROUND_ICON_INSTALLING);
278 ui_show_indeterminate_progress();
279 yaffs_files_total = 0;
280
281 if (ensure_root_path_mounted("SDCARD:") != 0)
282 return print_and_error("Can't mount /sdcard\n");
283
284 char tmp[PATH_MAX];
285
286 ui_print("Checking MD5 sums...\n");
Koushik K. Duttaface5882010-03-13 10:15:55 -0800287 sprintf(tmp, "cd %s && md5sum -c nandroid.md5", backup_path);
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800288 if (0 != __system(tmp))
289 return print_and_error("MD5 mismatch!\n");
290
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700291 int ret;
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700292#ifndef BOARD_RECOVERY_IGNORE_BOOTABLES
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700293 if (restore_boot)
294 {
Koushik K. Dutta84306552010-04-18 21:21:58 -0700295 ui_print("Erasing boot before restore...\n");
296 if (0 != (ret = format_root_device("BOOT:")))
297 return print_and_error("Error while formatting BOOT:!\n");
Koushik Dutta28a41b42010-09-13 14:55:17 -0700298 sprintf(tmp, "%s/boot.img", backup_path);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700299 ui_print("Restoring boot image...\n");
Koushik Dutta28a41b42010-09-13 14:55:17 -0700300 if (0 != (ret = write_raw_image("boot", tmp))) {
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700301 ui_print("Error while flashing boot image!");
302 return ret;
303 }
Koushik K. Duttaf7215942010-03-16 13:34:51 -0700304 }
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700305#endif
Koushik K. Duttaf7215942010-03-16 13:34:51 -0700306
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700307 if (restore_system && 0 != (ret = nandroid_restore_partition(backup_path, "SYSTEM:")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700308 return ret;
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800309
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700310 if (restore_data && 0 != (ret = nandroid_restore_partition(backup_path, "DATA:")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700311 return ret;
Koushik Dutta8b5e1852010-06-14 22:04:22 -0700312
313#ifdef HAS_DATADATA
314 if (restore_data && 0 != (ret = nandroid_restore_partition(backup_path, "DATADATA:")))
315 return ret;
316#endif
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800317
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700318 if (restore_data && 0 != (ret = nandroid_restore_partition_extended(backup_path, "SDCARD:/.android_secure", 0)))
319 return ret;
Koushik Dutta062d6b02010-07-03 13:54:21 -0700320
321 if (restore_cache && 0 != (ret = nandroid_restore_partition_extended(backup_path, "CACHE:", 0)))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700322 return ret;
323
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700324 if (restore_sdext && 0 != (ret = nandroid_restore_partition(backup_path, "SDEXT:")))
325 return ret;
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700326
Koushik K. Dutta3f38f322010-03-12 23:45:25 -0800327 sync();
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800328 ui_set_background(BACKGROUND_ICON_NONE);
329 ui_reset_progress();
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700330 ui_print("\nRestore complete!\n");
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800331 return 0;
332}
Koushik Dutta08370912010-06-26 12:25:02 -0700333
334void nandroid_generate_timestamp_path(char* backup_path)
335{
336 time_t t = time(NULL);
337 struct tm *tmp = localtime(&t);
338 if (tmp == NULL)
339 {
340 struct timeval tp;
341 gettimeofday(&tp, NULL);
342 sprintf(backup_path, "/sdcard/clockworkmod/backup/%d", tp.tv_sec);
343 }
344 else
345 {
346 strftime(backup_path, PATH_MAX, "/sdcard/clockworkmod/backup/%F.%H.%M.%S", tmp);
347 }
348}
349
350int nandroid_usage()
351{
352 printf("Usage: nandroid backup\n");
353 printf("Usage: nandroid restore <directory>\n");
354 return 1;
355}
356
357int nandroid_main(int argc, char** argv)
358{
359 if (argc > 3 || argc < 2)
360 return nandroid_usage();
361
362 if (strcmp("backup", argv[1]) == 0)
363 {
364 if (argc != 2)
365 return nandroid_usage();
366
367 char backup_path[PATH_MAX];
368 nandroid_generate_timestamp_path(backup_path);
369 return nandroid_backup(backup_path);
370 }
371
372 if (strcmp("restore", argv[1]) == 0)
373 {
374 if (argc != 3)
375 return nandroid_usage();
376 return nandroid_restore(argv[2], 1, 1, 1, 1, 1);
377 }
378
379 return nandroid_usage();
380}