Alice Ryhl | 15a8d79 | 2024-01-12 13:43:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <getopt.h> |
| 18 | #include <unistd.h> |
| 19 | |
| 20 | #include <android-base/file.h> |
| 21 | #include <android-base/logging.h> |
| 22 | #include <android-base/properties.h> |
| 23 | #include <android-base/strings.h> |
| 24 | #include <android-base/unique_fd.h> |
| 25 | #include <bootloader_message/bootloader_message.h> |
| 26 | |
| 27 | #include <functional> |
| 28 | #include <iostream> |
| 29 | |
Alice Ryhl | 15a8d79 | 2024-01-12 13:43:24 +0000 | [diff] [blame] | 30 | void PrintUsage(const char* progname) { |
T.J. Mercier | f83c00f | 2024-03-07 18:23:16 +0000 | [diff] [blame] | 31 | std::cerr << "USAGE: " << progname << " get [PROPERTY]" << std::endl; |
| 32 | std::cerr << " " << progname << " store [PROPERTY] [VALUE]" << std::endl; |
| 33 | std::cerr << " " << progname << " update-props" << std::endl; |
Alice Ryhl | 15a8d79 | 2024-01-12 13:43:24 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Alice Ryhl | 83587fc | 2024-02-22 11:51:51 +0000 | [diff] [blame] | 36 | int UpdateProps() { |
| 37 | misc_kcmdline_message m = {.version = MISC_KCMDLINE_MESSAGE_VERSION, |
| 38 | .magic = MISC_KCMDLINE_MAGIC_HEADER}; |
| 39 | std::string err; |
| 40 | if (!ReadMiscKcmdlineMessage(&m, &err)) { |
T.J. Mercier | f83c00f | 2024-03-07 18:23:16 +0000 | [diff] [blame] | 41 | LOG(ERROR) << "Failed to read from misc: " << err; |
Alice Ryhl | 15a8d79 | 2024-01-12 13:43:24 +0000 | [diff] [blame] | 42 | return 1; |
| 43 | } |
| 44 | |
Alice Ryhl | 83587fc | 2024-02-22 11:51:51 +0000 | [diff] [blame] | 45 | // If invalid, treat it as-if all flags are zero. |
| 46 | if (m.magic != MISC_KCMDLINE_MAGIC_HEADER || m.version != MISC_KCMDLINE_MESSAGE_VERSION) { |
| 47 | m = {.version = MISC_KCMDLINE_MESSAGE_VERSION, |
| 48 | .magic = MISC_KCMDLINE_MAGIC_HEADER, |
| 49 | .kcmdline_flags = 0}; |
| 50 | } |
| 51 | |
| 52 | bool use_rust_binder = (m.kcmdline_flags & MISC_KCMDLINE_BINDER_RUST) != 0; |
| 53 | android::base::SetProperty("kcmdline.binder", use_rust_binder ? "rust" : "c"); |
| 54 | |
| 55 | android::base::SetProperty("kcmdline.loaded", "1"); |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | int PrintProperty(const char* property_name) { |
| 60 | misc_kcmdline_message m = {.version = MISC_KCMDLINE_MESSAGE_VERSION, |
| 61 | .magic = MISC_KCMDLINE_MAGIC_HEADER}; |
| 62 | |
| 63 | std::string err; |
| 64 | if (!ReadMiscKcmdlineMessage(&m, &err)) { |
T.J. Mercier | f83c00f | 2024-03-07 18:23:16 +0000 | [diff] [blame] | 65 | LOG(ERROR) << "Failed to read from misc: " << err; |
Alice Ryhl | 83587fc | 2024-02-22 11:51:51 +0000 | [diff] [blame] | 66 | return 1; |
| 67 | } |
| 68 | |
| 69 | if (m.magic != MISC_KCMDLINE_MAGIC_HEADER || m.version != MISC_KCMDLINE_MESSAGE_VERSION) { |
T.J. Mercier | f83c00f | 2024-03-07 18:23:16 +0000 | [diff] [blame] | 70 | std::cout << "kcmdline message is invalid, treating all flags as zero" << std::endl; |
Alice Ryhl | 83587fc | 2024-02-22 11:51:51 +0000 | [diff] [blame] | 71 | m = {.version = MISC_KCMDLINE_MESSAGE_VERSION, |
| 72 | .magic = MISC_KCMDLINE_MAGIC_HEADER, |
| 73 | .kcmdline_flags = 0}; |
| 74 | } |
| 75 | |
| 76 | if (!strcmp(property_name, "binder")) { |
| 77 | bool use_rust_binder = (m.kcmdline_flags & MISC_KCMDLINE_BINDER_RUST) != 0; |
| 78 | const char* binder_value = use_rust_binder ? "rust" : "c"; |
T.J. Mercier | f83c00f | 2024-03-07 18:23:16 +0000 | [diff] [blame] | 79 | std::cout << "binder=" << binder_value << std::endl; |
Alice Ryhl | 83587fc | 2024-02-22 11:51:51 +0000 | [diff] [blame] | 80 | return 0; |
| 81 | } else { |
T.J. Mercier | f83c00f | 2024-03-07 18:23:16 +0000 | [diff] [blame] | 82 | LOG(ERROR) << "Unknown property name: " << property_name; |
Alice Ryhl | 83587fc | 2024-02-22 11:51:51 +0000 | [diff] [blame] | 83 | return 1; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | int StoreProperty(const char* property_name, const char* new_value) { |
Alice Ryhl | 15a8d79 | 2024-01-12 13:43:24 +0000 | [diff] [blame] | 88 | misc_kcmdline_message m = {.version = MISC_KCMDLINE_MESSAGE_VERSION, |
| 89 | .magic = MISC_KCMDLINE_MAGIC_HEADER}; |
| 90 | |
| 91 | std::string err; |
| 92 | if (!ReadMiscKcmdlineMessage(&m, &err)) { |
T.J. Mercier | f83c00f | 2024-03-07 18:23:16 +0000 | [diff] [blame] | 93 | LOG(ERROR) << "Failed to read from misc: " << err; |
Alice Ryhl | 15a8d79 | 2024-01-12 13:43:24 +0000 | [diff] [blame] | 94 | return 1; |
| 95 | } |
| 96 | |
| 97 | if (m.magic != MISC_KCMDLINE_MAGIC_HEADER || m.version != MISC_KCMDLINE_MESSAGE_VERSION) { |
T.J. Mercier | f83c00f | 2024-03-07 18:23:16 +0000 | [diff] [blame] | 98 | std::cout << "kcmdline message is invalid, resetting it" << std::endl; |
Alice Ryhl | 15a8d79 | 2024-01-12 13:43:24 +0000 | [diff] [blame] | 99 | m = {.version = MISC_KCMDLINE_MESSAGE_VERSION, |
| 100 | .magic = MISC_KCMDLINE_MAGIC_HEADER, |
| 101 | .kcmdline_flags = 0}; |
| 102 | } |
| 103 | |
| 104 | if (!strcmp(property_name, "binder")) { |
Alice Ryhl | 83587fc | 2024-02-22 11:51:51 +0000 | [diff] [blame] | 105 | if (!strcmp(new_value, "rust")) { |
Alice Ryhl | 15a8d79 | 2024-01-12 13:43:24 +0000 | [diff] [blame] | 106 | m.kcmdline_flags |= MISC_KCMDLINE_BINDER_RUST; |
| 107 | } else if (!strcmp(new_value, "c")) { |
| 108 | m.kcmdline_flags &= !MISC_KCMDLINE_BINDER_RUST; |
| 109 | } else { |
T.J. Mercier | f83c00f | 2024-03-07 18:23:16 +0000 | [diff] [blame] | 110 | LOG(ERROR) << "Binder property can only be 'c' or 'rust', but got " << new_value; |
Alice Ryhl | 15a8d79 | 2024-01-12 13:43:24 +0000 | [diff] [blame] | 111 | return 1; |
| 112 | } |
| 113 | } else { |
T.J. Mercier | f83c00f | 2024-03-07 18:23:16 +0000 | [diff] [blame] | 114 | LOG(ERROR) << "Unknown property name: " << property_name; |
Alice Ryhl | 15a8d79 | 2024-01-12 13:43:24 +0000 | [diff] [blame] | 115 | return 1; |
| 116 | } |
| 117 | |
| 118 | if (!WriteMiscKcmdlineMessage(m, &err)) { |
T.J. Mercier | f83c00f | 2024-03-07 18:23:16 +0000 | [diff] [blame] | 119 | LOG(ERROR) << "Failed to write to misc: " << err; |
Alice Ryhl | 15a8d79 | 2024-01-12 13:43:24 +0000 | [diff] [blame] | 120 | return 1; |
| 121 | } |
| 122 | |
| 123 | return 0; |
| 124 | } |
Alice Ryhl | 83587fc | 2024-02-22 11:51:51 +0000 | [diff] [blame] | 125 | |
| 126 | int main(int argc, char** argv) { |
| 127 | char *action, *property_name, *new_value; |
| 128 | |
| 129 | if (argc == 2) { |
| 130 | action = argv[1]; |
| 131 | property_name = NULL; |
| 132 | new_value = NULL; |
| 133 | } else if (argc == 3) { |
| 134 | action = argv[1]; |
| 135 | property_name = argv[2]; |
| 136 | new_value = NULL; |
| 137 | } else if (argc == 4) { |
| 138 | action = argv[1]; |
| 139 | property_name = argv[2]; |
| 140 | new_value = argv[3]; |
| 141 | } else { |
| 142 | PrintUsage(*argv); |
| 143 | return 1; |
| 144 | } |
| 145 | |
| 146 | if (!strcmp(action, "update-props") && property_name == NULL) { |
| 147 | return UpdateProps(); |
| 148 | } else if (!strcmp(action, "get") && property_name != NULL && new_value == NULL) { |
| 149 | return PrintProperty(property_name); |
| 150 | } else if (!strcmp(action, "store") && property_name != NULL && new_value != NULL) { |
| 151 | return StoreProperty(property_name, new_value); |
| 152 | } else { |
| 153 | PrintUsage(*argv); |
| 154 | return 1; |
| 155 | } |
| 156 | } |