From 0917a7aedfeffc47e1eb3bc1139402d55051aaa5 Mon Sep 17 00:00:00 2001 From: Koushik Dutta Date: Fri, 23 Sep 2011 03:50:29 -0700 Subject: [PATCH 1/2] note usage of extendedcommand; Change-Id: Iab212b3a0cc5259630883c4783a28bcecd9e7028 --- edifyscripting.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/edifyscripting.c b/edifyscripting.c index 389f1c9..ddbc877 100644 --- a/edifyscripting.c +++ b/edifyscripting.c @@ -1,3 +1,6 @@ +// Copyright ClockworkMod, LLC. Reference and porting purposes only. Usage of the extendedcommand API +// is restricted to those granted explicit permission, or by use of the ROM Manager Recovery API. +// https://github.com/koush/TestRomManager #include #include #include From 95fb821c19ab13574732ab6b03df0bc597598efb Mon Sep 17 00:00:00 2001 From: Koushik Dutta Date: Fri, 23 Sep 2011 03:57:25 -0700 Subject: [PATCH 2/2] shuffle some code Change-Id: Ifb5afe8ef6de7b6de58dfc07a30c1f89b5c1eb08 --- edifyscripting.c | 130 +++++++++++++++++++++++++++++++++++++++++++++ extendedcommands.c | 128 -------------------------------------------- 2 files changed, 130 insertions(+), 128 deletions(-) diff --git a/edifyscripting.c b/edifyscripting.c index ddbc877..e66df2b 100644 --- a/edifyscripting.c +++ b/edifyscripting.c @@ -297,3 +297,133 @@ int run_script_from_buffer(char* script_data, int script_len, char* filename) } return 0; } + + + +#define EXTENDEDCOMMAND_SCRIPT "/cache/recovery/extendedcommand" + +int run_and_remove_extendedcommand() +{ + char tmp[PATH_MAX]; + sprintf(tmp, "cp %s /tmp/%s", EXTENDEDCOMMAND_SCRIPT, basename(EXTENDEDCOMMAND_SCRIPT)); + __system(tmp); + remove(EXTENDEDCOMMAND_SCRIPT); + int i = 0; + for (i = 20; i > 0; i--) { + ui_print("Waiting for SD Card to mount (%ds)\n", i); + if (ensure_path_mounted("/sdcard") == 0) { + ui_print("SD Card mounted...\n"); + break; + } + sleep(1); + } + remove("/sdcard/clockworkmod/.recoverycheckpoint"); + if (i == 0) { + ui_print("Timed out waiting for SD card... continuing anyways."); + } + + ui_print("Verifying SD Card marker...\n"); + struct stat st; + if (stat("/sdcard/clockworkmod/.salted_hash", &st) != 0) { + ui_print("SD Card marker not found...\n"); + if (volume_for_path("/emmc") != NULL) { + ui_print("Checking Internal SD Card marker...\n"); + ensure_path_unmounted("/sdcard"); + if (ensure_path_mounted_at_mount_point("/emmc", "/sdcard") != 0) { + ui_print("Internal SD Card marker not found... continuing anyways.\n"); + // unmount everything, and remount as normal + ensure_path_unmounted("/emmc"); + ensure_path_unmounted("/sdcard"); + + ensure_path_mounted("/sdcard"); + } + } + } + + sprintf(tmp, "/tmp/%s", basename(EXTENDEDCOMMAND_SCRIPT)); + int ret; +#ifdef I_AM_KOUSH + if (0 != (ret = before_run_script(tmp))) { + 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"); + return ret; + } +#endif + return run_script(tmp); +} + +int extendedcommand_file_exists() +{ + struct stat file_info; + return 0 == stat(EXTENDEDCOMMAND_SCRIPT, &file_info); +} + +int edify_main(int argc, char** argv) { + load_volume_table(); + process_volumes(); + RegisterBuiltins(); + RegisterRecoveryHooks(); + FinishRegistration(); + + if (argc != 2) { + printf("edify \n"); + return 1; + } + + FILE* f = fopen(argv[1], "r"); + if (f == NULL) { + printf("%s: %s: No such file or directory\n", argv[0], argv[1]); + return 1; + } + char buffer[8192]; + int size = fread(buffer, 1, 8191, f); + fclose(f); + buffer[size] = '\0'; + + Expr* root; + int error_count = 0; + yy_scan_bytes(buffer, size); + int error = yyparse(&root, &error_count); + printf("parse returned %d; %d errors encountered\n", error, error_count); + if (error == 0 || error_count > 0) { + + //ExprDump(0, root, buffer); + + State state; + state.cookie = NULL; + state.script = buffer; + state.errmsg = NULL; + + char* result = Evaluate(&state, root); + if (result == NULL) { + printf("result was NULL, message is: %s\n", + (state.errmsg == NULL ? "(NULL)" : state.errmsg)); + free(state.errmsg); + } else { + printf("result is [%s]\n", result); + } + } + return 0; +} + +int run_script(char* filename) +{ + struct stat file_info; + if (0 != stat(filename, &file_info)) { + printf("Error executing stat on file: %s\n", filename); + return 1; + } + + int script_len = file_info.st_size; + char* script_data = (char*)malloc(script_len + 1); + FILE *file = fopen(filename, "rb"); + fread(script_data, script_len, 1, file); + // supposedly not necessary, but let's be safe. + script_data[script_len] = '\0'; + fclose(file); + LOGI("Running script:\n"); + LOGI("\n%s\n", script_data); + + int ret = run_script_from_buffer(script_data, script_len, filename); + free(script_data); + return ret; +} diff --git a/extendedcommands.c b/extendedcommands.c index 9b4c5e9..2b6d5c3 100644 --- a/extendedcommands.c +++ b/extendedcommands.c @@ -713,134 +713,6 @@ void show_partition_menu() } -#define EXTENDEDCOMMAND_SCRIPT "/cache/recovery/extendedcommand" - -int extendedcommand_file_exists() -{ - struct stat file_info; - return 0 == stat(EXTENDEDCOMMAND_SCRIPT, &file_info); -} - -int edify_main(int argc, char** argv) { - load_volume_table(); - process_volumes(); - RegisterBuiltins(); - RegisterRecoveryHooks(); - FinishRegistration(); - - if (argc != 2) { - printf("edify \n"); - return 1; - } - - FILE* f = fopen(argv[1], "r"); - if (f == NULL) { - printf("%s: %s: No such file or directory\n", argv[0], argv[1]); - return 1; - } - char buffer[8192]; - int size = fread(buffer, 1, 8191, f); - fclose(f); - buffer[size] = '\0'; - - Expr* root; - int error_count = 0; - yy_scan_bytes(buffer, size); - int error = yyparse(&root, &error_count); - printf("parse returned %d; %d errors encountered\n", error, error_count); - if (error == 0 || error_count > 0) { - - //ExprDump(0, root, buffer); - - State state; - state.cookie = NULL; - state.script = buffer; - state.errmsg = NULL; - - char* result = Evaluate(&state, root); - if (result == NULL) { - printf("result was NULL, message is: %s\n", - (state.errmsg == NULL ? "(NULL)" : state.errmsg)); - free(state.errmsg); - } else { - printf("result is [%s]\n", result); - } - } - return 0; -} - -int run_script(char* filename) -{ - struct stat file_info; - if (0 != stat(filename, &file_info)) { - printf("Error executing stat on file: %s\n", filename); - return 1; - } - - int script_len = file_info.st_size; - char* script_data = (char*)malloc(script_len + 1); - FILE *file = fopen(filename, "rb"); - fread(script_data, script_len, 1, file); - // supposedly not necessary, but let's be safe. - script_data[script_len] = '\0'; - fclose(file); - LOGI("Running script:\n"); - LOGI("\n%s\n", script_data); - - int ret = run_script_from_buffer(script_data, script_len, filename); - free(script_data); - return ret; -} - -int run_and_remove_extendedcommand() -{ - char tmp[PATH_MAX]; - sprintf(tmp, "cp %s /tmp/%s", EXTENDEDCOMMAND_SCRIPT, basename(EXTENDEDCOMMAND_SCRIPT)); - __system(tmp); - remove(EXTENDEDCOMMAND_SCRIPT); - int i = 0; - for (i = 20; i > 0; i--) { - ui_print("Waiting for SD Card to mount (%ds)\n", i); - if (ensure_path_mounted("/sdcard") == 0) { - ui_print("SD Card mounted...\n"); - break; - } - sleep(1); - } - remove("/sdcard/clockworkmod/.recoverycheckpoint"); - if (i == 0) { - ui_print("Timed out waiting for SD card... continuing anyways."); - } - - ui_print("Verifying SD Card marker...\n"); - struct stat st; - if (stat("/sdcard/clockworkmod/.salted_hash", &st) != 0) { - ui_print("SD Card marker not found...\n"); - if (volume_for_path("/emmc") != NULL) { - ui_print("Checking Internal SD Card marker...\n"); - ensure_path_unmounted("/sdcard"); - if (ensure_path_mounted_at_mount_point("/emmc", "/sdcard") != 0) { - ui_print("Internal SD Card marker not found... continuing anyways.\n"); - // unmount everything, and remount as normal - ensure_path_unmounted("/emmc"); - ensure_path_unmounted("/sdcard"); - - ensure_path_mounted("/sdcard"); - } - } - } - - sprintf(tmp, "/tmp/%s", basename(EXTENDEDCOMMAND_SCRIPT)); - int ret; -#ifdef I_AM_KOUSH - if (0 != (ret = before_run_script(tmp))) { - 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"); - return ret; - } -#endif - return run_script(tmp); -} - void show_nandroid_advanced_restore_menu() { if (ensure_path_mounted("/sdcard") != 0) {