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 K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 78 | int nandroid_backup_partition(char* backup_path, char* root, char* name) { |
| 79 | int ret = 0; |
| 80 | char mount_point[PATH_MAX]; |
| 81 | sprintf(mount_point, "/%s", name); |
| 82 | |
| 83 | ui_print("Backing up %s...\n", name); |
| 84 | if (0 != (ret = ensure_root_path_mounted(root) != 0)) { |
| 85 | ui_print("Can't mount %s!\n", mount_point); |
| 86 | return ret; |
| 87 | } |
| 88 | compute_directory_stats(mount_point); |
| 89 | char tmp[PATH_MAX]; |
| 90 | sprintf(tmp, "%s/%s.img", backup_path, name); |
| 91 | ret = mkyaffs2image(mount_point, tmp, 0, yaffs_callback); |
| 92 | ensure_root_path_unmounted(root); |
| 93 | if (0 != ret) { |
| 94 | ui_print("Error while making a yaffs2 image of %s!\n", mount_point); |
| 95 | return ret; |
| 96 | } |
| 97 | return 0; |
| 98 | } |
| 99 | |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 100 | int nandroid_backup(char* backup_path) |
| 101 | { |
| 102 | ui_set_background(BACKGROUND_ICON_INSTALLING); |
| 103 | |
| 104 | if (ensure_root_path_mounted("SDCARD:") != 0) |
| 105 | return print_and_error("Can't mount /sdcard\n"); |
| 106 | |
Koushik K. Dutta | 7868629 | 2010-03-25 17:58:45 -0700 | [diff] [blame] | 107 | int ret; |
| 108 | struct statfs s; |
| 109 | if (0 != (ret = statfs("/sdcard", &s))) |
| 110 | return print_and_error("Unable to stat /sdcard\n"); |
Koushik K. Dutta | c097093 | 2010-03-25 23:19:19 -0700 | [diff] [blame] | 111 | long sdcard_free = s.f_bavail * s.f_bsize; |
Koushik K. Dutta | 04159f7 | 2010-03-25 19:04:08 -0700 | [diff] [blame] | 112 | if (sdcard_free < 150000000L) |
Koushik K. Dutta | c097093 | 2010-03-25 23:19:19 -0700 | [diff] [blame] | 113 | ui_print("There may not be enough free space to complete backup... continuing...\n"); |
| 114 | // return print_and_error("There is not enough free space on the SD Card! At least 150MB is required to create a backup.\n"); |
Koushik K. Dutta | 7868629 | 2010-03-25 17:58:45 -0700 | [diff] [blame] | 115 | |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 116 | char tmp[PATH_MAX]; |
| 117 | sprintf(tmp, "mkdir -p %s", backup_path); |
| 118 | __system(tmp); |
| 119 | |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 120 | ui_print("Backing up boot...\n"); |
| 121 | sprintf(tmp, "%s/%s", backup_path, "boot.img"); |
| 122 | ret = dump_image("boot", tmp, NULL); |
| 123 | if (0 != ret) |
| 124 | return print_and_error("Error while dumping boot image!\n"); |
Koushik K. Dutta | 6923cc3 | 2010-03-29 14:46:00 -0700 | [diff] [blame] | 125 | |
| 126 | ui_print("Backing up recovery...\n"); |
| 127 | sprintf(tmp, "%s/%s", backup_path, "recovery.img"); |
| 128 | ret = dump_image("recovery", tmp, NULL); |
| 129 | if (0 != ret) |
| 130 | return print_and_error("Error while dumping boot image!\n"); |
| 131 | |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 132 | if (0 != (ret = nandroid_backup_partition(backup_path, "SYSTEM:", "system"))) |
| 133 | return ret; |
| 134 | |
| 135 | if (0 != (ret = nandroid_backup_partition(backup_path, "DATA:", "data"))) |
| 136 | return ret; |
| 137 | |
| 138 | if (0 != (ret = nandroid_backup_partition(backup_path, "CACHE:", "cache"))) |
| 139 | return ret; |
Koushik K. Dutta | 3f99539 | 2010-03-30 23:29:43 -0700 | [diff] [blame^] | 140 | |
| 141 | if (0 != ensure_root_path_mounted("SDEXT:")) |
| 142 | ui_print("No sd-ext found. Skipping backup.\n"); |
| 143 | else if (0 != (ret = nandroid_backup_partition(backup_path, "SDEXT:", "sd-ext"))) |
| 144 | return ret; |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 145 | |
Koushik K. Dutta | d717892 | 2010-03-24 14:38:40 -0700 | [diff] [blame] | 146 | ui_print("Generating md5 sum...\n"); |
| 147 | sprintf(tmp, "cd %s && (md5sum *img > nandroid.md5)", backup_path); |
Koushik K. Dutta | b0a2503 | 2010-03-24 10:34:38 -0700 | [diff] [blame] | 148 | if (0 != (ret = __system(tmp))) { |
| 149 | ui_print("Error while generating md5 sum!\n"); |
| 150 | return ret; |
| 151 | } |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 152 | |
Koushik K. Dutta | 3f38f32 | 2010-03-12 23:45:25 -0800 | [diff] [blame] | 153 | sync(); |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 154 | ui_set_background(BACKGROUND_ICON_NONE); |
| 155 | ui_reset_progress(); |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 156 | ui_print("\nBackup complete!\n"); |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | int nandroid_restore_partition(char* backup_path, char* root, char* name) { |
| 161 | int ret = 0; |
| 162 | char mount_point[PATH_MAX]; |
| 163 | sprintf(mount_point, "/%s", name); |
| 164 | |
| 165 | ui_print("Restoring %s...\n", name); |
| 166 | if (0 != (ret = ensure_root_path_unmounted(root))) { |
| 167 | ui_print("Can't unmount %s!\n", mount_point); |
| 168 | return ret; |
| 169 | } |
| 170 | if (0 != (ret = format_root_device(root))) { |
| 171 | ui_print("Error while formatting %s!\n", root); |
| 172 | return ret; |
| 173 | } |
| 174 | |
| 175 | if (0 != (ret = ensure_root_path_mounted(root))) { |
| 176 | ui_print("Can't mount %s!\n", mount_point); |
| 177 | return ret; |
| 178 | } |
| 179 | |
| 180 | char tmp[PATH_MAX]; |
| 181 | sprintf(tmp, "%s/%s.img", backup_path, name); |
| 182 | if (0 != (ret = unyaffs(tmp, mount_point, yaffs_callback))) { |
| 183 | ui_print("Error while restoring %s!\n", mount_point); |
| 184 | return ret; |
| 185 | } |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 186 | return 0; |
| 187 | } |
| 188 | |
Koushik K. Dutta | fe84a7f | 2010-03-25 18:19:23 -0700 | [diff] [blame] | 189 | int nandroid_restore(char* backup_path, int restore_boot, int restore_system, int restore_data, int restore_cache) |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 190 | { |
| 191 | ui_set_background(BACKGROUND_ICON_INSTALLING); |
| 192 | ui_show_indeterminate_progress(); |
| 193 | yaffs_files_total = 0; |
| 194 | |
| 195 | if (ensure_root_path_mounted("SDCARD:") != 0) |
| 196 | return print_and_error("Can't mount /sdcard\n"); |
| 197 | |
| 198 | char tmp[PATH_MAX]; |
| 199 | |
| 200 | ui_print("Checking MD5 sums...\n"); |
Koushik K. Dutta | face588 | 2010-03-13 10:15:55 -0800 | [diff] [blame] | 201 | sprintf(tmp, "cd %s && md5sum -c nandroid.md5", backup_path); |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 202 | if (0 != __system(tmp)) |
| 203 | return print_and_error("MD5 mismatch!\n"); |
| 204 | |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 205 | int ret; |
Koushik K. Dutta | fe84a7f | 2010-03-25 18:19:23 -0700 | [diff] [blame] | 206 | if (restore_boot) |
| 207 | { |
| 208 | sprintf(tmp, "flash_image boot %s/boot.img", backup_path); |
| 209 | ui_print("Restoring boot image...\n"); |
| 210 | if (0 != (ret = __system(tmp))) { |
| 211 | ui_print("Error while flashing boot image!"); |
| 212 | return ret; |
| 213 | } |
Koushik K. Dutta | f721594 | 2010-03-16 13:34:51 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Koushik K. Dutta | fe84a7f | 2010-03-25 18:19:23 -0700 | [diff] [blame] | 216 | if (restore_system && 0 != (ret = nandroid_restore_partition(backup_path, "SYSTEM:", "system"))) |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 217 | return ret; |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 218 | |
Koushik K. Dutta | fe84a7f | 2010-03-25 18:19:23 -0700 | [diff] [blame] | 219 | if (restore_data && 0 != (ret = nandroid_restore_partition(backup_path, "DATA:", "data"))) |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 220 | return ret; |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 221 | |
Koushik K. Dutta | fe84a7f | 2010-03-25 18:19:23 -0700 | [diff] [blame] | 222 | if (restore_cache && 0 != (ret = nandroid_restore_partition(backup_path, "CACHE:", "cache"))) |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 223 | return ret; |
| 224 | |
Koushik K. Dutta | 3f38f32 | 2010-03-12 23:45:25 -0800 | [diff] [blame] | 225 | sync(); |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 226 | ui_set_background(BACKGROUND_ICON_NONE); |
| 227 | ui_reset_progress(); |
Koushik K. Dutta | 4b249cd | 2010-03-15 16:31:03 -0700 | [diff] [blame] | 228 | ui_print("\nRestore complete!\n"); |
Koushik K. Dutta | ee57bbc | 2010-03-12 23:21:12 -0800 | [diff] [blame] | 229 | return 0; |
| 230 | } |