From 1bf1772625392efe721e432681418548b9343bdd Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Thu, 3 Nov 2016 23:52:01 -0700 Subject: DO NOT MERGE updater: Add "write_value()" function. write_value(value, filename) writes 'value' to 'filename'. It can be used to tune device settings when applying an OTA package. For example, write_value("960000", "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq"). Bug: 32463933 Test: recovery_component_test passes. Test: Apply an OTA package that contains a call to write_value(), and check the result. Change-Id: Ib009ecb8a45a94353f10c59e2383fe1f49796e35 (cherry picked from commit d0f3088aa95e255b39ed4b83da6b08866c2c3e0c) --- updater/install.cpp | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/updater/install.cpp b/updater/install.cpp index 005f9f97d..74feb56be 100644 --- a/updater/install.cpp +++ b/updater/install.cpp @@ -35,8 +35,10 @@ #include #include +#include #include +#include #include #include #include @@ -966,7 +968,6 @@ Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) { return StringValue(strdup(value)); } - // file_getprop(file, key) // // interprets 'file' as a getprop-style file (key=value pairs, one @@ -1428,6 +1429,31 @@ Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) { return v; } +// write_value(value, filename) +// Writes 'value' to 'filename'. +// Example: write_value("960000", "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq") +Value* WriteValueFn(const char* name, State* state, int argc, Expr* argv[]) { + if (argc != 2) { + return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc); + } + + char* value; + char* filename; + if (ReadArgs(state, argv, 2, &value, &filename) < 0) { + return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", + name); + } + + bool ret = android::base::WriteStringToFile(value, filename); + if (!ret) { + printf("%s: Failed to write to \"%s\": %s\n", name, filename, strerror(errno)); + } + + free(value); + free(filename); + return StringValue(strdup(ret ? "t" : "")); +} + // Immediately reboot the device. Recovery is not finished normally, // so if you reboot into recovery it will re-start applying the // current package (because nothing has cleared the copy of the @@ -1627,6 +1653,7 @@ void RegisterInstallFunctions() { RegisterFunction("read_file", ReadFileFn); RegisterFunction("sha1_check", Sha1CheckFn); RegisterFunction("rename", RenameFn); + RegisterFunction("write_value", WriteValueFn); RegisterFunction("wipe_cache", WipeCacheFn); -- cgit v1.2.3