blob: bea12a6c6fc4fb7d1b34ec2f6dde5070eb854e34 [file] [log] [blame]
Florian Mayer6e129a42022-01-19 14:03:44 -08001/*
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
Florian Mayerf6658f02022-09-21 12:55:33 -070017#include <getopt.h>
Florian Mayere0e618d2022-09-26 19:30:45 -070018#include <unistd.h>
Florian Mayerf6658f02022-09-21 12:55:33 -070019
Florian Mayere0e618d2022-09-26 19:30:45 -070020#include <android-base/file.h>
Florian Mayer6e129a42022-01-19 14:03:44 -080021#include <android-base/logging.h>
Florian Mayerf6658f02022-09-21 12:55:33 -070022#include <android-base/properties.h>
Florian Mayer6e129a42022-01-19 14:03:44 -080023#include <android-base/strings.h>
Florian Mayerc1445862022-10-31 16:04:15 -070024#include <android-base/unique_fd.h>
Florian Mayer6e129a42022-01-19 14:03:44 -080025#include <bootloader_message/bootloader_message.h>
26
Florian Mayere0e618d2022-09-26 19:30:45 -070027#include <functional>
Florian Mayer6e129a42022-01-19 14:03:44 -080028#include <iostream>
29
Florian Mayerf6658f02022-09-21 12:55:33 -070030void AddItem(std::string* s, const char* item) {
Florian Mayere0e618d2022-09-26 19:30:45 -070031 if (!s->empty()) *s += ",";
Florian Mayerf6658f02022-09-21 12:55:33 -070032 *s += item;
33}
34
Florian Mayer234143a2022-10-25 17:53:58 -070035bool CheckAndUnset(uint32_t& mode, uint32_t mask) {
36 bool is_set = mode & mask;
37 mode &= ~mask;
38 return is_set;
39}
40
41bool UpdateProp(const char* prop_name, const misc_memtag_message& m) {
42 uint32_t mode = m.memtag_mode;
Florian Mayerf6658f02022-09-21 12:55:33 -070043 std::string prop_str;
Florian Mayer234143a2022-10-25 17:53:58 -070044 if (CheckAndUnset(mode, MISC_MEMTAG_MODE_MEMTAG)) AddItem(&prop_str, "memtag");
45 if (CheckAndUnset(mode, MISC_MEMTAG_MODE_MEMTAG_ONCE)) AddItem(&prop_str, "memtag-once");
46 if (CheckAndUnset(mode, MISC_MEMTAG_MODE_MEMTAG_KERNEL)) AddItem(&prop_str, "memtag-kernel");
47 if (CheckAndUnset(mode, MISC_MEMTAG_MODE_MEMTAG_KERNEL_ONCE))
48 AddItem(&prop_str, "memtag-kernel-once");
49 if (CheckAndUnset(mode, MISC_MEMTAG_MODE_MEMTAG_OFF)) AddItem(&prop_str, "memtag-off");
Florian Mayerf6658f02022-09-21 12:55:33 -070050 if (android::base::GetProperty(prop_name, "") != prop_str)
51 android::base::SetProperty(prop_name, prop_str);
Florian Mayer234143a2022-10-25 17:53:58 -070052 if (mode) {
53 LOG(ERROR) << "MTE mode in misc message contained unknown bits: " << mode
54 << ". Ignoring and setting " << prop_name << " to " << prop_str;
55 }
56 return mode == 0;
Florian Mayerf6658f02022-09-21 12:55:33 -070057}
58
59void PrintUsage(const char* progname) {
Florian Mayer6325c522022-10-31 15:58:18 -070060 std::cerr
61 << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
62 "!!! YOU PROBABLY DO NOT NEED TO USE THIS !!!\n"
63 "!!! USE THE `arm64.memtag.bootctl` SYSTEM PROPERTY INSTEAD. !!!\n"
64 "!!! This program is an implementation detail that is used !!!\n"
65 "!!! by the system to apply MTE settings. !!!\n"
66 "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
67 "\n"
68 << "USAGE: " << progname
69 << "\n"
70 " [-s PROPERTY_NAME]\n"
71 " [none,][memtag,][memtag-once,][memtag-kernel,][memtag-kernel-once,][memtag-off,]\n"
72 " [default|force_on|force_off]\n"
73 " [-t PATH_TO_FAKE_MISC_PARTITION]\n"
74
75 "OPTIONS:\n"
76 " -s PROPERTY_NAME\n"
77 " Sets the system property 'PROPERTY_NAME' to the new MTE mode (if provided), or to\n"
78 " the current value from the /misc partition.\n"
79 " [none,][memtag,][memtag-once,][memtag-kernel,][memtag-kernel-once,][memtag-off,]\n"
80 " A set of MTE options to be applied, if provided. Multiple options may be\n"
81 " specified as a ','-delimited list, e.g. 'memtag,memtag-kernel'.\n"
82 " The options are described below:\n"
83 " - none: default settings for MTE for the product will be applied on next\n"
84 " reboot.\n"
85 " - memtag: MTE is persistently enabled in userspace upon the next reboot.\n"
86 " - memtag-once: MTE is enabled in userspace, only for the next reboot.\n"
87 " - memtag-kernel: MTE is persistently enabled in the kernel upon the next \n"
88 " reboot.\n"
89 " - memtag-kernel-once: MTE is enabled in the kernel, only for the next reboot.\n"
90 " - memtag-off: MTE is persistently disabled in both userspace and kernel upon \n"
91 " the next reboot.\n"
92 " [default|force_on|force_off]\n"
93 " An alternative method of configuring the MTE options to be applied, if provided.\n"
94 " This control is generally to be used by device_config only, and it overwrites\n"
95 " the previously described settings that are expected to be utilized by the user.\n"
96 " The options are described below:\n"
97 " - default: This flag is not overwriting the MTE mode, and so the setting\n"
98 " should be inherited from the userspace controls (if present), or the\n"
99 " default value from the bootloader's ROM.\n"
100 " - force_on: MTE is persistently enabled in userspace, overwriting the userspace\n"
101 " setting.\n"
102 " - force_off: MTE is persistently disabled in userspace and the kernel, \n"
103 " overwriting the userspace setting.\n";
Florian Mayerf6658f02022-09-21 12:55:33 -0700104}
105
106int StringToMode(const char* value) {
107 int memtag_mode = 0;
Florian Mayer6e129a42022-01-19 14:03:44 -0800108 for (const auto& field : android::base::Split(value, ",")) {
109 if (field == "memtag") {
Florian Mayerf6658f02022-09-21 12:55:33 -0700110 memtag_mode |= MISC_MEMTAG_MODE_MEMTAG;
Florian Mayer6e129a42022-01-19 14:03:44 -0800111 } else if (field == "memtag-once") {
Florian Mayerf6658f02022-09-21 12:55:33 -0700112 memtag_mode |= MISC_MEMTAG_MODE_MEMTAG_ONCE;
Florian Mayer6e129a42022-01-19 14:03:44 -0800113 } else if (field == "memtag-kernel") {
Florian Mayerf6658f02022-09-21 12:55:33 -0700114 memtag_mode |= MISC_MEMTAG_MODE_MEMTAG_KERNEL;
Florian Mayer6e129a42022-01-19 14:03:44 -0800115 } else if (field == "memtag-kernel-once") {
Florian Mayerf6658f02022-09-21 12:55:33 -0700116 memtag_mode |= MISC_MEMTAG_MODE_MEMTAG_KERNEL_ONCE;
Florian Mayer7a119c52022-06-15 00:02:25 +0000117 } else if (field == "memtag-off") {
Florian Mayerf6658f02022-09-21 12:55:33 -0700118 memtag_mode |= MISC_MEMTAG_MODE_MEMTAG_OFF;
Florian Mayer6e129a42022-01-19 14:03:44 -0800119 } else if (field != "none") {
Florian Mayer965e5092022-07-27 14:52:29 -0700120 LOG(ERROR) << "Unknown value for mode: " << field;
Florian Mayerf6658f02022-09-21 12:55:33 -0700121 return -1;
Florian Mayer6e129a42022-01-19 14:03:44 -0800122 }
123 }
Florian Mayerf6658f02022-09-21 12:55:33 -0700124 return memtag_mode;
125}
126
127bool HandleOverride(const std::string& override_value, misc_memtag_message* m) {
Florian Mayer965e5092022-07-27 14:52:29 -0700128 if (override_value == "force_off") {
129 // If the force_off override is active, only allow MEMTAG_MODE_MEMTAG_ONCE.
Florian Mayerf6658f02022-09-21 12:55:33 -0700130 m->memtag_mode |= MISC_MEMTAG_MODE_MEMTAG_OFF;
131 m->memtag_mode &= ~MISC_MEMTAG_MODE_MEMTAG;
Florian Mayer965e5092022-07-27 14:52:29 -0700132 } else if (override_value == "force_on") {
Florian Mayerf6658f02022-09-21 12:55:33 -0700133 m->memtag_mode |= MISC_MEMTAG_MODE_MEMTAG;
134 m->memtag_mode &= ~MISC_MEMTAG_MODE_MEMTAG_OFF;
Florian Mayer965e5092022-07-27 14:52:29 -0700135 } else if (!override_value.empty() && override_value != "default") {
Florian Mayerf6658f02022-09-21 12:55:33 -0700136 return false;
Florian Mayer965e5092022-07-27 14:52:29 -0700137 }
Florian Mayerf6658f02022-09-21 12:55:33 -0700138 return true;
139}
140
141int main(int argc, char** argv) {
142 const char* set_prop = nullptr;
143 int opt;
Florian Mayere0e618d2022-09-26 19:30:45 -0700144 std::function<bool(misc_memtag_message*, std::string*)> read_memtag_message =
145 ReadMiscMemtagMessage;
146 std::function<bool(const misc_memtag_message&, std::string*)> write_memtag_message =
147 WriteMiscMemtagMessage;
Florian Mayerc1445862022-10-31 16:04:15 -0700148
149 android::base::unique_fd fake_partition_fd;
Florian Mayere0e618d2022-09-26 19:30:45 -0700150 while ((opt = getopt(argc, argv, "s:t:")) != -1) {
Florian Mayerf6658f02022-09-21 12:55:33 -0700151 switch (opt) {
152 case 's':
Florian Mayer6325c522022-10-31 15:58:18 -0700153 // Set property in argument to state of misc partition. If given by
154 // itself, sets the property to the current state. We do this on device
155 // boot,
156 //
157 // Otherwise, applies new state and then sets property to newly applied
158 // state.
Florian Mayerf6658f02022-09-21 12:55:33 -0700159 set_prop = optarg;
160 break;
Florian Mayere0e618d2022-09-26 19:30:45 -0700161 case 't': {
162 // Use different fake misc partition for testing.
163 const char* filename = optarg;
Florian Mayerc1445862022-10-31 16:04:15 -0700164 fake_partition_fd.reset(open(filename, O_RDWR | O_CLOEXEC));
165 int raw_fd = fake_partition_fd.get();
166 CHECK_NE(raw_fd, -1);
167 CHECK_NE(ftruncate(raw_fd, sizeof(misc_memtag_message)), -1);
168 read_memtag_message = [raw_fd](misc_memtag_message* m, std::string*) {
169 CHECK(android::base::ReadFully(raw_fd, m, sizeof(*m)));
Florian Mayere0e618d2022-09-26 19:30:45 -0700170 return true;
171 };
Florian Mayerc1445862022-10-31 16:04:15 -0700172 write_memtag_message = [raw_fd](const misc_memtag_message& m, std::string*) {
173 CHECK(android::base::WriteFully(raw_fd, &m, sizeof(m)));
Florian Mayere0e618d2022-09-26 19:30:45 -0700174 return true;
175 };
176 break;
177 }
Florian Mayerf6658f02022-09-21 12:55:33 -0700178 default:
179 PrintUsage(argv[0]);
180 return 1;
181 }
182 }
183
184 const char* value = optind < argc ? argv[optind++] : nullptr;
185 const char* override_value = optind < argc ? argv[optind++] : nullptr;
186
187 if (optind != argc) { // Unknown argument.
188 PrintUsage(argv[0]);
189 return 1;
190 }
191
192 if (!value && set_prop) {
Florian Mayer6325c522022-10-31 15:58:18 -0700193 // -s <property> is given on its own. This means we want to read the state
194 // of the misc partition into the property.
Florian Mayerf6658f02022-09-21 12:55:33 -0700195 std::string err;
196 misc_memtag_message m = {};
Florian Mayere0e618d2022-09-26 19:30:45 -0700197 if (!read_memtag_message(&m, &err)) {
Florian Mayerf6658f02022-09-21 12:55:33 -0700198 LOG(ERROR) << "Failed to read memtag message: " << err;
199 return 1;
200 }
201 if (m.magic != MISC_MEMTAG_MAGIC_HEADER || m.version != MISC_MEMTAG_MESSAGE_VERSION) {
Florian Mayer234143a2022-10-25 17:53:58 -0700202 // This should not fail by construction.
203 CHECK(UpdateProp(set_prop, {}));
204 // This is an expected case, as the partition gets initialized to all zero.
Florian Mayerf6658f02022-09-21 12:55:33 -0700205 return 0;
206 }
Florian Mayer6325c522022-10-31 15:58:18 -0700207 // Unlike above, setting the system property here can fail if the misc partition
208 // was corrupted by another program (e.g. the bootloader).
Florian Mayer234143a2022-10-25 17:53:58 -0700209 return UpdateProp(set_prop, m) ? 0 : 1;
Florian Mayerf6658f02022-09-21 12:55:33 -0700210 }
211
212 if (!value) {
213 PrintUsage(argv[0]);
214 return 1;
215 }
216
217 misc_memtag_message m = {.version = MISC_MEMTAG_MESSAGE_VERSION,
218 .magic = MISC_MEMTAG_MAGIC_HEADER};
219 int memtag_mode = StringToMode(value);
220 bool valid_value = memtag_mode != -1;
221 m.memtag_mode = valid_value ? memtag_mode : 0;
222
223 bool valid_override = true;
224 if (override_value) valid_override = HandleOverride(override_value, &m);
225
Florian Mayer965e5092022-07-27 14:52:29 -0700226 if (!valid_value && !valid_override) {
227 return 1;
228 }
Florian Mayer6e129a42022-01-19 14:03:44 -0800229 std::string err;
Florian Mayere0e618d2022-09-26 19:30:45 -0700230 if (!write_memtag_message(m, &err)) {
Florian Mayer965e5092022-07-27 14:52:29 -0700231 LOG(ERROR) << "Failed to apply mode: " << value << ", override: " << override_value << err;
Florian Mayer6e129a42022-01-19 14:03:44 -0800232 return 1;
233 } else {
Florian Mayer965e5092022-07-27 14:52:29 -0700234 const char* parse_error = "";
235 const char* verb = "Applied";
236 if (!valid_value) {
237 parse_error = " (invalid mode)";
238 verb = "Partially applied";
239 } else if (!valid_override) {
240 // else if because we bail out if both are false above.
241 parse_error = " (invalid override)";
242 verb = "Partially applied";
243 }
244 LOG(INFO) << verb << " mode: " << value << ", "
Florian Mayerf6658f02022-09-21 12:55:33 -0700245 << "override: " << (override_value ? override_value : "") << parse_error;
Florian Mayer234143a2022-10-25 17:53:58 -0700246 // Because all the bits in memtag_mode were set above, this should never fail.
247 if (set_prop) CHECK(UpdateProp(set_prop, m));
Florian Mayer965e5092022-07-27 14:52:29 -0700248 return !valid_value || !valid_override;
Florian Mayer6e129a42022-01-19 14:03:44 -0800249 }
250}