summaryrefslogtreecommitdiff
path: root/kcmdlinectrl
diff options
context:
space:
mode:
authorAlice Ryhl <aliceryhl@google.com>2024-01-12 13:43:24 +0000
committerAlice Ryhl <aliceryhl@google.com>2024-01-12 15:16:01 +0000
commit15a8d79bd9163fc6e05537ddb86a1e7a02833397 (patch)
tree9599305d2a251303596b1893879e0b752f6c235c /kcmdlinectrl
parente5dbfd7ee6eb148e638a45985c3c8d14b672d7d2 (diff)
downloadextras-15a8d79bd9163fc6e05537ddb86a1e7a02833397.tar.gz
Add kcmdlinectrl for setting the kcmdline bootloader message
This utility makes it possible to read and write to the kcmdline bootloader message. This message can be used to tell the bootloader to set kcmdline flags for enabling features in the kernel. Test: Verified that a custom bootloader is able to read the data Bug: 278052745 Change-Id: I5f13a9bdff940517cb7b880815dfb8f396fc3844 Signed-off-by: Alice Ryhl <aliceryhl@google.com>
Diffstat (limited to 'kcmdlinectrl')
l---------kcmdlinectrl/.clang-format1
-rw-r--r--kcmdlinectrl/Android.bp26
-rw-r--r--kcmdlinectrl/OWNERS1
-rw-r--r--kcmdlinectrl/kcmdlinectrl.cc92
4 files changed, 120 insertions, 0 deletions
diff --git a/kcmdlinectrl/.clang-format b/kcmdlinectrl/.clang-format
new file mode 120000
index 00000000..242a033c
--- /dev/null
+++ b/kcmdlinectrl/.clang-format
@@ -0,0 +1 @@
+../../core/.clang-format-2 \ No newline at end of file
diff --git a/kcmdlinectrl/Android.bp b/kcmdlinectrl/Android.bp
new file mode 100644
index 00000000..37ef1473
--- /dev/null
+++ b/kcmdlinectrl/Android.bp
@@ -0,0 +1,26 @@
+// 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.
+
+package {
+ default_applicable_licenses: ["Android-Apache-2.0"],
+}
+
+cc_binary {
+ name: "kcmdlinectrl",
+ srcs: ["kcmdlinectrl.cc"],
+ shared_libs: [
+ "libbootloader_message",
+ "libbase",
+ ],
+}
diff --git a/kcmdlinectrl/OWNERS b/kcmdlinectrl/OWNERS
new file mode 100644
index 00000000..ad8cee6c
--- /dev/null
+++ b/kcmdlinectrl/OWNERS
@@ -0,0 +1 @@
+aliceryhl@google.com
diff --git a/kcmdlinectrl/kcmdlinectrl.cc b/kcmdlinectrl/kcmdlinectrl.cc
new file mode 100644
index 00000000..48500d86
--- /dev/null
+++ b/kcmdlinectrl/kcmdlinectrl.cc
@@ -0,0 +1,92 @@
+/*
+ * 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.
+ */
+
+#include <getopt.h>
+#include <unistd.h>
+
+#include <android-base/file.h>
+#include <android-base/logging.h>
+#include <android-base/properties.h>
+#include <android-base/strings.h>
+#include <android-base/unique_fd.h>
+#include <bootloader_message/bootloader_message.h>
+
+#include <functional>
+#include <iostream>
+
+using namespace std;
+
+void PrintUsage(const char* progname) {
+ std::cerr << "USAGE: " << progname << " PROPERTY_NAME [VALUE]" << 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];
+ } else {
+ PrintUsage(*argv);
+ return 1;
+ }
+
+ 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, resetting it" << endl;
+ m = {.version = MISC_KCMDLINE_MESSAGE_VERSION,
+ .magic = MISC_KCMDLINE_MAGIC_HEADER,
+ .kcmdline_flags = 0};
+ }
+
+ 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")) {
+ m.kcmdline_flags |= MISC_KCMDLINE_BINDER_RUST;
+ } else if (!strcmp(new_value, "c")) {
+ m.kcmdline_flags &= !MISC_KCMDLINE_BINDER_RUST;
+ } else {
+ LOG(ERROR) << "Binder property can only by 'c' or 'rust', but got " << new_value << endl;
+ return 1;
+ }
+ } else {
+ LOG(ERROR) << "No such property name " << property_name << endl;
+ return 1;
+ }
+
+ if (!WriteMiscKcmdlineMessage(m, &err)) {
+ LOG(ERROR) << "Failed to write to misc: " << err << endl;
+ return 1;
+ }
+
+ return 0;
+}