1 /*
2 * Copyright (C) 2019 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 <stdint.h>
19 #include <stdlib.h>
20
21 #include <iostream>
22 #include <map>
23 #include <memory>
24 #include <optional>
25 #include <string>
26 #include <string_view>
27
28 #include <android-base/logging.h>
29 #include <android-base/parseint.h>
30
31 #include "misc_writer/misc_writer.h"
32
33 using namespace std::string_literals;
34 using android::hardware::google::pixel::MiscWriter;
35 using android::hardware::google::pixel::MiscWriterActions;
36
Usage(std::string_view name)37 static int Usage(std::string_view name) {
38 std::cerr << name << " usage:\n";
39 std::cerr << name << " [--override-vendor-space-offset <offset>] --<misc_writer_action>\n";
40 std::cerr << "Supported misc_writer_action is one of: \n";
41 std::cerr << " --set-dark-theme Write the dark theme flag\n";
42 std::cerr << " --clear-dark-theme Clear the dark theme flag\n";
43 std::cerr << " --set-sota Write the silent OTA flag\n";
44 std::cerr << " --clear-sota Clear the silent OTA flag\n";
45 std::cerr << " --set-sota-config Set the silent OTA configs\n";
46 std::cerr << " --set-enable-pkvm Write the enable pKVM flag\n";
47 std::cerr << " --set-disable-pkvm Write the disable pKVM flag\n";
48 std::cerr << " --set-wrist-orientation <0-3> Write the wrist orientation flag\n";
49 std::cerr << " --clear-wrist-orientation Clear the wrist orientation flag\n";
50 std::cerr << " --set-timeformat Write the time format value (1=24hr, 0=12hr)\n";
51 std::cerr << " --set-timeoffset Write the time offset value (tz_time - utc_time)\n";
52 std::cerr << " --set-max-ram-size <2048-65536> Write the sw limit max ram size in MB\n";
53 std::cerr << " --set-max-ram-size <-1> Clear the sw limit max ram size\n";
54 std::cerr << " --set-timertcoffset Write the time offset value (utc_time - rtc_time)\n";
55 std::cerr << " --set-minrtc Write the minimum expected rtc value for tilb\n";
56 std::cerr << " --set-dsttransition Write the next dst transition in the current timezone\n";
57 std::cerr << " --set-dstoffset Write the time offset during the next dst transition\n";
58 std::cerr << " --set-display-mode <mode> Write the display mode at boot\n";
59 std::cerr << " --clear-display-mode Clear the display mode at boot\n";
60 std::cerr << "Writes the given hex string to the specified offset in vendor space in /misc "
61 "partition.\nDefault offset is used for each action unless "
62 "--override-vendor-space-offset is specified.\n";
63 return EXIT_FAILURE;
64 }
65
66 // misc_writer is a vendor tool that writes data to the vendor space in /misc.
main(int argc,char ** argv)67 int main(int argc, char** argv) {
68 constexpr struct option OPTIONS[] = {
69 { "set-dark-theme", no_argument, nullptr, 0 },
70 { "clear-dark-theme", no_argument, nullptr, 0 },
71 { "set-sota", no_argument, nullptr, 0 },
72 { "clear-sota", no_argument, nullptr, 0 },
73 { "set-wrist-orientation", required_argument, nullptr, 0 },
74 { "clear-wrist-orientation", no_argument, nullptr, 0 },
75 { "override-vendor-space-offset", required_argument, nullptr, 0 },
76 { "set-enable-pkvm", no_argument, nullptr, 0 },
77 { "set-disable-pkvm", no_argument, nullptr, 0 },
78 { "set-timeformat", required_argument, nullptr, 0},
79 { "set-timeoffset", required_argument, nullptr, 0},
80 { "set-max-ram-size", required_argument, nullptr, 0},
81 { "set-timertcoffset", required_argument, nullptr, 0},
82 { "set-minrtc", required_argument, nullptr, 0},
83 { "set-sota-config", no_argument, nullptr, 0 },
84 { "set-dsttransition", required_argument, nullptr, 0},
85 { "set-dstoffset", required_argument, nullptr, 0 },
86 { "set-display-mode", required_argument, nullptr, 0 },
87 { "clear-display-mode", no_argument, nullptr, 0 },
88 { nullptr, 0, nullptr, 0 },
89 };
90
91 std::map<std::string, MiscWriterActions> action_map{
92 { "set-dark-theme", MiscWriterActions::kSetDarkThemeFlag },
93 { "clear-dark-theme", MiscWriterActions::kClearDarkThemeFlag },
94 { "set-sota", MiscWriterActions::kSetSotaFlag },
95 { "clear-sota", MiscWriterActions::kClearSotaFlag },
96 { "set-enable-pkvm", MiscWriterActions::kSetEnablePkvmFlag },
97 { "set-disable-pkvm", MiscWriterActions::kSetDisablePkvmFlag },
98 { "clear-wrist-orientation", MiscWriterActions::kClearWristOrientationFlag },
99 { "set-sota-config", MiscWriterActions::kSetSotaConfig },
100 { "clear-display-mode", MiscWriterActions::kClearDisplayMode },
101 };
102
103 std::unique_ptr<MiscWriter> misc_writer;
104 std::optional<size_t> override_offset;
105
106 int arg;
107 int option_index = 0;
108 while ((arg = getopt_long(argc, argv, "", OPTIONS, &option_index)) != -1) {
109 if (arg != 0) {
110 LOG(ERROR) << "Invalid command argument";
111 return Usage(argv[0]);
112 }
113 auto option_name = OPTIONS[option_index].name;
114 if (option_name == "override-vendor-space-offset"s) {
115 LOG(WARNING) << "Overriding the vendor space offset in misc partition to " << optarg;
116 size_t offset;
117 if (!android::base::ParseUint(optarg, &offset)) {
118 LOG(ERROR) << "Failed to parse the offset: " << optarg;
119 return Usage(argv[0]);
120 }
121 override_offset = offset;
122 } else if (option_name == "set-wrist-orientation"s) {
123 int orientation;
124 if (!android::base::ParseInt(optarg, &orientation)) {
125 LOG(ERROR) << "Failed to parse the orientation: " << optarg;
126 return Usage(argv[0]);
127 }
128 if (orientation < 0 || orientation > 3) {
129 LOG(ERROR) << "Orientation out of range: " << optarg;
130 return Usage(argv[0]);
131 }
132 if (misc_writer) {
133 LOG(ERROR) << "Misc writer action has already been set";
134 return Usage(argv[0]);
135 }
136 misc_writer = std::make_unique<MiscWriter>(MiscWriterActions::kSetWristOrientationFlag,
137 '0' + orientation);
138 } else if (option_name == "set-timeformat"s) {
139 int timeformat;
140 if (!android::base::ParseInt(optarg, &timeformat)) {
141 LOG(ERROR) << "Failed to parse the timeformat: " << optarg;
142 return Usage(argv[0]);
143 }
144 if (timeformat < 0 || timeformat > 1) {
145 LOG(ERROR) << "Time format out of range: " << optarg;
146 return Usage(argv[0]);
147 }
148 if (misc_writer) {
149 LOG(ERROR) << "Misc writer action has already been set";
150 return Usage(argv[0]);
151 }
152 misc_writer = std::make_unique<MiscWriter>(MiscWriterActions::kWriteTimeFormat,
153 '0' + timeformat);
154 } else if (option_name == "set-timeoffset"s) {
155 int timeoffset;
156 if (!android::base::ParseInt(optarg, &timeoffset)) {
157 LOG(ERROR) << "Failed to parse the timeoffset: " << optarg;
158 return Usage(argv[0]);
159 }
160 if (timeoffset < MiscWriter::kMinTimeOffset || timeoffset > MiscWriter::kMaxTimeOffset) {
161 LOG(ERROR) << "Time offset out of range: " << optarg;
162 return Usage(argv[0]);
163 }
164 if (misc_writer) {
165 LOG(ERROR) << "Misc writer action has already been set";
166 return Usage(argv[0]);
167 }
168 misc_writer = std::make_unique<MiscWriter>(MiscWriterActions::kWriteTimeOffset,
169 std::to_string(timeoffset));
170 } else if (option_name == "set-max-ram-size"s) {
171 int max_ram_size;
172 if (!android::base::ParseInt(optarg, &max_ram_size)) {
173 LOG(ERROR) << "Failed to parse the max_ram_size: " << optarg;
174 return Usage(argv[0]);
175 }
176 if (max_ram_size != MiscWriter::kRamSizeDefault &&
177 (max_ram_size < MiscWriter::kRamSizeMin || max_ram_size > MiscWriter::kRamSizeMax)) {
178 LOG(ERROR) << "max_ram_size out of range: " << optarg;
179 return Usage(argv[0]);
180 }
181 if (misc_writer) {
182 LOG(ERROR) << "Misc writer action has already been set";
183 return Usage(argv[0]);
184 }
185
186 if (max_ram_size == MiscWriter::kRamSizeDefault) {
187 misc_writer = std::make_unique<MiscWriter>(MiscWriterActions::kClearMaxRamSize);
188 } else {
189 misc_writer = std::make_unique<MiscWriter>(MiscWriterActions::kSetMaxRamSize,
190 std::to_string(max_ram_size));
191 }
192 } else if (option_name == "set-timertcoffset"s) {
193 long long int timertcoffset = strtoll(optarg, NULL, 10);
194 if (0 == timertcoffset) {
195 LOG(ERROR) << "Failed to parse the timertcoffset:" << optarg;
196 return Usage(argv[0]);
197 }
198 if (misc_writer) {
199 LOG(ERROR) << "Misc writer action has already been set";
200 return Usage(argv[0]);
201 }
202 misc_writer = std::make_unique<MiscWriter>(MiscWriterActions::kWriteTimeRtcOffset,
203 std::to_string(timertcoffset));
204 } else if (option_name == "set-minrtc"s) {
205 long long int minrtc = strtoll(optarg, NULL, 10);
206 if (0 == minrtc) {
207 LOG(ERROR) << "Failed to parse the minrtc:" << optarg;
208 return Usage(argv[0]);
209 }
210 if (misc_writer) {
211 LOG(ERROR) << "Misc writer action has already been set";
212 return Usage(argv[0]);
213 }
214 misc_writer = std::make_unique<MiscWriter>(MiscWriterActions::kWriteTimeMinRtc,
215 std::to_string(minrtc));
216 } else if (option_name == "set-display-mode"s) {
217 std::string mode(optarg);
218 if (mode.size() > MiscWriter::kDisplayModeMaxSize) {
219 LOG(ERROR) << "Display mode too long:" << optarg;
220 return Usage(argv[0]);
221 }
222 if (misc_writer) {
223 LOG(ERROR) << "Misc writer action has already been set";
224 return Usage(argv[0]);
225 }
226 misc_writer = std::make_unique<MiscWriter>(MiscWriterActions::kSetDisplayMode, mode);
227 } else if (auto iter = action_map.find(option_name); iter != action_map.end()) {
228 if (misc_writer) {
229 LOG(ERROR) << "Misc writer action has already been set";
230 return Usage(argv[0]);
231 }
232 misc_writer = std::make_unique<MiscWriter>(iter->second);
233 } else if (option_name == "set-dsttransition"s) {
234 long long int dst_transition = strtoll(optarg, NULL, 10);
235 if (0 == dst_transition) {
236 LOG(ERROR) << "Failed to parse the dst transition:" << optarg;
237 return Usage(argv[0]);
238 }
239 if (misc_writer) {
240 LOG(ERROR) << "Misc writer action has already been set";
241 return Usage(argv[0]);
242 }
243 misc_writer = std::make_unique<MiscWriter>(MiscWriterActions::kWriteDstTransition,
244 std::to_string(dst_transition));
245 } else if (option_name == "set-dstoffset"s) {
246 int dst_offset;
247 if (!android::base::ParseInt(optarg, &dst_offset)) {
248 LOG(ERROR) << "Failed to parse the dst offset: " << optarg;
249 return Usage(argv[0]);
250 }
251 if (misc_writer) {
252 LOG(ERROR) << "Misc writer action has already been set";
253 return Usage(argv[0]);
254 }
255 misc_writer = std::make_unique<MiscWriter>(MiscWriterActions::kWriteDstOffset,
256 std::to_string(dst_offset));
257 } else {
258 LOG(FATAL) << "Unreachable path, option_name: " << option_name;
259 }
260 }
261
262 if (!misc_writer) {
263 LOG(ERROR) << "An action must be specified for misc writer";
264 return Usage(argv[0]);
265 }
266
267 if (!misc_writer->PerformAction(override_offset)) {
268 return EXIT_FAILURE;
269 }
270
271 return EXIT_SUCCESS;
272 }
273