blob: 8ba1cd5d5c7e6ac243fd4a20c951a82abe71cf51 [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
40#include "extendedcommands.h"
41#include "nandroid.h"
42
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -070043int print_and_error(char* message) {
44 ui_print(message);
45 return 1;
46}
47
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -080048int yaffs_files_total = 0;
49int yaffs_files_count = 0;
50void yaffs_callback(char* filename)
51{
52 char* justfile = basename(filename);
53 if (strlen(justfile) < 30)
Koushik K. Dutta3fb8d302010-03-15 17:05:27 -070054 ui_print(justfile);
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -080055 yaffs_files_count++;
56 if (yaffs_files_total != 0)
57 ui_set_progress((float)yaffs_files_count / (float)yaffs_files_total);
58 ui_reset_text_col();
59}
60
61void compute_directory_stats(char* directory)
62{
63 char tmp[PATH_MAX];
64 sprintf(tmp, "find %s | wc -l > /tmp/dircount", directory);
65 __system(tmp);
66 char count_text[100];
67 FILE* f = fopen("/tmp/dircount", "r");
68 fread(count_text, 1, sizeof(count_text), f);
69 fclose(f);
70 yaffs_files_count = 0;
71 yaffs_files_total = atoi(count_text);
72 ui_reset_progress();
73 ui_show_progress(1, 0);
74}
75
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -070076int nandroid_backup_partition(char* backup_path, char* root, char* name) {
77 int ret = 0;
78 char mount_point[PATH_MAX];
79 sprintf(mount_point, "/%s", name);
80
81 ui_print("Backing up %s...\n", name);
82 if (0 != (ret = ensure_root_path_mounted(root) != 0)) {
83 ui_print("Can't mount %s!\n", mount_point);
84 return ret;
85 }
86 compute_directory_stats(mount_point);
87 char tmp[PATH_MAX];
88 sprintf(tmp, "%s/%s.img", backup_path, name);
89 ret = mkyaffs2image(mount_point, tmp, 0, yaffs_callback);
90 ensure_root_path_unmounted(root);
91 if (0 != ret) {
92 ui_print("Error while making a yaffs2 image of %s!\n", mount_point);
93 return ret;
94 }
95 return 0;
96}
97
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -080098int nandroid_backup(char* backup_path)
99{
100 ui_set_background(BACKGROUND_ICON_INSTALLING);
101
102 if (ensure_root_path_mounted("SDCARD:") != 0)
103 return print_and_error("Can't mount /sdcard\n");
104
105 char tmp[PATH_MAX];
106 sprintf(tmp, "mkdir -p %s", backup_path);
107 __system(tmp);
108
109 int ret;
110 ui_print("Backing up boot...\n");
111 sprintf(tmp, "%s/%s", backup_path, "boot.img");
112 ret = dump_image("boot", tmp, NULL);
113 if (0 != ret)
114 return print_and_error("Error while dumping boot image!\n");
115
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700116 if (0 != (ret = nandroid_backup_partition(backup_path, "SYSTEM:", "system")))
117 return ret;
118
119 if (0 != (ret = nandroid_backup_partition(backup_path, "DATA:", "data")))
120 return ret;
121
122 if (0 != (ret = nandroid_backup_partition(backup_path, "CACHE:", "cache")))
123 return ret;
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800124
Koushik K. Duttad7178922010-03-24 14:38:40 -0700125 ui_print("Generating md5 sum...\n");
126 sprintf(tmp, "cd %s && (md5sum *img > nandroid.md5)", backup_path);
Koushik K. Duttab0a25032010-03-24 10:34:38 -0700127 if (0 != (ret = __system(tmp))) {
128 ui_print("Error while generating md5 sum!\n");
129 return ret;
130 }
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800131
Koushik K. Dutta3f38f322010-03-12 23:45:25 -0800132 sync();
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800133 ui_set_background(BACKGROUND_ICON_NONE);
134 ui_reset_progress();
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700135 ui_print("\nBackup complete!\n");
136 return 0;
137}
138
139int nandroid_restore_partition(char* backup_path, char* root, char* name) {
140 int ret = 0;
141 char mount_point[PATH_MAX];
142 sprintf(mount_point, "/%s", name);
143
144 ui_print("Restoring %s...\n", name);
145 if (0 != (ret = ensure_root_path_unmounted(root))) {
146 ui_print("Can't unmount %s!\n", mount_point);
147 return ret;
148 }
149 if (0 != (ret = format_root_device(root))) {
150 ui_print("Error while formatting %s!\n", root);
151 return ret;
152 }
153
154 if (0 != (ret = ensure_root_path_mounted(root))) {
155 ui_print("Can't mount %s!\n", mount_point);
156 return ret;
157 }
158
159 char tmp[PATH_MAX];
160 sprintf(tmp, "%s/%s.img", backup_path, name);
161 if (0 != (ret = unyaffs(tmp, mount_point, yaffs_callback))) {
162 ui_print("Error while restoring %s!\n", mount_point);
163 return ret;
164 }
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800165 return 0;
166}
167
168int nandroid_restore(char* backup_path)
169{
170 ui_set_background(BACKGROUND_ICON_INSTALLING);
171 ui_show_indeterminate_progress();
172 yaffs_files_total = 0;
173
174 if (ensure_root_path_mounted("SDCARD:") != 0)
175 return print_and_error("Can't mount /sdcard\n");
176
177 char tmp[PATH_MAX];
178
179 ui_print("Checking MD5 sums...\n");
Koushik K. Duttaface5882010-03-13 10:15:55 -0800180 sprintf(tmp, "cd %s && md5sum -c nandroid.md5", backup_path);
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800181 if (0 != __system(tmp))
182 return print_and_error("MD5 mismatch!\n");
183
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700184 int ret;
Koushik K. Duttaf7215942010-03-16 13:34:51 -0700185 sprintf(tmp, "flash_image boot %s/boot.img", backup_path);
186 ui_print("Restoring boot image...\n");
187 if (0 != (ret = __system(tmp))) {
188 ui_print("Error while flashing boot image!");
189 return ret;
190 }
191
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700192 if (0 != (ret = nandroid_restore_partition(backup_path, "SYSTEM:", "system")))
193 return ret;
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800194
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700195 if (0 != (ret = nandroid_restore_partition(backup_path, "DATA:", "data")))
196 return ret;
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800197
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700198 if (0 != (ret = nandroid_restore_partition(backup_path, "CACHE:", "cache")))
199 return ret;
200
Koushik K. Dutta3f38f322010-03-12 23:45:25 -0800201 sync();
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800202 ui_set_background(BACKGROUND_ICON_NONE);
203 ui_reset_progress();
Koushik K. Dutta4b249cd2010-03-15 16:31:03 -0700204 ui_print("\nRestore complete!\n");
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800205 return 0;
206}