| Koushik Dutta | 08bedb2 | 2012-04-10 13:23:33 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| Koushik Dutta | bec0995 | 2010-12-19 20:37:57 -0800 | [diff] [blame] | 16 | #include <ctype.h> |
| 17 | #include <errno.h> |
| 18 | #include <fcntl.h> |
| 19 | #include <getopt.h> |
| 20 | #include <limits.h> |
| 21 | #include <linux/input.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <sys/reboot.h> |
| 26 | #include <sys/types.h> |
| 27 | #include <time.h> |
| 28 | #include <unistd.h> |
| 29 | |
| 30 | #include <sys/wait.h> |
| 31 | #include <sys/limits.h> |
| 32 | #include <dirent.h> |
| 33 | #include <sys/stat.h> |
| 34 | |
| 35 | #include <signal.h> |
| 36 | #include <sys/wait.h> |
| 37 | |
| 38 | #include "bootloader.h" |
| 39 | #include "common.h" |
| 40 | #include "cutils/properties.h" |
| 41 | #include "firmware.h" |
| 42 | #include "install.h" |
| 43 | #include "minui/minui.h" |
| 44 | #include "minzip/DirUtil.h" |
| 45 | #include "roots.h" |
| 46 | #include "recovery_ui.h" |
| 47 | |
| Koushik Dutta | bec0995 | 2010-12-19 20:37:57 -0800 | [diff] [blame] | 48 | #include "extendedcommands.h" |
| 49 | #include "nandroid.h" |
| 50 | #include "mounts.h" |
| 51 | #include "flashutils/flashutils.h" |
| 52 | #include "edify/expr.h" |
| 53 | #include "mtdutils/mtdutils.h" |
| 54 | #include "mmcutils/mmcutils.h" |
| 55 | //#include "edify/parser.h" |
| 56 | |
| 57 | Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 58 | char** args = ReadVarArgs(state, argc, argv); |
| 59 | if (args == NULL) { |
| 60 | return NULL; |
| 61 | } |
| 62 | |
| 63 | int size = 0; |
| 64 | int i; |
| 65 | for (i = 0; i < argc; ++i) { |
| 66 | size += strlen(args[i]); |
| 67 | } |
| 68 | char* buffer = malloc(size+1); |
| 69 | size = 0; |
| 70 | for (i = 0; i < argc; ++i) { |
| 71 | strcpy(buffer+size, args[i]); |
| 72 | size += strlen(args[i]); |
| 73 | free(args[i]); |
| 74 | } |
| 75 | free(args); |
| 76 | buffer[size] = '\0'; |
| 77 | |
| 78 | char* line = strtok(buffer, "\n"); |
| 79 | while (line) { |
| 80 | ui_print("%s\n", line); |
| 81 | line = strtok(NULL, "\n"); |
| 82 | } |
| 83 | |
| 84 | return StringValue(buffer); |
| 85 | } |
| 86 | |
| 87 | Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 88 | if (argc < 1) { |
| 89 | return ErrorAbort(state, "%s() expects at least 1 arg", name); |
| 90 | } |
| 91 | char** args = ReadVarArgs(state, argc, argv); |
| 92 | if (args == NULL) { |
| 93 | return NULL; |
| 94 | } |
| 95 | |
| 96 | char** args2 = malloc(sizeof(char*) * (argc+1)); |
| 97 | memcpy(args2, args, sizeof(char*) * argc); |
| 98 | args2[argc] = NULL; |
| 99 | |
| 100 | fprintf(stderr, "about to run program [%s] with %d args\n", args2[0], argc); |
| 101 | |
| 102 | pid_t child = fork(); |
| 103 | if (child == 0) { |
| 104 | execv(args2[0], args2); |
| 105 | fprintf(stderr, "run_program: execv failed: %s\n", strerror(errno)); |
| 106 | _exit(1); |
| 107 | } |
| 108 | int status; |
| 109 | waitpid(child, &status, 0); |
| 110 | if (WIFEXITED(status)) { |
| 111 | if (WEXITSTATUS(status) != 0) { |
| 112 | fprintf(stderr, "run_program: child exited with status %d\n", |
| 113 | WEXITSTATUS(status)); |
| 114 | } |
| 115 | } else if (WIFSIGNALED(status)) { |
| 116 | fprintf(stderr, "run_program: child terminated by signal %d\n", |
| 117 | WTERMSIG(status)); |
| 118 | } |
| 119 | |
| 120 | int i; |
| 121 | for (i = 0; i < argc; ++i) { |
| 122 | free(args[i]); |
| 123 | } |
| 124 | free(args); |
| 125 | free(args2); |
| 126 | |
| 127 | char buffer[20]; |
| 128 | sprintf(buffer, "%d", status); |
| 129 | |
| 130 | return StringValue(strdup(buffer)); |
| 131 | } |
| 132 | |
| 133 | Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 134 | char* result = NULL; |
| 135 | if (argc != 1) { |
| 136 | return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc); |
| 137 | } |
| 138 | |
| 139 | char *path; |
| 140 | if (ReadArgs(state, argv, 1, &path) < 0) { |
| 141 | return NULL; |
| 142 | } |
| 143 | |
| 144 | ui_print("Formatting %s...\n", path); |
| 145 | if (0 != format_volume(path)) { |
| 146 | free(path); |
| 147 | return StringValue(strdup("")); |
| 148 | } |
| Koushik Dutta | 33fd0d0 | 2011-02-27 12:42:24 -0800 | [diff] [blame] | 149 | |
| 150 | if (strcmp(path, "/data") == 0 && has_datadata()) { |
| 151 | ui_print("Formatting /datadata...\n", path); |
| 152 | if (0 != format_volume("/datadata")) { |
| 153 | free(path); |
| 154 | return StringValue(strdup("")); |
| 155 | } |
| Koushik Dutta | 7364e82 | 2011-04-23 19:01:16 -0700 | [diff] [blame] | 156 | if (0 != format_volume("/sdcard/.android_secure")) { |
| 157 | free(path); |
| 158 | return StringValue(strdup("")); |
| 159 | } |
| Koushik Dutta | 33fd0d0 | 2011-02-27 12:42:24 -0800 | [diff] [blame] | 160 | } |
| Koushik Dutta | bec0995 | 2010-12-19 20:37:57 -0800 | [diff] [blame] | 161 | |
| 162 | done: |
| 163 | return StringValue(strdup(path)); |
| 164 | } |
| 165 | |
| 166 | Value* BackupFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 167 | char* result = NULL; |
| 168 | if (argc != 1) { |
| 169 | return ErrorAbort(state, "%s() expects 1 args, got %d", name, argc); |
| 170 | } |
| 171 | char* path; |
| 172 | if (ReadArgs(state, argv, 1, &path) < 0) { |
| 173 | return NULL; |
| 174 | } |
| 175 | |
| 176 | if (0 != nandroid_backup(path)) |
| 177 | return StringValue(strdup("")); |
| 178 | |
| 179 | return StringValue(strdup(path)); |
| 180 | } |
| 181 | |
| 182 | Value* RestoreFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 183 | if (argc < 1) { |
| 184 | return ErrorAbort(state, "%s() expects at least 1 arg", name); |
| 185 | } |
| 186 | char** args = ReadVarArgs(state, argc, argv); |
| 187 | if (args == NULL) { |
| 188 | return NULL; |
| 189 | } |
| 190 | |
| 191 | char** args2 = malloc(sizeof(char*) * (argc+1)); |
| 192 | memcpy(args2, args, sizeof(char*) * argc); |
| 193 | args2[argc] = NULL; |
| 194 | |
| 195 | char* path = strdup(args2[0]); |
| 196 | int restoreboot = 1; |
| 197 | int restoresystem = 1; |
| 198 | int restoredata = 1; |
| 199 | int restorecache = 1; |
| 200 | int restoresdext = 1; |
| 201 | int i; |
| 202 | for (i = 1; i < argc; i++) |
| 203 | { |
| 204 | if (args2[i] == NULL) |
| 205 | continue; |
| 206 | if (strcmp(args2[i], "noboot") == 0) |
| 207 | restoreboot = 0; |
| 208 | else if (strcmp(args2[i], "nosystem") == 0) |
| 209 | restoresystem = 0; |
| 210 | else if (strcmp(args2[i], "nodata") == 0) |
| 211 | restoredata = 0; |
| 212 | else if (strcmp(args2[i], "nocache") == 0) |
| 213 | restorecache = 0; |
| 214 | else if (strcmp(args2[i], "nosd-ext") == 0) |
| 215 | restoresdext = 0; |
| 216 | } |
| 217 | |
| 218 | for (i = 0; i < argc; ++i) { |
| 219 | free(args[i]); |
| 220 | } |
| 221 | free(args); |
| 222 | free(args2); |
| 223 | |
| Koushik Dutta | 5b7f34a | 2010-12-29 23:36:03 -0800 | [diff] [blame] | 224 | if (0 != nandroid_restore(path, restoreboot, restoresystem, restoredata, restorecache, restoresdext, 0)) { |
| Koushik Dutta | bec0995 | 2010-12-19 20:37:57 -0800 | [diff] [blame] | 225 | free(path); |
| 226 | return StringValue(strdup("")); |
| 227 | } |
| 228 | |
| 229 | return StringValue(path); |
| 230 | } |
| 231 | |
| 232 | Value* InstallZipFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 233 | char* result = NULL; |
| 234 | if (argc != 1) { |
| 235 | return ErrorAbort(state, "%s() expects 1 args, got %d", name, argc); |
| 236 | } |
| 237 | char* path; |
| 238 | if (ReadArgs(state, argv, 1, &path) < 0) { |
| 239 | return NULL; |
| 240 | } |
| 241 | |
| 242 | if (0 != install_zip(path)) |
| 243 | return StringValue(strdup("")); |
| 244 | |
| 245 | return StringValue(strdup(path)); |
| 246 | } |
| 247 | |
| Koushik Dutta | 30a937a | 2011-09-05 21:14:06 -0700 | [diff] [blame] | 248 | Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 249 | char* result = NULL; |
| 250 | if (argc != 1) { |
| 251 | return ErrorAbort(state, "%s() expects 1 args, got %d", name, argc); |
| 252 | } |
| 253 | char* path; |
| 254 | if (ReadArgs(state, argv, 1, &path) < 0) { |
| 255 | return NULL; |
| 256 | } |
| 257 | |
| 258 | if (0 != ensure_path_mounted(path)) |
| 259 | return StringValue(strdup("")); |
| 260 | |
| 261 | return StringValue(strdup(path)); |
| 262 | } |
| 263 | |
| Koushik Dutta | bec0995 | 2010-12-19 20:37:57 -0800 | [diff] [blame] | 264 | void RegisterRecoveryHooks() { |
| Koushik Dutta | 30a937a | 2011-09-05 21:14:06 -0700 | [diff] [blame] | 265 | RegisterFunction("mount", MountFn); |
| Koushik Dutta | bec0995 | 2010-12-19 20:37:57 -0800 | [diff] [blame] | 266 | RegisterFunction("format", FormatFn); |
| 267 | RegisterFunction("ui_print", UIPrintFn); |
| 268 | RegisterFunction("run_program", RunProgramFn); |
| 269 | RegisterFunction("backup_rom", BackupFn); |
| 270 | RegisterFunction("restore_rom", RestoreFn); |
| 271 | RegisterFunction("install_zip", InstallZipFn); |
| 272 | } |
| 273 | |
| 274 | static int hasInitializedEdify = 0; |
| 275 | int run_script_from_buffer(char* script_data, int script_len, char* filename) |
| 276 | { |
| 277 | if (!hasInitializedEdify) { |
| 278 | RegisterBuiltins(); |
| 279 | RegisterRecoveryHooks(); |
| 280 | FinishRegistration(); |
| 281 | hasInitializedEdify = 1; |
| 282 | } |
| 283 | |
| 284 | Expr* root; |
| 285 | int error_count = 0; |
| 286 | yy_scan_bytes(script_data, script_len); |
| 287 | int error = yyparse(&root, &error_count); |
| 288 | printf("parse returned %d; %d errors encountered\n", error, error_count); |
| 289 | if (error == 0 || error_count > 0) { |
| 290 | //ExprDump(0, root, buffer); |
| 291 | |
| 292 | State state; |
| 293 | state.cookie = NULL; |
| 294 | state.script = script_data; |
| 295 | state.errmsg = NULL; |
| 296 | |
| 297 | char* result = Evaluate(&state, root); |
| 298 | if (result == NULL) { |
| 299 | printf("result was NULL, message is: %s\n", |
| 300 | (state.errmsg == NULL ? "(NULL)" : state.errmsg)); |
| 301 | free(state.errmsg); |
| 302 | return -1; |
| 303 | } else { |
| 304 | printf("result is [%s]\n", result); |
| 305 | } |
| 306 | } |
| 307 | return 0; |
| 308 | } |
| Koushik Dutta | 95fb821 | 2011-09-23 03:57:25 -0700 | [diff] [blame] | 309 | |
| 310 | |
| 311 | |
| 312 | #define EXTENDEDCOMMAND_SCRIPT "/cache/recovery/extendedcommand" |
| 313 | |
| 314 | int run_and_remove_extendedcommand() |
| 315 | { |
| 316 | char tmp[PATH_MAX]; |
| 317 | sprintf(tmp, "cp %s /tmp/%s", EXTENDEDCOMMAND_SCRIPT, basename(EXTENDEDCOMMAND_SCRIPT)); |
| 318 | __system(tmp); |
| 319 | remove(EXTENDEDCOMMAND_SCRIPT); |
| 320 | int i = 0; |
| 321 | for (i = 20; i > 0; i--) { |
| 322 | ui_print("Waiting for SD Card to mount (%ds)\n", i); |
| 323 | if (ensure_path_mounted("/sdcard") == 0) { |
| 324 | ui_print("SD Card mounted...\n"); |
| 325 | break; |
| 326 | } |
| 327 | sleep(1); |
| 328 | } |
| 329 | remove("/sdcard/clockworkmod/.recoverycheckpoint"); |
| 330 | if (i == 0) { |
| 331 | ui_print("Timed out waiting for SD card... continuing anyways."); |
| 332 | } |
| 333 | |
| 334 | ui_print("Verifying SD Card marker...\n"); |
| 335 | struct stat st; |
| 336 | if (stat("/sdcard/clockworkmod/.salted_hash", &st) != 0) { |
| 337 | ui_print("SD Card marker not found...\n"); |
| 338 | if (volume_for_path("/emmc") != NULL) { |
| 339 | ui_print("Checking Internal SD Card marker...\n"); |
| 340 | ensure_path_unmounted("/sdcard"); |
| 341 | if (ensure_path_mounted_at_mount_point("/emmc", "/sdcard") != 0) { |
| 342 | ui_print("Internal SD Card marker not found... continuing anyways.\n"); |
| 343 | // unmount everything, and remount as normal |
| 344 | ensure_path_unmounted("/emmc"); |
| 345 | ensure_path_unmounted("/sdcard"); |
| 346 | |
| 347 | ensure_path_mounted("/sdcard"); |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | sprintf(tmp, "/tmp/%s", basename(EXTENDEDCOMMAND_SCRIPT)); |
| 353 | int ret; |
| 354 | #ifdef I_AM_KOUSH |
| 355 | if (0 != (ret = before_run_script(tmp))) { |
| 356 | ui_print("Error processing ROM Manager script. Please verify that you are performing the backup, restore, or ROM installation from ROM Manager v4.4.0.0 or higher.\n"); |
| 357 | return ret; |
| 358 | } |
| 359 | #endif |
| 360 | return run_script(tmp); |
| 361 | } |
| 362 | |
| 363 | int extendedcommand_file_exists() |
| 364 | { |
| 365 | struct stat file_info; |
| 366 | return 0 == stat(EXTENDEDCOMMAND_SCRIPT, &file_info); |
| 367 | } |
| 368 | |
| 369 | int edify_main(int argc, char** argv) { |
| 370 | load_volume_table(); |
| 371 | process_volumes(); |
| 372 | RegisterBuiltins(); |
| 373 | RegisterRecoveryHooks(); |
| 374 | FinishRegistration(); |
| 375 | |
| 376 | if (argc != 2) { |
| 377 | printf("edify <filename>\n"); |
| 378 | return 1; |
| 379 | } |
| 380 | |
| 381 | FILE* f = fopen(argv[1], "r"); |
| 382 | if (f == NULL) { |
| 383 | printf("%s: %s: No such file or directory\n", argv[0], argv[1]); |
| 384 | return 1; |
| 385 | } |
| 386 | char buffer[8192]; |
| 387 | int size = fread(buffer, 1, 8191, f); |
| 388 | fclose(f); |
| 389 | buffer[size] = '\0'; |
| 390 | |
| 391 | Expr* root; |
| 392 | int error_count = 0; |
| 393 | yy_scan_bytes(buffer, size); |
| 394 | int error = yyparse(&root, &error_count); |
| 395 | printf("parse returned %d; %d errors encountered\n", error, error_count); |
| 396 | if (error == 0 || error_count > 0) { |
| 397 | |
| 398 | //ExprDump(0, root, buffer); |
| 399 | |
| 400 | State state; |
| 401 | state.cookie = NULL; |
| 402 | state.script = buffer; |
| 403 | state.errmsg = NULL; |
| 404 | |
| 405 | char* result = Evaluate(&state, root); |
| 406 | if (result == NULL) { |
| 407 | printf("result was NULL, message is: %s\n", |
| 408 | (state.errmsg == NULL ? "(NULL)" : state.errmsg)); |
| 409 | free(state.errmsg); |
| 410 | } else { |
| 411 | printf("result is [%s]\n", result); |
| 412 | } |
| 413 | } |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | int run_script(char* filename) |
| 418 | { |
| 419 | struct stat file_info; |
| 420 | if (0 != stat(filename, &file_info)) { |
| 421 | printf("Error executing stat on file: %s\n", filename); |
| 422 | return 1; |
| 423 | } |
| 424 | |
| 425 | int script_len = file_info.st_size; |
| 426 | char* script_data = (char*)malloc(script_len + 1); |
| 427 | FILE *file = fopen(filename, "rb"); |
| 428 | fread(script_data, script_len, 1, file); |
| 429 | // supposedly not necessary, but let's be safe. |
| 430 | script_data[script_len] = '\0'; |
| 431 | fclose(file); |
| 432 | LOGI("Running script:\n"); |
| 433 | LOGI("\n%s\n", script_data); |
| 434 | |
| 435 | int ret = run_script_from_buffer(script_data, script_len, filename); |
| 436 | free(script_data); |
| 437 | return ret; |
| 438 | } |