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