blob: c4c92d83d214a69b2fc09754785f6c3af4dc9193 [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 K. Dutta4b249cd2010-03-15 16:31:03 -070083
84 ui_print("Backing up %s...\n", name);
85 if (0 != (ret = ensure_root_path_mounted(root) != 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, yaffs_callback);
93 ensure_root_path_unmounted(root);
94 if (0 != ret) {
95 ui_print("Error while making a yaffs2 image of %s!\n", mount_point);
96 return ret;
97 }
98 return 0;
99}
100
Koushik Dutta2f73e582010-04-18 16:00:21 -0700101int nandroid_backup(const char* backup_path)
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800102{
103 ui_set_background(BACKGROUND_ICON_INSTALLING);
104
105 if (ensure_root_path_mounted("SDCARD:") != 0)
106 return print_and_error("Can't mount /sdcard\n");
107
Koushik K. Dutta78686292010-03-25 17:58:45 -0700108 int ret;
109 struct statfs s;
110 if (0 != (ret = statfs("/sdcard", &s)))
111 return print_and_error("Unable to stat /sdcard\n");
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700112 uint64_t bavail = s.f_bavail;
113 uint64_t bsize = s.f_bsize;
114 uint64_t sdcard_free = bavail * bsize;
115 uint64_t sdcard_free_mb = sdcard_free / (uint64_t)(1024 * 1024);
116 ui_print("SD Card space free: %lluMB\n", sdcard_free_mb);
117 if (sdcard_free_mb < 150)
Koushik K. Duttac0970932010-03-25 23:19:19 -0700118 ui_print("There may not be enough free space to complete backup... continuing...\n");
Koushik K. Dutta78686292010-03-25 17:58:45 -0700119
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800120 char tmp[PATH_MAX];
121 sprintf(tmp, "mkdir -p %s", backup_path);
122 __system(tmp);
123
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800124 ui_print("Backing up boot...\n");
125 sprintf(tmp, "%s/%s", backup_path, "boot.img");
126 ret = dump_image("boot", tmp, NULL);
127 if (0 != ret)
128 return print_and_error("Error while dumping boot image!\n");
Koushik K. Dutta6923cc32010-03-29 14:46:00 -0700129
130 ui_print("Backing up recovery...\n");
131 sprintf(tmp, "%s/%s", backup_path, "recovery.img");
132 ret = dump_image("recovery", tmp, NULL);
133 if (0 != ret)
134 return print_and_error("Error while dumping boot image!\n");
135
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700136 if (0 != (ret = nandroid_backup_partition(backup_path, "SYSTEM:")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700137 return ret;
138
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700139 if (0 != (ret = nandroid_backup_partition(backup_path, "DATA:")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700140 return ret;
141
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700142 if (0 != (ret = nandroid_backup_partition(backup_path, "CACHE:")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700143 return ret;
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700144
Koushik K. Dutta71ef11f2010-04-02 22:17:42 -0700145 struct stat st;
146 if (0 != stat("/dev/block/mmcblk0p2", &st))
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700147 {
Koushik K. Dutta71ef11f2010-04-02 22:17:42 -0700148 ui_print("No sd-ext found. Skipping backup of sd-ext.\n");
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700149 }
150 else
151 {
152 if (0 != ensure_root_path_mounted("SDEXT:"))
153 ui_print("Could not mount sd-ext. sd-ext backup may not be supported on this device. Skipping backup of sd-ext.");
154 else if (0 != (ret = nandroid_backup_partition(backup_path, "SDEXT:")))
155 return ret;
156 }
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800157
Koushik K. Duttad7178922010-03-24 14:38:40 -0700158 ui_print("Generating md5 sum...\n");
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700159 sprintf(tmp, "nandroid-md5.sh %s", backup_path);
Koushik K. Duttab0a25032010-03-24 10:34:38 -0700160 if (0 != (ret = __system(tmp))) {
161 ui_print("Error while generating md5 sum!\n");
162 return ret;
163 }
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800164
Koushik K. Dutta3f38f322010-03-12 23:45:25 -0800165 sync();
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800166 ui_set_background(BACKGROUND_ICON_NONE);
167 ui_reset_progress();
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700168 ui_print("\nBackup complete!\n");
169 return 0;
170}
171
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700172typedef int (*format_function)(char* root);
173
Koushik Dutta2f73e582010-04-18 16:00:21 -0700174int nandroid_restore_partition(const char* backup_path, const char* root) {
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700175 int ret = 0;
176 char mount_point[PATH_MAX];
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700177 translate_root_path(root, mount_point, PATH_MAX);
178 char* name = basename(mount_point);
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700179
180 ui_print("Restoring %s...\n", name);
181 if (0 != (ret = ensure_root_path_unmounted(root))) {
182 ui_print("Can't unmount %s!\n", mount_point);
183 return ret;
184 }
Koushik Dutta2f73e582010-04-18 16:00:21 -0700185 if (0 != (ret = format_root_device(root))) {
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700186 ui_print("Error while formatting %s!\n", root);
187 return ret;
188 }
189
190 if (0 != (ret = ensure_root_path_mounted(root))) {
191 ui_print("Can't mount %s!\n", mount_point);
192 return ret;
193 }
194
195 char tmp[PATH_MAX];
196 sprintf(tmp, "%s/%s.img", backup_path, name);
197 if (0 != (ret = unyaffs(tmp, mount_point, yaffs_callback))) {
198 ui_print("Error while restoring %s!\n", mount_point);
199 return ret;
200 }
Koushik Dutta2f73e582010-04-18 16:00:21 -0700201
202 if (0 != strcmp(root, "CACHE")) {
203 ensure_root_path_unmounted(root);
204 }
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800205 return 0;
206}
207
Koushik Dutta2f73e582010-04-18 16:00:21 -0700208int 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 -0800209{
210 ui_set_background(BACKGROUND_ICON_INSTALLING);
211 ui_show_indeterminate_progress();
212 yaffs_files_total = 0;
213
214 if (ensure_root_path_mounted("SDCARD:") != 0)
215 return print_and_error("Can't mount /sdcard\n");
216
217 char tmp[PATH_MAX];
218
219 ui_print("Checking MD5 sums...\n");
Koushik K. Duttaface5882010-03-13 10:15:55 -0800220 sprintf(tmp, "cd %s && md5sum -c nandroid.md5", backup_path);
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800221 if (0 != __system(tmp))
222 return print_and_error("MD5 mismatch!\n");
223
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700224 int ret;
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700225 if (restore_boot)
226 {
227 sprintf(tmp, "flash_image boot %s/boot.img", backup_path);
228 ui_print("Restoring boot image...\n");
229 if (0 != (ret = __system(tmp))) {
230 ui_print("Error while flashing boot image!");
231 return ret;
232 }
Koushik K. Duttaf7215942010-03-16 13:34:51 -0700233 }
234
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700235 if (restore_system && 0 != (ret = nandroid_restore_partition(backup_path, "SYSTEM:")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700236 return ret;
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800237
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700238 if (restore_data && 0 != (ret = nandroid_restore_partition(backup_path, "DATA:")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700239 return ret;
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800240
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700241 if (restore_cache && 0 != (ret = nandroid_restore_partition(backup_path, "CACHE:")))
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700242 return ret;
243
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700244 if (restore_sdext)
245 {
246 struct statfs s;
247 sprintf(tmp, "%s/sd-ext.img", backup_path);
248 if (0 != (ret = statfs(tmp, &s)))
249 ui_print("sd-ext.img not found. Skipping restore of /sd-ext.");
Koushik Dutta2f73e582010-04-18 16:00:21 -0700250 else if (0 != (ret = nandroid_restore_partition(backup_path, "SDEXT:")))
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700251 return ret;
252 }
253
Koushik K. Dutta3f38f322010-03-12 23:45:25 -0800254 sync();
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800255 ui_set_background(BACKGROUND_ICON_NONE);
256 ui_reset_progress();
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700257 ui_print("\nRestore complete!\n");
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800258 return 0;
259}