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
PrintUsage(const char * progname)30 void PrintUsage(const char* progname) {
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;
34 }
35
UpdateProps()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)) {
41 LOG(ERROR) << "Failed to read from misc: " << err;
42 return 1;
43 }
44
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
PrintProperty(const char * property_name)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)) {
65 LOG(ERROR) << "Failed to read from misc: " << err;
66 return 1;
67 }
68
69 if (m.magic != MISC_KCMDLINE_MAGIC_HEADER || m.version != MISC_KCMDLINE_MESSAGE_VERSION) {
70 std::cout << "kcmdline message is invalid, treating all flags as zero" << std::endl;
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";
79 std::cout << "binder=" << binder_value << std::endl;
80 return 0;
81 } else {
82 LOG(ERROR) << "Unknown property name: " << property_name;
83 return 1;
84 }
85 }
86
StoreProperty(const char * property_name,const char * new_value)87 int StoreProperty(const char* property_name, const char* new_value) {
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)) {
93 LOG(ERROR) << "Failed to read from misc: " << err;
94 return 1;
95 }
96
97 if (m.magic != MISC_KCMDLINE_MAGIC_HEADER || m.version != MISC_KCMDLINE_MESSAGE_VERSION) {
98 std::cout << "kcmdline message is invalid, resetting it" << std::endl;
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")) {
105 if (!strcmp(new_value, "rust")) {
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 {
110 LOG(ERROR) << "Binder property can only be 'c' or 'rust', but got " << new_value;
111 return 1;
112 }
113 } else {
114 LOG(ERROR) << "Unknown property name: " << property_name;
115 return 1;
116 }
117
118 if (!WriteMiscKcmdlineMessage(m, &err)) {
119 LOG(ERROR) << "Failed to write to misc: " << err;
120 return 1;
121 }
122
123 return 0;
124 }
125
main(int argc,char ** argv)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 }
157