summaryrefslogtreecommitdiff
path: root/kcmdlinectrl
diff options
context:
space:
mode:
authorAlice Ryhl <aliceryhl@google.com>2024-02-22 11:51:51 +0000
committerAlice Ryhl <aliceryhl@google.com>2024-03-06 12:04:50 +0000
commit83587fc705e567bb94178753e8cb1f555d0d14e4 (patch)
treea5969d3f4b78b13ec4ebe829ef4da19fc2a5a0a2 /kcmdlinectrl
parentc112a3df3a82a4bae979f8d29a438b5c657b10eb (diff)
downloadextras-83587fc705e567bb94178753e8cb1f555d0d14e4.tar.gz
kcmdlinectrl: add system property support
Test: Verified that setprop/getprop work and that the value is loaded properly at boot Bug: 326222756 Change-Id: Ida9f1792cdd450b8ee01d99076fee1499a6b0bce
Diffstat (limited to 'kcmdlinectrl')
-rw-r--r--kcmdlinectrl/Android.bp1
-rw-r--r--kcmdlinectrl/kcmdlinectrl.cc104
-rw-r--r--kcmdlinectrl/kcmdlinectrl.rc22
3 files changed, 108 insertions, 19 deletions
diff --git a/kcmdlinectrl/Android.bp b/kcmdlinectrl/Android.bp
index 37ef1473..745c824a 100644
--- a/kcmdlinectrl/Android.bp
+++ b/kcmdlinectrl/Android.bp
@@ -23,4 +23,5 @@ cc_binary {
"libbootloader_message",
"libbase",
],
+ init_rc: ["kcmdlinectrl.rc"],
}
diff --git a/kcmdlinectrl/kcmdlinectrl.cc b/kcmdlinectrl/kcmdlinectrl.cc
index 48500d86..5dd55af6 100644
--- a/kcmdlinectrl/kcmdlinectrl.cc
+++ b/kcmdlinectrl/kcmdlinectrl.cc
@@ -30,24 +30,63 @@
using namespace std;
void PrintUsage(const char* progname) {
- std::cerr << "USAGE: " << progname << " PROPERTY_NAME [VALUE]" << endl;
+ std::cerr << "USAGE: " << progname << " get [PROPERTY]" << endl;
+ std::cerr << " " << progname << " store [PROPERTY] [VALUE]" << endl;
+ std::cerr << " " << progname << " update-props" << endl;
}
-int main(int argc, char** argv) {
- char *property_name, *new_value;
- if (argc == 2) {
- // Read property.
- property_name = argv[1];
- new_value = NULL;
- } else if (argc == 3) {
- // Write property.
- property_name = argv[1];
- new_value = argv[2];
+int UpdateProps() {
+ misc_kcmdline_message m = {.version = MISC_KCMDLINE_MESSAGE_VERSION,
+ .magic = MISC_KCMDLINE_MAGIC_HEADER};
+ std::string err;
+ if (!ReadMiscKcmdlineMessage(&m, &err)) {
+ LOG(ERROR) << "Failed to read from misc: " << err << endl;
+ return 1;
+ }
+
+ // If invalid, treat it as-if all flags are zero.
+ if (m.magic != MISC_KCMDLINE_MAGIC_HEADER || m.version != MISC_KCMDLINE_MESSAGE_VERSION) {
+ m = {.version = MISC_KCMDLINE_MESSAGE_VERSION,
+ .magic = MISC_KCMDLINE_MAGIC_HEADER,
+ .kcmdline_flags = 0};
+ }
+
+ bool use_rust_binder = (m.kcmdline_flags & MISC_KCMDLINE_BINDER_RUST) != 0;
+ android::base::SetProperty("kcmdline.binder", use_rust_binder ? "rust" : "c");
+
+ android::base::SetProperty("kcmdline.loaded", "1");
+ return 0;
+}
+
+int PrintProperty(const char* property_name) {
+ misc_kcmdline_message m = {.version = MISC_KCMDLINE_MESSAGE_VERSION,
+ .magic = MISC_KCMDLINE_MAGIC_HEADER};
+
+ std::string err;
+ if (!ReadMiscKcmdlineMessage(&m, &err)) {
+ LOG(ERROR) << "Failed to read from misc: " << err << endl;
+ return 1;
+ }
+
+ if (m.magic != MISC_KCMDLINE_MAGIC_HEADER || m.version != MISC_KCMDLINE_MESSAGE_VERSION) {
+ cout << "kcmdline message is invalid, treating all flags as zero" << endl;
+ m = {.version = MISC_KCMDLINE_MESSAGE_VERSION,
+ .magic = MISC_KCMDLINE_MAGIC_HEADER,
+ .kcmdline_flags = 0};
+ }
+
+ if (!strcmp(property_name, "binder")) {
+ bool use_rust_binder = (m.kcmdline_flags & MISC_KCMDLINE_BINDER_RUST) != 0;
+ const char* binder_value = use_rust_binder ? "rust" : "c";
+ cout << "binder=" << binder_value << endl;
+ return 0;
} else {
- PrintUsage(*argv);
+ LOG(ERROR) << "Unknown property name: " << property_name << endl;
return 1;
}
+}
+int StoreProperty(const char* property_name, const char* new_value) {
misc_kcmdline_message m = {.version = MISC_KCMDLINE_MESSAGE_VERSION,
.magic = MISC_KCMDLINE_MAGIC_HEADER};
@@ -65,12 +104,7 @@ int main(int argc, char** argv) {
}
if (!strcmp(property_name, "binder")) {
- if (new_value == NULL) {
- bool use_rust_binder = (m.kcmdline_flags & MISC_KCMDLINE_BINDER_RUST) != 0;
- const char* binder_value = use_rust_binder ? "rust" : "c";
- cout << "binder=" << binder_value << endl;
- return 0;
- } else if (!strcmp(new_value, "rust")) {
+ if (!strcmp(new_value, "rust")) {
m.kcmdline_flags |= MISC_KCMDLINE_BINDER_RUST;
} else if (!strcmp(new_value, "c")) {
m.kcmdline_flags &= !MISC_KCMDLINE_BINDER_RUST;
@@ -79,7 +113,7 @@ int main(int argc, char** argv) {
return 1;
}
} else {
- LOG(ERROR) << "No such property name " << property_name << endl;
+ LOG(ERROR) << "Unknown property name: " << property_name << endl;
return 1;
}
@@ -90,3 +124,35 @@ int main(int argc, char** argv) {
return 0;
}
+
+int main(int argc, char** argv) {
+ char *action, *property_name, *new_value;
+
+ if (argc == 2) {
+ action = argv[1];
+ property_name = NULL;
+ new_value = NULL;
+ } else if (argc == 3) {
+ action = argv[1];
+ property_name = argv[2];
+ new_value = NULL;
+ } else if (argc == 4) {
+ action = argv[1];
+ property_name = argv[2];
+ new_value = argv[3];
+ } else {
+ PrintUsage(*argv);
+ return 1;
+ }
+
+ if (!strcmp(action, "update-props") && property_name == NULL) {
+ return UpdateProps();
+ } else if (!strcmp(action, "get") && property_name != NULL && new_value == NULL) {
+ return PrintProperty(property_name);
+ } else if (!strcmp(action, "store") && property_name != NULL && new_value != NULL) {
+ return StoreProperty(property_name, new_value);
+ } else {
+ PrintUsage(*argv);
+ return 1;
+ }
+}
diff --git a/kcmdlinectrl/kcmdlinectrl.rc b/kcmdlinectrl/kcmdlinectrl.rc
new file mode 100644
index 00000000..e0a3e7f0
--- /dev/null
+++ b/kcmdlinectrl/kcmdlinectrl.rc
@@ -0,0 +1,22 @@
+# Copyright (C) 2022 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# adbd gets initialized in init, so run before that. this makes sure that the
+# user does not change the value before we initialize it
+on early-boot
+ exec_background -- /system/bin/kcmdlinectrl update-props
+
+on property:kcmdline.binder=*
+ wait_for_prop kcmdline.loaded 1
+ exec -- /system/bin/kcmdlinectrl store binder ${kcmdline.binder:-c}