Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 1 | #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. Dutta | 7868629 | 2010-03-25 17:58:45 -0700 | [diff] [blame] | 40 | #include <sys/vfs.h> |
| 41 | |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 42 | #include "extendedcommands.h" |
| 43 | #include "nandroid.h" |
| 44 | |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 45 | int print_and_error(char* message) { |
| 46 | ui_print(message); |
| 47 | return 1; |
| 48 | } |
| 49 | |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 50 | int yaffs_files_total = 0; |
| 51 | int yaffs_files_count = 0; |
| 52 | void yaffs_callback(char* filename) |
| 53 | { |
| 54 | char* justfile = basename(filename); |
| 55 | if (strlen(justfile) < 30) |
Koushik K. Dutta | 3fb8d30 | 2010-03-15 17:05:27 -0700 | [diff] [blame] | 56 | ui_print(justfile); |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 57 | 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 | |
| 63 | void 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 Dutta | 2f73e58 | 2010-04-18 16:00:21 -0700 | [diff] [blame^] | 78 | int nandroid_backup_partition(const char* backup_path, char* root) { |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 79 | int ret = 0; |
| 80 | char mount_point[PATH_MAX]; |
Koushik K. Dutta | 68b0190 | 2010-04-01 12:20:39 -0700 | [diff] [blame] | 81 | translate_root_path(root, mount_point, PATH_MAX); |
| 82 | char* name = basename(mount_point); |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 83 | |
| 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 Dutta | 2f73e58 | 2010-04-18 16:00:21 -0700 | [diff] [blame^] | 101 | int nandroid_backup(const char* backup_path) |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 102 | { |
| 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. Dutta | 7868629 | 2010-03-25 17:58:45 -0700 | [diff] [blame] | 108 | 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. Dutta | 68b0190 | 2010-04-01 12:20:39 -0700 | [diff] [blame] | 112 | 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. Dutta | c097093 | 2010-03-25 23:19:19 -0700 | [diff] [blame] | 118 | ui_print("There may not be enough free space to complete backup... continuing...\n"); |
Koushik K. Dutta | 7868629 | 2010-03-25 17:58:45 -0700 | [diff] [blame] | 119 | |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 120 | char tmp[PATH_MAX]; |
| 121 | sprintf(tmp, "mkdir -p %s", backup_path); |
| 122 | __system(tmp); |
| 123 | |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 124 | 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. Dutta | 6923cc3 | 2010-03-29 14:46:00 -0700 | [diff] [blame] | 129 | |
| 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. Dutta | 68b0190 | 2010-04-01 12:20:39 -0700 | [diff] [blame] | 136 | if (0 != (ret = nandroid_backup_partition(backup_path, "SYSTEM:"))) |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 137 | return ret; |
| 138 | |
Koushik K. Dutta | 68b0190 | 2010-04-01 12:20:39 -0700 | [diff] [blame] | 139 | if (0 != (ret = nandroid_backup_partition(backup_path, "DATA:"))) |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 140 | return ret; |
| 141 | |
Koushik K. Dutta | 68b0190 | 2010-04-01 12:20:39 -0700 | [diff] [blame] | 142 | if (0 != (ret = nandroid_backup_partition(backup_path, "CACHE:"))) |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 143 | return ret; |
Koushik K. Dutta | 3f99539 | 2010-03-30 23:29:43 -0700 | [diff] [blame] | 144 | |
Koushik K. Dutta | 71ef11f | 2010-04-02 22:17:42 -0700 | [diff] [blame] | 145 | struct stat st; |
| 146 | if (0 != stat("/dev/block/mmcblk0p2", &st)) |
Koushik K. Dutta | 6771aca | 2010-04-03 23:28:39 -0700 | [diff] [blame] | 147 | { |
Koushik K. Dutta | 71ef11f | 2010-04-02 22:17:42 -0700 | [diff] [blame] | 148 | ui_print("No sd-ext found. Skipping backup of sd-ext.\n"); |
Koushik K. Dutta | 6771aca | 2010-04-03 23:28:39 -0700 | [diff] [blame] | 149 | } |
| 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. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 157 | |
Koushik K. Dutta | d717892 | 2010-03-24 14:38:40 -0700 | [diff] [blame] | 158 | ui_print("Generating md5 sum...\n"); |
Koushik K. Dutta | 68b0190 | 2010-04-01 12:20:39 -0700 | [diff] [blame] | 159 | sprintf(tmp, "nandroid-md5.sh %s", backup_path); |
Koushik K. Dutta | b0a2503 | 2010-03-24 10:34:38 -0700 | [diff] [blame] | 160 | if (0 != (ret = __system(tmp))) { |
| 161 | ui_print("Error while generating md5 sum!\n"); |
| 162 | return ret; |
| 163 | } |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 164 | |
Koushik K. Dutta | 3f38f32 | 2010-03-12 23:45:25 -0800 | [diff] [blame] | 165 | sync(); |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 166 | ui_set_background(BACKGROUND_ICON_NONE); |
| 167 | ui_reset_progress(); |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 168 | ui_print("\nBackup complete!\n"); |
| 169 | return 0; |
| 170 | } |
| 171 | |
Koushik K. Dutta | 68b0190 | 2010-04-01 12:20:39 -0700 | [diff] [blame] | 172 | typedef int (*format_function)(char* root); |
| 173 | |
Koushik Dutta | 2f73e58 | 2010-04-18 16:00:21 -0700 | [diff] [blame^] | 174 | int nandroid_restore_partition(const char* backup_path, const char* root) { |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 175 | int ret = 0; |
| 176 | char mount_point[PATH_MAX]; |
Koushik K. Dutta | 68b0190 | 2010-04-01 12:20:39 -0700 | [diff] [blame] | 177 | translate_root_path(root, mount_point, PATH_MAX); |
| 178 | char* name = basename(mount_point); |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 179 | |
| 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 Dutta | 2f73e58 | 2010-04-18 16:00:21 -0700 | [diff] [blame^] | 185 | if (0 != (ret = format_root_device(root))) { |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 186 | 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 Dutta | 2f73e58 | 2010-04-18 16:00:21 -0700 | [diff] [blame^] | 201 | |
| 202 | if (0 != strcmp(root, "CACHE")) { |
| 203 | ensure_root_path_unmounted(root); |
| 204 | } |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 205 | return 0; |
| 206 | } |
| 207 | |
Koushik Dutta | 2f73e58 | 2010-04-18 16:00:21 -0700 | [diff] [blame^] | 208 | int nandroid_restore(const char* backup_path, int restore_boot, int restore_system, int restore_data, int restore_cache, int restore_sdext) |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 209 | { |
| 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. Dutta | face588 | 2010-03-13 10:15:55 -0800 | [diff] [blame] | 220 | sprintf(tmp, "cd %s && md5sum -c nandroid.md5", backup_path); |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 221 | if (0 != __system(tmp)) |
| 222 | return print_and_error("MD5 mismatch!\n"); |
| 223 | |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 224 | int ret; |
Koushik K. Dutta | fe84a7f | 2010-03-25 18:19:23 -0700 | [diff] [blame] | 225 | 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. Dutta | f721594 | 2010-03-16 13:34:51 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Koushik K. Dutta | 68b0190 | 2010-04-01 12:20:39 -0700 | [diff] [blame] | 235 | if (restore_system && 0 != (ret = nandroid_restore_partition(backup_path, "SYSTEM:"))) |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 236 | return ret; |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 237 | |
Koushik K. Dutta | 68b0190 | 2010-04-01 12:20:39 -0700 | [diff] [blame] | 238 | if (restore_data && 0 != (ret = nandroid_restore_partition(backup_path, "DATA:"))) |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 239 | return ret; |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 240 | |
Koushik K. Dutta | 68b0190 | 2010-04-01 12:20:39 -0700 | [diff] [blame] | 241 | if (restore_cache && 0 != (ret = nandroid_restore_partition(backup_path, "CACHE:"))) |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 242 | return ret; |
| 243 | |
Koushik K. Dutta | 68b0190 | 2010-04-01 12:20:39 -0700 | [diff] [blame] | 244 | 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 Dutta | 2f73e58 | 2010-04-18 16:00:21 -0700 | [diff] [blame^] | 250 | else if (0 != (ret = nandroid_restore_partition(backup_path, "SDEXT:"))) |
Koushik K. Dutta | 68b0190 | 2010-04-01 12:20:39 -0700 | [diff] [blame] | 251 | return ret; |
| 252 | } |
| 253 | |
Koushik K. Dutta | 3f38f32 | 2010-03-12 23:45:25 -0800 | [diff] [blame] | 254 | sync(); |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 255 | ui_set_background(BACKGROUND_ICON_NONE); |
| 256 | ui_reset_progress(); |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 257 | ui_print("\nRestore complete!\n"); |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 258 | return 0; |
| 259 | } |